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 : implementaion of SMESH idl descriptions
24 // File : StdMeshers_Import_1D2D.cxx
27 #include "StdMeshers_Import_1D2D.hxx"
29 #include "StdMeshers_Import_1D.hxx"
30 #include "StdMeshers_ImportSource.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMESH_Gen.hxx"
38 #include "SMESH_Group.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
43 #include "Utils_SALOME_Exception.hxx"
44 #include "utilities.h"
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
49 #include <TopExp_Explorer.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
59 //=============================================================================
61 * Creates StdMeshers_Import_1D2D
63 //=============================================================================
65 StdMeshers_Import_1D2D::StdMeshers_Import_1D2D(int hypId, int studyId, SMESH_Gen * gen)
66 :SMESH_2D_Algo(hypId, studyId, gen), _sourceHyp(0)
68 MESSAGE("StdMeshers_Import_1D2D::StdMeshers_Import_1D2D");
69 _name = "Import_1D2D";
70 _shapeType = (1 << TopAbs_FACE);
72 _compatibleHypothesis.push_back("ImportSource2D");
73 _requireDescretBoundary = false;
76 //=============================================================================
78 * Check presence of a hypothesis
80 //=============================================================================
82 bool StdMeshers_Import_1D2D::CheckHypothesis
84 const TopoDS_Shape& aShape,
85 SMESH_Hypothesis::Hypothesis_Status& aStatus)
89 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
90 if ( hyps.size() == 0 )
92 aStatus = SMESH_Hypothesis::HYP_MISSING;
93 return false; // can't work with no hypothesis
96 if ( hyps.size() > 1 )
98 aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
102 const SMESHDS_Hypothesis *theHyp = hyps.front();
104 string hypName = theHyp->GetName();
106 if (hypName == _compatibleHypothesis.front())
108 _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
109 aStatus = SMESH_Hypothesis::HYP_OK;
113 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
120 * \brief OrientedLink additionally storing a medium node
122 struct TLink : public SMESH_OrientedLink
124 const SMDS_MeshNode* _medium;
125 TLink( const SMDS_MeshNode* n1,
126 const SMDS_MeshNode* n2,
127 const SMDS_MeshNode* medium=0)
128 : SMESH_OrientedLink( n1,n2 ), _medium( medium ) {}
132 //=============================================================================
134 * Import elements from the other mesh
136 //=============================================================================
138 bool StdMeshers_Import_1D2D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
140 if ( !_sourceHyp ) return false;
142 const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
143 if ( srcGroups.empty() )
144 return error("Invalid source groups");
146 SMESH_MesherHelper helper(theMesh);
147 helper.SetSubShape(theShape);
148 SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
150 const TopoDS_Face& geomFace = TopoDS::Face( theShape );
151 const double faceTol = helper.MaxTolerance( geomFace );
152 const int shapeID = tgtMesh->ShapeToIndex( geomFace );
153 const bool toCheckOri = (helper.NbAncestors( geomFace, theMesh, TopAbs_SOLID ) == 1 );
155 Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
157 ( helper.GetSubShapeOri( tgtMesh->ShapeToMesh(), geomFace) == TopAbs_REVERSED );
158 gp_Pnt p; gp_Vec du, dv;
160 set<int> subShapeIDs;
161 subShapeIDs.insert( shapeID );
163 // get nodes on vertices
164 list < SMESH_TNodeXYZ > vertexNodes;
165 list < SMESH_TNodeXYZ >::iterator vNIt;
166 TopExp_Explorer exp( theShape, TopAbs_VERTEX );
167 for ( ; exp.More(); exp.Next() )
169 const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
170 if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
172 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
175 _gen->Compute(theMesh,v,/*anUpward=*/true);
176 n = SMESH_Algo::VertexNode( v, tgtMesh );
177 if ( !n ) return false; // very strange
179 vertexNodes.push_back( SMESH_TNodeXYZ( n ));
182 // to count now many times a link between nodes encounters
183 map<TLink, int> linkCount;
184 map<TLink, int>::iterator link2Nb;
186 // =========================
187 // Import faces from groups
188 // =========================
190 StdMeshers_Import_1D::TNodeNodeMap* n2n;
191 StdMeshers_Import_1D::TElemElemMap* e2e;
192 vector<const SMDS_MeshNode*> newNodes;
193 for ( int iG = 0; iG < srcGroups.size(); ++iG )
195 const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
197 const int meshID = srcGroup->GetMesh()->GetPersistentId();
198 const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
199 if ( !srcMesh ) continue;
200 StdMeshers_Import_1D::getMaps( srcMesh, &theMesh, n2n, e2e );
202 SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
203 SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
205 while ( srcElems->more() ) // loop on group contents
207 const SMDS_MeshElement* face = srcElems->next();
208 // find or create nodes of a new face
209 newNodes.resize( face->NbNodes() );
211 int nbCreatedNodes = 0;
212 SMDS_MeshElement::iterator node = face->begin_nodes();
213 for ( unsigned i = 0; i < newNodes.size(); ++i, ++node )
215 TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
218 if ( !subShapeIDs.count( n2nIt->second->getshapeId() ))
223 // find an existing vertex node
224 for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
225 if ( vNIt->SquareDistance( *node ) < 10 * faceTol * faceTol)
227 (*n2nIt).second = vNIt->_node;
228 vertexNodes.erase( vNIt );
232 if ( !n2nIt->second )
234 // find out if node lies on theShape
235 tmpNode->setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
236 if ( helper.CheckNodeUV( geomFace, tmpNode, uv, 10 * faceTol, /*force=*/true ))
238 SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
239 n2nIt->second = newNode;
240 tgtMesh->SetNodeOnFace( newNode, shapeID, uv.X(), uv.Y() );
244 if ( !(newNodes[i] = n2nIt->second ))
247 if ( !newNodes.back() )
248 continue; // not all nodes of the face lie on theShape
250 // try to find already created face
251 SMDS_MeshElement * newFace = 0;
252 if ( nbCreatedNodes == 0 &&
253 tgtMesh->FindElement(newNodes, SMDSAbs_Face, /*noMedium=*/false))
254 continue; // repeated face in source groups already created
256 // check future face orientation
263 uv = helper.GetNodeUV( geomFace, newNodes[++iNode] );
264 surface->D1( uv.X(),uv.Y(), p, du,dv );
265 geomNorm = reverse ? dv^du : du^dv;
267 while ( geomNorm.SquareMagnitude() < 1e-6 && iNode+1 < face->NbCornerNodes());
269 int iNext = helper.WrapIndex( iNode+1, face->NbCornerNodes() );
270 int iPrev = helper.WrapIndex( iNode-1, face->NbCornerNodes() );
272 SMESH_TNodeXYZ prevNode( newNodes[iPrev] );
273 SMESH_TNodeXYZ curNode ( newNodes[iNode] );
274 SMESH_TNodeXYZ nextNode( newNodes[iNext] );
275 gp_Vec n1n0( prevNode - curNode);
276 gp_Vec n1n2( nextNode - curNode );
277 gp_Vec meshNorm = n1n2 ^ n1n0;
279 if ( geomNorm * meshNorm < 0 )
280 std::reverse( newNodes.begin(), newNodes.end() );
284 switch ( newNodes.size() )
287 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2] );
290 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3] );
293 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2],
294 newNodes[3], newNodes[4], newNodes[5]);
297 newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3],
298 newNodes[4], newNodes[5], newNodes[6], newNodes[7]);
302 tgtMesh->SetMeshElementOnShape( newFace, shapeID );
303 e2e->insert( make_pair( face, newFace ));
306 int nbNodes = face->NbCornerNodes();
307 const SMDS_MeshNode* medium = 0;
308 for ( int i = 0; i < nbNodes; ++i )
310 const SMDS_MeshNode* n1 = newNodes[i];
311 const SMDS_MeshNode* n2 = newNodes[ (i+1)%nbNodes ];
312 if ( newFace->IsQuadratic() )
313 medium = newNodes[i+nbNodes];
314 link2Nb = linkCount.insert( make_pair( TLink( n1, n2, medium ), 0)).first;
318 helper.GetMeshDS()->RemoveNode(tmpNode);
321 // ==========================================================
322 // Put nodes on geom edges and create edges on them;
323 // check if the whole geom face is covered by imported faces
324 // ==========================================================
326 vector< TopoDS_Edge > edges;
327 for ( exp.Init( theShape, TopAbs_EDGE ); exp.More(); exp.Next() )
328 if ( subShapeIDs.insert( tgtMesh->ShapeToIndex( exp.Current() )).second )
329 edges.push_back( TopoDS::Edge( exp.Current() ));
331 bool isFaceMeshed = false;
332 if ( SMESHDS_SubMesh* tgtSM = tgtMesh->MeshElements( theShape ))
334 // the imported mesh is valid if all external links (encountered once)
336 subShapeIDs.erase( shapeID ); // to contain edges and vertices only
338 for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); ++link2Nb)
340 const TLink& link = (*link2Nb).first;
341 int nbFaces = link2Nb->second;
344 // check if a not shared link lie on face boundary
345 bool nodesOnBoundary = true;
346 list< TopoDS_Shape > bndShapes;
347 for ( int is1stN = 0; is1stN < 2 && nodesOnBoundary; ++is1stN )
349 const SMDS_MeshNode* n = is1stN ? link.node1() : link.node2();
350 if ( !subShapeIDs.count( n->getshapeId() ))
352 for ( unsigned iE = 0; iE < edges.size(); ++iE )
353 if ( helper.CheckNodeU( edges[iE], n, u, 10 * faceTol, /*force=*/true ))
355 BRep_Tool::Range(edges[iE],f,l);
356 if ( Abs(u-f) < 2 * faceTol || Abs(u-l) < 2 * faceTol )
357 // duplicated node on vertex
358 return error("Source elements overlap one another");
359 tgtSM->RemoveNode( n, /*isNodeDeleted=*/false );
360 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)n, edges[iE], u );
363 nodesOnBoundary = subShapeIDs.count( n->getshapeId());
365 if ( nodesOnBoundary )
367 TopoDS_Shape s = helper.GetSubShapeByNode( n, tgtMesh );
368 if ( s.ShapeType() == TopAbs_VERTEX )
369 bndShapes.push_front( s ); // vertex first
371 bndShapes.push_back( s ); // edges last
374 if ( !nodesOnBoundary )
375 break; // error: free internal link
376 if ( bndShapes.front().ShapeType() == TopAbs_EDGE &&
377 bndShapes.front() != bndShapes.back() )
378 break; // error: link nodes on different geom edges
380 // find geom edge the link is on
381 if ( bndShapes.back().ShapeType() != TopAbs_EDGE )
383 // find geom edge by two vertices
384 TopoDS_Shape geomEdge;
385 PShapeIteratorPtr edgeIt = helper.GetAncestors( bndShapes.back(), theMesh, TopAbs_EDGE );
386 while ( edgeIt->more() )
388 geomEdge = *(edgeIt->next());
389 if ( !helper.IsSubShape( bndShapes.front(), geomEdge ))
392 if ( geomEdge.IsNull() )
393 break; // vertices belong to different edges -> error: free internal link
394 bndShapes.push_back( geomEdge );
397 // create an edge if not yet exists
399 newNodes[0] = link.node1(), newNodes[1] = link.node2();
400 const SMDS_MeshElement* edge = tgtMesh->FindElement( newNodes, SMDSAbs_Edge );
401 if ( edge ) continue;
403 if ( link._reversed ) std::swap( newNodes[0], newNodes[1] );
406 newNodes.push_back( link._medium );
407 edge = tgtMesh->AddEdge( newNodes[0], newNodes[1], newNodes[2] );
409 TopoDS_Edge geomEdge = TopoDS::Edge(bndShapes.back());
410 helper.CheckNodeU( geomEdge, link._medium, u, 10*faceTol, /*force=*/true );
411 tgtSM->RemoveNode( link._medium, /*isNodeDeleted=*/false );
412 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)link._medium, geomEdge, u );
416 edge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
421 tgtMesh->SetMeshElementOnShape( edge, bndShapes.back() );
423 else if ( nbFaces > 2 )
425 return error( "Non-manifold source mesh");
428 isFaceMeshed = ( link2Nb == linkCount.end() && !linkCount.empty());
431 // check that source faces do not overlap:
432 // there must be only two edges sharing each vertex and bound to sub-edges of theShape
433 SMESH_MeshEditor editor( &theMesh );
434 set<int>::iterator subID = subShapeIDs.begin();
435 for ( ; subID != subShapeIDs.end(); ++subID )
437 const TopoDS_Shape& s = tgtMesh->IndexToShape( *subID );
438 if ( s.ShapeType() != TopAbs_VERTEX ) continue;
439 const SMDS_MeshNode* n = SMESH_Algo::VertexNode( TopoDS::Vertex(s), tgtMesh );
440 SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator(SMDSAbs_Edge);
442 while ( eIt->more() )
444 const SMDS_MeshElement* edge = eIt->next();
445 int sId = editor.FindShape( edge );
446 nbEdges += subShapeIDs.count( sId );
449 return false; // weird
451 return error( "Source elements overlap one another");
456 return error( "Source elements don't cover totally the geometrical face" );
458 // notify sub-meshes of edges on computation
459 for ( unsigned iE = 0; iE < edges.size(); ++iE )
460 theMesh.GetSubMesh( edges[iE] )->ComputeStateEngine(SMESH_subMesh::CHECK_COMPUTE_STATE);
466 vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
467 for ( unsigned i = 0; i < srcMeshes.size(); ++i )
468 StdMeshers_Import_1D::importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
473 //=============================================================================
475 * \brief Set needed event listeners and create a submesh for a copied mesh
477 * This method is called only if a submesh has HYP_OK algo_state.
479 //=============================================================================
481 void StdMeshers_Import_1D2D::SetEventListener(SMESH_subMesh* subMesh)
485 const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
486 SMESH_Mesh* tgtMesh = subMesh->GetFather();
487 Hypothesis_Status aStatus;
488 CheckHypothesis( *tgtMesh, tgtShape, aStatus );
490 StdMeshers_Import_1D::setEventListener( subMesh, _sourceHyp );
492 void StdMeshers_Import_1D2D::SubmeshRestored(SMESH_subMesh* subMesh)
494 SetEventListener(subMesh);
497 //=============================================================================
499 * Predict nb of mesh entities created by Compute()
501 //=============================================================================
503 bool StdMeshers_Import_1D2D::Evaluate(SMESH_Mesh & theMesh,
504 const TopoDS_Shape & theShape,
505 MapShapeNbElems& aResMap)
507 if ( !_sourceHyp ) return false;
509 const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
510 if ( srcGroups.empty() )
511 return error("Invalid source groups");
513 vector<int> aVec(SMDSEntity_Last,0);
515 bool toCopyMesh, toCopyGroups;
516 _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
517 if ( toCopyMesh ) // the whole mesh is copied
519 vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
520 for ( unsigned i = 0; i < srcMeshes.size(); ++i )
522 SMESH_subMesh* sm = StdMeshers_Import_1D::getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
523 if ( !sm || aResMap.count( sm )) continue; // already counted
524 const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
525 for (int i = 0; i < SMDSEntity_Last; i++)
526 aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
531 // std-like iterator used to get coordinates of nodes of mesh element
532 typedef SMDS_StdIterator< SMESH_TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
534 SMESH_MesherHelper helper(theMesh);
535 helper.SetSubShape(theShape);
537 const TopoDS_Face& geomFace = TopoDS::Face( theShape );
538 const double faceTol = helper.MaxTolerance( geomFace );
540 // take into account nodes on vertices
541 TopExp_Explorer exp( theShape, TopAbs_VERTEX );
542 for ( ; exp.More(); exp.Next() )
543 theMesh.GetSubMesh( exp.Current())->Evaluate( aResMap );
545 // to count now many times a link between nodes encounters,
546 // negative nb additionally means that a link is quadratic
547 map<SMESH_TLink, int> linkCount;
548 map<SMESH_TLink, int>::iterator link2Nb;
550 // count faces and nodes imported from groups
551 set<const SMDS_MeshNode* > allNodes;
553 for ( int iG = 0; iG < srcGroups.size(); ++iG )
555 const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
556 SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
557 SMDS_MeshNode *tmpNode =helper.AddNode(0,0,0);
558 while ( srcElems->more() ) // loop on group contents
560 const SMDS_MeshElement* face = srcElems->next();
561 // find out if face is located on geomEdge by projecting
562 // a gravity center of face to geomFace
564 gc = accumulate( TXyzIterator(face->nodesIterator()), TXyzIterator(), gc)/face->NbNodes();
565 tmpNode->setXYZ( gc.X(), gc.Y(), gc.Z());
566 if ( helper.CheckNodeUV( geomFace, tmpNode, uv, 10 * faceTol, /*force=*/true ))
568 ++aVec[ face->GetEntityType() ];
571 int nbConers = face->NbCornerNodes();
572 for ( int i = 0; i < face->NbNodes(); ++i )
574 const SMDS_MeshNode* n1 = face->GetNode(i);
575 allNodes.insert( n1 );
578 const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbConers );
579 link2Nb = linkCount.insert( make_pair( SMESH_TLink( n1, n2 ), 0)).first;
580 if ( (*link2Nb).second )
581 link2Nb->second += (link2Nb->second < 0 ) ? -1 : 1;
583 link2Nb->second += ( face->IsQuadratic() ) ? -1 : 1;
588 helper.GetMeshDS()->RemoveNode(tmpNode);
591 int nbNodes = allNodes.size();
594 // count nodes and edges on geom edges
597 for ( exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next() )
599 TopoDS_Edge geomEdge = TopoDS::Edge( exp.Current() );
600 SMESH_subMesh* sm = theMesh.GetSubMesh( geomEdge );
601 vector<int>& edgeVec = aResMap[sm];
602 if ( edgeVec.empty() )
604 edgeVec.resize(SMDSEntity_Last,0);
605 for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); )
607 const SMESH_TLink& link = (*link2Nb).first;
608 int nbFacesOfLink = Abs( link2Nb->second );
609 bool eraseLink = ( nbFacesOfLink != 1 );
610 if ( nbFacesOfLink == 1 )
612 if ( helper.CheckNodeU( geomEdge, link.node1(), u, 10*faceTol, /*force=*/true )&&
613 helper.CheckNodeU( geomEdge, link.node2(), u, 10*faceTol, /*force=*/true ))
615 bool isQuadratic = ( link2Nb->second < 0 );
616 ++edgeVec[ isQuadratic ? SMDSEntity_Quad_Edge : SMDSEntity_Edge ];
617 ++edgeVec[ SMDSEntity_Node ];
623 linkCount.erase(link2Nb++);
627 if ( edgeVec[ SMDSEntity_Node] > 0 )
628 --edgeVec[ SMDSEntity_Node ]; // for one node on vertex
630 else if ( !helper.IsSeamShape( geomEdge ) ||
631 geomEdge.Orientation() == TopAbs_FORWARD )
633 nbNodes -= 1+edgeVec[ SMDSEntity_Node ];
637 aVec[SMDSEntity_Node] = nbNodes;
640 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
641 aResMap.insert(make_pair(sm,aVec));