meshOctreeCubeCoordinatesI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 
27 \*---------------------------------------------------------------------------*/
28 
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 
39 :
40  posX_(-1),
41  posY_(-1),
42  posZ_(-1),
43  level_(0)
44 {}
45 
47 (
48  const label posX,
49  const label posY,
50  const label posZ,
51  const direction level
52 )
53 :
54  posX_(posX),
55  posY_(posY),
56  posZ_(posZ),
57  level_(level)
58 {}
59 
61 (
63 )
64 :
65  posX_(cc.posX_),
66  posY_(cc.posY_),
67  posZ_(cc.posZ_),
68  level_(cc.level_)
69 {}
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 // Member functions
73 
75 {
76  return level_;
77 }
78 
80 {
81  return posX_;
82 }
83 
85 {
86  return posY_;
87 }
88 
90 {
91  return posZ_;
92 }
93 
95 (
96  const label i
97 ) const
98 {
99  //- create new boxes in z-order fashion
100  FixedList<label, 3> addPos(0);
101  if( i & 1 )
102  addPos[0] = 1;
103  if( i & 2 )
104  addPos[1] = 1;
105  if( i & 4 )
106  addPos[2] = 1;
107 
109  (
110  2*posX_+addPos[0],
111  2*posY_+addPos[1],
112  posZ_<0?posZ_:2*posZ_+addPos[2],
113  level_+1
114  );
115 }
116 
118 (
119  const direction diff
120 ) const
121 {
122  const label divider = (1 << diff);
123 
125  (
126  posX_ / divider,
127  posY_ / divider,
128  posZ_<0?posZ_:posZ_ / divider,
129  level_ - diff
130  );
131 }
132 
134 (
135  const direction l
136 ) const
137 {
138  const direction diff = level_ - l;
139 
140  return reduceLevelBy(diff);
141 }
142 
144 (
145  const direction l
146 ) const
147 {
148  const direction diff = l - level_;
150  (
151  posX_ << diff,
152  posY_ << diff,
153  posZ_<0?posZ_:posZ_ << diff,
154  l
155  );
156 }
157 
159 (
160  const direction l
161 ) const
162 {
163  const direction diff = l - level_;
165  (
166  ((posX_ + 1) << diff) - 1,
167  ((posY_ + 1) << diff) - 1,
168  posZ_<0?posZ_:((posZ_ + 1) << diff) - 1,
169  l
170  );
171 }
172 
174 (
175  const boundBox& rootBox,
176  point& min,
177  point& max
178 ) const
179 {
180  vector dc = (rootBox.max() - rootBox.min());
181  const label shift = 1 << level_;
182 
183  dc.x() /= shift;
184  dc.y() /= shift;
185  if( posZ_>=0 )
186  dc.z() /= shift;
187 
188  min.x() = rootBox.min().x() + dc.x() * this->posX();
189  min.y() = rootBox.min().y() + dc.y() * this->posY();
190  if( posZ_ >= 0 )
191  {
192  min.z() = rootBox.min().z() + dc.z() * this->posZ();
193  }
194  else
195  {
196  min.z() = rootBox.min().z();
197  }
198 
199  max = min + dc;
200 }
201 
203 (
204  const boundBox& rootBox
205 ) const
206 {
207  point min, max;
208  cubeBox(rootBox, min, max);
209  return (max + min) / 2.0;
210 }
211 
213 (
214  const boundBox& rootBox
215 ) const
216 {
217  point min, max;
218  cubeBox(rootBox, min, max);
219  return (max.x() - min.x());
220 }
221 
223 (
224  meshOctreeCubeCoordinates& minCoord,
225  meshOctreeCubeCoordinates& maxCoord
226 ) const
227 {
228  minCoord = this->refineForPosition(0);
229  if( minCoord.posX_ > 0 )
230  minCoord.posX_ -= 1;
231  if( minCoord.posY_ > 0 )
232  minCoord.posY_ -= 1;
233  if( minCoord.posZ_ > 0 )
234  minCoord.posZ_ -= 1;
235 
236  maxCoord = this->refineForPosition(7);
237  const label maxc = (1 << maxCoord.level_) - 1;
238  if( maxCoord.posX_ < maxc )
239  maxCoord.posX_ += 1;
240  if( maxCoord.posY_ < maxc )
241  maxCoord.posY_ += 1;
242  if( maxCoord.posZ_ < maxc )
243  maxCoord.posZ_ += 1;
244 }
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 // Operators
248 
249 inline meshOctreeCubeCoordinates meshOctreeCubeCoordinates::operator+
250 (
251  const meshOctreeCubeCoordinates& cc
252 ) const
253 {
255  (
256  posX_ + cc.posX_,
257  posY_ + cc.posY_,
258  posZ_ + cc.posZ_,
259  level_ + cc.level_
260  );
261 
262  return ccc;
263 }
264 
265 inline void meshOctreeCubeCoordinates::operator=
266 (
267  const meshOctreeCubeCoordinates& cc
268 )
269 {
270  posX_ = cc.posX_;
271  posY_ = cc.posY_;
272  posZ_ = cc.posZ_;
273  level_ = cc.level_;
274 }
275 
276 inline bool meshOctreeCubeCoordinates::operator==
277 (
278  const meshOctreeCubeCoordinates& cc
279 ) const
280 {
281  if(
282  (level_ != cc.level_) ||
283  (posX_ != cc.posX_) ||
284  (posY_ != cc.posY_) ||
285  (posZ_ != cc.posZ_)
286  )
287  return false;
288 
289  return true;
290 }
291 
292 inline bool meshOctreeCubeCoordinates::operator!=
293 (
294  const meshOctreeCubeCoordinates& cc
295 ) const
296 {
297  if(
298  (level_ != cc.level_) ||
299  (posX_ != cc.posX_) ||
300  (posY_ != cc.posY_) ||
301  (posZ_ != cc.posZ_)
302  )
303  return true;
304 
305  return false;
306 }
307 
308 inline bool meshOctreeCubeCoordinates::operator<=
309 (
310  const meshOctreeCubeCoordinates& cc
311 ) const
312 {
313  const direction minL = Foam::min(level_, cc.level_);
314  const meshOctreeCubeCoordinates curr = this->reduceToLevel(minL);
315  const meshOctreeCubeCoordinates cctr = cc.reduceToLevel(minL);
316 
317  const label resx = curr.posX_ ^ cctr.posX_;
318  const label resy = curr.posY_ ^ cctr.posY_;
319  const label resz = curr.posZ_ ^ cctr.posZ_;
320 
321  label max(resx), dir(0);
322  if( (max <= resy) || ((max ^ resy) < resy) )
323  {
324  max = resy;
325  dir = 1;
326  }
327  if( (max <= resz) || ((max ^ resz) < resz) )
328  {
329  max = resz;
330  dir = 2;
331  }
332 
333  switch( dir )
334  {
335  case 0:
336  {
337  if( curr.posX_ <= cctr.posX_ )
338  return true;
339 
340  return false;
341  } break;
342  case 1:
343  {
344  if( curr.posY_ <= cctr.posY_ )
345  return true;
346 
347  return false;
348  } break;
349  case 2:
350  {
351  if( curr.posZ_ <= cctr.posZ_ )
352  return true;
353 
354  return false;
355  } break;
356  };
357 
358  return false;
359 }
360 
361 inline bool meshOctreeCubeCoordinates::operator>=
362 (
363  const meshOctreeCubeCoordinates& cc
364 ) const
365 {
366  const direction minL = Foam::min(level_, cc.level_);
367  const meshOctreeCubeCoordinates curr = this->reduceToLevel(minL);
368  const meshOctreeCubeCoordinates cctr = cc.reduceToLevel(minL);
369 
370  const label resx = curr.posX_ ^ cctr.posX_;
371  const label resy = curr.posY_ ^ cctr.posY_;
372  const label resz = curr.posZ_ ^ cctr.posZ_;
373 
374  label max(resx), dir(0);
375  if( (max <= resy) || ((max ^ resy) < resy) )
376  {
377  max = resy;
378  dir = 1;
379  }
380  if( (max <= resz) || ((max ^ resz) < resz) )
381  {
382  max = resz;
383  dir = 2;
384  }
385 
386  switch( dir )
387  {
388  case 0:
389  {
390  if( curr.posX_ >= cctr.posX_ )
391  return true;
392 
393  return false;
394  } break;
395  case 1:
396  {
397  if( curr.posY_ >= cctr.posY_ )
398  return true;
399 
400  return false;
401  } break;
402  case 2:
403  {
404  if( curr.posZ_ >= cctr.posZ_ )
405  return true;
406 
407  return false;
408  } break;
409  };
410 
411  return false;
412 }
413 
414 inline bool meshOctreeCubeCoordinates::operator<
415 (
416  const meshOctreeCubeCoordinates& cc
417 ) const
418 {
419  const direction minL = Foam::min(level_, cc.level_);
420  const meshOctreeCubeCoordinates curr = this->reduceToLevel(minL);
421  const meshOctreeCubeCoordinates cctr = cc.reduceToLevel(minL);
422 
423  const label resx = curr.posX_ ^ cctr.posX_;
424  const label resy = curr.posY_ ^ cctr.posY_;
425  const label resz = curr.posZ_ ^ cctr.posZ_;
426 
427  label max(resx), dir(0);
428  if( (max <= resy) || ((max ^ resy) < resy) )
429  {
430  max = resy;
431  dir = 1;
432  }
433  if( (max <= resz) || ((max ^ resz) < resz) )
434  {
435  max = resz;
436  dir = 2;
437  }
438 
439  switch( dir )
440  {
441  case 0:
442  {
443  if( curr.posX_ < cctr.posX_ )
444  return true;
445 
446  return false;
447  } break;
448  case 1:
449  {
450  if( curr.posY_ < cctr.posY_ )
451  return true;
452 
453  return false;
454  } break;
455  case 2:
456  {
457  if( curr.posZ_ < cctr.posZ_ )
458  return true;
459 
460  return false;
461  } break;
462  };
463 
464  return false;
465 }
466 
467 inline bool meshOctreeCubeCoordinates::operator>
468 (
469  const meshOctreeCubeCoordinates& cc
470 ) const
471 {
472  const direction minL = Foam::min(level_, cc.level_);
473  const meshOctreeCubeCoordinates curr = this->reduceToLevel(minL);
474  const meshOctreeCubeCoordinates cctr = cc.reduceToLevel(minL);
475 
476  const label resx = curr.posX_ ^ cctr.posX_;
477  const label resy = curr.posY_ ^ cctr.posY_;
478  const label resz = curr.posZ_ ^ cctr.posZ_;
479 
480  label max(resx), dir(0);
481  if( (max <= resy) || ((max ^ resy) < resy) )
482  {
483  max = resy;
484  dir = 1;
485  }
486  if( (max <= resz) || ((max ^ resz) < resz) )
487  {
488  max = resz;
489  dir = 2;
490  }
491 
492  switch( dir )
493  {
494  case 0:
495  {
496  if( curr.posX_ > cctr.posX_ )
497  return true;
498 
499  return false;
500  } break;
501  case 1:
502  {
503  if( curr.posY_ > cctr.posY_ )
504  return true;
505 
506  return false;
507  } break;
508  case 2:
509  {
510  if( curr.posZ_ > cctr.posZ_ )
511  return true;
512 
513  return false;
514  } break;
515  };
516 
517  return false;
518 }
519 
520 inline Istream& operator>>
521 (
522  Istream& is,
524 )
525 {
526  // Read beginning of meshOctreeCubeCoordinates
527  is.readBegin("meshOctreeCubeCoordinates");
528 
529  label l;
530  is >> l;
531  cc.level_ = l;
532  is >> cc.posX_;
533  is >> cc.posY_;
534  is >> cc.posZ_;
535 
536  // Read end of meshOctreeCubeCoordinates
537  is.readEnd("meshOctreeCubeCoordinates");
538 
539  // Check state of Istream
540  is.check("operator>>(Istream&, meshOctreeCubeCoordinates");
541 
542  return is;
543 }
544 
545 inline Ostream& operator<<
546 (
547  Ostream& os,
548  const meshOctreeCubeCoordinates& cc
549 )
550 {
551  os << token::BEGIN_LIST;
552 
553  os << label(cc.level_) << token::SPACE;
554  os << cc.posX_ << token::SPACE;
555  os << cc.posY_ << token::SPACE;
556  os << cc.posZ_ << token::END_LIST;
557 
558  // Check state of Ostream
559  os.check("operator<<(Ostream&, const meshOctreeCubeCoordinates");
560 
561  return os;
562 }
563 
564 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
565 
566 } // End namespace Foam
567 
568 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::Istream::readEnd
Istream & readEnd(const char *funcName)
Definition: Istream.C:105
Foam::meshOctreeCubeCoordinates::increaseToLevelMax
meshOctreeCubeCoordinates increaseToLevelMax(const direction l) const
return the maximal coordinates of the child at the given level
Definition: meshOctreeCubeCoordinatesI.H:159
Foam::meshOctreeCubeCoordinates::posX
label posX() const
return x, y, z coordinates
Definition: meshOctreeCubeCoordinatesI.H:79
Foam::boundBox::max
const point & max() const
Maximum describing the bounding box.
Definition: boundBoxI.H:60
Foam::meshOctreeCubeCoordinates
Definition: meshOctreeCubeCoordinates.H:55
Foam::meshOctreeCubeCoordinates::centre
point centre(const boundBox &) const
return centre
Definition: meshOctreeCubeCoordinatesI.H:203
Foam::meshOctreeCubeCoordinates::posY_
label posY_
Definition: meshOctreeCubeCoordinates.H:60
Foam::diff
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:407
Foam::label
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
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::meshOctreeCubeCoordinates::increaseToLevelMin
meshOctreeCubeCoordinates increaseToLevelMin(const direction l) const
return the minimal coordinates of the child at the given level
Definition: meshOctreeCubeCoordinatesI.H:144
Foam::meshOctreeCubeCoordinates::level_
direction level_
cube level in the octree structure
Definition: meshOctreeCubeCoordinates.H:64
Foam::meshOctreeCubeCoordinates::refineForPosition
meshOctreeCubeCoordinates refineForPosition(const label) const
return the coordinates of child cube at the given position
Definition: meshOctreeCubeCoordinatesI.H:95
Foam::meshOctreeCubeCoordinates::reduceToLevel
meshOctreeCubeCoordinates reduceToLevel(const direction) const
return the coordinates of the parent at the given level
Definition: meshOctreeCubeCoordinatesI.H:134
Foam::boundBox::min
const point & min() const
Minimum describing the bounding box.
Definition: boundBoxI.H:54
meshOctreeCubeCoordinates.H
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::meshOctreeCubeCoordinates::size
scalar size(const boundBox &) const
return size
Definition: meshOctreeCubeCoordinatesI.H:213
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::meshOctreeCubeCoordinates::posZ_
label posZ_
Definition: meshOctreeCubeCoordinates.H:61
Foam::meshOctreeCubeCoordinates::level
direction level() const
return level
Definition: meshOctreeCubeCoordinatesI.H:74
Foam::Vector< scalar >
Foam::FixedList< label, 3 >
Foam::meshOctreeCubeCoordinates::posX_
label posX_
coordinates in the octree structure
Definition: meshOctreeCubeCoordinates.H:59
Foam::direction
unsigned char direction
Definition: direction.H:43
Foam::boundBox
A bounding box defined in terms of the points at its extremities.
Definition: boundBox.H:55
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::meshOctreeCubeCoordinates::meshOctreeCubeCoordinates
meshOctreeCubeCoordinates()
Null constructor.
Definition: meshOctreeCubeCoordinatesI.H:38
Foam::meshOctreeCubeCoordinates::neighbourRange
void neighbourRange(meshOctreeCubeCoordinates &minCoord, meshOctreeCubeCoordinates &maxCoord) const
Definition: meshOctreeCubeCoordinatesI.H:223
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::Istream::readBegin
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
Foam::meshOctreeCubeCoordinates::cubeBox
void cubeBox(const boundBox &, point &, point &) const
return min and max points
Definition: meshOctreeCubeCoordinatesI.H:174
Foam::meshOctreeCubeCoordinates::posZ
label posZ() const
Definition: meshOctreeCubeCoordinatesI.H:89
Foam::meshOctreeCubeCoordinates::posY
label posY() const
Definition: meshOctreeCubeCoordinatesI.H:84
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::meshOctreeCubeCoordinates::reduceLevelBy
meshOctreeCubeCoordinates reduceLevelBy(const direction diff) const
Definition: meshOctreeCubeCoordinatesI.H:118
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::SPACE
@ SPACE
Definition: token.H:95