|
|
|
octree.HGo 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 Class 00026 Foam::octree 00027 00028 Description 00029 Octree, templated on type of shapes it refers to. 00030 00031 Uses the octreeData class, which is a list of 1D, 2D or 3D shapes. 00032 Various implementations of octreeData which know about cell&meshes, 00033 patches&meshes and points. 00034 00035 The octree can be used to 00036 - find the "nearest" shape to a point 00037 - find the shape which contains a point (only sensible for 3D shapes) 00038 - find intersections of line with shapes 00039 00040 The tree consists of 00041 - treeNode : holds sub-treeNodes or treeLeaves 00042 - treeLeaf : is the one that actually holds data 00043 00044 The data is stored purely as a list of indices into octreeData. 00045 00046 The construction on the depth of the tree is: 00047 - one can specify a minimum depth 00048 (though the tree will never be refined if all leaves contain <=1 00049 shapes) 00050 - after the minimum depth two statistics are used to decide further 00051 refinement: 00052 - average number of entries per leaf (leafRatio). Since inside a 00053 leaf most algorithms are n or n^2 this value has to be small. 00054 - average number of leaves a shape is in. Because of bounding boxes, 00055 a single shape can be in multiple leaves. If the bbs are large 00056 compared to the leaf size this multiplicity can become extremely 00057 large and will become larger with more levels. 00058 00059 Note: the octree gets constructed with an overall bounding box. If your 00060 mesh is regular it is a good idea to make this overall bb a bit wider than 00061 the bb of the shapes itself. Otherwise lots of shapes end up exactly on the 00062 edge of a bb and hence go into more than one subcube. 00063 00064 E.g. octree of face centres of lid driven cavity. 00065 00066 -# bb exact -> total 1457 leaves (every point in 7.1 leaves) 00067 -# bb.max incremented by 1% -> total 80 leaves 00068 (every point in exactly 1 leaf) 00069 00070 @par Ideas for parallel implementation: 00071 00072 The data inserted into the octree (the 00073 'octreeData') has to be local to the processor. The data to be looked 00074 for (usually a sample point) can be global to the domain. 00075 The algorithm: 00076 - search for all my points 00077 - collect results which have to do with a processor patch 00078 - global sum all these. If 0 exit. 00079 - exchange data on all processor patches 00080 - start again 00081 00082 So data transfers one processor per iteration. One could think of having 00083 an extra search mechanism one level above which does an initial search 00084 knowing the average shape of a processor distribution (square/cube for 00085 most decomposition methods) and can assign sample points to the (almost) 00086 correct processor at once. 00087 00088 SourceFiles 00089 octree.C 00090 00091 \*---------------------------------------------------------------------------*/ 00092 00093 #ifndef octree_H 00094 #define octree_H 00095 00096 #include "treeBoundBoxList.H" 00097 #include "treeLeaf.H" 00098 #include "pointIndexHit.H" 00099 #include "linePointRef.H" 00100 00101 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00102 00103 namespace Foam 00104 { 00105 00106 // Forward declaration of classes 00107 template<class Type> class treeNode; 00108 00109 // Forward declaration of friend functions and operators 00110 00111 template<class Type> 00112 Ostream& operator<<(Ostream&, const octree<Type>&); 00113 00114 00115 /*---------------------------------------------------------------------------*\ 00116 Class octreeName Declaration 00117 \*---------------------------------------------------------------------------*/ 00118 00119 TemplateName(octree); 00120 00121 00122 00123 /*---------------------------------------------------------------------------*\ 00124 Class octree Declaration 00125 \*---------------------------------------------------------------------------*/ 00126 00127 template <class Type> 00128 class octree 00129 : 00130 public octreeName 00131 { 00132 // Private data 00133 00134 //- Top treeNode. Modified by lower levels. 00135 treeNode<Type>* topNode_; 00136 00137 //- all shapes 00138 const Type shapes_; 00139 00140 //- Overall bb of octree 00141 const treeBoundBox octreeBb_; 00142 00143 //- Refinement crit: size of leaves. Average number of entries per 00144 // leaf. Should be fine enough for efficient searching at lowest level. 00145 const scalar maxLeafRatio_; 00146 00147 //- Refinement crit: multiplicity of entries (so in how many leaves 00148 // each shape is mentioned) 00149 const scalar maxShapeRatio_; 00150 00151 //- Refinement crit: min no. of levels 00152 const label minNLevels_; 00153 00154 //- Actual depth 00155 label deepestLevel_; 00156 00157 //- Total number of (references to) shapes in octree 00158 // (shapes can be stored in more than one treeLeaf) 00159 label nEntries_; 00160 00161 //- Total number of treeNodes 00162 label nNodes_; 00163 00164 //- Total number of treeLeaves 00165 label nLeaves_; 00166 00167 00168 // Static data members 00169 //- Refinement crit: max number of level 00170 static const label maxNLevels = 20; 00171 00172 00173 00174 // Private Member Functions 00175 00176 public: 00177 00178 // Data types 00179 00180 //- volume types 00181 enum volumeType 00182 { 00183 UNKNOWN, 00184 MIXED, 00185 INSIDE, 00186 OUTSIDE 00187 }; 00188 00189 00190 // Static data members 00191 00192 //- for debugging:return printable representation of volumeType 00193 static string volType(const label); 00194 00195 //- Code the vector with respect to the geometry. geomNormal guaranteed 00196 // to point outside. 00197 static label getVolType 00198 ( 00199 const vector& geomNormal, 00200 const vector& vec 00201 ); 00202 00203 // Constructors 00204 00205 //- Construct from components 00206 octree 00207 ( 00208 const treeBoundBox& octreeBb, 00209 const Type& shapes, 00210 const label minNLevels, // minimum number of levels 00211 const scalar maxLeafRatio, // max avg. size of leaves 00212 const scalar maxShapeRatio // max avg. duplicity. 00213 ); 00214 00215 00216 // Destructor 00217 00218 ~octree(); 00219 00220 00221 // Member Functions 00222 00223 // Access 00224 00225 const Type& shapes() const 00226 { 00227 return shapes_; 00228 } 00229 00230 const treeBoundBox& octreeBb() const 00231 { 00232 return octreeBb_; 00233 } 00234 00235 scalar maxShapeRatio() const 00236 { 00237 return maxShapeRatio_; 00238 } 00239 00240 scalar maxLeafRatio() const 00241 { 00242 return maxLeafRatio_; 00243 } 00244 00245 label minNLevels() const 00246 { 00247 return minNLevels_; 00248 } 00249 00250 // After construction: octree statistics 00251 00252 treeNode<Type>* topNode() const 00253 { 00254 return topNode_; 00255 } 00256 00257 label deepestLevel() const 00258 { 00259 return deepestLevel_; 00260 } 00261 00262 label nEntries() const 00263 { 00264 return nEntries_; 00265 } 00266 00267 label nNodes() const 00268 { 00269 return nNodes_; 00270 } 00271 00272 label nLeaves() const 00273 { 00274 return nLeaves_; 00275 } 00276 00277 void setEntries(const label n) 00278 { 00279 nEntries_ = n; 00280 } 00281 00282 void setNodes(const label n) 00283 { 00284 nNodes_ = n; 00285 } 00286 00287 void setLeaves(const label n) 00288 { 00289 nLeaves_ = n; 00290 } 00291 00292 // Queries 00293 00294 //- Returns type of sample with respect to nearest shape. 00295 // Meaning of type is determined by shapes.getSampleType(). 00296 // Normally based on outwards pointing normal. For e.g. 00297 // octreeDataFace returns one of 00298 // inside : on other side of normal on nearest face 00299 // outside : on same side as normal on nearest face 00300 // unknown : not above nearest face; surface probably not 00301 // closed. 00302 // mixed : should never happen 00303 label getSampleType(const point& sample) const; 00304 00305 //- Find shape containing point in tree 00306 // Returns -1 if not in found. Uses Type::contains. 00307 label find(const point& sample) const; 00308 00309 //- Calculate tightest fitting bounding box. Uses 00310 // Type::findTightest. 00311 bool findTightest 00312 ( 00313 const point& sample, 00314 treeBoundBox& tightest 00315 ) const; 00316 00317 //- Find nearest shape. Returns index of shape or -1 if not found. 00318 // tightestDist is both input and output. Uses Type::calcNearest. 00319 label findNearest 00320 ( 00321 const point& sample, 00322 treeBoundBox& tightest, 00323 scalar& tightestDist 00324 ) const; 00325 00326 //- Find nearest to line. Returns -1 or index of shape and 00327 // sets: 00328 // - tightest (is both input and output). 00329 // - linePoint : point on line (-GREAT,-GREAT,-GREAT if not found) 00330 // - shapePoint : point on shape (GREAT, GREAT, GREAT if not found) 00331 // Uses Type::calcNearest. 00332 label findNearest 00333 ( 00334 const linePointRef& ln, 00335 treeBoundBox& tightest, 00336 point& linePoint, 00337 point& shapePoint 00338 ) const; 00339 00340 //- Find (in no particular order) indices of all shapes inside or 00341 // overlapping bounding box (i.e. all shapes not outside box) 00342 labelList findBox(const boundBox& bb) const; 00343 00344 //- Find intersected shape along line. pointIndexHit contains index 00345 // of nearest shape intersected and the intersection point. Uses 00346 // findLeafLine. 00347 pointIndexHit findLine(const point& start, const point& end) const; 00348 00349 //- Like above but just tests whether line hits anything. So 00350 // returns first intersection found, not nessecarily nearest. 00351 pointIndexHit findLineAny(const point& start, const point& end) 00352 const; 00353 00354 //- Find leaf along line. Set leafIntPoint to leave point of 00355 // treeLeaf. 00356 const treeLeaf<Type>* findLeafLine 00357 ( 00358 const point& start, 00359 const point& end, 00360 point& leafIntPoint 00361 ) const; 00362 00363 00364 // Write 00365 00366 //- Dump graphical representation in .obj format 00367 void writeOBJ(Ostream& os, label& vertNo) const; 00368 00369 //- Print some stats on octree 00370 void printStats(Ostream& os) const; 00371 00372 00373 // STL iterator 00374 00375 class iterator; 00376 friend class iterator; 00377 00378 //- An STL iterator for octree 00379 class iterator 00380 { 00381 // Private data 00382 00383 //- Reference to the octree this is an iterator for 00384 octree& octree_; 00385 00386 //- List of pointers to treeLeaves 00387 List<treeLeaf<Type>*> leaves_; 00388 00389 //- Current treeLeaf index 00390 label curLeaf_; 00391 00392 public: 00393 00394 //- Construct for a given octree 00395 iterator(octree&); 00396 00397 //- Contruct for a given octree, at a certain position 00398 iterator(octree& oc, const label index); 00399 00400 00401 // Member operators 00402 00403 void operator=(const iterator&); 00404 00405 bool operator==(const iterator&) const; 00406 bool operator!=(const iterator&) const; 00407 00408 treeLeaf<Type>& operator*(); 00409 00410 iterator& operator++(); 00411 iterator operator++(int); 00412 }; 00413 00414 //- iterator set to the begining of the octree 00415 iterator begin(); 00416 00417 //- iterator set to beyond the end of the octree 00418 const iterator& end(); 00419 00420 00421 // STL const_iterator 00422 00423 class const_iterator; 00424 friend class const_iterator; 00425 00426 //- An STL const_iterator for octree 00427 class const_iterator 00428 { 00429 // Private data 00430 00431 //- Reference to the list this is an iterator for 00432 const octree& octree_; 00433 00434 //- List of treeLeafs 00435 List<const treeLeaf<Type>*> leaves_; 00436 00437 //- Current treeLeaf index 00438 label curLeaf_; 00439 00440 public: 00441 00442 //- Construct for a given octree 00443 const_iterator(const octree&); 00444 00445 //- Construct for a given octree and index 00446 const_iterator(const octree&, label); 00447 00448 // Member operators 00449 00450 void operator=(const const_iterator&); 00451 00452 bool operator==(const const_iterator&) const; 00453 bool operator!=(const const_iterator&) const; 00454 00455 const treeLeaf<Type>& operator*(); 00456 00457 const_iterator& operator++(); 00458 const_iterator operator++(int); 00459 }; 00460 00461 00462 //- const_iterator set to the begining of the octree 00463 inline const_iterator begin() const; 00464 inline const_iterator cbegin() const; 00465 00466 //- const_iterator set to beyond the end of the octree 00467 inline const const_iterator& end() const; 00468 inline const const_iterator& cend() const; 00469 00470 00471 // IOstream Operators 00472 00473 friend Ostream& operator<< <Type>(Ostream&, const octree<Type>&); 00474 00475 00476 private: 00477 00478 //- iterator returned by end() 00479 iterator endIter_; 00480 00481 //- const_iterator returned by end() 00482 const_iterator endConstIter_; 00483 }; 00484 00485 00486 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00487 00488 } // End namespace Foam 00489 00490 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00491 00492 #ifdef NoRepository 00493 # include "octree.C" 00494 #endif 00495 00496 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00497 00498 #endif 00499 00500 // ************************************************************************* // |