Back to index

salome-smesh  6.5.0
SMESH_Octree.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH  SMESH_Octree : global Octree implementation
00024 //  File      : SMESH_Octree.cxx
00025 //  Created   : Tue Jan 16 16:00:00 2007
00026 //  Author    : Nicolas Geimer & Aurélien Motteux(OCC)
00027 //  Module    : SMESH
00028 //
00029 #include "SMESH_Octree.hxx"
00030 
00031 //===========================================================================
00036 //===========================================================================
00037 
00038 SMESH_Octree::SMESH_Octree (SMESH_Octree::Limit* limit):
00039   myChildren(NULL),
00040   myFather(NULL),
00041   myIsLeaf( false ),
00042   myLimit( limit ),
00043   myLevel(0),
00044   myBox(NULL)
00045 {
00046 }
00047 
00048 //================================================================================
00052 //================================================================================
00053 
00054 void SMESH_Octree::compute()
00055 {
00056   if ( myLevel==0 )
00057   {
00058     myBox = buildRootBox();
00059     if ( myLimit->myMinBoxSize > 0. && maxSize() <= myLimit->myMinBoxSize )
00060       myIsLeaf = true;
00061     else
00062       buildChildren();
00063   }
00064 }
00065 
00066 //======================================
00070 //======================================
00071 
00072 SMESH_Octree::~SMESH_Octree ()
00073 {
00074   if(myChildren != NULL)
00075   {
00076     if(!isLeaf())
00077     {
00078       for(int i = 0; i<8; i++)
00079         delete myChildren[i];
00080       delete[] myChildren;
00081       myChildren = 0;
00082     }
00083   }
00084   if ( myBox )
00085     delete myBox;
00086   myBox = 0;
00087   if ( level() == 0 )
00088     delete myLimit;
00089   myLimit = 0;
00090 }
00091 
00092 //=================================================================
00096 //=================================================================
00097 
00098 void SMESH_Octree::buildChildren()
00099 {
00100   if ( isLeaf() ) return;
00101 
00102   myChildren = new SMESH_Octree*[8];
00103 
00104   gp_XYZ min = myBox->CornerMin();
00105   gp_XYZ max = myBox->CornerMax();
00106   gp_XYZ HSize = (max - min)/2.;
00107   gp_XYZ mid = min + HSize;
00108   gp_XYZ childHsize = HSize/2.;
00109 
00110   // get the whole model size
00111   double rootSize = 0;
00112   {
00113     SMESH_Octree* root = this;
00114     while ( root->myLevel > 0 )
00115       root = root->myFather;
00116     rootSize = root->maxSize();
00117   }
00118   Standard_Real XminChild, YminChild, ZminChild;
00119   gp_XYZ minChild;
00120   for (int i = 0; i < 8; i++)
00121   {
00122     // We build the eight boxes, we need 2 points to do that:
00123     // Min and Mid
00124     // In binary, we can write i from 0 to 7
00125     // For instance :
00126     // 5 is 101, it corresponds here in coordinates to ZYX
00127     // If coordinate is 0 in Y-> box from Ymin to Ymid
00128     // If coordinate is 1 in Y-> box from Ymid to Ymax
00129     // Same scheme for X and Z
00130     // I need the minChild to build the Bnd_B3d box.
00131 
00132     XminChild = (i%2==0)?min.X():mid.X();
00133     YminChild = ((i%4)/2==0)?min.Y():mid.Y();
00134     ZminChild = (i<4)?min.Z():mid.Z();
00135     minChild.SetCoord(XminChild, YminChild, ZminChild);
00136 
00137     // The child is of the same type than its father (For instance, a SMESH_OctreeNode)
00138     // We allocate the memory we need for the child
00139     myChildren[i] = allocateOctreeChild();
00140     // and we assign to him its box.
00141     myChildren[i]->myFather = this;
00142     myChildren[i]->myLimit = myLimit;
00143     myChildren[i]->myLevel = myLevel + 1;
00144     myChildren[i]->myBox = new Bnd_B3d(minChild+childHsize,childHsize);
00145     myChildren[i]->myBox->Enlarge( rootSize * 1e-10 );
00146     if ( myLimit->myMinBoxSize > 0. && myChildren[i]->maxSize() <= myLimit->myMinBoxSize )
00147       myChildren[i]->myIsLeaf = true;
00148   }
00149 
00150   // After building the 8 boxes, we put the data into the children.
00151   buildChildrenData();
00152 
00153   //After we pass to the next level of the Octree
00154   for (int i = 0; i<8; i++)
00155     myChildren[i]->buildChildren();
00156 }
00157 
00158 //================================================================================
00163 //================================================================================
00164 
00165 bool SMESH_Octree::isLeaf() const
00166 {
00167   return myIsLeaf || ((myLimit->myMaxLevel > 0) ? (level() >= myLimit->myMaxLevel) : false );
00168 }
00169 
00170 //===========================================================================
00174 //===========================================================================
00175 
00176 double SMESH_Octree::maxSize() const
00177 {
00178   if ( myBox )
00179   {
00180     gp_XYZ min = myBox->CornerMin();
00181     gp_XYZ max = myBox->CornerMax();
00182     gp_XYZ Size = (max - min);
00183     double returnVal = (Size.X()>Size.Y())?Size.X():Size.Y();
00184     return (returnVal>Size.Z())?returnVal:Size.Z();
00185   }
00186   return 0.;
00187 }