From 638def643f318731ae058597f5e89231896fcc4b Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sun, 12 Jan 2014 11:13:05 +0000 Subject: [PATCH] Fixed missing last iteration in a loop. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1557514 13f79535-47bb-0310-9956-ffa450edef68 --- .../spherical/twod/SphericalPolygonsSet.java | 2 +- .../twod/SphericalPolygonsSetTest.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSet.java b/src/main/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSet.java index dce71cc8d..06c691f86 100644 --- a/src/main/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSet.java +++ b/src/main/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSet.java @@ -628,7 +628,7 @@ public class SphericalPolygonsSet extends AbstractRegion { double sum = 0; Vector3D sumP = Vector3D.ZERO; for (Edge edge = startVertex.getOutgoing(); - edge.getEnd() != startVertex; + n == 0 || edge.getStart() != startVertex; edge = edge.getEnd().getOutgoing()) { final Vector3D middle = edge.getPointAt(0.5 * edge.getLength()); sumP = new Vector3D(1, sumP, edge.getLength(), middle); diff --git a/src/test/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSetTest.java b/src/test/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSetTest.java index b8f7a0174..59274c2ba 100644 --- a/src/test/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSetTest.java +++ b/src/test/java/org/apache/commons/math3/geometry/spherical/twod/SphericalPolygonsSetTest.java @@ -16,9 +16,12 @@ */ package org.apache.commons.math3.geometry.spherical.twod; +import java.util.List; + import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import org.apache.commons.math3.geometry.partitioning.Region.Location; import org.apache.commons.math3.geometry.partitioning.RegionFactory; +import org.apache.commons.math3.geometry.spherical.twod.SphericalPolygonsSet.Vertex; import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; @@ -81,6 +84,35 @@ public class SphericalPolygonsSetTest { Assert.assertEquals(Location.BOUNDARY, octant.checkPoint(new S2Point(v))); } } + + List loops = octant.getBoundaryLoops(); + Assert.assertEquals(1, loops.size()); + boolean xFound = false; + boolean yFound = false; + boolean zFound = false; + Vertex first = loops.get(0); + xFound = first.getLocation().getVector().distance(Vector3D.PLUS_I) < 1.0e-10; + yFound = first.getLocation().getVector().distance(Vector3D.PLUS_J) < 1.0e-10; + zFound = first.getLocation().getVector().distance(Vector3D.PLUS_K) < 1.0e-10; + int count = 1; + for (Vertex v = first.getOutgoing().getEnd(); v != first; v = v.getOutgoing().getEnd()) { + ++count; + Assert.assertTrue(v == v.getIncoming().getStart().getOutgoing().getEnd()); + Assert.assertEquals(0.5 * FastMath.PI, v.getIncoming().getLength(), 1.0e-10); + xFound = xFound || v.getLocation().getVector().distance(Vector3D.PLUS_I) < 1.0e-10; + yFound = yFound || v.getLocation().getVector().distance(Vector3D.PLUS_J) < 1.0e-10; + zFound = zFound || v.getLocation().getVector().distance(Vector3D.PLUS_K) < 1.0e-10; + } + Assert.assertTrue(xFound); + Assert.assertTrue(yFound); + Assert.assertTrue(zFound); + Assert.assertEquals(3, count); + + Assert.assertEquals(0.0, + new Vector3D(1, 1, 1).normalize().distance(((S2Point) octant.getBarycenter()).getVector()), + 1.0e-10); + Assert.assertEquals(0.5 * FastMath.PI, octant.getSize(), 1.0e-10); + } @Test