00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "octree.H"
00030 #include "treeLeaf.H"
00031 #include "treeNode.H"
00032 #include "long.H"
00033 #include "cpuTime.H"
00034 #include "linePointRef.H"
00035 #include "pointIndexHit.H"
00036
00037
00038
00039 template <class Type>
00040 Foam::string Foam::octree<Type>::volType(const label type)
00041 {
00042 if (type == UNKNOWN)
00043 {
00044 return "unknown";
00045 }
00046 else if (type == MIXED)
00047 {
00048 return "mixed";
00049 }
00050 else if (type == INSIDE)
00051 {
00052 return "inside";
00053 }
00054 else if (type == OUTSIDE)
00055 {
00056 return "outside";
00057 }
00058 else
00059 {
00060 FatalErrorIn("volType(const label)") << "type:" << type
00061 << " unknown." << abort(FatalError);
00062
00063 return "dummy";
00064 }
00065 }
00066
00067
00068
00069 template <class Type>
00070 Foam::label Foam::octree<Type>::getVolType
00071 (
00072 const vector& geomNormal,
00073 const vector& vec
00074 )
00075 {
00076 scalar sign = geomNormal & vec;
00077
00078 if (sign >= 0)
00079 {
00080 return octree<Type>::OUTSIDE;
00081 }
00082 else
00083 {
00084 return octree<Type>::INSIDE;
00085 }
00086 }
00087
00088
00089
00090
00091 template <class Type>
00092 Foam::octree<Type>::octree
00093 (
00094 const treeBoundBox& octreeBb,
00095 const Type& shapes,
00096 const label minNLevels,
00097 const scalar maxLeafRatio,
00098 const scalar maxShapeRatio
00099 )
00100 :
00101 topNode_(new treeNode<Type>(octreeBb)),
00102 shapes_(shapes),
00103 octreeBb_(octreeBb),
00104 maxLeafRatio_(maxLeafRatio),
00105 maxShapeRatio_(maxShapeRatio),
00106 minNLevels_(minNLevels),
00107 deepestLevel_(0),
00108 nEntries_(0),
00109 nNodes_(0),
00110 nLeaves_(0),
00111 endIter_(*this, -1),
00112 endConstIter_(*this, -1)
00113 {
00114 cpuTime timer;
00115
00116 setNodes(nNodes() + 1);
00117
00118 const label nShapes = shapes_.size();
00119
00120 labelList indices(nShapes);
00121 for (label i = 0; i < nShapes; i++)
00122 {
00123 indices[i] = i;
00124 }
00125
00126
00127 if (debug & 1)
00128 {
00129 Pout<< "octree : --- Start of Level " << deepestLevel_
00130 << " ----" << endl;
00131 }
00132 topNode_->distribute(0, *this, shapes_, indices);
00133
00134 if (debug & 1)
00135 {
00136 printStats(Pout);
00137 Pout<< "octree : --- End of Level " << deepestLevel_
00138 << " ----" << endl;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 label oldNLeaves = -1;
00155 deepestLevel_ = 1;
00156 while
00157 (
00158 (deepestLevel_ <= minNLevels_)
00159 || (
00160 (nEntries() > maxLeafRatio * nLeaves())
00161 && (nEntries() < maxShapeRatio * nShapes)
00162 )
00163 )
00164 {
00165 if (deepestLevel_ >= maxNLevels)
00166 {
00167 if (debug & 1)
00168 {
00169 Pout<< "octree : exiting since maxNLevels "
00170 << maxNLevels << " reached" << endl;
00171 }
00172 break;
00173 }
00174
00175 if (oldNLeaves == nLeaves())
00176 {
00177 if (debug & 1)
00178 {
00179 Pout<< "octree : exiting since nLeaves does not change"
00180 << endl;
00181 }
00182 break;
00183 }
00184 if (debug & 1)
00185 {
00186 Pout<< "octree : --- Start of Level " << deepestLevel_
00187 << " ----" << endl;
00188 }
00189
00190 oldNLeaves = nLeaves();
00191
00192 topNode_->redistribute
00193 (
00194 1,
00195 *this,
00196 shapes_,
00197 deepestLevel_
00198 );
00199
00200 if (debug & 1)
00201 {
00202 printStats(Pout);
00203
00204 Pout<< "octree : --- End of Level " << deepestLevel_
00205 << " ----" << endl;
00206 }
00207
00208 deepestLevel_++;
00209 }
00210
00211
00212 if (debug & 1)
00213 {
00214 Pout<< "octree : Constructed octree in = "
00215 << timer.cpuTimeIncrement()
00216 << " s\n" << endl << endl;
00217 }
00218
00219
00220 topNode_->setSubNodeType(0, *this, shapes_);
00221
00222 if (debug & 1)
00223 {
00224 Pout<< "octree : Added node information to octree in = "
00225 << timer.cpuTimeIncrement()
00226 << " s\n" << endl << endl;
00227 }
00228 }
00229
00230
00231
00232
00233 template <class Type>
00234 Foam::octree<Type>::~octree()
00235 {
00236 delete topNode_;
00237 }
00238
00239
00240
00241
00242 template <class Type>
00243 Foam::label Foam::octree<Type>::getSampleType(const point& sample) const
00244 {
00245 return topNode_->getSampleType(0, *this, shapes_, sample);
00246 }
00247
00248
00249 template <class Type>
00250 Foam::label Foam::octree<Type>::find(const point& sample) const
00251 {
00252 return topNode_->find(shapes_, sample);
00253 }
00254
00255
00256 template <class Type>
00257 bool Foam::octree<Type>::findTightest
00258 (
00259 const point& sample,
00260 treeBoundBox& tightest
00261 ) const
00262 {
00263 return topNode_->findTightest
00264 (
00265 shapes_,
00266 sample,
00267 tightest
00268 );
00269 }
00270
00271
00272 template <class Type>
00273 Foam::label Foam::octree<Type>::findNearest
00274 (
00275 const point& sample,
00276 treeBoundBox& tightest,
00277 scalar& tightestDist
00278 ) const
00279 {
00280 label tightestI = -1;
00281
00282 if (debug & 4)
00283 {
00284 Pout<< "octree::findNearest : searching for nearest for "
00285 << "sample:" << sample
00286 << " tightest:" << tightest << endl;
00287 }
00288
00289 topNode_->findNearest
00290 (
00291 shapes_,
00292 sample,
00293 tightest,
00294 tightestI,
00295 tightestDist
00296 );
00297
00298 if (debug & 4)
00299 {
00300 Pout<< "octree::findNearest : found nearest for "
00301 << "sample:" << sample << " with "
00302 << " tightestI:" << tightestI
00303 << " tightest:" << tightest
00304 << " tightestDist:" << tightestDist
00305 << endl;
00306 }
00307
00308 return tightestI;
00309 }
00310
00311
00312 template <class Type>
00313 Foam::label Foam::octree<Type>::findNearest
00314 (
00315 const linePointRef& ln,
00316 treeBoundBox& tightest,
00317 point& linePoint,
00318 point& shapePoint
00319 ) const
00320 {
00321
00322 label tightestI = -1;
00323 linePoint = point(-GREAT, -GREAT, -GREAT);
00324 shapePoint = point(GREAT, GREAT, GREAT);
00325
00326 topNode_->findNearest
00327 (
00328 shapes_,
00329 ln,
00330 tightest,
00331 tightestI,
00332 linePoint,
00333 shapePoint
00334 );
00335
00336 return tightestI;
00337 }
00338
00339
00340 template <class Type>
00341 Foam::labelList Foam::octree<Type>::findBox(const boundBox& bb) const
00342 {
00343
00344 labelHashSet elements(100);
00345
00346 topNode_->findBox(shapes_, bb, elements);
00347
00348 return elements.toc();
00349 }
00350
00351
00352 template <class Type>
00353 Foam::pointIndexHit Foam::octree<Type>::findLine
00354 (
00355 const point& treeStart,
00356 const point& treeEnd
00357 ) const
00358 {
00359
00360 pointIndexHit hitInfo(false, treeStart, -1);
00361
00362 const vector dir(treeEnd - treeStart);
00363
00364
00365 point start(treeStart);
00366 point end(treeEnd);
00367
00368 while (true)
00369 {
00370
00371 point leafIntPoint;
00372
00373 const treeLeaf<Type>* leafPtr = findLeafLine
00374 (
00375 start,
00376 end,
00377 leafIntPoint
00378 );
00379
00380 if (!leafPtr)
00381 {
00382
00383
00384 break;
00385 }
00386
00387
00388 scalar minS = GREAT;
00389
00390 const labelList& indices = leafPtr->indices();
00391
00392 forAll(indices, elemI)
00393 {
00394 label index = indices[elemI];
00395
00396 point pt;
00397 bool hit = shapes().intersects(index, start, end, pt);
00398
00399 if (hit)
00400 {
00401
00402 scalar s = (pt - treeStart) & dir;
00403
00404 if (s < minS)
00405 {
00406 minS = s;
00407
00408
00409 hitInfo.setHit();
00410 hitInfo.setPoint(pt);
00411 hitInfo.setIndex(index);
00412
00413
00414 end = pt;
00415 }
00416 }
00417 }
00418
00419 if (hitInfo.hit())
00420 {
00421
00422 break;
00423 }
00424
00425
00426 start = leafIntPoint;
00427 }
00428
00429 return hitInfo;
00430 }
00431
00432
00433 template <class Type>
00434 Foam::pointIndexHit Foam::octree<Type>::findLineAny
00435 (
00436 const point& start,
00437 const point& end
00438 ) const
00439 {
00440
00441 pointIndexHit hitInfo(false, start, -1);
00442
00443
00444 point p(start);
00445 while (true)
00446 {
00447
00448 point leafIntPoint;
00449
00450 const treeLeaf<Type>* leafPtr = findLeafLine(p, end, leafIntPoint);
00451
00452 if (!leafPtr)
00453 {
00454
00455
00456 break;
00457 }
00458
00459
00460
00461
00462 const vector edgeVec(end - p);
00463
00464 const labelList& indices = leafPtr->indices();
00465
00466 forAll(indices, elemI)
00467 {
00468 label index = indices[elemI];
00469
00470 point pt;
00471 bool hit = shapes().intersects
00472 (
00473 index,
00474 p,
00475 end,
00476 pt
00477 );
00478
00479 if (hit)
00480 {
00481 hitInfo.setHit();
00482 hitInfo.setPoint(pt);
00483 hitInfo.setIndex(index);
00484
00485 break;
00486 }
00487 }
00488
00489 if (hitInfo.hit())
00490 {
00491
00492 break;
00493 }
00494
00495
00496 p = leafIntPoint;
00497 }
00498
00499 return hitInfo;
00500 }
00501
00502
00503 template <class Type>
00504 const Foam::treeLeaf<Type>* Foam::octree<Type>::findLeafLine
00505 (
00506 const point& start,
00507 const point& end,
00508 point& leafIntPoint
00509 ) const
00510 {
00511
00512
00513 if (debug & 2)
00514 {
00515 Pout<< "octree::findLeafLine : searching for shapes on line "
00516 << "start:" << start
00517 << " end:" << end << endl;
00518 }
00519
00520
00521 if (octreeBb_.contains(start))
00522 {
00523 leafIntPoint = start;
00524 }
00525 else
00526 {
00527 if (!octreeBb_.intersects(start, end, leafIntPoint))
00528 {
00529 if (debug & 2)
00530 {
00531 Pout<< "octree::findLeafLine : start outside domain but does"
00532 << " not intersect : start:"
00533 << start << endl;
00534 }
00535 return NULL;
00536 }
00537
00538 if (debug & 2)
00539 {
00540 Pout<< "octree::findLeafLine : start propagated to inside"
00541 " domain : "
00542 << leafIntPoint << endl;
00543 }
00544 }
00545
00546
00547 const treeLeaf<Type>* leafPtr = topNode_->findLeafLine
00548 (
00549 0,
00550 shapes_,
00551 leafIntPoint,
00552 end
00553 );
00554
00555 if (debug & 2)
00556 {
00557 Pout<< "returning from octree::findLeafLine with "
00558 << "leafIntersection:" << leafIntPoint
00559 << " leafPtr:" << long(leafPtr) << endl;
00560 }
00561
00562 return leafPtr;
00563 }
00564
00565
00566 template <class Type>
00567 void Foam::octree<Type>::writeOBJ
00568 (
00569 Ostream& os,
00570 label& vertNo
00571 ) const
00572 {
00573 scalar minx = octreeBb_.min().x();
00574 scalar miny = octreeBb_.min().y();
00575 scalar minz = octreeBb_.min().z();
00576
00577 scalar maxx = octreeBb_.max().x();
00578 scalar maxy = octreeBb_.max().y();
00579 scalar maxz = octreeBb_.max().z();
00580
00581 os << "v " << minx << " " << miny << " " << minz << endl;
00582 os << "v " << maxx << " " << miny << " " << minz << endl;
00583 os << "v " << maxx << " " << maxy << " " << minz << endl;
00584 os << "v " << minx << " " << maxy << " " << minz << endl;
00585
00586 os << "v " << minx << " " << miny << " " << maxz << endl;
00587 os << "v " << maxx << " " << miny << " " << maxz << endl;
00588 os << "v " << maxx << " " << maxy << " " << maxz << endl;
00589 os << "v " << minx << " " << maxy << " " << maxz << endl;
00590
00591
00592 os << "l " << vertNo + 1 << " " << vertNo + 2 << endl;
00593 os << "l " << vertNo + 2 << " " << vertNo + 3 << endl;
00594 os << "l " << vertNo + 3 << " " << vertNo + 4 << endl;
00595 os << "l " << vertNo + 4 << " " << vertNo + 1 << endl;
00596
00597
00598 os << "l " << vertNo + 5 << " " << vertNo + 6 << endl;
00599 os << "l " << vertNo + 6 << " " << vertNo + 7 << endl;
00600 os << "l " << vertNo + 7 << " " << vertNo + 8 << endl;
00601 os << "l " << vertNo + 8 << " " << vertNo + 5 << endl;
00602
00603
00604 os << "l " << vertNo + 1 << " " << vertNo + 5 << endl;
00605 os << "l " << vertNo + 2 << " " << vertNo + 6 << endl;
00606 os << "l " << vertNo + 3 << " " << vertNo + 7 << endl;
00607 os << "l " << vertNo + 4 << " " << vertNo + 8 << endl;
00608
00609 vertNo += 8;
00610
00611 topNode_->writeOBJ(os, 1, vertNo);
00612 }
00613
00614
00615 template <class Type>
00616 void Foam::octree<Type>::printStats(Ostream& os) const
00617 {
00618 os << "Statistics after iteration " << deepestLevel() << ':' << endl
00619 << " nShapes :" << shapes().size() << endl
00620 << " nNodes :" << nNodes() << endl
00621 << " nLeaves :" << nLeaves() << endl
00622 << " nEntries :" << nEntries() << endl;
00623
00624 if (nLeaves() && shapes().size())
00625 {
00626 os
00627 << " Cells per leaf :"
00628 << scalar(nEntries())/nLeaves()
00629 << nl
00630 << " Every cell in :"
00631 << scalar(nEntries())/shapes().size() << " cubes"
00632 << endl;
00633 }
00634 }
00635
00636
00637
00638
00639
00640 template <class Type>
00641 Foam::octree<Type>::iterator::iterator(octree<Type>& oc)
00642 :
00643 octree_(oc),
00644 curLeaf_(oc.nLeaves())
00645 {
00646 leaves_.setSize(0);
00647 }
00648
00649
00650
00651 template <class Type>
00652 Foam::octree<Type>::iterator::iterator(octree<Type>& oc, label index)
00653 :
00654 octree_(oc),
00655 curLeaf_(index)
00656 {
00657 if (index >= 0)
00658 {
00659 leaves_.setSize(oc.nLeaves());
00660
00661 label leafIndex = 0;
00662 octree_.topNode()->findLeaves(leaves_, leafIndex);
00663
00664 if (leafIndex != oc.nLeaves())
00665 {
00666 FatalErrorIn
00667 (
00668 "octree::iterator::iterator"
00669 "(octree&, label)"
00670 )
00671 << "Traversal of tree returns : " << leafIndex << endl
00672 << "Statistics of octree say : " << oc.nLeaves() << endl
00673 << abort(FatalError);
00674 }
00675 }
00676 }
00677
00678
00679 template <class Type>
00680 void Foam::octree<Type>::iterator::operator=(const iterator& iter)
00681 {
00682 if ((curLeaf_ < 0) && (iter.curLeaf_ >= 0))
00683 {
00684 FatalErrorIn
00685 (
00686 "octree::iterator::operator="
00687 "(const iterator&)"
00688 )
00689 << "lhs : " << curLeaf_ << endl
00690 << "rhs : " << iter.curLeaf_ << endl
00691 << abort(FatalError);
00692 }
00693 curLeaf_ = iter.curLeaf_;
00694 }
00695
00696
00697 template <class Type>
00698 bool Foam::octree<Type>::iterator::operator==(const iterator& iter) const
00699 {
00700 label index1 =
00701 (curLeaf_ >= 0 ? curLeaf_ : octree_.nLeaves());
00702 label index2 =
00703 (iter.curLeaf_ >= 0 ? iter.curLeaf_ : iter.octree_.nLeaves());
00704
00705 return index1 == index2;
00706 }
00707
00708
00709 template <class Type>
00710 bool Foam::octree<Type>::iterator::operator!=(const iterator& iter) const
00711 {
00712 return !(iterator::operator==(iter));
00713 }
00714
00715
00716 template <class Type>
00717 Foam::treeLeaf<Type>& Foam::octree<Type>::iterator::operator*()
00718 {
00719 return *leaves_[curLeaf_];
00720 }
00721
00722
00723 template <class Type>
00724 typename Foam::octree<Type>::iterator&
00725 Foam::octree<Type>::iterator::operator++()
00726 {
00727 curLeaf_++;
00728 return *this;
00729 }
00730
00731
00732 template <class Type>
00733 typename Foam::octree<Type>::iterator
00734 Foam::octree<Type>::iterator::operator++(int)
00735 {
00736 iterator tmp = *this;
00737 ++*this;
00738 return tmp;
00739 }
00740
00741
00742 template <class Type>
00743 typename Foam::octree<Type>::iterator
00744 Foam::octree<Type>::begin()
00745 {
00746 return iterator(*this, 0);
00747 }
00748
00749
00750 template <class Type>
00751 const typename Foam::octree<Type>::iterator&
00752 Foam::octree<Type>::end()
00753 {
00754 return octree<Type>::endIter_;
00755 }
00756
00757
00758
00759
00760
00761 template <class Type>
00762 Foam::octree<Type>::const_iterator::const_iterator(const octree<Type>& oc)
00763 :
00764 octree_(oc),
00765 curLeaf_(oc.nLeaves())
00766 {
00767 leaves_.setSize(oc.nLeaves());
00768 }
00769
00770
00771
00772 template <class Type>
00773 Foam::octree<Type>::const_iterator::const_iterator
00774 (
00775 const octree<Type>& oc,
00776 label index
00777 )
00778 :
00779 octree_(oc),
00780 curLeaf_(index)
00781 {
00782 if (index >= 0)
00783 {
00784 leaves_.setSize(oc.nLeaves());
00785
00786 label leafIndex = 0;
00787 octree_.topNode()->findLeaves(leaves_, leafIndex);
00788
00789 if (leafIndex != oc.nLeaves())
00790 {
00791 FatalErrorIn
00792 (
00793 "octree::const_iterator::const_iterator"
00794 "(octree&, label)"
00795 )
00796 << "Traversal of tree returns : " << leafIndex << endl
00797 << "Statistics of octree say : " << oc.nLeaves() << endl
00798 << abort(FatalError);
00799 }
00800 }
00801 }
00802
00803
00804 template <class Type>
00805 void Foam::octree<Type>::const_iterator::operator=(const const_iterator& iter)
00806 {
00807 if ((curLeaf_ < 0) && (iter.curLeaf_ >= 0))
00808 {
00809 FatalErrorIn
00810 (
00811 "octree::const_iterator::operator="
00812 "(const const_iterator&)"
00813 )
00814 << "lhs : " << curLeaf_ << endl
00815 << "rhs : " << iter.curLeaf_ << endl
00816 << abort(FatalError);
00817 }
00818 curLeaf_ = iter.curLeaf_;
00819 curLeaf_ = iter.curLeaf_;
00820 }
00821
00822
00823 template <class Type>
00824 bool Foam::octree<Type>::const_iterator::operator==
00825 (
00826 const const_iterator& iter
00827 ) const
00828 {
00829 label index1 =
00830 (curLeaf_ >= 0 ? curLeaf_ : octree_.nLeaves());
00831 label index2 =
00832 (iter.curLeaf_ >= 0 ? iter.curLeaf_ : iter.octree_.nLeaves());
00833
00834 return index1 == index2;
00835 }
00836
00837
00838 template <class Type>
00839 bool Foam::octree<Type>::const_iterator::operator!=
00840 (
00841 const const_iterator& iter
00842 ) const
00843 {
00844 return !(const_iterator::operator==(iter));
00845 }
00846
00847
00848 template <class Type>
00849 const Foam::treeLeaf<Type>& Foam::octree<Type>::const_iterator::operator*()
00850 {
00851 return *leaves_[curLeaf_];
00852 }
00853
00854
00855 template <class Type>
00856 typename Foam::octree<Type>::const_iterator&
00857 Foam::octree<Type>::const_iterator::operator++()
00858 {
00859 curLeaf_++;
00860 return *this;
00861 }
00862
00863
00864 template <class Type>
00865 typename Foam::octree<Type>::const_iterator
00866 Foam::octree<Type>::const_iterator::operator++(int)
00867 {
00868 const_iterator tmp = *this;
00869 ++*this;
00870 return tmp;
00871 }
00872
00873
00874 template <class Type>
00875 typename Foam::octree<Type>::const_iterator
00876 Foam::octree<Type>::begin() const
00877 {
00878 return const_iterator(*this, 0);
00879 }
00880
00881
00882 template <class Type>
00883 typename Foam::octree<Type>::const_iterator
00884 Foam::octree<Type>::cbegin() const
00885 {
00886 return const_iterator(*this, 0);
00887 }
00888
00889
00890 template <class Type>
00891 const typename Foam::octree<Type>::const_iterator&
00892 Foam::octree<Type>::end() const
00893 {
00894 return octree<Type>::endConstIter_;
00895 }
00896
00897
00898 template <class Type>
00899 const typename Foam::octree<Type>::const_iterator&
00900 Foam::octree<Type>::cend() const
00901 {
00902 return octree<Type>::endConstIter_;
00903 }
00904
00905
00906
00907
00908 template <class Type>
00909 Foam::Ostream& Foam::operator<<(Ostream& os, const octree<Type>& oc)
00910 {
00911 return os << token::BEGIN_LIST
00912
00913 << token::SPACE << oc.octreeBb_
00914 << token::SPACE << oc.maxLeafRatio_
00915 << token::SPACE << oc.maxShapeRatio_
00916 << token::SPACE << oc.minNLevels_
00917 << token::SPACE << oc.deepestLevel_
00918 << token::SPACE << oc.nEntries_
00919 << token::SPACE << oc.nNodes_
00920 << token::SPACE << oc.nLeaves_
00921 << token::SPACE << *oc.topNode_
00922 << token::SPACE << token::END_LIST;
00923 }
00924
00925
00926