Vector3.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author *kile / http://kile.stravaganza.org/
  4. * @author philogb / http://blog.thejit.org/
  5. * @author mikael emtinger / http://gomo.se/
  6. * @author egraether / http://egraether.com/
  7. * @author WestLangley / http://github.com/WestLangley
  8. */
  9. THREE.Vector3 = function ( x, y, z ) {
  10. this.x = x || 0;
  11. this.y = y || 0;
  12. this.z = z || 0;
  13. };
  14. THREE.Vector3.prototype = {
  15. constructor: THREE.Vector3,
  16. set: function ( x, y, z ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. return this;
  21. },
  22. setX: function ( x ) {
  23. this.x = x;
  24. return this;
  25. },
  26. setY: function ( y ) {
  27. this.y = y;
  28. return this;
  29. },
  30. setZ: function ( z ) {
  31. this.z = z;
  32. return this;
  33. },
  34. copy: function ( v ) {
  35. this.x = v.x;
  36. this.y = v.y;
  37. this.z = v.z;
  38. return this;
  39. },
  40. add: function ( a, b ) {
  41. this.x = a.x + b.x;
  42. this.y = a.y + b.y;
  43. this.z = a.z + b.z;
  44. return this;
  45. },
  46. addSelf: function ( v ) {
  47. this.x += v.x;
  48. this.y += v.y;
  49. this.z += v.z;
  50. return this;
  51. },
  52. addScalar: function ( s ) {
  53. this.x += s;
  54. this.y += s;
  55. this.z += s;
  56. return this;
  57. },
  58. sub: function ( a, b ) {
  59. this.x = a.x - b.x;
  60. this.y = a.y - b.y;
  61. this.z = a.z - b.z;
  62. return this;
  63. },
  64. subSelf: function ( v ) {
  65. this.x -= v.x;
  66. this.y -= v.y;
  67. this.z -= v.z;
  68. return this;
  69. },
  70. multiply: function ( a, b ) {
  71. this.x = a.x * b.x;
  72. this.y = a.y * b.y;
  73. this.z = a.z * b.z;
  74. return this;
  75. },
  76. multiplySelf: function ( v ) {
  77. this.x *= v.x;
  78. this.y *= v.y;
  79. this.z *= v.z;
  80. return this;
  81. },
  82. multiplyScalar: function ( s ) {
  83. this.x *= s;
  84. this.y *= s;
  85. this.z *= s;
  86. return this;
  87. },
  88. divideSelf: function ( v ) {
  89. this.x /= v.x;
  90. this.y /= v.y;
  91. this.z /= v.z;
  92. return this;
  93. },
  94. divideScalar: function ( s ) {
  95. if ( s ) {
  96. this.x /= s;
  97. this.y /= s;
  98. this.z /= s;
  99. } else {
  100. this.x = 0;
  101. this.y = 0;
  102. this.z = 0;
  103. }
  104. return this;
  105. },
  106. minSelf: function ( v ) {
  107. if ( this.x > v.x ) {
  108. this.x = v.x;
  109. }
  110. if ( this.y > v.y ) {
  111. this.y = v.y;
  112. }
  113. if ( this.z > v.z ) {
  114. this.z = v.z;
  115. }
  116. return this;
  117. },
  118. maxSelf: function ( v ) {
  119. if ( this.x < v.x ) {
  120. this.x = v.x;
  121. }
  122. if ( this.y < v.y ) {
  123. this.y = v.y;
  124. }
  125. if ( this.z < v.z ) {
  126. this.z = v.z;
  127. }
  128. return this;
  129. },
  130. clampSelf: function ( min, max ) {
  131. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  132. if ( this.x < min.x ) {
  133. this.x = min.x;
  134. } else if ( this.x > max.x ) {
  135. this.x = max.x;
  136. }
  137. if ( this.y < min.y ) {
  138. this.y = min.y;
  139. } else if ( this.y > max.y ) {
  140. this.y = max.y;
  141. }
  142. if ( this.z < min.z ) {
  143. this.z = min.z;
  144. } else if ( this.z > max.z ) {
  145. this.z = max.z;
  146. }
  147. return this;
  148. },
  149. negate: function() {
  150. return this.multiplyScalar( - 1 );
  151. },
  152. dot: function ( v ) {
  153. return this.x * v.x + this.y * v.y + this.z * v.z;
  154. },
  155. lengthSq: function () {
  156. return this.x * this.x + this.y * this.y + this.z * this.z;
  157. },
  158. length: function () {
  159. return Math.sqrt( this.lengthSq() );
  160. },
  161. lengthManhattan: function () {
  162. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  163. },
  164. normalize: function () {
  165. return this.divideScalar( this.length() );
  166. },
  167. setLength: function ( l ) {
  168. return this.normalize().multiplyScalar( l );
  169. },
  170. lerpSelf: function ( v, alpha ) {
  171. this.x += ( v.x - this.x ) * alpha;
  172. this.y += ( v.y - this.y ) * alpha;
  173. this.z += ( v.z - this.z ) * alpha;
  174. return this;
  175. },
  176. cross: function ( a, b ) {
  177. this.x = a.y * b.z - a.z * b.y;
  178. this.y = a.z * b.x - a.x * b.z;
  179. this.z = a.x * b.y - a.y * b.x;
  180. return this;
  181. },
  182. crossSelf: function ( v ) {
  183. var x = this.x, y = this.y, z = this.z;
  184. this.x = y * v.z - z * v.y;
  185. this.y = z * v.x - x * v.z;
  186. this.z = x * v.y - y * v.x;
  187. return this;
  188. },
  189. angleTo: function ( v ) {
  190. return Math.acos( this.dot( v ) / this.length() / v.length() );
  191. },
  192. distanceTo: function ( v ) {
  193. return Math.sqrt( this.distanceToSquared( v ) );
  194. },
  195. distanceToSquared: function ( v ) {
  196. var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  197. return dx * dx + dy * dy + dz * dz;
  198. },
  199. getPositionFromMatrix: function ( m ) {
  200. this.x = m.elements[12];
  201. this.y = m.elements[13];
  202. this.z = m.elements[14];
  203. return this;
  204. },
  205. setEulerFromRotationMatrix: function ( m, order ) {
  206. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  207. // clamp, to handle numerical problems
  208. function clamp( x ) {
  209. return Math.min( Math.max( x, -1 ), 1 );
  210. }
  211. var te = m.elements;
  212. var m11 = te[0], m12 = te[4], m13 = te[8];
  213. var m21 = te[1], m22 = te[5], m23 = te[9];
  214. var m31 = te[2], m32 = te[6], m33 = te[10];
  215. if ( order === undefined || order === 'XYZ' ) {
  216. this.y = Math.asin( clamp( m13 ) );
  217. if ( Math.abs( m13 ) < 0.99999 ) {
  218. this.x = Math.atan2( - m23, m33 );
  219. this.z = Math.atan2( - m12, m11 );
  220. } else {
  221. this.x = Math.atan2( m32, m22 );
  222. this.z = 0;
  223. }
  224. } else if ( order === 'YXZ' ) {
  225. this.x = Math.asin( - clamp( m23 ) );
  226. if ( Math.abs( m23 ) < 0.99999 ) {
  227. this.y = Math.atan2( m13, m33 );
  228. this.z = Math.atan2( m21, m22 );
  229. } else {
  230. this.y = Math.atan2( - m31, m11 );
  231. this.z = 0;
  232. }
  233. } else if ( order === 'ZXY' ) {
  234. this.x = Math.asin( clamp( m32 ) );
  235. if ( Math.abs( m32 ) < 0.99999 ) {
  236. this.y = Math.atan2( - m31, m33 );
  237. this.z = Math.atan2( - m12, m22 );
  238. } else {
  239. this.y = 0;
  240. this.z = Math.atan2( m21, m11 );
  241. }
  242. } else if ( order === 'ZYX' ) {
  243. this.y = Math.asin( - clamp( m31 ) );
  244. if ( Math.abs( m31 ) < 0.99999 ) {
  245. this.x = Math.atan2( m32, m33 );
  246. this.z = Math.atan2( m21, m11 );
  247. } else {
  248. this.x = 0;
  249. this.z = Math.atan2( - m12, m22 );
  250. }
  251. } else if ( order === 'YZX' ) {
  252. this.z = Math.asin( clamp( m21 ) );
  253. if ( Math.abs( m21 ) < 0.99999 ) {
  254. this.x = Math.atan2( - m23, m22 );
  255. this.y = Math.atan2( - m31, m11 );
  256. } else {
  257. this.x = 0;
  258. this.y = Math.atan2( m13, m33 );
  259. }
  260. } else if ( order === 'XZY' ) {
  261. this.z = Math.asin( - clamp( m12 ) );
  262. if ( Math.abs( m12 ) < 0.99999 ) {
  263. this.x = Math.atan2( m32, m22 );
  264. this.y = Math.atan2( m13, m11 );
  265. } else {
  266. this.x = Math.atan2( - m23, m33 );
  267. this.y = 0;
  268. }
  269. }
  270. return this;
  271. },
  272. setEulerFromQuaternion: function ( q, order ) {
  273. // q is assumed to be normalized
  274. // clamp, to handle numerical problems
  275. function clamp( x ) {
  276. return Math.min( Math.max( x, -1 ), 1 );
  277. }
  278. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  279. var sqx = q.x * q.x;
  280. var sqy = q.y * q.y;
  281. var sqz = q.z * q.z;
  282. var sqw = q.w * q.w;
  283. if ( order === undefined || order === 'XYZ' ) {
  284. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  285. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  286. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  287. } else if ( order === 'YXZ' ) {
  288. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  289. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  290. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  291. } else if ( order === 'ZXY' ) {
  292. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  293. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  294. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  295. } else if ( order === 'ZYX' ) {
  296. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  297. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  298. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  299. } else if ( order === 'YZX' ) {
  300. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  301. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  302. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  303. } else if ( order === 'XZY' ) {
  304. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  305. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  306. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  307. }
  308. return this;
  309. },
  310. getScaleFromMatrix: function ( m ) {
  311. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  312. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  313. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  314. this.x = sx;
  315. this.y = sy;
  316. this.z = sz;
  317. return this;
  318. },
  319. equals: function ( v ) {
  320. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  321. },
  322. clone: function () {
  323. return new THREE.Vector3( this.x, this.y, this.z );
  324. }
  325. };