aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/3D/view-utils.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/3D/view-utils.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/3D/view-utils.js')
-rw-r--r--js/helper-classes/3D/view-utils.js1342
1 files changed, 1342 insertions, 0 deletions
diff --git a/js/helper-classes/3D/view-utils.js b/js/helper-classes/3D/view-utils.js
new file mode 100644
index 00000000..c8e20def
--- /dev/null
+++ b/js/helper-classes/3D/view-utils.js
@@ -0,0 +1,1342 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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
7var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
8 snapManagerModule = require("js/helper-classes/3D/snap-manager"),
9 Rectangle = require("js/helper-classes/3D/rectangle").Rectangle,
10 ElementsMediator = require("js/mediators/element-mediator").ElementMediator;
11///////////////////////////////////////////////////////////////////////
12// Class ViewUtils
13// Viewing Utility functions
14///////////////////////////////////////////////////////////////////////
15var ViewUtils = exports.ViewUtils = Object.create(Object.prototype,
16{
17 ///////////////////////////////////////////////////////////////////////
18 // Instance variables
19 ///////////////////////////////////////////////////////////////////////
20
21 m_viewportObj : { value: null, writable: true},
22 _perspectiveDist: { value: null, writable: true},
23
24 // keep a stack of viewport objects
25 _viewportObjStack: { value: [], writable: true },
26
27 _currentDocument: { value: null , writable: true},
28 _userContentLeft: { value: null , writable: true},
29 _userContentTop: { value: null , writable: true},
30
31 _rootElement: { value: null, writable: true},
32 _stageElement: { value: null, writable: true},
33
34 ///////////////////////////////////////////////////////////////////////
35 // Property accessors
36 ///////////////////////////////////////////////////////////////////////
37 setViewportObj: {
38 value: function( vp ) {
39 this.m_viewportObj = vp;
40 this._perspectiveDist = 1400;
41
42 var dist = this.getPerspectiveDistFromElement( vp );
43 var mode = this.getPerspectiveModeFromElement( vp );
44 }
45 },
46 getViewportObj: { value: function() { return this.m_viewportObj; } },
47
48 setRootElement: { value: function( elt ) { this._rootElement = elt; } },
49 getRootElement: { value: function () { return this._rootElement; } },
50
51 setStageElement: { value: function( elt ) { this._stageElement = elt; } },
52 getStageElement: { value: function () { return this._stageElement; } },
53
54 setCurrentDocument: { value: function(value) { this._currentDocument = value; }},
55
56 setUserContentLeft: { value: function(value) { this._userContentLeft = value; }},
57 setUserContentTop: { value: function(value) { this._userContentTop = value; }},
58
59
60 getPerspectiveDistance: { value: function () { return this._perspectiveDist; } },
61
62 ///////////////////////////////////////////////////////////////////////
63 // Camera and View Methods
64 ///////////////////////////////////////////////////////////////////////
65 getMatrixFromElement: {
66 value: function( elt ) {
67 var mat = ElementsMediator.getMatrix(elt);
68 if(mat)
69 {
70 return mat;
71 }
72 else
73 {
74 return Matrix.I(4);
75 }
76 }
77 },
78
79 setMatrixForElement: {
80 value: function( elt, mat, isChanging ) {
81 ElementsMediator.setMatrix(elt, mat, isChanging);
82 }
83 },
84
85
86 elementHas3D: {
87 value: function( elt ) {
88 return ElementsMediator.has3D(elt);
89 }
90 },
91
92 getElementPlane: {
93 value: function( elt ) {
94 var bounds = this.getElementViewBounds3D( elt );
95 var xArr = new Array(), yArr = new Array(), zArr = new Array();
96 for (var j=0; j<4; j++)
97 {
98 var pt = this.localToGlobal( bounds[j], elt );
99 xArr.push(pt[0]); yArr.push(pt[1]); zArr.push(pt[2]);
100 }
101 var normal = MathUtils.getPolygonNormal( 4, xArr, yArr, zArr );
102 //var d = -MathUtils.dot3( bounds[0], normal );
103 var d = -MathUtils.dot3( [xArr[0],yArr[0],zArr[0]], normal );
104 normal[3] = d;
105
106 return normal;
107 }
108 },
109
110 getUnprojectedElementPlane: {
111 value: function( elt ) {
112 var mat = this.getMatrixFromElement(elt);
113 var plane = [mat[8], mat[9], mat[10], mat[11]];
114
115 // The translation value is a point on the plane
116 this.pushViewportObj( elt );
117 var ptOnPlane = this.getCenterOfProjection();
118 this.popViewportObj();
119
120 ptOnPlane[2] = 0;
121
122 ptOnPlane = this.localToStageWorld(ptOnPlane, elt);
123 plane[3] = -vecUtils.vecDot(3, plane, ptOnPlane );
124
125 return plane;
126 }
127 },
128
129 getNormalToUnprojectedElementPlane: {
130 value: function( elt ) {
131 var mat = this.getMatrixFromElement(elt);
132
133 var xVec = [mat[0], mat[1], mat[2], mat[3]];
134 var yVec = [mat[4], mat[5], mat[6], mat[7]];
135
136 var stage = snapManagerModule.SnapManager.getStage();
137 var stageMat = this.getMatrixFromElement(stage);
138 var stagePlane = [stageMat[8], stageMat[9], stageMat[10], stageMat[11]];
139
140 var xDot = Math.abs(vecUtils.vecDot(3, xVec, stagePlane));
141 var yDot = Math.abs(vecUtils.vecDot(3, yVec, stagePlane));
142
143 var plane;
144 if(xDot > yDot)
145 {
146 plane = xVec;
147 }
148 else
149 {
150 plane = yVec;
151 }
152
153 // The translation value is a point on the plane
154 this.pushViewportObj( elt );
155 var ptOnPlane = this.getCenterOfProjection();
156 this.popViewportObj();
157
158 ptOnPlane[2] = 0;
159
160 ptOnPlane = this.localToStageWorld(ptOnPlane, elt);
161 plane[3] = -vecUtils.vecDot(3, plane, ptOnPlane );
162
163 return plane;
164 }
165 },
166
167 getPerspectiveModeFromElement: {
168 value: function( elt ) {
169 return ElementsMediator.getPerspectiveMode(elt);
170 }
171 },
172
173 getPerspectiveDistFromElement: {
174 value: function( elt ) {
175 return ElementsMediator.getPerspectiveDist(elt);
176 }
177 },
178
179 getEyePoint: {
180 value:function() {
181 // this function should use the center of projection - it is currently hard wired to (0,0).
182 var eye = [0, 0, this._perspectiveDist];
183
184 return eye;
185 }
186 },
187
188 getCameraMatrix: {
189 value: function() {
190 return this.getMatrixFromElement( this.m_viewportObj );
191 }
192 },
193
194 getElementViewBounds3D: {
195 value: function( elt ) {
196 var bounds = this.getElementBounds( elt, true );
197 var ptArray = new Array();
198 for (var i=0; i<4; i++)
199 {
200 var pt = bounds.getPoint( i );
201 pt[2] = 0; // z == 0
202 ptArray.push( pt );
203 }
204
205 return ptArray;
206 }
207 },
208
209 getCenterOfProjection: {
210 value: function() {
211 var cop;
212 var vp = this.getViewportObj();
213 if (vp)
214 {
215 var bounds = this.getViewportBounds();
216 cop = bounds.getCenter();
217 //cop = Vector.create( [bounds.getRight(), bounds.getBottom()] );
218 }
219
220 return cop;
221 }
222 },
223
224 preToPostScreen: {
225 value: function( pt, elt ) {
226 this.pushViewportObj( elt );
227 var viewPt = this.screenToView( pt[0], pt[1], pt[2] );
228 var mat = this.getMatrixFromElement( elt );
229 var worldPt = MathUtils.transformPoint( viewPt, mat );
230 var screenPt = this.viewToScreen( worldPt );
231 this.popViewportObj();
232