1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_Tree : tree implementation
24 // File : SMESH_Tree.hxx
25 // Created : Tue Jan 16 16:00:00 2007
26 // Author : Nicolas Geimer & Aurélien Motteux (OCC)
29 #ifndef _SMESH_Tree_HXX_
30 #define _SMESH_Tree_HXX_
32 #include "SMESH_Utils.hxx"
34 //================================================================================
35 // Data limiting the tree height
36 struct SMESH_TreeLimit {
37 // MaxLevel of the Tree
39 // Minimal size of the Box
43 // maxLevel-> 8^8 = 16777216 terminal trees at most
44 // minSize -> box size not checked
45 SMESH_TreeLimit(int maxLevel=8, double minSize=0.):myMaxLevel(maxLevel),myMinBoxSize(minSize) {}
46 virtual ~SMESH_TreeLimit() {} // it can be inherited
49 //================================================================================
51 * \brief Base class for 2D and 3D trees
53 //================================================================================
55 template< class BND_BOX,
61 typedef BND_BOX box_type;
63 // Constructor. limit must be provided at tree root construction.
64 // limit will be deleted by SMESH_Tree
65 SMESH_Tree (SMESH_TreeLimit* limit=0);
68 virtual ~SMESH_Tree ();
70 // Compute the Tree. Must be called by constructor of inheriting class
73 // Tell if Tree is a leaf or not.
74 // An inheriting class can influence it via myIsLeaf protected field
78 int level() const { return myLevel; }
80 // Return Bounding Box of the Tree
81 const box_type* getBox() const { return myBox; }
83 // Return height of the tree, full or from this level to topest leaf
84 int getHeight(const bool full=true) const;
86 static int nbChildren() { return NB_CHILDREN; }
88 // Compute the biggest dimension of my box
89 virtual double maxSize() const = 0;
92 // Return box of the whole tree
93 virtual box_type* buildRootBox() = 0;
96 virtual SMESH_Tree* newChild() const = 0;
98 // Allocate a bndbox according to childIndex. childIndex is zero based
99 virtual box_type* newChildBox(int childIndex) const = 0;
101 // Change size of a box by a factor; each dimension changes independently of others
102 virtual void enlargeByFactor( box_type* box, double factor ) const = 0;
104 // Fill in data of the children
105 virtual void buildChildrenData() = 0;
110 SMESH_Tree** myChildren;
112 // Point the father, NULL for the level 0
113 SMESH_Tree* myFather;
115 // Tell us if the Tree is a leaf or not
119 const SMESH_TreeLimit* myLimit;
121 // Bounding box of a tree
127 // Build the children recursively
128 void buildChildren();
131 //===========================================================================
133 * Constructor. limit must be provided at tree root construction.
134 * limit will be deleted by SMESH_Tree.
136 //===========================================================================
138 template< class BND_BOX, int NB_CHILDREN>
139 SMESH_Tree<BND_BOX,NB_CHILDREN>::SMESH_Tree (SMESH_TreeLimit* limit):
147 //if ( !myLimit ) myLimit = new SMESH_TreeLimit();
150 //================================================================================
152 * \brief Compute the Tree
154 //================================================================================
156 template< class BND_BOX, int NB_CHILDREN>
157 void SMESH_Tree<BND_BOX,NB_CHILDREN>::compute()
161 if ( !myLimit ) myLimit = new SMESH_TreeLimit();
162 myBox = buildRootBox();
163 if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize )
170 //======================================
172 * \brief SMESH_Tree Destructor
174 //======================================
176 template< class BND_BOX, int NB_CHILDREN>
177 SMESH_Tree<BND_BOX,NB_CHILDREN>::~SMESH_Tree ()
183 for(int i = 0; i<NB_CHILDREN; i++)
184 delete myChildren[i];
197 //=================================================================
199 * \brief Build the children boxes and call buildChildrenData()
201 //=================================================================
203 template< class BND_BOX, int NB_CHILDREN>
204 void SMESH_Tree<BND_BOX,NB_CHILDREN>::buildChildren()
206 if ( isLeaf() ) return;
208 myChildren = new SMESH_Tree*[NB_CHILDREN];
210 // get the whole model size
211 // double rootSize = 0;
213 // SMESH_Tree* root = this;
214 // while ( root->myLevel > 0 )
215 // root = root->myFather;
216 // rootSize = root->maxSize();
218 for (int i = 0; i < NB_CHILDREN; i++)
220 // The child is of the same type than its father (For instance, a SMESH_OctreeNode)
221 // We allocate the memory we need for the child
222 myChildren[i] = newChild();
223 // and we assign to him its box.
224 myChildren[i]->myFather = this;
225 if (myChildren[i]->myLimit)
226 delete myChildren[i]->myLimit;
227 myChildren[i]->myLimit = myLimit;
228 myChildren[i]->myLevel = myLevel + 1;
229 myChildren[i]->myBox = newChildBox( i );
230 enlargeByFactor( myChildren[i]->myBox, 1. + 1e-10 );
231 if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize )
232 myChildren[i]->myIsLeaf = true;
235 // After building the NB_CHILDREN boxes, we put the data into the children.
238 //After we pass to the next level of the Tree
239 for (int i = 0; i<NB_CHILDREN; i++)
240 myChildren[i]->buildChildren();
243 //================================================================================
245 * \brief Tell if Tree is a leaf or not
246 * An inheriting class can influence it via myIsLeaf protected field
248 //================================================================================
250 template< class BND_BOX, int NB_CHILDREN>
251 bool SMESH_Tree<BND_BOX,NB_CHILDREN>::isLeaf() const
253 return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false );
256 //================================================================================
258 * \brief Return height of the tree, full or from this level to topest leaf
260 //================================================================================
262 template< class BND_BOX, int NB_CHILDREN>
263 int SMESH_Tree<BND_BOX,NB_CHILDREN>::getHeight(const bool full) const
265 if ( full && myFather )
266 return myFather->getHeight( true );
272 for (int i = 0; i<NB_CHILDREN; i++)
274 int h = myChildren[i]->getHeight( false );