Vector3.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. QUnit.module( "Vector3" );
  5. QUnit.test( "constructor" , function( assert ) {
  6. var a = new THREE.Vector3();
  7. assert.ok( a.x == 0, "Passed!" );
  8. assert.ok( a.y == 0, "Passed!" );
  9. assert.ok( a.z == 0, "Passed!" );
  10. a = new THREE.Vector3( x, y, z );
  11. assert.ok( a.x === x, "Passed!" );
  12. assert.ok( a.y === y, "Passed!" );
  13. assert.ok( a.z === z, "Passed!" );
  14. });
  15. QUnit.test( "copy" , function( assert ) {
  16. var a = new THREE.Vector3( x, y, z );
  17. var b = new THREE.Vector3().copy( a );
  18. assert.ok( b.x == x, "Passed!" );
  19. assert.ok( b.y == y, "Passed!" );
  20. assert.ok( b.z == z, "Passed!" );
  21. // ensure that it is a true copy
  22. a.x = 0;
  23. a.y = -1;
  24. a.z = -2;
  25. assert.ok( b.x == x, "Passed!" );
  26. assert.ok( b.y == y, "Passed!" );
  27. assert.ok( b.z == z, "Passed!" );
  28. });
  29. QUnit.test( "set" , function( assert ) {
  30. var a = new THREE.Vector3();
  31. assert.ok( a.x == 0, "Passed!" );
  32. assert.ok( a.y == 0, "Passed!" );
  33. assert.ok( a.z == 0, "Passed!" );
  34. a.set( x, y, z );
  35. assert.ok( a.x == x, "Passed!" );
  36. assert.ok( a.y == y, "Passed!" );
  37. assert.ok( a.z == z, "Passed!" );
  38. });
  39. QUnit.test( "setX,setY,setZ", function( assert ) {
  40. var a = new THREE.Vector3();
  41. assert.ok( a.x == 0, "Passed!" );
  42. assert.ok( a.y == 0, "Passed!" );
  43. assert.ok( a.z == 0, "Passed!" );
  44. a.setX( x );
  45. a.setY( y );
  46. a.setZ( z );
  47. assert.ok( a.x == x, "Passed!" );
  48. assert.ok( a.y == y, "Passed!" );
  49. assert.ok( a.z == z, "Passed!" );
  50. });
  51. QUnit.test( "setComponent,getComponent", function( assert ) {
  52. var a = new THREE.Vector3();
  53. assert.ok( a.x == 0, "Passed!" );
  54. assert.ok( a.y == 0, "Passed!" );
  55. assert.ok( a.z == 0, "Passed!" );
  56. a.setComponent( 0, 1 );
  57. a.setComponent( 1, 2 );
  58. a.setComponent( 2, 3 );
  59. assert.ok( a.getComponent( 0 ) == 1, "Passed!" );
  60. assert.ok( a.getComponent( 1 ) == 2, "Passed!" );
  61. assert.ok( a.getComponent( 2 ) == 3, "Passed!" );
  62. });
  63. QUnit.test( "add" , function( assert ) {
  64. var a = new THREE.Vector3( x, y, z );
  65. var b = new THREE.Vector3( -x, -y, -z );
  66. a.add( b );
  67. assert.ok( a.x == 0, "Passed!" );
  68. assert.ok( a.y == 0, "Passed!" );
  69. assert.ok( a.z == 0, "Passed!" );
  70. var c = new THREE.Vector3().addVectors( b, b );
  71. assert.ok( c.x == -2*x, "Passed!" );
  72. assert.ok( c.y == -2*y, "Passed!" );
  73. assert.ok( c.z == -2*z, "Passed!" );
  74. });
  75. QUnit.test( "sub" , function( assert ) {
  76. var a = new THREE.Vector3( x, y, z );
  77. var b = new THREE.Vector3( -x, -y, -z );
  78. a.sub( b );
  79. assert.ok( a.x == 2*x, "Passed!" );
  80. assert.ok( a.y == 2*y, "Passed!" );
  81. assert.ok( a.z == 2*z, "Passed!" );
  82. var c = new THREE.Vector3().subVectors( a, a );
  83. assert.ok( c.x == 0, "Passed!" );
  84. assert.ok( c.y == 0, "Passed!" );
  85. assert.ok( c.z == 0, "Passed!" );
  86. });
  87. QUnit.test( "multiply/divide", function( assert ) {
  88. var a = new THREE.Vector3( x, y, z );
  89. var b = new THREE.Vector3( -x, -y, -z );
  90. a.multiplyScalar( -2 );
  91. assert.ok( a.x == x*-2, "Passed!" );
  92. assert.ok( a.y == y*-2, "Passed!" );
  93. assert.ok( a.z == z*-2, "Passed!" );
  94. b.multiplyScalar( -2 );
  95. assert.ok( b.x == 2*x, "Passed!" );
  96. assert.ok( b.y == 2*y, "Passed!" );
  97. assert.ok( b.z == 2*z, "Passed!" );
  98. a.divideScalar( -2 );
  99. assert.ok( a.x == x, "Passed!" );
  100. assert.ok( a.y == y, "Passed!" );
  101. assert.ok( a.z == z, "Passed!" );
  102. b.divideScalar( -2 );
  103. assert.ok( b.x == -x, "Passed!" );
  104. assert.ok( b.y == -y, "Passed!" );
  105. assert.ok( b.z == -z, "Passed!" );
  106. });
  107. QUnit.test( "min/max/clamp", function( assert ) {
  108. var a = new THREE.Vector3( x, y, z );
  109. var b = new THREE.Vector3( -x, -y, -z );
  110. var c = new THREE.Vector3();
  111. c.copy( a ).min( b );
  112. assert.ok( c.x == -x, "Passed!" );
  113. assert.ok( c.y == -y, "Passed!" );
  114. assert.ok( c.z == -z, "Passed!" );
  115. c.copy( a ).max( b );
  116. assert.ok( c.x == x, "Passed!" );
  117. assert.ok( c.y == y, "Passed!" );
  118. assert.ok( c.z == z, "Passed!" );
  119. c.set( -2*x, 2*y, -2*z );
  120. c.clamp( b, a );
  121. assert.ok( c.x == -x, "Passed!" );
  122. assert.ok( c.y == y, "Passed!" );
  123. assert.ok( c.z == -z, "Passed!" );
  124. });
  125. QUnit.test( "negate" , function( assert ) {
  126. var a = new THREE.Vector3( x, y, z );
  127. a.negate();
  128. assert.ok( a.x == -x, "Passed!" );
  129. assert.ok( a.y == -y, "Passed!" );
  130. assert.ok( a.z == -z, "Passed!" );
  131. });
  132. QUnit.test( "dot" , function( assert ) {
  133. var a = new THREE.Vector3( x, y, z );
  134. var b = new THREE.Vector3( -x, -y, -z );
  135. var c = new THREE.Vector3();
  136. var result = a.dot( b );
  137. assert.ok( result == (-x*x-y*y-z*z), "Passed!" );
  138. result = a.dot( c );
  139. assert.ok( result == 0, "Passed!" );
  140. });
  141. QUnit.test( "length/lengthSq", function( assert ) {
  142. var a = new THREE.Vector3( x, 0, 0 );
  143. var b = new THREE.Vector3( 0, -y, 0 );
  144. var c = new THREE.Vector3( 0, 0, z );
  145. var d = new THREE.Vector3();
  146. assert.ok( a.length() == x, "Passed!" );
  147. assert.ok( a.lengthSq() == x*x, "Passed!" );
  148. assert.ok( b.length() == y, "Passed!" );
  149. assert.ok( b.lengthSq() == y*y, "Passed!" );
  150. assert.ok( c.length() == z, "Passed!" );
  151. assert.ok( c.lengthSq() == z*z, "Passed!" );
  152. assert.ok( d.length() == 0, "Passed!" );
  153. assert.ok( d.lengthSq() == 0, "Passed!" );
  154. a.set( x, y, z );
  155. assert.ok( a.length() == Math.sqrt( x*x + y*y + z*z ), "Passed!" );
  156. assert.ok( a.lengthSq() == ( x*x + y*y + z*z ), "Passed!" );
  157. });
  158. QUnit.test( "normalize" , function( assert ) {
  159. var a = new THREE.Vector3( x, 0, 0 );
  160. var b = new THREE.Vector3( 0, -y, 0 );
  161. var c = new THREE.Vector3( 0, 0, z );
  162. a.normalize();
  163. assert.ok( a.length() == 1, "Passed!" );
  164. assert.ok( a.x == 1, "Passed!" );
  165. b.normalize();
  166. assert.ok( b.length() == 1, "Passed!" );
  167. assert.ok( b.y == -1, "Passed!" );
  168. c.normalize();
  169. assert.ok( c.length() == 1, "Passed!" );
  170. assert.ok( c.z == 1, "Passed!" );
  171. });
  172. QUnit.test( "distanceTo/distanceToSquared", function( assert ) {
  173. var a = new THREE.Vector3( x, 0, 0 );
  174. var b = new THREE.Vector3( 0, -y, 0 );
  175. var c = new THREE.Vector3( 0, 0, z );
  176. var d = new THREE.Vector3();
  177. assert.ok( a.distanceTo( d ) == x, "Passed!" );
  178. assert.ok( a.distanceToSquared( d ) == x*x, "Passed!" );
  179. assert.ok( b.distanceTo( d ) == y, "Passed!" );
  180. assert.ok( b.distanceToSquared( d ) == y*y, "Passed!" );
  181. assert.ok( c.distanceTo( d ) == z, "Passed!" );
  182. assert.ok( c.distanceToSquared( d ) == z*z, "Passed!" );
  183. });
  184. QUnit.test( "setLength" , function( assert ) {
  185. var a = new THREE.Vector3( x, 0, 0 );
  186. assert.ok( a.length() == x, "Passed!" );
  187. a.setLength( y );
  188. assert.ok( a.length() == y, "Passed!" );
  189. a = new THREE.Vector3( 0, 0, 0 );
  190. assert.ok( a.length() == 0, "Passed!" );
  191. a.setLength( y );
  192. assert.ok( a.length() == 0, "Passed!" );
  193. a.setLength();
  194. assert.ok( isNaN( a.length() ), "Passed!" );
  195. });
  196. QUnit.test( "projectOnVector" , function( assert ) {
  197. var a = new THREE.Vector3( 1, 0, 0 );
  198. var b = new THREE.Vector3();
  199. var normal = new THREE.Vector3( 10, 0, 0 );
  200. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new THREE.Vector3( 1, 0, 0 ) ), "Passed!" );
  201. a.set( 0, 1, 0 );
  202. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  203. a.set( 0, 0, -1 );
  204. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  205. a.set( -1, 0, 0 );
  206. assert.ok( b.copy( a ).projectOnVector( normal ).equals( new THREE.Vector3( -1, 0, 0 ) ), "Passed!" );
  207. });
  208. QUnit.test( "projectOnPlane" , function( assert ) {
  209. var a = new THREE.Vector3( 1, 0, 0 );
  210. var b = new THREE.Vector3();
  211. var normal = new THREE.Vector3( 1, 0, 0 );
  212. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  213. a.set( 0, 1, 0 );
  214. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  215. a.set( 0, 0, -1 );
  216. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new THREE.Vector3( 0, 0, -1 ) ), "Passed!" );
  217. a.set( -1, 0, 0 );
  218. assert.ok( b.copy( a ).projectOnPlane( normal ).equals( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  219. });
  220. QUnit.test( "reflect" , function( assert ) {
  221. var a = new THREE.Vector3();
  222. var normal = new THREE.Vector3( 0, 1, 0 );
  223. var b = new THREE.Vector3();
  224. a.set( 0, -1, 0 );
  225. assert.ok( b.copy( a ).reflect( normal ).equals( new THREE.Vector3( 0, 1, 0 ) ), "Passed!" );
  226. a.set( 1, -1, 0 );
  227. assert.ok( b.copy( a ).reflect( normal ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  228. a.set( 1, -1, 0 );
  229. normal.set( 0, -1, 0 );
  230. assert.ok( b.copy( a ).reflect( normal ).equals( new THREE.Vector3( 1, 1, 0 ) ), "Passed!" );
  231. });
  232. QUnit.test( "angleTo" , function( assert ) {
  233. var a = new THREE.Vector3( 0, -0.18851655680720186, 0.9820700116639124 );
  234. var b = new THREE.Vector3( 0, 0.18851655680720186, -0.9820700116639124 );
  235. assert.equal( a.angleTo( a ), 0 );
  236. assert.equal( a.angleTo( b ), Math.PI );
  237. var x = new THREE.Vector3( 1, 0, 0 );
  238. var y = new THREE.Vector3( 0, 1, 0 );
  239. var z = new THREE.Vector3( 0, 0, 1 );
  240. assert.equal( x.angleTo( y ), Math.PI / 2 );
  241. assert.equal( x.angleTo( z ), Math.PI / 2 );
  242. assert.equal( z.angleTo( x ), Math.PI / 2 );
  243. assert.ok( Math.abs( x.angleTo( new THREE.Vector3( 1, 1, 0 ) ) - ( Math.PI / 4 ) ) < 0.0000001 );
  244. });
  245. QUnit.test( "lerp/clone", function( assert ) {
  246. var a = new THREE.Vector3( x, 0, z );
  247. var b = new THREE.Vector3( 0, -y, 0 );
  248. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 0.5 ) ), "Passed!" );
  249. assert.ok( a.lerp( a, 0 ).equals( a.lerp( a, 1 ) ), "Passed!" );
  250. assert.ok( a.clone().lerp( b, 0 ).equals( a ), "Passed!" );
  251. assert.ok( a.clone().lerp( b, 0.5 ).x == x*0.5, "Passed!" );
  252. assert.ok( a.clone().lerp( b, 0.5 ).y == -y*0.5, "Passed!" );
  253. assert.ok( a.clone().lerp( b, 0.5 ).z == z*0.5, "Passed!" );
  254. assert.ok( a.clone().lerp( b, 1 ).equals( b ), "Passed!" );
  255. });
  256. QUnit.test( "equals" , function( assert ) {
  257. var a = new THREE.Vector3( x, 0, z );
  258. var b = new THREE.Vector3( 0, -y, 0 );
  259. assert.ok( a.x != b.x, "Passed!" );
  260. assert.ok( a.y != b.y, "Passed!" );
  261. assert.ok( a.z != b.z, "Passed!" );
  262. assert.ok( ! a.equals( b ), "Passed!" );
  263. assert.ok( ! b.equals( a ), "Passed!" );
  264. a.copy( b );
  265. assert.ok( a.x == b.x, "Passed!" );
  266. assert.ok( a.y == b.y, "Passed!" );
  267. assert.ok( a.z == b.z, "Passed!" );
  268. assert.ok( a.equals( b ), "Passed!" );
  269. assert.ok( b.equals( a ), "Passed!" );
  270. });
  271. QUnit.test( "applyMatrix4" , function( assert ) {
  272. var a = new THREE.Vector3( x, y, z );
  273. var b = new THREE.Vector4( x, y, z, 1 );
  274. var m = new THREE.Matrix4().makeRotationX( Math.PI );
  275. a.applyMatrix4( m );
  276. b.applyMatrix4( m );
  277. assert.ok( a.x == b.x / b.w, "Passed!" );
  278. assert.ok( a.y == b.y / b.w, "Passed!" );
  279. assert.ok( a.z == b.z / b.w, "Passed!" );
  280. m = new THREE.Matrix4().makeTranslation( 3, 2, 1 );
  281. a.applyMatrix4( m );
  282. b.applyMatrix4( m );
  283. assert.ok( a.x == b.x / b.w, "Passed!" );
  284. assert.ok( a.y == b.y / b.w, "Passed!" );
  285. assert.ok( a.z == b.z / b.w, "Passed!" );
  286. m = new THREE.Matrix4().set(
  287. 1, 0, 0, 0,
  288. 0, 1, 0, 0,
  289. 0, 0, 1, 0,
  290. 0, 0, 1, 0
  291. );
  292. a.applyMatrix4( m );
  293. b.applyMatrix4( m );
  294. assert.ok( a.x == b.x / b.w, "Passed!" );
  295. assert.ok( a.y == b.y / b.w, "Passed!" );
  296. assert.ok( a.z == b.z / b.w, "Passed!" );
  297. });
  298. QUnit.test( "setComponent/getComponent exceptions", function ( assert ) {
  299. var a = new THREE.Vector3();
  300. assert.throws(
  301. function () {
  302. a.setComponent( 3, 0 );
  303. },
  304. /index is out of range/,
  305. "setComponent with an out of range index throws Error"
  306. );
  307. assert.throws(
  308. function () {
  309. a.getComponent( 3 );
  310. },
  311. /index is out of range/,
  312. "getComponent with an out of range index throws Error"
  313. );
  314. } );
  315. QUnit.test( "lengthManhattan", function ( assert ) {
  316. var a = new THREE.Vector3( x, 0, 0 );
  317. var b = new THREE.Vector3( 0, - y, 0 );
  318. var c = new THREE.Vector3( 0, 0, z );
  319. var d = new THREE.Vector3();
  320. assert.ok( a.lengthManhattan() == x, "Positive x" );
  321. assert.ok( b.lengthManhattan() == y, "Negative y" );
  322. assert.ok( c.lengthManhattan() == z, "Positive z" );
  323. assert.ok( d.lengthManhattan() == 0, "Empty initialization" );
  324. a.set( x, y, z );
  325. assert.ok( a.lengthManhattan() == Math.abs( x ) + Math.abs( y ) + Math.abs( z ), "All components" );
  326. } );
  327. QUnit.test( "setScalar/addScalar/subScalar", function ( assert ) {
  328. var a = new THREE.Vector3();
  329. var s = 3;
  330. a.setScalar( s );
  331. assert.strictEqual( a.x, s, "setScalar: check x" );
  332. assert.strictEqual( a.y, s, "setScalar: check y" );
  333. assert.strictEqual( a.z, s, "setScalar: check z" );
  334. a.addScalar( s );
  335. assert.strictEqual( a.x, 2 * s, "addScalar: check x" );
  336. assert.strictEqual( a.y, 2 * s, "addScalar: check y" );
  337. assert.strictEqual( a.z, 2 * s, "addScalar: check z" );
  338. a.subScalar( 2 * s );
  339. assert.strictEqual( a.x, 0, "subScalar: check x" );
  340. assert.strictEqual( a.y, 0, "subScalar: check y" );
  341. assert.strictEqual( a.z, 0, "subScalar: check z" );
  342. } );
  343. QUnit.test( "addScaledVector", function ( assert ) {
  344. var a = new THREE.Vector3( x, y, z );
  345. var b = new THREE.Vector3( 2, 3, 4 );
  346. var s = 3;
  347. a.addScaledVector( b, s );
  348. assert.strictEqual( a.x, x + b.x * s, "Check x" );
  349. assert.strictEqual( a.y, y + b.y * s, "Check y" );
  350. assert.strictEqual( a.z, z + b.z * s, "Check z" );
  351. } );
  352. QUnit.test( "multiply/divide", function ( assert ) {
  353. var a = new THREE.Vector3( x, y, z );
  354. var b = new THREE.Vector3( 2 * x, 2 * y, 2 * z );
  355. var c = new THREE.Vector3( 4 * x, 4 * y, 4 * z );
  356. a.multiply( b );
  357. assert.strictEqual( a.x, x * b.x, "multiply: check x" );
  358. assert.strictEqual( a.y, y * b.y, "multiply: check y" );
  359. assert.strictEqual( a.z, z * b.z, "multiply: check z" );
  360. b.divide( c );
  361. assert.ok( Math.abs( b.x - 0.5 ) <= eps, "divide: check z" );
  362. assert.ok( Math.abs( b.y - 0.5 ) <= eps, "divide: check z" );
  363. assert.ok( Math.abs( b.z - 0.5 ) <= eps, "divide: check z" );
  364. } );
  365. QUnit.test( "multiplyVectors", function ( assert ) {
  366. var a = new THREE.Vector3( x, y, z );
  367. var b = new THREE.Vector3( 2, 3, - 5 );
  368. var c = new THREE.Vector3().multiplyVectors( a, b );
  369. assert.strictEqual( c.x, x * 2, "Check x" );
  370. assert.strictEqual( c.y, y * 3, "Check y" );
  371. assert.strictEqual( c.z, z * - 5, "Check z" );
  372. } );
  373. QUnit.test( "applyMatrix3", function ( assert ) {
  374. var a = new THREE.Vector3( x, y, z );
  375. var m = new THREE.Matrix3().set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  376. a.applyMatrix3( m );
  377. assert.strictEqual( a.x, 33, "Check x" );
  378. assert.strictEqual( a.y, 99, "Check y" );
  379. assert.strictEqual( a.z, 183, "Check z" );
  380. } );
  381. QUnit.test( "applyQuaternion", function ( assert ) {
  382. var a = new THREE.Vector3( x, y, z );
  383. a.applyQuaternion( new THREE.Quaternion() );
  384. assert.strictEqual( a.x, x, "Identity rotation: check x" );
  385. assert.strictEqual( a.y, y, "Identity rotation: check y" );
  386. assert.strictEqual( a.z, z, "Identity rotation: check z" );
  387. a.applyQuaternion( new THREE.Quaternion( x, y, z, w ) );
  388. assert.strictEqual( a.x, 108, "Normal rotation: check x" );
  389. assert.strictEqual( a.y, 162, "Normal rotation: check y" );
  390. assert.strictEqual( a.z, 216, "Normal rotation: check z" );
  391. } );
  392. QUnit.test( "project/unproject", function ( assert ) {
  393. var a = new THREE.Vector3( x, y, z );
  394. var camera = new THREE.PerspectiveCamera( 75, 16 / 9, 0.1, 300.0 );
  395. var projected = new THREE.Vector3( - 0.36653213611158914, - 0.9774190296309043, 1.0506835611870624 );
  396. a.project( camera );
  397. assert.ok( Math.abs( a.x - projected.x ) <= eps, "project: check x" );
  398. assert.ok( Math.abs( a.y - projected.y ) <= eps, "project: check y" );
  399. assert.ok( Math.abs( a.z - projected.z ) <= eps, "project: check z" );
  400. a.unproject( camera );
  401. assert.ok( Math.abs( a.x - x ) <= eps, "unproject: check x" );
  402. assert.ok( Math.abs( a.y - y ) <= eps, "unproject: check y" );
  403. assert.ok( Math.abs( a.z - z ) <= eps, "unproject: check z" );
  404. } );
  405. QUnit.test( "transformDirection", function ( assert ) {
  406. var a = new THREE.Vector3( x, y, z );
  407. var m = new THREE.Matrix4();
  408. var transformed = new THREE.Vector3( 0.3713906763541037, 0.5570860145311556, 0.7427813527082074 );
  409. a.transformDirection( m );
  410. assert.ok( Math.abs( a.x - transformed.x ) <= eps, "Check x" );
  411. assert.ok( Math.abs( a.y - transformed.y ) <= eps, "Check y" );
  412. assert.ok( Math.abs( a.z - transformed.z ) <= eps, "Check z" );
  413. } );
  414. QUnit.test( "applyEuler", function ( assert ) {
  415. var a = new THREE.Vector3( x, y, z );
  416. var euler = new THREE.Euler( 90, - 45, 0 );
  417. var expected = new THREE.Vector3( - 2.352970120501014, - 4.7441750936226645, 0.9779234597246458 );
  418. a.applyEuler( euler );
  419. assert.ok( Math.abs( a.x - expected.x ) <= eps, "Check x" );
  420. assert.ok( Math.abs( a.y - expected.y ) <= eps, "Check y" );
  421. assert.ok( Math.abs( a.z - expected.z ) <= eps, "Check z" );
  422. } );
  423. QUnit.test( "applyAxisAngle", function ( assert ) {
  424. var a = new THREE.Vector3( x, y, z );
  425. var axis = new THREE.Vector3( 0, 1, 0 );
  426. var angle = Math.PI / 4.0;
  427. var expected = new THREE.Vector3( 3 * Math.sqrt( 2 ), 3, Math.sqrt( 2 ) );
  428. a.applyAxisAngle( axis, angle );
  429. assert.ok( Math.abs( a.x - expected.x ) <= eps, "Check x" );
  430. assert.ok( Math.abs( a.y - expected.y ) <= eps, "Check y" );
  431. assert.ok( Math.abs( a.z - expected.z ) <= eps, "Check z" );
  432. } );
  433. QUnit.test( "clampScalar", function ( assert ) {
  434. var a = new THREE.Vector3( - 0.01, 0.5, 1.5 );
  435. var clamped = new THREE.Vector3( 0.1, 0.5, 1.0 );
  436. a.clampScalar( 0.1, 1.0 );
  437. assert.ok( Math.abs( a.x - clamped.x ) <= 0.001, "Check x" );
  438. assert.ok( Math.abs( a.y - clamped.y ) <= 0.001, "Check y" );
  439. assert.ok( Math.abs( a.z - clamped.z ) <= 0.001, "Check z" );
  440. } );
  441. QUnit.test( "cross", function ( assert ) {
  442. var a = new THREE.Vector3( x, y, z );
  443. var b = new THREE.Vector3( 2 * x, - y, 0.5 * z );
  444. var crossed = new THREE.Vector3( 18, 12, - 18 );
  445. a.cross( b );
  446. assert.ok( Math.abs( a.x - crossed.x ) <= eps, "Check x" );
  447. assert.ok( Math.abs( a.y - crossed.y ) <= eps, "Check y" );
  448. assert.ok( Math.abs( a.z - crossed.z ) <= eps, "Check z" );
  449. } );
  450. QUnit.test( "crossVectors", function ( assert ) {
  451. var a = new THREE.Vector3( x, y, z );
  452. var b = new THREE.Vector3( x, - y, z );
  453. var c = new THREE.Vector3();
  454. var crossed = new THREE.Vector3( 24, 0, - 12 );
  455. c.crossVectors( a, b );
  456. assert.ok( Math.abs( c.x - crossed.x ) <= eps, "Check x" );
  457. assert.ok( Math.abs( c.y - crossed.y ) <= eps, "Check y" );
  458. assert.ok( Math.abs( c.z - crossed.z ) <= eps, "Check z" );
  459. } );
  460. QUnit.test( "setFromSpherical", function ( assert ) {
  461. var a = new THREE.Vector3();
  462. var phi = Math.acos( - 0.5 );
  463. var theta = Math.sqrt( Math.PI ) * phi;
  464. var sph = new THREE.Spherical( 10, phi, theta );
  465. var expected = new THREE.Vector3( - 4.677914006701843, - 5, - 7.288149322420796 );
  466. a.setFromSpherical( sph );
  467. assert.ok( Math.abs( a.x - expected.x ) <= eps, "Check x" );
  468. assert.ok( Math.abs( a.y - expected.y ) <= eps, "Check y" );
  469. assert.ok( Math.abs( a.z - expected.z ) <= eps, "Check z" );
  470. } );
  471. QUnit.test( "setFromCylindrical", function ( assert ) {
  472. var a = new THREE.Vector3();
  473. var cyl = new THREE.Cylindrical( 10, Math.PI * 0.125, 20 );
  474. var expected = new THREE.Vector3( 3.826834323650898, 20, 9.238795325112868 );
  475. a.setFromCylindrical( cyl );
  476. assert.ok( Math.abs( a.x - expected.x ) <= eps, "Check x" );
  477. assert.ok( Math.abs( a.y - expected.y ) <= eps, "Check y" );
  478. assert.ok( Math.abs( a.z - expected.z ) <= eps, "Check z" );
  479. } );
  480. QUnit.test( "setFromMatrixPosition", function ( assert ) {
  481. var a = new THREE.Vector3();
  482. var m = new THREE.Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  483. a.setFromMatrixPosition( m );
  484. assert.strictEqual( a.x, 7, "Check x" );
  485. assert.strictEqual( a.y, 19, "Check y" );
  486. assert.strictEqual( a.z, 37, "Check z" );
  487. } );
  488. QUnit.test( "setFromMatrixScale", function ( assert ) {
  489. var a = new THREE.Vector3();
  490. var m = new THREE.Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  491. var expected = new THREE.Vector3( 25.573423705088842, 31.921779399024736, 35.70714214271425 );
  492. a.setFromMatrixScale( m );
  493. assert.ok( Math.abs( a.x - expected.x ) <= eps, "Check x" );
  494. assert.ok( Math.abs( a.y - expected.y ) <= eps, "Check y" );
  495. assert.ok( Math.abs( a.z - expected.z ) <= eps, "Check z" );
  496. } );
  497. QUnit.test( "setFromMatrixColumn", function ( assert ) {
  498. var a = new THREE.Vector3();
  499. var m = new THREE.Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  500. a.setFromMatrixColumn( m, 0 );
  501. assert.strictEqual( a.x, 2, "Index 0: check x" );
  502. assert.strictEqual( a.y, 11, "Index 0: check y" );
  503. assert.strictEqual( a.z, 23, "Index 0: check z" );
  504. a.setFromMatrixColumn( m, 2 );
  505. assert.strictEqual( a.x, 5, "Index 2: check x" );
  506. assert.strictEqual( a.y, 17, "Index 2: check y" );
  507. assert.strictEqual( a.z, 31, "Index 2: check z" );
  508. } );
  509. QUnit.test( "fromArray", function ( assert ) {
  510. var a = new THREE.Vector3();
  511. var array = [ 1, 2, 3, 4, 5, 6 ];
  512. a.fromArray( array );
  513. assert.strictEqual( a.x, 1, "No offset: check x" );
  514. assert.strictEqual( a.y, 2, "No offset: check y" );
  515. assert.strictEqual( a.z, 3, "No offset: check z" );
  516. a.fromArray( array, 3 );
  517. assert.strictEqual( a.x, 4, "With offset: check x" );
  518. assert.strictEqual( a.y, 5, "With offset: check y" );
  519. assert.strictEqual( a.z, 6, "With offset: check z" );
  520. } );
  521. QUnit.test( "toArray", function ( assert ) {
  522. var a = new THREE.Vector3( x, y, z );
  523. var array = a.toArray();
  524. assert.strictEqual( array[ 0 ], x, "No array, no offset: check x" );
  525. assert.strictEqual( array[ 1 ], y, "No array, no offset: check y" );
  526. assert.strictEqual( array[ 2 ], z, "No array, no offset: check z" );
  527. array = [];
  528. a.toArray( array );
  529. assert.strictEqual( array[ 0 ], x, "With array, no offset: check x" );
  530. assert.strictEqual( array[ 1 ], y, "With array, no offset: check y" );
  531. assert.strictEqual( array[ 2 ], z, "With array, no offset: check z" );
  532. array = [];
  533. a.toArray( array, 1 );
  534. assert.strictEqual( array[ 0 ], undefined, "With array and offset: check [0]" );
  535. assert.strictEqual( array[ 1 ], x, "With array and offset: check x" );
  536. assert.strictEqual( array[ 2 ], y, "With array and offset: check y" );
  537. assert.strictEqual( array[ 3 ], z, "With array and offset: check z" );
  538. } );
  539. QUnit.test( "fromBufferAttribute", function ( assert ) {
  540. var a = new THREE.Vector3();
  541. var attr = new THREE.BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
  542. a.fromBufferAttribute( attr, 0 );
  543. assert.strictEqual( a.x, 1, "Offset 0: check x" );
  544. assert.strictEqual( a.y, 2, "Offset 0: check y" );
  545. assert.strictEqual( a.z, 3, "Offset 0: check z" );
  546. a.fromBufferAttribute( attr, 1 );
  547. assert.strictEqual( a.x, 4, "Offset 1: check x" );
  548. assert.strictEqual( a.y, 5, "Offset 1: check y" );
  549. assert.strictEqual( a.z, 6, "Offset 1: check z" );
  550. } );
粤ICP备19079148号