38 #ifndef FCL_BV_SPLITTER_INL_H 39 #define FCL_BV_SPLITTER_INL_H 41 #include "fcl/geometry/bvh/detail/BV_splitter.h" 50 template <
typename BV>
51 BVSplitter<BV>::BVSplitter(SplitMethodType method)
52 : split_method(method)
58 template <
typename BV>
65 template <
typename BV>
70 tri_indices = tri_indices_;
75 template <
typename BV>
77 const BV& bv,
unsigned int* primitive_indices,
int num_primitives)
81 case SPLIT_METHOD_MEAN:
82 computeRule_mean(bv, primitive_indices, num_primitives);
84 case SPLIT_METHOD_MEDIAN:
85 computeRule_median(bv, primitive_indices, num_primitives);
87 case SPLIT_METHOD_BV_CENTER:
88 computeRule_bvcenter(bv, primitive_indices, num_primitives);
91 std::cerr <<
"Split method not supported" << std::endl;
96 template <
typename S,
typename BV>
102 return q[splitter.split_axis] > splitter.split_value;
107 template <
typename BV>
114 template <
typename S,
typename BV>
123 Vector3<S> center = bv.center();
126 if(bv.width() >= bv.height() && bv.width() >= bv.depth())
128 else if(bv.height() >= bv.width() && bv.height() >= bv.depth())
131 splitter.split_axis = axis;
132 splitter.split_value = center[axis];
137 template <
typename BV>
139 const BV& bv,
unsigned int* primitive_indices,
int num_primitives)
142 *
this, bv, primitive_indices, num_primitives);
146 template <
typename S,
typename BV>
152 unsigned int* primitive_indices,
157 if(bv.width() >= bv.height() && bv.width() >= bv.depth())
159 else if(bv.height() >= bv.width() && bv.height() >= bv.depth())
162 splitter.split_axis = axis;
167 for(
int i = 0; i < num_primitives; ++i)
169 const Triangle& t = splitter.tri_indices[primitive_indices[i]];
170 sum += splitter.vertices[t[0]][splitter.split_axis]
171 + splitter.vertices[t[1]][splitter.split_axis]
172 + splitter.vertices[t[2]][splitter.split_axis];
179 for(
int i = 0; i < num_primitives; ++i)
181 sum += splitter.vertices[primitive_indices[i]][splitter.split_axis];
185 splitter.split_value = sum / num_primitives;
190 template <
typename BV>
192 const BV& bv,
unsigned int* primitive_indices,
int num_primitives)
195 *
this, bv, primitive_indices, num_primitives);
199 template <
typename S,
typename BV>
205 unsigned int* primitive_indices,
210 if(bv.width() >= bv.height() && bv.width() >= bv.depth())
212 else if(bv.height() >= bv.width() && bv.height() >= bv.depth())
215 splitter.split_axis = axis;
216 std::vector<S> proj(num_primitives);
220 for(
int i = 0; i < num_primitives; ++i)
222 const Triangle& t = splitter.tri_indices[primitive_indices[i]];
223 proj[i] = (splitter.vertices[t[0]][splitter.split_axis]
224 + splitter.vertices[t[1]][splitter.split_axis]
225 + splitter.vertices[t[2]][splitter.split_axis]) / 3;
230 for(
int i = 0; i < num_primitives; ++i)
231 proj[i] = splitter.vertices[primitive_indices[i]][splitter.split_axis];
234 std::sort(proj.begin(), proj.end());
236 if(num_primitives % 2 == 1)
238 splitter.split_value = proj[(num_primitives - 1) / 2];
242 splitter.split_value = (proj[num_primitives / 2] + proj[num_primitives / 2 - 1]) / 2;
248 template <
typename BV>
250 const BV& bv,
unsigned int* primitive_indices,
int num_primitives)
253 *
this, bv, primitive_indices, num_primitives);
257 template <
typename S>
266 computeSplitVector<S, OBB<S>>(bv, splitter.split_vector);
267 computeSplitValue_bvcenter<S, OBB<S>>(bv, splitter.split_value);
272 template <
typename S>
278 unsigned int* primitive_indices,
281 computeSplitVector<S, OBB<S>>(bv, splitter.split_vector);
282 computeSplitValue_mean<S, OBB<S>>(
283 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
284 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
289 template <
typename S>
295 unsigned int* primitive_indices,
298 computeSplitVector<S, OBB<S>>(bv, splitter.split_vector);
299 computeSplitValue_median<S, OBB<S>>(
300 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
301 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
306 template <
typename S>
315 computeSplitVector<S, RSS<S>>(bv, splitter.split_vector);
316 computeSplitValue_bvcenter<S, RSS<S>>(bv, splitter.split_value);
321 template <
typename S>
327 unsigned int* primitive_indices,
330 computeSplitVector<S, RSS<S>>(bv, splitter.split_vector);
331 computeSplitValue_mean<S, RSS<S>>(
332 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
333 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
338 template <
typename S>
344 unsigned int* primitive_indices,
347 computeSplitVector<S, RSS<S>>(bv, splitter.split_vector);
348 computeSplitValue_median<S, RSS<S>>(
349 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
350 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
355 template <
typename S>
364 computeSplitVector<S, kIOS<S>>(bv, splitter.split_vector);
365 computeSplitValue_bvcenter<S, kIOS<S>>(bv, splitter.split_value);
370 template <
typename S>
376 unsigned int* primitive_indices,
379 computeSplitVector<S, kIOS<S>>(bv, splitter.split_vector);
380 computeSplitValue_mean<S, kIOS<S>>(
381 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
382 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
387 template <
typename S>
393 unsigned int* primitive_indices,
396 computeSplitVector<S, kIOS<S>>(bv, splitter.split_vector);
397 computeSplitValue_median<S, kIOS<S>>(
398 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
399 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
404 template <
typename S>
413 computeSplitVector<S, OBBRSS<S>>(bv, splitter.split_vector);
414 computeSplitValue_bvcenter<S, OBBRSS<S>>(bv, splitter.split_value);
419 template <
typename S>
425 unsigned int* primitive_indices,
428 computeSplitVector<S, OBBRSS<S>>(bv, splitter.split_vector);
429 computeSplitValue_mean<S, OBBRSS<S>>(
430 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
431 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
436 template <
typename S>
442 unsigned int* primitive_indices,
445 computeSplitVector<S, OBBRSS<S>>(bv, splitter.split_vector);
446 computeSplitValue_median<S, OBBRSS<S>>(
447 bv, splitter.vertices, splitter.tri_indices, primitive_indices,
448 num_primitives, splitter.type, splitter.split_vector, splitter.split_value);
453 template <
typename S>
460 return splitter.split_vector.dot(q) > splitter.split_value;
465 template <
typename S>
472 return splitter.split_vector.dot(q) > splitter.split_value;
477 template <
typename S>
484 return splitter.split_vector.dot(q) > splitter.split_value;
489 template <
typename S>
496 return splitter.split_vector.dot(q) > splitter.split_value;
501 template <
typename BV>
505 tri_indices =
nullptr;
506 type = BVH_MODEL_UNKNOWN;
510 template <
typename S,
typename BV>
513 static void run(
const BV& bv, Vector3<S>& split_vector)
515 split_vector = bv.axis.col(0);
520 template <
typename S,
typename BV>
521 void computeSplitVector(
const BV& bv, Vector3<S>& split_vector)
527 template <
typename S>
530 static void run(
const kIOS<S>& bv, Vector3<S>& split_vector)
560 split_vector = bv.
obb.
axis.col(0);
565 template <
typename S>
568 static void run(
const OBBRSS<S>& bv, Vector3<S>& split_vector)
570 split_vector = bv.
obb.
axis.col(0);
575 template <
typename S,
typename BV>
576 void computeSplitValue_bvcenter(
const BV& bv, S& split_value)
578 Vector3<S> center = bv.center();
579 split_value = center[0];
583 template <
typename S,
typename BV>
584 void computeSplitValue_mean(
586 Vector3<S>* vertices,
588 unsigned int* primitive_indices,
591 const Vector3<S>& split_vector,
597 S c[3] = {0.0, 0.0, 0.0};
599 for(
int i = 0; i < num_primitives; ++i)
601 const Triangle& t = triangles[primitive_indices[i]];
602 const Vector3<S>& p1 = vertices[t[0]];
603 const Vector3<S>& p2 = vertices[t[1]];
604 const Vector3<S>& p3 = vertices[t[2]];
606 c[0] += (p1[0] + p2[0] + p3[0]);
607 c[1] += (p1[1] + p2[1] + p3[1]);
608 c[2] += (p1[2] + p2[2] + p3[2]);
610 split_value = (c[0] * split_vector[0] + c[1] * split_vector[1] + c[2] * split_vector[2]) / (3 * num_primitives);
614 for(
int i = 0; i < num_primitives; ++i)
616 const Vector3<S>& p = vertices[primitive_indices[i]];
617 Vector3<S> v(p[0], p[1], p[2]);
618 sum += v.dot(split_vector);
621 split_value = sum / num_primitives;
626 template <
typename S,
typename BV>
627 void computeSplitValue_median(
629 Vector3<S>* vertices,
631 unsigned int* primitive_indices,
634 const Vector3<S>& split_vector,
637 std::vector<S> proj(num_primitives);
641 for(
int i = 0; i < num_primitives; ++i)
643 const Triangle& t = triangles[primitive_indices[i]];
644 const Vector3<S>& p1 = vertices[t[0]];
645 const Vector3<S>& p2 = vertices[t[1]];
646 const Vector3<S>& p3 = vertices[t[2]];
647 Vector3<S> centroid3(p1[0] + p2[0] + p3[0],
648 p1[1] + p2[1] + p3[1],
649 p1[2] + p2[2] + p3[2]);
651 proj[i] = centroid3.dot(split_vector) / 3;
656 for(
int i = 0; i < num_primitives; ++i)
658 const Vector3<S>& p = vertices[primitive_indices[i]];
659 Vector3<S> v(p[0], p[1], p[2]);
660 proj[i] = v.dot(split_vector);
664 std::sort(proj.begin(), proj.end());
666 if(num_primitives % 2 == 1)
668 split_value = proj[(num_primitives - 1) / 2];
672 split_value = (proj[num_primitives / 2] + proj[num_primitives / 2 - 1]) / 2;
void set(Vector3< S > *vertices_, Triangle *tri_indices_, BVHModelType type_)
Set the geometry data needed by the split rule.
Definition: BV_splitter-inl.h:66
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
triangle model
Definition: BVH_internal.h:79
BVHModelType
BVH model type.
Definition: BVH_internal.h:75
virtual ~BVSplitter()
Default deconstructor.
Definition: BV_splitter-inl.h:59
void clear()
Clear the geometry data set before.
Definition: BV_splitter-inl.h:502
Triangle with 3 indices for points.
Definition: triangle.h:47
bool apply(const Vector3< S > &q) const
Apply the split rule on a given point.
Definition: BV_splitter-inl.h:108
unknown model type
Definition: BVH_internal.h:78
OBB< S > obb
OBB related with kIOS.
Definition: kIOS.h:72
void computeRule(const BV &bv, unsigned int *primitive_indices, int num_primitives)
Compute the split rule according to a subset of geometry and the corresponding BV node...
Definition: BV_splitter-inl.h:76
Matrix3< S > axis
Orientation of OBB. The axes of the rotation matrix are the principle directions of the box...
Definition: OBB.h:62
Definition: BV_splitter-inl.h:97
Definition: BV_splitter-inl.h:147
A class describing the split rule that splits each BV node.
Definition: BV_splitter.h:65
Definition: BV_splitter-inl.h:115
Definition: BV_splitter-inl.h:511
A class describing the kIOS collision structure, which is a set of spheres.
Definition: kIOS.h:48
Oriented bounding box class.
Definition: OBB.h:51