Marching: write C++ data structures

This commit is contained in:
Théophile Bastian 2018-02-11 14:50:41 +01:00
parent 62730a03b4
commit 4804309182
2 changed files with 81 additions and 2 deletions

View file

@ -1,5 +1,9 @@
#include "MarchingCubes.hpp"
const std::vector<MarchingCubes::CubeTri>
MarchingCubes::edges_of_intersection[256] = {
};
MarchingCubes::MarchingCubes(
const ImplicitSurface& surface,
const Cuboid& box,
@ -24,7 +28,18 @@ Mesh MarchingCubes::operator()() {
for(double z = box.low(2); z < box.high(2) + resolution;
z += resolution)
{
//TODO apply marching cube indeed
Intersections intersections;
for(int dx=0; dx <= 1; dx++) {
for(int dy=0; dy <= 1; dy++) {
for(int dz=0; dz <= 1; dz++) {
double cx = x + resolution * dx;
double cy = y + resolution * dy;
double cz = z + resolution * dz;
intersections.set_corner(cx, cy, cz,
surface(cx, cy, cz));
}
}
}
}
}
}

View file

@ -15,7 +15,40 @@
class MarchingCubes {
private:
typedef unsigned char intersect_t;
class Intersections {
public:
typedef unsigned char intersect_t;
Intersections() : inters(0) {}
intersect_t value() const {
return inters;
}
/** The corners are indexed with three booleans, one for each
* axis (x, y, z). A false value means a lower coordinate along
* this axis.
* Eg., (false, true, false) means the corner (0, 1, 0) for a
* cube of side 1 placed at (0, 0, 0).
*/
void set_corner(bool x, bool y, bool z, bool val) {
intersect_t mask = 1 << (shift(x, y, z));
if(val)
inters |= mask;
else
inters &= ~mask;
}
bool get_corner(bool x, bool y, bool z) const {
return (inters & (1 << shift(x, y, z))) == 1;
}
private:
intersect_t inters;
int shift(bool x, bool y, bool z) const {
return x + (y << 1) + (z << 2);
}
};
public:
MarchingCubes(
@ -39,7 +72,38 @@ class MarchingCubes {
Mesh operator()();
struct CubeEdge {
CubeEdge() {}
CubeEdge(bool x0, bool y0, bool z0,
bool x1, bool y1, bool z1)
{
x[0] = x0;
y[0] = y0;
z[0] = z0;
x[1] = x1;
y[1] = y1;
z[1] = z1;
}
bool x[2], y[2], z[2];
};
struct CubeTri {
CubeTri() {}
CubeTri(const CubeEdge* edge_) {
for(size_t i=0; i < 3; ++i)
edge[i] = edge_[i];
}
CubeTri(const CubeEdge e0, const CubeEdge e1, const CubeEdge e2) {
edge[0] = e0;
edge[1] = e1;
edge[2] = e2;
}
CubeEdge edge[3];
};
private:
static const std::vector<CubeTri> edges_of_intersection[256];
const ImplicitSurface& surface;
Cuboid box;
double resolution;