Back to index

salome-smesh  6.5.0
SMESHDS_SubMesh.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 SMESHDS : management of mesh data and SMESH document
00024 //  File   : SMESH_SubMesh.cxx
00025 //  Author : Yves FRICAUD, OCC
00026 //  Module : SMESH
00027 //  $Header: 
00028 //
00029 #include "SMESHDS_SubMesh.hxx"
00030 #include "SMESHDS_Mesh.hxx"
00031 
00032 #include "utilities.h"
00033 #include "SMDS_SetIterator.hxx"
00034 #include <iostream>
00035 #include <cassert>
00036 
00037 using namespace std;
00038 
00039 
00040 //================================================================================
00044 //================================================================================
00045 
00046 SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
00047 {
00048   myParent = parent;
00049   myElements.clear();
00050   myNodes.clear();
00051   myIndex = index;
00052   myUnusedIdNodes = 0;
00053   myUnusedIdElements = 0;
00054 }
00055 
00056 //================================================================================
00060 //================================================================================
00061 
00062 SMESHDS_SubMesh::~SMESHDS_SubMesh()
00063 {
00064 }
00065 
00066 //=======================================================================
00067 //function : AddElement
00068 //purpose  : 
00069 //=======================================================================
00070 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
00071 {
00072   if (!IsComplexSubmesh())
00073     {
00074       if ( ME->GetType() == SMDSAbs_Node )
00075       {
00076         AddNode( static_cast< const SMDS_MeshNode* >( ME ));
00077         return;
00078       }
00079       //MESSAGE("in " << myIndex << " AddElement "<< ME->GetID());
00080       int oldShapeId = ME->getshapeId();
00081       if ( oldShapeId > 0 )
00082         {
00083           if (oldShapeId != myIndex)
00084             {
00085               MESSAGE("add element in subshape already belonging to another subshape "
00086                 << ME->GetID() << " " << oldShapeId << " " << myIndex);
00087               throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
00088             }
00089           else
00090             {
00091               int idInSubShape = ME->getIdInShape();
00092               if (idInSubShape >= 0)
00093                 {
00094                   MESSAGE("add element in subshape already belonging to that subshape "
00095                       << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
00096                   // check if ok: do nothing if ok
00097                   if (idInSubShape >= myElements.size())
00098                     {
00099                       MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
00100                       throw SALOME_Exception(LOCALIZED("out of bounds"));
00101                     }
00102                   if (ME != myElements[idInSubShape])
00103                     {
00104                       MESSAGE("not the same element");
00105                       throw SALOME_Exception(LOCALIZED("not the same element"));
00106                     }
00107                   MESSAGE("already done, OK, nothing to do");
00108                   return;
00109                 }
00110             }
00111         }
00112 
00113       SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
00114       elem->setShapeId(myIndex);
00115       elem->setIdInShape(myElements.size());
00116       myElements.push_back(ME);
00117     }
00118 }
00119 
00120 //=======================================================================
00121 //function : RemoveElement
00122 //purpose  : 
00123 //=======================================================================
00124 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
00125 {
00126   if (!ME)
00127     {
00128       MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
00129       return false;
00130     }
00131   //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
00132   if (!IsComplexSubmesh())
00133     {
00134       if ( ME->getshapeId() != myIndex )
00135         return false;
00136       int idInSubShape = ME->getIdInShape();
00137       //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
00138       SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
00139       elem->setShapeId(0);
00140       elem->setIdInShape(-1);
00141       if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
00142         {
00143           myElements[idInSubShape] = 0; // this vector entry is no more used
00144           myUnusedIdElements++;
00145           return true;
00146         }
00147       return false;
00148       //  }
00149     }
00150   MESSAGE("Try to remove an element from a complex submesh ");
00151   return false;
00152 }
00153 
00154 //=======================================================================
00155 //function : AddNode
00156 //purpose  : 
00157 //=======================================================================
00158 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
00159 {
00160   if ( !IsComplexSubmesh() )
00161     {
00162       int idInSubShape = N->getIdInShape();
00163       int shapeId = N->getshapeId();
00164       if ((shapeId > 0) && (idInSubShape >= 0))
00165         {
00166 //           MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
00167           // OK for vertex nodes
00168           throw SALOME_Exception(LOCALIZED("add node in subshape already belonging to a subshape"));
00169         }
00170       SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
00171       node->setShapeId(myIndex);
00172       node->setIdInShape(myNodes.size());
00173       myNodes.push_back(N);
00174       //MESSAGE("in "<< myIndex << " AddNode " << node->GetID());
00175     }
00176   //MESSAGE("try to add node in a complex submesh " << N->GetID());
00177 }
00178 
00179 //=======================================================================
00180 //function : RemoveNode
00181 //purpose  : 
00182 //=======================================================================
00183 
00184 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
00185 {
00186   if (!IsComplexSubmesh())
00187     {
00188       // if (!isNodeDeleted) // alive node has valid ID and can be found
00189       // {
00190       if ( N->getshapeId() != myIndex )
00191         return false;
00192       int idInSubShape = N->getIdInShape();
00193       //int shapeId = N->getshapeId();
00194       //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
00195       SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
00196       node->setShapeId(0);
00197       node->setIdInShape(-1);
00198       if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
00199         {
00200           myNodes[idInSubShape] = 0; // this vector entry is no more used
00201           myUnusedIdNodes++;
00202           return true;
00203         }
00204       return false;
00205       // }
00206     }
00207   MESSAGE("Try to remove a node from a complex submesh");
00208   return false;
00209 }
00210 
00211 //=======================================================================
00212 //function : NbElements
00213 //purpose  : 
00214 //=======================================================================
00215 int SMESHDS_SubMesh::NbElements() const
00216 {
00217   //MESSAGE(this << " NbElements " << IsComplexSubmesh() << " " << myElements.size() - myUnusedIdElements);
00218   if ( !IsComplexSubmesh() )
00219     return myElements.size() - myUnusedIdElements;
00220 
00221   int nbElems = 0;
00222   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
00223   for ( ; it != mySubMeshes.end(); it++ )
00224     nbElems += (*it)->NbElements();
00225 
00226   return nbElems;
00227 }
00228 
00229 //=======================================================================
00230 //function : NbNodes
00231 //purpose  : 
00232 //=======================================================================
00233 
00234 int SMESHDS_SubMesh::NbNodes() const
00235 {
00236   //MESSAGE(this << " NbNodes " << IsComplexSubmesh() << " " << myNodes.size() - myUnusedIdNodes);
00237   if ( !IsComplexSubmesh() )
00238     return myNodes.size() - myUnusedIdNodes;
00239 
00240   int nbElems = 0;
00241   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
00242   for ( ; it != mySubMeshes.end(); it++ )
00243     nbElems += (*it)->NbNodes();
00244 
00245   return nbElems;
00246 }
00247 
00255 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
00256 {
00257 protected:
00258   typename TSET::const_iterator _it, _end;
00259   TSET _table;
00260 public:
00261   MySetIterator(const TSET& table)
00262   {
00263     _table = table;
00264     _it = _table.begin();
00265     _end = _table.end();
00266     while ((_it != _end) && (*_it == 0))
00267       _it++;
00268   }
00269 
00270   virtual bool more()
00271   {
00272     while ((_it != _end) && (*_it == 0))
00273       _it++;
00274     return (_it != _end);
00275   }
00276 
00277   virtual ELEM next()
00278   {
00279     ELEM e = *_it;
00280     _it++;
00281     return e;
00282   }
00283 };
00284 
00285 // =====================
00286 // class MyIterator
00287 // =====================
00288 
00289 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
00290 {
00291  public:
00292   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
00293     : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
00294     {}
00295   bool more()
00296   {
00297     while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
00298     {
00299       myElemIt = getElements(*mySubIt);
00300       mySubIt++;
00301     }
00302     myMore = myElemIt.get() && myElemIt->more();
00303     return myMore;
00304   }
00305   VALUE next()
00306   {
00307     VALUE elem = 0;
00308     if ( myMore )
00309       elem = myElemIt->next();
00310     return elem;
00311   }
00312  protected:
00313   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
00314     getElements(const SMESHDS_SubMesh*) const = 0;
00315 
00316  private:
00317   bool                                        myMore;
00318   set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
00319   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
00320 };
00321 
00322 // =====================
00323 // class MyElemIterator
00324 // =====================
00325 
00326 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
00327 {
00328  public:
00329   MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
00330     :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
00331   SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
00332   { return theSubMesh->GetElements(); }
00333 };
00334 
00335 // =====================
00336 // class MyNodeIterator
00337 // =====================
00338 
00339 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
00340 {
00341  public:
00342   MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
00343     :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
00344   SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
00345   { return theSubMesh->GetNodes(); }
00346 };
00347   
00348 //=======================================================================
00349 //function : GetElements
00350 //purpose  : 
00351 //=======================================================================
00352 
00353 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
00354 {
00355   if ( IsComplexSubmesh() )
00356     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
00357   return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
00358 }
00359 
00360 //=======================================================================
00361 //function : GetNodes
00362 //purpose  : 
00363 //=======================================================================
00364 
00365 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
00366 {
00367   if ( IsComplexSubmesh() )
00368     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
00369 
00370   return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
00371 }
00372 
00373 //=======================================================================
00374 //function : Contains
00375 //purpose  : check if elem or node is in
00376 //=======================================================================
00377 
00378 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
00379 {
00380   // DO NOT TRY TO FIND A REMOVED ELEMENT !!
00381   //if ( IsComplexSubmesh() || !ME )
00382   if (!ME)
00383     return false;
00384 
00385   if (IsComplexSubmesh())
00386     {
00387       set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
00388       for (; aSubIt != mySubMeshes.end(); aSubIt++)
00389         if ((*aSubIt)->Contains(ME))
00390           return true;
00391       return false;
00392     }
00393 
00394   if (ME->GetType() == SMDSAbs_Node)
00395     {
00396       int idInShape = ME->getIdInShape();
00397       if ((idInShape >= 0) && (idInShape < myNodes.size()))
00398         if (myNodes[idInShape] == ME)
00399           return true;
00400     }
00401   else
00402     {
00403       int idInShape = ME->getIdInShape();
00404       if ((idInShape >= 0) && (idInShape < myElements.size()))
00405         if (myElements[idInShape] == ME)
00406           return true;
00407     }
00408   return false;
00409 }
00410 
00411 //=======================================================================
00412 //function : AddSubMesh
00413 //purpose  : 
00414 //=======================================================================
00415 
00416 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
00417 {
00418   ASSERT( theSubMesh );
00419   mySubMeshes.insert( theSubMesh );
00420 }
00421 
00422 //=======================================================================
00423 //function : RemoveSubMesh
00424 //purpose  : 
00425 //=======================================================================
00426 
00427 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
00428 {
00429   return mySubMeshes.erase( theSubMesh );
00430 }
00431 
00432 //=======================================================================
00433 //function : RemoveAllSubmeshes
00434 //purpose  : 
00435 //=======================================================================
00436 
00437 void SMESHDS_SubMesh::RemoveAllSubmeshes()
00438 {
00439   mySubMeshes.clear();
00440 }
00441 
00442 //=======================================================================
00443 //function : ContainsSubMesh
00444 //purpose  : 
00445 //=======================================================================
00446 
00447 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
00448 {
00449   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
00450 }
00451 
00452 //=======================================================================
00453 //function : GetSubMeshIterator
00454 //purpose  : 
00455 //=======================================================================
00456 
00457 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
00458 {
00459   typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
00460   return SMESHDS_SubMeshIteratorPtr
00461     ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
00462                                                                  mySubMeshes.end()));
00463 }
00464 
00465 //=======================================================================
00466 //function : Clear
00467 //purpose  : remove the contents
00468 //=======================================================================
00469 
00470 void SMESHDS_SubMesh::Clear()
00471 {
00472   myElements.clear();
00473   myNodes.clear();
00474   myUnusedIdNodes = 0;
00475   myUnusedIdElements = 0;
00476   SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
00477   while ( sub->more() ) {
00478     if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
00479       sm->Clear();
00480   }
00481 }
00482 
00483 int SMESHDS_SubMesh::getSize()
00484 {
00485   int c = NbNodes();
00486   int d = NbElements();
00487   //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
00488   //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
00489   return c+d;
00490 }
00491 
00492 void SMESHDS_SubMesh::compactList()
00493 {
00494   //MESSAGE("compactList old: nodes " << myNodes.size() << " elements " << myElements.size());
00495   //stringstream a;
00496   //stringstream b;
00497   //stringstream c;
00498   //stringstream d;
00499 
00500   std::vector<const SMDS_MeshElement*> newElems;
00501   newElems.clear();
00502   for (int i = 0; i < myElements.size(); i++)
00503     if (myElements[i])
00504       {
00505         SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
00506         elem->setIdInShape(newElems.size());
00507         newElems.push_back(elem);
00508         //a << elem->GetID() << " ";
00509         //b << elem->GetID() << " ";
00510       }
00511     //else
00512     //  a << "_ ";
00513   myElements.swap(newElems);
00514   myUnusedIdElements = 0;
00515   //MESSAGE("in " << myIndex << " oldElems " << a.str());
00516   //MESSAGE("in " << myIndex << " newElems " << b.str());
00517 
00518   std::vector<const SMDS_MeshNode*> newNodes;
00519   newNodes.clear();
00520   for (int i = 0; i < myNodes.size(); i++)
00521     if (myNodes[i])
00522       {
00523         SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
00524         node->setIdInShape(newNodes.size());
00525         newNodes.push_back(node);
00526         //c << node->GetID() << " ";
00527         //d << node->GetID() << " ";
00528       }
00529     //else
00530     //  c << "_ ";
00531   myNodes.swap(newNodes);
00532   myUnusedIdNodes = 0;
00533   //MESSAGE("in " << myIndex << " oldNodes " << c.str());
00534   //MESSAGE("in " << myIndex << " newNodes " << d.str());
00535   //MESSAGE("compactList new: nodes " << myNodes.size() << " elements " << myElements.size());
00536 }