Vector4.js 8.1 KB

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