AABBTree.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015 OpenFOAM Foundation
9  Copyright (C) 2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::AABBTree
29 
30 Description
31  Templated tree of axis-aligned bounding boxes (AABB)
32 
33  Designed to be templated on either faces or cells, the AABBTree will
34  decompose the input into a tree of AABB's. The maximum number of tree
35  levels and minimum number of objects per leaf are provided on construction,
36  and the contents (addressing) is stored.
37 
38 SourceFiles
39  AABBTree.C
40  AABBTreeBase.C
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef Foam_AABBTree_H
45 #define Foam_AABBTree_H
46 
47 #include "labelList.H"
48 #include "labelPair.H"
49 #include "DynamicList.H"
50 #include "pointField.H"
51 #include "treeBoundBox.H"
52 #include "Ostream.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward Declarations
60 template<class Type> class AABBTree;
61 
62 template<class Type>
64 
65 template<class Type>
66 Ostream& operator<<(Ostream&, const AABBTree<Type>&);
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class AABBTreeBase Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 //- Template invariant parts for AABBTree
74 class AABBTreeBase
75 {
76 protected:
77 
78  // Static Data
79 
80  //- Relative tolerance.
81  static scalar tolerance_;
82 
83 
84 public:
85 
86  // Constructors
87 
88  //- Default construct
89  AABBTreeBase() = default;
90 
91 
92  // Output Helpers
93 
94  //- Write treeBoundBox in OBJ format
95  static void writeOBJ
96  (
97  Ostream& os,
98  const treeBoundBox& bb,
99  label& vertIndex,
100  const bool writeLinesOnly = false
101  );
102 };
103 
104 
105 /*---------------------------------------------------------------------------*\
106  Class AABBTree Declaration
107 \*---------------------------------------------------------------------------*/
108 
109 template<class Type>
110 class AABBTree
111 :
112  public AABBTreeBase
113 {
114 protected:
115 
116  // Protected Data
117 
118  //- Maximum tree level
119  label maxLevel_;
120 
121  //- Minimum points per leaf
122  label minLeafSize_;
123 
124  //- Bounding boxes making up the tree
126 
127  //- Leaf addressing
130 
131  // Protected Member Functions
132 
133  //- Write OBJ for all bounding boxes
134  void writeOBJ
135  (
136  const bool leavesOnly,
137  const bool writeLinesOnly,
138  const treeBoundBox& bb,
139  const label nodeI,
140  const List<Pair<treeBoundBox>>& bbs,
141  const List<Pair<label>>& nodes,
142  label& vertI,
143  Ostream& os
144  ) const;
145 
146  //- Create the bounding boxes by interrogating points
147  void createBoxes
148  (
149  const bool equalBinSize,
150  const label level,
151  const UList<Type>& objects,
152  const pointField& points,
153  const labelUList& objectIDs,
154  const treeBoundBox& bb,
155  const label nodeI,
156 
158  DynamicList<labelPair>& nodes,
160  ) const;
161 
162 
163 public:
164 
165  // Constructors
166 
167  //- Default construct
168  AABBTree();
169 
170  //- Construct from components
171  // equalBinSize: divide into equal number of elements or equal span
172  AABBTree
173  (
174  const UList<Type>& objects,
175  const pointField& points,
176  const bool equalBinSize = true,
177  label maxLevel = 3,
178  label minBinSize = 100
179  );
180 
181 
182  // Public Member Functions
183 
184  // Access
185 
186  //- Return the bounding boxes making up the tree
187  const List<treeBoundBox>& boundBoxes() const noexcept
188  {
189  return boundBoxes_;
190  }
191 
192  //- Return the contents addressing
193  const List<labelList>& addressing() const noexcept
194  {
195  return addressing_;
196  }
197 
198 
199  // Evaluation
200 
201  //- Determine whether a point is inside the bounding boxes
202  bool pointInside(const point& pt) const;
203 
204  //- Determine whether a bounding box overlaps the tree bounding boxes
205  bool overlaps(const boundBox& bbIn) const;
206 
207 
208  // Write
210  //- Write all tree boxes (leaves) in OBJ format
211  void writeOBJ(Ostream& os) const;
212 
213 
214  // IOstream Operators
215 
216  friend Istream& operator>> <Type>(Istream&, AABBTree&);
217  friend Ostream& operator<< <Type>(Ostream&, const AABBTree&);
218 };
219 
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 } // End namespace Foam
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 #ifdef NoRepository
228  #include "AABBTree.C"
229 #endif
230 
231 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 
233 #endif
234 
235 // ************************************************************************* //
List< labelList > addressing_
Leaf addressing.
Definition: AABBTree.H:139
bool overlaps(const boundBox &bbIn) const
Determine whether a bounding box overlaps the tree bounding boxes.
Definition: AABBTree.C:418
List< treeBoundBox > boundBoxes_
Bounding boxes making up the tree.
Definition: AABBTree.H:134
bool pointInside(const point &pt) const
Determine whether a point is inside the bounding boxes.
Definition: AABBTree.C:403
static void writeOBJ(Ostream &os, const treeBoundBox &bb, label &vertIndex, const bool writeLinesOnly=false)
Write treeBoundBox in OBJ format.
Definition: AABBTreeBase.C:32
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static scalar tolerance_
Relative tolerance.
Definition: AABBTree.H:80
void writeOBJ(const bool leavesOnly, const bool writeLinesOnly, const treeBoundBox &bb, const label nodeI, const List< Pair< treeBoundBox >> &bbs, const List< Pair< label >> &nodes, label &vertI, Ostream &os) const
Write OBJ for all bounding boxes.
Definition: AABBTree.C:29
void createBoxes(const bool equalBinSize, const label level, const UList< Type > &objects, const pointField &points, const labelUList &objectIDs, const treeBoundBox &bb, const label nodeI, DynamicList< Pair< treeBoundBox >> &bbs, DynamicList< labelPair > &nodes, DynamicList< labelList > &addressing) const
Create the bounding boxes by interrogating points.
Definition: AABBTree.C:76
AABBTreeBase()=default
Default construct.
label maxLevel_
Maximum tree level.
Definition: AABBTree.H:124
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: instant.H:46
const pointField & points
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
Istream & operator>>(Istream &, directionInfo &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
Templated tree of axis-aligned bounding boxes (AABB)
Definition: AABBTree.H:55
AABBTree()
Default construct.
Definition: AABBTree.C:270
vector point
Point is a vector.
Definition: point.H:37
label minLeafSize_
Minimum points per leaf.
Definition: AABBTree.H:129
Standard boundBox with extra functionality for use in octree.
Definition: treeBoundBox.H:90
Template invariant parts for AABBTree.
Definition: AABBTree.H:71
const List< labelList > & addressing() const noexcept
Return the contents addressing.
Definition: AABBTree.H:217
Namespace for OpenFOAM.
const List< treeBoundBox > & boundBoxes() const noexcept
Return the bounding boxes making up the tree.
Definition: AABBTree.H:209