MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
marching_tets.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_MARCHINGTETS_HEADER
11#define MVE_MARCHINGTETS_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
22
42template <typename T>
43TriangleMesh::Ptr
44marching_tetrahedra (T& accessor);
45
46/* ------------------------- Lookup tables ------------------------ */
47
54extern int mt_freudenthal[6][4];
55
60extern int mt_edge_table[16];
61
67extern int mt_tri_table[16][7];
68
73extern int mt_edge_order[6][2];
74
75/* ------------------------- Implementation ----------------------- */
76
77template <typename T>
78TriangleMesh::Ptr
80{
81 TriangleMesh::Ptr ret(TriangleMesh::create());
82 TriangleMesh::VertexList& verts(ret->get_vertices());
83 TriangleMesh::FaceList& faces(ret->get_faces());
84 TriangleMesh::ColorList& colors(ret->get_vertex_colors());
85
86 typedef std::pair<std::size_t, std::size_t> Edge;
87 typedef std::map<Edge, unsigned int> EdgeMap;
88 typedef std::map<std::size_t, unsigned int> VertexMap;
89 EdgeMap edge_map;
90 VertexMap vert_map;
91
92 while (accessor.next())
93 {
94 /* Build unique cube index. */
95 int tetconfig = 0;
96 for (int i = 0; i < 4; ++i)
97 if (accessor.sdf[i] < 0.0f)
98 tetconfig |= (1 << i);
99
100 if (tetconfig == 0x0 || tetconfig == 0xf)
101 continue;
102
103 /* Get unique 6 bit edge configuration. */
104 int const edgeconfig = mt_edge_table[tetconfig];
105
106 /* Iterate of the ISO edges and provide vertex IDs. */
107 unsigned int vid[6];
108 for (int i = 0; i < 6; ++i)
109 {
110 if (!(edgeconfig & (1 << i)))
111 continue;
112
113 int const* ev = mt_edge_order[i];
114
115 /* Try to find vertex ID for edge. */
116 Edge edge(accessor.vid[ev[0]], accessor.vid[ev[1]]);
117 if (edge.first > edge.second)
118 std::swap(edge.first, edge.second);
119
120 EdgeMap::iterator iter = edge_map.find(edge);
121 if (iter != edge_map.end())
122 {
123 vid[i] = iter->second;
124 continue;
125 }
126
127 /* Fetch distance values of the edge. */
128 float d[2] = { accessor.sdf[ev[0]], accessor.sdf[ev[1]] };
129
130 /* NEW: Vertex snapping to prevent null faces. */
131 int snap = -1;
132 if (d[0] == 0.0f)
133 snap = ev[0];
134 else if (d[1] == 0.0f)
135 snap = ev[1];
136
137 if (snap >= 0)
138 {
139 VertexMap::iterator iter = vert_map.find(accessor.vid[snap]);
140 if (iter != vert_map.end())
141 {
142 vid[i] = iter->second;
143 continue;
144 }
145
146 if (accessor.has_colors())
147 colors.push_back(math::Vec4f(accessor.color[snap], 1.0f));
148 vid[i] = verts.size();
149 verts.push_back(accessor.pos[snap]);
150 vert_map.insert(std::make_pair(accessor.vid[snap], vid[i]));
151 continue;
152 }
153
154 /* Interpolate vertex position. */
155 float w[2] = { d[1] / (d[1] - d[0]), -d[0] / (d[1] - d[0]) };
157 (accessor.pos[ev[0]], accessor.pos[ev[1]], w[0], w[1]);
158
159 /* Iterpolate color value on the edge. */
160 if (accessor.has_colors())
161 {
163 (accessor.color[ev[0]], accessor.color[ev[1]], w[0], w[1]);
164 colors.push_back(math::Vec4f(col, 1.0f));
165 }
166
167 vid[i] = verts.size();
168 verts.push_back(x);
169 edge_map.insert(std::make_pair(edge, vid[i]));
170 }
171
172 /* Generate triangles by connecting the vertex IDs. */
173 for (int i = 0; mt_tri_table[tetconfig][i] != -1; i += 3)
174 {
175 unsigned int vids[3];
176 for (int k = 0; k < 3; ++k)
177 vids[k] = vid[mt_tri_table[tetconfig][i + k]];
178 if (vids[0] != vids[1] && vids[1] != vids[2] && vids[2] != vids[0])
179 for (int k = 0; k < 3; ++k)
180 faces.push_back(vids[k]);
181 }
182 }
183
184 return ret;
185}
186
189
190#endif /* MVE_MARCHINGTETS_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
int mt_freudenthal[6][4]
Freudenthal cube partitioning, that subdivides the cube into 6 tetrahera for continuous reconstructio...
Definition marching.cc:338
int mt_edge_table[16]
Defines the 6-bit edge mask (for all 6 edges in a tet) for all 16 tetrahedron configurations.
Definition marching.cc:344
int mt_tri_table[16][7]
Defines the triangle setup for the 16 tetrahedron configurations.
Definition marching.cc:350
TriangleMesh::Ptr marching_tetrahedra(T &accessor)
This function polygonizes a SDF that is partitioned into tetrahedrons.
int mt_edge_order[6][2]
Ordering in which edges in the tetrahedron are defined.
Definition marching.cc:370
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478