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.js163
1 files changed, 105 insertions, 58 deletions
diff --git a/node_modules/montage/ui/flow-bezier-spline.js b/node_modules/montage/ui/flow-bezier-spline.js
index 5f2a9e76..3475be8b 100755
--- a/node_modules/montage/ui/flow-bezier-spline.js
+++ b/node_modules/montage/ui/flow-bezier-spline.js
@@ -1,6 +1,14 @@
1var Montage = require("montage").Montage, 1var Montage = require("montage").Montage,
2 FlowBezierSpline = exports.FlowBezierSpline = Montage.create(Montage, { 2 FlowBezierSpline = exports.FlowBezierSpline = Montage.create(Montage, {
3 3
4 init: {
5 value: function() {
6 this._densities = [];
7 this._densitySummation = [];
8 return this;
9 }
10 },
11
4 knots: { 12 knots: {
5 get: function () { 13 get: function () {
6 if (!this._knots) { 14 if (!this._knots) {
@@ -37,17 +45,20 @@ var Montage = require("montage").Montage,
37 } 45 }
38 }, 46 },
39 47
48 _densities: {
49 enumerable: false,
50 value: null
51 },
52
40 densities: { 53 densities: {
41 get: function () { 54 get: function () {
42 if (!this._densities) {
43 this._densities = [];
44 }
45 return this._densities; 55 return this._densities;
46 }, 56 },
47 set: function (value) { 57 set: function (value) {
48 this._densities = value; 58 this._densities = value;
49 this._densitiesLength = this._densities.length; 59 this._densitiesLength = this._densities.length;
50 this._densitySummation = null; 60 this._densitySummation.wipe();
61 this._maxTime = null;
51 } 62 }
52 }, 63 },
53 64
@@ -75,7 +86,7 @@ var Montage = require("montage").Montage,
75 parameters: { 86 parameters: {
76 get: function () { 87 get: function () {
77 if (!this._parameters) { 88 if (!this._parameters) {
78 this._parameters = []; 89 this._parameters = {};
79 } 90 }
80 return this._parameters; 91 return this._parameters;
81 }, 92 },
@@ -122,9 +133,14 @@ var Montage = require("montage").Montage,
122 } 133 }
123 }, 134 },
124 135
136 _maxTime: {
137 enumerable: false,
138 value: null
139 },
140
125 maxTime: { 141 maxTime: {
126 get: function () { 142 get: function () {
127 if (!this._densitySummation) { 143 if ((this._densitySummation === null) || this._densitySummation.length) {
128 this._computeDensitySummation(); 144 this._computeDensitySummation();
129 } 145 }
130 return this._densitySummation[this._densitySummation.length - 1]; 146 return this._densitySummation[this._densitySummation.length - 1];
@@ -132,41 +148,55 @@ var Montage = require("montage").Montage,
132 set: function () {} 148 set: function () {}
133 }, 149 },
134 150
151 _densitySummation: {
152 enumerable: false,
153 value: null
154 },
155
135 _computeDensitySummation: { 156 _computeDensitySummation: {
136 enumerable: false, 157 enumerable: false,
137 value: function () { 158 value: function () {
138 var length = this.densities.length - 1, 159 var densities = this.densities, length = densities.length - 1,
139 sum = 0, 160 sum = 0,
140 i; 161 i,
162 densitySummation = this._densitySummation;
141 163
142 this._densitySummation = []; 164 densitySummation.wipe();
143 for (i = 0; i < length; i++) { 165 for (i = 0; i < length; i++) {
144 sum += (this._densities[i] + this._densities[i + 1]) / 2; 166 sum += (densities[i] + densities[i + 1]) / 2;
145 this._densitySummation[i] = sum; 167 densitySummation[i] = sum;
146 } 168 }
147 } 169 }
148 }, 170 },
149 171
150 getPositionAtTime: { 172 getPositionAtTime: {
151 value: function (time) { 173 value: function (time, position, parameters) {
152 var p0, p1, p2, p3, 174 var p0, p1, p2, p3,
153 a, b, c, 175 a, b, c,
154 t, y, 176 t, y,
155 start, 177 start,
156 parameters = {}, 178 _parameters = this._parameters,
157 i, j; 179 i, j,
180 parameterKeys,
181 parameterKeyCount,
182 jParameter,
183 jParameterData,
184 densitySummation = this._densitySummation;
185
186 position.length = 0;
187 parameters.wipe();
158 188
159 if ((time >= 0) && (time < this.maxTime)) { 189 if ((time >= 0) && (time < this.maxTime)) {
160 if (this._previousIndex && (time >= this._densitySummation[this._previousIndex - 1])) { 190 if (this._previousIndex && (time >= densitySummation[this._previousIndex - 1])) {
161 i = this._previousIndex; 191 i = this._previousIndex;
162 } else { 192 } else {
163 i = 0; 193 i = 0;
164 } 194 }
165 while (time >= this._densitySummation[i]) { 195 while (time >= densitySummation[i]) {
166 i++; 196 i++;
167 } 197 }
168 this._previousIndex = i; 198 this._previousIndex = i;
169 start = i ? this._densitySummation[i - 1] : 0; 199 start = i ? densitySummation[i - 1] : 0;
170 p0 = this._knots[i], 200 p0 = this._knots[i],
171 p1 = this._nextHandlers[i], 201 p1 = this._nextHandlers[i],
172 p2 = this._previousHandlers[i + 1], 202 p2 = this._previousHandlers[i + 1],
@@ -181,24 +211,29 @@ var Montage = require("montage").Montage,
181 } 211 }
182 y = 1 - t; 212 y = 1 - t;
183 // TODO: Redo this and create getParametersAtTime or getPositionAndParametersAtTime 213 // TODO: Redo this and create getParametersAtTime or getPositionAndParametersAtTime
184 for (j in this._parameters) { 214
185 if (this._parameters.hasOwnProperty(j)) { 215 parameterKeys = Object.keys(_parameters);
186 if ((typeof this._parameters[j].data[i] !== "undefined") && (typeof this._parameters[j].data[i + 1] !== "undefined")) { 216 parameterKeyCount = parameterKeys.length;
187 parameters[j] = (this._parameters[j].data[i] * y + this._parameters[j].data[i + 1] * t) + this._parameters[j].units; 217
188 } else { 218 for (j = 0; j < parameterKeyCount; j++) {
189 parameters[j] = this._parameters[j].data[this._parameters[j].data.length - 1] + this._parameters[j].units; 219 jParameter = _parameters[parameterKeys[j]];
190 } 220 jParameterData = jParameter.data;
221 if ((typeof jParameterData[i] !== "undefined") && (typeof jParameterData[i + 1] !== "undefined")) {
222 parameters[parameterKeys[j]] = (jParameterData[i] * y + jParameterData[i + 1] * t).toFixed(5) + jParameter.units;
223 } else {
224 parameters[parameterKeys[j]] = jParameterData[jParameterData.length - 1].toFixed(5) + jParameter.units;
191 } 225 }
226
192 } 227 }
193 return [ 228
194 p0[0]*(y*y*y)+p1[0]*(y*y*t*3)+p2[0]*(y*t*t*3)+p3[0]*(t*t*t), 229 position.push(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), 230 position.push(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), 231 position.push(p0[2]*(y*y*y)+p1[2]*(y*y*t*3)+p2[2]*(y*t*t*3)+p3[2]*(t*t*t));
197 parameters 232 position.push(parameters);
198 ]; 233
199 } else {
200 return null;
201 } 234 }
235
236 return position;
202 } 237 }
203 }, 238 },
204 239
@@ -223,9 +258,10 @@ var Montage = require("montage").Montage,
223 258
224 transform: { 259 transform: {
225 value: function (matrix) { 260 value: function (matrix) {
226 var spline = Montage.create(FlowBezierSpline); 261 var spline = Montage.create(FlowBezierSpline).init();
227 262
228 spline._densities = this._densities; 263 spline._densities = this._densities;
264 spline._densitySummation = this._densitySummation;
229 spline._knots = this.transformVectorArray(this.knots, matrix); 265 spline._knots = this.transformVectorArray(this.knots, matrix);
230 spline._previousHandlers = this.transformVectorArray(this.previousHandlers, matrix); 266 spline._previousHandlers = this.transformVectorArray(this.previousHandlers, matrix);
231 spline._nextHandlers = this.transformVectorArray(this.nextHandlers, matrix); 267 spline._nextHandlers = this.transformVectorArray(this.nextHandlers, matrix);
@@ -267,7 +303,7 @@ var Montage = require("montage").Montage,
267 cubicRealRoots: { 303 cubicRealRoots: {
268 enumerable: false, 304 enumerable: false,
269 value: function (a, b, c, d) {