36 #ifndef PCL_SURFACE_IMPL_MARCHING_CUBES_H_
37 #define PCL_SURFACE_IMPL_MARCHING_CUBES_H_
39 #include <pcl/surface/marching_cubes.h>
41 #include <pcl/common/vector_average.h>
42 #include <pcl/Vertices.h>
45 template <
typename Po
intNT>
51 template <
typename Po
intNT>
void
54 PointNT max_pt, min_pt;
57 lower_boundary_ = min_pt.getArray3fMap ();
58 upper_boundary_ = max_pt.getArray3fMap ();
60 const Eigen::Array3f size3_extend = 0.5f * percentage_extend_grid_
61 * (upper_boundary_ - lower_boundary_);
63 lower_boundary_ -= size3_extend;
64 upper_boundary_ += size3_extend;
69 template <
typename Po
intNT>
void
74 Eigen::Vector3f &output)
76 const float mu = (iso_level_ - val_p1) / (val_p2 - val_p1);
77 output = p1 + mu * (p2 - p1);
82 template <
typename Po
intNT>
void
84 const Eigen::Vector3i &index_3d,
88 if (leaf_node[0] < iso_level_) cubeindex |= 1;
89 if (leaf_node[1] < iso_level_) cubeindex |= 2;
90 if (leaf_node[2] < iso_level_) cubeindex |= 4;
91 if (leaf_node[3] < iso_level_) cubeindex |= 8;
92 if (leaf_node[4] < iso_level_) cubeindex |= 16;
93 if (leaf_node[5] < iso_level_) cubeindex |= 32;
94 if (leaf_node[6] < iso_level_) cubeindex |= 64;
95 if (leaf_node[7] < iso_level_) cubeindex |= 128;
101 const Eigen::Vector3f center = lower_boundary_
102 + size_voxel_ * index_3d.cast<
float> ().array ();
104 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > p;
106 for (
int i = 0; i < 8; ++i)
108 Eigen::Vector3f point = center;
110 point[1] =
static_cast<float> (center[1] + size_voxel_[1]);
113 point[2] =
static_cast<float> (center[2] + size_voxel_[2]);
115 if ((i & 0x1) ^ ((i >> 1) & 0x1))
116 point[0] =
static_cast<float> (center[0] + size_voxel_[0]);
122 std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > vertex_list;
123 vertex_list.resize (12);
125 interpolateEdge (p[0], p[1], leaf_node[0], leaf_node[1], vertex_list[0]);
127 interpolateEdge (p[1], p[2], leaf_node[1], leaf_node[2], vertex_list[1]);
129 interpolateEdge (p[2], p[3], leaf_node[2], leaf_node[3], vertex_list[2]);
131 interpolateEdge (p[3], p[0], leaf_node[3], leaf_node[0], vertex_list[3]);
133 interpolateEdge (p[4], p[5], leaf_node[4], leaf_node[5], vertex_list[4]);
135 interpolateEdge (p[5], p[6], leaf_node[5], leaf_node[6], vertex_list[5]);
137 interpolateEdge (p[6], p[7], leaf_node[6], leaf_node[7], vertex_list[6]);
139 interpolateEdge (p[7], p[4], leaf_node[7], leaf_node[4], vertex_list[7]);
141 interpolateEdge (p[0], p[4], leaf_node[0], leaf_node[4], vertex_list[8]);
143 interpolateEdge (p[1], p[5], leaf_node[1], leaf_node[5], vertex_list[9]);
145 interpolateEdge (p[2], p[6], leaf_node[2], leaf_node[6], vertex_list[10]);
147 interpolateEdge (p[3], p[7], leaf_node[3], leaf_node[7], vertex_list[11]);
150 for (
int i = 0;
triTable[cubeindex][i] != -1; i += 3)
153 p1.getVector3fMap () = vertex_list[
triTable[cubeindex][i]];
155 p2.getVector3fMap () = vertex_list[
triTable[cubeindex][i+1]];
157 p3.getVector3fMap () = vertex_list[
triTable[cubeindex][i+2]];
164 template <
typename Po
intNT>
void
166 Eigen::Vector3i &index3d)
170 leaf[0] = getGridValue (index3d);
171 leaf[1] = getGridValue (index3d + Eigen::Vector3i (1, 0, 0));
172 leaf[2] = getGridValue (index3d + Eigen::Vector3i (1, 0, 1));
173 leaf[3] = getGridValue (index3d + Eigen::Vector3i (0, 0, 1));
174 leaf[4] = getGridValue (index3d + Eigen::Vector3i (0, 1, 0));
175 leaf[5] = getGridValue (index3d + Eigen::Vector3i (1, 1, 0));
176 leaf[6] = getGridValue (index3d + Eigen::Vector3i (1, 1, 1));
177 leaf[7] = getGridValue (index3d + Eigen::Vector3i (0, 1, 1));
179 for (
int i = 0; i < 8; ++i)
181 if (std::isnan (leaf[i]))
191 template <
typename Po
intNT>
float
195 if (pos[0] < 0 || pos[0] >= res_x_)
197 if (pos[1] < 0 || pos[1] >= res_y_)
199 if (pos[2] < 0 || pos[2] >= res_z_)
202 return grid_[pos[0]*res_y_*res_z_ + pos[1]*res_z_ + pos[2]];
207 template <
typename Po
intNT>
void
212 performReconstruction (points, output.
polygons);
219 template <
typename Po
intNT>
void
221 std::vector<pcl::Vertices> &polygons)
223 if (!(iso_level_ >= 0 && iso_level_ < 1))
225 PCL_ERROR (
"[pcl::%s::performReconstruction] Invalid iso level %f! Please use a number between 0 and 1.\n",
226 getClassName ().c_str (), iso_level_);
236 grid_ = std::vector<float> (res_x_*res_y_*res_z_, NAN);
240 size_voxel_ = (upper_boundary_ - lower_boundary_)
241 * Eigen::Array3f (res_x_, res_y_, res_z_).inverse ();
248 double size_reserve = std::min((
double) intermediate_cloud.
points.max_size (),
249 2.0 * 6.0 * (
double) (res_y_*res_z_ + res_x_*res_z_ + res_x_*res_y_));
250 intermediate_cloud.
reserve ((std::size_t) size_reserve);
252 for (
int x = 1; x < res_x_-1; ++x)
253 for (
int y = 1; y < res_y_-1; ++y)
254 for (
int z = 1; z < res_z_-1; ++z)
256 Eigen::Vector3i index_3d (x, y, z);
257 std::vector<float> leaf_node;
258 getNeighborList1D (leaf_node, index_3d);
259 if (!leaf_node.empty ())
260 createSurface (leaf_node, index_3d, intermediate_cloud);
263 points.
swap (intermediate_cloud);
265 polygons.resize (points.
size () / 3);
266 for (std::size_t i = 0; i < polygons.size (); ++i)
270 for (
int j = 0; j < 3; ++j)
271 v.
vertices[j] =
static_cast<int> (i) * 3 + j;
276 #define PCL_INSTANTIATE_MarchingCubes(T) template class PCL_EXPORTS pcl::MarchingCubes<T>;
278 #endif // PCL_SURFACE_IMPL_MARCHING_CUBES_H_