1 #ifndef INCLUDE_AL_ISOSURFACE_HPP
2 #define INCLUDE_AL_ISOSURFACE_HPP
66 #include "al/graphics/al_Mesh.hpp"
67 #include "al/types/al_Buffer.hpp"
69 #include <unordered_map>
116 float level()
const {
return mIsolevel; }
126 double &volLengthZ)
const;
129 int posID(
int ix,
int iy,
int iz)
const {
130 return ix + mNF[0] * (iy + mNF[1] * iz);
133 template <
class VEC3I>
int posID(
const VEC3I &i3)
const {
134 return posID(i3[0], i3[1], i3[2]);
138 int cellID(
int ix,
int iy,
int iz)
const {
return 3 * posID(ix, iy, iz); }
141 int edgeID(
int cellID,
int edgeNo)
const {
142 return cellID + mEdgeIDOffsets[edgeNo];
186 void addCell(
int ix,
int iy,
int iz,
const T &xyz,
const T &Xyz,
const T &xYz,
187 const T &XYz,
const T &xyZ,
const T &XyZ,
const T &xYZ,
189 int inds[3] = {ix, iy, iz};
190 const float vals[8] = {xyz, Xyz, xYz, XYz, xyZ, XyZ, xYZ, XYZ};
195 void addCell(
const int *indices3,
const float *values8);
198 template <
class T>
void generate(
const T *scalarField);
207 void generate(
const T *scalarField,
int nX,
int nY,
int nZ,
float cellLengthX,
208 float cellLengthY,
float cellLengthZ) {
210 cellLengths(cellLengthX, cellLengthY, cellLengthZ);
215 void generate(
const T *scalarField,
int n,
float cellLength) {
216 generate(scalarField, n, n, n, cellLength, cellLength, cellLength);
227 void vertexAction(VertexAction &a) { mVertexAction = &a; }
229 const bool inBox()
const {
return mInBox; }
241 size_t operator()(
int v)
const {
return v; }
245 typedef std::unordered_map<int, int, IsosurfaceHashInt> EdgeToVertex;
247 EdgeToVertex mEdgeToVertex;
250 std::vector<EdgeTriangle>
253 std::vector<int> mEdgeToVertexArray;
258 int mEdgeIDOffsets[12];
263 bool mComputeNormals;
267 EdgeVertex calcIntersection(
int nX,
int nY,
int nZ,
int nEdgeNo,
268 const float *vals)
const;
269 void addEdgeVertex(
int x,
int y,
int z,
int cellID,
int edge,
272 void compressTriangles();
282 int Nxy = Nx * mNF[1];
287 for (
int z = mNF[2] - 2; z >= 0; --z) {
289 int z1 = (z + 1) * Nxy;
290 for (
int y = 0; y < mNF[1] - 1; ++y) {
292 int y1 = (y + 1) * Nx;
299 int z0y0_1 = z0y0 + 1;
300 int z0y1_1 = z0y1 + 1;
301 int z1y0_1 = z1y0 + 1;
302 int z1y1_1 = z1y1 + 1;
304 for (
int x = 0; x < mNF[0] - 1; ++x) {
305 float v8[] = {float(vals[z0y0 + x]), float(vals[z0y0_1 + x]),
306 float(vals[z0y1 + x]), float(vals[z0y1_1 + x]),
307 float(vals[z1y0 + x]), float(vals[z1y0_1 + x]),
308 float(vals[z1y1 + x]), float(vals[z1y1_1 + x])};
310 int i3[] = {x, y, z};
Isosurface generated using marching cubes.
Isosurface & normals(bool v)
Set whether to compute normals.
void addCell(const int *indices3, const float *values8)
Add a cell from a scalar field.
void end()
End cell-at-a-time mode.
Isosurface(float level=0, VertexAction &action=noVertexAction)
Isosurface & normalize(bool v)
Set whether to normalize normals (if being computed)
void addCell(int ix, int iy, int iz, const T &xyz, const T &Xyz, const T &xYz, const T &XYz, const T &xyZ, const T &XyZ, const T &xYZ, const T &XYZ)
Add a cell from a scalar field.
Isosurface & cellLengths(double v)
Set all lengths of cell.
Isosurface & fieldDims(int nx, int ny, int nz)
Set individual dimensions of the scalar field.
int fieldDim(int i) const
Get a field dimension.
float level() const
Get isolevel.
void generate(const T *scalarField, int nX, int nY, int nZ, float cellLengthX, float cellLengthY, float cellLengthZ)
Generate isosurface from scalar field.
bool validSurface() const
Returns true if a valid surface has been generated.
void generate(const T *scalarField)
Generate isosurface from scalar field.
Isosurface & inBox(bool v)
Set whether isosurface is assumed to fit snugly within a box.
bool volumeLengths(double &volLengthX, double &volLengthY, double &volLengthZ) const
Gets the length, width, and height of the isosurface volume.
Isosurface & cellLengths(double dx, double dy, double dz)
Set individual lengths of cell.
void begin()
Begin cell-at-a-time mode.
Isosurface & fieldDims(int n)
Set all dimensions of the scalar field.
Isosurface & level(float v)
Set isolevel.
Stores buffers related to rendering graphical objects.
Vec3i edgePos(int i) const
Returns position of edge vertices.