dgeoutils 2.4.41 → 2.4.44

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.
Files changed (52) hide show
  1. package/package.json +1 -4
  2. package/dist/cjs/DCircle.d.ts +0 -18
  3. package/dist/cjs/DCircle.js +0 -102
  4. package/dist/cjs/DLine.d.ts +0 -39
  5. package/dist/cjs/DLine.js +0 -343
  6. package/dist/cjs/DNumbers.d.ts +0 -8
  7. package/dist/cjs/DNumbers.js +0 -30
  8. package/dist/cjs/DPlane.d.ts +0 -25
  9. package/dist/cjs/DPlane.js +0 -132
  10. package/dist/cjs/DPoint.d.ts +0 -138
  11. package/dist/cjs/DPoint.js +0 -803
  12. package/dist/cjs/DPolygon.d.ts +0 -137
  13. package/dist/cjs/DPolygon.js +0 -1745
  14. package/dist/cjs/DPolygonLoop.d.ts +0 -60
  15. package/dist/cjs/DPolygonLoop.js +0 -439
  16. package/dist/cjs/FastSearch.d.ts +0 -6
  17. package/dist/cjs/FastSearch.js +0 -53
  18. package/dist/cjs/InterpolationMatrix.d.ts +0 -24
  19. package/dist/cjs/InterpolationMatrix.js +0 -173
  20. package/dist/cjs/TraceMatrix.d.ts +0 -22
  21. package/dist/cjs/TraceMatrix.js +0 -285
  22. package/dist/cjs/index.d.ts +0 -11
  23. package/dist/cjs/index.js +0 -37
  24. package/dist/cjs/utils.d.ts +0 -49
  25. package/dist/cjs/utils.js +0 -280
  26. package/dist/es2015/DCircle.js +0 -87
  27. package/dist/es2015/DLine.js +0 -290
  28. package/dist/es2015/DNumbers.js +0 -22
  29. package/dist/es2015/DPlane.js +0 -105
  30. package/dist/es2015/DPoint.js +0 -676
  31. package/dist/es2015/DPolygon.js +0 -1193
  32. package/dist/es2015/DPolygonLoop.js +0 -430
  33. package/dist/es2015/FastSearch.js +0 -25
  34. package/dist/es2015/InterpolationMatrix.js +0 -128
  35. package/dist/es2015/TraceMatrix.js +0 -229
  36. package/dist/es2015/index.js +0 -11
  37. package/dist/es2015/utils.js +0 -207
  38. package/dist/esm/DCircle.js +0 -99
  39. package/dist/esm/DLine.js +0 -340
  40. package/dist/esm/DNumbers.js +0 -27
  41. package/dist/esm/DPlane.js +0 -129
  42. package/dist/esm/DPoint.js +0 -800
  43. package/dist/esm/DPolygon.js +0 -1742
  44. package/dist/esm/DPolygonLoop.js +0 -436
  45. package/dist/esm/FastSearch.js +0 -50
  46. package/dist/esm/InterpolationMatrix.js +0 -170
  47. package/dist/esm/TraceMatrix.js +0 -282
  48. package/dist/esm/index.js +0 -11
  49. package/dist/esm/utils.js +0 -265
  50. package/dist/umd/dgeoutils.js +0 -4347
  51. package/dist/umd/dgeoutils.min.js +0 -1
  52. package/dist/umd/dgeoutils.min.js.map +0 -1
@@ -1,1742 +0,0 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __generator = (this && this.__generator) || function (thisArg, body) {
13
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
- function verb(n) { return function (v) { return step([n, v]); }; }
16
- function step(op) {
17
- if (f) throw new TypeError("Generator is already executing.");
18
- while (_) try {
19
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
- if (y = 0, t) op = [op[0] & 2, t.value];
21
- switch (op[0]) {
22
- case 0: case 1: t = op; break;
23
- case 4: _.label++; return { value: op[1], done: false };
24
- case 5: _.label++; y = op[1]; op = [0]; continue;
25
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
- default:
27
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
- if (t[2]) _.ops.pop();
32
- _.trys.pop(); continue;
33
- }
34
- op = body.call(thisArg, _);
35
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
- }
38
- };
39
- var __values = (this && this.__values) || function(o) {
40
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
41
- if (m) return m.call(o);
42
- if (o && typeof o.length === "number") return {
43
- next: function () {
44
- if (o && i >= o.length) o = void 0;
45
- return { value: o && o[i++], done: !o };
46
- }
47
- };
48
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
49
- };
50
- var __read = (this && this.__read) || function (o, n) {
51
- var m = typeof Symbol === "function" && o[Symbol.iterator];
52
- if (!m) return o;
53
- var i = m.call(o), r, ar = [], e;
54
- try {
55
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
56
- }
57
- catch (error) { e = { error: error }; }
58
- finally {
59
- try {
60
- if (r && !r.done && (m = i["return"])) m.call(i);
61
- }
62
- finally { if (e) throw e.error; }
63
- }
64
- return ar;
65
- };
66
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
67
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
68
- if (ar || !(i in from)) {
69
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
70
- ar[i] = from[i];
71
- }
72
- }
73
- return to.concat(ar || Array.prototype.slice.call(from));
74
- };
75
- import { DPoint } from './DPoint';
76
- import { DLine } from './DLine';
77
- import { DCircle } from './DCircle';
78
- import { DNumbers } from './DNumbers';
79
- import { io as jstsIo, operation } from 'jsts';
80
- import { DPolygonLoop } from './DPolygonLoop';
81
- import { DGeo, isDefAndNotNull } from './utils';
82
- var _a = operation.buffer.BufferParameters, CAP_ROUND = _a.CAP_ROUND, CAP_FLAT = _a.CAP_FLAT, CAP_SQUARE = _a.CAP_SQUARE;
83
- export var MIN_POINTS_IN_VALID_POLYGON = 3;
84
- var APPROXIMATION_VALUE = 0.1;
85
- var MAX_CONVEX_ITERATIONS = 100;
86
- var CLOSE_TO_INTERSECTION_DISTANCE = 0.001;
87
- var triangleCenter = function (triangle) {
88
- var p0 = triangle.at(0);
89
- var p1 = triangle.at(1);
90
- var p2 = triangle.at(2);
91
- return new DPoint((p0.x + p1.x + p2.x) / 3, (p0.y + p1.y + p2.y) / 3);
92
- };
93
- var containCalculator = function (poly, p) {
94
- var e_1, _a, e_2, _b;
95
- var hasSamePoint = poly.points.some(function (point) { return point.equal(p); });
96
- if (hasSamePoint) {
97
- return true;
98
- }
99
- try {
100
- for (var _c = __values(poly.loopPointsGenerator(true)()), _d = _c.next(); !_d.done; _d = _c.next()) {
101
- var _e = __read(_d.value, 3), polygonLine = _e[2];
102
- var onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
103
- if (onBorder) {
104
- return true;
105
- }
106
- }
107
- }
108
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
109
- finally {
110
- try {
111
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
112
- }
113
- finally { if (e_1) throw e_1.error; }
114
- }
115
- var totalFi = 0;
116
- try {
117
- for (var _f = __values(poly.loopPointsGenerator()()), _g = _f.next(); !_g.done; _g = _f.next()) {
118
- var _h = __read(_g.value, 2), _j = _h[0], x = _j.x, y = _j.y, _k = _h[1], a = _k.x, b = _k.y;
119
- var line1 = new DLine(x - p.x, y - p.y, 0);
120
- var line2 = new DLine(a - p.x, b - p.y, 0);
121
- var fiDif = line1.findFi(line2);
122
- if (line1.vectorProduct(line2).c > 0) {
123
- totalFi += fiDif;
124
- }
125
- else {
126
- totalFi -= fiDif;
127
- }
128
- }
129
- }
130
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
131
- finally {
132
- try {
133
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
134
- }
135
- finally { if (e_2) throw e_2.error; }
136
- }
137
- var eps = Math.PI / 10000;
138
- var result = false;
139
- var absTotalFi = Math.abs(totalFi);
140
- if (absTotalFi < eps) {
141
- result = false;
142
- }
143
- else if (Math.abs(2 * Math.PI - absTotalFi) < eps) {
144
- result = true;
145
- }
146
- else {
147
- throw new Error('contains2 faild');
148
- }
149
- return result;
150
- };
151
- var DPolygon = (function () {
152
- function DPolygon(pPoints) {
153
- if (pPoints === void 0) { pPoints = []; }
154
- this.pPoints = pPoints;
155
- this.properties = {};
156
- this.holes = [];
157
- this.searchStore = {};
158
- }
159
- DPolygon.arrayOfTrianglesToVertices = function (triangles, height) {
160
- return triangles.map(function (v) { return (isDefAndNotNull(height) ? v
161
- .loop()
162
- .height(height)
163
- .run() : v)
164
- .toArrayOfCoords(); })
165
- .flat(2);
166
- };
167
- DPolygon.minAreaRectangleSize = function (poly) {
168
- var _a = poly.clone().open(), first = _a.first, second = _a.second, last = _a.last;
169
- return new DPoint(first.distance(second), first.distance(last));
170
- };
171
- DPolygon.toDash = function (poly) {
172
- var e_3, _a;
173
- var p = new DPolygon();
174
- var result = [p];
175
- var trigger = true;
176
- try {
177
- for (var _b = __values(poly.points), _c = _b.next(); !_c.done; _c = _b.next()) {
178
- var point = _c.value;
179
- if (trigger) {
180
- p.push(point.clone());
181
- }
182
- if (point.properties.pieceBorder) {
183
- trigger = !trigger;
184
- if (trigger) {
185
- p = new DPolygon();
186
- result.push(p);
187
- p.push(point.clone());
188
- }
189
- }
190
- }
191
- }
192
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
193
- finally {
194
- try {
195
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
196
- }
197
- finally { if (e_3) throw e_3.error; }
198
- }
199
- return result;
200
- };
201
- DPolygon.minAreaRectangleDirection = function (poly) {
202
- var _a = poly.clone().open(), first = _a.first, second = _a.second, last = _a.last;
203
- if (!first || !second || !last) {
204
- return 0;
205
- }
206
- if (first.distance(second) > first.distance(last)) {
207
- return first.findLine(second).getFi();
208
- }
209
- return first.findLine(last).getFi();
210
- };
211
- DPolygon.parseFromWKT = function (wkt) {
212
- var data = wkt.trim().toUpperCase();
213
- var res = new DPolygon();
214
- if (data.indexOf('POLYGON') === 0) {
215
- var regexp = /POLYGON \(\((?<data>(?:(?!\)\)$).)*?)\)\)$/miu;
216
- var reg = regexp.exec(data);
217
- var _a = __read(reg.groups.data
218
- .split('), (')
219
- .map(function (p) { return new DPolygon(p.split(', ')
220
- .map(function (pares) { return DPoint.parse(pares.split(' ').map(Number)); })); })), path = _a[0], holes = _a.slice(1);
221
- if (holes && holes.length) {
222
- path.holes = holes;
223
- }
224
- res = path;
225
- }
226
- if (data.indexOf('LINESTRING') === 0) {
227
- var regexp = /LINESTRING \((?<data>(?:(?!\)$).)*?)\)$/miu;
228
- var reg = regexp.exec(data);
229
- res = new DPolygon(reg.groups.data
230
- .split(', ').map(function (t) { return DPoint.parse(t.split(' ').map(Number)); }));
231
- }
232
- if (data.indexOf('POINT') === 0) {
233
- res = new DPolygon([DPoint.parseFromWKT(data)]);
234
- }
235
- return res;
236
- };
237
- DPolygon.createSquareBySize = function (size) {
238
- return new DPolygon([DPoint.zero(), size.clone().setX(0), size.clone(), size.clone().setY(0)]).close();
239
- };
240
- DPolygon.prototype.loop = function () {
241
- return new DPolygonLoop(this);
242
- };
243
- Object.defineProperty(DPolygon.prototype, "points", {
244
- get: function () {
245
- return this.pPoints;
246
- },
247
- set: function (p) {
248
- this.pPoints = p;
249
- },
250
- enumerable: false,
251
- configurable: true
252
- });
253
- Object.defineProperty(DPolygon.prototype, "maxX", {
254
- get: function () {
255
- return this.reduce(function (a, r) { return Math.max(a, r.x); }, -Infinity);
256
- },
257
- enumerable: false,
258
- configurable: true
259
- });
260
- Object.defineProperty(DPolygon.prototype, "minX", {
261
- get: function () {
262
- return this.reduce(function (a, r) { return Math.min(a, r.x); }, Infinity);
263
- },
264
- enumerable: false,
265
- configurable: true
266
- });
267
- Object.defineProperty(DPolygon.prototype, "maxY", {
268
- get: function () {
269
- return this.reduce(function (a, r) { return Math.max(a, r.y); }, -Infinity);
270
- },
271
- enumerable: false,
272
- configurable: true
273
- });
274
- Object.defineProperty(DPolygon.prototype, "minY", {
275
- get: function () {
276
- return this.reduce(function (a, r) { return Math.min(a, r.y); }, Infinity);
277
- },
278
- enumerable: false,
279
- configurable: true
280
- });
281
- Object.defineProperty(DPolygon.prototype, "innerCenter", {
282
- get: function () {
283
- var center = this.center;
284
- return this.toTriangles().map(function (t) {
285
- var c = triangleCenter(t);
286
- c.properties.score = Math.min(c.distance(t.at(0)), c.distance(t.at(1)), c.distance(t.at(2))) + c.distance(center);
287
- return c;
288
- })
289
- .sort(function (a, b) { return a.properties.score - b.properties.score; })[0];
290
- },
291
- enumerable: false,
292
- configurable: true
293
- });
294
- Object.defineProperty(DPolygon.prototype, "center", {
295
- get: function () {
296
- return this.leftTop.move(this.size.divide(2));
297
- },
298
- enumerable: false,
299
- configurable: true
300
- });
301
- Object.defineProperty(DPolygon.prototype, "h", {
302
- get: function () {
303
- return this.maxY - this.minY;
304
- },
305
- enumerable: false,
306
- configurable: true
307
- });
308
- Object.defineProperty(DPolygon.prototype, "w", {
309
- get: function () {
310
- return this.maxX - this.minX;
311
- },
312
- enumerable: false,
313
- configurable: true
314
- });
315
- Object.defineProperty(DPolygon.prototype, "dY", {
316
- get: function () {
317
- return this.h;
318
- },
319
- enumerable: false,
320
- configurable: true
321
- });
322
- Object.defineProperty(DPolygon.prototype, "dX", {
323
- get: function () {
324
- return this.w;
325
- },
326
- enumerable: false,
327
- configurable: true
328
- });
329
- Object.defineProperty(DPolygon.prototype, "extend", {
330
- get: function () {
331
- var _a = this, minX = _a.minX, minY = _a.minY, maxX = _a.maxX, maxY = _a.maxY;
332
- return new DPolygon([
333
- new DPoint(minX, minY),
334
- new DPoint(maxX, minY),
335
- new DPoint(maxX, maxY),
336
- new DPoint(minX, maxY),
337
- new DPoint(minX, minY)
338
- ]);
339
- },
340
- enumerable: false,
341
- configurable: true
342
- });
343
- Object.defineProperty(DPolygon.prototype, "size", {
344
- get: function () {
345
- var _a = this, w = _a.w, h = _a.h;
346
- return new DPoint(w, h);
347
- },
348
- enumerable: false,
349
- configurable: true
350
- });
351
- Object.defineProperty(DPolygon.prototype, "leftTop", {
352
- get: function () {
353
- var _a = this, minX = _a.minX, minY = _a.minY;
354
- return new DPoint(minX, minY);
355
- },
356
- enumerable: false,
357
- configurable: true
358
- });
359
- Object.defineProperty(DPolygon.prototype, "minPoint", {
360
- get: function () {
361
- return this.leftTop;
362
- },
363
- enumerable: false,
364
- configurable: true
365
- });
366
- Object.defineProperty(DPolygon.prototype, "rightBottom", {
367
- get: function () {
368
- var _a = this, maxX = _a.maxX, maxY = _a.maxY;
369
- return new DPoint(maxX, maxY);
370
- },
371
- enumerable: false,
372
- configurable: true
373
- });
374
- Object.defineProperty(DPolygon.prototype, "maxPoint", {
375
- get: function () {
376
- return this.rightBottom;
377
- },
378
- enumerable: false,
379
- configurable: true
380
- });
381
- Object.defineProperty(DPolygon.prototype, "length", {
382
- get: function () {
383
- return this.pPoints.length;
384
- },
385
- enumerable: false,
386
- configurable: true
387
- });
388
- Object.defineProperty(DPolygon.prototype, "fullLength", {
389
- get: function () {
390
- return this.clone().open().perimeter;
391
- },
392
- enumerable: false,
393
- configurable: true
394
- });
395
- Object.defineProperty(DPolygon.prototype, "perimeter", {
396
- get: function () {
397
- var e_4, _a;
398
- var p = 0;
399
- try {
400
- for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
401
- var _d = __read(_c.value, 2), p1 = _d[0], p2 = _d[1];
402
- p += p1.distance(p2);
403
- }
404
- }
405
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
406
- finally {
407
- try {
408
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
409
- }
410
- finally { if (e_4) throw e_4.error; }
411
- }
412
- return p;
413
- },
414
- enumerable: false,
415
- configurable: true
416
- });
417
- Object.defineProperty(DPolygon.prototype, "area", {
418
- get: function () {
419
- var e_5, _a;
420
- var sum = 0;
421
- try {
422
- for (var _b = __values(this.deintersection.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
423
- var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
424
- sum += x * b - y * a;
425
- }
426
- }
427
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
428
- finally {
429
- try {
430
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
431
- }
432
- finally { if (e_5) throw e_5.error; }
433
- }
434
- return Math.abs(sum / 2) - this.holes.reduce(function (a, hole) { return a + hole.area; }, 0);
435
- },
436
- enumerable: false,
437
- configurable: true
438
- });
439
- Object.defineProperty(DPolygon.prototype, "filledDeintersection", {
440
- get: function () {
441
- var p = this.clone().deintersection.removeDuplicates().clockWise.open();
442
- var findSameCorners = function (startIndex) {
443
- if (startIndex === void 0) { startIndex = 0; }
444
- var store = {};
445
- for (var i = startIndex; i < p.length; i++) {
446
- var key = p.at(i).toString();
447
- if (typeof store[key] === 'number') {
448
- return [store[key], i];
449
- }
450
- store[key] = i;
451
- }
452
- return undefined;
453
- };
454
- var indexes = findSameCorners();
455
- var holes = [];
456
- while (indexes) {
457
- var a = new DPolygon(p.clone().removePart(indexes[0], indexes[1] - indexes[0]));
458
- if (a.isClockwise) {
459
- indexes = findSameCorners(indexes[0] + 1);
460
- }
461
- else {
462
- holes.push(a.close());
463
- p.removePart(indexes[0], indexes[1] - indexes[0]);
464
- indexes = findSameCorners();
465
- }
466
- }
467
- p.holes = holes;
468
- return p.close();
469
- },
470
- enumerable: false,
471
- configurable: true
472
- });
473
- Object.defineProperty(DPolygon.prototype, "deintersection", {
474
- get: function () {
475
- var e_6, _a;
476
- var p = this.clone().close();
477
- var store = {};
478
- for (var i = 0; i < p.length - 1; i++) {
479
- var k = p.at(i).toString();
480
- store[k] = store[k] || [];
481
- store[k].push(i);
482
- var _loop_1 = function (j) {
483
- var firstLine = p.at(i).findLine(p.at(i + 1));
484
- var secondLine = p.at(j).findLine(p.at(j + 1));
485
- var intersectionPoint = firstLine.intersection(secondLine);
486
- if (intersectionPoint &&
487
- !__spreadArray(__spreadArray([], __read(firstLine.points), false), __read(secondLine.points), false).some(function (t) { return t.like(intersectionPoint); })) {
488
- var part = p.removePart(i, j - i).reverse();
489
- p.insertAfter.apply(p, __spreadArray([i], __read(part), false));
490
- p.insertAfter(j, intersectionPoint);
491
- p.insertAfter(i, intersectionPoint);
492
- }
493
- };
494
- for (var j = i + 2; j < p.length - 1; j++) {
495
- _loop_1(j);
496
- }
497
- }
498
- try {
499
- for (var _b = __values(Object.keys(store)), _c = _b.next(); !_c.done; _c = _b.next()) {
500
- var key = _c.value;
501
- var record = store[key];
502
- if (record.length > 1) {
503
- var _loop_2 = function (j) {
504
- var origin_1 = p.clone();
505
- var d = record[j] - record[j - 1];
506
- if (d > 1) {
507
- var part = new DPolygon(origin_1.removePart(record[j - 1], d));
508
- var allInside = part.reduce(function (a, e) { return a && containCalculator(origin_1, e); }, true);
509
- if (allInside && origin_1.isClockwise === part.isClockwise) {
510
- origin_1.insertAfter.apply(origin_1, __spreadArray([record[j - 1] - 1], __read(part.reverse().points), false));
511
- p = origin_1;
512
- }
513
- }
514
- };
515
- for (var j = record.length - 1; j > 0; j--) {
516
- _loop_2(j);
517
- }
518
- }
519
- }
520
- }
521
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
522
- finally {
523
- try {
524
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
525
- }
526
- finally { if (e_6) throw e_6.error; }
527
- }
528
- return p;
529
- },
530
- enumerable: false,
531
- configurable: true
532
- });
533
- Object.defineProperty(DPolygon.prototype, "valid", {
534
- get: function () {
535
- return this.length > MIN_POINTS_IN_VALID_POLYGON;
536
- },
537
- enumerable: false,
538
- configurable: true
539
- });
540
- Object.defineProperty(DPolygon.prototype, "first", {
541
- get: function () {
542
- return this.at(0);
543
- },
544
- enumerable: false,
545
- configurable: true
546
- });
547
- Object.defineProperty(DPolygon.prototype, "second", {
548
- get: function () {
549
- return this.at(1);
550
- },
551
- enumerable: false,
552
- configurable: true
553
- });
554
- Object.defineProperty(DPolygon.prototype, "last", {
555
- get: function () {
556
- return this.at(this.length - 1);
557
- },
558
- enumerable: false,
559
- configurable: true
560
- });
561
- Object.defineProperty(DPolygon.prototype, "minAreaRectangle", {
562
- get: function () {
563
- var e_7, _a, e_8, _b;
564
- var p = this.convex;
565
- var resultPolygon = new DPolygon();
566
- var resultArea = Infinity;
567
- try {
568
- for (var _c = __values(p.loopPointsGenerator(true)()), _d = _c.next(); !_d.done; _d = _c.next()) {
569
- var _e = __read(_d.value, 3), l = _e[2];
570
- var maxWidth = 0;
571
- var maxWidthPoint1 = null;
572
- var maxWidthPoint2 = null;
573
- var maxHeight = 0;
574
- var maxHeightPoint = null;
575
- try {
576
- for (var _f = (e_8 = void 0, __values(p.loopPointsGenerator()())), _g = _f.next(); !_g.done; _g = _f.next()) {
577
- var _h = __read(_g.value, 4), z = _h[0], i = _h[3];
578
- var p1 = l.findPoint(l.findPerpendicular(z));
579
- var h = p1.distance(z);
580
- if (h >= maxHeight) {
581
- maxHeight = h;
582
- maxHeightPoint = z;
583
- }
584
- for (var j = i; j < p.length - 1; j++) {
585
- var p2 = l.findPoint(l.findPerpendicular(p.at(j)));
586
- var w = p1.distance(p2);
587
- if (w >= maxWidth) {
588
- maxWidth = w;
589
- maxWidthPoint1 = p1;
590
- maxWidthPoint2 = p2;
591
- }
592
- }
593
- }
594
- }
595
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
596
- finally {
597
- try {
598
- if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
599
- }
600
- finally { if (e_8) throw e_8.error; }
601
- }
602
- if (!maxWidthPoint1 || !maxWidthPoint2 || !maxHeightPoint) {
603
- continue;
604
- }
605
- var widthLine = maxWidthPoint1.findLine(maxWidthPoint2);
606
- var perpendicular1 = widthLine.findPerpendicular(maxWidthPoint1);
607
- var perpendicular2 = widthLine.findPerpendicular(maxWidthPoint2);
608
- var tempPolygon = new DPolygon([
609
- maxWidthPoint1,
610
- maxWidthPoint2,
611
- perpendicular2.findPoint(perpendicular2.findPerpendicular(maxHeightPoint)),
612
- perpendicular1.findPoint(perpendicular1.findPerpendicular(maxHeightPoint))
613
- ]).close();
614
- if (tempPolygon.area < resultArea) {
615
- resultPolygon = tempPolygon;
616
- resultArea = tempPolygon.area;
617
- }
618
- }
619
- }
620
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
621
- finally {
622
- try {
623
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
624
- }
625
- finally { if (e_7) throw e_7.error; }
626
- }
627
- return resultPolygon;
628
- },
629
- enumerable: false,
630
- configurable: true
631
- });
632
- Object.defineProperty(DPolygon.prototype, "convex", {
633
- get: function () {
634
- var p = this.clone().open();
635
- var isClockwise = p.isClockwise;
636
- if (!isClockwise) {
637
- p.reverse();
638
- }
639
- var l = 0;
640
- do {
641
- var p1 = p.last;
642
- var p2 = p.first;
643
- var p3 = p.second;
644
- var d = p2.findInnerAngle(p1, p3);
645
- if (d > Math.PI || DNumbers.likeZero(DNumbers.rad2Deg(d)) || DNumbers.likePI(d) || DNumbers.like2PI(d)) {
646
- p.removePart(-1, 1);
647
- }
648
- else {
649
- break;
650
- }
651
- } while (p.length);
652
- p.close();
653
- var iteration = 0;
654
- do {
655
- p = p.deintersection;
656
- l = p.length;
657
- for (var i = 1; i < p.length - 1; i++) {
658
- var p1 = p.at(i - 1);
659
- var p2 = p.at(i);
660
- var p3 = p.at(i + 1);
661
- var d = p2.findInnerAngle(p1, p3);
662
- if (d > Math.PI || DNumbers.likeZero(DNumbers.rad2Deg(d)) || DNumbers.likePI(d) || DNumbers.like2PI(d)) {
663
- p.removePart(--i, 1);
664
- }
665
- }
666
- iteration++;
667
- } while (p.length !== l && iteration < MAX_CONVEX_ITERATIONS);
668
- if (!isClockwise) {
669
- p.reverse();
670
- }
671
- return p;
672
- },
673
- enumerable: false,
674
- configurable: true
675
- });
676
- Object.defineProperty(DPolygon.prototype, "isClockwise", {
677
- get: function () {
678
- var e_9, _a;
679
- var sum = 0;
680
- try {
681
- for (var _b = __values(this.clone().close()
682
- .loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
683
- var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
684
- sum += (a - x) * (b + y);
685
- }
686
- }
687
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
688
- finally {
689
- try {
690
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
691
- }
692
- finally { if (e_9) throw e_9.error; }
693
- }
694
- return sum < 0;
695
- },
696
- enumerable: false,
697
- configurable: true
698
- });
699
- Object.defineProperty(DPolygon.prototype, "clockWise", {
700
- get: function () {
701
- if (this.isClockwise) {
702
- return this.clone();
703
- }
704
- return this.clone().reverse();
705
- },
706
- enumerable: false,
707
- configurable: true
708
- });
709
- Object.defineProperty(DPolygon.prototype, "noHoles", {
710
- get: function () {
711
- var res = this.clone();
712
- res.holes = [];
713
- return res;
714
- },
715
- enumerable: false,
716
- configurable: true
717
- });
718
- DPolygon.prototype.reduce = function (f, v) {
719
- return this.pPoints.reduce(f, v);
720
- };
721
- DPolygon.prototype.intersection = function (l, includeOnly) {
722
- var e_10, _a;
723
- if (includeOnly === void 0) { includeOnly = false; }
724
- var res = [];
725
- try {
726
- for (var _b = __values(this.loopPointsGenerator(true)()), _c = _b.next(); !_c.done; _c = _b.next()) {
727
- var _d = __read(_c.value, 3), line = _d[2];
728
- var intersect = line.intersection(l, 0, includeOnly);
729
- if (intersect) {
730
- res.push(intersect);
731
- }
732
- }
733
- }
734
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
735
- finally {
736
- try {
737
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
738
- }
739
- finally { if (e_10) throw e_10.error; }
740
- }
741
- return res;
742
- };
743
- DPolygon.prototype.setCenter = function (newCenter) {
744
- return this.loop()
745
- .move(newCenter.clone().move(this.center.minus()))
746
- .run();
747
- };
748
- DPolygon.prototype.toWKT = function (type, withZ) {
749
- if (type === void 0) { type = DPolygon.WKT_POLYGON; }
750
- if (withZ === void 0) { withZ = false; }
751
- if (type === DPolygon.WKT_POLYGON) {
752
- var h = '';
753
- if (this.holes && this.holes.length) {
754
- h = ", ".concat(this.holes.map(function (hole) { return hole.toString(); })
755
- .join(', '));
756
- }
757
- return "POLYGON ((".concat(this.deintersection.mapArray(function (r) { return "".concat(r.x, " ").concat(r.y).concat(withZ ? " ".concat(r.z) : ''); })
758
- .join(', '), ")").concat(h, ")");
759
- }
760
- return "LINESTRING (".concat(this.mapArray(function (r) { return "".concat(r.x, " ").concat(r.y).concat(withZ ? " ".concat(r.z) : ''); })
761
- .join(', '), ")");
762
- };
763
- DPolygon.prototype.filter = function (f) {
764
- this.pPoints = this.pPoints.filter(f);
765
- return this;
766
- };
767
- DPolygon.prototype.map = function (f) {
768
- this.pPoints = this.mapArray(f);
769
- this.holes = this.holes.map(function (h) { return h.map(f); });
770
- return this;
771
- };
772
- DPolygon.prototype.mapArray = function (f) {
773
- return this.pPoints.map(f);
774
- };
775
- DPolygon.prototype.sort = function (f) {
776
- this.points.sort(f);
777
- return this;
778
- };
779
- DPolygon.prototype.at = function (index) {
780
- var length = this.length;
781
- return this.points[(index % length + length) % length];
782
- };
783
- DPolygon.prototype.pop = function () {
784
- return this.pPoints.pop();
785
- };
786
- DPolygon.prototype.push = function () {
787
- var _a;
788
- var args = [];
789
- for (var _i = 0; _i < arguments.length; _i++) {
790
- args[_i] = arguments[_i];
791
- }
792
- return (_a = this.pPoints).push.apply(_a, __spreadArray([], __read(args), false));
793
- };
794
- DPolygon.prototype.shift = function () {
795
- return this.pPoints.shift();
796
- };
797
- DPolygon.prototype.unshift = function () {
798
- var _a;
799
- var args = [];
800
- for (var _i = 0; _i < arguments.length; _i++) {
801
- args[_i] = arguments[_i];
802
- }
803
- return (_a = this.pPoints).unshift.apply(_a, __spreadArray([], __read(args), false));
804
- };
805
- DPolygon.prototype.reverse = function () {
806
- this.pPoints = this.pPoints.reverse();
807
- this.holes = this.holes.map(function (h) { return h.reverse(); });
808
- return this;
809
- };
810
- DPolygon.prototype.getValue = function () {
811
- return (this.pPoints.map(function (r) { return r.getValue(); }) + this.holes
812
- .reduce(function (a, h) { return a + h.getValue(); }, ''));
813
- };
814
- DPolygon.prototype.toString = function () {
815
- return "(".concat(this.mapArray(function (r) { return r.toString(); }).join(', '), ")");
816
- };
817
- DPolygon.prototype.close = function () {
818
- var p0 = this.first;
819
- if (p0 && !this.closed) {
820
- this.push(p0.clone());
821
- }
822
- return this;
823
- };
824
- DPolygon.prototype.open = function () {
825
- var p = this.first;
826
- if (this.length > 2 && p && this.closed) {
827
- this.pop();
828
- }
829
- return this;
830
- };
831
- DPolygon.prototype.add = function (poly) {
832
- var res = new DPolygon(__spreadArray(__spreadArray([], __read(this.points), false), __read(poly.points), false)).close();
833
- res.holes = __spreadArray(__spreadArray([], __read(this.holes), false), __read(poly.holes), false).map(function (h) { return h.clone(); });
834
- return res;
835
- };
836
- DPolygon.prototype.has = function (p) {
837
- return this.pPoints.some(function (q) { return q.equal(p); });
838
- };
839
- DPolygon.prototype.clone = function () {
840
- var res = new DPolygon(this.points.map(function (r) { return r.clone(); }));
841
- res.holes = this.holes.map(function (h) { return h.clone(); });
842
- res.properties = __assign({}, this.properties);
843
- return res;
844
- };
845
- DPolygon.prototype.equal = function (p) {
846
- if (!(p instanceof DPolygon)) {
847
- return false;
848
- }
849
- if (this.length !== p.length || this.holes.length !== p.holes.length) {
850
- return false;
851
- }
852
- return (this.same(p) &&
853
- this.holes.reduce(function (a, hole) { return a && p.holes.some(function (pHoles) { return pHoles.same(hole); }); }, true));
854
- };
855
- DPolygon.prototype.same = function (p) {
856
- var pClone = p.clone().close();
857
- var thisAsString = this.clone()
858
- .close()
859
- .toString();
860
- for (var i = 0; i < pClone.length; i++) {
861
- if (thisAsString === pClone.toString() || thisAsString === pClone.clone().reverse()
862
- .toString()) {
863
- return true;
864
- }
865
- pClone.nextStart();
866
- }
867
- return false;
868
- };
869
- DPolygon.prototype.findIndex = function (a) {
870
- if (a instanceof DPoint) {
871
- return this.points.findIndex(function (t) { return t.equal(a); });
872
- }
873
- return this.points.findIndex(a);
874
- };
875
- DPolygon.prototype.approximation = function (e) {
876
- if (e === void 0) { e = Math.sqrt(this.perimeter) * APPROXIMATION_VALUE; }
877
- return new DPolygon(this.clone().douglasPeucker(this.pPoints, e));
878
- };
879
- DPolygon.prototype.insertAfter = function (index) {
880
- var _a;
881
- var points = [];
882
- for (var _i = 1; _i < arguments.length; _i++) {
883
- points[_i - 1] = arguments[_i];
884
- }
885
- (_a = this.pPoints).splice.apply(_a, __spreadArray([index + 1, 0], __read(points), false));
886
- };
887
- DPolygon.prototype.removePart = function (index, count) {
888
- return this.pPoints.splice(index + 1, count);
889
- };
890
- DPolygon.prototype.hasSimpleIntersection = function (p) {
891
- var extend1 = this.extend;
892
- var extend2 = p.extend;
893
- var extend1points = extend1.points;
894
- var extend2points = extend2.points;
895
- var in1 = extend1points.some(function (t) { return extend2.simpleInclude(t); });
896
- var in2 = extend2points.some(function (t) { return extend1.simpleInclude(t); });
897
- return in1 || in2;
898
- };
899
- DPolygon.prototype.simpleInclude = function (p) {
900
- return this.simpleIncludeX(p) && this.simpleIncludeY(p);
901
- };
902
- DPolygon.prototype.drawPolygonOnCanvas = function (canvas, fillColor, strokeColor, shadowColor, lineWidth, steps) {
903
- if (lineWidth === void 0) { lineWidth = 1; }
904
- if (steps === void 0) { steps = this.length - 1; }
905
- if (this.length > 1) {
906
- var ctx = canvas.getContext('2d');
907
- if (fillColor) {
908
- ctx.fillStyle = fillColor;
909
- }
910
- if (strokeColor) {
911
- ctx.strokeStyle = strokeColor;
912
- }
913
- if (lineWidth) {
914
- ctx.lineWidth = lineWidth;
915
- }
916
- if (fillColor || strokeColor) {
917
- ctx.beginPath();
918
- }
919
- this.goByPath(ctx, steps % this.length);
920
- if (shadowColor) {
921
- ctx.shadowColor = shadowColor;
922
- ctx.shadowBlur = 0;
923
- ctx.shadowOffsetX = 1;
924
- ctx.shadowOffsetY = 1;
925
- }
926
- if (fillColor) {
927
- ctx.closePath();
928
- ctx.fill();
929
- }
930
- if (strokeColor) {
931
- ctx.stroke();
932
- }
933
- }
934
- return this;
935
- };
936
- DPolygon.prototype.clearPolygonOnCanvas = function (canvas) {
937
- var ctx = canvas.getContext('2d');
938
- var old = ctx.globalCompositeOperation;
939
- ctx.globalCompositeOperation = 'destination-out';
940
- this.goByPath(ctx);
941
- ctx.fill();
942
- ctx.globalCompositeOperation = old;
943
- };
944
- DPolygon.prototype.contain = function (p, isBorderInside) {
945
- var e_11, _a;
946
- if (isBorderInside === void 0) { isBorderInside = false; }
947
- var simpleInclude = this.simpleInclude(p);
948
- if (!simpleInclude) {
949
- return false;
950
- }
951
- var onBorder = this.onBorder(p);
952
- if (onBorder) {
953
- return isBorderInside;
954
- }
955
- var poly = this.deintersection;
956
- var totalFi = 0;
957
- try {
958
- for (var _b = __values(poly.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
959
- var _d = __read(_c.value, 2), _e = _d[0], x = _e.x, y = _e.y, _f = _d[1], a = _f.x, b = _f.y;
960
- var line1 = new DLine(x - p.x, y - p.y, 0);
961
- var line2 = new DLine(a - p.x, b - p.y, 0);
962
- var fiDif = line1.findFi(line2);
963
- if (line1.vectorProduct(line2).c > 0) {
964
- totalFi += fiDif;
965
- }
966
- else {
967
- totalFi -= fiDif;
968
- }
969
- }
970
- }
971
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
972
- finally {
973
- try {
974
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
975
- }
976
- finally { if (e_11) throw e_11.error; }
977
- }
978
- var eps = Math.PI / 10000;
979
- var absTotalFi = Math.abs(totalFi);
980
- if (absTotalFi < eps) {
981
- return false;
982
- }
983
- else if (Math.abs(2 * Math.PI - absTotalFi) < eps) {
984
- return true;
985
- }
986
- throw new Error('contains2 faild');
987
- };
988
- DPolygon.prototype.onBorder = function (p) {
989
- var e_12, _a;
990
- var simpleInclude = this.simpleInclude(p);
991
- if (simpleInclude) {
992
- var poly = this.deintersection;
993
- var hasSamePoint = this.points.some(function (point) { return point.equal(p); });
994
- if (hasSamePoint) {
995
- return true;
996
- }
997
- try {
998
- for (var _b = __values(poly.loopPointsGenerator(true)()), _c = _b.next(); !_c.done; _c = _b.next()) {
999
- var _d = __read(_c.value, 3), polygonLine = _d[2];
1000
- var onBorder = polygonLine.x(p).equal(p) && polygonLine.inRange(p);
1001
- if (onBorder) {
1002
- return true;
1003
- }
1004
- }
1005
- }
1006
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
1007
- finally {
1008
- try {
1009
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1010
- }
1011
- finally { if (e_12) throw e_12.error; }
1012
- }
1013
- }
1014
- return false;
1015
- };
1016
- DPolygon.prototype.nextStart = function () {
1017
- this.open();
1018
- this.push(this.shift());
1019
- this.close();
1020
- return this;
1021
- };
1022
- DPolygon.prototype.removeDuplicates = function () {
1023
- for (var i = 0; i < this.length - 1; i++) {
1024
- var p1 = this.at(i);
1025
- var p2 = this.at(i + 1);
1026
- if (p1.equal(p2)) {
1027
- this.removePart(i, 1);
1028
- i--;
1029
- }
1030
- }
1031
- return this;
1032
- };
1033
- DPolygon.toGeoJSONFeatureCollection = function (polygons, format) {
1034
- if (format === void 0) { format = DGeo.parseFormat; }
1035
- return {
1036
- type: 'FeatureCollection',
1037
- features: polygons.map(function (polygon) { return polygon.toGeoJSONFeature(format); })
1038
- };
1039
- };
1040
- DPolygon.parse = function (a, format) {
1041
- if (format === void 0) { format = DGeo.parseFormat; }
1042
- if (a.type) {
1043
- switch (a.type) {
1044
- case 'FeatureCollection':
1045
- return a.features.reduce(function (ak, f) {
1046
- var t = DPolygon.parse(f, format);
1047
- if (Array.isArray(t)) {
1048
- ak.push.apply(ak, __spreadArray([], __read(t), false));
1049
- }
1050
- else {
1051
- ak.push(t);
1052
- }
1053
- return ak;
1054
- }, []);
1055
- case 'Feature': {
1056
- var t = DPolygon.parse(a.geometry, format);
1057
- var props_1 = __assign(__assign({}, a.properties), (a.id ? ({
1058
- id: a.id
1059
- }) : ({})));
1060
- if (Array.isArray(t)) {
1061
- t.forEach(function (record) {
1062
- record.properties = __assign({}, props_1);
1063
- });
1064
- }
1065
- else {
1066
- t.properties = __assign({}, props_1);
1067
- }
1068
- return t;
1069
- }
1070
- case 'LineString':
1071
- case 'MultiPoint':
1072
- return new DPolygon(a.coordinates.map(function (c) { return DPoint.parse(c, format); }));
1073
- case 'Polygon':
1074
- return a.coordinates.reduce(function (ak, line, index) {
1075
- if (index === 0) {
1076
- ak.points = line.map(function (c) { return DPoint.parse(c, format); });
1077
- }
1078
- else {
1079
- ak.holes.push(new DPolygon(line.map(function (c) { return DPoint.parse(c, format); })));
1080
- }
1081
- return ak;
1082
- }, new DPolygon());
1083
- case 'MultiLineString':
1084
- return a.coordinates.reduce(function (ak, line) {
1085
- ak.push(new DPolygon(line.map(function (c) { return DPoint.parse(c, format); })));
1086
- return ak;
1087
- }, []);
1088
- case 'MultiPolygon':
1089
- return a.coordinates.reduce(function (ak, coordinates) {
1090
- ak.push(DPolygon.parse({
1091
- type: 'Polygon',
1092
- coordinates: coordinates
1093
- }, format));
1094
- return ak;
1095
- }, []);
1096
- case 'GeometryCollection':
1097
- return a.geometries.reduce(function (ak, line) {
1098
- ak.push(DPolygon.parse(line, format));
1099
- return ak;
1100
- }, []);
1101
- default:
1102
- }
1103
- }
1104
- return new DPolygon(a
1105
- .map(function (r) { return DPoint.parse(r, format); }));
1106
- };
1107
- DPolygon.prototype.toArrayOfCoords = function (format) {
1108
- if (format === void 0) { format = DGeo.parseFormat; }
1109
- return this.mapArray(function (r) { return r.toCoords(format); });
1110
- };
1111
- DPolygon.prototype.toGeoJSONFeature = function (format) {
1112
- if (format === void 0) { format = DGeo.parseFormat; }
1113
- return {
1114
- type: 'Feature',
1115
- properties: __assign({}, this.properties),
1116
- geometry: this.toGeoJSON(format)
1117
- };
1118
- };
1119
- DPolygon.prototype.toGeoJSON = function (format) {
1120
- if (format === void 0) { format = DGeo.parseFormat; }
1121
- if (this.closed) {
1122
- return {
1123
- type: 'Polygon',
1124
- coordinates: __spreadArray([
1125
- this.toArrayOfCoords(format)
1126
- ], __read(this.holes.map(function (h) { return h.toArrayOfCoords(format); })), false)
1127
- };
1128
- }
1129
- return {
1130
- type: 'LineString',
1131
- coordinates: this.toArrayOfCoords(format)
1132
- };
1133
- };
1134
- DPolygon.prototype.divideToPieces = function (piecesCount, withAltitude) {
1135
- var e_13, _a;
1136
- if (withAltitude === void 0) { withAltitude = false; }
1137
- var fullLength = this.fullLength;
1138
- var pieceLength = fullLength / piecesCount;
1139
- var currentPieceLength = pieceLength;
1140
- var _loop_3 = function (p1, p2, i) {
1141
- var d = p1.distance(p2);
1142
- if (d === currentPieceLength) {
1143
- p2.properties.pieceBorder = true;
1144
- currentPieceLength = pieceLength;
1145
- }
1146
- else if (d - currentPieceLength > 0) {
1147
- var circle = new DCircle(p1, currentPieceLength);
1148
- var line_1 = p1.findLine(p2);
1149
- var intersectionPoint = line_1.intersectionWithCircle(circle)
1150
- .filter(function (p) { return line_1.inRange(p, CLOSE_TO_INTERSECTION_DISTANCE); })[0];
1151
- intersectionPoint.properties.pieceBorder = true;
1152
- this_1.insertAfter(i, intersectionPoint);
1153
- if (withAltitude) {
1154
- var p1z = p1.z;
1155
- intersectionPoint.z = p1z - (p1z - p2.z) * (p1.distance(intersectionPoint) / d);
1156
- }
1157
- currentPieceLength = pieceLength;
1158
- }
1159
- else {
1160
- currentPieceLength -= d;
1161
- }
1162
- };
1163
- var this_1 = this;
1164
- try {
1165
- for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
1166
- var _d = __read(_c.value, 4), p1 = _d[0], p2 = _d[1], i = _d[3];
1167
- _loop_3(p1, p2, i);
1168
- }
1169
- }
1170
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
1171
- finally {
1172
- try {
1173
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1174
- }
1175
- finally { if (e_13) throw e_13.error; }
1176
- }
1177
- return this;
1178
- };
1179
- DPolygon.prototype.prepareToFastSearch = function () {
1180
- var e_14, _a;
1181
- this.searchStore = {};
1182
- try {
1183
- for (var _b = __values(this.points), _c = _b.next(); !_c.done; _c = _b.next()) {
1184
- var _d = _c.value, x = _d.x, y = _d.y, z = _d.z;
1185
- if (!this.searchStore[x]) {
1186
- this.searchStore[x] = {};
1187
- }
1188
- if (!this.searchStore[x][y]) {
1189
- this.searchStore[x][y] = {};
1190
- }
1191
- this.searchStore[x][y][z || 'undefined'] = true;
1192
- }
1193
- }
1194
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
1195
- finally {
1196
- try {
1197
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1198
- }
1199
- finally { if (e_14) throw e_14.error; }
1200
- }
1201
- };
1202
- DPolygon.prototype.fastHas = function (_a) {
1203
- var x = _a.x, y = _a.y, z = _a.z;
1204
- if (!this.searchStore[x]) {
1205
- return false;
1206
- }
1207
- if (!this.searchStore[x][y]) {
1208
- return false;
1209
- }
1210
- if (!this.searchStore[x][y][z || 'undefined']) {
1211
- return false;
1212
- }
1213
- return this.searchStore[x][y][z || 'undefined'];
1214
- };
1215
- Object.defineProperty(DPolygon.prototype, "growingPiecesGenerator", {
1216
- get: function () {
1217
- var polygon = this;
1218
- return function () {
1219
- var r, _a, _b, p, e_15_1;
1220
- var e_15, _c;
1221
- return __generator(this, function (_d) {
1222
- switch (_d.label) {
1223
- case 0:
1224
- r = new DPolygon();
1225
- _d.label = 1;
1226
- case 1:
1227
- _d.trys.push([1, 6, 7, 8]);
1228
- _a = __values(polygon.pPoints), _b = _a.next();
1229
- _d.label = 2;
1230
- case 2:
1231
- if (!!_b.done) return [3, 5];
1232
- p = _b.value;
1233
- r.push(p);
1234
- if (!p.properties.pieceBorder) return [3, 4];
1235
- return [4, r.clone()];
1236
- case 3:
1237
- _d.sent();
1238
- _d.label = 4;
1239
- case 4:
1240
- _b = _a.next();
1241
- return [3, 2];
1242
- case 5: return [3, 8];
1243
- case 6:
1244
- e_15_1 = _d.sent();
1245
- e_15 = { error: e_15_1 };
1246
- return [3, 8];
1247
- case 7:
1248
- try {
1249
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1250
- }
1251
- finally { if (e_15) throw e_15.error; }
1252
- return [7];
1253
- case 8: return [2, r.clone()];
1254
- }
1255
- });
1256
- };
1257
- },
1258
- enumerable: false,
1259
- configurable: true
1260
- });
1261
- DPolygon.prototype.simpleUnion = function (p) {
1262
- try {
1263
- var res = this.simpleLogicFunction(p, true, true);
1264
- if (res === null) {
1265
- return null;
1266
- }
1267
- if (res instanceof DPolygon) {
1268
- return res;
1269
- }
1270
- return null;
1271
- }
1272
- catch (ex) {
1273
- return null;
1274
- }
1275
- };
1276
- DPolygon.prototype.simpleIntersection = function (p) {
1277
- return this.simpleLogicFunction(p, false, false);
1278
- };
1279
- DPolygon.prototype.simpleDifference = function (p) {
1280
- return this.simpleLogicFunction(p, true, false);
1281
- };
1282
- DPolygon.prototype.smartUnion = function (p) {
1283
- var e_16, _a;
1284
- var res = this.clone().simpleUnion(p);
1285
- if (res) {
1286
- var allHoles = __spreadArray(__spreadArray(__spreadArray([], __read(this.holes), false), __read(p.holes), false), __read(res.holes), false).map(function (h) { return h.clone(); });
1287
- var _loop_4 = function (a) {
1288
- var e_17, _b;
1289
- var _loop_5 = function (b) {
1290
- if (a.equal(b)) {
1291
- return "continue";
1292
- }
1293
- var r = a.simpleUnion(b);
1294
- if (r) {
1295
- allHoles = allHoles.filter(function (v) { return !v.equal(a) && !v.equal(b); });
1296
- allHoles.push(r);
1297
- }
1298
- };
1299
- try {
1300
- for (var allHoles_2 = (e_17 = void 0, __values(allHoles)), allHoles_2_1 = allHoles_2.next(); !allHoles_2_1.done; allHoles_2_1 = allHoles_2.next()) {
1301
- var b = allHoles_2_1.value;
1302
- _loop_5(b);
1303
- }
1304
- }
1305
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
1306
- finally {
1307
- try {
1308
- if (allHoles_2_1 && !allHoles_2_1.done && (_b = allHoles_2.return)) _b.call(allHoles_2);
1309
- }
1310
- finally { if (e_17) throw e_17.error; }
1311
- }
1312
- };
1313
- try {
1314
- for (var allHoles_1 = __values(allHoles), allHoles_1_1 = allHoles_1.next(); !allHoles_1_1.done; allHoles_1_1 = allHoles_1.next()) {
1315
- var a = allHoles_1_1.value;
1316
- _loop_4(a);
1317
- }
1318
- }
1319
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
1320
- finally {
1321
- try {
1322
- if (allHoles_1_1 && !allHoles_1_1.done && (_a = allHoles_1.return)) _a.call(allHoles_1);
1323
- }
1324
- finally { if (e_16) throw e_16.error; }
1325
- }
1326
- res.holes = allHoles;
1327
- }
1328
- return res;
1329
- };
1330
- DPolygon.prototype.toTriangles = function () {
1331
- var q = this.clone().removeDuplicates()
1332
- .open();
1333
- if (q.length < 3) {
1334
- return [];
1335
- }
1336
- if (q.length === 3) {
1337
- return [q];
1338
- }
1339
- var innerAndNotIntersect = function (poly, p1, p2) {
1340
- var l = p1.findLine(p2);
1341
- var center = l.center;
1342
- var intersections = poly.holes.reduce(function (a, hole) { return a && Boolean(hole.clone().close()
1343
- .intersection(l, true).length); }, Boolean(poly.clone().close()
1344
- .intersection(l, true).length));
1345
- var contain = poly.holes.reduce(function (a, hole) { return a && !hole
1346
- .contain(center); }, poly.contain(center));
1347
- return !intersections && contain;
1348
- };
1349
- var getTriangle = function (poly) {
1350
- for (var i = 0; i < poly.length; i++) {
1351
- var p0 = poly.at(0);
1352
- var p1 = poly.at(1);
1353
- var p2 = poly.at(2);
1354
- if (innerAndNotIntersect(poly, p0, p2)) {
1355
- poly.removePart(0, 1);
1356
- return new DPolygon([
1357
- p0.clone(),
1358
- p1.clone(),
1359
- p2.clone()
1360
- ]);
1361
- }
1362
- poly.push(poly.shift());
1363
- }
1364
- return undefined;
1365
- };
1366
- var p = this.clone().removeDuplicates().clockWise.open();
1367
- while (p.holes.length) {
1368
- var h = p.holes.shift()
1369
- .clone()
1370
- .clockWise
1371
- .reverse()
1372
- .close();
1373
- for (var i = 0; i < p.length; i++) {
1374
- if (innerAndNotIntersect(p, p.first, h.first)) {
1375
- p.insertAfter.apply(p, __spreadArray(__spreadArray([0], __read(h.points), false), [p.first], false));
1376
- break;
1377
- }
1378
- p.push(p.shift());
1379
- }
1380
- }
1381
- var res = [];
1382
- while (p.length > 3) {
1383
- var triangle = getTriangle(p);
1384
- if (triangle) {
1385
- res.push(triangle);
1386
- }
1387
- }
1388
- res.push(p);
1389
- return res;
1390
- };
1391
- DPolygon.prototype.getTrianglesPointIndexes = function () {
1392
- var innerAndNotIntersect = function (poly, p1, p2) {
1393
- var l = p1.findLine(p2);
1394
- var center = l.center;
1395
- var intersections = poly.holes.reduce(function (a, hole) { return a && Boolean(hole.clone().close()
1396
- .intersection(l, true).length); }, Boolean(poly.clone().close()
1397
- .intersection(l, true).length));
1398
- var contain = poly.holes.reduce(function (a, hole) { return a && !hole
1399
- .contain(center); }, poly.contain(center));
1400
- return !intersections && contain;
1401
- };
1402
- var getTriangle = function (poly) {
1403
- for (var i = 0; i < poly.length; i++) {
1404
- var p0 = poly.at(0);
1405
- var p1 = poly.at(1);
1406
- var p2 = poly.at(2);
1407
- if (innerAndNotIntersect(poly, p0, p2)) {
1408
- poly.removePart(0, 1);
1409
- return [
1410
- p0.properties.index,
1411
- p1.properties.index,
1412
- p2.properties.index
1413
- ];
1414
- }
1415
- poly.push(poly.shift());
1416
- }
1417
- return undefined;
1418
- };
1419
- var p = this.clone();
1420
- var index = 0;
1421
- p.points.forEach(function (f) {
1422
- f.properties.index = index++;
1423
- });
1424
- p.holes.forEach(function (h) {
1425
- h.pPoints.forEach(function (f) {
1426
- f.properties.index = index++;
1427
- });
1428
- });
1429
- p = p.clockWise.open();
1430
- while (p.holes.length) {
1431
- var h = p.holes.shift()
1432
- .clone()
1433
- .clockWise
1434
- .reverse()
1435
- .close();
1436
- for (var i = 0; i < p.length; i++) {
1437
- if (innerAndNotIntersect(p, p.first, h.first)) {
1438
- p.insertAfter.apply(p, __spreadArray(__spreadArray([0], __read(h.points), false), [p.first], false));
1439
- break;
1440
- }
1441
- p.push(p.shift());
1442
- }
1443
- }
1444
- var res = [];
1445
- while (p.length > 3) {
1446
- var triangle = getTriangle(p);
1447
- if (triangle) {
1448
- res.push.apply(res, __spreadArray([], __read(triangle), false));
1449
- }
1450
- }
1451
- res.push.apply(res, __spreadArray([], __read(p.points.map(function (f) { return f.properties.index; })), false));
1452
- return res;
1453
- };
1454
- Object.defineProperty(DPolygon.prototype, "closed", {
1455
- get: function () {
1456
- return this.first.equal(this.last);
1457
- },
1458
- enumerable: false,
1459
- configurable: true
1460
- });
1461
- DPolygon.prototype.buffer = function (v, quadrantSegments, type) {
1462
- if (quadrantSegments === void 0) { quadrantSegments = 64; }
1463
- if (type === void 0) { type = DPolygon.CAP_ROUND; }
1464
- var reader = new jstsIo.WKTReader();
1465
- var _a = this, noHoles = _a.noHoles, closed = _a.closed;
1466
- var points = reader
1467
- .read(noHoles.toWKT(closed ? DPolygon.WKT_POLYGON : DPolygon.WKT_LINESTRING))
1468
- .buffer(v, quadrantSegments, type)
1469
- .getCoordinates();
1470
- return new DPolygon(points.map(function (_a) {
1471
- var x = _a.x, y = _a.y;
1472
- return new DPoint(x, y);
1473
- }));
1474
- };
1475
- DPolygon.prototype.sideBuffers = function (v, quadrantSegments) {
1476
- if (quadrantSegments === void 0) { quadrantSegments = 64; }
1477
- var _a = this, first = _a.first, last = _a.last;
1478
- var buffer = this.buffer(v, quadrantSegments, DPolygon.CAP_FLAT).open();
1479
- var _b = __read(first.sortByDistance(buffer).points.map(function (r) { return r.properties.index; }), 2), start0 = _b[0], start1 = _b[1];
1480
- var _c = __read(last.sortByDistance(buffer).points.map(function (r) { return r.properties.index; }), 2), end0 = _c[0], end1 = _c[1];
1481
- var fromPoint = Math.min(Math.max(start0, start1), Math.max(end0, end1));
1482
- var toPoint = Math.max(Math.min(start0, start1), Math.min(end0, end1));
1483
- var linePart = new DPolygon(buffer.removePart(fromPoint - 1, toPoint - fromPoint + 1));
1484
- buffer.unshift(buffer.pop());
1485
- return [linePart.reverse(), buffer];
1486
- };
1487
- DPolygon.prototype.setProperties = function (v) {
1488
- this.properties = typeof v === 'object' ? v : v(this);
1489
- return this;
1490
- };
1491
- DPolygon.prototype.bezier = function (step) {
1492
- if (step === void 0) { step = 0.1; }
1493
- var res = new DPolygon();
1494
- for (var i = 0; i < 1; i += step) {
1495
- res.push(this.clone().getBezierPoint(i));
1496
- }
1497
- return res;
1498
- };
1499
- DPolygon.prototype.setGrowingHeight = function (from, to) {
1500
- var fullLength = this.fullLength;
1501
- var prevPoint = this.first;
1502
- var d = to - from;
1503
- var currentDistance = 0;
1504
- this.loop()
1505
- .setZ(function (p) {
1506
- currentDistance += prevPoint.distance(p);
1507
- prevPoint = p;
1508
- return from + currentDistance / fullLength * d;
1509
- })
1510
- .run();
1511
- return this;
1512
- };
1513
- DPolygon.prototype.loopPointsGenerator = function (withLine) {
1514
- if (withLine === void 0) { withLine = false; }
1515
- var that = this;
1516
- return function () {
1517
- var i, p1, p2;
1518
- return __generator(this, function (_a) {
1519
- switch (_a.label) {
1520
- case 0:
1521
- i = 0;
1522
- _a.label = 1;
1523
- case 1:
1524
- if (!(i < that.length - 1)) return [3, 4];
1525
- p1 = that.at(i);
1526
- p2 = that.at(i + 1);
1527
- return [4, [p1, p2, withLine ? p1.findLine(p2) : undefined, i]];
1528
- case 2:
1529
- _a.sent();
1530
- _a.label = 3;
1531
- case 3:
1532
- i++;
1533
- return [3, 1];
1534
- case 4: return [2];
1535
- }
1536
- });
1537
- };
1538
- };
1539
- DPolygon.prototype.getBezierPoint = function (v) {
1540
- var e_18, _a;
1541
- if (this.length === 1) {
1542
- return this.first;
1543
- }
1544
- try {
1545
- for (var _b = __values(this.loopPointsGenerator()()), _c = _b.next(); !_c.done; _c = _b.next()) {
1546
- var _d = __read(_c.value, 2), p1 = _d[0], p2 = _d[1];
1547
- p1.move(p2.clone().move(p1.clone().minus())
1548
- .scale(v));
1549
- }
1550
- }
1551
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
1552
- finally {
1553
- try {
1554
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1555
- }
1556
- finally { if (e_18) throw e_18.error; }
1557
- }
1558
- this.pop();
1559
- return this.getBezierPoint(v);
1560
- };
1561
- DPolygon.prototype.simpleIncludeX = function (p) {
1562
- var x = p.x;
1563
- return this.minX <= x && this.maxX >= x;
1564
- };
1565
- DPolygon.prototype.simpleIncludeY = function (p) {
1566
- var y = p.y;
1567
- return this.minY <= y && this.maxY >= y;
1568
- };
1569
- DPolygon.prototype.douglasPeucker = function (points, e) {
1570
- var dMax = 0;
1571
- var index = 0;
1572
- var end = points.length - 1;
1573
- var line = points[0].findLine(points[end]);
1574
- for (var i = 1; i < end; i++) {
1575
- var d = line.perpendicularDistance(points[i]);
1576
- if (d > dMax) {
1577
- index = i;
1578
- dMax = d;
1579
- }
1580
- }
1581
- if (dMax >= e) {
1582
- var recResult1 = this.douglasPeucker(points.slice(0, index + 1), e);
1583
- var recResult2 = this.douglasPeucker(points.slice(index), e);
1584
- recResult1.pop();
1585
- return __spreadArray(__spreadArray([], __read(recResult1), false), __read(recResult2), false);
1586
- }
1587
- return [points[0], points[end]];
1588
- };
1589
- DPolygon.prototype.goByPath = function (ctx, steps) {
1590
- if (steps === void 0) { steps = this.length - 1; }
1591
- var start = this.first;
1592
- ctx.moveTo(start.x, start.y);
1593
- for (var i = 1; i <= (steps % this.length); i++) {
1594
- var _a = this.at(i), x = _a.x, y = _a.y;
1595
- ctx.lineTo(x, y);
1596
- }
1597
- };
1598
- DPolygon.prototype.getJSTSGeometry = function (p, unionThis, unionThat) {
1599
- var unionOrIntersection = unionThat === unionThis;
1600
- var reader = new jstsIo.WKTReader();
1601
- var a = reader.read(this.noHoles.toWKT());
1602
- var b = reader.read(p.noHoles.toWKT());
1603
- if (!unionOrIntersection) {
1604
- return a.difference(b);
1605
- }
1606
- else if (unionThis) {
1607
- return a.union(b);
1608
- }
1609
- else if (!unionThis) {
1610
- return a.intersection(b);
1611
- }
1612
- return undefined;
1613
- };
1614
- DPolygon.prototype.simpleLogicFunction = function (p, unionThis, unionThat) {
1615
- var e_19, _a, e_20, _b, e_21, _c, e_22, _d;
1616
- var c = this.getJSTSGeometry(p, unionThis, unionThat);
1617
- if (c) {
1618
- var coordinates_1 = c.getCoordinates();
1619
- if (coordinates_1.length) {
1620
- var result = coordinates_1.reduce(function (ak, _a, index) {
1621
- var x = _a.x, y = _a.y;
1622
- var lastIndex = ak.length - 1;
1623
- var t = new DPoint(x, y);
1624
- var first = ak[lastIndex].first;
1625
- if (t.equal(first)) {
1626
- if (coordinates_1[index + 1]) {
1627
- var nextPoint = new DPoint(coordinates_1[index + 1].x, coordinates_1[index + 1].y);
1628
- if (ak[lastIndex].length > 1) {
1629
- ak.push(new DPolygon([nextPoint]));
1630
- }
1631
- }
1632
- }
1633
- else {
1634
- ak[lastIndex].push(t);
1635
- }
1636
- return ak;
1637
- }, [new DPolygon([new DPoint(coordinates_1[0].x, coordinates_1[0].y)])]);
1638
- if (unionThat && unionThis && result.length > 1) {
1639
- try {
1640
- for (var result_1 = __values(result), result_1_1 = result_1.next(); !result_1_1.done; result_1_1 = result_1.next()) {
1641
- var q = result_1_1.value;
1642
- var _loop_6 = function (r) {
1643
- var _e;
1644
- if (q.has(r.first) && !q.equal(r)) {
1645
- var index = q.findIndex(r.first);
1646
- (_e = q.points).splice.apply(_e, __spreadArray([index, 0], __read(r.points), false));
1647
- result = result.filter(function (h) { return !h.equal(r); });
1648
- return "continue";
1649
- }
1650
- if (result.length < 2) {
1651
- return "break";
1652
- }
1653
- };
1654
- try {
1655
- for (var result_2 = (e_20 = void 0, __values(result)), result_2_1 = result_2.next(); !result_2_1.done; result_2_1 = result_2.next()) {
1656
- var r = result_2_1.value;
1657
- var state_1 = _loop_6(r);
1658
- if (state_1 === "break")
1659
- break;
1660
- }
1661
- }
1662
- catch (e_20_1) { e_20 = { error: e_20_1 }; }
1663
- finally {
1664
- try {
1665
- if (result_2_1 && !result_2_1.done && (_b = result_2.return)) _b.call(result_2);
1666
- }
1667
- finally { if (e_20) throw e_20.error; }
1668
- }
1669
- if (result.length < 2) {
1670
- break;
1671
- }
1672
- }
1673
- }
1674
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
1675
- finally {
1676
- try {
1677
- if (result_1_1 && !result_1_1.done && (_a = result_1.return)) _a.call(result_1);
1678
- }
1679
- finally { if (e_19) throw e_19.error; }
1680
- }
1681
- }
1682
- result = result.filter(function (h) { return h.length > 2; }).map(function (h) { return h.close(); });
1683
- try {
1684
- for (var result_3 = __values(result), result_3_1 = result_3.next(); !result_3_1.done; result_3_1 = result_3.next()) {
1685
- var q = result_3_1.value;
1686
- var _loop_7 = function (r) {
1687
- if (result.length < 2) {
1688
- return "break";
1689
- }
1690
- if (!q.equal(r)) {
1691
- if (q.contain(r.first, true)) {
1692
- q.holes.push(r);
1693
- result = result.filter(function (h) { return !h.equal(r); });
1694
- }
1695
- }
1696
- };
1697
- try {
1698
- for (var result_4 = (e_22 = void 0, __values(result)), result_4_1 = result_4.next(); !result_4_1.done; result_4_1 = result_4.next()) {
1699
- var r = result_4_1.value;
1700
- var state_2 = _loop_7(r);
1701
- if (state_2 === "break")
1702
- break;
1703
- }
1704
- }
1705
- catch (e_22_1) { e_22 = { error: e_22_1 }; }
1706
- finally {
1707
- try {
1708
- if (result_4_1 && !result_4_1.done && (_d = result_4.return)) _d.call(result_4);
1709
- }
1710
- finally { if (e_22) throw e_22.error; }
1711
- }
1712
- if (result.length < 2) {
1713
- break;
1714
- }
1715
- }
1716
- }
1717
- catch (e_21_1) { e_21 = { error: e_21_1 }; }
1718
- finally {
1719
- try {
1720
- if (result_3_1 && !result_3_1.done && (_c = result_3.return)) _c.call(result_3);
1721
- }
1722
- finally { if (e_21) throw e_21.error; }
1723
- }
1724
- if (result.length === 0) {
1725
- return null;
1726
- }
1727
- if (result.length === 1) {
1728
- return result[0].close();
1729
- }
1730
- return result.map(function (g) { return g.close(); });
1731
- }
1732
- }
1733
- return null;
1734
- };
1735
- DPolygon.CAP_ROUND = CAP_ROUND;
1736
- DPolygon.CAP_FLAT = CAP_FLAT;
1737
- DPolygon.CAP_SQUARE = CAP_SQUARE;
1738
- DPolygon.WKT_LINESTRING = 'LINESTRING';
1739
- DPolygon.WKT_POLYGON = 'POLYGON';
1740
- return DPolygon;
1741
- }());
1742
- export { DPolygon };