Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

orient2d.js 7.6KB

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