vector.js 3.7 KB

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