Author: luc Date: Mon Aug 15 18:20:08 2011 New Revision: 1157932 URL: http://svn.apache.org/viewvc?rev=1157932&view=rev Log: clean up code, mainly replacing static dotProduct methods calls by non-static ones
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Line.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/OutlineExtractor.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Plane.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/PolyhedronsSet.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/SubPlane.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Line.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Line.java?rev=1157932&r1=1157931&r2=1157932&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Line.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Line.java Mon Aug 15 18:20:08 2011 @@ -16,11 +16,14 @@ */ package org.apache.commons.math.geometry.euclidean.threed; +import org.apache.commons.math.exception.MathArithmeticException; +import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Vector; import org.apache.commons.math.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math.geometry.euclidean.oned.Vector1D; import org.apache.commons.math.geometry.partitioning.Embedding; import org.apache.commons.math.util.FastMath; +import org.apache.commons.math.util.MathUtils; /** The class represent lines in a three dimensional space. @@ -45,24 +48,34 @@ public class Line implements Embedding<E /** Build a line from a point and a direction. * @param p point belonging to the line (this can be any point) * @param direction direction of the line - * @exception IllegalArgumentException if the direction norm is too small + * @exception MathArithmeticException if the direction norm is too small */ public Line(final Vector3D p, final Vector3D direction) { reset(p, direction); } + /** Copy constructor. + * <p>The created instance is completely independent from the + * original instance, it is a deep copy.</p> + * @param line line to copy + */ + public Line(final Line line) { + this.direction = line.direction; + this.zero = line.zero; + } + /** Reset the instance as if built from a point and a normal. * @param p point belonging to the line (this can be any point) * @param dir direction of the line - * @exception IllegalArgumentException if the direction norm is too small + * @exception MathArithmeticException if the direction norm is too small */ public void reset(final Vector3D p, final Vector3D dir) { final double norm = dir.getNorm(); if (norm == 0.0) { - throw new IllegalArgumentException("null norm"); + throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } this.direction = new Vector3D(1.0 / norm, dir); - zero = new Vector3D(1.0, p, -Vector3D.dotProduct(p, this.direction), this.direction); + zero = new Vector3D(1.0, p, -p.dotProduct(this.direction), this.direction); } /** Get a line with reversed direction. @@ -90,25 +103,33 @@ public class Line implements Embedding<E * <p>The abscissa is 0 if the projection of the point and the * projection of the frame origin on the line are the same * point.</p> - * @param point point to check (must be a {@link Vector3D Vector3D} - * instance) - * @return abscissa of the point (really a - * {org.apache.commons.math.geometry.euclidean.oned.Vector1D Vector1D} instance) + * @param point point to check + * @return abscissa of the point */ - public Vector1D toSubSpace(final Vector<Euclidean3D> point) { - Vector3D p3 = (Vector3D) point; - return new Vector1D(Vector3D.dotProduct(p3.subtract(zero), direction)); + public double getAbscissa(final Vector3D point) { + return point.subtract(zero).dotProduct(direction); } /** Get one point from the line. - * @param point desired abscissa for the point (must be a - * {org.apache.commons.math.geometry.euclidean.oned.Vector1D Vector1D} instance) + * @param point desired abscissa for the point * @return one point belonging to the line, at specified abscissa - * (really a {@link Vector3D Vector3D} instance) + */ + public Vector3D pointAt(final double abscissa) { + return new Vector3D(1.0, zero, abscissa, direction); + } + + /** {@inheritDoc} + * @see #getAbscissa(Vector3D) + */ + public Vector1D toSubSpace(final Vector<Euclidean3D> point) { + return new Vector1D(getAbscissa((Vector3D) point)); + } + + /** {@inheritDoc} + * @see #pointAt(double) */ public Vector3D toSpace(final Vector<Euclidean1D> point) { - Vector1D p1 = (Vector1D) point; - return new Vector3D(1.0, zero, p1.getX(), direction); + return pointAt(((Vector1D) point).getX()); } /** Check if the instance is similar to another line. @@ -137,28 +158,59 @@ public class Line implements Embedding<E */ public double distance(final Vector3D p) { final Vector3D d = p.subtract(zero); - final Vector3D n = new Vector3D(1.0, d, -Vector3D.dotProduct(d, direction), direction); + final Vector3D n = new Vector3D(1.0, d, -d.dotProduct(direction), direction); return n.getNorm(); } /** Compute the shortest distance between the instance and another line. - * @param line line to check agains the instance + * @param line line to check against the instance * @return shortest distance between the instance and the line */ public double distance(final Line line) { final Vector3D normal = Vector3D.crossProduct(direction, line.direction); - if (normal.getNorm() < 1.0e-10) { + final double n = normal.getNorm(); + if (n < MathUtils.SAFE_MIN) { // lines are parallel return distance(line.zero); } - // separating middle plane - final Plane middle = new Plane(new Vector3D(0.5, zero, 0.5, line.zero), normal); + // signed separation of the two parallel planes that contains the lines + final double offset = line.zero.subtract(zero).dotProduct(normal) / n; - // the lines are at the same distance on either side of the plane - return 2 * FastMath.abs(middle.getOffset(zero)); + return FastMath.abs(offset); } + /** Compute the point of the instance closest to another line. + * @param line line to check against the instance + * @return point of the instance closest to another line + */ + public Vector3D closestPoint(final Line line) { + + final double cos = direction.dotProduct(line.direction); + final double n = 1 - cos * cos; + if (n < MathUtils.EPSILON) { + // the lines are parallel + return zero; + } + + final Vector3D delta0 = line.zero.subtract(zero); + final double a = delta0.dotProduct(direction); + final double b = delta0.dotProduct(line.direction); + + return new Vector3D(1, zero, (a - b * cos) / n, direction); + + } + + /** Get the intersection point of the instance and another line. + * @param line other line + * @return intersection point of the instance and the other line + * or null if there are no intersection points + */ + public Vector3D intersection(final Line line) { + final Vector3D closest = closestPoint(line); + return line.contains(closest) ? closest : null; + } + } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/OutlineExtractor.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/OutlineExtractor.java?rev=1157932&r1=1157931&r2=1157932&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/OutlineExtractor.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/OutlineExtractor.java Mon Aug 15 18:20:08 2011 @@ -164,7 +164,7 @@ public class OutlineExtractor { (AbstractSubHyperplane<Euclidean3D, Euclidean2D>) facet; final Plane plane = (Plane) facet.getHyperplane(); - final double scal = Vector3D.dotProduct(plane.getNormal(), w); + final double scal = plane.getNormal().dotProduct(w); if (FastMath.abs(scal) > 1.0e-3) { Vector2D[][] vertices = ((PolygonsSet) absFacet.getRemainingRegion()).getVertices(); @@ -201,13 +201,13 @@ public class OutlineExtractor { int previous = closed ? (loop.length - 1) : 1; Vector3D previous3D = (Vector3D) plane.toSpace(loop[previous]); int current = (previous + 1) % loop.length; - Vector2D pPoint = new Vector2D(Vector3D.dotProduct(previous3D, u), - Vector3D.dotProduct(previous3D, v)); + Vector2D pPoint = new Vector2D(previous3D.dotProduct(u), + previous3D.dotProduct(v)); while (current < loop.length) { final Vector3D current3D = (Vector3D) plane.toSpace(loop[current]); - final Vector2D cPoint = new Vector2D(Vector3D.dotProduct(current3D, u), - Vector3D.dotProduct(current3D, v)); + final Vector2D cPoint = new Vector2D(current3D.dotProduct(u), + current3D.dotProduct(v)); final org.apache.commons.math.geometry.euclidean.twod.Line line = new org.apache.commons.math.geometry.euclidean.twod.Line(pPoint, cPoint); SubHyperplane<Euclidean2D> edge = line.wholeHyperplane(); Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Plane.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Plane.java?rev=1157932&r1=1157931&r2=1157932&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Plane.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Plane.java Mon Aug 15 18:20:08 2011 @@ -16,6 +16,8 @@ */ package org.apache.commons.math.geometry.euclidean.threed; +import org.apache.commons.math.exception.MathArithmeticException; +import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Vector; import org.apache.commons.math.geometry.euclidean.oned.Vector1D; import org.apache.commons.math.geometry.euclidean.twod.Euclidean2D; @@ -48,7 +50,7 @@ public class Plane implements Hyperplane /** Build a plane normal to a given direction and containing the origin. * @param normal normal direction to the plane - * @exception IllegalArgumentException if the normal norm is too small + * @exception MathArithmeticException if the normal norm is too small */ public Plane(final Vector3D normal) { setNormal(normal); @@ -59,11 +61,11 @@ public class Plane implements Hyperplane /** Build a plane from a point and a normal. * @param p point belonging to the plane * @param normal normal direction to the plane - * @exception IllegalArgumentException if the normal norm is too small + * @exception MathArithmeticException if the normal norm is too small */ public Plane(final Vector3D p, final Vector3D normal) { setNormal(normal); - originOffset = -Vector3D.dotProduct(p, w); + originOffset = -p.dotProduct(w); setFrame(); } @@ -73,10 +75,10 @@ public class Plane implements Hyperplane * @param p1 first point belonging to the plane * @param p2 second point belonging to the plane * @param p3 third point belonging to the plane - * @exception IllegalArgumentException if the points do not constitute a plane + * @exception MathArithmeticException if the points do not constitute a plane */ public Plane(final Vector3D p1, final Vector3D p2, final Vector3D p3) { - this(p1, Vector3D.crossProduct(p2.subtract(p1), p3.subtract(p1))); + this(p1, p2.subtract(p1).crossProduct(p3.subtract(p1))); } /** Copy constructor. @@ -109,7 +111,7 @@ public class Plane implements Hyperplane */ public void reset(final Vector3D p, final Vector3D normal) { setNormal(normal); - originOffset = -Vector3D.dotProduct(p, w); + originOffset = -p.dotProduct(w); setFrame(); } @@ -129,12 +131,12 @@ public class Plane implements Hyperplane /** Set the normal vactor. * @param normal normal direction to the plane (will be copied) - * @exception IllegalArgumentException if the normal norm is too small + * @exception MathArithmeticException if the normal norm is too small */ private void setNormal(final Vector3D normal) { final double norm = normal.getNorm(); if (norm < 1.0e-10) { - throw new IllegalArgumentException("null norm"); + throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } w = new Vector3D(1.0 / norm, normal); } @@ -220,9 +222,7 @@ public class Plane implements Hyperplane * @see #toSpace */ public Vector2D toSubSpace(final Vector<Euclidean3D> point) { - final Vector3D p3D = (Vector3D) point; - return new Vector2D(Vector3D.dotProduct(p3D, u), - Vector3D.dotProduct(p3D, v)); + return new Vector2D(point.dotProduct(u), point.dotProduct(v)); } /** Transform an in-plane point into a 3D space point. @@ -304,12 +304,12 @@ public class Plane implements Hyperplane */ public Vector3D intersection(final Line line) { final Vector3D direction = line.getDirection(); - final double dot = Vector3D.dotProduct(w, direction); + final double dot = w.dotProduct(direction); if (FastMath.abs(dot) < 1.0e-10) { return null; } final Vector3D point = (Vector3D) line.toSpace(Vector1D.ZERO); - final double k = -(originOffset + Vector3D.dotProduct(w, point)) / dot; + final double k = -(originOffset + w.dotProduct(point)) / dot; return new Vector3D(1.0, point, k, direction); } @@ -415,7 +415,7 @@ public class Plane implements Hyperplane * @return offset of the point */ public double getOffset(final Vector<Euclidean3D> point) { - return Vector3D.dotProduct((Vector3D) point, w) + originOffset; + return point.dotProduct(w) + originOffset; } /** Check if the instance has the same orientation as another hyperplane. @@ -424,7 +424,7 @@ public class Plane implements Hyperplane * the same orientation */ public boolean sameOrientationAs(final Hyperplane<Euclidean3D> other) { - return Vector3D.dotProduct(((Plane) other).w, w) > 0.0; + return (((Plane) other).w).dotProduct(w) > 0.0; } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/PolyhedronsSet.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/PolyhedronsSet.java?rev=1157932&r1=1157931&r2=1157932&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/PolyhedronsSet.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/PolyhedronsSet.java Mon Aug 15 18:20:08 2011 @@ -175,7 +175,7 @@ public class PolyhedronsSet extends Abst final Plane plane = (Plane) facet.getHyperplane(); final Vector3D facetB = plane.toSpace(polygon.getBarycenter()); - double scaled = area * Vector3D.dotProduct(facetB, plane.getNormal()); + double scaled = area * facetB.dotProduct(plane.getNormal()); if (reversed) { scaled = -scaled; } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/SubPlane.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/SubPlane.java?rev=1157932&r1=1157931&r2=1157932&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/SubPlane.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/SubPlane.java Mon Aug 15 18:20:08 2011 @@ -72,7 +72,7 @@ public class SubPlane extends AbstractSu Vector2D p = thisPlane.toSubSpace(inter.toSpace(Vector1D.ZERO)); Vector2D q = thisPlane.toSubSpace(inter.toSpace(Vector1D.ONE)); Vector3D crossP = Vector3D.crossProduct(inter.getDirection(), thisPlane.getNormal()); - if (Vector3D.dotProduct(crossP, otherPlane.getNormal()) < 0) { + if (crossP.dotProduct(otherPlane.getNormal()) < 0) { final Vector2D tmp = p; p = q; q = tmp; @@ -109,7 +109,7 @@ public class SubPlane extends AbstractSu Vector2D p = thisPlane.toSubSpace(inter.toSpace(Vector1D.ZERO)); Vector2D q = thisPlane.toSubSpace(inter.toSpace(Vector1D.ONE)); Vector3D crossP = Vector3D.crossProduct(inter.getDirection(), thisPlane.getNormal()); - if (Vector3D.dotProduct(crossP, otherPlane.getNormal()) < 0) { + if (crossP.dotProduct(otherPlane.getNormal()) < 0) { final Vector2D tmp = p; p = q; q = tmp;