summaryrefslogtreecommitdiff
path: root/src/com/google/common/geometry/R1Interval.java
blob: e8edbe4a9303d100fe4d3d6be533a77554cf7253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright 2005 Google Inc.
 *
 * Licensed 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 com.google.common.geometry;

/**
 * An R1Interval represents a closed, bounded interval on the real line. It is
 * capable of representing the empty interval (containing no points) and
 * zero-length intervals (containing a single point).
 *
 */

public strictfp class R1Interval {
  private final double[] bounds = new double[2];

  /** Interval constructor. If lo > hi, the interval is empty. */
  public R1Interval(double lo, double hi) {
    bounds[0] = lo;
    bounds[1] = hi;
  }

  /**
   * Returns an empty interval. (Any interval where lo > hi is considered
   * empty.)
   */
  public static R1Interval empty() {
    return new R1Interval(1, 0);
  }

  /**
   * Convenience method to construct an interval containing a single point.
   */
  public static R1Interval fromPoint(double p) {
    return new R1Interval(p, p);
  }

  /**
   * Convenience method to construct the minimal interval containing the two
   * given points. This is equivalent to starting with an empty interval and
   * calling AddPoint() twice, but it is more efficient.
   */
  public static R1Interval fromPointPair(double p1, double p2) {
    if (p1 <= p2) {
      return new R1Interval(p1, p2);
    } else {
      return new R1Interval(p2, p1);
    }
  }

  public double lo() {
    return bounds[0];
  }

  public double hi() {
    return bounds[1];
  }

  public double bound(int i) {
    return bounds[i];
  }

  public double[] bounds() {
    return bounds;
  }

  public void setLo(double p) {
    bounds[0] = p;
  }

  public void setHi(double p) {
    bounds[1] = p;
  }

  /**
   * Return true if the interval is empty, i.e. it contains no points.
   */
  public boolean isEmpty() {
    return lo() > hi();
  }

  /**
   * Return the center of the interval. For empty intervals, the result is
   * arbitrary.
   */
  public double getCenter() {
    return 0.5 * (lo() + hi());
  }


  /**
   * Return the length of the interval. The length of an empty interval is
   * negative.
   */
  public double getLength() {
    return hi() - lo();
  }


  public boolean contains(double p) {
    return p >= lo() && p <= hi();
  }

  public boolean interiorContains(double p) {
    return p > lo() && p < hi();
  }

  /** Return true if this interval contains the interval 'y'. */
  public boolean contains(R1Interval y) {
    if (y.isEmpty()) {
      return true;
    }
    return y.lo() >= lo() && y.hi() <= hi();
  }

  /**
   * Return true if the interior of this interval contains the entire interval
   * 'y' (including its boundary).
   */
  public boolean interiorContains(R1Interval y) {
    if (y.isEmpty()) {
      return true;
    }
    return y.lo() > lo() && y.hi() < hi();
  }

  /**
   * Return true if this interval intersects the given interval, i.e. if they
   * have any points in common.
   */
  public boolean intersects(R1Interval y) {
    if (lo() <= y.lo()) {
      return y.lo() <= hi() && y.lo() <= y.hi();
    } else {
      return lo() <= y.hi() && lo() <= hi();
    }
  }

  /**
   * Return true if the interior of this interval intersects any point of the
   * given interval (including its boundary).
   */
  public boolean interiorIntersects(R1Interval y) {
    return y.lo() < hi() && lo() < y.hi() && lo() < hi() && y.lo() <= y.hi();
  }

  /** Expand the interval so that it contains the given point "p". */
  public R1Interval addPoint(double p) {
    if (isEmpty()) {
      return R1Interval.fromPoint(p);
    } else if (p < lo()) {
      return new R1Interval(p, hi());
    } else if (p > hi()) {
      return new R1Interval(lo(), p);
    } else {
      return new R1Interval(lo(), hi());
    }
  }

  /**
   * 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
   * always empty.
   */
  public R1Interval expanded(double radius) {
    // assert (radius >= 0);
    if (isEmpty()) {
      return this;
    }
    return new R1Interval(lo() - radius, hi() + radius);
  }

  /**
   * Return the smallest interval that contains this interval and the given
   * interval "y".
   */
  public R1Interval union(R1Interval y) {
    if (isEmpty()) {
      return y;
    }
    if (y.isEmpty()) {
      return this;
    }
    return new R1Interval(Math.min(lo(), y.lo()), Math.max(hi(), y.hi()));
  }

  /**
   * Return the intersection of this interval with the given interval. Empty
   * intervals do not need to be special-cased.
   */
  public R1Interval intersection(R1Interval y) {
    return new R1Interval(Math.max(lo(), y.lo()), Math.min(hi(), y.hi()));
  }

  @Override
  public boolean equals(Object that) {
    if (that instanceof R1Interval) {
      R1Interval y = (R1Interval) that;
      // Return true if two intervals contain the same set of points.
      return (lo() == y.lo() && hi() == y.hi()) || (isEmpty() && y.isEmpty());

    }
    return false;
  }

  @Override
  public int hashCode() {
    if (isEmpty()) {
      return 17;
    }

    long value = 17;
    value = 37 * value + Double.doubleToLongBits(bounds[0]);
    value = 37 * value + Double.doubleToLongBits(bounds[1]);
    return (int) (value ^ (value >>> 32));
  }

  public boolean approxEquals(R1Interval y) {
    return approxEquals(y, 1e-15);
  }

  /**
   * Return true if length of the symmetric difference between the two intervals
   * is at most the given tolerance.
   *
   */
  public boolean approxEquals(R1Interval y, double maxError) {
    if (isEmpty()) {
      return y.getLength() <= maxError;
    }
    if (y.isEmpty()) {
      return getLength() <= maxError;
    }
    return Math.abs(y.lo() - lo()) + Math.abs(y.hi() - hi()) <= maxError;
  }

  @Override
  public String toString() {
    return "[" + lo() + ", " + hi() + "]";
  }
}