aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/flow-bezier-spline.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/ui/flow-bezier-spline.js')
-rwxr-xr-xnode_modules/montage/ui/flow-bezier-spline.js405
1 files changed, 405 insertions, 0 deletions
diff --git a/node_modules/montage/ui/flow-bezier-spline.js b/node_modules/montage/ui/flow-bezier-spline.js
new file mode 100755
index 00000000..5f2a9e76
--- /dev/null
+++ b/node_modules/montage/ui/flow-bezier-spline.js
@@ -0,0 +1,405 @@
1var Montage = require("montage").Montage,
2 FlowBezierSpline = exports.FlowBezierSpline = Montage.create(Montage, {
3
4 knots: {
5 get: function () {
6 if (!this._knots) {
7 this._knots = [];
8 }
9 return this._knots;
10 },
11 set: function (value) {
12 this._knots = value;
13 }
14 },
15
16 previousHandlers: {
17 get: function () {
18 if (!this._previousHandlers) {
19 this._previousHandlers = [];
20 }
21 return this._previousHandlers;
22 },
23 set: function (value) {
24 this._previousHandlers = value;
25 }
26 },
27
28 nextHandlers: {
29 get: function () {
30 if (!this._nextHandlers) {
31 this._nextHandlers = [];
32 }
33 return this._nextHandlers;
34 },
35 set: function (value) {
36 this._nextHandlers = value;
37 }
38 },
39
40 densities: {
41 get: function () {
42 if (!this._densities) {
43 this._densities = [];
44 }
45 return this._densities;
46 },
47 set: function (value) {
48 this._densities = value;
49 this._densitiesLength = this._densities.length;
50 this._densitySummation = null;
51 }
52 },
53
54 _parameters: {
55 value: {
56 rotateX: {
57 data: [0],
58 units: "rad"
59 },
60 rotateY: {
61 data: [0],
62 units: "rad"
63 },
64 rotateZ: {
65 data: [0],
66 units: "rad"
67 },
68 opacity: {
69 data: [1],
70 units: ""
71 }
72 }
73 },
74
75 parameters: {
76 get: function () {
77 if (!this._parameters) {
78 this._parameters = [];
79 }
80 return this._parameters;
81 },
82 set: function (value) {
83 this._parameters = value;
84 this._parametersLength = this._parameters.length;
85 }
86 },
87
88 knotsLength: {
89 get: function () {
90 if (!this._knots) {
91 return 0;
92 }
93 return this._knots.length;
94 },
95 set: function () {}
96 },
97
98 getKnot: {
99 value: function (index) {
100 return this._knots[index];
101 }
102 },
103
104 getPreviousHandler: {
105 value: function (index) {
106 return this._previousHandlers[index];
107 }
108 },
109
110 getNextHandler: {
111 value: function (index) {
112 return this._nextHandlers[index];
113 }
114 },
115
116 removeKnot: {
117 value: function (index) {
118 this._knots.splice(index, 1);
119 this._nextHandlers.splice(index, 1);
120 this._previousHandlers.splice(index, 1);
121 this._densities.splice(index, 1);
122 }
123 },
124
125 maxTime: {
126 get: function () {
127 if (!this._densitySummation) {
128 this._computeDensitySummation();
129 }
130 return this._densitySummation[this._densitySummation.length - 1];
131 },
132 set: function () {}
133 },
134
135 _computeDensitySummation: {
136 enumerable: false,
137 value: function () {
138 var length = this.densities.length - 1,
139 sum = 0,
140 i;
141
142 this._densitySummation = [];
143 for (i = 0; i < length; i++) {
144 sum += (this._densities[i] + this._densities[i + 1]) / 2;
145 this._densitySummation[i] = sum;
146 }
147 }
148 },
149
150 getPositionAtTime: {
151 value: function (time) {
152 var p0, p1, p2, p3,
153 a, b, c,
154 t, y,
155 start,
156 parameters = {},
157 i, j;
158
159 if ((time >= 0) && (time < this.maxTime)) {
160 if (this._previousIndex && (time >= this._densitySummation[this._previousIndex - 1])) {
161 i = this._previousIndex;
162 } else {
163 i = 0;
164 }
165 while (time >= this._densitySummation[i]) {
166 i++;
167 }
168 this._previousIndex = i;
169 start = i ? this._densitySummation[i - 1] : 0;
170 p0 = this._knots[i],
171 p1 = this._nextHandlers[i],
172 p2 = this._previousHandlers[i + 1],
173 p3 = this._knots[i + 1],
174 a = this._densities[i],
175 b = this._densities[i + 1],
176 c = a - b;
177 if ((c < -1e-10) || (c > 1e-10)) {
178 t = (a - Math.sqrt(a * a + (b - a) * 2 * (time - start))) / c;
179 } else {
180 t = (time - start) / a;
181 }
182 y = 1 - t;
183 // TODO: Redo this and create getParametersAtTime or getPositionAndParametersAtTime
184 for (j in this._parameters) {
185 if (this._parameters.hasOwnProperty(j)) {
186 if ((typeof this._parameters[j].data[i] !== "undefined") && (typeof this._parameters[j].data[i + 1] !== "undefined")) {
187 parameters[j] = (this._parameters[j].data[i] * y + this._parameters[j].data[i + 1] * t) + this._parameters[j].units;
188 } else {
189 parameters[j] = this._parameters[j].data[this._parameters[j].data.length - 1] + this._parameters[j].units;
190 }
191 }
192 }
193 return [
194 p0[0]*(y*y*y)+p1[0]*(y*y*t*3)+p2[0]*(y*t*t*3)+p3[0]*(t*t*t),
195 p0[1]*(y*y*y)+p1[1]*(y*y*t*3)+p2[1]*(y*t*t*3)+p3[1]*(t*t*t),
196 p0[2]*(y*y*y)+p1[2]*(y*y*t*3)+p2[2]*(y*t*t*3)+p3[2]*(t*t*t),
197 parameters
198 ];
199 } else {
200 return null;
201 }
202 }
203 },
204
205 transformVectorArray: {
206 value: function (vectors, matrix) {
207 var length = vectors.length,
208 out = [],
209 iVector,
210 i;
211
212 for (i = 0; i < length; i++) {
213 iVector = vectors[i];
214 out[i] = [
215 iVector[0] * matrix[0] + iVector[1] * matrix[4] + iVector[2] * matrix [8] + matrix[12],
216 iVector[0] * matrix[1] + iVector[1] * matrix[5] + iVector[2] * matrix [9] + matrix[13],
217 iVector[0] * matrix[2] + iVector[1] * matrix[6] + iVector[2] * matrix [10] + matrix[14]
218 ];
219 }
220 return out;
221 }
222 },
223
224 transform: {
225 value: function (matrix) {
226 var spline = Montage.create(FlowBezierSpline);
227
228 spline._densities = this._densities;
229 spline._knots = this.transformVectorArray(this.knots, matrix);
230 spline._previousHandlers = this.transformVectorArray(this.