You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2011/06/20 20:40:13 UTC

svn commit: r1137738 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java site/xdoc/changes.xml test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java

Author: luc
Date: Mon Jun 20 18:40:13 2011
New Revision: 1137738

URL: http://svn.apache.org/viewvc?rev=1137738&view=rev
Log:
Added a way to compute sub-lines intersections, considering sub-lines either as open sets or closed sets.

JIRA: MATH-591

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java?rev=1137738&r1=1137737&r2=1137738&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/SubLine.java Mon Jun 20 18:40:13 2011
@@ -28,6 +28,7 @@ import org.apache.commons.math.geometry.
 import org.apache.commons.math.geometry.partitioning.BSPTree;
 import org.apache.commons.math.geometry.partitioning.Hyperplane;
 import org.apache.commons.math.geometry.partitioning.Region;
+import org.apache.commons.math.geometry.partitioning.Region.Location;
 import org.apache.commons.math.geometry.partitioning.Side;
 import org.apache.commons.math.geometry.partitioning.SubHyperplane;
 import org.apache.commons.math.util.FastMath;
@@ -85,6 +86,43 @@ public class SubLine extends AbstractSub
 
     }
 
+    /** Get the intersection of the instance and another sub-line.
+     * <p>
+     * This method is related to the {@link Line#intersection(Hyperplane)
+     * intersection} method in the {@link Line Line} class, but in addition
+     * to compute the point along infinite lines, it also checks the point
+     * lies on both sub-line ranges.
+     * </p>
+     * @param subLine other sub-line which may intersect instance
+     * @param includeEndPoints if true, endpoints are considered to belong to
+     * instance (i.e. they are closed sets) and may be returned, otherwise endpoints
+     * are considered to not belong to instance (i.e. they are open sets) and intersection
+     * occurring on endpoints lead to null being returned
+     * @return the intersection point if there is one, null if the sub-lines don't intersect
+     */
+    public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) {
+
+        // retrieve the underlying lines
+        Line line1 = (Line) getHyperplane();
+        Line line2 = (Line) subLine.getHyperplane();
+
+        // compute the intersection on infinite line
+        Vector2D v2D = line1.intersection(line2);
+
+        // check location of point with respect to first sub-line
+        Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace(v2D));
+
+        // check location of point with respect to second sub-line
+        Location loc2 = subLine.getRemainingRegion().checkPoint(line2.toSubSpace(v2D));
+
+        if (includeEndPoints) {
+            return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v2D : null;
+        } else {
+            return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v2D : null;
+        }
+
+    }
+
     /** Build an interval set from two points.
      * @param start start point
      * @param end end point

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1137738&r1=1137737&r2=1137738&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Mon Jun 20 18:40:13 2011
@@ -52,6 +52,10 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-591">
+        Added a way to compute sub-lines intersections, considering sub-lines either
+        as open sets or closed sets
+      </action>
       <action dev="luc" type="add" issue="MATH-592">
         Added a way to build a sub-line from its endpoints, and to retrieve the enpoints
         from a sub-line

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java?rev=1137738&r1=1137737&r2=1137738&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java Mon Jun 20 18:40:13 2011
@@ -97,4 +97,52 @@ public class SubLineTest {
                           segments.get(0)[1].getY() > 0);
     }
 
+    @Test
+    public void testIntersectionInsideInside() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 2));
+        Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
+        Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, false)), 1.0e-12);
+    }
+
+    @Test
+    public void testIntersectionInsideBoundary() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1));
+        Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
+        Assert.assertNull(sub1.intersection(sub2, false));
+    }
+
+    @Test
+    public void testIntersectionInsideOutside() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5));
+        Assert.assertNull(sub1.intersection(sub2, true));
+        Assert.assertNull(sub1.intersection(sub2, false));
+    }
+
+    @Test
+    public void testIntersectionBoundaryBoundary() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1));
+        Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)),  1.0e-12);
+        Assert.assertNull(sub1.intersection(sub2, false));
+    }
+
+    @Test
+    public void testIntersectionBoundaryOutside() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5));
+        Assert.assertNull(sub1.intersection(sub2, true));
+        Assert.assertNull(sub1.intersection(sub2, false));
+    }
+
+    @Test
+    public void testIntersectionOutsideOutside() {
+        SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(1.5, 1));
+        SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5));
+        Assert.assertNull(sub1.intersection(sub2, true));
+        Assert.assertNull(sub1.intersection(sub2, false));
+    }
+
 }