Vuo  2.0.2
VuoCurve.c
Go to the documentation of this file.
1 
10 #include <string.h>
11 #include "type.h"
12 
14 #ifdef VUO_COMPILER
16  "title" : "Curve",
17  "description" : "Type of curve.",
18  "keywords" : [ ],
19  "version" : "1.0.0",
20  "dependencies" : [
21  "VuoList_VuoCurve",
22  "VuoCurveEasing",
23  "VuoLoopType",
24  "VuoPoint2d",
25  "VuoPoint3d",
26  "VuoReal"
27  ]
28  });
29 #endif
30 
37 {
38  const char *valueAsString = "";
39  if (json_object_get_type(js) == json_type_string)
40  valueAsString = json_object_get_string(js);
41 
42  if (strcmp(valueAsString, "quadratic") == 0)
43  return VuoCurve_Quadratic;
44  else if (strcmp(valueAsString, "cubic") == 0)
45  return VuoCurve_Cubic;
46  else if (strcmp(valueAsString, "circular") == 0)
47  return VuoCurve_Circular;
48  else if (strcmp(valueAsString, "exponential") == 0)
49  return VuoCurve_Exponential;
50 
51  return VuoCurve_Linear;
52 }
53 
59 {
60  char *valueAsString = "linear";
61 
62  if (value == VuoCurve_Quadratic)
63  valueAsString = "quadratic";
64  else if (value == VuoCurve_Cubic)
65  valueAsString = "cubic";
66  else if (value == VuoCurve_Circular)
67  valueAsString = "circular";
68  else if (value == VuoCurve_Exponential)
69  valueAsString = "exponential";
70 
71  return json_object_new_string(valueAsString);
72 }
73 
78 {
80  VuoListAppendValue_VuoCurve(l, VuoCurve_Linear);
81  VuoListAppendValue_VuoCurve(l, VuoCurve_Quadratic);
82  VuoListAppendValue_VuoCurve(l, VuoCurve_Cubic);
83  VuoListAppendValue_VuoCurve(l, VuoCurve_Circular);
84  VuoListAppendValue_VuoCurve(l, VuoCurve_Exponential);
85  return l;
86 }
87 
92 char * VuoCurve_getSummary(const VuoCurve value)
93 {
94  char *valueAsString = "Linear";
95 
96  if (value == VuoCurve_Quadratic)
97  valueAsString = "Quadratic";
98  else if (value == VuoCurve_Cubic)
99  valueAsString = "Cubic";
100  else if (value == VuoCurve_Circular)
101  valueAsString = "Circular";
102  else if (value == VuoCurve_Exponential)
103  valueAsString = "Exponential";
104 
105  return strdup(valueAsString);
106 }
107 
111 VuoReal VuoReal_curve(VuoReal time, VuoReal startPosition, VuoReal endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
112 {
113  VuoReal normalizedTime = MIN(MAX(time/duration,0.),1.);
114  if (loop == VuoLoopType_Loop)
115  {
116  normalizedTime = fmod(time/duration, 1);
117  if (time < 0)
118  normalizedTime = 1. + normalizedTime;
119  }
120  else if (loop == VuoLoopType_Mirror)
121  {
122  normalizedTime = fmod(time/duration, 2);
123  if (time < 0)
124  normalizedTime = 2. + normalizedTime;
125  if (normalizedTime > 1)
126  normalizedTime = 2. - normalizedTime;
127  }
128 
129  VuoReal x = normalizedTime;
130  if (easing == VuoCurveEasing_Out)
131  x = normalizedTime - 1.;
132  else if (easing == VuoCurveEasing_InOut)
133  {
134  if (normalizedTime < 0.5)
135  x = normalizedTime*2.;
136  else
137  x = (normalizedTime-1.)*2.;
138  }
139  else if (easing == VuoCurveEasing_Middle)
140  {
141  if (normalizedTime < 0.5)
142  x = (normalizedTime-0.5)*2.;
143  else
144  x = (normalizedTime-0.5)*2.;
145  }
146 
147  VuoReal normalizedValue = x;
148  if (curve == VuoCurve_Linear)
149  normalizedValue = fabs(x);
150  else if (curve == VuoCurve_Quadratic)
151  normalizedValue = pow(x,2);
152  else if (curve == VuoCurve_Cubic)
153  normalizedValue = pow(fabs(x),3);
154  else if (curve == VuoCurve_Circular)
155  normalizedValue = -(sqrt(1.-pow(x,2))-1.);
156  else if (curve == VuoCurve_Exponential)
157  normalizedValue = pow(2,10.*(fabs(x)-1.));
158 
159  if (easing == VuoCurveEasing_Out)
160  normalizedValue = 1. - normalizedValue;
161  else if (easing == VuoCurveEasing_InOut)
162  {
163  if (normalizedTime < 0.5)
164  normalizedValue /= 2.;
165  else
166  normalizedValue = 1 - normalizedValue/2.;
167  }
168  else if (easing == VuoCurveEasing_Middle)
169  {
170  if (normalizedTime < 0.5)
171  normalizedValue = (1.-normalizedValue)/2.;
172  else
173  normalizedValue = (normalizedValue+1.)/2.;
174  }
175 
176  return normalizedValue*(endPosition-startPosition) + startPosition;
177 }
178 
182 VuoPoint2d VuoPoint2d_curve(VuoReal time, VuoPoint2d startPosition, VuoPoint2d endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
183 {
184  VuoPoint2d p;
185  p.x = VuoReal_curve(time, startPosition.x, endPosition.x, duration, curve, easing, loop);
186  p.y = VuoReal_curve(time, startPosition.y, endPosition.y, duration, curve, easing, loop);
187  return p;
188 }
189 
193 VuoPoint3d VuoPoint3d_curve(VuoReal time, VuoPoint3d startPosition, VuoPoint3d endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
194 {
195  VuoPoint3d p;
196  p.x = VuoReal_curve(time, startPosition.x, endPosition.x, duration, curve, easing, loop);
197  p.y = VuoReal_curve(time, startPosition.y, endPosition.y, duration, curve, easing, loop);
198  p.z = VuoReal_curve(time, startPosition.z, endPosition.z, duration, curve, easing, loop);
199  return p;
200 }