Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESHUtils / SMESH_Octree.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH  SMESH_Octree : global Octree implementation
24 //  File      : SMESH_Octree.cxx
25 //  Created   : Tue Jan 16 16:00:00 2007
26 //  Author    : Nicolas Geimer & AurĂ©lien Motteux(OCC)
27 //  Module    : SMESH
28 //
29 #include "SMESH_Octree.hxx"
30
31 //===========================================================================
32 /*!
33  * Constructor. limit must be provided at tree root construction.
34  * limit will be deleted by SMESH_Octree.
35  */
36 //===========================================================================
37
38 SMESH_Octree::SMESH_Octree (SMESH_Octree::Limit* limit):
39   myChildren(NULL),
40   myFather(NULL),
41   myIsLeaf( false ),
42   myLimit( limit ),
43   myLevel(0),
44   myBox(NULL)
45 {
46 }
47
48 //================================================================================
49 /*!
50  * \brief Compute the Octree
51  */
52 //================================================================================
53
54 void SMESH_Octree::compute()
55 {
56   if ( myLevel==0 )
57   {
58     myBox = buildRootBox();
59     if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize )
60       myIsLeaf = true;
61     else
62       buildChildren();
63   }
64 }
65
66 //======================================
67 /*!
68  * \brief SMESH_Octree Destructor
69  */
70 //======================================
71
72 SMESH_Octree::~SMESH_Octree ()
73 {
74   if(myChildren != NULL)
75   {
76     if(!isLeaf())
77     {
78       for(int i = 0; i<8; i++)
79         delete myChildren[i];
80       delete[] myChildren;
81       myChildren = 0;
82     }
83   }
84   if ( myBox )
85     delete myBox;
86   myBox = 0;
87   if ( level() == 0 )
88     delete myLimit;
89   myLimit = 0;
90 }
91
92 //=================================================================
93 /*!
94  * \brief Build the 8 children boxes and call buildChildrenData()
95  */
96 //=================================================================
97
98 void SMESH_Octree::buildChildren()
99 {
100   if ( isLeaf() ) return;
101
102   myChildren = new SMESH_Octree*[8];
103
104   gp_XYZ min = myBox->CornerMin();
105   gp_XYZ max = myBox->CornerMax();
106   gp_XYZ HSize = (max - min)/2.;
107   gp_XYZ mid = min + HSize;
108   gp_XYZ childHsize = HSize/2.;
109
110   // get the whole model size
111   double rootSize = 0;
112   {
113     SMESH_Octree* root = this;
114     while ( root->myLevel > 0 )
115       root = root->myFather;
116     rootSize = root->maxSize();
117   }
118   Standard_Real XminChild, YminChild, ZminChild;
119   gp_XYZ minChild;
120   for (int i = 0; i < 8; i++)
121   {
122     // We build the eight boxes, we need 2 points to do that:
123     // Min and Mid
124     // In binary, we can write i from 0 to 7
125     // For instance :
126     // 5 is 101, it corresponds here in coordinates to ZYX
127     // If coordinate is 0 in Y-> box from Ymin to Ymid
128     // If coordinate is 1 in Y-> box from Ymid to Ymax
129     // Same scheme for X and Z
130     // I need the minChild to build the Bnd_B3d box.
131
132     XminChild = (i%2==0)?min.X():mid.X();
133     YminChild = ((i%4)/2==0)?min.Y():mid.Y();
134     ZminChild = (i<4)?min.Z():mid.Z();
135     minChild.SetCoord(XminChild, YminChild, ZminChild);
136
137     // The child is of the same type than its father (For instance, a SMESH_OctreeNode)
138     // We allocate the memory we need for the child
139     myChildren[i] = allocateOctreeChild();
140     // and we assign to him its box.
141     myChildren[i]->myFather = this;
142     myChildren[i]->myLimit = myLimit;
143     myChildren[i]->myLevel = myLevel + 1;
144     myChildren[i]->myBox = new Bnd_B3d(minChild+childHsize,childHsize);
145     myChildren[i]->myBox->Enlarge( rootSize * 1e-10 );
146     if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize )
147       myChildren[i]->myIsLeaf = true;
148   }
149
150   // After building the 8 boxes, we put the data into the children.
151   buildChildrenData();
152
153   //After we pass to the next level of the Octree
154   for (int i = 0; i<8; i++)
155     myChildren[i]->buildChildren();
156 }
157
158 //================================================================================
159 /*!
160  * \brief Tell if Octree is a leaf or not
161  *        An inheriting class can influence it via myIsLeaf protected field
162  */
163 //================================================================================
164
165 bool SMESH_Octree::isLeaf() const
166 {
167   return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false );
168 }
169
170 //===========================================================================
171 /*!
172  * \brief Compute the bigger dimension of my box
173  */
174 //===========================================================================
175
176 double SMESH_Octree::maxSize() const
177 {
178   if ( myBox )
179   {
180     gp_XYZ min = myBox->CornerMin();
181     gp_XYZ max = myBox->CornerMax();
182     gp_XYZ Size = (max - min);
183     double returnVal = (Size.X()>Size.Y())?Size.X():Size.Y();
184     return (returnVal>Size.Z())?returnVal:Size.Z();
185   }
186   return 0.;
187 }