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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {epsilon, splitter, resulterrbound, estimate, vec, sum} from './util.js';
  2. const ccwerrboundA = (3 + 16 * epsilon) * epsilon;
  3. const ccwerrboundB = (2 + 12 * epsilon) * epsilon;
  4. const ccwerrboundC = (9 + 64 * epsilon) * epsilon * epsilon;
  5. const B = vec(4);
  6. const C1 = vec(8);
  7. const C2 = vec(12);
  8. const D = vec(16);
  9. const u = vec(4);
  10. function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
  11. let acxtail, acytail, bcxtail, bcytail;
  12. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
  13. const acx = ax - cx;
  14. const bcx = bx - cx;
  15. const acy = ay - cy;
  16. const bcy = by - cy;
  17. s1 = acx * bcy;
  18. c = splitter * acx;
  19. ahi = c - (c - acx);
  20. alo = acx - ahi;
  21. c = splitter * bcy;
  22. bhi = c - (c - bcy);
  23. blo = bcy - bhi;
  24. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  25. t1 = acy * bcx;
  26. c = splitter * acy;
  27. ahi = c - (c - acy);
  28. alo = acy - ahi;
  29. c = splitter * bcx;
  30. bhi = c - (c - bcx);
  31. blo = bcx - bhi;
  32. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  33. _i = s0 - t0;
  34. bvirt = s0 - _i;
  35. B[0] = s0 - (_i + bvirt) + (bvirt - t0);
  36. _j = s1 + _i;
  37. bvirt = _j - s1;
  38. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  39. _i = _0 - t1;
  40. bvirt = _0 - _i;
  41. B[1] = _0 - (_i + bvirt) + (bvirt - t1);
  42. u3 = _j + _i;
  43. bvirt = u3 - _j;
  44. B[2] = _j - (u3 - bvirt) + (_i - bvirt);
  45. B[3] = u3;
  46. let det = estimate(4, B);
  47. let errbound = ccwerrboundB * detsum;
  48. if (det >= errbound || -det >= errbound) {
  49. return det;
  50. }
  51. bvirt = ax - acx;
  52. acxtail = ax - (acx + bvirt) + (bvirt - cx);
  53. bvirt = bx - bcx;
  54. bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
  55. bvirt = ay - acy;
  56. acytail = ay - (acy + bvirt) + (bvirt - cy);
  57. bvirt = by - bcy;
  58. bcytail = by - (bcy + bvirt) + (bvirt - cy);
  59. if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
  60. return det;
  61. }
  62. errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
  63. det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
  64. if (det >= errbound || -det >= errbound) return det;
  65. s1 = acxtail * bcy;
  66. c = splitter * acxtail;
  67. ahi = c - (c - acxtail);
  68. alo = acxtail - ahi;
  69. c = splitter * bcy;
  70. bhi = c - (c - bcy);
  71. blo = bcy - bhi;
  72. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  73. t1 = acytail * bcx;
  74. c = splitter * acytail;
  75. ahi = c - (c - acytail);
  76. alo = acytail - ahi;
  77. c = splitter * bcx;
  78. bhi = c - (c - bcx);
  79. blo = bcx - bhi;
  80. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  81. _i = s0 - t0;
  82. bvirt = s0 - _i;
  83. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  84. _j = s1 + _i;
  85. bvirt = _j - s1;
  86. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  87. _i = _0 - t1;
  88. bvirt = _0 - _i;
  89. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  90. u3 = _j + _i;
  91. bvirt = u3 - _j;
  92. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  93. u[3] = u3;
  94. const C1len = sum(4, B, 4, u, C1);
  95. s1 = acx * bcytail;
  96. c = splitter * acx;
  97. ahi = c - (c - acx);
  98. alo = acx - ahi;
  99. c = splitter * bcytail;
  100. bhi = c - (c - bcytail);
  101. blo = bcytail - bhi;
  102. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  103. t1 = acy * bcxtail;
  104. c = splitter * acy;
  105. ahi = c - (c - acy);
  106. alo = acy - ahi;
  107. c = splitter * bcxtail;
  108. bhi = c - (c - bcxtail);
  109. blo = bcxtail - bhi;
  110. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  111. _i = s0 - t0;
  112. bvirt = s0 - _i;
  113. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  114. _j = s1 + _i;
  115. bvirt = _j - s1;
  116. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  117. _i = _0 - t1;
  118. bvirt = _0 - _i;
  119. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  120. u3 = _j + _i;
  121. bvirt = u3 - _j;
  122. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  123. u[3] = u3;
  124. const C2len = sum(C1len, C1, 4, u, C2);
  125. s1 = acxtail * bcytail;
  126. c = splitter * acxtail;
  127. ahi = c - (c - acxtail);
  128. alo = acxtail - ahi;
  129. c = splitter * bcytail;
  130. bhi = c - (c - bcytail);
  131. blo = bcytail - bhi;
  132. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  133. t1 = acytail * bcxtail;
  134. c = splitter * acytail;
  135. ahi = c - (c - acytail);
  136. alo = acytail - ahi;
  137. c = splitter * bcxtail;
  138. bhi = c - (c - bcxtail);
  139. blo = bcxtail - bhi;
  140. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  141. _i = s0 - t0;
  142. bvirt = s0 - _i;
  143. u[0] = s0 - (_i + bvirt) + (bvirt - t0);
  144. _j = s1 + _i;
  145. bvirt = _j - s1;
  146. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  147. _i = _0 - t1;
  148. bvirt = _0 - _i;
  149. u[1] = _0 - (_i + bvirt) + (bvirt - t1);
  150. u3 = _j + _i;
  151. bvirt = u3 - _j;
  152. u[2] = _j - (u3 - bvirt) + (_i - bvirt);
  153. u[3] = u3;
  154. const Dlen = sum(C2len, C2, 4, u, D);
  155. return D[Dlen - 1];
  156. }
  157. export function orient2d(ax, ay, bx, by, cx, cy) {
  158. const detleft = (ay - cy) * (bx - cx);
  159. const detright = (ax - cx) * (by - cy);
  160. const det = detleft - detright;
  161. const detsum = Math.abs(detleft + detright);
  162. if (Math.abs(det) >= ccwerrboundA * detsum) return det;
  163. return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
  164. }
  165. export function orient2dfast(ax, ay, bx, by, cx, cy) {
  166. return (ay - cy) * (bx - cx) - (ax - cx) * (by - cy);
  167. }