1 | /**
|
---|
2 | * @author bhouston / http://exocortex.com
|
---|
3 | * @author mrdoob / http://mrdoob.com/
|
---|
4 | */
|
---|
5 |
|
---|
6 | THREE.Triangle = function ( a, b, c ) {
|
---|
7 |
|
---|
8 | this.a = ( a !== undefined ) ? a : new THREE.Vector3();
|
---|
9 | this.b = ( b !== undefined ) ? b : new THREE.Vector3();
|
---|
10 | this.c = ( c !== undefined ) ? c : new THREE.Vector3();
|
---|
11 |
|
---|
12 | };
|
---|
13 |
|
---|
14 | THREE.Triangle.normal = function() {
|
---|
15 |
|
---|
16 | var v0 = new THREE.Vector3();
|
---|
17 |
|
---|
18 | return function ( a, b, c, optionalTarget ) {
|
---|
19 |
|
---|
20 | var result = optionalTarget || new THREE.Vector3();
|
---|
21 |
|
---|
22 | result.subVectors( c, b );
|
---|
23 | v0.subVectors( a, b );
|
---|
24 | result.cross( v0 );
|
---|
25 |
|
---|
26 | var resultLengthSq = result.lengthSq();
|
---|
27 | if( resultLengthSq > 0 ) {
|
---|
28 |
|
---|
29 | return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 | return result.set( 0, 0, 0 );
|
---|
34 |
|
---|
35 | };
|
---|
36 |
|
---|
37 | }();
|
---|
38 |
|
---|
39 | // static/instance method to calculate barycoordinates
|
---|
40 | // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
|
---|
41 | THREE.Triangle.barycoordFromPoint = function() {
|
---|
42 |
|
---|
43 | var v0 = new THREE.Vector3();
|
---|
44 | var v1 = new THREE.Vector3();
|
---|
45 | var v2 = new THREE.Vector3();
|
---|
46 |
|
---|
47 | return function ( point, a, b, c, optionalTarget ) {
|
---|
48 |
|
---|
49 | v0.subVectors( c, a );
|
---|
50 | v1.subVectors( b, a );
|
---|
51 | v2.subVectors( point, a );
|
---|
52 |
|
---|
53 | var dot00 = v0.dot( v0 );
|
---|
54 | var dot01 = v0.dot( v1 );
|
---|
55 | var dot02 = v0.dot( v2 );
|
---|
56 | var dot11 = v1.dot( v1 );
|
---|
57 | var dot12 = v1.dot( v2 );
|
---|
58 |
|
---|
59 | var denom = ( dot00 * dot11 - dot01 * dot01 );
|
---|
60 |
|
---|
61 | var result = optionalTarget || new THREE.Vector3();
|
---|
62 |
|
---|
63 | // colinear or singular triangle
|
---|
64 | if( denom == 0 ) {
|
---|
65 | // arbitrary location outside of triangle?
|
---|
66 | // not sure if this is the best idea, maybe should be returning undefined
|
---|
67 | return result.set( -2, -1, -1 );
|
---|
68 | }
|
---|
69 |
|
---|
70 | var invDenom = 1 / denom;
|
---|
71 | var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
|
---|
72 | var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
|
---|
73 |
|
---|
74 | // barycoordinates must always sum to 1
|
---|
75 | return result.set( 1 - u - v, v, u );
|
---|
76 |
|
---|
77 | };
|
---|
78 |
|
---|
79 | }();
|
---|
80 |
|
---|
81 | THREE.Triangle.containsPoint = function() {
|
---|
82 |
|
---|
83 | var v1 = new THREE.Vector3();
|
---|
84 |
|
---|
85 | return function ( point, a, b, c ) {
|
---|
86 |
|
---|
87 | var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, v1 );
|
---|
88 |
|
---|
89 | return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
|
---|
90 |
|
---|
91 | };
|
---|
92 |
|
---|
93 | }();
|
---|
94 |
|
---|
95 | THREE.Triangle.prototype = {
|
---|
96 |
|
---|
97 | constructor: THREE.Triangle,
|
---|
98 |
|
---|
99 | set: function ( a, b, c ) {
|
---|
100 |
|
---|
101 | this.a.copy( a );
|
---|
102 | this.b.copy( b );
|
---|
103 | this.c.copy( c );
|
---|
104 |
|
---|
105 | return this;
|
---|
106 |
|
---|
107 | },
|
---|
108 |
|
---|
109 | setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
|
---|
110 |
|
---|
111 | this.a.copy( points[i0] );
|
---|
112 | this.b.copy( points[i1] );
|
---|
113 | this.c.copy( points[i2] );
|
---|
114 |
|
---|
115 | return this;
|
---|
116 |
|
---|
117 | },
|
---|
118 |
|
---|
119 | copy: function ( triangle ) {
|
---|
120 |
|
---|
121 | this.a.copy( triangle.a );
|
---|
122 | this.b.copy( triangle.b );
|
---|
123 | this.c.copy( triangle.c );
|
---|
124 |
|
---|
125 | return this;
|
---|
126 |
|
---|
127 | },
|
---|
128 |
|
---|
129 | area: function() {
|
---|
130 |
|
---|
131 | var v0 = new THREE.Vector3();
|
---|
132 | var v1 = new THREE.Vector3();
|
---|
133 |
|
---|
134 | return function () {
|
---|
135 |
|
---|
136 | v0.subVectors( this.c, this.b );
|
---|
137 | v1.subVectors( this.a, this.b );
|
---|
138 |
|
---|
139 | return v0.cross( v1 ).length() * 0.5;
|
---|
140 |
|
---|
141 | };
|
---|
142 |
|
---|
143 | }(),
|
---|
144 |
|
---|
145 | midpoint: function ( optionalTarget ) {
|
---|
146 |
|
---|
147 | var result = optionalTarget || new THREE.Vector3();
|
---|
148 | return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
|
---|
149 |
|
---|
150 | },
|
---|
151 |
|
---|
152 | normal: function ( optionalTarget ) {
|
---|
153 |
|
---|
154 | return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget );
|
---|
155 |
|
---|
156 | },
|
---|
157 |
|
---|
158 | plane: function ( optionalTarget ) {
|
---|
159 |
|
---|
160 | var result = optionalTarget || new THREE.Plane();
|
---|
161 |
|
---|
162 | return result.setFromCoplanarPoints( this.a, this.b, this.c );
|
---|
163 |
|
---|
164 | },
|
---|
165 |
|
---|
166 | barycoordFromPoint: function ( point, optionalTarget ) {
|
---|
167 |
|
---|
168 | return THREE.Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
|
---|
169 |
|
---|
170 | },
|
---|
171 |
|
---|
172 | containsPoint: function ( point ) {
|
---|
173 |
|
---|
174 | return THREE.Triangle.containsPoint( point, this.a, this.b, this.c );
|
---|
175 |
|
---|
176 | },
|
---|
177 |
|
---|
178 | equals: function ( triangle ) {
|
---|
179 |
|
---|
180 | return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
|
---|
181 |
|
---|
182 | },
|
---|
183 |
|
---|
184 | clone: function () {
|
---|
185 |
|
---|
186 | return new THREE.Triangle().copy( this );
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | };
|
---|