7 #include "../VoxelGrid.hpp"
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>
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)
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>
149 detail::readData<VoxelT>(is, voxel);
152 template <
typename VoxelT>
156 detail::writeData<VoxelT>(os, voxel);
159 template <
typename VoxelGr
idT>
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();
215 detail::readEigen<float>(is, voxelSizes);
217 detail::readEigen<float>(is, gridSizes);
219 detail::readEigen<float>(is, origin);
221 detail::readEigen<float>(is, orientationCoeffs);
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)
245 readVoxel<VoxelT>(is, voxelGrid.
getVoxel(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>();
297 detail::writeEigen<float>(os, voxelSizes);
299 detail::writeEigen<float>(os, gridSizes.cast<
float>());
301 detail::writeEigen<float>(os, origin);
303 detail::writeEigen<float>(os, orientationCoeffs);
306 for (std::size_t i = 0; i < voxelGrid.
numVoxels(); ++i)
308 writeVoxel<VoxelT>(os, voxelGrid.
getVoxel(i));