Vector4.js 11 KB

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