天天看点

PCL - PCLBase代碼研讀(二)- PCLBase實現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::reset

indices_

管理的指標替換為

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.
           

如果傳入的

indices

PointIndicesConstPtr

型別的,一樣要重新創建一個

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

PCLBase

deinitCompute

函數直接回傳

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_
           

继续阅读