Go to the documentation of this file.
8 #include <SimoxUtility/json/json.hpp>
12 #include "../VoxelGrid.hpp"
20 template <
class VoxelT>
23 template <
class VoxelT>
27 template <
class VoxelT>
30 template <
class VoxelT>
36 template <
class VoxelT,
class MemberT>
39 return [memberVariable](
const VoxelT &
v)
41 return nlohmann::json(
v.*memberVariable);
46 template <
class VoxelT,
class MemberT>
47 static VoxelAttributeGetter<VoxelT> makeGetter(MemberT(VoxelT::*memberFunc)()
const)
49 return [memberFunc](
const VoxelT &
v)
51 return (
v.*memberFunc)();
57 template <
class VoxelT,
class MemberT>
58 static VoxelAttributeSetter<VoxelT> makeSetter(MemberT VoxelT::*memberVariable)
60 return [memberVariable](
const nlohmann::json & j, VoxelT &
v)
62 return v.*memberVariable = j.get<MemberT>();
68 template <
class VoxelT,
class MemberT>
69 static VoxelAttributeSetter<VoxelT> makeSetter(
void(VoxelT::*memberFunc)(
const MemberT&))
71 return [memberFunc](
const nlohmann::json & j, VoxelT &
v)
73 return (
v.*memberFunc)(j.get<MemberT>());
107 template <
class VoxelT>
112 int indent = -1,
char indentChar =
' ')
118 template <
class VoxelT>
123 int indent = -1,
char indentChar =
' ')
132 template <
class VoxelT>
142 template <
class VoxelT>
148 template <
class VoxelT>
158 template <
class VoxelT>
167 template <
class VoxelT>
174 template <
class VoxelT>
175 static nlohmann::json
toJson(
181 template <
class VoxelT>
185 const std::string& voxelArrayName =
"voxels");
189 template <
class VoxelT>
190 static nlohmann::json
toJson(
192 const std::string& voxelArrayName =
"voxels");
198 template <
class VoxelT>
200 const nlohmann::json& j,
207 template <
class VoxelT>
209 const nlohmann::json& j,
211 const std::string& voxelArrayName =
"voxels");
216 int indent = -1,
char indentChar =
' ');
218 static void writeJson(std::ostream& os,
const nlohmann::json& j,
219 int indent = -1,
char indentChar =
' ');
224 static nlohmann::json
readJson(std::istream& is);
228 template<
class VoxelT>
238 template<
class VoxelT>
243 read(is, grid, attributeMap);
248 template<
class VoxelT>
254 toJson(j, grid, attributeMap);
258 template<
class VoxelT>
270 for (
const auto& item : attributeMap)
272 j[item.first] = nlohmann::json::array();
276 for (
const VoxelT& voxel : grid)
278 for (
const auto& item : attributeMap)
281 j[item.first].push_back(item.second(voxel));
286 template<
class VoxelT>
288 const std::string& voxelArrayName)
296 template<
class VoxelT>
300 toJson(j, grid, voxelArrayName);
305 template<
class VoxelT>
307 const nlohmann::json& j,
317 for (std::size_t i = 0; i < grid.
numVoxels(); ++i)
319 VoxelT& voxel = grid[i];
320 for (
const auto& item : attributeMap)
323 item.second(j[item.first].at(i), voxel);
329 template<
class VoxelT>
331 const nlohmann::json& j,
333 const std::string& voxelArrayName)
340 grid.
setVoxels(j.at(voxelArrayName).get<std::vector<VoxelT>>());
static void read(const std::string &filename, VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Read a voxel grid from file.
static void fromJson(const nlohmann::json &j, VoxelGrid< VoxelT > &grid, const VoxelAttributeSetterMap< VoxelT > &attributeMap)
Deserialize a voxel grid from JSON.
void setVoxels(const std::vector< VoxelT > &voxels)
Set the voxels.
std::function< nlohmann::json(const VoxelT &voxel)> VoxelAttributeGetter
Function to get an attribute of a voxel.
static void toJson(nlohmann::json &j, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Serialize a voxel grid to JSON.
std::map< std::string, VoxelAttributeGetter< VoxelT > > VoxelAttributeGetterMap
Map of attribute name to attribute getter.
static void read(std::istream &is, VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Read a voxel grid from is.
void resetStructure(const VoxelGridStructure &structure)
Set the voxel grid structure and reset voxel data.
static void write(const std::string &filename, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap, int indent=-1, char indentChar=' ')
Write a voxel grid to file.
static void writeJson(const std::string &filename, const nlohmann::json &j, int indent=-1, char indentChar=' ')
Write JSON json to file.
VoxelGridStructure getStructure() const
Get the voxel grid structure.
std::size_t numVoxels() const
Get the number of voxels in the grid.
Geometric structure of a 3D voxel grid.
Class to store voxel grids in a vectorized JSON format.
static nlohmann::json readJson(const std::string &filename)
Read JSON from file.
static void write(std::ostream &os, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap, int indent=-1, char indentChar=' ')
Write a voxel grid to os.
std::map< std::string, VoxelAttributeSetter< VoxelT > > VoxelAttributeSetterMap
Map of attribute name to attribute setter.
double v(double t, double v0, double a0, double j)
std::size_t getNumVoxels() const
Get the number of voxels contained in the structure.
const std::vector< VoxelT > & getVoxels() const
Get the voxels.
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
std::function< void(const nlohmann::json &j, VoxelT &voxel)> VoxelAttributeSetter
Function to set an attribute of a voxel.