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.

orient3d.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import {epsilon, splitter, resulterrbound, estimate, vec, sum, scale} from './util.js';
  2. const o3derrboundA = (7 + 56 * epsilon) * epsilon;
  3. const o3derrboundB = (3 + 28 * epsilon) * epsilon;
  4. const o3derrboundC = (26 + 288 * epsilon) * epsilon * epsilon;
  5. const bc = vec(4);
  6. const ca = vec(4);
  7. const ab = vec(4);
  8. const at_b = vec(4);
  9. const at_c = vec(4);
  10. const bt_c = vec(4);
  11. const bt_a = vec(4);
  12. const ct_a = vec(4);
  13. const ct_b = vec(4);
  14. const bct = vec(8);
  15. const cat = vec(8);
  16. const abt = vec(8);
  17. const u = vec(4);
  18. const _8 = vec(8);
  19. const _8b = vec(8);
  20. const _16 = vec(8);
  21. const _12 = vec(12);
  22. let fin = vec(192);
  23. let fin2 = vec(192);
  24. function finadd(finlen, alen, a) {
  25. finlen = sum(finlen, fin, alen, a, fin2);
  26. const tmp = fin; fin = fin2; fin2 = tmp;
  27. return finlen;
  28. }
  29. function tailinit(xtail, ytail, ax, ay, bx, by, a, b) {
  30. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, s1, s0, t1, t0, u3, negate;
  31. if (xtail === 0) {
  32. if (ytail === 0) {
  33. a[0] = 0;
  34. b[0] = 0;
  35. return 1;
  36. } else {
  37. negate = -ytail;
  38. s1 = negate * ax;
  39. c = splitter * negate;
  40. ahi = c - (c - negate);
  41. alo = negate - ahi;
  42. c = splitter * ax;
  43. bhi = c - (c - ax);
  44. blo = ax - bhi;
  45. a[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  46. a[1] = s1;
  47. s1 = ytail * bx;
  48. c = splitter * ytail;
  49. ahi = c - (c - ytail);
  50. alo = ytail - ahi;
  51. c = splitter * bx;
  52. bhi = c - (c - bx);
  53. blo = bx - bhi;
  54. b[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  55. b[1] = s1;
  56. return 2;
  57. }
  58. } else {
  59. if (ytail === 0) {
  60. s1 = xtail * ay;
  61. c = splitter * xtail;
  62. ahi = c - (c - xtail);
  63. alo = xtail - ahi;
  64. c = splitter * ay;
  65. bhi = c - (c - ay);
  66. blo = ay - bhi;
  67. a[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  68. a[1] = s1;
  69. negate = -xtail;
  70. s1 = negate * by;
  71. c = splitter * negate;
  72. ahi = c - (c - negate);
  73. alo = negate - ahi;
  74. c = splitter * by;
  75. bhi = c - (c - by);
  76. blo = by - bhi;
  77. b[0] = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  78. b[1] = s1;
  79. return 2;
  80. } else {
  81. s1 = xtail * ay;
  82. c = splitter * xtail;
  83. ahi = c - (c - xtail);
  84. alo = xtail - ahi;
  85. c = splitter * ay;
  86. bhi = c - (c - ay);
  87. blo = ay - bhi;
  88. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  89. t1 = ytail * ax;
  90. c = splitter * ytail;
  91. ahi = c - (c - ytail);
  92. alo = ytail - ahi;
  93. c = splitter * ax;
  94. bhi = c - (c - ax);
  95. blo = ax - bhi;
  96. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  97. _i = s0 - t0;
  98. bvirt = s0 - _i;
  99. a[0] = s0 - (_i + bvirt) + (bvirt - t0);
  100. _j = s1 + _i;
  101. bvirt = _j - s1;
  102. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  103. _i = _0 - t1;
  104. bvirt = _0 - _i;
  105. a[1] = _0 - (_i + bvirt) + (bvirt - t1);
  106. u3 = _j + _i;
  107. bvirt = u3 - _j;
  108. a[2] = _j - (u3 - bvirt) + (_i - bvirt);
  109. a[3] = u3;
  110. s1 = ytail * bx;
  111. c = splitter * ytail;
  112. ahi = c - (c - ytail);
  113. alo = ytail - ahi;
  114. c = splitter * bx;
  115. bhi = c - (c - bx);
  116. blo = bx - bhi;
  117. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  118. t1 = xtail * by;
  119. c = splitter * xtail;
  120. ahi = c - (c - xtail);
  121. alo = xtail - ahi;
  122. c = splitter * by;
  123. bhi = c - (c - by);
  124. blo = by - bhi;
  125. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  126. _i = s0 - t0;
  127. bvirt = s0 - _i;
  128. b[0] = s0 - (_i + bvirt) + (bvirt - t0);
  129. _j = s1 + _i;
  130. bvirt = _j - s1;
  131. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  132. _i = _0 - t1;
  133. bvirt = _0 - _i;
  134. b[1] = _0 - (_i + bvirt) + (bvirt - t1);
  135. u3 = _j + _i;
  136. bvirt = u3 - _j;
  137. b[2] = _j - (u3 - bvirt) + (_i - bvirt);
  138. b[3] = u3;
  139. return 4;
  140. }
  141. }
  142. }
  143. function tailadd(finlen, a, b, k, z) {
  144. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, s1, s0, u3;
  145. s1 = a * b;
  146. c = splitter * a;
  147. ahi = c - (c - a);
  148. alo = a - ahi;
  149. c = splitter * b;
  150. bhi = c - (c - b);
  151. blo = b - bhi;
  152. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  153. c = splitter * k;
  154. bhi = c - (c - k);
  155. blo = k - bhi;
  156. _i = s0 * k;
  157. c = splitter * s0;
  158. ahi = c - (c - s0);
  159. alo = s0 - ahi;
  160. u[0] = alo * blo - (_i - ahi * bhi - alo * bhi - ahi * blo);
  161. _j = s1 * k;
  162. c = splitter * s1;
  163. ahi = c - (c - s1);
  164. alo = s1 - ahi;
  165. _0 = alo * blo - (_j - ahi * bhi - alo * bhi - ahi * blo);
  166. _k = _i + _0;
  167. bvirt = _k - _i;
  168. u[1] = _i - (_k - bvirt) + (_0 - bvirt);
  169. u3 = _j + _k;
  170. u[2] = _k - (u3 - _j);
  171. u[3] = u3;
  172. finlen = finadd(finlen, 4, u);
  173. if (z !== 0) {
  174. c = splitter * z;
  175. bhi = c - (c - z);
  176. blo = z - bhi;
  177. _i = s0 * z;
  178. c = splitter * s0;
  179. ahi = c - (c - s0);
  180. alo = s0 - ahi;
  181. u[0] = alo * blo - (_i - ahi * bhi - alo * bhi - ahi * blo);
  182. _j = s1 * z;
  183. c = splitter * s1;
  184. ahi = c - (c - s1);
  185. alo = s1 - ahi;
  186. _0 = alo * blo - (_j - ahi * bhi - alo * bhi - ahi * blo);
  187. _k = _i + _0;
  188. bvirt = _k - _i;
  189. u[1] = _i - (_k - bvirt) + (_0 - bvirt);
  190. u3 = _j + _k;
  191. u[2] = _k - (u3 - _j);
  192. u[3] = u3;
  193. finlen = finadd(finlen, 4, u);
  194. }
  195. return finlen;
  196. }
  197. function orient3dadapt(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, permanent) {
  198. let finlen;
  199. let adxtail, bdxtail, cdxtail;
  200. let adytail, bdytail, cdytail;
  201. let adztail, bdztail, cdztail;
  202. let bvirt, c, ahi, alo, bhi, blo, _i, _j, _k, _0, s1, s0, t1, t0, u3;
  203. const adx = ax - dx;
  204. const bdx = bx - dx;
  205. const cdx = cx - dx;
  206. const ady = ay - dy;
  207. const bdy = by - dy;
  208. const cdy = cy - dy;
  209. const adz = az - dz;
  210. const bdz = bz - dz;
  211. const cdz = cz - dz;
  212. s1 = bdx * cdy;
  213. c = splitter * bdx;
  214. ahi = c - (c - bdx);
  215. alo = bdx - ahi;
  216. c = splitter * cdy;
  217. bhi = c - (c - cdy);
  218. blo = cdy - bhi;
  219. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  220. t1 = cdx * bdy;
  221. c = splitter * cdx;
  222. ahi = c - (c - cdx);
  223. alo = cdx - ahi;
  224. c = splitter * bdy;
  225. bhi = c - (c - bdy);
  226. blo = bdy - bhi;
  227. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  228. _i = s0 - t0;
  229. bvirt = s0 - _i;
  230. bc[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. bc[1] = _0 - (_i + bvirt) + (bvirt - t1);
  237. u3 = _j + _i;
  238. bvirt = u3 - _j;
  239. bc[2] = _j - (u3 - bvirt) + (_i - bvirt);
  240. bc[3] = u3;
  241. s1 = cdx * ady;
  242. c = splitter * cdx;
  243. ahi = c - (c - cdx);
  244. alo = cdx - ahi;
  245. c = splitter * ady;
  246. bhi = c - (c - ady);
  247. blo = ady - bhi;
  248. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  249. t1 = adx * cdy;
  250. c = splitter * adx;
  251. ahi = c - (c - adx);
  252. alo = adx - ahi;
  253. c = splitter * cdy;
  254. bhi = c - (c - cdy);
  255. blo = cdy - bhi;
  256. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  257. _i = s0 - t0;
  258. bvirt = s0 - _i;
  259. ca[0] = s0 - (_i + bvirt) + (bvirt - t0);
  260. _j = s1 + _i;
  261. bvirt = _j - s1;
  262. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  263. _i = _0 - t1;
  264. bvirt = _0 - _i;
  265. ca[1] = _0 - (_i + bvirt) + (bvirt - t1);
  266. u3 = _j + _i;
  267. bvirt = u3 - _j;
  268. ca[2] = _j - (u3 - bvirt) + (_i - bvirt);
  269. ca[3] = u3;
  270. s1 = adx * bdy;
  271. c = splitter * adx;
  272. ahi = c - (c - adx);
  273. alo = adx - ahi;
  274. c = splitter * bdy;
  275. bhi = c - (c - bdy);
  276. blo = bdy - bhi;
  277. s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
  278. t1 = bdx * ady;
  279. c = splitter * bdx;
  280. ahi = c - (c - bdx);
  281. alo = bdx - ahi;
  282. c = splitter * ady;
  283. bhi = c - (c - ady);
  284. blo = ady - bhi;
  285. t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
  286. _i = s0 - t0;
  287. bvirt = s0 - _i;
  288. ab[0] = s0 - (_i + bvirt) + (bvirt - t0);
  289. _j = s1 + _i;
  290. bvirt = _j - s1;
  291. _0 = s1 - (_j - bvirt) + (_i - bvirt);
  292. _i = _0 - t1;
  293. bvirt = _0 - _i;
  294. ab[1] = _0 - (_i + bvirt) + (bvirt - t1);
  295. u3 = _j + _i;
  296. bvirt = u3 - _j;
  297. ab[2] = _j - (u3 - bvirt) + (_i - bvirt);
  298. ab[3] = u3;
  299. finlen = sum(
  300. sum(
  301. scale(4, bc, adz, _8), _8,
  302. scale(4, ca, bdz, _8b), _8b, _16), _16,
  303. scale(4, ab, cdz, _8), _8, fin);
  304. let det = estimate(finlen, fin);
  305. let errbound = o3derrboundB * permanent;
  306. if (det >= errbound || -det >= errbound) {
  307. return det;
  308. }
  309. bvirt = ax - adx;
  310. adxtail = ax - (adx + bvirt) + (bvirt - dx);
  311. bvirt = bx - bdx;
  312. bdxtail = bx - (bdx + bvirt) + (bvirt - dx);
  313. bvirt = cx - cdx;
  314. cdxtail = cx - (cdx + bvirt) + (bvirt - dx);
  315. bvirt = ay - ady;
  316. adytail = ay - (ady + bvirt) + (bvirt - dy);
  317. bvirt = by - bdy;
  318. bdytail = by - (bdy + bvirt) + (bvirt - dy);
  319. bvirt = cy - cdy;
  320. cdytail = cy - (cdy + bvirt) + (bvirt - dy);
  321. bvirt = az - adz;
  322. adztail = az - (adz + bvirt) + (bvirt - dz);
  323. bvirt = bz - bdz;
  324. bdztail = bz - (bdz + bvirt) + (bvirt - dz);
  325. bvirt = cz - cdz;
  326. cdztail = cz - (cdz + bvirt) + (bvirt - dz);
  327. if (adxtail === 0 && bdxtail === 0 && cdxtail === 0 &&
  328. adytail === 0 && bdytail === 0 && cdytail === 0 &&
  329. adztail === 0 && bdztail === 0 && cdztail === 0) {
  330. return det;
  331. }
  332. errbound = o3derrboundC * permanent + resulterrbound * Math.abs(det);
  333. det +=
  334. adz * (bdx * cdytail + cdy * bdxtail - (bdy * cdxtail + cdx * bdytail)) + adztail * (bdx * cdy - bdy * cdx) +
  335. bdz * (cdx * adytail + ady * cdxtail - (cdy * adxtail + adx * cdytail)) + bdztail * (cdx * ady - cdy * adx) +
  336. cdz * (adx * bdytail + bdy * adxtail - (ady * bdxtail + bdx * adytail)) + cdztail * (adx * bdy - ady * bdx);
  337. if (det >= errbound || -det >= errbound) {
  338. return det;
  339. }
  340. const at_len = tailinit(adxtail, adytail, bdx, bdy, cdx, cdy, at_b, at_c);
  341. const bt_len = tailinit(bdxtail, bdytail, cdx, cdy, adx, ady, bt_c, bt_a);
  342. const ct_len = tailinit(cdxtail, cdytail, adx, ady, bdx, bdy, ct_a, ct_b);
  343. const bctlen = sum(bt_len, bt_c, ct_len, ct_b, bct);
  344. finlen = finadd(finlen, scale(bctlen, bct, adz, _16), _16);
  345. const catlen = sum(ct_len, ct_a, at_len, at_c, cat);
  346. finlen = finadd(finlen, scale(catlen, cat, bdz, _16), _16);
  347. const abtlen = sum(at_len, at_b, bt_len, bt_a, abt);
  348. finlen = finadd(finlen, scale(abtlen, abt, cdz, _16), _16);
  349. if (adztail !== 0) {
  350. finlen = finadd(finlen, scale(4, bc, adztail, _12), _12);
  351. finlen = finadd(finlen, scale(bctlen, bct, adztail, _16), _16);
  352. }
  353. if (bdztail !== 0) {
  354. finlen = finadd(finlen, scale(4, ca, bdztail, _12), _12);
  355. finlen = finadd(finlen, scale(catlen, cat, bdztail, _16), _16);
  356. }
  357. if (cdztail !== 0) {
  358. finlen = finadd(finlen, scale(4, ab, cdztail, _12), _12);
  359. finlen = finadd(finlen, scale(abtlen, abt, cdztail, _16), _16);
  360. }
  361. if (adxtail !== 0) {
  362. if (bdytail !== 0) {
  363. finlen = tailadd(finlen, adxtail, bdytail, cdz, cdztail);
  364. }
  365. if (cdytail !== 0) {
  366. finlen = tailadd(finlen, -adxtail, cdytail, bdz, bdztail);
  367. }
  368. }
  369. if (bdxtail !== 0) {
  370. if (cdytail !== 0) {
  371. finlen = tailadd(finlen, bdxtail, cdytail, adz, adztail);
  372. }
  373. if (adytail !== 0) {
  374. finlen = tailadd(finlen, -bdxtail, adytail, cdz, cdztail);
  375. }
  376. }
  377. if (cdxtail !== 0) {
  378. if (adytail !== 0) {
  379. finlen = tailadd(finlen, cdxtail, adytail, bdz, bdztail);
  380. }
  381. if (bdytail !== 0) {
  382. finlen = tailadd(finlen, -cdxtail, bdytail, adz, adztail);
  383. }
  384. }
  385. return fin[finlen - 1];
  386. }
  387. export function orient3d(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz) {
  388. const adx = ax - dx;
  389. const bdx = bx - dx;
  390. const cdx = cx - dx;
  391. const ady = ay - dy;
  392. const bdy = by - dy;
  393. const cdy = cy - dy;
  394. const adz = az - dz;
  395. const bdz = bz - dz;
  396. const cdz = cz - dz;
  397. const bdxcdy = bdx * cdy;
  398. const cdxbdy = cdx * bdy;
  399. const cdxady = cdx * ady;
  400. const adxcdy = adx * cdy;
  401. const adxbdy = adx * bdy;
  402. const bdxady = bdx * ady;
  403. const det =
  404. adz * (bdxcdy - cdxbdy) +
  405. bdz * (cdxady - adxcdy) +
  406. cdz * (adxbdy - bdxady);
  407. const permanent =
  408. (Math.abs(bdxcdy) + Math.abs(cdxbdy)) * Math.abs(adz) +
  409. (Math.abs(cdxady) + Math.abs(adxcdy)) * Math.abs(bdz) +
  410. (Math.abs(adxbdy) + Math.abs(bdxady)) * Math.abs(cdz);
  411. const errbound = o3derrboundA * permanent;
  412. if (det > errbound || -det > errbound) {
  413. return det;
  414. }
  415. return orient3dadapt(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz, permanent);
  416. }
  417. export function orient3dfast(ax, ay, az, bx, by, bz, cx, cy, cz, dx, dy, dz) {
  418. const adx = ax - dx;
  419. const bdx = bx - dx;
  420. const cdx = cx - dx;
  421. const ady = ay - dy;
  422. const bdy = by - dy;
  423. const cdy = cy - dy;
  424. const adz = az - dz;
  425. const bdz = bz - dz;
  426. const cdz = cz - dz;
  427. return adx * (bdy * cdz - bdz * cdy) +
  428. bdx * (cdy * adz - cdz * ady) +
  429. cdx * (ady * bdz - adz * bdy);
  430. }