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