原文:http://blog.csdn.net/lming_08/article/details/19432877

MarchingCubes算法简介

MarchingCubes(移动立方体)算法是目前三围数据场等值面生成中最常用的方法。它实际上是一个分而治之的方法,把等值面的抽取分布于每个体素中进行。对于每个被处理的体素,以三角面片逼近其内部的等值面片。每个体素是一个小立方体,构造三角面片的处理过程对每个体素都“扫描”一遍,就好像一个处理器在这些体素上移动一样,由此得名移动立方体算法。

MC算法主要有三步:1.将点云数据转换为体素网格数据;2.使用线性插值对每个体素抽取等值面;3.对等值面进行网格三角化

PCL源码剖析之MarchingCubesHoppe

PCL中使用MarchingCubesHoppe类进行三维重建执行的函数体为performReconstruction(),其代码如下:

  1. template <typename PointNT> void
  2. pcl::MarchingCubes<PointNT>::performReconstruction (pcl::PolygonMesh &output)
  3. {
  4. if (!(iso_level_ >= 0 && iso_level_ < 1))
  5. {
  6. PCL_ERROR ("[pcl::%s::performReconstruction] Invalid iso level %f! Please use a number between 0 and 1.\n", getClassName ().c_str (), iso_level_);
  7. output.cloud.width = output.cloud.height = 0;
  8. output.cloud.data.clear ();
  9. output.polygons.clear ();
  10. return;
  11. }
  12. // Create grid
  13. grid_ = std::vector<float> (res_x_*res_y_*res_z_, 0.0f);
  14. // Populate tree
  15. tree_->setInputCloud (input_);
  16. getBoundingBox ();
  17. // Transform the point cloud into a voxel grid
  18. // This needs to be implemented in a child class
  19. voxelizeData ();
  20. // Run the actual marching cubes algorithm, store it into a point cloud,
  21. // and copy the point cloud + connectivity into output
  22. pcl::PointCloud<PointNT> cloud;
  23. for (int x = 1; x < res_x_-1; ++x)
  24. for (int y = 1; y < res_y_-1; ++y)
  25. for (int z = 1; z < res_z_-1; ++z)
  26. {
  27. Eigen::Vector3i index_3d (x, y, z);
  28. std::vector<float> leaf_node;
  29. getNeighborList1D (leaf_node, index_3d);
  30. createSurface (leaf_node, index_3d, cloud);
  31. }
  32. pcl::toPCLPointCloud2 (cloud, output.cloud);
  33. output.polygons.resize (cloud.size () / 3);
  34. for (size_t i = 0; i < output.polygons.size (); ++i)
  35. {
  36. pcl::Vertices v;
  37. v.vertices.resize (3);
  38. for (int j = 0; j < 3; ++j)
  39. v.vertices[j] = static_cast<int> (i) * 3 + j;
  40. output.polygons[i] = v;
  41. }
  42. }

可以看出PCL将会产生res_x_ * res_y_ * res_z_个网格,即为Resolution分辨率。voxelizeData ();即将点云数据转换为体素网格数据,其实现如下:

  1. template <typename PointNT> void
  2. pcl::MarchingCubesHoppe<PointNT>::voxelizeData ()
  3. {
  4. for (int x = 0; x < res_x_; ++x)
  5. for (int y = 0; y < res_y_; ++y)
  6. for (int z = 0; z < res_z_; ++z)
  7. {
  8. std::vector<int> nn_indices;
  9. std::vector<float> nn_sqr_dists;
  10. Eigen::Vector3f point;
  11. point[0] = min_p_[0] + (max_p_[0] - min_p_[0]) * float (x) / float (res_x_);
  12. point[1] = min_p_[1] + (max_p_[1] - min_p_[1]) * float (y) / float (res_y_);
  13. point[2] = min_p_[2] + (max_p_[2] - min_p_[2]) * float (z) / float (res_z_);
  14. PointNT p;
  15. p.getVector3fMap () = point;
  16. tree_->nearestKSearch (p, 1, nn_indices, nn_sqr_dists);
  17. grid_[x * res_y_*res_z_ + y * res_z_ + z] = input_->points[nn_indices[0]].getNormalVector3fMap ().dot (
  18. point - input_->points[nn_indices[0]].getVector3fMap ());
  19. }
  20. }

该函数对每个体素网格数据进行赋值,其值为一符号距离函数值,其定义为:f(Pi) = (Pi - Oi) * N(Oi), 这里Pi为给定的点,Oi为Pi周围K近邻点集(输入点云的子集)的中心, N(Oi)为点Oi的法向量,中间的*为数量积;求出的值其实是点Oi到过点Pi的有向切平面的距离,图示如下:

点q处的法向量是单位法向量,所以点q到切平面的距离是dot(N(p), vec(p, q))

从代码中可以看出,这里的K = 1,即求出最近邻点。

下面的代码描述了对每个体素网格的处理过程,其主要过程是计算出每个体素网格与等值面的交点,然后按一定顺序将交点连接,从而形成三角面片。

  1. for (int x = 1; x < res_x_-1; ++x)
  2. for (int y = 1; y < res_y_-1; ++y)
  3. for (int z = 1; z < res_z_-1; ++z)
  4. {
  5. Eigen::Vector3i index_3d (x, y, z);
  6. std::vector<float> leaf_node;
  7. getNeighborList1D (leaf_node, index_3d);
  8. createSurface (leaf_node, index_3d, cloud);
  9. }

getNeighorList1D(leaf_node, index_3d);即是求出当前体素网格的8个顶点对应符号距离函数值,即数组grid_中对应的值。实现代码如下:

  1. template <typename PointNT> void
  2. pcl::MarchingCubes<PointNT>::getNeighborList1D (std::vector<float> &leaf,
  3. Eigen::Vector3i &index3d)
  4. {
  5. leaf = std::vector<float> (8, 0.0f);
  6. leaf[0] = getGridValue (index3d);
  7. leaf[1] = getGridValue (index3d + Eigen::Vector3i (1, 0, 0));
  8. leaf[2] = getGridValue (index3d + Eigen::Vector3i (1, 0, 1));
  9. leaf[3] = getGridValue (index3d + Eigen::Vector3i (0, 0, 1));
  10. leaf[4] = getGridValue (index3d + Eigen::Vector3i (0, 1, 0));
  11. leaf[5] = getGridValue (index3d + Eigen::Vector3i (1, 1, 0));
  12. leaf[6] = getGridValue (index3d + Eigen::Vector3i (1, 1, 1));
  13. leaf[7] = getGridValue (index3d + Eigen::Vector3i (0, 1, 1));
  14. }

createSurface (leaf_node, index_3d, cloud);即是求出每个体素网格与等值面的交点,然后按一定顺序将交点连接,从而形成三角面片。下面分片段剖析createSurface ()函数代码。

  1. int cubeindex = 0;
  2. Eigen::Vector3f vertex_list[12];
  3. if (leaf_node[0] < iso_level_) cubeindex |= 1;
  4. if (leaf_node[1] < iso_level_) cubeindex |= 2;
  5. if (leaf_node[2] < iso_level_) cubeindex |= 4;
  6. if (leaf_node[3] < iso_level_) cubeindex |= 8;
  7. if (leaf_node[4] < iso_level_) cubeindex |= 16;
  8. if (leaf_node[5] < iso_level_) cubeindex |= 32;
  9. if (leaf_node[6] < iso_level_) cubeindex |= 64;
  10. if (leaf_node[7] < iso_level_) cubeindex |= 128;

此段代码是将8个顶点的标量值与等值面相比较,如果标量值小于等值面值(即顶点在等值面下面),则将cubeindex相应的位置为1。这样就可以知道8个顶点中哪些在等值面下,哪些在等值面之上了。
立方体中顶点与棱边的编号如下所示:

例如,如果顶点3的值在等值面值之下并且所有其他顶点的值都在等值面之上,那么我们可以通过切割边2、3、11来创建一个三角面片。

算法使用一个边表将cubeindex映射为一个12bit的数值,每一位与一条边相关,如果边没有被等值面切割则设为0,切割则设为1。如果没有边被切割那么表返回0,这种情况发生在当cubeindex = 0(所有顶点在等值面之下)或0xff(所有顶点在等值面之上)。举个之前的例子,如果只有顶点3在等值面下面,cubeindex将会等于0000 1000 或8。边表edgeTable定义在marching_cubes.h文件中:

  1. const unsigned int edgeTable[256] = {
  2. 0x0  , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
  3. 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
  4. 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
  5. 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
  6. 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c,
  7. 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
  8. 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac,
  9. 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
  10. 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c,
  11. 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
  12. 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc,
  13. 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
  14. 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c,
  15. 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
  16. 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc ,
  17. 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
  18. 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
  19. 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
  20. 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
  21. 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
  22. 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
  23. 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
  24. 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
  25. 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
  26. 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
  27. 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
  28. 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
  29. 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
  30. 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
  31. 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
  32. 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
  33. 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0
  34. };

edgeTable[8] = 0x80c = 1000 0000 1100。这就表示边2、3、11与等值面相交。
判断出边与等值面相交之后,就要确定具体的交点。这里PCL使用线性插值,线性插值具体见线性插值。代码如下:

  1. // Find the vertices where the surface intersects the cube
  2. if (edgeTable[cubeindex] & 1)
  3. interpolateEdge (p[0], p[1], leaf_node[0], leaf_node[1], vertex_list[0]);
  4. if (edgeTable[cubeindex] & 2)
  5. interpolateEdge (p[1], p[2], leaf_node[1], leaf_node[2], vertex_list[1]);
  6. if (edgeTable[cubeindex] & 4)
  7. interpolateEdge (p[2], p[3], leaf_node[2], leaf_node[3], vertex_list[2]);
  8. if (edgeTable[cubeindex] & 8)
  9. interpolateEdge (p[3], p[0], leaf_node[3], leaf_node[0], vertex_list[3]);
  10. if (edgeTable[cubeindex] & 16)
  11. interpolateEdge (p[4], p[5], leaf_node[4], leaf_node[5], vertex_list[4]);
  12. if (edgeTable[cubeindex] & 32)
  13. interpolateEdge (p[5], p[6], leaf_node[5], leaf_node[6], vertex_list[5]);
  14. if (edgeTable[cubeindex] & 64)
  15. interpolateEdge (p[6], p[7], leaf_node[6], leaf_node[7], vertex_list[6]);
  16. if (edgeTable[cubeindex] & 128)
  17. interpolateEdge (p[7], p[4], leaf_node[7], leaf_node[4], vertex_list[7]);
  18. if (edgeTable[cubeindex] & 256)
  19. interpolateEdge (p[0], p[4], leaf_node[0], leaf_node[4], vertex_list[8]);
  20. if (edgeTable[cubeindex] & 512)
  21. interpolateEdge (p[1], p[5], leaf_node[1], leaf_node[5], vertex_list[9]);
  22. if (edgeTable[cubeindex] & 1024)
  23. interpolateEdge (p[2], p[6], leaf_node[2], leaf_node[6], vertex_list[10]);
  24. if (edgeTable[cubeindex] & 2048)
  25. interpolateEdge (p[3], p[7], leaf_node[3], leaf_node[7], vertex_list[11]);

文章参考于:http://www.doc88.com/p-5475997688638.html

http://paulbourke.net/geometry/polygonise/

http://books.google.com.hk/books?id=4k4kvDwP-lgC&printsec=frontcover&hl=zh-CN#v=onepage&q&f=false

PCL源码剖析之MarchingCubes算法的更多相关文章

  1. Apache Spark源码剖析

    Apache Spark源码剖析(全面系统介绍Spark源码,提供分析源码的实用技巧和合理的阅读顺序,充分了解Spark的设计思想和运行机理) 许鹏 著   ISBN 978-7-121-25420- ...

  2. STL"源码"剖析-重点知识总结

    STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...

  3. Java多线程9:ThreadLocal源码剖析

    ThreadLocal源码剖析 ThreadLocal其实比较简单,因为类里就三个public方法:set(T value).get().remove().先剖析源码清楚地知道ThreadLocal是 ...

  4. 【转载】STL"源码"剖析-重点知识总结

    原文:STL"源码"剖析-重点知识总结 STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点 ...

  5. (原创滴~)STL源码剖析读书总结1——GP和内存管理

    读完侯捷先生的<STL源码剖析>,感觉真如他本人所说的"庖丁解牛,恢恢乎游刃有余",STL底层的实现一览无余,给人一种自己的C++水平又提升了一个level的幻觉,呵呵 ...

  6. 《Apache Spark源码剖析》

    Spark Contributor,Databricks工程师连城,华为大数据平台开发部部长陈亮,网易杭州研究院副院长汪源,TalkingData首席数据科学家张夏天联袂力荐1.本书全面.系统地介绍了 ...

  7. STL源码剖析 迭代器(iterator)概念与编程技法(三)

    1 STL迭代器原理 1.1  迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型,STL设计的精髓在于,把容器(Containers)和算法(Algorithms)分开,而迭代器(i ...

  8. STL"源码"剖析

    STL"源码"剖析-重点知识总结   STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略 ...

  9. strlen源码剖析

      学习高效编程的有效途径之一就是阅读高手写的源代码,CRT(C/C++ Runtime Library)作为底层的函数库,实现必然高效.恰好手中就有glibc和VC的CRT源代码,于是挑了一个相对简 ...

随机推荐

  1. 【洛谷】4317:花神的数论题【数位DP】

    P4317 花神的数论题 题目背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 题目描述 话说花神这天又来讲课了.课后照例有超级难的神题啦…… 我 ...

  2. PYQT设计无边框窗体

    #UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGui try:    _fromUt ...

  3. HDU 5694 BD String 迭代

    BD String 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5694 Description Problem Description 众所周知, ...

  4. Codeforces Round #256 (Div. 2) C. Painting Fence

    C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Ch ...

  5. linux下插入的mysql数据乱码问题及第三方工具显示乱码问题

    一.lampp环境下的数据库乱码问题 问题描述: 在做mysql练习的时候发现新创建的数据库中插入数据表中的记录中文出现乱码的问题,如下图: 经过多方查证,整里如下文挡: 前提: 我自己的环境是使用的 ...

  6. MySQL数据库基准压力测试工具之MySQLSlap使用实例

    一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...

  7. 华为S5300系列升级固件S5300SI-V100R005C00SPC100.cc

    这个固件附带了web,注意,这个插件是升级V200的必经固件,所以必须升级为此固件之后才能往下升级. 升级小插曲: 1.升级的使用使用Windows,不要用Mac或者Linux,因为从Mac/Linu ...

  8. Maven系列--setting.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  9. Maven最佳实践 划分模块 配置多模块项目 pom modules

    所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...

  10. 第三方网站返回hybrid app H5页面缓存问题应对策略

    最近负责公司各产品线购买模块的开发,各项功能如期开发完成后测试那边反馈回来一个问题:IOS手机在点击支付宝购买后,跳转到支付宝网站时不输入支付密码,直接点返回,返回到我们自己的APP购买界面发现页面显 ...