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
|
/* <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> */
var Montage = require("montage/core/core").Montage;
var Component = require("montage/ui/component").Component;
var snapManager = require("js/helper-classes/3D/snap-manager").SnapManager;
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;
exports.DrawingToolBase = Montage.create(Component, {
dragPlane:
{
get: function () {
return this.application.ninja.toolsData.selectedToolInstance._dragPlane;
},
set: function (value) {
this.application.ninja.toolsData.selectedToolInstance._dragPlane = value;
}
},
/**
* Used on the initial MouseDown for Drawing Tools
*
* Returns: An array containing:
* 0 - HitRec point
* 1 - X value converted to screen point
* 2 - Y value converted to screen point
*/
getInitialSnapPoint: {
value: function(x, y, shapeCanvas)
{
// update the snap settings
snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() );
snapManager.enableElementSnap( snapManager.elementSnapEnabledAppLevel() );
snapManager.enableGridSnap( snapManager.gridSnapEnabledAppLevel() );
// do the snap
this.dragPlane = null;
var hitRec = snapManager.snap(x, y, true);
if (hitRec) {
if (shapeCanvas)
{
this.dragPlane = viewUtils.getUnprojectedElementPlane( shapeCanvas );
snapManager.setupDragPlaneFromPlane( this.dragPlane );
}
else
{
this.dragPlane = snapManager.setupDragPlanes( hitRec, true );
}
// console.log( "drag plane: " + this.dragPlane );
var wpHitRec = hitRec.convertToWorkingPlane( this.dragPlane );
var pt = hitRec.getScreenPoint();
return( [wpHitRec, pt[0], pt[1]] );
}
}
},
/**
* Used on the Mouse Move to calculate new snap point.
*/
getUpdatedSnapPoint: {
value: function(x,y, snap3d, downHitRec) {
// update the snap settings
snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() );
snapManager.enableElementSnap( snapManager.elementSnapEnabledAppLevel() );
snapManager.enableGridSnap( snapManager.gridSnapEnabledAppLevel() );
var hitRec = snapManager.snap(x, y, snap3d );
if (hitRec) {
// if ((hitRec.getType() !== hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) {
// hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() );
// }
//
// if(downHitRec !== null) {
// // if we are working off-plane, do a snap to the projected locations of the geometry
// var thePlane = workingPlane;
// if (snapManager.hasDragPlane())
// {
// thePlane = snapManager.getDragPlane();
// }
//
// // Return the up HitRec
// return hitRec;
// } else {
// return null;
// }
if(downHitRec) {
hitRec = hitRec.convertToWorkingPlane(this.dragPlane || downHitRec.getPlane());
} else if ((hitRec.getType() !== hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) {
hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() );
}
}
return hitRec;
}
},
getUpdatedSnapPointNoAppLevelEnabling: {
value: function(x,y, snap3d, downHitRec) {
// do the first snap
var hitRec = snapManager.snap(x, y, snap3d );
if (hitRec) {
if ((hitRec.getType() !== hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) {
hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() );
}
if(downHitRec !== null) {
// if we are working off-plane, do a snap to the projected locations of the geometry
var thePlane = workingPlane;
if (snapManager.hasDragPlane())
{
thePlane = snapManager.getDragPlane();
}
// Return the up HitRec
return hitRec;
} else {
return null;
}
}
}
},
setDownHitRec: {
value: function (x, y, do3DSnap) {
var hitRec = snapManager.snap(x, y, do3DSnap );
if (hitRec) {
if ((hitRec.getType() != hitRec.SNAP_TYPE_STAGE) && !hitRec.isSomeGridTypeSnap()) {
//hitRec = hitRec.convertToWorkingPlane( workingPlane );
snapManager.setupDragPlanes(hitRec);
hitRec = hitRec.convertToWorkingPlane( snapManager.getDragPlane() );
}
return hitRec;
}
}
},
drawSnapLastHit: {
value: function() {
snapManager.drawLastHit();
}
},
getHitRecPos: {
value: function (hitRec) {
if (!hitRec)
return null;
// get the hit rec. points in plane space
var psPos = hitRec.getLocalPoint();
var stageOffset = viewUtils.getElementOffset(this.application.ninja.currentDocument.model.documentRoot);
viewUtils.setViewportObj(this.application.ninja.currentDocument.model.documentRoot);
// get the matrix taking the local hit point in plane space
// to world space of whatever element it is in.
var planeMat = hitRec.getPlaneMatrix();
// get the center of the circle in stage world space
var swPos = viewUtils.postViewToStageWorld(MathUtils.transformPoint(psPos, hitRec.getPlaneMatrix()), hitRec.getElt());
//var swPos = hitRec.calculateStageWorldPoint(); todo figure out why we cannot just use this function instead of the above
// the stage world space point is now relative to the center of the 3D space. To
// calculate the left and top offsets, this must be offset by the stage dimensions
swPos[0] += snapManager.getStageWidth() / 2.0;
swPos[1] += snapManager.getStageHeight() / 2.0;
return swPos;
}
},
getCompletePoints: {
value: function(hitRec0, hitRec1) {
if (hitRec0 && hitRec1) {
// get the 2 snap points in plane space
var p0 = hitRec0.getLocalPoint(),
p1 = hitRec1.getLocalPoint();
var stageOffset = viewUtils.getElementOffset(this.application.ninja.currentDocument.model.documentRoot);
viewUtils.setViewportObj(this.application.ninja.currentDocument.model.documentRoot);
// get the matrix taking the local hit point in plane space
// to world space of whatever element it is in.
var planeMat = hitRec0.getPlaneMatrix();
// get the center of the circle in stage world space
var s0 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p0,hitRec0.getPlaneMatrix()), hitRec0.getElt() ),
s1 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p1,hitRec1.getPlaneMatrix()), hitRec1.getElt() );
// apply the projected snap points
var s0Proj = false, s1Proj = false;
// find a "reasonable" plane
var thePlane = workingPlane.slice(0);
if (snapManager.hasDragPlane()) {
thePlane = snapManager.getDragPlane();
}
var d0 = vecUtils.vecDot( 3, thePlane, s0 ) + thePlane[3],
d1 = vecUtils.vecDot( 3, thePlane, s1 ) + thePlane[3];
var sign0 = MathUtils.fpSign( d0 ), sign1 = MathUtils.fpSign( d1 );
if ((sign0 !== 0) || (sign1 !== 0)) {
// we need to pick a different plane
if ( MathUtils.fpCmp(d0,d1) === 0 ) {
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
} else {
var vec = vecUtils.vecSubtract(3, s1, s0 );
var yAxis = [0,1,0];
var tmp = vecUtils.vecCross( 3, vec, yAxis );
var mag = vecUtils.vecMag(3, tmp);
if (MathUtils.fpSign(mag) === 0) {
thePlane = [0,0,1];
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
} else {
var xAxis = vecUtils.vecCross( 3, yAxis, tmp );
thePlane = vecUtils.vecCross( 3, xAxis, yAxis );
vecUtils.vecNormalize(3, thePlane, 1.0 );
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
}
}
// recompute the plane matrix
planeMat = drawUtils.getPlaneToWorldMatrix(thePlane, MathUtils.getPointOnPlane(thePlane));
}
// unproject the bounds
//var planeMatInv = planeMat.inverse();
var planeMatInv = glmat4.inverse( planeMat, [] );
//var midPt = this.unprojectPoints( s0, s1, planeMat, planeMatInv, s1Proj );
// var midPt = this.unprojectPoints( s0, s1, planeMat, planeMatInv, true );
// get the 4 points of the bounding box in 2D space
p0 = MathUtils.transformPoint( s0, planeMatInv );
p1 = MathUtils.transformPoint( s1, planeMatInv );
// determine the midpoint of the oval
//midPt = s0.add(s1);
//midPt = midPt.multiply(0.5);
//midPt = MathUtils.makeDimension3(midPt);
var midPt = vec3.add(s0, s1, []);
midPt = vecUtils.vecScale( 3, midPt, 0.5 );
// the mid point is now relative to the center of the 3D space. To
// calculate the left and top offsets, this must be offset by the stage dimensions
midPt[0] += snapManager.getStageWidth() / 2.0;
midPt[1] += snapManager.getStageHeight() / 2.0;
// calculate the width and height.
var left = p0[0]; var top = p0[1];
var right = p1[0]; var bottom = p1[1];
var w = Math.abs(right - left),
h = Math.abs(bottom - top);
// return ({"width":w, "height":h, "planeMat":planeMat, "midPt":midPt});
var s0Offset = s0.slice(0);
var s1Offset = s1.slice(0);
s0Offset[0] += snapManager.getStageWidth() / 2.0;
s0Offset[1] += snapManager.getStageHeight() / 2.0;
s1Offset[0] += snapManager.getStageWidth() / 2.0;
s1Offset[1] += snapManager.getStageHeight() / 2.0;
return ({ "width": w, "height": h, "planeMat": planeMat, "midPt": midPt, "mouseDownPos": s0Offset, "mouseUpPos": s1Offset });
} else {
return null;
}
}
},
cleanupSnap: {
value: function() {
// set the drag plane to the working plane
snapManager.clear2DCache();
snapManager.setupDragPlaneFromPlane( workingPlane );
// update the snap settings
snapManager.enableSnapAlign( snapManager.snapAlignEnabledAppLevel() );
snapManager.enableElementSnap( snapManager.elementSnapEnabledAppLevel() );
snapManager.enableGridSnap( snapManager.gridSnapEnabledAppLevel() );
}
},
draw2DRectangle: {
value: function(x0, y0, x1, y1) {
var drawingContext = this.application.ninja.stage.drawingContext,
drawingPrefs = this.application.ninja.stage.drawingContextPreferences;
this.application.ninja.stage.clearDrawingCanvas();
//TODO Save and restore state
drawingContext.strokeStyle = drawingPrefs.color;
drawingContext.lineWidth = drawingPrefs.thickness;
drawingContext.strokeRect(x0 - 0.5 ,y0 - 0.5 ,x1 ,y1);
}
},
/**
* Feedback drawing functions
*/
drawRectangle: {
value: function(hitRec0, hitRec1) {
var p0 = hitRec0.getLocalPoint(),
p1 = hitRec1.getLocalPoint();
var stageMat = viewUtils.getMatrixFromElement(this.application.ninja.currentDocument.model.documentRoot);
var elt = hitRec0.getElt();
if (!elt) { elt = hitRec1.getElt(); }
if (!elt) { elt = this.application.ninja.currentDocument.model.documentRoot; }
if (elt)
{
viewUtils.pushViewportObj(elt);
var offset = viewUtils.getElementOffset(elt);
offset[2] = 0;
// check the plane to draw the rectangle on
var s0 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p0,hitRec0.getPlaneMatrix()), hitRec0.getElt() ),
s1 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(p1,hitRec1.getPlaneMatrix()), hitRec1.getElt() );
// find a "reasonable" plane
var planeMat = hitRec0.getPlaneMatrix();
var planeMatInv;
var thePlane = workingPlane.slice(0);
if (snapManager.hasDragPlane())
{
thePlane = snapManager.getDragPlane();
}
var d0 = vecUtils.vecDot( 3, thePlane, s0 ) + thePlane[3],
d1 = vecUtils.vecDot( 3, thePlane, s1 ) + thePlane[3];
var sign0 = MathUtils.fpSign( d0 ), sign1 = MathUtils.fpSign( d1 );
if ((sign0 !== 0) || (sign1 !== 0))
{
// we need to pick a different plane
if ( MathUtils.fpCmp(d0,d1) === 0 ){
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
}
else
{
var vec = vecUtils.vecSubtract(3, s1, s0 );
var yAxis = [0,1,0];
var tmp = vecUtils.vecCross( 3, vec, yAxis );
var mag = vecUtils.vecMag(3, tmp);
if (MathUtils.fpSign(mag) === 0)
{
thePlane = [0,0,1];
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
}
else
{
var xAxis = vecUtils.vecCross( 3, yAxis, tmp );
thePlane = vecUtils.vecCross( 3, xAxis, yAxis );
vecUtils.vecNormalize(3, thePlane, 1.0 );
thePlane[3] = -vecUtils.vecDot(3, thePlane, s0);
}
}
// recompute the plane matrix
planeMat = drawUtils.getPlaneToWorldMatrix(thePlane, MathUtils.getPointOnPlane(thePlane));
//planeMatInv = planeMat.inverse();
planeMatInv = glmat4.inverse( planeMat, [] );
p0 = MathUtils.transformPoint(p0,hitRec0.getPlaneMatrix());
p0 = MathUtils.transformPoint(p0, planeMatInv);
p1 = MathUtils.transformPoint(p1,hitRec1.getPlaneMatrix());
p1 = MathUtils.transformPoint(p1, planeMatInv);
}
else {
//planeMatInv = planeMat.inverse();
planeMatInv = glmat4.inverse( planeMat, [] );
}
// determine if the geometry is going to be projected. If so a second projected rectangle is drawn
var isProjected = ((MathUtils.fpCmp(thePlane[2],1.0) !== 0) || (MathUtils.fpSign(thePlane[3]) !== 0));
// TODO - We no longer need to project drawing after perspective fix. Need to clean up this code.
// For now, just setting isProjected to false so rest of the drawing still works.
isProjected = false;
// get and draw the unprojected object points
var projPtArr = [];
if (isProjected)
{
this.getProjectedObjectPoints( s0, s1, planeMat, planeMatInv, elt, stageMat, offset, projPtArr );
}
var localPt = [p0[0], p0[1], 0.0, 1.0];
s0 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt );
s0 = vecUtils.vecAdd(3, viewUtils.viewToScreen( MathUtils.transformPoint(s0, stageMat) ), offset );
localPt[1] = p1[1];
s1 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt );
s1 = vecUtils.vecAdd(3, viewUtils.viewToScreen( MathUtils.transformPoint(s1, stageMat) ), offset );
localPt[0] = p1[0];
var s2 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt );
s2 = vecUtils.vecAdd(3, viewUtils.viewToScreen( MathUtils.transformPoint(s2, stageMat) ), offset );
localPt[1] = p0[1];
var s3 = viewUtils.postViewToStageWorld( MathUtils.transformPoint(localPt,planeMat), elt );
s3 = vecUtils.vecAdd(3, viewUtils.viewToScreen( MathUtils.transformPoint(s3, stageMat) ), offset );
if ( isProjected)
{
var unprojPtArr = [s0, s1, s2, s3];
this.application.ninja.stage.draw3DProjectedAndUnprojectedRectangles( unprojPtArr, projPtArr );
}
else
{
this.application.ninja.stage.draw3DSelectionRectangle(s0[0], s0[1],
s1[0], s1[1],
s2[0], s2[1],
s3[0], s3[1],
s0[0], s0[1]);
}
viewUtils.popViewportObj();
}
}
},
drawLine: {
value: function (hitRec0, hitRec1, strokeSize, strokeColor) {
var p0 = hitRec0.getScreenPoint(),
p1 = hitRec1.getScreenPoint();
this.application.ninja.stage.drawLine(p0[0], p0[1], p1[0], p1[1], strokeSize, strokeColor);
}
},
/**
* Draw Helper Functions
*/
/**
* Returns a perfect square using the top/left/bottom/right values.
*/
toSquare: {
value: function(x0,x1,y0,y1) {
var dw = 1;
var dh = 1;
var w = x1 - x0,
h = y1 - y0;
if(w < 0) dw = -1;
if(h < 0) dh = -1;
if(Math.abs(w) >= Math.abs(h)) {
h = (Math.abs(w) * dh);
} else {
w = (Math.abs(h) * dw);
}
return [x0,y0,w,h];
}
},
toCenterRectangle: {
value: function(x0,x1,y0,y1) {
var x,y,w,h = 0;
x = x0 - (x1 - x0);
y = y0 - (y1 - y0);
w = x1 - x;
h = y1 - y;
return [x,y,w,h];
}
},
/**
* Helper Functions
*/
unprojectPoints:
{
value: function( s0In, s1In, planeMat, planeMatInv, fixedS1 ) {
var s0 = s0In.slice(0);
var s2 = s1In.slice(0);
var pt0, pt1;
// calculate the mid point of the rectangle
var midPt = vecUtils.vecAdd(3, s0, s2);
vecUtils.vecScale(3, midPt, 0.5);
s0[0] -= midPt[0]; s0[1] -= midPt[1];
s2[0] -= midPt[0]; s2[1] -= midPt[1];
// convert the 2 world space points to plane space
var p0 = MathUtils.transformPoint( s0, planeMatInv ),
p2 = MathUtils.transformPoint( s2, planeMatInv );
var z = p0[2];
// fill in the other 2 points on the plane to complete the 4 points
var p1 = [p0[0], p2[1], z],
p3 = [p2[0], p0[1], z];
// convert back to 3D space
s0 = MathUtils.transformPoint( p0, planeMat );
var s1 = MathUtils.transformPoint( p1, planeMat );
s2 = MathUtils.transformPoint( p2, planeMat );
var s3 = MathUtils.transformPoint( p3, planeMat );
// unproject the 4 points
var i;
var p = 1400;
var ptArr = [ s0, s1, s2, s3 ];
for (i=0; i<4; i++)
{
pt = ptArr[i];
if (MathUtils.fpCmp(p,-pt[2]) !== 0){
z = pt[2]*p/(p + pt[2]);
var x = pt[0]*(p - z)/p,
y = pt[1]*(p - z)/p;
pt[0] = x; pt[1] = y; pt[2] = z;
}
}
// back to 2D space...
for (i=0; i<4; i++)
ptArr[i] = MathUtils.transformPoint( ptArr[i], planeMatInv );
// find the 2 diagonal points to use
if (fixedS1)
{
p0 = ptArr[0]; p1 = ptArr[1]; p2 = ptArr[2]; p3 = ptArr[3];
pt0 = p0.slice(0); pt1 = p2.slice(0);
z = pt0[2];
}
else
{
p0 = ptArr[0]; p1 = ptArr[1]; p2 = ptArr[2]; p3 = ptArr[3];
z = p0[2];
pt0 = p0.slice(0);
pt1 = p2.slice(0);
if (p0[0] < p2[0]){
pt0[0] = Math.max(p0[0],p1[0]);
pt1[0] = Math.min(p2[0],p3[0]);
}
else {
pt0[0] = Math.min(p0[0],p1[0]);
pt1[0] = Math.max(p2[0],p3[0]);
}
if (p0[1] < p2[1]){
pt0[1] = Math.max(p0[1],p3[1]);
pt1[1] = Math.min(p1[1],p2[1]);
}
else {
pt0[1] = Math.min(p0[1],p3[1]);
pt1[1] = Math.max(p1[1],p2[1]);
}
}
pt0[2] = z; pt1[2] = z;
var ctr = vecUtils.vecAdd(3, pt0, pt1);
vecUtils.vecScale( 3, ctr, 0.5 );
// put the diagonal points back in 3D space
s0 = MathUtils.transformPoint( pt0, planeMat );
s1 = MathUtils.transformPoint( pt1, planeMat );
ctr = MathUtils.transformPoint( ctr, planeMat );
// add the translation back in
s0[0] += midPt[0]; s0[1] += midPt[1];
s1[0] += midPt[0]; s1[1] += midPt[1];
ctr[0] += midPt[0]; ctr[1] += midPt[1];
// set the returned values
s0In[0] = s0[0]; s0In[1] = s0[1]; s0In[2] = s0[2];
s1In[0] = s1[0]; s1In[1] = s1[1]; s1In[2] = s1[2];
return ctr;
}
},
getProjectedObjectPoints:
{
value: function( u0, u1, planeMat, planeMatInv, elt, stageMat, offset, rtnScrPts ){
var s0 = u0.slice(0);
var s2 = u1.slice(0);
var i, z;
// calculate the mid point of the rectangle
var midPt = vecUtils.vecAdd(3, s0, s2);
vecUtils.vecScale(3, midPt, 0.5);
s0[0] -= midPt[0]; s0[1] -= midPt[1];
s2[0] -= midPt[0]; s2[1] -= midPt[1];
// unproject the 2 points
var p = 1400;
var ptArr = [ s0, s2 ];
for (i=0; i<2; i++)
{
pt = ptArr[i];
if (MathUtils.fpCmp(p,-pt[2]) !== 0) {
z = pt[2]*p/(p + pt[2]);
var x = pt[0]*(p - z)/p,
y = pt[1]*(p - z)/p;
y = pt[1]*(p - z)/p;
pt[0] = x; pt[1] = y; pt[2] = z;
}
}
// back to 2D space...
for (i=0; i<2; i++)
ptArr[i] = MathUtils.transformPoint( ptArr[i], planeMatInv );
// fill in the other 2 points on the plane to complete the 4 points
var pt0 = ptArr[0], pt2 = ptArr[1];
z = pt0[2];
pt0[2] = z; pt2[2] = z;
var pt1 = [pt0[0], pt2[1], z],
pt3 = [pt2[0], pt0[1], z];
//
ptArr = [pt0, pt1, pt2, pt3];
var pt;
var dist, scale, pDist = 1400; // elt.webkitTransform is not always defined.
for (i=0; i<4; i++)
{
// put the point back in 3D space
pt = MathUtils.transformPoint( ptArr[i], planeMat );
// apply the perspective
dist = pDist - pt[2];
if (MathUtils.fpSign(dist) !== 0)
{
scale = pDist / dist;
pt[0] *= scale;
pt[1] *= scale;
pt[2] *= scale;
}
// add the translation back in
pt[0] += midPt[0]; pt[1] += midPt[1];
// to screen coordinates
pt = viewUtils.postViewToStageWorld( pt, elt );
pt = vecUtils.vecAdd(3, viewUtils.viewToScreen( MathUtils.transformPoint(pt, stageMat) ), offset );
// save the final result
rtnScrPts[i] = pt;
}
}
}
});
|