From d6eb9028c9ae8c70cb16ecd81e4879bca00c91ee Mon Sep 17 00:00:00 2001 From: Harvey Harrison Date: Sat, 15 Jun 2013 15:22:19 -0700 Subject: j3dcore: replace Distance with core utility class Replace the use of the j3dutils Distance class that is under a different license by reimplementing some mathematical distance calculations in a Utils class. This is new code and not based on the j3dutils version. Signed-off-by: Harvey Harrison --- .../javax/media/j3d/GeometryArrayRetained.java | 34 +++----- src/classes/share/javax/media/j3d/PickConeRay.java | 9 +- .../share/javax/media/j3d/PickConeSegment.java | 9 +- .../share/javax/media/j3d/PickCylinderRay.java | 9 +- .../share/javax/media/j3d/PickCylinderSegment.java | 9 +- src/classes/share/javax/media/j3d/Utils.java | 99 ++++++++++++++++++++++ 6 files changed, 123 insertions(+), 46 deletions(-) create mode 100644 src/classes/share/javax/media/j3d/Utils.java (limited to 'src/classes/share') diff --git a/src/classes/share/javax/media/j3d/GeometryArrayRetained.java b/src/classes/share/javax/media/j3d/GeometryArrayRetained.java index 186f7a2..f2f7600 100644 --- a/src/classes/share/javax/media/j3d/GeometryArrayRetained.java +++ b/src/classes/share/javax/media/j3d/GeometryArrayRetained.java @@ -8303,10 +8303,10 @@ ArrayList> morphUserLists = null; if (cyl instanceof PickCylinderSegment) { ((PickCylinderSegment)cyl).getEnd (end); - sqDist = Distance.pointToSegment(pt, origin, end, iPnt, null); + sqDist = Utils.ptToSegSquare(pt, origin, end, iPnt); } else { - sqDist = Distance.pointToRay(pt, origin, direction, iPnt, null); + sqDist = Utils.ptToRaySquare(pt, origin, direction, iPnt); } if (sqDist <= radius*radius) { originToIpnt.sub (iPnt, origin); @@ -8337,10 +8337,10 @@ ArrayList> morphUserLists = null; if (cone instanceof PickConeSegment) { ((PickConeSegment)cone).getEnd (end); - sqDist = Distance.pointToSegment (pt, origin, end, iPnt, null); + sqDist = Utils.ptToSegSquare(pt, origin, end, iPnt); } else { - sqDist = Distance.pointToRay (pt, origin, direction, iPnt, null); + sqDist = Utils.ptToRaySquare(pt, origin, direction, iPnt); } originToIpnt.sub(iPnt, origin); distance = originToIpnt.length(); @@ -11065,10 +11065,7 @@ int numDlistUsers(RenderBin renderBin) { if (coordinates.length == 2) { // a line - dist[0] = Math.sqrt(Distance.pointToSegment(center, - coordinates[0], - coordinates[1], - iPnt, null)); + dist[0] = Math.sqrt(Utils.ptToSegSquare(center, coordinates[0], coordinates[1], iPnt)); return; } @@ -11135,18 +11132,15 @@ int numDlistUsers(RenderBin renderBin) { double minDist; Point3d minPnt = new Point3d(); - dist[0] = Distance.pointToSegment(center, coordinates[0], - coordinates[1], iPnt, null); - minDist = Distance.pointToSegment(center, coordinates[1], - coordinates[2], minPnt, null); + dist[0] = Utils.ptToSegSquare(center, coordinates[0], coordinates[1], iPnt); + minDist = Utils.ptToSegSquare(center, coordinates[1], coordinates[2], minPnt); if (minDist < dist[0]) { dist[0] = minDist; iPnt.x = minPnt.x; iPnt.y = minPnt.y; iPnt.z = minPnt.z; } - minDist = Distance.pointToSegment(center, coordinates[2], - coordinates[0], minPnt, null); + minDist = Utils.ptToSegSquare(center, coordinates[2], coordinates[0], minPnt); if (minDist < dist[0]) { dist[0] = minDist; iPnt.x = minPnt.x; @@ -11181,18 +11175,15 @@ int numDlistUsers(RenderBin renderBin) { double minDist; Point3d minPnt = new Point3d(); - dist[0] = Distance.pointToSegment(center, coordinates[0], - coordinates[1], iPnt, null); - minDist = Distance.pointToSegment(center, coordinates[1], - coordinates[2], minPnt, null); + dist[0] = Utils.ptToSegSquare(center, coordinates[0], coordinates[1], iPnt); + minDist = Utils.ptToSegSquare(center, coordinates[1], coordinates[2], minPnt); if (minDist < dist[0]) { dist[0] = minDist; iPnt.x = minPnt.x; iPnt.y = minPnt.y; iPnt.z = minPnt.z; } - minDist = Distance.pointToSegment(center, coordinates[2], - coordinates[3], minPnt, null); + minDist = Utils.ptToSegSquare(center, coordinates[2], coordinates[3], minPnt); if (minDist < dist[0]) { dist[0] = minDist; iPnt.x = minPnt.x; @@ -11200,8 +11191,7 @@ int numDlistUsers(RenderBin renderBin) { iPnt.z = minPnt.z; } - minDist = Distance.pointToSegment(center, coordinates[3], - coordinates[0], minPnt, null); + minDist = Utils.ptToSegSquare(center, coordinates[3], coordinates[0], minPnt); if (minDist < dist[0]) { dist[0] = minDist; iPnt.x = minPnt.x; diff --git a/src/classes/share/javax/media/j3d/PickConeRay.java b/src/classes/share/javax/media/j3d/PickConeRay.java index dc2999f..25fa315 100644 --- a/src/classes/share/javax/media/j3d/PickConeRay.java +++ b/src/classes/share/javax/media/j3d/PickConeRay.java @@ -98,8 +98,7 @@ public final class PickConeRay extends PickCone { if (bounds instanceof BoundingSphere) { Point3d sphCenter = ((BoundingSphere)bounds).getCenter(); double sphRadius = ((BoundingSphere)bounds).getRadius(); - double sqDist = - Distance.pointToRay (sphCenter, origin, direction, rayPt, null); + double sqDist = Utils.ptToRaySquare(sphCenter, origin, direction, rayPt); vector.sub (rayPt, origin); distance = vector.length(); radius = getRadius (distance); @@ -119,8 +118,7 @@ public final class PickConeRay extends PickCone { Point3d center = ((BoundingBox)bounds).getCenter (); // First, see if cone is too far away from BoundingBox - double sqDist = - Distance.pointToRay (center, origin, direction, rayPt, null); + double sqDist = Utils.ptToRaySquare(center, origin, direction, rayPt); vector.sub (rayPt, origin); distance = vector.length(); @@ -199,8 +197,7 @@ public final class PickConeRay extends PickCone { bsphere.getCenter (sphCenter); double sphRadius = bsphere.getRadius(); - double sqDist = - Distance.pointToRay (sphCenter, origin, direction, rayPt, null); + double sqDist = Utils.ptToRaySquare(sphCenter, origin, direction, rayPt); vector.sub (rayPt, origin); distance = vector.length(); radius = getRadius (distance); diff --git a/src/classes/share/javax/media/j3d/PickConeSegment.java b/src/classes/share/javax/media/j3d/PickConeSegment.java index 78c6078..22aa417 100644 --- a/src/classes/share/javax/media/j3d/PickConeSegment.java +++ b/src/classes/share/javax/media/j3d/PickConeSegment.java @@ -122,8 +122,7 @@ public final class PickConeSegment extends PickCone { if (bounds instanceof BoundingSphere) { Point3d sphCenter = ((BoundingSphere)bounds).getCenter(); double sphRadius = ((BoundingSphere)bounds).getRadius(); - double sqDist = - Distance.pointToSegment (sphCenter, origin, end, rayPt, null); + double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, rayPt); vector.sub (rayPt, origin); distance = vector.length(); @@ -144,8 +143,7 @@ public final class PickConeSegment extends PickCone { Point3d center = ((BoundingBox)bounds).getCenter (); // First, see if cone is too far away from BoundingBox - double sqDist = - Distance.pointToSegment (center, origin, end, rayPt, null); + double sqDist = Utils.ptToSegSquare(center, origin, end, rayPt); vector.sub (rayPt, origin); distance = vector.length(); @@ -226,8 +224,7 @@ public final class PickConeSegment extends PickCone { bsphere.getCenter (sphCenter); double sphRadius = bsphere.getRadius(); - double sqDist = - Distance.pointToSegment (sphCenter, origin, end, rayPt, null); + double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, rayPt); vector.sub (rayPt, origin); distance = vector.length(); diff --git a/src/classes/share/javax/media/j3d/PickCylinderRay.java b/src/classes/share/javax/media/j3d/PickCylinderRay.java index 1e64577..6f69600 100644 --- a/src/classes/share/javax/media/j3d/PickCylinderRay.java +++ b/src/classes/share/javax/media/j3d/PickCylinderRay.java @@ -95,8 +95,7 @@ public final class PickCylinderRay extends PickCylinder { if (bounds instanceof BoundingSphere) { Point3d sphCenter = ((BoundingSphere)bounds).getCenter(); double sphRadius = ((BoundingSphere)bounds).getRadius(); - double sqDist = - Distance.pointToRay (sphCenter, origin, direction); + double sqDist = Utils.ptToRaySquare(sphCenter, origin, direction, null); if (sqDist <= (sphRadius+radius)*(sphRadius+radius)) { return true; } @@ -120,8 +119,7 @@ public final class PickCylinderRay extends PickCylinder { boxRadiusSquared += temp*temp; // First, see if cylinder is too far away from BoundingBox - double sqDist = - Distance.pointToRay (center, origin, direction); + double sqDist = Utils.ptToRaySquare(center, origin, direction, null); if (sqDist > boxRadiusSquared ) { return false; // we are too far to intersect @@ -183,8 +181,7 @@ public final class PickCylinderRay extends PickCylinder { bsphere.getCenter (sphCenter); double sphRadius = bsphere.getRadius(); - double sqDist = - Distance.pointToRay (sphCenter, origin, direction); + double sqDist = Utils.ptToRaySquare(sphCenter, origin, direction, null); if (sqDist > (sphRadius+radius) * (sphRadius+radius)) { return false; // we are too far to intersect } diff --git a/src/classes/share/javax/media/j3d/PickCylinderSegment.java b/src/classes/share/javax/media/j3d/PickCylinderSegment.java index c7cf01d..70ad3db 100644 --- a/src/classes/share/javax/media/j3d/PickCylinderSegment.java +++ b/src/classes/share/javax/media/j3d/PickCylinderSegment.java @@ -116,8 +116,7 @@ public final class PickCylinderSegment extends PickCylinder { if (bounds instanceof BoundingSphere) { Point3d sphCenter = ((BoundingSphere)bounds).getCenter(); double sphRadius = ((BoundingSphere)bounds).getRadius(); - double sqDist = - Distance.pointToSegment (sphCenter, origin, end); + double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, null); if (sqDist <= (sphRadius+radius)*(sphRadius+radius)) { return true; } @@ -141,8 +140,7 @@ public final class PickCylinderSegment extends PickCylinder { boxRadiusSquared += temp*temp; // First, see if cylinder is too far away from BoundingBox - double sqDist = - Distance.pointToSegment (center, origin, end); + double sqDist = Utils.ptToSegSquare(center, origin, end, null); if (sqDist > boxRadiusSquared) { return false; // we are too far to intersect } @@ -202,8 +200,7 @@ public final class PickCylinderSegment extends PickCylinder { bsphere.getCenter (sphCenter); double sphRadius = bsphere.getRadius(); - double sqDist = - Distance.pointToSegment (sphCenter, origin, end); + double sqDist = Utils.ptToSegSquare(sphCenter, origin, end, null); if (sqDist > (sphRadius+radius)*(sphRadius+radius)) { return false; // we are too far to intersect } diff --git a/src/classes/share/javax/media/j3d/Utils.java b/src/classes/share/javax/media/j3d/Utils.java new file mode 100644 index 0000000..5510877 --- /dev/null +++ b/src/classes/share/javax/media/j3d/Utils.java @@ -0,0 +1,99 @@ +/* + * Copyright 2013 Harvey Harrison + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + */ +package javax.media.j3d; + +import javax.vecmath.Point3d; +import javax.vecmath.Vector3d; + +/** + * A small utility class for internal use. Mainly contains some distance-calculation + * methods. + * + */ +class Utils { + +/** + * Returns the square of the minimum distance from the given point to the segment + * defined by start, end. + */ +static final double ptToSegSquare(Point3d pt, Point3d start, Point3d end, Point3d closest) { + Vector3d dir = new Vector3d(); + dir.sub(end, start); + + Vector3d dt = new Vector3d(); + dt.sub(pt, start); + + // Project the point onto the line defined by the segment + double proj = dir.dot(dt); + + // We projected 'before' the start point, just return the dSquared between + // the point and the start + if (proj <= 0.0d) { + if (closest != null) closest.set(start); + return dt.lengthSquared(); + } + + // Project the segment onto itself + double segSquared = dir.lengthSquared(); + + // If our point projected off the end of the segment, return the dSquared between + // the point and the end + if (proj >= segSquared) { + if (closest != null) closest.set(end); + dt.sub(pt, end); + return dt.lengthSquared(); + } + + // We projected somewhere along the segment, calculate the closest point + dt.scaleAdd(proj / segSquared, dir, start); + if (closest != null) closest.set(dt); + + // return the distance from the point to the closest point on the segment + dt.sub(pt, dt); + return dt.lengthSquared(); +} + +/** + * Returns the square of the minimum distance from the given point to the ray + * defined by start, dir. + */ +static final double ptToRaySquare(Point3d pt, Point3d start, Vector3d dir, Point3d closest) { + Vector3d dt = new Vector3d(); + dt.sub(pt, start); + + // Project the point onto the ray + double proj = dir.dot(dt); + + // We projected 'before' the start point, just return the dSquared between + // the point and the start + if (proj <= 0.0d) { + if (closest != null) closest.set(start); + return dt.lengthSquared(); + } + + // Project the ray onto itself + double raySquared = dir.lengthSquared(); + + // We projected somewhere along the ray, calculate the closest point + dt.scaleAdd(proj / raySquared, dir, start); + if (closest != null) closest.set(dt); + + // return the distance from the point to the closest point on the ray + dt.sub(pt, dt); + return dt.lengthSquared(); +} + +} -- cgit v1.2.3