Vector4.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author philogb / http://blog.thejit.org/
  4. * @author mikael emtinger / http://gomo.se/
  5. * @author egraether / http://egraether.com/
  6. * @author WestLangley / http://github.com/WestLangley
  7. */
  8. THREE.Vector4 = function ( x, y, z, w ) {
  9. this.x = x || 0;
  10. this.y = y || 0;
  11. this.z = z || 0;
  12. this.w = ( w !== undefined ) ? w : 1;
  13. };
  14. THREE.Vector4.prototype = {
  15. constructor: THREE.Vector4,
  16. set: function ( x, y, z, w ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. this.w = w;
  21. return this;
  22. },
  23. setScalar: function ( scalar ) {
  24. this.x = scalar;
  25. this.y = scalar;
  26. this.z = scalar;
  27. this.w = scalar;
  28. return this;
  29. },
  30. setX: function ( x ) {
  31. this.x = x;
  32. return this;
  33. },
  34. setY: function ( y ) {
  35. this.y = y;
  36. return this;
  37. },
  38. setZ: function ( z ) {
  39. this.z = z;
  40. return this;
  41. },
  42. setW: function ( w ) {
  43. this.w = w;
  44. return this;
  45. },
  46. setComponent: function ( index, value ) {
  47. switch ( index ) {
  48. case 0: this.x = value; break;
  49. case 1: this.y = value; break;
  50. case 2: this.z = value; break;
  51. case 3: this.w = value; break;
  52. default: throw new Error( 'index is out of range: ' + index );
  53. }
  54. },
  55. getComponent: function ( index ) {
  56. switch ( index ) {
  57. case 0: return this.x;
  58. case 1: return this.y;
  59. case 2: return this.z;
  60. case 3: return this.w;
  61. default: throw new Error( 'index is out of range: ' + index );
  62. }
  63. },
  64. clone: function () {
  65. return new this.constructor( this.x, this.y, this.z, this.w );
  66. },
  67. copy: function ( v ) {
  68. this.x = v.x;
  69. this.y = v.y;
  70. this.z = v.z;
  71. this.w = ( v.w !== undefined ) ? v.w : 1;
  72. return this;
  73. },
  74. add: function ( v, w ) {
  75. if ( w !== undefined ) {
  76. console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  77. return this.addVectors( v, w );
  78. }
  79. this.x += v.x;
  80. this.y += v.y;
  81. this.z += v.z;
  82. this.w += v.w;
  83. return this;
  84. },
  85. addScalar: function ( s ) {
  86. this.x += s;
  87. this.y += s;
  88. this.z += s;
  89. this.w += s;
  90. return this;
  91. },
  92. addVectors: function ( a, b ) {
  93. this.x = a.x + b.x;
  94. this.y = a.y + b.y;
  95. this.z = a.z + b.z;
  96. this.w = a.w + b.w;
  97. return this;
  98. },
  99. addScaledVector: function ( v, s ) {
  100. this.x += v.x * s;
  101. this.y += v.y * s;
  102. this.z += v.z * s;
  103. this.w += v.w * s;
  104. return this;
  105. },
  106. sub: function ( v, w ) {
  107. if ( w !== undefined ) {
  108. console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  109. return this.subVectors( v, w );
  110. }
  111. this.x -= v.x;
  112. this.y -= v.y;
  113. this.z -= v.z;
  114. this.w -= v.w;
  115. return this;
  116. },
  117. subScalar: function ( s ) {
  118. this.x -= s;
  119. this.y -= s;
  120. this.z -= s;
  121. this.w -= s;
  122. return this;
  123. },
  124. subVectors: function ( a, b ) {
  125. this.x = a.x - b.x;
  126. this.y = a.y - b.y;
  127. this.z = a.z - b.z;
  128. this.w = a.w - b.w;
  129. return this;
  130. },
  131. multiplyScalar: function ( scalar ) {
  132. if ( isFinite( scalar ) ) {
  133. this.x *= scalar;
  134. this.y *= scalar;
  135. this.z *= scalar;
  136. this.w *= scalar;
  137. } else {
  138. this.x = 0;
  139. this.y = 0;
  140. this.z = 0;
  141. this.w = 0;
  142. }
  143. return this;
  144. },
  145. applyMatrix4: function ( m ) {
  146. var x = this.x;
  147. var y = this.y;
  148. var z = this.z;
  149. var w = this.w;
  150. var e = m.elements;
  151. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  152. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  153. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  154. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  155. return this;
  156. },
  157. divideScalar: function ( scalar ) {
  158. return this.multiplyScalar( 1 / scalar );
  159. },
  160. setAxisAngleFromQuaternion: function ( q ) {
  161. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  162. // q is assumed to be normalized
  163. this.w = 2 * Math.acos( q.w );
  164. var s = Math.sqrt( 1 - q.w * q.w );
  165. if ( s < 0.0001 ) {
  166. this.x = 1;
  167. this.y = 0;
  168. this.z = 0;
  169. } else {
  170. this.x = q.x / s;
  171. this.y = q.y / s;
  172. this.z = q.z / s;
  173. }
  174. return this;
  175. },
  176. setAxisAngleFromRotationMatrix: function ( m ) {
  177. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  178. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  179. var angle, x, y, z, // variables for result
  180. epsilon = 0.01, // margin to allow for rounding errors
  181. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  182. te = m.elements,
  183. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  184. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  185. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  186. if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
  187. ( Math.abs( m13 - m31 ) < epsilon ) &&
  188. ( Math.abs( m23 - m32 ) < epsilon ) ) {
  189. // singularity found
  190. // first check for identity matrix which must have +1 for all terms
  191. // in leading diagonal and zero in other terms
  192. if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
  193. ( Math.abs( m13 + m31 ) < epsilon2 ) &&
  194. ( Math.abs( m23 + m32 ) < epsilon2 ) &&
  195. ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  196. // this singularity is identity matrix so angle = 0
  197. this.set( 1, 0, 0, 0 );
  198. return this; // zero angle, arbitrary axis
  199. }
  200. // otherwise this singularity is angle = 180
  201. angle = Math.PI;
  202. var xx = ( m11 + 1 ) / 2;
  203. var yy = ( m22 + 1 ) / 2;
  204. var zz = ( m33 + 1 ) / 2;
  205. var xy = ( m12 + m21 ) / 4;
  206. var xz = ( m13 + m31 ) / 4;
  207. var yz = ( m23 + m32 ) / 4;
  208. if ( ( xx > yy ) && ( xx > zz ) ) {
  209. // m11 is the largest diagonal term
  210. if ( xx < epsilon ) {
  211. x = 0;
  212. y = 0.707106781;
  213. z = 0.707106781;
  214. } else {
  215. x = Math.sqrt( xx );
  216. y = xy / x;
  217. z = xz / x;
  218. }
  219. } else if ( yy > zz ) {
  220. // m22 is the largest diagonal term
  221. if ( yy < epsilon ) {
  222. x = 0.707106781;
  223. y = 0;
  224. z = 0.707106781;
  225. } else {
  226. y = Math.sqrt( yy );
  227. x = xy / y;
  228. z = yz / y;
  229. }
  230. } else {
  231. // m33 is the largest diagonal term so base result on this
  232. if ( zz < epsilon ) {
  233. x = 0.707106781;
  234. y = 0.707106781;
  235. z = 0;
  236. } else {
  237. z = Math.sqrt( zz );
  238. x = xz / z;
  239. y = yz / z;
  240. }
  241. }
  242. this.set( x, y, z, angle );
  243. return this; // return 180 deg rotation
  244. }
  245. // as we have reached here there are no singularities so we can handle normally
  246. var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
  247. ( m13 - m31 ) * ( m13 - m31 ) +
  248. ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  249. if ( Math.abs( s ) < 0.001 ) s = 1;
  250. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  251. // caught by singularity test above, but I've left it in just in case
  252. this.x = ( m32 - m23 ) / s;
  253. this.y = ( m13 - m31 ) / s;
  254. this.z = ( m21 - m12 ) / s;
  255. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  256. return this;
  257. },
  258. min: function ( v ) {
  259. this.x = Math.min( this.x, v.x );
  260. this.y = Math.min( this.y, v.y );
  261. this.z = Math.min( this.z, v.z );
  262. this.w = Math.min( this.w, v.w );
  263. return this;
  264. },
  265. max: function ( v ) {
  266. this.x = Math.max( this.x, v.x );
  267. this.y = Math.max( this.y, v.y );
  268. this.z = Math.max( this.z, v.z );
  269. this.w = Math.max( this.w, v.w );
  270. return this;
  271. },
  272. clamp: function ( min, max ) {
  273. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  274. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  275. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  276. this.z = Math.max( min.z, Math.min( max.z, this.z ) );
  277. this.w = Math.max( min.w, Math.min( max.w, this.w ) );
  278. return this;
  279. },
  280. clampScalar: function () {
  281. var min, max;
  282. return function clampScalar( minVal, maxVal ) {
  283. if ( min === undefined ) {
  284. min = new THREE.Vector4();
  285. max = new THREE.Vector4();
  286. }
  287. min.set( minVal, minVal, minVal, minVal );
  288. max.set( maxVal, maxVal, maxVal, maxVal );
  289. return this.clamp( min, max );
  290. };
  291. }(),
  292. floor: function () {
  293. this.x = Math.floor( this.x );
  294. this.y = Math.floor( this.y );
  295. this.z = Math.floor( this.z );
  296. this.w = Math.floor( this.w );
  297. return this;
  298. },
  299. ceil: function () {
  300. this.x = Math.ceil( this.x );
  301. this.y = Math.ceil( this.y );
  302. this.z = Math.ceil( this.z );
  303. this.w = Math.ceil( this.w );
  304. return this;
  305. },
  306. round: function () {
  307. this.x = Math.round( this.x );
  308. this.y = Math.round( this.y );
  309. this.z = Math.round( this.z );
  310. this.w = Math.round( this.w );
  311. return this;
  312. },
  313. roundToZero: function () {
  314. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  315. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  316. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  317. this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
  318. return this;
  319. },
  320. negate: function () {
  321. this.x = - this.x;
  322. this.y = - this.y;
  323. this.z = - this.z;
  324. this.w = - this.w;
  325. return this;
  326. },
  327. dot: function ( v ) {
  328. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  329. },
  330. lengthSq: function () {
  331. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  332. },
  333. length: function () {
  334. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  335. },
  336. lengthManhattan: function () {
  337. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  338. },
  339. normalize: function () {
  340. return this.divideScalar( this.length() );
  341. },
  342. setLength: function ( length ) {
  343. return this.multiplyScalar( length / this.length() );
  344. },
  345. lerp: function ( v, alpha ) {
  346. this.x += ( v.x - this.x ) * alpha;
  347. this.y += ( v.y - this.y ) * alpha;
  348. this.z += ( v.z - this.z ) * alpha;
  349. this.w += ( v.w - this.w ) * alpha;
  350. return this;
  351. },
  352. lerpVectors: function ( v1, v2, alpha ) {
  353. this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  354. return this;
  355. },
  356. equals: function ( v ) {
  357. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  358. },
  359. fromArray: function ( array, offset ) {
  360. if ( offset === undefined ) offset = 0;
  361. this.x = array[ offset ];
  362. this.y = array[ offset + 1 ];
  363. this.z = array[ offset + 2 ];
  364. this.w = array[ offset + 3 ];
  365. return this;
  366. },
  367. toArray: function ( array, offset ) {
  368. if ( array === undefined ) array = [];
  369. if ( offset === undefined ) offset = 0;
  370. array[ offset ] = this.x;
  371. array[ offset + 1 ] = this.y;
  372. array[ offset + 2 ] = this.z;
  373. array[ offset + 3 ] = this.w;
  374. return array;
  375. },
  376. fromAttribute: function ( attribute, index, offset ) {
  377. if ( offset === undefined ) offset = 0;
  378. index = index * attribute.itemSize + offset;
  379. this.x = attribute.array[ index ];
  380. this.y = attribute.array[ index + 1 ];
  381. this.z = attribute.array[ index + 2 ];
  382. this.w = attribute.array[ index + 3 ];
  383. return this;
  384. }
  385. };