treeBoundBox.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::treeBoundBox
26 
27 Description
28  Standard boundBox + extra functionality for use in octree.
29 
30  Numbering of corner points is according to octant numbering.
31 
32  On the back plane (z=0):
33 
34  \verbatim
35  Y
36  ^
37  |
38  +--------+
39  |2 3|
40  | |
41  | |
42  | |
43  |0 1|
44  +--------+->X
45  \endverbatim
46 
47  For the front plane add 4 to the point labels.
48 
49 
50 SourceFiles
51  treeBoundBoxI.H
52  treeBoundBox.C
53  treeBoundBoxTemplates.C
54 
55 \*---------------------------------------------------------------------------*/
56 
57 #ifndef treeBoundBox_H
58 #define treeBoundBox_H
59 
60 #include "boundBox.H"
61 #include "direction.H"
62 #include "pointField.H"
63 #include "faceList.H"
64 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 namespace Foam
68 {
69 
70 class randomGenerator;
71 
72 
73 // Forward declaration of friend functions and operators
74 
75 class treeBoundBox;
76 
77 bool operator==(const treeBoundBox&, const treeBoundBox&);
78 bool operator!=(const treeBoundBox&, const treeBoundBox&);
79 
80 Istream& operator>>(Istream& is, treeBoundBox&);
81 Ostream& operator<<(Ostream& os, const treeBoundBox&);
82 
83 
84 /*---------------------------------------------------------------------------*\
85  Class treeBoundBox Declaration
86 \*---------------------------------------------------------------------------*/
87 
88 class treeBoundBox
89 :
90  public boundBox
91 {
92  //- To initialise edges.
93  static edgeList calcEdges(const label[12][2]);
94 
95  //- To initialise faceNormals.
96  static FixedList<vector, 6> calcFaceNormals();
97 
98 public:
99 
100  // Static Data Members
101 
102  //- As per boundBox::greatBox, but with great instead of vGreat
103  static const treeBoundBox greatBox;
104 
105  //- As per boundBox::invertedBox, but with great instead of vGreat
106  static const treeBoundBox invertedBox;
107 
108  //- Bits used for octant/point coding.
109  // Every octant/corner point is the combination of three faces.
110  struct octantBit
111  {
112  enum
113  {
114  rightHalf = 0x1 << 0,
115  topHalf = 0x1 << 1,
116  frontHalf = 0x1 << 2
117  };
118  };
119 
120  //- Face codes
121  struct faceId
122  {
123  enum
124  {
125  left = 0,
126  right = 1,
127  bottom = 2,
128  top = 3,
129  back = 4,
130  front = 5
131  };
132  };
133 
134  //- Bits used for face coding
135  struct faceBit
136  {
137  enum
138  {
139  noFace = 0,
140  left = 0x1 << faceId::left, //1
141  right = 0x1 << faceId::right, //2
142  bottom = 0x1 << faceId::bottom, //4
143  top = 0x1 << faceId::top, //8
144  back = 0x1 << faceId::back, //16
145  front = 0x1 << faceId::front, //32
146  };
147  };
148 
149  //- Edges codes.
150  // e01 = edge between 0 and 1.
151  struct edgeId
152  {
153  enum
154  {
155  e01 = 0,
156  e13 = 1,
157  e23 = 2,
158  e02 = 3,
159 
160  e45 = 4,
161  e57 = 5,
162  e67 = 6,
163  e46 = 7,
164 
165  e04 = 8,
166  e15 = 9,
167  e37 = 10,
168  e26 = 11
169  };
170  };
171 
172  //- Face to point addressing
173  static const faceList faces;
174 
175  //- Edge to point addressing
176  static const edgeList edges;
177 
178  //- Per face the unit normal
179  static const FixedList<vector, 6> faceNormals;
180 
181 
182  // Constructors
183 
184  //- Construct null setting points to zero
185  inline treeBoundBox();
186 
187  //- Construct from a boundBox
188  explicit inline treeBoundBox(const boundBox& bb);
189 
190  //- Construct from components
191  inline treeBoundBox(const point& min, const point& max);
192 
193  //- Construct as the bounding box of the given pointField.
194  // Local processor domain only (no reduce as in boundBox)
195  explicit treeBoundBox(const UList<point>&);
196 
197  //- Construct as subset of points
198  // Local processor domain only (no reduce as in boundBox)
199  treeBoundBox(const UList<point>&, const labelUList& indices);
200 
201  //- Construct as subset of points
202  // The indices could be from edge/triFace etc.
203  // Local processor domain only (no reduce as in boundBox)
204  template<unsigned Size>
206  (
207  const UList<point>&,
208  const FixedList<label, Size>& indices
209  );
210 
211  //- Construct from Istream
212  inline treeBoundBox(Istream&);
213 
214  //- Construct from dictionary
215  inline treeBoundBox(const dictionary&);
216 
217 
218  // Member Functions
219 
220  // Access
221 
222  //- Typical dimension length,height,width
223  inline scalar typDim() const;
224 
225  //- Vertex coordinates. In octant coding.
226  tmp<pointField> points() const;
227 
228 
229  // Check
230 
231  //- Corner point given octant
232  inline point corner(const direction) const;
233 
234  //- Sub box given by octant number. Midpoint calculated.
235  treeBoundBox subBbox(const direction) const;
236 
237  //- Sub box given by octant number. Midpoint provided.
238  treeBoundBox subBbox(const point& mid, const direction) const;
239 
240  //- Returns octant number given point and the calculated midpoint.
241  inline direction subOctant
242  (
243  const point& pt
244  ) const;
245 
246  //- Returns octant number given point and midpoint.
247  static inline direction subOctant
248  (
249  const point& mid,
250  const point& pt
251  );
252 
253  //- Returns octant number given point and the calculated midpoint.
254  // onEdge set if the point is on edge of subOctant
255  inline direction subOctant
256  (
257  const point& pt,
258  bool& onEdge
259  ) const;
260 
261  //- Returns octant number given point and midpoint.
262  // onEdge set if the point is on edge of subOctant
263  static inline direction subOctant
264  (
265  const point& mid,
266  const point& pt,
267  bool& onEdge
268  );
269 
270  //- Returns octant number given intersection and midpoint.
271  // onEdge set if the point is on edge of subOctant
272  // If onEdge, the direction vector determines which octant to use
273  // (acc. to which octant the point would be if it were moved
274  // along dir)
275  static inline direction subOctant
276  (
277  const point& mid,
278  const vector& dir,
279  const point& pt,
280  bool& onEdge
281  );
282 
283  //- Calculates optimal order to look for nearest to point.
284  // First will be the octant containing the point,
285  // second the octant with boundary nearest to the point etc.
286  inline void searchOrder
287  (
288  const point& pt,
289  FixedList<direction, 8>& octantOrder
290  ) const;
291 
292  //- Overlaps other bounding box?
293  using boundBox::overlaps;
294 
295  //- Intersects segment; set point to intersection position and face,
296  // return true if intersection found.
297  // (pt argument used during calculation even if not intersecting).
298  // Calculates intersections from outside supplied vector
299  // (overallStart, overallVec). This is so when
300  // e.g. tracking through lots of consecutive boxes
301  // (typical octree) we're not accumulating truncation errors. Set
302  // to start, (end-start) if not used.
303  bool intersects
304  (
305  const point& overallStart,
306  const vector& overallVec,
307  const point& start,
308  const point& end,
309  point& pt,
310  direction& ptBits
311  ) const;
312 
313  //- Like above but does not return faces point is on
314  bool intersects
315  (
316  const point& start,
317  const point& end,
318  point& pt
319  ) const;
320 
321  //- Contains point or other bounding box?
322  using boundBox::contains;
323 
324  //- Contains point (inside or on edge) and moving in direction
325  // dir would cause it to go inside.
326  bool contains(const vector& dir, const point&) const;
327 
328  //- Code position of point on bounding box faces
329  direction faceBits(const point&) const;
330 
331  //- Position of point relative to bounding box
332  direction posBits(const point&) const;
333 
334  //- Calculate nearest and furthest (to point) vertex coords of
335  // bounding box
336  void calcExtremities
337  (
338  const point& pt,
339  point& nearest,
340  point& furthest
341  ) const;
342 
343  //- Returns distance point to furthest away corner.
344  scalar maxDist(const point&) const;
345 
346  //- Compare distance to point with other bounding box
347  // return:
348  // -1 : all vertices of my bounding box are nearer than any of
349  // other
350  // +1 : all vertices of my bounding box are further away than
351  // any of other
352  // 0 : none of the above.
353  label distanceCmp(const point&, const treeBoundBox& other) const;
354 
355  //- Return asymmetrically extended bounding box, with guaranteed
356  // minimum width of s*mag(span) in any direction
357  inline treeBoundBox extend(const scalar s) const;
358 
359  // Write
360 
361  void writeOBJ(const fileName& fName) const;
362 
363 
364  // Friend Operators
365 
366  friend bool operator==(const treeBoundBox&, const treeBoundBox&);
367  friend bool operator!=(const treeBoundBox&, const treeBoundBox&);
368 
369 
370  // IOstream operator
371 
373  friend Ostream& operator<<(Ostream& os, const treeBoundBox&);
374 };
375 
376 
377 //- Data associated with treeBoundBox type are contiguous
378 template<>
379 inline bool contiguous<treeBoundBox>() {return contiguous<boundBox>();}
380 
381 
382 //- A treeBoundBox is scaleable
383 template<>
384 struct scaleable<treeBoundBox> : public scaleable<boundBox> {};
385 
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 } // End namespace Foam
390 
391 #include "treeBoundBoxI.H"
392 
393 #ifdef NoRepository
394  #include "treeBoundBoxTemplates.C"
395 #endif
396 
397 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398 
399 #endif
400 
401 // ************************************************************************* //
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:78
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:60
point nearest(const point &) const
Return the nearest point on the boundBox to the supplied point.
Definition: boundBox.C:300
const point & min() const
Minimum point defining the bounding box.
Definition: boundBoxI.H:88
const point & max() const
Maximum point defining the bounding box.
Definition: boundBoxI.H:94
bool overlaps(const boundBox &) const
Overlaps/touches boundingBox?
Definition: boundBoxI.H:154
bool contains(const point &) const
Contains point? (inside or on edge)
Definition: boundBoxI.H:204
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A class for handling file names.
Definition: fileName.H:82
A class for managing temporary objects.
Definition: tmp.H:55
Standard boundBox + extra functionality for use in octree.
Definition: treeBoundBox.H:90
void writeOBJ(const fileName &fName) const
Definition: treeBoundBox.C:610
void calcExtremities(const point &pt, point &nearest, point &furthest) const
Calculate nearest and furthest (to point) vertex coords of.
Definition: treeBoundBox.C:491
friend Istream & operator>>(Istream &is, treeBoundBox &)
friend Ostream & operator<<(Ostream &os, const treeBoundBox &)
scalar maxDist(const point &) const
Returns distance point to furthest away corner.
Definition: treeBoundBox.C:538
label distanceCmp(const point &, const treeBoundBox &other) const
Compare distance to point with other bounding box.
Definition: treeBoundBox.C:548
direction faceBits(const point &) const
Code position of point on bounding box faces.
Definition: treeBoundBox.C:423
void searchOrder(const point &pt, FixedList< direction, 8 > &octantOrder) const
Calculates optimal order to look for nearest to point.
treeBoundBox extend(const scalar s) const
Return asymmetrically extended bounding box, with guaranteed.
friend bool operator!=(const treeBoundBox &, const treeBoundBox &)
treeBoundBox subBbox(const direction) const
Sub box given by octant number. Midpoint calculated.
Definition: treeBoundBox.C:171
static const edgeList edges
Edge to point addressing.
Definition: treeBoundBox.H:175
point corner(const direction) const
Corner point given octant.
Definition: treeBoundBoxI.H:69
bool contains(const point &) const
Contains point or other bounding box?
Definition: boundBoxI.H:204
bool intersects(const point &overallStart, const vector &overallVec, const point &start, const point &end, point &pt, direction &ptBits) const
Intersects segment; set point to intersection position and face,.
Definition: treeBoundBox.C:227
static const treeBoundBox invertedBox
As per boundBox::invertedBox, but with great instead of vGreat.
Definition: treeBoundBox.H:105
static const treeBoundBox greatBox
As per boundBox::greatBox, but with great instead of vGreat.
Definition: treeBoundBox.H:102
direction subOctant(const point &pt) const
Returns octant number given point and the calculated midpoint.
Definition: treeBoundBoxI.H:81
static const FixedList< vector, 6 > faceNormals
Per face the unit normal.
Definition: treeBoundBox.H:178
scalar typDim() const
Typical dimension length,height,width.
Definition: treeBoundBoxI.H:63
static const faceList faces
Face to point addressing.
Definition: treeBoundBox.H:172
friend bool operator==(const treeBoundBox &, const treeBoundBox &)
direction posBits(const point &) const
Position of point relative to bounding box.
Definition: treeBoundBox.C:456
treeBoundBox()
Construct null setting points to zero.
Definition: treeBoundBoxI.H:31
tmp< pointField > points() const
Vertex coordinates. In octant coding.
Definition: treeBoundBox.C:157
Direction is an 8-bit unsigned integer type used to represent the Cartesian directions etc.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
bool operator!=(const particle &, const particle &)
Definition: particle.C:445
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
bool contiguous< boundBox >()
Data associated with boundBox type are contiguous.
Definition: boundBox.H:258
bool contiguous< treeBoundBox >()
Data associated with treeBoundBox type are contiguous.
Definition: treeBoundBox.H:378
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
uint8_t direction
Definition: direction.H:45
Bits used for face coding.
Definition: treeBoundBox.H:135
Bits used for octant/point coding.
Definition: treeBoundBox.H:110