poly-extrude 0.21.0 → 0.22.0
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/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/poly-extrude.js +2201 -157
- package/dist/poly-extrude.js.map +1 -1
- package/dist/poly-extrude.min.js +2 -2
- package/dist/poly-extrude.mjs +2201 -158
- package/dist/poly-extrude.mjs.map +1 -1
- package/dist/polyline.js +14 -21
- package/dist/polyline.js.map +1 -1
- package/dist/polylineoffset.d.ts +5 -0
- package/dist/polylineoffset.js +51 -1
- package/dist/polylineoffset.js.map +1 -1
- package/dist/util.d.ts +4 -0
- package/dist/util.js +7 -0
- package/dist/util.js.map +1 -1
- package/package.json +53 -52
- package/src/index.ts +3 -2
- package/src/polyline.ts +14 -22
- package/src/polylineoffset.ts +60 -1
- package/src/util.ts +12 -0
package/src/polyline.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { PolylineType, ResultType } from './type';
|
2
|
-
import { calLineDistance, degToRad, generateNormal, generateSideWallUV, merge, radToDeg } from './util';
|
2
|
+
import { calLineDistance, degToRad, generateNormal, generateSideWallUV, merge, radToDeg, pointEqual, pointDistance } from './util';
|
3
3
|
|
4
4
|
function checkOptions(options) {
|
5
5
|
options.lineWidth = Math.max(0, options.lineWidth);
|
@@ -351,7 +351,7 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
351
351
|
let p1 = line[i],
|
352
352
|
p2 = line[i + 1];
|
353
353
|
const current = p1;
|
354
|
-
if (pre &&
|
354
|
+
if (pre && pointEqual(pre, current)) {
|
355
355
|
repeatVertex();
|
356
356
|
i++;
|
357
357
|
continue;
|
@@ -361,30 +361,30 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
361
361
|
if (i === len - 1) {
|
362
362
|
p1 = line[len - 2];
|
363
363
|
p2 = line[len - 1];
|
364
|
-
if (
|
364
|
+
if (pointEqual(p1, p2)) {
|
365
365
|
for (let j = line.indexOf(p1); j >= 0; j--) {
|
366
366
|
const p = line[j];
|
367
|
-
if (!
|
367
|
+
if (!pointEqual(p, current)) {
|
368
368
|
p1 = p;
|
369
369
|
break;
|
370
370
|
}
|
371
371
|
}
|
372
372
|
}
|
373
373
|
} else {
|
374
|
-
if (
|
374
|
+
if (pointEqual(p1, p2)) {
|
375
375
|
for (let j = line.indexOf(p2); j < len; j++) {
|
376
376
|
const p = line[j];
|
377
|
-
if (!
|
377
|
+
if (!pointEqual(p, current)) {
|
378
378
|
p2 = p;
|
379
379
|
break;
|
380
380
|
}
|
381
381
|
}
|
382
382
|
}
|
383
|
-
if (
|
383
|
+
if (pointEqual(p1, p2)) {
|
384
384
|
lastRepeat = true;
|
385
385
|
for (let j = line.indexOf(p1); j >= 0; j--) {
|
386
386
|
const p = line[j];
|
387
|
-
if (!
|
387
|
+
if (!pointEqual(p, current)) {
|
388
388
|
p1 = p;
|
389
389
|
break;
|
390
390
|
}
|
@@ -392,7 +392,7 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
392
392
|
}
|
393
393
|
|
394
394
|
}
|
395
|
-
if (
|
395
|
+
if (pointEqual(p1, p2)) {
|
396
396
|
console.error('not find next vertex:index:', i, line);
|
397
397
|
repeatVertex();
|
398
398
|
i++;
|
@@ -410,16 +410,16 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
410
410
|
} else {
|
411
411
|
// 至少3个顶点才会触发
|
412
412
|
p0 = line[i - 1];
|
413
|
-
if (
|
413
|
+
if (pointEqual(p0, p2) || pointEqual(p0, p1)) {
|
414
414
|
for (let j = line.indexOf(p2); j >= 0; j--) {
|
415
415
|
const p = line[j];
|
416
|
-
if (!
|
416
|
+
if (!pointEqual(p, p2) && (!pointEqual(p, p1))) {
|
417
417
|
p0 = p;
|
418
418
|
break;
|
419
419
|
}
|
420
420
|
}
|
421
421
|
}
|
422
|
-
if (
|
422
|
+
if (pointEqual(p0, p2) || pointEqual(p0, p1) || pointEqual(p1, p2)) {
|
423
423
|
console.error('not find pre vertex:index:', i, line);
|
424
424
|
repeatVertex();
|
425
425
|
i++;
|
@@ -480,13 +480,13 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
480
480
|
let needCut = false;
|
481
481
|
if (cutCorner) {
|
482
482
|
const bufferRadius = radius * 2;
|
483
|
-
if (
|
483
|
+
if (pointDistance(current, op1) > bufferRadius || pointDistance(current, op2) > bufferRadius) {
|
484
484
|
needCut = true;
|
485
485
|
}
|
486
486
|
}
|
487
487
|
if (needCut && p0 && preleftline && prerightline) {
|
488
488
|
let cutPoint = op1;
|
489
|
-
if (
|
489
|
+
if (pointDistance(op1, p0) < pointDistance(op2, p0)) {
|
490
490
|
cutPoint = op2;
|
491
491
|
}
|
492
492
|
const dy = cutPoint[1] - current[1], dx = cutPoint[0] - current[0];
|
@@ -542,10 +542,6 @@ export function expandLine(line: Array<Array<number>>, options?: ExpandLineOptio
|
|
542
542
|
return { offsetPoints: points, leftPoints, rightPoints, line };
|
543
543
|
}
|
544
544
|
|
545
|
-
function equal(p1: Point, p2: Point) {
|
546
|
-
return p1[0] === p2[0] && p1[1] === p2[1];
|
547
|
-
}
|
548
|
-
|
549
545
|
|
550
546
|
// eslint-disable-next-line no-unused-vars
|
551
547
|
function calOffsetPoint(rad: number, radius: number, p: Point) {
|
@@ -567,10 +563,6 @@ const getAngle = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
|
|
567
563
|
// return (angle + 360) % 360;
|
568
564
|
};
|
569
565
|
|
570
|
-
function distance(p1: Point, p2: Point) {
|
571
|
-
const dx = p2[0] - p1[0], dy = p2[1] - p1[1];
|
572
|
-
return Math.sqrt(dx * dx + dy * dy);
|
573
|
-
}
|
574
566
|
|
575
567
|
export function leftOnLine(p: Point, p1: Point, p2: Point) {
|
576
568
|
const [x1, y1] = p1;
|
package/src/polylineoffset.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { PolylineType } from "./type";
|
2
|
-
import { mergeArray } from "./util";
|
2
|
+
import { mergeArray, pointDistance, pointEqual } from "./util";
|
3
|
+
import { Bezier } from 'bezier-js';
|
3
4
|
//https://github.com/bbecquet/Leaflet.PolylineOffset/blob/master/leaflet.polylineoffset.js
|
4
5
|
|
5
6
|
/**
|
@@ -230,4 +231,62 @@ export function polylineOffset(line: PolylineType, options: polylineOffsetOption
|
|
230
231
|
result.push([x, y, z]);
|
231
232
|
}
|
232
233
|
return result;
|
234
|
+
}
|
235
|
+
|
236
|
+
type polylineRoundOptions = {
|
237
|
+
roundSize: number,
|
238
|
+
steps?: number;
|
239
|
+
}
|
240
|
+
|
241
|
+
export function polylineRound(line: PolylineType, options: polylineRoundOptions): PolylineType {
|
242
|
+
options = Object.assign({ roundSize: 0, steps: 10 }, options);
|
243
|
+
if (options.roundSize === 0) {
|
244
|
+
return line;
|
245
|
+
}
|
246
|
+
if (!line || line.length < 3) {
|
247
|
+
return line;
|
248
|
+
}
|
249
|
+
const len = line.length;
|
250
|
+
const { roundSize, steps } = options;
|
251
|
+
|
252
|
+
const pts = [line[0]];
|
253
|
+
let pre = line[0];
|
254
|
+
|
255
|
+
for (let i = 1; i < len; i++) {
|
256
|
+
const p1 = line[i - 1], p2 = line[i], p3 = line[i + 1];
|
257
|
+
if (pointEqual(pre, p2)) {
|
258
|
+
continue;
|
259
|
+
}
|
260
|
+
if (!p3) {
|
261
|
+
continue;
|
262
|
+
}
|
263
|
+
const d1 = pointDistance(p2, p1), d2 = pointDistance(p2, p3);
|
264
|
+
if (d1 < roundSize || d2 < roundSize) {
|
265
|
+
pre = p2;
|
266
|
+
pts.push(p2);
|
267
|
+
continue;
|
268
|
+
}
|
269
|
+
const dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1];
|
270
|
+
const dx2 = p3[0] - p2[0], dy2 = p3[1] - p2[1];
|
271
|
+
|
272
|
+
const percent1 = (d1 - roundSize) / d1;
|
273
|
+
const percent2 = roundSize / d2;
|
274
|
+
const c1 = {
|
275
|
+
x: p1[0] + percent1 * dx1,
|
276
|
+
y: p1[1] + percent1 * dy1
|
277
|
+
};
|
278
|
+
const c2 = {
|
279
|
+
x: p2[0] + percent2 * dx2,
|
280
|
+
y: p2[1] + percent2 * dy2
|
281
|
+
};
|
282
|
+
const be = new Bezier([c1, { x: p2[0], y: p2[1] }, c2]);
|
283
|
+
const path = be.getLUT(steps);
|
284
|
+
for (let j = 0, len1 = path.length; j < len1; j++) {
|
285
|
+
const p = path[j];
|
286
|
+
pts.push([p.x, p.y]);
|
287
|
+
}
|
288
|
+
pre = p2;
|
289
|
+
}
|
290
|
+
pts.push(line[len- 1]);
|
291
|
+
return pts;
|
233
292
|
}
|
package/src/util.ts
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
import { Vector3 } from './math/Vector3';
|
2
2
|
import { PolygonType, PolylineType, ResultType } from './type';
|
3
3
|
|
4
|
+
type Point = Array<number>;
|
5
|
+
|
4
6
|
export function mergeArray(array1, array2) {
|
5
7
|
let index = array1.length - 1;
|
6
8
|
for (let i = 0, len = array2.length; i < len; i++) {
|
@@ -310,3 +312,13 @@ export function calLineDistance(line) {
|
|
310
312
|
}
|
311
313
|
return distance;
|
312
314
|
}
|
315
|
+
|
316
|
+
|
317
|
+
export function pointEqual(p1: Point, p2: Point) {
|
318
|
+
return p1[0] === p2[0] && p1[1] === p2[1];
|
319
|
+
}
|
320
|
+
|
321
|
+
export function pointDistance(p1: Point, p2: Point) {
|
322
|
+
const dx = p2[0] - p1[0], dy = p2[1] - p1[1];
|
323
|
+
return Math.sqrt(dx * dx + dy * dy);
|
324
|
+
}
|