VoxelGridStructure.h
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Geometry>
4
5namespace Eigen
6{
8}
9
10namespace visionx::voxelgrid
11{
12}
13
14namespace 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.
181 void setOrientation(const Eigen::Quaternionf& value);
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.
211 Eigen::Matrix4f getCenterPose() const;
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).
261 Eigen::Quaternionf _orientation = Eigen::Quaternionf::Identity();
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
uint8_t index
std::size_t getNumVoxels() const
Get the number of voxels contained in the structure.
Eigen::Matrix4f getCenterPose() const
Get the grid pose positioned at the grid center in the world frame.
Eigen::Vector3f getOrigin() const
Get the grid origin in the world frame (center of voxel [0 0 0]).
bool operator!=(const VoxelGridStructure &rhs) const
Eigen::Matrix32f getLocalBoundingBox() const
Get the local axis aligned bounding box of the grid (minimal/maximal values in columns).
std::size_t getFlatIndex(int x, int y, int z) const
Get the flat index for the given grid indices.
void setVoxelSize(float voxelSize)
Set the voxel size for cubic voxels.
Eigen::Vector3f getVoxelCenter(std::size_t index, bool local=false) const
Get the center of the voxel with the given indices.
Eigen::Vector3f getCenter() const
Get the geometric center of the grid the world frame (which differs from the origin for even grid siz...
void setOrientation(const Eigen::Quaternionf &value)
Set the grid orienation in the world frame.
Eigen::Matrix32f getLocalBoundingBoxOfCenters() const
Get the local axis aligned bounding box of the voxel centers (minimal/maximal values in columns).
void setOrigin(const Eigen::Vector3f &value)
Set the grid origin the world frame (center of voxel [0 0 0]).
Eigen::Vector3i getGridSize() const
Get the grid size.
Eigen::Vector3i getGridIndex(size_t index) const
Get the voxel indices of the voxel with the given index.
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...
Eigen::Vector3f getVoxelSize() const
Get the voxel size.
Eigen::Vector3f getExtent() const
Get extent of the grid along each axis (encompassing the whole voxels).
bool operator==(const VoxelGridStructure &rhs) const
Indicates whether rhs is equal to *this.
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...
VoxelGridStructure()
Construct an empty grid structure (0 voxels with size 1.0).
bool isInside(const Eigen::Vector3i &indices) const
Indicate whether the given point is inside the voxel.
void checkIsInside(const Eigen::Vector3i &indices) const
Assert that the given indices are valid grid indices.
void setPose(const Eigen::Matrix4f &pose)
Get the grid pose in the world frame.
friend std::ostream & operator<<(std::ostream &os, const VoxelGridStructure &grid)
Stream a human-readable description of the grid's structure.
Eigen::Vector3f getExtentOfCenters() const
Get extent of the grid along each axis (encompassing only voxel centers).
Eigen::Matrix4f getPose() const
Get the grid pose in the world frame.
void setGridSize(int gridSize)
Set the grid size for a cubic grid.
Eigen::Vector3i getGridIndexMax() const
Get the maximal (along each axis) grid index.
Eigen::Vector3i getGridIndexMin() const
Get the minimal (along each axis) grid index.
Eigen::Quaternionf getOrientation() const
Get the grid orienation in the world frame.
Quaternion< float, 0 > Quaternionf
Matrix< float, 3, 2 > Matrix32f
A 3x2 matrix.
This file offers overloads of toIce() and fromIce() functions for STL container types.