commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1136154 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/geometry/euclidean/twod/ site/xdoc/ test/java/org/apache/commons/math/geometry/euclidean/twod/
Date Wed, 15 Jun 2011 19:20:00 GMT
Author: luc
Date: Wed Jun 15 19:20:00 2011
New Revision: 1136154

URL: http://svn.apache.org/viewvc?rev=1136154&view=rev
Log:
Added a way to build a sub-line from its endpoints, and to retrieve the enpoints from a sub-line.

JIRA: MATH-592

Added:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
  (with props)
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Line.java
    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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Line.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Line.java?rev=1136154&r1=1136153&r2=1136154&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Line.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Line.java
Wed Jun 15 19:20:00 2011
@@ -186,7 +186,7 @@ public class Line implements Hyperplane<
     public Vector2D toSpace(final Vector<Euclidean1D> point) {
         final double abscissa = ((Vector1D) point).getX();
         return new Vector2D(abscissa * cos - originOffset * sin,
-                           abscissa * sin + originOffset * cos);
+                            abscissa * sin + originOffset * cos);
     }
 
     /** Get the intersection point of the instance and another line.

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=1136154&r1=1136153&r2=1136154&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
Wed Jun 15 19:20:00 2011
@@ -16,7 +16,11 @@
  */
 package org.apache.commons.math.geometry.euclidean.twod;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.commons.math.geometry.euclidean.oned.Euclidean1D;
+import org.apache.commons.math.geometry.euclidean.oned.Interval;
 import org.apache.commons.math.geometry.euclidean.oned.IntervalsSet;
 import org.apache.commons.math.geometry.euclidean.oned.OrientedPoint;
 import org.apache.commons.math.geometry.euclidean.oned.Vector1D;
@@ -43,6 +47,55 @@ public class SubLine extends AbstractSub
         super(hyperplane, remainingRegion);
     }
 
+    /** Create a sub-line from two endpoints.
+     * @param start start point
+     * @param end end point
+     */
+    public SubLine(final Vector2D start, final Vector2D end) {
+        super(new Line(start, end), buildIntervalSet(start, end));
+    }
+
+    /** Get the endpoints of the sub-line.
+     * <p>
+     * A subline may be any arbitrary number of disjoints segments, so the endpoints
+     * are provided as a list of endpoint pairs. Each element of the list represents
+     * one segment, and each segment contains a start point at index 0 and an end point
+     * at index 1. If the sub-line is unbounded in the negative infinity direction,
+     * the start point of the first segment will have infinite coordinates. If the
+     * sub-line is unbounded in the positive infinity direction, the end point of the
+     * last segment will have infinite coordinates. So a sub-line covering the whole
+     * line will contain just one row and both elements of this row will have infinite
+     * coordinates. If the sub-line is empty, the returned list will contain 0 segments.
+     * </p>
+     * @return list of segments endpoints
+     */
+    public List<Vector2D[]> getSegments() {
+
+        final Line line = (Line) getHyperplane();
+        final List<Interval> list = ((IntervalsSet) getRemainingRegion()).asList();
+        final List<Vector2D[]> segments = new ArrayList<Vector2D[]>();
+
+        for (final Interval interval : list) {
+            final Vector2D start = line.toSpace(new Vector1D(interval.getLower()));
+            final Vector2D end   = line.toSpace(new Vector1D(interval.getUpper()));
+            segments.add(new Vector2D[] { start, end });
+        }
+
+        return segments;
+
+    }
+
+    /** Build an interval set from two points.
+     * @param start start point
+     * @param end end point
+     * @return an interval set
+     */
+    private static IntervalsSet buildIntervalSet(final Vector2D start, final Vector2D end)
{
+        final Line line = new Line(start, end);
+        return new IntervalsSet(line.toSubSpace(start).getX(),
+                                line.toSubSpace(end).getX());
+    }
+
     /** {@inheritDoc} */
     protected AbstractSubHyperplane<Euclidean2D, Euclidean1D> buildNew(final Hyperplane<Euclidean2D>
hyperplane,
                                                                        final Region<Euclidean1D>
remainingRegion) {

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=1136154&r1=1136153&r2=1136154&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Jun 15 19:20:00 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-592">
+        Added a way to build a sub-line from its endpoints, and to retrieve the enpoints
+        from a sub-line
+      </action>
       <action dev="luc" type="fix" issue="MATH-594" due-to="Dennis Hendriks">
         Javadoc fixes in ODE.
       </action>

Added: 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=1136154&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
(added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
Wed Jun 15 19:20:00 2011
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.geometry.euclidean.twod;
+
+import java.util.List;
+
+import org.apache.commons.math.geometry.euclidean.oned.Euclidean1D;
+import org.apache.commons.math.geometry.euclidean.oned.IntervalsSet;
+import org.apache.commons.math.geometry.partitioning.RegionFactory;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class SubLineTest {
+
+    @Test
+    public void testEndPoints() {
+        SubLine sub = new SubLine(new Vector2D(-1, -7), new Vector2D(7, -1));
+        List<Vector2D[]> segments = sub.getSegments();
+        Assert.assertEquals(1, segments.size());
+        Assert.assertEquals(-1, segments.get(0)[0].getX(), 1.0e-10);
+        Assert.assertEquals(-7, segments.get(0)[0].getY(), 1.0e-10);
+        Assert.assertEquals( 7, segments.get(0)[1].getX(), 1.0e-10);
+        Assert.assertEquals(-1, segments.get(0)[1].getY(), 1.0e-10);
+    }
+
+    @Test
+    public void testNoEndPoints() {
+        SubLine wholeLine = new Line(new Vector2D(-1, 7), new Vector2D(7, 1)).wholeHyperplane();
+        List<Vector2D[]> segments = wholeLine.getSegments();
+        Assert.assertEquals(1, segments.size());
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[0].getX()) &&
+                          segments.get(0)[0].getX() < 0);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[0].getY()) &&
+                          segments.get(0)[0].getY() > 0);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[1].getX()) &&
+                          segments.get(0)[1].getX() > 0);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[1].getY()) &&
+                          segments.get(0)[1].getY() < 0);
+    }
+
+    @Test
+    public void testNoSegments() {
+        SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)),
+                                    new RegionFactory<Euclidean1D>().getComplement(new
IntervalsSet()));
+        List<Vector2D[]> segments = empty.getSegments();
+        Assert.assertEquals(0, segments.size());
+    }
+
+    @Test
+    public void testSeveralSegments() {
+        SubLine twoSubs = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)),
+                                    new RegionFactory<Euclidean1D>().union(new IntervalsSet(1,
2),
+                                                                           new IntervalsSet(3,
4)));
+        List<Vector2D[]> segments = twoSubs.getSegments();
+        Assert.assertEquals(2, segments.size());
+    }
+
+    @Test
+    public void testHalfInfiniteNeg() {
+        SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)),
+                                    new IntervalsSet(Double.NEGATIVE_INFINITY, 0.0));
+        List<Vector2D[]> segments = empty.getSegments();
+        Assert.assertEquals(1, segments.size());
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[0].getX()) &&
+                          segments.get(0)[0].getX() < 0);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[0].getY()) &&
+                          segments.get(0)[0].getY() < 0);
+        Assert.assertEquals( 3, segments.get(0)[1].getX(), 1.0e-10);
+        Assert.assertEquals(-4, segments.get(0)[1].getY(), 1.0e-10);
+    }
+
+    @Test
+    public void testHalfInfinitePos() {
+        SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)),
+                                    new IntervalsSet(0.0, Double.POSITIVE_INFINITY));
+        List<Vector2D[]> segments = empty.getSegments();
+        Assert.assertEquals(1, segments.size());
+        Assert.assertEquals( 3, segments.get(0)[0].getX(), 1.0e-10);
+        Assert.assertEquals(-4, segments.get(0)[0].getY(), 1.0e-10);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[1].getX()) &&
+                          segments.get(0)[1].getX() > 0);
+        Assert.assertTrue(Double.isInfinite(segments.get(0)[1].getY()) &&
+                          segments.get(0)[1].getY() > 0);
+    }
+
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/twod/SubLineTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message