dgeoutils 2.2.11 → 2.2.15

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.
package/README.md CHANGED
@@ -13,6 +13,8 @@
13
13
  [![Coverage branches](https://edejin.github.io/DGeoUtils/media/badges/badge-branches.svg)](https://edejin.github.io/DGeoUtils/media/lcov-report/index.html)
14
14
  [![Coverage statements](https://edejin.github.io/DGeoUtils/media/badges/badge-statements.svg)](https://edejin.github.io/DGeoUtils/media/lcov-report/index.html)
15
15
 
16
+ [Test time report](https://edejin.github.io/DGeoUtils/media/time-report.html)
17
+
16
18
  [Docs](https://edejin.github.io/DGeoUtils/index.html)
17
19
 
18
20
  [ESLint Report](https://edejin.github.io/DGeoUtils/media/eslit.html)
package/dist/DCircle.d.ts CHANGED
@@ -12,7 +12,7 @@ export declare class DCircle {
12
12
  clone(): DCircle;
13
13
  findPoints(c: DCircle): DPoint[] | number;
14
14
  equal({ center, r }: DCircle): boolean;
15
- findPolygonInside(pointCount?: number): DPolygon;
16
- findPolygonInsideOnSphere(pointCount?: number): DPolygon;
15
+ findPolygonInside(pointCount?: number, startAngle?: number, stopAngle?: number): DPolygon;
16
+ findPolygonInsideOnSphere(pointCount?: number, startAngle?: number, stopAngle?: number): DPolygon;
17
17
  private sphereOffset;
18
18
  }
package/dist/DCircle.js CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DCircle = void 0;
4
4
  const DPoint_1 = require("./DPoint");
5
5
  const DPolygon_1 = require("./DPolygon");
6
- const DNumbers_1 = require("./DNumbers");
7
6
  const utils_1 = require("./utils");
8
7
  class DCircle {
9
8
  constructor(center = DPoint_1.DPoint.zero(), r = 0) {
@@ -51,36 +50,42 @@ class DCircle {
51
50
  equal({ center, r }) {
52
51
  return this.center.equal(center) && this.r === r;
53
52
  }
54
- findPolygonInside(pointCount = 64) {
55
- const preAngle = 2 * Math.PI / pointCount;
53
+ findPolygonInside(pointCount = 64, startAngle = 0, stopAngle = 2 * Math.PI) {
54
+ const step = 2 * Math.PI / pointCount;
56
55
  const points = [];
57
- for (let i = 0; i < pointCount; i++) {
58
- const angle = preAngle * i;
59
- const x = this.r * Math.cos(angle) + this.center.x;
60
- const y = this.r * Math.sin(angle) + this.center.y;
61
- points.push(new DPoint_1.DPoint(x, y));
56
+ let angle = startAngle;
57
+ while (angle < stopAngle - step) {
58
+ points.push(new DPoint_1.DPoint(this.r).scale(Math.cos(angle), Math.sin(angle))
59
+ .move(this.center));
60
+ angle += step;
62
61
  }
63
- return new DPolygon_1.DPolygon([...points, points[0]]);
62
+ const x = this.r * Math.cos(stopAngle) + this.center.x;
63
+ const y = this.r * Math.sin(stopAngle) + this.center.y;
64
+ points.push(new DPoint_1.DPoint(x, y));
65
+ return new DPolygon_1.DPolygon(points);
64
66
  }
65
- findPolygonInsideOnSphere(pointCount = 64) {
67
+ findPolygonInsideOnSphere(pointCount = 64, startAngle = 0, stopAngle = 2 * Math.PI) {
66
68
  (0, utils_1.checkFunction)('findPolygonInsideOnSphere')
67
69
  .checkArgument('center')
68
70
  .shouldBeDegree(this.center);
69
- const res = new DPolygon_1.DPolygon();
70
- for (let i = 0; i < pointCount; i++) {
71
- res.push(this.sphereOffset(2 * Math.PI * i / pointCount));
71
+ const step = 2 * Math.PI / pointCount;
72
+ const points = [];
73
+ let angle = startAngle;
74
+ while (angle < stopAngle - step) {
75
+ points.push(this.sphereOffset(angle));
76
+ angle += step;
72
77
  }
73
- return res.close();
78
+ points.push(this.sphereOffset(stopAngle));
79
+ return new DPolygon_1.DPolygon(points);
74
80
  }
75
81
  sphereOffset(bearing, earthRadius = DPoint_1.EARTH_RADIUS_IN_METERS) {
76
- const lat1 = DNumbers_1.DNumbers.deg2Rad(this.center.y);
77
- const lon1 = DNumbers_1.DNumbers.deg2Rad(this.center.x);
82
+ const { x: lon1, y: lat1 } = this.center.clone().degreeToRadians();
78
83
  const dByR = this.r / earthRadius;
79
84
  const lat = Math.asin(Math.sin(lat1) * Math.cos(dByR) +
80
85
  Math.cos(lat1) * Math.sin(dByR) * Math.cos(bearing));
81
86
  const lon = lon1 +
82
87
  Math.atan2(Math.sin(bearing) * Math.sin(dByR) * Math.cos(lat1), Math.cos(dByR) - Math.sin(lat1) * Math.sin(lat));
83
- return new DPoint_1.DPoint(DNumbers_1.DNumbers.rad2Deg(lon), DNumbers_1.DNumbers.rad2Deg(lat));
88
+ return new DPoint_1.DPoint(lon, lat).radiansToDegrees();
84
89
  }
85
90
  }
86
91
  exports.DCircle = DCircle;
package/dist/DPoint.d.ts CHANGED
@@ -37,6 +37,7 @@ export declare class DPoint {
37
37
  height(z: number): DPoint;
38
38
  toWKT(): string;
39
39
  distance(p: DPoint): number;
40
+ distance3d(p: DPoint): number;
40
41
  setX(x: number): DPoint;
41
42
  setX(f: SetterFunction): DPoint;
42
43
  setZ(z: number): DPoint;
@@ -70,9 +71,11 @@ export declare class DPoint {
70
71
  scale(v: number): DPoint;
71
72
  scale(p: DPoint): DPoint;
72
73
  scale(x: number, y: number): DPoint;
74
+ scale(x: number, y: number, z: number): DPoint;
73
75
  divide(v: number): DPoint;
74
76
  divide(p: DPoint): DPoint;
75
77
  divide(x: number, y: number): DPoint;
78
+ divide(x: number, y: number, z: number): DPoint;
76
79
  equal(p: DPoint): boolean;
77
80
  like(p: DPoint, d?: number): boolean;
78
81
  flipVertically(size: DPoint): DPoint;
@@ -99,4 +102,5 @@ export declare class DPoint {
99
102
  setIfLessThan(p: DPoint): DPoint;
100
103
  minus(): DPoint;
101
104
  orthodromicPath(point: DPoint, pointsCount?: number): DPolygon;
105
+ findCloserPoint(p: DPolygon): DPoint;
102
106
  }
package/dist/DPoint.js CHANGED
@@ -128,6 +128,21 @@ class DPoint {
128
128
  const dy = p.y - this.y;
129
129
  return Math.sqrt(dx * dx + dy * dy);
130
130
  }
131
+ distance3d(p) {
132
+ (0, utils_1.checkFunction)('distance3d')
133
+ .checkArgument('this')
134
+ .shouldBeMeters(this)
135
+ .checkArgument('p')
136
+ .shouldBeMeters(p)
137
+ .checkArgument('this.z')
138
+ .shouldExist(this.z)
139
+ .checkArgument('p.z')
140
+ .shouldExist(p.z);
141
+ const dx = p.x - this.x;
142
+ const dy = p.y - this.y;
143
+ const dz = p.z - this.z;
144
+ return Math.sqrt(dx * dx + dy * dy + dz * dz);
145
+ }
131
146
  setX(x) {
132
147
  this.x = typeof x === 'number' ? x : x(this);
133
148
  return this;
@@ -182,27 +197,27 @@ class DPoint {
182
197
  this.y = -x * Math.sin(a) + y * Math.cos(a);
183
198
  return this;
184
199
  }
185
- move(x, y = x, z = x) {
200
+ move(x, y = x, z) {
186
201
  let xV = 0;
187
202
  let yV = 0;
188
203
  let zV = undefined;
189
204
  if (x instanceof DPoint) {
190
205
  xV = this.x + x.x;
191
206
  yV = this.y + x.y;
192
- if (this.z && x.z) {
207
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(x.z)) {
193
208
  zV = this.z + x.z;
194
209
  }
195
210
  }
196
211
  else {
197
212
  xV = this.x + x;
198
213
  yV = this.y + y;
199
- if (this.z && z) {
214
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(z)) {
200
215
  zV = this.z + z;
201
216
  }
202
217
  }
203
218
  this.x = xV;
204
219
  this.y = yV;
205
- if (zV) {
220
+ if ((0, utils_1.isDefAndNotNull)(zV)) {
206
221
  this.z = zV;
207
222
  }
208
223
  return this;
@@ -278,34 +293,54 @@ class DPoint {
278
293
  this.y = Math.abs(this.y);
279
294
  return this;
280
295
  }
281
- scale(x, y = x) {
296
+ scale(x, y = x, z) {
282
297
  let xV = 0;
283
298
  let yV = 0;
299
+ let zV = undefined;
284
300
  if (x instanceof DPoint) {
285
301
  xV = this.x * x.x;
286
302
  yV = this.y * x.y;
303
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(x.z)) {
304
+ zV = this.z * x.z;
305
+ }
287
306
  }
288
307
  else {
289
308
  xV = this.x * x;
290
309
  yV = this.y * y;
310
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(z)) {
311
+ zV = this.z * z;
312
+ }
291
313
  }
292
314
  this.x = xV;
293
315
  this.y = yV;
316
+ if ((0, utils_1.isDefAndNotNull)(zV)) {
317
+ this.z = zV;
318
+ }
294
319
  return this;
295
320
  }
296
- divide(x, y = x) {
321
+ divide(x, y = x, z) {
297
322
  let xV = 0;
298
323
  let yV = 0;
324
+ let zV = undefined;
299
325
  if (x instanceof DPoint) {
300
326
  xV = this.x / x.x;
301
327
  yV = this.y / x.y;
328
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(x.z)) {
329
+ zV = this.z / x.z;
330
+ }
302
331
  }
303
332
  else {
304
333
  xV = this.x / x;
305
334
  yV = this.y / y;
335
+ if ((0, utils_1.isDefAndNotNull)(this.z) && (0, utils_1.isDefAndNotNull)(z)) {
336
+ zV = this.z / z;
337
+ }
306
338
  }
307
339
  this.x = xV;
308
340
  this.y = yV;
341
+ if ((0, utils_1.isDefAndNotNull)(zV)) {
342
+ this.z = zV;
343
+ }
309
344
  return this;
310
345
  }
311
346
  equal(p) {
@@ -407,7 +442,7 @@ class DPoint {
407
442
  return this;
408
443
  }
409
444
  minus() {
410
- return this.clone().scale(-1);
445
+ return this.scale(-1);
411
446
  }
412
447
  orthodromicPath(point, pointsCount = 360) {
413
448
  (0, utils_1.checkFunction)('orthodromicPath')
@@ -427,5 +462,17 @@ class DPoint {
427
462
  return new DPoint(x, y).radiansToDegrees();
428
463
  }));
429
464
  }
465
+ findCloserPoint(p) {
466
+ let d = Infinity;
467
+ let res = DPoint.zero();
468
+ for (const t of p.points) {
469
+ const td = this.distance(t);
470
+ if (td < d) {
471
+ d = td;
472
+ res = t.clone();
473
+ }
474
+ }
475
+ return res;
476
+ }
430
477
  }
431
478
  exports.DPoint = DPoint;
@@ -101,6 +101,8 @@ export declare class DPolygon {
101
101
  getTrianglesPointIndexes(): number[];
102
102
  get closed(): boolean;
103
103
  buffer(v: number, quadrantSegments?: number, type?: number): DPolygon;
104
+ bezier(step?: number): DPolygon;
105
+ private getBezierPoint;
104
106
  private simpleIncludeX;
105
107
  private simpleIncludeY;
106
108
  private douglasPeucker;
package/dist/DPolygon.js CHANGED
@@ -807,6 +807,26 @@ class DPolygon {
807
807
  .getCoordinates();
808
808
  return new DPolygon(points.map(({ x, y }) => new DPoint_1.DPoint(x, y)));
809
809
  }
810
+ bezier(step = 0.1) {
811
+ const res = new DPolygon();
812
+ for (let i = 0; i < 1; i += step) {
813
+ res.push(this.clone().getBezierPoint(i));
814
+ }
815
+ return res;
816
+ }
817
+ getBezierPoint(v) {
818
+ if (this.length === 1) {
819
+ return this.first;
820
+ }
821
+ for (let i = 0; i < this.length - 1; i++) {
822
+ const p1 = this.at(i);
823
+ const p2 = this.at(i + 1);
824
+ p1.move(p2.clone().move(p1.clone().minus())
825
+ .scale(v));
826
+ }
827
+ this.pop();
828
+ return this.getBezierPoint(v);
829
+ }
810
830
  simpleIncludeX(p) {
811
831
  const { x } = p;
812
832
  return this.minX <= x && this.maxX >= x;
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './FastSearch';
7
7
  export * from './TraceMatrix';
8
8
  export * from './DPolygonLoop';
9
9
  export * from './DPlane';
10
+ export { gaussianElimination, createCanvas, createArray, createMatrix, isDefAndNotNull } from './utils';
10
11
  export declare const DGeo: {
11
12
  DEBUG: boolean;
12
13
  };
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.DGeo = void 0;
13
+ exports.DGeo = exports.isDefAndNotNull = exports.createMatrix = exports.createArray = exports.createCanvas = exports.gaussianElimination = void 0;
14
14
  __exportStar(require("./DCircle"), exports);
15
15
  __exportStar(require("./DLine"), exports);
16
16
  __exportStar(require("./DNumbers"), exports);
@@ -20,6 +20,12 @@ __exportStar(require("./FastSearch"), exports);
20
20
  __exportStar(require("./TraceMatrix"), exports);
21
21
  __exportStar(require("./DPolygonLoop"), exports);
22
22
  __exportStar(require("./DPlane"), exports);
23
+ var utils_1 = require("./utils");
24
+ Object.defineProperty(exports, "gaussianElimination", { enumerable: true, get: function () { return utils_1.gaussianElimination; } });
25
+ Object.defineProperty(exports, "createCanvas", { enumerable: true, get: function () { return utils_1.createCanvas; } });
26
+ Object.defineProperty(exports, "createArray", { enumerable: true, get: function () { return utils_1.createArray; } });
27
+ Object.defineProperty(exports, "createMatrix", { enumerable: true, get: function () { return utils_1.createMatrix; } });
28
+ Object.defineProperty(exports, "isDefAndNotNull", { enumerable: true, get: function () { return utils_1.isDefAndNotNull; } });
23
29
  exports.DGeo = {
24
30
  DEBUG: false
25
31
  };
package/dist/utils.d.ts CHANGED
@@ -1,18 +1,35 @@
1
+ /// <reference types="offscreencanvas" />
1
2
  import { DPoint } from './DPoint';
2
3
  export declare const warn: (...args: any[]) => void;
4
+ export declare const isDefAndNotNull: (a: any) => boolean;
3
5
  declare type CheckFunc = (p: DPoint) => CheckFunction;
6
+ declare type CheckFunc2 = (p: any) => CheckFunction;
4
7
  interface CheckArgument {
5
8
  shouldBeDegree: CheckFunc;
6
9
  shouldBeMeters: CheckFunc;
7
10
  shouldBeInt: CheckFunc;
8
11
  shouldBeUInt: CheckFunc;
9
12
  shouldBeRadians: CheckFunc;
13
+ shouldExist: CheckFunc2;
10
14
  }
11
15
  interface CheckFunction {
12
16
  checkArgument: (argName: string) => CheckArgument;
13
17
  }
14
18
  export declare const checkFunction: (funcName: string) => CheckFunction;
15
- export declare const createArray: (v: number) => number[];
16
- export declare const createMatrix: ({ h, w }: DPoint) => number[][];
17
- export declare const gaussianElimination: (matrix: number[][]) => number[];
19
+ export declare const createArray: (v: number, fillSymbol?: any) => number[];
20
+ export declare const createMatrix: ({ h, w }: DPoint, fillSymbol?: any) => number[][];
21
+ export declare const gaussianElimination: {
22
+ (matrix: number[][]): number[];
23
+ MIN: number;
24
+ };
25
+ declare type True = true;
26
+ export declare const createCanvas: {
27
+ (size: number): [HTMLCanvasElement, CanvasRenderingContext2D];
28
+ (size: number, offscreen: True): [OffscreenCanvas, OffscreenCanvasRenderingContext2D];
29
+ (w: number, h: number): [HTMLCanvasElement, CanvasRenderingContext2D];
30
+ (w: number, h: number, offscreen: True): [OffscreenCanvas, OffscreenCanvasRenderingContext2D];
31
+ (size: DPoint): [HTMLCanvasElement, CanvasRenderingContext2D];
32
+ (size: DPoint, offscreen: True): [OffscreenCanvas, OffscreenCanvasRenderingContext2D];
33
+ document?: Document;
34
+ };
18
35
  export {};
package/dist/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.gaussianElimination = exports.createMatrix = exports.createArray = exports.checkFunction = exports.warn = void 0;
3
+ exports.createCanvas = exports.gaussianElimination = exports.createMatrix = exports.createArray = exports.checkFunction = exports.isDefAndNotNull = exports.warn = void 0;
4
4
  const index_1 = require("./index");
5
5
  const DPoint_1 = require("./DPoint");
6
6
  const warn = (...args) => {
@@ -9,6 +9,8 @@ const warn = (...args) => {
9
9
  }
10
10
  };
11
11
  exports.warn = warn;
12
+ const isDefAndNotNull = (a) => a != undefined;
13
+ exports.isDefAndNotNull = isDefAndNotNull;
12
14
  const hook = (scope) => () => scope;
13
15
  const shouldBeInt = (scope, funcName, argName) => (p) => {
14
16
  if (!p.clone().round()
@@ -36,6 +38,12 @@ const shouldBeRadians = (scope, funcName, argName) => (p) => {
36
38
  }
37
39
  return scope;
38
40
  };
41
+ const shouldExist = (scope, funcName, argName) => (p) => {
42
+ if (!(0, exports.isDefAndNotNull)(p)) {
43
+ (0, exports.warn)(`"${funcName}" -> "${argName}" should exist!`);
44
+ }
45
+ return scope;
46
+ };
39
47
  const shouldBeMeters = (scope, funcName, argName) => (p) => {
40
48
  if (!p.likePseudoMercator) {
41
49
  (0, exports.warn)(`"${funcName}" -> "${argName}" should be meters!`);
@@ -50,7 +58,8 @@ const checkFunction = (funcName) => ({
50
58
  shouldBeMeters: hook(this),
51
59
  shouldBeInt: hook(this),
52
60
  shouldBeUInt: hook(this),
53
- shouldBeRadians: hook(this)
61
+ shouldBeRadians: hook(this),
62
+ shouldExist: hook(this)
54
63
  };
55
64
  }
56
65
  return {
@@ -58,22 +67,23 @@ const checkFunction = (funcName) => ({
58
67
  shouldBeMeters: shouldBeMeters(this, funcName, argName),
59
68
  shouldBeInt: shouldBeInt(this, funcName, argName),
60
69
  shouldBeUInt: shouldBeUInt(this, funcName, argName),
61
- shouldBeRadians: shouldBeRadians(this, funcName, argName)
70
+ shouldBeRadians: shouldBeRadians(this, funcName, argName),
71
+ shouldExist: shouldExist(this, funcName, argName)
62
72
  };
63
73
  }
64
74
  });
65
75
  exports.checkFunction = checkFunction;
66
- const createArray = (v) => new Array(v).fill(0);
76
+ const createArray = (v, fillSymbol = 0) => new Array(v).fill(fillSymbol);
67
77
  exports.createArray = createArray;
68
- const createMatrix = ({ h, w }) => (0, exports.createArray)(h).map(() => (0, exports.createArray)(w));
78
+ const createMatrix = ({ h, w }, fillSymbol = 0) => (0, exports.createArray)(h)
79
+ .map(() => (0, exports.createArray)(w, fillSymbol));
69
80
  exports.createMatrix = createMatrix;
70
- const GAUSSIAN_ELIMINATION_MIN = 1e-10;
71
81
  const gaussianElimination = (matrix) => {
72
82
  const n = matrix.length;
73
83
  const matrixClone = (0, exports.createMatrix)(new DPoint_1.DPoint(n + 1, n));
74
84
  for (let i = 0; i < n; i++) {
75
85
  for (let j = 0; j < n + 1; j++) {
76
- matrix[i][j] = matrix[i][j] === 0 ? GAUSSIAN_ELIMINATION_MIN : matrix[i][j];
86
+ matrix[i][j] = matrix[i][j] === 0 ? exports.gaussianElimination.MIN : matrix[i][j];
77
87
  matrixClone[i][j] = matrix[i][j];
78
88
  }
79
89
  }
@@ -111,3 +121,35 @@ const gaussianElimination = (matrix) => {
111
121
  return answer;
112
122
  };
113
123
  exports.gaussianElimination = gaussianElimination;
124
+ exports.gaussianElimination.MIN = 1e-10;
125
+ const createCanvas = (a, b, c) => {
126
+ var _a;
127
+ let w = 0;
128
+ let h = 0;
129
+ let offscreen = false;
130
+ if (a instanceof DPoint_1.DPoint) {
131
+ const { x, y } = a;
132
+ w = x;
133
+ h = y;
134
+ }
135
+ else {
136
+ w = a;
137
+ h = a;
138
+ }
139
+ if (typeof b === 'boolean') {
140
+ offscreen = b;
141
+ }
142
+ else if (typeof b === 'number') {
143
+ h = b;
144
+ }
145
+ if (typeof c === 'boolean') {
146
+ offscreen = c;
147
+ }
148
+ const canvas = offscreen ? new OffscreenCanvas(w, h) : ((_a = exports.createCanvas.document) !== null && _a !== void 0 ? _a : document).createElement('canvas');
149
+ if (!offscreen) {
150
+ canvas.width = w;
151
+ canvas.height = h;
152
+ }
153
+ return [canvas, canvas.getContext('2d')];
154
+ };
155
+ exports.createCanvas = createCanvas;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dgeoutils",
3
- "version": "2.2.11",
3
+ "version": "2.2.15",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "node_modules/.bin/tsc",
@@ -42,6 +42,7 @@
42
42
  "jest": "^27.0.6",
43
43
  "jest-canvas-mock": "^2.3.1",
44
44
  "jest-coverage-badges": "^1.1.2",
45
+ "jest-html-reporter": "^3.4.2",
45
46
  "jsdom": "^17.0.0",
46
47
  "ts-jest": "^27.0.4",
47
48
  "typedoc": "^0.21.9",