1 // Copyright (C) 2007-2016 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_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_MesherHelper.hxx"
46 #include "SMESH_Pattern.hxx"
47 #include "SMESH_subMesh.hxx"
48 #include "SMESH_subMeshEventListener.hxx"
50 #include <utilities.h>
52 #include <BRepAdaptor_Surface.hxx>
53 #include <BRepMesh_Delaun.hxx>
54 #include <BRep_Tool.hxx>
55 #include <Bnd_B2d.hxx>
56 #include <GeomAPI_ProjectPointOnSurf.hxx>
57 #include <GeomLib_IsPlanarSurface.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
61 #include <TopTools_ListIteratorOfListOfShape.hxx>
62 #include <TopTools_MapOfShape.hxx>
64 #include <TopoDS_Solid.hxx>
67 #include <gp_GTrsf.hxx>
72 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
74 namespace TAssocTool = StdMeshers_ProjectionUtils;
75 //typedef StdMeshers_ProjectionUtils TAssocTool;
77 //=======================================================================
78 //function : StdMeshers_Projection_2D
80 //=======================================================================
82 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
83 :SMESH_2D_Algo(hypId, studyId, gen)
85 _name = "Projection_2D";
86 _compatibleHypothesis.push_back("ProjectionSource2D");
90 //================================================================================
94 //================================================================================
96 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
99 //=======================================================================
100 //function : CheckHypothesis
102 //=======================================================================
104 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh& theMesh,
105 const TopoDS_Shape& theShape,
106 SMESH_Hypothesis::Hypothesis_Status& theStatus)
108 list <const SMESHDS_Hypothesis * >::const_iterator itl;
110 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
111 if ( hyps.size() == 0 )
113 theStatus = HYP_MISSING;
114 return false; // can't work with no hypothesis
117 if ( hyps.size() > 1 )
119 theStatus = HYP_ALREADY_EXIST;
123 const SMESHDS_Hypothesis *theHyp = hyps.front();
125 string hypName = theHyp->GetName();
129 if (hypName == "ProjectionSource2D")
131 _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
133 // Check hypo parameters
135 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
136 SMESH_Mesh* tgtMesh = & theMesh;
141 if ( _sourceHypo->HasVertexAssociation() )
144 TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
145 ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
146 if ( edge.IsNull() ||
147 !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
148 !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
150 theStatus = HYP_BAD_PARAMETER;
151 error("Invalid source vertices");
152 SCRUTE((edge.IsNull()));
153 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
154 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
159 edge = TAssocTool::GetEdgeByVertices
160 ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
161 if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
163 theStatus = HYP_BAD_PARAMETER;
164 error("Invalid target vertices");
165 SCRUTE((edge.IsNull()));
166 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
169 else if ( !_sourceHypo->IsCompoundSource() &&
170 !SMESH_MesherHelper::IsSubShape( edge, theShape ))
172 theStatus = HYP_BAD_PARAMETER;
173 error("Invalid target vertices");
174 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
178 // check a source face
179 if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
180 ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
182 theStatus = HYP_BAD_PARAMETER;
183 error("Invalid source face");
184 SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
185 SCRUTE((srcMesh == tgtMesh));
186 SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
191 theStatus = HYP_INCOMPATIBLE;
193 return ( theStatus == HYP_OK );
198 //================================================================================
200 * \brief define if a node is new or old
201 * \param node - node to check
202 * \retval bool - true if the node existed before Compute() is called
204 //================================================================================
206 bool isOldNode( const SMDS_MeshNode* node )
208 // old nodes are shared by edges and new ones are shared
209 // only by faces created by mapper
210 //if ( is1DComputed )
212 bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
217 // SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
218 // bool isNew = invFace->more();
223 //================================================================================
225 * \brief Class to remove mesh built by pattern mapper on edges
226 * and vertices in the case of failure of projection algo.
227 * It does it's job at destruction
229 //================================================================================
234 MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
235 ~MeshCleaner() { Clean(sm); }
236 void Release() { sm = 0; } // mesh will not be removed
237 static void Clean( SMESH_subMesh* sm, bool withSub=true )
239 if ( !sm || !sm->GetSubMeshDS() ) return;
240 // PAL16567, 18920. Remove face nodes as well
241 // switch ( sm->GetSubShape().ShapeType() ) {
242 // case TopAbs_VERTEX:
243 // case TopAbs_EDGE: {
244 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
245 SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
246 while ( nIt->more() ) {
247 const SMDS_MeshNode* node = nIt->next();
248 if ( !isOldNode( node ) )
249 mesh->RemoveNode( node );
251 // do not break but iterate over DependsOn()
254 if ( !withSub ) return;
255 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
256 while ( smIt->more() )
257 Clean( smIt->next(), false );
262 //================================================================================
264 * \brief find new nodes belonging to one free border of mesh on face
265 * \param sm - submesh on edge or vertex containing nodes to choose from
266 * \param face - the face bound by the submesh
267 * \param u2nodes - map to fill with nodes
268 * \param seamNodes - set of found nodes
269 * \retval bool - is a success
271 //================================================================================
273 bool getBoundaryNodes ( SMESH_subMesh* sm,
274 const TopoDS_Face& face,
275 map< double, const SMDS_MeshNode* > & u2nodes,
276 set< const SMDS_MeshNode* > & seamNodes)
280 if ( !sm || !sm->GetSubMeshDS() )
281 RETURN_BAD_RESULT("Null submesh");
283 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
284 switch ( sm->GetSubShape().ShapeType() ) {
286 case TopAbs_VERTEX: {
287 while ( nIt->more() ) {
288 const SMDS_MeshNode* node = nIt->next();
289 if ( isOldNode( node ) ) continue;
290 u2nodes.insert( make_pair( 0., node ));
291 seamNodes.insert( node );
298 // Get submeshes of sub-vertices
299 const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
300 if ( subSM.size() != 2 )
301 RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
302 " but we have " << subSM.size());
303 SMESH_subMesh* smV1 = subSM.begin()->second;
304 SMESH_subMesh* smV2 = subSM.rbegin()->second;
305 if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
306 RETURN_BAD_RESULT("Empty vertex submeshes");
308 const SMDS_MeshNode* nV1 = 0;
309 const SMDS_MeshNode* nE = 0;
311 // Look for nV1 - a new node on V1
312 nIt = smV1->GetSubMeshDS()->GetNodes();
313 while ( nIt->more() && !nE ) {
314 const SMDS_MeshNode* node = nIt->next();
315 if ( isOldNode( node ) ) continue;
318 // Find nE - a new node connected to nV1 and belonging to edge submesh;
319 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
320 SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
321 while ( vElems->more() && !nE ) {
322 const SMDS_MeshElement* elem = vElems->next();
323 int nbNodes = elem->NbNodes();
324 if ( elem->IsQuadratic() )
326 int iV1 = elem->GetNodeIndex( nV1 );
327 // try next after nV1
328 int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
329 if ( smDS->Contains( elem->GetNode( iE ) ))
330 nE = elem->GetNode( iE );
332 // try node before nV1
333 iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
334 if ( smDS->Contains( elem->GetNode( iE )))
335 nE = elem->GetNode( iE );
337 if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
338 if ( Abs( iV1 - iE ) == 1 )
339 nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
341 nE = elem->GetNode( elem->NbNodes() - 1 );
346 RETURN_BAD_RESULT("No new node found on V1");
348 RETURN_BAD_RESULT("new node on edge not found");
350 // Get the whole free border of a face
351 list< const SMDS_MeshNode* > bordNodes;
352 list< const SMDS_MeshElement* > bordFaces;
353 if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
354 RETURN_BAD_RESULT("free border of a face not found by nodes " <<
355 nV1->GetID() << " " << nE->GetID() );
357 // Insert nodes of the free border to the map until node on V2 encountered
358 SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
359 list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
360 bordIt++; // skip nV1
361 for ( ; bordIt != bordNodes.end(); ++bordIt ) {
362 const SMDS_MeshNode* node = *bordIt;
363 if ( v2smDS->Contains( node ))
365 if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
366 RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
367 " pos type " << node->GetPosition()->GetTypeOfPosition());
368 const SMDS_EdgePosition* pos =
369 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
370 u2nodes.insert( make_pair( pos->GetUParameter(), node ));
371 seamNodes.insert( node );
373 if ( u2nodes.size() != seamNodes.size() )
374 RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
375 ", " << u2nodes.size() << " != " << seamNodes.size() );
380 RETURN_BAD_RESULT ("Unexpected submesh type");
382 } // bool getBoundaryNodes()
384 //================================================================================
386 * \brief Check if two consecutive EDGEs are connected in 2D
387 * \param [in] E1 - a well oriented non-seam EDGE
388 * \param [in] E2 - a possibly well oriented seam EDGE
389 * \param [in] F - a FACE
390 * \return bool - result
392 //================================================================================
394 bool are2dConnected( const TopoDS_Edge & E1,
395 const TopoDS_Edge & E2,
396 const TopoDS_Face & F )
399 Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( E1, F, f, l );
400 gp_Pnt2d uvFirst1 = c1->Value( f );
401 gp_Pnt2d uvLast1 = c1->Value( l );
403 Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( E2, F, f, l );
404 gp_Pnt2d uvFirst2 = c2->Value( E2.Orientation() == TopAbs_REVERSED ? l : f );
405 double tol2 = Max( Precision::PConfusion() * Precision::PConfusion(),
406 1e-5 * uvLast1.SquareDistance( uvFirst1 ));
408 return (( uvFirst2.SquareDistance( uvFirst1 ) < tol2 ) ||
409 ( uvFirst2.SquareDistance( uvLast1 ) < tol2 ));
412 //================================================================================
414 * \brief Compose TSideVector for both FACEs keeping matching order of EDGEs
415 * and fill src2tgtNodes map
417 //================================================================================
419 TError getWires(const TopoDS_Face& tgtFace,
420 const TopoDS_Face& srcFace,
421 SMESH_Mesh * tgtMesh,
422 SMESH_Mesh * srcMesh,
423 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
424 TSideVector& srcWires,
425 TSideVector& tgtWires,
426 TAssocTool::TNodeNodeMap& src2tgtNodes,
429 src2tgtNodes.clear();
431 // get ordered src EDGEs
433 srcWires = StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*skipMediumNodes=*/0, err);
434 if (( err && !err->IsOK() ) ||
435 ( srcWires.empty() ))
438 SMESH_MesherHelper srcHelper( *srcMesh );
439 srcHelper.SetSubShape( srcFace );
441 // make corresponding sequence of tgt EDGEs
442 tgtWires.resize( srcWires.size() );
443 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
445 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
447 list< TopoDS_Edge > tgtEdges;
448 TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
449 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
451 TopoDS_Edge srcE = srcWire->Edge( iE );
452 TopoDS_Edge tgtE = TopoDS::Edge( shape2ShapeMap( srcE, /*isSrc=*/true));
453 TopoDS_Shape srcEbis = shape2ShapeMap( tgtE, /*isSrc=*/false );
454 if ( srcE.Orientation() != srcEbis.Orientation() )
456 // reverse a seam edge encountered for the second time
457 const int index = edgeMap.Add( tgtE );
458 if ( index < edgeMap.Extent() ) // E is a seam
460 // check which of edges to reverse, E or one already being in tgtEdges
461 if ( are2dConnected( tgtEdges.back(), tgtE, tgtFace ))
463 list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
464 std::advance( eIt, index-1 );
465 if ( are2dConnected( tgtEdges.back(), *eIt, tgtFace ))
473 if ( srcWire->NbEdges() == 1 && tgtMesh == srcMesh ) // circle
475 // try to verify ori by propagation
476 pair<int,TopoDS_Edge> nE =
477 StdMeshers_ProjectionUtils::GetPropagationEdge( srcMesh, tgtE, srcE );
478 if ( !nE.second.IsNull() )
481 tgtEdges.push_back( tgtE );
484 tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
485 /*theIsForward = */ true,
486 /*theIgnoreMediumNodes = */false));
487 StdMeshers_FaceSidePtr tgtWire = tgtWires[ iW ];
489 // Fill map of src to tgt nodes with nodes on edges
491 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
493 if ( srcMesh->GetSubMesh( srcWire->Edge(iE) )->IsEmpty() ||
494 tgtMesh->GetSubMesh( tgtWire->Edge(iE) )->IsEmpty() )
496 // add nodes on VERTEXes for a case of not meshes EDGEs
497 const SMDS_MeshNode* srcN = srcWire->VertexNode( iE );
498 const SMDS_MeshNode* tgtN = tgtWire->VertexNode( iE );
500 src2tgtNodes.insert( make_pair( srcN, tgtN ));
504 const bool skipMedium = true, isFwd = true;
505 StdMeshers_FaceSide srcEdge( srcFace, srcWire->Edge(iE), srcMesh, isFwd, skipMedium);
506 StdMeshers_FaceSide tgtEdge( tgtFace, tgtWire->Edge(iE), tgtMesh, isFwd, skipMedium);
508 vector< const SMDS_MeshNode* > srcNodes = srcEdge.GetOrderedNodes();
509 vector< const SMDS_MeshNode* > tgtNodes = tgtEdge.GetOrderedNodes();
511 if (( srcNodes.size() != tgtNodes.size() ) && tgtNodes.size() > 0 )
512 return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
513 "Different number of nodes on edges");
514 if ( !tgtNodes.empty() )
516 vector< const SMDS_MeshNode* >::iterator tn = tgtNodes.begin();
517 //if ( srcWire->Edge(iE).Orientation() == tgtWire->Edge(iE).Orientation() )
519 vector< const SMDS_MeshNode* >::iterator sn = srcNodes.begin();
520 for ( ; tn != tgtNodes.end(); ++tn, ++sn)
521 src2tgtNodes.insert( make_pair( *sn, *tn ));
525 // vector< const SMDS_MeshNode* >::reverse_iterator sn = srcNodes.rbegin();
526 // for ( ; tn != tgtNodes.end(); ++tn, ++sn)
527 // src2tgtNodes.insert( make_pair( *sn, *tn ));
532 } // loop on EDGEs of a WIRE
539 //================================================================================
541 * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
542 * if projection by 3D transformation is possible
544 //================================================================================
546 bool projectPartner(const TopoDS_Face& tgtFace,
547 const TopoDS_Face& srcFace,
548 const TSideVector& tgtWires,
549 const TSideVector& srcWires,
550 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
551 TAssocTool::TNodeNodeMap& src2tgtNodes,
552 const bool is1DComputed)
554 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
555 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
556 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
557 SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
558 SMESH_MesherHelper helper( *tgtMesh );
560 const double tol = 1.e-7 * srcMeshDS->getMaxDim();
562 // transformation to get location of target nodes from source ones
563 StdMeshers_ProjectionUtils::TrsfFinder3D trsf;
564 bool trsfIsOK = false;
565 if ( tgtFace.IsPartner( srcFace ))
567 gp_GTrsf srcTrsf = srcFace.Location().Transformation();
568 gp_GTrsf tgtTrsf = tgtFace.Location().Transformation();
569 gp_GTrsf t = srcTrsf.Inverted().Multiplied( tgtTrsf );
572 gp_Pnt srcP = BRep_Tool::Pnt( srcWires[0]->FirstVertex() );
573 gp_Pnt tgtP = BRep_Tool::Pnt( tgtWires[0]->FirstVertex() );
574 trsfIsOK = ( tgtP.Distance( trsf.Transform( srcP )) < tol );
577 trsf.Set( tgtTrsf.Inverted().Multiplied( srcTrsf ));
578 trsfIsOK = ( tgtP.Distance( trsf.Transform( srcP )) < tol );
583 // Try to find the 3D transformation
585 const int totNbSeg = 50;
586 vector< gp_XYZ > srcPnts, tgtPnts;
587 srcPnts.reserve( totNbSeg );
588 tgtPnts.reserve( totNbSeg );
589 gp_XYZ srcBC( 0,0,0 ), tgtBC( 0,0,0 );
590 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
592 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
593 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
595 size_t nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
596 double srcU = srcWires[iW]->FirstParameter( iE );
597 double tgtU = tgtWires[iW]->FirstParameter( iE );
598 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
599 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
600 for ( size_t i = 0; i < nbSeg; ++i )
602 srcPnts.push_back( srcWires[iW]->Value3d( srcU ).XYZ() );
603 tgtPnts.push_back( tgtWires[iW]->Value3d( tgtU ).XYZ() );
606 srcBC += srcPnts.back();
607 tgtBC += tgtPnts.back();
611 if ( !trsf.Solve( srcPnts, tgtPnts ))
616 const int nbTestPnt = 20;
617 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
619 gp_Pnt trsfTgt = trsf.Transform( srcBC / srcPnts.size() );
620 trsfIsOK = ( trsfTgt.SquareDistance( tgtBC / tgtPnts.size() ) < tol*tol );
621 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
623 gp_Pnt trsfTgt = trsf.Transform( srcPnts[i] );
624 trsfIsOK = ( trsfTgt.SquareDistance( tgtPnts[i] ) < tol*tol );
626 // check an in-FACE point
629 BRepAdaptor_Surface srcSurf( srcFace );
631 srcSurf.Value( 0.321 * ( srcSurf.FirstUParameter() + srcSurf.LastUParameter() ),
632 0.123 * ( srcSurf.FirstVParameter() + srcSurf.LastVParameter() ));
633 gp_Pnt tgtTrsfP = trsf.Transform( srcP );
635 GeomAPI_ProjectPointOnSurf& proj = helper.GetProjector( tgtFace, loc, 0.1*tol );
636 if ( !loc.IsIdentity() )
637 tgtTrsfP.Transform( loc.Transformation().Inverted() );
638 proj.Perform( tgtTrsfP );
639 trsfIsOK = ( proj.IsDone() &&
640 proj.NbPoints() > 0 &&
641 proj.LowerDistance() < tol );
649 // prepare the helper to adding quadratic elements if necessary
650 //helper.SetSubShape( tgtFace );
651 helper.IsQuadraticSubMesh( tgtFace );
653 SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
654 if ( !is1DComputed && srcSubDS->NbElements() )
655 helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
657 SMESH_MesherHelper srcHelper( *srcMesh );
658 srcHelper.SetSubShape( srcFace );
660 const SMDS_MeshNode* nullNode = 0;
661 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
663 // indices of nodes to create properly oriented faces
664 bool isReverse = ( !trsf.IsIdentity() );
665 int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
667 std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
669 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
670 vector< const SMDS_MeshNode* > tgtNodes;
671 while ( elemIt->more() ) // loop on all mesh faces on srcFace
673 const SMDS_MeshElement* elem = elemIt->next();
674 const int nbN = elem->NbCornerNodes();
675 tgtNodes.resize( nbN );
676 helper.SetElementsOnShape( false );
677 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
679 const SMDS_MeshNode* srcNode = elem->GetNode(i);
680 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
681 if ( srcN_tgtN->second == nullNode )
684 gp_Pnt tgtP = trsf.Transform( SMESH_TNodeXYZ( srcNode ));
685 SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
686 srcN_tgtN->second = n;
687 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
691 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode );
692 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), srcUV.X(), srcUV.Y() );
697 const TopoDS_Shape & srcE = srcMeshDS->IndexToShape( srcNode->getshapeId() );
698 const TopoDS_Shape & tgtE = shape2ShapeMap( srcE, /*isSrc=*/true );
699 double srcU = srcHelper.GetNodeU( TopoDS::Edge( srcE ), srcNode );
700 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtE ), srcU );
703 case SMDS_TOP_VERTEX:
705 const TopoDS_Shape & srcV = srcMeshDS->IndexToShape( srcNode->getshapeId() );
706 const TopoDS_Shape & tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
707 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
713 tgtNodes[i] = srcN_tgtN->second;
716 helper.SetElementsOnShape( true );
719 case 3: helper.AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
720 case 4: helper.AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
722 if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
723 helper.AddPolygonalFace( tgtNodes );
727 // check node positions
729 if ( !tgtFace.IsPartner( srcFace ) )
731 SMESH_MesherHelper edgeHelper( *tgtMesh );
732 edgeHelper.ToFixNodeParameters( true );
733 helper.ToFixNodeParameters( true );
737 const double tol2d = 1e-12;
738 srcN_tgtN = src2tgtNodes.begin();
739 for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
741 const SMDS_MeshNode* n = srcN_tgtN->second;
742 switch ( n->GetPosition()->GetTypeOfPosition() )
746 if ( nbOkPos > 10 ) break;
747 gp_XY uv = helper.GetNodeUV( tgtFace, n ), uvBis = uv;
748 if (( helper.CheckNodeUV( tgtFace, n, uv, tol )) &&
749 (( uv - uvBis ).SquareModulus() < tol2d ))
752 nbOkPos = -((int) src2tgtNodes.size() );
757 const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
758 edgeHelper.SetSubShape( tgtE );
759 edgeHelper.GetNodeU( tgtE, n, 0, &toCheck );
769 } // bool projectPartner()
771 //================================================================================
773 * \brief Preform projection in case if the faces are similar in 2D space
775 //================================================================================
777 bool projectBy2DSimilarity(const TopoDS_Face& tgtFace,
778 const TopoDS_Face& srcFace,
779 const TSideVector& tgtWires,
780 const TSideVector& srcWires,
781 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
782 TAssocTool::TNodeNodeMap& src2tgtNodes,
783 const bool is1DComputed)
785 SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
786 SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
788 // WARNING: we can have problems if the FACE is symmetrical in 2D,
789 // then the projection can be mirrored relating to what is expected
791 // 1) Find 2D transformation
793 StdMeshers_ProjectionUtils::TrsfFinder2D trsf;
795 // get 2 pairs of corresponding UVs
796 gp_Pnt2d srcP0 = srcWires[0]->Value2d(0.0);
797 gp_Pnt2d srcP1 = srcWires[0]->Value2d(0.333);
798 gp_Pnt2d tgtP0 = tgtWires[0]->Value2d(0.0);
799 gp_Pnt2d tgtP1 = tgtWires[0]->Value2d(0.333);
801 // make transformation
802 gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
803 gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
804 gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
805 toSrcCS .SetTransformation( srcCS );
806 fromTgtCS.SetTransformation( tgtCS );
808 trsf.Set( fromTgtCS * toSrcCS );
810 // check transformation
811 bool trsfIsOK = true;
812 const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
813 for ( double u = 0.12; ( u < 1. && trsfIsOK ); u += 0.1 )
815 gp_Pnt2d srcUV = srcWires[0]->Value2d( u );
816 gp_Pnt2d tgtUV = tgtWires[0]->Value2d( u );
817 gp_Pnt2d tgtUV2 = trsf.Transform( srcUV );
818 trsfIsOK = ( tgtUV.Distance( tgtUV2 ) < tol );
821 // Find trsf using a least-square approximation
825 const int totNbSeg = 50;
826 vector< gp_XY > srcPnts, tgtPnts;
827 srcPnts.reserve( totNbSeg );
828 tgtPnts.reserve( totNbSeg );
829 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
831 const double minSegLen = srcWires[iW]->Length() / totNbSeg;
832 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
834 size_t nbSeg = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
835 double srcU = srcWires[iW]->FirstParameter( iE );
836 double tgtU = tgtWires[iW]->FirstParameter( iE );
837 double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
838 double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
839 for ( size_t i = 0; i < nbSeg; ++i, srcU += srcDu, tgtU += tgtDu )
841 srcPnts.push_back( srcWires[iW]->Value2d( srcU ).XY() );
842 tgtPnts.push_back( tgtWires[iW]->Value2d( tgtU ).XY() );
846 if ( !trsf.Solve( srcPnts, tgtPnts ))
852 const int nbTestPnt = 10;
853 const size_t iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
854 for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
856 gp_Pnt2d trsfTgt = trsf.Transform( srcPnts[i] );
857 trsfIsOK = ( trsfTgt.Distance( tgtPnts[i] ) < tol );
862 } // "Find transformation" block
866 SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
868 SMESH_MesherHelper helper( *tgtMesh );
869 helper.SetSubShape( tgtFace );
871 helper.IsQuadraticSubMesh( tgtFace );
873 helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
874 helper.SetElementsOnShape( true );
875 Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
876 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
878 SMESH_MesherHelper srcHelper( *srcMesh );
879 srcHelper.SetSubShape( srcFace );
881 const SMDS_MeshNode* nullNode = 0;
882 TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
884 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
885 vector< const SMDS_MeshNode* > tgtNodes;
887 while ( elemIt->more() ) // loop on all mesh faces on srcFace
889 const SMDS_MeshElement* elem = elemIt->next();
890 const int nbN = elem->NbCornerNodes();
891 tgtNodes.resize( nbN );
892 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
894 const SMDS_MeshNode* srcNode = elem->GetNode(i);
895 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
896 if ( srcN_tgtN->second == nullNode )
899 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
900 elem->GetNode( helper.WrapIndex(i+1,nbN)), &uvOK);
901 gp_Pnt2d tgtUV = trsf.Transform( srcUV );
902 gp_Pnt tgtP = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
903 SMDS_MeshNode* n = tgtMeshDS->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
904 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
906 case SMDS_TOP_FACE: {
907 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
910 case SMDS_TOP_EDGE: {
911 TopoDS_Shape srcEdge = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
912 TopoDS_Edge tgtEdge = TopoDS::Edge( shape2ShapeMap( srcEdge, /*isSrc=*/true ));
913 double U = Precision::Infinite();
914 helper.CheckNodeU( tgtEdge, n, U, Precision::PConfusion());
915 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtEdge ), U );
918 case SMDS_TOP_VERTEX: {
919 TopoDS_Shape srcV = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
920 TopoDS_Shape tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
921 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
926 srcN_tgtN->second = n;
928 tgtNodes[i] = srcN_tgtN->second;
930 // create a new face (with reversed orientation)
933 case 3: helper.AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
934 case 4: helper.AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
936 } // loop on all mesh faces on srcFace
941 //================================================================================
943 * \brief Preform projection in case of quadrilateral faces
945 //================================================================================
947 bool projectQuads(const TopoDS_Face& tgtFace,
948 const TopoDS_Face& srcFace,
949 const TSideVector& tgtWires,
950 const TSideVector& srcWires,
951 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
952 TAssocTool::TNodeNodeMap& src2tgtNodes,
953 const bool is1DComputed)
955 // SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
956 // SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
957 // //SMESHDS_Mesh * tgtMeshDS = tgtMesh->GetMeshDS();
958 // SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
960 // if ( srcWires[0]->NbEdges() != 4 )
962 // if ( !is1DComputed )
964 // for ( int iE = 0; iE < 4; ++iE )
966 // SMESHDS_SubMesh* sm = srcMeshDS->MeshElements( srcWires[0]->Edge( iE ));
967 // if ( !sm ) return false;
968 // if ( sm->NbNodes() + sm->NbElements() == 0 ) return false;
970 // if ( BRepAdaptor_Surface( tgtFace ).GetType() != GeomAbs_Plane )
972 // // if ( BRepAdaptor_Surface( tgtFace ).GetType() == GeomAbs_Plane &&
973 // // BRepAdaptor_Surface( srcFace ).GetType() == GeomAbs_Plane )
974 // // return false; // too easy
976 // // load EDGEs to SMESH_Block
978 // SMESH_Block block;
979 // TopTools_IndexedMapOfOrientedShape blockSubShapes;
981 // const TopoDS_Solid& box = srcMesh->PseudoShape();
982 // TopoDS_Shell shell = TopoDS::Shell( TopExp_Explorer( box, TopAbs_SHELL ).Current() );
984 // block.LoadBlockShapes( shell, v, v, blockSubShapes ); // fill all since operator[] is missing
986 // const SMESH_Block::TShapeID srcFaceBID = SMESH_Block::ID_Fxy0;
987 // const SMESH_Block::TShapeID tgtFaceBID = SMESH_Block::ID_Fxy1;
988 // vector< int > edgeBID;
989 // block.GetFaceEdgesIDs( srcFaceBID, edgeBID ); // u0, u1, 0v, 1v
990 // blockSubShapes.Substitute( edgeBID[0], srcWires[0]->Edge(0) );
991 // blockSubShapes.Substitute( edgeBID[1], srcWires[0]->Edge(2) );
992 // blockSubShapes.Substitute( edgeBID[2], srcWires[0]->Edge(3) );
993 // blockSubShapes.Substitute( edgeBID[3], srcWires[0]->Edge(1) );
994 // block.GetFaceEdgesIDs( tgtFaceBID, edgeBID ); // u0, u1, 0v, 1v
995 // blockSubShapes.Substitute( edgeBID[0], tgtWires[0]->Edge(0) );
996 // blockSubShapes.Substitute( edgeBID[1], tgtWires[0]->Edge(2) );
997 // blockSubShapes.Substitute( edgeBID[2], tgtWires[0]->Edge(3) );
998 // blockSubShapes.Substitute( edgeBID[3], tgtWires[0]->Edge(1) );
999 // block.LoadFace( srcFace, srcFaceBID, blockSubShapes );
1000 // block.LoadFace( tgtFace, tgtFaceBID, blockSubShapes );
1002 // // remember connectivity of new faces in terms of ( node-or-XY )
1004 // typedef std::pair< const SMDS_MeshNode*, gp_XYZ > TNodeOrXY; // node-or-XY
1005 // typedef std::vector< TNodeOrXY* > TFaceConn; // face connectivity
1006 // std::vector< TFaceConn > newFacesVec; // connectivity of all faces
1007 // std::map< const SMDS_MeshNode*, TNodeOrXY > srcNode2tgtNXY; // src node -> node-or-XY
1009 // TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
1010 // std::map< const SMDS_MeshNode*, TNodeOrXY >::iterator srcN_tgtNXY;
1011 // std::pair< std::map< const SMDS_MeshNode*, TNodeOrXY >::iterator, bool > n2n_isNew;
1012 // TNodeOrXY nullNXY( (SMDS_MeshNode*)NULL, gp_XYZ(0,0,0) );
1014 // SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
1015 // newFacesVec.resize( srcSubDS->NbElements() );
1016 // int iFaceSrc = 0;
1018 // SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
1019 // while ( elemIt->more() ) // loop on all mesh faces on srcFace
1021 // const SMDS_MeshElement* elem = elemIt->next();
1022 // TFaceConn& tgtNodes = newFacesVec[ iFaceSrc++ ];
1024 // const int nbN = elem->NbCornerNodes();
1025 // tgtNodes.resize( nbN );
1026 // for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
1028 // const SMDS_MeshNode* srcNode = elem->GetNode(i);
1029 // n2n_isNew = srcNode2tgtNXY.insert( make_pair( srcNode, nullNXY ));
1030 // TNodeOrXY & tgtNodeOrXY = n2n_isNew.first->second;
1031 // if ( n2n_isNew.second ) // new src node encounters
1033 // srcN_tgtN = src2tgtNodes.find( srcNode );
1034 // if ( srcN_tgtN != src2tgtNodes.end() )
1036 // tgtNodeOrXY.first = srcN_tgtN->second; // tgt node exists
1040 // // find XY of src node withing the quadrilateral srcFace
1041 // if ( !block.ComputeParameters( SMESH_TNodeXYZ( srcNode ),
1042 // tgtNodeOrXY.second, srcFaceBID ))
1046 // tgtNodes[ i ] = & tgtNodeOrXY;
1050 // // as all XY are computed, create tgt nodes and faces
1052 // SMESH_MesherHelper helper( *tgtMesh );
1053 // helper.SetSubShape( tgtFace );
1054 // if ( is1DComputed )
1055 // helper.IsQuadraticSubMesh( tgtFace );
1057 // helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
1058 // helper.SetElementsOnShape( true );
1059 // Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
1061 // SMESH_MesherHelper srcHelper( *srcMesh );
1062 // srcHelper.SetSubShape( srcFace );
1064 // vector< const SMDS_MeshNode* > tgtNodes;
1067 // for ( size_t iFaceTgt = 0; iFaceTgt < newFacesVec.size(); ++iFaceTgt )
1069 // TFaceConn& tgtConn = newFacesVec[ iFaceTgt ];
1070 // tgtNodes.resize( tgtConn.size() );
1071 // for ( size_t iN = 0; iN < tgtConn.size(); ++iN )
1073 // const SMDS_MeshNode* & tgtN = tgtConn[ iN ]->first;
1074 // if ( !tgtN ) // create a node
1076 // if ( !block.FaceUV( tgtFaceBID, tgtConn[iN]->second, uv ))
1078 // gp_Pnt p = tgtSurface->Value( uv.X(), uv.Y() );
1079 // tgtN = helper.AddNode( p.X(), p.Y(), p.Z(), uv.X(), uv.Y() );
1081 // tgtNodes[ tgtNodes.size() - iN - 1] = tgtN; // reversed orientation
1083 // switch ( tgtNodes.size() )
1085 // case 3: helper.AddFace(tgtNodes[0], tgtNodes[1], tgtNodes[2]); break;
1086 // case 4: helper.AddFace(tgtNodes[0], tgtNodes[1], tgtNodes[2], tgtNodes[3]); break;
1088 // if ( tgtNodes.size() > 4 )
1089 // helper.AddPolygonalFace( tgtNodes );
1092 return false; //true;
1094 } // bool projectQuads(...)
1096 //================================================================================
1098 * \brief Fix bad faces by smoothing
1100 //================================================================================
1102 bool fixDistortedFaces( SMESH_MesherHelper& helper,
1103 TSideVector& tgtWires )
1105 SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMesh( helper.GetSubShape() );
1107 if ( helper.IsDistorted2D( faceSM, /*checkUV=*/true ))
1109 SMESH_MeshEditor editor( helper.GetMesh() );
1110 SMESHDS_SubMesh* smDS = faceSM->GetSubMeshDS();
1111 const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
1113 TIDSortedElemSet faces;
1114 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1115 for ( faceIt = smDS->GetElements(); faceIt->more(); )
1116 faces.insert( faces.end(), faceIt->next() );
1118 // choose smoothing algo
1119 //SMESH_MeshEditor:: SmoothMethod algo = SMESH_MeshEditor::CENTROIDAL;
1120 bool isConcaveBoundary = false;
1121 for ( size_t iW = 0; iW < tgtWires.size() && !isConcaveBoundary; ++iW )
1123 TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
1124 for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
1126 double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
1127 F, tgtWires[iW]->FirstVertex( iE ));
1128 isConcaveBoundary = ( angle < -5. * M_PI / 180. );
1130 prevEdge = tgtWires[iW]->Edge( iE );
1133 SMESH_MeshEditor:: SmoothMethod algo =
1134 isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
1136 // smooth in 2D or 3D?
1137 TopLoc_Location loc;
1138 Handle(Geom_Surface) surface = BRep_Tool::Surface( F, loc );
1139 bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
1142 set<const SMDS_MeshNode*> fixedNodes;
1143 editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
1144 /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
1146 helper.ToFixNodeParameters( true );
1148 return !helper.IsDistorted2D( faceSM, /*checkUV=*/true );
1153 typedef list< pair< const SMDS_MeshNode*, const BRepMesh_Triangle* > > TNodeTriaList;
1155 //================================================================================
1157 * \brief Add in-FACE nodes surrounding a given node to a queue
1159 //================================================================================
1161 void addCloseNodes( const SMDS_MeshNode* srcNode,
1162 const BRepMesh_Triangle* bmTria,
1163 const int srcFaceID,
1164 TNodeTriaList & noTriQueue )
1166 // find in-FACE nodes
1167 SMDS_ElemIteratorPtr elems = srcNode->GetInverseElementIterator(SMDSAbs_Face);
1168 while ( elems->more() )
1170 const SMDS_MeshElement* elem = elems->next();
1171 if ( elem->getshapeId() == srcFaceID )
1173 for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
1175 const SMDS_MeshNode* n = elem->GetNode( i );
1176 if ( !n->isMarked() )
1177 noTriQueue.push_back( make_pair( n, bmTria ));
1183 //================================================================================
1185 * \brief Find a delauney triangle containing a given 2D point and return
1186 * barycentric coordinates within the found triangle
1188 //================================================================================
1190 const BRepMesh_Triangle* findTriangle( const gp_XY& uv,
1191 const BRepMesh_Triangle* bmTria,
1192 Handle(BRepMesh_DataStructureOfDelaun)& triaDS,
1198 Standard_Boolean ori[3];
1204 triaDS->ElementNodes( *bmTria, nodeIDs );
1205 nodeUVs[0] = triaDS->GetNode( nodeIDs[0] ).Coord();
1206 nodeUVs[1] = triaDS->GetNode( nodeIDs[1] ).Coord();
1207 nodeUVs[2] = triaDS->GetNode( nodeIDs[2] ).Coord();
1209 SMESH_MeshAlgos::GetBarycentricCoords( uv,
1210 nodeUVs[0], nodeUVs[1], nodeUVs[2],
1212 if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
1214 bc[2] = 1 - bc[0] - bc[1];
1218 // look for a neighbor triangle, which is adjacent to a link intersected
1219 // by a segment( triangle center -> uv )
1221 gp_XY gc = ( nodeUVs[0] + nodeUVs[1] + nodeUVs[2] ) / 3.;
1222 gp_XY seg = uv - gc;
1224 bmTria->Edges( linkIDs, ori );
1225 int triaID = triaDS->IndexOf( *bmTria );
1228 for ( int i = 0; i < 3; ++i )
1230 const BRepMesh_PairOfIndex & triIDs = triaDS->ElementsConnectedTo( linkIDs[i] );
1231 if ( triIDs.Extent() < 2 )
1232 continue; // no neighbor triangle
1234 // check if a link intersects gc2uv
1235 const BRepMesh_Edge & link = triaDS->GetLink( linkIDs[i] );
1236 const BRepMesh_Vertex & n1 = triaDS->GetNode( link.FirstNode() );
1237 const BRepMesh_Vertex & n2 = triaDS->GetNode( link.LastNode() );
1238 gp_XY uv1 = n1.Coord();
1239 gp_XY lin = n2.Coord() - uv1; // link direction
1241 double crossSegLin = seg ^ lin;
1242 if ( Abs( crossSegLin ) < std::numeric_limits<double>::min() )
1243 continue; // parallel
1245 double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
1246 if ( 0. <= uSeg && uSeg <= 1. )
1248 bmTria = & triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
1256 //================================================================================
1258 * \brief Morph mesh on the target face to lie within FACE boundary w/o distortion
1261 * - make a CDT on the src FACE
1262 * - find a triangle containing a src node and get its barycentric coordinates
1263 * - move the node to a point with the same barycentric coordinates in a corresponding
1266 //================================================================================
1268 bool morph( SMESH_MesherHelper& tgtHelper,
1269 const TopoDS_Face& tgtFace,
1270 const TopoDS_Face& srcFace,
1271 const TSideVector& tgtWires,
1272 const TSideVector& srcWires,
1273 const TAssocTool::TNodeNodeMap& src2tgtNodes )
1275 if ( srcWires.size() != tgtWires.size() ) return false;
1276 if ( srcWires.size() == 1 ) return false; // tmp
1278 // count boundary points
1279 int iP = 1, nbP = 0;
1280 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1281 nbP += srcWires[iW]->NbPoints() - 1; // 1st and last points coincide
1283 // fill boundary points
1284 BRepMesh::Array1OfVertexOfDelaun srcVert( 1, 1 + nbP ), tgtVert( 1, 1 + nbP );
1285 vector< const SMDS_MeshNode* > bndSrcNodes( nbP + 1 ); bndSrcNodes[0] = 0;
1286 BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
1287 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1289 const UVPtStructVec& srcPnt = srcWires[iW]->GetUVPtStruct();
1290 const UVPtStructVec& tgtPnt = tgtWires[iW]->GetUVPtStruct();
1291 if ( srcPnt.size() != tgtPnt.size() ) return false;
1293 for ( int i = 0, nb = srcPnt.size() - 1; i < nb; ++i, ++iP )
1295 bndSrcNodes[ iP ] = srcPnt[i].node;
1296 srcPnt[i].node->setIsMarked( true );
1298 v.ChangeCoord() = srcPnt[i].UV();
1300 v.ChangeCoord() = tgtPnt[i].UV();
1304 // triangulate the srcFace in 2D
1305 BRepMesh_Delaun delauney( srcVert );
1306 Handle(BRepMesh_DataStructureOfDelaun) triaDS = delauney.Result();
1308 Handle(ShapeAnalysis_Surface) tgtSurface = tgtHelper.GetSurface( tgtFace );
1309 SMESHDS_Mesh* srcMesh = srcWires[0]->GetMesh()->GetMeshDS();
1310 SMESHDS_Mesh* tgtMesh = tgtHelper.GetMeshDS();
1311 const SMDS_MeshNode *srcNode, *tgtNode;
1312 const BRepMesh_Triangle *bmTria;
1314 // un-mark internal src nodes; later we will mark moved nodes
1315 SMDS_NodeIteratorPtr nIt = srcMesh->MeshElements( srcFace )->GetNodes();
1316 if ( !nIt || !nIt->more() ) return true;
1317 while ( nIt->more() )
1318 ( srcNode = nIt->next() )->setIsMarked( false );
1320 // initialize a queue of nodes with starting triangles
1321 const int srcFaceID = srcNode->getshapeId();
1322 TNodeTriaList noTriQueue;
1323 size_t iBndSrcN = 1;
1324 for ( ; iBndSrcN < bndSrcNodes.size() && noTriQueue.empty(); ++iBndSrcN )
1327 const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1328 const BRepMesh_PairOfIndex & triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1329 const BRepMesh_Triangle& tria = triaDS->GetElement( triaIds.Index(1) );
1331 addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1336 double bc[3]; // barycentric coordinates
1338 bool checkUV = true;
1339 const SMDS_FacePosition* pos;
1341 while ( !noTriQueue.empty() )
1343 srcNode = noTriQueue.front().first;
1344 bmTria = noTriQueue.front().second;
1345 noTriQueue.pop_front();
1346 if ( srcNode->isMarked() )
1348 srcNode->setIsMarked( true );
1350 // find a delauney triangle containing the src node
1351 gp_XY uv = tgtHelper.GetNodeUV( srcFace, srcNode, NULL, &checkUV );
1352 bmTria = findTriangle( uv, bmTria, triaDS, bc );
1356 // compute new coordinates for a corresponding tgt node
1357 gp_XY uvNew( 0., 0. ), nodeUV;
1358 triaDS->ElementNodes( *bmTria, nodeIDs );
1359 for ( int i = 0; i < 3; ++i )
1360 uvNew += bc[i] * tgtVert( nodeIDs[i]).Coord();
1361 gp_Pnt xyz = tgtSurface->Value( uvNew );
1363 // find and move tgt node
1364 TAssocTool::TNodeNodeMap::const_iterator n2n = src2tgtNodes.find( srcNode );
1365 if ( n2n == src2tgtNodes.end() ) continue;
1366 tgtNode = n2n->second;
1367 tgtMesh->MoveNode( tgtNode, xyz.X(), xyz.Y(), xyz.Z() );
1369 if (( pos = dynamic_cast< const SMDS_FacePosition* >( tgtNode->GetPosition() )))
1370 const_cast<SMDS_FacePosition*>( pos )->SetParameters( uvNew.X(), uvNew.Y() );
1372 addCloseNodes( srcNode, bmTria, srcFaceID, noTriQueue );
1374 // assure that all src nodes are visited
1375 for ( ; iBndSrcN < bndSrcNodes.size() && noTriQueue.empty(); ++iBndSrcN )
1377 const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1378 const BRepMesh_PairOfIndex & triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1379 const BRepMesh_Triangle& tria = triaDS->GetElement( triaIds.Index(1) );
1380 addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1387 //=======================================================================
1389 * Set initial association of VERTEXes for the case of projection
1390 * from a quadrangle FACE to a closed FACE, where opposite src EDGEs
1391 * have different nb of segments
1393 //=======================================================================
1395 void initAssoc4Quad2Closed(const TopoDS_Shape& tgtFace,
1396 SMESH_MesherHelper& tgtHelper,
1397 const TopoDS_Shape& srcFace,
1398 SMESH_Mesh* srcMesh,
1399 TAssocTool::TShapeShapeMap & assocMap)
1401 if ( !tgtHelper.HasRealSeam() || srcFace.ShapeType() != TopAbs_FACE )
1402 return; // no seam edge
1403 list< TopoDS_Edge > tgtEdges, srcEdges;
1404 list< int > tgtNbEW, srcNbEW;
1405 int tgtNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( tgtFace ), tgtEdges, tgtNbEW );
1406 int srcNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( srcFace ), srcEdges, srcNbEW );
1407 if ( tgtNbW != 1 || srcNbW != 1 ||
1408 tgtNbEW.front() != 4 || srcNbEW.front() != 4 )
1409 return; // not quads
1412 list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1413 for ( int i = 0; edgeS != srcEdges.end(); ++i, ++edgeS )
1414 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *edgeS ))
1415 srcNbSeg[ i ] = sm->NbNodes();
1417 return; // not meshed
1418 if ( srcNbSeg[0] == srcNbSeg[2] && srcNbSeg[1] == srcNbSeg[3] )
1419 return; // same nb segments
1420 if ( srcNbSeg[0] != srcNbSeg[2] && srcNbSeg[1] != srcNbSeg[3] )
1421 return; // all different nb segments
1423 edgeS = srcEdges.begin();
1424 if ( srcNbSeg[0] != srcNbSeg[2] )
1426 TAssocTool::InsertAssociation( tgtHelper.IthVertex( 0,*edgeT ),
1427 tgtHelper.IthVertex( 0,*edgeS ), assocMap );
1428 TAssocTool::InsertAssociation( tgtHelper.IthVertex( 1,*edgeT ),
1429 tgtHelper.IthVertex( 1,*edgeS ), assocMap );
1435 //=======================================================================
1436 //function : Compute
1438 //=======================================================================
1440 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1442 _src2tgtNodes.clear();
1447 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1448 SMESH_Mesh * tgtMesh = & theMesh;
1452 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1453 SMESH_MesherHelper helper( theMesh );
1455 // ---------------------------
1456 // Make sub-shapes association
1457 // ---------------------------
1459 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1460 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1462 helper.SetSubShape( tgtFace );
1464 TAssocTool::TShapeShapeMap shape2ShapeMap;
1465 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1466 if ( shape2ShapeMap.IsEmpty() )
1467 initAssoc4Quad2Closed( tgtFace, helper, srcShape, srcMesh, shape2ShapeMap );
1468 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1470 !shape2ShapeMap.IsBound( tgtFace ))
1472 if ( srcShape.ShapeType() == TopAbs_FACE )
1474 int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1475 int nbE2 = helper.Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1477 return error(COMPERR_BAD_SHAPE,
1478 SMESH_Comment("Different number of edges in source and target faces: ")
1479 << nbE2 << " and " << nbE1 );
1481 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1483 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1485 // ----------------------------------------------
1486 // Assure that mesh on a source Face is computed
1487 // ----------------------------------------------
1489 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1490 SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1492 string srcMeshError;
1493 if ( tgtMesh == srcMesh ) {
1494 if ( !TAssocTool::MakeComputed( srcSubMesh ))
1495 srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1498 if ( !srcSubMesh->IsMeshComputed() )
1499 srcMeshError = TAssocTool::SourceNotComputedError();
1501 if ( !srcMeshError.empty() )
1502 return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1508 // get ordered src and tgt EDGEs
1509 TSideVector srcWires, tgtWires;
1510 bool is1DComputed = false; // if any tgt EDGE is meshed
1511 TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh,
1512 shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1513 if ( err && !err->IsOK() )
1514 return error( err );
1516 bool projDone = false;
1520 // try to project from the same face with different location
1521 projDone = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1522 shape2ShapeMap, _src2tgtNodes, is1DComputed );
1526 // projection in case if the faces are similar in 2D space
1527 projDone = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1528 shape2ShapeMap, _src2tgtNodes, is1DComputed );
1532 // projection in case of quadrilateral faces
1533 // NOT IMPLEMENTED, returns false
1534 projDone = projectQuads( tgtFace, srcFace, tgtWires, srcWires,
1535 shape2ShapeMap, _src2tgtNodes, is1DComputed);
1538 // it will remove mesh built on edges and vertices in failure case
1539 MeshCleaner cleaner( tgtSubMesh );
1543 _src2tgtNodes.clear();
1544 // --------------------
1545 // Prepare to mapping
1546 // --------------------
1548 // Check if node projection to a face is needed
1550 SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1551 set< const SMDS_MeshNode* > faceNodes;
1552 for ( ; faceNodes.size() < 3 && faceIt->more(); ) {
1553 const SMDS_MeshElement* face = faceIt->next();
1554 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1555 while ( nodeIt->more() ) {
1556 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1557 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE &&
1558 faceNodes.insert( node ).second )
1559 uvBox.Add( helper.GetNodeUV( srcFace, node ));
1562 bool toProjectNodes = false;
1563 if ( faceNodes.size() == 1 )
1564 toProjectNodes = ( uvBox.IsVoid() || uvBox.CornerMin().IsEqual( gp_XY(0,0), 1e-12 ));
1565 else if ( faceNodes.size() > 1 )
1566 toProjectNodes = ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN );
1568 // Find the corresponding source and target vertex
1569 // and <theReverse> flag needed to call mapper.Apply()
1571 TopoDS_Vertex srcV1, tgtV1;
1572 bool reverse = false;
1574 TopExp_Explorer vSrcExp( srcFace, TopAbs_VERTEX );
1575 srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1576 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1578 list< TopoDS_Edge > tgtEdges, srcEdges;
1579 list< int > nbEdgesInWires;
1580 SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1581 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1583 if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1585 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1586 TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1587 reverse = ( ! srcE1.IsSame( srcE1bis ));
1589 //_sourceHypo->HasVertexAssociation() &&
1590 nbEdgesInWires.front() > 2 &&
1591 helper.IsRealSeam( tgtEdges.front() ))
1593 // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1594 // always puts a seam EDGE first (if possible) and as a result
1595 // we can't use only theReverse flag to correctly associate source
1596 // and target faces in the mapper. Thus we select srcV1 so that
1597 // GetOrderedEdges() to return EDGEs in a needed order
1598 TopoDS_Face tgtFaceBis = tgtFace;
1599 TopTools_MapOfShape checkedVMap( tgtEdges.size() );
1600 checkedVMap.Add ( srcV1 );
1601 for ( vSrcExp.Next(); vSrcExp.More(); )
1603 tgtFaceBis.Reverse();
1605 SMESH_Block::GetOrderedEdges( tgtFaceBis, tgtEdges, nbEdgesInWires, tgtV1 );
1607 list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1608 for ( ; edgeS != srcEdges.end() && ok ; ++edgeS, ++edgeT )
1609 ok = edgeT->IsSame( shape2ShapeMap( *edgeS, /*isSrc=*/true ));
1617 while ( vSrcExp.More() && !checkedVMap.Add( vSrcExp.Current() ))
1622 srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1623 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1625 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1629 // for the case: project to a closed face from a non-closed face w/o vertex assoc;
1630 // avoid projecting to a seam from two EDGEs with different nb nodes on them
1631 // ( test mesh_Projection_2D_01/B1 )
1632 if ( !_sourceHypo->HasVertexAssociation() &&
1633 nbEdgesInWires.front() > 2 &&
1634 helper.IsRealSeam( tgtEdges.front() ))
1636 TopoDS_Shape srcEdge1 = shape2ShapeMap( tgtEdges.front() );
1637 list< TopoDS_Edge >::iterator srcEdge2 =
1638 std::find( srcEdges.begin(), srcEdges.end(), srcEdge1);
1639 list< TopoDS_Edge >::iterator srcEdge3 =
1640 std::find( srcEdges.begin(), srcEdges.end(), srcEdge1.Reversed());
1641 if ( srcEdge2 == srcEdges.end() || srcEdge3 == srcEdges.end() ) // srcEdge1 is not a seam
1643 // find srcEdge2 which also will be projected to tgtEdges.front()
1644 for ( srcEdge2 = srcEdges.begin(); srcEdge2 != srcEdges.end(); ++srcEdge2 )
1645 if ( !srcEdge1.IsSame( *srcEdge2 ) &&
1646 tgtEdges.front().IsSame( shape2ShapeMap( *srcEdge2, /*isSrc=*/true )))
1648 // compare nb nodes on srcEdge1 and srcEdge2
1649 if ( srcEdge2 != srcEdges.end() )
1651 int nbN1 = 0, nbN2 = 0;
1652 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( srcEdge1 ))
1653 nbN1 = sm->NbNodes();
1654 if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *srcEdge2 ))
1655 nbN2 = sm->NbNodes();
1657 srcV1 = helper.IthVertex( 1, srcEdges.front() );
1662 else if ( nbEdgesInWires.front() == 1 ) // a sole edge in a wire
1664 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1665 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1667 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
1668 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
1669 if ( srcE1.IsSame( srcWire->Edge( iE )))
1671 reverse = ( tgtE1.Orientation() != tgtWires[iW]->Edge( iE ).Orientation() );
1678 RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1681 // Load pattern from the source face
1682 SMESH_Pattern mapper;
1683 mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1, /*keepNodes=*/true );
1684 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1685 return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1687 // --------------------
1688 // Perform 2D mapping
1689 // --------------------
1691 // Compute mesh on a target face
1693 mapper.Apply( tgtFace, tgtV1, reverse );
1694 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK ) {
1695 // std::ofstream file("/tmp/Pattern.smp" );
1696 // mapper.Save( file );
1697 return error("Can't apply source mesh pattern to the face");
1702 const bool toCreatePolygons = false, toCreatePolyedrs = false;
1703 mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1704 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1705 return error("Can't make mesh by source mesh pattern");
1707 // fill _src2tgtNodes
1708 std::vector< const SMDS_MeshNode* > *srcNodes, *tgtNodes;
1709 mapper.GetInOutNodes( srcNodes, tgtNodes );
1710 size_t nbN = std::min( srcNodes->size(), tgtNodes->size() );
1711 for ( size_t i = 0; i < nbN; ++i )
1712 if ( (*srcNodes)[i] && (*tgtNodes)[i] )
1713 _src2tgtNodes.insert( make_pair( (*srcNodes)[i], (*tgtNodes)[i] ));
1716 } // end of projection using Pattern mapping
1719 // -------------------------------------------------------------------------
1720 // mapper doesn't take care of nodes already existing on edges and vertices,
1721 // so we must merge nodes created by it with existing ones
1722 // -------------------------------------------------------------------------
1724 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1726 // Make groups of nodes to merge
1728 // loop on EDGE and VERTEX sub-meshes of a target FACE
1729 SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1730 /*complexShapeFirst=*/false);
1731 while ( smIt->more() )
1733 SMESH_subMesh* sm = smIt->next();
1734 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1735 if ( !smDS || smDS->NbNodes() == 0 )
1737 //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1740 if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1742 if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1744 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1745 SMESH_subMeshIteratorPtr smDegenIt
1746 = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1747 while ( smDegenIt->more() )
1748 if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1750 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1751 while ( nIt->more() )
1752 groupsOfNodes.back().push_back( nIt->next() );
1755 continue; // do not treat sm of degen VERTEX
1758 // Sort new and old nodes of a sub-mesh separately
1760 bool isSeam = helper.IsRealSeam( sm->GetId() );
1762 enum { NEW_NODES = 0, OLD_NODES };
1763 map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1764 map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1765 set< const SMDS_MeshNode* > seamNodes;
1767 // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1768 if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1769 ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1771 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1772 while ( nIt->more() )
1774 const SMDS_MeshNode* node = nIt->next();
1775 bool isOld = isOldNode( node );
1777 if ( !isOld && isSeam ) { // new node on a seam edge
1778 if ( seamNodes.count( node ) )
1779 continue; // node is already in the map
1782 // sort nodes on edges by their position
1783 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1784 switch ( node->GetPosition()->GetTypeOfPosition() )
1786 case SMDS_TOP_VERTEX: {
1787 if ( !is1DComputed && !pos2nodes.empty() )
1788 u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1790 pos2nodes.insert( make_pair( 0, node ));
1793 case SMDS_TOP_EDGE: {
1794 const SMDS_EdgePosition* pos =
1795 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1796 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1800 RETURN_BAD_RESULT("Wrong node position type: "<<
1801 node->GetPosition()->GetTypeOfPosition());
1804 const bool mergeNewToOld =
1805 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1806 const bool mergeSeamToNew =
1807 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1809 if ( !mergeNewToOld )
1810 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1811 u2nodesMaps[ OLD_NODES ].size() > 0 )
1813 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1814 newEnd = u2nodesMaps[ OLD_NODES ].end();
1815 for ( ; u_oldNode != newEnd; ++u_oldNode )
1816 SMESH_Algo::addBadInputElement( u_oldNode->second );
1817 return error( COMPERR_BAD_INPUT_MESH,
1818 SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1819 << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1820 << " #" << sm->GetId() );
1822 if ( isSeam && !mergeSeamToNew ) {
1823 const TopoDS_Shape& seam = sm->GetSubShape();
1824 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1825 u2nodesOnSeam.size() > 0 &&
1826 seam.ShapeType() == TopAbs_EDGE )
1828 int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1829 int nbE2 = helper.Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1830 if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1832 // find the 2 EDGEs of srcFace
1833 TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1834 for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1835 if ( seam.IsSame( src2tgtIt.Value() ))
1836 SMESH_Algo::addBadInputElements
1837 ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1838 return error( COMPERR_BAD_INPUT_MESH,
1839 "Different number of nodes on two edges projected to a seam edge" );
1844 // Make groups of nodes to merge
1846 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1847 u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1848 newEnd = u2nodesMaps[ NEW_NODES ].end();
1849 u_newOnSeam = u2nodesOnSeam.begin();
1850 if ( mergeNewToOld )
1851 for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1853 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1854 groupsOfNodes.back().push_back( u_oldNode->second );
1855 groupsOfNodes.back().push_back( u_newNode->second );
1856 if ( mergeSeamToNew )
1857 groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1859 else if ( mergeSeamToNew )
1860 for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1862 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1863 groupsOfNodes.back().push_back( u_newNode->second );
1864 groupsOfNodes.back().push_back( u_newOnSeam->second );
1867 } // loop on EDGE and VERTEX submeshes of a target FACE
1871 SMESH_MeshEditor editor( tgtMesh );
1872 int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1873 editor.MergeNodes( groupsOfNodes );
1874 int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1875 if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1876 return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1878 // ----------------------------------------------------------------
1879 // The mapper can't create quadratic elements, so convert if needed
1880 // ----------------------------------------------------------------
1882 SMDS_ElemIteratorPtr faceIt;
1883 faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1884 bool srcIsQuad = faceIt->next()->IsQuadratic();
1885 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1886 bool tgtIsQuad = faceIt->next()->IsQuadratic();
1887 if ( srcIsQuad && !tgtIsQuad )
1889 TIDSortedElemSet tgtFaces;
1890 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1891 while ( faceIt->more() )
1892 tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1894 editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1896 } // end of coincident nodes and quadratic elements treatment
1899 if ( !projDone || is1DComputed )
1900 // ----------------------------------------------------------------
1901 // The mapper can create distorted faces by placing nodes out of the FACE
1902 // boundary, also bad face can be created if EDGEs already discretized
1903 // --> fix bad faces by smoothing
1904 // ----------------------------------------------------------------
1905 if ( helper.IsDistorted2D( tgtSubMesh, /*checkUV=*/false ))
1907 morph( helper, tgtFace, srcFace, tgtWires, srcWires, _src2tgtNodes );
1909 if ( !fixDistortedFaces( helper, tgtWires ))
1910 return error("Invalid mesh generated");
1912 // ---------------------------
1913 // Check elements orientation
1914 // ---------------------------
1916 TopoDS_Face face = TopoDS::Face( theShape );
1917 if ( !theMesh.IsMainShape( tgtFace ))
1919 // find the main shape
1920 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1921 switch ( mainShape.ShapeType() ) {
1923 case TopAbs_SOLID: break;
1925 TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1926 for ( ; ancestIt.More(); ancestIt.Next() ) {
1927 TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1928 if ( type == TopAbs_SOLID ) {
1929 mainShape = ancestIt.Value();
1931 } else if ( type == TopAbs_SHELL ) {
1932 mainShape = ancestIt.Value();
1936 // find tgtFace in the main solid or shell to know it's true orientation.
1937 TopExp_Explorer exp( mainShape, TopAbs_FACE );
1938 for ( ; exp.More(); exp.Next() ) {
1939 if ( tgtFace.IsSame( exp.Current() )) {
1940 face = TopoDS::Face( exp.Current() );
1946 if ( helper.IsReversedSubMesh( face ))
1948 SMESH_MeshEditor editor( tgtMesh );
1949 SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1950 while ( eIt->more() ) {
1951 const SMDS_MeshElement* e = eIt->next();
1952 if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1953 RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1957 cleaner.Release(); // not to remove mesh
1963 //=======================================================================
1964 //function : Evaluate
1966 //=======================================================================
1968 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
1969 const TopoDS_Shape& theShape,
1970 MapShapeNbElems& aResMap)
1975 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1976 SMESH_Mesh * tgtMesh = & theMesh;
1980 // ---------------------------
1981 // Make sub-shapes association
1982 // ---------------------------
1984 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1985 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1987 TAssocTool::TShapeShapeMap shape2ShapeMap;
1988 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1989 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1991 !shape2ShapeMap.IsBound( tgtFace ))
1992 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1994 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1996 // -------------------------------------------------------
1997 // Assure that mesh on a source Face is computed/evaluated
1998 // -------------------------------------------------------
2000 std::vector<int> aVec;
2002 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
2003 if ( srcSubMesh->IsMeshComputed() )
2005 aVec.resize( SMDSEntity_Last, 0 );
2006 aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
2008 SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
2009 while ( elemIt->more() )
2010 aVec[ elemIt->next()->GetEntityType() ]++;
2014 MapShapeNbElems tmpResMap;
2015 MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
2016 if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
2017 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
2018 aVec = srcResMap[ srcSubMesh ];
2020 return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
2023 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
2024 aResMap.insert(std::make_pair(sm,aVec));
2030 //=============================================================================
2032 * \brief Sets a default event listener to submesh of the source face
2033 * \param subMesh - submesh where algo is set
2035 * This method is called when a submesh gets HYP_OK algo_state.
2036 * After being set, event listener is notified on each event of a submesh.
2037 * Arranges that CLEAN event is translated from source submesh to
2040 //=============================================================================
2042 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
2044 TAssocTool::SetEventListener( subMesh,
2045 _sourceHypo->GetSourceFace(),
2046 _sourceHypo->GetSourceMesh() );