diff options
Diffstat (limited to 'js/helper-classes/3D/snap-2d-record.js')
-rw-r--r-- | js/helper-classes/3D/snap-2d-record.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/js/helper-classes/3D/snap-2d-record.js b/js/helper-classes/3D/snap-2d-record.js new file mode 100644 index 00000000..c77b28b3 --- /dev/null +++ b/js/helper-classes/3D/snap-2d-record.js | |||
@@ -0,0 +1,130 @@ | |||
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 HitRecord | ||
9 | // | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils; | ||
12 | var snapManager = require("js/helper-classes/3D/snap-manager"); | ||
13 | var Snap2DRecord = exports.Snap2DRecord = Object.create(Object.prototype, | ||
14 | { | ||
15 | /////////////////////////////////////////////////////////////////////// | ||
16 | // Constant definitions | ||
17 | /////////////////////////////////////////////////////////////////////// | ||
18 | |||
19 | /////////////////////////////////////////////////////////////////////// | ||
20 | // Instance variables | ||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | _elt : { value: null , writable: true}, // the four boundary points for the element in global screen space | ||
23 | _screenPtArray : { value: null , writable: true}, // snap point in global screen space | ||
24 | _alignPtArray : { value: null , writable: true}, // points for snap-align. Kept in working plane space | ||
25 | |||
26 | _localToGlobalMat : { value: null, writable: true }, | ||
27 | _globalToLocalMat : { value: null, writable: true }, | ||
28 | |||
29 | // indices to the extremal align points | ||
30 | _xMinArray : { value: [], writable: true }, | ||
31 | _xMaxArray : { value: [] , writable: true}, | ||
32 | _yMinArray : { value: [] , writable: true}, | ||
33 | _yMaxArray : { value: [] , writable: true}, | ||
34 | |||
35 | /////////////////////////////////////////////////////////////////////// | ||
36 | // Property accessors | ||
37 | /////////////////////////////////////////////////////////////////////// | ||
38 | getElement: { value: function() { return this._elt; }}, | ||
39 | setElement: { value: function() { this._elt = e; }}, | ||
40 | |||
41 | getScreenPointArray: { value: function() { return this._screenPtArray; }}, | ||
42 | getAlignPointArray: { value: function() { return this._alignPtArray; }}, | ||
43 | |||
44 | getLocalToGlobalMatrix: { value: function() { return this._localToGlobalMat; }}, | ||
45 | setLocalToGlobalMatrix: { value: function() { this._localToGlobalMat = l2g.slice(0); }}, | ||
46 | |||
47 | getGlobalToLocalMatrix: { value: function() { return this._globalToLocalMat; }}, | ||
48 | setGlobalToLocalMatrix: { value: function() { this._globalToLocalMat = g2l.slice(0); }}, | ||
49 | |||
50 | /////////////////////////////////////////////////////////////////////// | ||
51 | // Methods | ||
52 | /////////////////////////////////////////////////////////////////////// | ||
53 | init: { | ||
54 | value: function( elt ) { | ||
55 | this._elt = elt; | ||
56 | var bounds = viewUtils.getElementViewBounds3D( elt ); | ||
57 | |||
58 | // get the screen and align points from the bounds | ||
59 | this._screenPtArray = new Array(); | ||
60 | this._alignPtArray = new Array(); | ||
61 | this._localToGlobalMat = viewUtils.getLocalToGlobalMatrix( elt ); | ||
62 | //this._globalToLocalMat = this._localToGlobalMat.inverse(); | ||
63 | this._globalToLocalMat = glmat4.inverse( this._localToGlobalMat, []); | ||
64 | for (var i=0; i<4; i++) | ||
65 | { | ||
66 | this._screenPtArray[i] = viewUtils.localToGlobal( bounds[i], elt ); | ||
67 | var worldPt = viewUtils.localToStageWorld( bounds[i], elt ); | ||
68 | this._alignPtArray[i] = MathUtils.transformPoint( worldPt, snapManager.SnapManager._worldToDragPlane ); | ||
69 | |||
70 | ////////////////////////////////////////////////////////////////////// | ||
71 | // DEBUG CODE | ||
72 | var tmp = MathUtils.transformHomogeneousPoint( bounds[i], this._localToGlobalMat ); | ||
73 | tmp = MathUtils.applyHomogeneousCoordinate( tmp ); | ||
74 | if (!MathUtils.pointsEqual( 3, tmp, this._screenPtArray[i] )) | ||
75 | console.log( "**** Snap2DRecord cache screen points do not agree **** " + tmp + " => " + this._screenPtArray[i] ); | ||
76 | ////////////////////////////////////////////////////////////////////// | ||
77 | } | ||
78 | |||
79 | // add the center point | ||
80 | var xCtr = 0.5*(bounds[0][0] + bounds[3][0]), yCtr = 0.5*(bounds[0][1] + bounds[1][1]); | ||
81 | var ctr = Vector.create( [xCtr, yCtr, 0] ); | ||
82 | this._screenPtArray[4] = viewUtils.localToGlobal( ctr, elt ); | ||
83 | var worldPt = viewUtils.localToStageWorld( ctr, elt ); | ||
84 | this._alignPtArray[4] = MathUtils.transformPoint( worldPt, snapManager.SnapManager._worldToDragPlane ); | ||
85 | |||
86 | // set up the align points | ||
87 | this.initAlignExtremalPoints() | ||
88 | } | ||
89 | }, | ||
90 | |||
91 | initAlignExtremalPoints: { | ||
92 | value: function() { | ||
93 | var xMinArray = [0], xMaxArray = [0], | ||
94 | yMinArray = [0], yMaxArray = [0]; | ||
95 | var alignPts = this._alignPtArray; | ||
96 | var xMin = alignPts[0][0], xMax = alignPts[0][0], | ||
97 | yMin = alignPts[0][1], yMax = alignPts[0][1]; | ||
98 | var sign; | ||
99 | for (var i=1; i<4; i++) | ||
100 | { | ||
101 | var pt = alignPts[i]; | ||
102 | sign = MathUtils.fpCmp(pt[0], xMin); if (sign < 0) { xMinArray = [i]; xMin = pt[0]; } else if (sign == 0) xMinArray.push(i); | ||
103 | sign = MathUtils.fpCmp(pt[0], xMax); if (sign > 0) { xMaxArray = [i]; xMax = pt[0]; } else if (sign == 0) xMaxArray.push(i); | ||
104 | sign = MathUtils.fpCmp(pt[1], yMin); if (sign < 0) { yMinArray = [i]; yMin = pt[1]; } else if (sign == 0) yMinArray.push(i); | ||
105 | sign = MathUtils.fpCmp(pt[1], yMax); if (sign > 0) { yMaxArray = [i]; yMax = pt[1]; } else if (sign == 0) yMaxArray.push(i); | ||
106 | } | ||
107 | this._xMinArray = xMinArray; | ||
108 | this._xMaxArray = xMaxArray; | ||
109 | this._yMinArray = yMinArray; | ||
110 | this._yMaxArray = yMaxArray; | ||
111 | } | ||
112 | }, | ||
113 | |||
114 | getScreenPoint: { | ||
115 | value: function( index ) { | ||
116 | var rtnPt; | ||
117 | if ((index >= 0) && (index < 4) && (this._screenPtArray != null)) | ||
118 | rtnPt = this._screenPtArray[index].slice(0); | ||
119 | |||
120 | return rtnPt; | ||
121 | } | ||
122 | }, | ||
123 | |||
124 | addAlignPoint: { | ||
125 | value: function( pt ) { | ||
126 | this._alignPtArray.push( pt ); | ||
127 | } | ||
128 | } | ||
129 | }); | ||
130 | |||