BVH_model.h 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
//
// Copyright (c) 2021 INRIA
//

#ifndef HPP_FCL_SERIALIZATION_BVH_MODEL_H
#define HPP_FCL_SERIALIZATION_BVH_MODEL_H

#include "hpp/fcl/BVH/BVH_model.h"
9

10
#include "hpp/fcl/serialization/fwd.h"
11
12
#include "hpp/fcl/serialization/BV_node.h"
#include "hpp/fcl/serialization/BV_splitter.h"
13
#include "hpp/fcl/serialization/collision_object.h"
14
#include "hpp/fcl/serialization/memory.h"
15
16
17
18
19
20

namespace boost
{
  namespace serialization
  {
  
21
22
23
24
25
26
27
28
29
30
    namespace internal
    {
      struct BVHModelBaseAccessor : hpp::fcl::BVHModelBase
      {
        typedef hpp::fcl::BVHModelBase Base;
        using Base::num_tris_allocated;
        using Base::num_vertices_allocated;
      };
    }
  
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    template <class Archive>
    void serialize(Archive & ar,
                   hpp::fcl::Triangle & triangle,
                   const unsigned int /*version*/)
    {
      ar & make_nvp("p0",triangle[0]);
      ar & make_nvp("p1",triangle[1]);
      ar & make_nvp("p2",triangle[2]);
    }
  
    template <class Archive>
    void save(Archive & ar,
              const hpp::fcl::BVHModelBase & bvh_model,
              const unsigned int /*version*/)
    {
46
      using namespace hpp::fcl;
47
48
      if(!(bvh_model.build_state == BVH_BUILD_STATE_PROCESSED || bvh_model.build_state == BVH_BUILD_STATE_UPDATED)
         && (bvh_model.getModelType() == BVH_MODEL_TRIANGLES))
49
      {
50
51
        throw std::invalid_argument("The BVH model is not in a BVH_BUILD_STATE_PROCESSED or BVH_BUILD_STATE_UPDATED state.\n"
                                    "The BVHModel could not be serialized.");
52
53
54
      }
      
      ar & make_nvp("base",boost::serialization::base_object<hpp::fcl::CollisionGeometry>(bvh_model));
55

56
      ar & make_nvp("num_vertices",bvh_model.num_vertices);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      if(bvh_model.num_vertices > 0)
      {
        typedef Eigen::Matrix<FCL_REAL,3,Eigen::Dynamic> AsVertixMatrix;
        const Eigen::Map<const AsVertixMatrix> vertices_map(reinterpret_cast<const double *>(bvh_model.vertices),3,bvh_model.num_vertices);
        ar & make_nvp("vertices",vertices_map);
      }

      ar & make_nvp("num_tris",bvh_model.num_tris);
      if(bvh_model.num_tris > 0)
      {
        typedef Eigen::Matrix<Triangle::index_type,3,Eigen::Dynamic> AsTriangleMatrix;
        const Eigen::Map<const AsTriangleMatrix> tri_indices_map(reinterpret_cast<const Triangle::index_type *>(bvh_model.tri_indices),3,bvh_model.num_tris);
        ar & make_nvp("tri_indices",tri_indices_map);
      }
71
72
73
74
75
76
      ar & make_nvp("build_state",bvh_model.build_state);
      
      if(bvh_model.prev_vertices)
      {
        const bool has_prev_vertices = true;
        ar << make_nvp("has_prev_vertices",has_prev_vertices);
77
78
79
        typedef Eigen::Matrix<FCL_REAL,3,Eigen::Dynamic> AsVertixMatrix;
        const Eigen::Map<const AsVertixMatrix> prev_vertices_map(reinterpret_cast<const double *>(bvh_model.prev_vertices),3,bvh_model.num_vertices);
        ar & make_nvp("prev_vertices",prev_vertices_map);
80
81
82
83
84
85
      }
      else
      {
        const bool has_prev_vertices = false;
        ar & make_nvp("has_prev_vertices",has_prev_vertices);
      }
86
      
87
88
89
90
91
92
93
94
95
96
//      if(bvh_model.convex)
//      {
//        const bool has_convex = true;
//        ar << make_nvp("has_convex",has_convex);
//      }
//      else
//      {
//        const bool has_convex = false;
//        ar << make_nvp("has_convex",has_convex);
//      }
97
98
99
100
101
102
103
104
    }
    
    template <class Archive>
    void load(Archive & ar,
              hpp::fcl::BVHModelBase & bvh_model,
              const unsigned int /*version*/)
    {
      using namespace hpp::fcl;
105
106
      
      ar >> make_nvp("base",boost::serialization::base_object<hpp::fcl::CollisionGeometry>(bvh_model));
107
      
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
      int num_vertices;
      ar >> make_nvp("num_vertices",num_vertices);
      if(num_vertices != bvh_model.num_vertices)
      {
        delete[] bvh_model.vertices;
        bvh_model.vertices = NULL;
        bvh_model.num_vertices = num_vertices;
        if(num_vertices > 0)
          bvh_model.vertices = new Vec3f[num_vertices];
      }
      if(num_vertices > 0)
      {
        typedef Eigen::Matrix<FCL_REAL,3,Eigen::Dynamic> AsVertixMatrix;
        Eigen::Map<AsVertixMatrix> vertices_map(reinterpret_cast<double *>(bvh_model.vertices),3,bvh_model.num_vertices);
        ar >> make_nvp("vertices",vertices_map);
      }
124
125
126
      else
        bvh_model.vertices = NULL;
      
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
      int num_tris;
      ar >> make_nvp("num_tris",num_tris);
      
      if(num_tris != bvh_model.num_tris)
      {
        delete[] bvh_model.tri_indices;
        bvh_model.tri_indices = NULL;
        bvh_model.num_tris = num_tris;
        if(num_tris> 0)
          bvh_model.tri_indices = new Triangle[num_tris];
      }
      if(num_tris > 0)
      {
        typedef Eigen::Matrix<Triangle::index_type,3,Eigen::Dynamic> AsTriangleMatrix;
        Eigen::Map<AsTriangleMatrix> tri_indices_map(reinterpret_cast<Triangle::index_type *>(bvh_model.tri_indices),3,bvh_model.num_tris);
        ar & make_nvp("tri_indices",tri_indices_map);
      }
      else
        bvh_model.tri_indices = NULL;

147
148
      ar >> make_nvp("build_state",bvh_model.build_state);
      
149
      typedef internal::BVHModelBaseAccessor Accessor;
150
151
152
      reinterpret_cast<Accessor &>(bvh_model).num_tris_allocated = num_tris;
      reinterpret_cast<Accessor &>(bvh_model).num_vertices_allocated = num_vertices;

153
154
155
156
      bool has_prev_vertices;
      ar >> make_nvp("has_prev_vertices",has_prev_vertices);
      if(has_prev_vertices)
      {
157
158
159
160
161
162
163
164
165
166
167
168
169
        if(num_vertices != bvh_model.num_vertices)
        {
          delete[] bvh_model.prev_vertices;
          bvh_model.prev_vertices = NULL;
          if(num_vertices > 0)
            bvh_model.prev_vertices = new Vec3f[num_vertices];
        }
        if(num_vertices > 0)
        {
          typedef Eigen::Matrix<FCL_REAL,3,Eigen::Dynamic> AsVertixMatrix;
          Eigen::Map<AsVertixMatrix> prev_vertices_map(reinterpret_cast<double *>(bvh_model.prev_vertices),3,bvh_model.num_vertices);
          ar & make_nvp("prev_vertices",prev_vertices_map);
        }
170
      }
171
172
      else
        bvh_model.prev_vertices = NULL;
173
      
174
175
//      bool has_convex = true;
//      ar >> make_nvp("has_convex",has_convex);
176
177
178
179
    }
    
    HPP_FCL_SERIALIZATION_SPLIT(hpp::fcl::BVHModelBase)
  
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    namespace internal
    {
      template<typename BV>
      struct BVHModelAccessor : hpp::fcl::BVHModel<BV>
      {
        typedef hpp::fcl::BVHModel<BV> Base;
        using Base::num_bvs_allocated;
        using Base::primitive_indices;
        using Base::bvs;
        using Base::num_bvs;
      };
    }
  
    template <class Archive, typename BV>
    void serialize(Archive & ar,
                   hpp::fcl::BVHModel<BV> & bvh_model,
                   const unsigned int version)
    {
      split_free(ar,bvh_model,version);
    }
  
    template <class Archive, typename BV>
    void save(Archive & ar,
              const hpp::fcl::BVHModel<BV> & bvh_model_,
              const unsigned int /*version*/)
    {
      using namespace hpp::fcl;
      typedef internal::BVHModelAccessor<BV> Accessor;
208
      typedef BVNode<BV> Node;
209
210
211
212
      
      const Accessor & bvh_model = reinterpret_cast<const Accessor &>(bvh_model_);
      ar & make_nvp("base",boost::serialization::base_object<BVHModelBase>(bvh_model));
      
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//      if(bvh_model.primitive_indices)
//      {
//        const bool with_primitive_indices = true;
//        ar & make_nvp("with_primitive_indices",with_primitive_indices);
//
//        int num_primitives = 0;
//        switch(bvh_model.getModelType())
//        {
//          case BVH_MODEL_TRIANGLES:
//            num_primitives = bvh_model.num_tris;
//            break;
//          case BVH_MODEL_POINTCLOUD:
//            num_primitives = bvh_model.num_vertices;
//            break;
//          default:
//            ;
//        }
//
//        ar & make_nvp("num_primitives",num_primitives);
//        if(num_primitives > 0)
//        {
//          typedef Eigen::Matrix<unsigned int,1,Eigen::Dynamic> AsPrimitiveIndexVector;
//          const Eigen::Map<const AsPrimitiveIndexVector> primitive_indices_map(reinterpret_cast<const unsigned int *>(bvh_model.primitive_indices),1,num_primitives);
//          ar & make_nvp("primitive_indices",primitive_indices_map);
////          ar & make_nvp("primitive_indices",make_array(bvh_model.primitive_indices,num_primitives));
//        }
//      }
//      else
//      {
//        const bool with_primitive_indices = false;
//        ar & make_nvp("with_primitive_indices",with_primitive_indices);
//      }
//
246
247
248
249
250
251
      
      if(bvh_model.bvs)
      {
        const bool with_bvs = true;
        ar & make_nvp("with_bvs",with_bvs);
        ar & make_nvp("num_bvs",bvh_model.num_bvs);
252
        ar & make_nvp("bvs",make_array(reinterpret_cast<const char *>(bvh_model.bvs),sizeof(Node)*(std::size_t)bvh_model.num_bvs)); // Assuming BVs are POD.
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
      }
      else
      {
        const bool with_bvs = false;
        ar & make_nvp("with_bvs",with_bvs);
      }
    }
  
    template <class Archive, typename BV>
    void load(Archive & ar,
              hpp::fcl::BVHModel<BV> & bvh_model_,
              const unsigned int /*version*/)
    {
      using namespace hpp::fcl;
      typedef internal::BVHModelAccessor<BV> Accessor;
268
      typedef BVNode<BV> Node;
269
270
271
272
273
      
      Accessor & bvh_model = reinterpret_cast<Accessor &>(bvh_model_);
      
      ar >> make_nvp("base",boost::serialization::base_object<BVHModelBase>(bvh_model));
      
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//      bool with_primitive_indices;
//      ar >> make_nvp("with_primitive_indices",with_primitive_indices);
//      if(with_primitive_indices)
//      {
//        int num_primitives;
//        ar >> make_nvp("num_primitives",num_primitives);
//
//        delete[] bvh_model.primitive_indices;
//        if(num_primitives > 0)
//        {
//          bvh_model.primitive_indices = new unsigned int[num_primitives];
//          ar & make_nvp("primitive_indices",make_array(bvh_model.primitive_indices,num_primitives));
//        }
//        else
//          bvh_model.primitive_indices = NULL;
//      }
290
291
292
293
294
      
      bool with_bvs;
      ar >> make_nvp("with_bvs",with_bvs);
      if(with_bvs)
      {
295
296
297
298
299
300
301
302
303
304
305
306
        int num_bvs;
        ar >> make_nvp("num_bvs",num_bvs);
        
        if(num_bvs != bvh_model.num_bvs)
        {
          delete[] bvh_model.bvs;
          bvh_model.bvs = NULL;
          bvh_model.num_bvs = num_bvs;
          if(num_bvs > 0)
            bvh_model.bvs = new BVNode<BV>[num_bvs];
        }
        if(num_bvs > 0)
307
        {
308
          ar >> make_nvp("bvs",make_array(reinterpret_cast<char *>(bvh_model.bvs),sizeof(Node)*(std::size_t)num_bvs));
309
310
311
312
313
314
        }
        else
          bvh_model.bvs = NULL;
      }
    }
  
315
316
317
  }
}

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
namespace hpp {
namespace fcl {

namespace internal
{
  template<typename BV>
  struct memory_footprint_evaluator< ::hpp::fcl::BVHModel<BV> >
  {
    static size_t run(const ::hpp::fcl::BVHModel<BV> & bvh_model)
    {
      return static_cast<size_t>(bvh_model.memUsage(false));
    }
  };
}

} // namespace fcl
} // namespace hpp

336
#endif // ifndef HPP_FCL_SERIALIZATION_BVH_MODEL_H