1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : SMESH_ProxyMesh.cxx
20 // Created : Thu Dec 2 12:32:53 2010
21 // Author : Edward AGAPOV (eap)
23 #include "SMESH_ProxyMesh.hxx"
25 #include "SMDS_IteratorOnIterators.hxx"
26 #include "SMDS_SetIterator.hxx"
27 #include "SMESH_MesherHelper.hxx"
29 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
33 //================================================================================
35 * \brief Constructor; mesh must be set by a descendant class
37 //================================================================================
39 SMESH_ProxyMesh::SMESH_ProxyMesh():_mesh(0)
42 //================================================================================
44 * \brief Make a proxy mesh from components. Components become empty
46 //================================================================================
48 SMESH_ProxyMesh::SMESH_ProxyMesh(vector<SMESH_ProxyMesh::Ptr>& components):
51 if ( components.empty() ) return;
53 for ( unsigned i = 0; i < components.size(); ++i )
55 SMESH_ProxyMesh* m = components[i].get();
58 takeTmpElemsInMesh( m );
60 if ( !_mesh ) _mesh = m->_mesh;
61 if ( _allowedTypes.empty() ) _allowedTypes = m->_allowedTypes;
63 if ( _subMeshes.size() < m->_subMeshes.size() )
64 _subMeshes.resize( m->_subMeshes.size(), 0 );
65 for ( unsigned j = 0; j < m->_subMeshes.size(); ++j )
67 if ( !m->_subMeshes[j] ) continue;
71 set< const SMDS_MeshElement * > elems( _subMeshes[j]->_elements.begin(),
72 _subMeshes[j]->_elements.end());
73 elems.insert( m->_subMeshes[j]->_elements.begin(),
74 m->_subMeshes[j]->_elements.end());
75 _subMeshes[j]->_elements.assign( elems.begin(), elems.end() );
76 m->_subMeshes[j]->_elements.clear();
78 if ( !_subMeshes[j]->_n2n )
79 _subMeshes[j]->_n2n = m->_subMeshes[j]->_n2n, m->_subMeshes[j]->_n2n = 0;
81 else if ( _subMeshes[j]->_n2n && m->_subMeshes[j]->_n2n )
82 _subMeshes[j]->_n2n->insert( m->_subMeshes[j]->_n2n->begin(),
83 m->_subMeshes[j]->_n2n->end());
87 _subMeshes[j] = m->_subMeshes[j];
94 //================================================================================
96 * \brief Destructor deletes proxy submeshes and tmp elemens
98 //================================================================================
100 SMESH_ProxyMesh::~SMESH_ProxyMesh()
102 for ( unsigned i = 0; i < _subMeshes.size(); ++i )
103 delete _subMeshes[i];
106 set< const SMDS_MeshElement* >::iterator i = _elemsInMesh.begin();
107 for ( ; i != _elemsInMesh.end(); ++i )
108 GetMeshDS()->RemoveFreeElement( *i, 0 );
109 _elemsInMesh.clear();
112 //================================================================================
114 * \brief Returns index of a shape
116 //================================================================================
118 int SMESH_ProxyMesh::shapeIndex(const TopoDS_Shape& shape) const
120 return ( shape.IsNull() || !_mesh->HasShapeToMesh() ? 0 : GetMeshDS()->ShapeToIndex(shape));
123 //================================================================================
125 * \brief Returns the submesh of a shape; it can be a proxy sub-mesh
127 //================================================================================
129 const SMESHDS_SubMesh* SMESH_ProxyMesh::GetSubMesh(const TopoDS_Shape& shape) const
131 const SMESHDS_SubMesh* sm = 0;
133 int i = shapeIndex(shape);
134 if ( i < _subMeshes.size() )
137 sm = GetMeshDS()->MeshElements( i );
142 //================================================================================
144 * \brief Returns the proxy sub-mesh of a shape; it can be NULL
146 //================================================================================
148 const SMESH_ProxyMesh::SubMesh*
149 SMESH_ProxyMesh::GetProxySubMesh(const TopoDS_Shape& shape) const
151 int i = shapeIndex(shape);
152 return i < _subMeshes.size() ? _subMeshes[i] : 0;
155 //================================================================================
157 * \brief Returns the proxy node of a node; the input node is returned if no proxy exists
159 //================================================================================
161 const SMDS_MeshNode* SMESH_ProxyMesh::GetProxyNode( const SMDS_MeshNode* node ) const
163 const SMDS_MeshNode* proxy = node;
164 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
166 if ( const SubMesh* proxySM = findProxySubMesh( node->getshapeId() ))
167 proxy = proxySM->GetProxyNode( node );
171 TopoDS_Shape shape = SMESH_MesherHelper::GetSubShapeByNode( node, GetMeshDS());
172 TopTools_ListIteratorOfListOfShape ancIt;
173 if ( !shape.IsNull() ) ancIt.Initialize( _mesh->GetAncestors( shape ));
174 for ( ; ancIt.More() && proxy == node; ancIt.Next() )
175 if ( const SubMesh* proxySM = findProxySubMesh( shapeIndex(ancIt.Value())))
176 proxy = proxySM->GetProxyNode( node );
181 //================================================================================
183 * \brief Returns number of proxy sub-meshes
185 //================================================================================
187 int SMESH_ProxyMesh::NbProxySubMeshes() const
190 for ( size_t i = 0; i < _subMeshes.size(); ++i )
191 nb += bool( _subMeshes[i] );
198 //================================================================================
200 * \brief Iterator filtering elements by type
202 //================================================================================
204 class TFilteringIterator : public SMDS_ElemIterator
206 SMDS_ElemIteratorPtr _iter;
207 const SMDS_MeshElement * _curElem;
208 vector< SMDSAbs_EntityType> _okTypes;
210 TFilteringIterator( const vector< SMDSAbs_EntityType>& okTypes,
211 const SMDS_ElemIteratorPtr& elemIterator)
212 :_iter(elemIterator), _curElem(0), _okTypes(okTypes)
220 virtual const SMDS_MeshElement* next()
222 const SMDS_MeshElement* res = _curElem;
224 while ( _iter->more() && !_curElem )
226 _curElem = _iter->next();
227 if ( find( _okTypes.begin(), _okTypes.end(), _curElem->GetEntityType()) == _okTypes.end())
235 //================================================================================
237 * \brief Returns iterator on all faces on the shape taking into account substitutions
239 //================================================================================
241 SMDS_ElemIteratorPtr SMESH_ProxyMesh::GetFaces(const TopoDS_Shape& shape) const
243 if ( !_mesh->HasShapeToMesh() )
244 return SMDS_ElemIteratorPtr();
246 _subContainer.RemoveAllSubmeshes();
248 TopTools_IndexedMapOfShape FF;
249 TopExp::MapShapes( shape, TopAbs_FACE, FF );
250 for ( int i = 1; i <= FF.Extent(); ++i )
251 if ( const SMESHDS_SubMesh* sm = GetSubMesh( FF(i)))
252 _subContainer.AddSubMesh( sm );
254 return _subContainer.SMESHDS_SubMesh::GetElements();
257 //================================================================================
259 * \brief Returns iterator on all faces of the mesh taking into account substitutions
260 * To be used in case of mesh without shape
262 //================================================================================
264 SMDS_ElemIteratorPtr SMESH_ProxyMesh::GetFaces() const
266 if ( _mesh->HasShapeToMesh() )
267 return SMDS_ElemIteratorPtr();
269 _subContainer.RemoveAllSubmeshes();
270 for ( unsigned i = 0; i < _subMeshes.size(); ++i )
272 _subContainer.AddSubMesh( _subMeshes[i] );
274 if ( _subContainer.NbSubMeshes() == 0 ) // no elements substituted
275 return GetMeshDS()->elementsIterator(SMDSAbs_Face);
277 // if _allowedTypes is empty, only elements from _subMeshes are returned,...
278 SMDS_ElemIteratorPtr proxyIter = _subContainer.SMESHDS_SubMesh::GetElements();
279 if ( _allowedTypes.empty() || NbFaces() == _mesh->NbFaces() )
282 // ... else elements filtered using allowedTypes are additionally returned
283 SMDS_ElemIteratorPtr facesIter = GetMeshDS()->elementsIterator(SMDSAbs_Face);
284 SMDS_ElemIteratorPtr filterIter( new TFilteringIterator( _allowedTypes, facesIter ));
285 vector< SMDS_ElemIteratorPtr > iters(2);
286 iters[0] = proxyIter;
287 iters[1] = filterIter;
289 typedef vector< SMDS_ElemIteratorPtr > TElemIterVector;
290 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
291 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
294 //================================================================================
296 * \brief Return total nb of faces taking into account substitutions
298 //================================================================================
300 int SMESH_ProxyMesh::NbFaces() const
303 if ( _mesh->HasShapeToMesh() )
305 TopTools_IndexedMapOfShape FF;
306 TopExp::MapShapes( _mesh->GetShapeToMesh(), TopAbs_FACE, FF );
307 for ( int i = 1; i <= FF.Extent(); ++i )
308 if ( const SMESHDS_SubMesh* sm = GetSubMesh( FF(i)))
309 nb += sm->NbElements();
313 if ( _subMeshes.empty() )
314 return GetMeshDS()->NbFaces();
316 for ( unsigned i = 0; i < _subMeshes.size(); ++i )
318 nb += _subMeshes[i]->NbElements();
320 // if _allowedTypes is empty, only elements from _subMeshes are returned,
321 // else elements filtered using allowedTypes are additionally returned
322 if ( !_allowedTypes.empty() )
324 for ( int t = SMDSEntity_Triangle; t <= SMDSEntity_Quad_Quadrangle; ++t )
327 ( find( _allowedTypes.begin(), _allowedTypes.end(), t ) != _allowedTypes.end() );
329 nb += GetMeshDS()->GetMeshInfo().NbEntities( SMDSAbs_EntityType( t ));
336 //================================================================================
338 * \brief Returns a proxy sub-mesh; it is created if not yet exists
340 //================================================================================
342 SMESH_ProxyMesh::SubMesh* SMESH_ProxyMesh::getProxySubMesh(int index)
344 if ( int(_subMeshes.size()) <= index )
345 _subMeshes.resize( index+1, 0 );
346 if ( !_subMeshes[index] )
347 _subMeshes[index] = newSubmesh( index );
348 return _subMeshes[index];
351 //================================================================================
353 * \brief Returns a proxy sub-mesh; it is created if not yet exists
355 //================================================================================
357 SMESH_ProxyMesh::SubMesh* SMESH_ProxyMesh::getProxySubMesh(const TopoDS_Shape& shape)
359 return getProxySubMesh( shapeIndex( shape ));
362 //================================================================================
364 * \brief Returns a proxy sub-mesh
366 //================================================================================
368 SMESH_ProxyMesh::SubMesh* SMESH_ProxyMesh::findProxySubMesh(int shapeIndex) const
370 return shapeIndex < int(_subMeshes.size()) ? _subMeshes[shapeIndex] : 0;
373 //================================================================================
375 * \brief Returns mesh DS
377 //================================================================================
379 SMESHDS_Mesh* SMESH_ProxyMesh::GetMeshDS() const
381 return (SMESHDS_Mesh*)( _mesh ? _mesh->GetMeshDS() : 0 );
384 //================================================================================
386 * \brief Move proxy sub-mesh from other proxy mesh to this, returns true if sub-mesh found
388 //================================================================================
390 bool SMESH_ProxyMesh::takeProxySubMesh( const TopoDS_Shape& shape,
391 SMESH_ProxyMesh* proxyMesh )
393 if ( proxyMesh && proxyMesh->_mesh == _mesh )
395 int iS = shapeIndex( shape );
396 if ( SubMesh* sm = proxyMesh->findProxySubMesh( iS ))
398 if ( iS >= int(_subMeshes.size()) )
399 _subMeshes.resize( iS + 1, 0 );
401 proxyMesh->_subMeshes[iS] = 0;
408 //================================================================================
410 * \brief Move tmp elements residing the _mesh from other proxy mesh to this
412 //================================================================================
414 void SMESH_ProxyMesh::takeTmpElemsInMesh( SMESH_ProxyMesh* proxyMesh )
418 _elemsInMesh.insert( proxyMesh->_elemsInMesh.begin(),
419 proxyMesh->_elemsInMesh.end());
420 proxyMesh->_elemsInMesh.clear();
424 //================================================================================
426 * \brief Removes tmp elements from the _mesh
428 //================================================================================
430 void SMESH_ProxyMesh::removeTmpElement( const SMDS_MeshElement* elem )
432 if ( elem && elem->GetID() > 0 )
434 set< const SMDS_MeshElement* >::iterator i = _elemsInMesh.find( elem );
435 if ( i != _elemsInMesh.end() )
437 GetMeshDS()->RemoveFreeElement( elem, 0 );
438 _elemsInMesh.erase( i );
447 //================================================================================
449 * \brief Stores tmp element residing the _mesh
451 //================================================================================
453 void SMESH_ProxyMesh::storeTmpElement( const SMDS_MeshElement* elem )
455 _elemsInMesh.insert( elem );
458 //================================================================================
460 * \brief Set node-node correspondence
462 //================================================================================
464 void SMESH_ProxyMesh::setNode2Node(const SMDS_MeshNode* srcNode,
465 const SMDS_MeshNode* proxyNode,
466 const SubMesh* subMesh)
468 SubMesh* sm = const_cast<SubMesh*>( subMesh );
469 if ( !subMesh->_n2n )
470 sm->_n2n = new TN2NMap;
471 sm->_n2n->insert( make_pair( srcNode, proxyNode ));
474 //================================================================================
476 * \brief Return true if the element is a temporary one
478 //================================================================================
480 bool SMESH_ProxyMesh::IsTemporary(const SMDS_MeshElement* elem ) const
482 return ( elem->GetID() < 1 ) || _elemsInMesh.count( elem );
485 //================================================================================
487 * \brief Return a proxy node or an input node
489 //================================================================================
491 const SMDS_MeshNode* SMESH_ProxyMesh::SubMesh::GetProxyNode( const SMDS_MeshNode* n ) const
493 TN2NMap::iterator n2n;
494 if ( _n2n && ( n2n = _n2n->find( n )) != _n2n->end())
499 //================================================================================
501 * \brief Deletes temporary elements
503 //================================================================================
505 void SMESH_ProxyMesh::SubMesh::Clear()
507 for ( unsigned i = 0; i < _elements.size(); ++i )
508 if ( _elements[i]->GetID() < 0 )
512 delete _n2n, _n2n = 0;
515 //================================================================================
517 * \brief Return number of elements in a proxy sub-mesh. The method is meaningful
518 * for a sub-mesh containing tmp faces.
520 //================================================================================
522 int SMESH_ProxyMesh::SubMesh::NbElements() const
524 return _uvPtStructVec.empty() ? _elements.size() : _uvPtStructVec.size() - 1;
527 //================================================================================
529 * \brief Return elements of a proxy sub-mesh. The method is meaningful
530 * for a sub-mesh containing tmp faces.
532 //================================================================================
534 SMDS_ElemIteratorPtr SMESH_ProxyMesh::SubMesh::GetElements() const
536 return SMDS_ElemIteratorPtr
537 ( new SMDS_ElementVectorIterator( _elements.begin(), _elements.end() ));
540 //================================================================================
542 * \brief Return number of nodes in a proxy sub-mesh. The method is meaningful
543 * for a sub-mesh containing nodes of 2D viscous layer.
545 //================================================================================
547 int SMESH_ProxyMesh::SubMesh::NbNodes() const
549 return _uvPtStructVec.size();
552 //================================================================================
554 * \brief Return nodes of a proxy sub-mesh. The method is meaningful
555 * for a sub-mesh containing nodes of 2D viscous layer.
557 //================================================================================
559 SMDS_NodeIteratorPtr SMESH_ProxyMesh::SubMesh::GetNodes() const
561 if ( !_uvPtStructVec.empty() )
562 return SMDS_NodeIteratorPtr ( new SMDS_SetIterator
564 UVPtStructVec::const_iterator,
565 UVPtStruct::NodeAccessor >
566 ( _uvPtStructVec.begin(), _uvPtStructVec.end() ));
568 return SMDS_NodeIteratorPtr
569 ( new SMDS_SetIterator< SMDS_pNode, std::vector< SMDS_pElement >::const_iterator>
570 ( _elements.begin(), _elements.end() ));
573 //================================================================================
575 * \brief Store an element
577 //================================================================================
579 void SMESH_ProxyMesh::SubMesh::AddElement(const SMDS_MeshElement * e)
581 _elements.push_back( e );
584 //================================================================================
586 * \brief Check presence of element inside it-self
588 //================================================================================
590 bool SMESH_ProxyMesh::SubMesh::Contains(const SMDS_MeshElement * ME) const
592 if ( ME->GetType() != SMDSAbs_Node )
593 return find( _elements.begin(), _elements.end(), ME ) != _elements.end();