@jgphilpott/polytree 0.0.8 → 0.0.9
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 +214 -16
- package/package.json +6 -5
- package/polytree.bundle.browser.js +3074 -2529
- package/polytree.bundle.browser.min.js +1 -1
- package/polytree.bundle.js +3431 -2670
- package/polytree.bundle.min.js +1 -1
package/README.md
CHANGED
|
@@ -1,28 +1,44 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img width="
|
|
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
|
-
#
|
|
5
|
+
# Polytree
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<details open>
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
<summary><h2 style="display:inline">Intro</h2></summary><br>
|
|
10
|
+
|
|
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).
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
- **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.
|
|
18
|
+
- **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.
|
|
22
|
+
|
|
23
|
+
</details><br>
|
|
24
|
+
|
|
25
|
+
<details open>
|
|
26
|
+
|
|
27
|
+
<summary><h2 style="display:inline">Getting Started</h2></summary>
|
|
10
28
|
|
|
11
29
|
### Node.js
|
|
12
30
|
|
|
13
|
-
|
|
31
|
+
#### Install
|
|
14
32
|
|
|
15
33
|
```bash
|
|
16
34
|
npm install polytree
|
|
17
35
|
```
|
|
18
36
|
|
|
19
|
-
|
|
37
|
+
#### Import
|
|
20
38
|
|
|
21
39
|
```js
|
|
22
40
|
import * as THREE from 'three';
|
|
23
41
|
import { Polytree } from 'polytree';
|
|
24
|
-
|
|
25
|
-
// Example usage coming soon!
|
|
26
42
|
```
|
|
27
43
|
|
|
28
44
|
### Browser
|
|
@@ -42,23 +58,205 @@ For browser usage, use the ES module-compatible bundle:
|
|
|
42
58
|
<script type="module">
|
|
43
59
|
import * as THREE from 'three';
|
|
44
60
|
import Polytree from 'polytree';
|
|
45
|
-
|
|
46
|
-
// Example usage coming soon!
|
|
47
61
|
</script>
|
|
48
62
|
```
|
|
49
63
|
|
|
50
64
|
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.
|
|
51
65
|
|
|
52
|
-
|
|
66
|
+
</details><br>
|
|
67
|
+
|
|
68
|
+
<details open>
|
|
69
|
+
|
|
70
|
+
<summary><h2 style="display:inline">Usage</h2></summary><br>
|
|
71
|
+
|
|
72
|
+
<details open>
|
|
73
|
+
|
|
74
|
+
<summary><h3 style="display:inline">Basic CSG Operations</h3></summary><br>
|
|
75
|
+
|
|
76
|
+
Polytree provides three core CSG operations that work directly with Three.js meshes:
|
|
77
|
+
|
|
78
|
+
#### Unite (Join)
|
|
79
|
+
|
|
80
|
+
Join two 3D objects into a single merged object:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
// Create two identical boxes.
|
|
84
|
+
const geometry1 = new THREE.BoxGeometry(2, 2, 2);
|
|
85
|
+
const geometry2 = new THREE.BoxGeometry(2, 2, 2);
|
|
86
|
+
|
|
87
|
+
const mesh1 = new THREE.Mesh(geometry1, new THREE.MeshBasicMaterial());
|
|
88
|
+
const mesh2 = new THREE.Mesh(geometry2, new THREE.MeshBasicMaterial());
|
|
89
|
+
|
|
90
|
+
// Offset the position of one box.
|
|
91
|
+
mesh1.position.set(1, 1, 1);
|
|
92
|
+
|
|
93
|
+
// Join the boxes together.
|
|
94
|
+
const result = await Polytree.unite(mesh1, mesh2);
|
|
95
|
+
|
|
96
|
+
scene.add(result);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Subtract (Remove)
|
|
100
|
+
|
|
101
|
+
Remove one object's volume from another:
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
// Create a box and a sphere.
|
|
105
|
+
const boxGeometry = new THREE.BoxGeometry(2, 2, 2);
|
|
106
|
+
const sphereGeometry = new THREE.SphereGeometry(1.5);
|
|
107
|
+
|
|
108
|
+
const boxMesh = new THREE.Mesh(boxGeometry, new THREE.MeshBasicMaterial());
|
|
109
|
+
const sphereMesh = new THREE.Mesh(sphereGeometry, new THREE.MeshBasicMaterial());
|
|
110
|
+
|
|
111
|
+
// Remove the sphere from the box (creates a cavity).
|
|
112
|
+
const result = await Polytree.subtract(boxMesh, sphereMesh);
|
|
113
|
+
|
|
114
|
+
scene.add(result);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Intersect (Overlap)
|
|
118
|
+
|
|
119
|
+
Keep only the overlapping volume of two objects:
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
// Create two overlapping spheres.
|
|
123
|
+
const sphere1 = new THREE.Mesh(
|
|
124
|
+
new THREE.SphereGeometry(1),
|
|
125
|
+
new THREE.MeshBasicMaterial()
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const sphere2 = new THREE.Mesh(
|
|
129
|
+
new THREE.SphereGeometry(1),
|
|
130
|
+
new THREE.MeshBasicMaterial()
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// Offset the position of one sphere.
|
|
134
|
+
sphere1.position.set(1, 0, 0);
|
|
135
|
+
|
|
136
|
+
// Keep only the overlapping volume.
|
|
137
|
+
const result = await Polytree.intersect(sphere1, sphere2);
|
|
138
|
+
|
|
139
|
+
scene.add(result);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
</details><br>
|
|
143
|
+
|
|
144
|
+
<details>
|
|
53
145
|
|
|
54
|
-
|
|
55
|
-
- Integration with [Polyslice](https://github.com/jgphilpott/polyslice) FDM slicer.
|
|
56
|
-
- General-purpose spatial querying and mesh manipulation.
|
|
146
|
+
<summary><h3 style="display:inline">Asynchronous Operations</h3></summary><br>
|
|
57
147
|
|
|
58
|
-
|
|
148
|
+
For better performance in web applications, use async operations to prevent UI blocking:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
// Async union with Promise.
|
|
152
|
+
const unionPromise = Polytree.unite(mesh1, mesh2);
|
|
153
|
+
unionPromise.then(result => {
|
|
154
|
+
scene.add(result);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Async with await.
|
|
158
|
+
const unionResult = await Polytree.unite(mesh1, mesh2);
|
|
159
|
+
scene.add(unionResult);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
</details><br>
|
|
163
|
+
|
|
164
|
+
<details>
|
|
165
|
+
|
|
166
|
+
<summary><h3 style="display:inline">Advanced: Polytree-to-Polytree Operations</h3></summary><br>
|
|
167
|
+
|
|
168
|
+
For maximum performance when chaining operations, work directly with Polytree objects:
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
// Convert meshes to polytrees once.
|
|
172
|
+
const polytree1 = Polytree.fromMesh(mesh1);
|
|
173
|
+
const polytree2 = Polytree.fromMesh(mesh2);
|
|
174
|
+
const polytree3 = Polytree.fromMesh(mesh3);
|
|
175
|
+
|
|
176
|
+
// Chain operations efficiently.
|
|
177
|
+
const intermediate = await Polytree.unite(polytree1, polytree2);
|
|
178
|
+
const final = await Polytree.subtract(intermediate, polytree3);
|
|
179
|
+
|
|
180
|
+
// Convert back to mesh for rendering.
|
|
181
|
+
const finalMesh = Polytree.toMesh(final);
|
|
182
|
+
|
|
183
|
+
scene.add(finalMesh);
|
|
184
|
+
|
|
185
|
+
// Clean up resources.
|
|
186
|
+
polytree1.delete();
|
|
187
|
+
polytree2.delete();
|
|
188
|
+
polytree3.delete();
|
|
189
|
+
intermediate.delete();
|
|
190
|
+
final.delete();
|
|
191
|
+
```
|
|
192
|
+
|
|
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
|
+
</details>
|
|
222
|
+
|
|
223
|
+
</details><br>
|
|
224
|
+
|
|
225
|
+
<details open>
|
|
226
|
+
|
|
227
|
+
<summary><h2 style="display:inline">Performance</h2></summary><br>
|
|
228
|
+
|
|
229
|
+
Polytree is designed for high-performance CSG operations:
|
|
230
|
+
|
|
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.
|
|
234
|
+
- **Async Support**: Non-blocking operations for smooth user experiences.
|
|
235
|
+
- **Minimal Dependencies**: Only Three.js as a dependency for lightweight integration.
|
|
236
|
+
|
|
237
|
+
</details><br>
|
|
238
|
+
|
|
239
|
+
<details open>
|
|
240
|
+
|
|
241
|
+
<summary><h2 style="display:inline">Applications</h2></summary><br>
|
|
242
|
+
|
|
243
|
+
- **3D Modeling**: Professional-grade boolean operations for CAD applications.
|
|
244
|
+
- **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.
|
|
249
|
+
|
|
250
|
+
</details><br>
|
|
251
|
+
|
|
252
|
+
<details open>
|
|
253
|
+
|
|
254
|
+
<summary><h2 style="display:inline">Contributing</h2></summary><br>
|
|
59
255
|
|
|
60
256
|
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).
|
|
61
257
|
|
|
258
|
+
</details>
|
|
259
|
+
|
|
62
260
|
---
|
|
63
261
|
|
|
64
|
-
**Polytree** is developed and maintained by [@jgphilpott](https://github.com/jgphilpott).
|
|
262
|
+
**Polytree** is developed and maintained by [@jgphilpott](https://github.com/jgphilpott).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jgphilpott/polytree",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "A Constructive Solid Geometry (CSG) library using Octree data structure.",
|
|
5
5
|
"funding": "https://github.com/sponsors/jgphilpott",
|
|
6
6
|
"main": "polytree.bundle.js",
|
|
@@ -26,9 +26,11 @@
|
|
|
26
26
|
"compile": "coffee -cb .",
|
|
27
27
|
"compile:watch": "coffee -cbw . &",
|
|
28
28
|
"build": "npm run compile && npm run build:node && npm run build:browser && npm run minify",
|
|
29
|
-
"build:node": "
|
|
29
|
+
"build:node": "./scripts/bundle.sh",
|
|
30
30
|
"build:browser": "node scripts/browserify.cjs",
|
|
31
|
-
"minify": "
|
|
31
|
+
"minify": "npm run minify:node && npm run minify:browser",
|
|
32
|
+
"minify:node": "uglifyjs polytree.bundle.js -cmo polytree.bundle.min.js",
|
|
33
|
+
"minify:browser": "uglifyjs polytree.bundle.browser.js -cmo polytree.bundle.browser.min.js",
|
|
32
34
|
"prepare": "npm run build",
|
|
33
35
|
"prepublishOnly": "npm run build"
|
|
34
36
|
},
|
|
@@ -36,13 +38,12 @@
|
|
|
36
38
|
"type": "git",
|
|
37
39
|
"url": "https://github.com/jgphilpott/polytree"
|
|
38
40
|
},
|
|
39
|
-
"keywords": ["Polytree","Octree","CSG","BVH"],
|
|
41
|
+
"keywords": ["Polytree", "Octree", "CSG", "BVH"],
|
|
40
42
|
"author": { "name": "Jacob Philpott", "email": "jacob.philpott@gmx.com" },
|
|
41
43
|
"license": "MIT",
|
|
42
44
|
"bugs": { "url": "https://github.com/jgphilpott/polytree/issues" },
|
|
43
45
|
"homepage": "https://github.com/jgphilpott/polytree#readme",
|
|
44
46
|
"devDependencies": {
|
|
45
|
-
"browserify": "^17.0.1",
|
|
46
47
|
"coffeescript": "^2.7.0",
|
|
47
48
|
"jest": "^30.1.3",
|
|
48
49
|
"uglify-js": "^3.19.3"
|