2022-02-17 15:04:36 -05:00
|
|
|
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
2022-01-29 20:37:45 -05:00
|
|
|
|
|
|
|
|
// dep: geo.base
|
2022-02-26 12:42:06 -05:00
|
|
|
// jscad dep injected in app so it comes before everything else
|
2022-02-20 14:31:27 -05:00
|
|
|
gapp.register("geo.csg", [], (root, exports) => {
|
|
|
|
|
|
|
|
|
|
const { base } = root;
|
2022-02-26 12:42:06 -05:00
|
|
|
const debug = false;
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-02-20 14:31:27 -05:00
|
|
|
const CSG = {
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
union() {
|
2022-02-26 12:42:06 -05:00
|
|
|
if (debug) {
|
2022-02-26 19:11:59 -05:00
|
|
|
console.log(JSON.stringify([ ...arguments ]));
|
|
|
|
|
}
|
|
|
|
|
let union = jscadModeling.booleans.union(...arguments);
|
|
|
|
|
if (debug) {
|
|
|
|
|
console.log(JSON.stringify(union));
|
2022-02-26 12:42:06 -05:00
|
|
|
}
|
2022-02-26 19:11:59 -05:00
|
|
|
return union;
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
subtract() {
|
|
|
|
|
return jscadModeling.booleans.subtract(...arguments);
|
|
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
intersect() {
|
|
|
|
|
return jscadModeling.booleans.intersect(...arguments);
|
|
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
toPositionArray(geom) {
|
2022-04-20 17:26:01 -04:00
|
|
|
if (!geom || geom.length === 0) {
|
|
|
|
|
return [].toFloat32();
|
|
|
|
|
}
|
2022-02-26 19:11:59 -05:00
|
|
|
const out = [];
|
|
|
|
|
const poly = jscadModeling.geometries.geom3
|
|
|
|
|
.toPolygons(geom)
|
|
|
|
|
.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();
|
2022-01-30 20:24:02 -05:00
|
|
|
},
|
2022-01-29 20:37:45 -05:00
|
|
|
|
2022-01-30 20:24:02 -05:00
|
|
|
// Construct a CSG from a THREE Geometry BufferAttribute array (or similar)
|
|
|
|
|
fromPositionArray(array) {
|
2022-02-26 12:42:06 -05:00
|
|
|
const polys = [];
|
|
|
|
|
if (debug) {
|
|
|
|
|
array = [...array].map(v => v.round(3));
|
|
|
|
|
}
|
2022-01-30 20:24:02 -05:00
|
|
|
for (let i=0, l=array.length; i<l; ) {
|
|
|
|
|
polys.push([ [
|
2022-01-29 20:37:45 -05:00
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
2022-01-30 20:24:02 -05:00
|
|
|
],[
|
2022-01-29 20:37:45 -05:00
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
2022-01-30 20:24:02 -05:00
|
|
|
],[
|
2022-01-29 20:37:45 -05:00
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
|
|
|
|
array[i++],
|
2022-01-30 20:24:02 -05:00
|
|
|
] ]);
|
2022-01-29 20:37:45 -05:00
|
|
|
}
|
2022-01-30 20:24:02 -05:00
|
|
|
return jscadModeling.geometries.geom3.fromPoints(polys);
|
2022-01-29 20:37:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 14:31:27 -05:00
|
|
|
gapp.overlay(base, {
|
|
|
|
|
CSG
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|