diff options
author | David Beaumont <dbeaumont@google.com> | 2011-10-31 18:44:14 -0400 |
---|---|---|
committer | Michael Bolin <bolinfest@google.com> | 2011-10-31 18:44:14 -0400 |
commit | 33f470054f67665c0d535ba3b11e5e9cedfcc8f2 (patch) | |
tree | 7d113532be633a9e3053938eba656d522504dbf3 | |
parent | 89b87704cb0060932f099cb45182145d9ec46337 (diff) | |
download | s2-geometry-library-java-33f470054f67665c0d535ba3b11e5e9cedfcc8f2.tar.gz |
Made S1Interval and R1Interval immutable.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=25081675
-rw-r--r-- | src/com/google/common/geometry/R1Interval.java | 13 | ||||
-rw-r--r-- | src/com/google/common/geometry/S1Interval.java | 39 | ||||
-rw-r--r-- | src/com/google/common/geometry/S2EdgeUtil.java | 6 | ||||
-rw-r--r-- | src/com/google/common/geometry/S2Loop.java | 2 | ||||
-rw-r--r-- | tests/com/google/common/geometry/R1IntervalTest.java | 3 | ||||
-rw-r--r-- | tests/com/google/common/geometry/S1IntervalTest.java | 4 |
6 files changed, 18 insertions, 49 deletions
diff --git a/src/com/google/common/geometry/R1Interval.java b/src/com/google/common/geometry/R1Interval.java index 5b912ec..c849084 100644 --- a/src/com/google/common/geometry/R1Interval.java +++ b/src/com/google/common/geometry/R1Interval.java @@ -24,9 +24,8 @@ package com.google.common.geometry; public final strictfp class R1Interval { - // TODO(dbeaumont): Make these final and make this class fully immutable. - private double lo; - private double hi; + private final double lo; + private final double hi; /** Interval constructor. If lo > hi, the interval is empty. */ public R1Interval(double lo, double hi) { @@ -70,14 +69,6 @@ public final strictfp class R1Interval { return hi; } - public void setLo(double p) { - this.lo = p; - } - - public void setHi(double p) { - this.hi = p; - } - /** * Return true if the interval is empty, i.e. it contains no points. */ diff --git a/src/com/google/common/geometry/S1Interval.java b/src/com/google/common/geometry/S1Interval.java index eafca63..e5597f0 100644 --- a/src/com/google/common/geometry/S1Interval.java +++ b/src/com/google/common/geometry/S1Interval.java @@ -38,9 +38,8 @@ package com.google.common.geometry; public final strictfp class S1Interval implements Cloneable { - // TODO(dbeaumont): Make this class immutable and fix callers. - private double lo; - private double hi; + private final double lo; + private final double hi; /** * Both endpoints must be in the range -Pi to Pi inclusive. The value -Pi is @@ -123,16 +122,6 @@ public final strictfp class S1Interval implements Cloneable { return hi; } - public void setLo(double lo) { - this.lo = lo; - // assert (isValid()); - } - - public void setHi(double hi) { - this.hi = hi; - // assert (isValid()); - } - /** * An interval is valid if neither bound exceeds Pi in absolute value, and the * value -Pi appears only in the Empty() and Full() intervals. @@ -350,8 +339,8 @@ public final strictfp class S1Interval implements Cloneable { } /** - * Return an interval that contains all points with a distance "radius" of a - * point in this interval. Note that the expansion of an empty interval is + * Return an interval that contains all points within a distance "radius" of + * a point in this interval. Note that the expansion of an empty interval is * always empty. The radius must be non-negative. */ public S1Interval expanded(double radius) { @@ -366,13 +355,13 @@ public final strictfp class S1Interval implements Cloneable { return full(); } - S1Interval result = - new S1Interval(Math.IEEEremainder(lo() - radius, 2 * S2.M_PI), - Math.IEEEremainder(hi() + radius, 2 * S2.M_PI)); - if (result.lo() == -S2.M_PI) { - result.setLo(S2.M_PI); + // NOTE(dbeaumont): Should this remainder be 2 * M_PI or just M_PI ?? + double lo = Math.IEEEremainder(lo() - radius, 2 * S2.M_PI); + double hi = Math.IEEEremainder(hi() + radius, 2 * S2.M_PI); + if (lo == -S2.M_PI) { + lo = S2.M_PI; } - return result; + return new S1Interval(lo, hi); } /** @@ -516,12 +505,4 @@ public final strictfp class S1Interval implements Cloneable { // the return result is approximately 2*Pi and not zero. return (b + S2.M_PI) - (a - S2.M_PI); } - - @Override - public Object clone() throws CloneNotSupportedException { - S1Interval clone = (S1Interval) super.clone(); - clone.setLo(lo()); - clone.setHi(hi()); - return clone; - } } diff --git a/src/com/google/common/geometry/S2EdgeUtil.java b/src/com/google/common/geometry/S2EdgeUtil.java index be3e921..f587935 100644 --- a/src/com/google/common/geometry/S2EdgeUtil.java +++ b/src/com/google/common/geometry/S2EdgeUtil.java @@ -204,12 +204,14 @@ public strictfp class S2EdgeUtil { // Minimum/maximum latitude occurs in the edge interior. This affects // the latitude bounds but not the longitude bounds. double absLat = Math.acos(Math.abs(aCrossB.get(2) / aCrossB.norm())); + R1Interval lat = bound.lat(); if (da < 0) { // It's possible that absLat < lat.lo() due to numerical errors. - bound.lat().setHi((Math.max(absLat, bound.lat().hi()))); + lat = new R1Interval(lat.lo(), Math.max(absLat, bound.lat().hi())); } else { - bound.lat().setLo(Math.min(-absLat, bound.lat().lo())); + lat = new R1Interval(Math.min(-absLat, bound.lat().lo()), lat.hi()); } + bound = new S2LatLngRect(lat, bound.lng()); } } a = b; diff --git a/src/com/google/common/geometry/S2Loop.java b/src/com/google/common/geometry/S2Loop.java index 26b3740..5fc5ac1 100644 --- a/src/com/google/common/geometry/S2Loop.java +++ b/src/com/google/common/geometry/S2Loop.java @@ -849,7 +849,7 @@ public final strictfp class S2Loop implements S2Region, Comparable<S2Loop> { // north pole in which case b.lng().isFull() due to the test above. if (b.lng().isFull() && contains(new S2Point(0, 0, -1))) { - b.lat().setLo(-S2.M_PI_2); + b = new S2LatLngRect(new R1Interval(-S2.M_PI_2, b.lat().hi()), b.lng()); } bound = b; } diff --git a/tests/com/google/common/geometry/R1IntervalTest.java b/tests/com/google/common/geometry/R1IntervalTest.java index b4c2bba..6bded62 100644 --- a/tests/com/google/common/geometry/R1IntervalTest.java +++ b/tests/com/google/common/geometry/R1IntervalTest.java @@ -42,9 +42,6 @@ public strictfp class R1IntervalTest extends GeometryTestCase { assertEquals(unit.hi(), 1.0); assertEquals(negunit.lo(), -1.0); assertEquals(negunit.hi(), 0.0); - R1Interval ten = new R1Interval(0, 0); - ten.setHi(10); - assertEquals(ten.hi(), 10.0); // is_empty() R1Interval half = new R1Interval(0.5, 0.5); diff --git a/tests/com/google/common/geometry/S1IntervalTest.java b/tests/com/google/common/geometry/S1IntervalTest.java index 2215104..71a68e5 100644 --- a/tests/com/google/common/geometry/S1IntervalTest.java +++ b/tests/com/google/common/geometry/S1IntervalTest.java @@ -67,9 +67,7 @@ public strictfp class S1IntervalTest extends GeometryTestCase { S1Interval quad23 = new S1Interval(S2.M_PI_2, -S2.M_PI_2); // inverted assertEquals(quad23.lo(), S2.M_PI_2); assertEquals(quad23.hi(), -S2.M_PI_2); - S1Interval quad1 = new S1Interval(0, 0); - quad1.setHi(S2.M_PI_2); - assertEquals(quad1.hi(), S2.M_PI_2); + S1Interval quad1 = new S1Interval(0, S2.M_PI_2); // is_valid(), is_empty(), is_inverted() S1Interval zero = new S1Interval(0, 0); |