1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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_Projection_2D.cxx
26 // Created : Fri Oct 20 11:37:07 2006
27 // Author : Edward AGAPOV (eap)
29 #include "StdMeshers_Projection_2D.hxx"
31 #include "StdMeshers_ProjectionSource2D.hxx"
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "StdMeshers_FaceSide.hxx"
35 #include "SMDS_EdgePosition.hxx"
36 #include "SMDS_FacePosition.hxx"
37 #include "SMESHDS_Hypothesis.hxx"
38 #include "SMESHDS_SubMesh.hxx"
39 #include "SMESH_Block.hxx"
40 #include "SMESH_Comment.hxx"
41 #include "SMESH_Gen.hxx"
42 #include "SMESH_Mesh.hxx"
43 #include "SMESH_MesherHelper.hxx"
44 #include "SMESH_Pattern.hxx"
45 #include "SMESH_subMesh.hxx"
46 #include "SMESH_subMeshEventListener.hxx"
48 #include "utilities.h"
50 #include <BRepAdaptor_Surface.hxx>
51 #include <BRep_Tool.hxx>
52 #include <Bnd_B2d.hxx>
53 #include <GeomAPI_ProjectPointOnSurf.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
65 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
67 namespace TAssocTool = StdMeshers_ProjectionUtils;
68 //typedef StdMeshers_ProjectionUtils TAssocTool;
70 //=======================================================================
71 //function : StdMeshers_Projection_2D
73 //=======================================================================
75 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
76 :SMESH_2D_Algo(hypId, studyId, gen)
78 _name = "Projection_2D";
79 _compatibleHypothesis.push_back("ProjectionSource2D");
83 //================================================================================
87 //================================================================================
89 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
92 //=======================================================================
93 //function : CheckHypothesis
95 //=======================================================================
97 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh& theMesh,
98 const TopoDS_Shape& theShape,
99 SMESH_Hypothesis::Hypothesis_Status& theStatus)
101 list <const SMESHDS_Hypothesis * >::const_iterator itl;
103 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
104 if ( hyps.size() == 0 )
106 theStatus = HYP_MISSING;
107 return false; // can't work with no hypothesis
110 if ( hyps.size() > 1 )
112 theStatus = HYP_ALREADY_EXIST;
116 const SMESHDS_Hypothesis *theHyp = hyps.front();
118 string hypName = theHyp->GetName();
122 if (hypName == "ProjectionSource2D")
124 _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
126 // Check hypo parameters
128 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
129 SMESH_Mesh* tgtMesh = & theMesh;
134 if ( _sourceHypo->HasVertexAssociation() )
137 TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
138 ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
139 if ( edge.IsNull() ||
140 !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
141 !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
143 theStatus = HYP_BAD_PARAMETER;
144 error("Invalid source vertices");
145 SCRUTE((edge.IsNull()));
146 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
147 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
152 edge = TAssocTool::GetEdgeByVertices
153 ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
154 if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
156 theStatus = HYP_BAD_PARAMETER;
157 error("Invalid target vertices");
158 SCRUTE((edge.IsNull()));
159 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
162 else if ( !_sourceHypo->IsCompoundSource() &&
163 !SMESH_MesherHelper::IsSubShape( edge, theShape ))
165 theStatus = HYP_BAD_PARAMETER;
166 error("Invalid target vertices");
167 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
171 // check a source face
172 if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
173 ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
175 theStatus = HYP_BAD_PARAMETER;
176 error("Invalid source face");
177 SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
178 SCRUTE((srcMesh == tgtMesh));
179 SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
184 theStatus = HYP_INCOMPATIBLE;
186 return ( theStatus == HYP_OK );
191 //================================================================================
193 * \brief define if a node is new or old
194 * \param node - node to check
195 * \retval bool - true if the node existed before Compute() is called
197 //================================================================================
199 bool isOldNode( const SMDS_MeshNode* node )
201 // old nodes are shared by edges and new ones are shared
202 // only by faces created by mapper
203 //if ( is1DComputed )
205 bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
210 // SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
211 // bool isNew = invFace->more();
216 //================================================================================
218 * \brief Class to remove mesh built by pattern mapper on edges
219 * and vertices in the case of failure of projection algo.
220 * It does it's job at destruction
222 //================================================================================
227 MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
228 ~MeshCleaner() { Clean(sm); }
229 void Release() { sm = 0; } // mesh will not be removed
230 static void Clean( SMESH_subMesh* sm, bool withSub=true )
232 if ( !sm || !sm->GetSubMeshDS() ) return;
233 // PAL16567, 18920. Remove face nodes as well
234 // switch ( sm->GetSubShape().ShapeType() ) {
235 // case TopAbs_VERTEX:
236 // case TopAbs_EDGE: {
237 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
238 SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
239 while ( nIt->more() ) {
240 const SMDS_MeshNode* node = nIt->next();
241 if ( !isOldNode( node ) )
242 mesh->RemoveNode( node );
244 // do not break but iterate over DependsOn()
247 if ( !withSub ) return;
248 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
249 while ( smIt->more() )
250 Clean( smIt->next(), false );
255 //================================================================================
257 * \brief find new nodes belonging to one free border of mesh on face
258 * \param sm - submesh on edge or vertex containg nodes to choose from
259 * \param face - the face bound by the submesh
260 * \param u2nodes - map to fill with nodes
261 * \param seamNodes - set of found nodes
262 * \retval bool - is a success
264 //================================================================================
266 bool getBoundaryNodes ( SMESH_subMesh* sm,
267 const TopoDS_Face& face,
268 map< double, const SMDS_MeshNode* > & u2nodes,
269 set< const SMDS_MeshNode* > & seamNodes)
273 if ( !sm || !sm->GetSubMeshDS() )
274 RETURN_BAD_RESULT("Null submesh");
276 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
277 switch ( sm->GetSubShape().ShapeType() ) {
279 case TopAbs_VERTEX: {
280 while ( nIt->more() ) {
281 const SMDS_MeshNode* node = nIt->next();
282 if ( isOldNode( node ) ) continue;
283 u2nodes.insert( make_pair( 0., node ));
284 seamNodes.insert( node );
291 // Get submeshes of sub-vertices
292 const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
293 if ( subSM.size() != 2 )
294 RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
295 " but we have " << subSM.size());
296 SMESH_subMesh* smV1 = subSM.begin()->second;
297 SMESH_subMesh* smV2 = subSM.rbegin()->second;
298 if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
299 RETURN_BAD_RESULT("Empty vertex submeshes");
301 const SMDS_MeshNode* nV1 = 0;
302 const SMDS_MeshNode* nE = 0;
304 // Look for nV1 - a new node on V1
305 nIt = smV1->GetSubMeshDS()->GetNodes();
306 while ( nIt->more() && !nE ) {
307 const SMDS_MeshNode* node = nIt->next();
308 if ( isOldNode( node ) ) continue;
311 // Find nE - a new node connected to nV1 and belonging to edge submesh;
312 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
313 SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
314 while ( vElems->more() && !nE ) {
315 const SMDS_MeshElement* elem = vElems->next();
316 int nbNodes = elem->NbNodes();
317 if ( elem->IsQuadratic() )
319 int iV1 = elem->GetNodeIndex( nV1 );
320 // try next after nV1
321 int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
322 if ( smDS->Contains( elem->GetNode( iE ) ))
323 nE = elem->GetNode( iE );
325 // try node before nV1
326 iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
327 if ( smDS->Contains( elem->GetNode( iE )))
328 nE = elem->GetNode( iE );
330 if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
331 if ( Abs( iV1 - iE ) == 1 )
332 nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
334 nE = elem->GetNode( elem->NbNodes() - 1 );
339 RETURN_BAD_RESULT("No new node found on V1");
341 RETURN_BAD_RESULT("new node on edge not found");
343 // Get the whole free border of a face
344 list< const SMDS_MeshNode* > bordNodes;
345 list< const SMDS_MeshElement* > bordFaces;
346 if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
347 RETURN_BAD_RESULT("free border of a face not found by nodes " <<
348 nV1->GetID() << " " << nE->GetID() );
350 // Insert nodes of the free border to the map until node on V2 encountered
351 SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
352 list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
353 bordIt++; // skip nV1
354 for ( ; bordIt != bordNodes.end(); ++bordIt ) {
355 const SMDS_MeshNode* node = *bordIt;
356 if ( v2smDS->Contains( node ))
358 if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
359 RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
360 " pos type " << node->GetPosition()->GetTypeOfPosition());
361 const SMDS_EdgePosition* pos =
362 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
363 u2nodes.insert( make_pair( pos->GetUParameter(), node ));
364 seamNodes.insert( node );
366 if ( u2nodes.size() != seamNodes.size() )
367 RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
368 ", " << u2nodes.size() << " != " << seamNodes.size() );
373 RETURN_BAD_RESULT ("Unexpected submesh type");
375 } // bool getBoundaryNodes()
377 //================================================================================
379 * \brief Check if two consecutive EDGEs are connected in 2D
380 * \param [in] E1 - a well oriented non-seam EDGE
381 * \param [in] E2 - a possibly well oriented seam EDGE
382 * \param [in] F - a FACE
383 * \return bool - result
385 //================================================================================
387 bool are2dConnected( const TopoDS_Edge & E1,
388 const TopoDS_Edge & E2,
389 const TopoDS_Face & F )
392 Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( E1, F, f, l );
393 gp_Pnt2d uvLast1 = c1->Value( E1.Orientation() == TopAbs_REVERSED ? f : l );
395 Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( E2, F, f, l );
396 gp_Pnt2d uvFirst2 = c2->Value( f );
397 gp_Pnt2d uvLast2 = c2->Value( l );
398 double tol2 = 1e-5 * uvLast2.SquareDistance( uvFirst2 );
400 return (( uvLast1.SquareDistance( uvFirst2 ) < tol2 ) ||
401 ( uvLast1.SquareDistance( uvLast2 ) < tol2 ));
404 //================================================================================
406 * \brief Compose TSideVector for both FACEs keeping matching order of EDGEs
407 * and fill src2tgtNodes map
409 //================================================================================
411 TError getWires(const TopoDS_Face& tgtFace,
412 const TopoDS_Face& srcFace,
413 SMESH_Mesh * tgtMesh,
414 SMESH_Mesh * srcMesh,
415 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
416 TSideVector& srcWires,
417 TSideVector& tgtWires,
418 TAssocTool::TNodeNodeMap& src2tgtNodes,
421 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
422 SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
424 src2tgtNodes.clear();
426 // get ordered src EDGEs
428 srcWires = StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*skipMediumNodes=*/0, err);
429 if ( err && !err->IsOK() )
432 // make corresponding sequence of tgt EDGEs
433 tgtWires.resize( srcWires.size() );
434 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
436 list< TopoDS_Edge > tgtEdges;
437 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
438 TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
439 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
441 TopoDS_Edge srcE = srcWire->Edge( iE );
442 TopoDS_Edge tgtE = TopoDS::Edge( shape2ShapeMap( srcE, /*isSrc=*/true));
443 // reverse a seam edge encountered for the second time
444 const int index = edgeMap.Add( tgtE );
445 if ( index < edgeMap.Extent() ) // E is a seam
447 // check which of edges to reverse, E or one already being in tgtEdges
448 if ( are2dConnected( tgtEdges.back(), tgtE, tgtFace ))
450 list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
451 std::advance( eIt, index-1 );
459 if ( srcWire->NbEdges() == 1 && tgtMesh == srcMesh ) // circle
461 // try to verify ori by propagation
462 pair<int,TopoDS_Edge> nE =
463 StdMeshers_ProjectionUtils::GetPropagationEdge( srcMesh, tgtE, srcE );
464 if ( !nE.second.IsNull() )
467 tgtEdges.push_back( tgtE );
470 // Fill map of src to tgt nodes with nodes on edges
472 if ( srcMesh->GetSubMesh( srcE )->IsEmpty() ||
473 tgtMesh->GetSubMesh( tgtE )->IsEmpty() )
475 // add nodes on VERTEXes for a case of not meshes EDGEs
476 const TopoDS_Shape& srcV = SMESH_MesherHelper::IthVertex( 0, srcE );
477 const TopoDS_Shape& tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
478 const SMDS_MeshNode* srcN = SMESH_Algo::VertexNode( TopoDS::Vertex( srcV ), srcMeshDS );
479 const SMDS_MeshNode* tgtN = SMESH_Algo::VertexNode( TopoDS::Vertex( tgtV ), tgtMeshDS );
481 src2tgtNodes.insert( make_pair( srcN, tgtN ));
485 map< double, const SMDS_MeshNode* > srcNodes, tgtNodes;
486 if (( ! SMESH_Algo::GetSortedNodesOnEdge( srcMeshDS, TopoDS::Edge( srcE ),
487 /*ignoreMediumNodes = */true,
490 ( ! SMESH_Algo::GetSortedNodesOnEdge( tgtMeshDS, TopoDS::Edge( tgtE ),
491 /*ignoreMediumNodes = */true,
494 return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
495 "Invalid node parameters on edges");
497 if (( srcNodes.size() != tgtNodes.size() ) && tgtNodes.size() > 0 )
498 return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
499 "Different number of nodes on edges");
500 if ( !tgtNodes.empty() )
502 map< double, const SMDS_MeshNode* >::iterator u_tn = tgtNodes.begin();
503 map< double, const SMDS_MeshNode* >::iterator u_sn = srcNodes.begin();
504 for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
505 src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
510 } // loop on EDGEs of a WIRE
512 tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
513 /*theIsForward = */ true,
514 /*theIgnoreMediumNodes = */false));
520 //================================================================================
522 * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
523 * if projection by 3D transformation is possible
525 //================================================================================
527 bool projectPartner(const TopoDS_Face& tgtFace,
528 const TopoDS_Face& srcFace,
529 const TSideVector& tgtWires,
530 const TSideVector& srcWires,
531 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
532 TAssocTool::TNodeNodeMap& src2tgtNodes,
533 const bool is1DComputed)
535 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
536 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
537 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
538 SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
539 SMESH_MesherHelper helper( *tgtMesh );
541 const double tol = 1.e-7 * srcMeshDS->getMaxDim();
543 // transformation to get location of target nodes from source ones
544 StdMeshers_ProjectionUtils::TrsfFinder3D trsf;
545 if ( tgtFace.IsPartner( srcFace ))
547 gp_Trsf srcTrsf = srcFace.Location();
548 gp_Trsf tgtTrsf = tgtFace.Location();
549 trsf.Set( srcTrsf.Inverted() * tgtTrsf );
553 // Try to find the 3D transformation
555 const int totNbSeg = 50;
556 vector< gp_XYZ > srcPnts, tgtPnts;
557 srcPnts.reserve( totNbSeg );
558 tgtPnts.reserve( totNbSeg );
559 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
561 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
562 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
564 int nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
565 double srcU = srcWires[iW]->FirstParameter( iE );
566 double tgtU = tgtWires[iW]->FirstParameter( iE );
567 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
568 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
569 for ( size_t i = 0; i < nbSeg; ++i )
571 srcPnts.push_back( srcWires[iW]->Value3d( srcU ).XYZ() );
572 tgtPnts.push_back( tgtWires[iW]->Value3d( tgtU ).XYZ() );
578 if ( !trsf.Solve( srcPnts, tgtPnts ))
583 bool trsfIsOK = true;
584 const int nbTestPnt = 20;
585 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
587 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
589 gp_Pnt trsfTgt = trsf.Transform( srcPnts[i] );
590 trsfIsOK = ( trsfTgt.SquareDistance( tgtPnts[i] ) < tol*tol );
592 // check an in-FACE point
595 BRepAdaptor_Surface srcSurf( srcFace );
597 srcSurf.Value( 0.5 * ( srcSurf.FirstUParameter() + srcSurf.LastUParameter() ),
598 0.5 * ( srcSurf.FirstVParameter() + srcSurf.LastVParameter() ));
599 gp_Pnt tgtTrsfP = trsf.Transform( srcP );
601 GeomAPI_ProjectPointOnSurf& proj = helper.GetProjector( tgtFace, loc, 0.1*tol );
602 if ( !loc.IsIdentity() )
603 tgtTrsfP.Transform( loc.Transformation().Inverted() );
604 proj.Perform( tgtTrsfP );
605 trsfIsOK = ( proj.IsDone() &&
606 proj.NbPoints() > 0 &&
607 proj.LowerDistance() < tol );
615 // prepare the helper to adding quadratic elements if necessary
616 helper.SetSubShape( tgtFace );
617 helper.IsQuadraticSubMesh( tgtFace );
619 SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
620 if ( !is1DComputed && srcSubDS->NbElements() )
621 helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
623 SMESH_MesherHelper srcHelper( *srcMesh );
624 srcHelper.SetSubShape( srcFace );
626 const SMDS_MeshNode* nullNode = 0;
627 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
629 // indices of nodes to create properly oriented faces
630 bool isReverse = ( !trsf.IsIdentity() );
631 int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
633 std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
635 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
636 vector< const SMDS_MeshNode* > tgtNodes;
637 while ( elemIt->more() ) // loop on all mesh faces on srcFace
639 const SMDS_MeshElement* elem = elemIt->next();
640 const int nbN = elem->NbCornerNodes();
641 tgtNodes.resize( nbN );
642 helper.SetElementsOnShape( false );
643 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
645 const SMDS_MeshNode* srcNode = elem->GetNode(i);
646 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
647 if ( srcN_tgtN->second == nullNode )
650 gp_Pnt tgtP = trsf.Transform( SMESH_TNodeXYZ( srcNode ));
651 SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
652 srcN_tgtN->second = n;
653 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
657 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode );
658 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), srcUV.X(), srcUV.Y() );
663 const TopoDS_Shape & srcE = srcMeshDS->IndexToShape( srcNode->getshapeId() );
664 const TopoDS_Shape & tgtE = shape2ShapeMap( srcE, /*isSrc=*/true );
665 double srcU = srcHelper.GetNodeU( TopoDS::Edge( srcE ), srcNode );
666 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtE ), srcU );
669 case SMDS_TOP_VERTEX:
671 const TopoDS_Shape & srcV = srcMeshDS->IndexToShape( srcNode->getshapeId() );
672 const TopoDS_Shape & tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
673 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
679 tgtNodes[i] = srcN_tgtN->second;
682 helper.SetElementsOnShape( true );
685 case 3: helper.AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
686 case 4: helper.AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
688 if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
689 helper.AddPolygonalFace( tgtNodes );
693 // check node positions
695 if ( !tgtFace.IsPartner( srcFace ) )
698 const double tol2d = 1e-12;
699 srcN_tgtN = src2tgtNodes.begin();
700 for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
702 const SMDS_MeshNode* n = srcN_tgtN->second;
703 switch ( n->GetPosition()->GetTypeOfPosition() )
707 gp_XY uv = helper.GetNodeUV( tgtFace, n ), uvBis = uv;
708 if (( helper.CheckNodeUV( tgtFace, n, uv, tol )) &&
709 (( uv - uvBis ).SquareModulus() < tol2d ) &&
718 const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
719 double u = helper.GetNodeU( tgtE, n ), uBis = u;
720 if (( !helper.CheckNodeU( tgtE, n, u, tol )) ||
721 (( u - uBis ) < tol2d ))
732 } // bool projectPartner()
734 //================================================================================
736 * \brief Preform projection in case if the faces are similar in 2D space
738 //================================================================================
740 bool projectBy2DSimilarity(const TopoDS_Face& tgtFace,
741 const TopoDS_Face& srcFace,
742 const TSideVector& tgtWires,
743 const TSideVector& srcWires,
744 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
745 TAssocTool::TNodeNodeMap& src2tgtNodes,
746 const bool is1DComputed)
748 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
749 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
751 // WARNING: we can have problems if the FACE is symmetrical in 2D,
752 // then the projection can be mirrored relating to what is expected
754 // 1) Find 2D transformation
756 StdMeshers_ProjectionUtils::TrsfFinder2D trsf;
758 // get 2 pairs of corresponding UVs
759 gp_Pnt2d srcP0 = srcWires[0]->Value2d(0.0);
760 gp_Pnt2d srcP1 = srcWires[0]->Value2d(0.333);
761 gp_Pnt2d tgtP0 = tgtWires[0]->Value2d(0.0);
762 gp_Pnt2d tgtP1 = tgtWires[0]->Value2d(0.333);
764 // make transformation
765 gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
766 gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
767 gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
768 toSrcCS .SetTransformation( srcCS );
769 fromTgtCS.SetTransformation( tgtCS );
771 trsf.Set( fromTgtCS * toSrcCS );
773 // check transformation
774 bool trsfIsOK = true;
775 const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
776 for ( double u = 0.12; ( u < 1. && trsfIsOK ); u += 0.1 )
778 gp_Pnt2d srcUV = srcWires[0]->Value2d( u );
779 gp_Pnt2d tgtUV = tgtWires[0]->Value2d( u );
780 gp_Pnt2d tgtUV2 = trsf.Transform( srcUV );
781 trsfIsOK = ( tgtUV.Distance( tgtUV2 ) < tol );
784 // Find trsf using a least-square approximation
788 const int totNbSeg = 50;
789 vector< gp_XY > srcPnts, tgtPnts;
790 srcPnts.resize( totNbSeg );
791 tgtPnts.resize( totNbSeg );
792 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
794 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
795 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
797 int nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
798 double srcU = srcWires[iW]->FirstParameter( iE );
799 double tgtU = tgtWires[iW]->FirstParameter( iE );
800 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
801 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
802 for ( size_t i = 0; i < nbSeg; ++i, srcU += srcDu, tgtU += tgtDu )
804 srcPnts.push_back( srcWires[iW]->Value2d( srcU ).XY() );
805 tgtPnts.push_back( tgtWires[iW]->Value2d( tgtU ).XY() );
809 if ( !trsf.Solve( srcPnts, tgtPnts ))
815 const int nbTestPnt = 10;
816 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
817 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
819 gp_Pnt2d trsfTgt = trsf.Transform( srcPnts[i] );
820 trsfIsOK = ( trsfTgt.Distance( tgtPnts[i] ) < tol );
825 } // "Find transformation" block
829 SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
831 SMESH_MesherHelper helper( *tgtMesh );
832 helper.SetSubShape( tgtFace );
834 helper.IsQuadraticSubMesh( tgtFace );
836 helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
837 helper.SetElementsOnShape( true );
838 Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
839 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
841 SMESH_MesherHelper srcHelper( *srcMesh );
842 srcHelper.SetSubShape( srcFace );
844 const SMDS_MeshNode* nullNode = 0;
845 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
847 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
848 vector< const SMDS_MeshNode* > tgtNodes;
850 while ( elemIt->more() ) // loop on all mesh faces on srcFace
852 const SMDS_MeshElement* elem = elemIt->next();
853 const int nbN = elem->NbCornerNodes();
854 tgtNodes.resize( nbN );
855 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
857 const SMDS_MeshNode* srcNode = elem->GetNode(i);
858 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
859 if ( srcN_tgtN->second == nullNode )
862 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
863 elem->GetNode( helper.WrapIndex(i+1,nbN)), &uvOK);
864 gp_Pnt2d tgtUV = trsf.Transform( srcUV );
865 gp_Pnt tgtP = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
866 SMDS_MeshNode* n = tgtMeshDS->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
867 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
869 case SMDS_TOP_FACE: {
870 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
873 case SMDS_TOP_EDGE: {
874 TopoDS_Shape srcEdge = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
875 TopoDS_Edge tgtEdge = TopoDS::Edge( shape2ShapeMap( srcEdge, /*isSrc=*/true ));
876 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtEdge ));
877 double U = srcHelper.GetNodeU( TopoDS::Edge( srcEdge ), srcNode );
878 helper.CheckNodeU( tgtEdge, n, U, Precision::PConfusion());
879 n->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition( U )));
882 case SMDS_TOP_VERTEX: {
883 TopoDS_Shape srcV = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
884 TopoDS_Shape tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
885 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
889 srcN_tgtN->second = n;
891 tgtNodes[i] = srcN_tgtN->second;
893 // create a new face (with reversed orientation)
896 case 3: helper.AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
897 case 4: helper.AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
902 } // bool projectBy2DSimilarity(...)
904 //================================================================================
906 * \brief Fix bad faces by smoothing
908 //================================================================================
910 void fixDistortedFaces( SMESH_MesherHelper& helper,
915 bool haveBadFaces = false;
917 const TopoDS_Face& F = TopoDS::Face( helper.GetSubShape() );
918 SMESHDS_SubMesh* smDS = helper.GetMeshDS()->MeshElements( F );
919 if ( !smDS || smDS->NbElements() == 0 ) return;
921 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
922 double prevArea2D = 0;
923 vector< const SMDS_MeshNode* > nodes;
925 while ( faceIt->more() && !haveBadFaces )
927 const SMDS_MeshElement* face = faceIt->next();
930 nodes.resize( face->NbCornerNodes() );
931 SMDS_MeshElement::iterator n = face->begin_nodes();
932 for ( size_t i = 0; i < nodes.size(); ++n, ++i )
936 const SMDS_MeshNode* inFaceNode = 0;
937 if ( helper.HasSeam() )
938 for ( size_t i = 0; ( i < nodes.size() && !inFaceNode ); ++i )
939 if ( !helper.IsSeamShape( nodes[ i ]->getshapeId() ))
940 inFaceNode = nodes[ i ];
942 uv.resize( nodes.size() );
943 for ( size_t i = 0; i < nodes.size(); ++i )
944 uv[ i ] = helper.GetNodeUV( F, nodes[ i ], inFaceNode );
946 // compare orientation of triangles
947 for ( int iT = 0, nbT = nodes.size()-2; iT < nbT; ++iT )
949 gp_XY v1 = uv[ iT+1 ] - uv[ 0 ];
950 gp_XY v2 = uv[ iT+2 ] - uv[ 0 ];
951 double area2D = v2 ^ v1;
952 if (( haveBadFaces = ( area2D * prevArea2D < 0 )))
962 SMESH_MeshEditor editor( helper.GetMesh() );
964 TIDSortedElemSet faces;
965 for ( faceIt = smDS->GetElements(); faceIt->more(); )
966 faces.insert( faces.end(), faceIt->next() );
968 // choose smoothing algo
969 //SMESH_MeshEditor:: SmoothMethod algo = SMESH_MeshEditor::CENTROIDAL;
970 bool isConcaveBoundary = false;
972 TSideVector tgtWires =
973 StdMeshers_FaceSide::GetFaceWires( F, *helper.GetMesh(),/*skipMediumNodes=*/0, err);
974 for ( size_t iW = 0; iW < tgtWires.size() && !isConcaveBoundary; ++iW )
976 TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
977 for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
979 double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
980 F, tgtWires[iW]->FirstVertex( iE ));
981 isConcaveBoundary = ( angle < -5. * M_PI / 180. );
983 prevEdge = tgtWires[iW]->Edge( iE );
986 SMESH_MeshEditor:: SmoothMethod algo =
987 isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
990 set<const SMDS_MeshNode*> fixedNodes;
991 editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
992 /*theTgtAspectRatio=*/1.0, /*the2D=*/false);
999 //=======================================================================
1000 //function : Compute
1002 //=======================================================================
1004 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1006 _src2tgtNodes.clear();
1008 MESSAGE("Projection_2D Compute");
1012 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1013 SMESH_Mesh * tgtMesh = & theMesh;
1017 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1019 // ---------------------------
1020 // Make sub-shapes association
1021 // ---------------------------
1023 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1024 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1026 TAssocTool::TShapeShapeMap shape2ShapeMap;
1027 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1028 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1030 !shape2ShapeMap.IsBound( tgtFace ))
1032 if ( srcShape.ShapeType() == TopAbs_FACE )
1034 int nbE1 = SMESH_MesherHelper::Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1035 int nbE2 = SMESH_MesherHelper::Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1037 return error(COMPERR_BAD_SHAPE,
1038 SMESH_Comment("Different number of edges in source and target faces: ")
1039 << nbE2 << " and " << nbE1 );
1041 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1043 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1045 // ----------------------------------------------
1046 // Assure that mesh on a source Face is computed
1047 // ----------------------------------------------
1049 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1050 SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1052 string srcMeshError;
1053 if ( tgtMesh == srcMesh ) {
1054 if ( !TAssocTool::MakeComputed( srcSubMesh ))
1055 srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1058 if ( !srcSubMesh->IsMeshComputed() )
1059 srcMeshError = TAssocTool::SourceNotComputedError();
1061 if ( !srcMeshError.empty() )
1062 return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1068 // get ordered src and tgt EDGEs
1069 TSideVector srcWires, tgtWires;
1070 bool is1DComputed = false; // if any tgt EDGE is meshed
1071 TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh,
1072 shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1073 if ( err && !err->IsOK() )
1074 return error( err );
1080 // try to project from the same face with different location
1081 done = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1082 shape2ShapeMap, _src2tgtNodes, is1DComputed );
1086 // projection in case if the faces are similar in 2D space
1087 done = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1088 shape2ShapeMap, _src2tgtNodes, is1DComputed);
1091 SMESH_MesherHelper helper( theMesh );
1092 helper.SetSubShape( tgtFace );
1096 _src2tgtNodes.clear();
1097 // --------------------
1098 // Prepare to mapping
1099 // --------------------
1101 // Check if node projection to a face is needed
1103 SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1104 int nbFaceNodes = 0;
1105 for ( ; nbFaceNodes < 3 && faceIt->more(); ) {
1106 const SMDS_MeshElement* face = faceIt->next();
1107 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1108 while ( nodeIt->more() ) {
1109 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1110 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1112 uvBox.Add( helper.GetNodeUV( srcFace, node ));
1116 const bool toProjectNodes =
1117 ( nbFaceNodes > 0 && ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN ));
1119 // Find the corresponding source and target vertex
1120 // and <theReverse> flag needed to call mapper.Apply()
1122 TopoDS_Vertex srcV1, tgtV1;
1123 bool reverse = false;
1125 if ( _sourceHypo->HasVertexAssociation() ) {
1126 srcV1 = _sourceHypo->GetSourceVertex(1);
1127 tgtV1 = _sourceHypo->GetTargetVertex(1);
1129 srcV1 = TopoDS::Vertex( TopExp_Explorer( srcFace, TopAbs_VERTEX ).Current() );
1130 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1132 list< TopoDS_Edge > tgtEdges, srcEdges;
1133 list< int > nbEdgesInWires;
1134 SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1135 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1137 if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1139 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1140 TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1141 reverse = ( ! srcE1.IsSame( srcE1bis ));
1143 _sourceHypo->HasVertexAssociation() &&
1144 nbEdgesInWires.front() > 2 &&
1145 helper.IsRealSeam( tgtEdges.front() ))
1147 // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1148 // always puts a seam EDGE first (if possible) and as a result
1149 // we can't use only theReverse flag to correctly associate source
1150 // and target faces in the mapper. Thus we select srcV1 so that
1151 // GetOrderedEdges() to return EDGEs in a needed order
1152 list< TopoDS_Edge >::iterator edge = srcEdges.begin();
1153 for ( ; edge != srcEdges.end(); ++edge ) {
1154 if ( srcE1bis.IsSame( *edge )) {
1155 srcV1 = helper.IthVertex( 0, *edge );
1161 else if ( nbEdgesInWires.front() == 1 )
1163 // TODO::Compare orientation of curves in a sole edge
1164 //RETURN_BAD_RESULT("Not implemented case");
1168 RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1171 // Load pattern from the source face
1172 SMESH_Pattern mapper;
1173 mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1 );
1174 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1175 return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1177 // --------------------
1178 // Perform 2D mapping
1179 // --------------------
1181 // Compute mesh on a target face
1183 mapper.Apply( tgtFace, tgtV1, reverse );
1184 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1185 return error("Can't apply source mesh pattern to the face");
1189 const bool toCreatePolygons = false, toCreatePolyedrs = false;
1190 mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1191 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1192 return error("Can't make mesh by source mesh pattern");
1194 // it will remove mesh built by pattern mapper on edges and vertices
1196 MeshCleaner cleaner( tgtSubMesh );
1198 // -------------------------------------------------------------------------
1199 // mapper doesn't take care of nodes already existing on edges and vertices,
1200 // so we must merge nodes created by it with existing ones
1201 // -------------------------------------------------------------------------
1203 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1205 // Make groups of nodes to merge
1207 // loop on EDGE and VERTEX sub-meshes of a target FACE
1208 SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1209 /*complexShapeFirst=*/false);
1210 while ( smIt->more() )
1212 SMESH_subMesh* sm = smIt->next();
1213 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1214 if ( !smDS || smDS->NbNodes() == 0 )
1216 //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1219 if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1221 if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1223 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1224 SMESH_subMeshIteratorPtr smDegenIt
1225 = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1226 while ( smDegenIt->more() )
1227 if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1229 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1230 while ( nIt->more() )
1231 groupsOfNodes.back().push_back( nIt->next() );
1234 continue; // do not treat sm of degen VERTEX
1237 // Sort new and old nodes of a submesh separately
1239 bool isSeam = helper.IsRealSeam( sm->GetId() );
1241 enum { NEW_NODES = 0, OLD_NODES };
1242 map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1243 map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1244 set< const SMDS_MeshNode* > seamNodes;
1246 // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1247 if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1248 ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1250 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1251 while ( nIt->more() )
1253 const SMDS_MeshNode* node = nIt->next();
1254 bool isOld = isOldNode( node );
1256 if ( !isOld && isSeam ) { // new node on a seam edge
1257 if ( seamNodes.count( node ) )
1258 continue; // node is already in the map
1261 // sort nodes on edges by their position
1262 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1263 switch ( node->GetPosition()->GetTypeOfPosition() )
1265 case SMDS_TOP_VERTEX: {
1266 if ( !is1DComputed && !pos2nodes.empty() )
1267 u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1269 pos2nodes.insert( make_pair( 0, node ));
1272 case SMDS_TOP_EDGE: {
1273 const SMDS_EdgePosition* pos =
1274 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1275 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1279 RETURN_BAD_RESULT("Wrong node position type: "<<
1280 node->GetPosition()->GetTypeOfPosition());
1283 const bool mergeNewToOld =
1284 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1285 const bool mergeSeamToNew =
1286 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1288 if ( !mergeNewToOld )
1289 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1290 u2nodesMaps[ OLD_NODES ].size() > 0 )
1292 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1293 newEnd = u2nodesMaps[ OLD_NODES ].end();
1294 for ( ; u_oldNode != newEnd; ++u_oldNode )
1295 SMESH_Algo::addBadInputElement( u_oldNode->second );
1296 return error( COMPERR_BAD_INPUT_MESH,
1297 SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1298 << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1299 << " #" << sm->GetId() );
1301 if ( isSeam && !mergeSeamToNew ) {
1302 const TopoDS_Shape& seam = sm->GetSubShape();
1303 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1304 u2nodesOnSeam.size() > 0 &&
1305 seam.ShapeType() == TopAbs_EDGE )
1307 int nbE1 = SMESH_MesherHelper::Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1308 int nbE2 = SMESH_MesherHelper::Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1309 if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1311 // find the 2 EDGEs of srcFace
1312 TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1313 for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1314 if ( seam.IsSame( src2tgtIt.Value() ))
1315 SMESH_Algo::addBadInputElements
1316 ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1317 return error( COMPERR_BAD_INPUT_MESH,
1318 "Different number of nodes on two edges projected to a seam edge" );
1323 // Make groups of nodes to merge
1325 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1326 u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1327 newEnd = u2nodesMaps[ NEW_NODES ].end();
1328 u_newOnSeam = u2nodesOnSeam.begin();
1329 if ( mergeNewToOld )
1330 for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1332 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1333 groupsOfNodes.back().push_back( u_oldNode->second );
1334 groupsOfNodes.back().push_back( u_newNode->second );
1335 if ( mergeSeamToNew )
1336 groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1338 else if ( mergeSeamToNew )
1339 for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1341 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1342 groupsOfNodes.back().push_back( u_newNode->second );
1343 groupsOfNodes.back().push_back( u_newOnSeam->second );
1346 } // loop on EDGE and VERTEX submeshes of a target FACE
1350 SMESH_MeshEditor editor( tgtMesh );
1351 int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1352 editor.MergeNodes( groupsOfNodes );
1353 int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1354 if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1355 return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1358 // ----------------------------------------------------------------
1359 // The mapper can create distorted faces by placing nodes out of the FACE
1360 // boundary -- fix bad faces by smoothing
1361 // ----------------------------------------------------------------
1363 fixDistortedFaces( helper, tgtWires );
1365 // ----------------------------------------------------------------
1366 // The mapper can't create quadratic elements, so convert if needed
1367 // ----------------------------------------------------------------
1369 faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1370 bool srcIsQuad = faceIt->next()->IsQuadratic();
1371 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1372 bool tgtIsQuad = faceIt->next()->IsQuadratic();
1373 if ( srcIsQuad && !tgtIsQuad )
1375 TIDSortedElemSet tgtFaces;
1376 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1377 while ( faceIt->more() )
1378 tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1380 editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1383 cleaner.Release(); // not to remove mesh
1385 } // end of projection using Pattern mapping
1388 // ---------------------------
1389 // Check elements orientation
1390 // ---------------------------
1392 TopoDS_Face face = TopoDS::Face( theShape );
1393 if ( !theMesh.IsMainShape( tgtFace ))
1395 // find the main shape
1396 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1397 switch ( mainShape.ShapeType() ) {
1399 case TopAbs_SOLID: break;
1401 TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1402 for ( ; ancestIt.More(); ancestIt.Next() ) {
1403 TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1404 if ( type == TopAbs_SOLID ) {
1405 mainShape = ancestIt.Value();
1407 } else if ( type == TopAbs_SHELL ) {
1408 mainShape = ancestIt.Value();
1412 // find tgtFace in the main solid or shell to know it's true orientation.
1413 TopExp_Explorer exp( mainShape, TopAbs_FACE );
1414 for ( ; exp.More(); exp.Next() ) {
1415 if ( tgtFace.IsSame( exp.Current() )) {
1416 face = TopoDS::Face( exp.Current() );
1422 if ( helper.IsReversedSubMesh( face ))
1424 SMESH_MeshEditor editor( tgtMesh );
1425 SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1426 while ( eIt->more() ) {
1427 const SMDS_MeshElement* e = eIt->next();
1428 if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1429 RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1437 //=======================================================================
1438 //function : Evaluate
1440 //=======================================================================
1442 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
1443 const TopoDS_Shape& theShape,
1444 MapShapeNbElems& aResMap)
1449 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1450 SMESH_Mesh * tgtMesh = & theMesh;
1454 // ---------------------------
1455 // Make sub-shapes association
1456 // ---------------------------
1458 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1459 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1461 TAssocTool::TShapeShapeMap shape2ShapeMap;
1462 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1463 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1465 !shape2ShapeMap.IsBound( tgtFace ))
1466 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1468 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1470 // -------------------------------------------------------
1471 // Assure that mesh on a source Face is computed/evaluated
1472 // -------------------------------------------------------
1474 std::vector<int> aVec;
1476 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1477 if ( srcSubMesh->IsMeshComputed() )
1479 aVec.resize( SMDSEntity_Last, 0 );
1480 aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
1482 SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
1483 while ( elemIt->more() )
1484 aVec[ elemIt->next()->GetEntityType() ]++;
1488 MapShapeNbElems tmpResMap;
1489 MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
1490 if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
1491 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
1492 aVec = srcResMap[ srcSubMesh ];
1494 return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
1497 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1498 aResMap.insert(std::make_pair(sm,aVec));
1504 //=============================================================================
1506 * \brief Sets a default event listener to submesh of the source face
1507 * \param subMesh - submesh where algo is set
1509 * This method is called when a submesh gets HYP_OK algo_state.
1510 * After being set, event listener is notified on each event of a submesh.
1511 * Arranges that CLEAN event is translated from source submesh to
1514 //=============================================================================
1516 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
1518 TAssocTool::SetEventListener( subMesh,
1519 _sourceHypo->GetSourceFace(),
1520 _sourceHypo->GetSourceMesh() );