• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-S2 / gis-tools / #106

16 Dec 2025 08:53AM UTC coverage: 92.044% (-0.004%) from 92.048%
#106

push

Mr Martian
pointcluster needs to allow get_tile to functionally match TileSource

6 of 9 new or added lines in 1 file covered. (66.67%)

2 existing lines in 1 file now uncovered.

97654 of 106095 relevant lines covered (92.04%)

433887.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.6
/src/geometry/predicates/orient3d.ts
1
import { estimate, predSum, resulterrbound, scale, splitter, vec } from './util.js';
168✔
2

3
import type { VectorPoint } from 's2json-spec';
4

5
const o3derrboundA = 7.771561172376103e-16; // (7 + 56 * epsilon) * epsilon;
110✔
6
const o3derrboundB = 3.330669073875473e-16; // (3 + 28 * epsilon) * epsilon;
110✔
7
const o3derrboundC = 3.2047474274603644e-31; // (26 + 288 * epsilon) * epsilon * epsilon;
142✔
8

9
/** Constants for orient3d */
10
export interface Orient3dConstants {
11
  bc: Float64Array;
12
  ca: Float64Array;
13
  ab: Float64Array;
14
  at_b: Float64Array;
15
  at_c: Float64Array;
16
  bt_c: Float64Array;
17
  bt_a: Float64Array;
18
  ct_a: Float64Array;
19
  ct_b: Float64Array;
20
  bct: Float64Array;
21
  cat: Float64Array;
22
  abt: Float64Array;
23
  u: Float64Array;
24

25
  _8: Float64Array;
26
  _8b: Float64Array;
27
  _16: Float64Array;
28
  _12: Float64Array;
29

30
  fin: Float64Array;
31
  fin2: Float64Array;
32
}
33

34
let constants: Orient3dConstants | undefined;
26✔
35

36
/**
37
 * build constants for future reuse
38
 * @returns - the constants
39
 */
40
function buildConstants(): Orient3dConstants {
12✔
41
  return {
24✔
42
    bc: vec(4),
30✔
43
    ca: vec(4),
30✔
44
    ab: vec(4),
30✔
45
    at_b: vec(4),
34✔
46
    at_c: vec(4),
34✔
47
    bt_c: vec(4),
34✔
48
    bt_a: vec(4),
34✔
49
    ct_a: vec(4),
34✔
50
    ct_b: vec(4),
34✔
51
    bct: vec(8),
32✔
52
    cat: vec(8),
32✔
53
    abt: vec(8),
32✔
54
    u: vec(4),
28✔
55

56
    _8: vec(8),
30✔
57
    _8b: vec(8),
32✔
58
    _16: vec(8),
32✔
59
    _12: vec(12),
34✔
60

61
    fin: vec(192),
36✔
62
    fin2: vec(192),
32✔
63
  };
2✔
64
}
65

66
/**
67
 * add to fin
68
 * @param finlen - length of array
69
 * @param alen - length of array
70
 * @param a - array
71
 * @param constants - constants
72
 * @returns - updated finlen
73
 */
74
function finadd(
2✔
75
  finlen: number,
16✔
76
  alen: number,
12✔
77
  a: number[] | Float64Array,
6✔
78
  constants: Orient3dConstants,
22✔
79
): number {
6✔
80
  let { fin, fin2 } = constants;
64✔
81
  finlen = predSum(finlen, fin, alen, a, fin2);
94✔
82
  const tmp = fin;
36✔
83
  fin = fin2;
26✔
84
  fin2 = tmp;
26✔
85
  return finlen;
26✔
86
}
87

88
/**
89
 * initialize the tail
90
 * @param xtail - xtail
91
 * @param ytail - ytail
92
 * @param ax - point A.x
93
 * @param ay - point A.y
94
 * @param bx - point B.x
95
 * @param by - point B.y
96
 * @param a - a values
97
 * @param b - b values
98
 * @returns - 4, 1, or 2 based on the length of the tail
99
 */
100
function tailinit(
2✔
101
  xtail: number,
14✔
102
  ytail: number,
14✔
103
  ax: number,
8✔
104
  ay: number,
8✔
105
  bx: number,
8✔
106
  by: number,
8✔
107
  a: number[] | Float64Array,
6✔
108
  b: number[] | Float64Array,
6✔
109
) {
6✔
110
  let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, s1, s0, t1, t0, u3, negate;
158✔
111
  if (xtail === 0) {
42✔
112
    if (ytail === 0) {
46✔
113
      a[0] = 0;
30✔
114
      b[0] = 0;
30✔
115
      return 1;
16✔
116
    } else {
26✔
117
      negate = -ytail;
44✔
118
      s1 = negate * ax;
46✔
119
      c = splitter * negate;
56✔
120
      ahi = c - (c - negate);
58✔
121
      alo = negate - ahi;
50✔
122
      c = splitter * ax;
48✔
123
      bhi = c - (c - ax);
50✔
124
      blo = ax - bhi;
42✔
125
      a[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
132✔
126
      a[1] = s1;
32✔
127
      s1 = ytail * bx;
44✔
128
      c = splitter * ytail;
54✔
129
      ahi = c - (c - ytail);
56✔
130
      alo = ytail - ahi;
48✔
131
      c = splitter * bx;
48✔
132
      bhi = c - (c - bx);
50✔
133
      blo = bx - bhi;
42✔
134
      b[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
132✔
135
      b[1] = s1;
32✔
136
      return 2;
16✔
137
    }
138
  } else {
22✔
139
    if (ytail === 0) {
46✔
140
      s1 = xtail * ay;
44✔
141
      c = splitter * xtail;
54✔
142
      ahi = c - (c - xtail);
56✔
143
      alo = xtail - ahi;
48✔
144
      c = splitter * ay;
48✔
145
      bhi = c - (c - ay);
50✔
146
      blo = ay - bhi;
42✔
147
      a[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
132✔
148
      a[1] = s1;
32✔
149
      negate = -xtail;
44✔
150
      s1 = negate * by;
46✔
151
      c = splitter * negate;
56✔
152
      ahi = c - (c - negate);
58✔
153
      alo = negate - ahi;
50✔
154
      c = splitter * by;
48✔
155
      bhi = c - (c - by);
50✔
156
      blo = by - bhi;
42✔
157
      b[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
132✔
158
      b[1] = s1;
32✔
159
      return 2;
16✔
160
    } else {
26✔
161
      s1 = xtail * ay;
44✔
162
      c = splitter * xtail;
54✔
163
      ahi = c - (c - xtail);
56✔
164
      alo = xtail - ahi;
48✔
165
      c = splitter * ay;
48✔
166
      bhi = c - (c - ay);
50✔
167
      blo = ay - bhi;
42✔
168
      s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
128✔
169
      t1 = ytail * ax;
44✔
170
      c = splitter * ytail;
54✔
171
      ahi = c - (c - ytail);
56✔
172
      alo = ytail - ahi;
48✔
173
      c = splitter * ax;
48✔
174
      bhi = c - (c - ax);
50✔
175
      blo = ax - bhi;
42✔
176
      t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
128✔
177
      _i = s0 - t0;
38✔
178
      bvirt = s0 - _i;
44✔
179
      a[0] = s0 - (_i + bvirt) + (bvirt - t0);
92✔
180
      _j = s1 + _i;
38✔
181
      bvirt = _j - s1;
44✔
182
      _0 = s1 - (_j - bvirt) + (_i - bvirt);
88✔
183
      _i = _0 - t1;
38✔
184
      bvirt = _0 - _i;
44✔
185
      a[1] = _0 - (_i + bvirt) + (bvirt - t1);
92✔
186
      u3 = _j + _i;
38✔
187
      bvirt = u3 - _j;
44✔
188
      a[2] = _j - (u3 - bvirt) + (_i - bvirt);
92✔
189
      a[3] = u3;
32✔
190
      s1 = ytail * bx;
44✔
191
      c = splitter * ytail;
54✔
192
      ahi = c - (c - ytail);
56✔
193
      alo = ytail - ahi;
48✔
194
      c = splitter * bx;
48✔
195
      bhi = c - (c - bx);
50✔
196
      blo = bx - bhi;
42✔
197
      s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
128✔
198
      t1 = xtail * by;
44✔
199
      c = splitter * xtail;
54✔
200
      ahi = c - (c - xtail);
56✔
201
      alo = xtail - ahi;
48✔
202
      c = splitter * by;
48✔
203
      bhi = c - (c - by);
50✔
204
      blo = by - bhi;
42✔
205
      t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
128✔
206
      _i = s0 - t0;
38✔
207
      bvirt = s0 - _i;
44✔
208
      b[0] = s0 - (_i + bvirt) + (bvirt - t0);
92✔
209
      _j = s1 + _i;
38✔
210
      bvirt = _j - s1;
44✔
211
      _0 = s1 - (_j - bvirt) + (_i - bvirt);
88✔
212
      _i = _0 - t1;
38✔
213
      bvirt = _0 - _i;
44✔
214
      b[1] = _0 - (_i + bvirt) + (bvirt - t1);
92✔
215
      u3 = _j + _i;
38✔
216
      bvirt = u3 - _j;
44✔
217
      b[2] = _j - (u3 - bvirt) + (_i - bvirt);
92✔
218
      b[3] = u3;
32✔
219
      return 4;
16✔
220
    }
221
  }
222
}
223

224
/**
225
 * Add to the tail of the sum
226
 * @param finlen - length of array
227
 * @param a - a
228
 * @param b - b
229
 * @param k - k
230
 * @param z - z
231
 * @returns - updated finlen
232
 */
233
function tailadd(finlen: number, a: number, b: number, k: number, z: number): number {
48✔
234
  if (constants === undefined) constants = buildConstants();
66✔
235
  const { u } = constants;
52✔
236
  let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, u3;
110✔
237
  const s1 = a * b;
38✔
238
  c = splitter * a;
38✔
239
  ahi = c - (c - a);
40✔
240
  alo = a - ahi;
32✔
241
  c = splitter * b;
38✔
242
  bhi = c - (c - b);
40✔
243
  blo = b - bhi;
32✔
244
  const s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
132✔
245
  c = splitter * k;
38✔
246
  bhi = c - (c - k);
40✔
247
  blo = k - bhi;
32✔
248
  _i = s0 * k;
28✔
249
  c = splitter * s0;
40✔
250
  ahi = c - (c - s0);
42✔
251
  alo = s0 - ahi;
34✔
252
  u[0] = alo * blo - (_i - ahi * bhi - alo * bhi - ahi * blo);
124✔
253
  _j = s1 * k;
28✔
254
  c = splitter * s1;
40✔
255
  ahi = c - (c - s1);
42✔
256
  alo = s1 - ahi;
34✔
257
  _0 = alo * blo - (_j - ahi * bhi - alo * bhi - ahi * blo);
120✔
258
  _k = _i + _0;
30✔
259
  bvirt = _k - _i;
36✔
260
  u[1] = _i - (_k - bvirt) + (_0 - bvirt);
84✔
261
  u3 = _j + _k;
30✔
262
  u[2] = _k - (u3 - _j);
48✔
263
  u[3] = u3;
24✔
264
  finlen = finadd(finlen, 4, u, constants);
86✔
265
  if (z !== 0) {
34✔
266
    c = splitter * z;
42✔
267
    bhi = c - (c - z);
44✔
268
    blo = z - bhi;
36✔
269
    _i = s0 * z;
32✔
270
    c = splitter * s0;
44✔
271
    ahi = c - (c - s0);
46✔
272
    alo = s0 - ahi;
38✔
273
    u[0] = alo * blo - (_i - ahi * bhi - alo * bhi - ahi * blo);
128✔
274
    _j = s1 * z;
32✔
275
    c = splitter * s1;
44✔
276
    ahi = c - (c - s1);
46✔
277
    alo = s1 - ahi;
38✔
278
    _0 = alo * blo - (_j - ahi * bhi - alo * bhi - ahi * blo);
124✔
279
    _k = _i + _0;
34✔
280
    bvirt = _k - _i;
40✔
281
    u[1] = _i - (_k - bvirt) + (_0 - bvirt);
88✔
282
    u3 = _j + _k;
34✔
283
    u[2] = _k - (u3 - _j);
52✔
284
    u[3] = u3;
28✔
285
    finlen = finadd(finlen, 4, u, constants);
86✔
286
  }
4✔
287

288
  return finlen;
26✔
289
}
290

291
/**
292
 * Get the 3D orientation of the point-plane of a-b-c via d
293
 * @param ax - x coordinate of first point
294
 * @param ay - y coordinate of first point
295
 * @param az - z coordinate of first point
296
 * @param bx - x coordinate of second point
297
 * @param by - y coordinate of second point
298
 * @param bz - z coordinate of second point
299
 * @param cx - x coordinate of origin point to create the abc plane
300
 * @param cy - y coordinate of origin point to create the abc plane
301
 * @param cz - z coordinate of origin point to create the abc plane
302
 * @param dx - x coordinate of compare point
303
 * @param dy - y coordinate of compare point
304
 * @param dz - z coordinate of compare point
305
 * @param permanent - if the point-plane of a-b-c via d is permanent
306
 * @returns - a positive value if the point-plane of a-b-c via d occur in counterclockwise order
307
 * (c lies to the left of the directed line defined by points a and b).
308
 * - Returns a negative value if they occur in clockwise order (c lies to the right of the directed line ab).
309
 * - Returns zero if they are collinear.
310
 */
311
function orient3dAdapt(
2✔
312
  ax: number,
8✔
313
  ay: number,
8✔
314
  az: number,
8✔
315
  bx: number,
8✔
316
  by: number,
8✔
317
  bz: number,
8✔
318
  cx: number,
8✔
319
  cy: number,
8✔
320
  cz: number,
8✔
321
  dx: number,
8✔
322
  dy: number,
8✔
323
  dz: number,
8✔
324
  permanent: number,
22✔
325
): number {
6✔
326
  if (constants === undefined) constants = buildConstants();
122✔
327
  const { bc, ca, ab, _8, _8b, _16, _12, fin, at_b, at_c, bt_c, bt_a, ct_a, ct_b, bct, cat, abt } =
196✔
328
    constants;
24✔
329
  let finlen;
26✔
330
  let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, s1, s0, t1, t0, u3;
142✔
331

332
  const adx = ax - dx;
44✔
333
  const bdx = bx - dx;
44✔
334
  const cdx = cx - dx;
44✔
335
  const ady = ay - dy;
44✔
336
  const bdy = by - dy;
44✔
337
  const cdy = cy - dy;
44✔
338
  const adz = az - dz;
44✔
339
  const bdz = bz - dz;
44✔
340
  const cdz = cz - dz;
44✔
341

342
  s1 = bdx * cdy;
34✔
343
  c = splitter * bdx;
42✔
344
  ahi = c - (c - bdx);
44✔
345
  alo = bdx - ahi;
36✔
346
  c = splitter * cdy;
42✔
347
  bhi = c - (c - cdy);
44✔
348
  blo = cdy - bhi;
36✔
349
  s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
350
  t1 = cdx * bdy;
34✔
351
  c = splitter * cdx;
42✔
352
  ahi = c - (c - cdx);
44✔
353
  alo = cdx - ahi;
36✔
354
  c = splitter * bdy;
42✔
355
  bhi = c - (c - bdy);
44✔
356
  blo = bdy - bhi;
36✔
357
  t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
358
  _i = s0 - t0;
30✔
359
  bvirt = s0 - _i;
36✔
360
  bc[0] = s0 - (_i + bvirt) + (bvirt - t0);
86✔
361
  _j = s1 + _i;
30✔
362
  bvirt = _j - s1;
36✔
363
  _0 = s1 - (_j - bvirt) + (_i - bvirt);
80✔
364
  _i = _0 - t1;
30✔
365
  bvirt = _0 - _i;
36✔
366
  bc[1] = _0 - (_i + bvirt) + (bvirt - t1);
86✔
367
  u3 = _j + _i;
30✔
368
  bvirt = u3 - _j;
36✔
369
  bc[2] = _j - (u3 - bvirt) + (_i - bvirt);
86✔
370
  bc[3] = u3;
26✔
371
  s1 = cdx * ady;
34✔
372
  c = splitter * cdx;
42✔
373
  ahi = c - (c - cdx);
44✔
374
  alo = cdx - ahi;
36✔
375
  c = splitter * ady;
42✔
376
  bhi = c - (c - ady);
44✔
377
  blo = ady - bhi;
36✔
378
  s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
379
  t1 = adx * cdy;
34✔
380
  c = splitter * adx;
42✔
381
  ahi = c - (c - adx);
44✔
382
  alo = adx - ahi;
36✔
383
  c = splitter * cdy;
42✔
384
  bhi = c - (c - cdy);
44✔
385
  blo = cdy - bhi;
36✔
386
  t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
387
  _i = s0 - t0;
30✔
388
  bvirt = s0 - _i;
36✔
389
  ca[0] = s0 - (_i + bvirt) + (bvirt - t0);
86✔
390
  _j = s1 + _i;
30✔
391
  bvirt = _j - s1;
36✔
392
  _0 = s1 - (_j - bvirt) + (_i - bvirt);
80✔
393
  _i = _0 - t1;
30✔
394
  bvirt = _0 - _i;
36✔
395
  ca[1] = _0 - (_i + bvirt) + (bvirt - t1);
86✔
396
  u3 = _j + _i;
30✔
397
  bvirt = u3 - _j;
36✔
398
  ca[2] = _j - (u3 - bvirt) + (_i - bvirt);
86✔
399
  ca[3] = u3;
26✔
400
  s1 = adx * bdy;
34✔
401
  c = splitter * adx;
42✔
402
  ahi = c - (c - adx);
44✔
403
  alo = adx - ahi;
36✔
404
  c = splitter * bdy;
42✔
405
  bhi = c - (c - bdy);
44✔
406
  blo = bdy - bhi;
36✔
407
  s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
408
  t1 = bdx * ady;
34✔
409
  c = splitter * bdx;
42✔
410
  ahi = c - (c - bdx);
44✔
411
  alo = bdx - ahi;
36✔
412
  c = splitter * ady;
42✔
413
  bhi = c - (c - ady);
44✔
414
  blo = ady - bhi;
36✔
415
  t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
120✔
416
  _i = s0 - t0;
30✔
417
  bvirt = s0 - _i;
36✔
418
  ab[0] = s0 - (_i + bvirt) + (bvirt - t0);
86✔
419
  _j = s1 + _i;
30✔
420
  bvirt = _j - s1;
36✔
421
  _0 = s1 - (_j - bvirt) + (_i - bvirt);
80✔
422
  _i = _0 - t1;
30✔
423
  bvirt = _0 - _i;
36✔
424
  ab[1] = _0 - (_i + bvirt) + (bvirt - t1);
86✔
425
  u3 = _j + _i;
30✔
426
  bvirt = u3 - _j;
36✔
427
  ab[2] = _j - (u3 - bvirt) + (_i - bvirt);
86✔
428
  ab[3] = u3;
26✔
429

430
  finlen = predSum(
34✔
431
    predSum(scale(4, bc, adz, _8), _8, scale(4, ca, bdz, _8b), _8b, _16),
140✔
432
    _16,
10✔
433
    scale(4, ab, cdz, _8),
46✔
434
    _8,
8✔
435
    fin,
6✔
436
  );
8✔
437

438
  let det = estimate(finlen, fin);
68✔
439
  let errbound = o3derrboundB * permanent;
84✔
440
  if (det >= errbound || -det >= errbound) {
90✔
441
    return det;
20✔
442
  }
4✔
443

444
  bvirt = ax - adx;
38✔
445
  const adxtail = ax - (adx + bvirt) + (bvirt - dx);
104✔
446
  bvirt = bx - bdx;
38✔
447
  const bdxtail = bx - (bdx + bvirt) + (bvirt - dx);
104✔
448
  bvirt = cx - cdx;
38✔
449
  const cdxtail = cx - (cdx + bvirt) + (bvirt - dx);
104✔
450
  bvirt = ay - ady;
38✔
451
  const adytail = ay - (ady + bvirt) + (bvirt - dy);
104✔
452
  bvirt = by - bdy;
38✔
453
  const bdytail = by - (bdy + bvirt) + (bvirt - dy);
104✔
454
  bvirt = cy - cdy;
38✔
455
  const cdytail = cy - (cdy + bvirt) + (bvirt - dy);
104✔
456
  bvirt = az - adz;
38✔
457
  const adztail = az - (adz + bvirt) + (bvirt - dz);
104✔
458
  bvirt = bz - bdz;
38✔
459
  const bdztail = bz - (bdz + bvirt) + (bvirt - dz);
104✔
460
  bvirt = cz - cdz;
38✔
461
  const cdztail = cz - (cdz + bvirt) + (bvirt - dz);
104✔
462

463
  if (
8✔
464
    adxtail === 0 &&
34✔
465
    bdxtail === 0 &&
34✔
466
    cdxtail === 0 &&
34✔
467
    adytail === 0 &&
34✔
468
    bdytail === 0 &&
34✔
469
    cdytail === 0 &&
34✔
470
    adztail === 0 &&
34✔
471
    bdztail === 0 &&
34✔
472
    cdztail === 0
28✔
UNCOV
473
  ) {
×
UNCOV
474
    return det;
×
475
  }
4✔
476

477
  errbound = o3derrboundC * permanent + resulterrbound * Math.abs(det);
142✔
478
  det +=
14✔
479
    adz * (bdx * cdytail + cdy * bdxtail - (bdy * cdxtail + cdx * bdytail)) +
148✔
480
    adztail * (bdx * cdy - bdy * cdx) +
72✔
481
    bdz * (cdx * adytail + ady * cdxtail - (cdy * adxtail + adx * cdytail)) +
148✔
482
    bdztail * (cdx * ady - cdy * adx) +
72✔
483
    cdz * (adx * bdytail + bdy * adxtail - (ady * bdxtail + bdx * adytail)) +
148✔
484
    cdztail * (adx * bdy - ady * bdx);
72✔
485
  if (det >= errbound || -det >= errbound) {
90✔
486
    return det;
20✔
487
  }
4✔
488

489
  const at_len = tailinit(adxtail, adytail, bdx, bdy, cdx, cdy, at_b, at_c);
152✔
490
  const bt_len = tailinit(bdxtail, bdytail, cdx, cdy, adx, ady, bt_c, bt_a);
152✔
491
  const ct_len = tailinit(cdxtail, cdytail, adx, ady, bdx, bdy, ct_a, ct_b);
152✔
492

493
  const bctlen = predSum(bt_len, bt_c, ct_len, ct_b, bct);
116✔
494
  finlen = finadd(finlen, scale(bctlen, bct, adz, _16), _16, constants);
144✔
495

496
  const catlen = predSum(ct_len, ct_a, at_len, at_c, cat);
116✔
497
  finlen = finadd(finlen, scale(catlen, cat, bdz, _16), _16, constants);
144✔
498

499
  const abtlen = predSum(at_len, at_b, bt_len, bt_a, abt);
116✔
500
  finlen = finadd(finlen, scale(abtlen, abt, cdz, _16), _16, constants);
144✔
501

502
  if (adztail !== 0) {
46✔
503
    finlen = finadd(finlen, scale(4, bc, adztail, _12), _12, constants);
144✔
504
    finlen = finadd(finlen, scale(bctlen, bct, adztail, _16), _16, constants);
152✔
505
  }
4✔
506
  if (bdztail !== 0) {
46✔
507
    finlen = finadd(finlen, scale(4, ca, bdztail, _12), _12, constants);
144✔
508
    finlen = finadd(finlen, scale(catlen, cat, bdztail, _16), _16, constants);
152✔
509
  }
4✔
510
  if (cdztail !== 0) {
46✔
511
    finlen = finadd(finlen, scale(4, ab, cdztail, _12), _12, constants);
144✔
512
    finlen = finadd(finlen, scale(abtlen, abt, cdztail, _16), _16, constants);
152✔
513
  }
4✔
514

515
  if (adxtail !== 0) {
46✔
516
    if (bdytail !== 0) {
50✔
517
      finlen = tailadd(finlen, adxtail, bdytail, cdz, cdztail);
122✔
518
    }
8✔
519
    if (cdytail !== 0) {
50✔
520
      finlen = tailadd(finlen, -adxtail, cdytail, bdz, bdztail);
124✔
521
    }
4✔
522
  }
4✔
523
  if (bdxtail !== 0) {
46✔
524
    if (cdytail !== 0) {
50✔
525
      finlen = tailadd(finlen, bdxtail, cdytail, adz, adztail);
122✔
526
    }
8✔
527
    if (adytail !== 0) {
50✔
528
      finlen = tailadd(finlen, -bdxtail, adytail, cdz, cdztail);
124✔
529
    }
4✔
530
  }
4✔
531
  if (cdxtail !== 0) {
46✔
532
    if (adytail !== 0) {
50✔
533
      finlen = tailadd(finlen, cdxtail, adytail, bdz, bdztail);
122✔
534
    }
8✔
535
    if (bdytail !== 0) {
50✔
536
      finlen = tailadd(finlen, -cdxtail, bdytail, adz, adztail);
124✔
537
    }
4✔
538
  }
4✔
539

540
  return fin[finlen - 1];
46✔
541
}
542

543
/**
544
 * Get the orientation of a tetrahedron
545
 * @param ax - x coordinate of first point
546
 * @param ay - y coordinate of first point
547
 * @param az - z coordinate of first point
548
 * @param bx - x coordinate of second point
549
 * @param by - y coordinate of second point
550
 * @param bz - z coordinate of second point
551
 * @param cx - x coordinate of origin point to create the abc plane
552
 * @param cy - y coordinate of origin point to create the abc plane
553
 * @param cz - z coordinate of origin point to create the abc plane
554
 * @param dx - x coordinate of compare point
555
 * @param dy - y coordinate of compare point
556
 * @param dz - z coordinate of compare point
557
 * @returns - a positive value if the point-plane of a-b-c via d occur in counterclockwise order
558
 * (c lies to the left of the directed line defined by points a and b).
559
 * - Returns a negative value if they occur in clockwise order (c lies to the right of the directed line ab).
560
 * - Returns zero if they are collinear.
561
 */
562
export function orient3d(
12✔
563
  ax: number,
8✔
564
  ay: number,
8✔
565
  az: number,
8✔
566
  bx: number,
8✔
567
  by: number,
8✔
568
  bz: number,
8✔
569
  cx: number,
8✔
570
  cy: number,
8✔
571
  cz: number,
8✔
572
  dx: number,
8✔
573
  dy: number,
8✔
574
  dz: number,
8✔
575
): number {
6✔
576
  const adx = ax - dx;
44✔
577
  const bdx = bx - dx;
44✔
578
  const cdx = cx - dx;
44✔
579
  const ady = ay - dy;
44✔
580
  const bdy = by - dy;
44✔
581
  const cdy = cy - dy;
44✔
582
  const adz = az - dz;
44✔
583
  const bdz = bz - dz;
44✔
584
  const cdz = cz - dz;
44✔
585

586
  const bdxcdy = bdx * cdy;
54✔
587
  const cdxbdy = cdx * bdy;
54✔
588

589
  const cdxady = cdx * ady;
54✔
590
  const adxcdy = adx * cdy;
54✔
591

592
  const adxbdy = adx * bdy;
54✔
593
  const bdxady = bdx * ady;
54✔
594

595
  const det = adz * (bdxcdy - cdxbdy) + bdz * (cdxady - adxcdy) + cdz * (adxbdy - bdxady);
180✔
596

597
  const permanent =
38✔
598
    (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * Math.abs(adz) +
112✔
599
    (Math.abs(cdxady) + Math.abs(adxcdy)) * Math.abs(bdz) +
112✔
600
    (Math.abs(adxbdy) + Math.abs(bdxady)) * Math.abs(cdz);
110✔
601

602
  const errbound = o3derrboundA * permanent;
88✔
603
  if (det > errbound || -det > errbound) {
86✔
604
    return det;
20✔
605
  }
4✔
606

607
  return orient3dAdapt(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, permanent);
160✔
608
}
609

610
/**
611
 * Find the orientation of a point relative to a vector a-b plane relative to the origin
612
 * @param a - first point
613
 * @param b - second point
614
 * @param c - comparison point
615
 * @returns - a positive value if the point-plane of a-b via c occur in counterclockwise order
616
 * (c lies to the left of the directed line defined by points a and b).
617
 * - Returns a negative value if they occur in clockwise order (c lies to the right of the directed line ab).
618
 * - Returns zero if they are collinear.
619
 */
620
export function orient3dfastVector(a: VectorPoint, b: VectorPoint, c: VectorPoint): number {
36✔
621
  return orient3dfast(a.x, a.y, a.z ?? 0, b.x, b.y, b.z ?? 0, 0, 0, 0, c.x, c.y, c.z ?? 0);
178✔
622
}
623

624
/**
625
 * @param ax - x coordinate of first point
626
 * @param ay - y coordinate of first point
627
 * @param az - z coordinate of first point
628
 * @param bx - x coordinate of second point
629
 * @param by - y coordinate of second point
630
 * @param bz - z coordinate of second point
631
 * @param cx - x coordinate of third point
632
 * @param cy - y coordinate of third point
633
 * @param cz - z coordinate of third point
634
 * @param dx - x coordinate of compare point
635
 * @param dy - y coordinate of compare point
636
 * @param dz - z coordinate of compare point
637
 * @returns - a positive value if the point-plane of a-b-c via d occur in counterclockwise order
638
 * (c lies to the left of the directed line defined by points a and b).
639
 * - Returns a negative value if they occur in clockwise order (c lies to the right of the directed line ab).
640
 * - Returns zero if they are collinear.
641
 */
642
export function orient3dfast(
12✔
643
  ax: number,
8✔
644
  ay: number,
8✔
645
  az: number,
8✔
646
  bx: number,
8✔
647
  by: number,
8✔
648
  bz: number,
8✔
649
  cx: number,
8✔
650
  cy: number,
8✔
651
  cz: number,
8✔
652
  dx: number,
8✔
653
  dy: number,
8✔
654
  dz: number,
8✔
655
): number {
6✔
656
  const adx = ax - dx;
44✔
657
  const bdx = bx - dx;
44✔
658
  const cdx = cx - dx;
44✔
659
  const ady = ay - dy;
44✔
660
  const bdy = by - dy;
44✔
661
  const cdy = cy - dy;
44✔
662
  const adz = az - dz;
44✔
663
  const bdz = bz - dz;
44✔
664
  const cdz = cz - dz;
44✔
665

666
  return (
14✔
667
    adx * (bdy * cdz - bdz * cdy) + bdx * (cdy * adz - cdz * ady) + cdx * (ady * bdz - adz * bdy)
186✔
668
  );
669
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc