Triangle.tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* global QUnit */
  2. import { BufferAttribute } from '../../../../src/core/BufferAttribute.js';
  3. import { Triangle } from '../../../../src/math/Triangle.js';
  4. import { Box3 } from '../../../../src/math/Box3.js';
  5. import { Plane } from '../../../../src/math/Plane.js';
  6. import { Vector3 } from '../../../../src/math/Vector3.js';
  7. import {
  8. zero3,
  9. one3,
  10. two3
  11. } from '../../utils/math-constants.js';
  12. export default QUnit.module( 'Maths', () => {
  13. QUnit.module( 'Triangle', () => {
  14. // INSTANCING
  15. QUnit.test( 'Instancing', ( assert ) => {
  16. let a = new Triangle();
  17. assert.ok( a.a.equals( zero3 ), 'Passed!' );
  18. assert.ok( a.b.equals( zero3 ), 'Passed!' );
  19. assert.ok( a.c.equals( zero3 ), 'Passed!' );
  20. a = new Triangle( one3.clone().negate(), one3.clone(), two3.clone() );
  21. assert.ok( a.a.equals( one3.clone().negate() ), 'Passed!' );
  22. assert.ok( a.b.equals( one3 ), 'Passed!' );
  23. assert.ok( a.c.equals( two3 ), 'Passed!' );
  24. } );
  25. // STATIC
  26. QUnit.todo( 'getNormal', ( assert ) => {
  27. assert.ok( false, 'everything\'s gonna be alright' );
  28. } );
  29. QUnit.todo( 'getBarycoord', ( assert ) => {
  30. assert.ok( false, 'everything\'s gonna be alright' );
  31. } );
  32. QUnit.todo( 'containsPoint', ( assert ) => {
  33. assert.ok( false, 'everything\'s gonna be alright' );
  34. } );
  35. QUnit.todo( 'getInterpolation', ( assert ) => {
  36. // static version of class member below
  37. // getInterpolation( point, p1, p2, p3, uv1, uv2, uv3, target )
  38. assert.ok( false, 'everything\'s gonna be alright' );
  39. } );
  40. QUnit.todo( 'isFrontFacing', ( assert ) => {
  41. // static version of class member below
  42. // isFrontFacing( a, b, c, direction )
  43. assert.ok( false, 'everything\'s gonna be alright' );
  44. } );
  45. // PUBLIC
  46. QUnit.test( 'set', ( assert ) => {
  47. const a = new Triangle();
  48. a.set( one3.clone().negate(), one3, two3 );
  49. assert.ok( a.a.equals( one3.clone().negate() ), 'Passed!' );
  50. assert.ok( a.b.equals( one3 ), 'Passed!' );
  51. assert.ok( a.c.equals( two3 ), 'Passed!' );
  52. } );
  53. QUnit.test( 'setFromPointsAndIndices', ( assert ) => {
  54. const a = new Triangle();
  55. const points = [ one3, one3.clone().negate(), two3 ];
  56. a.setFromPointsAndIndices( points, 1, 0, 2 );
  57. assert.ok( a.a.equals( one3.clone().negate() ), 'Passed!' );
  58. assert.ok( a.b.equals( one3 ), 'Passed!' );
  59. assert.ok( a.c.equals( two3 ), 'Passed!' );
  60. } );
  61. QUnit.test( 'setFromAttributeAndIndices', ( assert ) => {
  62. const a = new Triangle();
  63. const attribute = new BufferAttribute( new Float32Array( [ 1, 1, 1, - 1, - 1, - 1, 2, 2, 2 ] ), 3 );
  64. a.setFromAttributeAndIndices( attribute, 1, 0, 2 );
  65. assert.ok( a.a.equals( one3.clone().negate() ), 'Passed!' );
  66. assert.ok( a.b.equals( one3 ), 'Passed!' );
  67. assert.ok( a.c.equals( two3 ), 'Passed!' );
  68. } );
  69. QUnit.todo( 'clone', ( assert ) => {
  70. assert.ok( false, 'everything\'s gonna be alright' );
  71. } );
  72. QUnit.test( 'copy', ( assert ) => {
  73. const a = new Triangle( one3.clone().negate(), one3.clone(), two3.clone() );
  74. const b = new Triangle().copy( a );
  75. assert.ok( b.a.equals( one3.clone().negate() ), 'Passed!' );
  76. assert.ok( b.b.equals( one3 ), 'Passed!' );
  77. assert.ok( b.c.equals( two3 ), 'Passed!' );
  78. // ensure that it is a true copy
  79. a.a = one3;
  80. a.b = zero3;
  81. a.c = zero3;
  82. assert.ok( b.a.equals( one3.clone().negate() ), 'Passed!' );
  83. assert.ok( b.b.equals( one3 ), 'Passed!' );
  84. assert.ok( b.c.equals( two3 ), 'Passed!' );
  85. } );
  86. QUnit.test( 'getArea', ( assert ) => {
  87. let a = new Triangle();
  88. assert.ok( a.getArea() == 0, 'Passed!' );
  89. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  90. assert.ok( a.getArea() == 0.5, 'Passed!' );
  91. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  92. assert.ok( a.getArea() == 2, 'Passed!' );
  93. // colinear triangle.
  94. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 3, 0, 0 ) );
  95. assert.ok( a.getArea() == 0, 'Passed!' );
  96. } );
  97. QUnit.test( 'getMidpoint', ( assert ) => {
  98. let a = new Triangle();
  99. const midpoint = new Vector3();
  100. assert.ok( a.getMidpoint( midpoint ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  101. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  102. assert.ok( a.getMidpoint( midpoint ).equals( new Vector3( 1 / 3, 1 / 3, 0 ) ), 'Passed!' );
  103. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  104. assert.ok( a.getMidpoint( midpoint ).equals( new Vector3( 2 / 3, 0, 2 / 3 ) ), 'Passed!' );
  105. } );
  106. QUnit.test( 'getNormal', ( assert ) => {
  107. let a = new Triangle();
  108. const normal = new Vector3();
  109. assert.ok( a.getNormal( normal ).equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  110. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  111. assert.ok( a.getNormal( normal ).equals( new Vector3( 0, 0, 1 ) ), 'Passed!' );
  112. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  113. assert.ok( a.getNormal( normal ).equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  114. } );
  115. QUnit.test( 'getPlane', ( assert ) => {
  116. let a = new Triangle();
  117. const plane = new Plane();
  118. const normal = new Vector3();
  119. a.getPlane( plane );
  120. assert.notOk( isNaN( plane.distanceToPoint( a.a ) ), 'Passed!' );
  121. assert.notOk( isNaN( plane.distanceToPoint( a.b ) ), 'Passed!' );
  122. assert.notOk( isNaN( plane.distanceToPoint( a.c ) ), 'Passed!' );
  123. assert.notPropEqual( plane.normal, {
  124. x: NaN,
  125. y: NaN,
  126. z: NaN
  127. }, 'Passed!' );
  128. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  129. a.getPlane( plane );
  130. a.getNormal( normal );
  131. assert.ok( plane.distanceToPoint( a.a ) == 0, 'Passed!' );
  132. assert.ok( plane.distanceToPoint( a.b ) == 0, 'Passed!' );
  133. assert.ok( plane.distanceToPoint( a.c ) == 0, 'Passed!' );
  134. assert.ok( plane.normal.equals( normal ), 'Passed!' );
  135. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  136. a.getPlane( plane );
  137. a.getNormal( normal );
  138. assert.ok( plane.distanceToPoint( a.a ) == 0, 'Passed!' );
  139. assert.ok( plane.distanceToPoint( a.b ) == 0, 'Passed!' );
  140. assert.ok( plane.distanceToPoint( a.c ) == 0, 'Passed!' );
  141. assert.ok( plane.normal.clone().normalize().equals( normal ), 'Passed!' );
  142. } );
  143. QUnit.test( 'getBarycoord', ( assert ) => {
  144. let a = new Triangle();
  145. const barycoord = new Vector3();
  146. const midpoint = new Vector3();
  147. assert.ok( a.getBarycoord( a.a, barycoord ) === null, 'Passed!' );
  148. assert.ok( a.getBarycoord( a.b, barycoord ) === null, 'Passed!' );
  149. assert.ok( a.getBarycoord( a.c, barycoord ) === null, 'Passed!' );
  150. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  151. a.getMidpoint( midpoint );
  152. a.getBarycoord( a.a, barycoord );
  153. assert.ok( barycoord.equals( new Vector3( 1, 0, 0 ) ), 'Passed!' );
  154. a.getBarycoord( a.b, barycoord );
  155. assert.ok( barycoord.equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  156. a.getBarycoord( a.c, barycoord );
  157. assert.ok( barycoord.equals( new Vector3( 0, 0, 1 ) ), 'Passed!' );
  158. a.getBarycoord( midpoint, barycoord );
  159. assert.ok( barycoord.distanceTo( new Vector3( 1 / 3, 1 / 3, 1 / 3 ) ) < 0.0001, 'Passed!' );
  160. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  161. a.getMidpoint( midpoint );
  162. a.getBarycoord( a.a, barycoord );
  163. assert.ok( barycoord.equals( new Vector3( 1, 0, 0 ) ), 'Passed!' );
  164. a.getBarycoord( a.b, barycoord );
  165. assert.ok( barycoord.equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  166. a.getBarycoord( a.c, barycoord );
  167. assert.ok( barycoord.equals( new Vector3( 0, 0, 1 ) ), 'Passed!' );
  168. a.getBarycoord( midpoint, barycoord );
  169. assert.ok( barycoord.distanceTo( new Vector3( 1 / 3, 1 / 3, 1 / 3 ) ) < 0.0001, 'Passed!' );
  170. } );
  171. QUnit.todo( 'getInterpolation', ( assert ) => {
  172. // class member version
  173. // getInterpolation( point, uv1, uv2, uv3, target )
  174. assert.ok( false, 'everything\'s gonna be alright' );
  175. } );
  176. QUnit.test( 'containsPoint', ( assert ) => {
  177. let a = new Triangle();
  178. const midpoint = new Vector3();
  179. assert.ok( ! a.containsPoint( a.a ), 'Passed!' );
  180. assert.ok( ! a.containsPoint( a.b ), 'Passed!' );
  181. assert.ok( ! a.containsPoint( a.c ), 'Passed!' );
  182. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  183. a.getMidpoint( midpoint );
  184. assert.ok( a.containsPoint( a.a ), 'Passed!' );
  185. assert.ok( a.containsPoint( a.b ), 'Passed!' );
  186. assert.ok( a.containsPoint( a.c ), 'Passed!' );
  187. assert.ok( a.containsPoint( midpoint ), 'Passed!' );
  188. assert.ok( ! a.containsPoint( new Vector3( - 1, - 1, - 1 ) ), 'Passed!' );
  189. a = new Triangle( new Vector3( 2, 0, 0 ), new Vector3( 0, 0, 0 ), new Vector3( 0, 0, 2 ) );
  190. a.getMidpoint( midpoint );
  191. assert.ok( a.containsPoint( a.a ), 'Passed!' );
  192. assert.ok( a.containsPoint( a.b ), 'Passed!' );
  193. assert.ok( a.containsPoint( a.c ), 'Passed!' );
  194. assert.ok( a.containsPoint( midpoint ), 'Passed!' );
  195. assert.ok( ! a.containsPoint( new Vector3( - 1, - 1, - 1 ) ), 'Passed!' );
  196. } );
  197. QUnit.test( 'intersectsBox', ( assert ) => {
  198. const a = new Box3( one3.clone(), two3.clone() );
  199. const b = new Triangle( new Vector3( 1.5, 1.5, 2.5 ), new Vector3( 2.5, 1.5, 1.5 ), new Vector3( 1.5, 2.5, 1.5 ) );
  200. const c = new Triangle( new Vector3( 1.5, 1.5, 3.5 ), new Vector3( 3.5, 1.5, 1.5 ), new Vector3( 1.5, 1.5, 1.5 ) );
  201. const d = new Triangle( new Vector3( 1.5, 1.75, 3 ), new Vector3( 3, 1.75, 1.5 ), new Vector3( 1.5, 2.5, 1.5 ) );
  202. const e = new Triangle( new Vector3( 1.5, 1.8, 3 ), new Vector3( 3, 1.8, 1.5 ), new Vector3( 1.5, 2.5, 1.5 ) );
  203. const f = new Triangle( new Vector3( 1.5, 2.5, 3 ), new Vector3( 3, 2.5, 1.5 ), new Vector3( 1.5, 2.5, 1.5 ) );
  204. assert.ok( b.intersectsBox( a ), 'Passed!' );
  205. assert.ok( c.intersectsBox( a ), 'Passed!' );
  206. assert.ok( d.intersectsBox( a ), 'Passed!' );
  207. assert.ok( ! e.intersectsBox( a ), 'Passed!' );
  208. assert.ok( ! f.intersectsBox( a ), 'Passed!' );
  209. } );
  210. QUnit.test( 'closestPointToPoint', ( assert ) => {
  211. const a = new Triangle( new Vector3( - 1, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  212. const point = new Vector3();
  213. // point lies inside the triangle
  214. a.closestPointToPoint( new Vector3( 0, 0.5, 0 ), point );
  215. assert.ok( point.equals( new Vector3( 0, 0.5, 0 ) ), 'Passed!' );
  216. // point lies on a vertex
  217. a.closestPointToPoint( a.a, point );
  218. assert.ok( point.equals( a.a ), 'Passed!' );
  219. a.closestPointToPoint( a.b, point );
  220. assert.ok( point.equals( a.b ), 'Passed!' );
  221. a.closestPointToPoint( a.c, point );
  222. assert.ok( point.equals( a.c ), 'Passed!' );
  223. // point lies on an edge
  224. a.closestPointToPoint( zero3.clone(), point );
  225. assert.ok( point.equals( zero3.clone() ), 'Passed!' );
  226. // point lies outside the triangle
  227. a.closestPointToPoint( new Vector3( - 2, 0, 0 ), point );
  228. assert.ok( point.equals( new Vector3( - 1, 0, 0 ) ), 'Passed!' );
  229. a.closestPointToPoint( new Vector3( 2, 0, 0 ), point );
  230. assert.ok( point.equals( new Vector3( 1, 0, 0 ) ), 'Passed!' );
  231. a.closestPointToPoint( new Vector3( 0, 2, 0 ), point );
  232. assert.ok( point.equals( new Vector3( 0, 1, 0 ) ), 'Passed!' );
  233. a.closestPointToPoint( new Vector3( 0, - 2, 0 ), point );
  234. assert.ok( point.equals( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  235. } );
  236. QUnit.test( 'isFrontFacing', ( assert ) => {
  237. let a = new Triangle();
  238. let dir = new Vector3();
  239. assert.ok( ! a.isFrontFacing( dir ), 'Passed!' );
  240. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ) );
  241. dir = new Vector3( 0, 0, - 1 );
  242. assert.ok( a.isFrontFacing( dir ), 'Passed!' );
  243. a = new Triangle( new Vector3( 0, 0, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 1, 0, 0 ) );
  244. assert.ok( ! a.isFrontFacing( dir ), 'Passed!' );
  245. } );
  246. QUnit.test( 'equals', ( assert ) => {
  247. const a = new Triangle(
  248. new Vector3( 1, 0, 0 ),
  249. new Vector3( 0, 1, 0 ),
  250. new Vector3( 0, 0, 1 )
  251. );
  252. const b = new Triangle(
  253. new Vector3( 0, 0, 1 ),
  254. new Vector3( 0, 1, 0 ),
  255. new Vector3( 1, 0, 0 )
  256. );
  257. const c = new Triangle(
  258. new Vector3( - 1, 0, 0 ),
  259. new Vector3( 0, 1, 0 ),
  260. new Vector3( 0, 0, 1 )
  261. );
  262. assert.ok( a.equals( a ), 'a equals a' );
  263. assert.notOk( a.equals( b ), 'a does not equal b' );
  264. assert.notOk( a.equals( c ), 'a does not equal c' );
  265. assert.notOk( b.equals( c ), 'b does not equal c' );
  266. a.copy( b );
  267. assert.ok( a.equals( a ), 'a equals b after copy()' );
  268. } );
  269. } );
  270. } );
粤ICP备19079148号