MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
marching_cubes.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef MVE_MARCHINGCUBES_HEADER
11#define MVE_MARCHINGCUBES_HEADER
12
13#include <map>
14
15#include "math/vector.h"
16#include "math/functions.h"
17#include "mve/defines.h"
18#include "mve/mesh.h"
19#include "mve/image.h"
20
23
54template <typename T>
55TriangleMesh::Ptr
56marching_cubes (T& accessor);
57
60
61/* ------------------------- Lookup tables ------------------------ */
62
65
70extern int mc_edge_table[256];
71
77extern int mc_tri_table[256][16];
78
83extern int mc_edge_order[12][2];
84
85/* ------------------------- Implementation ----------------------- */
86
87template <typename T>
88TriangleMesh::Ptr
89marching_cubes (T& accessor)
90{
91 TriangleMesh::Ptr ret(TriangleMesh::create());
92 TriangleMesh::VertexList& verts(ret->get_vertices());
93 TriangleMesh::FaceList& faces(ret->get_faces());
94 TriangleMesh::ColorList& colors(ret->get_vertex_colors());
95
96 typedef std::pair<std::size_t, std::size_t> Edge;
97 typedef std::map<Edge, unsigned int> EdgeMap;
98 EdgeMap vert_ids;
99
100 while (accessor.next())
101 {
102 /* Builds a unique cube index given the SDF values at the cube voxels. */
103 int cubeconfig = 0;
104 for (int i = 0; i < 8; ++i)
105 if (accessor.sdf[i] < 0.0f)
106 cubeconfig |= (1 << i);
107
108 /* Try early bail out. */
109 if (cubeconfig == 0x00 || cubeconfig == 0xff)
110 continue;
111
112 /* Get unique 12 bit edge configuration. */
113 int const edgeconfig = mc_edge_table[cubeconfig];
114
115 /* Iterate over ISO edges and provide vertex IDs. */
116 unsigned int vid[12];
117 for (int i = 0; i < 12; ++i)
118 {
119 if (!(edgeconfig & (1 << i)))
120 continue;
121
122 int const* ev = mc_edge_order[i];
123
124 /* Try to find vertex ID for edge. */
125 Edge edge(accessor.vid[ev[0]], accessor.vid[ev[1]]);
126 if (edge.first > edge.second)
127 std::swap(edge.first, edge.second);
128
129 EdgeMap::iterator iter = vert_ids.find(edge);
130 if (iter != vert_ids.end())
131 {
132 vid[i] = iter->second;
133 continue;
134 }
135
136 /* Create new vertex on the edge. */
137 float d[2] = { accessor.sdf[ev[0]], accessor.sdf[ev[1]] };
138 float w[2] = { d[1] / (d[1] - d[0]), -d[0] / (d[1] - d[0]) };
140 (accessor.pos[ev[0]], accessor.pos[ev[1]], w[0], w[1]);
141
142 if (accessor.has_colors())
143 {
145 (accessor.color[ev[0]], accessor.color[ev[1]], w[0], w[1]);
146 colors.push_back(math::Vec4f(col, 1.0f));
147 }
148
149 vid[i] = verts.size();
150 vert_ids.insert(std::make_pair(edge, verts.size()));
151 verts.push_back(x);
152 }
153
154 /* Generate triangles by connecting the vertex IDs. */
155 for (int j = 0; mc_tri_table[cubeconfig][j] != -1; j += 3)
156 for (int k = 0; k < 3; ++k)
157 faces.push_back(vid[mc_tri_table[cubeconfig][j + k]]);
158 }
159
160 return ret;
161}
162
165
166#endif /* MVE_MARCHINGCUBES_HEADER */
Vector class for arbitrary dimensions and types.
Definition vector.h:87
std::vector< math::Vec3f > VertexList
Definition mesh.h:33
std::vector< math::Vec4f > ColorList
Definition mesh.h:34
std::shared_ptr< TriangleMesh > Ptr
Definition mesh.h:92
std::vector< VertexID > FaceList
Definition mesh.h:97
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_NAMESPACE_END
Definition defines.h:14
#define MVE_GEOM_NAMESPACE_END
Definition defines.h:20
#define MVE_GEOM_NAMESPACE_BEGIN
Definition defines.h:19
T interpolate(T const &v1, float w1)
Generic interpolation (weighting) of a single value.
Definition functions.h:80
TriangleMesh::Ptr marching_cubes(T &accessor)
This function polygonizes a SDF that is partitioned into cubes.
int mc_tri_table[256][16]
Defines the triangle setup for the 256 cube configurations.
Definition marching.cc:69
int mc_edge_order[12][2]
The ordering in which edges of the cube are defined.
Definition marching.cc:329
int mc_edge_table[256]
Defines the 12-bit edge mask, each bit corresponding to one of 12 edges, that contain the surface for...
Definition marching.cc:33
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478