Point Cloud Library (PCL)  1.9.1-dev
voxel_structure.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  *
37  */
38 
39 #pragma once
40 
41 #include <cstdlib>
42 
43 namespace pcl
44 {
45  namespace recognition
46  {
47  /** \brief This class is a box in R3 built of voxels ordered in a regular rectangular grid. Each voxel is of type T. */
48  template<class T, typename REAL = float>
50  {
51  public:
52  inline VoxelStructure (): voxels_(nullptr){}
53  inline virtual ~VoxelStructure (){ this->clear();}
54 
55  /** \brief Call this method before using an instance of this class. Parameter meaning is obvious. */
56  inline void
57  build (const REAL bounds[6], int num_of_voxels[3]);
58 
59  /** \brief Release the memory allocated for the voxels. */
60  inline void
61  clear (){ if ( voxels_ ){ delete[] voxels_; voxels_ = nullptr;}}
62 
63  /** \brief Returns a pointer to the voxel which contains p or NULL if p is not inside the structure. */
64  inline T*
65  getVoxel (const REAL p[3]);
66 
67  /** \brief Returns a pointer to the voxel with integer id (x,y,z) or NULL if (x,y,z) is out of bounds. */
68  inline T*
69  getVoxel (int x, int y, int z) const;
70 
71  /** \brief Returns the linear voxel array. */
72  const inline T*
73  getVoxels () const
74  {
75  return voxels_;
76  }
77 
78  /** \brief Returns the linear voxel array. */
79  inline T*
81  {
82  return voxels_;
83  }
84 
85  /** \brief Converts a linear id to a 3D id, i.e., computes the integer 3D coordinates of a voxel from its position in the voxel array.
86  *
87  * \param[in] linear_id the position of the voxel in the internal voxel array.
88  * \param[out] id3d an array of 3 integers for the integer coordinates of the voxel. */
89  inline void
90  compute3dId (int linear_id, int id3d[3]) const
91  {
92  // Compute the z axis id
93  id3d[2] = linear_id / num_of_voxels_xy_plane_;
94  int proj_xy = linear_id % num_of_voxels_xy_plane_;
95  // Compute the y axis id
96  id3d[1] = proj_xy / num_of_voxels_[0];
97  // Compute the x axis id
98  id3d[0] = proj_xy % num_of_voxels_[0];
99  }
100 
101  /** \brief Returns the number of voxels in x, y and z direction. */
102  inline const int*
104  {
105  return (num_of_voxels_);
106  }
107 
108  /** \brief Computes the center of the voxel with given integer coordinates.
109  *
110  * \param[in] id3 the integer coordinates along the x, y and z axis.
111  * \param[out] center */
112  inline void
113  computeVoxelCenter (const int id3[3], REAL center[3]) const
114  {
115  center[0] = min_center_[0] + static_cast<float> (id3[0])*spacing_[0];
116  center[1] = min_center_[1] + static_cast<float> (id3[1])*spacing_[1];
117  center[2] = min_center_[2] + static_cast<float> (id3[2])*spacing_[2];
118  }
119 
120  /** \brief Returns the total number of voxels. */
121  inline int
123  {
124  return (total_num_of_voxels_);
125  }
126 
127  /** \brief Returns the bounds of the voxel structure, which is pointer to the internal array of 6 doubles: (min_x, max_x, min_y, max_y, min_z, max_z). */
128  inline const float*
129  getBounds() const
130  {
131  return (bounds_);
132  }
133 
134  /** \brief Copies the bounds of the voxel structure to 'b'. */
135  inline void
136  getBounds(REAL b[6]) const
137  {
138  b[0] = bounds_[0];
139  b[1] = bounds_[1];
140  b[2] = bounds_[2];
141  b[3] = bounds_[3];
142  b[4] = bounds_[4];
143  b[5] = bounds_[5];
144  }
145 
146  /** \brief Returns the voxel spacing in x, y and z direction. That's the same as the voxel size along each axis. */
147  const REAL*
149  {
150  return (spacing_);
151  }
152 
153  /** \brief Saves pointers to the voxels which are neighbors of the voxels which contains 'p'. The containing voxel is returned too.
154  * 'neighs' has to be an array of pointers with space for at least 27 pointers (27 = 3^3 which is the max number of neighbors). The */
155  inline int
156  getNeighbors (const REAL* p, T **neighs) const;
157 
158  protected:
161  REAL bounds_[6];
162  REAL spacing_[3]; // spacing between the voxel in x, y and z direction = voxel size in x, y and z direction
163  REAL min_center_[3]; // the center of the voxel with integer coordinates (0, 0, 0)
164  };
165  } // namespace recognition
166 } // namespace pcl
167 
168 #include <pcl/recognition/impl/ransac_based/voxel_structure.hpp>
const float * getBounds() const
Returns the bounds of the voxel structure, which is pointer to the internal array of 6 doubles: (min_...
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
const int * getNumberOfVoxelsXYZ() const
Returns the number of voxels in x, y and z direction.
T * getVoxel(const REAL p[3])
Returns a pointer to the voxel which contains p or NULL if p is not inside the structure.
int getNumberOfVoxels() const
Returns the total number of voxels.
void compute3dId(int linear_id, int id3d[3]) const
Converts a linear id to a 3D id, i.e., computes the integer 3D coordinates of a voxel from its positi...
int getNeighbors(const REAL *p, T **neighs) const
Saves pointers to the voxels which are neighbors of the voxels which contains &#39;p&#39;.
void build(const REAL bounds[6], int num_of_voxels[3])
Call this method before using an instance of this class.
const REAL * getVoxelSpacing() const
Returns the voxel spacing in x, y and z direction.
This class is a box in R3 built of voxels ordered in a regular rectangular grid.
void getBounds(REAL b[6]) const
Copies the bounds of the voxel structure to &#39;b&#39;.
void computeVoxelCenter(const int id3[3], REAL center[3]) const
Computes the center of the voxel with given integer coordinates.
T * getVoxels()
Returns the linear voxel array.
const T * getVoxels() const
Returns the linear voxel array.
void clear()
Release the memory allocated for the voxels.