48 #include <pcl/pcl_exports.h>
64 template<
class UserData>
125 Node (std::vector<BoundedObject*>& sorted_objects,
int first_id,
int last_id)
128 auto firstBounds = sorted_objects[first_id]->getBounds();
129 std::copy(firstBounds, firstBounds + 6, bounds_);
132 for (
int i = first_id + 1 ; i <= last_id ; ++i )
138 if ( first_id != last_id )
141 int mid_id = (first_id + last_id) >> 1;
142 children_[0] =
new Node(sorted_objects, first_id, mid_id);
143 children_[1] =
new Node(sorted_objects, mid_id + 1, last_id);
148 object_ = sorted_objects[first_id];
149 children_[0] = children_[1] =
nullptr;
162 return static_cast<bool>(children_[0]);
186 return !
static_cast<bool>(children_[0]);
193 return (box[1] >= bounds_[0] && box[3] >= bounds_[2] && box[5] >= bounds_[4] &&
194 box[0] <= bounds_[1] && box[2] <= bounds_[3] && box[4] <= bounds_[5]);
201 return (bounds_[1] - bounds_[0]) * (bounds_[3] - bounds_[2]) * (bounds_[5] - bounds_[4]);
215 sorted_objects_ (nullptr)
231 build(std::vector<BoundedObject*>& objects)
235 if ( objects.empty () )
238 sorted_objects_ = &objects;
241 std::sort (objects.begin (), objects.end (), BoundedObject::compareCentroidsXCoordinates);
244 root_ =
new Node (objects, 0,
static_cast<int> (objects.size () - 1));
255 inline const std::vector<BoundedObject*>*
258 return (sorted_objects_);
264 intersect(
const float box[6], std::list<BoundedObject*>& intersected_objects)
const
269 bool got_intersection =
false;
272 std::list<Node*> working_list;
273 working_list.push_back (root_);
275 while ( !working_list.empty () )
277 Node* node = working_list.front ();
278 working_list.pop_front ();
291 intersected_objects.push_back (node->
getObject ());
292 got_intersection =
true;
297 return (got_intersection);
static bool compareCentroidsXCoordinates(const BoundedObject *a, const BoundedObject *b)
This method is for std::sort.
UserData data_
This is the user-defined data object.
BoundedObject(const UserData &data)
const float * getCentroid() const
virtual ~BoundedObject()=default
BoundedObject * getObject()
Node(std::vector< BoundedObject * > &sorted_objects, int first_id, int last_id)
'sorted_objects' is a sorted vector of bounded objects.
bool intersect(const float box[6]) const
Returns true if 'box' intersects or touches (with a side or a vertex) this node.
double computeBoundingBoxVolume() const
Computes and returns the volume of the bounding box of this node.
This class is an implementation of bounding volume hierarchies.
const std::vector< BoundedObject * > * getInputObjects() const
std::vector< BoundedObject * > * sorted_objects_
void clear()
Frees the memory allocated by this object.
bool intersect(const float box[6], std::list< BoundedObject * > &intersected_objects) const
Pushes back in 'intersected_objects' the bounded objects intersected by the input 'box' and returns t...
void build(std::vector< BoundedObject * > &objects)
Creates the tree.
void expandBoundingBox(T dst[6], const T src[6])
Expands the destination bounding box 'dst' such that it contains 'src'.