hpp-fcl 2.3.0
HPP fork of FCL -- The Flexible Collision Library
Loading...
Searching...
No Matches
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
17namespace boost {
18namespace serialization {
19
20namespace internal {
25};
26} // namespace internal
27
28template <class Archive>
29void 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
90template <class Archive>
91void 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
165namespace internal {
166template <typename BV>
169 using Base::bvs;
170 using Base::num_bvs;
173};
174} // namespace internal
175
176template <class Archive, typename BV>
177void serialize(Archive &ar, hpp::fcl::BVHModel<BV> &bvh_model,
178 const unsigned int version) {
179 split_free(ar, bvh_model, version);
180}
181
182template <class Archive, typename BV>
183void 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
247template <class Archive, typename BV>
248void 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
300namespace hpp {
301namespace fcl {
302
303namespace internal {
304template <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
A base class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewe...
Definition: BVH_model.h:63
Vec3f * prev_vertices
Geometry point data in previous frame.
Definition: BVH_model.h:72
unsigned int num_tris
Number of triangles.
Definition: BVH_model.h:75
Triangle * tri_indices
Geometry triangle index data, will be NULL for point clouds.
Definition: BVH_model.h:69
unsigned int num_vertices_allocated
Definition: BVH_model.h:261
BVHBuildState build_state
The state of BVH building process.
Definition: BVH_model.h:81
BVHModelType getModelType() const
Model type described by the instance.
Definition: BVH_model.h:87
Vec3f * vertices
Geometry point data.
Definition: BVH_model.h:66
unsigned int num_vertices
Number of points.
Definition: BVH_model.h:78
unsigned int num_tris_allocated
Definition: BVH_model.h:260
A class describing the bounding hierarchy of a mesh model or a point cloud model (which is viewed as ...
Definition: BVH_model.h:273
unsigned int * primitive_indices
Definition: BVH_model.h:339
BVNode< BV > * bvs
Bounding volume hierarchy.
Definition: BVH_model.h:342
unsigned int num_bvs_allocated
Definition: BVH_model.h:338
unsigned int num_bvs
Number of BV nodes in bounding volume hierarchy.
Definition: BVH_model.h:345
Triangle with 3 indices for points.
Definition: data_types.h:96
std::size_t index_type
Definition: data_types.h:98
#define HPP_FCL_SERIALIZATION_SPLIT(Type)
Definition: fwd.h:13
void save(Archive &ar, const hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)
Definition: BV_splitter.h:30
void load(Archive &ar, hpp::fcl::BVSplitter< BV > &splitter_, const unsigned int)
Definition: BV_splitter.h:44
void serialize(Archive &ar, hpp::fcl::AABB &aabb, const unsigned int)
Definition: AABB.h:15
Definition: AABB.h:11
Definition: broadphase_bruteforce.h:45
Eigen::Matrix< FCL_REAL, 3, 1 > Vec3f
Definition: data_types.h:66
Main namespace.
Definition: broadphase_bruteforce.h:44
hpp::fcl::BVHModel< BV > Base
Definition: BVH_model.h:168
hpp::fcl::BVHModelBase Base
Definition: BVH_model.h:22
A class describing a bounding volume node. It includes the tree structure providing in BVNodeBase and...
Definition: BV_node.h:109
static size_t run(const ::hpp::fcl::BVHModel< BV > &bvh_model)
Definition: BVH_model.h:306