poly-extrude 0.1.0 → 0.3.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/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { extrudePolygons } from './src/polygon';
2
2
  import { extrudePolylines, expandLine } from './src/polyline';
3
3
  import { cylinder } from './src/cylinder';
4
- export { extrudePolygons, extrudePolylines, expandLine, cylinder };
4
+ import { expandPaths } from './src/path';
5
+ export { extrudePolygons, extrudePolylines, expandLine, cylinder, expandPaths };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poly-extrude",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "main": "dist/poly-extrude.js",
6
6
  "module": "dist/poly-extrude.mjs",
package/readme.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # poly-extrude
2
2
 
3
+
3
4
  Extrude polygons/polylines. Born in [maptalks.three](https://github.com/maptalks/maptalks.three) project<br>
5
+
6
+ ## Examples
4
7
  [building](https://deyihu.github.io/poly-extrude/test/building.html)<br>
5
8
  ![](./gallery/building.png)<br>
6
9
  [buildings](https://deyihu.github.io/poly-extrude/test/buildings.html)<br>
@@ -11,8 +14,18 @@ Extrude polygons/polylines. Born in [maptalks.three](https://github.com/maptalks
11
14
  ![](./gallery/street.png)<br>
12
15
  [line-uv](https://deyihu.github.io/poly-extrude/test/line-uv.html)<br>
13
16
  ![](./gallery/line-uv.png)
14
-
15
- ## install
17
+ [ny-building](https://deyihu.github.io/poly-extrude/test/ny-building.html)<br>
18
+ ![](./gallery/ny-building.png)
19
+ [cylinder](https://deyihu.github.io/poly-extrude/test/cylinder.html)<br>
20
+ ![](./gallery/cylinder.png)
21
+ [brige](https://deyihu.github.io/poly-extrude/test/brige.html)<br>
22
+ ![](./gallery/brige.png)
23
+ [spring](https://deyihu.github.io/poly-extrude/test/spring.html)<br>
24
+ ![](./gallery/spring.png)
25
+ [expand paths](https://deyihu.github.io/poly-extrude/test/expand-paths-brige.html)<br>
26
+ ![](./gallery/expand-paths-brige.png)
27
+
28
+ ## Install
16
29
 
17
30
  ```sh
18
31
  npm i poly-extrude
@@ -31,35 +44,109 @@ pnpm i poly-extrude
31
44
  ### ESM
32
45
 
33
46
  ```js
34
- import {extrudePolygons,extrudePolylines} from 'poly-extrude';
35
- const polygons=[
36
- //polygon
37
- [
38
- //outring
39
- [[x,y],[x,y],...........],
40
- //holes
41
- [[x,y],[x,y],...........],
42
- ........
43
-
44
- ],
45
- //other polygons
46
- ......
47
+ import {
48
+ extrudePolygons,
49
+ extrudePolylines,
50
+ cylinder,
51
+ expandPaths
52
+ } from 'poly-extrude';
53
+ const polygons = [
54
+ //polygon
55
+ [
56
+ //outring
57
+ [
58
+ [x, y],
59
+ [x, y], ...........
60
+ ],
61
+ //holes
62
+ [
63
+ [x, y],
64
+ [x, y], ...........
65
+ ],
66
+ ........
67
+
68
+ ],
69
+ //other polygons
70
+ ......
47
71
  ]
48
72
 
49
- const result = extrudePolygons(polygons,{depth:2});
50
- const {positon,normal,uv,indices}=result;
73
+ const result = extrudePolygons(polygons, {
74
+ depth: 2
75
+ });
76
+ const {
77
+ positon,
78
+ normal,
79
+ uv,
80
+ indices
81
+ } = result;
51
82
  //do something
52
83
 
53
- const polylines=[
54
- // polyline
55
- [[x,y],[x,y],...........],
56
- //polyline
57
- [[x,y],[x,y],...........],
84
+ const polylines = [
85
+ // polyline
86
+ [
87
+ [x, y],
88
+ [x, y], ...........
89
+ ],
90
+ //polyline
91
+ [
92
+ [x, y],
93
+ [x, y], ...........
94
+ ],
58
95
  ];
59
96
 
60
- const result = extrudePolylines(polylines,{depth:2,lineWidth:2});
61
- const {positon,normal,uv,indices}=result;
97
+ const result = extrudePolylines(polylines, {
98
+ depth: 2,
99
+ lineWidth: 2
100
+ });
101
+ const {
102
+ positon,
103
+ normal,
104
+ uv,
105
+ indices
106
+ } = result;
107
+ //do something
108
+
109
+ const center = [0, 0];
110
+ const result = cylinder(center, {
111
+ radius: 1,
112
+ height: 2,
113
+ radialSegments: 6
114
+ });
115
+ const {
116
+ positon,
117
+ normal,
118
+ uv,
119
+ indices
120
+ } = result;
62
121
  //do something
122
+
123
+
124
+
125
+ const polylines = [
126
+ // polyline
127
+ [
128
+ [x, y],
129
+ [x, y], ...........
130
+ ],
131
+ //polyline
132
+ [
133
+ [x, y],
134
+ [x, y], ...........
135
+ ],
136
+ ];
137
+
138
+ const result = expandPaths(polylines, {
139
+ cornerRadius: 0.5,
140
+ lineWidth: 2
141
+ });
142
+ const {
143
+ positon,
144
+ normal,
145
+ uv,
146
+ indices
147
+ } = result;
148
+ //do something
149
+
63
150
  ```
64
151
 
65
152
  ### CDN
@@ -68,13 +155,19 @@ pnpm i poly-extrude
68
155
  <script src="https://unpkg.com/poly-extrude/dist/poly-extrude.js"></script>
69
156
 
70
157
  <script>
71
- const polygons=[
158
+ const polygons = [
72
159
  //polygon
73
160
  [
74
161
  //outring
75
- [[x,y],[x,y],...........],
162
+ [
163
+ [x, y],
164
+ [x, y], ...........
165
+ ],
76
166
  //holes
77
- [[x,y],[x,y],...........],
167
+ [
168
+ [x, y],
169
+ [x, y], ...........
170
+ ],
78
171
  ........
79
172
 
80
173
  ],
@@ -82,19 +175,81 @@ pnpm i poly-extrude
82
175
  ......
83
176
  ]
84
177
 
85
- const result = polyextrude.extrudePolygons(polygons,{depth:2})
86
- const {positon,normal,uv,indices}=result;
178
+ const result = polyextrude.extrudePolygons(polygons, {
179
+ depth: 2
180
+ })
181
+ const {
182
+ positon,
183
+ normal,
184
+ uv,
185
+ indices
186
+ } = result;
87
187
  //do something
88
188
 
89
- const polylines=[
90
- // polyline
91
- [[x,y],[x,y],...........],
92
- //polyline
93
- [[x,y],[x,y],...........],
189
+ const polylines = [
190
+ // polyline
191
+ [
192
+ [x, y],
193
+ [x, y], ...........
194
+ ],
195
+ //polyline
196
+ [
197
+ [x, y],
198
+ [x, y], ...........
199
+ ],
94
200
  ];
95
201
 
96
- const result = polyextrude.extrudePolylines(polylines,{depth:2,lineWidth:2});
97
- const {positon,normal,uv,indices}=result;
202
+ const result = polyextrude.extrudePolylines(polylines, {
203
+ depth: 2,
204
+ lineWidth: 2
205
+ });
206
+ const {
207
+ positon,
208
+ normal,
209
+ uv,
210
+ indices
211
+ } = result;
98
212
  //do something
213
+
214
+ const center = [0, 0];
215
+ const result = polyextrude.cylinder(center, {
216
+ radius: 1,
217
+ height: 2,
218
+ radialSegments: 6
219
+ });
220
+ const {
221
+ positon,
222
+ normal,
223
+ uv,
224
+ indices
225
+ } = result;
226
+ //do something
227
+
228
+
229
+
230
+ const polylines = [
231
+ // polyline
232
+ [
233
+ [x, y],
234
+ [x, y], ...........
235
+ ],
236
+ //polyline
237
+ [
238
+ [x, y],
239
+ [x, y], ...........
240
+ ],
241
+ ];
242
+
243
+ const result = polyextrude.expandPaths(polylines, {
244
+ cornerRadius: 0.5,
245
+ lineWidth: 2
246
+ });
247
+ const {
248
+ positon,
249
+ normal,
250
+ uv,
251
+ indices
252
+ } = result;
253
+ //do something
99
254
  </script>
100
255
  ```
@@ -0,0 +1,415 @@
1
+
2
+ // code copy from https://github.com/mrdoob/three.js/blob/dev/src/extras/core/Curve.js
3
+ // import * as MathUtils from '../../math/MathUtils.js';
4
+ // import { Vector2 } from '../../math/Vector2.js';
5
+ // import { Vector3 } from '../../math/Vector3.js';
6
+ // import { Matrix4 } from '../../math/Matrix4.js';
7
+
8
+ /**
9
+ * Extensible curve object.
10
+ *
11
+ * Some common of curve methods:
12
+ * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget )
13
+ * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget )
14
+ * .getPoints(), .getSpacedPoints()
15
+ * .getLength()
16
+ * .updateArcLengths()
17
+ *
18
+ * This following curves inherit from THREE.Curve:
19
+ *
20
+ * -- 2D curves --
21
+ * THREE.ArcCurve
22
+ * THREE.CubicBezierCurve
23
+ * THREE.EllipseCurve
24
+ * THREE.LineCurve
25
+ * THREE.QuadraticBezierCurve
26
+ * THREE.SplineCurve
27
+ *
28
+ * -- 3D curves --
29
+ * THREE.CatmullRomCurve3
30
+ * THREE.CubicBezierCurve3
31
+ * THREE.LineCurve3
32
+ * THREE.QuadraticBezierCurve3
33
+ *
34
+ * A series of curves can be represented as a THREE.CurvePath.
35
+ *
36
+ **/
37
+
38
+ class Curve {
39
+
40
+ constructor() {
41
+
42
+ this.type = 'Curve';
43
+
44
+ this.arcLengthDivisions = 200;
45
+
46
+ }
47
+
48
+ // Virtual base class method to overwrite and implement in subclasses
49
+
50
+ getPoint() {
51
+
52
+ console.warn('THREE.Curve: .getPoint() not implemented.');
53
+ return null;
54
+
55
+ }
56
+
57
+ // Get point at relative position in curve according to arc length
58
+ // - u [0 .. 1]
59
+
60
+ getPointAt(u, optionalTarget) {
61
+
62
+ const t = this.getUtoTmapping(u);
63
+ return this.getPoint(t, optionalTarget);
64
+
65
+ }
66
+
67
+ // Get sequence of points using getPoint( t )
68
+
69
+ getPoints(divisions = 5) {
70
+
71
+ const points = [];
72
+
73
+ for (let d = 0; d <= divisions; d++) {
74
+
75
+ points.push(this.getPoint(d / divisions));
76
+
77
+ }
78
+
79
+ return points;
80
+
81
+ }
82
+
83
+ // // Get sequence of points using getPointAt( u )
84
+
85
+ // getSpacedPoints(divisions = 5) {
86
+
87
+ // const points = [];
88
+
89
+ // for (let d = 0; d <= divisions; d++) {
90
+
91
+ // points.push(this.getPointAt(d / divisions));
92
+
93
+ // }
94
+
95
+ // return points;
96
+
97
+ // }
98
+
99
+ // Get total curve arc length
100
+
101
+ getLength() {
102
+
103
+ const lengths = this.getLengths();
104
+ return lengths[lengths.length - 1];
105
+
106
+ }
107
+
108
+ // Get list of cumulative segment lengths
109
+
110
+ getLengths(divisions = this.arcLengthDivisions) {
111
+
112
+ if (this.cacheArcLengths &&
113
+ (this.cacheArcLengths.length === divisions + 1) &&
114
+ !this.needsUpdate) {
115
+
116
+ return this.cacheArcLengths;
117
+
118
+ }
119
+
120
+ this.needsUpdate = false;
121
+
122
+ const cache = [];
123
+ let current, last = this.getPoint(0);
124
+ let sum = 0;
125
+
126
+ cache.push(0);
127
+
128
+ for (let p = 1; p <= divisions; p++) {
129
+
130
+ current = this.getPoint(p / divisions);
131
+ sum += current.distanceTo(last);
132
+ cache.push(sum);
133
+ last = current;
134
+
135
+ }
136
+
137
+ this.cacheArcLengths = cache;
138
+
139
+ return cache; // { sums: cache, sum: sum }; Sum is in the last element.
140
+
141
+ }
142
+
143
+ // updateArcLengths() {
144
+
145
+ // this.needsUpdate = true;
146
+ // this.getLengths();
147
+
148
+ // }
149
+
150
+ // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
151
+
152
+ getUtoTmapping(u, distance) {
153
+
154
+ const arcLengths = this.getLengths();
155
+
156
+ let i = 0;
157
+ const il = arcLengths.length;
158
+
159
+ let targetArcLength; // The targeted u distance value to get
160
+
161
+ if (distance) {
162
+
163
+ targetArcLength = distance;
164
+
165
+ } else {
166
+
167
+ targetArcLength = u * arcLengths[il - 1];
168
+
169
+ }
170
+
171
+ // binary search for the index with largest value smaller than target u distance
172
+
173
+ let low = 0, high = il - 1, comparison;
174
+
175
+ while (low <= high) {
176
+
177
+ i = Math.floor(low + (high - low) / 2); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
178
+
179
+ comparison = arcLengths[i] - targetArcLength;
180
+
181
+ if (comparison < 0) {
182
+
183
+ low = i + 1;
184
+
185
+ } else if (comparison > 0) {
186
+
187
+ high = i - 1;
188
+
189
+ } else {
190
+
191
+ high = i;
192
+ break;
193
+
194
+ // DONE
195
+
196
+ }
197
+
198
+ }
199
+
200
+ i = high;
201
+
202
+ if (arcLengths[i] === targetArcLength) {
203
+
204
+ return i / (il - 1);
205
+
206
+ }
207
+
208
+ // we could get finer grain at lengths, or use simple interpolation between two points
209
+
210
+ const lengthBefore = arcLengths[i];
211
+ const lengthAfter = arcLengths[i + 1];
212
+
213
+ const segmentLength = lengthAfter - lengthBefore;
214
+
215
+ // determine where we are between the 'before' and 'after' points
216
+
217
+ const segmentFraction = (targetArcLength - lengthBefore) / segmentLength;
218
+
219
+ // add that fractional amount to t
220
+
221
+ const t = (i + segmentFraction) / (il - 1);
222
+
223
+ return t;
224
+
225
+ }
226
+
227
+ // Returns a unit vector tangent at t
228
+ // In case any sub curve does not implement its tangent derivation,
229
+ // 2 points a small delta apart will be used to find its gradient
230
+ // which seems to give a reasonable approximation
231
+
232
+ // getTangent(t, optionalTarget) {
233
+
234
+ // const delta = 0.0001;
235
+ // let t1 = t - delta;
236
+ // let t2 = t + delta;
237
+
238
+ // // Capping in case of danger
239
+
240
+ // if (t1 < 0) t1 = 0;
241
+ // if (t2 > 1) t2 = 1;
242
+
243
+ // const pt1 = this.getPoint(t1);
244
+ // const pt2 = this.getPoint(t2);
245
+
246
+ // const tangent = optionalTarget || ((pt1.isVector2) ? new Vector2() : new Vector3());
247
+
248
+ // tangent.copy(pt2).sub(pt1).normalize();
249
+
250
+ // return tangent;
251
+
252
+ // }
253
+
254
+ // getTangentAt(u, optionalTarget) {
255
+
256
+ // const t = this.getUtoTmapping(u);
257
+ // return this.getTangent(t, optionalTarget);
258
+
259
+ // }
260
+
261
+ // computeFrenetFrames(segments, closed) {
262
+
263
+ // // see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
264
+
265
+ // const normal = new Vector3();
266
+
267
+ // const tangents = [];
268
+ // const normals = [];
269
+ // const binormals = [];
270
+
271
+ // const vec = new Vector3();
272
+ // const mat = new Matrix4();
273
+
274
+ // // compute the tangent vectors for each segment on the curve
275
+
276
+ // for (let i = 0; i <= segments; i++) {
277
+
278
+ // const u = i / segments;
279
+
280
+ // tangents[i] = this.getTangentAt(u, new Vector3());
281
+
282
+ // }
283
+
284
+ // // select an initial normal vector perpendicular to the first tangent vector,
285
+ // // and in the direction of the minimum tangent xyz component
286
+
287
+ // normals[0] = new Vector3();
288
+ // binormals[0] = new Vector3();
289
+ // let min = Number.MAX_VALUE;
290
+ // const tx = Math.abs(tangents[0].x);
291
+ // const ty = Math.abs(tangents[0].y);
292
+ // const tz = Math.abs(tangents[0].z);
293
+
294
+ // if (tx <= min) {
295
+
296
+ // min = tx;
297
+ // normal.set(1, 0, 0);
298
+
299
+ // }
300
+
301
+ // if (ty <= min) {
302
+
303
+ // min = ty;
304
+ // normal.set(0, 1, 0);
305
+
306
+ // }
307
+
308
+ // if (tz <= min) {
309
+
310
+ // normal.set(0, 0, 1);
311
+
312
+ // }
313
+
314
+ // vec.crossVectors(tangents[0], normal).normalize();
315
+
316
+ // normals[0].crossVectors(tangents[0], vec);
317
+ // binormals[0].crossVectors(tangents[0], normals[0]);
318
+
319
+ // // compute the slowly-varying normal and binormal vectors for each segment on the curve
320
+
321
+ // for (let i = 1; i <= segments; i++) {
322
+
323
+ // normals[i] = normals[i - 1].clone();
324
+
325
+ // binormals[i] = binormals[i - 1].clone();
326
+
327
+ // vec.crossVectors(tangents[i - 1], tangents[i]);
328
+
329
+ // if (vec.length() > Number.EPSILON) {
330
+
331
+ // vec.normalize();
332
+
333
+ // const theta = Math.acos(MathUtils.clamp(tangents[i - 1].dot(tangents[i]), - 1, 1)); // clamp for floating pt errors
334
+
335
+ // normals[i].applyMatrix4(mat.makeRotationAxis(vec, theta));
336
+
337
+ // }
338
+
339
+ // binormals[i].crossVectors(tangents[i], normals[i]);
340
+
341
+ // }
342
+
343
+ // // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
344
+
345
+ // if (closed === true) {
346
+
347
+ // let theta = Math.acos(MathUtils.clamp(normals[0].dot(normals[segments]), - 1, 1));
348
+ // theta /= segments;
349
+
350
+ // if (tangents[0].dot(vec.crossVectors(normals[0], normals[segments])) > 0) {
351
+
352
+ // theta = - theta;
353
+
354
+ // }
355
+
356
+ // for (let i = 1; i <= segments; i++) {
357
+
358
+ // // twist a little...
359
+ // normals[i].applyMatrix4(mat.makeRotationAxis(tangents[i], theta * i));
360
+ // binormals[i].crossVectors(tangents[i], normals[i]);
361
+
362
+ // }
363
+
364
+ // }
365
+
366
+ // return {
367
+ // tangents: tangents,
368
+ // normals: normals,
369
+ // binormals: binormals
370
+ // };
371
+
372
+ // }
373
+
374
+ // clone() {
375
+
376
+ // return new this.constructor().copy(this);
377
+
378
+ // }
379
+
380
+ // copy(source) {
381
+
382
+ // this.arcLengthDivisions = source.arcLengthDivisions;
383
+
384
+ // return this;
385
+
386
+ // }
387
+
388
+ // toJSON() {
389
+
390
+ // const data = {
391
+ // metadata: {
392
+ // version: 4.6,
393
+ // type: 'Curve',
394
+ // generator: 'Curve.toJSON'
395
+ // }
396
+ // };
397
+
398
+ // data.arcLengthDivisions = this.arcLengthDivisions;
399
+ // data.type = this.type;
400
+
401
+ // return data;
402
+
403
+ // }
404
+
405
+ // fromJSON(json) {
406
+
407
+ // this.arcLengthDivisions = json.arcLengthDivisions;
408
+
409
+ // return this;
410
+
411
+ // }
412
+
413
+ }
414
+
415
+ export { Curve };