@jgphilpott/polytree 0.0.9 → 0.1.1

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/README.md CHANGED
@@ -2,25 +2,59 @@
2
2
  <img width="321" height="321" src="https://raw.githubusercontent.com/jgphilpott/polytree/polytree/icon.png" alt="Polytree Icon">
3
3
  </p>
4
4
 
5
+ <p align="center">
6
+ <a href="https://github.com/jgphilpott/polytree/actions"><img src="https://github.com/jgphilpott/polytree/actions/workflows/nodejs.yml/badge.svg" alt="Polytree Tests"></a>
7
+ <a href="https://badge.fury.io/js/@jgphilpott%2Fpolytree"><img src="https://badge.fury.io/js/@jgphilpott%2Fpolytree.svg" alt="npm version"></a>
8
+ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License: MIT"></a>
9
+ </p>
10
+
5
11
  # Polytree
6
12
 
7
13
  <details open>
8
14
 
9
- <summary><h2 style="display:inline">Intro</h2></summary><br>
15
+ <summary><h2 style="display:inline">Intro</h2></summary>
16
+
17
+ **Polytree** is a modern, high-performance spatial querying and Constructive Solid Geometry (CSG) library for JavaScript and Node.js Built on an efficient [Octree data structure](https://en.wikipedia.org/wiki/Octree), it is designed for advanced 3D modeling, mesh analysis, geometric search, and seamless integration with [three.js](https://github.com/mrdoob/three.js).
18
+
19
+ Polytree goes beyond traditional CSG libraries by providing a comprehensive suite of spatial query functions—such as closest point, distance field, intersection testing, layer slicing, and volume analysis—making it ideal for 3D printing, CAD, simulation, and mesh analysis applications.
10
20
 
11
- **Polytree** is a modern, high-performance Constructive Solid Geometry (CSG) library for JavaScript and Node.js, built to utilize the efficiencies of Octree data structure. It is designed for robust 3D modeling, spatial queries, and seamless integration with [three.js](https://github.com/mrdoob/three.js).
21
+ **▶️ [View the Polytree Demo Site with GitHub Pages](https://jgphilpott.github.io/polytree)**
22
+
23
+ **📦 [View the Polytree npm Package](https://www.npmjs.com/package/@jgphilpott/polytree)**
12
24
 
13
25
  ### Features
14
26
 
27
+ - **Advanced Spatial Queries**: Closest point search, distance calculations, intersection testing, layer slicing, and volume analysis—optimized for mesh analysis, 3D printing, and simulation workflows.
15
28
  - **Complete CSG Operations**: Union, subtraction, and intersection with full test coverage.
16
- - **High Performance**: Optimized Octree-based spatial partitioning for fast operations.
17
- - **Dual API**: Both synchronous and asynchronous operation modes.
29
+ - **3D Printing & CAD Support**: Layer slicing, cross-section analysis, and spatial operations for manufacturing and design applications.
30
+ - **High Performance**: Octree-based spatial partitioning for fast queries and operations on large meshes.
31
+ - **Dual API**: Both synchronous and asynchronous operation modes for flexible integration.
32
+ - **Multi-Input Support**: Works directly with Three.js meshes, BufferGeometry, and Polytree instances.
18
33
  - **Lightweight**: Minimal dependencies with efficient memory usage.
19
- - **Three.js Integration**: Direct mesh-to-mesh operations with material preservation.
20
- - **Well Documented**: Comprehensive API documentation and examples.
21
- - **Robust Testing**: 450+ tests ensuring reliability across edge cases.
34
+ - **Three.js Integration**: Direct mesh-to-mesh operations and spatial queries with material preservation.
35
+ - **Well Documented**: Comprehensive API documentation and interactive examples.
36
+ - **Robust Testing**: 500+ tests ensuring reliability across edge cases.
37
+
38
+ </details>
39
+
40
+ <details open>
41
+
42
+ <summary><h2 style="display:inline">Table of Contents</h2></summary>
43
+
44
+ - [Intro](#intro)
45
+ - [Getting Started](#getting-started)
46
+ - [Usage](#usage)
47
+ - [Utility Functions](#utility-functions)
48
+ - [Basic CSG Operations](#basic-csg-operations)
49
+ - [Async CSG Operations](#async-csg-operations)
50
+ - [Async Array Operations](#async-array-operations)
51
+ - [Spatial Query Functions](#spatial-query-functions)
52
+ - [Advanced Polytree-to-Polytree Operations](#advanced-polytree-to-polytree-operations)
53
+ - [Performance](#performance)
54
+ - [Applications](#applications)
55
+ - [Contributing](#contributing)
22
56
 
23
- </details><br>
57
+ </details>
24
58
 
25
59
  <details open>
26
60
 
@@ -63,15 +97,76 @@ import Polytree from 'polytree';
63
97
 
64
98
  The browser bundle (`polytree.bundle.browser.js`) is specifically designed for ES module imports in browsers, while the main bundle (`polytree.bundle.js`) is for Node.js environments.
65
99
 
66
- </details><br>
100
+ </details>
67
101
 
68
102
  <details open>
69
103
 
70
- <summary><h2 style="display:inline">Usage</h2></summary><br>
104
+ <summary><h2 style="display:inline">Usage</h2></summary>
105
+
106
+ <details open>
107
+
108
+ <summary><h3 style="display:inline">Utility Functions</h3></summary>
109
+
110
+ Polytree provides utility functions for geometry analysis and calculations:
111
+
112
+ #### Surface Area Calculation
113
+
114
+ Calculate the surface area of Three.js meshes or geometries:
115
+
116
+ ```js
117
+ // Calculate surface area from a mesh:
118
+ const boxGeometry = new THREE.BoxGeometry(2, 3, 4);
119
+ const boxMesh = new THREE.Mesh(boxGeometry, new THREE.MeshBasicMaterial());
120
+ const surfaceArea = Polytree.getSurface(boxMesh);
121
+
122
+ console.log(`Surface area: ${surfaceArea}`); // Output: 52
123
+
124
+ // Calculate surface area directly from geometry:
125
+ const sphereGeometry = new THREE.SphereGeometry(1);
126
+ const sphereSurfaceArea = Polytree.getSurface(sphereGeometry);
127
+
128
+ console.log(`Sphere surface area: ${sphereSurfaceArea}`); // Output: ~12.47
129
+ ```
130
+
131
+ The `getSurface()` method:
132
+
133
+ - Accepts either Three.js `Mesh` objects or `BufferGeometry` objects.
134
+ - Returns the total surface area as a number.
135
+ - Works by summing the areas of all triangular faces.
136
+ - Handles both indexed and non-indexed geometries.
137
+
138
+ #### Volume Calculation
139
+
140
+ Calculate the volume of Three.js meshes or geometries:
141
+
142
+ ```js
143
+ // Calculate volume from a mesh:
144
+ const boxGeometry = new THREE.BoxGeometry(2, 2, 2);
145
+ const boxMesh = new THREE.Mesh(boxGeometry, new THREE.MeshBasicMaterial());
146
+ const volume = Polytree.getVolume(boxMesh);
147
+
148
+ console.log(`Volume: ${volume}`); // Output: 8
149
+
150
+ // Calculate volume directly from geometry:
151
+ const sphereGeometry = new THREE.SphereGeometry(1);
152
+ const sphereVolume = Polytree.getVolume(sphereGeometry);
153
+
154
+ console.log(`Sphere volume: ${sphereVolume}`); // Output: ~4.19 (approx 4/3 * π)
155
+ ```
156
+
157
+ The `getVolume()` method:
158
+
159
+ - Accepts either Three.js `Mesh` objects or `BufferGeometry` objects.
160
+ - Returns the total volume as a number.
161
+ - Uses the divergence theorem with signed tetrahedron volumes.
162
+ - Handles both indexed and non-indexed geometries.
163
+ - Automatically handles edge cases like empty geometries.
164
+
165
+ </details>
71
166
 
72
167
  <details open>
73
168
 
74
- <summary><h3 style="display:inline">Basic CSG Operations</h3></summary><br>
169
+ <summary><h3 style="display:inline">Basic CSG Operations</h3></summary>
75
170
 
76
171
  Polytree provides three core CSG operations that work directly with Three.js meshes:
77
172
 
@@ -139,11 +234,11 @@ const result = await Polytree.intersect(sphere1, sphere2);
139
234
  scene.add(result);
140
235
  ```
141
236
 
142
- </details><br>
237
+ </details>
143
238
 
144
239
  <details>
145
240
 
146
- <summary><h3 style="display:inline">Asynchronous Operations</h3></summary><br>
241
+ <summary><h3 style="display:inline">Async CSG Operations</h3></summary>
147
242
 
148
243
  For better performance in web applications, use async operations to prevent UI blocking:
149
244
 
@@ -159,11 +254,146 @@ const unionResult = await Polytree.unite(mesh1, mesh2);
159
254
  scene.add(unionResult);
160
255
  ```
161
256
 
162
- </details><br>
257
+ </details>
258
+
259
+ <details>
260
+
261
+ <summary><h3 style="display:inline">Async Array Operations</h3></summary>
262
+
263
+ Process multiple objects efficiently:
264
+
265
+ ```js
266
+ // Unite multiple objects asynchronously.
267
+ const meshArray = [mesh1, mesh2, mesh3, mesh4 ... meshX];
268
+ const polytreeArray = meshArray.map(mesh => Polytree.fromMesh(mesh));
269
+
270
+ Polytree.async.uniteArray(polytreeArray).then(result => {
271
+
272
+ const finalMesh = Polytree.toMesh(result);
273
+ scene.add(finalMesh);
274
+
275
+ // Clean up.
276
+ polytreeArray.forEach(polytree => {
277
+ polytree.delete()
278
+ });
279
+
280
+ result.delete();
281
+
282
+ });
283
+ ```
284
+
285
+ </details>
286
+
287
+ <details open>
288
+
289
+ <summary><h3 style="display:inline">Spatial Query Functions</h3></summary>
290
+
291
+ Polytree now includes advanced spatial query capabilities inspired by the `three-mesh-bvh` library, transforming it from a pure CSG library into a comprehensive spatial querying tool optimized for 3D printing applications.
292
+
293
+ #### Point-based Queries
294
+
295
+ Find the closest points and calculate distances for collision detection and mesh analysis:
296
+
297
+ ```js
298
+ const geometry = new THREE.BoxGeometry(2, 2, 2);
299
+ const mesh = new THREE.Mesh(geometry, material);
300
+ const testPoint = new THREE.Vector3(5, 0, 0);
301
+
302
+ // Find closest point on surface - works with Mesh, BufferGeometry, or Polytree.
303
+ const closestPoint = Polytree.closestPointToPoint(mesh, testPoint);
304
+ console.log(`Closest point: (${closestPoint.x}, ${closestPoint.y}, ${closestPoint.z})`);
305
+
306
+ // Calculate distance to surface.
307
+ const distance = Polytree.distanceToPoint(mesh, testPoint);
308
+ console.log(`Distance: ${distance} units`);
309
+ ```
310
+
311
+ #### Volume Analysis
312
+
313
+ Perform both exact and statistical volume calculations:
314
+
315
+ ```js
316
+ // Exact volume calculation.
317
+ const exactVolume = Polytree.getVolume(mesh);
318
+
319
+ // Monte Carlo volume estimation for complex geometries.
320
+ const estimatedVolume = Polytree.estimateVolumeViaSampling(mesh, 50000);
321
+ console.log(`Exact: ${exactVolume}, Estimated: ${estimatedVolume}`);
322
+ ```
323
+
324
+ #### Intersection Testing
325
+
326
+ Test intersections with bounding volumes for collision detection:
327
+
328
+ ```js
329
+ // Test sphere intersection.
330
+ const sphere = new THREE.Sphere(new THREE.Vector3(0, 0, 0), 2);
331
+ const intersectsSphere = Polytree.intersectsSphere(mesh, sphere);
332
+
333
+ // Test bounding box intersection.
334
+ const box = new THREE.Box3(min, max);
335
+ const intersectsBox = Polytree.intersectsBox(mesh, box);
336
+ ```
337
+
338
+ #### 3D Printing Layer Slicing
339
+
340
+ Generate layer slices for 3D printing applications:
341
+
342
+ ```js
343
+ // Slice geometry into horizontal layers.
344
+ const layers = Polytree.sliceIntoLayers(
345
+ mesh, // Input geometry
346
+ 0.2, // Layer height (0.2mm)
347
+ -10, // Minimum Z
348
+ 10, // Maximum Z
349
+ new THREE.Vector3(0, 0, 1) // Optional normal (default: Z-up)
350
+ );
351
+
352
+ console.log(`Generated ${layers.length} layers for 3D printing`);
353
+
354
+ // Single plane intersection for cross-section analysis.
355
+ const plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
356
+ const crossSection = Polytree.intersectPlane(mesh, plane);
357
+ ```
358
+
359
+ #### Advanced Spatial Operations
360
+
361
+ Custom spatial queries and triangle-based operations:
362
+
363
+ ```js
364
+ // Get triangles near a specific point.
365
+ const nearbyTriangles = Polytree.getTrianglesNearPoint(mesh, point, 2.0);
366
+
367
+ // Custom spatial query with callback.
368
+ const results = Polytree.shapecast(mesh, (triangle) => {
369
+ // Custom query logic - return true to collect triangle.
370
+ return triangle.normal.y > 0.8; // Find upward-facing triangles.
371
+ }, (triangle) => {
372
+ // Optional collection callback for processing results.
373
+ return { triangle, area: triangle.getArea() };
374
+ });
375
+ ```
376
+
377
+ #### Multi-Input Support
378
+
379
+ All spatial query functions accept Three.js meshes, BufferGeometry, or Polytree instances:
380
+
381
+ ```js
382
+ const geometry = new THREE.BoxGeometry(2, 2, 2);
383
+ const mesh = new THREE.Mesh(geometry, material);
384
+ const polytree = Polytree.fromMesh(mesh);
385
+
386
+ // All of these work identically.
387
+ const result1 = Polytree.closestPointToPoint(mesh, testPoint); // Mesh
388
+ const result2 = Polytree.closestPointToPoint(geometry, testPoint); // BufferGeometry
389
+ const result3 = Polytree.closestPointToPoint(polytree, testPoint); // Polytree
390
+ ```
391
+
392
+ </details>
163
393
 
164
394
  <details>
165
395
 
166
- <summary><h3 style="display:inline">Advanced: Polytree-to-Polytree Operations</h3></summary><br>
396
+ <summary><h3 style="display:inline">Advanced Polytree-to-Polytree Operations</h3></summary>
167
397
 
168
398
  For maximum performance when chaining operations, work directly with Polytree objects:
169
399
 
@@ -190,68 +420,44 @@ intermediate.delete();
190
420
  final.delete();
191
421
  ```
192
422
 
193
- </details><br>
194
-
195
- <details>
196
-
197
- <summary><h3 style="display:inline">Async Array Operations</h3></summary><br>
198
-
199
- Process multiple objects efficiently:
200
-
201
- ```js
202
- // Unite multiple objects asynchronously.
203
- const meshArray = [mesh1, mesh2, mesh3, mesh4 ... meshX];
204
- const polytreeArray = meshArray.map(mesh => Polytree.fromMesh(mesh));
205
-
206
- Polytree.async.uniteArray(polytreeArray).then(result => {
207
-
208
- const finalMesh = Polytree.toMesh(result);
209
- scene.add(finalMesh);
210
-
211
- // Clean up.
212
- polytreeArray.forEach(polytree => {
213
- polytree.delete()
214
- });
215
-
216
- result.delete();
217
-
218
- });
219
- ```
220
-
221
423
  </details>
222
424
 
223
- </details><br>
425
+ </details>
224
426
 
225
427
  <details open>
226
428
 
227
- <summary><h2 style="display:inline">Performance</h2></summary><br>
429
+ <summary><h2 style="display:inline">Performance</h2></summary>
228
430
 
229
431
  Polytree is designed for high-performance CSG operations:
230
432
 
231
- - **Octree Optimization**: Spatial partitioning reduces computational complexity.
232
- - **Memory Efficient**: Smart resource management with cleanup methods.
233
- - **Comprehensive Testing**: 450+ test cases ensuring reliability and performance.
433
+ - **Octree Optimization**: Spatial partitioning reduces computational complexity for both CSG and spatial query operations.
434
+ - **Memory Efficient**: Smart resource management with cleanup methods and automatic temporary object disposal.
435
+ - **Unified Architecture**: CSG operations + spatial queries in one optimized library, eliminating the need for multiple tools.
436
+ - **Multi-Input Support**: Functions work directly with Three.js meshes, geometries, and Polytree instances without manual conversion.
437
+ - **Comprehensive Testing**: 500+ test cases ensuring reliability and performance across all operations.
234
438
  - **Async Support**: Non-blocking operations for smooth user experiences.
235
439
  - **Minimal Dependencies**: Only Three.js as a dependency for lightweight integration.
236
440
 
237
- </details><br>
441
+ </details>
238
442
 
239
443
  <details open>
240
444
 
241
- <summary><h2 style="display:inline">Applications</h2></summary><br>
445
+ <summary><h2 style="display:inline">Applications</h2></summary>
242
446
 
243
447
  - **3D Modeling**: Professional-grade boolean operations for CAD applications.
244
448
  - **Game Development**: Runtime mesh manipulation and procedural geometry.
245
- - **3D Printing**: Solid geometry preparation and mesh optimization.
246
- - **Architectural Visualization**: Complex building geometry operations.
247
- - **Educational Tools**: Interactive 3D geometry learning applications.
248
- - **Integration with [Polyslice](https://github.com/jgphilpott/polyslice)**: Advanced FDM slicing workflows.
449
+ - **3D Printing & Manufacturing**: Complete slicing pipeline with layer generation, support analysis, and volume calculations.
450
+ - **Collision Detection**: Fast spatial queries for physics engines and interactive applications.
451
+ - **Mesh Analysis & Repair**: Distance field calculations, closest point queries, and geometric validation.
452
+ - **Architectural Visualization**: Complex building geometry operations with spatial analysis.
453
+ - **Educational Tools**: Interactive 3D geometry learning applications with real-time feedback.
454
+ - **Integration with [Polyslice](https://github.com/jgphilpott/polyslice)**: Advanced FDM slicing workflows and manufacturing optimization.
249
455
 
250
- </details><br>
456
+ </details>
251
457
 
252
458
  <details open>
253
459
 
254
- <summary><h2 style="display:inline">Contributing</h2></summary><br>
460
+ <summary><h2 style="display:inline">Contributing</h2></summary>
255
461
 
256
462
  Contributions, issues, and feature requests are welcome! Please [open an issue](https://github.com/jgphilpott/polytree/issues) or submit a [pull request](https://github.com/jgphilpott/polytree/pulls).
257
463
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jgphilpott/polytree",
3
- "version": "0.0.9",
4
- "description": "A Constructive Solid Geometry (CSG) library using Octree data structure.",
3
+ "version": "0.1.1",
4
+ "description": "A modern, high-performance spatial querying library designed specifically for three.js and Node.",
5
5
  "funding": "https://github.com/sponsors/jgphilpott",
6
6
  "main": "polytree.bundle.js",
7
7
  "exports": {
@@ -45,7 +45,7 @@
45
45
  "homepage": "https://github.com/jgphilpott/polytree#readme",
46
46
  "devDependencies": {
47
47
  "coffeescript": "^2.7.0",
48
- "jest": "^30.1.3",
48
+ "jest": "^30.2.0",
49
49
  "uglify-js": "^3.19.3"
50
50
  },
51
51
  "dependencies": {