grid-apps-cmms/src/geo/csg.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
// dep: geo.base
2022-10-24 11:43:43 -04:00
// dep: add.manifold
gapp.register("geo.csg", [], (root, exports) => {
const { base } = root;
2022-10-24 11:43:43 -04:00
const debug = true;
const CSG = {
2022-10-24 11:43:43 -04:00
// accepts 2 or more arguments with threejs vertex position arrays
union() {
2022-10-24 11:43:43 -04:00
mesh.log(`manifold.union ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.union(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
2022-02-26 19:11:59 -05:00
}
2022-10-24 11:43:43 -04:00
return output;
},
2022-10-24 11:43:43 -04:00
// accepts 2 or more arguments with threejs vertex position arrays
// the fist argument is the target mesh. the rest are negatives
subtract() {
2022-10-24 11:43:43 -04:00
mesh.log(`manifold.subtract ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.difference(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
}
return output;
},
2022-10-24 11:43:43 -04:00
// accepts 2 or more arguments with threejs vertex position arrays
intersect() {
2022-10-24 11:43:43 -04:00
mesh.log(`manifold.intersect ${arguments.length} meshes`);
const args = [...arguments].map(a => new Module.Manifold(a));
const result = Module.intersection(...args);
const output = result.getMesh();
for (let m of [ ...args, result ]) {
m.delete();
}
return output;
},
toPositionArray(geom) {
2022-10-24 11:43:43 -04:00
return add.manifold.mesh2pos(geom);
},
fromPositionArray(array) {
2022-10-24 11:43:43 -04:00
return add.manifold.pos2mesh(array);
}
};
gapp.overlay(base, {
CSG
});
});