1 | define(["dojo/_base/lang", "dojo/_base/array", "./_base"],function(lang, arrayUtil, gfx3d) { |
---|
2 | |
---|
3 | gfx3d.vector = { |
---|
4 | |
---|
5 | sum: function(){ |
---|
6 | // summary: |
---|
7 | // sum of the vectors |
---|
8 | var v = {x: 0, y: 0, z:0}; |
---|
9 | arrayUtil.forEach(arguments, function(item){ v.x += item.x; v.y += item.y; v.z += item.z; }); |
---|
10 | return v; |
---|
11 | }, |
---|
12 | |
---|
13 | center: function(){ |
---|
14 | // summary: |
---|
15 | // center of the vectors |
---|
16 | var l = arguments.length; |
---|
17 | if(l == 0){ |
---|
18 | return {x: 0, y: 0, z: 0}; |
---|
19 | } |
---|
20 | var v = gfx3d.vector.sum(arguments); |
---|
21 | return {x: v.x/l, y: v.y/l, z: v.z/l}; |
---|
22 | }, |
---|
23 | |
---|
24 | substract: function(/* Pointer */a, /* Pointer */b){ |
---|
25 | return {x: a.x - b.x, y: a.y - b.y, z: a.z - b.z}; |
---|
26 | }, |
---|
27 | |
---|
28 | _crossProduct: function(x, y, z, u, v, w){ |
---|
29 | // summary: |
---|
30 | // applies a cross product of two vectors, (x, y, z) and (u, v, w) |
---|
31 | // x: Number |
---|
32 | // x coordinate of first vector |
---|
33 | // y: Number |
---|
34 | // y coordinate of first vector |
---|
35 | // z: Number |
---|
36 | // z coordinate of first vector |
---|
37 | // u: Number |
---|
38 | // x coordinate of second vector |
---|
39 | // v: Number |
---|
40 | // y coordinate of second vector |
---|
41 | // w: Number |
---|
42 | // z coordinate of second vector |
---|
43 | return {x: y * w - z * v, y: z * u - x * w, z: x * v - y * u}; // Object |
---|
44 | }, |
---|
45 | |
---|
46 | crossProduct: function(/* Number||Point */ a, /* Number||Point */ b, /* Number, optional */ c, /* Number, optional */ d, /* Number, optional */ e, /* Number, optional */ f){ |
---|
47 | // summary: |
---|
48 | // applies a matrix to a point |
---|
49 | // matrix: dojox.gfx3d.matrix.Matrix3D |
---|
50 | // a 3D matrix object to be applied |
---|
51 | // a: Number|Point |
---|
52 | // x coordinate of first point, or the whole first point |
---|
53 | // b: Number|Point |
---|
54 | // y coordinate of first point, or the whole second point |
---|
55 | // c: Number |
---|
56 | // z coordinate of first point |
---|
57 | // d: Number |
---|
58 | // x coordinate of second point |
---|
59 | // e: Number |
---|
60 | // y coordinate of second point |
---|
61 | // f: Number |
---|
62 | // z coordinate of second point |
---|
63 | if(arguments.length == 6 && arrayUtil.every(arguments, function(item){ return typeof item == "number"; })){ |
---|
64 | return gfx3d.vector._crossProduct(a, b, c, d, e, f); // Object |
---|
65 | } |
---|
66 | |
---|
67 | return gfx3d.vector._crossProduct(a.x, a.y, a.z, b.x, b.y, b.z); // Object |
---|
68 | }, |
---|
69 | |
---|
70 | _dotProduct: function(x, y, z, u, v, w){ |
---|
71 | // summary: |
---|
72 | // applies a cross product of two vectors, (x, y, z) and (u, v, w) |
---|
73 | // x: Number |
---|
74 | // x coordinate of first point |
---|
75 | // y: Number |
---|
76 | // y coordinate of first point |
---|
77 | // z: Number |
---|
78 | // z coordinate of first point |
---|
79 | // u: Number |
---|
80 | // x coordinate of second point |
---|
81 | // v: Number |
---|
82 | // y coordinate of second point |
---|
83 | // w: Number |
---|
84 | // z coordinate of second point |
---|
85 | return x * u + y * v + z * w; // Number |
---|
86 | }, |
---|
87 | dotProduct: function(a, b, c, d, e, f){ |
---|
88 | // summary: |
---|
89 | // applies a matrix to a point |
---|
90 | // matrix: dojox.gfx3d.matrix.Matrix3D |
---|
91 | // a 3D matrix object to be applied |
---|
92 | // a: Number|Point |
---|
93 | // x coordinate of first point, or the whole first Point |
---|
94 | // b: Number|Point |
---|
95 | // y coordinate of first Point, or the whole second Point |
---|
96 | // c: Number? |
---|
97 | // z coordinate of first point |
---|
98 | // d: Number? |
---|
99 | // x coordinate of second point |
---|
100 | // e: Number? |
---|
101 | // y coordinate of second point |
---|
102 | // f: Number? |
---|
103 | // z coordinate of second point |
---|
104 | if(arguments.length == 6 && arrayUtil.every(arguments, function(item){ return typeof item == "number"; })){ |
---|
105 | return gfx3d.vector._dotProduct(a, b, c, d, e, f); // Object |
---|
106 | } |
---|
107 | return gfx3d.vector._dotProduct(a.x, a.y, a.z, b.x, b.y, b.z); // Object |
---|
108 | }, |
---|
109 | |
---|
110 | normalize: function(/* Point||Array*/ a, /* Point */ b, /* Point */ c){ |
---|
111 | // summary: |
---|
112 | // find the normal of the implicit surface |
---|
113 | // a: Object |
---|
114 | // a point |
---|
115 | // b: Object |
---|
116 | // a point |
---|
117 | // c: Object |
---|
118 | // a point |
---|
119 | var l, m, n; |
---|
120 | if(a instanceof Array){ |
---|
121 | l = a[0]; m = a[1]; n = a[2]; |
---|
122 | }else{ |
---|
123 | l = a; m = b; n = c; |
---|
124 | } |
---|
125 | |
---|
126 | var u = gfx3d.vector.substract(m, l); |
---|
127 | var v = gfx3d.vector.substract(n, l); |
---|
128 | return gfx3d.vector.crossProduct(u, v); |
---|
129 | } |
---|
130 | }; |
---|
131 | return gfx3d.vector; |
---|
132 | }); |
---|