/** * @author bhouston / http://exocortex.com * @author mrdoob / http://mrdoob.com/ */ THREE.Triangle = function ( a, b, c ) { this.a = ( a !== undefined ) ? a : new THREE.Vector3(); this.b = ( b !== undefined ) ? b : new THREE.Vector3(); this.c = ( c !== undefined ) ? c : new THREE.Vector3(); }; THREE.Triangle.normal = function() { var v0 = new THREE.Vector3(); return function ( a, b, c, optionalTarget ) { var result = optionalTarget || new THREE.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 barycoordinates // based on: http://www.blackpawn.com/texts/pointinpoly/default.html THREE.Triangle.barycoordFromPoint = function() { var v0 = new THREE.Vector3(); var v1 = new THREE.Vector3(); var v2 = new THREE.Vector3(); return function ( 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 THREE.Vector3(); // colinear 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; // barycoordinates must always sum to 1 return result.set( 1 - u - v, v, u ); }; }(); THREE.Triangle.containsPoint = function() { var v1 = new THREE.Vector3(); return function ( point, a, b, c ) { var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, v1 ); return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 ); }; }(); THREE.Triangle.intersectionRay = function () { // Compute the offset origin, edges, and normal. var diff = new THREE.Vector3(); var edge1 = new THREE.Vector3(); var edge2 = new THREE.Vector3(); var normal = new THREE.Vector3(); return function ( ray, a, b, c, backfaceCulling ) { //from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp edge1.subVectors( b, a ); edge2.subVectors( c, a ); normal.crossVectors( edge1, edge2 ); // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction, // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2)) // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q)) // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N) var DdN = ray.direction.dot(normal); var sign; if ( DdN > 0 ) { if ( backfaceCulling ) return null; sign = 1; } else if ( DdN < 0 ) { sign = - 1; DdN = - DdN; } else return null; diff.subVectors( ray.origin, a ); var DdQxE2 = sign * ray.direction.dot( edge2.crossVectors( diff, edge2 ) ); // b1 < 0, no intersection if ( DdQxE2 < 0 ) return null; var DdE1xQ = sign * ray.direction.dot( edge1.cross( diff ) ); // b2 < 0, no intersection if ( DdE1xQ < 0 ) return null; // b1+b2 > 1, no intersection if ( DdQxE2 + DdE1xQ > DdN ) return null // Line intersects triangle, check if ray does. var QdN = - sign * diff.dot( normal ); // t < 0, no intersection if ( QdN < 0 ) return null // Ray intersects triangle. return ray.at( QdN / DdN ); } }(); THREE.Triangle.prototype = { constructor: THREE.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; }, 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 THREE.Vector3(); var v1 = new THREE.Vector3(); return function () { 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 THREE.Vector3(); return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 ); }, normal: function ( optionalTarget ) { return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget ); }, plane: function ( optionalTarget ) { var result = optionalTarget || new THREE.Plane(); return result.setFromCoplanarPoints( this.a, this.b, this.c ); }, barycoordFromPoint: function ( point, optionalTarget ) { return THREE.Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget ); }, containsPoint: function ( point ) { return THREE.Triangle.containsPoint( point, this.a, this.b, this.c ); }, intersectionRay: function ( ray, backfaceCulling ) { return THREE.Triangle.intersectionRay( ray, this.a, this.b, this.c, backfaceCulling ); }, equals: function ( triangle ) { return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c ); }, clone: function () { return new THREE.Triangle().copy( this ); } };