VoxelGridStructure.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Geometry>
4 
5 namespace Eigen
6 {
8 }
9 
10 namespace visionx::voxelgrid
11 {
12 }
13 
14 namespace visionx::voxelgrid
15 {
16 
17  /*
18  /// A flat index, i.e. running index of a voxel in { 0, ..., number of voxels - 1 }.
19  using FlatIndex = std::size_t;
20  /// A grid index, i.e. index of voxel along each dimension (can be negative).
21  using GridIndex = Eigen::Vector3i;
22  */
23 
24  /**
25  * @brief Geometric structure of a 3D voxel grid.
26  *
27  * The grid structure is defined by the voxel size(s) and grid size(s).
28  * The voxel size (R^3) is the extent of a single voxel along each
29  * axis (x, y, z). (Cubic voxels have constant extent along all axes).
30  * The grid size (N^3) is the number of voxels along each axis.
31  * (Cubic grids have constant number of voxels along all axes.)
32  *
33  * In addition, the voxel grid can have a pose, i.e. position and orientation.
34  *
35  * The total number of voxels to be contained in a structure is the
36  * product of the grid size coefficients.
37  *
38  *
39  * @par Grid Size and Voxel Index
40  *
41  * To indicate a voxel, one can use either of:
42  * - a grid index i = (i_x i_y i_z) in Z^3 (Eigen::Vector3i)
43  * - a flat index j in N^1 (std::size_t)
44  *
45  * A grid index specifies the voxel index along each axis. A flat index
46  * specifies a voxel's running index (from 0 to #voxels - 1).
47  *
48  * Each coefficient of a grid index is in the range
49  * `[ - gridSize/2, gridSize/2 )`
50  * More precisely, it is in the set
51  * `{ - floor(gridSize/2), ..., 0, ..., ceil(gridSize/2) - 1 }`
52  *
53  * In other words,
54  * - for an ODD grid size: [ - floor(gridSize/2), floor(gridSize/2) ]
55  * - for an EVEN grid size: [ - gridSize/2 , gridSize/2 - 1 ]
56  *
57  * For example (along one axis):
58  *
59  * | Grid Size | Valid grid indices |
60  * +-----------+--------------------------+
61  * | 3 | { -1, 0, 1 } |
62  * | 4 | { -2, -1, 0, 1 } |
63  * | 5 | { -2, -1, 0, 1, 2 } |
64  *
65  * Note that grid indices are centered around zero and can be negative.
66  * A flat index is always non-negative.
67  *
68  *
69  * @par Voxel Grid Origin
70  *
71  * The grid origin coincides with the center of voxel (0 0 0) and can be
72  * chosen freely upon construction. It differs from the grid center in
73  * axes with even grid size.
74  *
75  * @code
76  *
77  * odd grid size (3) even grid size (4)
78  * +---+---+---+ +---+---+---+---+ ^
79  * | | | | 1 | | | | | 1 | voxel size
80  * +---+---+---+ +---+---+---+---+ v
81  * c | | o | | 0 | | | o | | 0
82  * +---+---+---+ +---+---+---+---+----- c
83  * | | | | -1 | | | | | -1
84  * +---+---+---+ +---+---+---+---+
85  * -1 0 1 | | | | | -2
86  * c +---+---+---+---+
87  * -2 -1 | 0 1
88  * c
89  * (i = origin, c = center)
90  *
91  * @endcode
92  *
93  */
95  {
96  public:
97  /// Construct an empty grid structure (0 voxels with size 1.0).
99 
100  /// Construct a voxel grid structure with cubic voxels and cubic grid.
101  VoxelGridStructure(int gridSize,
102  float voxelSize,
103  const Eigen::Vector3f& origin = Eigen::Vector3f::Zero(),
104  const Eigen::Quaternionf& orientation = Eigen::Quaternionf::Identity());
105 
106  /// Construct a voxel grid structure with cubic voxels.
107  VoxelGridStructure(const Eigen::Vector3i& gridSize,
108  float voxelSize,
109  const Eigen::Vector3f& origin = Eigen::Vector3f::Zero(),
110  const Eigen::Quaternionf& orientation = Eigen::Quaternionf::Identity());
111 
112  /// Construct a voxel grid structure with cubic grid.
113  VoxelGridStructure(int gridSize,
114  const Eigen::Vector3f& voxelSize,
115  const Eigen::Vector3f& origin = Eigen::Vector3f::Zero(),
116  const Eigen::Quaternionf& orientation = Eigen::Quaternionf::Identity());
117 
118  /// Construct a voxel grid structure.
119  VoxelGridStructure(const Eigen::Vector3i& gridSize,
120  const Eigen::Vector3f& voxelSize,
121  const Eigen::Vector3f& origin = Eigen::Vector3f::Zero(),
122  const Eigen::Quaternionf& orientation = Eigen::Quaternionf::Identity());
123 
124 
125  /// Get the grid size.
126  Eigen::Vector3i getGridSize() const;
127  /// Set the grid size for a cubic grid.
128  void setGridSize(int gridSize);
129  /// Set the grid size.
130  void setGridSize(const Eigen::Vector3i& gridSize);
131 
132  /// Get the voxel size.
133  Eigen::Vector3f getVoxelSize() const;
134  /// Set the voxel size for cubic voxels.
135  void setVoxelSize(float voxelSize);
136  /// Set the voxel size.
137  void setVoxelSize(const Eigen::Vector3f& voxelSize);
138 
139 
140  /// Get the number of voxels contained in the structure.
141  std::size_t getNumVoxels() const;
142 
143  /// Get the flat index for the given grid indices.
144  std::size_t getFlatIndex(int x, int y, int z) const;
145  /// Get the flat index for the given grid indices.
146  std::size_t getFlatIndex(const Eigen::Vector3i& indices) const;
147  /// Get the flat index into the grid data of the voxel closest to point.
148  std::size_t getFlatIndex(const Eigen::Vector3f& point, bool local = false) const;
149 
150  /// Get the voxel indices of the voxel with the given index.
151  Eigen::Vector3i getGridIndex(size_t index) const;
152  /// Get the indices of the voxel closest to point.
153  /// @param local If false (default), the point is transformed to local coordinate system.
154  Eigen::Vector3i getGridIndex(const Eigen::Vector3f& point, bool local = false) const;
155 
156  /// Get the minimal (along each axis) grid index.
157  Eigen::Vector3i getGridIndexMin() const;
158  /// Get the maximal (along each axis) grid index.
159  Eigen::Vector3i getGridIndexMax() const;
160 
161  /// Get the center of the voxel with the given indices.
162  Eigen::Vector3f getVoxelCenter(std::size_t index, bool local = false) const;
163  /// Get the center of the voxel with the given indices.
164  Eigen::Vector3f getVoxelCenter(int x, int y, int z, bool local = false) const;
165  /// Get the center of the voxel with the given indices.
166  Eigen::Vector3f getVoxelCenter(const Eigen::Vector3i& indices, bool local = false) const;
167 
168 
169  /// Get the grid origin in the world frame (center of voxel [0 0 0]).
170  /// Note that this differs from the geometric center for even grid sizes.
171  /// @see getCenter()
172  Eigen::Vector3f getOrigin() const;
173  /// Set the grid origin the world frame (center of voxel [0 0 0]).
174  /// Note that this differs from the geometric center for even grid sizes.
175  /// @see setCenter()
176  void setOrigin(const Eigen::Vector3f& value);
177 
178  /// Get the grid orienation in the world frame.
180  /// Set the grid orienation in the world frame.
182 
183  /**
184  * @brief Get the geometric center of the grid the world frame
185  * (which differs from the origin for even grid sizes).
186  */
187  Eigen::Vector3f getCenter() const;
188  /**
189  * @brief Set the geometric center of the grid the world frame
190  * (which differs from the origin for even grid sizes).
191  *
192  * @note This computes the origin from the given center using the
193  * current orientation. If you want to set grid center and orientation,
194  * set the orientation first, or (equivalently), use `setGridCenterPose()`.
195  *
196  * @see setPoseGridCenter()
197  */
198  void setCenter(const Eigen::Vector3f& center);
199 
200 
201  /// Get the grid pose in the world frame.
202  /// Note that this differs from the geometric center for even grid sizes.
203  /// @see getCenterPose()
204  Eigen::Matrix4f getPose() const;
205  /// Get the grid pose in the world frame.
206  /// Note that this differs from the geometric center for even grid sizes.
207  /// @see setCenterPose()
208  void setPose(const Eigen::Matrix4f& pose);
209 
210  /// Get the grid pose positioned at the grid center in the world frame.
212  /// Set the grid pose so that the grid center is the position of the
213  /// given pose under the given orientation.
214  void setCenterPose(const Eigen::Matrix4f& pose);
215 
216 
217  /// Get extent of the grid along each axis (encompassing the whole voxels).
218  Eigen::Vector3f getExtent() const;
219  /// Get extent of the grid along each axis (encompassing only voxel centers).
220  Eigen::Vector3f getExtentOfCenters() const;
221  /// Get the local axis aligned bounding box of the grid (minimal/maximal values in columns).
223  /// Get the local axis aligned bounding box of the voxel centers (minimal/maximal values in columns).
225 
226 
227  /// Indicate whether the given point is inside the voxel.
228  bool isInside(const Eigen::Vector3i& indices) const;
229  /// Indicate whether the given point is inside the voxel.
230  bool isInside(const Eigen::Vector3f& point, bool local = false) const;
231 
232  /// Assert that the given indices are valid grid indices.
233  void checkIsInside(const Eigen::Vector3i& indices) const;
234 
235 
236  /// Indicates whether `rhs` is equal to `*this`.
237  /// Uses `v.isApprox()` to compare voxel size, origin and orientation.
238  bool operator==(const VoxelGridStructure& rhs) const;
239  bool operator!=(const VoxelGridStructure& rhs) const;
240 
241  /// Stream a human-readable description of the grid's structure.
242  friend std::ostream& operator<<(std::ostream& os, const VoxelGridStructure& grid);
243 
244 
245  private:
246  /// Get half the grid size.
247  Eigen::Vector3f halfGridSize() const;
248  /// Get the translation from origin to grid center (in local or global frame).
249  Eigen::Vector3f getOriginToGridCenter(bool local) const;
250 
251 
252  /// The number of voxels in one direction.
253  Eigen::Vector3i _gridSize = Eigen::Vector3i::Zero();
254  /// The extent of a single voxel.
255  Eigen::Vector3f _voxelSize = Eigen::Vector3f::Ones();
256 
257 
258  /// The grid origin in the world frame. Coincides with the center of voxel (0 0 0).
259  Eigen::Vector3f _origin = Eigen::Vector3f::Zero();
260  /// The grid orientation in the world frame (pivots around origin).
262 
263 
264  private:
265  /// IO format for vectors.
266  static const Eigen::IOFormat _iofVector;
267  /// IO format for sizes (delimited by 'x').
268  static const Eigen::IOFormat _iofTimes;
269  };
270 
271 
272 } // namespace visionx::voxelgrid
visionx::voxelgrid::VoxelGridStructure::getGridIndexMin
Eigen::Vector3i getGridIndexMin() const
Get the minimal (along each axis) grid index.
Definition: VoxelGridStructure.cpp:175
visionx::voxelgrid::VoxelGridStructure::getGridIndexMax
Eigen::Vector3i getGridIndexMax() const
Get the maximal (along each axis) grid index.
Definition: VoxelGridStructure.cpp:185
Eigen
Definition: Elements.h:32
index
uint8_t index
Definition: EtherCATFrame.h:59
visionx::voxelgrid::VoxelGridStructure::getFlatIndex
std::size_t getFlatIndex(int x, int y, int z) const
Get the flat index for the given grid indices.
Definition: VoxelGridStructure.cpp:113
visionx::voxelgrid::VoxelGridStructure::getVoxelSize
Eigen::Vector3f getVoxelSize() const
Get the voxel size.
Definition: VoxelGridStructure.cpp:89
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
visionx::voxelgrid::VoxelGridStructure::setGridSize
void setGridSize(int gridSize)
Set the grid size for a cubic grid.
Definition: VoxelGridStructure.cpp:77
visionx::voxelgrid::VoxelGridStructure::VoxelGridStructure
VoxelGridStructure()
Construct an empty grid structure (0 voxels with size 1.0).
Definition: VoxelGridStructure.cpp:29
visionx::voxelgrid::VoxelGridStructure::getOrientation
Eigen::Quaternionf getOrientation() const
Get the grid orienation in the world frame.
Definition: VoxelGridStructure.cpp:235
visionx::voxelgrid::VoxelGridStructure::setPose
void setPose(const Eigen::Matrix4f &pose)
Get the grid pose in the world frame.
Definition: VoxelGridStructure.cpp:265
visionx::voxelgrid::VoxelGridStructure::getPose
Eigen::Matrix4f getPose() const
Get the grid pose in the world frame.
Definition: VoxelGridStructure.cpp:259
visionx::voxelgrid::VoxelGridStructure::getExtentOfCenters
Eigen::Vector3f getExtentOfCenters() const
Get extent of the grid along each axis (encompassing only voxel centers).
Definition: VoxelGridStructure.cpp:291
visionx::voxelgrid::VoxelGridStructure::getExtent
Eigen::Vector3f getExtent() const
Get extent of the grid along each axis (encompassing the whole voxels).
Definition: VoxelGridStructure.cpp:285
visionx::voxelgrid
Definition: exceptions.cpp:7
visionx::voxelgrid::VoxelGridStructure::getLocalBoundingBoxOfCenters
Eigen::Matrix32f getLocalBoundingBoxOfCenters() const
Get the local axis aligned bounding box of the voxel centers (minimal/maximal values in columns).
Definition: VoxelGridStructure.cpp:306
visionx::voxelgrid::VoxelGridStructure::isInside
bool isInside(const Eigen::Vector3i &indices) const
Indicate whether the given point is inside the voxel.
Definition: VoxelGridStructure.cpp:315
visionx::voxelgrid::VoxelGridStructure
Geometric structure of a 3D voxel grid.
Definition: VoxelGridStructure.h:94
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
visionx::voxelgrid::VoxelGridStructure::getGridSize
Eigen::Vector3i getGridSize() const
Get the grid size.
Definition: VoxelGridStructure.cpp:71
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:717
visionx::voxelgrid::VoxelGridStructure::operator<<
friend std::ostream & operator<<(std::ostream &os, const VoxelGridStructure &grid)
Stream a human-readable description of the grid's structure.
Definition: VoxelGridStructure.cpp:380
visionx::voxelgrid::VoxelGridStructure::setCenter
void setCenter(const Eigen::Vector3f &center)
Set the geometric center of the grid the world frame (which differs from the origin for even grid siz...
Definition: VoxelGridStructure.cpp:253
visionx::voxelgrid::VoxelGridStructure::setOrigin
void setOrigin(const Eigen::Vector3f &value)
Set the grid origin the world frame (center of voxel [0 0 0]).
Definition: VoxelGridStructure.cpp:229
visionx::voxelgrid::VoxelGridStructure::setOrientation
void setOrientation(const Eigen::Quaternionf &value)
Set the grid orienation in the world frame.
Definition: VoxelGridStructure.cpp:241
visionx::voxelgrid::VoxelGridStructure::getCenterPose
Eigen::Matrix4f getCenterPose() const
Get the grid pose positioned at the grid center in the world frame.
Definition: VoxelGridStructure.cpp:272
visionx::voxelgrid::VoxelGridStructure::getVoxelCenter
Eigen::Vector3f getVoxelCenter(std::size_t index, bool local=false) const
Get the center of the voxel with the given indices.
Definition: VoxelGridStructure.cpp:196
visionx::voxelgrid::VoxelGridStructure::operator!=
bool operator!=(const VoxelGridStructure &rhs) const
Definition: VoxelGridStructure.cpp:354
visionx::voxelgrid::VoxelGridStructure::getNumVoxels
std::size_t getNumVoxels() const
Get the number of voxels contained in the structure.
Definition: VoxelGridStructure.cpp:107
armarx::Quaternion< float, 0 >
Eigen::Matrix32f
Matrix< float, 3, 2 > Matrix32f
A 3x2 matrix.
Definition: DebugDrawerTopic.h:21
visionx::voxelgrid::VoxelGridStructure::setVoxelSize
void setVoxelSize(float voxelSize)
Set the voxel size for cubic voxels.
Definition: VoxelGridStructure.cpp:95
visionx::voxelgrid::VoxelGridStructure::checkIsInside
void checkIsInside(const Eigen::Vector3i &indices) const
Assert that the given indices are valid grid indices.
Definition: VoxelGridStructure.cpp:337
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
visionx::voxelgrid::VoxelGridStructure::getCenter
Eigen::Vector3f getCenter() const
Get the geometric center of the grid the world frame (which differs from the origin for even grid siz...
Definition: VoxelGridStructure.cpp:247
visionx::voxelgrid::VoxelGridStructure::getOrigin
Eigen::Vector3f getOrigin() const
Get the grid origin in the world frame (center of voxel [0 0 0]).
Definition: VoxelGridStructure.cpp:223
visionx::voxelgrid::VoxelGridStructure::getGridIndex
Eigen::Vector3i getGridIndex(size_t index) const
Get the voxel indices of the voxel with the given index.
Definition: VoxelGridStructure.cpp:141
visionx::voxelgrid::VoxelGridStructure::getLocalBoundingBox
Eigen::Matrix32f getLocalBoundingBox() const
Get the local axis aligned bounding box of the grid (minimal/maximal values in columns).
Definition: VoxelGridStructure.cpp:297
visionx::voxelgrid::VoxelGridStructure::operator==
bool operator==(const VoxelGridStructure &rhs) const
Indicates whether rhs is equal to *this.
Definition: VoxelGridStructure.cpp:347
visionx::voxelgrid::VoxelGridStructure::setCenterPose
void setCenterPose(const Eigen::Matrix4f &pose)
Set the grid pose so that the grid center is the position of the given pose under the given orientati...
Definition: VoxelGridStructure.cpp:278