├── .gitignore
├── README.md
├── common.js
├── style
└── style.css
├── lib
├── jquery
│ ├── jquery.slider-1.3.css
│ ├── jquery.slider-1.3.js
│ └── jquery-1.8.2.min.js
├── kalmanjs
│ └── LinearKalmanFilter.js
├── plotjs
│ └── PlotJS.js
└── sylvester
│ └── sylvester.js
├── 1D.html
└── 2D.html
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | /.idea
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | KalmanJS
2 | ========
3 |
4 | JavaScript implementation and interactive playground for Kalman filters.
--------------------------------------------------------------------------------
/common.js:
--------------------------------------------------------------------------------
1 | function createControl(name, value, min, max, step) {
2 | $('#controls').append(
3 | '
'
4 | );
5 |
6 | $('#' + name)
7 | .slider({
8 | showValue: true,
9 | showRange: true,
10 | onChange: function(value, el) {
11 | options[name] = parseFloat(value);
12 | run();
13 | }
14 | });
15 | };
--------------------------------------------------------------------------------
/style/style.css:
--------------------------------------------------------------------------------
1 | BODY, HTML {
2 | width: 100%;
3 | height: 100%;
4 | margin: 0;
5 | padding: 0;
6 | }
7 | .plot-main {
8 | width: 50%;
9 | height: 690px;
10 | float: left;
11 | }
12 | .plot-sub {
13 | width: 25%;
14 | height: 230px;
15 | float: left;
16 | }
17 | #controls {
18 | clear: both;
19 | }
20 | #controls P {
21 | width: 22%;
22 | float: left;
23 | margin-left: 40px;
24 | }
25 | #github {
26 | position: absolute;
27 | left: 1px;
28 | top: 1px;
29 | }
30 | #github A {
31 | display: block;
32 | float: left;
33 | line-height: 24px;
34 | padding: 0 10px;
35 | color: #FFF;
36 | background-color: rgba(0, 0, 0, 0.5);
37 | margin: 0 1px 0 0;
38 | }
--------------------------------------------------------------------------------
/lib/jquery/jquery.slider-1.3.css:
--------------------------------------------------------------------------------
1 | .slider-wrap {
2 | position: relative;
3 | box-sizing: border-box;
4 | width: 100%;
5 | height: 4px;
6 | margin: 4px 0;
7 | background-color: #CCC;
8 | border-radius: 2px;
9 | }
10 | .slider-wrap.with-value,
11 | .slider-wrap.with-range {
12 | margin: 4px 0 24px;
13 | }
14 | .slider-connector-left {
15 | position: absolute;
16 | width: 0;
17 | height: 4px;
18 | left: 0;
19 | top: 0;
20 | background-color: #AAA;
21 | border-radius: 2px 0 0 2px;
22 | }
23 | .slider-handle-left,
24 | .slider-handle-right {
25 | position: absolute;
26 | width: 6px;
27 | height: 12px;
28 | left: 0;
29 | top: -4px;
30 | margin-left: -4px;
31 | background-color: #666;
32 | border-left: 1px solid #CCC;
33 | border-right: 1px solid #CCC;
34 | border-radius: 2px;
35 | box-shadow: 0 0 5px #666;
36 | }
37 | .slider-handle-left:hover,
38 | .slider-handle-right:hover {
39 | box-shadow: 0 0 10px #666;
40 | }
41 | .slider-value {
42 | position: absolute;
43 | width: 60px;
44 | text-align: center;
45 | left: 50%;
46 | margin-left: -20px;
47 | top: 8px;
48 | font-size: 12px;
49 | color: #666;
50 | }
51 | .slider-range-min,
52 | .slider-range-max {
53 | position: absolute;
54 | top: 8px;
55 | font-size: 12px;
56 | color: #666;
57 | }
58 | .slider-range-min {
59 | left: 0;
60 | }
61 | .slider-range-max {
62 | right: 0;
63 | }
64 | .slider-unselectable {
65 | -moz-user-select: none;
66 | -khtml-user-select: none;
67 | -webkit-user-select: none;
68 | user-select: none;
69 | }
--------------------------------------------------------------------------------
/lib/kalmanjs/LinearKalmanFilter.js:
--------------------------------------------------------------------------------
1 | function LinearKalmanFilter(
2 | stateTransitionMatrix, // A
3 | controlMatrix, // B
4 | observationMatrix, // H
5 | initialStateEstimate, // X
6 | initialCovarianceEstimate, // P
7 | processErrorEstimate, // Q
8 | measurementErrorEstimate // R
9 | ) {
10 | this.stateTransitionMatrix = stateTransitionMatrix;
11 | this.controlMatrix = controlMatrix;
12 | this.observationMatrix = observationMatrix;
13 | this.stateEstimate = initialStateEstimate;
14 | this.covarianceEstimate = initialCovarianceEstimate;
15 | this.processErrorEstimate = processErrorEstimate;
16 | this.measurementErrorEstimate = measurementErrorEstimate;
17 |
18 | this.predictedStateEstimate = null;
19 | this.predictedProbabilityEstimate = null;
20 | this.innovation = null;
21 | this.innovationCovariance = null;
22 | this.kalmanGain = null;
23 | }
24 |
25 | LinearKalmanFilter.prototype.getStateEstimate = function() {
26 | return this.stateEstimate;
27 | };
28 |
29 | LinearKalmanFilter.prototype.predict = function(controlVector) {
30 | this.predictedStateEstimate = this.stateTransitionMatrix
31 | .multiply(this.stateEstimate)
32 | .add(this.controlMatrix.multiply(controlVector));
33 |
34 | this.predictedProbabilityEstimate = this.stateTransitionMatrix
35 | .multiply(this.covarianceEstimate)
36 | .multiply(this.stateTransitionMatrix.transpose())
37 | .add(this.processErrorEstimate);
38 | };
39 |
40 | LinearKalmanFilter.prototype.observe = function(measurementVector) {
41 | if (measurementVector !== null) {
42 | this.innovation = measurementVector
43 | .subtract(this.observationMatrix.multiply(this.predictedStateEstimate));
44 |
45 | this.innovationCovariance = this.observationMatrix
46 | .multiply(this.predictedProbabilityEstimate)
47 | .multiply(this.observationMatrix.transpose())
48 | .add(this.measurementErrorEstimate);
49 | } else {
50 | this.innovation = Matrix.Zero(this.stateEstimate.dimensions().rows, this.stateEstimate.dimensions().cols);
51 |
52 | if (this.innovationCovariance === null) {
53 | this.innovationCovariance = Matrix.I(this.stateEstimate.dimensions().rows);
54 | }
55 | }
56 | };
57 |
58 | LinearKalmanFilter.prototype.update = function() {
59 | this.kalmanGain = this.predictedProbabilityEstimate
60 | .multiply(this.observationMatrix.transpose())
61 | .multiply(this.innovationCovariance.inverse());
62 |
63 | this.stateEstimate = this.predictedStateEstimate
64 | .add(this.kalmanGain.multiply(this.innovation));
65 |
66 | this.covarianceEstimate = Matrix.I(this.covarianceEstimate.dimensions().rows)
67 | .subtract(this.kalmanGain.multiply(this.observationMatrix))
68 | .multiply(this.predictedProbabilityEstimate);
69 | };
70 |
71 | LinearKalmanFilter.prototype.inspect = function() {
72 | for (var key in this) {
73 | if (this[key] === null || typeof(this[key]) !== 'object' || typeof(this[key].inspect) !== 'function') {
74 | continue;
75 | }
76 |
77 | console.log(key, '\n' + this[key].inspect());
78 | }
79 | };
80 |
81 | Math.randomGaussian = function(deviation, mean) {
82 | deviation = typeof(deviation) !== 'undefined' ? deviation : 0.5;
83 | mean = typeof(mean) !== 'undefined' ? mean : 0;
84 |
85 | return ((Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1)) * deviation + mean;
86 | };
--------------------------------------------------------------------------------
/1D.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | KalmanJS :: Singe Variable Linear Kalman Filter
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
239 |
240 |
241 |
242 |
--------------------------------------------------------------------------------
/2D.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | KalmanJS :: 2D Linear Kalman Filter
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
408 |
409 |
410 |
411 |
--------------------------------------------------------------------------------
/lib/plotjs/PlotJS.js:
--------------------------------------------------------------------------------
1 | /**
2 | * PlotJS is a very simple and minimalistic library to plot simple data as
3 | * line-graphs.
4 | */
5 | var PlotJS = function() {
6 | var id,
7 | data = [],
8 | title = null,
9 | colors = ['#F00', '#0F0', '#00F', '#FF0', '#0FF', '#F0F'],
10 | canvas,
11 | c,
12 | width,
13 | height,
14 | plotWidth,
15 | plotHeight,
16 | lastFunctionRangeStart = null,
17 | lastFunctionRangeEnd = null,
18 | lastFunctionRangeStep = null;
19 |
20 | function getItemCount() {
21 | var max = 0,
22 | i;
23 |
24 | for (i = 0; i < data.length; i++) {
25 | if (data[i].values.length > max) {
26 | max = data[i].values.length;
27 | }
28 | }
29 |
30 | return max;
31 | }
32 |
33 | function getExtVal(src, cmp) {
34 | var min = null,
35 | i;
36 |
37 | for (i = 0; i < src.length; i++) {
38 | if (
39 | !isNaN(src[i])
40 | && (min === null || cmp(src[i], min))
41 | ) {
42 | min = src[i];
43 | }
44 | }
45 |
46 | return min;
47 | }
48 |
49 | function getMinVal() {
50 | var min = null,
51 | value,
52 | i;
53 |
54 | for (i = 0; i < data.length; i++) {
55 | value = getExtVal(data[i].values, function(a, b) { return a < b; });
56 |
57 | if (min === null || value < min) {
58 | min = value;
59 | }
60 | }
61 |
62 | return min;
63 | }
64 |
65 | function getMaxVal() {
66 | var max = null,
67 | value,
68 | i;
69 |
70 | for (i = 0; i < data.length; i++) {
71 | value = getExtVal(data[i].values, function(a, b) { return a > b; });
72 |
73 | if (max === null || value > max) {
74 | max = value;
75 | }
76 | }
77 |
78 | return max;
79 | }
80 |
81 | function getMinSeries() {
82 | var min = null,
83 | value,
84 | i;
85 |
86 | for (i = 0; i < data.length; i++) {
87 | value = getExtVal(data[i].series, function(a, b) { return a < b; });
88 |
89 | if (min === null || value < min) {
90 | min = value;
91 | }
92 | }
93 |
94 | if (min !== null) {
95 | return min;
96 | } else {
97 | return 0;
98 | }
99 | }
100 |
101 | function getMaxSeries() {
102 | var max = null,
103 | value,
104 | i;
105 |
106 | for (i = 0; i < data.length; i++) {
107 | value = getExtVal(data[i].series, function(a, b) { return a > b; });
108 |
109 | if (max === null || value > max) {
110 | max = value;
111 | }
112 | }
113 |
114 | if (max !== null) {
115 | return max;
116 | } else {
117 | if (data.length > 0) {
118 | return data[0].values.length;
119 | } else {
120 | return 1;
121 | }
122 | }
123 | }
124 |
125 | function getRange(minVal, maxVal) {
126 | var rangeMin = minVal;
127 |
128 | if (maxVal > 10 && rangeMin > 0 && rangeMin < 2) {
129 | rangeMin = 0;
130 | }
131 |
132 | return {
133 | min: rangeMin,
134 | max: maxVal
135 | };
136 | }
137 |
138 | function round(number, decimals) {
139 | if (typeof(number) !== 'number') {
140 | return number;
141 | }
142 |
143 | return number.toFixed(decimals);
144 | }
145 |
146 | function map(x, inMin, inMax, outMin, outMax) {
147 | return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
148 | }
149 |
150 | var PlotJS = function() {
151 |
152 | };
153 |
154 | PlotJS.prototype.setSeries = function(items) {
155 | for (var i = 0; i < data.length; i++) {
156 | data[i].series = items;
157 | }
158 |
159 | return this;
160 | };
161 |
162 | PlotJS.prototype.isNumericSeries = function() {
163 | return data.length === 0 || (data[0].series.length > 0 && typeof(data[0].series[0]) === 'number');
164 | };
165 |
166 | PlotJS.prototype.addData = function(items, name, color) {
167 | if (typeof(items) !== 'object' || items === null) {
168 | return this;
169 | }
170 |
171 | var series = [],
172 | values = [],
173 | key,
174 | i;
175 |
176 | if (typeof(items.length) === 'undefined') {
177 | for (key in items) {
178 | if (typeof(key) === 'string' && parseFloat(key) == key) {
179 | key = parseFloat(key);
180 | }
181 |
182 | series.push(key);
183 | values.push(items[key]);
184 | }
185 | } else {
186 | values = items;
187 |
188 | for (i = 0; i < items.length; i++) {
189 | series.push(i + 1);
190 | }
191 | }
192 |
193 | data.push({
194 | series: series,
195 | values: values,
196 | name: name || 'Series ' + (data.length + 1),
197 | color: color || colors[data.length % colors.length]
198 | });
199 |
200 | return this;
201 | };
202 |
203 | PlotJS.prototype.setData = function(items, name, color) {
204 | this.clearData().addData(items, name, color);
205 |
206 | return this;
207 | };
208 |
209 | PlotJS.prototype.addFunction = function(
210 | fn,
211 | rangeStart,
212 | rangeEnd,
213 | step,
214 | name,
215 | color
216 | ) {
217 | rangeStart = typeof(rangeStart) === 'number' ? rangeStart : lastFunctionRangeStart || 0;
218 | rangeEnd = typeof(rangeEnd) === 'number' ? rangeEnd : lastFunctionRangeEnd || 100;
219 | step = typeof(step) === 'number' ? step : lastFunctionRangeStep || 0.1;
220 |
221 | var items = {},
222 | x;
223 |
224 | for (x = rangeStart; x < rangeEnd; x += step) {
225 | items[x] = fn(x);
226 | }
227 |
228 | lastFunctionRangeStart = rangeStart;
229 | lastFunctionRangeEnd = rangeEnd;
230 | lastFunctionRangeStep = step;
231 |
232 | return this.addData(items, name, color);
233 | };
234 |
235 | PlotJS.prototype.clearData = function() {
236 | data = [];
237 |
238 | return this;
239 | };
240 |
241 | PlotJS.prototype.getData = function() {
242 | return data;
243 | };
244 |
245 | PlotJS.prototype.getCanvas = function() {
246 | return canvas;
247 | };
248 |
249 | PlotJS.prototype.getContext = function() {
250 | return c;
251 | };
252 |
253 | PlotJS.prototype.setTitle = function(newTitle) {
254 | title = newTitle;
255 |
256 | return this;
257 | };
258 |
259 | PlotJS.prototype.getTitle = function() {
260 | return title;
261 | };
262 |
263 | PlotJS.prototype.draw = function(canvasId, options) {
264 | var baseOptions = {
265 | paddingLeft: 40,
266 | paddingBottom: 40,
267 | paddingRight: 10,
268 | paddingTop: 10,
269 | textPaddingX: 4,
270 | textPaddingY: 4,
271 | xMinStep: 40,
272 | seriesDecimals: 0,
273 | valueDecimals: 0,
274 | titleTop: 20,
275 | legendLineWidth: 20,
276 | legendLineHeight: 20,
277 | legendTop: 60,
278 | rangeMin: null,
279 | rangeMax: null,
280 | axisStyle: '#333',
281 | axisWidth: 1,
282 | axisFont: '12px Arial',
283 | titleFont: '20px Arial',
284 | legendFont: '16px Arial',
285 | titleStyle: '#333',
286 | bgStyle: 'rgba(255, 255, 255, 255)',
287 | plotStyle: '#FFF',
288 | axisTextStyle: '#333',
289 | zeroLineStyle: '#EEE',
290 | stepWidth: 4,
291 | stepHeight: 4,
292 | ySteps: 5,
293 | seriesSteps: 10,
294 | drawZeroLine: true,
295 | drawLegend: false,
296 | showPoints: true,
297 | animate: false,
298 | animationDuration: 1000
299 | },
300 | optionKey;
301 |
302 | if (options === null || typeof(options) !== 'object') {
303 | options = {};
304 | }
305 |
306 | for (optionKey in baseOptions) {
307 | if (typeof(options[optionKey]) === 'undefined') {
308 | options[optionKey] = baseOptions[optionKey];
309 | }
310 | }
311 |
312 | id = canvasId || 'plot';
313 | canvas = document.getElementById(id);
314 |
315 | if (canvas === null) {
316 | throw new Error('Canvas element #' + id + ' not found');
317 | }
318 |
319 | c = canvas.getContext('2d');
320 | width = canvas.width = canvas.offsetWidth;
321 | height = canvas.height = canvas.offsetHeight;
322 | plotWidth = width - options.paddingLeft - options.paddingRight;
323 | plotHeight = height - options.paddingTop - options.paddingBottom;
324 |
325 | var itemCount = getItemCount(),
326 | displayLimit = itemCount;
327 |
328 | if (options.animate) {
329 | if (arguments.length > 2) {
330 | displayLimit = arguments[2];
331 | } else {
332 | displayLimit = 1;
333 | }
334 | }
335 |
336 | // transform so that plot zero is the image origin and y is flipped
337 | c.setTransform(
338 | 1,
339 | 0,
340 | 0,
341 | -1,
342 | options.paddingLeft + 0.5,
343 | height - options.paddingBottom + 0.5
344 | );
345 |
346 | // draw entire background
347 | c.fillStyle = options.bgStyle;
348 | c.fillRect(-options.paddingLeft, -options.paddingBottom, width, height);
349 |
350 | // draw plot background
351 | c.fillStyle = options.plotStyle;
352 | c.fillRect(0, 0, plotWidth, plotHeight);
353 |
354 | // draw ticks
355 | var xPos,
356 | yVal,
357 | yPos,
358 | lastDrawnX = null,
359 | minVal = getMinVal(),
360 | maxVal = getMaxVal(),
361 | minSeries = getMinSeries(),
362 | maxSeries = getMaxSeries(),
363 | valueRange = getRange(minVal, maxVal),
364 | seriesRange = {
365 | min: minSeries,
366 | max: maxSeries,
367 | diff: maxSeries - minSeries
368 | };
369 |
370 | if (options.rangeMin !== null) {
371 | valueRange.min = options.rangeMin;
372 | }
373 |
374 | if (options.rangeMax !== null) {
375 | valueRange.max = options.rangeMax;
376 | }
377 | var valDiff = valueRange.max - valueRange.min,
378 | xStep = plotWidth / (itemCount - 1),
379 | yStep = valDiff / options.ySteps,
380 | itemToX = function(item) {
381 | return item * xStep;
382 | },
383 | seriesToX = function(value) {
384 | return map(
385 | value,
386 | seriesRange.min,
387 | seriesRange.max,
388 | 0,
389 | plotWidth
390 | );
391 | },
392 | valToY = function(value) {
393 | return map(
394 | value,
395 | valueRange.min,
396 | valueRange.max,
397 | 0,
398 | plotHeight
399 | );
400 | },
401 | drawText = function(text, x, y, align, baseline, font) {
402 | c.save();
403 | c.textAlign = align || 'left';
404 | c.textBaseline = baseline || 'middle';
405 | c.font = font || '13px Arial';
406 | c.setTransform(
407 | 1,
408 | 0,
409 | 0,
410 | 1,
411 | options.paddingLeft + 0.5,
412 | height - options.paddingBottom + 0.5
413 | );
414 | c.fillStyle = options.axisTextStyle;
415 | c.fillText(text, x, -y);
416 | c.restore();
417 | };
418 |
419 |
420 | // draw zero line if requested
421 | if (options.drawZeroLine) {
422 | var zeroLineY = valToY(0),
423 | zeroLineX = seriesToX(0);
424 |
425 | if (zeroLineY > 0) {
426 | c.strokeStyle = options.zeroLineStyle;
427 | c.beginPath();
428 | c.moveTo(0, zeroLineY);
429 | c.lineTo(plotWidth, zeroLineY);
430 | c.closePath();
431 | c.stroke();
432 | }
433 |
434 | if (zeroLineX > 0) {
435 | c.strokeStyle = options.zeroLineStyle;
436 | c.beginPath();
437 | c.moveTo(zeroLineX, 0);
438 | c.lineTo(zeroLineX, plotHeight);
439 | c.closePath();
440 | c.stroke();
441 | }
442 | }
443 |
444 | // horizontal ticks and labels
445 | c.strokeStyle = options.axisStyle;
446 | c.lineWidth = options.axisWidth;
447 |
448 | if (this.isNumericSeries()) {
449 | var seriesStep = seriesRange.diff / options.seriesSteps,
450 | seriesVal,
451 | seriesAlign,
452 | seriesCounter = 0;
453 |
454 | for (i = seriesRange.min; i <= seriesRange.max + 0.0001; i += seriesStep) {
455 | xPos = seriesToX(i);
456 |
457 | /*if (
458 | (lastDrawnX !== null && xPos - lastDrawnX < options.xMinStep)
459 | && xVal !== itemCount - 1
460 | ) {
461 | continue;
462 | }*/
463 |
464 | c.beginPath();
465 | c.moveTo(xPos, 0);
466 | c.lineTo(xPos, options.stepHeight);
467 | c.closePath();
468 | c.stroke();
469 |
470 | seriesVal = round(i, options.seriesDecimals);
471 |
472 | if (seriesCounter == 0) {
473 | seriesAlign = 'left';
474 | } else if (seriesCounter == options.seriesSteps) {
475 | seriesAlign = 'right';
476 | } else {
477 | seriesAlign = 'center';
478 | }
479 |
480 | drawText(
481 | seriesVal,
482 | xPos,
483 | -options.textPaddingY,
484 | seriesAlign,
485 | 'top',
486 | options.axisFont
487 | );
488 |
489 | lastDrawnX = xPos;
490 | seriesCounter++;
491 | }
492 | } else {
493 | var xVal,
494 | text,
495 | fillMap = {};
496 |
497 | for (i = 0; i < data.length; i++) {
498 | for (xVal = 0; xVal < itemCount; xVal++) {
499 | xPos = itemToX(xVal);
500 |
501 | if (
502 | (lastDrawnX !== null && xPos - lastDrawnX < options.xMinStep)
503 | && xVal !== itemCount - 1
504 | //&& Math.floor(series[xVal]) !== series[xVal]
505 | ) {
506 | continue;
507 | }
508 |
509 | text = typeof(data[i].series[xVal]) !== 'undefined'
510 | ? round(data[i].series[xVal], options.seriesDecimals)
511 | : round(xVal, options.seriesDecimals);
512 |
513 | if (typeof(fillMap[xPos]) !== 'undefined') {
514 | continue;
515 | }
516 |
517 | c.beginPath();
518 | c.moveTo(xPos, 0);
519 | c.lineTo(xPos, options.stepHeight);
520 | c.closePath();
521 | c.stroke();
522 |
523 | drawText(
524 | text,
525 | xPos,
526 | -options.textPaddingY,
527 | 'center',
528 | 'top',
529 | options.axisFont
530 | );
531 |
532 | lastDrawnX = xPos;
533 |
534 | fillMap[xPos] = text;
535 | }
536 | }
537 | }
538 |
539 | // vertical ticks and labels
540 | for (yVal = valueRange.min; yVal <= valueRange.max; yVal += yStep) {
541 | yPos = valToY(yVal);
542 |
543 | c.beginPath();
544 | c.moveTo(0, yPos);
545 | c.lineTo(options.stepWidth, yPos);
546 | c.closePath();
547 | c.stroke();
548 |
549 | drawText(
550 | round(yVal, options.valueDecimals),
551 | -options.textPaddingX,
552 | yPos,
553 | 'right',
554 | 'middle',
555 | options.axisFont
556 | );
557 | }
558 |
559 | // draw axis lines
560 | c.strokeStyle = options.axisStyle;
561 | c.lineWidth = options.axisWidth;
562 |
563 | c.beginPath();
564 | c.moveTo(0, 0);
565 | c.lineTo(plotWidth, 0);
566 | c.closePath();
567 | c.stroke();
568 |
569 | c.beginPath();
570 | c.moveTo(0, 0);
571 | c.lineTo(0, plotHeight);
572 | c.closePath();
573 | c.stroke();
574 |
575 | // draw data
576 | var xValue,
577 | yValue,
578 | i,
579 | j,
580 | lastNaN = false,
581 | lastItem = null;
582 |
583 | for (i = 0; i < data.length; i++) {
584 | if (data[i].values.length === 0) {
585 | continue;
586 | }
587 |
588 | c.strokeStyle = data[i].color;
589 | c.fillStyle = data[i].color;
590 | c.beginPath();
591 |
592 | for (j = 0; j < Math.min(data[i].values.length, displayLimit); j++) {
593 | xValue = data[i].series[j];
594 | yValue = data[i].values[j];
595 |
596 | if (isNaN(yValue)) {
597 | lastNaN = true;
598 |
599 | continue;
600 | }
601 |
602 | if (data[i].series.length == 0 || !this.isNumericSeries()) {
603 | xPos = itemToX(j);
604 | } else {
605 | xPos = seriesToX(xValue);
606 | }
607 |
608 | yPos = valToY(yValue);
609 |
610 | if (lastNaN) {
611 | c.moveTo(xPos, yPos);
612 | } else {
613 | c.lineTo(xPos, yPos);
614 | }
615 |
616 | if (options.showPoints) {
617 | c.fillRect(xPos - 1, yPos - 1, 3, 3);
618 | }
619 |
620 | lastNaN = false;
621 | lastItem = yValue;
622 | }
623 |
624 | c.stroke();
625 | }
626 |
627 | // draw legent if requested
628 | if (options.drawLegend) {
629 | for (i = 0; i < data.length; i++) {
630 | if (data[i].values.length === 0) {
631 | continue;
632 | }
633 |
634 | xPos = plotWidth - plotWidth / 4;
635 | yPos = plotHeight - options.legendTop + options.legendLineHeight * i;
636 |
637 | c.strokeStyle = data[i].color;
638 | c.beginPath();
639 | c.moveTo(xPos, yPos);
640 | c.lineTo(xPos + options.legendLineWidth, yPos);
641 | c.stroke();
642 |
643 | drawText(
644 | data[i].name,
645 | xPos + options.legendLineWidth * 1.25,
646 | yPos,
647 | 'left',
648 | 'middle',
649 | options.legendFont
650 | );
651 | }
652 | }
653 |
654 | // draw title if available
655 | if (title !== null) {
656 | drawText(
657 | title,
658 | plotWidth / 2,
659 | height - options.paddingBottom - options.titleTop,
660 | 'center',
661 | 'top',
662 | options.titleFont
663 | );
664 | }
665 |
666 | if (options.animate && displayLimit < itemCount) {
667 | var self = this;
668 |
669 | window.setTimeout(function() {
670 | self.draw(canvasId, options, displayLimit + 1);
671 | }, options.animationDuration / itemCount);
672 | }
673 |
674 | return this;
675 | };
676 |
677 | return new PlotJS;
678 | };
--------------------------------------------------------------------------------
/lib/jquery/jquery.slider-1.3.js:
--------------------------------------------------------------------------------
1 | (function($){
2 | var instances = 0;
3 |
4 | function Slider(input, options) {
5 | this.defaults = {
6 | classPrefix: '',
7 | width: '100%',
8 | showValue: false,
9 | showRange: false,
10 | onStart: null,
11 | onChange: null,
12 | onEnd: null,
13 | minChangeInterval: 0,
14 | decimals: 1
15 | };
16 |
17 | var step = $(input).data('step') || 1;
18 |
19 | this.defaults['decimals'] = step < 1 ? ((1 / step) + '').length - 1 : 0;
20 |
21 | this.options = $.extend({}, this.defaults, options);
22 | this.input = $(input);
23 | this.dragging = 0;
24 | this.ranged = false;
25 | this.startValue = 0;
26 | this.startValueLeft = null;
27 | this.startValueRight = null;
28 | this.wrapId = null;
29 | this.handleLeftId = null;
30 | this.handleRightId = null;
31 | this.connectorId = null;
32 | this.valueId = null;
33 | this.rangeMinId = null;
34 | this.rangeMaxId = null;
35 | this.wrap = null;
36 | this.handleLeft = null;
37 | this.handleRight = null;
38 | this.connector = null;
39 | this.value = null;
40 | this.rangeMin = null;
41 | this.rangeMax = null;
42 | this.lastChangeTime = null;
43 | this.activeHandle = 0;
44 |
45 | this.setupDom = function() {
46 | instances++;
47 |
48 | // check for range slider
49 | this.startValue = this.input.val();
50 |
51 | if (this.startValue.indexOf(' ') != -1) {
52 | var startValues = this.startValue.split(' ');
53 |
54 | this.startValueLeft = parseInt(startValues[0]);
55 | this.startValueRight = startValues[1];
56 |
57 | this.startValue = this.startValueLeft;
58 | this.ranged = true;
59 | }
60 |
61 | // create the main wrap
62 | var baseId = this.input.attr('id');
63 |
64 | if (baseId == null) {
65 | baseId = instances;
66 | }
67 |
68 | this.wrapId = 'slider-wrap-' + baseId;
69 |
70 | this.input.after($('', {
71 | 'id': this.wrapId,
72 | 'class': this.options.classPrefix + 'slider-wrap'
73 | }));
74 |
75 | this.wrap = $('#' + this.wrapId);
76 | this.wrap.css({
77 | width: this.options.width
78 | });
79 |
80 | // add connector
81 | this.connectorId = 'slider-connector-left-' + baseId;
82 |
83 | this.wrap.append($('', {
84 | 'id': this.connectorId,
85 | 'class': this.options.classPrefix + 'slider-connector-left'
86 | }));
87 |
88 | this.connector = $('#' + this.connectorId);
89 |
90 | // add left handle
91 | this.handleLeftId = 'slider-handle-left-' + baseId;
92 |
93 | this.wrap.append($('', {
94 | 'id': this.handleLeftId,
95 | 'class': this.options.classPrefix + 'slider-handle-left'
96 | }));
97 |
98 | this.handleLeft = $('#' + this.handleLeftId);
99 |
100 | if (this.ranged) {
101 | this.handleRightId = 'slider-handle-right-' + baseId;
102 |
103 | this.wrap.append($('', {
104 | 'id': this.handleRightId,
105 | 'class': this.options.classPrefix + 'slider-handle-right'
106 | }));
107 |
108 | this.handleRight = $('#' + this.handleRightId);
109 | }
110 |
111 | // add labels
112 | if (this.options.showValue) {
113 | this.valueId = 'slider-value-' + baseId;
114 |
115 | this.wrap.append($('', {
116 | 'id': this.valueId,
117 | 'class': this.options.classPrefix + 'slider-value'
118 | }));
119 |
120 | this.wrap.addClass('with-value');
121 |
122 | this.value = $('#' + this.valueId);
123 |
124 | if (!this.ranged) {
125 | this.value.html(this.round(this.startValue, this.options.decimals));
126 | } else {
127 | this.value.html(
128 | this.round(this.startValueLeft, this.options.decimals) + ' - ' +
129 | this.round(this.startValueRight, this.options.decimals)
130 | );
131 | }
132 | }
133 |
134 | if (this.options.showRange) {
135 | var minValue = this.input.data('min') || 0,
136 | maxValue = this.input.data('max') || 100;
137 |
138 | this.rangeMinId = 'slider-range-min-' + baseId;
139 | this.rangeMaxId = 'slider-range-max-' + baseId;
140 |
141 | this.wrap.append($('', {
142 | 'id': this.rangeMinId,
143 | 'class': this.options.classPrefix + 'slider-range-min'
144 | }));
145 |
146 | this.wrap.append($('', {
147 | 'id': this.rangeMaxId,
148 | 'class': this.options.classPrefix + 'slider-range-max'
149 | }));
150 |
151 | this.wrap.addClass('with-range');
152 |
153 | this.rangeMin = $('#' + this.rangeMinId);
154 | this.rangeMax = $('#' + this.rangeMaxId);
155 |
156 | this.rangeMin.html(minValue);
157 | this.rangeMax.html(maxValue);
158 | }
159 |
160 | // set default value
161 | this.setValue(this.input.val());
162 |
163 | this.input.hide();
164 | };
165 |
166 | this.setupEvents = function() {
167 | var self = this;
168 |
169 | this.handleLeft.mousedown(function(e) {
170 | self.onDragStart(e);
171 | self.onDragStartLeft(e);
172 |
173 | $(document).unbind('mousemove').mousemove(function(e) {
174 | self.onDragProgress(e);
175 | });
176 |
177 | $(document).one('mouseup', function(e) {
178 | $(document).unbind('mousemove');
179 |
180 | self.onDragEnd(e);
181 | });
182 |
183 | $(document).one('mousedown', function() {
184 | self.activeHandle = 0;
185 | });
186 |
187 | for (var i = 0; i < window._jQsliders.length; i++) {
188 | window._jQsliders[i].activeHandle = 0;
189 | }
190 |
191 | self.activeHandle = 1;
192 |
193 | e.preventDefault();
194 |
195 | return false;
196 | });
197 |
198 | if (this.ranged) {
199 | this.handleRight.mousedown(function(e) {
200 | self.onDragStart(e);
201 | self.onDragStartRight(e);
202 |
203 | $(document).unbind('mousemove').mousemove(function(e) {
204 | self.onDragProgress(e);
205 | });
206 |
207 | $(document).one('mouseup', function(e) {
208 | $(document).unbind('mousemove');
209 |
210 | self.onDragEnd(e);
211 | });
212 |
213 | $(document).one('mousedown', function() {
214 | self.activeHandle = 0;
215 | });
216 |
217 | for (var i = 0; i < window._jQsliders.length; i++) {
218 | window._jQsliders[i].activeHandle = 0;
219 | }
220 |
221 | self.activeHandle = 2;
222 |
223 | e.preventDefault();
224 |
225 | return false;
226 | });
227 | }
228 |
229 | $(document).keydown(function(e) {
230 | self.onKeyDown(e);
231 | });
232 |
233 | this.wrap.click(function(e) {
234 | self.jumpTo(e.clientX);
235 | });
236 | };
237 |
238 | this.onDragStart = function() {
239 | this.disableTextSelect();
240 | };
241 |
242 | this.onDragStartLeft = function() {
243 | this.dragging = 1;
244 |
245 | if (typeof(this.options.onStart) == 'function') {
246 | this.options.onStart('left', this.input[0]);
247 | }
248 |
249 | if (typeof(this.options.onStartLeft) == 'function') {
250 | this.options.onStartLeft(this.input[0]);
251 | }
252 | };
253 |
254 | this.onDragStartRight = function() {
255 | this.dragging = 2;
256 |
257 | if (typeof(this.options.onStart) == 'function') {
258 | this.options.onStart('right', this.input[0]);
259 | }
260 |
261 | if (typeof(this.options.onStartRight) == 'function') {
262 | this.options.onStartRight(this.input[0]);
263 | }
264 | };
265 |
266 | this.onDragProgress = function(e) {
267 | if (this.dragging == 0) {
268 | return;
269 | }
270 |
271 | var wrapLeft = this.wrap.offset().left,
272 | wrapWidth = parseInt(this.wrap.width()),
273 | pos = Math.min(Math.max(e.clientX - wrapLeft, 0), wrapWidth);
274 |
275 | if (this.ranged) {
276 | var leftPos = this.handleLeft.position().left,
277 | rightPos = this.handleRight.position().left;
278 |
279 | if (this.dragging == 1 && pos > rightPos) {
280 | pos = rightPos;
281 | } else if (this.dragging == 2 && pos < leftPos) {
282 | pos = leftPos;
283 | }
284 | }
285 |
286 | var minValue = this.input.data('min') || 0,
287 | maxValue = this.input.data('max') || 100,
288 | step = this.input.data('step') || 1,
289 | range = maxValue - minValue,
290 | normalizedValue = pos / wrapWidth,
291 | value = Math.round(
292 | (range * normalizedValue + minValue) / step
293 | ) * step,
294 | currentTime,
295 | sinceLast;
296 |
297 | normalizedValue = (value - minValue) / range;
298 | pos = wrapWidth * normalizedValue;
299 |
300 | if (!this.ranged) {
301 | this.handleLeft.css({
302 | left: pos
303 | });
304 |
305 | this.connector.css({
306 | width: pos
307 | });
308 |
309 | this.input.attr('value', value);
310 |
311 | if (this.options.showValue) {
312 | this.value.html(this.round(value, this.options.decimals));
313 | }
314 |
315 | if (typeof(this.options.onChange) == 'function') {
316 | if (this.options.minChangeInterval == 0) {
317 | this.options.onChange(value, this.input[0]);
318 | } else {
319 | currentTime = this.getMillitime();
320 | sinceLast = currentTime - this.lastChangeTime;
321 |
322 | if (
323 | this.options.minChangeInterval == null
324 | || sinceLast >= this.options.minChangeInterval
325 | ) {
326 | this.options.onChange(value, this.input[0]);
327 | this.lastChangeTime = currentTime;
328 | }
329 | }
330 | }
331 | } else {
332 | var handle = this.dragging == 1
333 | ? this.handleLeft
334 | : this.handleRight,
335 | currentValues = this.input.val().split(' '),
336 | valueLeft = parseInt(currentValues[0]),
337 | valueRight = parseInt(currentValues[1]);
338 |
339 | handle.css({
340 | left: pos
341 | });
342 |
343 | var posLeft = this.handleLeft.position().left,
344 | posRight = this.handleRight.position().left;
345 |
346 | this.connector.css({
347 | left: posLeft,
348 | width: posRight - posLeft
349 | });
350 |
351 | if (this.dragging == 1) {
352 | valueLeft = value;
353 | } else {
354 | valueRight = value;
355 | }
356 |
357 | this.input.attr('value', valueLeft + ' ' + valueRight);
358 |
359 | if (this.options.showValue) {
360 | this.value.html(
361 | this.round(valueLeft, this.options.decimals) + ' - ' +
362 | this.round(valueRight, this.options.decimals)
363 | );
364 | }
365 |
366 | if (typeof(this.options.onChange) == 'function') {
367 | this.options.onChange(
368 | valueLeft,
369 | valueRight,
370 | this.input[0]
371 | );
372 | }
373 |
374 | if (typeof(this.options.onChange) == 'function') {
375 | if (this.options.minChangeInterval == 0) {
376 | this.options.onChange(
377 | valueLeft,
378 | valueRight,
379 | this.input[0]
380 | );
381 | } else {
382 | currentTime = this.getMillitime();
383 | sinceLast = currentTime - this.lastChangeTime;
384 |
385 | if (
386 | this.options.minChangeInterval == null
387 | || sinceLast >= this.options.minChangeInterval
388 | ) {
389 | this.options.onChange(
390 | valueLeft,
391 | valueRight,
392 | this.input[0]
393 | );
394 | this.lastChangeTime = currentTime;
395 | }
396 | }
397 | }
398 | }
399 |
400 | this.input.trigger('change');
401 | };
402 |
403 | this.onDragEnd = function() {
404 | if (this.dragging == 0) {
405 | return;
406 | }
407 |
408 | if (typeof(this.options.onEnd) == 'function') {
409 | this.options.onEnd(
410 | this.input.val(),
411 | this.dragging == 1 ? 'left' : 'right',
412 | this.input[0]
413 | );
414 | }
415 |
416 | this.dragging = 0;
417 |
418 | this.enableTextSelect();
419 | };
420 |
421 | this.onKeyDown = function(e) {
422 | if (
423 | this.activeHandle == 0
424 | || (e.keyCode != 37 && e.keyCode != 39)
425 | ) {
426 | return;
427 | }
428 |
429 | var rawValue = this.input.val(),
430 | leftValue,
431 | rightValue,
432 | changeValue;
433 |
434 | if (this.ranged) {
435 | leftValue = parseInt(rawValue.split(' ')[0]);
436 | rightValue = parseInt(rawValue.split(' ')[1]);
437 |
438 | if (this.activeHandle == 1) {
439 | changeValue = leftValue;
440 | } else {
441 | changeValue = rightValue;
442 | }
443 | } else {
444 | leftValue = parseInt(rawValue);
445 | changeValue = leftValue;
446 | }
447 |
448 | var minValue = parseInt(this.input.data('min')) || 0,
449 | maxValue = parseInt(this.input.data('max')) || 100,
450 | step = parseInt(this.input.data('step')) || 1;
451 |
452 | if (e.shiftKey) {
453 | step *= 10;
454 | }
455 |
456 | if (e.keyCode == 37) {
457 | changeValue = Math.max(changeValue - step, minValue);
458 | } else {
459 | changeValue = Math.min(changeValue + step, maxValue);
460 | }
461 |
462 | if (this.ranged) {
463 | if (this.activeHandle == 1) {
464 | if (changeValue > rightValue) {
465 | changeValue = rightValue;
466 | }
467 |
468 | this.setValue(changeValue + ' ' + rightValue);
469 | } else {
470 | if (changeValue < leftValue) {
471 | changeValue = leftValue;
472 | }
473 |
474 | this.setValue(leftValue + ' ' + changeValue);
475 | }
476 | } else {
477 | this.setValue(changeValue);
478 | }
479 | };
480 |
481 | this.jumpTo = function(clientX) {
482 | if (this.dragging != 0) {
483 | return;
484 | }
485 |
486 | var wrapLeft = this.wrap.offset().left,
487 | wrapWidth = parseInt(this.wrap.width()),
488 | pos = Math.min(Math.max(clientX - wrapLeft, 0), wrapWidth),
489 | event = {
490 | clientX: clientX
491 | };
492 |
493 | for (var i = 0; i < window._jQsliders.length; i++) {
494 | window._jQsliders[i].activeHandle = 0;
495 | }
496 |
497 | this.dragging = !this.ranged || pos < wrapWidth / 2 ? 1 : 2;
498 | this.activeHandle = this.dragging;
499 | this.onDragProgress(event);
500 | this.dragging = 0;
501 | };
502 |
503 | this.setValue = function(value) {
504 | this.startValue = value;
505 |
506 | if (typeof(value) == 'string' && value.indexOf(' ') != -1) {
507 | var startValues = value.split(' ');
508 |
509 | this.startValueLeft = parseInt(startValues[0]);
510 | this.startValueRight = startValues[1];
511 |
512 | this.startValue = this.startValueLeft;
513 | this.ranged = true;
514 | }
515 |
516 | var minValue = this.input.data('min') || 0,
517 | maxValue = this.input.data('max') || 100,
518 | step = this.input.data('step') || 1,
519 | range = maxValue - minValue,
520 | wrapWidth = parseInt(this.wrap.width());
521 |
522 | if (this.ranged) {
523 | if (this.startValueLeft < minValue) {
524 | this.startValueLeft = minValue;
525 | }
526 |
527 | if (this.startValueRight < minValue) {
528 | this.startValueRight = minValue;
529 | }
530 |
531 | if (this.startValueLeft > maxValue) {
532 | this.startValueLeft = maxValue;
533 | }
534 |
535 | if (this.startValueRight > maxValue) {
536 | this.startValueRight = maxValue;
537 | }
538 |
539 | var valueLeft = Math.round(this.startValueLeft / step) * step,
540 | normalizedValueLeft = (valueLeft - minValue) / range,
541 | posLeft = wrapWidth * normalizedValueLeft,
542 | valueRight = Math.round(this.startValueRight / step) * step,
543 | normalizedValueRight = (valueRight - minValue) / range,
544 | posRight = wrapWidth * normalizedValueRight;
545 |
546 | this.handleLeft.css({
547 | left: posLeft
548 | });
549 |
550 | this.handleRight.css({
551 | left: posRight
552 | });
553 |
554 | this.connector.css({
555 | left: posLeft,
556 | width: posRight - posLeft
557 | });
558 |
559 | this.input.attr('value', valueLeft + ' ' + valueRight);
560 |
561 | if (this.options.showValue) {
562 | this.value.html(
563 | this.round(valueLeft, this.options.decimals) + ' - ' +
564 | this.round(valueRight, this.options.decimals)
565 | );
566 | }
567 |
568 | if (typeof(this.options.onChange) == 'function') {
569 | this.options.onChange(
570 | valueLeft,
571 | valueRight,
572 | this.input[0]
573 | );
574 | }
575 | } else {
576 | if (this.startValue < minValue) {
577 | this.startValue = minValue;
578 | } else if (this.startValue > maxValue) {
579 | this.startValue = maxValue;
580 | }
581 |
582 | var singleValue = Math.round(this.startValue / step) * step,
583 | normalizedValue = (singleValue - minValue) / range,
584 | pos = wrapWidth * normalizedValue;
585 |
586 | this.handleLeft.css({
587 | left: pos
588 | });
589 |
590 | this.connector.css({
591 | width: pos
592 | });
593 |
594 | this.input.attr('value', value);
595 |
596 | if (this.options.showValue) {
597 | this.value.html(this.round(singleValue, this.options.decimals));
598 | }
599 |
600 | if (typeof(this.options.onChange) == 'function') {
601 | this.options.onChange(
602 | singleValue,
603 | this.input[0]
604 | );
605 | }
606 | }
607 |
608 | this.input.trigger('change');
609 | };
610 |
611 | this.disableTextSelect = function() {
612 | $('*')
613 | .attr('unselectable', 'on')
614 | .css('user-select', 'none')
615 | .on('selectstart', false)
616 | .addClass('slider-unselectable');
617 | };
618 |
619 | this.enableTextSelect = function() {
620 | $('*')
621 | .removeAttr('unselectable')
622 | .css('user-select', '')
623 | .unbind('selectstart')
624 | .removeClass('slider-unselectable');
625 | };
626 |
627 | this.getMillitime = function() {
628 | return (new Date()).getTime();
629 | };
630 |
631 | this.round = function(number, decimals) {
632 | if (typeof(number) !== 'number') {
633 | return number;
634 | }
635 |
636 | return number.toFixed(decimals);
637 | };
638 | }
639 |
640 | Slider.prototype = {
641 | init: function() {
642 | if (typeof(window._jQsliders) == 'undefined') {
643 | window._jQsliders = [];
644 | }
645 |
646 | window._jQsliders.push(this);
647 |
648 | this.setupDom();
649 | this.setupEvents();
650 | },
651 |
652 | range: function(min, max, step) {
653 | this.input.data('min', min);
654 | this.input.data('max', max);
655 |
656 | if (typeof(step) == 'number') {
657 | this.input.data('step', step);
658 | }
659 |
660 | if (this.options.showRange) {
661 | this.rangeMin.html(min);
662 | this.rangeMax.html(max);
663 | }
664 |
665 | this.setValue(this.input.val());
666 | },
667 |
668 | min: function(min) {
669 | this.range(min, this.input.data('max'));
670 | },
671 |
672 | max: function(max) {
673 | this.range(this.input.data('min'), max);
674 | },
675 |
676 | val: function(value, right) {
677 | if (typeof(right) != 'undefined') {
678 | value = parseInt(value) + ' ' + parseInt(right);
679 | } else if (parseInt(value) == value) {
680 | value = parseInt(value);
681 | }
682 |
683 | this.setValue(value);
684 | },
685 |
686 | change: function(callback) {
687 | this.options.onChange = callback;
688 | },
689 |
690 | start: function(callback) {
691 | this.options.onStart = callback;
692 | },
693 |
694 | end: function(callback) {
695 | this.options.onEnd = callback;
696 | }
697 | };
698 |
699 | $.fn.slider = function(options) {
700 | var args = Array.prototype.slice.call(arguments);
701 |
702 | if(this.length) {
703 | this.each(function() {
704 | var slider = $(this).data('slider');
705 |
706 | if (slider == null) {
707 | slider = new Slider(this, options);
708 |
709 | slider.init();
710 |
711 | $(this).data('slider', slider);
712 | } else {
713 | if (args.length == 0) {
714 | return;
715 | }
716 |
717 | var method = args[0];
718 |
719 | if (typeof(slider[method]) == 'function') {
720 | slider[method].apply(slider, args.slice(1));
721 | }
722 | }
723 | });
724 | }
725 |
726 | return this;
727 | };
728 | })(jQuery);
--------------------------------------------------------------------------------
/lib/sylvester/sylvester.js:
--------------------------------------------------------------------------------
1 | var Sylvester = {
2 | precision: 1e-6
3 | };
4 |
5 | Sylvester.Vector = function() {};
6 |
7 | Sylvester.Vector.create = function(elements) {
8 | var V = new Sylvester.Vector();
9 | return V.setElements(elements);
10 | };
11 | var $V = Sylvester.Vector.create;
12 |
13 | Sylvester.Vector.Random = function(n) {
14 | var elements = [];
15 | while (n--) { elements.push(Math.random()); }
16 | return Sylvester.Vector.create(elements);
17 | };
18 |
19 | Sylvester.Vector.Zero = function(n) {
20 | var elements = [];
21 | while (n--) { elements.push(0); }
22 | return Sylvester.Vector.create(elements);
23 | };
24 |
25 | Sylvester.Vector.prototype = {
26 | e: function(i) {
27 | return (i < 1 || i > this.elements.length) ? null : this.elements[i-1];
28 | },
29 |
30 | dimensions: function() {
31 | return this.elements.length;
32 | },
33 |
34 | modulus: function() {
35 | return Math.sqrt(this.dot(this));
36 | },
37 |
38 | eql: function(vector) {
39 | var n = this.elements.length;
40 | var V = vector.elements || vector;
41 | if (n !== V.length) { return false; }
42 | while (n--) {
43 | if (Math.abs(this.elements[n] - V[n]) > Sylvester.precision) { return false; }
44 | }
45 | return true;
46 | },
47 |
48 | dup: function() {
49 | return Sylvester.Vector.create(this.elements);
50 | },
51 |
52 | map: function(fn, context) {
53 | var elements = [];
54 | this.each(function(x, i) {
55 | elements.push(fn.call(context, x, i));
56 | });
57 | return Sylvester.Vector.create(elements);
58 | },
59 |
60 | forEach: function(fn, context) {
61 | var n = this.elements.length;
62 | for (var i = 0; i < n; i++) {
63 | fn.call(context, this.elements[i], i+1);
64 | }
65 | },
66 |
67 | toUnitVector: function() {
68 | var r = this.modulus();
69 | if (r === 0) { return this.dup(); }
70 | return this.map(function(x) { return x/r; });
71 | },
72 |
73 | angleFrom: function(vector) {
74 | var V = vector.elements || vector;
75 | var n = this.elements.length, k = n, i;
76 | if (n !== V.length) { return null; }
77 | var dot = 0, mod1 = 0, mod2 = 0;
78 | // Work things out in parallel to save time
79 | this.each(function(x, i) {
80 | dot += x * V[i-1];
81 | mod1 += x * x;
82 | mod2 += V[i-1] * V[i-1];
83 | });
84 | mod1 = Math.sqrt(mod1); mod2 = Math.sqrt(mod2);
85 | if (mod1*mod2 === 0) { return null; }
86 | var theta = dot / (mod1*mod2);
87 | if (theta < -1) { theta = -1; }
88 | if (theta > 1) { theta = 1; }
89 | return Math.acos(theta);
90 | },
91 |
92 | isParallelTo: function(vector) {
93 | var angle = this.angleFrom(vector);
94 | return (angle === null) ? null : (angle <= Sylvester.precision);
95 | },
96 |
97 | isAntiparallelTo: function(vector) {
98 | var angle = this.angleFrom(vector);
99 | return (angle === null) ? null : (Math.abs(angle - Math.PI) <= Sylvester.precision);
100 | },
101 |
102 | isPerpendicularTo: function(vector) {
103 | var dot = this.dot(vector);
104 | return (dot === null) ? null : (Math.abs(dot) <= Sylvester.precision);
105 | },
106 |
107 | add: function(vector) {
108 | var V = vector.elements || vector;
109 | if (this.elements.length !== V.length) { return null; }
110 | return this.map(function(x, i) { return x + V[i-1]; });
111 | },
112 |
113 | subtract: function(vector) {
114 | var V = vector.elements || vector;
115 | if (this.elements.length !== V.length) { return null; }
116 | return this.map(function(x, i) { return x - V[i-1]; });
117 | },
118 |
119 | multiply: function(k) {
120 | return this.map(function(x) { return x*k; });
121 | },
122 |
123 | dot: function(vector) {
124 | var V = vector.elements || vector;
125 | var i, product = 0, n = this.elements.length;
126 | if (n !== V.length) { return null; }
127 | while (n--) { product += this.elements[n] * V[n]; }
128 | return product;
129 | },
130 |
131 | cross: function(vector) {
132 | var B = vector.elements || vector;
133 | if (this.elements.length !== 3 || B.length !== 3) { return null; }
134 | var A = this.elements;
135 | return Sylvester.Vector.create([
136 | (A[1] * B[2]) - (A[2] * B[1]),
137 | (A[2] * B[0]) - (A[0] * B[2]),
138 | (A[0] * B[1]) - (A[1] * B[0])
139 | ]);
140 | },
141 |
142 | max: function() {
143 | var m = 0, i = this.elements.length;
144 | while (i--) {
145 | if (Math.abs(this.elements[i]) > Math.abs(m)) { m = this.elements[i]; }
146 | }
147 | return m;
148 | },
149 |
150 | indexOf: function(x) {
151 | var index = null, n = this.elements.length;
152 | for (var i = 0; i < n; i++) {
153 | if (index === null && this.elements[i] === x) {
154 | index = i + 1;
155 | }
156 | }
157 | return index;
158 | },
159 |
160 | toDiagonalMatrix: function() {
161 | return Sylvester.Matrix.Diagonal(this.elements);
162 | },
163 |
164 | round: function() {
165 | return this.map(function(x) { return Math.round(x); });
166 | },
167 |
168 | snapTo: function(x) {
169 | return this.map(function(y) {
170 | return (Math.abs(y - x) <= Sylvester.precision) ? x : y;
171 | });
172 | },
173 |
174 | distanceFrom: function(obj) {
175 | if (obj.anchor || (obj.start && obj.end)) { return obj.distanceFrom(this); }
176 | var V = obj.elements || obj;
177 | if (V.length !== this.elements.length) { return null; }
178 | var sum = 0, part;
179 | this.each(function(x, i) {
180 | part = x - V[i-1];
181 | sum += part * part;
182 | });
183 | return Math.sqrt(sum);
184 | },
185 |
186 | liesOn: function(line) {
187 | return line.contains(this);
188 | },
189 |
190 | liesIn: function(plane) {
191 | return plane.contains(this);
192 | },
193 |
194 | rotate: function(t, obj) {
195 | var V, R = null, x, y, z;
196 | if (t.determinant) { R = t.elements; }
197 | switch (this.elements.length) {
198 | case 2:
199 | V = obj.elements || obj;
200 | if (V.length !== 2) { return null; }
201 | if (!R) { R = Sylvester.Matrix.Rotation(t).elements; }
202 | x = this.elements[0] - V[0];
203 | y = this.elements[1] - V[1];
204 | return Sylvester.Vector.create([
205 | V[0] + R[0][0] * x + R[0][1] * y,
206 | V[1] + R[1][0] * x + R[1][1] * y
207 | ]);
208 | break;
209 | case 3:
210 | if (!obj.direction) { return null; }
211 | var C = obj.pointClosestTo(this).elements;
212 | if (!R) { R = Sylvester.Matrix.Rotation(t, obj.direction).elements; }
213 | x = this.elements[0] - C[0];
214 | y = this.elements[1] - C[1];
215 | z = this.elements[2] - C[2];
216 | return Sylvester.Vector.create([
217 | C[0] + R[0][0] * x + R[0][1] * y + R[0][2] * z,
218 | C[1] + R[1][0] * x + R[1][1] * y + R[1][2] * z,
219 | C[2] + R[2][0] * x + R[2][1] * y + R[2][2] * z
220 | ]);
221 | break;
222 | default:
223 | return null;
224 | }
225 | },
226 |
227 | reflectionIn: function(obj) {
228 | if (obj.anchor) {
229 | // obj is a plane or line
230 | var P = this.elements.slice();
231 | var C = obj.pointClosestTo(P).elements;
232 | return Sylvester.Vector.create([C[0] + (C[0] - P[0]), C[1] + (C[1] - P[1]), C[2] + (C[2] - (P[2] || 0))]);
233 | } else {
234 | // obj is a point
235 | var Q = obj.elements || obj;
236 | if (this.elements.length !== Q.length) { return null; }
237 | return this.map(function(x, i) { return Q[i-1] + (Q[i-1] - x); });
238 | }
239 | },
240 |
241 | to3D: function() {
242 | var V = this.dup();
243 | switch (V.elements.length) {
244 | case 3: break;
245 | case 2: V.elements.push(0); break;
246 | default: return null;
247 | }
248 | return V;
249 | },
250 |
251 | inspect: function() {
252 | return '[' + this.elements.join(', ') + ']';
253 | },
254 |
255 | setElements: function(els) {
256 | this.elements = (els.elements || els).slice();
257 | return this;
258 | }
259 | };
260 |
261 | Sylvester.Vector.prototype.x = Sylvester.Vector.prototype.multiply;
262 | Sylvester.Vector.prototype.each = Sylvester.Vector.prototype.forEach;
263 |
264 | Sylvester.Vector.i = Sylvester.Vector.create([1,0,0]);
265 | Sylvester.Vector.j = Sylvester.Vector.create([0,1,0]);
266 | Sylvester.Vector.k = Sylvester.Vector.create([0,0,1]);
267 |
268 | Sylvester.Matrix = function() {};
269 |
270 | Sylvester.Matrix.create = function(elements) {
271 | var M = new Sylvester.Matrix();
272 | return M.setElements(elements);
273 | };
274 | var $M = Sylvester.Matrix.create;
275 |
276 | Sylvester.Matrix.I = function(n) {
277 | var els = [], i = n, j;
278 | while (i--) { j = n;
279 | els[i] = [];
280 | while (j--) {
281 | els[i][j] = (i === j) ? 1 : 0;
282 | }
283 | }
284 | return Sylvester.Matrix.create(els);
285 | };
286 |
287 | Sylvester.Matrix.Diagonal = function(elements) {
288 | var i = elements.length;
289 | var M = Sylvester.Matrix.I(i);
290 | while (i--) {
291 | M.elements[i][i] = elements[i];
292 | }
293 | return M;
294 | };
295 |
296 | Sylvester.Matrix.Rotation = function(theta, a) {
297 | if (!a) {
298 | return Sylvester.Matrix.create([
299 | [Math.cos(theta), -Math.sin(theta)],
300 | [Math.sin(theta), Math.cos(theta)]
301 | ]);
302 | }
303 | var axis = a.dup();
304 | if (axis.elements.length !== 3) { return null; }
305 | var mod = axis.modulus();
306 | var x = axis.elements[0]/mod, y = axis.elements[1]/mod, z = axis.elements[2]/mod;
307 | var s = Math.sin(theta), c = Math.cos(theta), t = 1 - c;
308 | // Formula derived here: http://www.gamedev.net/reference/articles/article1199.asp
309 | // That proof rotates the co-ordinate system so theta becomes -theta and sin
310 | // becomes -sin here.
311 | return Sylvester.Matrix.create([
312 | [ t*x*x + c, t*x*y - s*z, t*x*z + s*y ],
313 | [ t*x*y + s*z, t*y*y + c, t*y*z - s*x ],
314 | [ t*x*z - s*y, t*y*z + s*x, t*z*z + c ]
315 | ]);
316 | };
317 |
318 | Sylvester.Matrix.RotationX = function(t) {
319 | var c = Math.cos(t), s = Math.sin(t);
320 | return Sylvester.Matrix.create([
321 | [ 1, 0, 0 ],
322 | [ 0, c, -s ],
323 | [ 0, s, c ]
324 | ]);
325 | };
326 | Sylvester.Matrix.RotationY = function(t) {
327 | var c = Math.cos(t), s = Math.sin(t);
328 | return Sylvester.Matrix.create([
329 | [ c, 0, s ],
330 | [ 0, 1, 0 ],
331 | [ -s, 0, c ]
332 | ]);
333 | };
334 | Sylvester.Matrix.RotationZ = function(t) {
335 | var c = Math.cos(t), s = Math.sin(t);
336 | return Sylvester.Matrix.create([
337 | [ c, -s, 0 ],
338 | [ s, c, 0 ],
339 | [ 0, 0, 1 ]
340 | ]);
341 | };
342 |
343 | Sylvester.Matrix.Random = function(n, m) {
344 | return Sylvester.Matrix.Zero(n, m).map(
345 | function() { return Math.random(); }
346 | );
347 | };
348 |
349 | Sylvester.Matrix.Zero = function(n, m) {
350 | var els = [], i = n, j;
351 | while (i--) { j = m;
352 | els[i] = [];
353 | while (j--) {
354 | els[i][j] = 0;
355 | }
356 | }
357 | return Sylvester.Matrix.create(els);
358 | };
359 |
360 | Sylvester.Matrix.prototype = {
361 | e: function(i,j) {
362 | if (i < 1 || i > this.elements.length || j < 1 || j > this.elements[0].length) { return null; }
363 | return this.elements[i-1][j-1];
364 | },
365 |
366 | row: function(i) {
367 | if (i > this.elements.length) { return null; }
368 | return Sylvester.Vector.create(this.elements[i-1]);
369 | },
370 |
371 | col: function(j) {
372 | if (this.elements.length === 0) { return null; }
373 | if (j > this.elements[0].length) { return null; }
374 | var col = [], n = this.elements.length;
375 | for (var i = 0; i < n; i++) { col.push(this.elements[i][j-1]); }
376 | return Sylvester.Vector.create(col);
377 | },
378 |
379 | dimensions: function() {
380 | var cols = (this.elements.length === 0) ? 0 : this.elements[0].length;
381 | return {rows: this.elements.length, cols: cols};
382 | },
383 |
384 | rows: function() {
385 | return this.elements.length;
386 | },
387 |
388 | cols: function() {
389 | if (this.elements.length === 0) { return 0; }
390 | return this.elements[0].length;
391 | },
392 |
393 | eql: function(matrix) {
394 | var M = matrix.elements || matrix;
395 | if (!M[0] || typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
396 | if (this.elements.length === 0 || M.length === 0) {
397 | return this.elements.length === M.length;
398 | }
399 | if (this.elements.length !== M.length) { return false; }
400 | if (this.elements[0].length !== M[0].length) { return false; }
401 | var i = this.elements.length, nj = this.elements[0].length, j;
402 | while (i--) { j = nj;
403 | while (j--) {
404 | if (Math.abs(this.elements[i][j] - M[i][j]) > Sylvester.precision) { return false; }
405 | }
406 | }
407 | return true;
408 | },
409 |
410 | dup: function() {
411 | return Sylvester.Matrix.create(this.elements);
412 | },
413 |
414 | map: function(fn, context) {
415 | if (this.elements.length === 0) { return Sylvester.Matrix.create([]); }
416 | var els = [], i = this.elements.length, nj = this.elements[0].length, j;
417 | while (i--) { j = nj;
418 | els[i] = [];
419 | while (j--) {
420 | els[i][j] = fn.call(context, this.elements[i][j], i + 1, j + 1);
421 | }
422 | }
423 | return Sylvester.Matrix.create(els);
424 | },
425 |
426 | isSameSizeAs: function(matrix) {
427 | var M = matrix.elements || matrix;
428 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
429 | if (this.elements.length === 0) { return M.length === 0; }
430 | return (this.elements.length === M.length &&
431 | this.elements[0].length === M[0].length);
432 | },
433 |
434 | add: function(matrix) {
435 | if (this.elements.length === 0) return this.map(function(x) { return x });
436 | var M = matrix.elements || matrix;
437 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
438 | if (!this.isSameSizeAs(M)) { return null; }
439 | return this.map(function(x, i, j) { return x + M[i-1][j-1]; });
440 | },
441 |
442 | subtract: function(matrix) {
443 | if (this.elements.length === 0) return this.map(function(x) { return x });
444 | var M = matrix.elements || matrix;
445 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
446 | if (!this.isSameSizeAs(M)) { return null; }
447 | return this.map(function(x, i, j) { return x - M[i-1][j-1]; });
448 | },
449 |
450 | canMultiplyFromLeft: function(matrix) {
451 | if (this.elements.length === 0) { return false; }
452 | var M = matrix.elements || matrix;
453 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
454 | // this.columns should equal matrix.rows
455 | return (this.elements[0].length === M.length);
456 | },
457 |
458 | multiply: function(matrix) {
459 | if (this.elements.length === 0) { return null; }
460 | if (!matrix.elements) {
461 | return this.map(function(x) { return x * matrix; });
462 | }
463 | var returnVector = matrix.modulus ? true : false;
464 | var M = matrix.elements || matrix;
465 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
466 | if (!this.canMultiplyFromLeft(M)) { return null; }
467 | var i = this.elements.length, nj = M[0].length, j;
468 | var cols = this.elements[0].length, c, elements = [], sum;
469 | while (i--) { j = nj;
470 | elements[i] = [];
471 | while (j--) { c = cols;
472 | sum = 0;
473 | while (c--) {
474 | sum += this.elements[i][c] * M[c][j];
475 | }
476 | elements[i][j] = sum;
477 | }
478 | }
479 | var M = Sylvester.Matrix.create(elements);
480 | return returnVector ? M.col(1) : M;
481 | },
482 |
483 | minor: function(a, b, c, d) {
484 | if (this.elements.length === 0) { return null; }
485 | var elements = [], ni = c, i, nj, j;
486 | var rows = this.elements.length, cols = this.elements[0].length;
487 | while (ni--) { i = c - ni - 1;
488 | elements[i] = [];
489 | nj = d;
490 | while (nj--) { j = d - nj - 1;
491 | elements[i][j] = this.elements[(a+i-1)%rows][(b+j-1)%cols];
492 | }
493 | }
494 | return Sylvester.Matrix.create(elements);
495 | },
496 |
497 | transpose: function() {
498 | if (this.elements.length === 0) return Sylvester.Matrix.create([]);
499 | var rows = this.elements.length, i, cols = this.elements[0].length, j;
500 | var elements = [], i = cols;
501 | while (i--) { j = rows;
502 | elements[i] = [];
503 | while (j--) {
504 | elements[i][j] = this.elements[j][i];
505 | }
506 | }
507 | return Sylvester.Matrix.create(elements);
508 | },
509 |
510 | isSquare: function() {
511 | var cols = (this.elements.length === 0) ? 0 : this.elements[0].length;
512 | return (this.elements.length === cols);
513 | },
514 |
515 | max: function() {
516 | if (this.elements.length === 0) { return null; }
517 | var m = 0, i = this.elements.length, nj = this.elements[0].length, j;
518 | while (i--) { j = nj;
519 | while (j--) {
520 | if (Math.abs(this.elements[i][j]) > Math.abs(m)) { m = this.elements[i][j]; }
521 | }
522 | }
523 | return m;
524 | },
525 |
526 | indexOf: function(x) {
527 | if (this.elements.length === 0) { return null; }
528 | var index = null, ni = this.elements.length, i, nj = this.elements[0].length, j;
529 | for (i = 0; i < ni; i++) {
530 | for (j = 0; j < nj; j++) {
531 | if (this.elements[i][j] === x) { return {i: i+1, j: j+1}; }
532 | }
533 | }
534 | return null;
535 | },
536 |
537 | diagonal: function() {
538 | if (!this.isSquare) { return null; }
539 | var els = [], n = this.elements.length;
540 | for (var i = 0; i < n; i++) {
541 | els.push(this.elements[i][i]);
542 | }
543 | return Sylvester.Vector.create(els);
544 | },
545 |
546 | toRightTriangular: function() {
547 | if (this.elements.length === 0) return Sylvester.Matrix.create([]);
548 | var M = this.dup(), els;
549 | var n = this.elements.length, i, j, np = this.elements[0].length, p;
550 | for (i = 0; i < n; i++) {
551 | if (M.elements[i][i] === 0) {
552 | for (j = i + 1; j < n; j++) {
553 | if (M.elements[j][i] !== 0) {
554 | els = [];
555 | for (p = 0; p < np; p++) { els.push(M.elements[i][p] + M.elements[j][p]); }
556 | M.elements[i] = els;
557 | break;
558 | }
559 | }
560 | }
561 | if (M.elements[i][i] !== 0) {
562 | for (j = i + 1; j < n; j++) {
563 | var multiplier = M.elements[j][i] / M.elements[i][i];
564 | els = [];
565 | for (p = 0; p < np; p++) {
566 | // Elements with column numbers up to an including the number of the
567 | // row that we're subtracting can safely be set straight to zero,
568 | // since that's the point of this routine and it avoids having to
569 | // loop over and correct rounding errors later
570 | els.push(p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier);
571 | }
572 | M.elements[j] = els;
573 | }
574 | }
575 | }
576 | return M;
577 | },
578 |
579 | determinant: function() {
580 | if (this.elements.length === 0) { return 1; }
581 | if (!this.isSquare()) { return null; }
582 | var M = this.toRightTriangular();
583 | var det = M.elements[0][0], n = M.elements.length;
584 | for (var i = 1; i < n; i++) {
585 | det = det * M.elements[i][i];
586 | }
587 | return det;
588 | },
589 |
590 | isSingular: function() {
591 | return (this.isSquare() && this.determinant() === 0);
592 | },
593 |
594 | trace: function() {
595 | if (this.elements.length === 0) { return 0; }
596 | if (!this.isSquare()) { return null; }
597 | var tr = this.elements[0][0], n = this.elements.length;
598 | for (var i = 1; i < n; i++) {
599 | tr += this.elements[i][i];
600 | }
601 | return tr;
602 | },
603 |
604 | rank: function() {
605 | if (this.elements.length === 0) { return 0; }
606 | var M = this.toRightTriangular(), rank = 0;
607 | var i = this.elements.length, nj = this.elements[0].length, j;
608 | while (i--) { j = nj;
609 | while (j--) {
610 | if (Math.abs(M.elements[i][j]) > Sylvester.precision) { rank++; break; }
611 | }
612 | }
613 | return rank;
614 | },
615 |
616 | augment: function(matrix) {
617 | if (this.elements.length === 0) { return this.dup(); }
618 | var M = matrix.elements || matrix;
619 | if (typeof(M[0][0]) === 'undefined') { M = Sylvester.Matrix.create(M).elements; }
620 | var T = this.dup(), cols = T.elements[0].length;
621 | var i = T.elements.length, nj = M[0].length, j;
622 | if (i !== M.length) { return null; }
623 | while (i--) { j = nj;
624 | while (j--) {
625 | T.elements[i][cols + j] = M[i][j];
626 | }
627 | }
628 | return T;
629 | },
630 |
631 | inverse: function() {
632 | if (this.elements.length === 0) { return null; }
633 | if (!this.isSquare() || this.isSingular()) { return null; }
634 | var n = this.elements.length, i= n, j;
635 | var M = this.augment(Sylvester.Matrix.I(n)).toRightTriangular();
636 | var np = M.elements[0].length, p, els, divisor;
637 | var inverse_elements = [], new_element;
638 | // Sylvester.Matrix is non-singular so there will be no zeros on the
639 | // diagonal. Cycle through rows from last to first.
640 | while (i--) {
641 | // First, normalise diagonal elements to 1
642 | els = [];
643 | inverse_elements[i] = [];
644 | divisor = M.elements[i][i];
645 | for (p = 0; p < np; p++) {
646 | new_element = M.elements[i][p] / divisor;
647 | els.push(new_element);
648 | // Shuffle off the current row of the right hand side into the results
649 | // array as it will not be modified by later runs through this loop
650 | if (p >= n) { inverse_elements[i].push(new_element); }
651 | }
652 | M.elements[i] = els;
653 | // Then, subtract this row from those above it to give the identity matrix
654 | // on the left hand side
655 | j = i;
656 | while (j--) {
657 | els = [];
658 | for (p = 0; p < np; p++) {
659 | els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]);
660 | }
661 | M.elements[j] = els;
662 | }
663 | }
664 | return Sylvester.Matrix.create(inverse_elements);
665 | },
666 |
667 | round: function() {
668 | return this.map(function(x) { return Math.round(x); });
669 | },
670 |
671 | snapTo: function(x) {
672 | return this.map(function(p) {
673 | return (Math.abs(p - x) <= Sylvester.precision) ? x : p;
674 | });
675 | },
676 |
677 | inspect: function() {
678 | var matrix_rows = [];
679 | var n = this.elements.length;
680 | if (n === 0) return '[]';
681 | for (var i = 0; i < n; i++) {
682 | matrix_rows.push(Sylvester.Vector.create(this.elements[i]).inspect());
683 | }
684 | return matrix_rows.join('\n');
685 | },
686 |
687 | setElements: function(els) {
688 | var i, j, elements = els.elements || els;
689 | if (elements[0] && typeof(elements[0][0]) !== 'undefined') {
690 | i = elements.length;
691 | this.elements = [];
692 | while (i--) { j = elements[i].length;
693 | this.elements[i] = [];
694 | while (j--) {
695 | this.elements[i][j] = elements[i][j];
696 | }
697 | }
698 | return this;
699 | }
700 | var n = elements.length;
701 | this.elements = [];
702 | for (i = 0; i < n; i++) {
703 | this.elements.push([elements[i]]);
704 | }
705 | return this;
706 | }
707 | };
708 |
709 | Sylvester.Matrix.prototype.toUpperTriangular = Sylvester.Matrix.prototype.toRightTriangular;
710 | Sylvester.Matrix.prototype.det = Sylvester.Matrix.prototype.determinant;
711 | Sylvester.Matrix.prototype.tr = Sylvester.Matrix.prototype.trace;
712 | Sylvester.Matrix.prototype.rk = Sylvester.Matrix.prototype.rank;
713 | Sylvester.Matrix.prototype.inv = Sylvester.Matrix.prototype.inverse;
714 | Sylvester.Matrix.prototype.x = Sylvester.Matrix.prototype.multiply;
715 |
716 | Sylvester.Line = function() {};
717 |
718 | Sylvester.Line.prototype = {
719 | eql: function(line) {
720 | return (this.isParallelTo(line) && this.contains(line.anchor));
721 | },
722 |
723 | dup: function() {
724 | return Sylvester.Line.create(this.anchor, this.direction);
725 | },
726 |
727 | translate: function(vector) {
728 | var V = vector.elements || vector;
729 | return Sylvester.Line.create([
730 | this.anchor.elements[0] + V[0],
731 | this.anchor.elements[1] + V[1],
732 | this.anchor.elements[2] + (V[2] || 0)
733 | ], this.direction);
734 | },
735 |
736 | isParallelTo: function(obj) {
737 | if (obj.normal || (obj.start && obj.end)) { return obj.isParallelTo(this); }
738 | var theta = this.direction.angleFrom(obj.direction);
739 | return (Math.abs(theta) <= Sylvester.precision || Math.abs(theta - Math.PI) <= Sylvester.precision);
740 | },
741 |
742 | distanceFrom: function(obj) {
743 | if (obj.normal || (obj.start && obj.end)) { return obj.distanceFrom(this); }
744 | if (obj.direction) {
745 | // obj is a line
746 | if (this.isParallelTo(obj)) { return this.distanceFrom(obj.anchor); }
747 | var N = this.direction.cross(obj.direction).toUnitVector().elements;
748 | var A = this.anchor.elements, B = obj.anchor.elements;
749 | return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
750 | } else {
751 | // obj is a point
752 | var P = obj.elements || obj;
753 | var A = this.anchor.elements, D = this.direction.elements;
754 | var PA1 = P[0] - A[0], PA2 = P[1] - A[1], PA3 = (P[2] || 0) - A[2];
755 | var modPA = Math.sqrt(PA1*PA1 + PA2*PA2 + PA3*PA3);
756 | if (modPA === 0) return 0;
757 | // Assumes direction vector is normalized
758 | var cosTheta = (PA1 * D[0] + PA2 * D[1] + PA3 * D[2]) / modPA;
759 | var sin2 = 1 - cosTheta*cosTheta;
760 | return Math.abs(modPA * Math.sqrt(sin2 < 0 ? 0 : sin2));
761 | }
762 | },
763 |
764 | contains: function(obj) {
765 | if (obj.start && obj.end) { return this.contains(obj.start) && this.contains(obj.end); }
766 | var dist = this.distanceFrom(obj);
767 | return (dist !== null && dist <= Sylvester.precision);
768 | },
769 |
770 | positionOf: function(point) {
771 | if (!this.contains(point)) { return null; }
772 | var P = point.elements || point;
773 | var A = this.anchor.elements, D = this.direction.elements;
774 | return (P[0] - A[0]) * D[0] + (P[1] - A[1]) * D[1] + ((P[2] || 0) - A[2]) * D[2];
775 | },
776 |
777 | liesIn: function(plane) {
778 | return plane.contains(this);
779 | },
780 |
781 | intersects: function(obj) {
782 | if (obj.normal) { return obj.intersects(this); }
783 | return (!this.isParallelTo(obj) && this.distanceFrom(obj) <= Sylvester.precision);
784 | },
785 |
786 | intersectionWith: function(obj) {
787 | if (obj.normal || (obj.start && obj.end)) { return obj.intersectionWith(this); }
788 | if (!this.intersects(obj)) { return null; }
789 | var P = this.anchor.elements, X = this.direction.elements,
790 | Q = obj.anchor.elements, Y = obj.direction.elements;
791 | var X1 = X[0], X2 = X[1], X3 = X[2], Y1 = Y[0], Y2 = Y[1], Y3 = Y[2];
792 | var PsubQ1 = P[0] - Q[0], PsubQ2 = P[1] - Q[1], PsubQ3 = P[2] - Q[2];
793 | var XdotQsubP = - X1*PsubQ1 - X2*PsubQ2 - X3*PsubQ3;
794 | var YdotPsubQ = Y1*PsubQ1 + Y2*PsubQ2 + Y3*PsubQ3;
795 | var XdotX = X1*X1 + X2*X2 + X3*X3;
796 | var YdotY = Y1*Y1 + Y2*Y2 + Y3*Y3;
797 | var XdotY = X1*Y1 + X2*Y2 + X3*Y3;
798 | var k = (XdotQsubP * YdotY / XdotX + XdotY * YdotPsubQ) / (YdotY - XdotY * XdotY);
799 | return Sylvester.Vector.create([P[0] + k*X1, P[1] + k*X2, P[2] + k*X3]);
800 | },
801 |
802 | pointClosestTo: function(obj) {
803 | if (obj.start && obj.end) {
804 | // obj is a line segment
805 | var P = obj.pointClosestTo(this);
806 | return (P === null) ? null : this.pointClosestTo(P);
807 | } else if (obj.direction) {
808 | // obj is a line
809 | if (this.intersects(obj)) { return this.intersectionWith(obj); }
810 | if (this.isParallelTo(obj)) { return null; }
811 | var D = this.direction.elements, E = obj.direction.elements;
812 | var D1 = D[0], D2 = D[1], D3 = D[2], E1 = E[0], E2 = E[1], E3 = E[2];
813 | // Create plane containing obj and the shared normal and intersect this
814 | // with it Thank you:
815 | // http://www.cgafaq.info/wiki/Sylvester.Line-line_distance
816 | var x = (D3 * E1 - D1 * E3), y = (D1 * E2 - D2 * E1), z = (D2 * E3 - D3 * E2);
817 | var N = [x * E3 - y * E2, y * E1 - z * E3, z * E2 - x * E1];
818 | var P = Sylvester.Plane.create(obj.anchor, N);
819 | return P.intersectionWith(this);
820 | } else {
821 | // obj is a point
822 | var P = obj.elements || obj;
823 | if (this.contains(P)) { return Sylvester.Vector.create(P); }
824 | var A = this.anchor.elements, D = this.direction.elements;
825 | var D1 = D[0], D2 = D[1], D3 = D[2], A1 = A[0], A2 = A[1], A3 = A[2];
826 | var x = D1 * (P[1]-A2) - D2 * (P[0]-A1), y = D2 * ((P[2] || 0) - A3) - D3 * (P[1]-A2),
827 | z = D3 * (P[0]-A1) - D1 * ((P[2] || 0) - A3);
828 | var V = Sylvester.Vector.create([D2 * x - D3 * z, D3 * y - D1 * x, D1 * z - D2 * y]);
829 | var k = this.distanceFrom(P) / V.modulus();
830 | return Sylvester.Vector.create([
831 | P[0] + V.elements[0] * k,
832 | P[1] + V.elements[1] * k,
833 | (P[2] || 0) + V.elements[2] * k
834 | ]);
835 | }
836 | },
837 |
838 | // Returns a copy of the line rotated by t radians about the given line. Works
839 | // by finding the argument's closest point to this line's anchor point (call
840 | // this C) and rotating the anchor about C. Also rotates the line's direction
841 | // about the argument's. Be careful with this - the rotation axis' direction
842 | // affects the outcome!
843 | rotate: function(t, line) {
844 | // If we're working in 2D
845 | if (typeof(line.direction) === 'undefined') { line = Sylvester.Line.create(line.to3D(), Sylvester.Vector.k); }
846 | var R = Sylvester.Matrix.Rotation(t, line.direction).elements;
847 | var C = line.pointClosestTo(this.anchor).elements;
848 | var A = this.anchor.elements, D = this.direction.elements;
849 | var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2];
850 | var x = A1 - C1, y = A2 - C2, z = A3 - C3;
851 | return Sylvester.Line.create([
852 | C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z,
853 | C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z,
854 | C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z
855 | ], [
856 | R[0][0] * D[0] + R[0][1] * D[1] + R[0][2] * D[2],
857 | R[1][0] * D[0] + R[1][1] * D[1] + R[1][2] * D[2],
858 | R[2][0] * D[0] + R[2][1] * D[1] + R[2][2] * D[2]
859 | ]);
860 | },
861 |
862 | reverse: function() {
863 | return Sylvester.Line.create(this.anchor, this.direction.x(-1));
864 | },
865 |
866 | reflectionIn: function(obj) {
867 | if (obj.normal) {
868 | // obj is a plane
869 | var A = this.anchor.elements, D = this.direction.elements;
870 | var A1 = A[0], A2 = A[1], A3 = A[2], D1 = D[0], D2 = D[1], D3 = D[2];
871 | var newA = this.anchor.reflectionIn(obj).elements;
872 | // Add the line's direction vector to its anchor, then mirror that in the plane
873 | var AD1 = A1 + D1, AD2 = A2 + D2, AD3 = A3 + D3;
874 | var Q = obj.pointClosestTo([AD1, AD2, AD3]).elements;
875 | var newD = [Q[0] + (Q[0] - AD1) - newA[0], Q[1] + (Q[1] - AD2) - newA[1], Q[2] + (Q[2] - AD3) - newA[2]];
876 | return Sylvester.Line.create(newA, newD);
877 | } else if (obj.direction) {
878 | // obj is a line - reflection obtained by rotating PI radians about obj
879 | return this.rotate(Math.PI, obj);
880 | } else {
881 | // obj is a point - just reflect the line's anchor in it
882 | var P = obj.elements || obj;
883 | return Sylvester.Line.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.direction);
884 | }
885 | },
886 |
887 | setVectors: function(anchor, direction) {
888 | // Need to do this so that line's properties are not references to the
889 | // arguments passed in
890 | anchor = Sylvester.Vector.create(anchor);
891 | direction = Sylvester.Vector.create(direction);
892 | if (anchor.elements.length === 2) {anchor.elements.push(0); }
893 | if (direction.elements.length === 2) { direction.elements.push(0); }
894 | if (anchor.elements.length > 3 || direction.elements.length > 3) { return null; }
895 | var mod = direction.modulus();
896 | if (mod === 0) { return null; }
897 | this.anchor = anchor;
898 | this.direction = Sylvester.Vector.create([
899 | direction.elements[0] / mod,
900 | direction.elements[1] / mod,
901 | direction.elements[2] / mod
902 | ]);
903 | return this;
904 | }
905 | };
906 |
907 | Sylvester.Line.create = function(anchor, direction) {
908 | var L = new Sylvester.Line();
909 | return L.setVectors(anchor, direction);
910 | };
911 | var $L = Sylvester.Line.create;
912 |
913 | Sylvester.Line.X = Sylvester.Line.create(Sylvester.Vector.Zero(3), Sylvester.Vector.i);
914 | Sylvester.Line.Y = Sylvester.Line.create(Sylvester.Vector.Zero(3), Sylvester.Vector.j);
915 | Sylvester.Line.Z = Sylvester.Line.create(Sylvester.Vector.Zero(3), Sylvester.Vector.k);
916 |
917 | Sylvester.Line.Segment = function() {};
918 |
919 | Sylvester.Line.Segment.prototype = {
920 | eql: function(segment) {
921 | return (this.start.eql(segment.start) && this.end.eql(segment.end)) ||
922 | (this.start.eql(segment.end) && this.end.eql(segment.start));
923 | },
924 |
925 | dup: function() {
926 | return Sylvester.Line.Segment.create(this.start, this.end);
927 | },
928 |
929 | length: function() {
930 | var A = this.start.elements, B = this.end.elements;
931 | var C1 = B[0] - A[0], C2 = B[1] - A[1], C3 = B[2] - A[2];
932 | return Math.sqrt(C1*C1 + C2*C2 + C3*C3);
933 | },
934 |
935 | toVector: function() {
936 | var A = this.start.elements, B = this.end.elements;
937 | return Sylvester.Vector.create([B[0] - A[0], B[1] - A[1], B[2] - A[2]]);
938 | },
939 |
940 | midpoint: function() {
941 | var A = this.start.elements, B = this.end.elements;
942 | return Sylvester.Vector.create([(B[0] + A[0])/2, (B[1] + A[1])/2, (B[2] + A[2])/2]);
943 | },
944 |
945 | bisectingPlane: function() {
946 | return Sylvester.Plane.create(this.midpoint(), this.toVector());
947 | },
948 |
949 | translate: function(vector) {
950 | var V = vector.elements || vector;
951 | var S = this.start.elements, E = this.end.elements;
952 | return Sylvester.Line.Segment.create(
953 | [S[0] + V[0], S[1] + V[1], S[2] + (V[2] || 0)],
954 | [E[0] + V[0], E[1] + V[1], E[2] + (V[2] || 0)]
955 | );
956 | },
957 |
958 | isParallelTo: function(obj) {
959 | return this.line.isParallelTo(obj);
960 | },
961 |
962 | distanceFrom: function(obj) {
963 | var P = this.pointClosestTo(obj);
964 | return (P === null) ? null : P.distanceFrom(obj);
965 | },
966 |
967 | contains: function(obj) {
968 | if (obj.start && obj.end) { return this.contains(obj.start) && this.contains(obj.end); }
969 | var P = (obj.elements || obj).slice();
970 | if (P.length === 2) { P.push(0); }
971 | if (this.start.eql(P)) { return true; }
972 | var S = this.start.elements;
973 | var V = Sylvester.Vector.create([S[0] - P[0], S[1] - P[1], S[2] - (P[2] || 0)]);
974 | var vect = this.toVector();
975 | return V.isAntiparallelTo(vect) && V.modulus() <= vect.modulus();
976 | },
977 |
978 | intersects: function(obj) {
979 | return (this.intersectionWith(obj) !== null);
980 | },
981 |
982 | intersectionWith: function(obj) {
983 | if (!this.line.intersects(obj)) { return null; }
984 | var P = this.line.intersectionWith(obj);
985 | return (this.contains(P) ? P : null);
986 | },
987 |
988 | pointClosestTo: function(obj) {
989 | if (obj.normal) {
990 | // obj is a plane
991 | var V = this.line.intersectionWith(obj);
992 | if (V === null) { return null; }
993 | return this.pointClosestTo(V);
994 | } else {
995 | // obj is a line (segment) or point
996 | var P = this.line.pointClosestTo(obj);
997 | if (P === null) { return null; }
998 | if (this.contains(P)) { return P; }
999 | return (this.line.positionOf(P) < 0 ? this.start : this.end).dup();
1000 | }
1001 | },
1002 |
1003 | setPoints: function(startPoint, endPoint) {
1004 | startPoint = Sylvester.Vector.create(startPoint).to3D();
1005 | endPoint = Sylvester.Vector.create(endPoint).to3D();
1006 | if (startPoint === null || endPoint === null) { return null; }
1007 | this.line = Sylvester.Line.create(startPoint, endPoint.subtract(startPoint));
1008 | this.start = startPoint;
1009 | this.end = endPoint;
1010 | return this;
1011 | }
1012 | };
1013 |
1014 | Sylvester.Line.Segment.create = function(v1, v2) {
1015 | var S = new Sylvester.Line.Segment();
1016 | return S.setPoints(v1, v2);
1017 | };
1018 |
1019 | Sylvester.Plane = function() {};
1020 |
1021 | Sylvester.Plane.prototype = {
1022 | eql: function(plane) {
1023 | return (this.contains(plane.anchor) && this.isParallelTo(plane));
1024 | },
1025 |
1026 | dup: function() {
1027 | return Sylvester.Plane.create(this.anchor, this.normal);
1028 | },
1029 |
1030 | translate: function(vector) {
1031 | var V = vector.elements || vector;
1032 | return Sylvester.Plane.create([
1033 | this.anchor.elements[0] + V[0],
1034 | this.anchor.elements[1] + V[1],
1035 | this.anchor.elements[2] + (V[2] || 0)
1036 | ], this.normal);
1037 | },
1038 |
1039 | isParallelTo: function(obj) {
1040 | var theta;
1041 | if (obj.normal) {
1042 | // obj is a plane
1043 | theta = this.normal.angleFrom(obj.normal);
1044 | return (Math.abs(theta) <= Sylvester.precision || Math.abs(Math.PI - theta) <= Sylvester.precision);
1045 | } else if (obj.direction) {
1046 | // obj is a line
1047 | return this.normal.isPerpendicularTo(obj.direction);
1048 | }
1049 | return null;
1050 | },
1051 |
1052 | isPerpendicularTo: function(plane) {
1053 | var theta = this.normal.angleFrom(plane.normal);
1054 | return (Math.abs(Math.PI/2 - theta) <= Sylvester.precision);
1055 | },
1056 |
1057 | distanceFrom: function(obj) {
1058 | if (this.intersects(obj) || this.contains(obj)) { return 0; }
1059 | if (obj.anchor) {
1060 | // obj is a plane or line
1061 | var A = this.anchor.elements, B = obj.anchor.elements, N = this.normal.elements;
1062 | return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
1063 | } else {
1064 | // obj is a point
1065 | var P = obj.elements || obj;
1066 | var A = this.anchor.elements, N = this.normal.elements;
1067 | return Math.abs((A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2]);
1068 | }
1069 | },
1070 |
1071 | contains: function(obj) {
1072 | if (obj.normal) { return null; }
1073 | if (obj.direction) {
1074 | return (this.contains(obj.anchor) && this.contains(obj.anchor.add(obj.direction)));
1075 | } else {
1076 | var P = obj.elements || obj;
1077 | var A = this.anchor.elements, N = this.normal.elements;
1078 | var diff = Math.abs(N[0]*(A[0] - P[0]) + N[1]*(A[1] - P[1]) + N[2]*(A[2] - (P[2] || 0)));
1079 | return (diff <= Sylvester.precision);
1080 | }
1081 | },
1082 |
1083 | intersects: function(obj) {
1084 | if (typeof(obj.direction) === 'undefined' && typeof(obj.normal) === 'undefined') { return null; }
1085 | return !this.isParallelTo(obj);
1086 | },
1087 |
1088 | intersectionWith: function(obj) {
1089 | if (!this.intersects(obj)) { return null; }
1090 | if (obj.direction) {
1091 | // obj is a line
1092 | var A = obj.anchor.elements, D = obj.direction.elements,
1093 | P = this.anchor.elements, N = this.normal.elements;
1094 | var multiplier = (N[0]*(P[0]-A[0]) + N[1]*(P[1]-A[1]) + N[2]*(P[2]-A[2])) / (N[0]*D[0] + N[1]*D[1] + N[2]*D[2]);
1095 | return Sylvester.Vector.create([A[0] + D[0]*multiplier, A[1] + D[1]*multiplier, A[2] + D[2]*multiplier]);
1096 | } else if (obj.normal) {
1097 | // obj is a plane
1098 | var direction = this.normal.cross(obj.normal).toUnitVector();
1099 | // To find an anchor point, we find one co-ordinate that has a value of
1100 | // zero somewhere on the intersection, and remember which one we picked
1101 | var N = this.normal.elements, A = this.anchor.elements,
1102 | O = obj.normal.elements, B = obj.anchor.elements;
1103 | var solver = Sylvester.Matrix.Zero(2,2), i = 0;
1104 | while (solver.isSingular()) {
1105 | i++;
1106 | solver = Sylvester.Matrix.create([
1107 | [ N[i%3], N[(i+1)%3] ],
1108 | [ O[i%3], O[(i+1)%3] ]
1109 | ]);
1110 | }
1111 | // Then we solve the simultaneous equations in the remaining dimensions
1112 | var inverse = solver.inverse().elements;
1113 | var x = N[0]*A[0] + N[1]*A[1] + N[2]*A[2];
1114 | var y = O[0]*B[0] + O[1]*B[1] + O[2]*B[2];
1115 | var intersection = [
1116 | inverse[0][0] * x + inverse[0][1] * y,
1117 | inverse[1][0] * x + inverse[1][1] * y
1118 | ];
1119 | var anchor = [];
1120 | for (var j = 1; j <= 3; j++) {
1121 | // This formula picks the right element from intersection by cycling
1122 | // depending on which element we set to zero above
1123 | anchor.push((i === j) ? 0 : intersection[(j + (5 - i)%3)%3]);
1124 | }
1125 | return Sylvester.Line.create(anchor, direction);
1126 | }
1127 | },
1128 |
1129 | pointClosestTo: function(point) {
1130 | var P = point.elements || point;
1131 | var A = this.anchor.elements, N = this.normal.elements;
1132 | var dot = (A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2];
1133 | return Sylvester.Vector.create([P[0] + N[0] * dot, P[1] + N[1] * dot, (P[2] || 0) + N[2] * dot]);
1134 | },
1135 |
1136 | rotate: function(t, line) {
1137 | var R = t.determinant ? t.elements : Sylvester.Matrix.Rotation(t, line.direction).elements;
1138 | var C = line.pointClosestTo(this.anchor).elements;
1139 | var A = this.anchor.elements, N = this.normal.elements;
1140 | var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2];
1141 | var x = A1 - C1, y = A2 - C2, z = A3 - C3;
1142 | return Sylvester.Plane.create([
1143 | C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z,
1144 | C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z,
1145 | C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z
1146 | ], [
1147 | R[0][0] * N[0] + R[0][1] * N[1] + R[0][2] * N[2],
1148 | R[1][0] * N[0] + R[1][1] * N[1] + R[1][2] * N[2],
1149 | R[2][0] * N[0] + R[2][1] * N[1] + R[2][2] * N[2]
1150 | ]);
1151 | },
1152 |
1153 | reflectionIn: function(obj) {
1154 | if (obj.normal) {
1155 | // obj is a plane
1156 | var A = this.anchor.elements, N = this.normal.elements;
1157 | var A1 = A[0], A2 = A[1], A3 = A[2], N1 = N[0], N2 = N[1], N3 = N[2];
1158 | var newA = this.anchor.reflectionIn(obj).elements;
1159 | // Add the plane's normal to its anchor, then mirror that in the other plane
1160 | var AN1 = A1 + N1, AN2 = A2 + N2, AN3 = A3 + N3;
1161 | var Q = obj.pointClosestTo([AN1, AN2, AN3]).elements;
1162 | var newN = [Q[0] + (Q[0] - AN1) - newA[0], Q[1] + (Q[1] - AN2) - newA[1], Q[2] + (Q[2] - AN3) - newA[2]];
1163 | return Sylvester.Plane.create(newA, newN);
1164 | } else if (obj.direction) {
1165 | // obj is a line
1166 | return this.rotate(Math.PI, obj);
1167 | } else {
1168 | // obj is a point
1169 | var P = obj.elements || obj;
1170 | return Sylvester.Plane.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.normal);
1171 | }
1172 | },
1173 |
1174 | setVectors: function(anchor, v1, v2) {
1175 | anchor = Sylvester.Vector.create(anchor);
1176 | anchor = anchor.to3D(); if (anchor === null) { return null; }
1177 | v1 = Sylvester.Vector.create(v1);
1178 | v1 = v1.to3D(); if (v1 === null) { return null; }
1179 | if (typeof(v2) === 'undefined') {
1180 | v2 = null;
1181 | } else {
1182 | v2 = Sylvester.Vector.create(v2);
1183 | v2 = v2.to3D(); if (v2 === null) { return null; }
1184 | }
1185 | var A1 = anchor.elements[0], A2 = anchor.elements[1], A3 = anchor.elements[2];
1186 | var v11 = v1.elements[0], v12 = v1.elements[1], v13 = v1.elements[2];
1187 | var normal, mod;
1188 | if (v2 !== null) {
1189 | var v21 = v2.elements[0], v22 = v2.elements[1], v23 = v2.elements[2];
1190 | normal = Sylvester.Vector.create([
1191 | (v12 - A2) * (v23 - A3) - (v13 - A3) * (v22 - A2),
1192 | (v13 - A3) * (v21 - A1) - (v11 - A1) * (v23 - A3),
1193 | (v11 - A1) * (v22 - A2) - (v12 - A2) * (v21 - A1)
1194 | ]);
1195 | mod = normal.modulus();
1196 | if (mod === 0) { return null; }
1197 | normal = Sylvester.Vector.create([normal.elements[0] / mod, normal.elements[1] / mod, normal.elements[2] / mod]);
1198 | } else {
1199 | mod = Math.sqrt(v11*v11 + v12*v12 + v13*v13);
1200 | if (mod === 0) { return null; }
1201 | normal = Sylvester.Vector.create([v1.elements[0] / mod, v1.elements[1] / mod, v1.elements[2] / mod]);
1202 | }
1203 | this.anchor = anchor;
1204 | this.normal = normal;
1205 | return this;
1206 | }
1207 | };
1208 |
1209 | Sylvester.Plane.create = function(anchor, v1, v2) {
1210 | var P = new Sylvester.Plane();
1211 | return P.setVectors(anchor, v1, v2);
1212 | };
1213 | var $P = Sylvester.Plane.create;
1214 |
1215 | Sylvester.Plane.XY = Sylvester.Plane.create(Sylvester.Vector.Zero(3), Sylvester.Vector.k);
1216 | Sylvester.Plane.YZ = Sylvester.Plane.create(Sylvester.Vector.Zero(3), Sylvester.Vector.i);
1217 | Sylvester.Plane.ZX = Sylvester.Plane.create(Sylvester.Vector.Zero(3), Sylvester.Vector.j);
1218 | Sylvester.Plane.YX = Sylvester.Plane.XY; Sylvester.Plane.ZY = Sylvester.Plane.YZ; Sylvester.Plane.XZ = Sylvester.Plane.ZX;
1219 |
1220 | Sylvester.Plane.fromPoints = function(points) {
1221 | var np = points.length, list = [], i, P, n, N, A, B, C, D, theta, prevN, totalN = Sylvester.Vector.Zero(3);
1222 | for (i = 0; i < np; i++) {
1223 | P = Sylvester.Vector.create(points[i]).to3D();
1224 | if (P === null) { return null; }
1225 | list.push(P);
1226 | n = list.length;
1227 | if (n > 2) {
1228 | // Compute plane normal for the latest three points
1229 | A = list[n-1].elements; B = list[n-2].elements; C = list[n-3].elements;
1230 | N = Sylvester.Vector.create([
1231 | (A[1] - B[1]) * (C[2] - B[2]) - (A[2] - B[2]) * (C[1] - B[1]),
1232 | (A[2] - B[2]) * (C[0] - B[0]) - (A[0] - B[0]) * (C[2] - B[2]),
1233 | (A[0] - B[0]) * (C[1] - B[1]) - (A[1] - B[1]) * (C[0] - B[0])
1234 | ]).toUnitVector();
1235 | if (n > 3) {
1236 | // If the latest normal is not (anti)parallel to the previous one, we've
1237 | // strayed off the plane. This might be a slightly long-winded way of
1238 | // doing things, but we need the sum of all the normals to find which
1239 | // way the plane normal should point so that the points form an
1240 | // anticlockwise list.
1241 | theta = N.angleFrom(prevN);
1242 | if (theta !== null) {
1243 | if (!(Math.abs(theta) <= Sylvester.precision || Math.abs(theta - Math.PI) <= Sylvester.precision)) { return null; }
1244 | }
1245 | }
1246 | totalN = totalN.add(N);
1247 | prevN = N;
1248 | }
1249 | }
1250 | // We need to add in the normals at the start and end points, which the above
1251 | // misses out
1252 | A = list[1].elements; B = list[0].elements; C = list[n-1].elements; D = list[n-2].elements;
1253 | totalN = totalN.add(Sylvester.Vector.create([
1254 | (A[1] - B[1]) * (C[2] - B[2]) - (A[2] - B[2]) * (C[1] - B[1]),
1255 | (A[2] - B[2]) * (C[0] - B[0]) - (A[0] - B[0]) * (C[2] - B[2]),
1256 | (A[0] - B[0]) * (C[1] - B[1]) - (A[1] - B[1]) * (C[0] - B[0])
1257 | ]).toUnitVector()).add(Sylvester.Vector.create([
1258 | (B[1] - C[1]) * (D[2] - C[2]) - (B[2] - C[2]) * (D[1] - C[1]),
1259 | (B[2] - C[2]) * (D[0] - C[0]) - (B[0] - C[0]) * (D[2] - C[2]),
1260 | (B[0] - C[0]) * (D[1] - C[1]) - (B[1] - C[1]) * (D[0] - C[0])
1261 | ]).toUnitVector());
1262 | return Sylvester.Plane.create(list[0], totalN);
1263 | };
1264 |
1265 | Sylvester.Polygon = function() {};
1266 |
1267 | Sylvester.Polygon.prototype = {
1268 | v: function(i) {
1269 | return this.vertices.at(i - 1).data;
1270 | },
1271 |
1272 | nodeFor: function(vertex) {
1273 | return this.vertices.withData(vertex);
1274 | },
1275 |
1276 | dup: function() {
1277 | return Sylvester.Polygon.create(this.vertices, this.plane);
1278 | },
1279 |
1280 | translate: function(vector) {
1281 | var P = vector.elements || vector;
1282 | this.vertices.each(function(node) {
1283 | var E = node.data.elements;
1284 | node.data.setElements([E[0] + P[0], E[1] + P[1], E[2] + (P[2] || 0)]);
1285 | });
1286 | this.plane = this.plane.translate(vector);
1287 | this.updateTrianglePlanes(function(plane) { return plane.translate(vector); });
1288 | return this;
1289 | },
1290 |
1291 | rotate: function(t, line) {
1292 | var R = Sylvester.Matrix.Rotation(t, line.direction);
1293 | this.vertices.each(function(node) {
1294 | node.data.setElements(node.data.rotate(R, line).elements);
1295 | });
1296 | this.plane = this.plane.rotate(R, line);
1297 | this.updateTrianglePlanes(function(plane) { return plane.rotate(R, line); });
1298 | return this;
1299 | },
1300 |
1301 | scale: function(k, point) {
1302 | var P = point.elements || point;
1303 | this.vertices.each(function(node) {
1304 | var E = node.data.elements;
1305 | node.data.setElements([
1306 | P[0] + k * (E[0] - P[0]),
1307 | P[1] + k * (E[1] - P[1]),
1308 | (P[2] || 0) + k * (E[2] - (P[2] || 0))
1309 | ]);
1310 | });
1311 | var anchor = this.vertices.first.data;
1312 | this.plane.anchor.setElements(anchor);
1313 | this.updateTrianglePlanes(function(plane) { return Sylvester.Plane.create(anchor, plane.normal); });
1314 | return this;
1315 | },
1316 |
1317 | // Updates the plane properties of all the cached triangles belonging to the
1318 | // polygon according to the given function. For example, suppose you just
1319 | // rotated the polygon, you should call:
1320 | //
1321 | // poly.updateTrianglePlanes(function(plane) { return plane.rotate(t, line); });
1322 | //
1323 | // This method is called automatically by Sylvester.Polygon.translate,
1324 | // Sylvester.Polygon.rotate and Sylvester.Polygon.scale transformation methods.
1325 | updateTrianglePlanes: function(fn) {
1326 | var i;
1327 | if (this.cached.triangles !== null) {
1328 | i = this.cached.triangles.length;
1329 | while (i--) {
1330 | this.cached.triangles[i].plane = fn(this.cached.triangles[i].plane);
1331 | }
1332 | }
1333 | if (this.cached.surfaceIntegralElements !== null) {
1334 | i = this.cached.surfaceIntegralElements.length;
1335 | while (i--) {
1336 | this.cached.surfaceIntegralElements[i].plane = fn(this.cached.surfaceIntegralElements[i].plane);
1337 | }
1338 | }
1339 | },
1340 |
1341 | isTriangle: function() {
1342 | return this.vertices.length === 3;
1343 | },
1344 |
1345 | // Returns a collection of triangles used for calculating area and center of
1346 | // mass. Some of the triangles will not lie inside the polygon - this
1347 | // collection is essentially a series of itervals in a surface integral, so
1348 | // some are 'negative'. If you want the polygon broken into constituent
1349 | // triangles, use toTriangles(). This method is used because it's much faster
1350 | // than toTriangles().
1351 | //
1352 | // The triangles generated share vertices with the original polygon, so they
1353 | // transform with the polygon. They are cached after first calculation and
1354 | // should remain in sync with changes to the parent polygon.
1355 | trianglesForSurfaceIntegral: function() {
1356 | if (this.cached.surfaceIntegralElements !== null) { return this.cached.surfaceIntegralElements; }
1357 | var triangles = [];
1358 | var firstVertex = this.vertices.first.data;
1359 | var plane = this.plane;
1360 | this.vertices.each(function(node, i) {
1361 | if (i < 2) { return; }
1362 | var points = [firstVertex, node.prev.data, node.data];
1363 | // If the vertices lie on a straigh line, give the polygon's own plane. If
1364 | // the element has no area, it doesn't matter which way its normal faces.
1365 | triangles.push(Sylvester.Polygon.create(points, Sylvester.Plane.fromPoints(points) || plane));
1366 | });
1367 | return this.setCache('surfaceIntegralElements', triangles);
1368 | },
1369 |
1370 | area: function() {
1371 | if (this.isTriangle()) {
1372 | // Area is half the modulus of the cross product of two sides
1373 | var A = this.vertices.first, B = A.next, C = B.next;
1374 | A = A.data.elements; B = B.data.elements; C = C.data.elements;
1375 | return 0.5 * Sylvester.Vector.create([
1376 | (A[1] - B[1]) * (C[2] - B[2]) - (A[2] - B[2]) * (C[1] - B[1]),
1377 | (A[2] - B[2]) * (C[0] - B[0]) - (A[0] - B[0]) * (C[2] - B[2]),
1378 | (A[0] - B[0]) * (C[1] - B[1]) - (A[1] - B[1]) * (C[0] - B[0])
1379 | ]).modulus();
1380 | } else {
1381 | var trigs = this.trianglesForSurfaceIntegral(), area = 0;
1382 | var i = trigs.length;
1383 | while (i--) {
1384 | area += trigs[i].area() * trigs[i].plane.normal.dot(this.plane.normal);
1385 | }
1386 | return area;
1387 | }
1388 | },
1389 |
1390 | centroid: function() {
1391 | if (this.isTriangle()) {
1392 | var A = this.v(1).elements, B = this.v(2).elements, C = this.v(3).elements;
1393 | return Sylvester.Vector.create([(A[0] + B[0] + C[0])/3, (A[1] + B[1] + C[1])/3, (A[2] + B[2] + C[2])/3]);
1394 | } else {
1395 | var A, M = 0, V = Sylvester.Vector.Zero(3), P, C, trigs = this.trianglesForSurfaceIntegral();
1396 | var i = trigs.length;
1397 | while (i--) {
1398 | A = trigs[i].area() * trigs[i].plane.normal.dot(this.plane.normal);
1399 | M += A;
1400 | P = V.elements;
1401 | C = trigs[i].centroid().elements;
1402 | V.setElements([P[0] + C[0] * A, P[1] + C[1] * A, P[2] + C[2] * A]);
1403 | }
1404 | return V.x(1/M);
1405 | }
1406 | },
1407 |
1408 | projectionOn: function(plane) {
1409 | var points = [];
1410 | this.vertices.each(function(node) { points.push(plane.pointClosestTo(node.data)); });
1411 | return Sylvester.Polygon.create(points);
1412 | },
1413 |
1414 | removeVertex: function(vertex) {
1415 | if (this.isTriangle()) { return; }
1416 | var node = this.nodeFor(vertex);
1417 | if (node === null) { return null; }
1418 | this.clearCache();
1419 | // Previous and next entries in the main vertex list
1420 | var prev = node.prev, next = node.next;
1421 | var prevWasConvex = prev.data.isConvex(this);
1422 | var nextWasConvex = next.data.isConvex(this);
1423 | if (node.data.isConvex(this)) {
1424 | this.convexVertices.remove(this.convexVertices.withData(node.data));
1425 | } else {
1426 | this.reflexVertices.remove(this.reflexVertices.withData(node.data));
1427 | }
1428 | this.vertices.remove(node);
1429 | // Deal with previous vertex's change of class
1430 | if (prevWasConvex !== prev.data.isConvex(this)) {
1431 | if (prevWasConvex) {
1432 | this.convexVertices.remove(this.convexVertices.withData(prev.data));
1433 | this.reflexVertices.append(new Sylvester.LinkedList.Node(prev.data));
1434 | } else {
1435 | this.reflexVertices.remove(this.reflexVertices.withData(prev.data));
1436 | this.convexVertices.append(new Sylvester.LinkedList.Node(prev.data));
1437 | }
1438 | }
1439 | // Deal with next vertex's change of class
1440 | if (nextWasConvex !== next.data.isConvex(this)) {
1441 | if (nextWasConvex) {
1442 | this.convexVertices.remove(this.convexVertices.withData(next.data));
1443 | this.reflexVertices.append(new Sylvester.LinkedList.Node(next.data));
1444 | } else {
1445 | this.reflexVertices.remove(this.reflexVertices.withData(next.data));
1446 | this.convexVertices.append(new Sylvester.LinkedList.Node(next.data));
1447 | }
1448 | }
1449 | return this;
1450 | },
1451 |
1452 | contains: function(point) {
1453 | return this.containsByWindingNumber(point);
1454 | },
1455 |
1456 | containsByWindingNumber: function(point) {
1457 | var P = point.elements || point;
1458 | if (!this.plane.contains(P)) { return false; }
1459 | if (this.hasEdgeContaining(P)) { return false; }
1460 | var V, W, A, B, theta = 0, dt, loops = 0, self = this;
1461 | this.vertices.each(function(node) {
1462 | V = node.data.elements;
1463 | W = node.next.data.elements;
1464 | A = Sylvester.Vector.create([V[0] - P[0], V[1] - P[1], V[2] - (P[2] || 0)]);
1465 | B = Sylvester.Vector.create([W[0] - P[0], W[1] - P[1], W[2] - (P[2] || 0)]);
1466 | dt = A.angleFrom(B);
1467 | if (dt === null || dt === 0) { return; }
1468 | theta += (A.cross(B).isParallelTo(self.plane.normal) ? 1 : -1) * dt;
1469 | if (theta >= 2 * Math.PI - Sylvester.precision) { loops++; theta -= 2 * Math.PI; }
1470 | if (theta <= -2 * Math.PI + Sylvester.precision) { loops--; theta += 2 * Math.PI; }
1471 | });
1472 | return loops !== 0;
1473 | },
1474 |
1475 | hasEdgeContaining: function(point) {
1476 | var P = (point.elements || point);
1477 | var success = false;
1478 | this.vertices.each(function(node) {
1479 | if (Sylvester.Line.Segment.create(node.data, node.next.data).contains(P)) { success = true; }
1480 | });
1481 | return success;
1482 | },
1483 |
1484 | toTriangles: function() {
1485 | if (this.cached.triangles !== null) { return this.cached.triangles; }
1486 | return this.setCache('triangles', this.triangulateByEarClipping());
1487 | },
1488 |
1489 | // Implementation of ear clipping algorithm
1490 | // Found in 'Triangulation by ear clipping', by David Eberly
1491 | // at http://www.geometrictools.com
1492 | // This will not deal with overlapping sections - contruct your polygons
1493 | // sensibly
1494 | triangulateByEarClipping: function() {
1495 | var poly = this.dup(), triangles = [], success, convexNode, mainNode, trig;
1496 | while (!poly.isTriangle()) {
1497 | success = false;
1498 | while (!success) {
1499 | success = true;
1500 | // Ear tips must be convex vertices - let's pick one at random
1501 | convexNode = poly.convexVertices.randomNode();
1502 | mainNode = poly.vertices.withData(convexNode.data);
1503 | // For convex vertices, this order will always be anticlockwise
1504 | trig = Sylvester.Polygon.create([mainNode.data, mainNode.next.data, mainNode.prev.data], this.plane);
1505 | // Now test whether any reflex vertices lie within the ear
1506 | poly.reflexVertices.each(function(node) {
1507 | // Don't test points belonging to this triangle. node won't be equal
1508 | // to convexNode as node is reflex and vertex is convex.
1509 | if (node.data !== mainNode.prev.data && node.data !== mainNode.next.data) {
1510 | if (trig.contains(node.data) || trig.hasEdgeContaining(node.data)) { success = false; }
1511 | }
1512 | });
1513 | }
1514 | triangles.push(trig);
1515 | poly.removeVertex(mainNode.data);
1516 | }
1517 | // Need to do this to renumber the remaining vertices
1518 | triangles.push(Sylvester.Polygon.create(poly.vertices, this.plane));
1519 | return triangles;
1520 | },
1521 |
1522 | setVertices: function(points, plane) {
1523 | var pointSet = points.toArray ? points.toArray() : points;
1524 | this.plane = (plane && plane.normal) ? plane.dup() : Sylvester.Plane.fromPoints(pointSet);
1525 | if (this.plane === null) { return null; }
1526 | this.vertices = new Sylvester.LinkedList.Circular();
1527 | // Construct linked list of vertices. If each point is already a polygon
1528 | // vertex, we reference it rather than creating a new vertex.
1529 | var i = pointSet.length, newVertex;
1530 | while (i--) {
1531 | newVertex = pointSet[i].isConvex ? pointSet[i] : new Sylvester.Polygon.Vertex(pointSet[i]);
1532 | this.vertices.prepend(new Sylvester.LinkedList.Node(newVertex));
1533 | }
1534 | this.clearCache();
1535 | this.populateVertexTypeLists();
1536 | return this;
1537 | },
1538 |
1539 | populateVertexTypeLists: function() {
1540 | this.convexVertices = new Sylvester.LinkedList.Circular();
1541 | this.reflexVertices = new Sylvester.LinkedList.Circular();
1542 | var self = this;
1543 | this.vertices.each(function(node) {
1544 | // Split vertices into convex / reflex groups. The
1545 | // Sylvester.LinkedList.Node class wraps each vertex so it can belong to
1546 | // many linked lists.
1547 | self[node.data.type(self) + 'Vertices'].append(new Sylvester.LinkedList.Node(node.data));
1548 | });
1549 | },
1550 |
1551 | copyVertices: function() {
1552 | this.clearCache();
1553 | this.vertices.each(function(node) {
1554 | node.data = new Sylvester.Polygon.Vertex(node.data);
1555 | });
1556 | this.populateVertexTypeLists();
1557 | },
1558 |
1559 | clearCache: function() {
1560 | this.cached = {
1561 | triangles: null,
1562 | surfaceIntegralElements: null
1563 | };
1564 | },
1565 |
1566 | setCache: function(key, value) {
1567 | this.cached[key] = value;
1568 | return value;
1569 | },
1570 |
1571 | inspect: function() {
1572 | var points = [];
1573 | this.vertices.each(function(node) { points.push(node.data.inspect()); });
1574 | return points.join(' -> ');
1575 | }
1576 | };
1577 |
1578 | Sylvester.Polygon.create = function(points, plane) {
1579 | var P = new Sylvester.Polygon();
1580 | return P.setVertices(points, plane);
1581 | };
1582 |
1583 | Sylvester.Polygon.Vertex = function(point) {
1584 | this.setElements(point);
1585 | if (this.elements.length === 2) { this.elements.push(0); }
1586 | if (this.elements.length !== 3) { return null; }
1587 | };
1588 | Sylvester.Polygon.Vertex.prototype = new Sylvester.Vector;
1589 |
1590 | // Returns true iff the vertex's internal angle is 0 <= x < 180
1591 | // in the context of the given polygon object. Returns null if the
1592 | // vertex does not exist in the polygon.
1593 | Sylvester.Polygon.Vertex.prototype.isConvex = function(polygon) {
1594 | var node = polygon.nodeFor(this);
1595 | if (node === null) { return null; }
1596 | var prev = node.prev.data, next = node.next.data;
1597 | var A = next.subtract(this);
1598 | var B = prev.subtract(this);
1599 | var theta = A.angleFrom(B);
1600 | if (theta <= Sylvester.precision) { return true; }
1601 | if (Math.abs(theta - Math.PI) <= Sylvester.precision) { return false; }
1602 | return (A.cross(B).dot(polygon.plane.normal) > 0);
1603 | };
1604 | // Returns true iff the vertex's internal angle is 180 <= x < 360
1605 | Sylvester.Polygon.Vertex.prototype.isReflex = function(polygon) {
1606 | var result = this.isConvex(polygon);
1607 | return (result === null) ? null : !result;
1608 | };
1609 | Sylvester.Polygon.Vertex.prototype.type = function(polygon) {
1610 | var result = this.isConvex(polygon);
1611 | return (result === null) ? null : (result ? 'convex' : 'reflex');
1612 | };
1613 |
1614 | // Method for converting a set of arrays/vectors/whatever to a set of Sylvester.Polygon.Vertex objects
1615 | Sylvester.Polygon.Vertex.convert = function(points) {
1616 | var pointSet = points.toArray ? points.toArray() : points;
1617 | var list = [], n = pointSet.length;
1618 | for (var i = 0; i < n; i++) {
1619 | list.push(new Sylvester.Polygon.Vertex(pointSet[i]));
1620 | }
1621 | return list;
1622 | };
1623 |
1624 | Sylvester.LinkedList = function() {};
1625 |
1626 | Sylvester.LinkedList.prototype = {
1627 | length: 0,
1628 | first: null,
1629 | last: null,
1630 |
1631 | forEach: function(fn, context) {
1632 | var node = this.first, n = this.length;
1633 | for (var i = 0; i < n; i++) {
1634 | fn.call(context, node, i);
1635 | node = node.next;
1636 | }
1637 | },
1638 |
1639 | at: function(i) {
1640 | if (!(i >= 0 && i < this.length)) { return null; }
1641 | var node = this.first;
1642 | while (i--) { node = node.next; }
1643 | return node;
1644 | },
1645 |
1646 | randomNode: function() {
1647 | var n = Math.floor(Math.random() * this.length);
1648 | return this.at(n);
1649 | },
1650 |
1651 | toArray: function() {
1652 | var arr = [], node = this.first, n = this.length;
1653 | while (n--) {
1654 | arr.push(node.data || node);
1655 | node = node.next;
1656 | }
1657 | return arr;
1658 | }
1659 | };
1660 |
1661 | Sylvester.LinkedList.prototype.each = Sylvester.LinkedList.prototype.forEach;
1662 |
1663 | Sylvester.LinkedList.Node = function(data) {
1664 | this.prev = null; this.next = null;
1665 | this.data = data;
1666 | };
1667 |
1668 | Sylvester.LinkedList.Circular = function() {};
1669 | Sylvester.LinkedList.Circular.Methods = {
1670 | append: function(node) {
1671 | if (this.first === null) {
1672 | node.prev = node;
1673 | node.next = node;
1674 | this.first = node;
1675 | this.last = node;
1676 | } else {
1677 | node.prev = this.last;
1678 | node.next = this.first;
1679 | this.first.prev = node;
1680 | this.last.next = node;
1681 | this.last = node;
1682 | }
1683 | this.length++;
1684 | },
1685 |
1686 | prepend: function(node) {
1687 | if (this.first === null) {
1688 | this.append(node);
1689 | return;
1690 | } else {
1691 | node.prev = this.last;
1692 | node.next = this.first;
1693 | this.first.prev = node;
1694 | this.last.next = node;
1695 | this.first = node;
1696 | }
1697 | this.length++;
1698 | },
1699 |
1700 | insertAfter: function(node, newNode) {
1701 | newNode.prev = node;
1702 | newNode.next = node.next;
1703 | node.next.prev = newNode;
1704 | node.next = newNode;
1705 | if (newNode.prev === this.last) { this.last = newNode; }
1706 | this.length++;
1707 | },
1708 |
1709 | insertBefore: function(node, newNode) {
1710 | newNode.prev = node.prev;
1711 | newNode.next = node;
1712 | node.prev.next = newNode;
1713 | node.prev = newNode;
1714 | if (newNode.next === this.first) { this.first = newNode; }
1715 | this.length++;
1716 | },
1717 |
1718 | remove: function(node) {
1719 | if (this.length > 1) {
1720 | node.prev.next = node.next;
1721 | node.next.prev = node.prev;
1722 | if (node === this.first) { this.first = node.next; }
1723 | if (node === this.last) { this.last = node.prev; }
1724 | } else {
1725 | this.first = null;
1726 | this.last = null;
1727 | }
1728 | node.prev = null;
1729 | node.next = null;
1730 | this.length--;
1731 | },
1732 |
1733 | withData: function(data) {
1734 | var nodeFromStart = this.first, nodeFromEnd = this.last, n = Math.ceil(this.length / 2);
1735 | while (n--) {
1736 | if (nodeFromStart.data === data) { return nodeFromStart; }
1737 | if (nodeFromEnd.data === data) { return nodeFromEnd; }
1738 | nodeFromStart = nodeFromStart.next;
1739 | nodeFromEnd = nodeFromEnd.prev;
1740 | }
1741 | return null;
1742 | }
1743 | };
1744 |
1745 | Sylvester.LinkedList.Circular.prototype = new Sylvester.LinkedList;
1746 | for (var method in Sylvester.LinkedList.Circular.Methods) {
1747 | Sylvester.LinkedList.Circular.prototype[method] = Sylvester.LinkedList.Circular.Methods[method];
1748 | }
1749 |
1750 | Sylvester.LinkedList.Circular.fromArray = function(list, useNodes) {
1751 | var linked = new Sylvester.LinkedList.Circular();
1752 | var n = list.length;
1753 | while (n--) { linked.prepend(useNodes ? new Sylvester.LinkedList.Node(list[n]) : list[n]); }
1754 | return linked;
1755 | };
1756 |
1757 | (function() {
1758 | var api = (typeof require === 'function' && typeof exports === 'object')
1759 | ? exports
1760 | : this;
1761 |
1762 | api.Line = Sylvester.Line;
1763 | api.Matrix = Sylvester.Matrix;
1764 | api.Plane = Sylvester.Plane;
1765 | api.Polygon = Sylvester.Polygon;
1766 | api.Vector = Sylvester.Vector;
1767 |
1768 | if (typeof WScript !== 'undefined')
1769 | this.Sylvester = Sylvester;
1770 | })();
1771 |
--------------------------------------------------------------------------------
/lib/jquery/jquery-1.8.2.min.js:
--------------------------------------------------------------------------------
1 | /*! jQuery v1.8.2 jquery.com | jquery.org/license */
2 | (function(a,b){function G(a){var b=F[a]={};return p.each(a.split(s),function(a,c){b[c]=!0}),b}function J(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(I,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:+d+""===d?+d:H.test(d)?p.parseJSON(d):d}catch(f){}p.data(a,c,d)}else d=b}return d}function K(a){var b;for(b in a){if(b==="data"&&p.isEmptyObject(a[b]))continue;if(b!=="toJSON")return!1}return!0}function ba(){return!1}function bb(){return!0}function bh(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function bi(a,b){do a=a[b];while(a&&a.nodeType!==1);return a}function bj(a,b,c){b=b||0;if(p.isFunction(b))return p.grep(a,function(a,d){var e=!!b.call(a,d,a);return e===c});if(b.nodeType)return p.grep(a,function(a,d){return a===b===c});if(typeof b=="string"){var d=p.grep(a,function(a){return a.nodeType===1});if(be.test(b))return p.filter(b,d,!c);b=p.filter(b,d)}return p.grep(a,function(a,d){return p.inArray(a,b)>=0===c})}function bk(a){var b=bl.split("|"),c=a.createDocumentFragment();if(c.createElement)while(b.length)c.createElement(b.pop());return c}function bC(a,b){return a.getElementsByTagName(b)[0]||a.appendChild(a.ownerDocument.createElement(b))}function bD(a,b){if(b.nodeType!==1||!p.hasData(a))return;var c,d,e,f=p._data(a),g=p._data(b,f),h=f.events;if(h){delete g.handle,g.events={};for(c in h)for(d=0,e=h[c].length;d").appendTo(e.body),c=b.css("display");b.remove();if(c==="none"||c===""){bI=e.body.appendChild(bI||p.extend(e.createElement("iframe"),{frameBorder:0,width:0,height:0}));if(!bJ||!bI.createElement)bJ=(bI.contentWindow||bI.contentDocument).document,bJ.write(""),bJ.close();b=bJ.body.appendChild(bJ.createElement(a)),c=bH(b,"display"),e.body.removeChild(bI)}return bS[a]=c,c}function ci(a,b,c,d){var e;if(p.isArray(b))p.each(b,function(b,e){c||ce.test(a)?d(a,e):ci(a+"["+(typeof e=="object"?b:"")+"]",e,c,d)});else if(!c&&p.type(b)==="object")for(e in b)ci(a+"["+e+"]",b[e],c,d);else d(a,b)}function cz(a){return function(b,c){typeof b!="string"&&(c=b,b="*");var d,e,f,g=b.toLowerCase().split(s),h=0,i=g.length;if(p.isFunction(c))for(;h)[^>]*$|#([\w\-]*)$)/,v=/^<(\w+)\s*\/?>(?:<\/\1>|)$/,w=/^[\],:{}\s]*$/,x=/(?:^|:|,)(?:\s*\[)+/g,y=/\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,z=/"[^"\\\r\n]*"|true|false|null|-?(?:\d\d*\.|)\d+(?:[eE][\-+]?\d+|)/g,A=/^-ms-/,B=/-([\da-z])/gi,C=function(a,b){return(b+"").toUpperCase()},D=function(){e.addEventListener?(e.removeEventListener("DOMContentLoaded",D,!1),p.ready()):e.readyState==="complete"&&(e.detachEvent("onreadystatechange",D),p.ready())},E={};p.fn=p.prototype={constructor:p,init:function(a,c,d){var f,g,h,i;if(!a)return this;if(a.nodeType)return this.context=this[0]=a,this.length=1,this;if(typeof a=="string"){a.charAt(0)==="<"&&a.charAt(a.length-1)===">"&&a.length>=3?f=[null,a,null]:f=u.exec(a);if(f&&(f[1]||!c)){if(f[1])return c=c instanceof p?c[0]:c,i=c&&c.nodeType?c.ownerDocument||c:e,a=p.parseHTML(f[1],i,!0),v.test(f[1])&&p.isPlainObject(c)&&this.attr.call(a,c,!0),p.merge(this,a);g=e.getElementById(f[2]);if(g&&g.parentNode){if(g.id!==f[2])return d.find(a);this.length=1,this[0]=g}return this.context=e,this.selector=a,this}return!c||c.jquery?(c||d).find(a):this.constructor(c).find(a)}return p.isFunction(a)?d.ready(a):(a.selector!==b&&(this.selector=a.selector,this.context=a.context),p.makeArray(a,this))},selector:"",jquery:"1.8.2",length:0,size:function(){return this.length},toArray:function(){return k.call(this)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var d=p.merge(this.constructor(),a);return d.prevObject=this,d.context=this.context,b==="find"?d.selector=this.selector+(this.selector?" ":"")+c:b&&(d.selector=this.selector+"."+b+"("+c+")"),d},each:function(a,b){return p.each(this,a,b)},ready:function(a){return p.ready.promise().done(a),this},eq:function(a){return a=+a,a===-1?this.slice(a):this.slice(a,a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(k.apply(this,arguments),"slice",k.call(arguments).join(","))},map:function(a){return this.pushStack(p.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:j,sort:[].sort,splice:[].splice},p.fn.init.prototype=p.fn,p.extend=p.fn.extend=function(){var a,c,d,e,f,g,h=arguments[0]||{},i=1,j=arguments.length,k=!1;typeof h=="boolean"&&(k=h,h=arguments[1]||{},i=2),typeof h!="object"&&!p.isFunction(h)&&(h={}),j===i&&(h=this,--i);for(;i0)return;d.resolveWith(e,[p]),p.fn.trigger&&p(e).trigger("ready").off("ready")},isFunction:function(a){return p.type(a)==="function"},isArray:Array.isArray||function(a){return p.type(a)==="array"},isWindow:function(a){return a!=null&&a==a.window},isNumeric:function(a){return!isNaN(parseFloat(a))&&isFinite(a)},type:function(a){return a==null?String(a):E[m.call(a)]||"object"},isPlainObject:function(a){if(!a||p.type(a)!=="object"||a.nodeType||p.isWindow(a))return!1;try{if(a.constructor&&!n.call(a,"constructor")&&!n.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}var d;for(d in a);return d===b||n.call(a,d)},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},error:function(a){throw new Error(a)},parseHTML:function(a,b,c){var d;return!a||typeof a!="string"?null:(typeof b=="boolean"&&(c=b,b=0),b=b||e,(d=v.exec(a))?[b.createElement(d[1])]:(d=p.buildFragment([a],b,c?null:[]),p.merge([],(d.cacheable?p.clone(d.fragment):d.fragment).childNodes)))},parseJSON:function(b){if(!b||typeof b!="string")return null;b=p.trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(w.test(b.replace(y,"@").replace(z,"]").replace(x,"")))return(new Function("return "+b))();p.error("Invalid JSON: "+b)},parseXML:function(c){var d,e;if(!c||typeof c!="string")return null;try{a.DOMParser?(e=new DOMParser,d=e.parseFromString(c,"text/xml")):(d=new ActiveXObject("Microsoft.XMLDOM"),d.async="false",d.loadXML(c))}catch(f){d=b}return(!d||!d.documentElement||d.getElementsByTagName("parsererror").length)&&p.error("Invalid XML: "+c),d},noop:function(){},globalEval:function(b){b&&r.test(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(A,"ms-").replace(B,C)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,c,d){var e,f=0,g=a.length,h=g===b||p.isFunction(a);if(d){if(h){for(e in a)if(c.apply(a[e],d)===!1)break}else for(;f0&&a[0]&&a[i-1]||i===0||p.isArray(a));if(j)for(;h-1)i.splice(c,1),e&&(c<=g&&g--,c<=h&&h--)}),this},has:function(a){return p.inArray(a,i)>-1},empty:function(){return i=[],this},disable:function(){return i=j=c=b,this},disabled:function(){return!i},lock:function(){return j=b,c||l.disable(),this},locked:function(){return!j},fireWith:function(a,b){return b=b||[],b=[a,b.slice?b.slice():b],i&&(!d||j)&&(e?j.push(b):k(b)),this},fire:function(){return l.fireWith(this,arguments),this},fired:function(){return!!d}};return l},p.extend({Deferred:function(a){var b=[["resolve","done",p.Callbacks("once memory"),"resolved"],["reject","fail",p.Callbacks("once memory"),"rejected"],["notify","progress",p.Callbacks("memory")]],c="pending",d={state:function(){return c},always:function(){return e.done(arguments).fail(arguments),this},then:function(){var a=arguments;return p.Deferred(function(c){p.each(b,function(b,d){var f=d[0],g=a[b];e[d[1]](p.isFunction(g)?function(){var a=g.apply(this,arguments);a&&p.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f+"With"](this===e?c:this,[a])}:c[f])}),a=null}).promise()},promise:function(a){return a!=null?p.extend(a,d):d}},e={};return d.pipe=d.then,p.each(b,function(a,f){var g=f[2],h=f[3];d[f[1]]=g.add,h&&g.add(function(){c=h},b[a^1][2].disable,b[2][2].lock),e[f[0]]=g.fire,e[f[0]+"With"]=g.fireWith}),d.promise(e),a&&a.call(e,e),e},when:function(a){var b=0,c=k.call(arguments),d=c.length,e=d!==1||a&&p.isFunction(a.promise)?d:0,f=e===1?a:p.Deferred(),g=function(a,b,c){return function(d){b[a]=this,c[a]=arguments.length>1?k.call(arguments):d,c===h?f.notifyWith(b,c):--e||f.resolveWith(b,c)}},h,i,j;if(d>1){h=new Array(d),i=new Array(d),j=new Array(d);for(;ba",c=n.getElementsByTagName("*"),d=n.getElementsByTagName("a")[0],d.style.cssText="top:1px;float:left;opacity:.5";if(!c||!c.length)return{};f=e.createElement("select"),g=f.appendChild(e.createElement("option")),h=n.getElementsByTagName("input")[0],b={leadingWhitespace:n.firstChild.nodeType===3,tbody:!n.getElementsByTagName("tbody").length,htmlSerialize:!!n.getElementsByTagName("link").length,style:/top/.test(d.getAttribute("style")),hrefNormalized:d.getAttribute("href")==="/a",opacity:/^0.5/.test(d.style.opacity),cssFloat:!!d.style.cssFloat,checkOn:h.value==="on",optSelected:g.selected,getSetAttribute:n.className!=="t",enctype:!!e.createElement("form").enctype,html5Clone:e.createElement("nav").cloneNode(!0).outerHTML!=="<:nav>",boxModel:e.compatMode==="CSS1Compat",submitBubbles:!0,changeBubbles:!0,focusinBubbles:!1,deleteExpando:!0,noCloneEvent:!0,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableMarginRight:!0,boxSizingReliable:!0,pixelPosition:!1},h.checked=!0,b.noCloneChecked=h.cloneNode(!0).checked,f.disabled=!0,b.optDisabled=!g.disabled;try{delete n.test}catch(o){b.deleteExpando=!1}!n.addEventListener&&n.attachEvent&&n.fireEvent&&(n.attachEvent("onclick",m=function(){b.noCloneEvent=!1}),n.cloneNode(!0).fireEvent("onclick"),n.detachEvent("onclick",m)),h=e.createElement("input"),h.value="t",h.setAttribute("type","radio"),b.radioValue=h.value==="t",h.setAttribute("checked","checked"),h.setAttribute("name","t"),n.appendChild(h),i=e.createDocumentFragment(),i.appendChild(n.lastChild),b.checkClone=i.cloneNode(!0).cloneNode(!0).lastChild.checked,b.appendChecked=h.checked,i.removeChild(h),i.appendChild(n);if(n.attachEvent)for(k in{submit:!0,change:!0,focusin:!0})j="on"+k,l=j in n,l||(n.setAttribute(j,"return;"),l=typeof n[j]=="function"),b[k+"Bubbles"]=l;return p(function(){var c,d,f,g,h="padding:0;margin:0;border:0;display:block;overflow:hidden;",i=e.getElementsByTagName("body")[0];if(!i)return;c=e.createElement("div"),c.style.cssText="visibility:hidden;border:0;width:0;height:0;position:static;top:0;margin-top:1px",i.insertBefore(c,i.firstChild),d=e.createElement("div"),c.appendChild(d),d.innerHTML="",f=d.getElementsByTagName("td"),f[0].style.cssText="padding:0;margin:0;border:0;display:none",l=f[0].offsetHeight===0,f[0].style.display="",f[1].style.display="none",b.reliableHiddenOffsets=l&&f[0].offsetHeight===0,d.innerHTML="",d.style.cssText="box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;padding:1px;border:1px;display:block;width:4px;margin-top:1%;position:absolute;top:1%;",b.boxSizing=d.offsetWidth===4,b.doesNotIncludeMarginInBodyOffset=i.offsetTop!==1,a.getComputedStyle&&(b.pixelPosition=(a.getComputedStyle(d,null)||{}).top!=="1%",b.boxSizingReliable=(a.getComputedStyle(d,null)||{width:"4px"}).width==="4px",g=e.createElement("div"),g.style.cssText=d.style.cssText=h,g.style.marginRight=g.style.width="0",d.style.width="1px",d.appendChild(g),b.reliableMarginRight=!parseFloat((a.getComputedStyle(g,null)||{}).marginRight)),typeof d.style.zoom!="undefined"&&(d.innerHTML="",d.style.cssText=h+"width:1px;padding:1px;display:inline;zoom:1",b.inlineBlockNeedsLayout=d.offsetWidth===3,d.style.display="block",d.style.overflow="visible",d.innerHTML="",d.firstChild.style.width="5px",b.shrinkWrapBlocks=d.offsetWidth!==3,c.style.zoom=1),i.removeChild(c),c=d=f=g=null}),i.removeChild(n),c=d=f=g=h=i=n=null,b}();var H=/(?:\{[\s\S]*\}|\[[\s\S]*\])$/,I=/([A-Z])/g;p.extend({cache:{},deletedIds:[],uuid:0,expando:"jQuery"+(p.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){return a=a.nodeType?p.cache[a[p.expando]]:a[p.expando],!!a&&!K(a)},data:function(a,c,d,e){if(!p.acceptData(a))return;var f,g,h=p.expando,i=typeof c=="string",j=a.nodeType,k=j?p.cache:a,l=j?a[h]:a[h]&&h;if((!l||!k[l]||!e&&!k[l].data)&&i&&d===b)return;l||(j?a[h]=l=p.deletedIds.pop()||p.guid++:l=h),k[l]||(k[l]={},j||(k[l].toJSON=p.noop));if(typeof c=="object"||typeof c=="function")e?k[l]=p.extend(k[l],c):k[l].data=p.extend(k[l].data,c);return f=k[l],e||(f.data||(f.data={}),f=f.data),d!==b&&(f[p.camelCase(c)]=d),i?(g=f[c],g==null&&(g=f[p.camelCase(c)])):g=f,g},removeData:function(a,b,c){if(!p.acceptData(a))return;var d,e,f,g=a.nodeType,h=g?p.cache:a,i=g?a[p.expando]:p.expando;if(!h[i])return;if(b){d=c?h[i]:h[i].data;if(d){p.isArray(b)||(b in d?b=[b]:(b=p.camelCase(b),b in d?b=[b]:b=b.split(" ")));for(e=0,f=b.length;e1,null,!1))},removeData:function(a){return this.each(function(){p.removeData(this,a)})}}),p.extend({queue:function(a,b,c){var d;if(a)return b=(b||"fx")+"queue",d=p._data(a,b),c&&(!d||p.isArray(c)?d=p._data(a,b,p.makeArray(c)):d.push(c)),d||[]},dequeue:function(a,b){b=b||"fx";var c=p.queue(a,b),d=c.length,e=c.shift(),f=p._queueHooks(a,b),g=function(){p.dequeue(a,b)};e==="inprogress"&&(e=c.shift(),d--),e&&(b==="fx"&&c.unshift("inprogress"),delete f.stop,e.call(a,g,f)),!d&&f&&f.empty.fire()},_queueHooks:function(a,b){var c=b+"queueHooks";return p._data(a,c)||p._data(a,c,{empty:p.Callbacks("once memory").add(function(){p.removeData(a,b+"queue",!0),p.removeData(a,c,!0)})})}}),p.fn.extend({queue:function(a,c){var d=2;return typeof a!="string"&&(c=a,a="fx",d--),arguments.length1)},removeAttr:function(a){return this.each(function(){p.removeAttr(this,a)})},prop:function(a,b){return p.access(this,p.prop,a,b,arguments.length>1)},removeProp:function(a){return a=p.propFix[a]||a,this.each(function(){try{this[a]=b,delete this[a]}catch(c){}})},addClass:function(a){var b,c,d,e,f,g,h;if(p.isFunction(a))return this.each(function(b){p(this).addClass(a.call(this,b,this.className))});if(a&&typeof a=="string"){b=a.split(s);for(c=0,d=this.length;c=0)d=d.replace(" "+c[f]+" "," ");e.className=a?p.trim(d):""}}}return this},toggleClass:function(a,b){var c=typeof a,d=typeof b=="boolean";return p.isFunction(a)?this.each(function(c){p(this).toggleClass(a.call(this,c,this.className,b),b)}):this.each(function(){if(c==="string"){var e,f=0,g=p(this),h=b,i=a.split(s);while(e=i[f++])h=d?h:!g.hasClass(e),g[h?"addClass":"removeClass"](e)}else if(c==="undefined"||c==="boolean")this.className&&p._data(this,"__className__",this.className),this.className=this.className||a===!1?"":p._data(this,"__className__")||""})},hasClass:function(a){var b=" "+a+" ",c=0,d=this.length;for(;c=0)return!0;return!1},val:function(a){var c,d,e,f=this[0];if(!arguments.length){if(f)return c=p.valHooks[f.type]||p.valHooks[f.nodeName.toLowerCase()],c&&"get"in c&&(d=c.get(f,"value"))!==b?d:(d=f.value,typeof d=="string"?d.replace(P,""):d==null?"":d);return}return e=p.isFunction(a),this.each(function(d){var f,g=p(this);if(this.nodeType!==1)return;e?f=a.call(this,d,g.val()):f=a,f==null?f="":typeof f=="number"?f+="":p.isArray(f)&&(f=p.map(f,function(a){return a==null?"":a+""})),c=p.valHooks[this.type]||p.valHooks[this.nodeName.toLowerCase()];if(!c||!("set"in c)||c.set(this,f,"value")===b)this.value=f})}}),p.extend({valHooks:{option:{get:function(a){var b=a.attributes.value;return!b||b.specified?a.value:a.text}},select:{get:function(a){var b,c,d,e,f=a.selectedIndex,g=[],h=a.options,i=a.type==="select-one";if(f<0)return null;c=i?f:0,d=i?f+1:h.length;for(;c=0}),c.length||(a.selectedIndex=-1),c}}},attrFn:{},attr:function(a,c,d,e){var f,g,h,i=a.nodeType;if(!a||i===3||i===8||i===2)return;if(e&&p.isFunction(p.fn[c]))return p(a)[c](d);if(typeof a.getAttribute=="undefined")return p.prop(a,c,d);h=i!==1||!p.isXMLDoc(a),h&&(c=c.toLowerCase(),g=p.attrHooks[c]||(T.test(c)?M:L));if(d!==b){if(d===null){p.removeAttr(a,c);return}return g&&"set"in g&&h&&(f=g.set(a,d,c))!==b?f:(a.setAttribute(c,d+""),d)}return g&&"get"in g&&h&&(f=g.get(a,c))!==null?f:(f=a.getAttribute(c),f===null?b:f)},removeAttr:function(a,b){var c,d,e,f,g=0;if(b&&a.nodeType===1){d=b.split(s);for(;g=0}})});var V=/^(?:textarea|input|select)$/i,W=/^([^\.]*|)(?:\.(.+)|)$/,X=/(?:^|\s)hover(\.\S+|)\b/,Y=/^key/,Z=/^(?:mouse|contextmenu)|click/,$=/^(?:focusinfocus|focusoutblur)$/,_=function(a){return p.event.special.hover?a:a.replace(X,"mouseenter$1 mouseleave$1")};p.event={add:function(a,c,d,e,f){var g,h,i,j,k,l,m,n,o,q,r;if(a.nodeType===3||a.nodeType===8||!c||!d||!(g=p._data(a)))return;d.handler&&(o=d,d=o.handler,f=o.selector),d.guid||(d.guid=p.guid++),i=g.events,i||(g.events=i={}),h=g.handle,h||(g.handle=h=function(a){return typeof p!="undefined"&&(!a||p.event.triggered!==a.type)?p.event.dispatch.apply(h.elem,arguments):b},h.elem=a),c=p.trim(_(c)).split(" ");for(j=0;j=0&&(s=s.slice(0,-1),i=!0),s.indexOf(".")>=0&&(t=s.split("."),s=t.shift(),t.sort());if((!f||p.event.customEvent[s])&&!p.event.global[s])return;c=typeof c=="object"?c[p.expando]?c:new p.Event(s,c):new p.Event(s),c.type=s,c.isTrigger=!0,c.exclusive=i,c.namespace=t.join("."),c.namespace_re=c.namespace?new RegExp("(^|\\.)"+t.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,m=s.indexOf(":")<0?"on"+s:"";if(!f){h=p.cache;for(j in h)h[j].events&&h[j].events[s]&&p.event.trigger(c,d,h[j].handle.elem,!0);return}c.result=b,c.target||(c.target=f),d=d!=null?p.makeArray(d):[],d.unshift(c),n=p.event.special[s]||{};if(n.trigger&&n.trigger.apply(f,d)===!1)return;q=[[f,n.bindType||s]];if(!g&&!n.noBubble&&!p.isWindow(f)){r=n.delegateType||s,k=$.test(r+s)?f:f.parentNode;for(l=f;k;k=k.parentNode)q.push([k,r]),l=k;l===(f.ownerDocument||e)&&q.push([l.defaultView||l.parentWindow||a,r])}for(j=0;j=0:p.find(m,this,null,[f]).length),h[m]&&j.push(l);j.length&&u.push({elem:f,matches:j})}o.length>q&&u.push({elem:this,matches:o.slice(q)});for(d=0;d0?this.on(b,null,a,c):this.trigger(b)},Y.test(b)&&(p.event.fixHooks[b]=p.event.keyHooks),Z.test(b)&&(p.event.fixHooks[b]=p.event.mouseHooks)}),function(a,b){function bc(a,b,c,d){c=c||[],b=b||r;var e,f,i,j,k=b.nodeType;if(!a||typeof a!="string")return c;if(k!==1&&k!==9)return[];i=g(b);if(!i&&!d)if(e=P.exec(a))if(j=e[1]){if(k===9){f=b.getElementById(j);if(!f||!f.parentNode)return c;if(f.id===j)return c.push(f),c}else if(b.ownerDocument&&(f=b.ownerDocument.getElementById(j))&&h(b,f)&&f.id===j)return c.push(f),c}else{if(e[2])return w.apply(c,x.call(b.getElementsByTagName(a),0)),c;if((j=e[3])&&_&&b.getElementsByClassName)return w.apply(c,x.call(b.getElementsByClassName(j),0)),c}return bp(a.replace(L,"$1"),b,c,d,i)}function bd(a){return function(b){var c=b.nodeName.toLowerCase();return c==="input"&&b.type===a}}function be(a){return function(b){var c=b.nodeName.toLowerCase();return(c==="input"||c==="button")&&b.type===a}}function bf(a){return z(function(b){return b=+b,z(function(c,d){var e,f=a([],c.length,b),g=f.length;while(g--)c[e=f[g]]&&(c[e]=!(d[e]=c[e]))})})}function bg(a,b,c){if(a===b)return c;var d=a.nextSibling;while(d){if(d===b)return-1;d=d.nextSibling}return 1}function bh(a,b){var c,d,f,g,h,i,j,k=C[o][a];if(k)return b?0:k.slice(0);h=a,i=[],j=e.preFilter;while(h){if(!c||(d=M.exec(h)))d&&(h=h.slice(d[0].length)),i.push(f=[]);c=!1;if(d=N.exec(h))f.push(c=new q(d.shift())),h=h.slice(c.length),c.type=d[0].replace(L," ");for(g in e.filter)(d=W[g].exec(h))&&(!j[g]||(d=j[g](d,r,!0)))&&(f.push(c=new q(d.shift())),h=h.slice(c.length),c.type=g,c.matches=d);if(!c)break}return b?h.length:h?bc.error(a):C(a,i).slice(0)}function bi(a,b,d){var e=b.dir,f=d&&b.dir==="parentNode",g=u++;return b.first?function(b,c,d){while(b=b[e])if(f||b.nodeType===1)return a(b,c,d)}:function(b,d,h){if(!h){var i,j=t+" "+g+" ",k=j+c;while(b=b[e])if(f||b.nodeType===1){if((i=b[o])===k)return b.sizset;if(typeof i=="string"&&i.indexOf(j)===0){if(b.sizset)return b}else{b[o]=k;if(a(b,d,h))return b.sizset=!0,b;b.sizset=!1}}}else while(b=b[e])if(f||b.nodeType===1)if(a(b,d,h))return b}}function bj(a){return a.length>1?function(b,c,d){var e=a.length;while(e--)if(!a[e](b,c,d))return!1;return!0}:a[0]}function bk(a,b,c,d,e){var f,g=[],h=0,i=a.length,j=b!=null;for(;h-1},h,!0),m=[function(a,c,d){return!g&&(d||c!==l)||((b=c).nodeType?j(a,c,d):k(a,c,d))}];for(;i1&&bj(m),i>1&&a.slice(0,i-1).join("").replace(L,"$1"),c,i0,f=a.length>0,g=function(h,i,j,k,m){var n,o,p,q=[],s=0,u="0",x=h&&[],y=m!=null,z=l,A=h||f&&e.find.TAG("*",m&&i.parentNode||i),B=t+=z==null?1:Math.E;y&&(l=i!==r&&i,c=g.el);for(;(n=A[u])!=null;u++){if(f&&n){for(o=0;p=a[o];o++)if(p(n,i,j)){k.push(n);break}y&&(t=B,c=++g.el)}d&&((n=!p&&n)&&s--,h&&x.push(n))}s+=u;if(d&&u!==s){for(o=0;p=b[o];o++)p(x,q,i,j);if(h){if(s>0)while(u--)!x[u]&&!q[u]&&(q[u]=v.call(k));q=bk(q)}w.apply(k,q),y&&!h&&q.length>0&&s+b.length>1&&bc.uniqueSort(k)}return y&&(t=B,l=z),x};return g.el=0,d?z(g):g}function bo(a,b,c,d){var e=0,f=b.length;for(;e2&&(j=h[0]).type==="ID"&&b.nodeType===9&&!f&&e.relative[h[1].type]){b=e.find.ID(j.matches[0].replace(V,""),b,f)[0];if(!b)return c;a=a.slice(h.shift().length)}for(g=W.POS.test(a)?-1:h.length-1;g>=0;g--){j=h[g];if(e.relative[k=j.type])break;if(l=e.find[k])if(d=l(j.matches[0].replace(V,""),R.test(h[0].type)&&b.parentNode||b,f)){h.splice(g,1),a=d.length&&h.join("");if(!a)return w.apply(c,x.call(d,0)),c;break}}}return i(a,m)(d,b,f,c,R.test(a)),c}function bq(){}var c,d,e,f,g,h,i,j,k,l,m=!0,n="undefined",o=("sizcache"+Math.random()).replace(".",""),q=String,r=a.document,s=r.documentElement,t=0,u=0,v=[].pop,w=[].push,x=[].slice,y=[].indexOf||function(a){var b=0,c=this.length;for(;be.cacheLength&&delete a[b.shift()],a[c]=d},a)},B=A(),C=A(),D=A(),E="[\\x20\\t\\r\\n\\f]",F="(?:\\\\.|[-\\w]|[^\\x00-\\xa0])+",G=F.replace("w","w#"),H="([*^$|!~]?=)",I="\\["+E+"*("+F+")"+E+"*(?:"+H+E+"*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|("+G+")|)|)"+E+"*\\]",J=":("+F+")(?:\\((?:(['\"])((?:\\\\.|[^\\\\])*?)\\2|([^()[\\]]*|(?:(?:"+I+")|[^:]|\\\\.)*|.*))\\)|)",K=":(even|odd|eq|gt|lt|nth|first|last)(?:\\("+E+"*((?:-\\d)?\\d*)"+E+"*\\)|)(?=[^-]|$)",L=new RegExp("^"+E+"+|((?:^|[^\\\\])(?:\\\\.)*)"+E+"+$","g"),M=new RegExp("^"+E+"*,"+E+"*"),N=new RegExp("^"+E+"*([\\x20\\t\\r\\n\\f>+~])"+E+"*"),O=new RegExp(J),P=/^(?:#([\w\-]+)|(\w+)|\.([\w\-]+))$/,Q=/^:not/,R=/[\x20\t\r\n\f]*[+~]/,S=/:not\($/,T=/h\d/i,U=/input|select|textarea|button/i,V=/\\(?!\\)/g,W={ID:new RegExp("^#("+F+")"),CLASS:new RegExp("^\\.("+F+")"),NAME:new RegExp("^\\[name=['\"]?("+F+")['\"]?\\]"),TAG:new RegExp("^("+F.replace("w","w*")+")"),ATTR:new RegExp("^"+I),PSEUDO:new RegExp("^"+J),POS:new RegExp(K,"i"),CHILD:new RegExp("^:(only|nth|first|last)-child(?:\\("+E+"*(even|odd|(([+-]|)(\\d*)n|)"+E+"*(?:([+-]|)"+E+"*(\\d+)|))"+E+"*\\)|)","i"),needsContext:new RegExp("^"+E+"*[>+~]|"+K,"i")},X=function(a){var b=r.createElement("div");try{return a(b)}catch(c){return!1}finally{b=null}},Y=X(function(a){return a.appendChild(r.createComment("")),!a.getElementsByTagName("*").length}),Z=X(function(a){return a.innerHTML="",a.firstChild&&typeof a.firstChild.getAttribute!==n&&a.firstChild.getAttribute("href")==="#"}),$=X(function(a){a.innerHTML="";var b=typeof a.lastChild.getAttribute("multiple");return b!=="boolean"&&b!=="string"}),_=X(function(a){return a.innerHTML="",!a.getElementsByClassName||!a.getElementsByClassName("e").length?!1:(a.lastChild.className="e",a.getElementsByClassName("e").length===2)}),ba=X(function(a){a.id=o+0,a.innerHTML="",s.insertBefore(a,s.firstChild);var b=r.getElementsByName&&r.getElementsByName(o).length===2+r.getElementsByName(o+0).length;return d=!r.getElementById(o),s.removeChild(a),b});try{x.call(s.childNodes,0)[0].nodeType}catch(bb){x=function(a){var b,c=[];for(;b=this[a];a++)c.push(b);return c}}bc.matches=function(a,b){return bc(a,null,null,b)},bc.matchesSelector=function(a,b){return bc(b,null,null,[a]).length>0},f=bc.getText=function(a){var b,c="",d=0,e=a.nodeType;if(e){if(e===1||e===9||e===11){if(typeof a.textContent=="string")return a.textContent;for(a=a.firstChild;a;a=a.nextSibling)c+=f(a)}else if(e===3||e===4)return a.nodeValue}else for(;b=a[d];d++)c+=f(b);return c},g=bc.isXML=function(a){var b=a&&(a.ownerDocument||a).documentElement;return b?b.nodeName!=="HTML":!1},h=bc.contains=s.contains?function(a,b){var c=a.nodeType===9?a.documentElement:a,d=b&&b.parentNode;return a===d||!!(d&&d.nodeType===1&&c.contains&&c.contains(d))}:s.compareDocumentPosition?function(a,b){return b&&!!(a.compareDocumentPosition(b)&16)}:function(a,b){while(b=b.parentNode)if(b===a)return!0;return!1},bc.attr=function(a,b){var c,d=g(a);return d||(b=b.toLowerCase()),(c=e.attrHandle[b])?c(a):d||$?a.getAttribute(b):(c=a.getAttributeNode(b),c?typeof a[b]=="boolean"?a[b]?b:null:c.specified?c.value:null:null)},e=bc.selectors={cacheLength:50,createPseudo:z,match:W,attrHandle:Z?{}:{href:function(a){return a.getAttribute("href",2)},type:function(a){return a.getAttribute("type")}},find:{ID:d?function(a,b,c){if(typeof b.getElementById!==n&&!c){var d=b.getElementById(a);return d&&d.parentNode?[d]:[]}}:function(a,c,d){if(typeof c.getElementById!==n&&!d){var e=c.getElementById(a);return e?e.id===a||typeof e.getAttributeNode!==n&&e.getAttributeNode("id").value===a?[e]:b:[]}},TAG:Y?function(a,b){if(typeof b.getElementsByTagName!==n)return b.getElementsByTagName(a)}:function(a,b){var c=b.getElementsByTagName(a);if(a==="*"){var d,e=[],f=0;for(;d=c[f];f++)d.nodeType===1&&e.push(d);return e}return c},NAME:ba&&function(a,b){if(typeof b.getElementsByName!==n)return b.getElementsByName(name)},CLASS:_&&function(a,b,c){if(typeof b.getElementsByClassName!==n&&!c)return b.getElementsByClassName(a)}},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(a){return a[1]=a[1].replace(V,""),a[3]=(a[4]||a[5]||"").replace(V,""),a[2]==="~="&&(a[3]=" "+a[3]+" "),a.slice(0,4)},CHILD:function(a){return a[1]=a[1].toLowerCase(),a[1]==="nth"?(a[2]||bc.error(a[0]),a[3]=+(a[3]?a[4]+(a[5]||1):2*(a[2]==="even"||a[2]==="odd")),a[4]=+(a[6]+a[7]||a[2]==="odd")):a[2]&&bc.error(a[0]),a},PSEUDO:function(a){var b,c;if(W.CHILD.test(a[0]))return null;if(a[3])a[2]=a[3];else if(b=a[4])O.test(b)&&(c=bh(b,!0))&&(c=b.indexOf(")",b.length-c)-b.length)&&(b=b.slice(0,c),a[0]=a[0].slice(0,c)),a[2]=b;return a.slice(0,3)}},filter:{ID:d?function(a){return a=a.replace(V,""),function(b){return b.getAttribute("id")===a}}:function(a){return a=a.replace(V,""),function(b){var c=typeof b.getAttributeNode!==n&&b.getAttributeNode("id");return c&&c.value===a}},TAG:function(a){return a==="*"?function(){return!0}:(a=a.replace(V,"").toLowerCase(),function(b){return b.nodeName&&b.nodeName.toLowerCase()===a})},CLASS:function(a){var b=B[o][a];return b||(b=B(a,new RegExp("(^|"+E+")"+a+"("+E+"|$)"))),function(a){return b.test(a.className||typeof a.getAttribute!==n&&a.getAttribute("class")||"")}},ATTR:function(a,b,c){return function(d,e){var f=bc.attr(d,a);return f==null?b==="!=":b?(f+="",b==="="?f===c:b==="!="?f!==c:b==="^="?c&&f.indexOf(c)===0:b==="*="?c&&f.indexOf(c)>-1:b==="$="?c&&f.substr(f.length-c.length)===c:b==="~="?(" "+f+" ").indexOf(c)>-1:b==="|="?f===c||f.substr(0,c.length+1)===c+"-":!1):!0}},CHILD:function(a,b,c,d){return a==="nth"?function(a){var b,e,f=a.parentNode;if(c===1&&d===0)return!0;if(f){e=0;for(b=f.firstChild;b;b=b.nextSibling)if(b.nodeType===1){e++;if(a===b)break}}return e-=d,e===c||e%c===0&&e/c>=0}:function(b){var c=b;switch(a){case"only":case"first":while(c=c.previousSibling)if(c.nodeType===1)return!1;if(a==="first")return!0;c=b;case"last":while(c=c.nextSibling)if(c.nodeType===1)return!1;return!0}}},PSEUDO:function(a,b){var c,d=e.pseudos[a]||e.setFilters[a.toLowerCase()]||bc.error("unsupported pseudo: "+a);return d[o]?d(b):d.length>1?(c=[a,a,"",b],e.setFilters.hasOwnProperty(a.toLowerCase())?z(function(a,c){var e,f=d(a,b),g=f.length;while(g--)e=y.call(a,f[g]),a[e]=!(c[e]=f[g])}):function(a){return d(a,0,c)}):d}},pseudos:{not:z(function(a){var b=[],c=[],d=i(a.replace(L,"$1"));return d[o]?z(function(a,b,c,e){var f,g=d(a,null,e,[]),h=a.length;while(h--)if(f=g[h])a[h]=!(b[h]=f)}):function(a,e,f){return b[0]=a,d(b,null,f,c),!c.pop()}}),has:z(function(a){return function(b){return bc(a,b).length>0}}),contains:z(function(a){return function(b){return(b.textContent||b.innerText||f(b)).indexOf(a)>-1}}),enabled:function(a){return a.disabled===!1},disabled:function(a){return a.disabled===!0},checked:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&!!a.checked||b==="option"&&!!a.selected},selected:function(a){return a.parentNode&&a.parentNode.selectedIndex,a.selected===!0},parent:function(a){return!e.pseudos.empty(a)},empty:function(a){var b;a=a.firstChild;while(a){if(a.nodeName>"@"||(b=a.nodeType)===3||b===4)return!1;a=a.nextSibling}return!0},header:function(a){return T.test(a.nodeName)},text:function(a){var b,c;return a.nodeName.toLowerCase()==="input"&&(b=a.type)==="text"&&((c=a.getAttribute("type"))==null||c.toLowerCase()===b)},radio:bd("radio"),checkbox:bd("checkbox"),file:bd("file"),password:bd("password"),image:bd("image"),submit:be("submit"),reset:be("reset"),button:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&a.type==="button"||b==="button"},input:function(a){return U.test(a.nodeName)},focus:function(a){var b=a.ownerDocument;return a===b.activeElement&&(!b.hasFocus||b.hasFocus())&&(!!a.type||!!a.href)},active:function(a){return a===a.ownerDocument.activeElement},first:bf(function(a,b,c){return[0]}),last:bf(function(a,b,c){return[b-1]}),eq:bf(function(a,b,c){return[c<0?c+b:c]}),even:bf(function(a,b,c){for(var d=0;d=0;)a.push(d);return a}),gt:bf(function(a,b,c){for(var d=c<0?c+b:c;++d",a.querySelectorAll("[selected]").length||e.push("\\["+E+"*(?:checked|disabled|ismap|multiple|readonly|selected|value)"),a.querySelectorAll(":checked").length||e.push(":checked")}),X(function(a){a.innerHTML="",a.querySelectorAll("[test^='']").length&&e.push("[*^$]="+E+"*(?:\"\"|'')"),a.innerHTML="",a.querySelectorAll(":enabled").length||e.push(":enabled",":disabled")}),e=new RegExp(e.join("|")),bp=function(a,d,f,g,h){if(!g&&!h&&(!e||!e.test(a))){var i,j,k=!0,l=o,m=d,n=d.nodeType===9&&a;if(d.nodeType===1&&d.nodeName.toLowerCase()!=="object"){i=bh(a),(k=d.getAttribute("id"))?l=k.replace(c,"\\$&"):d.setAttribute("id",l),l="[id='"+l+"'] ",j=i.length;while(j--)i[j]=l+i[j].join("");m=R.test(a)&&d.parentNode||d,n=i.join(",")}if(n)try{return w.apply(f,x.call(m.querySelectorAll(n),0)),f}catch(p){}finally{k||d.removeAttribute("id")}}return b(a,d,f,g,h)},h&&(X(function(b){a=h.call(b,"div");try{h.call(b,"[test!='']:sizzle"),f.push("!=",J)}catch(c){}}),f=new RegExp(f.join("|")),bc.matchesSelector=function(b,c){c=c.replace(d,"='$1']");if(!g(b)&&!f.test(c)&&(!e||!e.test(c)))try{var i=h.call(b,c);if(i||a||b.document&&b.document.nodeType!==11)return i}catch(j){}return bc(c,null,null,[b]).length>0})}(),e.pseudos.nth=e.pseudos.eq,e.filters=bq.prototype=e.pseudos,e.setFilters=new bq,bc.attr=p.attr,p.find=bc,p.expr=bc.selectors,p.expr[":"]=p.expr.pseudos,p.unique=bc.uniqueSort,p.text=bc.getText,p.isXMLDoc=bc.isXML,p.contains=bc.contains}(a);var bc=/Until$/,bd=/^(?:parents|prev(?:Until|All))/,be=/^.[^:#\[\.,]*$/,bf=p.expr.match.needsContext,bg={children:!0,contents:!0,next:!0,prev:!0};p.fn.extend({find:function(a){var b,c,d,e,f,g,h=this;if(typeof a!="string")return p(a).filter(function(){for(b=0,c=h.length;b0)for(e=d;e=0:p.filter(a,this).length>0:this.filter(a).length>0)},closest:function(a,b){var c,d=0,e=this.length,f=[],g=bf.test(a)||typeof a!="string"?p(a,b||this.context):0;for(;d-1:p.find.matchesSelector(c,a)){f.push(c);break}c=c.parentNode}}return f=f.length>1?p.unique(f):f,this.pushStack(f,"closest",a)},index:function(a){return a?typeof a=="string"?p.inArray(this[0],p(a)):p.inArray(a.jquery?a[0]:a,this):this[0]&&this[0].parentNode?this.prevAll().length:-1},add:function(a,b){var c=typeof a=="string"?p(a,b):p.makeArray(a&&a.nodeType?[a]:a),d=p.merge(this.get(),c);return this.pushStack(bh(c[0])||bh(d[0])?d:p.unique(d))},addBack:function(a){return this.add(a==null?this.prevObject:this.prevObject.filter(a))}}),p.fn.andSelf=p.fn.addBack,p.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return p.dir(a,"parentNode")},parentsUntil:function(a,b,c){return p.dir(a,"parentNode",c)},next:function(a){return bi(a,"nextSibling")},prev:function(a){return bi(a,"previousSibling")},nextAll:function(a){return p.dir(a,"nextSibling")},prevAll:function(a){return p.dir(a,"previousSibling")},nextUntil:function(a,b,c){return p.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return p.dir(a,"previousSibling",c)},siblings:function(a){return p.sibling((a.parentNode||{}).firstChild,a)},children:function(a){return p.sibling(a.firstChild)},contents:function(a){return p.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:p.merge([],a.childNodes)}},function(a,b){p.fn[a]=function(c,d){var e=p.map(this,b,c);return bc.test(a)||(d=c),d&&typeof d=="string"&&(e=p.filter(d,e)),e=this.length>1&&!bg[a]?p.unique(e):e,this.length>1&&bd.test(a)&&(e=e.reverse()),this.pushStack(e,a,k.call(arguments).join(","))}}),p.extend({filter:function(a,b,c){return c&&(a=":not("+a+")"),b.length===1?p.find.matchesSelector(b[0],a)?[b[0]]:[]:p.find.matches(a,b)},dir:function(a,c,d){var e=[],f=a[c];while(f&&f.nodeType!==9&&(d===b||f.nodeType!==1||!p(f).is(d)))f.nodeType===1&&e.push(f),f=f[c];return e},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var bl="abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video",bm=/ jQuery\d+="(?:null|\d+)"/g,bn=/^\s+/,bo=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,bp=/<([\w:]+)/,bq=/]","i"),bv=/^(?:checkbox|radio)$/,bw=/checked\s*(?:[^=]|=\s*.checked.)/i,bx=/\/(java|ecma)script/i,by=/^\s*\s*$/g,bz={option:[1,""],legend:[1,""],thead:[1,""],tr:[2,""],td:[3,""],col:[2,""],area:[1,""],_default:[0,"",""]},bA=bk(e),bB=bA.appendChild(e.createElement("div"));bz.optgroup=bz.option,bz.tbody=bz.tfoot=bz.colgroup=bz.caption=bz.thead,bz.th=bz.td,p.support.htmlSerialize||(bz._default=[1,"X","
"]),p.fn.extend({text:function(a){return p.access(this,function(a){return a===b?p.text(this):this.empty().append((this[0]&&this[0].ownerDocument||e).createTextNode(a))},null,a,arguments.length)},wrapAll:function(a){if(p.isFunction(a))return this.each(function(b){p(this).wrapAll(a.call(this,b))});if(this[0]){var b=p(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){return p.isFunction(a)?this.each(function(b){p(this).wrapInner(a.call(this,b))}):this.each(function(){var b=p(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){var b=p.isFunction(a);return this.each(function(c){p(this).wrapAll(b?a.call(this,c):a)})},unwrap:function(){return this.parent().each(function(){p.nodeName(this,"body")||p(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){(this.nodeType===1||this.nodeType===11)&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){(this.nodeType===1||this.nodeType===11)&&this.insertBefore(a,this.firstChild)})},before:function(){if(!bh(this[0]))return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=p.clean(arguments);return this.pushStack(p.merge(a,this),"before",this.selector)}},after:function(){if(!bh(this[0]))return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=p.clean(arguments);return this.pushStack(p.merge(this,a),"after",this.selector)}},remove:function(a,b){var c,d=0;for(;(c=this[d])!=null;d++)if(!a||p.filter(a,[c]).length)!b&&c.nodeType===1&&(p.cleanData(c.getElementsByTagName("*")),p.cleanData([c])),c.parentNode&&c.parentNode.removeChild(c);return this},empty:function(){var a,b=0;for(;(a=this[b])!=null;b++){a.nodeType===1&&p.cleanData(a.getElementsByTagName("*"));while(a.firstChild)a.removeChild(a.firstChild)}return this},clone:function(a,b){return a=a==null?!1:a,b=b==null?a:b,this.map(function(){return p.clone(this,a,b)})},html:function(a){return p.access(this,function(a){var c=this[0]||{},d=0,e=this.length;if(a===b)return c.nodeType===1?c.innerHTML.replace(bm,""):b;if(typeof a=="string"&&!bs.test(a)&&(p.support.htmlSerialize||!bu.test(a))&&(p.support.leadingWhitespace||!bn.test(a))&&!bz[(bp.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(bo,"<$1>$2>");try{for(;d1&&typeof j=="string"&&bw.test(j))return this.each(function(){p(this).domManip(a,c,d)});if(p.isFunction(j))return this.each(function(e){var f=p(this);a[0]=j.call(this,e,c?f.html():b),f.domManip(a,c,d)});if(this[0]){e=p.buildFragment(a,this,k),g=e.fragment,f=g.firstChild,g.childNodes.length===1&&(g=f);if(f){c=c&&p.nodeName(f,"tr");for(h=e.cacheable||l-1;i0?this.clone(!0):this).get(),p(g[e])[b](d),f=f.concat(d);return this.pushStack(f,a,g.selector)}}),p.extend({clone:function(a,b,c){var d,e,f,g;p.support.html5Clone||p.isXMLDoc(a)||!bu.test("<"+a.nodeName+">")?g=a.cloneNode(!0):(bB.innerHTML=a.outerHTML,bB.removeChild(g=bB.firstChild));if((!p.support.noCloneEvent||!p.support.noCloneChecked)&&(a.nodeType===1||a.nodeType===11)&&!p.isXMLDoc(a)){bE(a,g),d=bF(a),e=bF(g);for(f=0;d[f];++f)e[f]&&bE(d[f],e[f])}if(b){bD(a,g);if(c){d=bF(a),e=bF(g);for(f=0;d[f];++f)bD(d[f],e[f])}}return d=e=null,g},clean:function(a,b,c,d){var f,g,h,i,j,k,l,m,n,o,q,r,s=b===e&&bA,t=[];if(!b||typeof b.createDocumentFragment=="undefined")b=e;for(f=0;(h=a[f])!=null;f++){typeof h=="number"&&(h+="");if(!h)continue;if(typeof h=="string")if(!br.test(h))h=b.createTextNode(h);else{s=s||bk(b),l=b.createElement("div"),s.appendChild(l),h=h.replace(bo,"<$1>$2>"),i=(bp.exec(h)||["",""])[1].toLowerCase(),j=bz[i]||bz._default,k=j[0],l.innerHTML=j[1]+h+j[2];while(k--)l=l.lastChild;if(!p.support.tbody){m=bq.test(h),n=i==="table"&&!m?l.firstChild&&l.firstChild.childNodes:j[1]===""&&!m?l.childNodes:[];for(g=n.length-1;g>=0;--g)p.nodeName(n[g],"tbody")&&!n[g].childNodes.length&&n[g].parentNode.removeChild(n[g])}!p.support.leadingWhitespace&&bn.test(h)&&l.insertBefore(b.createTextNode(bn.exec(h)[0]),l.firstChild),h=l.childNodes,l.parentNode.removeChild(l)}h.nodeType?t.push(h):p.merge(t,h)}l&&(h=l=s=null);if(!p.support.appendChecked)for(f=0;(h=t[f])!=null;f++)p.nodeName(h,"input")?bG(h):typeof h.getElementsByTagName!="undefined"&&p.grep(h.getElementsByTagName("input"),bG);if(c){q=function(a){if(!a.type||bx.test(a.type))return d?d.push(a.parentNode?a.parentNode.removeChild(a):a):c.appendChild(a)};for(f=0;(h=t[f])!=null;f++)if(!p.nodeName(h,"script")||!q(h))c.appendChild(h),typeof h.getElementsByTagName!="undefined"&&(r=p.grep(p.merge([],h.getElementsByTagName("script")),q),t.splice.apply(t,[f+1,0].concat(r)),f+=r.length)}return t},cleanData:function(a,b){var c,d,e,f,g=0,h=p.expando,i=p.cache,j=p.support.deleteExpando,k=p.event.special;for(;(e=a[g])!=null;g++)if(b||p.acceptData(e)){d=e[h],c=d&&i[d];if(c){if(c.events)for(f in c.events)k[f]?p.event.remove(e,f):p.removeEvent(e,f,c.handle);i[d]&&(delete i[d],j?delete e[h]:e.removeAttribute?e.removeAttribute(h):e[h]=null,p.deletedIds.push(d))}}}}),function(){var a,b;p.uaMatch=function(a){a=a.toLowerCase();var b=/(chrome)[ \/]([\w.]+)/.exec(a)||/(webkit)[ \/]([\w.]+)/.exec(a)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a)||/(msie) ([\w.]+)/.exec(a)||a.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}},a=p.uaMatch(g.userAgent),b={},a.browser&&(b[a.browser]=!0,b.version=a.version),b.chrome?b.webkit=!0:b.webkit&&(b.safari=!0),p.browser=b,p.sub=function(){function a(b,c){return new a.fn.init(b,c)}p.extend(!0,a,this),a.superclass=this,a.fn=a.prototype=this(),a.fn.constructor=a,a.sub=this.sub,a.fn.init=function c(c,d){return d&&d instanceof p&&!(d instanceof a)&&(d=a(d)),p.fn.init.call(this,c,d,b)},a.fn.init.prototype=a.fn;var b=a(e);return a}}();var bH,bI,bJ,bK=/alpha\([^)]*\)/i,bL=/opacity=([^)]*)/,bM=/^(top|right|bottom|left)$/,bN=/^(none|table(?!-c[ea]).+)/,bO=/^margin/,bP=new RegExp("^("+q+")(.*)$","i"),bQ=new RegExp("^("+q+")(?!px)[a-z%]+$","i"),bR=new RegExp("^([-+])=("+q+")","i"),bS={},bT={position:"absolute",visibility:"hidden",display:"block"},bU={letterSpacing:0,fontWeight:400},bV=["Top","Right","Bottom","Left"],bW=["Webkit","O","Moz","ms"],bX=p.fn.toggle;p.fn.extend({css:function(a,c){return p.access(this,function(a,c,d){return d!==b?p.style(a,c,d):p.css(a,c)},a,c,arguments.length>1)},show:function(){return b$(this,!0)},hide:function(){return b$(this)},toggle:function(a,b){var c=typeof a=="boolean";return p.isFunction(a)&&p.isFunction(b)?bX.apply(this,arguments):this.each(function(){(c?a:bZ(this))?p(this).show():p(this).hide()})}}),p.extend({cssHooks:{opacity:{get:function(a,b){if(b){var c=bH(a,"opacity");return c===""?"1":c}}}},cssNumber:{fillOpacity:!0,fontWeight:!0,lineHeight:!0,opacity:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{"float":p.support.cssFloat?"cssFloat":"styleFloat"},style:function(a,c,d,e){if(!a||a.nodeType===3||a.nodeType===8||!a.style)return;var f,g,h,i=p.camelCase(c),j=a.style;c=p.cssProps[i]||(p.cssProps[i]=bY(j,i)),h=p.cssHooks[c]||p.cssHooks[i];if(d===b)return h&&"get"in h&&(f=h.get(a,!1,e))!==b?f:j[c];g=typeof d,g==="string"&&(f=bR.exec(d))&&(d=(f[1]+1)*f[2]+parseFloat(p.css(a,c)),g="number");if(d==null||g==="number"&&isNaN(d))return;g==="number"&&!p.cssNumber[i]&&(d+="px");if(!h||!("set"in h)||(d=h.set(a,d,e))!==b)try{j[c]=d}catch(k){}},css:function(a,c,d,e){var f,g,h,i=p.camelCase(c);return c=p.cssProps[i]||(p.cssProps[i]=bY(a.style,i)),h=p.cssHooks[c]||p.cssHooks[i],h&&"get"in h&&(f=h.get(a,!0,e)),f===b&&(f=bH(a,c)),f==="normal"&&c in bU&&(f=bU[c]),d||e!==b?(g=parseFloat(f),d||p.isNumeric(g)?g||0:f):f},swap:function(a,b,c){var d,e,f={};for(e in b)f[e]=a.style[e],a.style[e]=b[e];d=c.call(a);for(e in b)a.style[e]=f[e];return d}}),a.getComputedStyle?bH=function(b,c){var d,e,f,g,h=a.getComputedStyle(b,null),i=b.style;return h&&(d=h[c],d===""&&!p.contains(b.ownerDocument,b)&&(d=p.style(b,c)),bQ.test(d)&&bO.test(c)&&(e=i.width,f=i.minWidth,g=i.maxWidth,i.minWidth=i.maxWidth=i.width=d,d=h.width,i.width=e,i.minWidth=f,i.maxWidth=g)),d}:e.documentElement.currentStyle&&(bH=function(a,b){var c,d,e=a.currentStyle&&a.currentStyle[b],f=a.style;return e==null&&f&&f[b]&&(e=f[b]),bQ.test(e)&&!bM.test(b)&&(c=f.left,d=a.runtimeStyle&&a.runtimeStyle.left,d&&(a.runtimeStyle.left=a.currentStyle.left),f.left=b==="fontSize"?"1em":e,e=f.pixelLeft+"px",f.left=c,d&&(a.runtimeStyle.left=d)),e===""?"auto":e}),p.each(["height","width"],function(a,b){p.cssHooks[b]={get:function(a,c,d){if(c)return a.offsetWidth===0&&bN.test(bH(a,"display"))?p.swap(a,bT,function(){return cb(a,b,d)}):cb(a,b,d)},set:function(a,c,d){return b_(a,c,d?ca(a,b,d,p.support.boxSizing&&p.css(a,"boxSizing")==="border-box"):0)}}}),p.support.opacity||(p.cssHooks.opacity={get:function(a,b){return bL.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?.01*parseFloat(RegExp.$1)+"":b?"1":""},set:function(a,b){var c=a.style,d=a.currentStyle,e=p.isNumeric(b)?"alpha(opacity="+b*100+")":"",f=d&&d.filter||c.filter||"";c.zoom=1;if(b>=1&&p.trim(f.replace(bK,""))===""&&c.removeAttribute){c.removeAttribute("filter");if(d&&!d.filter)return}c.filter=bK.test(f)?f.replace(bK,e):f+" "+e}}),p(function(){p.support.reliableMarginRight||(p.cssHooks.marginRight={get:function(a,b){return p.swap(a,{display:"inline-block"},function(){if(b)return bH(a,"marginRight")})}}),!p.support.pixelPosition&&p.fn.position&&p.each(["top","left"],function(a,b){p.cssHooks[b]={get:function(a,c){if(c){var d=bH(a,b);return bQ.test(d)?p(a).position()[b]+"px":d}}}})}),p.expr&&p.expr.filters&&(p.expr.filters.hidden=function(a){return a.offsetWidth===0&&a.offsetHeight===0||!p.support.reliableHiddenOffsets&&(a.style&&a.style.display||bH(a,"display"))==="none"},p.expr.filters.visible=function(a){return!p.expr.filters.hidden(a)}),p.each({margin:"",padding:"",border:"Width"},function(a,b){p.cssHooks[a+b]={expand:function(c){var d,e=typeof c=="string"?c.split(" "):[c],f={};for(d=0;d<4;d++)f[a+bV[d]+b]=e[d]||e[d-2]||e[0];return f}},bO.test(a)||(p.cssHooks[a+b].set=b_)});var cd=/%20/g,ce=/\[\]$/,cf=/\r?\n/g,cg=/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,ch=/^(?:select|textarea)/i;p.fn.extend({serialize:function(){return p.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?p.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||ch.test(this.nodeName)||cg.test(this.type))}).map(function(a,b){var c=p(this).val();return c==null?null:p.isArray(c)?p.map(c,function(a,c){return{name:b.name,value:a.replace(cf,"\r\n")}}):{name:b.name,value:c.replace(cf,"\r\n")}}).get()}}),p.param=function(a,c){var d,e=[],f=function(a,b){b=p.isFunction(b)?b():b==null?"":b,e[e.length]=encodeURIComponent(a)+"="+encodeURIComponent(b)};c===b&&(c=p.ajaxSettings&&p.ajaxSettings.traditional);if(p.isArray(a)||a.jquery&&!p.isPlainObject(a))p.each(a,function(){f(this.name,this.value)});else for(d in a)ci(d,a[d],c,f);return e.join("&").replace(cd,"+")};var cj,ck,cl=/#.*$/,cm=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,cn=/^(?:about|app|app\-storage|.+\-extension|file|res|widget):$/,co=/^(?:GET|HEAD)$/,cp=/^\/\//,cq=/\?/,cr=/