poly-extrude 0.13.0 → 0.15.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.
Files changed (68) hide show
  1. package/dist/cylinder.d.ts +11 -0
  2. package/{src → dist}/cylinder.js +108 -111
  3. package/dist/cylinder.js.map +1 -0
  4. package/dist/index.d.ts +8 -0
  5. package/dist/index.js +9 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/math/Curve.d.ts +41 -0
  8. package/dist/math/Curve.js +142 -0
  9. package/dist/math/Curve.js.map +1 -0
  10. package/dist/math/Interpolations.d.ts +8 -0
  11. package/dist/math/Interpolations.js +48 -0
  12. package/dist/math/Interpolations.js.map +1 -0
  13. package/dist/math/Matrix4.d.ts +8 -0
  14. package/dist/math/Matrix4.js +582 -0
  15. package/dist/math/Matrix4.js.map +1 -0
  16. package/dist/math/QuadraticBezierCurve3.d.ts +10 -0
  17. package/dist/math/QuadraticBezierCurve3.js +22 -0
  18. package/dist/math/QuadraticBezierCurve3.js.map +1 -0
  19. package/dist/math/Quaternion.d.ts +46 -0
  20. package/dist/math/Quaternion.js +415 -0
  21. package/dist/math/Quaternion.js.map +1 -0
  22. package/dist/math/Vector3.d.ts +42 -0
  23. package/dist/math/Vector3.js +403 -0
  24. package/dist/math/Vector3.js.map +1 -0
  25. package/dist/path/PathPoint.d.ts +15 -0
  26. package/dist/path/PathPoint.js +35 -0
  27. package/dist/path/PathPoint.js.map +1 -0
  28. package/dist/path/PathPointList.d.ts +27 -0
  29. package/dist/path/PathPointList.js +212 -0
  30. package/dist/path/PathPointList.js.map +1 -0
  31. package/dist/path.d.ts +11 -0
  32. package/{src → dist}/path.js +334 -360
  33. package/dist/path.js.map +1 -0
  34. package/dist/plane.d.ts +2 -0
  35. package/{src → dist}/plane.js +57 -58
  36. package/dist/plane.js.map +1 -0
  37. package/dist/poly-extrude.js +1288 -1581
  38. package/dist/poly-extrude.js.map +1 -1
  39. package/dist/poly-extrude.min.js +2 -2
  40. package/dist/poly-extrude.mjs +1287 -1582
  41. package/dist/poly-extrude.mjs.map +1 -0
  42. package/dist/polygon.d.ts +9 -0
  43. package/{src → dist}/polygon.js +163 -179
  44. package/dist/polygon.js.map +1 -0
  45. package/dist/polyline.d.ts +24 -0
  46. package/{src → dist}/polyline.js +420 -456
  47. package/dist/polyline.js.map +1 -0
  48. package/dist/tube.d.ts +12 -0
  49. package/{src → dist}/tube.js +124 -142
  50. package/dist/tube.js.map +1 -0
  51. package/dist/type.d.ts +9 -0
  52. package/dist/type.js +2 -0
  53. package/dist/type.js.map +1 -0
  54. package/dist/util.d.ts +21 -0
  55. package/{src → dist}/util.js +217 -242
  56. package/dist/util.js.map +1 -0
  57. package/package.json +10 -5
  58. package/readme.md +12 -2
  59. package/src/cylinder.ts +124 -0
  60. package/src/index.ts +13 -0
  61. package/src/path.ts +385 -0
  62. package/src/plane.ts +60 -0
  63. package/src/polygon.ts +189 -0
  64. package/src/polyline.ts +473 -0
  65. package/src/tube.ts +155 -0
  66. package/src/type.ts +9 -0
  67. package/src/util.ts +243 -0
  68. package/index.js +0 -7
package/src/tube.ts ADDED
@@ -0,0 +1,155 @@
1
+ import { Vector3 } from './math/Vector3';
2
+ import { PathPoint } from './path/PathPoint';
3
+ import { PathPointList } from './path/PathPointList';
4
+ import { PolylineType, ResultType } from './type';
5
+ import { line2Vectors, merge } from './util';
6
+ const UP = new Vector3(0, 0, 1);
7
+ const normalDir = new Vector3();
8
+
9
+ type TubesOptions = {
10
+ radius?: number;
11
+ cornerSplit?: number;
12
+ radialSegments?: number;
13
+ startRad?: number;
14
+ }
15
+
16
+ type TubesResult = ResultType & {
17
+ lines: Array<PolylineType>;
18
+ }
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);
22
+ const results = lines.map(line => {
23
+ const points = line2Vectors(line);
24
+ const pathPointList = new PathPointList();
25
+ //@ts-ignore
26
+ pathPointList.set(points, 0, options.cornerSplit, UP);
27
+ const result = generateTubeVertexData(pathPointList, options) as Record<string, any>;
28
+ result.line = line;
29
+ result.position = new Float32Array(result.points);
30
+ result.indices = new Uint32Array(result.indices);
31
+ result.uv = new Float32Array(result.uv);
32
+ result.normal = new Float32Array(result.normal);
33
+ return result;
34
+ });
35
+ const result = merge(results as Array<ResultType>) as TubesResult;
36
+ result.lines = lines;
37
+ return result;
38
+ }
39
+
40
+ // Vertex Data Generate Functions
41
+ // code copy from https://github.com/shawn0326/three.path/blob/master/src/PathGeometry.js
42
+ function generateTubeVertexData(pathPointList, options) {
43
+ const radius = Math.max(options.radius || 1, 0.00000001);
44
+ const progress = options.progress !== undefined ? options.progress : 1;
45
+ const radialSegments = Math.max(3, options.radialSegments || 8);
46
+ const startRad = options.startRad || 0;
47
+
48
+ const circum = radius * 2 * Math.PI;
49
+ const totalDistance = pathPointList.distance();
50
+ const progressDistance = progress * totalDistance;
51
+ if (progressDistance === 0) {
52
+ return null;
53
+ }
54
+
55
+ let count = 0;
56
+
57
+ // modify data
58
+ const points: number[] = [];
59
+ const normal: number[] = [];
60
+ const uv: number[] = [];
61
+ // const uv2 = [];
62
+ const indices: number[] = [];
63
+ let verticesCount = 0;
64
+
65
+ let pIndex = -1;
66
+ let nIndex = -1;
67
+ let uIndex = -1;
68
+ let iIndex = -1;
69
+ function addVertices(pathPoint, radius, radialSegments) {
70
+ const first = points.length === 0;
71
+ const uvDist = pathPoint.dist / circum;
72
+ // const uvDist2 = pathPoint.dist / totalDistance;
73
+
74
+ for (let i = 0; i <= radialSegments; i++) {
75
+ let r = i;
76
+ if (r === radialSegments) {
77
+ r = 0;
78
+ }
79
+ normalDir.copy(pathPoint.up).applyAxisAngle(pathPoint.dir, startRad + Math.PI * 2 * r / radialSegments).normalize();
80
+
81
+ const scale = radius * pathPoint.widthScale;
82
+ points[++pIndex] = pathPoint.pos.x + normalDir.x * scale;
83
+ points[++pIndex] = pathPoint.pos.y + normalDir.y * scale;
84
+ points[++pIndex] = pathPoint.pos.z + normalDir.z * scale;
85
+
86
+ normal[++nIndex] = normalDir.x;
87
+ normal[++nIndex] = normalDir.y;
88
+ normal[++nIndex] = normalDir.z;
89
+
90
+ uv[++uIndex] = uvDist;
91
+ uv[++uIndex] = i / radialSegments;
92
+
93
+ // uvs.push(uvDist, r / radialSegments);
94
+
95
+ // if (generateUv2) {
96
+ // uv2.push(uvDist2, r / radialSegments);
97
+ // }
98
+
99
+ verticesCount++;
100
+ }
101
+
102
+ if (!first) {
103
+ const begin1 = verticesCount - (radialSegments + 1) * 2;
104
+ const begin2 = verticesCount - (radialSegments + 1);
105
+
106
+ for (let i = 0; i < radialSegments; i++) {
107
+ indices[++iIndex] = begin2 + i;
108
+ indices[++iIndex] = begin1 + i;
109
+ indices[++iIndex] = begin1 + i + 1;
110
+ indices[++iIndex] = begin2 + i;
111
+ indices[++iIndex] = begin1 + i + 1;
112
+ indices[++iIndex] = begin2 + i + 1;
113
+ // index.push(
114
+ // begin2 + i,
115
+ // begin1 + i,
116
+ // begin1 + i + 1,
117
+ // begin2 + i,
118
+ // begin1 + i + 1,
119
+ // begin2 + i + 1
120
+ // );
121
+
122
+ count += 6;
123
+ }
124
+ }
125
+ }
126
+
127
+ if (progressDistance > 0) {
128
+ for (let i = 0; i < pathPointList.count; i++) {
129
+ const pathPoint = pathPointList.array[i];
130
+
131
+ if (pathPoint.dist > progressDistance) {
132
+ const prevPoint = pathPointList.array[i - 1];
133
+ const lastPoint = new PathPoint();
134
+
135
+ // linear lerp for progress
136
+ const alpha = (progressDistance - prevPoint.dist) / (pathPoint.dist - prevPoint.dist);
137
+ lastPoint.lerpPathPoints(prevPoint, pathPoint, alpha);
138
+
139
+ addVertices(lastPoint, radius, radialSegments);
140
+ break;
141
+ } else {
142
+ addVertices(pathPoint, radius, radialSegments);
143
+ }
144
+ }
145
+ }
146
+
147
+ return {
148
+ points,
149
+ normal,
150
+ uv,
151
+ // uv2,
152
+ indices,
153
+ count
154
+ };
155
+ }
package/src/type.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type PolylineType = Array<number[]>;
2
+ export type PolygonType = Array<Array<number[]>>;
3
+ export type ResultType = {
4
+ position: Float32Array;
5
+ normal: Float32Array;
6
+ uv: Float32Array;
7
+ indices: Uint32Array;
8
+ results?: Array<any>;
9
+ }
package/src/util.ts ADDED
@@ -0,0 +1,243 @@
1
+ import { Vector3 } from './math/Vector3';
2
+ import { PolylineType, ResultType } from './type';
3
+
4
+ /**
5
+ * https://github.com/Turfjs/turf/blob/master/packages/turf-boolean-clockwise/index.ts
6
+ * @param {*} ring
7
+ * @returns
8
+ */
9
+ export function isClockwise(ring: PolylineType) {
10
+ let sum = 0;
11
+ let i = 1;
12
+ let prev;
13
+ let cur;
14
+ const len = ring.length;
15
+
16
+ while (i < len) {
17
+ prev = cur || ring[0];
18
+ cur = ring[i];
19
+ sum += (cur[0] - prev[0]) * (cur[1] + prev[1]);
20
+ i++;
21
+ }
22
+ return sum > 0;
23
+ }
24
+
25
+ function v3Sub(out, v1, v2) {
26
+ out[0] = v1[0] - v2[0];
27
+ out[1] = v1[1] - v2[1];
28
+ out[2] = v1[2] - v2[2];
29
+ return out;
30
+ }
31
+
32
+ function v3Normalize(out, v) {
33
+ const x = v[0];
34
+ const y = v[1];
35
+ const z = v[2];
36
+ const d = Math.sqrt(x * x + y * y + z * z) || 1;
37
+ out[0] = x / d;
38
+ out[1] = y / d;
39
+ out[2] = z / d;
40
+ return out;
41
+ }
42
+
43
+ function v3Cross(out, v1, v2) {
44
+ const ax = v1[0], ay = v1[1], az = v1[2],
45
+ bx = v2[0], by = v2[1], bz = v2[2];
46
+
47
+ out[0] = ay * bz - az * by;
48
+ out[1] = az * bx - ax * bz;
49
+ out[2] = ax * by - ay * bx;
50
+ return out;
51
+ }
52
+
53
+ export function generateNormal(indices, position) {
54
+
55
+ function v3Set(p, a, b, c) {
56
+ p[0] = a; p[1] = b; p[2] = c;
57
+ }
58
+
59
+ const p1 = [];
60
+ const p2 = [];
61
+ const p3 = [];
62
+
63
+ const v21 = [];
64
+ const v32 = [];
65
+
66
+ const n = [];
67
+
68
+ const len = indices.length;
69
+ const normals = new Float32Array(position.length);
70
+ let f = 0;
71
+ while (f < len) {
72
+
73
+ // const i1 = indices[f++] * 3;
74
+ // const i2 = indices[f++] * 3;
75
+ // const i3 = indices[f++] * 3;
76
+ // const i1 = indices[f];
77
+ // const i2 = indices[f + 1];
78
+ // const i3 = indices[f + 2];
79
+ const a = indices[f], b = indices[f + 1], c = indices[f + 2];
80
+ const i1 = a * 3, i2 = b * 3, i3 = c * 3;
81
+
82
+ v3Set(p1, position[i1], position[i1 + 1], position[i1 + 2]);
83
+ v3Set(p2, position[i2], position[i2 + 1], position[i2 + 2]);
84
+ v3Set(p3, position[i3], position[i3 + 1], position[i3 + 2]);
85
+
86
+ v3Sub(v32, p3, p2);
87
+ v3Sub(v21, p1, p2);
88
+ v3Cross(n, v32, v21);
89
+ // Already be weighted by the triangle area
90
+ for (let i = 0; i < 3; i++) {
91
+ normals[i1 + i] += n[i];
92
+ normals[i2 + i] += n[i];
93
+ normals[i3 + i] += n[i];
94
+ }
95
+ f += 3;
96
+ }
97
+
98
+ let i = 0;
99
+ const l = normals.length;
100
+ while (i < l) {
101
+ v3Set(n, normals[i], normals[i + 1], normals[i + 2]);
102
+ v3Normalize(n, n);
103
+ normals[i] = n[0] || 0;
104
+ normals[i + 1] = n[1] || 0;
105
+ normals[i + 2] = n[2] || 0;
106
+ i += 3;
107
+ }
108
+
109
+ return normals;
110
+ }
111
+
112
+ export function merge(results: Array<ResultType>) {
113
+ if (results.length === 1) {
114
+ const result = {
115
+ position: results[0].position,
116
+ normal: results[0].normal,
117
+ uv: results[0].uv,
118
+ indices: results[0].indices,
119
+ results
120
+ };
121
+ return result;
122
+ }
123
+ let plen = 0, ilen = 0;
124
+ for (let i = 0, len = results.length; i < len; i++) {
125
+ const { position, indices } = results[i];
126
+ plen += position.length;
127
+ ilen += indices.length;
128
+ }
129
+ const result = {
130
+ position: new Float32Array(plen),
131
+ normal: new Float32Array(plen),
132
+ uv: new Float32Array(plen / 3 * 2),
133
+ indices: new Uint32Array(ilen),
134
+ results
135
+ };
136
+ let pOffset = 0, pCount = 0, iIdx = 0, uvOffset = 0;
137
+ for (let i = 0, len = results.length; i < len; i++) {
138
+ const { position, indices, normal, uv } = results[i];
139
+ result.position.set(position, pOffset);
140
+ result.normal.set(normal, pOffset);
141
+ result.uv.set(uv, uvOffset);
142
+ let j = 0;
143
+ const len1 = indices.length;
144
+ while (j < len1) {
145
+ const pIndex = indices[j] + pCount;
146
+ result.indices[iIdx] = pIndex;
147
+ iIdx++;
148
+ j++;
149
+ }
150
+ uvOffset += uv.length;
151
+ pOffset += position.length;
152
+ pCount += position.length / 3;
153
+ }
154
+ return result;
155
+ }
156
+
157
+ export function radToDeg(rad) {
158
+ return rad * 180 / Math.PI;
159
+ }
160
+
161
+ export function degToRad(angle) {
162
+ return angle / 180 * Math.PI;
163
+ }
164
+
165
+ // https://github.com/mrdoob/three.js/blob/16f13e3b07e31d0e9a00df7c3366bbe0e464588c/src/geometries/ExtrudeGeometry.js?_pjax=%23js-repo-pjax-container#L736
166
+ export function generateSideWallUV(uvs, vertices, indexA, indexB, indexC, indexD) {
167
+
168
+ const idx1 = indexA * 3, idx2 = indexB * 3, idx3 = indexC * 3, idx4 = indexD * 3;
169
+ const a_x = vertices[idx1];
170
+ const a_y = vertices[idx1 + 1];
171
+ const a_z = vertices[idx1 + 2];
172
+ const b_x = vertices[idx2];
173
+ const b_y = vertices[idx2 + 1];
174
+ const b_z = vertices[idx2 + 2];
175
+ const c_x = vertices[idx3];
176
+ const c_y = vertices[idx3 + 1];
177
+ const c_z = vertices[idx3 + 2];
178
+ const d_x = vertices[idx4];
179
+ const d_y = vertices[idx4 + 1];
180
+ const d_z = vertices[idx4 + 2];
181
+
182
+ let uIndex = uvs.length - 1;
183
+ if (Math.abs(a_y - b_y) < Math.abs(a_x - b_x)) {
184
+ uvs[++uIndex] = a_x;
185
+ uvs[++uIndex] = 1 - a_z;
186
+ uvs[++uIndex] = b_x;
187
+ uvs[++uIndex] = 1 - b_z;
188
+ uvs[++uIndex] = c_x;
189
+ uvs[++uIndex] = 1 - c_z;
190
+ uvs[++uIndex] = d_x;
191
+ uvs[++uIndex] = 1 - d_z;
192
+
193
+ // uvs.push(a_x, 1 - a_z);
194
+ // uvs.push(b_x, 1 - b_z);
195
+ // uvs.push(c_x, 1 - c_z);
196
+ // uvs.push(d_x, 1 - d_z);
197
+ } else {
198
+ uvs[++uIndex] = a_y;
199
+ uvs[++uIndex] = 1 - a_z;
200
+ uvs[++uIndex] = b_y;
201
+ uvs[++uIndex] = 1 - b_z;
202
+ uvs[++uIndex] = c_y;
203
+ uvs[++uIndex] = 1 - c_z;
204
+ uvs[++uIndex] = d_y;
205
+ uvs[++uIndex] = 1 - d_z;
206
+
207
+ // uvs.push(a_y, 1 - a_z);
208
+ // uvs.push(b_y, 1 - b_z);
209
+ // uvs.push(c_y, 1 - c_z);
210
+ // uvs.push(d_y, 1 - d_z);
211
+ }
212
+
213
+ }
214
+
215
+ export function line2Vectors(line) {
216
+ const points: Vector3[] = [];
217
+ for (let i = 0, len = line.length; i < len; i++) {
218
+ const p = line[i];
219
+ const [x, y, z] = p;
220
+ const v = new Vector3(x, y, z || 0);
221
+ points[i] = v;
222
+ }
223
+ return points;
224
+ }
225
+
226
+ export function calLineDistance(line) {
227
+ let distance = 0;
228
+ for (let i = 0, len = line.length; i < len; i++) {
229
+ const p1 = line[i], p2 = line[i + 1];
230
+ if (i === 0) {
231
+ p1.distance = 0;
232
+ }
233
+ if (p1 && p2) {
234
+ const [x1, y1, z1] = p1;
235
+ const [x2, y2, z2] = p2;
236
+ const dx = x1 - x2, dy = y1 - y2, dz = (z1 || 0) - (z2 || 0);
237
+ const dis = Math.sqrt(dx * dx + dy * dy + dz * dz);
238
+ distance += dis;
239
+ p2.distance = distance;
240
+ }
241
+ }
242
+ return distance;
243
+ }
package/index.js DELETED
@@ -1,7 +0,0 @@
1
- import { extrudePolygons } from './src/polygon';
2
- import { extrudePolylines, expandLine, leftOnLine, extrudeSlopes } from './src/polyline';
3
- import { cylinder } from './src/cylinder';
4
- import { expandPaths } from './src/path';
5
- import { expandTubes } from './src/tube';
6
- import { plane } from './src/plane';
7
- export { extrudePolygons, extrudePolylines, extrudeSlopes, expandLine, leftOnLine, cylinder, expandPaths, expandTubes, plane };