fix triangulation of csg union

This commit is contained in:
Stewart Allen 2022-02-26 19:11:59 -05:00
commit 563694a2f1
2 changed files with 26 additions and 8 deletions

View file

@ -410,6 +410,7 @@ function orderClosest(array, fn, from) {
// an unwrapped array in the original dimensions // an unwrapped array in the original dimensions
function triangulate(array, holes, dims) { function triangulate(array, holes, dims) {
let narray; let narray;
let info;
if (dims === 2) { if (dims === 2) {
narray = array; narray = array;
} else { } else {
@ -434,6 +435,7 @@ function triangulate(array, holes, dims) {
d1 = i; d1 = i;
} }
} }
info = { d1, d2, dmax, delta: delta.slice() };
delta[d1] = dmax = 0; delta[d1] = dmax = 0;
for (let i = 0; i < dims; i++) { for (let i = 0; i < dims; i++) {
if (delta[i] > dmax) { if (delta[i] > dmax) {
@ -455,6 +457,9 @@ function triangulate(array, holes, dims) {
oa[e++] = array[ai + j]; oa[e++] = array[ai + j];
} }
} }
if (oa.length === 0) {
console.log('debug_triangulate', {array, oa, info});
}
return oa; return oa;
} }

View file

@ -13,11 +13,13 @@ const CSG = {
union() { union() {
if (debug) { if (debug) {
for (let p of [ ...arguments ].map(a => a.polygons.map(p => p.vertices))) { console.log(JSON.stringify([ ...arguments ]));
console.log(JSON.stringify(p));
}
} }
return jscadModeling.booleans.union(...arguments); let union = jscadModeling.booleans.union(...arguments);
if (debug) {
console.log(JSON.stringify(union));
}
return union;
}, },
subtract() { subtract() {
@ -29,10 +31,21 @@ const CSG = {
}, },
toPositionArray(geom) { toPositionArray(geom) {
return geom.polygons const out = [];
.map(p => p.vertices.flat()) const poly = jscadModeling.geometries.geom3
.map(a => base.util.triangulate(a, undefined, 3)) .toPolygons(geom)
.flat().toFloat32(); .map(p => p.vertices);
for (let p of poly) {
let p0 = p[0];
for (let i = 0; i<p.length - 2; i++) {
out.push(p0, p[i+1], p[i+2]);
}
}
return out.flat().toFloat32();
// return geom.polygons
// .map(p => p.vertices.flat())
// .map(a => base.util.triangulate(a, undefined, 3))
// .flat().toFloat32();
}, },
// Construct a CSG from a THREE Geometry BufferAttribute array (or similar) // Construct a CSG from a THREE Geometry BufferAttribute array (or similar)