diff options
Diffstat (limited to 'js/helper-classes/3D/element-planes.js')
-rw-r--r-- | js/helper-classes/3D/element-planes.js | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/js/helper-classes/3D/element-planes.js b/js/helper-classes/3D/element-planes.js new file mode 100644 index 00000000..7ccf311e --- /dev/null +++ b/js/helper-classes/3D/element-planes.js | |||
@@ -0,0 +1,134 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /////////////////////////////////////////////////////////////////////// | ||
8 | // Class ElementPlanes | ||
9 | // This class represents the 5 planes generated by an element | ||
10 | // in the world - the face itself, and one plane going back in Z | ||
11 | // for each side of the element. Lines drawn will clip against | ||
12 | // those 5 planes. | ||
13 | /////////////////////////////////////////////////////////////////////// | ||
14 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils, | ||
15 | vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils, | ||
16 | Rectangle = require("js/helper-classes/3D/rectangle").Rectangle; | ||
17 | |||
18 | var ElementPlanes = exports.ElementPlanes = Object.create(Object.prototype, { | ||
19 | /////////////////////////////////////////////////////////////////////// | ||
20 | // Instance variables | ||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | |||
23 | // maintain a back pointer to the original element | ||
24 | _elt: { value: null, writable: true }, | ||
25 | |||
26 | // the 4 3D boundary points in world space | ||
27 | _boundaryPts: { value: null, writable: true }, | ||
28 | |||
29 | // the 2D boundary rectangle. Used for quick rejection tests. | ||
30 | _rect: { value: null, writable: true }, | ||
31 | _zMin: { value: 0, writable: true }, | ||
32 | _zMax: { value: 0, writable: true }, | ||
33 | |||
34 | // keep a flag indicating that the element is back facing | ||
35 | _backFacing: { value: false, writable: true }, | ||
36 | |||
37 | // cache the normal | ||
38 | _planeEq: { value: null, writable: true }, | ||
39 | |||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | // Property accessors | ||
42 | /////////////////////////////////////////////////////////////////////// | ||
43 | setElement: { value: function( elt ) { this._elt = elt; } }, | ||
44 | getElement: { value: function() { return this._elt; } }, | ||
45 | |||
46 | getPlaneEq: { value: function() { return this._planeEq.slice(0); } }, | ||
47 | setPlaneEq: { value: function(p) { this._planeEq = p; } }, | ||
48 | |||
49 | getRectangle: { value: function() { return this._rect; } }, | ||
50 | getBoundaryPoints: { value: function() { return this._boundaryPts; } }, | ||
51 | isBackFacing: { value: function() { return this._backFacing; } }, | ||
52 | |||
53 | getZMin: { value: function() { return this._zMin; } }, | ||
54 | getZMax: { value: function() { return this._zMax; } }, | ||
55 | |||
56 | /////////////////////////////////////////////////////////////////////// | ||
57 | // Methods | ||
58 | /////////////////////////////////////////////////////////////////////// | ||
59 | init: { | ||
60 | value: function() | ||
61 | { | ||
62 | if (this._elt) | ||
63 | { | ||
64 | // get the 3D boundary points | ||
65 | var elt = this._elt; | ||
66 | this._boundaryPts = viewUtils.getElementViewBounds3D( elt ); | ||
67 | //var mat = viewUtils.getMatrixFromElement( elt ); | ||
68 | |||
69 | var tmpMat = viewUtils.getLocalToGlobalMatrix( elt ); | ||
70 | for (var i=0; i<4; i++) | ||
71 | { | ||
72 | // this._boundaryPts[i] = viewUtils.localToGlobal(this._boundaryPts[i], elt ); | ||
73 | this._boundaryPts[i] = viewUtils.localToGlobal2(this._boundaryPts[i], tmpMat); | ||
74 | } | ||
75 | |||
76 | // set the backfacing flag based on the direction of the normal | ||
77 | // for the purposes of stage drawing, we use a left-handed coordinate system | ||
78 | // (as opposed to the left-handed system used in the browser). With that | ||
79 | // assumption, the points are specified counterclockwise on the stage, producing | ||
80 | // a negative Z for the normal, hence a positive Z implies back facing. | ||
81 | var nrm = this.computeNormal(); | ||
82 | this._backFacing = false; | ||
83 | if (nrm[2] > 0) | ||
84 | this._backFacing = true; | ||
85 | else | ||
86 | MathUtils.negate( nrm ); | ||
87 | |||
88 | // create the plane equation | ||
89 | //var d = -( nrm.dot( this._boundaryPts[0]) ); | ||
90 | var d = -vecUtils.vecDot(3, nrm, this._boundaryPts[0]); | ||
91 | var planeEq = Vector.create( [nrm[0], nrm[1], nrm[2], d] ); | ||
92 | this.setPlaneEq( planeEq ); | ||
93 | |||
94 | // get the 2D rectangle | ||
95 | var rect = Object.create(Rectangle, {}); | ||
96 | rect.setLeft( this._boundaryPts[0][0] ); | ||
97 | rect.setTop( this._boundaryPts[0][1] ); | ||
98 | rect.setRight( this._boundaryPts[0][0] ); | ||
99 | rect.setBottom( this._boundaryPts[0][1] ); | ||
100 | this._zMin = this._boundaryPts[0][2]; | ||
101 | this._zMax = this._boundaryPts[0][2]; | ||
102 | for (var i=1; i<4; i++) | ||
103 | { | ||
104 | rect.unionPoint( this._boundaryPts[i] ); | ||
105 | var z = this._boundaryPts[i][2]; | ||
106 | if (z < this._zMin) this._zMin = z; | ||
107 | if (z > this._zMax) this._zMax = z; | ||
108 | } | ||
109 | this._rect = rect; | ||
110 | } | ||
111 | } | ||
112 | }, | ||
113 | |||
114 | computeNormal: { | ||
115 | value: function() | ||
116 | { | ||
117 | var xPts = new Array(), yPts = new Array(), zPts = new Array(); | ||
118 | var n = this._boundaryPts.length; | ||
119 | for (var i=0; i<n; i++) | ||
120 | { | ||
121 | var pt = this._boundaryPts[i]; | ||
122 | xPts.push( pt[0] ); | ||
123 | yPts.push( pt[1] ); | ||
124 | zPts.push( pt[2] ); | ||
125 | } | ||
126 | var nrm = MathUtils.getPolygonNormal( n, xPts, yPts, zPts ); | ||
127 | |||
128 | return nrm; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | }); | ||
133 | |||
134 | |||