@jgphilpott/polytree 0.0.7 → 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 CHANGED
@@ -1,34 +1,262 @@
1
1
  <p align="center">
2
- <img width="320" height="320" src="https://raw.githubusercontent.com/jgphilpott/polytree/polytree/icon.png" alt="Polytree Logo">
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
- # Intro
5
+ # Polytree
6
6
 
7
- **Polytree** is a modern, modular 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).
7
+ <details open>
8
8
 
9
- ## Getting Started
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>
28
+
29
+ ### Node.js
30
+
31
+ #### Install
10
32
 
11
33
  ```bash
12
34
  npm install polytree
13
35
  ```
14
36
 
37
+ #### Import
38
+
15
39
  ```js
16
40
  import * as THREE from 'three';
17
41
  import { Polytree } from 'polytree';
42
+ ```
43
+
44
+ ### Browser
45
+
46
+ For browser usage, use the ES module-compatible bundle:
18
47
 
19
- // Example usage coming soon!
48
+ ```html
49
+ <script type="importmap">
50
+ {
51
+ "imports": {
52
+ "three": "./path/to/three.module.min.js",
53
+ "polytree": "./path/to/polytree.bundle.browser.js"
54
+ }
55
+ }
56
+ </script>
57
+
58
+ <script type="module">
59
+ import * as THREE from 'three';
60
+ import Polytree from 'polytree';
61
+ </script>
20
62
  ```
21
63
 
22
- ## Applications
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.
65
+
66
+ </details><br>
67
+
68
+ <details open>
69
+
70
+ <summary><h2 style="display:inline">Usage</h2></summary><br>
71
+
72
+ <details open>
23
73
 
24
- - 3D modeling and design for 3D printing.
25
- - Integration with [Polyslice](https://github.com/jgphilpott/polyslice) FDM slicer.
26
- - General-purpose spatial querying and mesh manipulation.
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
+ ```
27
141
 
28
- ## Contributing
142
+ </details><br>
143
+
144
+ <details>
145
+
146
+ <summary><h3 style="display:inline">Asynchronous Operations</h3></summary><br>
147
+
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>
29
255
 
30
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).
31
257
 
258
+ </details>
259
+
32
260
  ---
33
261
 
34
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.7",
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",
@@ -10,19 +10,27 @@
10
10
  }
11
11
  },
12
12
  "files": [
13
- "polytree.bundle.js",
13
+ "LICENSE",
14
14
  "README.md",
15
- "LICENSE"
15
+ "polytree.bundle.js",
16
+ "polytree.bundle.min.js",
17
+ "polytree.bundle.browser.js",
18
+ "polytree.bundle.browser.min.js"
16
19
  ],
17
20
  "directories": {
18
21
  "example": "examples"
19
22
  },
20
23
  "scripts": {
21
24
  "test": "jest",
22
- "pretest": "npm run compile",
25
+ "pretest": "npm run build",
23
26
  "compile": "coffee -cb .",
24
27
  "compile:watch": "coffee -cbw . &",
25
- "build": "npm run compile && cat app/polytree.js app/triangle.intersection.js > polytree.bundle.js",
28
+ "build": "npm run compile && npm run build:node && npm run build:browser && npm run minify",
29
+ "build:node": "./scripts/bundle.sh",
30
+ "build:browser": "node scripts/browserify.cjs",
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",
26
34
  "prepare": "npm run build",
27
35
  "prepublishOnly": "npm run build"
28
36
  },
@@ -30,14 +38,15 @@
30
38
  "type": "git",
31
39
  "url": "https://github.com/jgphilpott/polytree"
32
40
  },
33
- "keywords": ["Polytree","Octree","CSG","BVH"],
41
+ "keywords": ["Polytree", "Octree", "CSG", "BVH"],
34
42
  "author": { "name": "Jacob Philpott", "email": "jacob.philpott@gmx.com" },
35
43
  "license": "MIT",
36
44
  "bugs": { "url": "https://github.com/jgphilpott/polytree/issues" },
37
45
  "homepage": "https://github.com/jgphilpott/polytree#readme",
38
46
  "devDependencies": {
39
47
  "coffeescript": "^2.7.0",
40
- "jest": "^30.1.3"
48
+ "jest": "^30.1.3",
49
+ "uglify-js": "^3.19.3"
41
50
  },
42
51
  "dependencies": {
43
52
  "three": "^0.180.0"