Author: luc
Date: Sun Apr 22 20:01:44 2012
New Revision: 1328959

URL: http://svn.apache.org/viewvc?rev=1328959&view=rev
Log:
avoid division by zero when dealing with single-point intervals sets

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.java?rev=1328959&r1=1328958&r2=1328959&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.java
 Sun Apr 22 20:01:44 2012
@@ -23,6 +23,7 @@ import java.util.List;
 import org.apache.commons.math3.geometry.partitioning.AbstractRegion;
 import org.apache.commons.math3.geometry.partitioning.BSPTree;
 import org.apache.commons.math3.geometry.partitioning.SubHyperplane;
+import org.apache.commons.math3.util.Precision;
 
 /** This class represents a 1D region: a set of intervals.
  * @version $Id$
@@ -108,21 +109,21 @@ public class IntervalsSet extends Abstra
         if (Double.isInfinite(upper) && (upper > 0)) {
             // the tree must be open on the positive infinity side
             return new BSPTree<Euclidean1D>(lowerCut,
-                               new BSPTree<Euclidean1D>(Boolean.FALSE),
-                               new BSPTree<Euclidean1D>(Boolean.TRUE),
-                               null);
+                                            new 
BSPTree<Euclidean1D>(Boolean.FALSE),
+                                            new 
BSPTree<Euclidean1D>(Boolean.TRUE),
+                                            null);
         }
 
         // the tree must be bounded on the two sides
         final SubHyperplane<Euclidean1D> upperCut =
             new OrientedPoint(new Vector1D(upper), true).wholeHyperplane();
         return new BSPTree<Euclidean1D>(lowerCut,
-                           new BSPTree<Euclidean1D>(Boolean.FALSE),
-                           new BSPTree<Euclidean1D>(upperCut,
-                                       new BSPTree<Euclidean1D>(Boolean.FALSE),
-                                       new BSPTree<Euclidean1D>(Boolean.TRUE),
-                                       null),
-                                       null);
+                                        new 
BSPTree<Euclidean1D>(Boolean.FALSE),
+                                        new BSPTree<Euclidean1D>(upperCut,
+                                                                 new 
BSPTree<Euclidean1D>(Boolean.FALSE),
+                                                                 new 
BSPTree<Euclidean1D>(Boolean.TRUE),
+                                                                 null),
+                                        null);
 
     }
 
@@ -146,7 +147,13 @@ public class IntervalsSet extends Abstra
                 sum  += interval.getLength() * interval.getMidPoint();
             }
             setSize(size);
-            setBarycenter(Double.isInfinite(size) ? Vector1D.NaN : new 
Vector1D(sum / size));
+            if (Double.isInfinite(size)) {
+                setBarycenter(Vector1D.NaN);
+            } else if (size >= Precision.SAFE_MIN) {
+                setBarycenter(new Vector1D(sum / size));
+            } else {
+                setBarycenter(((OrientedPoint) 
getTree(false).getCut().getHyperplane()).getLocation());
+            }
         }
     }
 

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.java?rev=1328959&r1=1328958&r2=1328959&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.java
 Sun Apr 22 20:01:44 2012
@@ -24,6 +24,7 @@ import org.apache.commons.math3.geometry
 import org.apache.commons.math3.geometry.partitioning.Region;
 import org.apache.commons.math3.geometry.partitioning.RegionFactory;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -95,4 +96,11 @@ public class IntervalsSetTest {
 
     }
 
+    @Test
+    public void testSinglePoint() {
+        IntervalsSet set = new IntervalsSet(1.0, 1.0);
+        Assert.assertEquals(0.0, set.getSize(), Precision.SAFE_MIN);
+        Assert.assertEquals(1.0, ((Vector1D) set.getBarycenter()).getX(), 
Precision.EPSILON);
+    }
+
 }


Reply via email to