PCL 法向量估计源码学习

news2024/10/7 12:18:21

一、思路:

二、源码


#ifndef PCL_FEATURES_IMPL_NORMAL_3D_H_
#define PCL_FEATURES_IMPL_NORMAL_3D_H_

#include <pcl/features/normal_3d.h>

///
template <typename PointInT, typename PointOutT> void
pcl::NormalEstimation<PointInT, PointOutT>::computeFeature (PointCloudOut &output)
{
  // Allocate enough space to hold the results
  // \note This resize is irrelevant for a radiusSearch ().
  std::vector<int> nn_indices (k_);
  std::vector<float> nn_dists (k_);

  output.is_dense = true;
  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
  //  如果云设置为密集,则不检查每个点的NaN/Inf值,从而节省几个周期
  if (input_->is_dense)
  {
    // Iterating over the entire index vector
	 // 迭代索引,计算每个索引对应点的 法向量
    for (size_t idx = 0; idx < indices_->size (); ++idx)
    {

		// 判断点云领域内是否有点,并且计算点云的法向量 和 曲率
      if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0 ||
          !computePointNormal (*surface_, nn_indices, output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2], output.points[idx].curvature))
      {
        output.points[idx].normal[0] = output.points[idx].normal[1] = output.points[idx].normal[2] = output.points[idx].curvature = std::numeric_limits<float>::quiet_NaN ();

        output.is_dense = false;
        continue;
      }
	  // 设置视角点 可以理解为法相的朝向问题
      flipNormalTowardsViewpoint (input_->points[(*indices_)[idx]], vpx_, vpy_, vpz_,
                                  output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2]);

    }
  }
  else
  {
    // Iterating over the entire index vector
    for (size_t idx = 0; idx < indices_->size (); ++idx)
    {
      if (!isFinite ((*input_)[(*indices_)[idx]]) ||
          this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0 ||
          !computePointNormal (*surface_, nn_indices, output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2], output.points[idx].curvature))
      {
        output.points[idx].normal[0] = output.points[idx].normal[1] = output.points[idx].normal[2] = output.points[idx].curvature = std::numeric_limits<float>::quiet_NaN ();

        output.is_dense = false;
        continue;
      }

      flipNormalTowardsViewpoint (input_->points[(*indices_)[idx]], vpx_, vpy_, vpz_,
                                  output.points[idx].normal[0], output.points[idx].normal[1], output.points[idx].normal[2]);

    }
  }
}

#define PCL_INSTANTIATE_NormalEstimation(T,NT) template class PCL_EXPORTS pcl::NormalEstimation<T,NT>;

#endif    // PCL_FEATURES_IMPL_NORMAL_3D_H_ 

1、 computePointNormal计算协方差矩阵和曲率

template <typename PointT> inline bool
  computePointNormal (const pcl::PointCloud<PointT> &cloud, const std::vector<int> &indices,
                      Eigen::Vector4f &plane_parameters, float &curvature)
  {
    // Placeholder for the 3x3 covariance matrix at each surface patch
    EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
    // 16-bytes aligned placeholder for the XYZ centroid of a surface patch
    Eigen::Vector4f xyz_centroid;
    // 计算每个点的协方差矩阵和质心
    if (indices.size () < 3 ||
        computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix, xyz_centroid) == 0)
    {
      plane_parameters.setConstant (std::numeric_limits<float>::quiet_NaN ());
      curvature = std::numeric_limits<float>::quiet_NaN ();
      return false;
    }
    // Get the plane normal and surface curvature
	// 通过协方差矩阵和质心来计算点云的   局部平面 的法相 nx ny  nz  和  局部曲率
    solvePlaneParameters (covariance_matrix, xyz_centroid, plane_parameters, curvature);
    return true;
  }



 inline bool
      computePointNormal (const pcl::PointCloud<PointInT> &cloud, const std::vector<int> &indices,
                          float &nx, float &ny, float &nz, float &curvature)
      {
        if (indices.size () < 3 ||
            computeMeanAndCovarianceMatrix (cloud, indices, covariance_matrix_, xyz_centroid_) == 0)
        {
          nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
          return false;
        }

        // Get the plane normal and surface curvature
        solvePlaneParameters (covariance_matrix_, nx, ny, nz, curvature);
        return true;
      }

 1.1 computeMeanAndCovarianceMatrix 计算协方差矩阵

//
template <typename PointT, typename Scalar> inline unsigned int
pcl::computeMeanAndCovarianceMatrix (const pcl::PointCloud<PointT> &cloud,
                                     Eigen::Matrix<Scalar, 3, 3> &covariance_matrix,
                                     Eigen::Matrix<Scalar, 4, 1> &centroid)
{
  // create the buffer on the stack which is much faster than using cloud[indices[i]] and centroid as a buffer
  Eigen::Matrix<Scalar, 1, 9, Eigen::RowMajor> accu = Eigen::Matrix<Scalar, 1, 9, Eigen::RowMajor>::Zero ();
  size_t point_count;
  if (cloud.is_dense)
  {
    point_count = cloud.size ();
    // For each point in the cloud
    for (size_t i = 0; i < point_count; ++i)
    {
      accu [0] += cloud[i].x * cloud[i].x;
      accu [1] += cloud[i].x * cloud[i].y;
      accu [2] += cloud[i].x * cloud[i].z;
      accu [3] += cloud[i].y * cloud[i].y; // 4
      accu [4] += cloud[i].y * cloud[i].z; // 5
      accu [5] += cloud[i].z * cloud[i].z; // 8
      accu [6] += cloud[i].x;
      accu [7] += cloud[i].y;
      accu [8] += cloud[i].z;
    }
  }
  else
  {
    point_count = 0;
    for (size_t i = 0; i < cloud.size (); ++i)
    {
      if (!isFinite (cloud[i]))
        continue;

      accu [0] += cloud[i].x * cloud[i].x;
      accu [1] += cloud[i].x * cloud[i].y;
      accu [2] += cloud[i].x * cloud[i].z;
      accu [3] += cloud[i].y * cloud[i].y;
      accu [4] += cloud[i].y * cloud[i].z;
      accu [5] += cloud[i].z * cloud[i].z;
      accu [6] += cloud[i].x;
      accu [7] += cloud[i].y;
      accu [8] += cloud[i].z;
      ++point_count;
    }
  }
  accu /= static_cast<Scalar> (point_count);
  if (point_count != 0)
  {
    //centroid.head<3> () = accu.tail<3> ();    -- does not compile with Clang 3.0
    centroid[0] = accu[6]; centroid[1] = accu[7]; centroid[2] = accu[8];
    centroid[3] = 1;
    covariance_matrix.coeffRef (0) = accu [0] - accu [6] * accu [6];
    covariance_matrix.coeffRef (1) = accu [1] - accu [6] * accu [7];
    covariance_matrix.coeffRef (2) = accu [2] - accu [6] * accu [8];
    covariance_matrix.coeffRef (4) = accu [3] - accu [7] * accu [7];
    covariance_matrix.coeffRef (5) = accu [4] - accu [7] * accu [8];
    covariance_matrix.coeffRef (8) = accu [5] - accu [8] * accu [8];
    covariance_matrix.coeffRef (3) = covariance_matrix.coeff (1);
    covariance_matrix.coeffRef (6) = covariance_matrix.coeff (2);
    covariance_matrix.coeffRef (7) = covariance_matrix.coeff (5);
  }
  return (static_cast<unsigned int> (point_count));
}

 1.2 solvePlaneParameters计算平面法向量和曲率

//
inline void
pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
                           const Eigen::Vector4f &point,
                           Eigen::Vector4f &plane_parameters, float &curvature)
{
  solvePlaneParameters (covariance_matrix, plane_parameters [0], plane_parameters [1], plane_parameters [2], curvature);

  plane_parameters[3] = 0;
  // Hessian form (D = nc . p_plane (centroid here) + p)
  plane_parameters[3] = -1 * plane_parameters.dot (point);
}

//
inline void
pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
                           float &nx, float &ny, float &nz, float &curvature)
{
  // Avoid getting hung on Eigen's optimizers
//  for (int i = 0; i < 9; ++i)
//    if (!pcl_isfinite (covariance_matrix.coeff (i)))
//    {
//      //PCL_WARN ("[pcl::solvePlaneParameteres] Covariance matrix has NaN/Inf values!\n");
//      nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
//      return;
//    }
  // Extract the smallest eigenvalue and its eigenvector
  EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
  pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);

  nx = eigen_vector [0];
  ny = eigen_vector [1];
  nz = eigen_vector [2];

  // Compute the curvature surface change
  float eig_sum = covariance_matrix.coeff (0) + covariance_matrix.coeff (4) + covariance_matrix.coeff (8);
  if (eig_sum != 0)
    curvature = fabsf (eigen_value / eig_sum);
  else
    curvature = 0;
}

2、 flipNormalTowardsViewpoint法向量的方向问题

 /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
    * \param point a given point
    * \param vp_x the X coordinate of the viewpoint
    * \param vp_y the X coordinate of the viewpoint
    * \param vp_z the X coordinate of the viewpoint
    * \param normal the plane normal to be flipped
    * \ingroup features
    */
  template <typename PointT, typename Scalar> inline void
  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,  
	  Eigen::Matrix<Scalar, 4, 1>& normal)
  {
    Eigen::Matrix <Scalar, 4, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z, 0);

    // Dot product between the (viewpoint - point) and the plane normal
    float cos_theta = vp.dot (normal);

    // Flip the plane normal
    if (cos_theta < 0)
    {
      normal *= -1;
      normal[3] = 0.0f;
      // Hessian form (D = nc . p_plane (centroid here) + p)
      normal[3] = -1 * normal.dot (point.getVector4fMap ());
    }
  }

  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
    * \param point a given point
    * \param vp_x the X coordinate of the viewpoint
    * \param vp_y the X coordinate of the viewpoint
    * \param vp_z the X coordinate of the viewpoint
    * \param normal the plane normal to be flipped
    * \ingroup features
    */
  template <typename PointT, typename Scalar> inline void
  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
                              Eigen::Matrix<Scalar, 3, 1>& normal)
  {
    Eigen::Matrix <Scalar, 3, 1> vp (vp_x - point.x, vp_y - point.y, vp_z - point.z);

    // Flip the plane normal
    if (vp.dot (normal) < 0)
      normal *= -1;
  }
  
  /** \brief Flip (in place) the estimated normal of a point towards a given viewpoint
    * \param point a given point
    * \param vp_x the X coordinate of the viewpoint
    * \param vp_y the X coordinate of the viewpoint
    * \param vp_z the X coordinate of the viewpoint
    * \param nx the resultant X component of the plane normal
    * \param ny the resultant Y component of the plane normal
    * \param nz the resultant Z component of the plane normal
    * \ingroup features
    */
  template <typename PointT> inline void
  flipNormalTowardsViewpoint (const PointT &point, float vp_x, float vp_y, float vp_z,
                              float &nx, float &ny, float &nz)
  {
    // See if we need to flip any plane normals
    vp_x -= point.x;
    vp_y -= point.y;
    vp_z -= point.z;

    // Dot product between the (viewpoint - point) and the plane normal
    float cos_theta = (vp_x * nx + vp_y * ny + vp_z * nz);

    // Flip the plane normal
    if (cos_theta < 0)
    {
      nx *= -1;
      ny *= -1;
      nz *= -1;
    }
  }











2.2 设置参数

 inline void
      setViewPoint (float vpx, float vpy, float vpz)
      {
        vpx_ = vpx;
        vpy_ = vpy;
        vpz_ = vpz;
        use_sensor_origin_ = false;
      }

      /** \brief Get the viewpoint.
        * \param [out] vpx x-coordinate of the view point
        * \param [out] vpy y-coordinate of the view point
        * \param [out] vpz z-coordinate of the view point
        * \note this method returns the currently used viewpoint for normal flipping.
        * If the viewpoint is set manually using the setViewPoint method, this method will return the set view point coordinates.
        * If an input cloud is set, it will return the sensor origin otherwise it will return the origin (0, 0, 0)
        */
      inline void
      getViewPoint (float &vpx, float &vpy, float &vpz)
      {
        vpx = vpx_;
        vpy = vpy_;
        vpz = vpz_;
      }


inline void
      useSensorOriginAsViewPoint ()
      {
        use_sensor_origin_ = true;
        if (input_)
        {
          vpx_ = input_->sensor_origin_.coeff (0);
          vpy_ = input_->sensor_origin_.coeff (1);
          vpz_ = input_->sensor_origin_.coeff (2);
        }
        else
        {
          vpx_ = 0;
          vpy_ = 0;
          vpz_ = 0;
        }
      }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/717789.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Matlab】智能优化算法_海洋捕食者算法MPA

【Matlab】智能优化算法_海洋捕食者算法MPA 1.背景介绍1.1 布朗运动1.2 莱维运动 2.数学模型2.1 MPA配方2.2 MPA优化场景2.3 涡流形成与FAD效应 3.文件结构4.伪代码5.详细代码及注释5.1 func_plot.m5.2 Get_Functions_details.m5.3 initialization.m5.4 levy.m5.5 main.m5.6 MP…

【H5】文件上传(ajax)

系列文章 【移动设备】iData 50P 技术规格 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/130604517 【H5】avalon前端数据双向绑定 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/131067187 【H5】安卓自动更新方案&a…

Python_装饰器

目录 简单装饰器 语法糖 *args、**kwargs处理有参数的函数 带参数的装饰器 类装饰器 不带参数的类装饰器 带参数的类装饰器 装饰器执行顺序 functools.wraps 讲 Python 装饰器前&#xff0c;我想先举个例子&#xff0c;虽有点污&#xff0c;但跟装饰器这个话题很贴切。…

select……for update 到底加的什么锁

先上结论 主键索引唯一索引普通索引普通字段等值查询行锁行锁行锁间隙锁&#xff0c;锁表范围查询间隙锁&#xff0c;锁范围行间隙锁&#xff0c;锁范围行间隙锁&#xff0c;锁范围行间隙锁&#xff0c;锁表 数据表准备 DROP TABLE IF EXISTS t_user_test; CREATE TABLE t_u…

【Web3】MetaMask钱包配置

目录 主网更换测试网 私钥如何登录钱包 主网更换测试网 私钥如何登录钱包

docker安装ES,IK分词器,Kibana

dockerhub上自己搜要拉的镜像版本 // 拉取es 6.8.0的镜像版本 docker pull elasticsearch:6.8.0// 运行es镜像 docker run -d -p 9300:9300 -p 9200:9200 --name elasticsearch elasticsearch:6.8.0运行报错了 ERROR: [1] bootstrap checks failed [1]: max virtual memory are…

Michael.W基于Foundry精读Openzeppelin第3期——Arrays.sol

Michael.W基于Foundry精读Openzeppelin第3期——Arrays.sol 0. 版本0.1 Arrays.sol 1. 补充&#xff1a;关于storage的定长数组和动态数组的layout2. 目标合约3. 代码精读3.1 unsafeAccess(address[] storage, uint256)3.2 unsafeAccess(bytes32[] storage, uint256)3.3 unsafe…

限时等待的互斥量

本文结束一种新的锁&#xff0c;称为 timed_mutex 代码如下&#xff1a; #include<iostream> #include<mutex> #include<thread> #include<string> #include<chrono>using namespace std;timed_mutex tmx;void fun1(int id, const string&a…

MySql入门操作

一.前节回顾 1.web项目环境配置 2.通用增删改&#xff0c;通用查询方法 3.前台&#xff0c;后台代码显示效果 所有你都理解了吗&#xff1f; 二.Mysql数据库介绍 1.什么是MySQL&#xff1f; MySQL是一种开源的关系型数据库管理系统。它是目前最流行和广泛使用的数据库之一&…

【Java|golang】2679. 矩阵中的和

给你一个下标从 0 开始的二维整数数组 nums 。一开始你的分数为 0 。你需要执行以下操作直到矩阵变为空&#xff1a; 矩阵中每一行选取最大的一个数&#xff0c;并删除它。如果一行中有多个最大的数&#xff0c;选择任意一个并删除。 在步骤 1 删除的所有数字中找到最大的一个…

NodeJS 后端返回Base64格式数据显示图片 ⑩⑨ (一篇就够了)

文章目录 ✨文章有误请指正&#xff0c;如果觉得对你有用&#xff0c;请点三连一波&#xff0c;蟹蟹支持&#x1f618;前言Base64前端服务器总结 ✨文章有误请指正&#xff0c;如果觉得对你有用&#xff0c;请点三连一波&#xff0c;蟹蟹支持&#x1f618; ⡖⠒⠒⠒⠤⢄⠀⠀⠀ …

【Java语法小记】求字符串中某个字符的数量——IntStream流的使用

文章目录 引入需求代码原理解读s.chars()IntStream filter​(IntPredicate predicate)long count()补充&#xff1a;IntStream peek​(IntConsumer action) 流操作和管道 引入需求 从一段代码引入 return s.length() - (int) s.chars().filter(c -> c S).count(); 其中 (…

文心一言 VS 讯飞星火 VS chatgpt (54)-- 算法导论6.2 6题

文心一言 VS 讯飞星火 VS chatgpt &#xff08;53&#xff09;-- 算法导论6.2 5题 六、证明:对一个大小为 n的堆&#xff0c;MAX-HEAPIFY 的最坏情况运行时间为 Ω(Ign)。(提示对于n个结点的堆&#xff0c;可以通过对每个结点设定恰当的值&#xff0c;使得从根结点到叶结点路径…

2023年房地产投资退出途径研究报告

第一章 房地产投资概况 房地产&#xff08;Real Estate&#xff09;是一个涵盖了土地及其上的永久性建筑&#xff08;如建筑物和房屋&#xff09;和自然资源&#xff08;如矿产&#xff0c;水源&#xff0c;作物&#xff09;的经济学概念。它可以分为四类&#xff1a;住宅房地…

大数据面试题-场景题

1.手写Flink的UV 手写Flink的UV 2.Flink的分组TopN Flink的分组TopN 3.Spark的分组TopN 1&#xff09;方法1&#xff1a; &#xff08;1&#xff09;按照key对数据进行聚合&#xff08;groupByKey&#xff09; &#xff08;2&#xff09;将value转换为数组&#xff0c;利…

2023如何自学网络安全

自学网络安全可以按照以下步骤进行&#xff1a; 学习基础知识&#xff1a;开始之前&#xff0c;建议先学习计算机网络和操作系统的基础知识&#xff0c;了解网络通信的原理和常见的网络攻击方式。可以通过阅读相关的书籍、在线教程或参加网络安全相关的课程来学习。 学习网络安…

Sanic、uvloop及Asyncio的局限

Sanic sanic使用基于libuv的uvloop事件循环替代python asnycio自带的事件循环替代&#xff0c;以此提高异步性能。Flask和Django是同步框架&#xff0c;Tornado、FastApi和Sanic均为异步框架&#xff0c;Sanic的性能最好。Sanic入门教程&#xff1a;Sanic&#xff1a;一款号称…

阶段小作业:基于docker安装mysql

1.在docker hub 搜索Mysql镜像 docker search --limit 5 Mtsql 2.拉取Mysql 5.7 镜像 docker pull mysql 注意mysql是小写哦 3.创建mysql容器&#xff0c;主机3306端口号映射到容器3306端口 docker run -d -p 3306:3306 --privilegedtrue -v /tmp/mysql/log:/var/log/mysql …

OpenVRLoader 与UnityXR Interaction ToolKit不兼容

1、游戏的VR设备监听与输入都是基于UnityXR,但是当接入OpenVRLoader 时无法正常通过Openvr_xr_plugin去获取设备的输入输出。 2、Openxr 和OpenVRLoader同时打开也还是会没有输入信息。 3、我们需要修改com.unity.xr.interaction.toolkit插件代码,不能直接用packmanage的将插…

从零开始的职场攻略,我是如何成为优秀活动策划的?

想要提升活动策划能力&#xff0c;成为活动操盘手&#xff0c;除了避免踩坑之外&#xff0c;你还需要额外掌握以下 4 项能力。当你持续向着这几个方向提高的时候&#xff0c;你可以感受到作为活动策划带来的成就感&#xff0c;甚至你的整个职业生涯都能够因此迎来一个飞跃。 1…