1 // Copyright (C) 2007-2020 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 : implementation 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_Mesh.hxx"
39 #include "SMESHDS_SubMesh.hxx"
40 #include "SMESH_Block.hxx"
41 #include "SMESH_Comment.hxx"
42 #include "SMESH_Gen.hxx"
43 #include "SMESH_Mesh.hxx"
44 #include "SMESH_MeshAlgos.hxx"
45 #include "SMESH_MeshEditor.hxx"
46 #include "SMESH_MesherHelper.hxx"
47 #include "SMESH_Pattern.hxx"
48 #include "SMESH_subMesh.hxx"
49 #include "SMESH_subMeshEventListener.hxx"
51 #include <utilities.h>
53 #include <BRepAdaptor_Curve.hxx>
54 #include <BRepAdaptor_Surface.hxx>
55 #include <BRepMesh_Delaun.hxx>
56 #include <BRep_Tool.hxx>
57 #include <Bnd_B2d.hxx>
58 #include <GeomAPI_ProjectPointOnSurf.hxx>
59 #include <GeomLib_IsPlanarSurface.hxx>
60 #include <Precision.hxx>
62 #include <TopExp_Explorer.hxx>
63 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
64 #include <TopTools_ListIteratorOfListOfShape.hxx>
65 #include <TopTools_MapOfShape.hxx>
67 #include <TopoDS_Solid.hxx>
70 #include <gp_GTrsf.hxx>
75 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
77 // enable printing algo + projection shapes while meshing
78 //#define PRINT_WHO_COMPUTE_WHAT
81 namespace TAssocTool = StdMeshers_ProjectionUtils;
82 //typedef StdMeshers_ProjectionUtils TAssocTool;
84 //=======================================================================
85 //function : StdMeshers_Projection_2D
87 //=======================================================================
89 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, SMESH_Gen* gen)
90 :SMESH_2D_Algo(hypId, gen)
92 _name = "Projection_2D";
93 _compatibleHypothesis.push_back("ProjectionSource2D");
97 //================================================================================
101 //================================================================================
103 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
106 //=======================================================================
107 //function : CheckHypothesis
109 //=======================================================================
111 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh& theMesh,
112 const TopoDS_Shape& theShape,
113 SMESH_Hypothesis::Hypothesis_Status& theStatus)
115 list <const SMESHDS_Hypothesis * >::const_iterator itl;
117 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
118 if ( hyps.size() == 0 )
120 theStatus = HYP_MISSING;
121 return false; // can't work with no hypothesis
124 if ( hyps.size() > 1 )
126 theStatus = HYP_ALREADY_EXIST;
130 const SMESHDS_Hypothesis *theHyp = hyps.front();
132 string hypName = theHyp->GetName();
136 if (hypName == "ProjectionSource2D")
138 _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
140 // Check hypo parameters
142 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
143 SMESH_Mesh* tgtMesh = & theMesh;
148 if ( _sourceHypo->HasVertexAssociation() )
151 TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
152 ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
153 if ( edge.IsNull() ||
154 !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
155 !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
157 theStatus = HYP_BAD_PARAMETER;
158 error("Invalid source vertices");
159 SCRUTE((edge.IsNull()));
160 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
161 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
166 edge = TAssocTool::GetEdgeByVertices
167 ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
168 if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
170 theStatus = HYP_BAD_PARAMETER;
171 error("Invalid target vertices");
172 SCRUTE((edge.IsNull()));
173 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
176 else if ( !_sourceHypo->IsCompoundSource() &&
177 !SMESH_MesherHelper::IsSubShape( edge, theShape ))
179 theStatus = HYP_BAD_PARAMETER;
180 error("Invalid target vertices");
181 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
185 // check a source face
186 if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
187 ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
189 theStatus = HYP_BAD_PARAMETER;
190 error("Invalid source face");
191 SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
192 SCRUTE((srcMesh == tgtMesh));
193 SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
198 theStatus = HYP_INCOMPATIBLE;
200 return ( theStatus == HYP_OK );
205 //================================================================================
207 * \brief define if a node is new or old
208 * \param node - node to check
209 * \retval bool - true if the node existed before Compute() is called
211 //================================================================================
213 bool isOldNode( const SMDS_MeshNode* node )
215 // old nodes are shared by edges and new ones are shared
216 // only by faces created by mapper
217 //if ( is1DComputed )
219 bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
224 // SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
225 // bool isNew = invFace->more();
230 //================================================================================
232 * \brief Class to remove mesh built by pattern mapper on edges
233 * and vertices in the case of failure of projection algo.
234 * It does it's job at destruction
236 //================================================================================
241 MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
242 ~MeshCleaner() { Clean(sm); }
243 void Release() { sm = 0; } // mesh will not be removed
244 static void Clean( SMESH_subMesh* sm, bool withSub=true )
246 if ( !sm || !sm->GetSubMeshDS() ) return;
247 // PAL16567, 18920. Remove face nodes as well
248 // switch ( sm->GetSubShape().ShapeType() ) {
249 // case TopAbs_VERTEX:
250 // case TopAbs_EDGE: {
251 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
252 SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
253 while ( nIt->more() ) {
254 const SMDS_MeshNode* node = nIt->next();
255 if ( !isOldNode( node ) )
256 mesh->RemoveNode( node );
258 // do not break but iterate over DependsOn()
261 if ( !withSub ) return;
262 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
263 while ( smIt->more() )
264 Clean( smIt->next(), false );
269 //================================================================================
271 * \brief find new nodes belonging to one free border of mesh on face
272 * \param sm - submesh on edge or vertex containing nodes to choose from
273 * \param face - the face bound by the submesh
274 * \param u2nodes - map to fill with nodes
275 * \param seamNodes - set of found nodes
276 * \retval bool - is a success
278 //================================================================================
280 bool getBoundaryNodes ( SMESH_subMesh* sm,
281 const TopoDS_Face& /*face*/,
282 map< double, const SMDS_MeshNode* > & u2nodes,
283 set< const SMDS_MeshNode* > & seamNodes)
287 if ( !sm || !sm->GetSubMeshDS() )
288 RETURN_BAD_RESULT("Null submesh");
290 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
291 switch ( sm->GetSubShape().ShapeType() ) {
293 case TopAbs_VERTEX: {
294 while ( nIt->more() ) {
295 const SMDS_MeshNode* node = nIt->next();
296 if ( isOldNode( node ) ) continue;
297 u2nodes.insert( make_pair( 0., node ));
298 seamNodes.insert( node );
305 // Get submeshes of sub-vertices
306 const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
307 if ( subSM.size() != 2 )
308 RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
309 " but we have " << subSM.size());
310 SMESH_subMesh* smV1 = subSM.begin()->second;
311 SMESH_subMesh* smV2 = subSM.rbegin()->second;
312 if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
313 RETURN_BAD_RESULT("Empty vertex submeshes");
315 const SMDS_MeshNode* nV1 = 0;
316 const SMDS_MeshNode* nE = 0;
318 // Look for nV1 - a new node on V1
319 nIt = smV1->GetSubMeshDS()->GetNodes();
320 while ( nIt->more() && !nE ) {
321 const SMDS_MeshNode* node = nIt->next();
322 if ( isOldNode( node ) ) continue;
325 // Find nE - a new node connected to nV1 and belonging to edge submesh;
326 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
327 SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
328 while ( vElems->more() && !nE ) {
329 const SMDS_MeshElement* elem = vElems->next();
330 int nbNodes = elem->NbNodes();
331 if ( elem->IsQuadratic() )
333 int iV1 = elem->GetNodeIndex( nV1 );
334 // try next after nV1
335 int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
336 if ( smDS->Contains( elem->GetNode( iE ) ))
337 nE = elem->GetNode( iE );
339 // try node before nV1
340 iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
341 if ( smDS->Contains( elem->GetNode( iE )))
342 nE = elem->GetNode( iE );
344 if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
345 if ( Abs( iV1 - iE ) == 1 )
346 nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
348 nE = elem->GetNode( elem->NbNodes() - 1 );
353 RETURN_BAD_RESULT("No new node found on V1");
355 RETURN_BAD_RESULT("new node on edge not found");
357 // Get the whole free border of a face
358 list< const SMDS_MeshNode* > bordNodes;
359 list< const SMDS_MeshElement* > bordFaces;
360 if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
361 RETURN_BAD_RESULT("free border of a face not found by nodes " <<
362 nV1->GetID() << " " << nE->GetID() );
364 // Insert nodes of the free border to the map until node on V2 encountered
365 SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
366 list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
367 bordIt++; // skip nV1
368 for ( ; bordIt != bordNodes.end(); ++bordIt ) {
369 const SMDS_MeshNode* node = *bordIt;
370 if ( v2smDS->Contains( node ))
372 if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
373 RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
374 " pos type " << node->GetPosition()->GetTypeOfPosition());
375 SMDS_EdgePositionPtr pos = node->GetPosition();
376 u2nodes.insert( make_pair( pos->GetUParameter(), node ));
377 seamNodes.insert( node );
379 if ( u2nodes.size() != seamNodes.size() )
380 RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
381 ", " << u2nodes.size() << " != " << seamNodes.size() );
386 RETURN_BAD_RESULT ("Unexpected submesh type");
388 } // bool getBoundaryNodes()
390 //================================================================================
392 * \brief Check if two consecutive EDGEs are connected in 2D
393 * \param [in] E1 - a well oriented non-seam EDGE
394 * \param [in] E2 - a possibly well oriented seam EDGE
395 * \param [in] F - a FACE
396 * \return bool - result
398 //================================================================================
400 bool are2dConnected( const TopoDS_Edge & E1,
401 const TopoDS_Edge & E2,
402 const TopoDS_Face & F )
405 Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( E1, F, f, l );
406 gp_Pnt2d uvFirst1 = c1->Value( f );
407 gp_Pnt2d uvLast1 = c1->Value( l );
409 Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( E2, F, f, l );
410 gp_Pnt2d uvFirst2 = c2->Value( E2.Orientation() == TopAbs_REVERSED ? l : f );
411 double tol2 = Max( Precision::PConfusion() * Precision::PConfusion(),
412 1e-5 * uvLast1.SquareDistance( uvFirst1 ));
414 return (( uvFirst2.SquareDistance( uvFirst1 ) < tol2 ) ||
415 ( uvFirst2.SquareDistance( uvLast1 ) < tol2 ));
418 //================================================================================
420 * \brief Compose TSideVector for both FACEs keeping matching order of EDGEs
421 * and fill src2tgtNodes map
423 //================================================================================
425 TError getWires(const TopoDS_Face& tgtFace,
426 const TopoDS_Face& srcFace,
427 SMESH_Mesh * tgtMesh,
428 SMESH_Mesh * srcMesh,
429 SMESH_MesherHelper* tgtHelper,
430 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
431 TSideVector& srcWires,
432 TSideVector& tgtWires,
433 TAssocTool::TNodeNodeMap& src2tgtNodes,
436 src2tgtNodes.clear();
438 // get ordered src EDGEs
440 srcWires = StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*skipMediumNodes=*/0, err );
441 if (( err && !err->IsOK() ) ||
442 ( srcWires.empty() ))
444 #ifdef PRINT_WHO_COMPUTE_WHAT
445 cout << "Projection_2D" << " F "
446 << tgtMesh->GetMeshDS()->ShapeToIndex( tgtFace ) << " <- "
447 << srcMesh->GetMeshDS()->ShapeToIndex( srcFace ) << endl;
450 // make corresponding sequence of tgt EDGEs
451 tgtWires.resize( srcWires.size() );
452 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
454 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
456 list< TopoDS_Edge > tgtEdges;
457 TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
458 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
460 TopoDS_Edge srcE = srcWire->Edge( iE );
461 TopoDS_Edge tgtE = TopoDS::Edge( shape2ShapeMap( srcE, /*isSrc=*/true));
462 TopoDS_Shape srcEbis = shape2ShapeMap( tgtE, /*isSrc=*/false );
463 if ( srcE.Orientation() != srcEbis.Orientation() )
465 // reverse a seam edge encountered for the second time
466 const int index = edgeMap.Add( tgtE );
467 if ( index < edgeMap.Extent() ) // E is a seam
469 // check which of edges to reverse, E or one already being in tgtEdges
470 if ( are2dConnected( tgtEdges.back(), tgtE, tgtFace ))
472 list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
473 std::advance( eIt, index-1 );
474 if ( are2dConnected( tgtEdges.back(), *eIt, tgtFace ))
482 if ( srcWire->NbEdges() == 1 && tgtMesh == srcMesh ) // circle
484 // try to verify ori by propagation
485 pair<int,TopoDS_Edge> nE =
486 StdMeshers_ProjectionUtils::GetPropagationEdge( srcMesh, tgtE, srcE );
487 if ( !nE.second.IsNull() )
490 tgtEdges.push_back( tgtE );
493 tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
494 /*theIsForward = */ true,
495 /*theIgnoreMediumNodes = */false,
497 StdMeshers_FaceSidePtr tgtWire = tgtWires[ iW ];
499 // Fill map of src to tgt nodes with nodes on edges
501 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
503 #ifdef PRINT_WHO_COMPUTE_WHAT
504 if ( tgtMesh->GetSubMesh( tgtWire->Edge(iE) )->IsEmpty() )
505 cout << "Projection_2D" << " E "
506 << tgtWire->EdgeID(iE) << " <- " << srcWire->EdgeID(iE) << endl;
508 if ( srcMesh->GetSubMesh( srcWire->Edge(iE) )->IsEmpty() ||
509 tgtMesh->GetSubMesh( tgtWire->Edge(iE) )->IsEmpty() )
511 // add nodes on VERTEXes for a case of not meshes EDGEs
512 const SMDS_MeshNode* srcN = srcWire->VertexNode( iE );
513 const SMDS_MeshNode* tgtN = tgtWire->VertexNode( iE );
515 src2tgtNodes.insert( make_pair( srcN, tgtN ));
519 const bool skipMedium = true, isFwd = true;
520 StdMeshers_FaceSide srcEdge( srcFace, srcWire->Edge(iE),
521 srcMesh, isFwd, skipMedium, srcWires[0]->FaceHelper() );
522 StdMeshers_FaceSide tgtEdge( tgtFace, tgtWire->Edge(iE),
523 tgtMesh, isFwd, skipMedium, tgtHelper);
525 vector< const SMDS_MeshNode* > srcNodes = srcEdge.GetOrderedNodes();
526 vector< const SMDS_MeshNode* > tgtNodes = tgtEdge.GetOrderedNodes();
528 if (( srcNodes.size() != tgtNodes.size() ) && tgtNodes.size() > 0 )
529 return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
530 "Different number of nodes on edges");
531 if ( !tgtNodes.empty() )
533 vector< const SMDS_MeshNode* >::iterator tn = tgtNodes.begin();
534 //if ( srcWire->Edge(iE).Orientation() == tgtWire->Edge(iE).Orientation() )
536 vector< const SMDS_MeshNode* >::iterator sn = srcNodes.begin();
537 for ( ; tn != tgtNodes.end(); ++tn, ++sn)
538 src2tgtNodes.insert( make_pair( *sn, *tn ));
542 // vector< const SMDS_MeshNode* >::reverse_iterator sn = srcNodes.rbegin();
543 // for ( ; tn != tgtNodes.end(); ++tn, ++sn)
544 // src2tgtNodes.insert( make_pair( *sn, *tn ));
549 } // loop on EDGEs of a WIRE
556 //================================================================================
558 * \brief Perform projection in case if tgtFace.IsPartner( srcFace ) and in case
559 * if projection by 3D transformation is possible
561 //================================================================================
563 bool projectPartner(const TopoDS_Face& tgtFace,
564 const TopoDS_Face& srcFace,
565 const TSideVector& tgtWires,
566 const TSideVector& srcWires,
567 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
568 TAssocTool::TNodeNodeMap& src2tgtNodes,
569 const bool is1DComputed)
571 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
572 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
573 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
574 SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
575 SMESH_MesherHelper* helper = tgtWires[0]->FaceHelper();
577 const double tol = 1.e-7 * srcMeshDS->getMaxDim();
579 // transformation to get location of target nodes from source ones
580 StdMeshers_ProjectionUtils::TrsfFinder3D trsf;
581 bool trsfIsOK = false;
582 if ( tgtFace.IsPartner( srcFace ))
584 gp_GTrsf srcTrsf = srcFace.Location().Transformation();
585 gp_GTrsf tgtTrsf = tgtFace.Location().Transformation();
586 gp_GTrsf t = srcTrsf.Inverted().Multiplied( tgtTrsf );
589 gp_Pnt srcP = BRep_Tool::Pnt( srcWires[0]->FirstVertex() );
590 gp_Pnt tgtP = BRep_Tool::Pnt( tgtWires[0]->FirstVertex() );
591 trsfIsOK = ( tgtP.Distance( trsf.Transform( srcP )) < tol );
594 trsf.Set( tgtTrsf.Inverted().Multiplied( srcTrsf ));
595 trsfIsOK = ( tgtP.Distance( trsf.Transform( srcP )) < tol );
600 // Try to find the 3D transformation
602 const int totNbSeg = 50;
603 vector< gp_XYZ > srcPnts, tgtPnts;
604 srcPnts.reserve( totNbSeg );
605 tgtPnts.reserve( totNbSeg );
606 gp_XYZ srcBC( 0,0,0 ), tgtBC( 0,0,0 );
607 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
609 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
610 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
612 size_t nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
613 double srcU = srcWires[iW]->FirstParameter( iE );
614 double tgtU = tgtWires[iW]->FirstParameter( iE );
615 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
616 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
617 for ( size_t i = 0; i < nbSeg; ++i )
619 srcPnts.push_back( srcWires[iW]->Value3d( srcU ).XYZ() );
620 tgtPnts.push_back( tgtWires[iW]->Value3d( tgtU ).XYZ() );
623 srcBC += srcPnts.back();
624 tgtBC += tgtPnts.back();
628 if ( !trsf.Solve( srcPnts, tgtPnts ))
633 const int nbTestPnt = 20;
634 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
636 gp_Pnt trsfTgt = trsf.Transform( srcBC / srcPnts.size() );
637 trsfIsOK = ( trsfTgt.SquareDistance( tgtBC / tgtPnts.size() ) < tol*tol );
638 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
640 gp_Pnt trsfTgt = trsf.Transform( srcPnts[i] );
641 trsfIsOK = ( trsfTgt.SquareDistance( tgtPnts[i] ) < tol*tol );
643 // check an in-FACE point
646 BRepAdaptor_Surface srcSurf( srcFace );
648 srcSurf.Value( 0.321 * ( srcSurf.FirstUParameter() + srcSurf.LastUParameter() ),
649 0.123 * ( srcSurf.FirstVParameter() + srcSurf.LastVParameter() ));
650 gp_Pnt tgtTrsfP = trsf.Transform( srcP );
652 GeomAPI_ProjectPointOnSurf& proj = helper->GetProjector( tgtFace, loc, 0.1*tol );
653 if ( !loc.IsIdentity() )
654 tgtTrsfP.Transform( loc.Transformation().Inverted() );
655 proj.Perform( tgtTrsfP );
656 trsfIsOK = ( proj.IsDone() &&
657 proj.NbPoints() > 0 &&
658 proj.LowerDistance() < tol );
666 // prepare the helper to adding quadratic elements if necessary
667 helper->IsQuadraticSubMesh( tgtFace );
669 SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
670 if ( !is1DComputed && srcSubDS->NbElements() )
671 helper->SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
673 SMESH_MesherHelper* srcHelper = srcWires[0]->FaceHelper();
674 SMESH_MesherHelper edgeHelper( *tgtMesh );
675 edgeHelper.ToFixNodeParameters( true );
677 const SMDS_MeshNode* nullNode = 0;
678 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
680 // indices of nodes to create properly oriented faces
681 bool isReverse = ( !trsf.IsIdentity() );
682 int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
684 std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
686 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
687 vector< const SMDS_MeshNode* > tgtNodes;
688 while ( elemIt->more() ) // loop on all mesh faces on srcFace
690 const SMDS_MeshElement* elem = elemIt->next();
691 const int nbN = elem->NbCornerNodes();
692 tgtNodes.resize( nbN );
693 helper->SetElementsOnShape( false );
694 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
696 const SMDS_MeshNode* srcNode = elem->GetNode(i);
697 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
698 if ( srcN_tgtN->second == nullNode )
701 gp_Pnt tgtP = trsf.Transform( SMESH_TNodeXYZ( srcNode ));
702 SMDS_MeshNode* n = helper->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
703 srcN_tgtN->second = n;
704 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
708 gp_Pnt2d srcUV = srcHelper->GetNodeUV( srcFace, srcNode );
709 tgtMeshDS->SetNodeOnFace( n, helper->GetSubShapeID(), srcUV.X(), srcUV.Y() );
714 const TopoDS_Edge& srcE = TopoDS::Edge( srcMeshDS->IndexToShape( srcNode->getshapeId()));
715 const TopoDS_Edge& tgtE = TopoDS::Edge( shape2ShapeMap( srcE, /*isSrc=*/true ));
716 double srcU = srcHelper->GetNodeU( srcE, srcNode );
717 tgtMeshDS->SetNodeOnEdge( n, tgtE, srcU );
718 if ( !tgtFace.IsPartner( srcFace ))
720 edgeHelper.SetSubShape( tgtE );
721 double tol = BRep_Tool::Tolerance( tgtE );
722 bool isOk = edgeHelper.CheckNodeU( tgtE, n, srcU, 2 * tol, /*force=*/true );
723 if ( !isOk ) // projection of n to tgtE failed (23395)
725 double sF, sL, tF, tL;
726 BRep_Tool::Range( srcE, sF, sL );
727 BRep_Tool::Range( tgtE, tF, tL );
728 double srcR = ( srcU - sF ) / ( sL - sF );
729 double tgtU = tF + srcR * ( tL - tF );
730 tgtMeshDS->SetNodeOnEdge( n, tgtE, tgtU );
731 gp_Pnt newP = BRepAdaptor_Curve( tgtE ).Value( tgtU );
732 double dist = newP.Distance( tgtP );
733 if ( tol < dist && dist < 1000*tol )
734 tgtMeshDS->MoveNode( n, newP.X(), newP.Y(), newP.Z() );
739 case SMDS_TOP_VERTEX:
741 const TopoDS_Shape & srcV = srcMeshDS->IndexToShape( srcNode->getshapeId() );
742 const TopoDS_Shape & tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
743 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
749 tgtNodes[i] = srcN_tgtN->second;
752 helper->SetElementsOnShape( true );
755 case 3: helper->AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
756 case 4: helper->AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
758 if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
759 helper->AddPolygonalFace( tgtNodes );
763 // check node positions
765 if ( !tgtFace.IsPartner( srcFace ) )
767 helper->ToFixNodeParameters( true );
770 const double tol2d = 1e-12;
771 srcN_tgtN = src2tgtNodes.begin();
772 for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
774 const SMDS_MeshNode* n = srcN_tgtN->second;
775 switch ( n->GetPosition()->GetTypeOfPosition() )
779 if ( nbOkPos > 10 ) break;
780 gp_XY uv = helper->GetNodeUV( tgtFace, n ), uvBis = uv;
781 if (( helper->CheckNodeUV( tgtFace, n, uv, tol )) &&
782 (( uv - uvBis ).SquareModulus() < tol2d ))
785 nbOkPos = -((int) src2tgtNodes.size() );
790 // const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
791 // edgeHelper.SetSubShape( tgtE );
792 // edgeHelper.GetNodeU( tgtE, n, 0, &toCheck );
802 } // bool projectPartner()
804 //================================================================================
806 * \brief Perform projection in case if the faces are similar in 2D space
808 //================================================================================
810 bool projectBy2DSimilarity(const TopoDS_Face& tgtFace,
811 const TopoDS_Face& srcFace,
812 const TSideVector& tgtWires,
813 const TSideVector& srcWires,
814 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
815 TAssocTool::TNodeNodeMap& src2tgtNodes,
816 const bool is1DComputed)
818 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
819 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
821 // WARNING: we can have problems if the FACE is symmetrical in 2D,
822 // then the projection can be mirrored relating to what is expected
824 // 1) Find 2D transformation
826 StdMeshers_ProjectionUtils::TrsfFinder2D trsf;
828 // get 2 pairs of corresponding UVs
829 gp_Pnt2d srcP0 = srcWires[0]->Value2d(0.0);
830 gp_Pnt2d srcP1 = srcWires[0]->Value2d(0.333);
831 gp_Pnt2d tgtP0 = tgtWires[0]->Value2d(0.0);
832 gp_Pnt2d tgtP1 = tgtWires[0]->Value2d(0.333);
834 // make transformation
835 gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
836 gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
837 gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
838 toSrcCS .SetTransformation( srcCS );
839 fromTgtCS.SetTransformation( tgtCS );
841 trsf.Set( fromTgtCS * toSrcCS );
843 // check transformation
844 bool trsfIsOK = true;
845 const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
846 for ( double u = 0.12; ( u < 1. && trsfIsOK ); u += 0.1 )
848 gp_Pnt2d srcUV = srcWires[0]->Value2d( u );
849 gp_Pnt2d tgtUV = tgtWires[0]->Value2d( u );
850 gp_Pnt2d tgtUV2 = trsf.Transform( srcUV );
851 trsfIsOK = ( tgtUV.Distance( tgtUV2 ) < tol );
854 // Find trsf using a least-square approximation
858 const int totNbSeg = 50;
859 vector< gp_XY > srcPnts, tgtPnts;
860 srcPnts.reserve( totNbSeg );
861 tgtPnts.reserve( totNbSeg );
862 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
864 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
865 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
867 size_t nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
868 double srcU = srcWires[iW]->FirstParameter( iE );
869 double tgtU = tgtWires[iW]->FirstParameter( iE );
870 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
871 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
872 for ( size_t i = 0; i < nbSeg; ++i, srcU += srcDu, tgtU += tgtDu )
874 srcPnts.push_back( srcWires[iW]->Value2d( srcU ).XY() );
875 tgtPnts.push_back( tgtWires[iW]->Value2d( tgtU ).XY() );
879 if ( !trsf.Solve( srcPnts, tgtPnts ))
885 const int nbTestPnt = 10;
886 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
887 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
889 gp_Pnt2d trsfTgt = trsf.Transform( srcPnts[i] );
890 trsfIsOK = ( trsfTgt.Distance( tgtPnts[i] ) < tol );
895 } // "Find transformation" block
899 SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
901 SMESH_MesherHelper* helper = tgtWires[0]->FaceHelper();
903 helper->IsQuadraticSubMesh( tgtFace );
905 helper->SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
906 helper->SetElementsOnShape( true );
907 Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
908 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
910 SMESH_MesherHelper* srcHelper = srcWires[0]->FaceHelper();
912 const SMDS_MeshNode* nullNode = 0;
913 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
915 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
916 vector< const SMDS_MeshNode* > tgtNodes;
918 while ( elemIt->more() ) // loop on all mesh faces on srcFace
920 const SMDS_MeshElement* elem = elemIt->next();
921 const int nbN = elem->NbCornerNodes();
922 tgtNodes.resize( nbN );
923 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
925 const SMDS_MeshNode* srcNode = elem->GetNode(i);
926 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
927 if ( srcN_tgtN->second == nullNode )
930 gp_Pnt2d srcUV = srcHelper->GetNodeUV( srcFace, srcNode,
931 elem->GetNode( helper->WrapIndex(i+1,nbN)), &uvOK);
932 gp_Pnt2d tgtUV = trsf.Transform( srcUV );
933 gp_Pnt tgtP = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
934 SMDS_MeshNode* n = tgtMeshDS->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
935 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
937 case SMDS_TOP_FACE: {
938 tgtMeshDS->SetNodeOnFace( n, helper->GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
941 case SMDS_TOP_EDGE: {
942 TopoDS_Shape srcEdge = srcHelper->GetSubShapeByNode( srcNode, srcHelper->GetMeshDS() );
943 TopoDS_Edge tgtEdge = TopoDS::Edge( shape2ShapeMap( srcEdge, /*isSrc=*/true ));
944 double U = Precision::Infinite();
945 helper->CheckNodeU( tgtEdge, n, U, Precision::PConfusion());
946 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtEdge ), U );
949 case SMDS_TOP_VERTEX: {
950 TopoDS_Shape srcV = srcHelper->GetSubShapeByNode( srcNode, srcHelper->GetMeshDS() );
951 TopoDS_Shape tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
952 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
957 srcN_tgtN->second = n;
959 tgtNodes[i] = srcN_tgtN->second;
961 // create a new face (with reversed orientation)
964 case 3: helper->AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
965 case 4: helper->AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
967 } // loop on all mesh faces on srcFace
972 //================================================================================
974 * \brief Perform projection in case of quadrilateral faces
976 //================================================================================
978 bool projectQuads(const TopoDS_Face& /*tgtFace*/,
979 const TopoDS_Face& /*srcFace*/,
980 const TSideVector& /*tgtWires*/,
981 const TSideVector& /*srcWires*/,
982 const TAssocTool::TShapeShapeMap& /*shape2ShapeMap*/,
983 TAssocTool::TNodeNodeMap& /*src2tgtNodes*/,
984 const bool /*is1DComputed*/)
986 // SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
987 // SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
988 // //SMESHDS_Mesh * tgtMeshDS = tgtMesh->GetMeshDS();
989 // SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
991 // if ( srcWires[0]->NbEdges() != 4 )
993 // if ( !is1DComputed )
995 // for ( int iE = 0; iE < 4; ++iE )
997 // SMESHDS_SubMesh* sm = srcMeshDS->MeshElements( srcWires[0]->Edge( iE ));
998 // if ( !sm ) return false;
999 // if ( sm->NbNodes() + sm->NbElements() == 0 ) return false;
1001 // if ( BRepAdaptor_Surface( tgtFace ).GetType() != GeomAbs_Plane )
1003 // // if ( BRepAdaptor_Surface( tgtFace ).GetType() == GeomAbs_Plane &&
1004 // // BRepAdaptor_Surface( srcFace ).GetType() == GeomAbs_Plane )
1005 // // return false; // too easy
1007 // // load EDGEs to SMESH_Block
1009 // SMESH_Block block;
1010 // TopTools_IndexedMapOfOrientedShape blockSubShapes;
1012 // const TopoDS_Solid& box = srcMesh->PseudoShape();
1013 // TopoDS_Shell shell = TopoDS::Shell( TopExp_Explorer( box, TopAbs_SHELL ).Current() );
1015 // block.LoadBlockShapes( shell, v, v, blockSubShapes ); // fill all since operator[] is missing
1017 // const SMESH_Block::TShapeID srcFaceBID = SMESH_Block::ID_Fxy0;
1018 // const SMESH_Block::TShapeID tgtFaceBID = SMESH_Block::ID_Fxy1;
1019 // vector< int > edgeBID;
1020 // block.GetFaceEdgesIDs( srcFaceBID, edgeBID ); // u0, u1, 0v, 1v
1021 // blockSubShapes.Substitute( edgeBID[0], srcWires[0]->Edge(0) );
1022 // blockSubShapes.Substitute( edgeBID[1], srcWires[0]->Edge(2) );
1023 // blockSubShapes.Substitute( edgeBID[2], srcWires[0]->Edge(3) );
1024 // blockSubShapes.Substitute( edgeBID[3], srcWires[0]->Edge(1) );
1025 // block.GetFaceEdgesIDs( tgtFaceBID, edgeBID ); // u0, u1, 0v, 1v
1026 // blockSubShapes.Substitute( edgeBID[0], tgtWires[0]->Edge(0) );
1027 // blockSubShapes.Substitute( edgeBID[1], tgtWires[0]->Edge(2) );
1028 // blockSubShapes.Substitute( edgeBID[2], tgtWires[0]->Edge(3) );
1029 // blockSubShapes.Substitute( edgeBID[3], tgtWires[0]->Edge(1) );
1030 // block.LoadFace( srcFace, srcFaceBID, blockSubShapes );
1031 // block.LoadFace( tgtFace, tgtFaceBID, blockSubShapes );
1033 // // remember connectivity of new faces in terms of ( node-or-XY )
1035 // typedef std::pair< const SMDS_MeshNode*, gp_XYZ > TNodeOrXY; // node-or-XY
1036 // typedef std::vector< TNodeOrXY* > TFaceConn; // face connectivity
1037 // std::vector< TFaceConn > newFacesVec; // connectivity of all faces
1038 // std::map< const SMDS_MeshNode*, TNodeOrXY > srcNode2tgtNXY; // src node -> node-or-XY
1040 // TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
1041 // std::map< const SMDS_MeshNode*, TNodeOrXY >::iterator srcN_tgtNXY;
1042 // std::pair< std::map< const SMDS_MeshNode*, TNodeOrXY >::iterator, bool > n2n_isNew;
1043 // TNodeOrXY nullNXY( (SMDS_MeshNode*)NULL, gp_XYZ(0,0,0) );
1045 // SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
1046 // newFacesVec.resize( srcSubDS->NbElements() );
1047 // int iFaceSrc = 0;
1049 // SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
1050 // while ( elemIt->more() ) // loop on all mesh faces on srcFace
1052 // const SMDS_MeshElement* elem = elemIt->next();
1053 // TFaceConn& tgtNodes = newFacesVec[ iFaceSrc++ ];
1055 // const int nbN = elem->NbCornerNodes();
1056 // tgtNodes.resize( nbN );
1057 // for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
1059 // const SMDS_MeshNode* srcNode = elem->GetNode(i);
1060 // n2n_isNew = srcNode2tgtNXY.insert( make_pair( srcNode, nullNXY ));
1061 // TNodeOrXY & tgtNodeOrXY = n2n_isNew.first->second;
1062 // if ( n2n_isNew.second ) // new src node encounters
1064 // srcN_tgtN = src2tgtNodes.find( srcNode );
1065 // if ( srcN_tgtN != src2tgtNodes.end() )
1067 // tgtNodeOrXY.first = srcN_tgtN->second; // tgt node exists
1071 // // find XY of src node within the quadrilateral srcFace
1072 // if ( !block.ComputeParameters( SMESH_TNodeXYZ( srcNode ),
1073 // tgtNodeOrXY.second, srcFaceBID ))
1077 // tgtNodes[ i ] = & tgtNodeOrXY;
1081 // // as all XY are computed, create tgt nodes and faces
1083 // SMESH_MesherHelper helper = *tgtWires[0]->FaceHelper();
1084 // if ( is1DComputed )
1085 // helper.IsQuadraticSubMesh( tgtFace );
1087 // helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
1088 // helper.SetElementsOnShape( true );
1089 // Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
1091 // SMESH_MesherHelper srcHelper = *srcWires[0]->FaceHelper();
1093 // vector< const SMDS_MeshNode* > tgtNodes;
1096 // for ( size_t iFaceTgt = 0; iFaceTgt < newFacesVec.size(); ++iFaceTgt )
1098 // TFaceConn& tgtConn = newFacesVec[ iFaceTgt ];
1099 // tgtNodes.resize( tgtConn.size() );
1100 // for ( size_t iN = 0; iN < tgtConn.size(); ++iN )
1102 // const SMDS_MeshNode* & tgtN = tgtConn[ iN ]->first;
1103 // if ( !tgtN ) // create a node
1105 // if ( !block.FaceUV( tgtFaceBID, tgtConn[iN]->second, uv ))
1107 // gp_Pnt p = tgtSurface->Value( uv.X(), uv.Y() );
1108 // tgtN = helper.AddNode( p.X(), p.Y(), p.Z(), uv.X(), uv.Y() );
1110 // tgtNodes[ tgtNodes.size() - iN - 1] = tgtN; // reversed orientation
1112 // switch ( tgtNodes.size() )
1114 // case 3: helper.AddFace(tgtNodes[0], tgtNodes[1], tgtNodes[2]); break;
1115 // case 4: helper.AddFace(tgtNodes[0], tgtNodes[1], tgtNodes[2], tgtNodes[3]); break;
1117 // if ( tgtNodes.size() > 4 )
1118 // helper.AddPolygonalFace( tgtNodes );
1121 return false; //true;
1123 } // bool projectQuads(...)
1125 //================================================================================
1127 * \brief Fix bad faces by smoothing
1129 //================================================================================
1131 bool fixDistortedFaces( SMESH_MesherHelper& helper,
1132 TSideVector& tgtWires )
1134 SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMesh( helper.GetSubShape() );
1136 //if ( helper.IsDistorted2D( faceSM, /*checkUV=*/true ))
1138 SMESH_MeshEditor editor( helper.GetMesh() );
1139 SMESHDS_SubMesh* smDS = faceSM->GetSubMeshDS();
1140 const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
1142 TIDSortedElemSet faces;
1143 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1144 for ( faceIt = smDS->GetElements(); faceIt->more(); )
1145 faces.insert( faces.end(), faceIt->next() );
1147 // choose smoothing algo
1148 //SMESH_MeshEditor:: SmoothMethod algo = SMESH_MeshEditor::CENTROIDAL;
1149 bool isConcaveBoundary = false;
1150 for ( size_t iW = 0; iW < tgtWires.size() && !isConcaveBoundary; ++iW )
1152 TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
1153 for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
1155 double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
1156 F, tgtWires[iW]->FirstVertex( iE ));
1157 isConcaveBoundary = ( angle < -5. * M_PI / 180. );
1159 prevEdge = tgtWires[iW]->Edge( iE );
1162 SMESH_MeshEditor:: SmoothMethod algo =
1163 isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
1165 // smooth in 2D or 3D?
1166 TopLoc_Location loc;
1167 Handle(Geom_Surface) surface = BRep_Tool::Surface( F, loc );
1168 bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
1171 set<const SMDS_MeshNode*> fixedNodes;
1172 editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
1173 /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
1175 helper.ToFixNodeParameters( true );
1177 return !helper.IsDistorted2D( faceSM, /*checkUV=*/true );
1182 //=======================================================================
1184 * Set initial association of VERTEXes for the case of projection
1185 * from a quadrangle FACE to a closed FACE, where opposite src EDGEs
1186 * have different nb of segments
1188 //=======================================================================
1190 void initAssoc4Quad2Closed(const TopoDS_Shape& tgtFace,
1191 SMESH_MesherHelper& tgtHelper,
1192 const TopoDS_Shape& srcFace,
1193 SMESH_Mesh* srcMesh,
1194 TAssocTool::TShapeShapeMap & assocMap)
1196 if ( !tgtHelper.HasRealSeam() || srcFace.ShapeType() != TopAbs_FACE )
1197 return; // no seam edge
1198 list< TopoDS_Edge > tgtEdges, srcEdges;
1199 list< int > tgtNbEW, srcNbEW;
1200 int tgtNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( tgtFace ), tgtEdges, tgtNbEW );
1201 int srcNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( srcFace ), srcEdges, srcNbEW );
1202 if ( tgtNbW != 1 || srcNbW != 1 ||
1203 tgtNbEW.front() != 4 || srcNbEW.front() != 4 )
1204 return; // not quads
1207 list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1208 for ( int i = 0; edgeS != srcEdges.end(); ++i, ++edgeS )
1209 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *edgeS ))
1210 srcNbSeg[ i ] = sm->NbNodes();
1212 return; // not meshed
1213 if ( srcNbSeg[0] == srcNbSeg[2] && srcNbSeg[1] == srcNbSeg[3] )
1214 return; // same nb segments
1215 if ( srcNbSeg[0] != srcNbSeg[2] && srcNbSeg[1] != srcNbSeg[3] )
1216 return; // all different nb segments
1218 edgeS = srcEdges.begin();
1219 if ( srcNbSeg[0] != srcNbSeg[2] )
1221 TAssocTool::InsertAssociation( tgtHelper.IthVertex( 0,*edgeT ),
1222 tgtHelper.IthVertex( 0,*edgeS ), assocMap );
1223 TAssocTool::InsertAssociation( tgtHelper.IthVertex( 1,*edgeT ),
1224 tgtHelper.IthVertex( 1,*edgeS ), assocMap );
1230 //=======================================================================
1231 //function : Compute
1233 //=======================================================================
1235 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1237 _src2tgtNodes.clear();
1242 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1243 SMESH_Mesh * tgtMesh = & theMesh;
1247 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1248 SMESH_MesherHelper helper( theMesh );
1250 // ---------------------------
1251 // Make sub-shapes association
1252 // ---------------------------
1254 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1255 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1257 helper.SetSubShape( tgtFace );
1259 TAssocTool::TShapeShapeMap shape2ShapeMap;
1260 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1261 if ( shape2ShapeMap.IsEmpty() )
1262 initAssoc4Quad2Closed( tgtFace, helper, srcShape, srcMesh, shape2ShapeMap );
1263 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1265 !shape2ShapeMap.IsBound( tgtFace ))
1267 if ( srcShape.ShapeType() == TopAbs_FACE )
1269 int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1270 int nbE2 = helper.Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1272 return error(COMPERR_BAD_SHAPE,
1273 SMESH_Comment("Different number of edges in source and target faces: ")
1274 << nbE2 << " and " << nbE1 );
1276 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1278 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1280 // ----------------------------------------------
1281 // Assure that mesh on a source Face is computed
1282 // ----------------------------------------------
1284 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1285 SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1287 string srcMeshError;
1288 if ( tgtMesh == srcMesh ) {
1289 if ( !TAssocTool::MakeComputed( srcSubMesh ))
1290 srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1293 if ( !srcSubMesh->IsMeshComputed() )
1294 srcMeshError = TAssocTool::SourceNotComputedError();
1296 if ( !srcMeshError.empty() )
1297 return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1303 // get ordered src and tgt EDGEs
1304 TSideVector srcWires, tgtWires;
1305 bool is1DComputed = false; // if any tgt EDGE is meshed
1306 TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh, &helper,
1307 shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1308 if ( err && !err->IsOK() )
1309 return error( err );
1311 bool projDone = false;
1315 // try to project from the same face with different location
1316 projDone = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1317 shape2ShapeMap, _src2tgtNodes, is1DComputed );
1321 // projection in case if the faces are similar in 2D space
1322 projDone = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1323 shape2ShapeMap, _src2tgtNodes, is1DComputed );
1327 // projection in case of quadrilateral faces
1328 // NOT IMPLEMENTED, returns false
1329 projDone = projectQuads( tgtFace, srcFace, tgtWires, srcWires,
1330 shape2ShapeMap, _src2tgtNodes, is1DComputed);
1333 // it will remove mesh built on edges and vertices in failure case
1334 MeshCleaner cleaner( tgtSubMesh );
1338 _src2tgtNodes.clear();
1339 // --------------------
1340 // Prepare to mapping
1341 // --------------------
1343 // Check if node projection to a face is needed
1345 SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1346 set< const SMDS_MeshNode* > faceNodes;
1347 for ( ; faceNodes.size() < 3 && faceIt->more(); ) {
1348 const SMDS_MeshElement* face = faceIt->next();
1349 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1350 while ( nodeIt->more() ) {
1351 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1352 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE &&
1353 faceNodes.insert( node ).second )
1354 uvBox.Add( helper.GetNodeUV( srcFace, node ));
1357 bool toProjectNodes = false;
1358 if ( faceNodes.size() == 1 )
1359 toProjectNodes = ( uvBox.IsVoid() || uvBox.CornerMin().IsEqual( gp_XY(0,0), 1e-12 ));
1360 else if ( faceNodes.size() > 1 )
1361 toProjectNodes = ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN );
1363 // Find the corresponding source and target vertex
1364 // and <theReverse> flag needed to call mapper.Apply()
1366 TopoDS_Vertex srcV1, tgtV1;
1367 bool reverse = false;
1369 TopExp_Explorer vSrcExp( srcFace, TopAbs_VERTEX );
1370 srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1371 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1373 list< TopoDS_Edge > tgtEdges, srcEdges;
1374 list< int > nbEdgesInWires;
1375 SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1376 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1378 if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1380 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1381 TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1382 reverse = ( ! srcE1.IsSame( srcE1bis ));
1383 if ( ( reverse || srcE1.Orientation() != srcE1bis.Orientation() ) &&
1384 nbEdgesInWires.front() > 2 &&
1385 helper.IsRealSeam( tgtEdges.front() ))
1387 if ( srcE1.Orientation() != srcE1bis.Orientation() )
1389 // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1390 // always puts a seam EDGE first (if possible) and as a result
1391 // we can't use only theReverse flag to correctly associate source
1392 // and target faces in the mapper. Thus we select srcV1 so that
1393 // GetOrderedEdges() to return EDGEs in a needed order
1394 TopoDS_Face tgtFaceBis = tgtFace;
1395 TopTools_MapOfShape checkedVMap( tgtEdges.size() );
1396 checkedVMap.Add ( srcV1 );
1397 for ( vSrcExp.Next(); vSrcExp.More(); )
1399 tgtFaceBis.Reverse();
1401 SMESH_Block::GetOrderedEdges( tgtFaceBis, tgtEdges, nbEdgesInWires, tgtV1 );
1403 list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1404 for ( ; edgeS != srcEdges.end() && ok ; ++edgeS, ++edgeT )
1405 ok = edgeT->IsSame( shape2ShapeMap( *edgeS, /*isSrc=*/true ));
1413 while ( vSrcExp.More() && !checkedVMap.Add( vSrcExp.Current() ))
1418 srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1419 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1421 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1425 // for the case: project to a closed face from a non-closed face w/o vertex assoc;
1426 // avoid projecting to a seam from two EDGEs with different nb nodes on them
1427 // ( test mesh_Projection_2D_01/B1 )
1428 if ( !_sourceHypo->HasVertexAssociation() &&
1429 nbEdgesInWires.front() > 2 &&
1430 helper.IsRealSeam( tgtEdges.front() ))
1432 TopoDS_Shape srcEdge1 = shape2ShapeMap( tgtEdges.front() );
1433 list< TopoDS_Edge >::iterator srcEdge2 =
1434 std::find( srcEdges.begin(), srcEdges.end(), srcEdge1);
1435 list< TopoDS_Edge >::iterator srcEdge3 =
1436 std::find( srcEdges.begin(), srcEdges.end(), srcEdge1.Reversed());
1437 if ( srcEdge2 == srcEdges.end() || srcEdge3 == srcEdges.end() ) // srcEdge1 is not a seam
1439 // find srcEdge2 which also will be projected to tgtEdges.front()
1440 for ( srcEdge2 = srcEdges.begin(); srcEdge2 != srcEdges.end(); ++srcEdge2 )
1441 if ( !srcEdge1.IsSame( *srcEdge2 ) &&
1442 tgtEdges.front().IsSame( shape2ShapeMap( *srcEdge2, /*isSrc=*/true )))
1444 // compare nb nodes on srcEdge1 and srcEdge2
1445 if ( srcEdge2 != srcEdges.end() )
1447 int nbN1 = 0, nbN2 = 0;
1448 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( srcEdge1 ))
1449 nbN1 = sm->NbNodes();
1450 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *srcEdge2 ))
1451 nbN2 = sm->NbNodes();
1453 srcV1 = helper.IthVertex( 1, srcEdges.front() );
1458 else if ( nbEdgesInWires.front() == 1 ) // a sole edge in a wire
1460 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1461 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1463 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
1464 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
1465 if ( srcE1.IsSame( srcWire->Edge( iE )))
1467 reverse = ( tgtE1.Orientation() != tgtWires[iW]->Edge( iE ).Orientation() );
1474 RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1477 // Load pattern from the source face
1478 SMESH_Pattern mapper;
1479 mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1, /*keepNodes=*/true );
1480 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1481 return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1483 // --------------------
1484 // Perform 2D mapping
1485 // --------------------
1487 // Compute mesh on a target face
1489 mapper.Apply( tgtFace, tgtV1, reverse );
1490 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK ) {
1491 // std::ofstream file("/tmp/Pattern.smp" );
1492 // mapper.Save( file );
1493 return error("Can't apply source mesh pattern to the face");
1498 const bool toCreatePolygons = false, toCreatePolyedrs = false;
1499 mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1500 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1501 return error("Can't make mesh by source mesh pattern");
1503 // fill _src2tgtNodes
1504 std::vector< const SMDS_MeshNode* > *srcNodes, *tgtNodes;
1505 mapper.GetInOutNodes( srcNodes, tgtNodes );
1506 size_t nbN = std::min( srcNodes->size(), tgtNodes->size() );
1507 for ( size_t i = 0; i < nbN; ++i )
1508 if ( (*srcNodes)[i] && (*tgtNodes)[i] )
1509 _src2tgtNodes.insert( make_pair( (*srcNodes)[i], (*tgtNodes)[i] ));
1512 } // end of projection using Pattern mapping
1515 // -------------------------------------------------------------------------
1516 // mapper doesn't take care of nodes already existing on edges and vertices,
1517 // so we must merge nodes created by it with existing ones
1518 // -------------------------------------------------------------------------
1520 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1522 // Make groups of nodes to merge
1524 // loop on EDGE and VERTEX sub-meshes of a target FACE
1525 SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1526 /*complexShapeFirst=*/false);
1527 while ( smIt->more() )
1529 SMESH_subMesh* sm = smIt->next();
1530 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1531 if ( !smDS || smDS->NbNodes() == 0 )
1533 //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1536 if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1538 if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1540 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1541 SMESH_subMeshIteratorPtr smDegenIt
1542 = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1543 while ( smDegenIt->more() )
1544 if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1546 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1547 while ( nIt->more() )
1548 groupsOfNodes.back().push_back( nIt->next() );
1551 continue; // do not treat sm of degen VERTEX
1554 // Sort new and old nodes of a sub-mesh separately
1556 bool isSeam = helper.IsRealSeam( sm->GetId() );
1558 enum { NEW_NODES = 0, OLD_NODES };
1559 map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1560 map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1561 set< const SMDS_MeshNode* > seamNodes;
1563 // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1564 if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1566 //RETURN_BAD_RESULT("getBoundaryNodes() failed");
1569 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1570 while ( nIt->more() )
1572 const SMDS_MeshNode* node = nIt->next();
1573 bool isOld = isOldNode( node );
1575 if ( !isOld && isSeam ) { // new node on a seam edge
1576 if ( seamNodes.count( node ) )
1577 continue; // node is already in the map
1580 // sort nodes on edges by their position
1581 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1582 switch ( node->GetPosition()->GetTypeOfPosition() )
1584 case SMDS_TOP_VERTEX: {
1585 if ( !is1DComputed && !pos2nodes.empty() )
1586 u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1588 pos2nodes.insert( make_pair( 0, node ));
1591 case SMDS_TOP_EDGE: {
1592 SMDS_EdgePositionPtr pos = node->GetPosition();
1593 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1597 RETURN_BAD_RESULT("Wrong node position type: "<<
1598 node->GetPosition()->GetTypeOfPosition());
1601 const bool mergeNewToOld =
1602 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1603 const bool mergeSeamToNew =
1604 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1606 if ( !mergeNewToOld )
1607 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1608 u2nodesMaps[ OLD_NODES ].size() > 0 )
1610 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1611 newEnd = u2nodesMaps[ OLD_NODES ].end();
1612 for ( ; u_oldNode != newEnd; ++u_oldNode )
1613 SMESH_Algo::addBadInputElement( u_oldNode->second );
1614 return error( COMPERR_BAD_INPUT_MESH,
1615 SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1616 << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1617 << " #" << sm->GetId() );
1619 if ( isSeam && !mergeSeamToNew ) {
1620 const TopoDS_Shape& seam = sm->GetSubShape();
1621 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1622 u2nodesOnSeam.size() > 0 &&
1623 seam.ShapeType() == TopAbs_EDGE )
1625 int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1626 int nbE2 = helper.Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1627 if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1629 // find the 2 EDGEs of srcFace
1630 TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1631 for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1632 if ( seam.IsSame( src2tgtIt.Value() ))
1633 SMESH_Algo::addBadInputElements
1634 ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1635 return error( COMPERR_BAD_INPUT_MESH,
1636 "Different number of nodes on two edges projected to a seam edge" );
1641 // Make groups of nodes to merge
1643 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1644 u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1645 newEnd = u2nodesMaps[ NEW_NODES ].end();
1646 u_newOnSeam = u2nodesOnSeam.begin();
1647 if ( mergeNewToOld )
1648 for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1650 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1651 groupsOfNodes.back().push_back( u_oldNode->second );
1652 groupsOfNodes.back().push_back( u_newNode->second );
1653 if ( mergeSeamToNew )
1654 groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1656 else if ( mergeSeamToNew )
1657 for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1659 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1660 groupsOfNodes.back().push_back( u_newNode->second );
1661 groupsOfNodes.back().push_back( u_newOnSeam->second );
1664 } // loop on EDGE and VERTEX submeshes of a target FACE
1668 SMESH_MeshEditor editor( tgtMesh );
1669 int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1670 editor.MergeNodes( groupsOfNodes );
1671 int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1672 if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1673 return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1675 // ----------------------------------------------------------------
1676 // The mapper can't create quadratic elements, so convert if needed
1677 // ----------------------------------------------------------------
1679 SMDS_ElemIteratorPtr faceIt;
1680 faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1681 bool srcIsQuad = faceIt->next()->IsQuadratic();
1682 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1683 bool tgtIsQuad = faceIt->next()->IsQuadratic();
1684 if ( srcIsQuad && !tgtIsQuad )
1686 TIDSortedElemSet tgtFaces;
1687 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1688 while ( faceIt->more() )
1689 tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1691 editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1693 } // end of coincident nodes and quadratic elements treatment
1696 if ( !projDone || is1DComputed )
1697 // ----------------------------------------------------------------
1698 // The mapper can create distorted faces by placing nodes out of the FACE
1699 // boundary, also bad faces can be created if EDGEs already discretized
1700 // --> fix bad faces by smoothing
1701 // ----------------------------------------------------------------
1702 if ( helper.IsDistorted2D( tgtSubMesh, /*checkUV=*/false, &helper ))
1704 TAssocTool::Morph morph( srcWires );
1705 morph.Perform( helper, tgtWires, helper.GetSurface( tgtFace ),
1706 _src2tgtNodes, /*moveAll=*/true );
1708 if ( !fixDistortedFaces( helper, tgtWires ))
1709 return error("Invalid mesh generated");
1711 // ---------------------------
1712 // Check elements orientation
1713 // ---------------------------
1715 TopoDS_Face face = TopoDS::Face( theShape );
1716 if ( !theMesh.IsMainShape( tgtFace ))
1718 // find the main shape
1719 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1720 switch ( mainShape.ShapeType() ) {
1722 case TopAbs_SOLID: break;
1724 TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1725 for ( ; ancestIt.More(); ancestIt.Next() ) {
1726 TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1727 if ( type == TopAbs_SOLID ) {
1728 mainShape = ancestIt.Value();
1730 } else if ( type == TopAbs_SHELL ) {
1731 mainShape = ancestIt.Value();
1735 // find tgtFace in the main solid or shell to know it's true orientation.
1736 TopExp_Explorer exp( mainShape, TopAbs_FACE );
1737 for ( ; exp.More(); exp.Next() ) {
1738 if ( tgtFace.IsSame( exp.Current() )) {
1739 face = TopoDS::Face( exp.Current() );
1745 if ( helper.IsReversedSubMesh( face ))
1747 SMESH_MeshEditor editor( tgtMesh );
1748 SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1749 while ( eIt->more() ) {
1750 const SMDS_MeshElement* e = eIt->next();
1751 if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1752 RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1756 cleaner.Release(); // not to remove mesh
1762 //=======================================================================
1763 //function : Evaluate
1765 //=======================================================================
1767 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
1768 const TopoDS_Shape& theShape,
1769 MapShapeNbElems& aResMap)
1774 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1775 SMESH_Mesh * tgtMesh = & theMesh;
1779 // ---------------------------
1780 // Make sub-shapes association
1781 // ---------------------------
1783 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1784 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1786 TAssocTool::TShapeShapeMap shape2ShapeMap;
1787 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1788 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1790 !shape2ShapeMap.IsBound( tgtFace ))
1791 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1793 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1795 // -------------------------------------------------------
1796 // Assure that mesh on a source Face is computed/evaluated
1797 // -------------------------------------------------------
1799 std::vector<smIdType> aVec;
1801 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1802 if ( srcSubMesh->IsMeshComputed() )
1804 aVec.resize( SMDSEntity_Last, 0 );
1805 aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
1807 SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
1808 while ( elemIt->more() )
1809 aVec[ elemIt->next()->GetEntityType() ]++;
1813 MapShapeNbElems tmpResMap;
1814 MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
1815 if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
1816 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
1817 aVec = srcResMap[ srcSubMesh ];
1819 return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
1822 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1823 aResMap.insert(std::make_pair(sm,aVec));
1829 //=============================================================================
1831 * \brief Sets a default event listener to submesh of the source face
1832 * \param subMesh - submesh where algo is set
1834 * This method is called when a submesh gets HYP_OK algo_state.
1835 * After being set, event listener is notified on each event of a submesh.
1836 * Arranges that CLEAN event is translated from source submesh to
1839 //=============================================================================
1841 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
1843 TAssocTool::SetEventListener( subMesh,
1844 _sourceHypo->GetSourceFace(),
1845 _sourceHypo->GetSourceMesh() );