aboutsummaryrefslogtreecommitdiff
path: root/js/tools/Rotate3DToolBase.js
blob: 545aedad9a6f7c1d8f9300dd987e154dcc220433 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

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 LLC 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 HOLDER 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> */

/* Base class for the 3D rotation tools
Subclass RotateObject3DTool will rotate the object that was clicked.
Subclass RotateStage3DTool will rotate the stage.
 */
var Montage = require("montage/core/core").Montage,
    ModifierToolBase = require("js/tools/modifier-tool-base").ModifierToolBase,
    snapManager = require("js/helper-classes/3D/snap-manager").SnapManager,
    viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils,
    vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
    drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils,
    ElementsMediator = require("js/mediators/element-mediator").ElementMediator;

exports.Rotate3DToolBase = Montage.create(ModifierToolBase, {
    _canSnap: { value: false },

    _inLocalMode: { value: true, enumerable: true },

    rotateStage: {
        value: false
    },

    initializeSnapping : {
        value : function(event)
        {
//            console.log( "initializeSnapping" );

            var selectedElements = this.application.ninja.selectedElements;
            if(this.rotateStage) {
                selectedElements = [this.application.ninja.currentDocument.model.documentRoot];
            }

            this._mouseDownHitRec = null;
            this._mouseUpHitRec   = null;

            snapManager.clearAvoidList();
            snapManager.clearDragPlane();

            // the translate tool does snap align to the bounds of the object only.
            // turn off snap align to the cursor.  This needs to be re-enabled in the mouse up method
            snapManager.enableSnapAlign( false );

            // snap to element and snap to grid are conditionally enabled based
            // on the snap results of the mouse down.  enable everything for the first snap
            this._snapToElements = snapManager.elementSnapEnabledAppLevel();
            this._snapToGrid = snapManager.gridSnapEnabledAppLevel();

            this._dragPlane = null;
            this._clickedOnStage = false;
            var do3DSnap = true;

            if(this._handleMode === null)
            {
                snapManager.enableElementSnap   ( true  );
                snapManager.enableGridSnap      ( true  );
            }
//            else
//            {
//                this._delta = null;
                // special case for z-translation
//                if(this._handleMode === 0)
//                {
//                    this._dragPlane = viewUtils.getNormalToUnprojectedElementPlane(this._target);
//                    snapManager.setupDragPlaneFromPlane(this._dragPlane);
//                    do3DSnap = false;

//                    snapManager.enableElementSnap ( false );
//                    snapManager.enableGridSnap        ( false );
//                }
//            }

            if (selectedElements.length)
            {
                var point = webkitConvertPointFromPageToNode(this.application.ninja.stage.canvas,
                                                                new WebKitPoint(event.pageX, event.pageY));

                // do the snap before setting up the avoid list to allow
                // a snap on the mouse down
                var hitRec = snapManager.snap(point.x, point.y, do3DSnap);

//                if(this._handleMode === 2)
//                {
//                    // translate z doesn't snap to element so hitRec's element will always be different
//                    // from what the browser says we clicked on. So, skip this check.
//                }
//                else
//                {
//                    // Check that hitRec's element matches element that browser says we clicked on
//                    // TODO - This is still not working when using a handle that is on top of an
//                    // element that is not currently selected
//                    var elt = this.application.ninja.stage.GetSelectableElement(event);
//                    if(elt && (elt !== hitRec.getElement()))
//                    {
//                        hitRec = snapManager.findHitRecordForElement(elt);
//                    }
//                    if(elt === this.application.ninja.currentDocument.model.domContainer)
//                    {
//                        this._clickedOnStage = true;
//                    }
//                }

                // we don't want to snap to selected objects during the drag
//                var len = this._targets.length;
//                for(var i=0; i<len; i++)
//                {
//                    snapManager.addToAvoidList( this._targets[i].elt );
//                }
                if (hitRec)
                {
                    // disable snap attributes
                    if (hitRec.getType() == hitRec.SNAP_TYPE_ELEMENT)
                    {
                        this._snapToElements = false;
                        this._snapToGrid = false;
                    }
                    else if (hitRec.getType() == hitRec.SNAP_TYPE_ELEMENT_CENTER)
                    {
                        snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() );
                    }

                    if(this._handleMode === 0)
                        this.clickedObject = this._target;

                    // parameterize the snap point on the target
                    this._snapParam = this.parameterizeSnap( hitRec );

                    if(!this._dragPlane)
                    {
                        if (selectedElements.length === 1)
                        {
                            this._dragPlane = viewUtils.getUnprojectedElementPlane(this._clickedObject);
                            snapManager.setupDragPlaneFromPlane(this._dragPlane);
                        }
                        else
                        {
                            this._dragPlane = snapManager.setupDragPlanes( hitRec, true );
                        }

                    }

                    // no quadrant snapping for the rotate tool
                    this._shouldUseQuadPt = false;

                    var wpHitRec = hitRec.convertToWorkingPlane( this._dragPlane );
                    this._mouseDownHitRec = wpHitRec;
                    this._mouseUpHitRec = null;

                    var pt = hitRec.getScreenPoint();
                    this.downPoint.x = pt[0];
                    this.downPoint.y = pt[1];
                    this.downPoint.z = pt[2];

                    // TODO - need to figure out snapManager dependency by drawUtils.
                    // For now, bypassing by calling snapManager.drawLastHit();
//                  drawUtils.refreshDisplay();
//                    snapManager.drawLastHit();
                }
            }
            else
            {
                this.target = null;
            }
        }
    },

    drawWithoutSnapping:
    {
        value: function(event)
        {
            if(this._handleMode !== null)
            {
                this._matL = this._handles[this._handleMode]._matL.slice(0);
                this._planeEq = this._handles[this._handleMode]._planeEq.slice(0);
                this._dirVecL = this._handles[this._handleMode]._dirVecL.slice(0);
                this._startPoint = MathUtils.getLocalPoint(this.downPoint.x,
                                                            this.downPoint.y,
                                                            this._planeEq,
                                                            this._matL);
                if(!this._startPoint)
                {
                    this._startPoint = [this.downPoint.x, this.downPoint.y];
                }
            }
        }
    },

    HandleMouseMove: {
        value: function(event) {
            if(this._escape) {
                this._escape = false;
                this.isDrawing = true;
            }

            if(this.isDrawing) {
                this._hasDraw = true;   // Flag for position of element
                this.doDraw(event);
            } else {
                this._showFeedbackOnMouseMove(event);
                if(this._canSnap)
                {
                    this.doSnap(event);
                }
            }

            this.DrawHandles(this._delta);

            if(this._canSnap)
            {
                snapManager.drawLastHit();
            }
        }
    },

    modifyElements: {
        value: function(data, event) {
            var mat,
                angle,
                pt0 = data.pt0,
                pt1 = data.pt1;

            var selectedElements = this.application.ninja.selectedElements;
            if(this.rotateStage) {
                selectedElements = [this.application.ninja.currentDocument.model.documentRoot];
            }

            if(this._handleMode !== null)
            {
                if(this._activateOriginHandle)
                {
                    // move the transform origin handle directly to the snap point (pt1)
                    this._origin[0] = pt1.x;
                    this._origin[1] = pt1.y;
                    this._origin[2] = pt1.z;

                    var sw2gMat = viewUtils.getStageWorldToGlobalMatrix();
                    var g2swMat = glmat4.inverse( sw2gMat, [] );
                    var swOrigin = MathUtils.transformAndDivideHomogeneousPoint( this._origin, g2swMat );
                    //console.log( "modifyElements, _origin: " + this._origin + ", in stageWorld: " + swOrigin );

                    var len = selectedElements.length;
                    if(len === 1)
                    {
                        var elt = selectedElements[0];
                        var g2lMat = elt.elementModel.getProperty("g2l");
                        var localOrigin = MathUtils.transformAndDivideHomogeneousPoint( this._origin, g2lMat );

                        viewUtils.pushViewportObj( elt );
                        var viewOrigin = viewUtils.screenToView( localOrigin[0], localOrigin[1], localOrigin[2] );
                        viewUtils.popViewportObj();
                        this._startOriginArray[0] = viewOrigin;
                        //console.log( "Rotate3DToolBase.modifyElements, _startOriginArray[0]: " + this._startOriginArray[0] );
                    }

                    this.downPoint.x = pt1.x;
                    this.downPoint.y = pt1.y;
                    this.downPoint.z = pt1.z;
                    this.DrawHandles();
                    return;
                }

                angle = this._getAngleToRotate(pt1.x, pt1.y);
                if(event.shiftKey)
                {
                    var f = Math.floor(angle/(Math.PI/4));
                    angle = f*Math.PI/4;
                }
                this._delta = angle;
                switch(this._handleMode)
                {
                    case 0:
                        // Rotate X;
                        mat = Matrix.RotationX(angle);
                        break;
                    case 1:
                        // Rotate Y
                        mat = Matrix.RotationY(angle);
                        break;
                    case 2:
                        // Rotate Z
                        mat = Matrix.RotationZ(angle);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                if (event.ctrlKey || event.metaKey)
                {
                    var zAngle = this._mouseSpeed * (pt1.y - this.downPoint.y) * Math.PI / 180.0;
                    if (zAngle === 0)
                    {
                        zAngle = 0.01 * Math.PI / 180.0;
                    }
                    if(event.shiftKey)
                    {
                        var f = Math.floor(zAngle/(Math.PI/4));
                        zAngle = f*Math.PI/4;
                    }
                    mat = Matrix.RotationZ(zAngle);
                }
                else
                {
                    var yAngle = this._mouseSpeed * (pt1.x - this.downPoint.x) * Math.PI / 180.0;
                    var xAngle = -this._mouseSpeed * (pt1.y - this.downPoint.y) * Math.PI / 180.0;
                    if(event.shiftKey)
                    {
                        var f = Math.floor(yAngle/(Math.PI/4));
                        yAngle = f*Math.PI/4;
                        f = Math.floor(xAngle/(Math.PI/4));
                        xAngle = f*Math.PI/4;
                    }

                    // check the orientation of the X axis
                    //if (drawUtils.drawYZ)  xAngle = -xAngle;
                    var yMat  = Matrix.RotationY( yAngle ),
                        xMat  = Matrix.RotationX( xAngle );

                    mat = glmat4.multiply(yMat, xMat, []);
                }
            }

            if(this._inLocalMode && (this.application.ninja.selectedElements.length === 1 || this.rotateStage) )
            {
//              console.log( "modifyElements: rotateLocally " );
                this._rotateLocally(mat);
            }
            else
            {
                this._rotateGlobally(mat);
            }

//            this.UpdateSelection();
            NJevent("elementChanging", {type : "Changing", redraw: false});
        }
    },

    _rotateLocally: {
        value: function (rotMat) {
            var selectedElements = this.application.ninja.selectedElements;

            if(this.rotateStage) {
                selectedElements = [this.application.ninja.currentDocument.model.documentRoot];
            }
            var len = selectedElements.length;
            for(var i = 0; i < len; i++) {
                var elt = selectedElements[i];
                var curMat = elt.elementModel.getProperty("mat");

                // pre-translate by the transformation center
                var tMat = Matrix.I(4);

                // _startOriginArray is the location of the center of rotation
                // in view space of the element.
                var transformCtr = this._startOriginArray[i];

                tMat[12] = transformCtr[0];
                tMat[13] = transformCtr[1];
                tMat[14] = transformCtr[2];

                var mat = glmat4.multiply(curMat, tMat, []);

                // translate back
                tMat[12] = -transformCtr[0];
                tMat[13] = -transformCtr[1];
                tMat[14] = -transformCtr[2];

                glmat4.multiply(mat, rotMat, mat);


                glmat4.multiply(mat, tMat, mat);

                // while moving, set inline style to improve performance
                viewUtils.setMatrixForElement( elt, mat, true );
            }
        }
    },

    _rotateGlobally: {
        value: function (rotMat) {
            var len = this.application.ninja.selectedElements.length;
            for(var i = 0; i < len; i++) {
                var elt = this.application.ninja.selectedElements[i].elementModel.getProperty("elt");
                var curMat = this.application.ninja.selectedElements[i].elementModel.getProperty("mat");

                // pre-translate by the transformation center
                var tMat = Matrix.I(4);

                var transformCtr = this._startOriginArray[i].slice(0);
                transformCtr = MathUtils.transformPoint(transformCtr, curMat);

//              console.log( "modifyElements: rotateGlobally, ctr: " + transformCtr );

                tMat[12] = transformCtr[0];
                tMat[13] = transformCtr[1];
                tMat[14] = transformCtr[2];

                var mat = glmat4.multiply(tMat, rotMat, []);

                // translate back
                tMat[12] = -transformCtr[0];
                tMat[13] = -transformCtr[1];
                tMat[14] = -transformCtr[2];

                glmat4.multiply(mat, tMat, mat);

                glmat4.multiply(mat, curMat, mat);

                viewUtils.setMatrixForElement(this.application.ninja.selectedElements[i], mat, true );
            }
        }
    },

    _getAngleToRotate: {
        value: function (x, y) {
            var angle;
            var pt = MathUtils.getLocalPoint(x, y, this._planeEq, this._matL);
            if(!pt)
            {
                //TODO - should this be _startPoint.x/y instead of downPoint.x/y?
                var st = [this.downPoint.x, this.downPoint.y];
                pt = [x, y];
                var sub = vecUtils.vecSubtract(2, pt, st);
                var dot = vecUtils.vecDot(2, sub, this._dirVecL);

                angle = vecUtils.vecDist(2, pt, st) * 0.1;

                if (dot < 0)
                {
                    angle = -angle;
                }
            }
            else
            {
                angle = MathUtils.getAngleBetweenPoints(this._startPoint, pt);
            }
            return angle;
        }
    },

    captureSelectionChange: {
        value: function(event){
            this.eventManager.addEventListener("selectionDrawn", this, true);
        }
    },

    captureSelectionDrawn: {
        value: function(event){
            this._origin = null;
            this._startOriginArray = null;

            var len = this.application.ninja.selectedElements.length;
            if(len)
            {
                if(len === 1)
                {
                    this.target = this.application.ninja.selectedElements[0];

                    viewUtils.pushViewportObj( this.target );
                    var eltCtr = viewUtils.getCenterOfProjection();
                    eltCtr[2] = 0;
                    viewUtils.popViewportObj();

                    var ctrOffset = this.target.elementModel.props3D.m_transformCtr;
                    if(ctrOffset)
                    {
                        eltCtr[2] = 0;
                        eltCtr = vecUtils.vecAdd(3, eltCtr, ctrOffset);
                    }

                    this._origin = viewUtils.localToGlobal(eltCtr, this.target);
                    //console.log( "Rotate3DToolBase.captureSelectionDrawn _origin: " + this._origin );
                    this._updateTargets();
                    this._setTransformOrigin(false);
                }
                else
                {
                    this.target = this.application.ninja.currentDocument.model.documentRoot;
                    //this._origin = drawUtils._selectionCtr.slice(0);
                    //this._origin[0] += this.application.ninja.stage.userContentLeft;
                    //this._origin[1] += this.application.ninja.stage.userContentTop;
                    this._updateTargets();
                    this._origin = this.calculateMultiSelOrigin();
                    this._setTransformOrigin(true);
                }
            }
            else
            {
                this.target = null;
            }
            this.DrawHandles();

            if(event)
            {
                this.eventManager.removeEventListener("selectionDrawn", this, true);
            }
        }
    },

    /*
    _updateHandlesOrigin: {
        value: function () { }
    },
    */

    _updateTargets: {
        value: function(addToUndo) {
            var mod3dObject = [],
                self = this;

            this.application.ninja.selectedElements.forEach(function(element) {
                var curMat = viewUtils.getMatrixFromElement(element);
                var curMatInv = glmat4.inverse(curMat, []);

                viewUtils.pushViewportObj( element );
                var eltCtr = viewUtils.getCenterOfProjection();
                viewUtils.popViewportObj();

                // cache the local to global and global to local matrices
                var l2gMat = viewUtils.getLocalToGlobalMatrix( element );
                var g2lMat = glmat4.inverse( l2gMat, [] );
                eltCtr = MathUtils.transformAndDivideHomogeneousPoint( eltCtr, l2gMat );

                element.elementModel.setProperty("ctr", eltCtr);
                element.elementModel.setProperty("mat", curMat);
                element.elementModel.setProperty("matInv", curMatInv);
                element.elementModel.setProperty("l2g", l2gMat);
                element.elementModel.setProperty("g2l", g2lMat);

                if(addToUndo) {
                    var previousMat = element.elementModel.getProperty("mat").slice(0);
                    var previousStyleStr = {dist:element.elementModel.getProperty("dist"), mat:MathUtils.scientificToDecimal(previousMat, 5)};
                    var newStyleStr = {dist:viewUtils.getPerspectiveDistFromElement(element), mat:MathUtils.scientificToDecimal(viewUtils.getMatrixFromElement(element), 5)};

                    mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr});
                }

            });

            if(addToUndo) {
                ElementsMediator.set3DProperties(mod3dObject, "Change", "rotateTool");
            }

            this.application.ninja.selectedElements.forEach(function(element) {
                element.elementModel.setProperty("mat", viewUtils.getMatrixFromElement(element));
                element.elementModel.setProperty("matInv", glmat4.inverse(element.elementModel.getProperty("mat"), []));
                element.elementModel.setProperty("dist", viewUtils.getPerspectiveDistFromElement(element));
            });
        }
    },

    calculateMultiSelOrigin:
    {
        value: function()
        {
            var minPt,  maxPt, i,j;
            this._startOriginArray = [];
            var len = this.application.ninja.selectedElements.length;
            for (i = 0; i < len; i++)
            {
                // get the next element and localToGlobal matrix
                var elt = this.application.ninja.selectedElements[i];
                var l2g = elt.elementModel.getProperty("l2g");

                // get the element bounds in 'plane' space
                var bounds = viewUtils.getElementViewBounds3D( elt );
                for (j=0;  j<4;  j++)
                {
                    var localPt = bounds[j];
                    //var pt = MathUtils.transformAndDivideHomogeneousPoint( localPt, l2g );
                    var pt = viewUtils.localToStageWorld( localPt, elt );
                    if (!minPt)
                    {
                        minPt = pt.slice();
                        maxPt = pt.slice();
                    }
                    else
                    {
                        minPt[0] = Math.min(minPt[0],pt[0]);  minPt[1] = Math.min(minPt[1],pt[1]);  minPt[2] = Math.min(minPt[2],pt[2]);
                        maxPt[0] = Math.max(maxPt[0],pt[0]);  maxPt[1] = Math.max(maxPt[1],pt[1]);  maxPt[2] = Math.max(maxPt[2],pt[2]);
                    }
                }
            }
            var stageWorldCtr = [ 0.5*(minPt[0] + maxPt[0]),  0.5*(minPt[1] + maxPt[1]), 0.5*(minPt[2] + maxPt[2]) ];
            var globalCtr = MathUtils.transformAndDivideHomogeneousPoint( stageWorldCtr, viewUtils.getStageWorldToGlobalMatrix() );
//          console.log( "resetting _origin to: " + this._origin );

            return globalCtr;
        }
    },

    _setTransformOrigin: {
        value: function(shouldUpdateCenter) {
            if(!this._origin) {
                return;
            }

            var elt,
                element,
                eltCtr,
                ctrOffset,
                matInv;

            if(this.rotateStage || (this.application.ninja.selectedElements.length === 1)) {
                if(this.rotateStage) {
                    element = this.application.ninja.currentDocument.model.documentRoot;
                } else {
                    element = this.application.ninja.selectedElements[0];
                }

                if(shouldUpdateCenter) {

//                    eltCtr = element.elementModel.getProperty("ctr");
//                    ctrOffset = vecUtils.vecSubtract(3, this._origin, eltCtr);

//                    matInv = element.elementModel.getProperty("matInv");
//                    ctrOffset = MathUtils.transformVector(ctrOffset, matInv);

                    element.elementModel.props3D.m_transformCtr = this._startOriginArray[0].slice();
                } else {
                    this._startOriginArray = [];
                    ctrOffset = element.elementModel.props3D.m_transformCtr;
                    if(!ctrOffset) {
                        ctrOffset = [0,0,0];
                    }
                    this._startOriginArray[0] = ctrOffset;
                }
            } else {
               /*
                this._startOriginArray = [];
                var len = this.application.ninja.selectedElements.length;
                for (var i = 0; i < len; i++) {
                    eltCtr = this.application.ninja.selectedElements[i].elementModel.getProperty("ctr");
                    ctrOffset = vecUtils.vecSubtract(3, this._origin, eltCtr);
                    matInv = this.application.ninja.selectedElements[i].elementModel.getProperty("matInv");
                    ctrOffset = MathUtils.transformVector(ctrOffset, matInv);
                    this._startOriginArray[i] = ctrOffset;
                }
                */

                // Update transform ctr for all elements if transform origin was modified
                if (!this._origin)  this._origin = this.calculateMultiSelOrigin();
                var globalCtr = this._origin;
                var len = this.application.ninja.selectedElements.length;
                for (var i=0;  i<len;  i++)
                {
                    // get the next element and localToGlobal matrix
                    elt = this.application.ninja.selectedElements[i];
                    var l2g = viewUtils.getLocalToGlobalMatrix( elt );
                    var g2l = glmat4.inverse( l2g, [] );
                    elt.elementModel.setProperty("g2l", g2l);
                    elt.elementModel.setProperty("l2g", l2g);

                    var localCtr = MathUtils.transformAndDivideHomogeneousPoint(globalCtr,  g2l);
                    viewUtils.pushViewportObj( elt );
                    localCtr = viewUtils.screenToView( localCtr[0],  localCtr[1],  localCtr[2] );
                    viewUtils.popViewportObj();

                    this._startOriginArray[i] = localCtr;
            }
        }
        }
    },

    HandleDoubleClick: {
        value: function () {

            if(!this._target)
            {
                return;
            }

            if(this._activateOriginHandle)
            {
                var len = this.application.ninja.selectedElements.length;
                if( (len === 1) || (this._toolID === "rotateStage3DTool") )
                {
                    this._target.elementModel.props3D.m_transformCtr = null;
                }

                this._handleMode = null;
                this._activateOriginHandle = false;
                this.application.ninja.stage.drawingCanvas.style.cursor = this._cursor;

                this.captureSelectionDrawn(null);
            }
        }
    },

    Reset: {
        value: function() {
            var mat,
                iMat,
                dist,
                mod3dObject = [],
                self = this;

            this.application.ninja.selectedElements.forEach(function(element) {
                // Reset to the identity matrix
                iMat = Matrix.I(4);
                mat = ElementsMediator.getMatrix(element);
//                iMat[12] = mat[12];
//                iMat[13] = mat[13];
//                iMat[14] = mat[14];

                dist = ElementsMediator.getPerspectiveDist(element);

                var previousStyleStr = {dist:dist, mat:mat};
                var newStyleStr = {dist:dist, mat:iMat};

                mod3dObject.push({element:element, properties:newStyleStr, previousProperties: previousStyleStr});
            });

            ElementsMediator.set3DProperties(mod3dObject, "Change", "rotateTool");

            this.isDrawing = false;
            this.endDraw(event);

            // Need to force stage to draw immediately so the new selection center is calculated
            this.application.ninja.stage.draw();
            // And captureSelectionDrawn to draw the transform handles
            this.captureSelectionDrawn(null);
        }
    },

        /**
    * SHIFT/ALT/SPACE Key Handlers
    */
    HandleShiftKeyDown: {
        value: function (event) {
        }
    },

    HandleShiftKeyUp: {
        value: function () {
        }
    },

    HandleSpaceKeyDown: {
        value: function () {
        }
    },

    HandleSpaceUp: {
        value: function () {
        }
    },

    HandleAltKeyDown: {
        value: function(event) {
            this._inLocalMode = !this._inLocalMode;
            this.DrawHandles();
        }
    },

    HandleAltKeyUp: {
        value: function(event) {
            this._inLocalMode = !this._inLocalMode;
            this.DrawHandles();
        }
    },

    handleScroll: {
        value: function(event) {
            this.captureSelectionDrawn(null);
        }
    },

    /**
     * This function is for specifying custom feedback routine
     * upon mouse over.
     * For example, the drawing tools will add a glow when mousing
     * over existing canvas elements to signal to the user that
     * the drawing operation will act on the targeted canvas.
     */
    _showFeedbackOnMouseMove : {
        value: function (event) {
            this._canSnap = false;
            if(this._target && this._handles)