Triangle3.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.Triangle3 = function ( a, b, c ) {
  6. this.a = new THREE.Vector3();
  7. this.b = new THREE.Vector3();
  8. this.c = new THREE.Vector3();
  9. if( a !== undefined && b !== undefined && c !== undefined ) {
  10. this.a.copy( a );
  11. this.b.copy( b );
  12. this.c.copy( c );
  13. }
  14. };
  15. // static/instance method to calculate barycoordinates
  16. THREE.Triangle3.barycoordFromPoint = function ( point, a, b, c, optionalTarget ) {
  17. THREE.Triangle3.__v0.sub( c, a );
  18. THREE.Triangle3.__v1.sub( b, a );
  19. THREE.Triangle3.__v2.sub( point, a );
  20. var dot00 = THREE.Triangle3.__v0.dot( THREE.Triangle3.__v0 );
  21. var dot01 = THREE.Triangle3.__v0.dot( THREE.Triangle3.__v1 );
  22. var dot02 = THREE.Triangle3.__v0.dot( THREE.Triangle3.__v2 );
  23. var dot11 = THREE.Triangle3.__v1.dot( THREE.Triangle3.__v1 );
  24. var dot12 = THREE.Triangle3.__v1.dot( THREE.Triangle3.__v2 );
  25. var denom = ( dot00 * dot11 - dot01 * dot01 );
  26. var result = optionalTarget || new THREE.Vector3();
  27. // colinear or singular triangle
  28. if( denom == 0 ) {
  29. // arbitrary location outside of triangle?
  30. // not sure if this is the best idea, maybe should be returning undefined
  31. return result.set( -2, -1, -1 );
  32. }
  33. var invDenom = 1 / denom;
  34. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  35. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  36. // barycoordinates must always sum to 1
  37. return result.set( 1 - u - v, v, u );
  38. };
  39. THREE.Triangle3.containsPoint = function ( point, a, b, c ) {
  40. // NOTE: need to use __v3 here because __v0, __v1 and __v2 are used in barycoordFromPoint.
  41. var result = THREE.Triangle3.barycoordFromPoint( point, a, b, c, THREE.Triangle3.__v3 );
  42. return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
  43. };
  44. THREE.Triangle3.prototype = {
  45. constructor: THREE.Triangle3,
  46. set: function ( a, b, c ) {
  47. this.a.copy( a );
  48. this.b.copy( b );
  49. this.c.copy( c );
  50. return this;
  51. },
  52. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  53. this.a.copy( points[i0] );
  54. this.b.copy( points[i1] );
  55. this.c.copy( points[i2] );
  56. return this;
  57. },
  58. copy: function ( triangle ) {
  59. this.a.copy( triangle.a );
  60. this.b.copy( triangle.b );
  61. this.c.copy( triangle.c );
  62. return this;
  63. },
  64. area: function () {
  65. THREE.Triangle3.__v0.sub( this.c, this.b );
  66. THREE.Triangle3.__v1.sub( this.a, this.b );
  67. return THREE.Triangle3.__v0.crossSelf( THREE.Triangle3.__v1 ).length() * 0.5;
  68. },
  69. midpoint: function ( optionalTarget ) {
  70. var result = optionalTarget || new THREE.Vector3();
  71. return result.add( this.a, this.b ).addSelf( this.c ).multiplyScalar( 1 / 3 );
  72. },
  73. normal: function ( optionalTarget ) {
  74. var result = optionalTarget || new THREE.Vector3();
  75. result.sub( this.c, this.b );
  76. THREE.Triangle3.__v0.sub( this.a, this.b );
  77. result.crossSelf( THREE.Triangle3.__v0 );
  78. var resultLengthSq = result.lengthSq();
  79. if( resultLengthSq > 0 ) {
  80. return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
  81. }
  82. return result.set( 0, 0, 0 );
  83. },
  84. plane: function ( optionalTarget ) {
  85. var result = optionalTarget || new THREE.Plane();
  86. return result.setFromCoplanarPoints( this.a, this.b, this.c );
  87. },
  88. barycoordFromPoint: function ( point, optionalTarget ) {
  89. return THREE.Triangle3.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
  90. },
  91. containsPoint: function ( point ) {
  92. return THREE.Triangle3.containsPoint( point, this.a, this.b, this.c );
  93. },
  94. equals: function ( triangle ) {
  95. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  96. },
  97. clone: function () {
  98. return new THREE.Triangle3().copy( this );
  99. }
  100. };
  101. THREE.Triangle3.__v0 = new THREE.Vector3();
  102. THREE.Triangle3.__v1 = new THREE.Vector3();
  103. THREE.Triangle3.__v2 = new THREE.Vector3();
  104. THREE.Triangle3.__v3 = new THREE.Vector3();