neo-viz 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +9 -0
- data/.livereload +20 -0
- data/.rspec +2 -0
- data/.rvmrc +3 -0
- data/Gemfile +6 -0
- data/LICENSE +19 -0
- data/README.md +120 -0
- data/Rakefile +12 -0
- data/bin/neo-viz +12 -0
- data/config.ru +35 -0
- data/lib/neo-viz.rb +185 -0
- data/lib/neo-viz/version.rb +6 -0
- data/neo-viz.gemspec +37 -0
- data/public/coffeescripts/app_context.coffee +89 -0
- data/public/coffeescripts/canvas_util.coffee +68 -0
- data/public/coffeescripts/event_broker.coffee +16 -0
- data/public/coffeescripts/filters.coffee +113 -0
- data/public/coffeescripts/main.coffee.erb +132 -0
- data/public/coffeescripts/neo4j.coffee +90 -0
- data/public/coffeescripts/renderer.coffee +141 -0
- data/public/coffeescripts/space.coffee +81 -0
- data/public/images/ajax-loader.gif +0 -0
- data/public/javascripts/data.js +45 -0
- data/public/javascripts/data2.js +1287 -0
- data/public/javascripts/main.sprockets.js +9 -0
- data/public/lib/arbor/arbor-tween.js +86 -0
- data/public/lib/arbor/arbor.js +67 -0
- data/public/lib/jQuery/jquery-1.6.1.min.js +18 -0
- data/public/lib/jasmine-1.1.0/MIT.LICENSE +20 -0
- data/public/lib/jasmine-1.1.0/jasmine-html.js +190 -0
- data/public/lib/jasmine-1.1.0/jasmine.css +166 -0
- data/public/lib/jasmine-1.1.0/jasmine.js +2476 -0
- data/public/lib/jasmine-1.1.0/jasmine_favicon.png +0 -0
- data/public/lib/stdlib/stdlib.js +115 -0
- data/public/lib/sylvester-0.1.3/CHANGELOG.txt +29 -0
- data/public/lib/sylvester-0.1.3/sylvester.js +1 -0
- data/public/lib/sylvester-0.1.3/sylvester.js.gz +0 -0
- data/public/lib/sylvester-0.1.3/sylvester.src.js +1254 -0
- data/public/scss/main.scss +152 -0
- data/public/scss/mixins.scss +37 -0
- data/spec/coffeescripts/canvas_util_spec.coffee +4 -0
- data/spec/coffeescripts/filters_spec.coffee +37 -0
- data/spec/coffeescripts/neo4j_spec.coffee +76 -0
- data/spec/neo_viz_spec.rb +48 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/support/struct_matcher.rb +117 -0
- data/views/_filters.haml +22 -0
- data/views/_partial.haml +39 -0
- data/views/embedded.haml +15 -0
- data/views/index.haml +19 -0
- data/views/jasmine_specs_runner.haml +50 -0
- metadata +236 -0
Binary file
|
@@ -0,0 +1,115 @@
|
|
1
|
+
|
2
|
+
// **************************************************************************
|
3
|
+
// Copyright 2007 - 2008 The JSLab Team, Tavs Dokkedahl and Allan Jacobs
|
4
|
+
// Contact: http://www.jslab.dk/contact.php
|
5
|
+
//
|
6
|
+
// This file is part of the JSLab Standard Library (JSL) Program.
|
7
|
+
//
|
8
|
+
// JSL is free software; you can redistribute it and/or modify
|
9
|
+
// it under the terms of the GNU General Public License as published by
|
10
|
+
// the Free Software Foundation; either version 3 of the License, or
|
11
|
+
// any later version.
|
12
|
+
//
|
13
|
+
// JSL is distributed in the hope that it will be useful,
|
14
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
// GNU General Public License for more details.
|
17
|
+
//
|
18
|
+
// You should have received a copy of the GNU General Public License
|
19
|
+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
// ***************************************************************************
|
21
|
+
// File created 2009-01-27 03:05:59
|
22
|
+
|
23
|
+
// Return new array with duplicate values removed
|
24
|
+
Array.prototype.unique =
|
25
|
+
function() {
|
26
|
+
var a = [];
|
27
|
+
var l = this.length;
|
28
|
+
for(var i=0; i<l; i++) {
|
29
|
+
for(var j=i+1; j<l; j++) {
|
30
|
+
// If this[i] is found later in the array
|
31
|
+
if (this[i] === this[j])
|
32
|
+
j = ++i;
|
33
|
+
}
|
34
|
+
a.push(this[i]);
|
35
|
+
}
|
36
|
+
return a;
|
37
|
+
};
|
38
|
+
|
39
|
+
// Compute the intersection of n arrays
|
40
|
+
Array.prototype.intersect =
|
41
|
+
function() {
|
42
|
+
if (!arguments.length)
|
43
|
+
return [];
|
44
|
+
var a1 = this;
|
45
|
+
var a = a2 = null;
|
46
|
+
var n = 0;
|
47
|
+
while(n < arguments.length) {
|
48
|
+
a = [];
|
49
|
+
a2 = arguments[n];
|
50
|
+
var l = a1.length;
|
51
|
+
var l2 = a2.length;
|
52
|
+
for(var i=0; i<l; i++) {
|
53
|
+
for(var j=0; j<l2; j++) {
|
54
|
+
if (a1[i] === a2[j])
|
55
|
+
a.push(a1[i]);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
a1 = a;
|
59
|
+
n++;
|
60
|
+
}
|
61
|
+
return a.unique();
|
62
|
+
};
|
63
|
+
|
64
|
+
// Get the union of n arrays
|
65
|
+
Array.prototype.union =
|
66
|
+
function() {
|
67
|
+
var a = [].concat(this);
|
68
|
+
var l = arguments.length;
|
69
|
+
for(var i=0; i<l; i++) {
|
70
|
+
a = a.concat(arguments[i]);
|
71
|
+
}
|
72
|
+
return a.unique();
|
73
|
+
};
|
74
|
+
|
75
|
+
// Return elements which are in A but not in arg0 through argn
|
76
|
+
Array.prototype.diff =
|
77
|
+
function() {
|
78
|
+
var a1 = this;
|
79
|
+
var a = a2 = null;
|
80
|
+
var n = 0;
|
81
|
+
while(n < arguments.length) {
|
82
|
+
a = [];
|
83
|
+
a2 = arguments[n];
|
84
|
+
var l = a1.length;
|
85
|
+
var l2 = a2.length;
|
86
|
+
var diff = true;
|
87
|
+
for(var i=0; i<l; i++) {
|
88
|
+
for(var j=0; j<l2; j++) {
|
89
|
+
if (a1[i] === a2[j]) {
|
90
|
+
diff = false;
|
91
|
+
break;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
diff ? a.push(a1[i]) : diff = true;
|
95
|
+
}
|
96
|
+
a1 = a;
|
97
|
+
n++;
|
98
|
+
}
|
99
|
+
return a.unique();
|
100
|
+
};
|
101
|
+
|
102
|
+
Array.prototype.contains =
|
103
|
+
function() {
|
104
|
+
if (arguments.length != 1)
|
105
|
+
throw "'contains' only takes one argument"
|
106
|
+
return this.indexOf(arguments[0]) >= 0
|
107
|
+
};
|
108
|
+
|
109
|
+
// Array Remove - By John Resig (MIT Licensed) http://ejohn.org/blog/javascript-array-remove/
|
110
|
+
Array.prototype.remove =
|
111
|
+
function(from, to) {
|
112
|
+
var rest = this.slice((to || from) + 1 || this.length);
|
113
|
+
this.length = from < 0 ? this.length + from : from;
|
114
|
+
return this.push.apply(this, rest);
|
115
|
+
};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Sylvester Changelog
|
2
|
+
-------------------
|
3
|
+
|
4
|
+
|
5
|
+
Version 0.1.0 - 16 April 2007
|
6
|
+
-----------------------------
|
7
|
+
* Initial release
|
8
|
+
* Includes Vector, Matrix, Line and Plane classes
|
9
|
+
|
10
|
+
|
11
|
+
Version 0.1.1 - 13 May 2007
|
12
|
+
---------------------------
|
13
|
+
* Fixed bug in Matrix#augment where some columns got over-written
|
14
|
+
* Added Vector#each and Plane#isPerpendicularTo
|
15
|
+
* Modified all methods that accept vectors as arguments so they also accept plain arrays
|
16
|
+
* Removed sanity checking of vector and matrix elements
|
17
|
+
* Most methods rewritten for speed
|
18
|
+
|
19
|
+
|
20
|
+
Version 0.1.2 - 18 June 2007
|
21
|
+
----------------------------
|
22
|
+
* Fixed bugs caused by variables being passed by reference, not by value
|
23
|
+
* Changed Vector#angleFrom implementation to process faster
|
24
|
+
* Allowed matrix methods to accept plain matrices if they have the correct nested format
|
25
|
+
|
26
|
+
|
27
|
+
Version 0.1.3 - 5 July 2007
|
28
|
+
---------------------------
|
29
|
+
* Fixed variable referencing issue in Line#translate and Plane#translate
|
@@ -0,0 +1 @@
|
|
1
|
+
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 17={3i:\'0.1.3\',16:1e-6};l v(){}v.23={e:l(i){8(i<1||i>7.4.q)?w:7.4[i-1]},2R:l(){8 7.4.q},1u:l(){8 F.1x(7.2u(7))},24:l(a){9 n=7.4.q;9 V=a.4||a;o(n!=V.q){8 1L}J{o(F.13(7.4[n-1]-V[n-1])>17.16){8 1L}}H(--n);8 2x},1q:l(){8 v.u(7.4)},1b:l(a){9 b=[];7.28(l(x,i){b.19(a(x,i))});8 v.u(b)},28:l(a){9 n=7.4.q,k=n,i;J{i=k-n;a(7.4[i],i+1)}H(--n)},2q:l(){9 r=7.1u();o(r===0){8 7.1q()}8 7.1b(l(x){8 x/r})},1C:l(a){9 V=a.4||a;9 n=7.4.q,k=n,i;o(n!=V.q){8 w}9 b=0,1D=0,1F=0;7.28(l(x,i){b+=x*V[i-1];1D+=x*x;1F+=V[i-1]*V[i-1]});1D=F.1x(1D);1F=F.1x(1F);o(1D*1F===0){8 w}9 c=b/(1D*1F);o(c<-1){c=-1}o(c>1){c=1}8 F.37(c)},1m:l(a){9 b=7.1C(a);8(b===w)?w:(b<=17.16)},34:l(a){9 b=7.1C(a);8(b===w)?w:(F.13(b-F.1A)<=17.16)},2k:l(a){9 b=7.2u(a);8(b===w)?w:(F.13(b)<=17.16)},2j:l(a){9 V=a.4||a;o(7.4.q!=V.q){8 w}8 7.1b(l(x,i){8 x+V[i-1]})},2C:l(a){9 V=a.4||a;o(7.4.q!=V.q){8 w}8 7.1b(l(x,i){8 x-V[i-1]})},22:l(k){8 7.1b(l(x){8 x*k})},x:l(k){8 7.22(k)},2u:l(a){9 V=a.4||a;9 i,2g=0,n=7.4.q;o(n!=V.q){8 w}J{2g+=7.4[n-1]*V[n-1]}H(--n);8 2g},2f:l(a){9 B=a.4||a;o(7.4.q!=3||B.q!=3){8 w}9 A=7.4;8 v.u([(A[1]*B[2])-(A[2]*B[1]),(A[2]*B[0])-(A[0]*B[2]),(A[0]*B[1])-(A[1]*B[0])])},2A:l(){9 m=0,n=7.4.q,k=n,i;J{i=k-n;o(F.13(7.4[i])>F.13(m)){m=7.4[i]}}H(--n);8 m},2Z:l(x){9 a=w,n=7.4.q,k=n,i;J{i=k-n;o(a===w&&7.4[i]==x){a=i+1}}H(--n);8 a},3g:l(){8 S.2X(7.4)},2d:l(){8 7.1b(l(x){8 F.2d(x)})},2V:l(x){8 7.1b(l(y){8(F.13(y-x)<=17.16)?x:y})},1o:l(a){o(a.K){8 a.1o(7)}9 V=a.4||a;o(V.q!=7.4.q){8 w}9 b=0,2b;7.28(l(x,i){2b=x-V[i-1];b+=2b*2b});8 F.1x(b)},3a:l(a){8 a.1h(7)},2T:l(a){8 a.1h(7)},1V:l(t,a){9 V,R,x,y,z;2S(7.4.q){27 2:V=a.4||a;o(V.q!=2){8 w}R=S.1R(t).4;x=7.4[0]-V[0];y=7.4[1]-V[1];8 v.u([V[0]+R[0][0]*x+R[0][1]*y,V[1]+R[1][0]*x+R[1][1]*y]);1I;27 3:o(!a.U){8 w}9 C=a.1r(7).4;R=S.1R(t,a.U).4;x=7.4[0]-C[0];y=7.4[1]-C[1];z=7.4[2]-C[2];8 v.u([C[0]+R[0][0]*x+R[0][1]*y+R[0][2]*z,C[1]+R[1][0]*x+R[1][1]*y+R[1][2]*z,C[2]+R[2][0]*x+R[2][1]*y+R[2][2]*z]);1I;2P:8 w}},1t:l(a){o(a.K){9 P=7.4.2O();9 C=a.1r(P).4;8 v.u([C[0]+(C[0]-P[0]),C[1]+(C[1]-P[1]),C[2]+(C[2]-(P[2]||0))])}1d{9 Q=a.4||a;o(7.4.q!=Q.q){8 w}8 7.1b(l(x,i){8 Q[i-1]+(Q[i-1]-x)})}},1N:l(){9 V=7.1q();2S(V.4.q){27 3:1I;27 2:V.4.19(0);1I;2P:8 w}8 V},2n:l(){8\'[\'+7.4.2K(\', \')+\']\'},26:l(a){7.4=(a.4||a).2O();8 7}};v.u=l(a){9 V=25 v();8 V.26(a)};v.i=v.u([1,0,0]);v.j=v.u([0,1,0]);v.k=v.u([0,0,1]);v.2J=l(n){9 a=[];J{a.19(F.2F())}H(--n);8 v.u(a)};v.1j=l(n){9 a=[];J{a.19(0)}H(--n);8 v.u(a)};l S(){}S.23={e:l(i,j){o(i<1||i>7.4.q||j<1||j>7.4[0].q){8 w}8 7.4[i-1][j-1]},33:l(i){o(i>7.4.q){8 w}8 v.u(7.4[i-1])},2E:l(j){o(j>7.4[0].q){8 w}9 a=[],n=7.4.q,k=n,i;J{i=k-n;a.19(7.4[i][j-1])}H(--n);8 v.u(a)},2R:l(){8{2D:7.4.q,1p:7.4[0].q}},2D:l(){8 7.4.q},1p:l(){8 7.4[0].q},24:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(7.4.q!=M.q||7.4[0].q!=M[0].q){8 1L}9 b=7.4.q,15=b,i,G,10=7.4[0].q,j;J{i=15-b;G=10;J{j=10-G;o(F.13(7.4[i][j]-M[i][j])>17.16){8 1L}}H(--G)}H(--b);8 2x},1q:l(){8 S.u(7.4)},1b:l(a){9 b=[],12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;b[i]=[];J{j=10-G;b[i][j]=a(7.4[i][j],i+1,j+1)}H(--G)}H(--12);8 S.u(b)},2i:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}8(7.4.q==M.q&&7.4[0].q==M[0].q)},2j:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2i(M)){8 w}8 7.1b(l(x,i,j){8 x+M[i-1][j-1]})},2C:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2i(M)){8 w}8 7.1b(l(x,i,j){8 x-M[i-1][j-1]})},2B:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}8(7.4[0].q==M.q)},22:l(a){o(!a.4){8 7.1b(l(x){8 x*a})}9 b=a.1u?2x:1L;9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2B(M)){8 w}9 d=7.4.q,15=d,i,G,10=M[0].q,j;9 e=7.4[0].q,4=[],21,20,c;J{i=15-d;4[i]=[];G=10;J{j=10-G;21=0;20=e;J{c=e-20;21+=7.4[i][c]*M[c][j]}H(--20);4[i][j]=21}H(--G)}H(--d);9 M=S.u(4);8 b?M.2E(1):M},x:l(a){8 7.22(a)},32:l(a,b,c,d){9 e=[],12=c,i,G,j;9 f=7.4.q,1p=7.4[0].q;J{i=c-12;e[i]=[];G=d;J{j=d-G;e[i][j]=7.4[(a+i-1)%f][(b+j-1)%1p]}H(--G)}H(--12);8 S.u(e)},31:l(){9 a=7.4.q,1p=7.4[0].q;9 b=[],12=1p,i,G,j;J{i=1p-12;b[i]=[];G=a;J{j=a-G;b[i][j]=7.4[j][i]}H(--G)}H(--12);8 S.u(b)},1y:l(){8(7.4.q==7.4[0].q)},2A:l(){9 m=0,12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;J{j=10-G;o(F.13(7.4[i][j])>F.13(m)){m=7.4[i][j]}}H(--G)}H(--12);8 m},2Z:l(x){9 a=w,12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;J{j=10-G;o(7.4[i][j]==x){8{i:i+1,j:j+1}}}H(--G)}H(--12);8 w},30:l(){o(!7.1y){8 w}9 a=[],n=7.4.q,k=n,i;J{i=k-n;a.19(7.4[i][i])}H(--n);8 v.u(a)},1K:l(){9 M=7.1q(),1c;9 n=7.4.q,k=n,i,1s,1n=7.4[0].q,p;J{i=k-n;o(M.4[i][i]==0){2e(j=i+1;j<k;j++){o(M.4[j][i]!=0){1c=[];1s=1n;J{p=1n-1s;1c.19(M.4[i][p]+M.4[j][p])}H(--1s);M.4[i]=1c;1I}}}o(M.4[i][i]!=0){2e(j=i+1;j<k;j++){9 a=M.4[j][i]/M.4[i][i];1c=[];1s=1n;J{p=1n-1s;1c.19(p<=i?0:M.4[j][p]-M.4[i][p]*a)}H(--1s);M.4[j]=1c}}}H(--n);8 M},3h:l(){8 7.1K()},2z:l(){o(!7.1y()){8 w}9 M=7.1K();9 a=M.4[0][0],n=M.4.q-1,k=n,i;J{i=k-n+1;a=a*M.4[i][i]}H(--n);8 a},3f:l(){8 7.2z()},2y:l(){8(7.1y()&&7.2z()===0)},2Y:l(){o(!7.1y()){8 w}9 a=7.4[0][0],n=7.4.q-1,k=n,i;J{i=k-n+1;a+=7.4[i][i]}H(--n);8 a},3e:l(){8 7.2Y()},1Y:l(){9 M=7.1K(),1Y=0;9 a=7.4.q,15=a,i,G,10=7.4[0].q,j;J{i=15-a;G=10;J{j=10-G;o(F.13(M.4[i][j])>17.16){1Y++;1I}}H(--G)}H(--a);8 1Y},3d:l(){8 7.1Y()},2W:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}9 T=7.1q(),1p=T.4[0].q;9 b=T.4.q,15=b,i,G,10=M[0].q,j;o(b!=M.q){8 w}J{i=15-b;G=10;J{j=10-G;T.4[i][1p+j]=M[i][j]}H(--G)}H(--b);8 T},2w:l(){o(!7.1y()||7.2y()){8 w}9 a=7.4.q,15=a,i,j;9 M=7.2W(S.I(a)).1K();9 b,1n=M.4[0].q,p,1c,2v;9 c=[],2c;J{i=a-1;1c=[];b=1n;c[i]=[];2v=M.4[i][i];J{p=1n-b;2c=M.4[i][p]/2v;1c.19(2c);o(p>=15){c[i].19(2c)}}H(--b);M.4[i]=1c;2e(j=0;j<i;j++){1c=[];b=1n;J{p=1n-b;1c.19(M.4[j][p]-M.4[i][p]*M.4[j][i])}H(--b);M.4[j]=1c}}H(--a);8 S.u(c)},3c:l(){8 7.2w()},2d:l(){8 7.1b(l(x){8 F.2d(x)})},2V:l(x){8 7.1b(l(p){8(F.13(p-x)<=17.16)?x:p})},2n:l(){9 a=[];9 n=7.4.q,k=n,i;J{i=k-n;a.19(v.u(7.4[i]).2n())}H(--n);8 a.2K(\'\\n\')},26:l(a){9 i,4=a.4||a;o(1g(4[0][0])!=\'1f\'){9 b=4.q,15=b,G,10,j;7.4=[];J{i=15-b;G=4[i].q;10=G;7.4[i]=[];J{j=10-G;7.4[i][j]=4[i][j]}H(--G)}H(--b);8 7}9 n=4.q,k=n;7.4=[];J{i=k-n;7.4.19([4[i]])}H(--n);8 7}};S.u=l(a){9 M=25 S();8 M.26(a)};S.I=l(n){9 a=[],k=n,i,G,j;J{i=k-n;a[i]=[];G=k;J{j=k-G;a[i][j]=(i==j)?1:0}H(--G)}H(--n);8 S.u(a)};S.2X=l(a){9 n=a.q,k=n,i;9 M=S.I(n);J{i=k-n;M.4[i][i]=a[i]}H(--n);8 M};S.1R=l(b,a){o(!a){8 S.u([[F.1H(b),-F.1G(b)],[F.1G(b),F.1H(b)]])}9 d=a.1q();o(d.4.q!=3){8 w}9 e=d.1u();9 x=d.4[0]/e,y=d.4[1]/e,z=d.4[2]/e;9 s=F.1G(b),c=F.1H(b),t=1-c;8 S.u([[t*x*x+c,t*x*y-s*z,t*x*z+s*y],[t*x*y+s*z,t*y*y+c,t*y*z-s*x],[t*x*z-s*y,t*y*z+s*x,t*z*z+c]])};S.3b=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[1,0,0],[0,c,-s],[0,s,c]])};S.39=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[c,0,s],[0,1,0],[-s,0,c]])};S.38=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[c,-s,0],[s,c,0],[0,0,1]])};S.2J=l(n,m){8 S.1j(n,m).1b(l(){8 F.2F()})};S.1j=l(n,m){9 a=[],12=n,i,G,j;J{i=n-12;a[i]=[];G=m;J{j=m-G;a[i][j]=0}H(--G)}H(--12);8 S.u(a)};l 14(){}14.23={24:l(a){8(7.1m(a)&&7.1h(a.K))},1q:l(){8 14.u(7.K,7.U)},2U:l(a){9 V=a.4||a;8 14.u([7.K.4[0]+V[0],7.K.4[1]+V[1],7.K.4[2]+(V[2]||0)],7.U)},1m:l(a){o(a.W){8 a.1m(7)}9 b=7.U.1C(a.U);8(F.13(b)<=17.16||F.13(b-F.1A)<=17.16)},1o:l(a){o(a.W){8 a.1o(7)}o(a.U){o(7.1m(a)){8 7.1o(a.K)}9 N=7.U.2f(a.U).2q().4;9 A=7.K.4,B=a.K.4;8 F.13((A[0]-B[0])*N[0]+(A[1]-B[1])*N[1]+(A[2]-B[2])*N[2])}1d{9 P=a.4||a;9 A=7.K.4,D=7.U.4;9 b=P[0]-A[0],2a=P[1]-A[1],29=(P[2]||0)-A[2];9 c=F.1x(b*b+2a*2a+29*29);o(c===0)8 0;9 d=(b*D[0]+2a*D[1]+29*D[2])/c;9 e=1-d*d;8 F.13(c*F.1x(e<0?0:e))}},1h:l(a){9 b=7.1o(a);8(b!==w&&b<=17.16)},2T:l(a){8 a.1h(7)},1v:l(a){o(a.W){8 a.1v(7)}8(!7.1m(a)&&7.1o(a)<=17.16)},1U:l(a){o(a.W){8 a.1U(7)}o(!7.1v(a)){8 w}9 P=7.K.4,X=7.U.4,Q=a.K.4,Y=a.U.4;9 b=X[0],1z=X[1],1B=X[2],1T=Y[0],1S=Y[1],1M=Y[2];9 c=P[0]-Q[0],2s=P[1]-Q[1],2r=P[2]-Q[2];9 d=-b*c-1z*2s-1B*2r;9 e=1T*c+1S*2s+1M*2r;9 f=b*b+1z*1z+1B*1B;9 g=1T*1T+1S*1S+1M*1M;9 h=b*1T+1z*1S+1B*1M;9 k=(d*g/f+h*e)/(g-h*h);8 v.u([P[0]+k*b,P[1]+k*1z,P[2]+k*1B])},1r:l(a){o(a.U){o(7.1v(a)){8 7.1U(a)}o(7.1m(a)){8 w}9 D=7.U.4,E=a.U.4;9 b=D[0],1l=D[1],1k=D[2],1P=E[0],1O=E[1],1Q=E[2];9 x=(1k*1P-b*1Q),y=(b*1O-1l*1P),z=(1l*1Q-1k*1O);9 N=v.u([x*1Q-y*1O,y*1P-z*1Q,z*1O-x*1P]);9 P=11.u(a.K,N);8 P.1U(7)}1d{9 P=a.4||a;o(7.1h(P)){8 v.u(P)}9 A=7.K.4,D=7.U.4;9 b=D[0],1l=D[1],1k=D[2],1w=A[0],18=A[1],1a=A[2];9 x=b*(P[1]-18)-1l*(P[0]-1w),y=1l*((P[2]||0)-1a)-1k*(P[1]-18),z=1k*(P[0]-1w)-b*((P[2]||0)-1a);9 V=v.u([1l*x-1k*z,1k*y-b*x,b*z-1l*y]);9 k=7.1o(P)/V.1u();8 v.u([P[0]+V.4[0]*k,P[1]+V.4[1]*k,(P[2]||0)+V.4[2]*k])}},1V:l(t,a){o(1g(a.U)==\'1f\'){a=14.u(a.1N(),v.k)}9 R=S.1R(t,a.U).4;9 C=a.1r(7.K).4;9 A=7.K.4,D=7.U.4;9 b=C[0],1E=C[1],1J=C[2],1w=A[0],18=A[1],1a=A[2];9 x=1w-b,y=18-1E,z=1a-1J;8 14.u([b+R[0][0]*x+R[0][1]*y+R[0][2]*z,1E+R[1][0]*x+R[1][1]*y+R[1][2]*z,1J+R[2][0]*x+R[2][1]*y+R[2][2]*z],[R[0][0]*D[0]+R[0][1]*D[1]+R[0][2]*D[2],R[1][0]*D[0]+R[1][1]*D[1]+R[1][2]*D[2],R[2][0]*D[0]+R[2][1]*D[1]+R[2][2]*D[2]])},1t:l(a){o(a.W){9 A=7.K.4,D=7.U.4;9 b=A[0],18=A[1],1a=A[2],2N=D[0],1l=D[1],1k=D[2];9 c=7.K.1t(a).4;9 d=b+2N,2h=18+1l,2o=1a+1k;9 Q=a.1r([d,2h,2o]).4;9 e=[Q[0]+(Q[0]-d)-c[0],Q[1]+(Q[1]-2h)-c[1],Q[2]+(Q[2]-2o)-c[2]];8 14.u(c,e)}1d o(a.U){8 7.1V(F.1A,a)}1d{9 P=a.4||a;8 14.u(7.K.1t([P[0],P[1],(P[2]||0)]),7.U)}},1Z:l(a,b){a=v.u(a);b=v.u(b);o(a.4.q==2){a.4.19(0)}o(b.4.q==2){b.4.19(0)}o(a.4.q>3||b.4.q>3){8 w}9 c=b.1u();o(c===0){8 w}7.K=a;7.U=v.u([b.4[0]/c,b.4[1]/c,b.4[2]/c]);8 7}};14.u=l(a,b){9 L=25 14();8 L.1Z(a,b)};14.X=14.u(v.1j(3),v.i);14.Y=14.u(v.1j(3),v.j);14.Z=14.u(v.1j(3),v.k);l 11(){}11.23={24:l(a){8(7.1h(a.K)&&7.1m(a))},1q:l(){8 11.u(7.K,7.W)},2U:l(a){9 V=a.4||a;8 11.u([7.K.4[0]+V[0],7.K.4[1]+V[1],7.K.4[2]+(V[2]||0)],7.W)},1m:l(a){9 b;o(a.W){b=7.W.1C(a.W);8(F.13(b)<=17.16||F.13(F.1A-b)<=17.16)}1d o(a.U){8 7.W.2k(a.U)}8 w},2k:l(a){9 b=7.W.1C(a.W);8(F.13(F.1A/2-b)<=17.16)},1o:l(a){o(7.1v(a)||7.1h(a)){8 0}o(a.K){9 A=7.K.4,B=a.K.4,N=7.W.4;8 F.13((A[0]-B[0])*N[0]+(A[1]-B[1])*N[1]+(A[2]-B[2])*N[2])}1d{9 P=a.4||a;9 A=7.K.4,N=7.W.4;8 F.13((A[0]-P[0])*N[0]+(A[1]-P[1])*N[1]+(A[2]-(P[2]||0))*N[2])}},1h:l(a){o(a.W){8 w}o(a.U){8(7.1h(a.K)&&7.1h(a.K.2j(a.U)))}1d{9 P=a.4||a;9 A=7.K.4,N=7.W.4;9 b=F.13(N[0]*(A[0]-P[0])+N[1]*(A[1]-P[1])+N[2]*(A[2]-(P[2]||0)));8(b<=17.16)}},1v:l(a){o(1g(a.U)==\'1f\'&&1g(a.W)==\'1f\'){8 w}8!7.1m(a)},1U:l(a){o(!7.1v(a)){8 w}o(a.U){9 A=a.K.4,D=a.U.4,P=7.K.4,N=7.W.4;9 b=(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]);8 v.u([A[0]+D[0]*b,A[1]+D[1]*b,A[2]+D[2]*b])}1d o(a.W){9 c=7.W.2f(a.W).2q();9 N=7.W.4,A=7.K.4,O=a.W.4,B=a.K.4;9 d=S.1j(2,2),i=0;H(d.2y()){i++;d=S.u([[N[i%3],N[(i+1)%3]],[O[i%3],O[(i+1)%3]]])}9 e=d.2w().4;9 x=N[0]*A[0]+N[1]*A[1]+N[2]*A[2];9 y=O[0]*B[0]+O[1]*B[1]+O[2]*B[2];9 f=[e[0][0]*x+e[0][1]*y,e[1][0]*x+e[1][1]*y];9 g=[];2e(9 j=1;j<=3;j++){g.19((i==j)?0:f[(j+(5-i)%3)%3])}8 14.u(g,c)}},1r:l(a){9 P=a.4||a;9 A=7.K.4,N=7.W.4;9 b=(A[0]-P[0])*N[0]+(A[1]-P[1])*N[1]+(A[2]-(P[2]||0))*N[2];8 v.u([P[0]+N[0]*b,P[1]+N[1]*b,(P[2]||0)+N[2]*b])},1V:l(t,a){9 R=S.1R(t,a.U).4;9 C=a.1r(7.K).4;9 A=7.K.4,N=7.W.4;9 b=C[0],1E=C[1],1J=C[2],1w=A[0],18=A[1],1a=A[2];9 x=1w-b,y=18-1E,z=1a-1J;8 11.u([b+R[0][0]*x+R[0][1]*y+R[0][2]*z,1E+R[1][0]*x+R[1][1]*y+R[1][2]*z,1J+R[2][0]*x+R[2][1]*y+R[2][2]*z],[R[0][0]*N[0]+R[0][1]*N[1]+R[0][2]*N[2],R[1][0]*N[0]+R[1][1]*N[1]+R[1][2]*N[2],R[2][0]*N[0]+R[2][1]*N[1]+R[2][2]*N[2]])},1t:l(a){o(a.W){9 A=7.K.4,N=7.W.4;9 b=A[0],18=A[1],1a=A[2],2M=N[0],2L=N[1],2Q=N[2];9 c=7.K.1t(a).4;9 d=b+2M,2p=18+2L,2m=1a+2Q;9 Q=a.1r([d,2p,2m]).4;9 e=[Q[0]+(Q[0]-d)-c[0],Q[1]+(Q[1]-2p)-c[1],Q[2]+(Q[2]-2m)-c[2]];8 11.u(c,e)}1d o(a.U){8 7.1V(F.1A,a)}1d{9 P=a.4||a;8 11.u(7.K.1t([P[0],P[1],(P[2]||0)]),7.W)}},1Z:l(a,b,c){a=v.u(a);a=a.1N();o(a===w){8 w}b=v.u(b);b=b.1N();o(b===w){8 w}o(1g(c)==\'1f\'){c=w}1d{c=v.u(c);c=c.1N();o(c===w){8 w}}9 d=a.4[0],18=a.4[1],1a=a.4[2];9 e=b.4[0],1W=b.4[1],1X=b.4[2];9 f,1i;o(c!==w){9 g=c.4[0],2l=c.4[1],2t=c.4[2];f=v.u([(1W-18)*(2t-1a)-(1X-1a)*(2l-18),(1X-1a)*(g-d)-(e-d)*(2t-1a),(e-d)*(2l-18)-(1W-18)*(g-d)]);1i=f.1u();o(1i===0){8 w}f=v.u([f.4[0]/1i,f.4[1]/1i,f.4[2]/1i])}1d{1i=F.1x(e*e+1W*1W+1X*1X);o(1i===0){8 w}f=v.u([b.4[0]/1i,b.4[1]/1i,b.4[2]/1i])}7.K=a;7.W=f;8 7}};11.u=l(a,b,c){9 P=25 11();8 P.1Z(a,b,c)};11.2I=11.u(v.1j(3),v.k);11.2H=11.u(v.1j(3),v.i);11.2G=11.u(v.1j(3),v.j);11.36=11.2I;11.35=11.2H;11.3j=11.2G;9 $V=v.u;9 $M=S.u;9 $L=14.u;9 $P=11.u;',62,206,'||||elements|||this|return|var||||||||||||function|||if||length||||create|Vector|null|||||||||Math|nj|while||do|anchor||||||||Matrix||direction||normal||||kj|Plane|ni|abs|Line|ki|precision|Sylvester|A2|push|A3|map|els|else||undefined|typeof|contains|mod|Zero|D3|D2|isParallelTo|kp|distanceFrom|cols|dup|pointClosestTo|np|reflectionIn|modulus|intersects|A1|sqrt|isSquare|X2|PI|X3|angleFrom|mod1|C2|mod2|sin|cos|break|C3|toRightTriangular|false|Y3|to3D|E2|E1|E3|Rotation|Y2|Y1|intersectionWith|rotate|v12|v13|rank|setVectors|nc|sum|multiply|prototype|eql|new|setElements|case|each|PA3|PA2|part|new_element|round|for|cross|product|AD2|isSameSizeAs|add|isPerpendicularTo|v22|AN3|inspect|AD3|AN2|toUnitVector|PsubQ3|PsubQ2|v23|dot|divisor|inverse|true|isSingular|determinant|max|canMultiplyFromLeft|subtract|rows|col|random|ZX|YZ|XY|Random|join|N2|N1|D1|slice|default|N3|dimensions|switch|liesIn|translate|snapTo|augment|Diagonal|trace|indexOf|diagonal|transpose|minor|row|isAntiparallelTo|ZY|YX|acos|RotationZ|RotationY|liesOn|RotationX|inv|rk|tr|det|toDiagonalMatrix|toUpperTriangular|version|XZ'.split('|'),0,{}))
|
Binary file
|
@@ -0,0 +1,1254 @@
|
|
1
|
+
// === Sylvester ===
|
2
|
+
// Vector and Matrix mathematics modules for JavaScript
|
3
|
+
// Copyright (c) 2007 James Coglan
|
4
|
+
//
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
// a copy of this software and associated documentation files (the "Software"),
|
7
|
+
// to deal in the Software without restriction, including without limitation
|
8
|
+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
9
|
+
// and/or sell copies of the Software, and to permit persons to whom the
|
10
|
+
// Software is furnished to do so, subject to the following conditions:
|
11
|
+
//
|
12
|
+
// The above copyright notice and this permission notice shall be included
|
13
|
+
// in all copies or substantial portions of the Software.
|
14
|
+
//
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
16
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
18
|
+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
20
|
+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
21
|
+
// DEALINGS IN THE SOFTWARE.
|
22
|
+
|
23
|
+
var Sylvester = {
|
24
|
+
version: '0.1.3',
|
25
|
+
precision: 1e-6
|
26
|
+
};
|
27
|
+
|
28
|
+
function Vector() {}
|
29
|
+
Vector.prototype = {
|
30
|
+
|
31
|
+
// Returns element i of the vector
|
32
|
+
e: function(i) {
|
33
|
+
return (i < 1 || i > this.elements.length) ? null : this.elements[i-1];
|
34
|
+
},
|
35
|
+
|
36
|
+
// Returns the number of elements the vector has
|
37
|
+
dimensions: function() {
|
38
|
+
return this.elements.length;
|
39
|
+
},
|
40
|
+
|
41
|
+
// Returns the modulus ('length') of the vector
|
42
|
+
modulus: function() {
|
43
|
+
return Math.sqrt(this.dot(this));
|
44
|
+
},
|
45
|
+
|
46
|
+
// Returns true iff the vector is equal to the argument
|
47
|
+
eql: function(vector) {
|
48
|
+
var n = this.elements.length;
|
49
|
+
var V = vector.elements || vector;
|
50
|
+
if (n != V.length) { return false; }
|
51
|
+
do {
|
52
|
+
if (Math.abs(this.elements[n-1] - V[n-1]) > Sylvester.precision) { return false; }
|
53
|
+
} while (--n);
|
54
|
+
return true;
|
55
|
+
},
|
56
|
+
|
57
|
+
// Returns a copy of the vector
|
58
|
+
dup: function() {
|
59
|
+
return Vector.create(this.elements);
|
60
|
+
},
|
61
|
+
|
62
|
+
// Maps the vector to another vector according to the given function
|
63
|
+
map: function(fn) {
|
64
|
+
var elements = [];
|
65
|
+
this.each(function(x, i) {
|
66
|
+
elements.push(fn(x, i));
|
67
|
+
});
|
68
|
+
return Vector.create(elements);
|
69
|
+
},
|
70
|
+
|
71
|
+
// Calls the iterator for each element of the vector in turn
|
72
|
+
each: function(fn) {
|
73
|
+
var n = this.elements.length, k = n, i;
|
74
|
+
do { i = k - n;
|
75
|
+
fn(this.elements[i], i+1);
|
76
|
+
} while (--n);
|
77
|
+
},
|
78
|
+
|
79
|
+
// Returns a new vector created by normalizing the receiver
|
80
|
+
toUnitVector: function() {
|
81
|
+
var r = this.modulus();
|
82
|
+
if (r === 0) { return this.dup(); }
|
83
|
+
return this.map(function(x) { return x/r; });
|
84
|
+
},
|
85
|
+
|
86
|
+
// Returns the angle between the vector and the argument (also a vector)
|
87
|
+
angleFrom: function(vector) {
|
88
|
+
var V = vector.elements || vector;
|
89
|
+
var n = this.elements.length, k = n, i;
|
90
|
+
if (n != V.length) { return null; }
|
91
|
+
var dot = 0, mod1 = 0, mod2 = 0;
|
92
|
+
// Work things out in parallel to save time
|
93
|
+
this.each(function(x, i) {
|
94
|
+
dot += x * V[i-1];
|
95
|
+
mod1 += x * x;
|
96
|
+
mod2 += V[i-1] * V[i-1];
|
97
|
+
});
|
98
|
+
mod1 = Math.sqrt(mod1); mod2 = Math.sqrt(mod2);
|
99
|
+
if (mod1*mod2 === 0) { return null; }
|
100
|
+
var theta = dot / (mod1*mod2);
|
101
|
+
if (theta < -1) { theta = -1; }
|
102
|
+
if (theta > 1) { theta = 1; }
|
103
|
+
return Math.acos(theta);
|
104
|
+
},
|
105
|
+
|
106
|
+
// Returns true iff the vector is parallel to the argument
|
107
|
+
isParallelTo: function(vector) {
|
108
|
+
var angle = this.angleFrom(vector);
|
109
|
+
return (angle === null) ? null : (angle <= Sylvester.precision);
|
110
|
+
},
|
111
|
+
|
112
|
+
// Returns true iff the vector is antiparallel to the argument
|
113
|
+
isAntiparallelTo: function(vector) {
|
114
|
+
var angle = this.angleFrom(vector);
|
115
|
+
return (angle === null) ? null : (Math.abs(angle - Math.PI) <= Sylvester.precision);
|
116
|
+
},
|
117
|
+
|
118
|
+
// Returns true iff the vector is perpendicular to the argument
|
119
|
+
isPerpendicularTo: function(vector) {
|
120
|
+
var dot = this.dot(vector);
|
121
|
+
return (dot === null) ? null : (Math.abs(dot) <= Sylvester.precision);
|
122
|
+
},
|
123
|
+
|
124
|
+
// Returns the result of adding the argument to the vector
|
125
|
+
add: function(vector) {
|
126
|
+
var V = vector.elements || vector;
|
127
|
+
if (this.elements.length != V.length) { return null; }
|
128
|
+
return this.map(function(x, i) { return x + V[i-1]; });
|
129
|
+
},
|
130
|
+
|
131
|
+
// Returns the result of subtracting the argument from the vector
|
132
|
+
subtract: function(vector) {
|
133
|
+
var V = vector.elements || vector;
|
134
|
+
if (this.elements.length != V.length) { return null; }
|
135
|
+
return this.map(function(x, i) { return x - V[i-1]; });
|
136
|
+
},
|
137
|
+
|
138
|
+
// Returns the result of multiplying the elements of the vector by the argument
|
139
|
+
multiply: function(k) {
|
140
|
+
return this.map(function(x) { return x*k; });
|
141
|
+
},
|
142
|
+
|
143
|
+
x: function(k) { return this.multiply(k); },
|
144
|
+
|
145
|
+
// Returns the scalar product of the vector with the argument
|
146
|
+
// Both vectors must have equal dimensionality
|
147
|
+
dot: function(vector) {
|
148
|
+
var V = vector.elements || vector;
|
149
|
+
var i, product = 0, n = this.elements.length;
|
150
|
+
if (n != V.length) { return null; }
|
151
|
+
do { product += this.elements[n-1] * V[n-1]; } while (--n);
|
152
|
+
return product;
|
153
|
+
},
|
154
|
+
|
155
|
+
// Returns the vector product of the vector with the argument
|
156
|
+
// Both vectors must have dimensionality 3
|
157
|
+
cross: function(vector) {
|
158
|
+
var B = vector.elements || vector;
|
159
|
+
if (this.elements.length != 3 || B.length != 3) { return null; }
|
160
|
+
var A = this.elements;
|
161
|
+
return Vector.create([
|
162
|
+
(A[1] * B[2]) - (A[2] * B[1]),
|
163
|
+
(A[2] * B[0]) - (A[0] * B[2]),
|
164
|
+
(A[0] * B[1]) - (A[1] * B[0])
|
165
|
+
]);
|
166
|
+
},
|
167
|
+
|
168
|
+
// Returns the (absolute) largest element of the vector
|
169
|
+
max: function() {
|
170
|
+
var m = 0, n = this.elements.length, k = n, i;
|
171
|
+
do { i = k - n;
|
172
|
+
if (Math.abs(this.elements[i]) > Math.abs(m)) { m = this.elements[i]; }
|
173
|
+
} while (--n);
|
174
|
+
return m;
|
175
|
+
},
|
176
|
+
|
177
|
+
// Returns the index of the first match found
|
178
|
+
indexOf: function(x) {
|
179
|
+
var index = null, n = this.elements.length, k = n, i;
|
180
|
+
do { i = k - n;
|
181
|
+
if (index === null && this.elements[i] == x) {
|
182
|
+
index = i + 1;
|
183
|
+
}
|
184
|
+
} while (--n);
|
185
|
+
return index;
|
186
|
+
},
|
187
|
+
|
188
|
+
// Returns a diagonal matrix with the vector's elements as its diagonal elements
|
189
|
+
toDiagonalMatrix: function() {
|
190
|
+
return Matrix.Diagonal(this.elements);
|
191
|
+
},
|
192
|
+
|
193
|
+
// Returns the result of rounding the elements of the vector
|
194
|
+
round: function() {
|
195
|
+
return this.map(function(x) { return Math.round(x); });
|
196
|
+
},
|
197
|
+
|
198
|
+
// Returns a copy of the vector with elements set to the given value if they
|
199
|
+
// differ from it by less than Sylvester.precision
|
200
|
+
snapTo: function(x) {
|
201
|
+
return this.map(function(y) {
|
202
|
+
return (Math.abs(y - x) <= Sylvester.precision) ? x : y;
|
203
|
+
});
|
204
|
+
},
|
205
|
+
|
206
|
+
// Returns the vector's distance from the argument, when considered as a point in space
|
207
|
+
distanceFrom: function(obj) {
|
208
|
+
if (obj.anchor) { return obj.distanceFrom(this); }
|
209
|
+
var V = obj.elements || obj;
|
210
|
+
if (V.length != this.elements.length) { return null; }
|
211
|
+
var sum = 0, part;
|
212
|
+
this.each(function(x, i) {
|
213
|
+
part = x - V[i-1];
|
214
|
+
sum += part * part;
|
215
|
+
});
|
216
|
+
return Math.sqrt(sum);
|
217
|
+
},
|
218
|
+
|
219
|
+
// Returns true if the vector is point on the given line
|
220
|
+
liesOn: function(line) {
|
221
|
+
return line.contains(this);
|
222
|
+
},
|
223
|
+
|
224
|
+
// Return true iff the vector is a point in the given plane
|
225
|
+
liesIn: function(plane) {
|
226
|
+
return plane.contains(this);
|
227
|
+
},
|
228
|
+
|
229
|
+
// Rotates the vector about the given object. The object should be a
|
230
|
+
// point if the vector is 2D, and a line if it is 3D. Be careful with line directions!
|
231
|
+
rotate: function(t, obj) {
|
232
|
+
var V, R, x, y, z;
|
233
|
+
switch (this.elements.length) {
|
234
|
+
case 2:
|
235
|
+
V = obj.elements || obj;
|
236
|
+
if (V.length != 2) { return null; }
|
237
|
+
R = Matrix.Rotation(t).elements;
|
238
|
+
x = this.elements[0] - V[0];
|
239
|
+
y = this.elements[1] - V[1];
|
240
|
+
return Vector.create([
|
241
|
+
V[0] + R[0][0] * x + R[0][1] * y,
|
242
|
+
V[1] + R[1][0] * x + R[1][1] * y
|
243
|
+
]);
|
244
|
+
break;
|
245
|
+
case 3:
|
246
|
+
if (!obj.direction) { return null; }
|
247
|
+
var C = obj.pointClosestTo(this).elements;
|
248
|
+
R = Matrix.Rotation(t, obj.direction).elements;
|
249
|
+
x = this.elements[0] - C[0];
|
250
|
+
y = this.elements[1] - C[1];
|
251
|
+
z = this.elements[2] - C[2];
|
252
|
+
return Vector.create([
|
253
|
+
C[0] + R[0][0] * x + R[0][1] * y + R[0][2] * z,
|
254
|
+
C[1] + R[1][0] * x + R[1][1] * y + R[1][2] * z,
|
255
|
+
C[2] + R[2][0] * x + R[2][1] * y + R[2][2] * z
|
256
|
+
]);
|
257
|
+
break;
|
258
|
+
default:
|
259
|
+
return null;
|
260
|
+
}
|
261
|
+
},
|
262
|
+
|
263
|
+
// Returns the result of reflecting the point in the given point, line or plane
|
264
|
+
reflectionIn: function(obj) {
|
265
|
+
if (obj.anchor) {
|
266
|
+
// obj is a plane or line
|
267
|
+
var P = this.elements.slice();
|
268
|
+
var C = obj.pointClosestTo(P).elements;
|
269
|
+
return Vector.create([C[0] + (C[0] - P[0]), C[1] + (C[1] - P[1]), C[2] + (C[2] - (P[2] || 0))]);
|
270
|
+
} else {
|
271
|
+
// obj is a point
|
272
|
+
var Q = obj.elements || obj;
|
273
|
+
if (this.elements.length != Q.length) { return null; }
|
274
|
+
return this.map(function(x, i) { return Q[i-1] + (Q[i-1] - x); });
|
275
|
+
}
|
276
|
+
},
|
277
|
+
|
278
|
+
// Utility to make sure vectors are 3D. If they are 2D, a zero z-component is added
|
279
|
+
to3D: function() {
|
280
|
+
var V = this.dup();
|
281
|
+
switch (V.elements.length) {
|
282
|
+
case 3: break;
|
283
|
+
case 2: V.elements.push(0); break;
|
284
|
+
default: return null;
|
285
|
+
}
|
286
|
+
return V;
|
287
|
+
},
|
288
|
+
|
289
|
+
// Returns a string representation of the vector
|
290
|
+
inspect: function() {
|
291
|
+
return '[' + this.elements.join(', ') + ']';
|
292
|
+
},
|
293
|
+
|
294
|
+
// Set vector's elements from an array
|
295
|
+
setElements: function(els) {
|
296
|
+
this.elements = (els.elements || els).slice();
|
297
|
+
return this;
|
298
|
+
}
|
299
|
+
};
|
300
|
+
|
301
|
+
// Constructor function
|
302
|
+
Vector.create = function(elements) {
|
303
|
+
var V = new Vector();
|
304
|
+
return V.setElements(elements);
|
305
|
+
};
|
306
|
+
|
307
|
+
// i, j, k unit vectors
|
308
|
+
Vector.i = Vector.create([1,0,0]);
|
309
|
+
Vector.j = Vector.create([0,1,0]);
|
310
|
+
Vector.k = Vector.create([0,0,1]);
|
311
|
+
|
312
|
+
// Random vector of size n
|
313
|
+
Vector.Random = function(n) {
|
314
|
+
var elements = [];
|
315
|
+
do { elements.push(Math.random());
|
316
|
+
} while (--n);
|
317
|
+
return Vector.create(elements);
|
318
|
+
};
|
319
|
+
|
320
|
+
// Vector filled with zeros
|
321
|
+
Vector.Zero = function(n) {
|
322
|
+
var elements = [];
|
323
|
+
do { elements.push(0);
|
324
|
+
} while (--n);
|
325
|
+
return Vector.create(elements);
|
326
|
+
};
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
function Matrix() {}
|
331
|
+
Matrix.prototype = {
|
332
|
+
|
333
|
+
// Returns element (i,j) of the matrix
|
334
|
+
e: function(i,j) {
|
335
|
+
if (i < 1 || i > this.elements.length || j < 1 || j > this.elements[0].length) { return null; }
|
336
|
+
return this.elements[i-1][j-1];
|
337
|
+
},
|
338
|
+
|
339
|
+
// Returns row k of the matrix as a vector
|
340
|
+
row: function(i) {
|
341
|
+
if (i > this.elements.length) { return null; }
|
342
|
+
return Vector.create(this.elements[i-1]);
|
343
|
+
},
|
344
|
+
|
345
|
+
// Returns column k of the matrix as a vector
|
346
|
+
col: function(j) {
|
347
|
+
if (j > this.elements[0].length) { return null; }
|
348
|
+
var col = [], n = this.elements.length, k = n, i;
|
349
|
+
do { i = k - n;
|
350
|
+
col.push(this.elements[i][j-1]);
|
351
|
+
} while (--n);
|
352
|
+
return Vector.create(col);
|
353
|
+
},
|
354
|
+
|
355
|
+
// Returns the number of rows/columns the matrix has
|
356
|
+
dimensions: function() {
|
357
|
+
return {rows: this.elements.length, cols: this.elements[0].length};
|
358
|
+
},
|
359
|
+
|
360
|
+
// Returns the number of rows in the matrix
|
361
|
+
rows: function() {
|
362
|
+
return this.elements.length;
|
363
|
+
},
|
364
|
+
|
365
|
+
// Returns the number of columns in the matrix
|
366
|
+
cols: function() {
|
367
|
+
return this.elements[0].length;
|
368
|
+
},
|
369
|
+
|
370
|
+
// Returns true iff the matrix is equal to the argument. You can supply
|
371
|
+
// a vector as the argument, in which case the receiver must be a
|
372
|
+
// one-column matrix equal to the vector.
|
373
|
+
eql: function(matrix) {
|
374
|
+
var M = matrix.elements || matrix;
|
375
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
376
|
+
if (this.elements.length != M.length ||
|
377
|
+
this.elements[0].length != M[0].length) { return false; }
|
378
|
+
var ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
|
379
|
+
do { i = ki - ni;
|
380
|
+
nj = kj;
|
381
|
+
do { j = kj - nj;
|
382
|
+
if (Math.abs(this.elements[i][j] - M[i][j]) > Sylvester.precision) { return false; }
|
383
|
+
} while (--nj);
|
384
|
+
} while (--ni);
|
385
|
+
return true;
|
386
|
+
},
|
387
|
+
|
388
|
+
// Returns a copy of the matrix
|
389
|
+
dup: function() {
|
390
|
+
return Matrix.create(this.elements);
|
391
|
+
},
|
392
|
+
|
393
|
+
// Maps the matrix to another matrix (of the same dimensions) according to the given function
|
394
|
+
map: function(fn) {
|
395
|
+
var els = [], ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
|
396
|
+
do { i = ki - ni;
|
397
|
+
nj = kj;
|
398
|
+
els[i] = [];
|
399
|
+
do { j = kj - nj;
|
400
|
+
els[i][j] = fn(this.elements[i][j], i + 1, j + 1);
|
401
|
+
} while (--nj);
|
402
|
+
} while (--ni);
|
403
|
+
return Matrix.create(els);
|
404
|
+
},
|
405
|
+
|
406
|
+
// Returns true iff the argument has the same dimensions as the matrix
|
407
|
+
isSameSizeAs: function(matrix) {
|
408
|
+
var M = matrix.elements || matrix;
|
409
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
410
|
+
return (this.elements.length == M.length &&
|
411
|
+
this.elements[0].length == M[0].length);
|
412
|
+
},
|
413
|
+
|
414
|
+
// Returns the result of adding the argument to the matrix
|
415
|
+
add: function(matrix) {
|
416
|
+
var M = matrix.elements || matrix;
|
417
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
418
|
+
if (!this.isSameSizeAs(M)) { return null; }
|
419
|
+
return this.map(function(x, i, j) { return x + M[i-1][j-1]; });
|
420
|
+
},
|
421
|
+
|
422
|
+
// Returns the result of subtracting the argument from the matrix
|
423
|
+
subtract: function(matrix) {
|
424
|
+
var M = matrix.elements || matrix;
|
425
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
426
|
+
if (!this.isSameSizeAs(M)) { return null; }
|
427
|
+
return this.map(function(x, i, j) { return x - M[i-1][j-1]; });
|
428
|
+
},
|
429
|
+
|
430
|
+
// Returns true iff the matrix can multiply the argument from the left
|
431
|
+
canMultiplyFromLeft: function(matrix) {
|
432
|
+
var M = matrix.elements || matrix;
|
433
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
434
|
+
// this.columns should equal matrix.rows
|
435
|
+
return (this.elements[0].length == M.length);
|
436
|
+
},
|
437
|
+
|
438
|
+
// Returns the result of multiplying the matrix from the right by the argument.
|
439
|
+
// If the argument is a scalar then just multiply all the elements. If the argument is
|
440
|
+
// a vector, a vector is returned, which saves you having to remember calling
|
441
|
+
// col(1) on the result.
|
442
|
+
multiply: function(matrix) {
|
443
|
+
if (!matrix.elements) {
|
444
|
+
return this.map(function(x) { return x * matrix; });
|
445
|
+
}
|
446
|
+
var returnVector = matrix.modulus ? true : false;
|
447
|
+
var M = matrix.elements || matrix;
|
448
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
449
|
+
if (!this.canMultiplyFromLeft(M)) { return null; }
|
450
|
+
var ni = this.elements.length, ki = ni, i, nj, kj = M[0].length, j;
|
451
|
+
var cols = this.elements[0].length, elements = [], sum, nc, c;
|
452
|
+
do { i = ki - ni;
|
453
|
+
elements[i] = [];
|
454
|
+
nj = kj;
|
455
|
+
do { j = kj - nj;
|
456
|
+
sum = 0;
|
457
|
+
nc = cols;
|
458
|
+
do { c = cols - nc;
|
459
|
+
sum += this.elements[i][c] * M[c][j];
|
460
|
+
} while (--nc);
|
461
|
+
elements[i][j] = sum;
|
462
|
+
} while (--nj);
|
463
|
+
} while (--ni);
|
464
|
+
var M = Matrix.create(elements);
|
465
|
+
return returnVector ? M.col(1) : M;
|
466
|
+
},
|
467
|
+
|
468
|
+
x: function(matrix) { return this.multiply(matrix); },
|
469
|
+
|
470
|
+
// Returns a submatrix taken from the matrix
|
471
|
+
// Argument order is: start row, start col, nrows, ncols
|
472
|
+
// Element selection wraps if the required index is outside the matrix's bounds, so you could
|
473
|
+
// use this to perform row/column cycling or copy-augmenting.
|
474
|
+
minor: function(a, b, c, d) {
|
475
|
+
var elements = [], ni = c, i, nj, j;
|
476
|
+
var rows = this.elements.length, cols = this.elements[0].length;
|
477
|
+
do { i = c - ni;
|
478
|
+
elements[i] = [];
|
479
|
+
nj = d;
|
480
|
+
do { j = d - nj;
|
481
|
+
elements[i][j] = this.elements[(a+i-1)%rows][(b+j-1)%cols];
|
482
|
+
} while (--nj);
|
483
|
+
} while (--ni);
|
484
|
+
return Matrix.create(elements);
|
485
|
+
},
|
486
|
+
|
487
|
+
// Returns the transpose of the matrix
|
488
|
+
transpose: function() {
|
489
|
+
var rows = this.elements.length, cols = this.elements[0].length;
|
490
|
+
var elements = [], ni = cols, i, nj, j;
|
491
|
+
do { i = cols - ni;
|
492
|
+
elements[i] = [];
|
493
|
+
nj = rows;
|
494
|
+
do { j = rows - nj;
|
495
|
+
elements[i][j] = this.elements[j][i];
|
496
|
+
} while (--nj);
|
497
|
+
} while (--ni);
|
498
|
+
return Matrix.create(elements);
|
499
|
+
},
|
500
|
+
|
501
|
+
// Returns true iff the matrix is square
|
502
|
+
isSquare: function() {
|
503
|
+
return (this.elements.length == this.elements[0].length);
|
504
|
+
},
|
505
|
+
|
506
|
+
// Returns the (absolute) largest element of the matrix
|
507
|
+
max: function() {
|
508
|
+
var m = 0, ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
|
509
|
+
do { i = ki - ni;
|
510
|
+
nj = kj;
|
511
|
+
do { j = kj - nj;
|
512
|
+
if (Math.abs(this.elements[i][j]) > Math.abs(m)) { m = this.elements[i][j]; }
|
513
|
+
} while (--nj);
|
514
|
+
} while (--ni);
|
515
|
+
return m;
|
516
|
+
},
|
517
|
+
|
518
|
+
// Returns the indeces of the first match found by reading row-by-row from left to right
|
519
|
+
indexOf: function(x) {
|
520
|
+
var index = null, ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
|
521
|
+
do { i = ki - ni;
|
522
|
+
nj = kj;
|
523
|
+
do { j = kj - nj;
|
524
|
+
if (this.elements[i][j] == x) { return {i: i+1, j: j+1}; }
|
525
|
+
} while (--nj);
|
526
|
+
} while (--ni);
|
527
|
+
return null;
|
528
|
+
},
|
529
|
+
|
530
|
+
// If the matrix is square, returns the diagonal elements as a vector.
|
531
|
+
// Otherwise, returns null.
|
532
|
+
diagonal: function() {
|
533
|
+
if (!this.isSquare) { return null; }
|
534
|
+
var els = [], n = this.elements.length, k = n, i;
|
535
|
+
do { i = k - n;
|
536
|
+
els.push(this.elements[i][i]);
|
537
|
+
} while (--n);
|
538
|
+
return Vector.create(els);
|
539
|
+
},
|
540
|
+
|
541
|
+
// Make the matrix upper (right) triangular by Gaussian elimination.
|
542
|
+
// This method only adds multiples of rows to other rows. No rows are
|
543
|
+
// scaled up or switched, and the determinant is preserved.
|
544
|
+
toRightTriangular: function() {
|
545
|
+
var M = this.dup(), els;
|
546
|
+
var n = this.elements.length, k = n, i, np, kp = this.elements[0].length, p;
|
547
|
+
do { i = k - n;
|
548
|
+
if (M.elements[i][i] == 0) {
|
549
|
+
for (j = i + 1; j < k; j++) {
|
550
|
+
if (M.elements[j][i] != 0) {
|
551
|
+
els = []; np = kp;
|
552
|
+
do { p = kp - np;
|
553
|
+
els.push(M.elements[i][p] + M.elements[j][p]);
|
554
|
+
} while (--np);
|
555
|
+
M.elements[i] = els;
|
556
|
+
break;
|
557
|
+
}
|
558
|
+
}
|
559
|
+
}
|
560
|
+
if (M.elements[i][i] != 0) {
|
561
|
+
for (j = i + 1; j < k; j++) {
|
562
|
+
var multiplier = M.elements[j][i] / M.elements[i][i];
|
563
|
+
els = []; np = kp;
|
564
|
+
do { p = kp - np;
|
565
|
+
// Elements with column numbers up to an including the number
|
566
|
+
// of the row that we're subtracting can safely be set straight to
|
567
|
+
// zero, since that's the point of this routine and it avoids having
|
568
|
+
// to loop over and correct rounding errors later
|
569
|
+
els.push(p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier);
|
570
|
+
} while (--np);
|
571
|
+
M.elements[j] = els;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
} while (--n);
|
575
|
+
return M;
|
576
|
+
},
|
577
|
+
|
578
|
+
toUpperTriangular: function() { return this.toRightTriangular(); },
|
579
|
+
|
580
|
+
// Returns the determinant for square matrices
|
581
|
+
determinant: function() {
|
582
|
+
if (!this.isSquare()) { return null; }
|
583
|
+
var M = this.toRightTriangular();
|
584
|
+
var det = M.elements[0][0], n = M.elements.length - 1, k = n, i;
|
585
|
+
do { i = k - n + 1;
|
586
|
+
det = det * M.elements[i][i];
|
587
|
+
} while (--n);
|
588
|
+
return det;
|
589
|
+
},
|
590
|
+
|
591
|
+
det: function() { return this.determinant(); },
|
592
|
+
|
593
|
+
// Returns true iff the matrix is singular
|
594
|
+
isSingular: function() {
|
595
|
+
return (this.isSquare() && this.determinant() === 0);
|
596
|
+
},
|
597
|
+
|
598
|
+
// Returns the trace for square matrices
|
599
|
+
trace: function() {
|
600
|
+
if (!this.isSquare()) { return null; }
|
601
|
+
var tr = this.elements[0][0], n = this.elements.length - 1, k = n, i;
|
602
|
+
do { i = k - n + 1;
|
603
|
+
tr += this.elements[i][i];
|
604
|
+
} while (--n);
|
605
|
+
return tr;
|
606
|
+
},
|
607
|
+
|
608
|
+
tr: function() { return this.trace(); },
|
609
|
+
|
610
|
+
// Returns the rank of the matrix
|
611
|
+
rank: function() {
|
612
|
+
var M = this.toRightTriangular(), rank = 0;
|
613
|
+
var ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
|
614
|
+
do { i = ki - ni;
|
615
|
+
nj = kj;
|
616
|
+
do { j = kj - nj;
|
617
|
+
if (Math.abs(M.elements[i][j]) > Sylvester.precision) { rank++; break; }
|
618
|
+
} while (--nj);
|
619
|
+
} while (--ni);
|
620
|
+
return rank;
|
621
|
+
},
|
622
|
+
|
623
|
+
rk: function() { return this.rank(); },
|
624
|
+
|
625
|
+
// Returns the result of attaching the given argument to the right-hand side of the matrix
|
626
|
+
augment: function(matrix) {
|
627
|
+
var M = matrix.elements || matrix;
|
628
|
+
if (typeof(M[0][0]) == 'undefined') { M = Matrix.create(M).elements; }
|
629
|
+
var T = this.dup(), cols = T.elements[0].length;
|
630
|
+
var ni = T.elements.length, ki = ni, i, nj, kj = M[0].length, j;
|
631
|
+
if (ni != M.length) { return null; }
|
632
|
+
do { i = ki - ni;
|
633
|
+
nj = kj;
|
634
|
+
do { j = kj - nj;
|
635
|
+
T.elements[i][cols + j] = M[i][j];
|
636
|
+
} while (--nj);
|
637
|
+
} while (--ni);
|
638
|
+
return T;
|
639
|
+
},
|
640
|
+
|
641
|
+
// Returns the inverse (if one exists) using Gauss-Jordan
|
642
|
+
inverse: function() {
|
643
|
+
if (!this.isSquare() || this.isSingular()) { return null; }
|
644
|
+
var ni = this.elements.length, ki = ni, i, j;
|
645
|
+
var M = this.augment(Matrix.I(ni)).toRightTriangular();
|
646
|
+
var np, kp = M.elements[0].length, p, els, divisor;
|
647
|
+
var inverse_elements = [], new_element;
|
648
|
+
// Matrix is non-singular so there will be no zeros on the diagonal
|
649
|
+
// Cycle through rows from last to first
|
650
|
+
do { i = ni - 1;
|
651
|
+
// First, normalise diagonal elements to 1
|
652
|
+
els = []; np = kp;
|
653
|
+
inverse_elements[i] = [];
|
654
|
+
divisor = M.elements[i][i];
|
655
|
+
do { p = kp - np;
|
656
|
+
new_element = M.elements[i][p] / divisor;
|
657
|
+
els.push(new_element);
|
658
|
+
// Shuffle of the current row of the right hand side into the results
|
659
|
+
// array as it will not be modified by later runs through this loop
|
660
|
+
if (p >= ki) { inverse_elements[i].push(new_element); }
|
661
|
+
} while (--np);
|
662
|
+
M.elements[i] = els;
|
663
|
+
// Then, subtract this row from those above it to
|
664
|
+
// give the identity matrix on the left hand side
|
665
|
+
for (j = 0; j < i; j++) {
|
666
|
+
els = []; np = kp;
|
667
|
+
do { p = kp - np;
|
668
|
+
els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]);
|
669
|
+
} while (--np);
|
670
|
+
M.elements[j] = els;
|
671
|
+
}
|
672
|
+
} while (--ni);
|
673
|
+
return Matrix.create(inverse_elements);
|
674
|
+
},
|
675
|
+
|
676
|
+
inv: function() { return this.inverse(); },
|
677
|
+
|
678
|
+
// Returns the result of rounding all the elements
|
679
|
+
round: function() {
|
680
|
+
return this.map(function(x) { return Math.round(x); });
|
681
|
+
},
|
682
|
+
|
683
|
+
// Returns a copy of the matrix with elements set to the given value if they
|
684
|
+
// differ from it by less than Sylvester.precision
|
685
|
+
snapTo: function(x) {
|
686
|
+
return this.map(function(p) {
|
687
|
+
return (Math.abs(p - x) <= Sylvester.precision) ? x : p;
|
688
|
+
});
|
689
|
+
},
|
690
|
+
|
691
|
+
// Returns a string representation of the matrix
|
692
|
+
inspect: function() {
|
693
|
+
var matrix_rows = [];
|
694
|
+
var n = this.elements.length, k = n, i;
|
695
|
+
do { i = k - n;
|
696
|
+
matrix_rows.push(Vector.create(this.elements[i]).inspect());
|
697
|
+
} while (--n);
|
698
|
+
return matrix_rows.join('\n');
|
699
|
+
},
|
700
|
+
|
701
|
+
// Set the matrix's elements from an array. If the argument passed
|
702
|
+
// is a vector, the resulting matrix will be a single column.
|
703
|
+
setElements: function(els) {
|
704
|
+
var i, elements = els.elements || els;
|
705
|
+
if (typeof(elements[0][0]) != 'undefined') {
|
706
|
+
var ni = elements.length, ki = ni, nj, kj, j;
|
707
|
+
this.elements = [];
|
708
|
+
do { i = ki - ni;
|
709
|
+
nj = elements[i].length; kj = nj;
|
710
|
+
this.elements[i] = [];
|
711
|
+
do { j = kj - nj;
|
712
|
+
this.elements[i][j] = elements[i][j];
|
713
|
+
} while (--nj);
|
714
|
+
} while(--ni);
|
715
|
+
return this;
|
716
|
+
}
|
717
|
+
var n = elements.length, k = n;
|
718
|
+
this.elements = [];
|
719
|
+
do { i = k - n;
|
720
|
+
this.elements.push([elements[i]]);
|
721
|
+
} while (--n);
|
722
|
+
return this;
|
723
|
+
}
|
724
|
+
};
|
725
|
+
|
726
|
+
// Constructor function
|
727
|
+
Matrix.create = function(elements) {
|
728
|
+
var M = new Matrix();
|
729
|
+
return M.setElements(elements);
|
730
|
+
};
|
731
|
+
|
732
|
+
// Identity matrix of size n
|
733
|
+
Matrix.I = function(n) {
|
734
|
+
var els = [], k = n, i, nj, j;
|
735
|
+
do { i = k - n;
|
736
|
+
els[i] = []; nj = k;
|
737
|
+
do { j = k - nj;
|
738
|
+
els[i][j] = (i == j) ? 1 : 0;
|
739
|
+
} while (--nj);
|
740
|
+
} while (--n);
|
741
|
+
return Matrix.create(els);
|
742
|
+
};
|
743
|
+
|
744
|
+
// Diagonal matrix - all off-diagonal elements are zero
|
745
|
+
Matrix.Diagonal = function(elements) {
|
746
|
+
var n = elements.length, k = n, i;
|
747
|
+
var M = Matrix.I(n);
|
748
|
+
do { i = k - n;
|
749
|
+
M.elements[i][i] = elements[i];
|
750
|
+
} while (--n);
|
751
|
+
return M;
|
752
|
+
};
|
753
|
+
|
754
|
+
// Rotation matrix about some axis. If no axis is
|
755
|
+
// supplied, assume we're after a 2D transform
|
756
|
+
Matrix.Rotation = function(theta, a) {
|
757
|
+
if (!a) {
|
758
|
+
return Matrix.create([
|
759
|
+
[Math.cos(theta), -Math.sin(theta)],
|
760
|
+
[Math.sin(theta), Math.cos(theta)]
|
761
|
+
]);
|
762
|
+
}
|
763
|
+
var axis = a.dup();
|
764
|
+
if (axis.elements.length != 3) { return null; }
|
765
|
+
var mod = axis.modulus();
|
766
|
+
var x = axis.elements[0]/mod, y = axis.elements[1]/mod, z = axis.elements[2]/mod;
|
767
|
+
var s = Math.sin(theta), c = Math.cos(theta), t = 1 - c;
|
768
|
+
// Formula derived here: http://www.gamedev.net/reference/articles/article1199.asp
|
769
|
+
// That proof rotates the co-ordinate system so theta
|
770
|
+
// becomes -theta and sin becomes -sin here.
|
771
|
+
return Matrix.create([
|
772
|
+
[ t*x*x + c, t*x*y - s*z, t*x*z + s*y ],
|
773
|
+
[ t*x*y + s*z, t*y*y + c, t*y*z - s*x ],
|
774
|
+
[ t*x*z - s*y, t*y*z + s*x, t*z*z + c ]
|
775
|
+
]);
|
776
|
+
};
|
777
|
+
|
778
|
+
// Special case rotations
|
779
|
+
Matrix.RotationX = function(t) {
|
780
|
+
var c = Math.cos(t), s = Math.sin(t);
|
781
|
+
return Matrix.create([
|
782
|
+
[ 1, 0, 0 ],
|
783
|
+
[ 0, c, -s ],
|
784
|
+
[ 0, s, c ]
|
785
|
+
]);
|
786
|
+
};
|
787
|
+
Matrix.RotationY = function(t) {
|
788
|
+
var c = Math.cos(t), s = Math.sin(t);
|
789
|
+
return Matrix.create([
|
790
|
+
[ c, 0, s ],
|
791
|
+
[ 0, 1, 0 ],
|
792
|
+
[ -s, 0, c ]
|
793
|
+
]);
|
794
|
+
};
|
795
|
+
Matrix.RotationZ = function(t) {
|
796
|
+
var c = Math.cos(t), s = Math.sin(t);
|
797
|
+
return Matrix.create([
|
798
|
+
[ c, -s, 0 ],
|
799
|
+
[ s, c, 0 ],
|
800
|
+
[ 0, 0, 1 ]
|
801
|
+
]);
|
802
|
+
};
|
803
|
+
|
804
|
+
// Random matrix of n rows, m columns
|
805
|
+
Matrix.Random = function(n, m) {
|
806
|
+
return Matrix.Zero(n, m).map(
|
807
|
+
function() { return Math.random(); }
|
808
|
+
);
|
809
|
+
};
|
810
|
+
|
811
|
+
// Matrix filled with zeros
|
812
|
+
Matrix.Zero = function(n, m) {
|
813
|
+
var els = [], ni = n, i, nj, j;
|
814
|
+
do { i = n - ni;
|
815
|
+
els[i] = [];
|
816
|
+
nj = m;
|
817
|
+
do { j = m - nj;
|
818
|
+
els[i][j] = 0;
|
819
|
+
} while (--nj);
|
820
|
+
} while (--ni);
|
821
|
+
return Matrix.create(els);
|
822
|
+
};
|
823
|
+
|
824
|
+
|
825
|
+
|
826
|
+
function Line() {}
|
827
|
+
Line.prototype = {
|
828
|
+
|
829
|
+
// Returns true if the argument occupies the same space as the line
|
830
|
+
eql: function(line) {
|
831
|
+
return (this.isParallelTo(line) && this.contains(line.anchor));
|
832
|
+
},
|
833
|
+
|
834
|
+
// Returns a copy of the line
|
835
|
+
dup: function() {
|
836
|
+
return Line.create(this.anchor, this.direction);
|
837
|
+
},
|
838
|
+
|
839
|
+
// Returns the result of translating the line by the given vector/array
|
840
|
+
translate: function(vector) {
|
841
|
+
var V = vector.elements || vector;
|
842
|
+
return Line.create([
|
843
|
+
this.anchor.elements[0] + V[0],
|
844
|
+
this.anchor.elements[1] + V[1],
|
845
|
+
this.anchor.elements[2] + (V[2] || 0)
|
846
|
+
], this.direction);
|
847
|
+
},
|
848
|
+
|
849
|
+
// Returns true if the line is parallel to the argument. Here, 'parallel to'
|
850
|
+
// means that the argument's direction is either parallel or antiparallel to
|
851
|
+
// the line's own direction. A line is parallel to a plane if the two do not
|
852
|
+
// have a unique intersection.
|
853
|
+
isParallelTo: function(obj) {
|
854
|
+
if (obj.normal) { return obj.isParallelTo(this); }
|
855
|
+
var theta = this.direction.angleFrom(obj.direction);
|
856
|
+
return (Math.abs(theta) <= Sylvester.precision || Math.abs(theta - Math.PI) <= Sylvester.precision);
|
857
|
+
},
|
858
|
+
|
859
|
+
// Returns the line's perpendicular distance from the argument,
|
860
|
+
// which can be a point, a line or a plane
|
861
|
+
distanceFrom: function(obj) {
|
862
|
+
if (obj.normal) { return obj.distanceFrom(this); }
|
863
|
+
if (obj.direction) {
|
864
|
+
// obj is a line
|
865
|
+
if (this.isParallelTo(obj)) { return this.distanceFrom(obj.anchor); }
|
866
|
+
var N = this.direction.cross(obj.direction).toUnitVector().elements;
|
867
|
+
var A = this.anchor.elements, B = obj.anchor.elements;
|
868
|
+
return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
|
869
|
+
} else {
|
870
|
+
// obj is a point
|
871
|
+
var P = obj.elements || obj;
|
872
|
+
var A = this.anchor.elements, D = this.direction.elements;
|
873
|
+
var PA1 = P[0] - A[0], PA2 = P[1] - A[1], PA3 = (P[2] || 0) - A[2];
|
874
|
+
var modPA = Math.sqrt(PA1*PA1 + PA2*PA2 + PA3*PA3);
|
875
|
+
if (modPA === 0) return 0;
|
876
|
+
// Assumes direction vector is normalized
|
877
|
+
var cosTheta = (PA1 * D[0] + PA2 * D[1] + PA3 * D[2]) / modPA;
|
878
|
+
var sin2 = 1 - cosTheta*cosTheta;
|
879
|
+
return Math.abs(modPA * Math.sqrt(sin2 < 0 ? 0 : sin2));
|
880
|
+
}
|
881
|
+
},
|
882
|
+
|
883
|
+
// Returns true iff the argument is a point on the line
|
884
|
+
contains: function(point) {
|
885
|
+
var dist = this.distanceFrom(point);
|
886
|
+
return (dist !== null && dist <= Sylvester.precision);
|
887
|
+
},
|
888
|
+
|
889
|
+
// Returns true iff the line lies in the given plane
|
890
|
+
liesIn: function(plane) {
|
891
|
+
return plane.contains(this);
|
892
|
+
},
|
893
|
+
|
894
|
+
// Returns true iff the line has a unique point of intersection with the argument
|
895
|
+
intersects: function(obj) {
|
896
|
+
if (obj.normal) { return obj.intersects(this); }
|
897
|
+
return (!this.isParallelTo(obj) && this.distanceFrom(obj) <= Sylvester.precision);
|
898
|
+
},
|
899
|
+
|
900
|
+
// Returns the unique intersection point with the argument, if one exists
|
901
|
+
intersectionWith: function(obj) {
|
902
|
+
if (obj.normal) { return obj.intersectionWith(this); }
|
903
|
+
if (!this.intersects(obj)) { return null; }
|
904
|
+
var P = this.anchor.elements, X = this.direction.elements,
|
905
|
+
Q = obj.anchor.elements, Y = obj.direction.elements;
|
906
|
+
var X1 = X[0], X2 = X[1], X3 = X[2], Y1 = Y[0], Y2 = Y[1], Y3 = Y[2];
|
907
|
+
var PsubQ1 = P[0] - Q[0], PsubQ2 = P[1] - Q[1], PsubQ3 = P[2] - Q[2];
|
908
|
+
var XdotQsubP = - X1*PsubQ1 - X2*PsubQ2 - X3*PsubQ3;
|
909
|
+
var YdotPsubQ = Y1*PsubQ1 + Y2*PsubQ2 + Y3*PsubQ3;
|
910
|
+
var XdotX = X1*X1 + X2*X2 + X3*X3;
|
911
|
+
var YdotY = Y1*Y1 + Y2*Y2 + Y3*Y3;
|
912
|
+
var XdotY = X1*Y1 + X2*Y2 + X3*Y3;
|
913
|
+
var k = (XdotQsubP * YdotY / XdotX + XdotY * YdotPsubQ) / (YdotY - XdotY * XdotY);
|
914
|
+
return Vector.create([P[0] + k*X1, P[1] + k*X2, P[2] + k*X3]);
|
915
|
+
},
|
916
|
+
|
917
|
+
// Returns the point on the line that is closest to the given point or line
|
918
|
+
pointClosestTo: function(obj) {
|
919
|
+
if (obj.direction) {
|
920
|
+
// obj is a line
|
921
|
+
if (this.intersects(obj)) { return this.intersectionWith(obj); }
|
922
|
+
if (this.isParallelTo(obj)) { return null; }
|
923
|
+
var D = this.direction.elements, E = obj.direction.elements;
|
924
|
+
var D1 = D[0], D2 = D[1], D3 = D[2], E1 = E[0], E2 = E[1], E3 = E[2];
|
925
|
+
// Create plane containing obj and the shared normal and intersect this with it
|
926
|
+
// Thank you: http://www.cgafaq.info/wiki/Line-line_distance
|
927
|
+
var x = (D3 * E1 - D1 * E3), y = (D1 * E2 - D2 * E1), z = (D2 * E3 - D3 * E2);
|
928
|
+
var N = Vector.create([x * E3 - y * E2, y * E1 - z * E3, z * E2 - x * E1]);
|
929
|
+
var P = Plane.create(obj.anchor, N);
|
930
|
+
return P.intersectionWith(this);
|
931
|
+
} else {
|
932
|
+
// obj is a point
|
933
|
+
var P = obj.elements || obj;
|
934
|
+
if (this.contains(P)) { return Vector.create(P); }
|
935
|
+
var A = this.anchor.elements, D = this.direction.elements;
|
936
|
+
var D1 = D[0], D2 = D[1], D3 = D[2], A1 = A[0], A2 = A[1], A3 = A[2];
|
937
|
+
var x = D1 * (P[1]-A2) - D2 * (P[0]-A1), y = D2 * ((P[2] || 0) - A3) - D3 * (P[1]-A2),
|
938
|
+
z = D3 * (P[0]-A1) - D1 * ((P[2] || 0) - A3);
|
939
|
+
var V = Vector.create([D2 * x - D3 * z, D3 * y - D1 * x, D1 * z - D2 * y]);
|
940
|
+
var k = this.distanceFrom(P) / V.modulus();
|
941
|
+
return Vector.create([
|
942
|
+
P[0] + V.elements[0] * k,
|
943
|
+
P[1] + V.elements[1] * k,
|
944
|
+
(P[2] || 0) + V.elements[2] * k
|
945
|
+
]);
|
946
|
+
}
|
947
|
+
},
|
948
|
+
|
949
|
+
// Returns a copy of the line rotated by t radians about the given line. Works by
|
950
|
+
// finding the argument's closest point to this line's anchor point (call this C) and
|
951
|
+
// rotating the anchor about C. Also rotates the line's direction about the argument's.
|
952
|
+
// Be careful with this - the rotation axis' direction affects the outcome!
|
953
|
+
rotate: function(t, line) {
|
954
|
+
// If we're working in 2D
|
955
|
+
if (typeof(line.direction) == 'undefined') { line = Line.create(line.to3D(), Vector.k); }
|
956
|
+
var R = Matrix.Rotation(t, line.direction).elements;
|
957
|
+
var C = line.pointClosestTo(this.anchor).elements;
|
958
|
+
var A = this.anchor.elements, D = this.direction.elements;
|
959
|
+
var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2];
|
960
|
+
var x = A1 - C1, y = A2 - C2, z = A3 - C3;
|
961
|
+
return Line.create([
|
962
|
+
C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z,
|
963
|
+
C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z,
|
964
|
+
C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z
|
965
|
+
], [
|
966
|
+
R[0][0] * D[0] + R[0][1] * D[1] + R[0][2] * D[2],
|
967
|
+
R[1][0] * D[0] + R[1][1] * D[1] + R[1][2] * D[2],
|
968
|
+
R[2][0] * D[0] + R[2][1] * D[1] + R[2][2] * D[2]
|
969
|
+
]);
|
970
|
+
},
|
971
|
+
|
972
|
+
// Returns the line's reflection in the given point or line
|
973
|
+
reflectionIn: function(obj) {
|
974
|
+
if (obj.normal) {
|
975
|
+
// obj is a plane
|
976
|
+
var A = this.anchor.elements, D = this.direction.elements;
|
977
|
+
var A1 = A[0], A2 = A[1], A3 = A[2], D1 = D[0], D2 = D[1], D3 = D[2];
|
978
|
+
var newA = this.anchor.reflectionIn(obj).elements;
|
979
|
+
// Add the line's direction vector to its anchor, then mirror that in the plane
|
980
|
+
var AD1 = A1 + D1, AD2 = A2 + D2, AD3 = A3 + D3;
|
981
|
+
var Q = obj.pointClosestTo([AD1, AD2, AD3]).elements;
|
982
|
+
var newD = [Q[0] + (Q[0] - AD1) - newA[0], Q[1] + (Q[1] - AD2) - newA[1], Q[2] + (Q[2] - AD3) - newA[2]];
|
983
|
+
return Line.create(newA, newD);
|
984
|
+
} else if (obj.direction) {
|
985
|
+
// obj is a line - reflection obtained by rotating PI radians about obj
|
986
|
+
return this.rotate(Math.PI, obj);
|
987
|
+
} else {
|
988
|
+
// obj is a point - just reflect the line's anchor in it
|
989
|
+
var P = obj.elements || obj;
|
990
|
+
return Line.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.direction);
|
991
|
+
}
|
992
|
+
},
|
993
|
+
|
994
|
+
// Set the line's anchor point and direction.
|
995
|
+
setVectors: function(anchor, direction) {
|
996
|
+
// Need to do this so that line's properties are not
|
997
|
+
// references to the arguments passed in
|
998
|
+
anchor = Vector.create(anchor);
|
999
|
+
direction = Vector.create(direction);
|
1000
|
+
if (anchor.elements.length == 2) {anchor.elements.push(0); }
|
1001
|
+
if (direction.elements.length == 2) { direction.elements.push(0); }
|
1002
|
+
if (anchor.elements.length > 3 || direction.elements.length > 3) { return null; }
|
1003
|
+
var mod = direction.modulus();
|
1004
|
+
if (mod === 0) { return null; }
|
1005
|
+
this.anchor = anchor;
|
1006
|
+
this.direction = Vector.create([
|
1007
|
+
direction.elements[0] / mod,
|
1008
|
+
direction.elements[1] / mod,
|
1009
|
+
direction.elements[2] / mod
|
1010
|
+
]);
|
1011
|
+
return this;
|
1012
|
+
}
|
1013
|
+
};
|
1014
|
+
|
1015
|
+
|
1016
|
+
// Constructor function
|
1017
|
+
Line.create = function(anchor, direction) {
|
1018
|
+
var L = new Line();
|
1019
|
+
return L.setVectors(anchor, direction);
|
1020
|
+
};
|
1021
|
+
|
1022
|
+
// Axes
|
1023
|
+
Line.X = Line.create(Vector.Zero(3), Vector.i);
|
1024
|
+
Line.Y = Line.create(Vector.Zero(3), Vector.j);
|
1025
|
+
Line.Z = Line.create(Vector.Zero(3), Vector.k);
|
1026
|
+
|
1027
|
+
|
1028
|
+
|
1029
|
+
function Plane() {}
|
1030
|
+
Plane.prototype = {
|
1031
|
+
|
1032
|
+
// Returns true iff the plane occupies the same space as the argument
|
1033
|
+
eql: function(plane) {
|
1034
|
+
return (this.contains(plane.anchor) && this.isParallelTo(plane));
|
1035
|
+
},
|
1036
|
+
|
1037
|
+
// Returns a copy of the plane
|
1038
|
+
dup: function() {
|
1039
|
+
return Plane.create(this.anchor, this.normal);
|
1040
|
+
},
|
1041
|
+
|
1042
|
+
// Returns the result of translating the plane by the given vector
|
1043
|
+
translate: function(vector) {
|
1044
|
+
var V = vector.elements || vector;
|
1045
|
+
return Plane.create([
|
1046
|
+
this.anchor.elements[0] + V[0],
|
1047
|
+
this.anchor.elements[1] + V[1],
|
1048
|
+
this.anchor.elements[2] + (V[2] || 0)
|
1049
|
+
], this.normal);
|
1050
|
+
},
|
1051
|
+
|
1052
|
+
// Returns true iff the plane is parallel to the argument. Will return true
|
1053
|
+
// if the planes are equal, or if you give a line and it lies in the plane.
|
1054
|
+
isParallelTo: function(obj) {
|
1055
|
+
var theta;
|
1056
|
+
if (obj.normal) {
|
1057
|
+
// obj is a plane
|
1058
|
+
theta = this.normal.angleFrom(obj.normal);
|
1059
|
+
return (Math.abs(theta) <= Sylvester.precision || Math.abs(Math.PI - theta) <= Sylvester.precision);
|
1060
|
+
} else if (obj.direction) {
|
1061
|
+
// obj is a line
|
1062
|
+
return this.normal.isPerpendicularTo(obj.direction);
|
1063
|
+
}
|
1064
|
+
return null;
|
1065
|
+
},
|
1066
|
+
|
1067
|
+
// Returns true iff the receiver is perpendicular to the argument
|
1068
|
+
isPerpendicularTo: function(plane) {
|
1069
|
+
var theta = this.normal.angleFrom(plane.normal);
|
1070
|
+
return (Math.abs(Math.PI/2 - theta) <= Sylvester.precision);
|
1071
|
+
},
|
1072
|
+
|
1073
|
+
// Returns the plane's distance from the given object (point, line or plane)
|
1074
|
+
distanceFrom: function(obj) {
|
1075
|
+
if (this.intersects(obj) || this.contains(obj)) { return 0; }
|
1076
|
+
if (obj.anchor) {
|
1077
|
+
// obj is a plane or line
|
1078
|
+
var A = this.anchor.elements, B = obj.anchor.elements, N = this.normal.elements;
|
1079
|
+
return Math.abs((A[0] - B[0]) * N[0] + (A[1] - B[1]) * N[1] + (A[2] - B[2]) * N[2]);
|
1080
|
+
} else {
|
1081
|
+
// obj is a point
|
1082
|
+
var P = obj.elements || obj;
|
1083
|
+
var A = this.anchor.elements, N = this.normal.elements;
|
1084
|
+
return Math.abs((A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2]);
|
1085
|
+
}
|
1086
|
+
},
|
1087
|
+
|
1088
|
+
// Returns true iff the plane contains the given point or line
|
1089
|
+
contains: function(obj) {
|
1090
|
+
if (obj.normal) { return null; }
|
1091
|
+
if (obj.direction) {
|
1092
|
+
return (this.contains(obj.anchor) && this.contains(obj.anchor.add(obj.direction)));
|
1093
|
+
} else {
|
1094
|
+
var P = obj.elements || obj;
|
1095
|
+
var A = this.anchor.elements, N = this.normal.elements;
|
1096
|
+
var diff = Math.abs(N[0]*(A[0] - P[0]) + N[1]*(A[1] - P[1]) + N[2]*(A[2] - (P[2] || 0)));
|
1097
|
+
return (diff <= Sylvester.precision);
|
1098
|
+
}
|
1099
|
+
},
|
1100
|
+
|
1101
|
+
// Returns true iff the plane has a unique point/line of intersection with the argument
|
1102
|
+
intersects: function(obj) {
|
1103
|
+
if (typeof(obj.direction) == 'undefined' && typeof(obj.normal) == 'undefined') { return null; }
|
1104
|
+
return !this.isParallelTo(obj);
|
1105
|
+
},
|
1106
|
+
|
1107
|
+
// Returns the unique intersection with the argument, if one exists. The result
|
1108
|
+
// will be a vector if a line is supplied, and a line if a plane is supplied.
|
1109
|
+
intersectionWith: function(obj) {
|
1110
|
+
if (!this.intersects(obj)) { return null; }
|
1111
|
+
if (obj.direction) {
|
1112
|
+
// obj is a line
|
1113
|
+
var A = obj.anchor.elements, D = obj.direction.elements,
|
1114
|
+
P = this.anchor.elements, N = this.normal.elements;
|
1115
|
+
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]);
|
1116
|
+
return Vector.create([A[0] + D[0]*multiplier, A[1] + D[1]*multiplier, A[2] + D[2]*multiplier]);
|
1117
|
+
} else if (obj.normal) {
|
1118
|
+
// obj is a plane
|
1119
|
+
var direction = this.normal.cross(obj.normal).toUnitVector();
|
1120
|
+
// To find an anchor point, we find one co-ordinate that has a value
|
1121
|
+
// of zero somewhere on the intersection, and remember which one we picked
|
1122
|
+
var N = this.normal.elements, A = this.anchor.elements,
|
1123
|
+
O = obj.normal.elements, B = obj.anchor.elements;
|
1124
|
+
var solver = Matrix.Zero(2,2), i = 0;
|
1125
|
+
while (solver.isSingular()) {
|
1126
|
+
i++;
|
1127
|
+
solver = Matrix.create([
|
1128
|
+
[ N[i%3], N[(i+1)%3] ],
|
1129
|
+
[ O[i%3], O[(i+1)%3] ]
|
1130
|
+
]);
|
1131
|
+
}
|
1132
|
+
// Then we solve the simultaneous equations in the remaining dimensions
|
1133
|
+
var inverse = solver.inverse().elements;
|
1134
|
+
var x = N[0]*A[0] + N[1]*A[1] + N[2]*A[2];
|
1135
|
+
var y = O[0]*B[0] + O[1]*B[1] + O[2]*B[2];
|
1136
|
+
var intersection = [
|
1137
|
+
inverse[0][0] * x + inverse[0][1] * y,
|
1138
|
+
inverse[1][0] * x + inverse[1][1] * y
|
1139
|
+
];
|
1140
|
+
var anchor = [];
|
1141
|
+
for (var j = 1; j <= 3; j++) {
|
1142
|
+
// This formula picks the right element from intersection by
|
1143
|
+
// cycling depending on which element we set to zero above
|
1144
|
+
anchor.push((i == j) ? 0 : intersection[(j + (5 - i)%3)%3]);
|
1145
|
+
}
|
1146
|
+
return Line.create(anchor, direction);
|
1147
|
+
}
|
1148
|
+
},
|
1149
|
+
|
1150
|
+
// Returns the point in the plane closest to the given point
|
1151
|
+
pointClosestTo: function(point) {
|
1152
|
+
var P = point.elements || point;
|
1153
|
+
var A = this.anchor.elements, N = this.normal.elements;
|
1154
|
+
var dot = (A[0] - P[0]) * N[0] + (A[1] - P[1]) * N[1] + (A[2] - (P[2] || 0)) * N[2];
|
1155
|
+
return Vector.create([P[0] + N[0] * dot, P[1] + N[1] * dot, (P[2] || 0) + N[2] * dot]);
|
1156
|
+
},
|
1157
|
+
|
1158
|
+
// Returns a copy of the plane, rotated by t radians about the given line
|
1159
|
+
// See notes on Line#rotate.
|
1160
|
+
rotate: function(t, line) {
|
1161
|
+
var R = Matrix.Rotation(t, line.direction).elements;
|
1162
|
+
var C = line.pointClosestTo(this.anchor).elements;
|
1163
|
+
var A = this.anchor.elements, N = this.normal.elements;
|
1164
|
+
var C1 = C[0], C2 = C[1], C3 = C[2], A1 = A[0], A2 = A[1], A3 = A[2];
|
1165
|
+
var x = A1 - C1, y = A2 - C2, z = A3 - C3;
|
1166
|
+
return Plane.create([
|
1167
|
+
C1 + R[0][0] * x + R[0][1] * y + R[0][2] * z,
|
1168
|
+
C2 + R[1][0] * x + R[1][1] * y + R[1][2] * z,
|
1169
|
+
C3 + R[2][0] * x + R[2][1] * y + R[2][2] * z
|
1170
|
+
], [
|
1171
|
+
R[0][0] * N[0] + R[0][1] * N[1] + R[0][2] * N[2],
|
1172
|
+
R[1][0] * N[0] + R[1][1] * N[1] + R[1][2] * N[2],
|
1173
|
+
R[2][0] * N[0] + R[2][1] * N[1] + R[2][2] * N[2]
|
1174
|
+
]);
|
1175
|
+
},
|
1176
|
+
|
1177
|
+
// Returns the reflection of the plane in the given point, line or plane.
|
1178
|
+
reflectionIn: function(obj) {
|
1179
|
+
if (obj.normal) {
|
1180
|
+
// obj is a plane
|
1181
|
+
var A = this.anchor.elements, N = this.normal.elements;
|
1182
|
+
var A1 = A[0], A2 = A[1], A3 = A[2], N1 = N[0], N2 = N[1], N3 = N[2];
|
1183
|
+
var newA = this.anchor.reflectionIn(obj).elements;
|
1184
|
+
// Add the plane's normal to its anchor, then mirror that in the other plane
|
1185
|
+
var AN1 = A1 + N1, AN2 = A2 + N2, AN3 = A3 + N3;
|
1186
|
+
var Q = obj.pointClosestTo([AN1, AN2, AN3]).elements;
|
1187
|
+
var newN = [Q[0] + (Q[0] - AN1) - newA[0], Q[1] + (Q[1] - AN2) - newA[1], Q[2] + (Q[2] - AN3) - newA[2]];
|
1188
|
+
return Plane.create(newA, newN);
|
1189
|
+
} else if (obj.direction) {
|
1190
|
+
// obj is a line
|
1191
|
+
return this.rotate(Math.PI, obj);
|
1192
|
+
} else {
|
1193
|
+
// obj is a point
|
1194
|
+
var P = obj.elements || obj;
|
1195
|
+
return Plane.create(this.anchor.reflectionIn([P[0], P[1], (P[2] || 0)]), this.normal);
|
1196
|
+
}
|
1197
|
+
},
|
1198
|
+
|
1199
|
+
// Sets the anchor point and normal to the plane. If three arguments are specified,
|
1200
|
+
// the normal is calculated by assuming the three points should lie in the same plane.
|
1201
|
+
// If only two are sepcified, the second is taken to be the normal. Normal vector is
|
1202
|
+
// normalised before storage.
|
1203
|
+
setVectors: function(anchor, v1, v2) {
|
1204
|
+
anchor = Vector.create(anchor);
|
1205
|
+
anchor = anchor.to3D(); if (anchor === null) { return null; }
|
1206
|
+
v1 = Vector.create(v1);
|
1207
|
+
v1 = v1.to3D(); if (v1 === null) { return null; }
|
1208
|
+
if (typeof(v2) == 'undefined') {
|
1209
|
+
v2 = null;
|
1210
|
+
} else {
|
1211
|
+
v2 = Vector.create(v2);
|
1212
|
+
v2 = v2.to3D(); if (v2 === null) { return null; }
|
1213
|
+
}
|
1214
|
+
var A1 = anchor.elements[0], A2 = anchor.elements[1], A3 = anchor.elements[2];
|
1215
|
+
var v11 = v1.elements[0], v12 = v1.elements[1], v13 = v1.elements[2];
|
1216
|
+
var normal, mod;
|
1217
|
+
if (v2 !== null) {
|
1218
|
+
var v21 = v2.elements[0], v22 = v2.elements[1], v23 = v2.elements[2];
|
1219
|
+
normal = Vector.create([
|
1220
|
+
(v12 - A2) * (v23 - A3) - (v13 - A3) * (v22 - A2),
|
1221
|
+
(v13 - A3) * (v21 - A1) - (v11 - A1) * (v23 - A3),
|
1222
|
+
(v11 - A1) * (v22 - A2) - (v12 - A2) * (v21 - A1)
|
1223
|
+
]);
|
1224
|
+
mod = normal.modulus();
|
1225
|
+
if (mod === 0) { return null; }
|
1226
|
+
normal = Vector.create([normal.elements[0] / mod, normal.elements[1] / mod, normal.elements[2] / mod]);
|
1227
|
+
} else {
|
1228
|
+
mod = Math.sqrt(v11*v11 + v12*v12 + v13*v13);
|
1229
|
+
if (mod === 0) { return null; }
|
1230
|
+
normal = Vector.create([v1.elements[0] / mod, v1.elements[1] / mod, v1.elements[2] / mod]);
|
1231
|
+
}
|
1232
|
+
this.anchor = anchor;
|
1233
|
+
this.normal = normal;
|
1234
|
+
return this;
|
1235
|
+
}
|
1236
|
+
};
|
1237
|
+
|
1238
|
+
// Constructor function
|
1239
|
+
Plane.create = function(anchor, v1, v2) {
|
1240
|
+
var P = new Plane();
|
1241
|
+
return P.setVectors(anchor, v1, v2);
|
1242
|
+
};
|
1243
|
+
|
1244
|
+
// X-Y-Z planes
|
1245
|
+
Plane.XY = Plane.create(Vector.Zero(3), Vector.k);
|
1246
|
+
Plane.YZ = Plane.create(Vector.Zero(3), Vector.i);
|
1247
|
+
Plane.ZX = Plane.create(Vector.Zero(3), Vector.j);
|
1248
|
+
Plane.YX = Plane.XY; Plane.ZY = Plane.YZ; Plane.XZ = Plane.ZX;
|
1249
|
+
|
1250
|
+
// Utility functions
|
1251
|
+
var $V = Vector.create;
|
1252
|
+
var $M = Matrix.create;
|
1253
|
+
var $L = Line.create;
|
1254
|
+
var $P = Plane.create;
|