1 // Copyright (C) 2007-2010 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.
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 // 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)
29 #include "SMESH_Octree.hxx"
31 //===========================================================================
33 * Constructor. limit must be provided at tree root construction.
34 * limit will be deleted by SMESH_Octree.
36 //===========================================================================
38 SMESH_Octree::SMESH_Octree (SMESH_Octree::Limit* limit):
48 //================================================================================
50 * \brief Compute the Octree
52 //================================================================================
54 void SMESH_Octree::compute()
58 myBox = buildRootBox();
59 if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize )
66 //======================================
68 * \brief SMESH_Octree Destructor
70 //======================================
72 SMESH_Octree::~SMESH_Octree ()
74 if(myChildren != NULL)
78 for(int i = 0; i<8; i++)
92 //=================================================================
94 * \brief Build the 8 children boxes and call buildChildrenData()
96 //=================================================================
98 void SMESH_Octree::buildChildren()
100 if ( isLeaf() ) return;
102 myChildren = new SMESH_Octree*[8];
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.;
110 Standard_Real XminChild, YminChild, ZminChild;
112 for (int i = 0; i < 8; i++)
114 // We build the eight boxes, we need 2 points to do that:
116 // In binary, we can write i from 0 to 7
118 // 5 is 101, it corresponds here in coordinates to ZYX
119 // If coordinate is 0 in Y-> box from Ymin to Ymid
120 // If coordinate is 1 in Y-> box from Ymid to Ymax
121 // Same scheme for X and Z
122 // I need the minChild to build the Bnd_B3d box.
124 XminChild = (i%2==0)?min.X():mid.X();
125 YminChild = ((i%4)/2==0)?min.Y():mid.Y();
126 ZminChild = (i<4)?min.Z():mid.Z();
127 minChild.SetCoord(XminChild, YminChild, ZminChild);
129 // The child is of the same type than its father (For instance, a SMESH_OctreeNode)
130 // We allocate the memory we need for the child
131 myChildren[i] = allocateOctreeChild();
132 // and we assign to him its box.
133 myChildren[i]->myFather = this;
134 myChildren[i]->myLimit = myLimit;
135 myChildren[i]->myLevel = myLevel + 1;
136 myChildren[i]->myBox = new Bnd_B3d(minChild+childHsize,childHsize);
137 if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize )
138 myChildren[i]->myIsLeaf = true;
141 // After building the 8 boxes, we put the data into the children.
144 //After we pass to the next level of the Octree
145 for (int i = 0; i<8; i++)
146 myChildren[i]->buildChildren();
149 //================================================================================
151 * \brief Tell if Octree is a leaf or not
152 * An inheriting class can influence it via myIsLeaf protected field
154 //================================================================================
156 bool SMESH_Octree::isLeaf() const
158 return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false );
161 //===========================================================================
163 * \brief Compute the bigger dimension of my box
165 //===========================================================================
167 double SMESH_Octree::maxSize() const
171 gp_XYZ min = myBox->CornerMin();
172 gp_XYZ max = myBox->CornerMax();
173 gp_XYZ Size = (max - min);
174 double returnVal = (Size.X()>Size.Y())?Size.X():Size.Y();
175 return (returnVal>Size.Z())?returnVal:Size.Z();