1 // Copyright (C) 2007-2008 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // File : StdMeshers_Projection_2D.cxx
25 // Created : Fri Oct 20 11:37:07 2006
26 // Author : Edward AGAPOV (eap)
28 #include "StdMeshers_Projection_2D.hxx"
30 #include "StdMeshers_ProjectionSource2D.hxx"
31 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "SMESHDS_Hypothesis.hxx"
34 #include "SMESHDS_SubMesh.hxx"
35 #include "SMESH_Block.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_Pattern.hxx"
40 #include "SMESH_subMesh.hxx"
41 #include "SMESH_subMeshEventListener.hxx"
42 #include "SMESH_Comment.hxx"
43 #include "SMDS_EdgePosition.hxx"
45 #include "utilities.h"
47 #include <BRep_Tool.hxx>
48 #include <Bnd_B2d.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
59 typedef StdMeshers_ProjectionUtils TAssocTool;
61 //=======================================================================
62 //function : StdMeshers_Projection_2D
64 //=======================================================================
66 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
67 :SMESH_2D_Algo(hypId, studyId, gen)
69 _name = "Projection_2D";
70 _shapeType = (1 << TopAbs_FACE); // 1 bit per shape type
72 _compatibleHypothesis.push_back("ProjectionSource2D");
76 //================================================================================
80 //================================================================================
82 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
85 //=======================================================================
86 //function : CheckHypothesis
88 //=======================================================================
90 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh& theMesh,
91 const TopoDS_Shape& theShape,
92 SMESH_Hypothesis::Hypothesis_Status& theStatus)
94 list <const SMESHDS_Hypothesis * >::const_iterator itl;
96 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
97 if ( hyps.size() == 0 )
99 theStatus = HYP_MISSING;
100 return false; // can't work with no hypothesis
103 if ( hyps.size() > 1 )
105 theStatus = HYP_ALREADY_EXIST;
109 const SMESHDS_Hypothesis *theHyp = hyps.front();
111 string hypName = theHyp->GetName();
115 if (hypName == "ProjectionSource2D")
117 _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
119 // Check hypo parameters
121 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
122 SMESH_Mesh* tgtMesh = & theMesh;
127 if ( _sourceHypo->HasVertexAssociation() )
130 TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
131 ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
132 if ( edge.IsNull() ||
133 !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
134 !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
136 theStatus = HYP_BAD_PARAMETER;
137 SCRUTE((edge.IsNull()));
138 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
139 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
144 edge = TAssocTool::GetEdgeByVertices
145 ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
146 if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
148 theStatus = HYP_BAD_PARAMETER;
149 SCRUTE((edge.IsNull()));
150 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
153 else if ( !_sourceHypo->IsCompoundSource() &&
154 !SMESH_MesherHelper::IsSubShape( edge, theShape ))
156 theStatus = HYP_BAD_PARAMETER;
157 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
161 // check a source face
162 if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
163 ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
165 theStatus = HYP_BAD_PARAMETER;
166 SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
167 SCRUTE((srcMesh == tgtMesh));
168 SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
173 theStatus = HYP_INCOMPATIBLE;
175 return ( theStatus == HYP_OK );
180 //================================================================================
182 * \brief define if a node is new or old
183 * \param node - node to check
184 * \retval bool - true if the node existed before Compute() is called
186 //================================================================================
188 bool isOldNode( const SMDS_MeshNode* node )
190 // old nodes are shared by edges and new ones are shared
191 // only by faces created by mapper
192 SMDS_ElemIteratorPtr invEdge = node->GetInverseElementIterator(SMDSAbs_Edge);
193 bool isOld = invEdge->more();
197 //================================================================================
199 * \brief Class to remove mesh built by pattern mapper on edges
200 * and vertices in the case of failure of projection algo.
201 * It does it's job at destruction
203 //================================================================================
208 MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
209 ~MeshCleaner() { Clean(sm); }
210 void Release() { sm = 0; } // mesh will not be removed
211 static void Clean( SMESH_subMesh* sm, bool withSub=true )
214 // PAL16567, 18920. Remove face nodes as well
215 // switch ( sm->GetSubShape().ShapeType() ) {
216 // case TopAbs_VERTEX:
217 // case TopAbs_EDGE: {
218 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
219 SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
220 while ( nIt->more() ) {
221 const SMDS_MeshNode* node = nIt->next();
222 if ( !isOldNode( node ) )
223 mesh->RemoveNode( node );
225 // do not break but iterate over DependsOn()
228 if ( !withSub ) return;
229 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
230 while ( smIt->more() )
231 Clean( smIt->next(), false );
236 //================================================================================
238 * \brief find new nodes belonging to one free border of mesh on face
239 * \param sm - submesh on edge or vertex containg nodes to choose from
240 * \param face - the face bound the submesh
241 * \param u2nodes - map to fill with nodes
242 * \param seamNodes - set of found nodes
243 * \retval bool - is a success
245 //================================================================================
247 bool getBoundaryNodes ( SMESH_subMesh* sm,
248 const TopoDS_Face& face,
249 map< double, const SMDS_MeshNode* > & u2nodes,
250 set< const SMDS_MeshNode* > & seamNodes)
254 if ( !sm || !sm->GetSubMeshDS() )
255 RETURN_BAD_RESULT("Null submesh");
257 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
258 switch ( sm->GetSubShape().ShapeType() ) {
260 case TopAbs_VERTEX: {
261 while ( nIt->more() ) {
262 const SMDS_MeshNode* node = nIt->next();
263 if ( isOldNode( node ) ) continue;
264 u2nodes.insert( make_pair( 0., node ));
265 seamNodes.insert( node );
272 // Get submeshes of sub-vertices
273 const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
274 if ( subSM.size() != 2 )
275 RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
276 " but we have " << subSM.size());
277 SMESH_subMesh* smV1 = subSM.begin()->second;
278 SMESH_subMesh* smV2 = subSM.rbegin()->second;
279 if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
280 RETURN_BAD_RESULT("Empty vertex submeshes");
282 // Look for a new node on V1
283 nIt = smV1->GetSubMeshDS()->GetNodes();
284 const SMDS_MeshNode* nV1 = 0;
285 while ( nIt->more() && !nV1 ) {
286 const SMDS_MeshNode* node = nIt->next();
287 if ( !isOldNode( node ) ) nV1 = node;
290 RETURN_BAD_RESULT("No new node found on V1");
292 // Find a new node connected to nV1 and belonging to edge submesh;
293 const SMDS_MeshNode* nE = 0;
294 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
295 SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
296 while ( vElems->more() && !nE ) {
297 const SMDS_MeshElement* elem = vElems->next();
298 int nbNodes = elem->NbNodes();
299 if ( elem->IsQuadratic() )
301 int iV1 = elem->GetNodeIndex( nV1 );
302 // try next after nV1
303 int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
304 if ( smDS->Contains( elem->GetNode( iE ) ))
305 nE = elem->GetNode( iE );
307 // try node before nV1
308 iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
309 if ( smDS->Contains( elem->GetNode( iE )))
310 nE = elem->GetNode( iE );
312 if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
313 if ( Abs( iV1 - iE ) == 1 )
314 nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
316 nE = elem->GetNode( elem->NbNodes() - 1 );
320 RETURN_BAD_RESULT("new node on edge not found");
322 // Get the whole free border of a face
323 list< const SMDS_MeshNode* > bordNodes;
324 list< const SMDS_MeshElement* > bordFaces;
325 if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
326 RETURN_BAD_RESULT("free border of a face not found by nodes " <<
327 nV1->GetID() << " " << nE->GetID() );
329 // Insert nodes of the free border to the map until node on V2 encountered
330 SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
331 list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
332 bordIt++; // skip nV1
333 for ( ; bordIt != bordNodes.end(); ++bordIt ) {
334 const SMDS_MeshNode* node = *bordIt;
335 if ( v2smDS->Contains( node ))
337 if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
338 RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
339 " pos type " << node->GetPosition()->GetTypeOfPosition());
340 const SMDS_EdgePosition* pos =
341 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
342 u2nodes.insert( make_pair( pos->GetUParameter(), node ));
343 seamNodes.insert( node );
345 if ( u2nodes.size() != seamNodes.size() )
346 RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
347 ", " << u2nodes.size() << " != " << seamNodes.size() );
352 RETURN_BAD_RESULT ("Unexpected submesh type");
354 } // bool getBoundaryNodes()
356 //================================================================================
358 * \brief Preform projection in case if tgtFace.IsPartner( srcFace )
359 * \param tgtFace - target face
360 * \param srcFace - source face
361 * \param tgtMesh - target mesh
362 * \param srcMesh - source mesh
363 * \retval bool - true if succeeded
365 //================================================================================
367 bool projectPartner(const TopoDS_Face& tgtFace,
368 const TopoDS_Face& srcFace,
369 SMESH_Mesh * tgtMesh,
370 SMESH_Mesh * srcMesh,
371 const TAssocTool::TShapeShapeMap& shape2ShapeMap)
373 if ( !tgtFace.IsPartner( srcFace ))
376 // Fill map of src to tgt nodes with nodes on edges
378 map<const SMDS_MeshNode* , const SMDS_MeshNode*> src2tgtNodes;
379 map<const SMDS_MeshNode* , const SMDS_MeshNode*>::iterator srcN_tgtN;
381 for ( TopExp_Explorer srcEdge( srcFace, TopAbs_EDGE); srcEdge.More(); srcEdge.Next() )
383 const TopoDS_Shape& tgtEdge = shape2ShapeMap( srcEdge.Current() );
384 if ( !tgtEdge.IsPartner( srcEdge.Current() ))
387 map< double, const SMDS_MeshNode* > srcNodes, tgtNodes;
388 if ( !SMESH_Algo::GetSortedNodesOnEdge( srcMesh->GetMeshDS(),
389 TopoDS::Edge( srcEdge.Current() ),
390 /*ignoreMediumNodes = */true,
393 !SMESH_Algo::GetSortedNodesOnEdge( tgtMesh->GetMeshDS(),
394 TopoDS::Edge( tgtEdge ),
395 /*ignoreMediumNodes = */true,
398 srcNodes.size() != tgtNodes.size())
401 map< double, const SMDS_MeshNode* >::iterator u_tn = tgtNodes.begin();
402 map< double, const SMDS_MeshNode* >::iterator u_sn = srcNodes.begin();
403 for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
404 src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
409 // transformation to get location of target nodes from source ones
410 gp_Trsf srcTrsf = srcFace.Location();
411 gp_Trsf tgtTrsf = tgtFace.Location();
412 gp_Trsf trsf = srcTrsf.Inverted() * tgtTrsf;
414 // prepare the helper adding quadratic elements if necessary
415 SMESH_MesherHelper helper( *tgtMesh );
416 helper.IsQuadraticSubMesh( tgtFace );
417 helper.SetElementsOnShape( true );
419 const SMDS_MeshNode* nullNode = 0;
421 SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
422 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
423 while ( elemIt->more() ) // loop on all mesh faces on srcFace
425 const SMDS_MeshElement* elem = elemIt->next();
426 vector< const SMDS_MeshNode* > tgtFaceNodes;
427 tgtFaceNodes.reserve( elem->NbNodes() );
428 SMDS_ElemIteratorPtr nodeIt = elem->nodesIterator();
429 while ( nodeIt->more() ) // loop on nodes of the source element
431 const SMDS_MeshNode* srcNode = (const SMDS_MeshNode*) nodeIt->next();
432 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
433 if ( srcN_tgtN->second == nullNode )
436 gp_Pnt tgtP = gp_Pnt(srcNode->X(),srcNode->Y(),srcNode->Z()).Transformed( trsf );
437 srcN_tgtN->second = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
439 tgtFaceNodes.push_back( srcN_tgtN->second );
441 // create a new face (with reversed orientation)
442 if ( tgtFaceNodes.size() == 3 )
443 helper.AddFace( tgtFaceNodes[0],tgtFaceNodes[2],tgtFaceNodes[1]);
445 helper.AddFace( tgtFaceNodes[0],tgtFaceNodes[3],tgtFaceNodes[2],tgtFaceNodes[1]);
453 //=======================================================================
456 //=======================================================================
458 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
463 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
464 SMESH_Mesh * tgtMesh = & theMesh;
468 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
470 // ---------------------------
471 // Make subshapes association
472 // ---------------------------
474 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
475 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
477 TAssocTool::TShapeShapeMap shape2ShapeMap;
478 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap, tgtFace );
479 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
481 !shape2ShapeMap.IsBound( tgtFace ))
482 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
484 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
486 // ----------------------------------------------
487 // Assure that mesh on a source Face is computed
488 // ----------------------------------------------
490 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
491 SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
493 if ( tgtMesh == srcMesh ) {
494 if ( !TAssocTool::MakeComputed( srcSubMesh ))
495 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
498 if ( !srcSubMesh->IsMeshComputed() )
499 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
502 // try to project from same face with different location
503 if ( projectPartner( tgtFace, srcFace, tgtMesh, srcMesh, shape2ShapeMap ))
506 // --------------------
507 // Prepare to mapping
508 // --------------------
510 SMESH_MesherHelper helper( theMesh );
511 helper.SetSubShape( tgtFace );
513 // Check if node projection to a face is needed
515 SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
517 for ( ; nbFaceNodes < 3 && faceIt->more(); ) {
518 const SMDS_MeshElement* face = faceIt->next();
519 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
520 while ( nodeIt->more() ) {
521 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
522 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
524 uvBox.Add( helper.GetNodeUV( srcFace, node ));
528 const bool toProjectNodes =
529 ( nbFaceNodes > 0 && ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN ));
531 // Load pattern from the source face
532 SMESH_Pattern mapper;
533 mapper.Load( srcMesh, srcFace, toProjectNodes );
534 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
535 return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
537 // Find the first target vertex corresponding to first vertex of the <mapper>
538 // and <theReverse> flag needed to call mapper.Apply()
540 TopoDS_Vertex srcV1 = TopoDS::Vertex( mapper.GetSubShape( 1 ));
541 if ( srcV1.IsNull() )
542 RETURN_BAD_RESULT("Mesh is not bound to the face");
543 if ( !shape2ShapeMap.IsBound( srcV1 ))
544 RETURN_BAD_RESULT("Not associated vertices, srcV1 " << srcV1.TShape().operator->() );
545 TopoDS_Vertex tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1 ));
547 if ( !SMESH_MesherHelper::IsSubShape( srcV1, srcFace ))
548 RETURN_BAD_RESULT("Wrong srcV1 " << srcV1.TShape().operator->());
549 if ( !SMESH_MesherHelper::IsSubShape( tgtV1, tgtFace ))
550 RETURN_BAD_RESULT("Wrong tgtV1 " << tgtV1.TShape().operator->());
552 // try to find out orientation by order of edges
553 bool reverse = false;
554 list< TopoDS_Edge > tgtEdges, srcEdges;
555 list< int > nbEdgesInWires;
556 SMESH_Block::GetOrderedEdges( tgtFace, tgtV1, tgtEdges, nbEdgesInWires);
557 SMESH_Block::GetOrderedEdges( srcFace, srcV1, srcEdges, nbEdgesInWires);
558 if ( nbEdgesInWires.front() > 1 ) // possible to find out
560 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
561 TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
562 reverse = ( ! srcE1.IsSame( srcE1bis ));
564 else if ( nbEdgesInWires.front() == 1 )
566 // TODO::Compare orientation of curves in a sole edge
567 //RETURN_BAD_RESULT("Not implemented case");
571 RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
574 // --------------------
575 // Perform 2D mapping
576 // --------------------
578 // Compute mesh on a target face
580 mapper.Apply( tgtFace, tgtV1, reverse );
581 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
582 return error("Can't apply source mesh pattern to the face");
586 const bool toCreatePolygons = false, toCreatePolyedrs = false;
587 mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
588 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
589 return error("Can't make mesh by source mesh pattern");
591 // it will remove mesh built by pattern mapper on edges and vertices
593 MeshCleaner cleaner( tgtSubMesh );
595 // -------------------------------------------------------------------------
596 // mapper doesn't take care of nodes already existing on edges and vertices,
597 // so we must merge nodes created by it with existing ones
598 // -------------------------------------------------------------------------
600 SMESH_MeshEditor editor( tgtMesh );
601 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
603 // Make groups of nodes to merge
605 // loop on edge and vertex submeshes of a target face
606 SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(false,false);
607 while ( smIt->more() )
609 SMESH_subMesh* sm = smIt->next();
610 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
612 // Sort new and old nodes of a submesh separately
614 bool isSeam = helper.IsRealSeam( sm->GetId() );
616 enum { NEW_NODES = 0, OLD_NODES };
617 map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
618 map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
619 set< const SMDS_MeshNode* > seamNodes;
621 // mapper puts on a seam edge nodes from 2 edges
622 if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
623 RETURN_BAD_RESULT("getBoundaryNodes() failed");
625 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
626 while ( nIt->more() )
628 const SMDS_MeshNode* node = nIt->next();
629 bool isOld = isOldNode( node );
631 if ( !isOld && isSeam ) { // new node on a seam edge
632 if ( seamNodes.find( node ) != seamNodes.end())
633 continue; // node is already in the map
636 // sort nodes on edges by their position
637 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
638 switch ( node->GetPosition()->GetTypeOfPosition() )
640 case SMDS_TOP_VERTEX: {
641 pos2nodes.insert( make_pair( 0, node ));
644 case SMDS_TOP_EDGE: {
645 const SMDS_EdgePosition* pos =
646 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
647 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
651 RETURN_BAD_RESULT("Wrong node position type: "<<
652 node->GetPosition()->GetTypeOfPosition());
655 if ( u2nodesMaps[ NEW_NODES ].size() != u2nodesMaps[ OLD_NODES ].size() )
657 if ( u2nodesMaps[ NEW_NODES ].size() == 0 &&
658 sm->GetSubShape().ShapeType() == TopAbs_EDGE &&
659 helper.IsDegenShape( sm->GetId() ) )
660 // NPAL15894 (tt88bis.py) - project mesh built by NETGEN_1d_2D that
661 // does not make segments/nodes on degenerated edges
664 RETURN_BAD_RESULT("Different nb of old and new nodes on shape #"<< sm->GetId() <<" "<<
665 u2nodesMaps[ OLD_NODES ].size() << " != " <<
666 u2nodesMaps[ NEW_NODES ].size());
668 if ( isSeam && u2nodesMaps[ OLD_NODES ].size() != u2nodesOnSeam.size() ) {
669 RETURN_BAD_RESULT("Different nb of old and seam nodes " <<
670 u2nodesMaps[ OLD_NODES ].size() << " != " << u2nodesOnSeam.size());
672 // Make groups of nodes to merge
673 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
674 u_newNode = u2nodesMaps[ NEW_NODES ].begin();
675 newEnd = u2nodesMaps[ NEW_NODES ].end();
676 u_newOnSeam = u2nodesOnSeam.begin();
677 for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode ) {
678 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
679 groupsOfNodes.back().push_back( u_oldNode->second );
680 groupsOfNodes.back().push_back( u_newNode->second );
682 groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
688 int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
689 editor.MergeNodes( groupsOfNodes );
690 int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
691 if ( nbFaceBeforeMerge != nbFaceAtferMerge )
692 return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
694 // ---------------------------
695 // Check elements orientation
696 // ---------------------------
698 TopoDS_Face face = tgtFace;
699 if ( !theMesh.IsMainShape( tgtFace ))
701 // find the main shape
702 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
703 switch ( mainShape.ShapeType() ) {
705 case TopAbs_SOLID: break;
707 TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
708 for ( ; ancestIt.More(); ancestIt.Next() ) {
709 TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
710 if ( type == TopAbs_SOLID ) {
711 mainShape = ancestIt.Value();
713 } else if ( type == TopAbs_SHELL ) {
714 mainShape = ancestIt.Value();
718 // find tgtFace in the main solid or shell to know it's true orientation.
719 TopExp_Explorer exp( mainShape, TopAbs_FACE );
720 for ( ; exp.More(); exp.Next() ) {
721 if ( tgtFace.IsSame( exp.Current() )) {
722 face = TopoDS::Face( exp.Current() );
728 if ( SMESH_Algo::IsReversedSubMesh( face, meshDS ))
730 SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
731 while ( eIt->more() ) {
732 const SMDS_MeshElement* e = eIt->next();
733 if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
734 RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
738 cleaner.Release(); // do not remove mesh
744 //=======================================================================
745 //function : Evaluate
747 //=======================================================================
749 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
750 const TopoDS_Shape& theShape,
751 MapShapeNbElems& aResMap)
756 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
757 SMESH_Mesh * tgtMesh = & theMesh;
761 // ---------------------------
762 // Make subshapes association
763 // ---------------------------
765 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
766 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
768 TAssocTool::TShapeShapeMap shape2ShapeMap;
769 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap, tgtFace );
770 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
772 !shape2ShapeMap.IsBound( tgtFace ))
773 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
775 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
777 // ----------------------------------------------
778 // Assure that mesh on a source Face is computed
779 // ----------------------------------------------
781 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
783 if ( !srcSubMesh->IsMeshComputed() )
784 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
787 std::vector<int> aVec(SMDSEntity_Last);
788 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
790 aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
792 //bool quadratic = false;
793 SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
794 while ( elemIt->more() ) {
795 const SMDS_MeshElement* E = elemIt->next();
796 if( E->NbNodes()==3 ) {
797 aVec[SMDSEntity_Triangle]++;
799 else if( E->NbNodes()==4 ) {
800 aVec[SMDSEntity_Quadrangle]++;
802 else if( E->NbNodes()==6 && E->IsQuadratic() ) {
803 aVec[SMDSEntity_Quad_Triangle]++;
805 else if( E->NbNodes()==8 && E->IsQuadratic() ) {
806 aVec[SMDSEntity_Quad_Quadrangle]++;
809 aVec[SMDSEntity_Polygon]++;
813 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
814 aResMap.insert(std::make_pair(sm,aVec));
820 //=============================================================================
822 * \brief Sets a default event listener to submesh of the source face
823 * \param subMesh - submesh where algo is set
825 * This method is called when a submesh gets HYP_OK algo_state.
826 * After being set, event listener is notified on each event of a submesh.
827 * Arranges that CLEAN event is translated from source submesh to
830 //=============================================================================
832 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
834 TAssocTool::SetEventListener( subMesh,
835 _sourceHypo->GetSourceFace(),
836 _sourceHypo->GetSourceMesh() );