Skip to content

Commit daab77c

Browse files
author
Kyle Stemen
committed
Fix Walnut intersection of two meshes
1 parent 88b4cba commit daab77c

File tree

6 files changed

+5269
-102445
lines changed

6 files changed

+5269
-102445
lines changed

packages/modeling/dist/jscad-modeling.min.js

Lines changed: 1049 additions & 18367 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/modeling/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
},
6464
"gitHead": "a90b9bad95a417661c619dc733e62c587dc71a4a",
6565
"dependencies": {
66-
"@bluelightning32/walnut": "^1.0.0"
66+
"@bluelightning32/walnut": "^1.0.1"
6767
}
6868
}

packages/modeling/src/operations/booleans/intersectGeom3.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const walnut = require('./walnut')
1414
*/
1515
const intersect = (...geometries) => {
1616
geometries = flatten(geometries)
17-
console.log('walnut is', walnut.walnut)
1817
if (walnut.walnut !== null) {
1918
fromWalnut = walnut.intersect(geometries[0], geometries[1])
2019
return fromWalnut

packages/modeling/src/operations/booleans/walnut.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ const walnutInit = require('@bluelightning32/walnut')
55

66
const PRECISION = -10
77

8-
console.log('jscad walnut imported')
9-
108
function init(wasmPath, callback) {
119
if (module.exports.walnut !== null) {
1210
console.log('walnut already initialized')
1311
callback()
1412
return
1513
}
16-
console.log('walnut init starting')
14+
console.log('walnut initializing')
1715
function findWasm(file, scriptDirectory) {
18-
console.log('findWasm', wasmPath)
1916
return wasmPath
2017
}
2118

@@ -46,7 +43,7 @@ function toWalnut(obj){
4643
obj.polygons.forEach(p=>vertexCount += p.length)
4744
let mesh = module.exports.walnut._AllocateMesh(vertexCount)
4845

49-
polyBuffer = w_AllocateFloatVertexArray(4096)
46+
const polyBuffer = w_AllocateFloatVertexArray(4096)
5047
tmpBuffer = module.exports.walnut._AllocateTempVertexBuffer()
5148

5249
obj.polygons.forEach(p=>{
@@ -73,6 +70,9 @@ function toWalnutTransformed(obj){
7370
obj.polygons.forEach(p=>vertexCount += p.length)
7471
let mesh = module.exports.walnut._AllocateMesh(vertexCount)
7572

73+
const polyBuffer = w_AllocateFloatVertexArray(4096)
74+
tmpBuffer = module.exports.walnut._AllocateTempVertexBuffer()
75+
7676
let v = [0,0,0]
7777
obj.polygons.forEach(p=>{
7878
if(p.vertices) p = p.vertices // jscad format
@@ -84,52 +84,75 @@ function toWalnutTransformed(obj){
8484
})
8585
module.exports.walnut._AddFloatPolygonToMesh(count/3, polyBuffer.vertexPointer, tmpBuffer, mesh, PRECISION)
8686
})
87+
88+
module.exports.walnut._FreeTempVertexBuffer(tmpBuffer)
89+
polyBuffer.free()
90+
8791
mesh.vertexCount = vertexCount
8892
return mesh
8993
}
9094

9195
function toGeom(mesh){
92-
let triangleCount = module.exports.walnut._GetTriangleCountInMesh(mesh)
93-
let vertexCount = triangleCount * 3
94-
let vertexPointer = module.exports.walnut._AllocateFloatVertexArray(vertexCount)
95-
module.exports.walnut._GetFloatTrianglesFromMesh(mesh, vertexPointer)
96-
97-
let vertices = new Float32Array(vertexCount * 3)
98-
vertices.set(module.exports.walnut.HEAPF32.subarray(vertexPointer/4, vertexPointer/4 + vertexCount*3))
99-
let indices = new Uint16Array(vertexCount * 3)
100-
for (let i = 0; i < vertexCount; ++i) {
101-
indices[i] = i
102-
}
103-
module.exports.walnut._FreeFloatVertexArray(vertexPointer)
104-
return {indices, vertices, type: 'mesh'}
96+
const combined = module.exports.walnut._GetDoublePolygonArrayFromMesh(mesh, 0)
97+
98+
const combinedFields = module.exports.walnut.HEAPU32.subarray(combined/4, combined/4 + 4)
99+
const polygonCount = combinedFields[0]
100+
const planes = module.exports.walnut.HEAPF64.subarray(combinedFields[1]/8, combinedFields[1]/8 + polygonCount*4)
101+
const vertexCounts = module.exports.walnut.HEAPU32.subarray(combinedFields[2]/4, combinedFields[2]/4 + polygonCount)
102+
103+
let totalVertexCount = 0
104+
for (let i = 0; i < polygonCount; ++i) {
105+
totalVertexCount += vertexCounts[i]
106+
}
107+
108+
const vertices = module.exports.walnut.HEAPF64.subarray(combinedFields[3]/8, combinedFields[3]/8 + 3*totalVertexCount)
109+
110+
const polygons = new Array(polygonCount)
111+
let vertexIndex = 0
112+
for (let i = 0; i < polygonCount; ++i) {
113+
const outputVertices = new Array(vertexCounts[i])
114+
for (let j = 0; j < vertexCounts[i]; ++j, vertexIndex += 3) {
115+
outputVertices[j] = [
116+
vertices[vertexIndex + 0],
117+
vertices[vertexIndex + 1],
118+
vertices[vertexIndex + 2],
119+
]
120+
}
121+
const plane = [
122+
planes[i*4 + 0],
123+
planes[i*4 + 1],
124+
planes[i*4 + 2],
125+
planes[i*4 + 3]
126+
]
127+
polygons[i] = {
128+
vertices: outputVertices,
129+
plane: plane
130+
}
131+
}
132+
identity = new Array(16)
133+
mat4.identity(identity)
134+
return {
135+
polygons: polygons,
136+
transforms: identity
137+
}
105138
}
106139

107140
function intersect(geom1, geom2){
108-
console.log('intersect', geom1, geom2);
109-
let time = Date.now()
110141
let wMesh1 = toWalnut(geom1)
111-
console.log('Mesh1 transfered to walnut ', Date.now() - time);
112-
time = Date.now()
113142
let wMesh2 = toWalnut(geom2)
114-
console.log('Mesh2 transfered to walnut ', Date.now() - time);
115143

116144
let wResult = module.exports.walnut._AllocateMesh(wMesh1.vertexCount)
117145

118146
let filterSuccess = module.exports.walnut._IntersectMeshes(wMesh1, wMesh2, wResult)
119-
console.log('intersect ', Date.now() - time);
120-
time = Date.now()
121147

122148
let result = toGeom(wResult)
123149

124150
module.exports.walnut._FreeMesh(wMesh1)
125151
module.exports.walnut._FreeMesh(wMesh2)
126152
module.exports.walnut._FreeMesh(wResult)
127153

128-
result.color = geom1.color
129154
result.transforms = Array(16)
130155
mat4.identity(result.transforms)
131-
console.log('filter result', filterSuccess, result)
132-
console.log('toGeom ', Date.now() - time);
133156
return result
134157
}
135158

0 commit comments

Comments
 (0)