Vuo  2.0.0
VuoPoint3d.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "VuoReal.h"
13 #include "VuoBoolean.h"
14 #include <math.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
31 typedef float __attribute__((ext_vector_type(3))) VuoPoint3d;
32 
36 typedef struct
37 {
38  VuoPoint3d center;
39  VuoPoint3d size;
40 } VuoBox;
41 
42 VuoPoint3d VuoPoint3d_makeFromJson(struct json_object * js);
43 struct json_object * VuoPoint3d_getJson(const VuoPoint3d value);
44 char * VuoPoint3d_getSummary(const VuoPoint3d value);
45 
46 #define VuoPoint3d_SUPPORTS_COMPARISON
47 bool VuoPoint3d_areEqual(const VuoPoint3d value1, const VuoPoint3d value2);
48 bool VuoPoint3d_isLessThan(const VuoPoint3d a, const VuoPoint3d b);
49 
50 VuoPoint3d VuoPoint3d_random(const VuoPoint3d minimum, const VuoPoint3d maximum);
51 VuoPoint3d VuoPoint3d_randomWithState(unsigned short state[3], const VuoPoint3d minimum, const VuoPoint3d maximum);
52 
54 
57 VuoPoint3d VuoPoint3d_makeFromString(const char *str);
58 char * VuoPoint3d_getString(const VuoPoint3d value);
59 void VuoPoint3d_retain(VuoPoint3d value);
60 void VuoPoint3d_release(VuoPoint3d value);
62 
66 static inline VuoPoint3d VuoPoint3d_make(float x, float y, float z) __attribute__((const));
67 static inline VuoPoint3d VuoPoint3d_make(float x, float y, float z)
68 {
69  return (VuoPoint3d){x, y, z};
70 }
71 
77 static inline VuoPoint3d VuoPoint3d_makeFromArray(float *f)
78 {
79  return (VuoPoint3d){ f[0], f[1], f[2] };
80 }
81 
87 static inline void VuoPoint3d_setArray(float *f, VuoPoint3d p)
88 {
89  f[0] = p.x;
90  f[1] = p.y;
91  f[2] = p.z;
92 }
93 
97 static inline VuoBox VuoBox_make(VuoPoint3d center, VuoPoint3d size) __attribute__((const));
98 static inline VuoBox VuoBox_make(VuoPoint3d center, VuoPoint3d size)
99 {
100  return (VuoBox){ center, size };
101 }
102 
106 static inline VuoBox VuoBox_makeWithPoints(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) __attribute__((const));
107 static inline VuoBox VuoBox_makeWithPoints(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
108 {
109  return (VuoBox){
110  (VuoPoint3d){ (xmin+xmax)/2.f, (ymin+ymax)/2.f, (zmin+zmax)/2.f },
111  (VuoPoint3d){ xmax-xmin, ymax-ymin, zmax-zmin }
112  };
113 }
114 
118 static inline VuoPoint3d VuoPoint3d_crossProduct(VuoPoint3d u, VuoPoint3d v) __attribute__((const));
119 static inline VuoPoint3d VuoPoint3d_crossProduct(VuoPoint3d u, VuoPoint3d v)
120 {
121  return (VuoPoint3d){
122  u.y*v.z - u.z*v.y,
123  u.z*v.x - u.x*v.z,
124  u.x*v.y - u.y*v.x
125  };
126 }
127 
131 static inline float VuoPoint3d_dotProduct(VuoPoint3d u, VuoPoint3d v) __attribute__((const));
132 static inline float VuoPoint3d_dotProduct(VuoPoint3d u, VuoPoint3d v)
133 {
134  return u.x*v.x+u.y*v.y+u.z*v.z;
135 }
136 
140 static inline float VuoPoint3d_magnitude(VuoPoint3d a) __attribute__((const));
141 static inline float VuoPoint3d_magnitude(VuoPoint3d a)
142 {
143  return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z);
144 }
145 
149 static inline VuoPoint3d VuoPoint3d_normalize(VuoPoint3d a) __attribute__((const));
150 static inline VuoPoint3d VuoPoint3d_normalize(VuoPoint3d a)
151 {
152  return a / sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
153 }
154 
158 static inline VuoPoint3d VuoPoint3d_add(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
159 static inline VuoPoint3d VuoPoint3d_add(VuoPoint3d a, VuoPoint3d b)
160 {
161  return a + b;
162 }
163 
167 static inline VuoPoint3d VuoPoint3d_subtract(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
168 static inline VuoPoint3d VuoPoint3d_subtract(VuoPoint3d a, VuoPoint3d b)
169 {
170  return a - b;
171 }
172 
173 
177 static inline float VuoPoint3d_squaredMagnitude(VuoPoint3d a) __attribute__((const));
178 static inline float VuoPoint3d_squaredMagnitude(VuoPoint3d a)
179 {
180  return (a.x*a.x + a.y*a.y + a.z*a.z);
181 }
182 
186 static inline VuoPoint3d VuoPoint3d_divide(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
187 static inline VuoPoint3d VuoPoint3d_divide(VuoPoint3d a, VuoPoint3d b)
188 {
189  return a / b;
190 }
191 
195 static inline VuoPoint3d VuoPoint3d_multiply(VuoPoint3d a, float b) __attribute__((const));
196 static inline VuoPoint3d VuoPoint3d_multiply(VuoPoint3d a, float b)
197 {
198  return a * b;
199 }
200 
204 static inline VuoPoint3d VuoPoint3d_min(const VuoPoint3d l, const VuoPoint3d r) __attribute__((const));
205 static inline VuoPoint3d VuoPoint3d_min(const VuoPoint3d l, const VuoPoint3d r)
206 {
207  return (VuoPoint3d){
208  fminf(l.x, r.x),
209  fminf(l.y, r.y),
210  fminf(l.z, r.z)};
211 }
212 
216 static inline VuoPoint3d VuoPoint3d_max(const VuoPoint3d l, const VuoPoint3d r) __attribute__((const));
217 static inline VuoPoint3d VuoPoint3d_max(const VuoPoint3d l, const VuoPoint3d r)
218 {
219  return (VuoPoint3d){
220  fmaxf(l.x, r.x),
221  fmaxf(l.y, r.y),
222  fmaxf(l.z, r.z)};
223 }
224 
228 static inline VuoPoint3d VuoPoint3d_makeNonzero(VuoPoint3d a) __attribute__((const));
229 static inline VuoPoint3d VuoPoint3d_makeNonzero(VuoPoint3d a)
230 {
231  if (fabs(a.x) < 0.000001)
232  a.x = copysign(0.000001, a.x);
233  if (fabs(a.y) < 0.000001)
234  a.y = copysign(0.000001, a.y);
235  if (fabs(a.z) < 0.000001)
236  a.z = copysign(0.000001, a.z);
237  return a;
238 }
239 
243 static inline float VuoPoint3d_distance(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
244 static inline float VuoPoint3d_distance(VuoPoint3d a, VuoPoint3d b)
245 {
246  return sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z));
247 }
248 
252 static inline VuoPoint3d VuoPoint3d_lerp(VuoPoint3d a, VuoPoint3d b, float t) __attribute__((const));
253 static inline VuoPoint3d VuoPoint3d_lerp(VuoPoint3d a, VuoPoint3d b, float t)
254 {
255  return a * (1 - t) + b * t;
256 }
257 
261 static inline VuoPoint3d VuoPoint3d_scale(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
262 static inline VuoPoint3d VuoPoint3d_scale(VuoPoint3d a, VuoPoint3d b)
263 {
264  return a * b;
265 }
266 
275 static inline VuoPoint3d VuoPoint3d_mod(VuoPoint3d a, VuoPoint3d b) __attribute__((const));
276 static inline VuoPoint3d VuoPoint3d_mod(VuoPoint3d a, VuoPoint3d b)
277 {
278  return (VuoPoint3d){
279  a.x - b.x * floorf(a.x / b.x),
280  a.y - b.y * floorf(a.y / b.y),
281  a.z - b.z * floorf(a.z / b.z)
282  };
283 }
284 
288 static inline VuoPoint3d VuoPoint3d_spring(VuoReal timeSinceDrop, VuoPoint3d dropPosition, VuoPoint3d restingPosition, VuoReal period, VuoReal damping)
289 {
290  VuoPoint3d p;
291  p.x = VuoReal_spring(timeSinceDrop, dropPosition.x, restingPosition.x, period, damping);
292  p.y = VuoReal_spring(timeSinceDrop, dropPosition.y, restingPosition.y, period, damping);
293  p.z = VuoReal_spring(timeSinceDrop, dropPosition.z, restingPosition.z, period, damping);
294  return p;
295 }
296 
300 static inline VuoPoint3d VuoPoint3d_clamp(VuoPoint3d point, VuoReal limitA, VuoReal limitB)
301 {
302  return (VuoPoint3d){
303  (float)VuoReal_clamp(point.x, limitA, limitB),
304  (float)VuoReal_clamp(point.y, limitA, limitB),
305  (float)VuoReal_clamp(point.z, limitA, limitB)
306  };
307 }
308 
312 static inline VuoPoint3d VuoPoint3d_clampn(VuoPoint3d point, VuoPoint3d limitA, VuoPoint3d limitB)
313 {
314  return (VuoPoint3d){
315  (float)VuoReal_clamp(point.x, limitA.x, limitB.x),
316  (float)VuoReal_clamp(point.y, limitA.y, limitB.y),
317  (float)VuoReal_clamp(point.z, limitA.z, limitB.z)
318  };
319 }
320 
330 static inline VuoPoint3d VuoPoint3d_bezier3(VuoPoint3d p0, VuoPoint3d p1, VuoPoint3d p2, VuoPoint3d p3, VuoReal time)
331 {
332  return (VuoPoint3d) {
333  (float)VuoReal_bezier3(p0.x,p1.x,p2.x,p3.x,time),
334  (float)VuoReal_bezier3(p0.y,p1.y,p2.y,p3.y,time),
335  (float)VuoReal_bezier3(p0.z,p1.z,p2.z,p3.z,time)
336  };
337 }
338 
342 static inline VuoPoint3d VuoPoint3d_snap(VuoPoint3d a, VuoPoint3d center, VuoPoint3d snap)
343 {
344  VuoPoint3d nonzeroSnap = VuoPoint3d_makeNonzero(snap);
345  return (VuoPoint3d) {
346  center.x + nonzeroSnap.x * (int)round( (a.x-center.x) / nonzeroSnap.x ),
347  center.y + nonzeroSnap.y * (int)round( (a.y-center.y) / nonzeroSnap.y ),
348  center.z + nonzeroSnap.z * (int)round( (a.z-center.z) / nonzeroSnap.z )
349  };
350 }
351 
356 {
357  float xmin, ymin, zmin, xmax, ymax, zmax;
358 
359  VuoPoint3d extents_a = VuoPoint3d_multiply(a.size, .5);
360  VuoPoint3d extents_b = VuoPoint3d_multiply(b.size, .5);
361 
362  xmin = fmin(a.center.x - extents_a.x, b.center.x - extents_b.x);
363  xmax = fmax(a.center.x + extents_a.x, b.center.x + extents_b.x);
364 
365  ymin = fmin(a.center.y - extents_a.y, b.center.y - extents_b.y);
366  ymax = fmax(a.center.y + extents_a.y, b.center.y + extents_b.y);
367 
368  zmin = fmin(a.center.z - extents_a.z, b.center.z - extents_b.z);
369  zmax = fmax(a.center.z + extents_a.z, b.center.z + extents_b.z);
370 
371  return VuoBox_makeWithPoints( xmin, xmax, ymin, ymax, zmin, zmax );
372 }
373 
377 static inline VuoBoolean VuoBox_contains(VuoBox aabb, VuoPoint3d point)
378 {
379  VuoPoint3d exents = (VuoPoint3d){aabb.size.x / 2.f, aabb.size.y / 2.f};
380  VuoPoint3d min = aabb.center - exents;
381  VuoPoint3d max = aabb.center + exents;
382 
383  return point.x > min.x && point.x < max.x &&
384  point.y > min.y && point.y < max.y &&
385  point.z > min.z && point.z < max.z;
386 }
387 
392 {
393  VuoPoint3d dist = VuoPoint3d_subtract(a.center, b.center);
394  VuoPoint3d size = VuoPoint3d_add(a.size, b.size);
395 
396  return fabs(dist.x) * 2 < size.x &&
397  fabs(dist.y) * 2 < size.y &&
398  fabs(dist.z) * 2 < size.z;
399 }
400 
405 #ifdef __cplusplus
406 }
407 #endif