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 #include <HYDROData_Tool.h>
33 #include <Message_ProgressSentry.hxx>
35 //================================================================================
37 //! Data limiting the tree height
38 struct HYDROData_TreeLimit
40 int myMaxLevel; //!<MaxLevel of the Tree
41 double myMinBoxSize; //!< Minimal size of the Box
43 /*! \brief Constructor with default values
45 * maxLevel-> 8^8 = 16777216 terminal trees at most
46 * minSize -> 0 : box size not checked
48 HYDROData_TreeLimit(int maxLevel = 8, double minSize = 0.) :
49 myMaxLevel(maxLevel), myMinBoxSize(minSize)
53 virtual ~HYDROData_TreeLimit()
55 } //!< virtual destructor can be inherited
58 //================================================================================
61 * \brief Base class for 2D and 3D trees
63 template<class BND_BOX, int NB_CHILDREN>
68 typedef BND_BOX box_type;
70 //! Constructor. limit must be provided at tree root construction.
71 //! limit will be deleted by HYDROData_Tree
72 HYDROData_Tree(HYDROData_TreeLimit* limit = 0);
75 virtual ~HYDROData_Tree();
77 //! Compute the Tree. Must be called by constructor of inheriting class
80 //! Tell if Tree is a leaf or not.
81 //! An inheriting class can influence it via myIsLeaf protected field
90 //! Return Bounding Box of the Tree
91 const box_type* getBox() const
96 //! Return height of the tree, full or from this level to top leaf
97 int getHeight(const bool full = true) const;
99 static int nbChildren()
104 //! Compute the biggest dimension of my box
105 virtual double maxSize() const = 0;
108 //! Return box of the whole tree
109 virtual box_type* buildRootBox() = 0;
112 virtual HYDROData_Tree* newChild() const = 0;
114 //! Allocate a bndbox according to childIndex. childIndex is zero based
115 virtual box_type* newChildBox(int childIndex) const = 0;
117 //! Fill in data of the children
118 virtual void buildChildrenData() = 0;
120 //! Build the children recursively
121 void buildChildren();
125 HYDROData_Tree** myChildren; //!< Array of children
127 HYDROData_Tree* myFather; //!< Point the father, NULL for the level 0
129 const HYDROData_TreeLimit* myLimit; //!< Tree limit
131 box_type* myBox; //!< Bounding box of a tree
133 int myLevel; //!< Level of the Tree
135 bool myIsLeaf; //!< Tell us if the Tree is a leaf or not
139 * Constructor. limit must be provided at tree root construction.
140 * limit will be deleted by HYDROData_Tree.
142 template<class BND_BOX, int NB_CHILDREN>
143 HYDROData_Tree<BND_BOX, NB_CHILDREN>::HYDROData_Tree(HYDROData_TreeLimit* limit) :
144 myChildren(0), myFather(0), myLimit(limit), myBox(0), myLevel(0), myIsLeaf(false)
146 //if ( !myLimit ) myLimit = new HYDROData_TreeLimit();
150 * \brief Compute the Tree
152 template<class BND_BOX, int NB_CHILDREN>
153 void HYDROData_Tree<BND_BOX, NB_CHILDREN>::compute()
158 myLimit = new HYDROData_TreeLimit();
159 myBox = buildRootBox();
160 if (myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize)
168 * \brief HYDROData_Tree Destructor
170 template<class BND_BOX, int NB_CHILDREN>
171 HYDROData_Tree<BND_BOX, NB_CHILDREN>::~HYDROData_Tree()
177 for (int i = 0; i < NB_CHILDREN; i++)
178 delete myChildren[i];
192 * \brief Build the children boxes and call buildChildrenData()
194 template<class BND_BOX, int NB_CHILDREN>
195 void HYDROData_Tree<BND_BOX, NB_CHILDREN>::buildChildren()
200 myChildren = new HYDROData_Tree*[NB_CHILDREN];
202 // get the whole model size
205 HYDROData_Tree* root = this;
206 while (root->myLevel > 0)
207 root = root->myFather;
208 rootSize = root->maxSize();
211 Handle(Message_ProgressIndicator) aZIProgress = myLevel == 0 ? HYDROData_Tool::GetZIProgress() : NULL;
212 Message_ProgressSentry aPSentry(aZIProgress, "QuadTree", 0, NB_CHILDREN + 1, 1);
214 for (int i = 0; i < NB_CHILDREN; i++)
216 // The child is of the same type than its father (For instance, a HYDROData_OctreeNode)
217 // We allocate the memory we need for the child
218 myChildren[i] = newChild();
219 // and we assign to him its box.
220 myChildren[i]->myFather = this;
221 if (myChildren[i]->myLimit)
222 delete myChildren[i]->myLimit;
223 myChildren[i]->myLimit = myLimit;
224 myChildren[i]->myLevel = myLevel + 1;
225 myChildren[i]->myBox = newChildBox(i);
226 myChildren[i]->myBox->Enlarge(rootSize * 1e-10);
227 if (myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize)
228 myChildren[i]->myIsLeaf = true;
231 // --- After building the NB_CHILDREN boxes, we put the data into the children.
234 if (aPSentry.More()) {
238 // --- After we pass to the next level of the Tree
239 for (int i = 0; i < NB_CHILDREN && aPSentry.More(); i++, aPSentry.Next()) {
240 myChildren[i]->buildChildren();
247 * \brief Tell if Tree is a leaf or not
248 * An inheriting class can influence it via myIsLeaf protected field
250 template<class BND_BOX, int NB_CHILDREN>
251 bool HYDROData_Tree<BND_BOX, NB_CHILDREN>::isLeaf() const
253 return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false);
257 * \brief Return height of the tree, full or from this level to top leaf
259 template<class BND_BOX, int NB_CHILDREN>
260 int HYDROData_Tree<BND_BOX, NB_CHILDREN>::getHeight(const bool full) const
262 if (full && myFather)
263 return myFather->getHeight(true);
269 for (int i = 0; i < NB_CHILDREN; i++)
271 int h = myChildren[i]->getHeight(false);