vector.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gfx3d.vector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gfx3d.vector"] = true;
  8. dojo.provide("dojox.gfx3d.vector");
  9. dojo.mixin(dojox.gfx3d.vector, {
  10. sum: function(){
  11. // summary: sum of the vectors
  12. var v = {x: 0, y: 0, z:0};
  13. dojo.forEach(arguments, function(item){ v.x += item.x; v.y += item.y; v.z += item.z; });
  14. return v;
  15. },
  16. center: function(){
  17. // summary: center of the vectors
  18. var l = arguments.length;
  19. if(l == 0){
  20. return {x: 0, y: 0, z: 0};
  21. }
  22. var v = dojox.gfx3d.vector.sum(arguments);
  23. return {x: v.x/l, y: v.y/l, z: v.z/l};
  24. },
  25. substract: function(/* Pointer */a, /* Pointer */b){
  26. return {x: a.x - b.x, y: a.y - b.y, z: a.z - b.z};
  27. },
  28. _crossProduct: function(x, y, z, u, v, w){
  29. // summary: applies a cross product of two vectorss, (x, y, z) and (u, v, w)
  30. // x: Number: an x coordinate of a point
  31. // y: Number: a y coordinate of a point
  32. // z: Number: a z coordinate of a point
  33. // u: Number: an x coordinate of a point
  34. // v: Number: a y coordinate of a point
  35. // w: Number: a z coordinate of a point
  36. return {x: y * w - z * v, y: z * u - x * w, z: x * v - y * u}; // Object
  37. },
  38. crossProduct: function(/* Number||Point */ a, /* Number||Point */ b, /* Number, optional */ c, /* Number, optional */ d, /* Number, optional */ e, /* Number, optional */ f){
  39. // summary: applies a matrix to a point
  40. // matrix: dojox.gfx3d.matrix.Matrix3D: a 3D matrix object to be applied
  41. // a: Number: an x coordinate of a point
  42. // b: Number: a y coordinate of a point
  43. // c: Number: a z coordinate of a point
  44. // d: Number: an x coordinate of a point
  45. // e: Number: a y coordinate of a point
  46. // f: Number: a z coordinate of a point
  47. if(arguments.length == 6 && dojo.every(arguments, function(item){ return typeof item == "number"; })){
  48. return dojox.gfx3d.vector._crossProduct(a, b, c, d, e, f); // Object
  49. }
  50. // branch
  51. // a: Object: a point
  52. // b: Object: a point
  53. // c: null
  54. // d: null
  55. // e: null
  56. // f: null
  57. return dojox.gfx3d.vector._crossProduct(a.x, a.y, a.z, b.x, b.y, b.z); // Object
  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 && dojo.every(arguments, function(item){ return typeof item == "number"; })){
  79. return dojox.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 dojox.gfx3d.vector._dotProduct(a.x, a.y, a.z, b.x, b.y, b.z); // Object
  89. },
  90. normalize: function(/* Point||Array*/ a, /* Point */ b, /* Point */ c){
  91. // summary: find the normal of the implicit surface
  92. // a: Object: a point
  93. // b: Object: a point
  94. // c: Object: a point
  95. var l, m, n;
  96. if(a instanceof Array){
  97. l = a[0]; m = a[1]; n = a[2];
  98. }else{
  99. l = a; m = b; n = c;
  100. }
  101. var u = dojox.gfx3d.vector.substract(m, l);
  102. var v = dojox.gfx3d.vector.substract(n, l);
  103. return dojox.gfx3d.vector.crossProduct(u, v);
  104. }
  105. });
  106. }