UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_AABB_tree.hh
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2017 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi\unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Utils_AABBtree.hh
22//
23
24
25#pragma once
26
27#ifndef UTILS_AABB_TREE_dot_HH
28#define UTILS_AABB_TREE_dot_HH
29
30#include "Utils.hh"
31
32#include <string>
33#include <vector>
34#include <set>
35#include <map>
36
37namespace Utils {
38
39 using std::string;
40 using std::vector;
41 using std::set;
42 using std::map;
43
44 /*\
45 | _ _ ____ ____ _
46 | / \ / \ | __ )| __ )| |_ _ __ ___ ___
47 | / _ \ / _ \ | _ \| _ \| __| '__/ _ \/ _ \
48 | / ___ \ / ___ \| |_) | |_) | |_| | | __/ __/
49 | /_/ \_\/_/ \_\____/|____/ \__|_| \___|\___|
50 \*/
51
62 template <typename Real>
63 class AABBtree {
64 public:
65
66 using integer = int;
67 using AABB_SET = set<integer>;
68 using AABB_MAP = map<integer,AABB_SET>;
69
70 private:
71
72 Malloc<Real> m_rmem{"AABBtree_real"};
73 Malloc<integer> m_imem{"AABBtree_integer"};
74
75 // AABBtree structure
76 integer m_dim{0};
77 integer m_2dim{0};
78 integer m_num_objects{0};
79 integer m_num_tree_nodes{0};
80
81 integer * m_father{nullptr}; // m_nmax
82 integer * m_child{nullptr}; // m_nmax
83 integer * m_ptr_nodes{nullptr}; // m_nmax
84 integer * m_num_nodes{nullptr}; // m_nmax
85 integer * m_id_nodes{nullptr}; // m_num_objects
86 Real * m_bbox_tree{nullptr}; // m_nmax*m_2dim
87 Real * m_bbox_objs{nullptr}; // m_num_objects*m_2dim
88
89 mutable vector<integer> m_stack;
90
91 integer m_nmax{0};
92
93 // parameters
94 integer m_max_num_objects_per_node{16};
95 Real m_bbox_long_edge_ratio{Real(0.8)};
96 Real m_bbox_overlap_tolerance{Real(0.1)};
97 Real m_bbox_min_size_tolerance{Real(0)};
98
99 // statistic
100 mutable integer m_num_check = 0;
101
102 using OVERLAP_FUN = bool (*) ( Real const bbox1[], Real const bbox2[], integer dim );
103
104 OVERLAP_FUN m_check_overlap{nullptr};
105 OVERLAP_FUN m_check_overlap_with_point{nullptr};
106
107 static bool overlap1( Real const bbox1[], Real const bbox2[], integer dim );
108 static bool overlap2( Real const bbox1[], Real const bbox2[], integer dim );
109 static bool overlap3( Real const bbox1[], Real const bbox2[], integer dim );
110 static bool overlap4( Real const bbox1[], Real const bbox2[], integer dim );
111 static bool overlap5( Real const bbox1[], Real const bbox2[], integer dim );
112 static bool overlap6( Real const bbox1[], Real const bbox2[], integer dim );
113 static bool overlap7( Real const bbox1[], Real const bbox2[], integer dim );
114 static bool overlap8( Real const bbox1[], Real const bbox2[], integer dim );
115
116 static bool pnt_overlap1( Real const pnt[], Real const bbox2[], integer dim );
117 static bool pnt_overlap2( Real const pnt[], Real const bbox2[], integer dim );
118 static bool pnt_overlap3( Real const pnt[], Real const bbox2[], integer dim );
119 static bool pnt_overlap4( Real const pnt[], Real const bbox2[], integer dim );
120 static bool pnt_overlap5( Real const pnt[], Real const bbox2[], integer dim );
121 static bool pnt_overlap6( Real const pnt[], Real const bbox2[], integer dim );
122 static bool pnt_overlap7( Real const pnt[], Real const bbox2[], integer dim );
123 static bool pnt_overlap8( Real const pnt[], Real const bbox2[], integer dim );
124
125 static bool check_overlap( Real const bb1[], Real const bb2[], integer dim );
126 static bool check_overlap_with_point( Real const bb1[], Real const pnt[], integer dim );
127
128 Real max_bbox_distance( Real const bbox[], Real const pnt[] ) const;
129
130 public:
131
133 AABBtree() = default;
134
140 AABBtree( AABBtree<Real> const & t );
141
148
154 void set_bbox_long_edge_ratio( Real ratio );
155
161 void set_bbox_overlap_tolerance( Real tol );
162
168 void set_bbox_min_size_tolerance( Real tol );
169
176 void allocate( integer nbox, integer dim );
177
186 void
188 Real const bb_min[], integer ldim0,
189 Real const bb_max[], integer ldim1
190 );
191
199 void
201 Real const bbox_min[],
202 Real const bbox_max[],
203 integer ipos
204 );
205
209 void build();
210
221 void
223 Real const bb_min[], integer ldim0,
224 Real const bb_max[], integer ldim1,
225 integer nbox,
227 ) {
228 allocate( nbox, dim );
229 add_bboxes( bb_min, ldim0, bb_max, ldim1 );
230 build();
231 }
232
239 void intersect_with_one_point( Real const pnt[], AABB_SET & bb_index ) const;
240
247 void intersect_with_one_bbox( Real const bbox[], AABB_SET & bb_index ) const;
248
249
256 void intersect( AABBtree<Real> const & aabb, AABB_MAP & bb_index ) const;
257
264 void intersect_with_one_point_and_refine( Real const pnt[], AABB_SET & bb_index ) const;
265
272 void intersect_with_one_bbox_and_refine( Real const bbox[], AABB_SET & bb_index ) const;
273
274
281 void intersect_and_refine( AABBtree<Real> const & aabb, AABB_MAP & bb_index ) const;
282
289 void min_distance_candidates( Real const pnt[], AABB_SET & bb_index ) const;
290
299 void pnt_bbox_minmax( Real const pnt[], Real const bbox[], Real & dmin, Real & dmax ) const;
300
306 integer dim() const { return m_dim; }
307
313 integer num_objects() const { return m_num_objects; }
314
320 integer num_tree_nodes() const { return m_num_tree_nodes; }
321
327 integer num_check() const { return m_num_check; }
328
335 integer num_tree_nodes( integer nmin ) const;
336
343 void get_root_bbox( Real bb_min[], Real bb_max[] ) const;
344
354 void
356 Real bb_min[], integer ldim0,
357 Real bb_max[], integer ldim1,
358 integer nmin
359 ) const;
360
367 void get_bbox_indexes_of_a_node( integer i_pos, AABB_SET & bb_index ) const;
368
374 string info() const;
375 };
376
377 /*
378 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
379 */
380
381 #ifndef UTILS_OS_WINDOWS
382 extern template class AABBtree<float>;
383 extern template class AABBtree<double>;
384 #endif
385
386}
387
388#endif
389
390//
391// eof: Utils_AABBtree.hh
392//
A class representing an axis-aligned bounding box tree.
Definition Utils_AABB_tree.hh:63
void get_root_bbox(Real bb_min[], Real bb_max[]) const
Gets the bounding box of the root node.
Definition Utils_AABB_tree.cc:1160
integer num_check() const
Returns the number of overlap checks performed.
Definition Utils_AABB_tree.hh:327
void build()
Builds the AABB tree.
Definition Utils_AABB_tree.cc:509
void set_bbox_min_size_tolerance(Real tol)
Sets the bounding box minimum size tolerance.
Definition Utils_AABB_tree.cc:358
void replace_bbox(Real const bbox_min[], Real const bbox_max[], integer ipos)
Replaces a bounding box at a specific position.
Definition Utils_AABB_tree.cc:483
void min_distance_candidates(Real const pnt[], AABB_SET &bb_index) const
Finds candidates for minimum distance to a point.
Definition Utils_AABB_tree.cc:1069
int integer
Integer type.
Definition Utils_AABB_tree.hh:66
void build(Real const bb_min[], integer ldim0, Real const bb_max[], integer ldim1, integer nbox, integer dim)
Builds the AABB tree with specified bounding boxes.
Definition Utils_AABB_tree.hh:222
void set_max_num_objects_per_node(integer n)
Sets the maximum number of objects per node.
Definition Utils_AABB_tree.cc:316
void get_bboxes_of_the_tree(Real bb_min[], integer ldim0, Real bb_max[], integer ldim1, integer nmin) const
Gets bounding boxes of the tree.
Definition Utils_AABB_tree.cc:1169
void pnt_bbox_minmax(Real const pnt[], Real const bbox[], Real &dmin, Real &dmax) const
Calculates minimum and maximum distance from a point to a bounding box.
Definition Utils_AABB_tree.cc:1041
void intersect_with_one_point_and_refine(Real const pnt[], AABB_SET &bb_index) const
Intersects the tree with a point and refines the search.
Definition Utils_AABB_tree.cc:772
void set_bbox_long_edge_ratio(Real ratio)
Sets the bounding box long edge ratio.
Definition Utils_AABB_tree.cc:330
void set_bbox_overlap_tolerance(Real tol)
Sets the bounding box overlap tolerance.
Definition Utils_AABB_tree.cc:344
integer num_tree_nodes() const
Returns the number of tree nodes.
Definition Utils_AABB_tree.hh:320
void intersect(AABBtree< Real > const &aabb, AABB_MAP &bb_index) const
Intersects the tree with another AABB tree.
Definition Utils_AABB_tree.cc:908
integer num_objects() const
Returns the number of objects (bounding boxes).
Definition Utils_AABB_tree.hh:313
integer dim() const
Returns the spatial dimension of the bounding boxes.
Definition Utils_AABB_tree.hh:306
void intersect_with_one_bbox_and_refine(Real const bbox[], AABB_SET &bb_index) const
Intersects the tree with a bounding box and refines the search.
Definition Utils_AABB_tree.cc:860
void add_bboxes(Real const bb_min[], integer ldim0, Real const bb_max[], integer ldim1)
Adds bounding boxes to the tree.
Definition Utils_AABB_tree.cc:454
void intersect_with_one_bbox(Real const bbox[], AABB_SET &bb_index) const
Intersects the tree with a bounding box.
Definition Utils_AABB_tree.cc:820
void get_bbox_indexes_of_a_node(integer i_pos, AABB_SET &bb_index) const
Gets the indices of bounding boxes in a specific tree node.
Definition Utils_AABB_tree.cc:1131
void intersect_and_refine(AABBtree< Real > const &aabb, AABB_MAP &bb_index) const
Intersects the tree with another AABB tree and refines the search.
Definition Utils_AABB_tree.cc:970
map< integer, AABB_SET > AABB_MAP
Map for bounding box indices and their overlaps.
Definition Utils_AABB_tree.hh:68
AABBtree()=default
Default constructor.
void intersect_with_one_point(Real const pnt[], AABB_SET &bb_index) const
Intersects the tree with a point.
Definition Utils_AABB_tree.cc:731
set< integer > AABB_SET
Set of integers representing bounding box indices.
Definition Utils_AABB_tree.hh:67
void allocate(integer nbox, integer dim)
Allocates memory for the AABB tree.
Definition Utils_AABB_tree.cc:372
string info() const
Returns information about the AABB tree.
Definition Utils_AABB_tree.cc:292
Class for dynamic memory allocation of objects.
Definition Malloc.hxx:79
Definition SystemUtils.cc:39