PCL - PCLBase代碼研讀(二)- PCLBase實現

PCL - PCLBase代碼研讀(二)- PCLBase實現

前言

接續PCL - PCLBase代碼研讀(一)- PCLBase架構,這邊繼續來看PCLBase成員函數的實現。本篇主要關注的是common/include/pcl/impl/pcl_base.hpp這份文件。

constructor & destructor

注意到use_indices_fake_indices_都被初始化為false:

#ifndef PCL_PCL_IMPL_BASE_HPP_
#define PCL_PCL_IMPL_BASE_HPP_

#include <pcl/pcl_base.h>
#include <pcl/console/print.h>
#include <cstddef>

///
template <typename PointT>
pcl::PCLBase<PointT>::PCLBase ()
  : input_ ()
  , use_indices_ (false)
  , fake_indices_ (false)
{
}

copy constructor:

///
template <typename PointT>
pcl::PCLBase<PointT>::PCLBase (const PCLBase& base)
  : input_ (base.input_)
  , indices_ (base.indices_)
  , use_indices_ (base.use_indices_)
  , fake_indices_ (base.fake_indices_)
{
}

setter

setInputCloud

設定input_

///
template <typename PointT> void
pcl::PCLBase<PointT>::setInputCloud (const PointCloudConstPtr &cloud)
{ 
  input_ = cloud; 
}

setIndices

indices_的型別是IndicesPtr,傳入的indices與它類型相同,所以直接用=賦值就好。fake_indices_ = false;表示使用的indices_是外部傳入的,而非內部產生的。

///
template <typename PointT> void
pcl::PCLBase<PointT>::setIndices (const IndicesPtr &indices)
{
  indices_ = indices;
  fake_indices_ = false;
  use_indices_  = true;
}

如果傳入的indices型別與indices_類型不同,則需要先用*來取出indices所指向的Indices物件(參考How to get the Object being pointed by a shared pointer?),然後再用取出的Indices物件去創建一個新的Indices物件,最後用std::shared_ptr::resetindices_管理的指標替換為new Indices

///
template <typename PointT> void
pcl::PCLBase<PointT>::setIndices (const IndicesConstPtr &indices)
{
  indices_.reset (new Indices (*indices));
  fake_indices_ = false;
  use_indices_  = true;
}

注:參考std::shared_ptr::reset

Replaces the managed object with an object pointed to by ptr.

如果傳入的indicesPointIndicesConstPtr型別的,一樣要重新創建一個Indices物件後對indices_reset

///
template <typename PointT> void
pcl::PCLBase<PointT>::setIndices (const PointIndicesConstPtr &indices)
{
  indices_.reset (new Indices (indices->indices));
  fake_indices_ = false;
  use_indices_  = true;
}

目前沒看到這個函數在哪裡被使用到,暫不展開。

///
template <typename PointT> void
pcl::PCLBase<PointT>::setIndices (std::size_t row_start, std::size_t col_start, std::size_t nb_rows, std::size_t nb_cols)
{
  if ((nb_rows > input_->height) || (row_start > input_->height))
  {
    PCL_ERROR ("[PCLBase::setIndices] cloud is only %d height\n", input_->height);
    return;
  }

  if ((nb_cols > input_->width) || (col_start > input_->width))
  {
    PCL_ERROR ("[PCLBase::setIndices] cloud is only %d width\n", input_->width);
    return;
  }

  std::size_t row_end = row_start + nb_rows;
  if (row_end > input_->height)
  {
    PCL_ERROR ("[PCLBase::setIndices] %d is out of rows range %d\n", row_end, input_->height);
    return;
  }

  std::size_t col_end = col_start + nb_cols;
  if (col_end > input_->width)
  {
    PCL_ERROR ("[PCLBase::setIndices] %d is out of columns range %d\n", col_end, input_->width);
    return;
  }

  indices_.reset (new Indices);
  indices_->reserve (nb_cols * nb_rows);
  for(std::size_t i = row_start; i < row_end; i++)
    for(std::size_t j = col_start; j < col_end; j++)
      indices_->push_back (static_cast<int> ((i * input_->width) + j));
  fake_indices_ = false;
  use_indices_  = true;
}

initCompute

template <typename PointT> bool
pcl::PCLBase<PointT>::initCompute ()
{

檢查input_指標是否為空,如果是,則退出:

  // Check if input was set
  if (!input_)
  {
    PCL_ERROR ("[initCompute] No input set.\n");
    return (false);
  }

檢查indices_指標是否為空,如果是,則更新indices_,並將它標註為虛假的(fake_indices_ = true;),然後透過indices_.reset (new Indices);indices_管理的物件替換為new Indices

  // If no point indices have been given, construct a set of indices for the entire input point cloud
  if (!indices_)
  {
    fake_indices_ = true;
    indices_.reset (new Indices);
  }

如果indices_的內容是PCLBase內部自行創造的(fake_indices_為true),並且indices_的大小不等於input_的大小,則將indices_resize為input_的大小,然後依序填入0,1,2,3,…,indices_->size()-1:

  // If we have a set of fake indices, but they do not match the number of points in the cloud, update them
  if (fake_indices_ && indices_->size () != input_->size ())
  {
    const auto indices_size = indices_->size ();
    try
    {
      indices_->resize (input_->size ());
    }
    catch (const std::bad_alloc&)
    {
      PCL_ERROR ("[initCompute] Failed to allocate %lu indices.\n", input_->size ());
    }
    for (auto i = indices_size; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
  }

  return (true);
}

deinitCompute

PCLBasedeinitCompute函數直接回傳true,不做任何事情。

///
template <typename PointT> bool
pcl::PCLBase<PointT>::deinitCompute ()
{
  return (true);
}

#define PCL_INSTANTIATE_PCLBase(T) template class PCL_EXPORTS pcl::PCLBase<T>;

#endif  //#ifndef PCL_PCL_IMPL_BASE_HPP_
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值