Vuo 2.4.4
Loading...
Searching...
No Matches
VuoCurve.c
Go to the documentation of this file.
1
10#include <string.h>
11#include "VuoCurve.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
31
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
58json_object * VuoCurve_getJson(const VuoCurve value)
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
92char * 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
111VuoReal VuoReal_curve(VuoReal time, VuoReal startPosition, VuoReal endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
112{
113 if (duration == 0)
114 return startPosition;
115
116 VuoReal normalizedTime = MIN(MAX(time/duration,0.),1.);
117 if (loop == VuoLoopType_Loop)
118 {
119 normalizedTime = fmod(time/duration, 1);
120 if (time < 0)
121 normalizedTime = 1. + normalizedTime;
122 }
123 else if (loop == VuoLoopType_Mirror)
124 {
125 normalizedTime = fmod(time/duration, 2);
126 if (time < 0)
127 normalizedTime = 2. + normalizedTime;
128 if (normalizedTime > 1)
129 normalizedTime = 2. - normalizedTime;
130 }
131
132 VuoReal x = normalizedTime;
133 if (easing == VuoCurveEasing_Out)
134 x = normalizedTime - 1.;
135 else if (easing == VuoCurveEasing_InOut)
136 {
137 if (normalizedTime < 0.5)
138 x = normalizedTime*2.;
139 else
140 x = (normalizedTime-1.)*2.;
141 }
142 else if (easing == VuoCurveEasing_Middle)
143 {
144 if (normalizedTime < 0.5)
145 x = (normalizedTime-0.5)*2.;
146 else
147 x = (normalizedTime-0.5)*2.;
148 }
149
150 VuoReal normalizedValue = x;
151 if (curve == VuoCurve_Linear)
152 normalizedValue = fabs(x);
153 else if (curve == VuoCurve_Quadratic)
154 normalizedValue = pow(x,2);
155 else if (curve == VuoCurve_Cubic)
156 normalizedValue = pow(fabs(x),3);
157 else if (curve == VuoCurve_Circular)
158 normalizedValue = -(sqrt(1.-pow(x,2))-1.);
159 else if (curve == VuoCurve_Exponential)
160 normalizedValue = pow(2,10.*(fabs(x)-1.));
161
162 if (easing == VuoCurveEasing_Out)
163 normalizedValue = 1. - normalizedValue;
164 else if (easing == VuoCurveEasing_InOut)
165 {
166 if (normalizedTime < 0.5)
167 normalizedValue /= 2.;
168 else
169 normalizedValue = 1 - normalizedValue/2.;
170 }
171 else if (easing == VuoCurveEasing_Middle)
172 {
173 if (normalizedTime < 0.5)
174 normalizedValue = (1.-normalizedValue)/2.;
175 else
176 normalizedValue = (normalizedValue+1.)/2.;
177 }
178
179 return normalizedValue*(endPosition-startPosition) + startPosition;
180}
181
185VuoPoint2d VuoPoint2d_curve(VuoReal time, VuoPoint2d startPosition, VuoPoint2d endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
186{
187 VuoPoint2d p;
188 p.x = VuoReal_curve(time, startPosition.x, endPosition.x, duration, curve, easing, loop);
189 p.y = VuoReal_curve(time, startPosition.y, endPosition.y, duration, curve, easing, loop);
190 return p;
191}
192
196VuoPoint3d VuoPoint3d_curve(VuoReal time, VuoPoint3d startPosition, VuoPoint3d endPosition, VuoReal duration, VuoCurve curve, VuoCurveEasing easing, VuoLoopType loop)
197{
198 VuoPoint3d p;
199 p.x = VuoReal_curve(time, startPosition.x, endPosition.x, duration, curve, easing, loop);
200 p.y = VuoReal_curve(time, startPosition.y, endPosition.y, duration, curve, easing, loop);
201 p.z = VuoReal_curve(time, startPosition.z, endPosition.z, duration, curve, easing, loop);
202 return p;
203}