Skip to content

Commit 0095e3d

Browse files
hagbardMichael Bolin
authored andcommitted
A more complex CL to avoid exposing the 'bounds' arrays from several of the
geometry class (paving the way for them to be made immutable later). ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=24952612
1 parent b9d250e commit 0095e3d

10 files changed

Lines changed: 87 additions & 106 deletions

File tree

src/com/google/common/geometry/R1Interval.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
*
2323
*/
2424

25-
public strictfp class R1Interval {
26-
private final double[] bounds = new double[2];
25+
public final strictfp class R1Interval {
26+
27+
// TODO(dbeaumont): Make these final and make this class fully immutable.
28+
private double lo;
29+
private double hi;
2730

2831
/** Interval constructor. If lo > hi, the interval is empty. */
2932
public R1Interval(double lo, double hi) {
30-
bounds[0] = lo;
31-
bounds[1] = hi;
33+
this.lo = lo;
34+
this.hi = hi;
3235
}
3336

3437
/**
@@ -60,27 +63,19 @@ public static R1Interval fromPointPair(double p1, double p2) {
6063
}
6164

6265
public double lo() {
63-
return bounds[0];
66+
return lo;
6467
}
6568

6669
public double hi() {
67-
return bounds[1];
68-
}
69-
70-
public double bound(int i) {
71-
return bounds[i];
72-
}
73-
74-
public double[] bounds() {
75-
return bounds;
70+
return hi;
7671
}
7772

7873
public void setLo(double p) {
79-
bounds[0] = p;
74+
this.lo = p;
8075
}
8176

8277
public void setHi(double p) {
83-
bounds[1] = p;
78+
this.hi = p;
8479
}
8580

8681
/**
@@ -98,7 +93,6 @@ public double getCenter() {
9893
return 0.5 * (lo() + hi());
9994
}
10095

101-
10296
/**
10397
* Return the length of the interval. The length of an empty interval is
10498
* negative.
@@ -107,7 +101,6 @@ public double getLength() {
107101
return hi() - lo();
108102
}
109103

110-
111104
public boolean contains(double p) {
112105
return p >= lo() && p <= hi();
113106
}
@@ -221,8 +214,8 @@ public int hashCode() {
221214
}
222215

223216
long value = 17;
224-
value = 37 * value + Double.doubleToLongBits(bounds[0]);
225-
value = 37 * value + Double.doubleToLongBits(bounds[1]);
217+
value = 37 * value + Double.doubleToLongBits(lo);
218+
value = 37 * value + Double.doubleToLongBits(hi);
226219
return (int) (value ^ (value >>> 32));
227220
}
228221

src/com/google/common/geometry/R2Vector.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
* norm, comparison etc.
2222
*
2323
*/
24-
public strictfp class R2Vector {
25-
double x;
26-
double y;
24+
public final strictfp class R2Vector {
25+
final double x;
26+
final double y;
27+
28+
public R2Vector() {
29+
this(0, 0);
30+
}
2731

2832
public R2Vector(double x, double y) {
2933
this.x = x;
@@ -38,9 +42,6 @@ public R2Vector(double[] coord) {
3842
y = coord[1];
3943
}
4044

41-
public R2Vector() {
42-
}
43-
4445
public double get(int index) {
4546
if (index > 1) {
4647
throw new ArrayIndexOutOfBoundsException(index);
@@ -57,11 +58,11 @@ public static R2Vector mul(final R2Vector p, double m) {
5758
}
5859

5960
public double norm2() {
60-
return x * x + y * y;
61+
return (x * x) + (y * y);
6162
}
6263

6364
public static double dotProd(final R2Vector p1, final R2Vector p2) {
64-
return p1.x * p2.x + p1.y * p2.y;
65+
return (p1.x * p2.x) + (p1.y * p2.y);
6566
}
6667

6768
public double dotProd(R2Vector that) {

src/com/google/common/geometry/S1Interval.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
*
3737
*/
3838

39-
public strictfp class S1Interval implements Cloneable {
39+
public final strictfp class S1Interval implements Cloneable {
4040

41-
private final double[] bounds = new double[2];
41+
// TODO(dbeaumont): Make this class immutable and fix callers.
42+
private double lo;
43+
private double hi;
4244

4345
/**
4446
* Both endpoints must be in the range -Pi to Pi inclusive. The value -Pi is
@@ -50,32 +52,33 @@ public S1Interval(double lo, double hi) {
5052

5153
/**
5254
* Copy constructor. Assumes that the given interval is valid.
55+
*
56+
* TODO(dbeaumont): Make this class immutable and remove this method.
5357
*/
5458
public S1Interval(S1Interval interval) {
55-
bounds[0] = interval.bounds[0];
56-
bounds[1] = interval.bounds[1];
59+
this.lo = interval.lo;
60+
this.hi = interval.hi;
5761
}
5862

5963
/**
6064
* Internal constructor that assumes that both arguments are in the correct
6165
* range, i.e. normalization from -Pi to Pi is already done.
6266
*/
6367
private S1Interval(double lo, double hi, boolean checked) {
64-
bounds[0] = lo;
65-
bounds[1] = hi;
66-
68+
double newLo = lo;
69+
double newHi = hi;
6770
if (!checked) {
6871
if (lo == -S2.M_PI && hi != S2.M_PI) {
69-
setLo(S2.M_PI);
72+
newLo = S2.M_PI;
7073
}
7174
if (hi == -S2.M_PI && lo != S2.M_PI) {
72-
setHi(S2.M_PI);
75+
newHi = S2.M_PI;
7376
}
7477
}
75-
// assert (isValid());
78+
this.lo = newLo;
79+
this.hi = newHi;
7680
}
7781

78-
7982
public static S1Interval empty() {
8083
return new S1Interval(S2.M_PI, -S2.M_PI, true);
8184
}
@@ -112,30 +115,21 @@ public static S1Interval fromPointPair(double p1, double p2) {
112115
}
113116
}
114117

115-
116118
public double lo() {
117-
return bounds[0];
119+
return lo;
118120
}
119121

120122
public double hi() {
121-
return bounds[1];
123+
return hi;
122124
}
123125

124-
public double bound(int i) {
125-
return bounds[i];
126-
}
127-
128-
public double[] bounds() {
129-
return bounds;
130-
}
131-
132-
public void setLo(double p) {
133-
bounds[0] = p;
126+
public void setLo(double lo) {
127+
this.lo = lo;
134128
// assert (isValid());
135129
}
136130

137-
public void setHi(double p) {
138-
bounds[1] = p;
131+
public void setHi(double hi) {
132+
this.hi = hi;
139133
// assert (isValid());
140134
}
141135

@@ -148,7 +142,6 @@ public boolean isValid() {
148142
&& !(lo() == -S2.M_PI && hi() != S2.M_PI) && !(hi() == -S2.M_PI && lo() != S2.M_PI));
149143
}
150144

151-
152145
/** Return true if the interval contains all points on the unit circle. */
153146
public boolean isFull() {
154147
return hi() - lo() == 2 * S2.M_PI;
@@ -166,7 +159,6 @@ public boolean isInverted() {
166159
return lo() > hi();
167160
}
168161

169-
170162
/**
171163
* Return the midpoint of the interval. For full and empty intervals, the
172164
* result is arbitrary.
@@ -194,7 +186,6 @@ public double getLength() {
194186
return (length > 0) ? length : -1;
195187
}
196188

197-
198189
/**
199190
* Return the complement of the interior of the interval. An interval and its
200191
* complement have the same boundary but do not share any interior values. The
@@ -221,7 +212,6 @@ public boolean contains(double p) {
221212
return fastContains(p);
222213
}
223214

224-
225215
/**
226216
* Return true if the interval (which is closed) contains the point 'p'. Skips
227217
* the normalization of 'p' from -Pi to Pi.
@@ -235,7 +225,6 @@ public boolean fastContains(double p) {
235225
}
236226
}
237227

238-
239228
/** Return true if the interior of the interval contains the point 'p'. */
240229
public boolean interiorContains(double p) {
241230
// Works for empty, full, and singleton intervals.
@@ -251,7 +240,6 @@ public boolean interiorContains(double p) {
251240
}
252241
}
253242

254-
255243
/**
256244
* Return true if the interval contains the given interval 'y'. Works for
257245
* empty, full, and singleton intervals.
@@ -332,7 +320,6 @@ public boolean interiorIntersects(final S1Interval y) {
332320
}
333321
}
334322

335-
336323
/**
337324
* Expand the interval by the minimum amount necessary so that it contains the
338325
* given point "p" (an angle in the range [-Pi, Pi]).
@@ -388,7 +375,6 @@ public S1Interval expanded(double radius) {
388375
return result;
389376
}
390377

391-
392378
/**
393379
* Return the smallest interval that contains this interval and the given
394380
* interval "y".
@@ -432,7 +418,6 @@ public S1Interval union(final S1Interval y) {
432418
}
433419
}
434420

435-
436421
/**
437422
* Return the smallest interval that contains the intersection of this
438423
* interval with "y". Note that the region of intersection may consist of two
@@ -472,7 +457,6 @@ public S1Interval intersection(final S1Interval y) {
472457
return empty();
473458
}
474459

475-
476460
/**
477461
* Return true if the length of the symmetric difference between the two
478462
* intervals is at most the given tolerance.

src/com/google/common/geometry/S2Cell.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
*
2424
*/
2525

26-
public strictfp class S2Cell implements S2Region {
26+
public final strictfp class S2Cell implements S2Region {
2727

2828
private static final int MAX_CELL_SIZE = 1 << S2CellId.MAX_LEVEL;
2929

30-
// This structure occupies 44 bytes plus one pointer for the vtable.
3130
byte face;
3231
byte level;
3332
byte orientation;
@@ -182,17 +181,17 @@ public S2Point getCenterRaw() {
182181
public R2Vector getCenterUV() {
183182
MutableInteger i = new MutableInteger(0);
184183
MutableInteger j = new MutableInteger(0);
185-
R2Vector centerUv = new R2Vector();
186184
cellId.toFaceIJOrientation(i, j, null);
187185
int cellSize = 1 << (S2CellId.MAX_LEVEL - level);
188186

189-
int sij = (i.intValue() & -cellSize) * 2 + cellSize - MAX_CELL_SIZE;
190-
centerUv.x = S2Projections.stToUV((1.0 / MAX_CELL_SIZE) * sij);
187+
// TODO(dbeaumont): Figure out a better naming of the variables here (and elsewhere).
188+
int si = (i.intValue() & -cellSize) * 2 + cellSize - MAX_CELL_SIZE;
189+
double x = S2Projections.stToUV((1.0 / MAX_CELL_SIZE) * si);
191190

192-
sij = (j.intValue() & -cellSize) * 2 + cellSize - MAX_CELL_SIZE;
193-
centerUv.y = S2Projections.stToUV((1.0 / MAX_CELL_SIZE) * sij);
191+
int sj = (j.intValue() & -cellSize) * 2 + cellSize - MAX_CELL_SIZE;
192+
double y = S2Projections.stToUV((1.0 / MAX_CELL_SIZE) * sj);
194193

195-
return centerUv;
194+
return new R2Vector(x, y);
196195
}
197196

198197
/**
@@ -365,8 +364,8 @@ public boolean contains(S2Point p) {
365364
// We can't just call XYZtoFaceUV, because for points that lie on the
366365
// boundary between two faces (i.e. u or v is +1/-1) we need to return
367366
// true for both adjacent cells.
368-
R2Vector uvPoint = new R2Vector();
369-
if (!S2Projections.faceXyzToUv(face, p, uvPoint)) {
367+
R2Vector uvPoint = S2Projections.faceXyzToUv(face, p);
368+
if (uvPoint == null) {
370369
return false;
371370
}
372371
return (uvPoint.x >= uv[0][0] && uvPoint.x <= uv[0][1] && uvPoint.y >= uv[1][0]

src/com/google/common/geometry/S2CellId.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*
4949
*
5050
*/
51-
public strictfp class S2CellId implements Comparable<S2CellId> {
51+
public final strictfp class S2CellId implements Comparable<S2CellId> {
5252

5353
// Although only 60 bits are needed to represent the index of a leaf
5454
// cell, we need an extra bit in order to represent the position of
@@ -146,8 +146,8 @@ public static S2CellId fromFacePosLevel(int face, long pos, int level) {
146146
* necessarily unit length).
147147
*/
148148
public static S2CellId fromPoint(S2Point p) {
149-
R2Vector uv = new R2Vector();
150-
int face = S2Projections.xyzToFaceUV(p, uv);
149+
int face = S2Projections.xyzToFace(p);
150+
R2Vector uv = S2Projections.validFaceXyzToUv(face, p);
151151
int i = stToIJ(S2Projections.uvToST(uv.x));
152152
int j = stToIJ(S2Projections.uvToST(uv.y));
153153
return fromFaceIJ(face, i, j);
@@ -863,8 +863,9 @@ private static S2CellId fromFaceIJWrap(int face, int i, int j) {
863863

864864
// Find the leaf cell coordinates on the adjacent face, and convert
865865
// them to a cell id at the appropriate level.
866-
R2Vector st = new R2Vector();
867-
face = S2Projections.xyzToFaceUV(S2Projections.faceUvToXyz(face, s, t), st);
866+
S2Point p = S2Projections.faceUvToXyz(face, s, t);
867+
face = S2Projections.xyzToFace(p);
868+
R2Vector st = S2Projections.validFaceXyzToUv(face, p);
868869
return fromFaceIJ(face, stToIJ(st.x), stToIJ(st.y));
869870
}
870871

@@ -960,5 +961,4 @@ public int compareTo(S2CellId that) {
960961
return unsignedLongLessThan(this.id, that.id) ? -1 :
961962
unsignedLongGreaterThan(this.id, that.id) ? 1 : 0;
962963
}
963-
964964
}

0 commit comments

Comments
 (0)