index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // index.ts
  2. import { featureCollection, polygon } from "@turf/helpers";
  3. function tin(points, z) {
  4. let isPointZ = false;
  5. return featureCollection(
  6. triangulate(
  7. points.features.map((p) => {
  8. const point = {
  9. x: p.geometry.coordinates[0],
  10. y: p.geometry.coordinates[1]
  11. };
  12. if (z) {
  13. point.z = p.properties[z];
  14. } else if (p.geometry.coordinates.length === 3) {
  15. isPointZ = true;
  16. point.z = p.geometry.coordinates[2];
  17. }
  18. return point;
  19. })
  20. ).map((triangle) => {
  21. const a = [triangle.a.x, triangle.a.y];
  22. const b = [triangle.b.x, triangle.b.y];
  23. const c = [triangle.c.x, triangle.c.y];
  24. let properties = {};
  25. if (isPointZ) {
  26. a.push(triangle.a.z);
  27. b.push(triangle.b.z);
  28. c.push(triangle.c.z);
  29. } else {
  30. properties = {
  31. a: triangle.a.z,
  32. b: triangle.b.z,
  33. c: triangle.c.z
  34. };
  35. }
  36. return polygon([[a, b, c, a]], properties);
  37. })
  38. );
  39. }
  40. var Triangle = class {
  41. constructor(a, b, c) {
  42. this.a = a;
  43. this.b = b;
  44. this.c = c;
  45. const A = b.x - a.x;
  46. const B = b.y - a.y;
  47. const C = c.x - a.x;
  48. const D = c.y - a.y;
  49. const E = A * (a.x + b.x) + B * (a.y + b.y);
  50. const F = C * (a.x + c.x) + D * (a.y + c.y);
  51. const G = 2 * (A * (c.y - b.y) - B * (c.x - b.x));
  52. let dx;
  53. let dy;
  54. this.x = (D * E - B * F) / G;
  55. this.y = (A * F - C * E) / G;
  56. dx = this.x - a.x;
  57. dy = this.y - a.y;
  58. this.r = dx * dx + dy * dy;
  59. }
  60. };
  61. function byX(a, b) {
  62. return b.x - a.x;
  63. }
  64. function dedup(edges) {
  65. let j = edges.length;
  66. let a;
  67. let b;
  68. let i;
  69. let m;
  70. let n;
  71. outer: while (j) {
  72. b = edges[--j];
  73. a = edges[--j];
  74. i = j;
  75. while (i) {
  76. n = edges[--i];
  77. m = edges[--i];
  78. if (a === m && b === n || a === n && b === m) {
  79. edges.splice(j, 2);
  80. edges.splice(i, 2);
  81. j -= 2;
  82. continue outer;
  83. }
  84. }
  85. }
  86. }
  87. function triangulate(vertices) {
  88. if (vertices.length < 3) {
  89. return [];
  90. }
  91. vertices.sort(byX);
  92. let i = vertices.length - 1;
  93. const xmin = vertices[i].x;
  94. const xmax = vertices[0].x;
  95. let ymin = vertices[i].y;
  96. let ymax = ymin;
  97. const epsilon = 1e-12;
  98. let a;
  99. let b;
  100. let c;
  101. let A;
  102. let B;
  103. let G;
  104. while (i--) {
  105. if (vertices[i].y < ymin) {
  106. ymin = vertices[i].y;
  107. }
  108. if (vertices[i].y > ymax) {
  109. ymax = vertices[i].y;
  110. }
  111. }
  112. let dx = xmax - xmin;
  113. let dy = ymax - ymin;
  114. const dmax = dx > dy ? dx : dy;
  115. const xmid = (xmax + xmin) * 0.5;
  116. const ymid = (ymax + ymin) * 0.5;
  117. const open = [
  118. new Triangle(
  119. {
  120. __sentinel: true,
  121. x: xmid - 20 * dmax,
  122. y: ymid - dmax
  123. },
  124. {
  125. __sentinel: true,
  126. x: xmid,
  127. y: ymid + 20 * dmax
  128. },
  129. {
  130. __sentinel: true,
  131. x: xmid + 20 * dmax,
  132. y: ymid - dmax
  133. }
  134. )
  135. ];
  136. const closed = [];
  137. const edges = [];
  138. let j;
  139. i = vertices.length;
  140. while (i--) {
  141. edges.length = 0;
  142. j = open.length;
  143. while (j--) {
  144. dx = vertices[i].x - open[j].x;
  145. if (dx > 0 && dx * dx > open[j].r) {
  146. closed.push(open[j]);
  147. open.splice(j, 1);
  148. continue;
  149. }
  150. dy = vertices[i].y - open[j].y;
  151. if (dx * dx + dy * dy > open[j].r) {
  152. continue;
  153. }
  154. edges.push(
  155. open[j].a,
  156. open[j].b,
  157. open[j].b,
  158. open[j].c,
  159. open[j].c,
  160. open[j].a
  161. );
  162. open.splice(j, 1);
  163. }
  164. dedup(edges);
  165. j = edges.length;
  166. while (j) {
  167. b = edges[--j];
  168. a = edges[--j];
  169. c = vertices[i];
  170. A = b.x - a.x;
  171. B = b.y - a.y;
  172. G = 2 * (A * (c.y - b.y) - B * (c.x - b.x));
  173. if (Math.abs(G) > epsilon) {
  174. open.push(new Triangle(a, b, c));
  175. }
  176. }
  177. }
  178. Array.prototype.push.apply(closed, open);
  179. i = closed.length;
  180. while (i--) {
  181. if (closed[i].a.__sentinel || closed[i].b.__sentinel || closed[i].c.__sentinel) {
  182. closed.splice(i, 1);
  183. }
  184. }
  185. return closed;
  186. }
  187. var turf_tin_default = tin;
  188. export {
  189. turf_tin_default as default,
  190. tin
  191. };
  192. //# sourceMappingURL=index.js.map