source: node_modules/robust-predicates/umd/orient2d.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 7.6 KB
Line 
1(function (global, factory) {
2typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3typeof define === 'function' && define.amd ? define(['exports'], factory) :
4(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.predicates = {}));
5})(this, (function (exports) { 'use strict';
6
7const epsilon = 1.1102230246251565e-16;
8const splitter = 134217729;
9const resulterrbound = (3 + 8 * epsilon) * epsilon;
10
11// fast_expansion_sum_zeroelim routine from oritinal code
12function sum(elen, e, flen, f, h) {
13 let Q, Qnew, hh, bvirt;
14 let enow = e[0];
15 let fnow = f[0];
16 let eindex = 0;
17 let findex = 0;
18 if ((fnow > enow) === (fnow > -enow)) {
19 Q = enow;
20 enow = e[++eindex];
21 } else {
22 Q = fnow;
23 fnow = f[++findex];
24 }
25 let hindex = 0;
26 if (eindex < elen && findex < flen) {
27 if ((fnow > enow) === (fnow > -enow)) {
28 Qnew = enow + Q;
29 hh = Q - (Qnew - enow);
30 enow = e[++eindex];
31 } else {
32 Qnew = fnow + Q;
33 hh = Q - (Qnew - fnow);
34 fnow = f[++findex];
35 }
36 Q = Qnew;
37 if (hh !== 0) {
38 h[hindex++] = hh;
39 }
40 while (eindex < elen && findex < flen) {
41 if ((fnow > enow) === (fnow > -enow)) {
42 Qnew = Q + enow;
43 bvirt = Qnew - Q;
44 hh = Q - (Qnew - bvirt) + (enow - bvirt);
45 enow = e[++eindex];
46 } else {
47 Qnew = Q + fnow;
48 bvirt = Qnew - Q;
49 hh = Q - (Qnew - bvirt) + (fnow - bvirt);
50 fnow = f[++findex];
51 }
52 Q = Qnew;
53 if (hh !== 0) {
54 h[hindex++] = hh;
55 }
56 }
57 }
58 while (eindex < elen) {
59 Qnew = Q + enow;
60 bvirt = Qnew - Q;
61 hh = Q - (Qnew - bvirt) + (enow - bvirt);
62 enow = e[++eindex];
63 Q = Qnew;
64 if (hh !== 0) {
65 h[hindex++] = hh;
66 }
67 }
68 while (findex < flen) {
69 Qnew = Q + fnow;
70 bvirt = Qnew - Q;
71 hh = Q - (Qnew - bvirt) + (fnow - bvirt);
72 fnow = f[++findex];
73 Q = Qnew;
74 if (hh !== 0) {
75 h[hindex++] = hh;
76 }
77 }
78 if (Q !== 0 || hindex === 0) {
79 h[hindex++] = Q;
80 }
81 return hindex;
82}
83
84function estimate(elen, e) {
85 let Q = e[0];
86 for (let i = 1; i < elen; i++) Q += e[i];
87 return Q;
88}
89
90function vec(n) {
91 return new Float64Array(n);
92}
93
94const ccwerrboundA = (3 + 16 * epsilon) * epsilon;
95const ccwerrboundB = (2 + 12 * epsilon) * epsilon;
96const ccwerrboundC = (9 + 64 * epsilon) * epsilon * epsilon;
97
98const B = vec(4);
99const C1 = vec(8);
100const C2 = vec(12);
101const D = vec(16);
102const u = vec(4);
103
104function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
105 let acxtail, acytail, bcxtail, bcytail;
106 let bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
107
108 const acx = ax - cx;
109 const bcx = bx - cx;
110 const acy = ay - cy;
111 const bcy = by - cy;
112
113 s1 = acx * bcy;
114 c = splitter * acx;
115 ahi = c - (c - acx);
116 alo = acx - ahi;
117 c = splitter * bcy;
118 bhi = c - (c - bcy);
119 blo = bcy - bhi;
120 s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
121 t1 = acy * bcx;
122 c = splitter * acy;
123 ahi = c - (c - acy);
124 alo = acy - ahi;
125 c = splitter * bcx;
126 bhi = c - (c - bcx);
127 blo = bcx - bhi;
128 t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
129 _i = s0 - t0;
130 bvirt = s0 - _i;
131 B[0] = s0 - (_i + bvirt) + (bvirt - t0);
132 _j = s1 + _i;
133 bvirt = _j - s1;
134 _0 = s1 - (_j - bvirt) + (_i - bvirt);
135 _i = _0 - t1;
136 bvirt = _0 - _i;
137 B[1] = _0 - (_i + bvirt) + (bvirt - t1);
138 u3 = _j + _i;
139 bvirt = u3 - _j;
140 B[2] = _j - (u3 - bvirt) + (_i - bvirt);
141 B[3] = u3;
142
143 let det = estimate(4, B);
144 let errbound = ccwerrboundB * detsum;
145 if (det >= errbound || -det >= errbound) {
146 return det;
147 }
148
149 bvirt = ax - acx;
150 acxtail = ax - (acx + bvirt) + (bvirt - cx);
151 bvirt = bx - bcx;
152 bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
153 bvirt = ay - acy;
154 acytail = ay - (acy + bvirt) + (bvirt - cy);
155 bvirt = by - bcy;
156 bcytail = by - (bcy + bvirt) + (bvirt - cy);
157
158 if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
159 return det;
160 }
161
162 errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
163 det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
164 if (det >= errbound || -det >= errbound) return det;
165
166 s1 = acxtail * bcy;
167 c = splitter * acxtail;
168 ahi = c - (c - acxtail);
169 alo = acxtail - ahi;
170 c = splitter * bcy;
171 bhi = c - (c - bcy);
172 blo = bcy - bhi;
173 s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
174 t1 = acytail * bcx;
175 c = splitter * acytail;
176 ahi = c - (c - acytail);
177 alo = acytail - ahi;
178 c = splitter * bcx;
179 bhi = c - (c - bcx);
180 blo = bcx - bhi;
181 t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
182 _i = s0 - t0;
183 bvirt = s0 - _i;
184 u[0] = s0 - (_i + bvirt) + (bvirt - t0);
185 _j = s1 + _i;
186 bvirt = _j - s1;
187 _0 = s1 - (_j - bvirt) + (_i - bvirt);
188 _i = _0 - t1;
189 bvirt = _0 - _i;
190 u[1] = _0 - (_i + bvirt) + (bvirt - t1);
191 u3 = _j + _i;
192 bvirt = u3 - _j;
193 u[2] = _j - (u3 - bvirt) + (_i - bvirt);
194 u[3] = u3;
195 const C1len = sum(4, B, 4, u, C1);
196
197 s1 = acx * bcytail;
198 c = splitter * acx;
199 ahi = c - (c - acx);
200 alo = acx - ahi;
201 c = splitter * bcytail;
202 bhi = c - (c - bcytail);
203 blo = bcytail - bhi;
204 s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
205 t1 = acy * bcxtail;
206 c = splitter * acy;
207 ahi = c - (c - acy);
208 alo = acy - ahi;
209 c = splitter * bcxtail;
210 bhi = c - (c - bcxtail);
211 blo = bcxtail - bhi;
212 t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
213 _i = s0 - t0;
214 bvirt = s0 - _i;
215 u[0] = s0 - (_i + bvirt) + (bvirt - t0);
216 _j = s1 + _i;
217 bvirt = _j - s1;
218 _0 = s1 - (_j - bvirt) + (_i - bvirt);
219 _i = _0 - t1;
220 bvirt = _0 - _i;
221 u[1] = _0 - (_i + bvirt) + (bvirt - t1);
222 u3 = _j + _i;
223 bvirt = u3 - _j;
224 u[2] = _j - (u3 - bvirt) + (_i - bvirt);
225 u[3] = u3;
226 const C2len = sum(C1len, C1, 4, u, C2);
227
228 s1 = acxtail * bcytail;
229 c = splitter * acxtail;
230 ahi = c - (c - acxtail);
231 alo = acxtail - ahi;
232 c = splitter * bcytail;
233 bhi = c - (c - bcytail);
234 blo = bcytail - bhi;
235 s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
236 t1 = acytail * bcxtail;
237 c = splitter * acytail;
238 ahi = c - (c - acytail);
239 alo = acytail - ahi;
240 c = splitter * bcxtail;
241 bhi = c - (c - bcxtail);
242 blo = bcxtail - bhi;
243 t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
244 _i = s0 - t0;
245 bvirt = s0 - _i;
246 u[0] = s0 - (_i + bvirt) + (bvirt - t0);
247 _j = s1 + _i;
248 bvirt = _j - s1;
249 _0 = s1 - (_j - bvirt) + (_i - bvirt);
250 _i = _0 - t1;
251 bvirt = _0 - _i;
252 u[1] = _0 - (_i + bvirt) + (bvirt - t1);
253 u3 = _j + _i;
254 bvirt = u3 - _j;
255 u[2] = _j - (u3 - bvirt) + (_i - bvirt);
256 u[3] = u3;
257 const Dlen = sum(C2len, C2, 4, u, D);
258
259 return D[Dlen - 1];
260}
261
262function orient2d(ax, ay, bx, by, cx, cy) {
263 const detleft = (ay - cy) * (bx - cx);
264 const detright = (ax - cx) * (by - cy);
265 const det = detleft - detright;
266
267 const detsum = Math.abs(detleft + detright);
268 if (Math.abs(det) >= ccwerrboundA * detsum) return det;
269
270 return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
271}
272
273function orient2dfast(ax, ay, bx, by, cx, cy) {
274 return (ay - cy) * (bx - cx) - (ax - cx) * (by - cy);
275}
276
277exports.orient2d = orient2d;
278exports.orient2dfast = orient2dfast;
279
280}));
Note: See TracBrowser for help on using the repository browser.