diff --git a/src/SMESHUtils/SMESH_Tree.hxx b/src/SMESHUtils/SMESH_Tree.hxx index bf099d305..8f6d66f1a 100644 --- a/src/SMESHUtils/SMESH_Tree.hxx +++ b/src/SMESHUtils/SMESH_Tree.hxx @@ -19,257 +19,257 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // - -// SMESH SMESH_Tree : tree implementation -// File : SMESH_Tree.hxx -// Created : Tue Jan 16 16:00:00 2007 -// Author : Nicolas Geimer & Aurélien Motteux (OCC) -// Module : SMESH -// -#ifndef _SMESH_Tree_HXX_ -#define _SMESH_Tree_HXX_ - -#include "SMESH_Utils.hxx" - -//================================================================================ -// Data limiting the tree height -struct SMESH_TreeLimit { - // MaxLevel of the Tree - int myMaxLevel; - // Minimal size of the Box - double myMinBoxSize; - - // Default: - // maxLevel-> 8^8 = 16777216 terminal trees at most - // minSize -> box size not checked - SMESH_TreeLimit(int maxLevel=8, double minSize=0.):myMaxLevel(maxLevel),myMinBoxSize(minSize) {} - virtual ~SMESH_TreeLimit() {} // it can be inherited -}; - -//================================================================================ -/*! - * \brief Base class for 2D and 3D trees - */ -//================================================================================ - -template< class BND_BOX, - int NB_CHILDREN> -class SMESH_Tree -{ - public: - - typedef BND_BOX box_type; - - // Constructor. limit must be provided at tree root construction. - // limit will be deleted by SMESH_Tree - SMESH_Tree (SMESH_TreeLimit* limit=0); - - // Destructor - virtual ~SMESH_Tree (); - - // Compute the Tree. Must be called by constructor of inheriting class - void compute(); - - // Tell if Tree is a leaf or not. - // An inheriting class can influence it via myIsLeaf protected field - bool isLeaf() const; - - // Return its level - int level() const { return myLevel; } - - // Return Bounding Box of the Tree - const box_type* getBox() const { return myBox; } - - // Return height of the tree, full or from this level to topest leaf - int getHeight(const bool full=true) const; - - static int nbChildren() { return NB_CHILDREN; } - - // Compute the bigger dimension of my box - virtual double maxSize() const = 0; - -protected: - // Return box of the whole tree - virtual box_type* buildRootBox() = 0; - - // Allocate a child - virtual SMESH_Tree* newChild() const = 0; - - // Allocate a bndbox according to childIndex. childIndex is zero based - virtual box_type* newChildBox(int childIndex) const = 0; - - // Fill in data of the children - virtual void buildChildrenData() = 0; - - // members - - // Array of children - SMESH_Tree** myChildren; - - // Point the father, NULL for the level 0 - SMESH_Tree* myFather; - - // Tell us if the Tree is a leaf or not - bool myIsLeaf; - - // Tree limit - const SMESH_TreeLimit* myLimit; - -private: - // Build the children recursively - void buildChildren(); - - // Level of the Tree - int myLevel; - - box_type* myBox; -}; - -//=========================================================================== -/*! - * Constructor. limit must be provided at tree root construction. - * limit will be deleted by SMESH_Tree. - */ -//=========================================================================== - -template< class BND_BOX, int NB_CHILDREN> -SMESH_Tree::SMESH_Tree (SMESH_TreeLimit* limit): - myChildren(0), - myFather(0), - myIsLeaf( false ), - myLimit( limit ), - myLevel(0), - myBox(0) -{ - if ( !myLimit ) myLimit = new SMESH_TreeLimit(); -} - -//================================================================================ -/*! - * \brief Compute the Tree - */ -//================================================================================ - -template< class BND_BOX, int NB_CHILDREN> -void SMESH_Tree::compute() -{ - if ( myLevel==0 ) - { - myBox = buildRootBox(); - if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize ) - myIsLeaf = true; - else - buildChildren(); - } -} - -//====================================== -/*! - * \brief SMESH_Tree Destructor - */ -//====================================== - -template< class BND_BOX, int NB_CHILDREN> -SMESH_Tree::~SMESH_Tree () -{ - if ( myChildren ) - { - if ( !isLeaf() ) - { - for(int i = 0; i -void SMESH_Tree::buildChildren() -{ - if ( isLeaf() ) return; - - myChildren = new SMESH_Tree*[NB_CHILDREN]; - - // get the whole model size - double rootSize = 0; - { - SMESH_Tree* root = this; - while ( root->myLevel > 0 ) - root = root->myFather; - rootSize = root->maxSize(); - } - for (int i = 0; i < NB_CHILDREN; i++) - { - // The child is of the same type than its father (For instance, a SMESH_OctreeNode) - // We allocate the memory we need for the child - myChildren[i] = newChild(); - // and we assign to him its box. - myChildren[i]->myFather = this; - myChildren[i]->myLimit = myLimit; - myChildren[i]->myLevel = myLevel + 1; - myChildren[i]->myBox = newChildBox( i ); - myChildren[i]->myBox->Enlarge( rootSize * 1e-10 ); - if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize ) - myChildren[i]->myIsLeaf = true; - } - - // After building the NB_CHILDREN boxes, we put the data into the children. - buildChildrenData(); - - //After we pass to the next level of the Tree - for (int i = 0; ibuildChildren(); -} - -//================================================================================ -/*! - * \brief Tell if Tree is a leaf or not - * An inheriting class can influence it via myIsLeaf protected field - */ -//================================================================================ - -template< class BND_BOX, int NB_CHILDREN> -bool SMESH_Tree::isLeaf() const -{ - return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false ); -} - -//================================================================================ -/*! - * \brief Return height of the tree, full or from this level to topest leaf - */ -//================================================================================ - -template< class BND_BOX, int NB_CHILDREN> -int SMESH_Tree::getHeight(const bool full) const -{ - if ( full && myFather ) - return myFather->getHeight( true ); - - if ( isLeaf() ) - return 1; - - int heigth = 0; - for (int i = 0; igetHeight( false ); - if ( h > heigth ) - heigth = h; - } - return heigth + 1; -} - -#endif + +// SMESH SMESH_Tree : tree implementation +// File : SMESH_Tree.hxx +// Created : Tue Jan 16 16:00:00 2007 +// Author : Nicolas Geimer & Aurélien Motteux (OCC) +// Module : SMESH +// +#ifndef _SMESH_Tree_HXX_ +#define _SMESH_Tree_HXX_ + +#include "SMESH_Utils.hxx" + +//================================================================================ +// Data limiting the tree height +struct SMESH_TreeLimit { + // MaxLevel of the Tree + int myMaxLevel; + // Minimal size of the Box + double myMinBoxSize; + + // Default: + // maxLevel-> 8^8 = 16777216 terminal trees at most + // minSize -> box size not checked + SMESH_TreeLimit(int maxLevel=8, double minSize=0.):myMaxLevel(maxLevel),myMinBoxSize(minSize) {} + virtual ~SMESH_TreeLimit() {} // it can be inherited +}; + +//================================================================================ +/*! + * \brief Base class for 2D and 3D trees + */ +//================================================================================ + +template< class BND_BOX, + int NB_CHILDREN> +class SMESH_Tree +{ + public: + + typedef BND_BOX box_type; + + // Constructor. limit must be provided at tree root construction. + // limit will be deleted by SMESH_Tree + SMESH_Tree (SMESH_TreeLimit* limit=0); + + // Destructor + virtual ~SMESH_Tree (); + + // Compute the Tree. Must be called by constructor of inheriting class + void compute(); + + // Tell if Tree is a leaf or not. + // An inheriting class can influence it via myIsLeaf protected field + bool isLeaf() const; + + // Return its level + int level() const { return myLevel; } + + // Return Bounding Box of the Tree + const box_type* getBox() const { return myBox; } + + // Return height of the tree, full or from this level to topest leaf + int getHeight(const bool full=true) const; + + static int nbChildren() { return NB_CHILDREN; } + + // Compute the bigger dimension of my box + virtual double maxSize() const = 0; + +protected: + // Return box of the whole tree + virtual box_type* buildRootBox() = 0; + + // Allocate a child + virtual SMESH_Tree* newChild() const = 0; + + // Allocate a bndbox according to childIndex. childIndex is zero based + virtual box_type* newChildBox(int childIndex) const = 0; + + // Fill in data of the children + virtual void buildChildrenData() = 0; + + // members + + // Array of children + SMESH_Tree** myChildren; + + // Point the father, NULL for the level 0 + SMESH_Tree* myFather; + + // Tell us if the Tree is a leaf or not + bool myIsLeaf; + + // Tree limit + const SMESH_TreeLimit* myLimit; + +private: + // Build the children recursively + void buildChildren(); + + // Level of the Tree + int myLevel; + + box_type* myBox; +}; + +//=========================================================================== +/*! + * Constructor. limit must be provided at tree root construction. + * limit will be deleted by SMESH_Tree. + */ +//=========================================================================== + +template< class BND_BOX, int NB_CHILDREN> +SMESH_Tree::SMESH_Tree (SMESH_TreeLimit* limit): + myChildren(0), + myFather(0), + myIsLeaf( false ), + myLimit( limit ), + myLevel(0), + myBox(0) +{ + if ( !myLimit ) myLimit = new SMESH_TreeLimit(); +} + +//================================================================================ +/*! + * \brief Compute the Tree + */ +//================================================================================ + +template< class BND_BOX, int NB_CHILDREN> +void SMESH_Tree::compute() +{ + if ( myLevel==0 ) + { + myBox = buildRootBox(); + if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize ) + myIsLeaf = true; + else + buildChildren(); + } +} + +//====================================== +/*! + * \brief SMESH_Tree Destructor + */ +//====================================== + +template< class BND_BOX, int NB_CHILDREN> +SMESH_Tree::~SMESH_Tree () +{ + if ( myChildren ) + { + if ( !isLeaf() ) + { + for(int i = 0; i +void SMESH_Tree::buildChildren() +{ + if ( isLeaf() ) return; + + myChildren = new SMESH_Tree*[NB_CHILDREN]; + + // get the whole model size + double rootSize = 0; + { + SMESH_Tree* root = this; + while ( root->myLevel > 0 ) + root = root->myFather; + rootSize = root->maxSize(); + } + for (int i = 0; i < NB_CHILDREN; i++) + { + // The child is of the same type than its father (For instance, a SMESH_OctreeNode) + // We allocate the memory we need for the child + myChildren[i] = newChild(); + // and we assign to him its box. + myChildren[i]->myFather = this; + myChildren[i]->myLimit = myLimit; + myChildren[i]->myLevel = myLevel + 1; + myChildren[i]->myBox = newChildBox( i ); + myChildren[i]->myBox->Enlarge( rootSize * 1e-10 ); + if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize ) + myChildren[i]->myIsLeaf = true; + } + + // After building the NB_CHILDREN boxes, we put the data into the children. + buildChildrenData(); + + //After we pass to the next level of the Tree + for (int i = 0; ibuildChildren(); +} + +//================================================================================ +/*! + * \brief Tell if Tree is a leaf or not + * An inheriting class can influence it via myIsLeaf protected field + */ +//================================================================================ + +template< class BND_BOX, int NB_CHILDREN> +bool SMESH_Tree::isLeaf() const +{ + return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false ); +} + +//================================================================================ +/*! + * \brief Return height of the tree, full or from this level to topest leaf + */ +//================================================================================ + +template< class BND_BOX, int NB_CHILDREN> +int SMESH_Tree::getHeight(const bool full) const +{ + if ( full && myFather ) + return myFather->getHeight( true ); + + if ( isLeaf() ) + return 1; + + int heigth = 0; + for (int i = 0; igetHeight( false ); + if ( h > heigth ) + heigth = h; + } + return heigth + 1; +} + +#endif