serialization/BVH_model.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021-2022 INRIA
3 //
4 
5 #ifndef HPP_FCL_SERIALIZATION_BVH_MODEL_H
6 #define HPP_FCL_SERIALIZATION_BVH_MODEL_H
7 
9 
16 
17 namespace boost {
18 namespace serialization {
19 
20 namespace internal {
25 };
26 } // namespace internal
27 
28 template <class Archive>
29 void save(Archive &ar, const hpp::fcl::BVHModelBase &bvh_model,
30  const unsigned int /*version*/) {
31  using namespace hpp::fcl;
32  if (!(bvh_model.build_state == BVH_BUILD_STATE_PROCESSED ||
33  bvh_model.build_state == BVH_BUILD_STATE_UPDATED) &&
34  (bvh_model.getModelType() == BVH_MODEL_TRIANGLES)) {
35  throw std::invalid_argument(
36  "The BVH model is not in a BVH_BUILD_STATE_PROCESSED or "
37  "BVH_BUILD_STATE_UPDATED state.\n"
38  "The BVHModel could not be serialized.");
39  }
40 
41  ar &make_nvp("base",
42  boost::serialization::base_object<hpp::fcl::CollisionGeometry>(
43  bvh_model));
44 
45  ar &make_nvp("num_vertices", bvh_model.num_vertices);
46  if (bvh_model.num_vertices > 0) {
47  typedef Eigen::Matrix<FCL_REAL, 3, Eigen::Dynamic> AsVertixMatrix;
48  const Eigen::Map<const AsVertixMatrix> vertices_map(
49  reinterpret_cast<const double *>(bvh_model.vertices), 3,
50  bvh_model.num_vertices);
51  ar &make_nvp("vertices", vertices_map);
52  }
53 
54  ar &make_nvp("num_tris", bvh_model.num_tris);
55  if (bvh_model.num_tris > 0) {
56  typedef Eigen::Matrix<Triangle::index_type, 3, Eigen::Dynamic>
57  AsTriangleMatrix;
58  const Eigen::Map<const AsTriangleMatrix> tri_indices_map(
59  reinterpret_cast<const Triangle::index_type *>(bvh_model.tri_indices),
60  3, bvh_model.num_tris);
61  ar &make_nvp("tri_indices", tri_indices_map);
62  }
63  ar &make_nvp("build_state", bvh_model.build_state);
64 
65  if (bvh_model.prev_vertices) {
66  const bool has_prev_vertices = true;
67  ar << make_nvp("has_prev_vertices", has_prev_vertices);
68  typedef Eigen::Matrix<FCL_REAL, 3, Eigen::Dynamic> AsVertixMatrix;
69  const Eigen::Map<const AsVertixMatrix> prev_vertices_map(
70  reinterpret_cast<const double *>(bvh_model.prev_vertices), 3,
71  bvh_model.num_vertices);
72  ar &make_nvp("prev_vertices", prev_vertices_map);
73  } else {
74  const bool has_prev_vertices = false;
75  ar &make_nvp("has_prev_vertices", has_prev_vertices);
76  }
77 
78  // if(bvh_model.convex)
79  // {
80  // const bool has_convex = true;
81  // ar << make_nvp("has_convex",has_convex);
82  // }
83  // else
84  // {
85  // const bool has_convex = false;
86  // ar << make_nvp("has_convex",has_convex);
87  // }
88 }
89 
90 template <class Archive>
91 void load(Archive &ar, hpp::fcl::BVHModelBase &bvh_model,
92  const unsigned int /*version*/) {
93  using namespace hpp::fcl;
94 
95  ar >> make_nvp("base",
96  boost::serialization::base_object<hpp::fcl::CollisionGeometry>(
97  bvh_model));
98 
99  unsigned int num_vertices;
100  ar >> make_nvp("num_vertices", num_vertices);
101  if (num_vertices != bvh_model.num_vertices) {
102  delete[] bvh_model.vertices;
103  bvh_model.vertices = NULL;
104  bvh_model.num_vertices = num_vertices;
105  if (num_vertices > 0) bvh_model.vertices = new Vec3f[num_vertices];
106  }
107  if (num_vertices > 0) {
108  typedef Eigen::Matrix<FCL_REAL, 3, Eigen::Dynamic> AsVertixMatrix;
109  Eigen::Map<AsVertixMatrix> vertices_map(
110  reinterpret_cast<double *>(bvh_model.vertices), 3,
111  bvh_model.num_vertices);
112  ar >> make_nvp("vertices", vertices_map);
113  } else
114  bvh_model.vertices = NULL;
115 
116  unsigned int num_tris;
117  ar >> make_nvp("num_tris", num_tris);
118 
119  if (num_tris != bvh_model.num_tris) {
120  delete[] bvh_model.tri_indices;
121  bvh_model.tri_indices = NULL;
122  bvh_model.num_tris = num_tris;
123  if (num_tris > 0) bvh_model.tri_indices = new Triangle[num_tris];
124  }
125  if (num_tris > 0) {
126  typedef Eigen::Matrix<Triangle::index_type, 3, Eigen::Dynamic>
127  AsTriangleMatrix;
128  Eigen::Map<AsTriangleMatrix> tri_indices_map(
129  reinterpret_cast<Triangle::index_type *>(bvh_model.tri_indices), 3,
130  bvh_model.num_tris);
131  ar &make_nvp("tri_indices", tri_indices_map);
132  } else
133  bvh_model.tri_indices = NULL;
134 
135  ar >> make_nvp("build_state", bvh_model.build_state);
136 
137  typedef internal::BVHModelBaseAccessor Accessor;
138  reinterpret_cast<Accessor &>(bvh_model).num_tris_allocated = num_tris;
139  reinterpret_cast<Accessor &>(bvh_model).num_vertices_allocated = num_vertices;
140 
141  bool has_prev_vertices;
142  ar >> make_nvp("has_prev_vertices", has_prev_vertices);
143  if (has_prev_vertices) {
144  if (num_vertices != bvh_model.num_vertices) {
145  delete[] bvh_model.prev_vertices;
146  bvh_model.prev_vertices = NULL;
147  if (num_vertices > 0) bvh_model.prev_vertices = new Vec3f[num_vertices];
148  }
149  if (num_vertices > 0) {
150  typedef Eigen::Matrix<FCL_REAL, 3, Eigen::Dynamic> AsVertixMatrix;
151  Eigen::Map<AsVertixMatrix> prev_vertices_map(
152  reinterpret_cast<double *>(bvh_model.prev_vertices), 3,
153  bvh_model.num_vertices);
154  ar &make_nvp("prev_vertices", prev_vertices_map);
155  }
156  } else
157  bvh_model.prev_vertices = NULL;
158 
159  // bool has_convex = true;
160  // ar >> make_nvp("has_convex",has_convex);
161 }
162 
164 
165 namespace internal {
166 template <typename BV>
169  using Base::bvs;
170  using Base::num_bvs;
173 };
174 } // namespace internal
175 
176 template <class Archive, typename BV>
177 void serialize(Archive &ar, hpp::fcl::BVHModel<BV> &bvh_model,
178  const unsigned int version) {
179  split_free(ar, bvh_model, version);
180 }
181 
182 template <class Archive, typename BV>
183 void save(Archive &ar, const hpp::fcl::BVHModel<BV> &bvh_model_,
184  const unsigned int /*version*/) {
185  using namespace hpp::fcl;
186  typedef internal::BVHModelAccessor<BV> Accessor;
187  typedef BVNode<BV> Node;
188 
189  const Accessor &bvh_model = reinterpret_cast<const Accessor &>(bvh_model_);
190  ar &make_nvp("base",
191  boost::serialization::base_object<BVHModelBase>(bvh_model));
192 
193  // if(bvh_model.primitive_indices)
194  // {
195  // const bool with_primitive_indices = true;
196  // ar & make_nvp("with_primitive_indices",with_primitive_indices);
197  //
198  // int num_primitives = 0;
199  // switch(bvh_model.getModelType())
200  // {
201  // case BVH_MODEL_TRIANGLES:
202  // num_primitives = bvh_model.num_tris;
203  // break;
204  // case BVH_MODEL_POINTCLOUD:
205  // num_primitives = bvh_model.num_vertices;
206  // break;
207  // default:
208  // ;
209  // }
210  //
211  // ar & make_nvp("num_primitives",num_primitives);
212  // if(num_primitives > 0)
213  // {
214  // typedef Eigen::Matrix<unsigned int,1,Eigen::Dynamic>
215  // AsPrimitiveIndexVector; const Eigen::Map<const
216  // AsPrimitiveIndexVector>
217  // primitive_indices_map(reinterpret_cast<const unsigned int
218  // *>(bvh_model.primitive_indices),1,num_primitives); ar &
219  // make_nvp("primitive_indices",primitive_indices_map);
222  // }
223  // }
224  // else
225  // {
226  // const bool with_primitive_indices = false;
227  // ar & make_nvp("with_primitive_indices",with_primitive_indices);
228  // }
229  //
230 
231  if (bvh_model.bvs) {
232  const bool with_bvs = true;
233  ar &make_nvp("with_bvs", with_bvs);
234  ar &make_nvp("num_bvs", bvh_model.num_bvs);
235  ar &make_nvp(
236  "bvs",
237  make_array(
238  reinterpret_cast<const char *>(bvh_model.bvs),
239  sizeof(Node) *
240  (std::size_t)bvh_model.num_bvs)); // Assuming BVs are POD.
241  } else {
242  const bool with_bvs = false;
243  ar &make_nvp("with_bvs", with_bvs);
244  }
245 }
246 
247 template <class Archive, typename BV>
248 void load(Archive &ar, hpp::fcl::BVHModel<BV> &bvh_model_,
249  const unsigned int /*version*/) {
250  using namespace hpp::fcl;
251  typedef internal::BVHModelAccessor<BV> Accessor;
252  typedef BVNode<BV> Node;
253 
254  Accessor &bvh_model = reinterpret_cast<Accessor &>(bvh_model_);
255 
256  ar >> make_nvp("base",
257  boost::serialization::base_object<BVHModelBase>(bvh_model));
258 
259  // bool with_primitive_indices;
260  // ar >> make_nvp("with_primitive_indices",with_primitive_indices);
261  // if(with_primitive_indices)
262  // {
263  // int num_primitives;
264  // ar >> make_nvp("num_primitives",num_primitives);
265  //
266  // delete[] bvh_model.primitive_indices;
267  // if(num_primitives > 0)
268  // {
269  // bvh_model.primitive_indices = new unsigned int[num_primitives];
270  // ar &
271  // make_nvp("primitive_indices",make_array(bvh_model.primitive_indices,num_primitives));
272  // }
273  // else
274  // bvh_model.primitive_indices = NULL;
275  // }
276 
277  bool with_bvs;
278  ar >> make_nvp("with_bvs", with_bvs);
279  if (with_bvs) {
280  unsigned int num_bvs;
281  ar >> make_nvp("num_bvs", num_bvs);
282 
283  if (num_bvs != bvh_model.num_bvs) {
284  delete[] bvh_model.bvs;
285  bvh_model.bvs = NULL;
286  bvh_model.num_bvs = num_bvs;
287  if (num_bvs > 0) bvh_model.bvs = new BVNode<BV>[num_bvs];
288  }
289  if (num_bvs > 0) {
290  ar >> make_nvp("bvs", make_array(reinterpret_cast<char *>(bvh_model.bvs),
291  sizeof(Node) * (std::size_t)num_bvs));
292  } else
293  bvh_model.bvs = NULL;
294  }
295 }
296 
297 } // namespace serialization
298 } // namespace boost
299 
300 namespace hpp {
301 namespace fcl {
302 
303 namespace internal {
304 template <typename BV>
306  static size_t run(const ::hpp::fcl::BVHModel<BV> &bvh_model) {
307  return static_cast<size_t>(bvh_model.memUsage(false));
308  }
309 };
310 } // namespace internal
311 
312 } // namespace fcl
313 } // namespace hpp
314 
315 #endif // ifndef HPP_FCL_SERIALIZATION_BVH_MODEL_H
hpp::fcl::BVHModel::num_bvs_allocated
unsigned int num_bvs_allocated
Definition: BVH/BVH_model.h:338
hpp::fcl::internal::memory_footprint_evaluator< ::hpp::fcl::BVHModel< BV > >::run
static size_t run(const ::hpp::fcl::BVHModel< BV > &bvh_model)
Definition: serialization/BVH_model.h:306
hpp::fcl::BVHModelBase::build_state
BVHBuildState build_state
The state of BVH building process.
Definition: BVH/BVH_model.h:81
hpp::fcl::Vec3f
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:66
boost::serialization::internal::BVHModelAccessor
Definition: serialization/BVH_model.h:167
boost::serialization::internal::BVHModelAccessor::Base
hpp::fcl::BVHModel< BV > Base
Definition: serialization/BVH_model.h:168
hpp::fcl::Triangle::index_type
std::size_t index_type
Definition: data_types.h:98
hpp::fcl::BVHModel::num_bvs
unsigned int num_bvs
Number of BV nodes in bounding volume hierarchy.
Definition: BVH/BVH_model.h:345
hpp::fcl::BVHModelBase::num_vertices_allocated
unsigned int num_vertices_allocated
Definition: BVH/BVH_model.h:261
HPP_FCL_SERIALIZATION_SPLIT
#define HPP_FCL_SERIALIZATION_SPLIT(Type)
Definition: fwd.h:13
collision_object.h
triangle.h
hpp::fcl::BVH_BUILD_STATE_PROCESSED
@ BVH_BUILD_STATE_PROCESSED
Definition: BVH_internal.h:54
fwd.h
boost::serialization::internal::BVHModelBaseAccessor::Base
hpp::fcl::BVHModelBase Base
Definition: serialization/BVH_model.h:22
boost
hpp::fcl::BVHModelBase::num_vertices
unsigned int num_vertices
Number of points.
Definition: BVH/BVH_model.h:78
hpp::fcl::BVHModel::bvs
BVNode< BV > * bvs
Bounding volume hierarchy.
Definition: BVH/BVH_model.h:342
hpp::fcl::BVHModelBase::tri_indices
Triangle * tri_indices
Geometry triangle index data, will be NULL for point clouds.
Definition: BVH/BVH_model.h:69
boost::serialization::load
void load(Archive &ar, hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)
Definition: serialization/BV_splitter.h:44
hpp::fcl::BVH_MODEL_TRIANGLES
@ BVH_MODEL_TRIANGLES
unknown model type
Definition: BVH_internal.h:82
version
version
hpp::fcl::BVHModelBase::prev_vertices
Vec3f * prev_vertices
Geometry point data in previous frame.
Definition: BVH/BVH_model.h:72
hpp::fcl::BVNode
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV/BV_node.h:109
hpp::fcl::BVHModelBase::getModelType
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH/BVH_model.h:87
hpp
Main namespace.
Definition: broadphase_bruteforce.h:44
BV_node.h
hpp::fcl
Definition: broadphase_bruteforce.h:45
hpp::fcl::BVHModel::primitive_indices
unsigned int * primitive_indices
Definition: BVH/BVH_model.h:339
boost::serialization::internal::BVHModelBaseAccessor
Definition: serialization/BVH_model.h:21
hpp::fcl::BVHModelBase::num_tris_allocated
unsigned int num_tris_allocated
Definition: BVH/BVH_model.h:260
hpp::fcl::BVHModel
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH/BVH_model.h:273
BVH_model.h
boost::serialization::serialize
void serialize(Archive &ar, hpp::fcl::AABB &aabb, const unsigned int)
Definition: serialization/AABB.h:15
hpp::fcl::internal::memory_footprint_evaluator
Definition: memory.h:13
hpp::fcl::BVH_BUILD_STATE_UPDATED
@ BVH_BUILD_STATE_UPDATED
Definition: BVH_internal.h:57
hpp::fcl::BVHModelBase
A base class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewe...
Definition: BVH/BVH_model.h:63
hpp::fcl::Triangle
Triangle with 3 indices for points.
Definition: data_types.h:96
BV_splitter.h
boost::serialization::save
void save(Archive &ar, const hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)
Definition: serialization/BV_splitter.h:30
memory.h
hpp::fcl::BVHModelBase::vertices
Vec3f * vertices
Geometry point data.
Definition: BVH/BVH_model.h:66
hpp::fcl::BVHModelBase::num_tris
unsigned int num_tris
Number of triangles.
Definition: BVH/BVH_model.h:75


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:13