Vector4.js 8.5 KB

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