aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/geom')
-rwxr-xr-xjs/lib/geom/circle.js61
-rwxr-xr-xjs/lib/geom/geom-obj.js59
-rwxr-xr-xjs/lib/geom/rectangle.js40
3 files changed, 142 insertions, 18 deletions
diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js
index 218dcfa6..0f1f49a9 100755
--- a/js/lib/geom/circle.js
+++ b/js/lib/geom/circle.js
@@ -4,9 +4,12 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 7var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; 8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive;
9var MaterialsModel = require("js/models/materials-model").MaterialsModel; 9var MaterialsModel = require("js/models/materials-model").MaterialsModel;
10var drawUtils = require("js/helper-classes/3D/draw-utils").DrawUtils;
11var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils;
12
10/////////////////////////////////////////////////////////////////////// 13///////////////////////////////////////////////////////////////////////
11// Class GLCircle 14// Class GLCircle
12// GL representation of a circle. 15// GL representation of a circle.
@@ -46,6 +49,10 @@ var Circle = function GLCircle() {
46 this._fillColor = fillColor; 49 this._fillColor = fillColor;
47 50
48 this._strokeStyle = strokeStyle; 51 this._strokeStyle = strokeStyle;
52
53 this._matrix = Matrix.I(4);
54 //this._matrix[12] = xOffset;
55 //this._matrix[13] = yOffset;
49 } 56 }
50 57
51 this.m_world = world; 58 this.m_world = world;
@@ -187,7 +194,7 @@ var Circle = function GLCircle() {
187 // get the normalized device coordinates (NDC) for 194 // get the normalized device coordinates (NDC) for
188 // all position and dimensions. 195 // all position and dimensions.
189 var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); 196 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
190 var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, 197 var xNDC = 2*this._xOffset/vpw, yNDC = -2*this._yOffset/vph,
191 xRadNDC = this._width/vpw, yRadNDC = this._height/vph, 198 xRadNDC = this._width/vpw, yRadNDC = this._height/vph,
192 xStrokeNDC = 2*this._strokeWidth/vpw, yStrokeNDC = 2*this._strokeWidth/vph, 199 xStrokeNDC = 2*this._strokeWidth/vpw, yStrokeNDC = 2*this._strokeWidth/vph,
193 xInnRadNDC = this._innerRadius*xRadNDC, yInnRadNDC = this._innerRadius*yRadNDC; 200 xInnRadNDC = this._innerRadius*xRadNDC, yInnRadNDC = this._innerRadius*yRadNDC;
@@ -706,6 +713,49 @@ var Circle = function GLCircle() {
706 return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0); 713 return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0);
707 }; 714 };
708 715
716 this.getNearPoint = function( pt, dir )
717 {
718 var world = this.getWorld();
719 if (!world) throw( "null world in getNearPoint" );
720
721 // the input point and direction are in GL space
722 // project to the z == 0 plane
723 var mat = this.getMatrix();
724 var plane = [0,0,1,0];
725 plane = MathUtils.transformPlane( plane, mat );
726 var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane );
727
728 // get the center of the circle in GL space
729 var ctr = this.getGLCenter();
730
731 // transform the projected point to the plane of the circle
732 var planePt = MathUtils.transformPoint( projPt, mat );
733
734 // get a matrix mapping the circle to a 2D coordinate system
735 var normal = [ mat[8], mat[9], mat[10] ];
736 var planeMat = drawUtils.getPlaneToWorldMatrix(normal, ctr);
737 var planeMatInv = glmat4.inverse( planeMat, [] );
738 var planePt2D = MathUtils.transformPoint( planePt, planeMatInv );
739
740 // get 2 points on the axes of the oval
741 var wPt = this.preViewToGL( [this._xOffset + 0.5*this.getWidth(), this._yOffset, 0] ),
742 hPt = this.preViewToGL( [this._xOffset, this._yOffset + 0.5*this.getHeight(), 0] );
743 var w = vecUtils.vecDist( 2, wPt, ctr ),
744 h = vecUtils.vecDist( 2, hPt, ctr );
745 var aspect = w/h;
746
747 // get the angle of the projected point relative to the circle
748 var angle = Math.atan2( planePt2D[1], planePt2D[0]/aspect );
749 var degrees = angle*180.0/Math.PI;
750
751 // get the corresponding point on the object
752 var pt = [ Math.cos(angle)*w, Math.sin(angle)*h, 0 ];
753 var glPt = MathUtils.transformPoint( pt, planeMat );
754
755 return glPt;
756 }
757
758 /*
709 this.getNearPoint = function( pt, dir ) { 759 this.getNearPoint = function( pt, dir ) {
710 var world = this.getWorld(); 760 var world = this.getWorld();
711 if (!world) throw( "null world in getNearPoint" ); 761 if (!world) throw( "null world in getNearPoint" );
@@ -725,7 +775,7 @@ var Circle = function GLCircle() {
725 // get the normalized device coordinates (NDC) for 775 // get the normalized device coordinates (NDC) for
726 // the position and radii. 776 // the position and radii.
727 var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); 777 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
728 var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, 778 var xNDC = 2*this._xOffset/vpw, yNDC = -2*this._yOffset/vph,
729 xRadNDC = this._width/vpw, yRadNDC = this._height/vph; 779 xRadNDC = this._width/vpw, yRadNDC = this._height/vph;
730 var projMat = world.makePerspectiveMatrix(); 780 var projMat = world.makePerspectiveMatrix();
731 var z = -world.getViewDistance(); 781 var z = -world.getViewDistance();
@@ -755,6 +805,7 @@ var Circle = function GLCircle() {
755 805
756 return objPt; 806 return objPt;
757 }; 807 };
808 */
758 809
759 this.recalcTexMapCoords = function( vrts, uvs ) { 810 this.recalcTexMapCoords = function( vrts, uvs ) {
760 var n = vrts.length/3; 811 var n = vrts.length/3;
diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js
index 2f5559a7..4efa5d60 100755
--- a/js/lib/geom/geom-obj.js
+++ b/js/lib/geom/geom-obj.js
@@ -415,6 +415,65 @@ var GeomObj = function GLGeomObj()
415 } 415 }
416 }; 416 };
417 417
418
419 this.getGLCenter = function()
420 {
421 // get the normalized device coordinates (NDC) for
422 // all position and dimensions.
423 var world = this.getWorld();
424 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
425 var xNDC = 2*this._xOffset/vpw, yNDC = -2*this._yOffset/vph;
426
427 var aspect = world.getAspect();
428 var zn = world.getZNear(), zf = world.getZFar();
429 var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
430 b = -t,
431 r = aspect*t,
432 l = -r;
433
434 // calculate the object coordinates from their NDC coordinates
435 var z = -world.getViewDistance();
436
437 // unproject to get the position of the origin in GL
438 var x = -z*(r-l)/(2.0*zn)*xNDC,
439 y = -z*(t-b)/(2.0*zn)*yNDC;
440 z = 0.0;
441
442 // transform by the object's transformation matrix
443 var ctr = MathUtils.transformPoint( [x, y, z], this.getMatrix() );
444
445 return ctr;
446 };
447
448 this.preViewToGL = function( preViewPt )
449 {
450 // get the normalized device coordinates (NDC) for
451 // all position and dimensions.
452 var world = this.getWorld();
453 var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
454 var xNDC = 2*preViewPt[0]/vpw, yNDC = -2*preViewPt[1]/vph;
455
456 var aspect = world.getAspect();
457 var zn = world.getZNear(), zf = world.getZFar();
458 var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
459 b = -t,
460 r = aspect*t,
461 l = -r;
462
463 // calculate the object coordinates from their NDC coordinates
464 var z = -world.getViewDistance();
465
466 // unproject to get the position of the origin in GL
467 var x = -z*(r-l)/(2.0*zn)*xNDC,
468 y = -z*(t-b)/(2.0*zn)*yNDC;
469 z = 0.0;
470
471 // transform by the object's transformation matrix
472 var glPt = MathUtils.transformPoint( [x, y, z], this.getMatrix() );
473
474 return glPt;
475 };
476
418 this.buildBuffers = function () { 477 this.buildBuffers = function () {
419 // this function must be overridden by the base class 478 // this function must be overridden by the base class
420 alert("GLGeomObj.buildBuffers must be overridden by base class"); 479 alert("GLGeomObj.buildBuffers must be overridden by base class");
diff --git a/js/lib/geom/rectangle.js b/js/lib/geom/rectangle.js
index 51db0dd0..fc6043d6 100755
--- a/js/lib/geom/rectangle.js
+++ b/js/lib/geom/rectangle.js
@@ -7,6 +7,7 @@
7var GeomObj = require("js/lib/geom/geom-obj").GeomObj; 7var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; 8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive;
9var MaterialsModel = require("js/models/materials-model").MaterialsModel; 9var MaterialsModel = require("js/models/materials-model").MaterialsModel;
10
10 /////////////////////////////////////////////////////////////////////// 11 ///////////////////////////////////////////////////////////////////////
11// Class GLRectangle 12// Class GLRectangle
12// GL representation of a rectangle. 13// GL representation of a rectangle.
@@ -58,6 +59,10 @@ var Rectangle = function GLRectangle()
58 this.setBRRadius(brRadius); 59 this.setBRRadius(brRadius);
59 60
60 this._strokeStyle = strokeStyle; 61 this._strokeStyle = strokeStyle;
62
63 this._matrix = Matrix.I(4);
64 //this._matrix[12] = xoffset;
65 //this._matrix[13] = yoffset;
61 } 66 }
62 67
63 // the overall radius includes the fill and the stroke. separate the two based onthe stroke width 68 // the overall radius includes the fill and the stroke. separate the two based onthe stroke width
@@ -377,26 +382,36 @@ var Rectangle = function GLRectangle()
377 if (brRad > minDimen) brRad = minDimen; 382 if (brRad > minDimen) brRad = minDimen;
378 if (trRad > minDimen) trRad = minDimen; 383 if (trRad > minDimen) trRad = minDimen;
379 384
385 var viewUtils = require("js/helper-classes/3D/view-utils").ViewUtils;
386 var world = this.getWorld();
387 viewUtils.pushViewportObj( world.getCanvas() );
388 var cop = viewUtils.getCenterOfProjection();
389 viewUtils.popViewportObj();
390 var xCtr = cop[0] + this._xOffset, yCtr = cop[1] - this._yOffset;
391 var xLeft = xCtr - 0.5*this.getWidth(), yTop = yCtr - 0.5*this.getHeight();
392 var xDist = cop[0] - xLeft, yDist = cop[1] - yTop;
393 var xOff = 0.5*world.getViewportWidth() - xDist, yOff = 0.5*world.getViewportHeight() - yDist;
394
380 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) { 395 if ((tlRad <= 0) && (blRad <= 0) && (brRad <= 0) && (trRad <= 0)) {
381 ctx.rect(pt[0], pt[1], width - 2*inset, height - 2*inset); 396 ctx.rect(pt[0]+xOff, pt[1]+yOff, width - 2*inset, height - 2*inset);