From 9d122c5f1632b1225a543049913c36b9a030110c Mon Sep 17 00:00:00 2001 From: hwc487 Date: Mon, 6 Feb 2012 15:50:07 -0800 Subject: Enhanced the overlap test for the selection tool marquee select. --- js/helper-classes/3D/math-utils.js | 97 +++++++++++++++++++++++++++++++++++- js/helper-classes/3D/snap-manager.js | 6 --- 2 files changed, 96 insertions(+), 7 deletions(-) (limited to 'js/helper-classes/3D') diff --git a/js/helper-classes/3D/math-utils.js b/js/helper-classes/3D/math-utils.js index 71ed62a0..58f0680a 100644 --- a/js/helper-classes/3D/math-utils.js +++ b/js/helper-classes/3D/math-utils.js @@ -8,7 +8,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot // Class Utils // Math Utility functions /////////////////////////////////////////////////////////////////////// -var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; +var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils, + ViewUtils = require("js/helper-classes/3D/view-utils").ViewUtils, + Rectangle = require("js/helper-classes/3D/rectangle").Rectangle; var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { /////////////////////////////////////////////////////////////////////// @@ -536,6 +538,99 @@ var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { } }, + rectsOverlap: + { + value: function( pt, width, height, elt ) + { + // only consider rectangles with non-zero area + if ((width == 0) || (height == 0)) return false; + + // get the mins/maxs of the onput rectangle + var xMin, xMax, yMin, yMax; + if (width > 0) { xMin = pt[0]; xMax = pt[0] + width; } + else { xMax = pt[0]; xMin = pt[0] + width; } + if (height > 0) { yMin = pt[1]; yMax = pt[1] + height; } + else { yMax = pt[1]; yMin = pt[1] + height; } + + // get the bounds of the element in global screen space + var bounds = ViewUtils.getElementViewBounds3D( elt ); + var bounds3D = []; + for (var i=0; i<4; i++) + bounds3D[i] = ViewUtils.localToGlobal( bounds[i], elt ); + + // get the min/maxs for the element + var xMinElt = bounds3D[0][0], xMaxElt = bounds3D[0][0], + yMinElt = bounds3D[0][1], yMaxElt = bounds3D[0][1]; + for (var i=1; i<4; i++) + { + if (bounds3D[i][0] < xMinElt) xMinElt = bounds3D[i][0]; + else if (bounds3D[i][0] > xMaxElt) xMaxElt = bounds3D[i][0]; + if (bounds3D[i][1] < yMinElt) yMinElt = bounds3D[i][1]; + else if (bounds3D[i][1] > yMaxElt) yMaxElt = bounds3D[i][1]; + } + + // test 1. Overall bounding box test + if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax)) + return false; + + // test 2. See if any of the corners of the element are contained in the rectangle + var rect = Object.create(Rectangle, {}); + rect.set( pt[0], pt[1], width, height ); + for (var i=0; i<4; i++) + { + if (rect.contains( bounds3D[i][0], bounds3D[i][1] )) return true; + } + + // test 3. Bounding box tests on individual edges of the element + for (var i=0; i<4; i++) + { + var pt0 = bounds3D[i], + pt1 = bounds3D[(i+1)%4]; + + // get the extremes of the edge + if (pt0[0] < pt1[0]) { xMinElt = pt0[0]; xMaxElt = pt1[0]; } + else { xMaxElt = pt0[0]; xMinElt = pt1[0]; } + if (pt0[1] < pt1[1]) { yMinElt = pt0[1]; yMaxElt = pt1[1]; } + else { yMaxElt = pt0[1]; yMinElt = pt1[1]; } + + if ((xMaxElt < xMin) || (xMinElt > xMax) || (yMaxElt < yMin) || (yMinElt > yMax)) + continue; + else + { + // intersect the element edge with the 4 sides of the rectangle + // vertical edges + var xRect = xMin; + for (var j=0; j<2; j++) + { + if ((xMinElt < xRect) && (xMaxElt > xRect)) + { + var t = (xRect - pt0[0])/(pt1[0] - pt0[0]); + var y = pt0[1] + t*(pt1[1] - pt0[1]); + if ((y >= yMin) && (y <= yMax)) return true; + } + xRect = xMax; + } + + // horizontal edges + var yRect = yMin; + for (var j=0; j<2; j++) + { + if ((yMinElt < yRect) && (yMaxElt > yRect)) + { + var t = (yRect - pt0[1])/(pt1[1] - pt0[1]); + var x = pt0[0] + t*(pt1[0] - pt0[0]); + if ((x >= xMin) && (x <= xMax)) return true; + } + yRect = yMax; + } + } + } + + // if we get here there is no overlap + return false; + } + }, + /////////////////////////////////////////////////////////////////////// // Bezier Methods /////////////////////////////////////////////////////////////////////// diff --git a/js/helper-classes/3D/snap-manager.js b/js/helper-classes/3D/snap-manager.js index 7e1260bf..f3e8b823 100644 --- a/js/helper-classes/3D/snap-manager.js +++ b/js/helper-classes/3D/snap-manager.js @@ -971,7 +971,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { value: function( screenPt, hitRecs ) { // start at the stage. var stage = this.getStage(); - //var stagePt = viewUtils.parentToChild( screenPt, stage ); // the root should be the 'view' canvas, so the first matrix is the camera viewUtils.setViewportObj( stage ); @@ -997,7 +996,6 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { hit = this.snapToElement( elt, globalScrPt ); if (hit) { - //hitRecs.push( hit ); if (!hit.checkType()) { console.log( "invalid hit record: " + hit.getTypeString() ); @@ -1016,17 +1014,14 @@ var SnapManager = exports.SnapManager = Montage.create(Component, { } // test the rest of the tree var n = elt.childElementCount; - //var eltPt = viewUtils.parentToChild( parentPt, elt, true ); if (n > 0) { for (var i=0; i