Salome HOME
Implememtation of evaluation for improvement 0019296.
[modules/smesh.git] / src / SMESH / SMESH_Octree.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH  SMESH_Octree : global Octree implementation
23 //
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  * \brief SMESH_Octree Constructor
34  * \param maxLevel     - The level max the octree can reach (If <0 unlimited)
35  */
36 //===========================================================================
37 SMESH_Octree::SMESH_Octree (const int maxLevel, const double minBoxSize):
38     myChildren(NULL),
39     myFather(NULL),
40     myLevel(0),
41     myMaxLevel(maxLevel),
42     myMinBoxSize(minBoxSize),
43     myIsLeaf(-1)
44 {
45   myBox = new Bnd_B3d();
46 }
47
48 //======================================
49 /*!
50  * \brief SMESH_Octree Destructor
51  */
52 //======================================
53 SMESH_Octree::~SMESH_Octree ()
54 {
55   if(myChildren != NULL)
56   {
57     if(!myIsLeaf)
58     {
59       for(int i = 0; i<8; i++)
60         delete myChildren[i];
61       delete[] myChildren;
62     }
63   }
64   delete myBox;
65 }
66
67 //===========================================================================
68 /*!
69  * \brief Set the bounding box of the Octree
70  * \param box          - 3d Bounding Box of the Octree
71  */
72 //===========================================================================
73 void SMESH_Octree::setBox(const Bnd_B3d* box)
74 {
75 //   delete myBox;
76 //   myBox=new Bnd_B3d(*box);
77   *myBox = *box;
78 }
79
80 //===========================================================================
81 /*!
82  * \brief Set box to the 3d Bounding Box of the Octree
83  * \param box          - Set box to the 3d Bounding Box of the Octree
84  */
85 //===========================================================================
86 void SMESH_Octree::getBox(Bnd_B3d& box)
87 {
88 //   if(box != NULL)
89 //     delete box;
90 //   box = new Bnd_B3d (*myBox);
91   box = *myBox;
92 }
93
94 //===========================================================================
95 /*!
96  * \brief Set the max level of the Octree
97  * \param maxLevel     - The level max the octree can reach (If <0 unlimited)
98  */
99 //===========================================================================
100 void SMESH_Octree::setMaxLevel(const int maxLevel)
101 {myMaxLevel = maxLevel;}
102
103
104 //===========================================================================
105 /*!
106  * \brief Compute the bigger dimension of the box
107  * \param box          - 3d Box
108  * \retval double - bigger dimension of the box
109  */
110 //===========================================================================
111 double SMESH_Octree::maxSize(const Bnd_B3d* box)
112 {
113   if(box ==NULL)
114     return 0;
115   gp_XYZ min = box->CornerMin();
116   gp_XYZ max = box->CornerMax();
117   gp_XYZ Size = (max - min);
118   double returnVal = (Size.X()>Size.Y())?Size.X():Size.Y();
119   return (returnVal>Size.Z())?returnVal:Size.Z();
120 }
121
122 //=============================
123 /*!
124  * \brief Compute the Octree
125  */
126 //=============================
127 void SMESH_Octree::Compute()
128 {
129   // As soon as the Octree is a Leaf, I stop building his children
130   if(!isLeaf())
131     buildChildren();
132 }
133
134 //=================================================================
135 /*!
136  * \brief Build the 8 children boxes and call buildChildrenData()
137  */
138 //=================================================================
139 void SMESH_Octree::buildChildren()
140 {
141   myChildren = new SMESH_Octree*[8];
142
143   gp_XYZ min = myBox->CornerMin();
144   gp_XYZ max = myBox->CornerMax();
145   gp_XYZ HSize = (max - min)/2.;
146   gp_XYZ mid = min + HSize;
147   gp_XYZ childHsize = HSize/2.;
148
149   Standard_Real XminChild, YminChild, ZminChild;
150   Bnd_B3d* box;
151   gp_XYZ minChild;
152   for (int i = 0; i < 8; i++)
153   {
154     // We build the eight boxes, we need 2 points to do that:
155     // Min and Mid
156     // In binary, we can write i from 0 to 7
157     // For instance :
158     // 5 is 101, it corresponds here in coordinates to ZYX
159     // If coordinate is 0 in Y-> box from Ymin to Ymid
160     // If coordinate is 1 in Y-> box from Ymid to Ymax
161     // Same scheme for X and Z
162     // I need the minChild to build the Bnd_B3d box.
163
164     XminChild = (i%2==0)?min.X():mid.X();
165     YminChild = ((i%4)/2==0)?min.Y():mid.Y();
166     ZminChild = (i<4)?min.Z():mid.Z();
167     minChild.SetCoord(XminChild, YminChild, ZminChild);
168
169     box = new Bnd_B3d(minChild+childHsize,childHsize);
170     // The child is of the same type than its father (For instance, a SMESH_OctreeNode)
171     // We allocate the memory we need for the child
172     myChildren[i] = allocateOctreeChild();
173     // and we assign to him its box.
174     myChildren[i]->setBox(box);
175     delete box;
176   }
177
178   // After building the 8 boxes, we put the data into the children..
179   buildChildrenData();
180
181   //After we pass to the next level of the Octree
182   for (int i = 0; i < 8; i++)
183     myChildren[i]->Compute();
184 }