1 // Copyright (C) 2007-2014 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 // HYDRO HYDROData_Tree : tree implementation (from SMESH module)
24 // File : HYDROData_Tree.hxx
25 // Created : Tue Jan 16 16:00:00 2007
26 // Author : Nicolas Geimer & Aurélien Motteux (OCC)
29 #ifndef _HYDROData_Tree_HXX_
30 #define _HYDROData_Tree_HXX_
32 //================================================================================
34 //! Data limiting the tree height
35 struct HYDROData_TreeLimit
37 int myMaxLevel; //!<MaxLevel of the Tree
38 double myMinBoxSize; //!< Minimal size of the Box
40 /*! \brief Constructor with default values
42 * maxLevel-> 8^8 = 16777216 terminal trees at most
43 * minSize -> 0 : box size not checked
45 HYDROData_TreeLimit(int maxLevel = 8, double minSize = 0.) :
46 myMaxLevel(maxLevel), myMinBoxSize(minSize)
50 virtual ~HYDROData_TreeLimit()
52 } //!< virtual destructor can be inherited
55 //================================================================================
58 * \brief Base class for 2D and 3D trees
60 template<class BND_BOX, int NB_CHILDREN>
65 typedef BND_BOX box_type;
67 //! Constructor. limit must be provided at tree root construction.
68 //! limit will be deleted by HYDROData_Tree
69 HYDROData_Tree(HYDROData_TreeLimit* limit = 0);
72 virtual ~HYDROData_Tree();
74 //! Compute the Tree. Must be called by constructor of inheriting class
77 //! Tell if Tree is a leaf or not.
78 //! An inheriting class can influence it via myIsLeaf protected field
87 //! Return Bounding Box of the Tree
88 const box_type* getBox() const
93 //! Return height of the tree, full or from this level to top leaf
94 int getHeight(const bool full = true) const;
96 static int nbChildren()
101 //! Compute the biggest dimension of my box
102 virtual double maxSize() const = 0;
105 //! Return box of the whole tree
106 virtual box_type* buildRootBox() = 0;
109 virtual HYDROData_Tree* newChild() const = 0;
111 //! Allocate a bndbox according to childIndex. childIndex is zero based
112 virtual box_type* newChildBox(int childIndex) const = 0;
114 //! Fill in data of the children
115 virtual void buildChildrenData() = 0;
117 //! Build the children recursively
118 void buildChildren();
122 HYDROData_Tree** myChildren; //!< Array of children
124 HYDROData_Tree* myFather; //!< Point the father, NULL for the level 0
126 const HYDROData_TreeLimit* myLimit; //!< Tree limit
128 box_type* myBox; //!< Bounding box of a tree
130 int myLevel; //!< Level of the Tree
132 bool myIsLeaf; //!< Tell us if the Tree is a leaf or not
136 * Constructor. limit must be provided at tree root construction.
137 * limit will be deleted by HYDROData_Tree.
139 template<class BND_BOX, int NB_CHILDREN>
140 HYDROData_Tree<BND_BOX, NB_CHILDREN>::HYDROData_Tree(HYDROData_TreeLimit* limit) :
141 myChildren(0), myFather(0), myLimit(limit), myBox(0), myLevel(0), myIsLeaf(false)
143 //if ( !myLimit ) myLimit = new HYDROData_TreeLimit();
147 * \brief Compute the Tree
149 template<class BND_BOX, int NB_CHILDREN>
150 void HYDROData_Tree<BND_BOX, NB_CHILDREN>::compute()
155 myLimit = new HYDROData_TreeLimit();
156 myBox = buildRootBox();
157 if (myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize)
165 * \brief HYDROData_Tree Destructor
167 template<class BND_BOX, int NB_CHILDREN>
168 HYDROData_Tree<BND_BOX, NB_CHILDREN>::~HYDROData_Tree()
174 for (int i = 0; i < NB_CHILDREN; i++)
175 delete myChildren[i];
189 * \brief Build the children boxes and call buildChildrenData()
191 template<class BND_BOX, int NB_CHILDREN>
192 void HYDROData_Tree<BND_BOX, NB_CHILDREN>::buildChildren()
197 myChildren = new HYDROData_Tree*[NB_CHILDREN];
199 // get the whole model size
202 HYDROData_Tree* root = this;
203 while (root->myLevel > 0)
204 root = root->myFather;
205 rootSize = root->maxSize();
207 for (int i = 0; i < NB_CHILDREN; i++)
209 // The child is of the same type than its father (For instance, a HYDROData_OctreeNode)
210 // We allocate the memory we need for the child
211 myChildren[i] = newChild();
212 // and we assign to him its box.
213 myChildren[i]->myFather = this;
214 if (myChildren[i]->myLimit)
215 delete myChildren[i]->myLimit;
216 myChildren[i]->myLimit = myLimit;
217 myChildren[i]->myLevel = myLevel + 1;
218 myChildren[i]->myBox = newChildBox(i);
219 myChildren[i]->myBox->Enlarge(rootSize * 1e-10);
220 if (myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize)
221 myChildren[i]->myIsLeaf = true;
224 // --- After building the NB_CHILDREN boxes, we put the data into the children.
227 // --- After we pass to the next level of the Tree
228 for (int i = 0; i < NB_CHILDREN; i++)
229 myChildren[i]->buildChildren();
233 * \brief Tell if Tree is a leaf or not
234 * An inheriting class can influence it via myIsLeaf protected field
236 template<class BND_BOX, int NB_CHILDREN>
237 bool HYDROData_Tree<BND_BOX, NB_CHILDREN>::isLeaf() const
239 return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false);
243 * \brief Return height of the tree, full or from this level to top leaf
245 template<class BND_BOX, int NB_CHILDREN>
246 int HYDROData_Tree<BND_BOX, NB_CHILDREN>::getHeight(const bool full) const
248 if (full && myFather)
249 return myFather->getHeight(true);
255 for (int i = 0; i < NB_CHILDREN; i++)
257 int h = myChildren[i]->getHeight(false);