dgeoutils 2.2.5 → 2.2.6
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/DPolygon.d.ts +6 -1
- package/dist/DPolygon.js +66 -0
- package/package.json +1 -1
package/dist/DPolygon.d.ts
CHANGED
|
@@ -182,7 +182,8 @@ export declare class DPolygon {
|
|
|
182
182
|
* @param f
|
|
183
183
|
*/
|
|
184
184
|
filter(f: (p: DPoint) => boolean): DPolygon;
|
|
185
|
-
map(f: (r: DPoint
|
|
185
|
+
map(f: (r: DPoint) => DPoint): DPolygon;
|
|
186
|
+
map(f: (r: DPoint, index: number) => DPoint): DPolygon;
|
|
186
187
|
at(index: number): DPoint;
|
|
187
188
|
pop(): DPoint;
|
|
188
189
|
push(...args: DPoint[]): number;
|
|
@@ -298,6 +299,10 @@ export declare class DPolygon {
|
|
|
298
299
|
* 
|
|
299
300
|
*/
|
|
300
301
|
toTriangles(): DPolygon[];
|
|
302
|
+
/**
|
|
303
|
+
* Divide polygon to triangles and return points indexes
|
|
304
|
+
*/
|
|
305
|
+
getTrianglesPointIndexes(): number[];
|
|
301
306
|
get closed(): boolean;
|
|
302
307
|
/**
|
|
303
308
|
* @param v
|
package/dist/DPolygon.js
CHANGED
|
@@ -904,6 +904,72 @@ class DPolygon {
|
|
|
904
904
|
res.push(p);
|
|
905
905
|
return res;
|
|
906
906
|
}
|
|
907
|
+
/**
|
|
908
|
+
* Divide polygon to triangles and return points indexes
|
|
909
|
+
*/
|
|
910
|
+
getTrianglesPointIndexes() {
|
|
911
|
+
const innerAndNotIntersect = (poly, p1, p2) => {
|
|
912
|
+
const l = p1.findLine(p2);
|
|
913
|
+
const { center } = l;
|
|
914
|
+
const intersections = poly.holes.reduce((a, hole) => a && Boolean(hole.clone().close()
|
|
915
|
+
.intersection(l, true).length), Boolean(poly.clone().close()
|
|
916
|
+
.intersection(l, true).length));
|
|
917
|
+
const contain = poly.holes.reduce((a, hole) => a && !hole
|
|
918
|
+
.contain(center), poly.contain(center));
|
|
919
|
+
return !intersections && contain;
|
|
920
|
+
};
|
|
921
|
+
const getTriangle = (poly) => {
|
|
922
|
+
for (let i = 0; i < poly.length; i++) {
|
|
923
|
+
const p0 = poly.at(0);
|
|
924
|
+
const p1 = poly.at(1);
|
|
925
|
+
const p2 = poly.at(2);
|
|
926
|
+
if (innerAndNotIntersect(poly, p0, p2)) {
|
|
927
|
+
poly.removePart(0, 1);
|
|
928
|
+
return [
|
|
929
|
+
p0.properties.index,
|
|
930
|
+
p1.properties.index,
|
|
931
|
+
p2.properties.index
|
|
932
|
+
];
|
|
933
|
+
}
|
|
934
|
+
poly.push(poly.shift());
|
|
935
|
+
}
|
|
936
|
+
return undefined;
|
|
937
|
+
};
|
|
938
|
+
let p = this.clone();
|
|
939
|
+
let index = 0;
|
|
940
|
+
p.points.forEach((f) => {
|
|
941
|
+
f.properties.index = index++;
|
|
942
|
+
});
|
|
943
|
+
p.holes.forEach((h) => {
|
|
944
|
+
h.pPoints.forEach((f) => {
|
|
945
|
+
f.properties.index = index++;
|
|
946
|
+
});
|
|
947
|
+
});
|
|
948
|
+
p = p.clockWise.open();
|
|
949
|
+
while (p.holes.length) {
|
|
950
|
+
const h = p.holes.shift()
|
|
951
|
+
.clone()
|
|
952
|
+
.clockWise
|
|
953
|
+
.reverse()
|
|
954
|
+
.close();
|
|
955
|
+
for (let i = 0; i < p.length; i++) {
|
|
956
|
+
if (innerAndNotIntersect(p, p.first, h.first)) {
|
|
957
|
+
p.insertAfter(0, ...h.points, p.first);
|
|
958
|
+
break;
|
|
959
|
+
}
|
|
960
|
+
p.push(p.shift());
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
const res = [];
|
|
964
|
+
while (p.length > 3) {
|
|
965
|
+
const triangle = getTriangle(p);
|
|
966
|
+
if (triangle) {
|
|
967
|
+
res.push(...triangle);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
res.push(...p.points.map((f) => f.properties.index));
|
|
971
|
+
return res;
|
|
972
|
+
}
|
|
907
973
|
get closed() {
|
|
908
974
|
return this.first.equal(this.last);
|
|
909
975
|
}
|