GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/BVH/BV_splitter.cpp Lines: 109 120 90.8 %
Date: 2024-02-09 12:57:42 Branches: 36 84 42.9 %

Line Branch Exec Source
1
/*
2
 * Software License Agreement (BSD License)
3
 *
4
 *  Copyright (c) 2011-2014, Willow Garage, Inc.
5
 *  Copyright (c) 2014-2015, Open Source Robotics Foundation
6
 *  All rights reserved.
7
 *
8
 *  Redistribution and use in source and binary forms, with or without
9
 *  modification, are permitted provided that the following conditions
10
 *  are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *   * Redistributions in binary form must reproduce the above
15
 *     copyright notice, this list of conditions and the following
16
 *     disclaimer in the documentation and/or other materials provided
17
 *     with the distribution.
18
 *   * Neither the name of Open Source Robotics Foundation nor the names of its
19
 *     contributors may be used to endorse or promote products derived
20
 *     from this software without specific prior written permission.
21
 *
22
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
 *  POSSIBILITY OF SUCH DAMAGE.
34
 */
35
36
/** \author Jia Pan */
37
38
#include <hpp/fcl/internal/BV_splitter.h>
39
40
namespace hpp {
41
namespace fcl {
42
43
template <typename BV>
44
1129992
void computeSplitVector(const BV& bv, Vec3f& split_vector) {
45
1129992
  split_vector = bv.axes.col(0);
46
1129992
}
47
48
template <>
49
138119
void computeSplitVector<kIOS>(const kIOS& bv, Vec3f& split_vector) {
50
  /*
51
    switch(bv.num_spheres)
52
    {
53
    case 1:
54
    split_vector = Vec3f(1, 0, 0);
55
    break;
56
    case 3:
57
    {
58
    Vec3f v[3];
59
    v[0] = bv.spheres[1].o - bv.spheres[0].o;
60
    v[0].normalize();
61
    generateCoordinateSystem(v[0], v[1], v[2]);
62
    split_vector = v[1];
63
    }
64
    break;
65
    case 5:
66
    {
67
    Vec3f v[2];
68
    v[0] = bv.spheres[1].o - bv.spheres[0].o;
69
    v[1] = bv.spheres[3].o - bv.spheres[0].o;
70
    split_vector = v[0].cross(v[1]);
71
    split_vector.normalize();
72
    }
73
    break;
74
    default:
75
    ;
76
    }
77
  */
78
138119
  split_vector = bv.obb.axes.col(0);
79
138119
}
80
81
template <>
82
1012716
void computeSplitVector<OBBRSS>(const OBBRSS& bv, Vec3f& split_vector) {
83
1012716
  split_vector = bv.obb.axes.col(0);
84
1012716
}
85
86
template <typename BV>
87
541848
void computeSplitValue_bvcenter(const BV& bv, FCL_REAL& split_value) {
88
541848
  Vec3f center = bv.center();
89
541848
  split_value = center[0];
90
541848
}
91
92
template <typename BV>
93
2403722
void computeSplitValue_mean(const BV&, Vec3f* vertices, Triangle* triangles,
94
                            unsigned int* primitive_indices,
95
                            unsigned int num_primitives, BVHModelType type,
96
                            const Vec3f& split_vector, FCL_REAL& split_value) {
97
2403722
  if (type == BVH_MODEL_TRIANGLES) {
98

2403722
    Vec3f c(Vec3f::Zero());
99
100
15385106
    for (unsigned int i = 0; i < num_primitives; ++i) {
101
12981384
      const Triangle& t = triangles[primitive_indices[i]];
102
12981384
      const Vec3f& p1 = vertices[t[0]];
103
12981384
      const Vec3f& p2 = vertices[t[1]];
104
12981384
      const Vec3f& p3 = vertices[t[2]];
105
106

12981384
      c += p1 + p2 + p3;
107
    }
108
2403722
    split_value = c.dot(split_vector) / (3 * num_primitives);
109
  } else if (type == BVH_MODEL_POINTCLOUD) {
110
    FCL_REAL sum = 0;
111
    for (unsigned int i = 0; i < num_primitives; ++i) {
112
      const Vec3f& p = vertices[primitive_indices[i]];
113
      sum += p.dot(split_vector);
114
    }
115
116
    split_value = sum / num_primitives;
117
  }
118
2403722
}
119
120
template <typename BV>
121
486092
void computeSplitValue_median(const BV&, Vec3f* vertices, Triangle* triangles,
122
                              unsigned int* primitive_indices,
123
                              unsigned int num_primitives, BVHModelType type,
124
                              const Vec3f& split_vector,
125
                              FCL_REAL& split_value) {
126
972184
  std::vector<FCL_REAL> proj(num_primitives);
127
128
486092
  if (type == BVH_MODEL_TRIANGLES) {
129
3378974
    for (unsigned int i = 0; i < num_primitives; ++i) {
130
2892882
      const Triangle& t = triangles[primitive_indices[i]];
131
2892882
      const Vec3f& p1 = vertices[t[0]];
132
2892882
      const Vec3f& p2 = vertices[t[1]];
133
2892882
      const Vec3f& p3 = vertices[t[2]];
134



2892882
      Vec3f centroid3(p1[0] + p2[0] + p3[0], p1[1] + p2[1] + p3[1],
135


2892882
                      p1[2] + p2[2] + p3[2]);
136
137
2892882
      proj[i] = centroid3.dot(split_vector) / 3;
138
    }
139
  } else if (type == BVH_MODEL_POINTCLOUD) {
140
    for (unsigned int i = 0; i < num_primitives; ++i) {
141
      const Vec3f& p = vertices[primitive_indices[i]];
142
      Vec3f v(p[0], p[1], p[2]);
143
      proj[i] = v.dot(split_vector);
144
    }
145
  }
146
147
486092
  std::sort(proj.begin(), proj.end());
148
149
486092
  if (num_primitives % 2 == 1) {
150
305462
    split_value = proj[(num_primitives - 1) / 2];
151
  } else {
152
180630
    split_value = (proj[num_primitives / 2] + proj[num_primitives / 2 - 1]) / 2;
153
  }
154
486092
}
155
156
template <>
157
88424
void BVSplitter<OBB>::computeRule_bvcenter(const OBB& bv, unsigned int*,
158
                                           unsigned int) {
159
88424
  computeSplitVector<OBB>(bv, split_vector);
160
88424
  computeSplitValue_bvcenter<OBB>(bv, split_value);
161
88424
}
162
163
template <>
164
110639
void BVSplitter<OBB>::computeRule_mean(const OBB& bv,
165
                                       unsigned int* primitive_indices,
166
                                       unsigned int num_primitives) {
167
110639
  computeSplitVector<OBB>(bv, split_vector);
168
110639
  computeSplitValue_mean<OBB>(bv, vertices, tri_indices, primitive_indices,
169
110639
                              num_primitives, type, split_vector, split_value);
170
110639
}
171
172
template <>
173
88424
void BVSplitter<OBB>::computeRule_median(const OBB& bv,
174
                                         unsigned int* primitive_indices,
175
                                         unsigned int num_primitives) {
176
88424
  computeSplitVector<OBB>(bv, split_vector);
177
88424
  computeSplitValue_median<OBB>(bv, vertices, tri_indices, primitive_indices,
178
88424
                                num_primitives, type, split_vector,
179
88424
                                split_value);
180
88424
}
181
182
template <>
183
98004
void BVSplitter<RSS>::computeRule_bvcenter(const RSS& bv, unsigned int*,
184
                                           unsigned int) {
185
98004
  computeSplitVector<RSS>(bv, split_vector);
186
98004
  computeSplitValue_bvcenter<RSS>(bv, split_value);
187
98004
}
188
189
template <>
190
109379
void BVSplitter<RSS>::computeRule_mean(const RSS& bv,
191
                                       unsigned int* primitive_indices,
192
                                       unsigned int num_primitives) {
193
109379
  computeSplitVector<RSS>(bv, split_vector);
194
109379
  computeSplitValue_mean<RSS>(bv, vertices, tri_indices, primitive_indices,
195
109379
                              num_primitives, type, split_vector, split_value);
196
109379
}
197
198
template <>
199
70126
void BVSplitter<RSS>::computeRule_median(const RSS& bv,
200
                                         unsigned int* primitive_indices,
201
                                         unsigned int num_primitives) {
202
70126
  computeSplitVector<RSS>(bv, split_vector);
203
70126
  computeSplitValue_median<RSS>(bv, vertices, tri_indices, primitive_indices,
204
70126
                                num_primitives, type, split_vector,
205
70126
                                split_value);
206
70126
}
207
208
template <>
209
42248
void BVSplitter<kIOS>::computeRule_bvcenter(const kIOS& bv, unsigned int*,
210
                                            unsigned int) {
211
42248
  computeSplitVector<kIOS>(bv, split_vector);
212
42248
  computeSplitValue_bvcenter<kIOS>(bv, split_value);
213
42248
}
214
215
template <>
216
53623
void BVSplitter<kIOS>::computeRule_mean(const kIOS& bv,
217
                                        unsigned int* primitive_indices,
218
                                        unsigned int num_primitives) {
219
53623
  computeSplitVector<kIOS>(bv, split_vector);
220
53623
  computeSplitValue_mean<kIOS>(bv, vertices, tri_indices, primitive_indices,
221
53623
                               num_primitives, type, split_vector, split_value);
222
53623
}
223
224
template <>
225
42248
void BVSplitter<kIOS>::computeRule_median(const kIOS& bv,
226
                                          unsigned int* primitive_indices,
227
                                          unsigned int num_primitives) {
228
42248
  computeSplitVector<kIOS>(bv, split_vector);
229
42248
  computeSplitValue_median<kIOS>(bv, vertices, tri_indices, primitive_indices,
230
42248
                                 num_primitives, type, split_vector,
231
42248
                                 split_value);
232
42248
}
233
234
template <>
235
42248
void BVSplitter<OBBRSS>::computeRule_bvcenter(const OBBRSS& bv, unsigned int*,
236
                                              unsigned int) {
237
42248
  computeSplitVector<OBBRSS>(bv, split_vector);
238
42248
  computeSplitValue_bvcenter<OBBRSS>(bv, split_value);
239
42248
}
240
241
template <>
242
928220
void BVSplitter<OBBRSS>::computeRule_mean(const OBBRSS& bv,
243
                                          unsigned int* primitive_indices,
244
                                          unsigned int num_primitives) {
245
928220
  computeSplitVector<OBBRSS>(bv, split_vector);
246
928220
  computeSplitValue_mean<OBBRSS>(bv, vertices, tri_indices, primitive_indices,
247
928220
                                 num_primitives, type, split_vector,
248
928220
                                 split_value);
249
928220
}
250
251
template <>
252
42248
void BVSplitter<OBBRSS>::computeRule_median(const OBBRSS& bv,
253
                                            unsigned int* primitive_indices,
254
                                            unsigned int num_primitives) {
255
42248
  computeSplitVector<OBBRSS>(bv, split_vector);
256
42248
  computeSplitValue_median<OBBRSS>(bv, vertices, tri_indices, primitive_indices,
257
42248
                                   num_primitives, type, split_vector,
258
42248
                                   split_value);
259
42248
}
260
261
template <>
262
1685027
bool BVSplitter<OBB>::apply(const Vec3f& q) const {
263
1685027
  return split_vector.dot(Vec3f(q[0], q[1], q[2])) > split_value;
264
}
265
266
template <>
267
1623891
bool BVSplitter<RSS>::apply(const Vec3f& q) const {
268
1623891
  return split_vector.dot(Vec3f(q[0], q[1], q[2])) > split_value;
269
}
270
271
template <>
272
809058
bool BVSplitter<kIOS>::apply(const Vec3f& q) const {
273
809058
  return split_vector.dot(Vec3f(q[0], q[1], q[2])) > split_value;
274
}
275
276
template <>
277
4802463
bool BVSplitter<OBBRSS>::apply(const Vec3f& q) const {
278
4802463
  return split_vector.dot(Vec3f(q[0], q[1], q[2])) > split_value;
279
}
280
281
template class BVSplitter<RSS>;
282
template class BVSplitter<OBBRSS>;
283
template class BVSplitter<OBB>;
284
template class BVSplitter<kIOS>;
285
286
}  // namespace fcl
287
288
}  // namespace hpp