Triangle.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import { Vector3 } from './Vector3.js';
  2. import { Plane } from './Plane.js';
  3. /**
  4. * @author bhouston / http://clara.io
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. const _v0 = new Vector3();
  8. const _v1 = new Vector3();
  9. const _v2 = new Vector3();
  10. const _v3 = new Vector3();
  11. const _vab = new Vector3();
  12. const _vac = new Vector3();
  13. const _vbc = new Vector3();
  14. const _vap = new Vector3();
  15. const _vbp = new Vector3();
  16. const _vcp = new Vector3();
  17. function Triangle( a, b, c ) {
  18. this.a = ( a !== undefined ) ? a : new Vector3();
  19. this.b = ( b !== undefined ) ? b : new Vector3();
  20. this.c = ( c !== undefined ) ? c : new Vector3();
  21. }
  22. Object.assign( Triangle, {
  23. getNormal: function ( a, b, c, target ) {
  24. if ( target === undefined ) {
  25. console.warn( 'THREE.Triangle: .getNormal() target is now required' );
  26. target = new Vector3();
  27. }
  28. target.subVectors( c, b );
  29. _v0.subVectors( a, b );
  30. target.cross( _v0 );
  31. const targetLengthSq = target.lengthSq();
  32. if ( targetLengthSq > 0 ) {
  33. return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
  34. }
  35. return target.set( 0, 0, 0 );
  36. },
  37. // static/instance method to calculate barycentric coordinates
  38. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  39. getBarycoord: function ( point, a, b, c, target ) {
  40. _v0.subVectors( c, a );
  41. _v1.subVectors( b, a );
  42. _v2.subVectors( point, a );
  43. const dot00 = _v0.dot( _v0 );
  44. const dot01 = _v0.dot( _v1 );
  45. const dot02 = _v0.dot( _v2 );
  46. const dot11 = _v1.dot( _v1 );
  47. const dot12 = _v1.dot( _v2 );
  48. const denom = ( dot00 * dot11 - dot01 * dot01 );
  49. if ( target === undefined ) {
  50. console.warn( 'THREE.Triangle: .getBarycoord() target is now required' );
  51. target = new Vector3();
  52. }
  53. // collinear or singular triangle
  54. if ( denom === 0 ) {
  55. // arbitrary location outside of triangle?
  56. // not sure if this is the best idea, maybe should be returning undefined
  57. return target.set( - 2, - 1, - 1 );
  58. }
  59. const invDenom = 1 / denom;
  60. const u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  61. const v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  62. // barycentric coordinates must always sum to 1
  63. return target.set( 1 - u - v, v, u );
  64. },
  65. containsPoint: function ( point, a, b, c ) {
  66. Triangle.getBarycoord( point, a, b, c, _v3 );
  67. return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 );
  68. },
  69. getUV: function ( point, p1, p2, p3, uv1, uv2, uv3, target ) {
  70. this.getBarycoord( point, p1, p2, p3, _v3 );
  71. target.set( 0, 0 );
  72. target.addScaledVector( uv1, _v3.x );
  73. target.addScaledVector( uv2, _v3.y );
  74. target.addScaledVector( uv3, _v3.z );
  75. return target;
  76. },
  77. isFrontFacing: function ( a, b, c, direction ) {
  78. _v0.subVectors( c, b );
  79. _v1.subVectors( a, b );
  80. // strictly front facing
  81. return ( _v0.cross( _v1 ).dot( direction ) < 0 ) ? true : false;
  82. }
  83. } );
  84. Object.assign( Triangle.prototype, {
  85. set: function ( a, b, c ) {
  86. this.a.copy( a );
  87. this.b.copy( b );
  88. this.c.copy( c );
  89. return this;
  90. },
  91. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  92. this.a.copy( points[ i0 ] );
  93. this.b.copy( points[ i1 ] );
  94. this.c.copy( points[ i2 ] );
  95. return this;
  96. },
  97. clone: function () {
  98. return new this.constructor().copy( this );
  99. },
  100. copy: function ( triangle ) {
  101. this.a.copy( triangle.a );
  102. this.b.copy( triangle.b );
  103. this.c.copy( triangle.c );
  104. return this;
  105. },
  106. getArea: function () {
  107. _v0.subVectors( this.c, this.b );
  108. _v1.subVectors( this.a, this.b );
  109. return _v0.cross( _v1 ).length() * 0.5;
  110. },
  111. getMidpoint: function ( target ) {
  112. if ( target === undefined ) {
  113. console.warn( 'THREE.Triangle: .getMidpoint() target is now required' );
  114. target = new Vector3();
  115. }
  116. return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  117. },
  118. getNormal: function ( target ) {
  119. return Triangle.getNormal( this.a, this.b, this.c, target );
  120. },
  121. getPlane: function ( target ) {
  122. if ( target === undefined ) {
  123. console.warn( 'THREE.Triangle: .getPlane() target is now required' );
  124. target = new Plane();
  125. }
  126. return target.setFromCoplanarPoints( this.a, this.b, this.c );
  127. },
  128. getBarycoord: function ( point, target ) {
  129. return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
  130. },
  131. getUV: function ( point, uv1, uv2, uv3, target ) {
  132. return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, target );
  133. },
  134. containsPoint: function ( point ) {
  135. return Triangle.containsPoint( point, this.a, this.b, this.c );
  136. },
  137. isFrontFacing: function ( direction ) {
  138. return Triangle.isFrontFacing( this.a, this.b, this.c, direction );
  139. },
  140. intersectsBox: function ( box ) {
  141. return box.intersectsTriangle( this );
  142. },
  143. closestPointToPoint: function ( p, target ) {
  144. if ( target === undefined ) {
  145. console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' );
  146. target = new Vector3();
  147. }
  148. const a = this.a, b = this.b, c = this.c;
  149. let v, w;
  150. // algorithm thanks to Real-Time Collision Detection by Christer Ericson,
  151. // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
  152. // under the accompanying license; see chapter 5.1.5 for detailed explanation.
  153. // basically, we're distinguishing which of the voronoi regions of the triangle
  154. // the point lies in with the minimum amount of redundant computation.
  155. _vab.subVectors( b, a );
  156. _vac.subVectors( c, a );
  157. _vap.subVectors( p, a );
  158. const d1 = _vab.dot( _vap );
  159. const d2 = _vac.dot( _vap );
  160. if ( d1 <= 0 && d2 <= 0 ) {
  161. // vertex region of A; barycentric coords (1, 0, 0)
  162. return target.copy( a );
  163. }
  164. _vbp.subVectors( p, b );
  165. const d3 = _vab.dot( _vbp );
  166. const d4 = _vac.dot( _vbp );
  167. if ( d3 >= 0 && d4 <= d3 ) {
  168. // vertex region of B; barycentric coords (0, 1, 0)
  169. return target.copy( b );
  170. }
  171. const vc = d1 * d4 - d3 * d2;
  172. if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
  173. v = d1 / ( d1 - d3 );
  174. // edge region of AB; barycentric coords (1-v, v, 0)
  175. return target.copy( a ).addScaledVector( _vab, v );
  176. }
  177. _vcp.subVectors( p, c );
  178. const d5 = _vab.dot( _vcp );
  179. const d6 = _vac.dot( _vcp );
  180. if ( d6 >= 0 && d5 <= d6 ) {
  181. // vertex region of C; barycentric coords (0, 0, 1)
  182. return target.copy( c );
  183. }
  184. const vb = d5 * d2 - d1 * d6;
  185. if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
  186. w = d2 / ( d2 - d6 );
  187. // edge region of AC; barycentric coords (1-w, 0, w)
  188. return target.copy( a ).addScaledVector( _vac, w );
  189. }
  190. const va = d3 * d6 - d5 * d4;
  191. if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
  192. _vbc.subVectors( c, b );
  193. w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
  194. // edge region of BC; barycentric coords (0, 1-w, w)
  195. return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC
  196. }
  197. // face region
  198. const denom = 1 / ( va + vb + vc );
  199. // u = va * denom
  200. v = vb * denom;
  201. w = vc * denom;
  202. return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w );
  203. },
  204. equals: function ( triangle ) {
  205. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  206. }
  207. } );
  208. export { Triangle };
粤ICP备19079148号