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;
171  using Base::num_bvs_allocated;
172  using Base::primitive_indices;
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
Vec3f * prev_vertices
Geometry point data in previous frame.
Definition: BVH/BVH_model.h:72
Main namespace.
#define HPP_FCL_SERIALIZATION_SPLIT(Type)
Definition: fwd.h:13
static size_t run(const ::hpp::fcl::BVHModel< BV > &bvh_model)
void serialize(Archive &ar, hpp::fcl::AABB &aabb, const unsigned int)
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
unsigned int num_vertices
Number of points.
Definition: BVH/BVH_model.h:78
unsigned int num_vertices_allocated
unknown model type
Definition: BVH_internal.h:82
Triangle * tri_indices
Geometry triangle index data, will be NULL for point clouds.
Definition: BVH/BVH_model.h:69
Vec3f * vertices
Geometry point data.
Definition: BVH/BVH_model.h:66
unsigned int num_tris
Number of triangles.
Definition: BVH/BVH_model.h:75
void save(Archive &ar, const hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH/BVH_model.h:87
unsigned int num_tris_allocated
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV/BV_node.h:109
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Triangle with 3 indices for points.
Definition: data_types.h:96
BVHBuildState build_state
The state of BVH building process.
Definition: BVH/BVH_model.h:81
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:66
void load(Archive &ar, hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:00