Point Cloud Library (PCL)  1.7.1
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 #ifndef PCL_RECOGNITION_VOXEL_STRUCTURE_H_
40 #define PCL_RECOGNITION_VOXEL_STRUCTURE_H_
41 
42 #include <cstdlib>
43 
44 namespace pcl
45 {
46  namespace recognition
47  {
48  /** \brief This class is a box in R3 built of voxels ordered in a regular rectangular grid. Each voxel is of type T. */
49  template<class T, typename REAL = float>
51  {
52  public:
53  inline VoxelStructure (): voxels_(NULL){}
54  inline virtual ~VoxelStructure (){ this->clear();}
55 
56  /** \brief Call this method before using an instance of this class. Parameter meaning is obvious. */
57  inline void
58  build (const REAL bounds[6], int num_of_voxels[3]);
59 
60  /** \brief Release the memory allocated for the voxels. */
61  inline void
62  clear (){ if ( voxels_ ){ delete[] voxels_; voxels_ = NULL;}}
63 
64  /** \brief Returns a pointer to the voxel which contains p or NULL if p is not inside the structure. */
65  inline T*
66  getVoxel (const REAL p[3]);
67 
68  /** \brief Returns a pointer to the voxel with integer id (x,y,z) or NULL if (x,y,z) is out of bounds. */
69  inline T*
70  getVoxel (int x, int y, int z) const;
71 
72  /** \brief Returns the linear voxel array. */
73  const inline T*
74  getVoxels () const
75  {
76  return voxels_;
77  }
78 
79  /** \brief Returns the linear voxel array. */
80  inline T*
82  {
83  return voxels_;
84  }
85 
86  /** \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.
87  *
88  * \param[in] linear_id the position of the voxel in the internal voxel array.
89  * \param[out] id3d an array of 3 integers for the integer coordinates of the voxel. */
90  inline void
91  compute3dId (int linear_id, int id3d[3]) const
92  {
93  // Compute the z axis id
94  id3d[2] = linear_id / num_of_voxels_xy_plane_;
95  int proj_xy = linear_id % num_of_voxels_xy_plane_;
96  // Compute the y axis id
97  id3d[1] = proj_xy / num_of_voxels_[0];
98  // Compute the x axis id
99  id3d[0] = proj_xy % num_of_voxels_[0];
100  }
101 
102  /** \brief Returns the number of voxels in x, y and z direction. */
103  inline const int*
105  {
106  return (num_of_voxels_);
107  }
108 
109  /** \brief Computes the center of the voxel with given integer coordinates.
110  *
111  * \param[in] id3 the integer coordinates along the x, y and z axis.
112  * \param[out] center */
113  inline void
114  computeVoxelCenter (const int id3[3], REAL center[3]) const
115  {
116  center[0] = min_center_[0] + static_cast<float> (id3[0])*spacing_[0];
117  center[1] = min_center_[1] + static_cast<float> (id3[1])*spacing_[1];
118  center[2] = min_center_[2] + static_cast<float> (id3[2])*spacing_[2];
119  }
120 
121  /** \brief Returns the total number of voxels. */
122  inline int
124  {
125  return (total_num_of_voxels_);
126  }
127 
128  /** \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). */
129  inline const float*
130  getBounds() const
131  {
132  return (bounds_);
133  }
134 
135  /** \brief Copies the bounds of the voxel structure to 'b'. */
136  inline void
137  getBounds(REAL b[6]) const
138  {
139  b[0] = bounds_[0];
140  b[1] = bounds_[1];
141  b[2] = bounds_[2];
142  b[3] = bounds_[3];
143  b[4] = bounds_[4];
144  b[5] = bounds_[5];
145  }
146 
147  /** \brief Returns the voxel spacing in x, y and z direction. That's the same as the voxel size along each axis. */
148  const REAL*
150  {
151  return (spacing_);
152  }
153 
154  /** \brief Saves pointers to the voxels which are neighbors of the voxels which contains 'p'. The containing voxel is returned too.
155  * '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 */
156  inline int
157  getNeighbors (const REAL* p, T **neighs) const;
158 
159  protected:
162  REAL bounds_[6];
163  REAL spacing_[3]; // spacing betwen the voxel in x, y and z direction = voxel size in x, y and z direction
164  REAL min_center_[3]; // the center of the voxel with integer coordinates (0, 0, 0)
165  };
166  } // namespace recognition
167 } // namespace pcl
168 
169 #include <pcl/recognition/impl/ransac_based/voxel_structure.hpp>
170 
171 #endif // PCL_RECOGNITION_VOXEL_STRUCTURE_H_
const REAL * getVoxelSpacing() const
Returns the voxel spacing in x, y and z direction.
const int * getNumberOfVoxelsXYZ() const
Returns the number of voxels in x, y and z direction.
int getNumberOfVoxels() const
Returns the total number of voxels.
int getNeighbors(const REAL *p, T **neighs) const
Saves pointers to the voxels which are neighbors of the voxels which contains 'p'.
void clear()
Release the memory allocated for the voxels.
T * getVoxel(const REAL p[3])
Returns a pointer to the voxel which contains p or NULL if p is not inside the structure.
const float * getBounds() const
Returns the bounds of the voxel structure, which is pointer to the internal array of 6 doubles: (min_...
void build(const REAL bounds[6], int num_of_voxels[3])
Call this method before using an instance of this class.
const T * getVoxels() const
Returns the linear voxel array.
void getBounds(REAL b[6]) const
Copies the bounds of the voxel structure to 'b'.
This class is a box in R3 built of voxels ordered in a regular rectangular grid.
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...
T * getVoxels()
Returns the linear voxel array.
void computeVoxelCenter(const int id3[3], REAL center[3]) const
Computes the center of the voxel with given integer coordinates.