Triangle.js 4.0 KB

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