7 #include "../VoxelGrid.hpp"
26 template <
typename VoxelT>
27 static void readVoxel(std::istream& is, VoxelT& voxel);
36 template <
typename VoxelT>
37 static void writeVoxel(std::ostream& os,
const VoxelT& voxel);
48 template <
typename VoxelT>
57 template <
typename VoxelT>
68 template <
typename VoxelGr
idT>
78 template <
typename VoxelGr
idT>
79 static VoxelGridT
read(std::istream& is);
87 template <
typename VoxelT>
95 template <
typename VoxelT>
102 static void checkPositive(
int value);
103 static void checkPositive(
float value);
113 template <
typename DataT>
116 is.read(
reinterpret_cast<char*
>(&
data),
sizeof(DataT));
119 template <
typename DataT>
122 os.write(
reinterpret_cast<const char*
>(&
data),
sizeof(DataT));
125 template <
typename ReadT,
typename Derived>
126 void readEigen(std::istream& is, Eigen::MatrixBase<Derived>& matrix)
130 auto cast = matrix.template cast<ReadT>().eval();
131 is.read(
reinterpret_cast<char*
>(
cast.data()),
cast.size() *
sizeof(
Scalar));
133 matrix =
cast.template cast<Scalar>();
136 template <
typename WriteT,
typename Derived>
137 void writeEigen(std::ostream& os,
const Eigen::MatrixBase<Derived>& matrix)
139 auto eval = matrix.template cast<WriteT>().eval();
140 os.write(
reinterpret_cast<const char*
>(eval.data()),
141 eval.size() *
sizeof(WriteT));
149 template <
typename VoxelT>
152 detail::readData<VoxelT>(is, voxel);
155 template <
typename VoxelT>
158 detail::writeData<VoxelT>(os, voxel);
162 template <
typename VoxelGr
idT>
170 template <
typename VoxelGr
idT>
178 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>
206 Eigen::Vector3f voxelSizes = Eigen::Vector3f::Zero();
207 Eigen::Vector3f gridSizes = Eigen::Vector3f::Zero();
208 Eigen::Vector3f origin = Eigen::Vector3f::Zero();
209 Eigen::Vector4f orientationCoeffs = Eigen::Vector4f::Zero();
214 detail::readEigen<float>(is, voxelSizes);
216 detail::readEigen<float>(is, gridSizes);
218 detail::readEigen<float>(is, origin);
220 detail::readEigen<float>(is, orientationCoeffs);
222 catch (
const std::ios_base::failure& e)
227 checkPositive(voxelSizes.minCoeff());
228 checkPositive(gridSizes.minCoeff());
231 orientation.w() = orientationCoeffs(0);
232 orientation.coeffs().head<3>() = orientationCoeffs.tail<3>();
240 for (std::size_t i = 0; i < voxelGrid.
numVoxels(); ++i)
244 readVoxel<VoxelT>(is, voxelGrid.
getVoxel(i));
246 catch (
const std::ios_base::failure& e)
248 std::stringstream ss;
249 ss <<
"Failed to read voxel " << i <<
" of " << voxelGrid.
numVoxels() <<
".";
256 template <
typename VoxelT>
260 ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
264 ofs.open(
filename, std::ios_base::binary | std::ios_base::out);
266 catch (
const std::ios_base::failure& e)
273 write(voxelGrid, ofs);
275 catch (
const std::ios_base::failure& e)
281 template <
typename VoxelT>
284 const Eigen::Vector3f voxelSizes = voxelGrid.
getVoxelSizes();
285 const Eigen::Vector3i gridSizes = voxelGrid.
getGridSizes();
286 const Eigen::Vector3f origin = voxelGrid.
getOrigin();
290 Eigen::Vector4f orientationCoeffs;
291 orientationCoeffs(0) = orientation.w();
292 orientationCoeffs.tail<3>() = orientation.coeffs().head<3>();
295 detail::writeEigen<float>(os, voxelSizes);
297 detail::writeEigen<float>(os, gridSizes.cast<
float>());
299 detail::writeEigen<float>(os, origin);
301 detail::writeEigen<float>(os, orientationCoeffs);
304 for (std::size_t i = 0; i < voxelGrid.
numVoxels(); ++i)
306 writeVoxel<VoxelT>(os, voxelGrid.
getVoxel(i));