public VolumetricSpace voxelizeMesh(Mesh3D mesh, float iso) {
AABB box = mesh.getBoundingBox();
Vec3D bmin = box.getMin();
Vec3D bmax = box.getMax();
ScaleMap wx = new ScaleMap(bmin.x, bmax.x, 1, volume.resX - 2);
ScaleMap wy = new ScaleMap(bmin.y, bmax.y, 1, volume.resY - 2);
ScaleMap wz = new ScaleMap(bmin.z, bmax.z, 1, volume.resZ - 2);
ScaleMap gx = new ScaleMap(1, volume.resX - 2, bmin.x, bmax.x);
ScaleMap gy = new ScaleMap(1, volume.resY - 2, bmin.y, bmax.y);
ScaleMap gz = new ScaleMap(1, volume.resZ - 2, bmin.z, bmax.z);
volume.setScale(box.getExtent().scale(2f));
Triangle3D tri = new Triangle3D();
AABB voxel = new AABB(new Vec3D(), volume.voxelSize.scale(0.5f));
for (Face f : mesh.getFaces()) {
tri.a = f.a;
tri.b = f.b;
tri.c = f.c;
AABB bounds = tri.getBoundingBox();
Vec3D min = bounds.getMin();
Vec3D max = bounds.getMax();
min = new Vec3D((int) wx.getClippedValueFor(min.x),
(int) wy.getClippedValueFor(min.y),
(int) wz.getClippedValueFor(min.z));
max = new Vec3D((int) wx.getClippedValueFor(max.x),
(int) wy.getClippedValueFor(max.y),
(int) wz.getClippedValueFor(max.z));
for (int z = (int) min.z; z <= max.z; z++) {
for (int y = (int) min.y; y <= max.y; y++) {
for (int x = (int) min.x; x <= max.x; x++) {
if (x < volume.resX1 && y < volume.resY1
&& z < volume.resZ1) {
voxel.set((float) gx.getClippedValueFor(x),
(float) gy.getClippedValueFor(y),
(float) gz.getClippedValueFor(z));
if (voxel.intersectsTriangle(tri)) {
setVoxelAt(x, y, z, iso);
}
}
}