smesh/src/SMESHDS/SMESHDS_SubMesh.cxx

450 lines
12 KiB
C++
Raw Normal View History

2016-03-18 22:10:20 +05:00
// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
2009-02-17 10:27:49 +05:00
//
2012-08-09 16:03:55 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
2003-07-10 16:13:42 +06:00
//
2012-08-09 16:03:55 +06:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
2014-02-20 18:25:37 +06:00
// version 2.1 of the License, or (at your option) any later version.
2003-07-10 16:13:42 +06:00
//
2012-08-09 16:03:55 +06:00
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
2003-07-10 16:13:42 +06:00
//
2012-08-09 16:03:55 +06:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2003-07-10 16:13:42 +06:00
//
2012-08-09 16:03:55 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2009-02-17 10:27:49 +05:00
//
2012-08-09 16:03:55 +06:00
2009-02-17 10:27:49 +05:00
// SMESH SMESHDS : management of mesh data and SMESH document
2003-07-10 16:13:42 +06:00
// File : SMESH_SubMesh.cxx
// Author : Yves FRICAUD, OCC
// Module : SMESH
// $Header:
2009-02-17 10:27:49 +05:00
//
#include "SMESHDS_SubMesh.hxx"
2003-05-19 19:25:06 +06:00
#include "SMESHDS_Mesh.hxx"
#include "SMDS_SetIterator.hxx"
#include "SMDS_ElementFactory.hxx"
2004-12-01 15:48:31 +05:00
#include <utilities.h>
2012-08-09 16:03:55 +06:00
//================================================================================
/*!
* \brief Constructor
*/
//================================================================================
SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
{
myParent = parent;
myIndex = index;
myNbElements = 0;
myNbNodes = 0;
2012-08-09 16:03:55 +06:00
}
//================================================================================
/*!
* \brief Destructor
*/
//================================================================================
SMESHDS_SubMesh::~SMESHDS_SubMesh()
{
}
2003-05-19 19:25:06 +06:00
//=======================================================================
//function : AddElement
2015-08-05 22:59:31 +05:00
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2013-08-08 18:39:06 +06:00
void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * elem)
2003-05-19 19:25:06 +06:00
{
2012-08-09 16:03:55 +06:00
if (!IsComplexSubmesh())
2015-08-05 22:59:31 +05:00
{
if ( elem->GetType() == SMDSAbs_Node )
2015-08-05 22:59:31 +05:00
{
AddNode( static_cast< const SMDS_MeshNode* >( elem ));
2015-08-05 22:59:31 +05:00
return;
}
int oldShapeId = elem->GetShapeID();
2015-08-05 22:59:31 +05:00
if ( oldShapeId > 0 )
2012-08-09 16:03:55 +06:00
{
2015-08-05 22:59:31 +05:00
if (oldShapeId != myIndex)
2012-08-09 16:03:55 +06:00
{
2015-08-05 22:59:31 +05:00
throw SALOME_Exception
(LOCALIZED("add element in subshape already belonging to a subshape"));
2012-08-09 16:03:55 +06:00
}
}
2015-08-05 22:59:31 +05:00
elem->setShapeID( myIndex );
myNbElements++;
2015-08-05 22:59:31 +05:00
}
2003-05-19 19:25:06 +06:00
}
//=======================================================================
//function : RemoveElement
2015-08-05 22:59:31 +05:00
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2013-08-08 18:39:06 +06:00
bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * elem )
2003-05-19 19:25:06 +06:00
{
if ( !elem || elem->IsNull() || elem->getshapeId() != myIndex )
2013-08-08 18:39:06 +06:00
{
return false;
}
if ( !IsComplexSubmesh() )
2013-08-08 18:39:06 +06:00
{
elem->setShapeID( 0 );
myNbElements--;
return true;
2013-08-08 18:39:06 +06:00
}
2004-06-18 14:34:31 +06:00
return false;
2003-05-19 19:25:06 +06:00
}
2003-05-19 19:25:06 +06:00
//=======================================================================
//function : AddNode
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2013-04-01 19:05:47 +06:00
void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
2003-05-19 19:25:06 +06:00
{
2004-12-01 15:48:31 +05:00
if ( !IsComplexSubmesh() )
2013-04-01 19:05:47 +06:00
{
const int shapeId = N->getshapeId();
if ( shapeId > 0 )
2012-08-09 16:03:55 +06:00
{
2013-04-01 19:05:47 +06:00
if ( shapeId != myIndex )
throw SALOME_Exception
(LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
return; // already in
2012-08-09 16:03:55 +06:00
}
N->setShapeID( myIndex );
myNbNodes++;
2013-04-01 19:05:47 +06:00
}
2003-05-19 19:25:06 +06:00
}
//=======================================================================
//function : RemoveNode
2015-08-05 22:59:31 +05:00
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2004-12-01 15:48:31 +05:00
bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
2003-05-19 19:25:06 +06:00
{
if ( !N || N->getshapeId() != myIndex )
2013-08-08 18:39:06 +06:00
{
return false;
}
if ( !IsComplexSubmesh() )
{
N->setShapeID( 0 );
myNbNodes--;
return true;
}
2004-06-18 14:34:31 +06:00
return false;
2003-05-19 19:25:06 +06:00
}
//=======================================================================
//function : NbElements
2016-01-22 22:10:27 +05:00
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2013-08-08 18:39:06 +06:00
int SMESHDS_SubMesh::NbElements() const
2003-05-19 19:25:06 +06:00
{
2004-12-01 15:48:31 +05:00
if ( !IsComplexSubmesh() )
return myNbElements;
2004-12-01 15:48:31 +05:00
int nbElems = 0;
TSubMeshSet::const_iterator it = mySubMeshes.begin();
2004-12-01 15:48:31 +05:00
for ( ; it != mySubMeshes.end(); it++ )
nbElems += (*it)->NbElements();
return nbElems;
2003-05-19 19:25:06 +06:00
}
//=======================================================================
//function : NbNodes
//purpose :
2003-05-19 19:25:06 +06:00
//=======================================================================
2004-12-01 15:48:31 +05:00
int SMESHDS_SubMesh::NbNodes() const
2003-05-19 19:25:06 +06:00
{
2012-08-09 16:03:55 +06:00
if ( !IsComplexSubmesh() )
return myNbNodes;
2004-12-01 15:48:31 +05:00
int nbElems = 0;
TSubMeshSet::const_iterator it = mySubMeshes.begin();
2004-12-01 15:48:31 +05:00
for ( ; it != mySubMeshes.end(); it++ )
nbElems += (*it)->NbNodes();
return nbElems;
2003-05-19 19:25:06 +06:00
}
2012-08-09 16:03:55 +06:00
/*!
2016-11-10 17:02:48 +05:00
* Template class used for iteration on vector of elements which can resize
* during iteration. The iterator returns only elements present upon its creation.
2012-08-09 16:03:55 +06:00
*/
template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
2003-05-19 19:25:06 +06:00
{
2012-08-09 16:03:55 +06:00
protected:
2016-11-10 17:02:48 +05:00
int _iCur, _iEnd, _iDelta;
const TSET& _table;
2012-08-09 16:03:55 +06:00
public:
2016-11-10 17:02:48 +05:00
MySetIterator(const TSET& table, bool reverse): _table( table )
2012-08-09 16:03:55 +06:00
{
2016-11-10 17:02:48 +05:00
if ( reverse )
{
_iCur = _table.size()-1;
_iEnd = -1;
_iDelta = -1;
}
else
{
_iCur = 0;
_iEnd = _table.size();
_iDelta = 1;
}
if ( more() && !_table[ _iCur ])
next();
2012-08-09 16:03:55 +06:00
}
virtual bool more()
{
2016-11-10 17:02:48 +05:00
return ( _iEnd - _iCur ) * _iDelta > 0;
2012-08-09 16:03:55 +06:00
}
virtual ELEM next()
{
2016-11-10 17:02:48 +05:00
ELEM e = more() ? _table[ _iCur ] : 0;
_iCur += _iDelta;
while ( more() && !_table[ _iCur ])
_iCur += _iDelta;
2012-08-09 16:03:55 +06:00
return e;
}
};
2004-12-01 15:48:31 +05:00
// =====================
// class MyIterator
// =====================
template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
{
public:
MyIterator (const TSubMeshSet& theSubMeshes)
: myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
{}
2004-12-01 15:48:31 +05:00
bool more()
{
2012-08-09 16:03:55 +06:00
while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
2004-12-01 15:48:31 +05:00
{
myElemIt = getElements(*mySubIt);
mySubIt++;
}
myMore = myElemIt.get() && myElemIt->more();
return myMore;
}
VALUE next()
{
VALUE elem = 0;
if ( myMore )
elem = myElemIt->next();
return elem;
}
protected:
2004-12-01 15:48:31 +05:00
virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
getElements(const SMESHDS_SubMesh*) const = 0;
2004-12-01 15:48:31 +05:00
private:
bool myMore;
TSubMeshSet::const_iterator mySubIt, mySubEnd;
boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
2004-12-01 15:48:31 +05:00
};
// =====================
// class MyElemIterator
// =====================
class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
{
public:
MyElemIterator (const TSubMeshSet& theSubMeshes)
2004-12-01 15:48:31 +05:00
:MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
{ return theSubMesh->GetElements(); }
};
// =====================
// class MyNodeIterator
// =====================
class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
{
public:
MyNodeIterator (const TSubMeshSet& theSubMeshes)
2004-12-01 15:48:31 +05:00
:MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
{ return theSubMesh->GetNodes(); }
};
2004-12-01 15:48:31 +05:00
//=======================================================================
//function : GetElements
//purpose :
2004-12-01 15:48:31 +05:00
//=======================================================================
SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
{
2004-12-01 15:48:31 +05:00
if ( IsComplexSubmesh() )
return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
return myParent->shapeElementsIterator( myIndex, myNbElements );
2003-05-19 19:25:06 +06:00
}
2004-12-01 15:48:31 +05:00
//=======================================================================
//function : GetNodes
//purpose :
2004-12-01 15:48:31 +05:00
//=======================================================================
SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
{
2004-12-01 15:48:31 +05:00
if ( IsComplexSubmesh() )
return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
return myParent->shapeNodesIterator( myIndex, myNbNodes );
}
2004-12-01 15:48:31 +05:00
//=======================================================================
//function : Contains
//purpose : check if elem or node is in
//=======================================================================
bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
{
if ( !ME || ME->IsNull() )
2004-12-01 15:48:31 +05:00
return false;
2015-08-05 22:59:31 +05:00
if ( IsComplexSubmesh() )
{
TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
2015-08-05 22:59:31 +05:00
for (; aSubIt != mySubMeshes.end(); aSubIt++)
if ((*aSubIt)->Contains(ME))
return true;
return false;
}
return ME->getshapeId() == myIndex;
2015-08-05 22:59:31 +05:00
}
//=======================================================================
//function : IsQuadratic
//purpose : Return true if my 1st element is quadratic
//=======================================================================
bool SMESHDS_SubMesh::IsQuadratic() const
{
if ( IsComplexSubmesh() )
{
TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
2015-08-05 22:59:31 +05:00
for (; aSubIt != mySubMeshes.end(); aSubIt++)
if ((*aSubIt)->IsQuadratic())
return true;
return false;
}
if ( myNbElements == 0 )
return false;
2015-08-05 22:59:31 +05:00
SMDS_ElemIteratorPtr it = GetElements();
return it->more() && it->next()->IsQuadratic();
2004-12-01 15:48:31 +05:00
}
//=======================================================================
//function : AddSubMesh
//purpose :
2004-12-01 15:48:31 +05:00
//=======================================================================
void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
{
ASSERT( theSubMesh );
mySubMeshes.insert( theSubMesh );
}
//=======================================================================
//function : RemoveSubMesh
//purpose :
//=======================================================================
bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
{
return mySubMeshes.erase( theSubMesh );
}
2012-08-09 16:03:55 +06:00
//=======================================================================
//function : RemoveAllSubmeshes
//purpose :
//=======================================================================
void SMESHDS_SubMesh::RemoveAllSubmeshes()
{
mySubMeshes.clear();
}
2004-12-01 15:48:31 +05:00
//=======================================================================
//function : ContainsSubMesh
//purpose :
2004-12-01 15:48:31 +05:00
//=======================================================================
bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
{
return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
}
//=======================================================================
//function : GetSubMeshIterator
//purpose :
//=======================================================================
SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
{
typedef SMDS_SetIterator< const SMESHDS_SubMesh*, TSubMeshSet::const_iterator > TIterator;
return boost::make_shared< TIterator >( mySubMeshes.begin(), mySubMeshes.end());
}
2009-02-17 10:27:49 +05:00
//=======================================================================
//function : Clear
//purpose : remove the contents
//=======================================================================
void SMESHDS_SubMesh::Clear()
{
if ( myParent && myParent->NbNodes() > 0 )
{
if ( myNbElements > 0 )
for ( SMDS_ElemIteratorPtr it = GetElements(); it->more(); )
{
const SMDS_MeshElement * elem = it->next();
elem->setShapeID( 0 );
}
if ( myNbNodes > 0 )
for ( SMDS_NodeIteratorPtr it = GetNodes(); it->more(); )
{
const SMDS_MeshNode * elem = it->next();
elem->setShapeID( 0 );
}
}
myNbElements = 0;
myNbNodes = 0;
2013-08-08 18:39:06 +06:00
if ( NbSubMeshes() > 0 )
{
SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
while ( sub->more() ) {
if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
sm->Clear();
}
2009-02-17 10:27:49 +05:00
}
}