aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/3D/hit-record.js
blob: 7e39bb50a40054ebfb4feb0314151179bee84604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* <copyright>
Copyright (c) 2012, Motorola Mobility, Inc
All Rights Reserved.
BSD License.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  - Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  - Neither the name of Motorola Mobility nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */

///////////////////////////////////////////////////////////////////////
// Class HitRecord
//
///////////////////////////////////////////////////////////////////////

var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
var drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils;
var snapManagerModule = require("js/helper-classes/3D/snap-manager");

var HitRecord = exports.HitRecord = Object.create(Object.prototype,
{

    ///////////////////////////////////////////////////////////////////////
    // Constant definitions
    ///////////////////////////////////////////////////////////////////////
    SNAP_TYPE_STAGE : { value: 1, writable: true },
    SNAP_TYPE_GRID_VERTEX : { value: 2, writable: true },
    SNAP_TYPE_GRID_HORIZONTAL : { value: 13, writable: true },
    SNAP_TYPE_GRID_VERTICAL: { value: 14, writable: true },
    SNAP_TYPE_ALIGN_MERGED: { value: 15, writable: true },
    SNAP_TYPE_HORIZONTAL_FROM_START: { value: 3, writable: true },
    SNAP_TYPE_VERTICAL_FROM_START: { value: 4, writable: true },
    SNAP_TYPE_ELEMENT: { value: 5, writable: true },
    SNAP_TYPE_ELEMENT_EDGE: { value: 6, writable: true },
    SNAP_TYPE_ELEMENT_VERTEX: { value: 7, writable: true },
    SNAP_TYPE_ALIGN_HORIZONTAL: { value: 8, writable: true },
    SNAP_TYPE_ALIGN_VERTICAL: { value: 9, writable: true },
    SNAP_TYPE_ALIGN_BOTH: { value: 10, writable: true },
    SNAP_TYPE_ELEMENT_CENTER: { value: 11, writable: true },
    SNAP_TYPE_CONTAINED_ELEMENT: { value: 12, writable: true },
    SNAP_TYPE_UNDEFINED: { value: null, writable: -1 },

    ///////////////////////////////////////////////////////////////////////
    // Instance variables
    ///////////////////////////////////////////////////////////////////////
    _type: { value: this.SNAP_TYPE_UNDEFINED, writable: true },
    _elt: { value: null, writable: true },              // this can be null.  example: snapping to the working plane
    _screenPt: { value: null, writable: true },     // snap point in global screen space
    _localPoint: { value: null, writable: true },       // snap point in the local space of the element
    _plane: { value: null, writable: true },        // plane equation at the snap point in local object space
    _planeMat: { value: Matrix.I(4) , writable: true },         // transform to take the point from plane space to the transformed view space of the element
    _assocScrPt: { value: null, writable: true },       // associated screen point
    _assocScrPt2: { value: null, writable: true },      // a second associated point for drawing multiple snap align hits
    _planarHit: { value: false, writable: true },
    _snapBoundaryIndex : { value: -1, writable: true },     // this used for snap align to object boundaries
    _isQuadPt :{ value: false, writable: true },    // used for snapping to an object's quadrant point

    ///////////////////////////////////////////////////////////////////////
    // Property accessors
    ///////////////////////////////////////////////////////////////////////
    getElt : { value: function()            {  return this._elt;                    }},
    setElt : { value: function(e)           {  this._elt = e;                       }},
    getElement : { value: function()            {  return this._elt;                    }},
    setElement : { value: function(e)           {  this._elt = e;                       }},

    getZIndex : { value: function()         {  return this._zIndex; }},
    setZIndex : { value: function(i)            {  this._zIndex = i;                    }},

    setScreenPoint : { value: function(s)           {  this._screenPt = s.slice(0);         }},
    getScreenPoint : { value: function()            {  return this._screenPt.slice(0);      }},

    setLocalPoint : { value: function(s)            {  this._localPt = s.slice(0);          }},
    getLocalPoint : { value:  function()            {  return this._localPt.slice(0);       }},

    setAssociatedScreenPoint : { value: function(s)         {  this._assocScrPt = s.slice(0);       }},
    getAssociatedScreenPoint : { value: function()          {  return this._assocScrPt.slice(0);    }},
    hasAssociatedScreenPoint : { value: function()          {  return this._assocScrPt != null;     }},

    setAssociatedScreenPoint2 : { value: function(s)            {  this._assocScrPt2 = s.slice(0);      }},
    getAssociatedScreenPoint2 : { value: function()         {  return this._assocScrPt2.slice(0);   }},
    hasAssociatedScreenPoint2 : { value: function()         {  return this._assocScrPt2 != null;    }},

    setPlane : { value: function(p)         {  this._plane = p.slice(0);            }},
    getPlane : { value: function()          {  return this._plane.slice(0);         }},

    setPlaneMatrix : { value: function(pm)          {  this._planeMat = pm.slice(0);        }},
    getPlaneMatrix : { value: function()            {  return this._planeMat.slice(0);      }},

    setPlanarHit : { value:  function(p)            {  this._planarHit = p;                 }},
    isPlanarHit : { value: function()           {  return this._planarHit;              }},

    getSnapBoundaryIndex : { value: function()          {  return this._snapBoundaryIndex;      }},
    setSnapBoundaryIndex : { value: function(i)         {  this._snapBoundaryIndex = i;         }},

    getType : { value: function()           {  return this._type;                   }},
    setType : { value: function(t)          {this._type = t;  if (!this.checkType())  { throw new Error("invalid snap type");  return;  }   }},

    getUseQuadPoint : { value: function()           {  return this._isQuadPt;               }},
    setUseQuadPoint : { value: function(q)          {this._isQuadPt = q;                    }},

    ///////////////////////////////////////////////////////////////////////
    // Methods
    ///////////////////////////////////////////////////////////////////////

    checkType :{
        value: function()   {
            var ok = false;
            switch (this._type)
            {
                case this.SNAP_TYPE_STAGE:
                //case this.SNAP_TYPE_GRID:
                case this.SNAP_TYPE_GRID_VERTEX:
                case this.SNAP_TYPE_GRID_HORIZONTAL:
                case this.SNAP_TYPE_GRID_VERTICAL:
                case this.SNAP_TYPE_HORIZONTAL_FROM_START:
                case this.SNAP_TYPE_VERTICAL_FROM_START:
                    ok = true;
                    break;

                case this.SNAP_TYPE_ALIGN_MERGED:
                    ok = true;
                    break;

                case this.SNAP_TYPE_ELEMENT:
                case this.SNAP_TYPE_ELEMENT_EDGE:
                case this.SNAP_TYPE_ELEMENT_VERTEX:
                case this.SNAP_TYPE_ELEMENT_CENTER:
                case this.SNAP_TYPE_CONTAINED_ELEMENT:
                    ok = true;
                    break;

                case this.SNAP_TYPE_ALIGN_HORIZONTAL:
                case this.SNAP_TYPE_ALIGN_VERTICAL:
                case this.SNAP_TYPE_ALIGN_BOTH:
                    ok = true;
                    break;
            }

            return ok;
        }
    },

    isSomeGridTypeSnap : {
        value: function() {
            return ((this._type == this.SNAP_TYPE_GRID_VERTEX) ||
                    (this._type == this.SNAP_TYPE_GRID_HORIZONTAL) ||
                    (this._type == this.SNAP_TYPE_GRID_VERTICAL)
                   );
        }
    },

    convertToWorkingPlane : {
        value: function( wp ) {
            var swp = this.calculateStageWorldPoint();
            var wpMat = drawUtils.getPlaneToWorldMatrix(wp, MathUtils.getPointOnPlane(wp));
            //var wpMatInv = wpMat.inverse();
            var wpMatInv = glmat4.inverse( wpMat, []);
            var localPt = MathUtils.transformPoint( swp, wpMatInv );

            //  create a new hit record
            var hr = Object.create(HitRecord);//new HitRecord();
            hr.setType(  this.SNAP_TYPE_STAGE );
            hr.setScreenPoint(  this.getScreenPoint() );
            hr.setLocalPoint(  localPt );
            hr.setPlane( wp );
            hr.setPlaneMatrix( wpMat );
            hr.setElt( snapManagerModule.SnapManager.getStage() );

            return hr;
        }
    },

    calculateStageWorldPoint : {
        value: function() {
            var wPt;
            var elt = this.getElt();
            if (elt != null)
            {
                var localPt = this.getLocalPoint();
                var planeMat = this.getPlaneMatrix();
                wPt = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat),  elt );
            }

            return wPt;
        }
    },


    calculateScreenPoint : {
        value: function() {
            var scrPt;

            var stage = snapManagerModule.SnapManager.getStage();
            var stageMat = viewUtils.getMatrixFromElement( stage );
            var offset = viewUtils.getElementOffset( stage );
            offset[2] = 0;
            viewUtils.pushViewportObj( stage );

            var elt = this.getElt();
            if (elt != null)
            {
                var localPt = this.getLocalPoint();
                var planeMat = this.getPlaneMatrix();
                scrPt = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat),  elt );
                scrPt = vecUtils.vecAdd( 3, viewUtils.viewToScreen( MathUtils.transformPoint(scrPt, stageMat) ), offset);
            }
            viewUtils.popViewportObj();

            return scrPt;
        }
    },

    calculateElementWorldPoint : {
        value: function() {
            var localPt = this.getLocalPoint();
            var worldPt = MathUtils.transformPoint( localPt, this.getPlaneMatrix() );

            return worldPt;
        }
    },

    calculateElementScreenPoint : {
        value: function() {
            var worldPt = this.calculateElementWorldPoint();
            viewUtils.pushViewportObj( this._elt );
            var scrPt = viewUtils.viewToScreen( worldPt );
            viewUtils.popViewportObj();

            return scrPt;
        }
    },

    calculateElementScreenToPlane : {
        value: function( scrPt,  plane ) {
            var elt = this.getElt();
            viewUtils.pushViewportObj( elt );
            var viewPt = viewUtils.screenToView( scrPt[0], scrPt[1], scrPt[2] );
            var eyePt;
            if(viewUtils.getPerspectiveDistFromElement(elt))
            {
                eyePt = viewUtils.getEyePoint();
            }
            else
            {
                eyePt = [viewPt[0], viewPt[1], 1400];
            }
            var projPt = MathUtils.vecIntersectPlane( eyePt, MathUtils.vecSubtract(viewPt,eyePt), plane );

            return projPt;
        }
    },

    calculateElementPreTransformScreenPoint : {
        value: function() {
            var localPt = this.getLocalPoint();

            var worldPt = MathUtils.transformPoint( localPt, this.getPlaneMatrix() );
            var mat = viewUtils.getMatrixFromElement( this._elt );
            glmat4.inverse( mat );
            localPt = MathUtils.transformPoint( worldPt, mat );

            viewUtils.pushViewportObj( this._elt );
            var scrPt = viewUtils.viewToScreen( localPt );
            viewUtils.popViewportObj();

            return scrPt;
        }
    },

    getTypeString :{
        value: function()   {
            var str;
            switch (this.getType())
            {
                case this.SNAP_TYPE_STAGE:                      str = "SNAP_TYPE_STAGE";                    break;
                case this.SNAP_TYPE_GRID_VERTEX:                str = "SNAP_TYPE_GRID_VERTEX";              break;
                case this.SNAP_TYPE_GRID_HORIZONTAL:            str = "SNAP_TYPE_GRID_HORIZONTAL";          break;
                case this.SNAP_TYPE_GRID_VERTICAL:              str = "SNAP_TYPE_GRID_VERTICAL";            break;
                case this.SNAP_TYPE_HORIZONTAL_FROM_START:      str = "SNAP_TYPE_HORIZONTAL_FROM_START";    break;
                case this.SNAP_TYPE_VERTICAL_FROM_START:        str = "SNAP_TYPE_VERTICAL_FROM_START";      break;
                case this.SNAP_TYPE_ELEMENT:                    str = "SNAP_TYPE_ELEMENT";                  break;
                case this.SNAP_TYPE_ELEMENT_EDGE:               str = "SNAP_TYPE_ELEMENT_EDGE";             break;
                case this.SNAP_TYPE_ELEMENT_VERTEX:             str = "SNAP_TYPE_ELEMENT_VERTEX";           break;
                case this.SNAP_TYPE_ELEMENT_CENTER:             str = "SNAP_TYPE_ELEMENT_CENTER";           break;
                case this.SNAP_TYPE_CONTAINED_ELEMENT:          str = "SNAP_TYPE_CONTAINED_ELEMENT";        break;
                case this.SNAP_TYPE_ALIGN_HORIZONTAL:           str = "SNAP_TYPE_ALIGN_HORIZONTAL";         break;
                case this.SNAP_TYPE_ALIGN_VERTICAL:             str = "SNAP_TYPE_ALIGN_VERTICAL";           break;
                case this.SNAP_TYPE_ALIGN_BOTH:                 str = "SNAP_TYPE_ALIGN_BOTH";               break;
                case this.SNAP_TYPE_ALIGN_MERGED:               str = "this.SNAP_TYPE_ALIGN_MERGED";        break;

                default:
                    str = "SNAP_TYPE_UNDEFINED";
                    break;
            }

            return str;
        }
    },

    test:
    {
        value: function()
        {
            var elt = this.getElement();
            var stage = viewUtils.getStage();
            if (elt === stage)  return;

            var localPt = this.calculateElementPreTransformScreenPoint();
            var stageWorldPt = this.calculateStageWorldPoint();
            var globalPt = this.getScreenPoint();
            var err = false;

            var test1 = viewUtils.localToGlobal( localPt, elt );
            var dist = vecUtils.vecDist(3, test1, globalPt);
            if (MathUtils.fpSign(dist) != 0)
            {
                err = true;
                console.log( "**** transform error 1 ***** " + dist + ", localPt: " + localPt );
            }

            var stageWorldToGlobal = viewUtils.getStageWorldToGlobalMatrix();
            var test2 = MathUtils.transformAndDivideHomogeneousPoint( stageWorldPt, stageWorldToGlobal );
            dist = vecUtils.vecDist(3, test2, globalPt);
            if (MathUtils.fpSign(dist) != 0)
            {
                err = true;
                console.log( "**** transform error 2 ***** " + dist + ", localPt: " + localPt );
            }

            var localToGlobal = viewUtils.getLocalToGlobalMatrix( elt );
            var globalToLocal = glmat4.inverse( localToGlobal, [] );
            var test3 = MathUtils.transformAndDivideHomogeneousPoint( globalPt, globalToLocal );
            dist = vecUtils.vecDist(3, test3, localPt);
            if (MathUtils.fpSign(dist) != 0)
            {
                err = true;
                console.log( "**** transform error 3 ***** " + dist + ", localPt: " + localPt );
            }

            var objToStageWorld = viewUtils.getObjToStageWorldMatrix( elt, true );
            var test4 = MathUtils.transformAndDivideHomogeneousPoint( localPt, objToStageWorld );
            dist = vecUtils.vecDist(3, test4, stageWorldPt);
            if (MathUtils.fpSign(dist) != 0)
            {
                err = true;
                console.log( "**** transform error 4 ***** " + dist + ", localPt: " + localPt );
            }

            //if (!err)  console.log( "no hitRecord error" );
        }
    }
});