aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/3D/StageLine.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/3D/StageLine.js')
-rwxr-xr-xjs/helper-classes/3D/StageLine.js566
1 files changed, 354 insertions, 212 deletions
diff --git a/js/helper-classes/3D/StageLine.js b/js/helper-classes/3D/StageLine.js
index 787a4568..f9abc5ce 100755
--- a/js/helper-classes/3D/StageLine.js
+++ b/js/helper-classes/3D/StageLine.js
@@ -1,24 +1,25 @@
1/* <copyright> 1/* <copyright>
2Copyright (c) 2012, Motorola Mobility, Inc 2Copyright (c) 2012, Motorola Mobility LLC.
3All Rights Reserved. 3All Rights Reserved.
4BSD License.
5 4
6Redistribution and use in source and binary forms, with or without 5Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met: 6modification, are permitted provided that the following conditions are met:
8 7
9 - Redistributions of source code must retain the above copyright notice, 8* Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer. 9 this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright 10
12 notice, this list of conditions and the following disclaimer in the 11* Redistributions in binary form must reproduce the above copyright notice,
13 documentation and/or other materials provided with the distribution. 12 this list of conditions and the following disclaimer in the documentation
14 - Neither the name of Motorola Mobility nor the names of its contributors 13 and/or other materials provided with the distribution.
15 may be used to endorse or promote products derived from this software 14
16 without specific prior written permission. 15* Neither the name of Motorola Mobility LLC nor the names of its
16 contributors may be used to endorse or promote products derived from this
17 software without specific prior written permission.
17 18
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@@ -33,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
33// The line class represents a line intersected with all planes on the scene 34// The line class represents a line intersected with all planes on the scene
34/////////////////////////////////////////////////////////////////////// 35///////////////////////////////////////////////////////////////////////
35var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; 36var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
37var viewUtils = require( "js/helper-classes/3D/view-utils").ViewUtils;
36var LinePlaneIntersectRec = require("js/helper-classes/3D/LinePlaneIntersectRec").LinePlaneIntersectRec; 38var LinePlaneIntersectRec = require("js/helper-classes/3D/LinePlaneIntersectRec").LinePlaneIntersectRec;
37 39
38var StageLine = exports.StageLine = Object.create(Object.prototype, { 40var StageLine = exports.StageLine = Object.create(Object.prototype, {
@@ -95,7 +97,7 @@ var StageLine = exports.StageLine = Object.create(Object.prototype, {
95 if (minPt[2] > plane.getZMax()) return; 97 if (minPt[2] > plane.getZMax()) return;
96 98
97 // get the boundary points for the plane 99 // get the boundary points for the plane
98 var boundaryPts = plane.getBoundaryPoints(); 100 var boundaryPts = plane.getBoundaryPoints().slice();
99 101
100 // get the points and direction vector for the current line 102 // get the points and direction vector for the current line
101 var pt0 = this.getPoint0(), pt1 = this.getPoint1(); 103 var pt0 = this.getPoint0(), pt1 = this.getPoint1();
@@ -114,13 +116,14 @@ var StageLine = exports.StageLine = Object.create(Object.prototype, {
114 116
115 // see if the intersection point is contained in the bounds 117 // see if the intersection point is contained in the bounds
116 //var contains = this.boundaryContainsPoint( boundaryPts, plane.isBackFacing(), pt ); 118 //var contains = this.boundaryContainsPoint( boundaryPts, plane.isBackFacing(), pt );
117 var contains = MathUtils.boundaryContainsPoint( boundaryPts, pt, plane.isBackFacing() ); 119 var onEdge = [];
120 var contains = MathUtils.boundaryContainsPoint( boundaryPts, pt, plane.isBackFacing(), onEdge );
118 if (contains == MathUtils.INSIDE) 121 if (contains == MathUtils.INSIDE)
119 { 122 {
120 // add the intersection 123 // add the intersection
121 var dot = MathUtils.dot3( pt0, planeEq ) + planeEq[3]; 124 var dot = MathUtils.dot3( pt0, planeEq ) + planeEq[3];
122 var deltaVis = (dot > 0) ? 1 : -1; 125 var deltaVis = (dot > 0) ? 1 : -1;
123// if (plane.isBackFacing()) 126// if (plane.isBackFacing())
124// deltaVis = (dot < 0) ? 1 : -1; 127// deltaVis = (dot < 0) ? 1 : -1;
125 128
126 this.addIntersection( plane, t, deltaVis ); 129 this.addIntersection( plane, t, deltaVis );
@@ -129,23 +132,40 @@ var StageLine = exports.StageLine = Object.create(Object.prototype, {
129 { 132 {
130 if (MathUtils.fpCmp(t,1.0) < 0) 133 if (MathUtils.fpCmp(t,1.0) < 0)
131 { 134 {
132 // take the dot product between the line and the normal to the plane 135 // determine if the intersection is on a front side (no intersection) of the polygons
133 // to determine the change in visibility 136 //var ctr = [ 0.5*(boundaryPts[0][0] + boundaryPts[2][0]), 0.5*(boundaryPts[0][1] + boundaryPts[2][1]), 0.5*(boundaryPts[0][2] + boundaryPts[2][2]) ];
134 var vec = vecUtils.vecSubtract( 3, pt1, pt0 ); 137 //var vec = vecUtils.vecSubtract(3, pt, ctr );
135 var dot = vecUtils.vecDot( 3, vec, plane.getPlaneEq() ); 138 if (this.edgeGoesBehindPlane( plane, boundaryPts, onEdge[0], onEdge[1], pt0, pt1 ))
136 var sign = MathUtils.fpSign( dot );
137 if (sign == 0)
138 throw new Error( "coplanar intersection being treated as not coplanar" );
139 if (!plane.isBackFacing())
140 { 139 {
141 if (sign < 0) 140 this.addIntersection( plane, t, 1 );
142 this.addIntersection( plane, t, 1 );
143 } 141 }
144 else 142 else if (this.edgeGoesBehindPlane( plane, boundaryPts, onEdge[0], onEdge[1], pt1, pt0 ))
145 { 143 {
146 if (sign > 0) 144 this.addIntersection( plane, t, -1 );
147 this.addIntersection( plane, t, -1 ); 145 }
146
147 /*
148 if ( !this.edgeIsFrontFacing(boundaryPts, planeEq, plane.isBackFacing(), onEdge[0], onEdge[1]) )
149 {
150 // take the dot product between the line and the normal to the plane
151 // to determine the change in visibility
152 var vec = vecUtils.vecSubtract( 3, pt1, pt0 );
153 var dot = vecUtils.vecDot( 3, vec, planeEq );
154 var sign = MathUtils.fpSign( dot );
155 if (sign == 0)
156 throw new Error( "coplanar intersection being treated as not coplanar" );
157 if (!plane.isBackFacing())
158 {
159 if (sign < 0)
160 this.addIntersection( plane, t, 1 );
161 }
162 else
163 {
164 if (sign > 0)
165 this.addIntersection( plane, t, -1 );
166 }
148 } 167 }
168 */
149 } 169 }
150 } 170 }
151 } 171 }
@@ -175,14 +195,14 @@ var StageLine = exports.StageLine = Object.create(Object.prototype, {
175 MathUtils.negate( vec ); 195 MathUtils.negate( vec );
176 planeEq = [-vec[1], vec[0], 0]; 196 planeEq = [-vec[1], vec[0], 0];
177 var normal = [planeEq[0], planeEq[1], planeEq[2]]; 197 var normal = [planeEq[0], planeEq[1], planeEq[2]];
178// var d = -planeEq.dot(bPt0); 198// var d = -planeEq.dot(bPt0);
179 var d = -vecUtils.vecDot(3, planeEq, bPt0); 199 var d = -vecUtils.vecDot(3, planeEq, bPt0);
180 planeEq[3] = d; 200 planeEq[3] = d;
181 201
182 t = MathUtils.vecIntersectPlaneForParam( pt0, lineDir, planeEq ); 202 t = MathUtils.vecIntersectPlaneForParam( pt0, lineDir, planeEq );
183 if (t) 203 if (t)
184 { 204 {
185 if ((MathUtils.fpSign(t) > 0) && (MathUtils.fpCmp(t,1.0) <= 0)) // the strict vs not-strict inequality comparisons are IMPORTANT! 205 if ((MathUtils.fpSign(t) > 0) && (MathUtils.fpCmp(t,1.0) <= 0)) // the strict vs not-strict inequality comparisons are IMPORTANT!
186 { 206 {
187 // get the intersection point 207 // get the intersection point
188 var pt = MathUtils.interpolateLine3D( pt0, pt1, t ); 208 var pt = MathUtils.interpolateLine3D( pt0, pt1, t );
@@ -207,184 +227,306 @@ var StageLine = exports.StageLine = Object.create(Object.prototype, {
207 } 227 }
208 }, 228 },
209 229
210 doCoplanarIntersection: { 230 edgeGoesBehindPlane:
211 value: function( plane ) 231 {
212 { 232 value: function( plane, boundaryPts, iEdge, t, lPt0, lPt1 )
213 // get the boundary points for the plane 233 {
214 var boundaryPts = plane.getBoundaryPoints(); 234 var rtnVal = false;
215 var planeEq = plane.getPlaneEq(); 235
216 236 if ( MathUtils.fpCmp(t,1.0) == 0 )
217 if (plane.isBackFacing()) 237 {
218 { 238 iEdge = (iEdge + 1) % 4;
219 var tmp; 239 t = 0.0;
220 tmp = boundaryPts[0]; boundaryPts[0] = boundaryPts[3]; boundaryPts[3] = tmp; 240 }
221 tmp = boundaryPts[1]; boundaryPts[1] = boundaryPts[2]; boundaryPts[2] = tmp; 241
222 } 242 // boundary points (line points: lPt0, lPt1)
223 243 var bPt0, bPt1, bPt2, bVec, bVec0, bVec1, lVec, d;
224 var pt0 = this.getPoint0(), 244
225 pt1 = this.getPoint1(); 245 var planeEq = plane.getPlaneEq();
226 246 if (MathUtils.fpSign(t) == 0)
227 // keep a couple flags to prevent counting crossings twice in edge cases 247 {
228 var gotEnter = false, 248 // get the 3 relevant points. The line goes through pt1.
229 gotExit = false; 249 bPt0 = boundaryPts[(iEdge+3)%4].slice();
230 250 bPt1 = boundaryPts[iEdge].slice();
231 var bp1 = boundaryPts[3]; 251 bPt2 = boundaryPts[(iEdge+1)%4].slice();
232 for (var i=0; i<4; i++) 252 bVec0 = vecUtils.vecSubtract(2, bPt0, bPt1);
233 { 253 bVec1 = vecUtils.vecSubtract(2, bPt2, bPt1);
234 var bp0 = bp1; 254 lVec = vecUtils.vecSubtract(2, lPt1, bPt1);
235 bp1 = boundaryPts[i]; 255
236 var vec = vecUtils.vecSubtract(3, bp1, bp0); 256 var c0 = vecUtils.vecCross(2, bVec1, lVec),
237 var nrm = vecUtils.vecCross(3, vec, planeEq); 257 c1 = vecUtils.vecCross(2, lVec, bVec0);
238 nrm[3] = -vecUtils.vecDot(3, bp0, nrm); 258// if ((MathUtils.fpSign(c0) < 0) && (MathUtils.fpSign(c1) < 0))
239 259// rtnVal = true;
240 var d0 = vecUtils.vecDot(3, nrm, pt0) + nrm[3], 260 if (!plane.isBackFacing() && (MathUtils.fpSign(c0) < 0) && (MathUtils.fpSign(c1) < 0))
241 d1 = vecUtils.vecDot(3, nrm, pt1) + nrm[3]; 261