123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import { Vector3 } from './Vector3';
- import { Line3 } from './Line3';
- import { Plane } from './Plane';
- /**
- * @author bhouston / http://clara.io
- * @author mrdoob / http://mrdoob.com/
- */
- function Triangle( a, b, c ) {
- this.a = ( a !== undefined ) ? a : new Vector3();
- this.b = ( b !== undefined ) ? b : new Vector3();
- this.c = ( c !== undefined ) ? c : new Vector3();
- }
- Object.assign( Triangle, {
- normal: function () {
- var v0 = new Vector3();
- return function normal( a, b, c, optionalTarget ) {
- var result = optionalTarget || new Vector3();
- result.subVectors( c, b );
- v0.subVectors( a, b );
- result.cross( v0 );
- var resultLengthSq = result.lengthSq();
- if ( resultLengthSq > 0 ) {
- return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
- }
- return result.set( 0, 0, 0 );
- };
- }(),
- // static/instance method to calculate barycentric coordinates
- // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
- barycoordFromPoint: function () {
- var v0 = new Vector3();
- var v1 = new Vector3();
- var v2 = new Vector3();
- return function barycoordFromPoint( point, a, b, c, optionalTarget ) {
- v0.subVectors( c, a );
- v1.subVectors( b, a );
- v2.subVectors( point, a );
- var dot00 = v0.dot( v0 );
- var dot01 = v0.dot( v1 );
- var dot02 = v0.dot( v2 );
- var dot11 = v1.dot( v1 );
- var dot12 = v1.dot( v2 );
- var denom = ( dot00 * dot11 - dot01 * dot01 );
- var result = optionalTarget || new Vector3();
- // collinear or singular triangle
- if ( denom === 0 ) {
- // arbitrary location outside of triangle?
- // not sure if this is the best idea, maybe should be returning undefined
- return result.set( - 2, - 1, - 1 );
- }
- var invDenom = 1 / denom;
- var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
- var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
- // barycentric coordinates must always sum to 1
- return result.set( 1 - u - v, v, u );
- };
- }(),
- containsPoint: function () {
- var v1 = new Vector3();
- return function containsPoint( point, a, b, c ) {
- var result = Triangle.barycoordFromPoint( point, a, b, c, v1 );
- return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
- };
- }()
- } );
- Object.assign( Triangle.prototype, {
- constructor: Triangle,
- set: function ( a, b, c ) {
- this.a.copy( a );
- this.b.copy( b );
- this.c.copy( c );
- return this;
- },
- setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
- this.a.copy( points[ i0 ] );
- this.b.copy( points[ i1 ] );
- this.c.copy( points[ i2 ] );
- return this;
- },
- clone: function () {
- return new this.constructor().copy( this );
- },
- copy: function ( triangle ) {
- this.a.copy( triangle.a );
- this.b.copy( triangle.b );
- this.c.copy( triangle.c );
- return this;
- },
- area: function () {
- var v0 = new Vector3();
- var v1 = new Vector3();
- return function area() {
- v0.subVectors( this.c, this.b );
- v1.subVectors( this.a, this.b );
- return v0.cross( v1 ).length() * 0.5;
- };
- }(),
- midpoint: function ( optionalTarget ) {
- var result = optionalTarget || new Vector3();
- return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
- },
- normal: function ( optionalTarget ) {
- return Triangle.normal( this.a, this.b, this.c, optionalTarget );
- },
- plane: function ( optionalTarget ) {
- var result = optionalTarget || new Plane();
- return result.setFromCoplanarPoints( this.a, this.b, this.c );
- },
- barycoordFromPoint: function ( point, optionalTarget ) {
- return Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
- },
- containsPoint: function ( point ) {
- return Triangle.containsPoint( point, this.a, this.b, this.c );
- },
- closestPointToPoint: function () {
- var plane = new Plane();
- var edgeList = [ new Line3(), new Line3(), new Line3() ];
- var projectedPoint = new Vector3();
- var closestPoint = new Vector3();
- return function closestPointToPoint( point, optionalTarget ) {
- var result = optionalTarget || new Vector3();
- var minDistance = Infinity;
- // project the point onto the plane of the triangle
- plane.setFromCoplanarPoints( this.a, this.b, this.c );
- plane.projectPoint( point, projectedPoint );
- // check if the projection lies within the triangle
- if( this.containsPoint( projectedPoint ) === true ) {
- // if so, this is the closest point
- result.copy( projectedPoint );
- } else {
- // if not, the point falls outside the triangle. the result is the closest point to the triangle's edges or vertices
- edgeList[ 0 ].set( this.a, this.b );
- edgeList[ 1 ].set( this.b, this.c );
- edgeList[ 2 ].set( this.c, this.a );
- for( var i = 0; i < edgeList.length; i ++ ) {
- edgeList[ i ].closestPointToPoint( projectedPoint, true, closestPoint );
- var distance = projectedPoint.distanceToSquared( closestPoint );
- if( distance < minDistance ) {
- minDistance = distance;
- result.copy( closestPoint );
- }
- }
- }
- return result;
- };
- }(),
- equals: function ( triangle ) {
- return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
- }
- } );
- export { Triangle };
|