OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

treeNode.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "treeNode.H"
00030 #include "octree.H"
00031 #include "treeLeaf.H"
00032 #include "treeBoundBox.H"
00033 #include "long.H"
00034 #include "linePointRef.H"
00035 
00036 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00037 
00038 template <class Type>
00039 const Foam::label Foam::treeNode<Type>::leafOffset(100);
00040 
00041 
00042 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00043 
00044 template <class Type>
00045 void Foam::treeNode<Type>::setAsNode(const label octant)
00046 {
00047     subNodeTypes_ |= (0x1 << octant);
00048 }
00049 
00050 
00051 template <class Type>
00052 void Foam::treeNode<Type>::setAsLeaf(const label octant)
00053 {
00054     subNodeTypes_ &= ~(0x1 << octant);
00055 }
00056 
00057 
00058 // Set pointer to sub node
00059 template <class Type>
00060 void Foam::treeNode<Type>::setNodePtr
00061 (
00062     const label octant,
00063     treeElem<Type>* treeNodePtr
00064 )
00065 {
00066     setAsNode(octant);
00067     subNodes_[octant] = treeNodePtr;
00068 }
00069 
00070 
00071 // Set pointer to sub leaf
00072 template <class Type>
00073 void Foam::treeNode<Type>::setLeafPtr
00074 (
00075     const label octant,
00076     treeElem<Type>* treeLeafPtr
00077 )
00078 {
00079     setAsLeaf(octant);
00080     subNodes_[octant] = treeLeafPtr;
00081 }
00082 
00083 
00084 template <class Type>
00085 void Foam::treeNode<Type>::setVolType
00086 (
00087     const label octant,
00088     const label type
00089 )
00090 {
00091     if ((type < 0) || (type > 3))
00092     {
00093         FatalErrorIn("treeNode<Type>::setVolType(const label, const label)")
00094             << "Type " << type << " not within range 0..3" << endl;
00095     }
00096 
00097     // Clear out two bits at position 2*octant
00098     volType_ &= ~(0x3 << 2*octant);
00099 
00100     // Add the two bits of type
00101     volType_ |= (type << 2*octant);
00102 }
00103 
00104 
00105 template <class Type>
00106 void Foam::treeNode<Type>::space(Ostream& os, const label n)
00107 {
00108     for (label i=0; i<n; i++)
00109     {
00110         os<< ' ';
00111     }
00112 }
00113 
00114 
00115 // look in single octant starting from <start>
00116 template <class Type>
00117 const Foam::treeLeaf<Type>* Foam::treeNode<Type>::findLeafLineOctant
00118 (
00119     const int level,
00120     const Type& shapes,
00121     const label octant,
00122     const vector& direction,
00123     point& start,
00124     const point& end
00125 ) const
00126 {
00127     static const char* functionName =
00128         "treeNode<Type>::findLeafLineOctant"
00129         "(const int, const Type&, const label, const vector&,"
00130         " point&, const point&)";
00131 
00132     if (debug & 2)
00133     {
00134         space(Pout, 2*level);
00135         Pout<< "findLeafLineOctant : bb:" << this->bb()
00136             << "  start:" << start
00137             << "  end:" << end
00138             << "  mid:" << midpoint()
00139             << " Searching octant:" << octant
00140             << endl;
00141     }
00142 
00143     if (subNodes()[octant])
00144     {
00145         if (isNode(octant))
00146         {
00147             // Node: recurse into subnodes
00148             const treeNode<Type>* subNodePtr = getNodePtr(octant);
00149 
00150             if (subNodePtr->bb().contains(direction, start))
00151             {
00152                 // Search on lower level
00153                 const treeLeaf<Type>* subLeafPtr = subNodePtr->findLeafLine
00154                 (
00155                     level + 1,
00156                     shapes,
00157                     start,
00158                     end
00159                 );
00160 
00161                 if (debug & 2)
00162                 {
00163                     space(Pout, 2*level);
00164                     Pout<< "findLeafLineOctant : bb:" << this->bb()
00165                         << " returning from sub treeNode"
00166                         << " with start:" << start << "  subLeaf:"
00167                         << long(subLeafPtr) << endl;
00168                 }
00169 
00170                 return subLeafPtr;
00171             }
00172             else
00173             {
00174                 FatalErrorIn(functionName)
00175                     << "Sub node " << subNodePtr->bb()
00176                     << " at octant " << octant
00177                     << " does not contain start " << start
00178                     << abort(FatalError);
00179             }
00180         }
00181         else
00182         {
00183             // Leaf
00184             const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
00185 
00186             if (subLeafPtr->bb().contains(direction, start))
00187             {
00188                 // Step to end of subleaf bb
00189                 point tmp;
00190                 if
00191                 (
00192                     !subLeafPtr->bb().intersects
00193                     (
00194                         end,
00195                         start,
00196                         tmp
00197                     )
00198                 )
00199                 {
00200                     FatalErrorIn(functionName)
00201                         << "Sub leaf contains start " << start
00202                         << " but line does not intersect its bb "
00203                         << subLeafPtr->bb()
00204                         << abort(FatalError);
00205                 }
00206                 start = tmp;
00207 
00208                 if (debug & 2)
00209                 {
00210                     space(Pout, 2*level);
00211                     Pout<< "findLeafLineOctant : returning from intersecting"
00212                         << " treeLeaf " << subLeafPtr->bb()
00213                         << " with start:" << start << "  subLeaf:"
00214                         << long(subLeafPtr) << endl;
00215                 }
00216 
00217                 return subLeafPtr;
00218             }
00219             else
00220             {
00221                 FatalErrorIn(functionName)
00222                     << "Sub leaf " << subLeafPtr->bb()
00223                     << " at octant " << octant
00224                     << " does not contain start " << start
00225                     << abort(FatalError);
00226             }
00227         }
00228     }
00229     else
00230     {
00231         // Empty subNode. Transfer across.
00232         const treeBoundBox emptyBb = this->bb().subBbox(midpoint(), octant);
00233 
00234         if (emptyBb.contains(direction, start))
00235         {
00236             if (debug & 2)
00237             {
00238                 space(Pout, 2*level);
00239                 Pout<< "findLeafLineOctant : Empty node. Octant:" << octant
00240                     << "  start:" << start
00241                     << "  bb:" << this->bb()
00242                     << "  emptyBb:" << emptyBb << endl;
00243             }
00244 
00245             // Update start by clipping to emptyBb
00246             point tmp;
00247             if
00248             (
00249                 !emptyBb.intersects
00250                 (
00251                     end,
00252                     start,
00253                     tmp
00254                 )
00255             )
00256             {
00257                 FatalErrorIn(functionName)
00258                     << "Empty node contains start " << start
00259                     << " but line does not intersect its (calculated)"
00260                     << " bb " << emptyBb
00261                     << endl << "This might be due to truncation error"
00262                     << abort(FatalError);
00263             }
00264             start = tmp;
00265 
00266             if (debug & 2)
00267             {
00268                 space(Pout, 2*level);
00269                 Pout<< "findLeafLineOctant : returning from intersecting with"
00270                     << " empty " << emptyBb
00271                     << " with start:" << start << "  subLeaf:" << 0 << endl;
00272             }
00273 
00274             return NULL;
00275         }
00276         else
00277         {
00278             FatalErrorIn(functionName)
00279                 << "Empty node " << emptyBb
00280                 << " at octant " << octant
00281                 << " does not contain start " << start
00282                 << abort(FatalError);
00283         }
00284     }
00285 
00286     FatalErrorIn(functionName)
00287         << "Octant " << octant << " of cube " << this->bb()
00288         << " does not contain start " << start
00289         << abort(FatalError);
00290 
00291     return NULL;
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00296 
00297 // Construct from components
00298 template <class Type>
00299 Foam::treeNode<Type>::treeNode(const treeBoundBox& bb)
00300 :
00301     treeElem<Type>(bb),
00302     treeNodeName(),
00303     mid_(bb.midpoint()),
00304     subNodeTypes_(0),
00305     volType_(0)
00306 {
00307     for (label octantI=0; octantI<8; octantI++)
00308     {
00309         subNodes_[octantI] = NULL;
00310         setVolType(octantI, octree<Type>::UNKNOWN);
00311     }
00312 }
00313 
00314 
00315 // Construct from Istream
00316 template <class Type>
00317 Foam::treeNode<Type>::treeNode(Istream& is)
00318 {
00319     is >> *this;
00320 }
00321 
00322 
00323 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00324 
00325 template <class Type>
00326 Foam::treeNode<Type>::~treeNode()
00327 {
00328     for (int octant=0; octant<8; octant++)
00329     {
00330         if (subNodes()[octant])
00331         {
00332             if (isNode(octant))
00333             {
00334                 delete getNodePtr(octant);
00335             }
00336             else
00337             {
00338                 delete getLeafPtr(octant);
00339             }
00340         }
00341     }
00342 }
00343 
00344 
00345 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00346 
00347 // Distributes cells to subLeaves
00348 template <class Type>
00349 void Foam::treeNode<Type>::distribute
00350 (
00351     const label level,
00352     octree<Type>& top,
00353     const Type& shapes,
00354     const labelList& indices
00355 )
00356 {
00357     if (debug & 1)
00358     {
00359         space(Pout, level);
00360         Pout<< "treeNode::distributing " << indices.size() << endl;
00361     }
00362 
00363     // Create subLeaves if necessary
00364     for (label octant=0; octant<8; octant++)
00365     {
00366         if (subNodes()[octant])
00367         {
00368             printNode(Pout, level);
00369             FatalErrorIn
00370             (
00371                 "treeNode<Type>::distribute(const label, octree<Type>&, "
00372                 "const Type&, const labelList&)"
00373             )   << "subNode already available at octant:" << octant
00374                 << abort(FatalError);
00375         }
00376         else
00377         {
00378             treeLeaf<Type>* subLeafPtr = new treeLeaf<Type>
00379             (
00380                 this->bb().subBbox(midpoint(), octant),
00381                 indices.size()
00382             );
00383 
00384             top.setLeaves(top.nLeaves() + 1);
00385             setLeafPtr(octant, subLeafPtr);
00386         }
00387     }
00388 
00389 
00390     // add cells to correct sub leaf
00391     forAll(indices, i)
00392     {
00393         const label shapei = indices[i];
00394 
00395         for (label octant=0; octant<8; octant++)
00396         {
00397             treeLeaf<Type>* leafPtr = getLeafPtr(octant);
00398 
00399             if (shapes.overlaps(shapei, leafPtr->bb()))
00400             {
00401                 if (debug == 1)
00402                 {
00403                     space(Pout, level);
00404                     Pout<< "inserting " << shapei;
00405                     shapes.write(Pout, shapei);
00406                     Pout<< " into " << leafPtr->bb() << endl;
00407                 }
00408                 leafPtr->insert(shapei);
00409                 top.setEntries(top.nEntries() + 1);
00410             }
00411         }
00412     }
00413 
00414     // Trim size of subLeaves
00415     for (label octant=0; octant<8; octant++)
00416     {
00417         treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
00418 
00419         if (subLeafPtr->size() == 0)
00420         {
00421             // Contains no data. Delete.
00422             setLeafPtr(octant, NULL);
00423             delete subLeafPtr;
00424             top.setLeaves(top.nLeaves() - 1);
00425         }
00426         else
00427         {
00428             // Trim to actual size.
00429             subLeafPtr->trim();
00430         }
00431     }
00432 
00433     if (debug & 1)
00434     {
00435         space(Pout, level);
00436         Pout<< "end of treeNode::distribute" << endl;
00437     }
00438 }
00439 
00440 
00441 // Descends to refineLevel and checks the subLeaves for redistribution
00442 template <class Type>
00443 void Foam::treeNode<Type>::redistribute
00444 (
00445     const label level,
00446     octree<Type>& top,
00447     const Type& shapes,
00448     const label refineLevel
00449 )
00450 {
00451     if (debug & 1)
00452     {
00453         space(Pout, level);
00454         Pout<< "treeNode::redistribute with level:" << level
00455             << "  refineLevel:" << refineLevel << endl;
00456     }
00457 
00458     // Descend to correct level
00459     if (level < refineLevel)
00460     {
00461         for (label octant=0; octant<8; octant++)
00462         {
00463             if (subNodes()[octant])
00464             {
00465                 if (isNode(octant))
00466                 {
00467                     getNodePtr(octant)->redistribute
00468                     (
00469                         level+1,
00470                         top,
00471                         shapes,
00472                         refineLevel
00473                     );
00474                 }
00475             }
00476         }
00477     }
00478     else
00479     {
00480         // Reached correct (should also be deepest) level of treeNode
00481         if (debug & 1)
00482         {
00483             space(Pout, level);
00484             Pout<< "treeNode::redistribute : now at correct level" << endl;
00485         }
00486 
00487         // handle redistribution of sub leaves
00488         for (label octant=0; octant<8; octant++)
00489         {
00490             if (subNodes()[octant])
00491             {
00492                 if (isNode(octant))
00493                 {
00494                     FatalErrorIn
00495                     (
00496                         "treeNode<Type>::redistribute(const int, octree& top,"
00497                         "const int, const treeBoundBox&)"
00498                     )   << "found treeNode instead of treeLeaf" << endl
00499                         << abort(FatalError);
00500                 }
00501                 else
00502                 {
00503                     treeLeaf<Type>* leafPtr = getLeafPtr(octant);
00504 
00505                     treeLeaf<Type>* newSubPtr = leafPtr->redistribute
00506                     (
00507                         level,
00508                         top,
00509                         shapes
00510                     );
00511 
00512                     if (newSubPtr && (newSubPtr != leafPtr))
00513                     {
00514                         // redistribute has created nodePtr
00515                         // so delete no longer used subPtr and update info
00516                         if (debug & 1)
00517                         {
00518                             Pout<< "deleting "
00519                                 << top.nEntries() - leafPtr->size()
00520                                 << " entries" << endl;
00521                         }
00522                         top.setEntries(top.nEntries() - leafPtr->size());
00523 
00524                         delete leafPtr;
00525 
00526                         top.setLeaves(top.nLeaves() - 1);
00527 
00528                         setNodePtr(octant, newSubPtr);
00529                     }
00530                 }
00531             }
00532         }
00533         if (debug & 1)
00534         {
00535             space(Pout, level);
00536             Pout<< "end of treeNode::redistribute for correct level" << endl;
00537         }
00538     }
00539 
00540     if (debug & 1)
00541     {
00542         space(Pout, level);
00543         Pout<< "return from treeNode::redistribute with bb:" << this->bb()
00544             << endl;
00545     }
00546 }
00547 
00548 
00549 // Set type of node.
00550 template <class Type>
00551 Foam::label Foam::treeNode<Type>::setSubNodeType
00552 (
00553     const label level,
00554     octree<Type>& top,
00555     const Type& shapes
00556 )
00557 {
00558     if (debug & 4)
00559     {
00560         space(Pout, level);
00561         Pout<< "treeNode::setSubNodeType with level:" << level
00562             << "   bb:" << this->bb() << endl;
00563     }
00564 
00565     label myType = -1;
00566 
00567     for (label octant=0; octant<8; octant++)
00568     {
00569         label subType = -1;
00570 
00571         if (subNodes()[octant])
00572         {
00573             if (isNode(octant))
00574             {
00575                 subType = getNodePtr(octant)->setSubNodeType
00576                 (
00577                     level+1,
00578                     top,
00579                     shapes
00580                 );
00581             }
00582             else
00583             {
00584                 subType = getLeafPtr(octant)->setSubNodeType
00585                 (
00586                     level+1,
00587                     top,
00588                     shapes
00589                 );
00590             }
00591         }
00592         else
00593         {
00594             // No data in this one. Set type for octant acc. to its bounding
00595             // box.
00596             const treeBoundBox subBb = this->bb().subBbox(midpoint(), octant);
00597 
00598             subType = shapes.getSampleType(top, subBb.midpoint());
00599         }
00600 
00601         if (debug & 4)
00602         {
00603             space(Pout, level);
00604             Pout<< "treeNode::setSubNodeType : setting octant with bb:"
00605                 << this->bb().subBbox(midpoint(), octant)
00606                 << "  to type:" << octree<Type>::volType(subType) << endl;
00607         }
00608         setVolType(octant, subType);
00609 
00610         // Combine sub node types into type for treeNode. Result is 'mixed' if
00611         // types differ among subnodes.
00612         if (myType == -1)
00613         {
00614             myType = subType;
00615         }
00616         else if (subType != myType)
00617         {
00618             myType = octree<Type>::MIXED;
00619         }
00620     }
00621 
00622     if (debug & 4)
00623     {
00624         space(Pout, level);
00625         Pout<< "return from treeNode::setSubNodeType with type:"
00626             << octree<Type>::volType(myType)
00627             << "  bb:" << this->bb() << endl;
00628     }
00629 
00630     return myType;
00631 }
00632 
00633 
00634 // Get type of node.
00635 template <class Type>
00636 Foam::label Foam::treeNode<Type>::getSampleType
00637 (
00638     const label level,
00639     const octree<Type>& top,
00640     const Type& shapes,
00641     const point& sample
00642 ) const
00643 {
00644     if (debug & 4)
00645     {
00646         space(Pout, level);
00647         Pout<< "treeNode::getSampleType with level:" << level
00648             << " bb:" << this->bb() << "  sample:" << sample << endl;
00649     }
00650 
00651     // Determine octant of bb. If on edge just use whichever octant.
00652     bool onEdge = false;
00653 
00654     label octant = this->bb().subOctant(midpoint(), sample, onEdge);
00655 
00656     label type = getVolType(octant);
00657 
00658     if (type == octree<Type>::MIXED)
00659     {
00660         // At this level multiple sub types. Recurse to resolve.
00661         if (subNodes()[octant])
00662         {
00663             if (isNode(octant))
00664             {
00665                 // Node: recurse into subnodes
00666                 type = getNodePtr(octant)->getSampleType
00667                 (
00668                     level + 1,
00669                     top,
00670                     shapes,
00671                     sample
00672                 );
00673             }
00674             else
00675             {
00676                 // Leaf
00677                 type = getLeafPtr(octant)->getSampleType
00678                 (
00679                     level + 1,
00680                     top,
00681                     shapes,
00682                     sample
00683                 );
00684             }
00685         }
00686         else
00687         {
00688             // Problem: empty subnode should have a type
00689             FatalErrorIn
00690             (
00691                 "treeNode<Type>::getSampleType"
00692                 "(const label, octree<Type>&, const Type&, const point&)"
00693             )   << "Empty node bb:" << this->bb().subBbox(midpoint(), octant)
00694                 << " has non-mixed type:"
00695                 << octree<Type>::volType(type)
00696                 << abort(FatalError);
00697         }
00698     }
00699 
00700     if (type == octree<Type>::MIXED)
00701     {
00702         FatalErrorIn
00703         (
00704             "treeNode<Type>::getSampleType"
00705             "(const label, octree<Type>&, const Type&, const point&)"
00706         )   << "Type is MIXED when searching for " << sample
00707             << " at level " << this->bb() << endl
00708             << "This probably is because the octree has not been constructed"
00709             << " with search facility." << exit(FatalError);
00710     }
00711 
00712     if (debug & 4)
00713     {
00714         space(Pout, level);
00715         Pout<< "return from treeNode::getSampleType with type:"
00716             << octree<Type>::volType(type)
00717             << "  bb:" << this->bb()
00718             << "  sample:" << sample << endl;
00719     }
00720     return type;
00721 }
00722 
00723 
00724 template <class Type>
00725 Foam::label Foam::treeNode<Type>::find
00726 (
00727     const Type& shapes,
00728     const point& sample
00729 ) const
00730 {
00731     // Find octant of sample. Don't care if on edge (since any item on edge
00732     // will have been inserted in both subcubes)
00733     bool onEdge = false;
00734 
00735     label octant = this->bb().subOctant(midpoint(), sample, onEdge);
00736 
00737     if (subNodes()[octant])
00738     {
00739         if (isNode(octant))
00740         {
00741             // Node: recurse into subnodes
00742             return getNodePtr(octant)->find(shapes, sample);
00743         }
00744         else
00745         {
00746             // Leaf: let leaf::find handle this
00747             return getLeafPtr(octant)->find(shapes, sample);
00748         }
00749     }
00750     return -1;
00751 }
00752 
00753 
00754 template <class Type>
00755 bool Foam::treeNode<Type>::findTightest
00756 (
00757     const Type& shapes,
00758     const point& sample,
00759     treeBoundBox& tightest
00760 ) const
00761 {
00762     bool changed = false;
00763     bool onEdge = false;
00764     // Estimate for best place to start searching
00765     label sampleOctant = this->bb().subOctant(midpoint(), sample, onEdge);
00766 
00767     // Go into all suboctants (one containing sample first) and update tightest.
00768     // Order of visiting is if e.g. sampleOctant = 5:
00769     //  5 1 2 3 4 0 6 7
00770     for (label octantI=0; octantI<8; octantI++)
00771     {
00772         label octant;
00773         if (octantI == 0)
00774         {
00775             // Use sampleOctant first
00776             octant = sampleOctant;
00777         }
00778         else if (octantI == sampleOctant)
00779         {
00780             octant = 0;
00781         }
00782         else
00783         {
00784             octant = octantI;
00785         }
00786 
00787         if (subNodes()[octant])
00788         {
00789             if (isNode(octant))
00790             {
00791                 // Node: recurse into subnodes
00792                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
00793 
00794                 if (subNodePtr->bb().overlaps(tightest))
00795                 {
00796                     // there might be a better fit inside this subNode
00797                     changed |= subNodePtr->findTightest
00798                     (
00799                         shapes,
00800                         sample,
00801                         tightest
00802                     );
00803                 }
00804             }
00805             else
00806             {
00807                 // Leaf: let leaf::find handle this
00808                 const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
00809 
00810                 if (subLeafPtr->bb().overlaps(tightest))
00811                 {
00812                     // there might be a better fit inside this subLeaf
00813                     changed |= subLeafPtr->findTightest
00814                     (
00815                         shapes,
00816                         sample,
00817                         tightest
00818                     );
00819                 }
00820             }
00821         }
00822     }
00823 
00824     return changed;
00825 }
00826 
00827 
00828 template <class Type>
00829 bool Foam::treeNode<Type>::findNearest
00830 (
00831     const Type& shapes,
00832     const point& sample,
00833     treeBoundBox& tightest,
00834     label& tightestI,
00835     scalar& tightestDist
00836 ) const
00837 {
00838     if (debug & 8)
00839     {
00840         Pout<< "In findNearest with sample:" << sample << " cube:"
00841             << this->bb() << " tightest:" << tightest << endl;
00842     }
00843 
00844     bool changed = false;
00845     bool onEdge = false;
00846     // Estimate for best place to start searching
00847     label sampleOctant = this->bb().subOctant(midpoint(), sample, onEdge);
00848 
00849     // Go into all suboctants (one containing sample first) and update tightest.
00850     // Order of visiting is if e.g. sampleOctant = 5:
00851     //  5 1 2 3 4 0 6 7
00852     for (label octantI=0; octantI<8; octantI++)
00853     {
00854         label octant;
00855         if (octantI == 0)
00856         {
00857             // Use sampleOctant first
00858             octant = sampleOctant;
00859         }
00860         else if (octantI == sampleOctant)
00861         {
00862             octant = 0;
00863         }
00864         else
00865         {
00866             octant = octantI;
00867         }
00868 
00869         if (subNodes()[octant])
00870         {
00871             if (isNode(octant))
00872             {
00873                 // Node
00874                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
00875 
00876                 if (subNodePtr->bb().overlaps(tightest))
00877                 {
00878                     // there might be a better fit inside this subNode
00879                     changed |= subNodePtr->findNearest
00880                     (
00881                         shapes,
00882                         sample,
00883                         tightest,
00884                         tightestI,
00885                         tightestDist
00886                     );
00887                 }
00888             }
00889             else
00890             {
00891                 // Leaf: let leaf::find handle this
00892                 const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
00893 
00894                 if (subLeafPtr->bb().overlaps(tightest))
00895                 {
00896                     // there might be a better fit inside this subNode
00897                     changed |= subLeafPtr->findNearest
00898                     (
00899                         shapes,
00900                         sample,
00901                         tightest,
00902                         tightestI,
00903                         tightestDist
00904                     );
00905                 }
00906             }
00907         }
00908     }
00909 
00910     if (debug & 8)
00911     {
00912         Pout<< "Exiting findNearest for sample:" << sample << " cube:"
00913             << this->bb() << " tightestI:" << tightestI << endl;
00914     }
00915 
00916     return changed;
00917 }
00918 
00919 
00920 template <class Type>
00921 bool Foam::treeNode<Type>::findNearest
00922 (
00923     const Type& shapes,
00924     const linePointRef& ln,
00925     treeBoundBox& tightest,
00926     label& tightestI,
00927     point& linePoint,   // nearest point on line
00928     point& shapePoint   // nearest point on shape
00929 ) const
00930 {
00931     bool changed = false;
00932     bool onEdge = false;
00933     // Estimate for best place to start searching
00934     label sampleOctant = this->bb().subOctant(midpoint(), ln.centre(), onEdge);
00935 
00936     // Go into all suboctants (one containing sample first) and update tightest.
00937     // Order of visiting is if e.g. sampleOctant = 5:
00938     //  5 1 2 3 4 0 6 7
00939     for (label octantI=0; octantI<8; octantI++)
00940     {
00941         label octant;
00942         if (octantI == 0)
00943         {
00944             // Use sampleOctant first
00945             octant = sampleOctant;
00946         }
00947         else if (octantI == sampleOctant)
00948         {
00949             octant = 0;
00950         }
00951         else
00952         {
00953             octant = octantI;
00954         }
00955 
00956         if (subNodes()[octant])
00957         {
00958             if (isNode(octant))
00959             {
00960                 // Node
00961                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
00962 
00963                 if (subNodePtr->bb().overlaps(tightest))
00964                 {
00965                     // there might be a better fit inside this subNode
00966                     changed |= subNodePtr->findNearest
00967                     (
00968                         shapes,
00969                         ln,
00970                         tightest,
00971                         tightestI,
00972                         linePoint,
00973                         shapePoint
00974                     );
00975                 }
00976             }
00977             else
00978             {
00979                 // Leaf: let leaf::find handle this
00980                 const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
00981 
00982                 if (subLeafPtr->bb().overlaps(tightest))
00983                 {
00984                     // there might be a better fit inside this subNode
00985                     changed |= subLeafPtr->findNearest
00986                     (
00987                         shapes,
00988                         ln,
00989                         tightest,
00990                         tightestI,
00991                         linePoint,
00992                         shapePoint
00993                     );
00994                 }
00995             }
00996         }
00997     }
00998 
00999     return changed;
01000 }
01001 
01002 
01003 template <class Type>
01004 bool Foam::treeNode<Type>::findBox
01005 (
01006     const Type& shapes,
01007     const boundBox& box,
01008     labelHashSet& elements
01009 ) const
01010 {
01011     bool changed = false;
01012     bool onEdge = false;
01013     // Estimate for best place to start searching
01014     label sampleOctant = this->bb().subOctant
01015     (
01016         midpoint(),
01017         box.midpoint(),
01018         onEdge
01019     );
01020 
01021     // Go into all suboctants (one containing sample first) and update tightest.
01022     // Order of visiting is if e.g. sampleOctant = 5:
01023     //  5 1 2 3 4 0 6 7
01024     for (label octantI=0; octantI<8; octantI++)
01025     {
01026         label octant;
01027         if (octantI == 0)
01028         {
01029             // Use sampleOctant first
01030             octant = sampleOctant;
01031         }
01032         else if (octantI == sampleOctant)
01033         {
01034             octant = 0;
01035         }
01036         else
01037         {
01038             octant = octantI;
01039         }
01040 
01041         if (subNodes()[octant])
01042         {
01043             if (isNode(octant))
01044             {
01045                 // Node
01046                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
01047 
01048                 if (subNodePtr->bb().overlaps(box))
01049                 {
01050                     // Visit sub node.
01051                     changed |= subNodePtr->findBox(shapes, box, elements);
01052                 }
01053             }
01054             else
01055             {
01056                 // Leaf: let leaf::find handle this
01057                 const treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
01058 
01059                 if (subLeafPtr->bb().overlaps(box))
01060                 {
01061                     // Visit sub leaf.
01062                     changed |= subLeafPtr->findBox(shapes, box, elements);
01063                 }
01064             }
01065         }
01066     }
01067 
01068     return changed;
01069 }
01070 
01071 
01072 // look from <start> in current cube (given by this->bb()).
01073 template <class Type>
01074 const Foam::treeLeaf<Type>* Foam::treeNode<Type>::findLeafLine
01075 (
01076     const int level,
01077     const Type& shapes,
01078     point& start,
01079     const point& end
01080 ) const
01081 {
01082     if (debug & 2)
01083     {
01084         space(Pout, 2*level);
01085         Pout<< "findLeafLine : bb:" << this->bb() << "  mid:" << midpoint()
01086             << "  start:" << start << endl;
01087     }
01088 
01089     scalar typDim = this->bb().avgDim();
01090 
01091     const vector direction = end - start;
01092 
01093     // Loop on current level until start has been updated to be outside
01094     // of this->bb(). Note that max only four subcubes can be crossed so this is
01095     // check on whether there are any truncation error problems.
01096 
01097     label iter = 0;
01098 
01099     while (true)
01100     {
01101         if (!this->bb().contains(direction, start))
01102         {
01103             if (debug & 2)
01104             {
01105                 space(Pout, 2*level);
01106                 Pout<< "findLeafLine : Start not inside bb " << this->bb()
01107                     << ". Returning with start:" << start << "  subLeaf:"
01108                     << 0 << endl;
01109             }
01110             return NULL;
01111         }
01112 
01113         // Check if start and <end> equal
01114         if ((mag(start - end)/typDim) < SMALL)
01115         {
01116             if (debug & 2)
01117             {
01118                 space(Pout, 2*level);
01119                 Pout<< "findLeafLine : start equals end"
01120                     << ". Returning with start:" << start << "  subLeaf:"
01121                     << 0 << endl;
01122             }
01123             return NULL;
01124         }
01125 
01126         if (iter >= 4)
01127         {
01128             // Too many iterations. Is hanging. Handle outside of loop.
01129             break;
01130         }
01131 
01132         bool onEdge = false;
01133         label octant = this->bb().subOctant
01134         (
01135             midpoint(), direction, start, onEdge
01136         );
01137 
01138         // Try finding non-empty treeleaf in octant
01139         const treeLeaf<Type>* leafPtr = findLeafLineOctant
01140         (
01141             level,
01142             shapes,
01143             octant,
01144             direction,
01145             start,
01146             end
01147         );
01148 
01149         if (leafPtr)
01150         {
01151             // Found treeLeaf -> return
01152             if (debug & 2)
01153             {
01154                 space(Pout, 2*level);
01155                 Pout<< "findLeafLine : Found treeLeaf"
01156                         << ". Returning with start:" << start << "  subLeaf:"
01157                         << long(leafPtr) << endl;
01158             }
01159 
01160             return leafPtr;
01161         }
01162 
01163         iter++;
01164     }
01165 
01166     // Check if is hanging. Max 4 octants can be crossed by a straight line
01167     FatalErrorIn
01168     (
01169         "treeNode<Type>::findLeafLine"
01170         "(const label, octree<Type>&, point&,"
01171         " const point&)"
01172     )   << "Did not leave bb " << this->bb()
01173         << " after " << iter
01174         << " iterations of updating starting point."
01175         << "start:" << start << "  end:" << end
01176         << abort(FatalError);
01177 
01178     return NULL;
01179 }
01180 
01181 
01182 template <class Type>
01183 void Foam::treeNode<Type>::findLeaves
01184 (
01185     List<treeLeaf<Type>*>& leafArray,
01186     label& leafIndex
01187 ) const
01188 {
01189     // Go into all sub boxes
01190     for (label octant=0; octant<8; octant++)
01191     {
01192         if (subNodes()[octant])
01193         {
01194             if (isNode(octant))
01195             {
01196                 // Node: recurse into subnodes
01197                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
01198                 subNodePtr->findLeaves(leafArray, leafIndex);
01199             }
01200             else
01201             {
01202                 // Leaf: store
01203                 treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
01204                 leafArray[leafIndex++] = subLeafPtr;
01205             }
01206         }
01207     }
01208 }
01209 
01210 
01211 template <class Type>
01212 void Foam::treeNode<Type>::findLeaves
01213 (
01214     List<const treeLeaf<Type>*>& leafArray,
01215     label& leafIndex
01216 ) const
01217 {
01218     // Go into all sub boxes
01219     for (label octant=0; octant<8; octant++)
01220     {
01221         if (subNodes()[octant])
01222         {
01223             if (isNode(octant))
01224             {
01225                 // Node: recurse into subnodes
01226                 const treeNode<Type>* subNodePtr = getNodePtr(octant);
01227                 subNodePtr->findLeaves(leafArray, leafIndex);
01228             }
01229             else
01230             {
01231                 // Leaf: store
01232                 treeLeaf<Type>* subLeafPtr = getLeafPtr(octant);
01233                 leafArray[leafIndex++] = subLeafPtr;
01234             }
01235         }
01236     }
01237 }
01238 
01239 
01240 template <class Type>
01241 void Foam::treeNode<Type>::printNode
01242 (
01243     Ostream& os,
01244     const label level
01245 ) const
01246 {
01247     space(os, 2*level);
01248 
01249     os << "node:" << this->bb() << endl;
01250 
01251     for (label octant=0; octant<8; octant++)
01252     {
01253         label type = getVolType(octant);
01254 
01255         string typeString = octree<Type>::volType(type);
01256 
01257         if (!subNodes_[octant])
01258         {
01259             space(os, level);
01260             os << octant << ":" << typeString << " : null" << endl;
01261         }
01262         else if (isNode(octant))
01263         {
01264             space(os, level);
01265             os << octant << ":" << typeString << " : node" << endl;
01266             getNodePtr(octant)->printNode(os, level+1);
01267         }
01268         else
01269         {
01270             space(os, level);
01271             os << octant << ":" << typeString << " : leaf" << endl;
01272 
01273             treeLeaf<Type>* leafPtr = getLeafPtr(octant);
01274             leafPtr->printLeaf(os, level+1);
01275         }
01276     }
01277 }
01278 
01279 
01280 template <class Type>
01281 void Foam::treeNode<Type>::writeOBJ
01282 (
01283     Ostream& os,
01284     const label level,
01285     label& vertNo
01286 ) const
01287 {
01288     point midPoint(this->bb().midpoint());
01289 
01290     label midVertNo = vertNo;
01291     os << "v " << midPoint.x() << " " << midPoint.y() << " "
01292        << midPoint.z() << endl;
01293     vertNo++;
01294 
01295     for (label octant=0; octant<8; octant++)
01296     {
01297         if (subNodes_[octant])
01298         {
01299             if (isNode(octant))
01300             {
01301                 treeNode<Type>* nodePtr = getNodePtr(octant);
01302 
01303                 point subMidPoint(nodePtr->bb().midpoint());
01304                 os << "v " << subMidPoint.x() << " " << subMidPoint.y() << " "
01305                    << subMidPoint.z() << endl;
01306                 os << "l " << midVertNo + 1<< " " << vertNo + 1 << endl;
01307                 vertNo++;
01308 
01309                 nodePtr->writeOBJ(os, level+1, vertNo);
01310             }
01311             else
01312             {
01313                 treeLeaf<Type>* leafPtr = getLeafPtr(octant);
01314 
01315                 point subMidPoint(leafPtr->bb().midpoint());
01316                 os << "v " << subMidPoint.x() << " " << subMidPoint.y() << " "
01317                    << subMidPoint.z() << endl;
01318                 os << "l " << midVertNo + 1<< " " << vertNo + 1 << endl;
01319                 vertNo++;
01320 
01321                 //leafPtr->writeOBJ(os, level+1, vertNo);
01322             }
01323         }
01324     }
01325 }
01326 
01327 
01328 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
01329 
01330 template <class Type>
01331 Foam::Istream& Foam::operator>>(Istream& is, treeNode<Type>& oc)
01332 {
01333     for (label octant = 0; octant < 8; octant++)
01334     {
01335         oc.subNodes_[octant] = NULL;
01336     }
01337 
01338     is >> oc.bb();
01339 
01340     label nPtrs;
01341 
01342     // Read number of entries folllowing
01343     is >> nPtrs;
01344 
01345     is.readBegin("treeNode");
01346     for (label octant = 0; octant < nPtrs; octant++)
01347     {
01348         label index;
01349         is >> index;
01350 
01351         if (index >= treeNode<Type>::leafOffset)
01352         {
01353             // Leaf recognized by out of range index
01354             treeLeaf<Type>* leafPtr = new treeLeaf<Type>(is);
01355             oc.setLeafPtr(index - treeNode<Type>::leafOffset, leafPtr);
01356         }
01357         else
01358         {
01359             oc.setNodePtr(index, new treeNode<Type>(is));
01360         }
01361     }
01362 
01363     // Read end of treeNode list
01364     is.readEnd("treeNode");
01365 
01366     // Check state of Istream
01367     is.check("Istream& operator>>(Istream&, treeNode&)");
01368 
01369     return is;
01370 }
01371 
01372 
01373 template <class Type>
01374 Foam::Ostream& Foam::operator<<(Ostream& os, const treeNode<Type>& tn)
01375 {
01376     // Count valid subnodes:
01377     //   - treeNode
01378     //   - treeLeafs with non-zero cell list.
01379     label nPtrs = 0;
01380     for (label octant = 0; octant < 8; octant++)
01381     {
01382         if (tn.subNodes_[octant])
01383         {
01384             if (tn.isNode(octant) || tn.getLeafPtr(octant)->indices().size())
01385             {
01386                 nPtrs++;
01387             }
01388         }
01389     }
01390 
01391 
01392     // output subnodes as list of length nPtrs
01393     os << token::SPACE << tn.bb() << token::SPACE << nPtrs
01394        << token::SPACE << token::BEGIN_LIST;
01395 
01396     for (label octant = 0; octant < 8; octant++)
01397     {
01398         if (tn.subNodes_[octant])
01399         {
01400             if (tn.isNode(octant))
01401             {
01402                 const treeNode<Type>* subNodePtr = tn.getNodePtr(octant);
01403 
01404                 // Node: output index, value
01405                 os  << token::SPACE << octant << token::SPACE << *subNodePtr
01406                     << token::NL;
01407             }
01408             else if (tn.getLeafPtr(octant)->indices().size())
01409             {
01410                 // treeLeaf: mark by putting index invalid
01411                 const treeLeaf<Type>* subLeafPtr = tn.getLeafPtr(octant);
01412 
01413                 os  << token::SPACE << octant + treeNode<Type>::leafOffset
01414                     << token::SPACE << *subLeafPtr
01415                     << token::NL;
01416             }
01417         }
01418     }
01419 
01420     os  << token::SPACE << token::END_LIST;
01421 
01422     return os;
01423 }
01424 
01425 
01426 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd