@webviz/subsurface-viewer 0.0.2-alpha.5 → 0.0.2-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,592 @@
1
1
  export function makeFullMesh(e) {
2
+ class Node {
3
+ constructor(i, x, y) {
4
+ // vertex index in coordinates array
5
+ this.i = i;
6
+ // vertex coordinates
7
+ this.x = x;
8
+ this.y = y;
9
+ // previous and next vertex nodes in a polygon ring
10
+ this.prev = this;
11
+ this.next = this;
12
+ // z-order curve value
13
+ this.z = 0;
14
+ // previous and next nodes in z-order
15
+ this.prevZ = this;
16
+ this.nextZ = this;
17
+ // indicates whether this is a steiner point
18
+ this.steiner = false;
19
+ }
20
+ }
21
+ /**
22
+ * The fastest and smallest JavaScript polygon triangulation library for your WebGL apps.
23
+ * https://github.com/mapbox/earcut
24
+ * The library is used as source code because of issues with imports in webworkers.
25
+ */
26
+ function earcut(data, dim) {
27
+ dim = dim || 2;
28
+ const outerLen = data.length;
29
+ const outerNode = linkedList(data, 0, outerLen, dim, true);
30
+ const triangles = [];
31
+ if (!outerNode || outerNode.next === outerNode.prev)
32
+ return triangles;
33
+ let minX = Number.POSITIVE_INFINITY, minY = Number.POSITIVE_INFINITY, maxX = Number.NEGATIVE_INFINITY, maxY = Number.NEGATIVE_INFINITY, x, y, invSize = 0;
34
+ // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
35
+ if (data.length > 80 * dim) {
36
+ minX = maxX = data[0];
37
+ minY = maxY = data[1];
38
+ for (let i = dim; i < outerLen; i += dim) {
39
+ x = data[i];
40
+ y = data[i + 1];
41
+ if (x < minX)
42
+ minX = x;
43
+ if (y < minY)
44
+ minY = y;
45
+ if (x > maxX)
46
+ maxX = x;
47
+ if (y > maxY)
48
+ maxY = y;
49
+ }
50
+ // minX, minY and invSize are later used to transform coords into integers for z-order calculation
51
+ invSize = Math.max(maxX - minX, maxY - minY);
52
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
53
+ }
54
+ earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
55
+ return triangles;
56
+ // create a circular doubly linked list from polygon points in the specified winding order
57
+ function linkedList(data, start, end, dim, clockwise) {
58
+ let i;
59
+ let last = null;
60
+ if (clockwise === signedArea(data, start, end, dim) > 0) {
61
+ for (i = start; i < end; i += dim)
62
+ last = insertNode(i, data[i], data[i + 1], last);
63
+ }
64
+ else {
65
+ for (i = end - dim; i >= start; i -= dim)
66
+ last = insertNode(i, data[i], data[i + 1], last);
67
+ }
68
+ if (last && equals(last, last.next)) {
69
+ removeNode(last);
70
+ last = last.next;
71
+ }
72
+ return last;
73
+ }
74
+ // eliminate colinear or duplicate points
75
+ function filterPoints(start, end) {
76
+ if (!start)
77
+ return start;
78
+ if (!end)
79
+ end = start;
80
+ let p = start, again;
81
+ do {
82
+ again = false;
83
+ if (!p.steiner &&
84
+ (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
85
+ removeNode(p);
86
+ p = end = p.prev;
87
+ if (p === p.next)
88
+ break;
89
+ again = true;
90
+ }
91
+ else {
92
+ p = p.next;
93
+ }
94
+ } while (again || p !== end);
95
+ return end;
96
+ }
97
+ // main ear slicing loop which triangulates a polygon (given as a linked list)
98
+ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
99
+ if (!ear)
100
+ return;
101
+ // interlink polygon nodes in z-order
102
+ if (!pass && invSize)
103
+ indexCurve(ear, minX, minY, invSize);
104
+ let stop = ear, prev, next;
105
+ // iterate through ears, slicing them one by one
106
+ while (ear.prev !== ear.next) {
107
+ prev = ear.prev;
108
+ next = ear.next;
109
+ if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
110
+ // cut off the triangle
111
+ triangles.push((prev.i / dim) | 0);
112
+ triangles.push((ear.i / dim) | 0);
113
+ triangles.push((next.i / dim) | 0);
114
+ removeNode(ear);
115
+ // skipping the next vertex leads to less sliver triangles
116
+ ear = next.next;
117
+ stop = next.next;
118
+ continue;
119
+ }
120
+ ear = next;
121
+ // if we looped through the whole remaining polygon and can't find any more ears
122
+ if (ear === stop) {
123
+ // try filtering points and slicing again
124
+ if (!pass) {
125
+ earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
126
+ // if this didn't work, try curing all small self-intersections locally
127
+ }
128
+ else if (pass === 1) {
129
+ ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
130
+ earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
131
+ // as a last resort, try splitting the remaining polygon into two
132
+ }
133
+ else if (pass === 2) {
134
+ splitEarcut(ear, triangles, dim, minX, minY, invSize);
135
+ }
136
+ break;
137
+ }
138
+ }
139
+ }
140
+ //min & max are calculated like this for speed
141
+ function getMin(a, b, c) {
142
+ if (a < b) {
143
+ if (a < c) {
144
+ return a;
145
+ }
146
+ return c;
147
+ }
148
+ if (b < c) {
149
+ return b;
150
+ }
151
+ return c;
152
+ }
153
+ function getMax(a, b, c) {
154
+ if (a > b) {
155
+ if (a > c) {
156
+ return a;
157
+ }
158
+ return c;
159
+ }
160
+ if (b > c) {
161
+ return b;
162
+ }
163
+ return c;
164
+ }
165
+ function triangleBBox(ax, bx, cx, ay, by, cy) {
166
+ const x0 = getMin(ax, bx, cx);
167
+ const y0 = getMin(ay, by, cy);
168
+ const x1 = getMax(ax, bx, cx);
169
+ const y1 = getMax(ay, by, cy);
170
+ return [x0, y0, x1, y1];
171
+ }
172
+ // check whether a polygon node forms a valid ear with adjacent nodes
173
+ function isEar(ear) {
174
+ const a = ear.prev, b = ear, c = ear.next;
175
+ if (area(a, b, c) >= 0)
176
+ return false; // reflex, can't be an ear
177
+ // now make sure we don't have other points inside the potential ear
178
+ const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
179
+ const [x0, y0, x1, y1] = triangleBBox(ax, bx, cx, ay, by, cy);
180
+ let p = c.next;
181
+ while (p !== a) {
182
+ if (p.x >= x0 &&
183
+ p.x <= x1 &&
184
+ p.y >= y0 &&
185
+ p.y <= y1 &&
186
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
187
+ area(p.prev, p, p.next) >= 0)
188
+ return false;
189
+ p = p.next;
190
+ }
191
+ return true;
192
+ }
193
+ function isEarHashed(ear, minX, minY, invSize) {
194
+ const a = ear.prev, b = ear, c = ear.next;
195
+ if (area(a, b, c) >= 0)
196
+ return false; // reflex, can't be an ear
197
+ const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
198
+ const [x0, y0, x1, y1] = triangleBBox(ax, bx, cx, ay, by, cy);
199
+ // z-order range for the current triangle bbox;
200
+ const minZ = zOrder(x0, y0, minX, minY, invSize), maxZ = zOrder(x1, y1, minX, minY, invSize);
201
+ let p = ear.prevZ;
202
+ let n = ear.nextZ;
203
+ // look for points inside the triangle in both directions
204
+ while (p && p.z >= minZ && n && n.z <= maxZ) {
205
+ if (p.x >= x0 &&
206
+ p.x <= x1 &&
207
+ p.y >= y0 &&
208
+ p.y <= y1 &&
209
+ p !== a &&
210
+ p !== c &&
211
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
212
+ area(p.prev, p, p.next) >= 0)
213
+ return false;
214
+ p = p.prevZ;
215
+ if (n.x >= x0 &&
216
+ n.x <= x1 &&
217
+ n.y >= y0 &&
218
+ n.y <= y1 &&
219
+ n !== a &&
220
+ n !== c &&
221
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
222
+ area(n.prev, n, n.next) >= 0)
223
+ return false;
224
+ n = n.nextZ;
225
+ }
226
+ // look for remaining points in decreasing z-order
227
+ while (p && p.z >= minZ) {
228
+ if (p.x >= x0 &&
229
+ p.x <= x1 &&
230
+ p.y >= y0 &&
231
+ p.y <= y1 &&
232
+ p !== a &&
233
+ p !== c &&
234
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
235
+ area(p.prev, p, p.next) >= 0)
236
+ return false;
237
+ p = p.prevZ;
238
+ }
239
+ // look for remaining points in increasing z-order
240
+ while (n && n.z <= maxZ) {
241
+ if (n.x >= x0 &&
242
+ n.x <= x1 &&
243
+ n.y >= y0 &&
244
+ n.y <= y1 &&
245
+ n !== a &&
246
+ n !== c &&
247
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
248
+ area(n.prev, n, n.next) >= 0)
249
+ return false;
250
+ n = n.nextZ;
251
+ }
252
+ return true;
253
+ }
254
+ // go through all polygon nodes and cure small local self-intersections
255
+ function cureLocalIntersections(start, triangles, dim) {
256
+ let p = start;
257
+ do {
258
+ const a = p.prev, b = p.next.next;
259
+ if (!equals(a, b) &&
260
+ intersects(a, p, p.next, b) &&
261
+ locallyInside(a, b) &&
262
+ locallyInside(b, a)) {
263
+ triangles.push((a.i / dim) | 0);
264
+ triangles.push((p.i / dim) | 0);
265
+ triangles.push((b.i / dim) | 0);
266
+ // remove two nodes involved
267
+ removeNode(p);
268
+ removeNode(p.next);
269
+ p = start = b;
270
+ }
271
+ p = p.next;
272
+ } while (p !== start);
273
+ return filterPoints(p);
274
+ }
275
+ // try splitting polygon into two and triangulate them independently
276
+ function splitEarcut(start, triangles, dim, minX, minY, invSize) {
277
+ // look for a valid diagonal that divides the polygon into two
278
+ let a = start;
279
+ do {
280
+ let b = a.next.next;
281
+ while (b !== a.prev) {
282
+ if (a.i !== b.i && isValidDiagonal(a, b)) {
283
+ // split the polygon in two by the diagonal
284
+ let c = splitPolygon(a, b);
285
+ // filter colinear points around the cuts
286
+ a = filterPoints(a, a.next);
287
+ c = filterPoints(c, c.next);
288
+ // run earcut on each half
289
+ earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
290
+ earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
291
+ return;
292
+ }
293
+ b = b.next;
294
+ }
295
+ a = a.next;
296
+ } while (a !== start);
297
+ }
298
+ // interlink polygon nodes in z-order
299
+ function indexCurve(start, minX, minY, invSize) {
300
+ let p = start;
301
+ do {
302
+ if (p.z === 0)
303
+ p.z = zOrder(p.x, p.y, minX, minY, invSize);
304
+ p.prevZ = p.prev;
305
+ p.nextZ = p.next;
306
+ p = p.next;
307
+ } while (p !== start);
308
+ if (p.prevZ) {
309
+ p.prevZ.nextZ = null;
310
+ }
311
+ p.prevZ = null;
312
+ sortLinked(p);
313
+ }
314
+ // Simon Tatham's linked list merge sort algorithm
315
+ // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
316
+ function sortLinked(list) {
317
+ let i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
318
+ do {
319
+ p = list;
320
+ list = null;
321
+ tail = null;
322
+ numMerges = 0;
323
+ while (p) {
324
+ numMerges++;
325
+ q = p;
326
+ pSize = 0;
327
+ for (i = 0; i < inSize; i++) {
328
+ pSize++;
329
+ q = q.nextZ;
330
+ if (!q)
331
+ break;
332
+ }
333
+ qSize = inSize;
334
+ while (pSize > 0 || (qSize > 0 && q)) {
335
+ if (p &&
336
+ pSize !== 0 &&
337
+ (qSize === 0 || !q || p.z <= q.z)) {
338
+ e = p;
339
+ p = p === null || p === void 0 ? void 0 : p.nextZ;
340
+ pSize--;
341
+ }
342
+ else {
343
+ e = q;
344
+ q = q === null || q === void 0 ? void 0 : q.nextZ;
345
+ qSize--;
346
+ }
347
+ if (tail)
348
+ tail.nextZ = e;
349
+ else
350
+ list = e;
351
+ if (e === null || e === void 0 ? void 0 : e.prevZ)
352
+ e.prevZ = tail;
353
+ tail = e;
354
+ }
355
+ p = q;
356
+ }
357
+ if (tail)
358
+ tail.nextZ = null;
359
+ inSize *= 2;
360
+ } while (numMerges > 1);
361
+ return list;
362
+ }
363
+ // z-order of a point given coords and inverse of the longer side of data bbox
364
+ function zOrder(x, y, minX, minY, invSize) {
365
+ // coords are transformed into non-negative 15-bit integer range
366
+ x = ((x - minX) * invSize) | 0;
367
+ y = ((y - minY) * invSize) | 0;
368
+ x = (x | (x << 8)) & 0x00ff00ff;
369
+ x = (x | (x << 4)) & 0x0f0f0f0f;
370
+ x = (x | (x << 2)) & 0x33333333;
371
+ x = (x | (x << 1)) & 0x55555555;
372
+ y = (y | (y << 8)) & 0x00ff00ff;
373
+ y = (y | (y << 4)) & 0x0f0f0f0f;
374
+ y = (y | (y << 2)) & 0x33333333;
375
+ y = (y | (y << 1)) & 0x55555555;
376
+ return x | (y << 1);
377
+ }
378
+ // check if a point lies within a convex triangle
379
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
380
+ return ((cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
381
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
382
+ (bx - px) * (cy - py) >= (cx - px) * (by - py));
383
+ }
384
+ // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
385
+ function isValidDiagonal(a, b) {
386
+ return (a.next.i !== b.i &&
387
+ a.prev.i !== b.i &&
388
+ !intersectsPolygon(a, b) && // dones't intersect other edges
389
+ ((locallyInside(a, b) &&
390
+ locallyInside(b, a) &&
391
+ middleInside(a, b) && // locally visible
392
+ (area(a.prev, a, b.prev) || area(a, b.prev, b))) || // does not create opposite-facing sectors
393
+ (equals(a, b) &&
394
+ area(a.prev, a, a.next) > 0 &&
395
+ area(b.prev, b, b.next) > 0))); // special zero-length case
396
+ }
397
+ // signed area of a triangle
398
+ function area(p, q, r) {
399
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
400
+ }
401
+ // check if two points are equal
402
+ function equals(p1, p2) {
403
+ return p1.x === p2.x && p1.y === p2.y;
404
+ }
405
+ // check if two segments intersect
406
+ function intersects(p1, q1, p2, q2) {
407
+ const o1 = sign(area(p1, q1, p2));
408
+ const o2 = sign(area(p1, q1, q2));
409
+ const o3 = sign(area(p2, q2, p1));
410
+ const o4 = sign(area(p2, q2, q1));
411
+ if (o1 !== o2 && o3 !== o4)
412
+ return true; // general case
413
+ if (o1 === 0 && onSegment(p1, p2, q1))
414
+ return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
415
+ if (o2 === 0 && onSegment(p1, q2, q1))
416
+ return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
417
+ if (o3 === 0 && onSegment(p2, p1, q2))
418
+ return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
419
+ if (o4 === 0 && onSegment(p2, q1, q2))
420
+ return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
421
+ return false;
422
+ }
423
+ // for collinear points p, q, r, check if point q lies on segment pr
424
+ function onSegment(p, q, r) {
425
+ return (q.x <= Math.max(p.x, r.x) &&
426
+ q.x >= Math.min(p.x, r.x) &&
427
+ q.y <= Math.max(p.y, r.y) &&
428
+ q.y >= Math.min(p.y, r.y));
429
+ }
430
+ function sign(num) {
431
+ if (num > 0) {
432
+ return 1;
433
+ }
434
+ if (num < 0) {
435
+ return -1;
436
+ }
437
+ return 0;
438
+ }
439
+ // check if a polygon diagonal intersects any polygon segments
440
+ function intersectsPolygon(a, b) {
441
+ let p = a;
442
+ do {
443
+ if (p.i !== a.i &&
444
+ p.next.i !== a.i &&
445
+ p.i !== b.i &&
446
+ p.next.i !== b.i &&
447
+ intersects(p, p.next, a, b))
448
+ return true;
449
+ p = p.next;
450
+ } while (p !== a);
451
+ return false;
452
+ }
453
+ // check if a polygon diagonal is locally inside the polygon
454
+ function locallyInside(a, b) {
455
+ return area(a.prev, a, a.next) < 0
456
+ ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
457
+ : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
458
+ }
459
+ // check if the middle point of a polygon diagonal is inside the polygon
460
+ function middleInside(a, b) {
461
+ const px = (a.x + b.x) / 2;
462
+ const py = (a.y + b.y) / 2;
463
+ let p = a;
464
+ let inside = false;
465
+ do {
466
+ if (p.y > py !== p.next.y > py &&
467
+ p.next.y !== p.y &&
468
+ px <
469
+ ((p.next.x - p.x) * (py - p.y)) / (p.next.y - p.y) + p.x)
470
+ inside = !inside;
471
+ p = p.next;
472
+ } while (p !== a);
473
+ return inside;
474
+ }
475
+ // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
476
+ // if one belongs to the outer ring and another to a hole, it merges it into a single ring
477
+ function splitPolygon(a, b) {
478
+ const a2 = new Node(a.i, a.x, a.y), b2 = new Node(b.i, b.x, b.y), an = a.next, bp = b.prev;
479
+ a.next = b;
480
+ b.prev = a;
481
+ a2.next = an;
482
+ an.prev = a2;
483
+ b2.next = a2;
484
+ a2.prev = b2;
485
+ bp.next = b2;
486
+ b2.prev = bp;
487
+ return b2;
488
+ }
489
+ // create a node and optionally link it with previous one (in a circular doubly linked list)
490
+ function insertNode(i, x, y, last) {
491
+ const p = new Node(i, x, y);
492
+ if (!last) {
493
+ p.prev = p;
494
+ p.next = p;
495
+ }
496
+ else {
497
+ p.next = last.next;
498
+ p.prev = last;
499
+ last.next.prev = p;
500
+ last.next = p;
501
+ }
502
+ return p;
503
+ }
504
+ function removeNode(p) {
505
+ p.next.prev = p.prev;
506
+ p.prev.next = p.next;
507
+ if (p.prevZ)
508
+ p.prevZ.nextZ = p.nextZ;
509
+ if (p.nextZ)
510
+ p.nextZ.prevZ = p.prevZ;
511
+ }
512
+ function signedArea(data, start, end, dim) {
513
+ let sum = 0;
514
+ for (let i = start, j = end - dim; i < end; i += dim) {
515
+ sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
516
+ j = i;
517
+ }
518
+ return sum;
519
+ }
520
+ }
521
+ const get3DPoint = (points, index) => {
522
+ return points.slice(index * 3, (index + 1) * 3);
523
+ };
524
+ const substractPoints = (a, b) => {
525
+ return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
526
+ };
527
+ const crossProduct = (a, b) => {
528
+ return [
529
+ a[1] * b[2] - a[2] * b[1],
530
+ a[2] * b[0] - a[0] * b[2],
531
+ a[0] * b[1] - a[1] * b[0],
532
+ ];
533
+ };
534
+ const dotProduct = (a, b) => {
535
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
536
+ };
537
+ const normalize = (a) => {
538
+ const len = Math.sqrt(dotProduct(a, a));
539
+ return [a[0] / len, a[1] / len, a[2] / len];
540
+ };
541
+ /**
542
+ * Projects a 3D point to the coordinate system of the plane formed by two 3D orthogonal unit vectors u and v.
543
+ * @param u the first vector
544
+ * @param v the second vector
545
+ * @param p the point to be projected as [x, y, z] triplet.
546
+ * @returns projected point as [x, y] triplet.
547
+ */
548
+ const projectPoint = (u, v, p) => {
549
+ const a = dotProduct(p, u);
550
+ const b = dotProduct(p, v);
551
+ return [a, b];
552
+ };
553
+ /**
554
+ * Projects a polygon on the plane passing throught its points.
555
+ * Assumes the polygon to be flat, i.e. all the points lie on the same plane.
556
+ * @param points Polygon to be projected.
557
+ * @returns Projected polygon in the 2D coordinate system of the plane.
558
+ */
559
+ const projectPolygon = (points) => {
560
+ const p0 = get3DPoint(points, 0);
561
+ const p1 = get3DPoint(points, 1);
562
+ const p2 = get3DPoint(points, 2);
563
+ const v1 = substractPoints(p1, p0);
564
+ const v2 = substractPoints(p2, p0);
565
+ const normal = normalize(crossProduct(v1, v2));
566
+ const u = normalize(v1);
567
+ const v = normalize(crossProduct(normal, u));
568
+ const res = [];
569
+ const count = points.length / 3;
570
+ for (let i = 0; i < count; ++i) {
571
+ const p = get3DPoint(points, i);
572
+ const fp = projectPoint(u, v, p);
573
+ res.push(...fp);
574
+ }
575
+ return res;
576
+ };
577
+ const getLineSegment = (index0, index1, out) => {
578
+ const i1 = polys[index0];
579
+ const i2 = polys[index1];
580
+ const p1 = get3DPoint(params.points, i1);
581
+ const p2 = get3DPoint(params.points, i2);
582
+ p1[2] *= z_sign;
583
+ p2[2] *= z_sign;
584
+ out.push(...p1);
585
+ out.push(...p2);
586
+ };
2
587
  // Keep
3
588
  const t0 = performance.now();
4
589
  const params = e.data;
5
- const points = params.points;
6
590
  const polys = params.polys;
7
591
  const properties = params.properties;
8
592
  const isZIncreasingDownwards = params.isZIncreasingDownwards;
@@ -14,8 +598,8 @@ export function makeFullMesh(e) {
14
598
  let propertyValueRangeMax = -99999999;
15
599
  const z_sign = isZIncreasingDownwards ? -1 : 1;
16
600
  let pn = 0;
17
- let indice = 0;
18
601
  let i = 0;
602
+ let vertexIndex = 0;
19
603
  while (i < polys.length) {
20
604
  const n = polys[i];
21
605
  const propertyValue = properties[pn++];
@@ -31,78 +615,27 @@ export function makeFullMesh(e) {
31
615
  : propertyValueRangeMax;
32
616
  }
33
617
  // Lines.
34
- for (let j = i + 1; j < i + n; j++) {
35
- const i1 = polys[j];
36
- const i2 = polys[j + 1];
37
- const x0 = points[3 * i1 + 0];
38
- const y0 = points[3 * i1 + 1];
39
- const z0 = points[3 * i1 + 2] * z_sign;
40
- const x1 = points[3 * i2 + 0];
41
- const y1 = points[3 * i2 + 1];
42
- const z1 = points[3 * i2 + 2] * z_sign;
43
- line_positions.push(x0, y0, z0);
44
- line_positions.push(x1, y1, z1);
45
- }
46
- // Triangles.
47
- if (n == 4) {
48
- const i1 = polys[i + 1];
49
- const i2 = polys[i + 2];
50
- const i3 = polys[i + 3];
51
- const i4 = polys[i + 4];
52
- const x1 = points[3 * i1 + 0];
53
- const y1 = points[3 * i1 + 1];
54
- const z1 = points[3 * i1 + 2] * z_sign;
55
- const x2 = points[3 * i2 + 0];
56
- const y2 = points[3 * i2 + 1];
57
- const z2 = points[3 * i2 + 2] * z_sign;
58
- const x3 = points[3 * i3 + 0];
59
- const y3 = points[3 * i3 + 1];
60
- const z3 = points[3 * i3 + 2] * z_sign;
61
- const x4 = points[3 * i4 + 0];
62
- const y4 = points[3 * i4 + 1];
63
- const z4 = points[3 * i4 + 2] * z_sign;
64
- // t1
65
- indices.push(indice++, indice++, indice++);
66
- positions.push(x1, y1, z1);
67
- positions.push(x2, y2, z2);
68
- positions.push(x3, y3, z3);
69
- vertexProperties.push(propertyValue);
70
- vertexProperties.push(propertyValue);
71
- vertexProperties.push(propertyValue);
72
- // t2
73
- indices.push(indice++, indice++, indice++);
74
- positions.push(x1, y1, z1);
75
- positions.push(x3, y3, z3);
76
- positions.push(x4, y4, z4);
77
- vertexProperties.push(propertyValue);
78
- vertexProperties.push(propertyValue);
79
- vertexProperties.push(propertyValue);
618
+ for (let j = i + 1; j < i + n; ++j) {
619
+ getLineSegment(j, j + 1, line_positions);
80
620
  }
81
- else if (n == 3) {
82
- // Refactor this n == 3 && n == 4.
83
- const i1 = polys[i + 1];
84
- const i2 = polys[i + 2];
85
- const i3 = polys[i + 3];
86
- const x1 = points[3 * i1 + 0];
87
- const y1 = points[3 * i1 + 1];
88
- const z1 = points[3 * i1 + 2] * z_sign;
89
- const x2 = points[3 * i2 + 0];
90
- const y2 = points[3 * i2 + 1];
91
- const z2 = points[3 * i2 + 2] * z_sign;
92
- const x3 = points[3 * i3 + 0];
93
- const y3 = points[3 * i3 + 1];
94
- const z3 = points[3 * i3 + 2] * z_sign;
95
- // t1
96
- indices.push(indice++, indice++, indice++);
97
- positions.push(x1, y1, z1);
98
- positions.push(x2, y2, z2);
99
- positions.push(x3, y3, z3);
100
- vertexProperties.push(propertyValue);
101
- vertexProperties.push(propertyValue);
102
- vertexProperties.push(propertyValue);
621
+ getLineSegment(i + 1, i + n, line_positions);
622
+ const polygon = [];
623
+ const vertexIndices = [];
624
+ for (let p = 1; p <= n; ++p) {
625
+ const i0 = polys[i + p];
626
+ const point = get3DPoint(params.points, i0);
627
+ point[2] *= z_sign;
628
+ vertexIndices.push(i0);
629
+ polygon.push(...point);
103
630
  }
104
- else {
105
- console.error("Only triangles or four corners are expected.");
631
+ // As the triangulation algorythm works in 2D space
632
+ // the polygon should be projected on the plane passing through its points.
633
+ const flatPoly = projectPolygon(polygon);
634
+ const triangles = earcut(flatPoly, 2);
635
+ for (const t of triangles) {
636
+ positions.push(...get3DPoint(polygon, t));
637
+ vertexProperties.push(propertyValue);
638
+ indices.push(vertexIndex++);
106
639
  }
107
640
  i = i + n + 1;
108
641
  }