poly-extrude 0.17.1 → 0.18.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/src/polygon.ts CHANGED
@@ -13,10 +13,10 @@ type PolygonsResult = ResultType & {
13
13
  }
14
14
 
15
15
 
16
- export function extrudePolygons(polygons: Array<PolygonType>, options?: PolygonsOptions): PolygonsResult {
17
- options = Object.assign({}, { depth: 2, top: true }, options);
16
+ export function extrudePolygons(polygons: Array<PolygonType>, opts?: PolygonsOptions): PolygonsResult {
17
+ const options = Object.assign({}, { depth: 2, top: true }, opts);
18
18
  const results = polygons.map(polygon => {
19
- validatePolygon(polygon);
19
+ validatePolygon(polygon, true);
20
20
  const result = flatVertices(polygon, options) as Record<string, any>;
21
21
  result.polygon = polygon;
22
22
  const triangles = earcut(result.flatVertices, result.holes, 2);
@@ -28,7 +28,7 @@ export function extrudePolygons(polygons: Array<PolygonType>, options?: Polygons
28
28
  result.normal = generateNormal(result.indices, result.position);
29
29
  return result;
30
30
  });
31
- const result = merge(results as Array<ResultType>) as PolygonsResult;
31
+ const result = merge<PolygonsResult>(results as Array<ResultType>);
32
32
  result.polygons = polygons;
33
33
  return result;
34
34
 
@@ -193,9 +193,10 @@ function simplePolygon(polygon, options = {}) {
193
193
  }
194
194
  }
195
195
 
196
- export function polygons(polygons, options = {}): PolygonsResult {
196
+ export function polygons(polygons, opts = {}): PolygonsResult {
197
+ const options = Object.assign({}, opts);
197
198
  const results = polygons.map(polygon => {
198
- validatePolygon(polygon);
199
+ validatePolygon(polygon, true);
199
200
 
200
201
  const result = simplePolygon(polygon, options) as Record<string, any>;
201
202
  result.polygon = polygon;
@@ -205,7 +206,7 @@ export function polygons(polygons, options = {}): PolygonsResult {
205
206
  result.normal = new Float32Array(result.normal);
206
207
  return result;
207
208
  });
208
- const result = merge(results as Array<ResultType>) as PolygonsResult;
209
+ const result = merge<PolygonsResult>(results as Array<ResultType>);
209
210
  result.polygons = polygons;
210
211
  return result;
211
212
 
@@ -1,8 +1,8 @@
1
1
  import { Vector3 } from './math/Vector3';
2
- import { PathPoint } from './path/PathPoint';
2
+ // import { PathPoint } from './path/PathPoint';
3
3
  import { PathPointList } from './path/PathPointList';
4
4
  import { PolygonType, PolylineType, ResultType } from './type';
5
- import { generateNormal, isClockwise, line2Vectors, merge, validateRing, isClosedRing, calPolygonPointsCount, getPolygonsBBOX, mergeArray } from './util';
5
+ import { generateNormal, line2Vectors, merge, isClosedRing, calPolygonPointsCount, getPolygonsBBOX, mergeArray, validatePolygon } from './util';
6
6
  import earcut from 'earcut';
7
7
  const UP = new Vector3(0, 0, 1);
8
8
  const normalDir = new Vector3();
@@ -11,18 +11,23 @@ type PolygonsOnPathOptions = {
11
11
  extrudePath: PolylineType;
12
12
  openEnd?: boolean;
13
13
  openEndUV?: boolean;
14
+ polygonRotation?: number;
14
15
 
15
16
  }
16
17
 
18
+ type PrivatePolygonsOnPathOptions = PolygonsOnPathOptions & {
19
+ center: Point;
20
+ bbox: [number, number, number, number]
21
+ }
22
+
17
23
  type PolygonsOnPathResult = ResultType & {
18
24
  polygons: Array<PolygonType>;
19
25
  }
20
26
 
21
-
22
27
  type Point = [number, number];
23
28
 
24
- export function extrudePolygonsOnPath(polygons: Array<PolygonType>, options?: PolygonsOnPathOptions) {
25
- options = Object.assign({}, { openEnd: false, openEndUV: true }, options);
29
+ export function extrudePolygonsOnPath(polygons: Array<PolygonType>, opts?: PolygonsOnPathOptions) {
30
+ const options = (Object.assign({}, { openEnd: false, openEndUV: true, polygonRotation: 0 }, opts)) as PrivatePolygonsOnPathOptions;
26
31
  const { extrudePath, openEnd } = options;
27
32
  if (!extrudePath || !Array.isArray(extrudePath) || extrudePath.length < 2) {
28
33
  console.error('extrudePath is error:', extrudePath);
@@ -31,6 +36,8 @@ export function extrudePolygonsOnPath(polygons: Array<PolygonType>, options?: Po
31
36
  const bbox = getPolygonsBBOX(polygons);
32
37
  const [minx, miny, maxx, maxy] = bbox;
33
38
  const center = [(minx + maxx) / 2, (miny + maxy) / 2] as Point;
39
+ options.center = center;
40
+ options.bbox = bbox;
34
41
 
35
42
  const points = line2Vectors(extrudePath);
36
43
  const pathPointList = new PathPointList();
@@ -38,19 +45,8 @@ export function extrudePolygonsOnPath(polygons: Array<PolygonType>, options?: Po
38
45
  pathPointList.set(points, 0, options.cornerSplit, UP);
39
46
 
40
47
  const results = polygons.map(polygon => {
41
- for (let i = 0, len = polygon.length; i < len; i++) {
42
- const ring = polygon[i];
43
- validateRing(ring);
44
- if (i === 0) {
45
- if (!isClockwise(ring)) {
46
- polygon[i] = ring.reverse();
47
- }
48
- } else if (isClockwise(ring)) {
49
- polygon[i] = ring.reverse();
50
- }
51
- }
52
-
53
- const result = generatePolygonOnPathVertexData(pathPointList, polygon, center) as Record<string, any>;
48
+ validatePolygon(polygon, false);
49
+ const result = generatePolygonOnPathVertexData(pathPointList, polygon, options as PrivatePolygonsOnPathOptions) as Record<string, any>;
54
50
  if (!openEnd) {
55
51
  generateStartAndEnd(result, polygon, options);
56
52
  }
@@ -61,7 +57,7 @@ export function extrudePolygonsOnPath(polygons: Array<PolygonType>, options?: Po
61
57
  result.normal = new Float32Array(result.normal);
62
58
  return result;
63
59
  });
64
- const result = merge(results as Array<ResultType>) as PolygonsOnPathResult;
60
+ const result = merge<PolygonsOnPathResult>(results as Array<ResultType>);
65
61
  result.polygons = polygons;
66
62
  return result;
67
63
  }
@@ -76,7 +72,8 @@ function getAngle(c1: Point, c2: Point) {
76
72
  }
77
73
 
78
74
 
79
- function transformPolygon(polygon: PolygonType, center: Point) {
75
+ function transformPolygon(polygon: PolygonType, options: PrivatePolygonsOnPathOptions) {
76
+ const { center, polygonRotation } = options;
80
77
  const [cx, cy] = center;
81
78
  const list = [];
82
79
  polygon.forEach((ring, rIndex) => {
@@ -100,7 +97,7 @@ function transformPolygon(polygon: PolygonType, center: Point) {
100
97
  // dz: 0,
101
98
  distance,
102
99
  radius: Math.sqrt(offsetx * offsetx + offsety * offsety),
103
- angle: -getAngle(center, p as Point)
100
+ angle: -getAngle(center, p as Point) + polygonRotation
104
101
  }
105
102
  tempPoint = p;
106
103
  }
@@ -115,8 +112,8 @@ function transformPolygon(polygon: PolygonType, center: Point) {
115
112
  const TEMP_VECTOR3 = new Vector3(0, 0, 0);
116
113
  // Vertex Data Generate Functions
117
114
  // code copy from https://github.com/shawn0326/three.path/blob/master/src/PathGeometry.js
118
- function generatePolygonOnPathVertexData(pathPointList, polygon: PolygonType, center: Point) {
119
- const tpolygon = transformPolygon(polygon, center);
115
+ function generatePolygonOnPathVertexData(pathPointList, polygon: PolygonType, options: PrivatePolygonsOnPathOptions) {
116
+ const tpolygon = transformPolygon(polygon, options);
120
117
  // let count = 0;
121
118
  // modify data
122
119
  const points: number[] = [];
@@ -154,9 +151,13 @@ function generatePolygonOnPathVertexData(pathPointList, polygon: PolygonType, ce
154
151
  v.y *= radius;
155
152
  v.z *= radius;
156
153
 
157
- points[++pIndex] = pathPoint.pos.x + v.x;
158
- points[++pIndex] = pathPoint.pos.y + v.y;
159
- points[++pIndex] = pathPoint.pos.z + v.z;
154
+ const x = pathPoint.pos.x + v.x;
155
+ const y = pathPoint.pos.y + v.y;
156
+ const z = pathPoint.pos.z + v.z;
157
+
158
+ points[++pIndex] = x;
159
+ points[++pIndex] = y;
160
+ points[++pIndex] = z;
160
161
 
161
162
  // if (i === 0 || i === radialSegments - 1) {
162
163
  // console.log(i, radialSegments, v.x, v.y, v.z);
@@ -174,15 +175,15 @@ function generatePolygonOnPathVertexData(pathPointList, polygon: PolygonType, ce
174
175
 
175
176
  if (first && !isLast) {
176
177
  let index = startPoints.length - 1;
177
- startPoints[++index] = pathPoint.pos.x + v.x;
178
- startPoints[++index] = pathPoint.pos.y + v.y;
179
- startPoints[++index] = pathPoint.pos.z + v.z;
178
+ startPoints[++index] = x;
179
+ startPoints[++index] = y;
180
+ startPoints[++index] = z;
180
181
  }
181
182
  if (end && !isLast) {
182
183
  let index = endPoints.length - 1;
183
- endPoints[++index] = pathPoint.pos.x + v.x;
184
- endPoints[++index] = pathPoint.pos.y + v.y;
185
- endPoints[++index] = pathPoint.pos.z + v.z;
184
+ endPoints[++index] = x;
185
+ endPoints[++index] = y;
186
+ endPoints[++index] = z;
186
187
  }
187
188
  }
188
189
 
package/src/polyline.ts CHANGED
@@ -18,8 +18,8 @@ type PolylinesResult = ResultType & {
18
18
  lines: Array<PolylineType>;
19
19
  }
20
20
 
21
- export function extrudePolylines(lines: Array<PolylineType>, options?: PolylinesOptions): PolylinesResult {
22
- options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false, pathUV: false }, options);
21
+ export function extrudePolylines(lines: Array<PolylineType>, opts?: PolylinesOptions): PolylinesResult {
22
+ const options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false, pathUV: false }, opts);
23
23
  checkOptions(options);
24
24
  const results = lines.map(line => {
25
25
  const result = expandLine(line, options) as Record<string, any>;
@@ -32,7 +32,7 @@ export function extrudePolylines(lines: Array<PolylineType>, options?: Polylines
32
32
  result.normal = generateNormal(result.indices, result.position);
33
33
  return result;
34
34
  });
35
- const result = merge(results as Array<ResultType>) as PolylinesResult;
35
+ const result = merge<PolylinesResult>(results as Array<ResultType>);
36
36
  result.lines = lines;
37
37
  return result;
38
38
  }
@@ -42,8 +42,8 @@ type SlopesOptions = PolylinesOptions & {
42
42
  sideDepth?: number
43
43
  }
44
44
 
45
- export function extrudeSlopes(lines: Array<PolylineType>, options?: SlopesOptions): PolylinesResult {
46
- options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false, pathUV: false, isSlope: true }, options);
45
+ export function extrudeSlopes(lines: Array<PolylineType>, opts?: SlopesOptions): PolylinesResult {
46
+ const options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false, pathUV: false, isSlope: true }, opts);
47
47
  checkOptions(options);
48
48
  const { depth, side, sideDepth } = options;
49
49
  const results = lines.map(line => {
@@ -73,7 +73,7 @@ export function extrudeSlopes(lines: Array<PolylineType>, options?: SlopesOption
73
73
  result.normal = generateNormal(result.indices, result.position);
74
74
  return result;
75
75
  });
76
- const result = merge(results as Array<ResultType>) as PolylinesResult;
76
+ const result = merge<PolylinesResult>(results as Array<ResultType>);
77
77
  result.lines = lines;
78
78
  return result;
79
79
  }
package/src/tube.ts CHANGED
@@ -17,8 +17,8 @@ type TubesResult = ResultType & {
17
17
  lines: Array<PolylineType>;
18
18
  }
19
19
 
20
- export function expandTubes(lines: Array<PolylineType>, options?: TubesOptions) {
21
- options = Object.assign({}, { radius: 1, cornerSplit: 0, radialSegments: 8, startRad: -Math.PI / 4 }, options);
20
+ export function expandTubes(lines: Array<PolylineType>, opts?: TubesOptions) {
21
+ const options = Object.assign({}, { radius: 1, cornerSplit: 0, radialSegments: 8, startRad: -Math.PI / 4 }, opts);
22
22
  const results = lines.map(line => {
23
23
  const points = line2Vectors(line);
24
24
  const pathPointList = new PathPointList();
@@ -32,7 +32,7 @@ export function expandTubes(lines: Array<PolylineType>, options?: TubesOptions)
32
32
  result.normal = new Float32Array(result.normal);
33
33
  return result;
34
34
  });
35
- const result = merge(results as Array<ResultType>) as TubesResult;
35
+ const result = merge<TubesResult>(results as Array<ResultType>);
36
36
  result.lines = lines;
37
37
  return result;
38
38
  }
package/src/util.ts CHANGED
@@ -72,7 +72,7 @@ export function getPolygonsBBOX(polygons, bbox?) {
72
72
  return bbox;
73
73
  }
74
74
 
75
- export function validatePolygon(polygon: PolygonType) {
75
+ export function validatePolygon(polygon: PolygonType, ignoreEndPoint: boolean) {
76
76
  for (let i = 0, len = polygon.length; i < len; i++) {
77
77
  const ring = polygon[i];
78
78
  validateRing(ring);
@@ -83,8 +83,10 @@ export function validatePolygon(polygon: PolygonType) {
83
83
  } else if (isClockwise(ring)) {
84
84
  polygon[i] = ring.reverse();
85
85
  }
86
- if (isClosedRing(ring)) {
87
- ring.splice(ring.length - 1, 1);
86
+ if (ignoreEndPoint) {
87
+ if (isClosedRing(ring)) {
88
+ ring.splice(ring.length - 1, 1);
89
+ }
88
90
  }
89
91
  }
90
92
  }
@@ -176,7 +178,7 @@ export function generateNormal(indices, position) {
176
178
  return normals;
177
179
  }
178
180
 
179
- export function merge(results: Array<ResultType>) {
181
+ export function merge<T extends ResultType>(results: Array<ResultType>): T {
180
182
  if (results.length === 1) {
181
183
  const result = {
182
184
  position: results[0].position,
@@ -185,7 +187,7 @@ export function merge(results: Array<ResultType>) {
185
187
  indices: results[0].indices,
186
188
  results
187
189
  };
188
- return result;
190
+ return result as T;
189
191
  }
190
192
  let plen = 0, ilen = 0;
191
193
  for (let i = 0, len = results.length; i < len; i++) {
@@ -218,14 +220,14 @@ export function merge(results: Array<ResultType>) {
218
220
  pOffset += position.length;
219
221
  pCount += position.length / 3;
220
222
  }
221
- return result;
223
+ return result as T;
222
224
  }
223
225
 
224
- export function radToDeg(rad) {
226
+ export function radToDeg(rad: number) {
225
227
  return rad * 180 / Math.PI;
226
228
  }
227
229
 
228
- export function degToRad(angle) {
230
+ export function degToRad(angle: number) {
229
231
  return angle / 180 * Math.PI;
230
232
  }
231
233
 
@@ -279,7 +281,7 @@ export function generateSideWallUV(uvs, vertices, indexA, indexB, indexC, indexD
279
281
 
280
282
  }
281
283
 
282
- export function line2Vectors(line) {
284
+ export function line2Vectors(line: PolylineType) {
283
285
  const points: Vector3[] = [];
284
286
  for (let i = 0, len = line.length; i < len; i++) {
285
287
  const p = line[i];