23 template <
typename VoxelT>
24 static void readVoxel(std::istream& is, VoxelT& voxel);
33 template <
typename VoxelT>
34 static void writeVoxel(std::ostream& os,
const VoxelT& voxel);
44 template <
typename VoxelT>
53 template <
typename VoxelT>
64 template <
typename VoxelGr
idT>
65 static VoxelGridT
read(
const std::string& filename);
74 template <
typename VoxelGr
idT>
75 static VoxelGridT
read(std::istream& is);
83 template <
typename VoxelT>
91 template <
typename VoxelT>
97 static void checkPositive(
int value);
98 static void checkPositive(
float value);
107 template <
typename DataT>
111 is.read(
reinterpret_cast<char*
>(&
data),
sizeof(DataT));
114 template <
typename DataT>
118 os.write(
reinterpret_cast<const char*
>(&
data),
sizeof(DataT));
121 template <
typename ReadT,
typename Derived>
123 readEigen(std::istream& is, Eigen::MatrixBase<Derived>& matrix)
125 using Scalar =
typename Eigen::MatrixBase<Derived>::Scalar;
127 auto cast = matrix.template cast<ReadT>().eval();
128 is.read(
reinterpret_cast<char*
>(cast.data()), cast.size() *
sizeof(
Scalar));
130 matrix = cast.template cast<Scalar>();
133 template <
typename WriteT,
typename Derived>
135 writeEigen(std::ostream& os,
const Eigen::MatrixBase<Derived>& matrix)
137 auto eval = matrix.template cast<WriteT>().eval();
138 os.write(
reinterpret_cast<const char*
>(eval.data()), eval.size() *
sizeof(WriteT));
145 template <
typename VoxelT>
152 template <
typename VoxelT>
159 template <
typename VoxelGr
idT>
164 read(grid, filename);
168 template <
typename VoxelGr
idT>
177 template <
typename VoxelT>
182 ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
186 ifs.open(filename, std::ios_base::binary | std::ios_base::in);
188 catch (
const std::ios_base::failure& e)
195 read(voxelGrid, ifs);
197 catch (
const std::ios_base::failure& e)
203 template <
typename VoxelT>
207 Eigen::Vector3f voxelSizes = Eigen::Vector3f::Zero();
208 Eigen::Vector3f gridSizes = Eigen::Vector3f::Zero();
209 Eigen::Vector3f origin = Eigen::Vector3f::Zero();
210 Eigen::Vector4f orientationCoeffs = Eigen::Vector4f::Zero();
223 catch (
const std::ios_base::failure& e)
228 checkPositive(voxelSizes.minCoeff());
229 checkPositive(gridSizes.minCoeff());
232 orientation.w() = orientationCoeffs(0);
233 orientation.coeffs().head<3>() = orientationCoeffs.tail<3>();
241 for (std::size_t i = 0; i < voxelGrid.
numVoxels(); ++i)
247 catch (
const std::ios_base::failure& e)
249 std::stringstream ss;
250 ss <<
"Failed to read voxel " << i <<
" of " << voxelGrid.
numVoxels() <<
".";
256 template <
typename VoxelT>
261 ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
265 ofs.open(filename, std::ios_base::binary | std::ios_base::out);
267 catch (
const std::ios_base::failure& e)
274 write(voxelGrid, ofs);
276 catch (
const std::ios_base::failure& e)
282 template <
typename VoxelT>
286 const Eigen::Vector3f voxelSizes = voxelGrid.
getVoxelSizes();
287 const Eigen::Vector3i gridSizes = voxelGrid.
getGridSizes();
288 const Eigen::Vector3f origin = voxelGrid.
getOrigin();
292 Eigen::Vector4f orientationCoeffs;
293 orientationCoeffs(0) = orientation.w();
294 orientationCoeffs.tail<3>() = orientation.coeffs().head<3>();
306 for (std::size_t i = 0; i < voxelGrid.
numVoxels(); ++i)
A 3D grid of voxels of type _VoxelT.
Eigen::Vector3f getOrigin() const
Get the grid origin in the world frame (center of voxel [0 0 0]).
void setVoxelSizes(float voxelSize)
Set the voxel size of cubic voxels. The grid data is not updated.
VoxelT & getVoxel(std::size_t index)
Get the voxel with the given index.
void setOrientation(const Eigen::Quaternionf &value)
Set the grid orienation in the world frame.
void setOrigin(const Eigen::Vector3f &value)
Set the grid origin the world frame (center of voxel [0 0 0]).
std::size_t numVoxels() const
Get the number of voxels in the grid.
Eigen::Vector3i getGridSizes() const
Get the grid size.
Eigen::Vector3f getVoxelSizes() const
Get the voxel size.
void setGridSizes(float gridSizes)
Set the grid size for a cubic grid. Resets the grid data.
Eigen::Quaternionf getOrientation() const
Get the grid orienation in the world frame.
Indicates that voxel data with an invalid number of voxels has been passed to a VoxelGrid.
static void read(VoxelGrid< VoxelT > &voxelGrid, const std::string &filename)
Read a voxel grid from the file.
static void writeVoxel(std::ostream &os, const VoxelT &voxel)
Write a voxel to the out stream.
static void write(const VoxelGrid< VoxelT > &voxelGrid, const std::string &filename)
Write a voxel grid to a file.
static void readVoxel(std::istream &is, VoxelT &voxel)
Read a voxel from the out stream.
Quaternion< float, 0 > Quaternionf
void readData(std::istream &is, DataT &data)
void readEigen(std::istream &is, Eigen::MatrixBase< Derived > &matrix)
void writeEigen(std::ostream &os, const Eigen::MatrixBase< Derived > &matrix)
void writeData(std::ostream &os, const DataT &data)