Vector4.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. copy: function ( v ) {
  24. this.x = v.x;
  25. this.y = v.y;
  26. this.z = v.z;
  27. this.w = ( v.w !== undefined ) ? v.w : 1;
  28. return this;
  29. },
  30. add: function ( a, b ) {
  31. this.x = a.x + b.x;
  32. this.y = a.y + b.y;
  33. this.z = a.z + b.z;
  34. this.w = a.w + b.w;
  35. return this;
  36. },
  37. addSelf: function ( v ) {
  38. this.x += v.x;
  39. this.y += v.y;
  40. this.z += v.z;
  41. this.w += v.w;
  42. return this;
  43. },
  44. sub: function ( a, b ) {
  45. this.x = a.x - b.x;
  46. this.y = a.y - b.y;
  47. this.z = a.z - b.z;
  48. this.w = a.w - b.w;
  49. return this;
  50. },
  51. subSelf: function ( v ) {
  52. this.x -= v.x;
  53. this.y -= v.y;
  54. this.z -= v.z;
  55. this.w -= v.w;
  56. return this;
  57. },
  58. multiplyScalar: function ( s ) {
  59. this.x *= s;
  60. this.y *= s;
  61. this.z *= s;
  62. this.w *= s;
  63. return this;
  64. },
  65. divideScalar: function ( s ) {
  66. if ( s ) {
  67. this.x /= s;
  68. this.y /= s;
  69. this.z /= s;
  70. this.w /= s;
  71. } else {
  72. this.x = 0;
  73. this.y = 0;
  74. this.z = 0;
  75. this.w = 1;
  76. }
  77. return this;
  78. },
  79. negate: function() {
  80. return this.multiplyScalar( -1 );
  81. },
  82. dot: function ( v ) {
  83. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  84. },
  85. lengthSq: function () {
  86. return this.dot( this );
  87. },
  88. length: function () {
  89. return Math.sqrt( this.lengthSq() );
  90. },
  91. lengthManhattan: function () {
  92. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  93. },
  94. normalize: function () {
  95. return this.divideScalar( this.length() );
  96. },
  97. setLength: function ( l ) {
  98. return this.normalize().multiplyScalar( l );
  99. },
  100. lerpSelf: function ( v, alpha ) {
  101. this.x += ( v.x - this.x ) * alpha;
  102. this.y += ( v.y - this.y ) * alpha;
  103. this.z += ( v.z - this.z ) * alpha;
  104. this.w += ( v.w - this.w ) * alpha;
  105. return this;
  106. },
  107. clone: function () {
  108. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  109. },
  110. setAxisAngleFromQuaternion: function ( q ) {
  111. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  112. // q is assumed to be normalized
  113. this.w = 2 * Math.acos( q.w );
  114. var s = Math.sqrt( 1 - q.w * q.w );
  115. if ( s < 0.0001 ) {
  116. this.x = 1;
  117. this.y = 0;
  118. this.z = 0;
  119. } else {
  120. this.x = q.x / s;
  121. this.y = q.y / s;
  122. this.z = q.z / s;
  123. }
  124. return this;
  125. },
  126. setAxisAngleFromRotationMatrix: function ( m ) {
  127. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  128. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  129. var angle, x, y, z, // variables for result
  130. epsilon = 0.01, // margin to allow for rounding errors
  131. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  132. te = m.elements,
  133. m11 = te[0], m12 = te[4], m13 = te[8],
  134. m21 = te[1], m22 = te[5], m23 = te[9],
  135. m31 = te[2], m32 = te[6], m33 = te[10];
  136. if ( ( Math.abs( m12 - m21 ) < epsilon )
  137. && ( Math.abs( m13 - m31 ) < epsilon )
  138. && ( Math.abs( m23 - m32 ) < epsilon ) ) {
  139. // singularity found
  140. // first check for identity matrix which must have +1 for all terms
  141. // in leading diagonal and zero in other terms
  142. if ( ( Math.abs( m12 + m21 ) < epsilon2 )
  143. && ( Math.abs( m13 + m31 ) < epsilon2 )
  144. && ( Math.abs( m23 + m32 ) < epsilon2 )
  145. && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  146. // this singularity is identity matrix so angle = 0
  147. this.set( 1, 0, 0, 0 );
  148. return this; // zero angle, arbitrary axis
  149. }
  150. // otherwise this singularity is angle = 180
  151. angle = Math.PI;
  152. var xx = ( m11 + 1 ) / 2;
  153. var yy = ( m22 + 1 ) / 2;
  154. var zz = ( m33 + 1 ) / 2;
  155. var xy = ( m12 + m21 ) / 4;
  156. var xz = ( m13 + m31 ) / 4;
  157. var yz = ( m23 + m32 ) / 4;
  158. if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
  159. if ( xx < epsilon ) {
  160. x = 0;
  161. y = 0.707106781;
  162. z = 0.707106781;
  163. } else {
  164. x = Math.sqrt( xx );
  165. y = xy / x;
  166. z = xz / x;
  167. }
  168. } else if ( yy > zz ) { // m22 is the largest diagonal term
  169. if ( yy < epsilon ) {
  170. x = 0.707106781;
  171. y = 0;
  172. z = 0.707106781;
  173. } else {
  174. y = Math.sqrt( yy );
  175. x = xy / y;
  176. z = yz / y;
  177. }
  178. } else { // m33 is the largest diagonal term so base result on this
  179. if ( zz < epsilon ) {
  180. x = 0.707106781;
  181. y = 0.707106781;
  182. z = 0;
  183. } else {
  184. z = Math.sqrt( zz );
  185. x = xz / z;
  186. y = yz / z;
  187. }
  188. }
  189. this.set( x, y, z, angle );
  190. return this; // return 180 deg rotation
  191. }
  192. // as we have reached here there are no singularities so we can handle normally
  193. var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
  194. + ( m13 - m31 ) * ( m13 - m31 )
  195. + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  196. if ( Math.abs( s ) < 0.001 ) s = 1;
  197. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  198. // caught by singularity test above, but I've left it in just in case
  199. this.x = ( m32 - m23 ) / s;
  200. this.y = ( m13 - m31 ) / s;
  201. this.z = ( m21 - m12 ) / s;
  202. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  203. return this;
  204. }
  205. };
粤ICP备19079148号