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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429 | 1x
1x
1x
1065x
704x
704x
361x
361x
1x
1x
1x
1x
520x
1x
200x
1x
171x
171x
170x
1x
1x
176x
176x
176x
1x
1x
1x
1x
13x
13x
13x
1x
18x
18x
18x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
3x
3x
3x
1x
1x
5x
5x
5x
5x
1x
176x
176x
176x
176x
176x
176x
176x
1x
1x
169x
169x
169x
169x
1x
2x
1x
2x
1x
1x
338x
338x
338x
338x
338x
253x
85x
1x
338x
338x
338x
338x
253x
85x
1x | import {Interval} from "./Interval";
import {S2} from "./S2";
export class S1Interval extends Interval {
constructor(lo:number|decimal.Decimal, hi:number|decimal.Decimal, checked:boolean = false) {
super(lo, hi);
if (!checked) {
Iif (this.lo.eq(-S2.M_PI) && !this.hi.eq(S2.M_PI)) {
this.lo = S2.toDecimal(S2.M_PI);
}
Iif (this.hi.eq(-S2.M_PI) && !this.lo.eq(S2.M_PI)) {
this.hi = S2.toDecimal(S2.M_PI);
}
}
}
/**
* An interval is valid if neither bound exceeds Pi in absolute value, and the
* value -Pi appears only in the Empty() and Full() intervals.
*/
isValid():boolean {
return this.lo.abs().lte(S2.M_PI) && this.hi.abs().lte(S2.M_PI)
&& !(this.lo.eq(-S2.M_PI) && !this.hi.eq(S2.M_PI))
&& !(this.hi.eq(-S2.M_PI) && !this.lo.eq(S2.M_PI));
// return (Math.abs(this.lo) <= S2.M_PI && Math.abs(this.hi) <= S2.M_PI
// && !(this.lo == -S2.M_PI && this.hi != S2.M_PI) && !(this.hi == -S2.M_PI && this.lo != S2.M_PI));
}
/** Return true if the interval contains all points on the unit circle. */
isFull() {
// console.log(this.hi.minus(this.lo).eq(2 * S2.M_PI));
return this.hi.minus(this.lo).eq(2 * S2.M_PI)
}
/** Return true if the interval is empty, i.e. it contains no points. */
public isEmpty() {
return this.lo.minus(this.hi).eq(2 * S2.M_PI);
}
/* Return true if this.lo > this.hi. (This is true for empty intervals.) */
public isInverted():boolean {
return this.lo.gt(this.hi);
}
/**
* Return the midpoint of the interval. For full and empty intervals, the
* result is arbitrary.
*/
public getCenter():decimal.Decimal {
let center = this.lo.plus(this.hi).dividedBy(2);
// let center = 0.5 * (this.lo + this.hi);
if (!this.isInverted()) {
return center;
}
// Return the center in the range (-Pi, Pi].
return (center.lte(0)) ? (center.plus(S2.M_PI)) : (center.minus(S2.M_PI));
}
/**
* Return the length of the interval. The length of an empty interval is
* negative.
*/
public getLength():decimal.Decimal {
let length = this.hi.minus(this.lo);
Eif (length.gte(0)) {
return length;
}
length = length.plus(2 * S2.M_PI);
// Empty intervals have a negative length.
return (length.gt(0)) ? length : S2.toDecimal(-1);
}
/**
* Return the complement of the interior of the interval. An interval and its
* complement have the same boundary but do not share any interior values. The
* complement operator is not a bijection, since the complement of a singleton
* interval (containing a single value) is the same as the complement of an
* empty interval.
*/
public complement():S1Interval {
Iif (this.lo.eq(this.hi)) {
return S1Interval.full(); // Singleton.
}
return new S1Interval(this.hi, this.lo, true); // Handles
// empty and
// full.
}
/** Return true if the interval (which is closed) contains the point 'p'. */
public contains(_p:number|decimal.Decimal):boolean {
let p = S2.toDecimal(_p);
// Works for empty, full, and singleton intervals.
// assert (Math.abs(p) <= S2.M_PI);
Iif (p.eq(-S2.M_PI)) {
p = S2.toDecimal(S2.M_PI);
}
return this.fastContains(p);
}
/**
* Return true if the interval (which is closed) contains the point 'p'. Skips
* the normalization of 'p' from -Pi to Pi.
*
*/
public fastContains(_p:number|decimal.Decimal):boolean {
const p = S2.toDecimal(_p);
Iif (this.isInverted()) {
return (p.gte(this.lo) || p.lte(this.hi)) && !this.isEmpty();
} else {
return p.gte(this.lo) && p.lte(this.hi);
}
}
/** Return true if the interior of the interval contains the point 'p'. */
public interiorContains(_p:number|decimal.Decimal):boolean {
// Works for empty, full, and singleton intervals.
// assert (Math.abs(p) <= S2.M_PI);
let p = S2.toDecimal(_p);
Iif (p.eq(-S2.M_PI)) {
p = S2.toDecimal(S2.M_PI);
}
Iif (this.isInverted()) {
return p.gt(this.lo) || p.lt(this.hi);
} else {
return (p.gt(this.lo) && p.lt(this.hi)) || this.isFull();
}
}
/**
* Return true if the interval contains the given interval 'y'. Works for
* empty, full, and singleton intervals.
*/
public containsI(y:S1Interval):boolean {
// It might be helpful to compare the structure of these tests to
// the simpler Contains(number) method above.
Iif (this.isInverted()) {
if (y.isInverted()) {
return y.lo.gte(this.lo) && y.hi.lte(this.hi);
}
return (y.lo.gte(this.lo) || y.hi.lte(this.hi)) && !this.isEmpty();
} else {
Iif (y.isInverted()) {
return this.isFull() || y.isEmpty();
}
return y.lo.gte(this.lo) && y.hi.lte(this.hi);
}
}
/**
* Returns true if the interior of this interval contains the entire interval
* 'y'. Note that x.InteriorContains(x) is true only when x is the empty or
* full interval, and x.InteriorContains(S1Interval(p,p)) is equivalent to
* x.InteriorContains(p).
*/
public interiorContainsI(y:S1Interval):boolean {
Iif (this.isInverted()) {
if (!y.isInverted()) {
return this.lo.gt(this.lo) || y.hi.lt(this.hi);
}
return (y.lo.gt(this.lo) && y.hi.lt(this.hi)) || y.isEmpty();
} else {
Iif (y.isInverted()) {
return this.isFull() || y.isEmpty();
}
return (y.lo.gt(this.lo) && y.hi.lt(this.hi)) || this.isFull();
}
}
/**
* Return true if the two intervals contain any points in common. Note that
* the point +/-Pi has two representations, so the intervals [-Pi,-3] and
* [2,Pi] intersect, for example.
*/
public intersects(y:S1Interval):boolean {
Iif (this.isEmpty() || y.isEmpty()) {
return false;
}
Iif (this.isInverted()) {
// Every non-empty inverted interval contains Pi.
return y.isInverted() || y.lo.lte(this.hi) || y.hi.gte(this.lo);
} else {
Iif (y.isInverted()) {
return y.lo.lte(this.hi) || y.hi.gte(this.lo);
}
return y.lo.lte(this.hi) && y.hi.gte(this.lo);
}
}
/**
* Return true if the interior of this interval contains any point of the
* interval 'y' (including its boundary). Works for empty, full, and singleton
* intervals.
*/
public interiorIntersects(y:S1Interval):boolean {
if (this.isEmpty() || y.isEmpty() || this.lo.eq(this.hi)) {
return false;
}
if (this.isInverted()) {
return y.isInverted() || y.lo.lt(this.hi) || y.hi.gt(this.lo);
} else {
if (y.isInverted()) {
return y.lo.lt(this.hi) || y.hi.gt(this.lo);
}
return (y.lo.lt(this.hi) && y.hi.gt(this.lo)) || this.isFull();
}
}
/**
* Expand the interval by the minimum amount necessary so that it contains the
* given point "p" (an angle in the range [-Pi, Pi]).
*/
public addPoint(_p:number|decimal.Decimal):S1Interval {
let p = S2.toDecimal(_p);
// assert (Math.abs(p) <= S2.M_PI);
Iif (p.eq(-S2.M_PI)) {
p = S2.toDecimal(S2.M_PI);
}
Eif (this.fastContains(p)) {
return new S1Interval(this.lo, this.hi);
}
if (this.isEmpty()) {
return S1Interval.fromPoint(p);
} else {
// Compute distance from p to each endpoint.
let dlo = S1Interval.positiveDistance(p, this.lo);
let dhi = S1Interval.positiveDistance(this.hi, p);
if (dlo.lt(dhi)) {
return new S1Interval(p, this.hi);
} else {
return new S1Interval(this.lo, p);
}
// Adding a point can never turn a non-full interval into a full one.
}
}
/**
* 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 expanded(_radius:number|decimal.Decimal):S1Interval {
const radius = S2.toDecimal(_radius);
// assert (radius >= 0);
Iif (this.isEmpty()) {
return this;
}
// Check whether this interval will be full after expansion, allowing
// for a 1-bit rounding error when computing each endpoint.
Iif (this.getLength().plus(radius.times(2)).gte(2*S2.M_PI-1e-15)) {
return S1Interval.full();
}
// NOTE(dbeaumont): Should this remainder be 2 * M_PI or just M_PI ??
let lo = S2.IEEEremainder(this.lo.minus(radius), 2 * S2.M_PI);
let hi = S2.IEEEremainder(this.hi.plus(radius), 2 * S2.M_PI);
Iif (lo.eq(-S2.M_PI)) {
lo = S2.toDecimal(S2.M_PI);
}
return new S1Interval(lo, hi);
}
/**
* Return the smallest interval that contains this interval and the given
* interval "y".
*/
public union(y:S1Interval):S1Interval {
// The y.is_full() case is handled correctly in all cases by the code
// below, but can follow three separate code paths depending on whether
// this interval is inverted, is non-inverted but contains Pi, or neither.
if (y.isEmpty()) {
return this;
}
if (this.fastContains(y.lo)) {
if (this.fastContains(y.hi)) {
// Either this interval contains y, or the union of the two
// intervals is the Full() interval.
if (this.containsI(y)) {
return this; // is_full() code path
}
return S1Interval.full();
}
return new S1Interval(this.lo, this.hi, true);
}
if (this.fastContains(y.hi)) {
return new S1Interval(y.lo, this.hi, true);
}
// This interval contains neither endpoint of y. This means that either y
// contains all of this interval, or the two intervals are disjoint.
if (this.isEmpty() || y.fastContains(this.lo)) {
return y;
}
// Check which pair of endpoints are closer together.
let dlo = S1Interval.positiveDistance(y.hi, this.lo);
let dhi = S1Interval.positiveDistance(this.hi, y.lo);
if (dlo < dhi) {
return new S1Interval(y.lo, this.hi, true);
} else {
return new S1Interval(this.lo, y.hi, true);
}
}
/**
* Return the smallest interval that contains the intersection of this
* interval with "y". Note that the region of intersection may consist of two
* disjoint intervals.
*/
public intersection(y:S1Interval):S1Interval {
// The y.is_full() case is handled correctly in all cases by the code
// below, but can follow three separate code paths depending on whether
// this interval is inverted, is non-inverted but contains Pi, or neither.
if (y.isEmpty()) {
return S1Interval.empty();
}
if (this.fastContains(y.lo)) {
if (this.fastContains(y.hi)) {
// Either this interval contains y, or the region of intersection
// consists of two disjoint subintervals. In either case, we want
// to return the shorter of the two original intervals.
if (y.getLength().lt(this.getLength())) {
return y; // is_full() code path
}
return this;
}
return new S1Interval(y.lo, this.hi, true);
}
if (this.fastContains(y.hi)) {
return new S1Interval(this.lo, y.hi, true);
}
// This interval contains neither endpoint of y. This means that either y
// contains all of this interval, or the two intervals are disjoint.
if (y.fastContains(this.lo)) {
return this; // is_empty() okay here
}
// assert (!intersects(y));
return S1Interval.empty();
}
/**
* Return true if the length of the symmetric difference between the two
* intervals is at most the given tolerance.
*/
public approxEquals(y:S1Interval, ImaxError:number=1e-9):boolean {
Iif (this.isEmpty()) {
return y.getLength().lte(maxError);
}
Iif (y.isEmpty()) {
return this.getLength().lte(maxError);
}
return S2.IEEEremainder(y.lo.minus(this.lo), 2 * S2.M_PI).abs()
.plus(S2.IEEEremainder(y.hi.minus(this.hi), 2 * S2.M_PI).abs())
.lte(maxError);
}
static empty():S1Interval {
return new S1Interval(S2.M_PI, -S2.M_PI, true);
}
static full():S1Interval {
return new S1Interval(-S2.M_PI, S2.M_PI, true);
}
static fromPoint(_p:number|decimal.Decimal):S1Interval {
let p = S2.toDecimal(_p);
if (p.eq(-S2.M_PI)) {
p = S2.toDecimal(S2.M_PI);
}
return new S1Interval(p, p, true);
}
/**
* 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.
*/
static fromPointPair(_p1:number|decimal.Decimal, _p2:number|decimal.Decimal):S1Interval {
// assert (Math.abs(p1) <= S2.M_PI && Math.abs(p2) <= S2.M_PI);
let p1 = S2.toDecimal(_p1);
let p2 = S2.toDecimal(_p2);
Iif (p1.eq(-S2.M_PI)) {
p1 = S2.toDecimal(S2.M_PI);
}
Iif (p2.eq(-S2.M_PI)) {
p2 = S2.toDecimal(S2.M_PI);
}
if (S1Interval.positiveDistance(p1, p2).lte(S2.M_PI)) {
return new S1Interval(p1, p2, true);
} else {
return new S1Interval(p2, p1, true);
}
}
/**
* Compute the distance from "a" to "b" in the range [0, 2*Pi). This is
* equivalent to (drem(b - a - S2.M_PI, 2 * S2.M_PI) + S2.M_PI), except that
* it is more numerically stable (it does not lose precision for very small
* positive distances).
*/
public static positiveDistance(_a:number|decimal.Decimal, _b:number|decimal.Decimal):decimal.Decimal {
const a = S2.toDecimal(_a);
const b = S2.toDecimal(_b);
let d = b.minus(a);
if (d .gte(0)) {
return d;
}
// We want to ensure that if b == Pi and a == (-Pi + eps),
// the return result is approximately 2*Pi and not zero.
return b.plus(S2.M_PI).minus(a.minus(S2.M_PI));
}
} |