Directory: | ./ |
---|---|
File: | include/coal/serialization/BVH_model.h |
Date: | 2025-04-01 09:23:31 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 65 | 70 | 92.9% |
Branches: | 36 | 106 | 34.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2021-2022 INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef COAL_SERIALIZATION_BVH_MODEL_H | ||
6 | #define COAL_SERIALIZATION_BVH_MODEL_H | ||
7 | |||
8 | #include "coal/BVH/BVH_model.h" | ||
9 | |||
10 | #include "coal/serialization/fwd.h" | ||
11 | #include "coal/serialization/BV_node.h" | ||
12 | #include "coal/serialization/BV_splitter.h" | ||
13 | #include "coal/serialization/collision_object.h" | ||
14 | #include "coal/serialization/memory.h" | ||
15 | #include "coal/serialization/triangle.h" | ||
16 | |||
17 | namespace boost { | ||
18 | namespace serialization { | ||
19 | |||
20 | namespace internal { | ||
21 | struct BVHModelBaseAccessor : coal::BVHModelBase { | ||
22 | typedef coal::BVHModelBase Base; | ||
23 | using Base::num_tris_allocated; | ||
24 | using Base::num_vertices_allocated; | ||
25 | }; | ||
26 | } // namespace internal | ||
27 | |||
28 | template <class Archive> | ||
29 | 18 | void save(Archive &ar, const coal::BVHModelBase &bvh_model, | |
30 | const unsigned int /*version*/) { | ||
31 | using namespace coal; | ||
32 | 36 | if (!(bvh_model.build_state == BVH_BUILD_STATE_PROCESSED || | |
33 |
2/8✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
|
18 | bvh_model.build_state == BVH_BUILD_STATE_UPDATED) && |
34 | ✗ | (bvh_model.getModelType() == BVH_MODEL_TRIANGLES)) { | |
35 | ✗ | COAL_THROW_PRETTY( | |
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 | std::invalid_argument); | ||
40 | } | ||
41 | |||
42 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | ar &make_nvp( |
43 | "base", | ||
44 | 18 | boost::serialization::base_object<coal::CollisionGeometry>(bvh_model)); | |
45 | |||
46 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("num_vertices", bvh_model.num_vertices); |
47 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("vertices", bvh_model.vertices); |
48 | |||
49 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("num_tris", bvh_model.num_tris); |
50 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("tri_indices", bvh_model.tri_indices); |
51 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("build_state", bvh_model.build_state); |
52 | |||
53 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("prev_vertices", bvh_model.prev_vertices); |
54 | |||
55 | // if(bvh_model.convex) | ||
56 | // { | ||
57 | // const bool has_convex = true; | ||
58 | // ar << make_nvp("has_convex",has_convex); | ||
59 | // } | ||
60 | // else | ||
61 | // { | ||
62 | // const bool has_convex = false; | ||
63 | // ar << make_nvp("has_convex",has_convex); | ||
64 | // } | ||
65 | 18 | } | |
66 | |||
67 | template <class Archive> | ||
68 | 18 | void load(Archive &ar, coal::BVHModelBase &bvh_model, | |
69 | const unsigned int /*version*/) { | ||
70 | using namespace coal; | ||
71 | |||
72 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | ar >> make_nvp("base", |
73 | 18 | boost::serialization::base_object<coal::CollisionGeometry>( | |
74 | bvh_model)); | ||
75 | |||
76 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("num_vertices", bvh_model.num_vertices); |
77 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("vertices", bvh_model.vertices); |
78 | |||
79 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("num_tris", bvh_model.num_tris); |
80 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("tri_indices", bvh_model.tri_indices); |
81 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("build_state", bvh_model.build_state); |
82 | |||
83 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("prev_vertices", bvh_model.prev_vertices); |
84 | |||
85 | // bool has_convex = true; | ||
86 | // ar >> make_nvp("has_convex",has_convex); | ||
87 | 18 | } | |
88 | |||
89 | 36 | COAL_SERIALIZATION_SPLIT(coal::BVHModelBase) | |
90 | |||
91 | namespace internal { | ||
92 | template <typename BV> | ||
93 | struct BVHModelAccessor : coal::BVHModel<BV> { | ||
94 | typedef coal::BVHModel<BV> Base; | ||
95 | using Base::bvs; | ||
96 | using Base::num_bvs; | ||
97 | using Base::num_bvs_allocated; | ||
98 | using Base::primitive_indices; | ||
99 | }; | ||
100 | } // namespace internal | ||
101 | |||
102 | template <class Archive, typename BV> | ||
103 | 36 | void serialize(Archive &ar, coal::BVHModel<BV> &bvh_model, | |
104 | const unsigned int version) { | ||
105 | 36 | split_free(ar, bvh_model, version); | |
106 | 36 | } | |
107 | |||
108 | template <class Archive, typename BV> | ||
109 | 18 | void save(Archive &ar, const coal::BVHModel<BV> &bvh_model_, | |
110 | const unsigned int /*version*/) { | ||
111 | using namespace coal; | ||
112 | typedef internal::BVHModelAccessor<BV> Accessor; | ||
113 | typedef BVNode<BV> Node; | ||
114 | |||
115 | 18 | const Accessor &bvh_model = reinterpret_cast<const Accessor &>(bvh_model_); | |
116 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | ar &make_nvp("base", |
117 | 18 | boost::serialization::base_object<BVHModelBase>(bvh_model)); | |
118 | |||
119 | // if(bvh_model.primitive_indices) | ||
120 | // { | ||
121 | // const bool with_primitive_indices = true; | ||
122 | // ar & make_nvp("with_primitive_indices",with_primitive_indices); | ||
123 | // | ||
124 | // int num_primitives = 0; | ||
125 | // switch(bvh_model.getModelType()) | ||
126 | // { | ||
127 | // case BVH_MODEL_TRIANGLES: | ||
128 | // num_primitives = bvh_model.num_tris; | ||
129 | // break; | ||
130 | // case BVH_MODEL_POINTCLOUD: | ||
131 | // num_primitives = bvh_model.num_vertices; | ||
132 | // break; | ||
133 | // default: | ||
134 | // ; | ||
135 | // } | ||
136 | // | ||
137 | // ar & make_nvp("num_primitives",num_primitives); | ||
138 | // if(num_primitives > 0) | ||
139 | // { | ||
140 | // typedef Eigen::Matrix<unsigned int,1,Eigen::Dynamic> | ||
141 | // AsPrimitiveIndexVector; const Eigen::Map<const | ||
142 | // AsPrimitiveIndexVector> | ||
143 | // primitive_indices_map(reinterpret_cast<const unsigned int | ||
144 | // *>(bvh_model.primitive_indices),1,num_primitives); ar & | ||
145 | // make_nvp("primitive_indices",primitive_indices_map); | ||
146 | //// ar & | ||
147 | /// make_nvp("primitive_indices",make_array(bvh_model.primitive_indices,num_primitives)); | ||
148 | // } | ||
149 | // } | ||
150 | // else | ||
151 | // { | ||
152 | // const bool with_primitive_indices = false; | ||
153 | // ar & make_nvp("with_primitive_indices",with_primitive_indices); | ||
154 | // } | ||
155 | // | ||
156 | |||
157 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | if (bvh_model.bvs.get()) { |
158 | 18 | const bool with_bvs = true; | |
159 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("with_bvs", with_bvs); |
160 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar &make_nvp("num_bvs", bvh_model.num_bvs); |
161 |
2/4✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
|
18 | ar &make_nvp( |
162 | "bvs", | ||
163 | make_array( | ||
164 | 18 | reinterpret_cast<const char *>(bvh_model.bvs->data()), | |
165 | sizeof(Node) * | ||
166 | 18 | (std::size_t)bvh_model.num_bvs)); // Assuming BVs are POD. | |
167 | } else { | ||
168 | ✗ | const bool with_bvs = false; | |
169 | ✗ | ar &make_nvp("with_bvs", with_bvs); | |
170 | } | ||
171 | 18 | } | |
172 | |||
173 | template <class Archive, typename BV> | ||
174 | 18 | void load(Archive &ar, coal::BVHModel<BV> &bvh_model_, | |
175 | const unsigned int /*version*/) { | ||
176 | using namespace coal; | ||
177 | typedef internal::BVHModelAccessor<BV> Accessor; | ||
178 | typedef BVNode<BV> Node; | ||
179 | |||
180 | 18 | Accessor &bvh_model = reinterpret_cast<Accessor &>(bvh_model_); | |
181 | |||
182 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | ar >> make_nvp("base", |
183 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
18 | boost::serialization::base_object<BVHModelBase>(bvh_model)); |
184 | |||
185 | // bool with_primitive_indices; | ||
186 | // ar >> make_nvp("with_primitive_indices",with_primitive_indices); | ||
187 | // if(with_primitive_indices) | ||
188 | // { | ||
189 | // int num_primitives; | ||
190 | // ar >> make_nvp("num_primitives",num_primitives); | ||
191 | // | ||
192 | // delete[] bvh_model.primitive_indices; | ||
193 | // if(num_primitives > 0) | ||
194 | // { | ||
195 | // bvh_model.primitive_indices = new unsigned int[num_primitives]; | ||
196 | // ar & | ||
197 | // make_nvp("primitive_indices",make_array(bvh_model.primitive_indices,num_primitives)); | ||
198 | // } | ||
199 | // else | ||
200 | // bvh_model.primitive_indices = NULL; | ||
201 | // } | ||
202 | |||
203 | bool with_bvs; | ||
204 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("with_bvs", with_bvs); |
205 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
18 | if (with_bvs) { |
206 | unsigned int num_bvs; | ||
207 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
18 | ar >> make_nvp("num_bvs", num_bvs); |
208 | |||
209 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
18 | if (num_bvs != bvh_model.num_bvs) { |
210 | 8 | bvh_model.bvs.reset(); | |
211 | 8 | bvh_model.num_bvs = num_bvs; | |
212 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
8 | if (num_bvs > 0) |
213 |
3/6✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
|
8 | bvh_model.bvs.reset(new |
214 | typename BVHModel<BV>::bv_node_vector_t(num_bvs)); | ||
215 | } | ||
216 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
18 | if (num_bvs > 0) { |
217 |
2/4✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
|
18 | ar >> make_nvp("bvs", |
218 | 18 | make_array(reinterpret_cast<char *>(bvh_model.bvs->data()), | |
219 | 18 | sizeof(Node) * (std::size_t)num_bvs)); | |
220 | } else | ||
221 | ✗ | bvh_model.bvs.reset(); | |
222 | } | ||
223 | 18 | } | |
224 | |||
225 | } // namespace serialization | ||
226 | } // namespace boost | ||
227 | |||
228 | namespace coal { | ||
229 | |||
230 | namespace internal { | ||
231 | template <typename BV> | ||
232 | struct memory_footprint_evaluator<::coal::BVHModel<BV>> { | ||
233 | 3 | static size_t run(const ::coal::BVHModel<BV> &bvh_model) { | |
234 | 3 | return static_cast<size_t>(bvh_model.memUsage(false)); | |
235 | } | ||
236 | }; | ||
237 | } // namespace internal | ||
238 | |||
239 | } // namespace coal | ||
240 | |||
241 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::AABB>) | |
242 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::OBB>) | |
243 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::RSS>) | |
244 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::OBBRSS>) | |
245 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::kIOS>) | |
246 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::KDOP<16>>) | |
247 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::KDOP<18>>) | |
248 | 36 | COAL_SERIALIZATION_DECLARE_EXPORT(::coal::BVHModel<::coal::KDOP<24>>) | |
249 | |||
250 | #endif // ifndef COAL_SERIALIZATION_BVH_MODEL_H | ||
251 |