Vuo  2.3.2
VuoPoint4d.c
Go to the documentation of this file.
1 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "type.h"
14 
16 #ifdef VUO_COMPILER
18  "title": "4D Point",
19  "description": "A floating-point 4-dimensional Cartesian spatial location.",
20  "keywords": [ "coordinate" ],
21  "version": "1.0.0",
22  "dependencies": [
23  "VuoList_VuoPoint4d",
24  "VuoReal",
25  "VuoText",
26  ],
27 });
28 #endif
30 
45 {
46  VuoPoint4d point = {0,0,0,0};
47 
48  json_type t = json_object_get_type(js);
49  if (t == json_type_string)
50  {
51  const char *s = json_object_get_string(js);
52  float x, y, z, w;
53  sscanf(s, "%20g, %20g, %20g, %20g", &x, &y, &z, &w);
54  return (VuoPoint4d){x, y, z, w};
55  }
56  else if (t == json_type_array)
57  {
58  int len = json_object_array_length(js);
59  if (len >= 1)
60  point.x = json_object_get_double(json_object_array_get_idx(js, 0));
61  if (len >= 2)
62  point.y = json_object_get_double(json_object_array_get_idx(js, 1));
63  if (len >= 3)
64  point.z = json_object_get_double(json_object_array_get_idx(js, 2));
65  if (len >= 4)
66  point.w = json_object_get_double(json_object_array_get_idx(js, 3));
67  return point;
68  }
69 
70  json_object *o = NULL;
71 
72  if (json_object_object_get_ex(js, "x", &o))
73  point.x = VuoReal_makeFromJson(o);
74  else if (json_object_object_get_ex(js, "X", &o))
75  point.x = VuoReal_makeFromJson(o);
76 
77  if (json_object_object_get_ex(js, "y", &o))
78  point.y = VuoReal_makeFromJson(o);
79  else if (json_object_object_get_ex(js, "Y", &o))
80  point.y = VuoReal_makeFromJson(o);
81 
82  if (json_object_object_get_ex(js, "z", &o))
83  point.z = VuoReal_makeFromJson(o);
84  else if (json_object_object_get_ex(js, "Z", &o))
85  point.z = VuoReal_makeFromJson(o);
86 
87  if (json_object_object_get_ex(js, "w", &o))
88  point.w = VuoReal_makeFromJson(o);
89  else if (json_object_object_get_ex(js, "W", &o))
90  point.w = VuoReal_makeFromJson(o);
91 
92  return point;
93 }
94 
99 json_object * VuoPoint4d_getJson(const VuoPoint4d value)
100 {
101  json_object *js = json_object_new_object();
102 
103  json_object *xObject = VuoReal_getJson(value.x);
104  json_object_object_add(js, "x", xObject);
105 
106  json_object *yObject = VuoReal_getJson(value.y);
107  json_object_object_add(js, "y", yObject);
108 
109  json_object *zObject = VuoReal_getJson(value.z);
110  json_object_object_add(js, "z", zObject);
111 
112  json_object *wObject = VuoReal_getJson(value.w);
113  json_object_object_add(js, "w", wObject);
114 
115  return js;
116 }
117 
122 char * VuoPoint4d_getSummary(const VuoPoint4d value)
123 {
124  return VuoText_format("%g, %g, %g, %g", value.x, value.y, value.z, value.w);
125 }
126 
130 bool VuoPoint4d_areEqual(const VuoPoint4d value1, const VuoPoint4d value2)
131 {
132  return VuoReal_areEqual(value1.x, value2.x)
133  && VuoReal_areEqual(value1.y, value2.y)
134  && VuoReal_areEqual(value1.z, value2.z)
135  && VuoReal_areEqual(value1.w, value2.w);
136 }
137 
142 {
143  unsigned long count = VuoListGetCount_VuoPoint4d(values);
144  if (count <= 1)
145  return true;
146 
147  VuoPoint4d *points = VuoListGetData_VuoPoint4d(values);
148  VuoPoint4d min, max;
149  min = max = points[0];
150  for (unsigned long i = 1; i < count; ++i)
151  {
152  min = VuoPoint4d_min(min, points[i]);
153  max = VuoPoint4d_max(max, points[i]);
154  }
155  VuoPoint4d diff = max - min;
156  return diff.x <= tolerance.x
157  && diff.y <= tolerance.y
158  && diff.z <= tolerance.z
159  && diff.w <= tolerance.w;
160 }
161 
169 bool VuoPoint4d_isLessThan(const VuoPoint4d a, const VuoPoint4d b)
170 {
175  return false;
176 }
177 
181 bool VuoPoint4d_isWithinRange(VuoPoint4d value, VuoPoint4d minimum, VuoPoint4d maximum)
182 {
183  return minimum.x <= value.x && value.x <= maximum.x
184  && minimum.y <= value.y && value.y <= maximum.y
185  && minimum.z <= value.z && value.z <= maximum.z
186  && minimum.w <= value.w && value.w <= maximum.w;
187 }
188 
192 VuoPoint4d VuoPoint4d_minList(VuoList_VuoPoint4d values, VuoInteger *outputPosition)
193 {
194  unsigned long count = VuoListGetCount_VuoPoint4d(values);
195  if (count == 0)
196  {
197  *outputPosition = 0;
198  return 0;
199  }
200 
201  VuoPoint4d *points = VuoListGetData_VuoPoint4d(values);
202  VuoPoint4d min = FLT_MAX;
203  for (unsigned long i = 0; i < count; ++i)
204  if (VuoPoint4d_isLessThan(points[i], min))
205  {
206  min = points[i];
207  *outputPosition = i + 1;
208  }
209 
210  return min;
211 }
212 
216 VuoPoint4d VuoPoint4d_maxList(VuoList_VuoPoint4d values, VuoInteger *outputPosition)
217 {
218  unsigned long count = VuoListGetCount_VuoPoint4d(values);
219  if (count == 0)
220  {
221  *outputPosition = 0;
222  return 0;
223  }
224 
225  VuoPoint4d *points = VuoListGetData_VuoPoint4d(values);
226  VuoPoint4d max = -FLT_MAX;
227  for (unsigned long i = 0; i < count; ++i)
228  if (VuoPoint4d_isLessThan(max, points[i]))
229  {
230  max = points[i];
231  *outputPosition = i + 1;
232  }
233 
234  return max;
235 }
236 
241 {
242  VuoInteger count = VuoListGetCount_VuoPoint4d(values);
243  if (count == 0)
244  return 0;
245 
246  VuoPoint4d sum = 0;
247  for (VuoInteger i = 1; i <= count; ++i)
248  sum += VuoListGetValue_VuoPoint4d(values, i);
249 
250  VuoPoint4d divisor = count;
251  return sum / divisor;
252 }
253 
259 VuoPoint4d VuoPoint4d_random(const VuoPoint4d minimum, const VuoPoint4d maximum)
260 {
261  return VuoPoint4d_make(
262  VuoReal_random(minimum.x, maximum.x),
263  VuoReal_random(minimum.y, maximum.y),
264  VuoReal_random(minimum.z, maximum.z),
265  VuoReal_random(minimum.w, maximum.w));
266 }
267 
273 VuoPoint4d VuoPoint4d_randomWithState(unsigned short state[3], const VuoPoint4d minimum, const VuoPoint4d maximum)
274 {
275  return VuoPoint4d_make(
276  VuoReal_randomWithState(state, minimum.x, maximum.x),
277  VuoReal_randomWithState(state, minimum.y, maximum.y),
278  VuoReal_randomWithState(state, minimum.z, maximum.z),
279  VuoReal_randomWithState(state, minimum.w, maximum.w));
280 }