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