1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : StdMeshers_Projection_2D.cxx
26 // Created : Fri Oct 20 11:37:07 2006
27 // Author : Edward AGAPOV (eap)
29 #include "StdMeshers_Projection_2D.hxx"
31 #include "StdMeshers_ProjectionSource2D.hxx"
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "StdMeshers_FaceSide.hxx"
35 #include "SMDS_EdgePosition.hxx"
36 #include "SMDS_FacePosition.hxx"
37 #include "SMESHDS_Hypothesis.hxx"
38 #include "SMESHDS_SubMesh.hxx"
39 #include "SMESH_Block.hxx"
40 #include "SMESH_Comment.hxx"
41 #include "SMESH_Gen.hxx"
42 #include "SMESH_Mesh.hxx"
43 #include "SMESH_MesherHelper.hxx"
44 #include "SMESH_Pattern.hxx"
45 #include "SMESH_subMesh.hxx"
46 #include "SMESH_subMeshEventListener.hxx"
48 #include "utilities.h"
50 #include <BRep_Tool.hxx>
51 #include <Bnd_B2d.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
65 namespace TAssocTool = StdMeshers_ProjectionUtils;
66 //typedef StdMeshers_ProjectionUtils TAssocTool;
68 //=======================================================================
69 //function : StdMeshers_Projection_2D
71 //=======================================================================
73 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
74 :SMESH_2D_Algo(hypId, studyId, gen)
76 _name = "Projection_2D";
77 _compatibleHypothesis.push_back("ProjectionSource2D");
81 //================================================================================
85 //================================================================================
87 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
90 //=======================================================================
91 //function : CheckHypothesis
93 //=======================================================================
95 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh& theMesh,
96 const TopoDS_Shape& theShape,
97 SMESH_Hypothesis::Hypothesis_Status& theStatus)
99 list <const SMESHDS_Hypothesis * >::const_iterator itl;
101 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
102 if ( hyps.size() == 0 )
104 theStatus = HYP_MISSING;
105 return false; // can't work with no hypothesis
108 if ( hyps.size() > 1 )
110 theStatus = HYP_ALREADY_EXIST;
114 const SMESHDS_Hypothesis *theHyp = hyps.front();
116 string hypName = theHyp->GetName();
120 if (hypName == "ProjectionSource2D")
122 _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
124 // Check hypo parameters
126 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
127 SMESH_Mesh* tgtMesh = & theMesh;
132 if ( _sourceHypo->HasVertexAssociation() )
135 TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
136 ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
137 if ( edge.IsNull() ||
138 !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
139 !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
141 theStatus = HYP_BAD_PARAMETER;
142 SCRUTE((edge.IsNull()));
143 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
144 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
149 edge = TAssocTool::GetEdgeByVertices
150 ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
151 if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
153 theStatus = HYP_BAD_PARAMETER;
154 SCRUTE((edge.IsNull()));
155 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
158 else if ( !_sourceHypo->IsCompoundSource() &&
159 !SMESH_MesherHelper::IsSubShape( edge, theShape ))
161 theStatus = HYP_BAD_PARAMETER;
162 SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
166 // check a source face
167 if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
168 ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
170 theStatus = HYP_BAD_PARAMETER;
171 SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
172 SCRUTE((srcMesh == tgtMesh));
173 SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
178 theStatus = HYP_INCOMPATIBLE;
180 return ( theStatus == HYP_OK );
185 //================================================================================
187 * \brief define if a node is new or old
188 * \param node - node to check
189 * \retval bool - true if the node existed before Compute() is called
191 //================================================================================
193 bool isOldNode( const SMDS_MeshNode* node/*, const bool is1DComputed*/ )
195 // old nodes are shared by edges and new ones are shared
196 // only by faces created by mapper
197 //if ( is1DComputed )
199 bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
204 // SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
205 // bool isNew = invFace->more();
210 //================================================================================
212 * \brief Class to remove mesh built by pattern mapper on edges
213 * and vertices in the case of failure of projection algo.
214 * It does it's job at destruction
216 //================================================================================
221 MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
222 ~MeshCleaner() { Clean(sm); }
223 void Release() { sm = 0; } // mesh will not be removed
224 static void Clean( SMESH_subMesh* sm, bool withSub=true )
226 if ( !sm || !sm->GetSubMeshDS() ) return;
227 // PAL16567, 18920. Remove face nodes as well
228 // switch ( sm->GetSubShape().ShapeType() ) {
229 // case TopAbs_VERTEX:
230 // case TopAbs_EDGE: {
231 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
232 SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
233 while ( nIt->more() ) {
234 const SMDS_MeshNode* node = nIt->next();
235 if ( !isOldNode( node ) )
236 mesh->RemoveNode( node );
238 // do not break but iterate over DependsOn()
241 if ( !withSub ) return;
242 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
243 while ( smIt->more() )
244 Clean( smIt->next(), false );
249 //================================================================================
251 * \brief find new nodes belonging to one free border of mesh on face
252 * \param sm - submesh on edge or vertex containg nodes to choose from
253 * \param face - the face bound by the submesh
254 * \param u2nodes - map to fill with nodes
255 * \param seamNodes - set of found nodes
256 * \retval bool - is a success
258 //================================================================================
260 bool getBoundaryNodes ( SMESH_subMesh* sm,
261 const TopoDS_Face& face,
262 map< double, const SMDS_MeshNode* > & u2nodes,
263 set< const SMDS_MeshNode* > & seamNodes)
267 if ( !sm || !sm->GetSubMeshDS() )
268 RETURN_BAD_RESULT("Null submesh");
270 SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
271 switch ( sm->GetSubShape().ShapeType() ) {
273 case TopAbs_VERTEX: {
274 while ( nIt->more() ) {
275 const SMDS_MeshNode* node = nIt->next();
276 if ( isOldNode( node ) ) continue;
277 u2nodes.insert( make_pair( 0., node ));
278 seamNodes.insert( node );
285 // Get submeshes of sub-vertices
286 const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
287 if ( subSM.size() != 2 )
288 RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
289 " but we have " << subSM.size());
290 SMESH_subMesh* smV1 = subSM.begin()->second;
291 SMESH_subMesh* smV2 = subSM.rbegin()->second;
292 if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
293 RETURN_BAD_RESULT("Empty vertex submeshes");
295 const SMDS_MeshNode* nV1 = 0;
296 const SMDS_MeshNode* nE = 0;
298 // Look for nV1 - a new node on V1
299 nIt = smV1->GetSubMeshDS()->GetNodes();
300 while ( nIt->more() && !nE ) {
301 const SMDS_MeshNode* node = nIt->next();
302 if ( isOldNode( node ) ) continue;
305 // Find nE - a new node connected to nV1 and belonging to edge submesh;
306 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
307 SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
308 while ( vElems->more() && !nE ) {
309 const SMDS_MeshElement* elem = vElems->next();
310 int nbNodes = elem->NbNodes();
311 if ( elem->IsQuadratic() )
313 int iV1 = elem->GetNodeIndex( nV1 );
314 // try next after nV1
315 int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
316 if ( smDS->Contains( elem->GetNode( iE ) ))
317 nE = elem->GetNode( iE );
319 // try node before nV1
320 iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
321 if ( smDS->Contains( elem->GetNode( iE )))
322 nE = elem->GetNode( iE );
324 if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
325 if ( Abs( iV1 - iE ) == 1 )
326 nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
328 nE = elem->GetNode( elem->NbNodes() - 1 );
333 RETURN_BAD_RESULT("No new node found on V1");
335 RETURN_BAD_RESULT("new node on edge not found");
337 // Get the whole free border of a face
338 list< const SMDS_MeshNode* > bordNodes;
339 list< const SMDS_MeshElement* > bordFaces;
340 if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
341 RETURN_BAD_RESULT("free border of a face not found by nodes " <<
342 nV1->GetID() << " " << nE->GetID() );
344 // Insert nodes of the free border to the map until node on V2 encountered
345 SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
346 list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
347 bordIt++; // skip nV1
348 for ( ; bordIt != bordNodes.end(); ++bordIt ) {
349 const SMDS_MeshNode* node = *bordIt;
350 if ( v2smDS->Contains( node ))
352 if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
353 RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
354 " pos type " << node->GetPosition()->GetTypeOfPosition());
355 const SMDS_EdgePosition* pos =
356 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
357 u2nodes.insert( make_pair( pos->GetUParameter(), node ));
358 seamNodes.insert( node );
360 if ( u2nodes.size() != seamNodes.size() )
361 RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
362 ", " << u2nodes.size() << " != " << seamNodes.size() );
367 RETURN_BAD_RESULT ("Unexpected submesh type");
369 } // bool getBoundaryNodes()
371 //================================================================================
373 * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
374 * if projection by transformation is possible
376 //================================================================================
378 bool projectPartner(const TopoDS_Face& tgtFace,
379 const TopoDS_Face& srcFace,
380 SMESH_Mesh * tgtMesh,
381 SMESH_Mesh * srcMesh,
382 const TAssocTool::TShapeShapeMap& shape2ShapeMap)
384 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
385 SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
387 const double tol = 1.e-7 * srcMeshDS->getMaxDim();
389 gp_Trsf trsf; // transformation to get location of target nodes from source ones
390 if ( tgtFace.IsPartner( srcFace ))
392 gp_Trsf srcTrsf = srcFace.Location();
393 gp_Trsf tgtTrsf = tgtFace.Location();
394 trsf = srcTrsf.Inverted() * tgtTrsf;
398 // Try to find the transformation
400 // make any local coord systems of src and tgt faces
401 vector<gp_Pnt> srcPP, tgtPP; // 3 points on face boundaries to make axes of CS
402 int tgtNbVert = SMESH_MesherHelper::Count( tgtFace, TopAbs_VERTEX, /*ignoreSame=*/true );
403 int srcNbVert = SMESH_MesherHelper::Count( srcFace, TopAbs_VERTEX, /*ignoreSame=*/true );
404 SMESH_subMesh * srcSM = srcMesh->GetSubMesh( srcFace );
405 SMESH_subMeshIteratorPtr smIt = srcSM->getDependsOnIterator(/*includeSelf=*/false,false);
406 srcSM = smIt->next(); // sm of a vertex
407 while ( smIt->more() && srcPP.size() < 3 )
409 srcSM = smIt->next();
410 SMESHDS_SubMesh* srcSmds = srcSM->GetSubMeshDS();
411 if ( !srcSmds ) continue;
412 SMDS_NodeIteratorPtr nIt = srcSmds->GetNodes();
413 while ( nIt->more() )
415 SMESH_TNodeXYZ p ( nIt->next());
417 switch ( srcPP.size() )
419 case 0: pOK = true; break;
421 case 1: pOK = ( srcPP[0].SquareDistance( p ) > 10*tol ); break;
425 gp_Vec p0p1( srcPP[0], srcPP[1] ), p0p( srcPP[0], p );
426 // pOK = !p0p1.IsParallel( p0p, tol );
427 pOK = !p0p1.IsParallel( p0p, 3.14/20 ); // angle min 18 degrees
434 // find corresponding point on target shape
437 const TopoDS_Shape& tgtShape = shape2ShapeMap( srcSM->GetSubShape(), /*isSrc=*/true );
438 if ( tgtShape.ShapeType() == TopAbs_VERTEX )
440 tgtP = BRep_Tool::Pnt( TopoDS::Vertex( tgtShape ));
441 if ( srcNbVert == tgtNbVert || tgtPP.empty() )
444 pOK = (( tgtP.Distance( tgtPP[0] ) > tol*tol ) &&
445 ( tgtPP.size() == 1 || tgtP.Distance( tgtPP[1] ) > tol*tol ));
446 //cout << "V - nS " << p._node->GetID() << " - nT " << SMESH_Algo::VertexNode(TopoDS::Vertex( tgtShape),tgtMeshDS)->GetID() << endl;
448 else if ( tgtPP.size() > 0 )
450 if ( SMESHDS_SubMesh* tgtSmds = tgtMeshDS->MeshElements( tgtShape ))
452 double srcDist = srcPP[0].Distance( p );
453 double eTol = BRep_Tool::Tolerance( TopoDS::Edge( tgtShape ));
454 if (eTol < tol) eTol = tol;
455 SMDS_NodeIteratorPtr nItT = tgtSmds->GetNodes();
456 while ( nItT->more() && !pOK )
458 const SMDS_MeshNode* n = nItT->next();
459 tgtP = SMESH_TNodeXYZ( n );
460 pOK = ( fabs( srcDist - tgtPP[0].Distance( tgtP )) < 2*eTol );
461 //cout << "E - nS " << p._node->GetID() << " - nT " << n->GetID()<< " OK - " << pOK<< " " << fabs( srcDist - tgtPP[0].Distance( tgtP ))<< " tol " << eTol<< endl;
468 srcPP.push_back( p );
469 tgtPP.push_back( tgtP );
472 if ( srcPP.size() != 3 )
475 // make transformation
476 gp_Trsf fromTgtCS, toSrcCS; // from/to global CS
477 gp_Ax2 srcCS( srcPP[0], gp_Vec( srcPP[0], srcPP[1] ), gp_Vec( srcPP[0], srcPP[2]));
478 gp_Ax2 tgtCS( tgtPP[0], gp_Vec( tgtPP[0], tgtPP[1] ), gp_Vec( tgtPP[0], tgtPP[2]));
479 toSrcCS .SetTransformation( gp_Ax3( srcCS ));
480 fromTgtCS.SetTransformation( gp_Ax3( tgtCS ));
483 trsf = fromTgtCS * toSrcCS;
486 // Fill map of src to tgt nodes with nodes on edges
488 map<const SMDS_MeshNode* , const SMDS_MeshNode*> src2tgtNodes;
489 map<const SMDS_MeshNode* , const SMDS_MeshNode*>::iterator srcN_tgtN;
491 for ( TopExp_Explorer srcExp( srcFace, TopAbs_EDGE); srcExp.More(); srcExp.Next() )
493 const TopoDS_Shape& srcEdge = srcExp.Current();
494 const TopoDS_Shape& tgtEdge = shape2ShapeMap( srcEdge, /*isSrc=*/true );
495 if ( srcMesh->GetSubMesh( srcEdge )->IsEmpty() ||
496 tgtMesh->GetSubMesh( tgtEdge )->IsEmpty() )
499 map< double, const SMDS_MeshNode* > srcNodes, tgtNodes;
500 if (( ! SMESH_Algo::GetSortedNodesOnEdge( srcMeshDS,
501 TopoDS::Edge( srcEdge ),
502 /*ignoreMediumNodes = */true,
505 ( ! SMESH_Algo::GetSortedNodesOnEdge( tgtMeshDS,
506 TopoDS::Edge( tgtEdge ),
507 /*ignoreMediumNodes = */true,
510 (( srcNodes.size() != tgtNodes.size() ) && tgtNodes.size() > 0 )
514 if ( !tgtEdge.IsPartner( srcEdge ))
516 if ( tgtNodes.empty() )
518 // check that transformation is OK by three nodes
519 gp_Pnt p0S = SMESH_TNodeXYZ( (srcNodes.begin()) ->second);
520 gp_Pnt p1S = SMESH_TNodeXYZ( (srcNodes.rbegin()) ->second);
521 gp_Pnt p2S = SMESH_TNodeXYZ( (++srcNodes.begin())->second);
523 gp_Pnt p0T = SMESH_TNodeXYZ( (tgtNodes.begin()) ->second);
524 gp_Pnt p1T = SMESH_TNodeXYZ( (tgtNodes.rbegin()) ->second);
525 gp_Pnt p2T = SMESH_TNodeXYZ( (++tgtNodes.begin())->second);
527 // transform source points, they must coincide with target ones
528 if ( p0T.SquareDistance( p0S.Transformed( trsf )) > tol ||
529 p1T.SquareDistance( p1S.Transformed( trsf )) > tol ||
530 p2T.SquareDistance( p2S.Transformed( trsf )) > tol )
532 //cout << "KO trsf, 3 dist: "
533 //<< p0T.SquareDistance( p0S.Transformed( trsf ))<< ", "
534 //<< p1T.SquareDistance( p1S.Transformed( trsf ))<< ", "
535 //<< p2T.SquareDistance( p2S.Transformed( trsf ))<< ", "<<endl;
539 if ( !tgtNodes.empty() )
541 map< double, const SMDS_MeshNode* >::iterator u_tn = tgtNodes.begin();
542 map< double, const SMDS_MeshNode* >::iterator u_sn = srcNodes.begin();
543 for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
544 src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
550 // prepare the helper to adding quadratic elements if necessary
551 SMESH_MesherHelper helper( *tgtMesh );
552 helper.SetSubShape( tgtFace );
553 helper.IsQuadraticSubMesh( tgtFace );
555 SMESH_MesherHelper srcHelper( *srcMesh );
556 srcHelper.SetSubShape( srcFace );
558 const SMDS_MeshNode* nullNode = 0;
560 // indices of nodes to create properly oriented faces
561 bool isReverse = ( trsf.Form() != gp_Identity );
562 int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
564 std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
566 SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
567 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
568 vector< const SMDS_MeshNode* > tgtNodes;
569 while ( elemIt->more() ) // loop on all mesh faces on srcFace
571 const SMDS_MeshElement* elem = elemIt->next();
572 const int nbN = elem->NbCornerNodes();
573 tgtNodes.resize( nbN );
574 helper.SetElementsOnShape( false );
575 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
577 const SMDS_MeshNode* srcNode = elem->GetNode(i);
578 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
579 if ( srcN_tgtN->second == nullNode )
582 gp_Pnt tgtP = gp_Pnt( SMESH_TNodeXYZ( srcNode )).Transformed( trsf );
583 SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
584 srcN_tgtN->second = n;
585 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
589 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode );
590 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), srcUV.X(), srcUV.Y() );
595 const TopoDS_Shape & srcE = srcMeshDS->IndexToShape( srcNode->getshapeId() );
596 const TopoDS_Shape & tgtE = shape2ShapeMap( srcE, /*isSrc=*/true );
597 double srcU = srcHelper.GetNodeU( TopoDS::Edge( srcE ), srcNode );
598 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtE ), srcU );
601 case SMDS_TOP_VERTEX:
603 const TopoDS_Shape & srcV = srcMeshDS->IndexToShape( srcNode->getshapeId() );
604 const TopoDS_Shape & tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
605 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
611 tgtNodes[i] = srcN_tgtN->second;
614 helper.SetElementsOnShape( true );
617 case 3: helper.AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
618 case 4: helper.AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
620 if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
621 helper.AddPolygonalFace( tgtNodes );
625 // check node positions
627 if ( !tgtFace.IsPartner( srcFace ) )
630 const double tol2d = 1e-12;
631 srcN_tgtN = src2tgtNodes.begin();
632 for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
634 const SMDS_MeshNode* n = srcN_tgtN->second;
635 switch ( n->GetPosition()->GetTypeOfPosition() )
639 gp_XY uv = helper.GetNodeUV( tgtFace, n ), uvBis = uv;
640 if (( helper.CheckNodeUV( tgtFace, n, uv, tol )) &&
641 (( uv - uvBis ).SquareModulus() < tol2d ) &&
650 const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
651 double u = helper.GetNodeU( tgtE, n ), uBis = u;
652 if (( !helper.CheckNodeU( tgtE, n, u, tol )) ||
653 (( u - uBis ) < tol2d ))
664 } // bool projectPartner()
666 //================================================================================
668 * \brief Check if two consecutive EDGEs are connected in 2D
669 * \param [in] E1 - a well oriented non-seam EDGE
670 * \param [in] E2 - a possibly well oriented seam EDGE
671 * \param [in] F - a FACE
672 * \return bool - result
674 //================================================================================
676 bool are2dConnected( const TopoDS_Edge & E1,
677 const TopoDS_Edge & E2,
678 const TopoDS_Face & F )
681 Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( E1, F, f, l );
682 gp_Pnt2d uvLast1 = c1->Value( E1.Orientation() == TopAbs_REVERSED ? f : l );
684 Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( E2, F, f, l );
685 gp_Pnt2d uvFirst2 = c2->Value( f );
686 gp_Pnt2d uvLast2 = c2->Value( l );
687 double tol2 = 1e-5 * uvLast2.SquareDistance( uvFirst2 );
689 return (( uvLast1.SquareDistance( uvFirst2 ) < tol2 ) ||
690 ( uvLast1.SquareDistance( uvLast2 ) < tol2 ));
693 //================================================================================
695 * \brief Preform projection in case if the faces are similar in 2D space
697 //================================================================================
699 bool projectBy2DSimilarity(const TopoDS_Face& tgtFace,
700 const TopoDS_Face& srcFace,
701 SMESH_Mesh * tgtMesh,
702 SMESH_Mesh * srcMesh,
703 const TAssocTool::TShapeShapeMap& shape2ShapeMap,
704 const bool is1DComputed)
708 // get ordered src EDGEs
710 TSideVector srcWires =
711 StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*ignoreMediumNodes = */false, err);
712 if ( err && !err->IsOK() )
715 // make corresponding sequence of tgt EDGEs
716 TSideVector tgtWires( srcWires.size() );
717 for ( size_t iW = 0; iW < srcWires.size(); ++iW )
719 list< TopoDS_Edge > tgtEdges;
720 StdMeshers_FaceSidePtr srcWire = srcWires[iW];
721 TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
722 for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
724 TopoDS_Edge E = TopoDS::Edge( shape2ShapeMap( srcWire->Edge( iE ), /*isSrc=*/true));
725 // reverse a seam edge encountered for the second time
726 const int index = edgeMap.Add( E );
727 if ( index < edgeMap.Extent() ) // E is a seam
729 // check which of edges to reverse, E or one already being in tgtEdges
730 if ( are2dConnected( tgtEdges.back(), E, tgtFace ))
732 list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
733 std::advance( eIt, index-1 );
741 tgtEdges.push_back( E );
743 tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
744 /*theIsForward = */ true,
745 /*theIgnoreMediumNodes = */false));
747 srcWires[iW]->GetUVPtStruct().size() !=
748 tgtWires[iW]->GetUVPtStruct().size())
752 // 2) Find transformation
756 // get 2 pairs of corresponding UVs
757 gp_Pnt2d srcP0 = srcWires[0]->Value2d(0.0);
758 gp_Pnt2d srcP1 = srcWires[0]->Value2d(0.333);
759 gp_Pnt2d tgtP0 = tgtWires[0]->Value2d(0.0);
760 gp_Pnt2d tgtP1 = tgtWires[0]->Value2d(0.333);
762 // make transformation
763 gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
764 gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
765 gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
766 toSrcCS .SetTransformation( srcCS );
767 fromTgtCS.SetTransformation( tgtCS );
770 trsf = fromTgtCS * toSrcCS;
772 // check transformation
773 const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
774 for ( double u = 0.12; u < 1.; u += 0.1 )
776 gp_Pnt2d srcUV = srcWires[0]->Value2d( u );
777 gp_Pnt2d tgtUV = tgtWires[0]->Value2d( u );
778 gp_Pnt2d tgtUV2 = srcUV.Transformed( trsf );
779 if ( tgtUV.Distance( tgtUV2 ) > tol )
786 typedef map<const SMDS_MeshNode* , const SMDS_MeshNode*, TIDCompare> TN2NMap;
787 TN2NMap src2tgtNodes;
788 TN2NMap::iterator srcN_tgtN;
790 // fill src2tgtNodes in with nodes on EDGEs
791 for ( unsigned iW = 0; iW < srcWires.size(); ++iW )
794 const vector<UVPtStruct>& srcUVs = srcWires[iW]->GetUVPtStruct();
795 const vector<UVPtStruct>& tgtUVs = tgtWires[iW]->GetUVPtStruct();
796 for ( unsigned i = 0; i < srcUVs.size(); ++i )
797 src2tgtNodes.insert( make_pair( srcUVs[i].node, tgtUVs[i].node ));
801 for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
803 TopoDS_Vertex srcV = srcWires[iW]->FirstVertex(iE);
804 TopoDS_Vertex tgtV = tgtWires[iW]->FirstVertex(iE);
805 const SMDS_MeshNode* srcNode = SMESH_Algo::VertexNode( srcV, srcMesh->GetMeshDS() );
806 const SMDS_MeshNode* tgtNode = SMESH_Algo::VertexNode( tgtV, tgtMesh->GetMeshDS() );
807 if ( tgtNode && srcNode )
808 src2tgtNodes.insert( make_pair( srcNode, tgtNode ));
814 SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
816 SMESH_MesherHelper helper( *tgtMesh );
817 helper.SetSubShape( tgtFace );
819 helper.IsQuadraticSubMesh( tgtFace );
821 helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
822 helper.SetElementsOnShape( true );
823 Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
824 SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
826 SMESH_MesherHelper srcHelper( *srcMesh );
827 srcHelper.SetSubShape( srcFace );
829 const SMDS_MeshNode* nullNode = 0;
831 SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
832 vector< const SMDS_MeshNode* > tgtNodes;
834 while ( elemIt->more() ) // loop on all mesh faces on srcFace
836 const SMDS_MeshElement* elem = elemIt->next();
837 const int nbN = elem->NbCornerNodes();
838 tgtNodes.resize( nbN );
839 for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
841 const SMDS_MeshNode* srcNode = elem->GetNode(i);
842 srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
843 if ( srcN_tgtN->second == nullNode )
846 gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
847 elem->GetNode( helper.WrapIndex(i+1,nbN)), &uvOK);
848 gp_Pnt2d tgtUV = srcUV.Transformed( trsf );
849 gp_Pnt tgtP = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
850 SMDS_MeshNode* n = tgtMeshDS->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
851 switch ( srcNode->GetPosition()->GetTypeOfPosition() )
853 case SMDS_TOP_FACE: {
854 tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
857 case SMDS_TOP_EDGE: {
858 TopoDS_Shape srcEdge = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
859 TopoDS_Edge tgtEdge = TopoDS::Edge( shape2ShapeMap( srcEdge, /*isSrc=*/true ));
860 tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtEdge ));
861 double U = srcHelper.GetNodeU( TopoDS::Edge( srcEdge ), srcNode );
862 helper.CheckNodeU( tgtEdge, n, U, Precision::PConfusion());
863 n->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition( U )));
866 case SMDS_TOP_VERTEX: {
867 TopoDS_Shape srcV = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
868 TopoDS_Shape tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
869 tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
873 srcN_tgtN->second = n;
875 tgtNodes[i] = srcN_tgtN->second;
877 // create a new face (with reversed orientation)
880 case 3: helper.AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
881 case 4: helper.AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
886 } // bool projectBy2DSimilarity(...)
891 //=======================================================================
894 //=======================================================================
896 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
898 MESSAGE("Projection_2D Compute");
902 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
903 SMESH_Mesh * tgtMesh = & theMesh;
907 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
909 // ---------------------------
910 // Make sub-shapes association
911 // ---------------------------
913 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
914 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
916 TAssocTool::TShapeShapeMap shape2ShapeMap;
917 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
918 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
920 !shape2ShapeMap.IsBound( tgtFace ))
922 if ( srcShape.ShapeType() == TopAbs_FACE )
924 int nbE1 = SMESH_MesherHelper::Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
925 int nbE2 = SMESH_MesherHelper::Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
927 return error(COMPERR_BAD_SHAPE,
928 SMESH_Comment("Different number of edges in source and target faces: ")
929 << nbE2 << " and " << nbE1 );
931 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
933 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
935 // ----------------------------------------------
936 // Assure that mesh on a source Face is computed
937 // ----------------------------------------------
939 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
940 SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
943 if ( tgtMesh == srcMesh ) {
944 if ( !TAssocTool::MakeComputed( srcSubMesh ))
945 srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
948 if ( !srcSubMesh->IsMeshComputed() )
949 srcMeshError = TAssocTool::SourceNotComputedError();
951 if ( !srcMeshError.empty() )
952 return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
958 // find out if EDGEs are meshed or not
959 bool is1DComputed = false;
960 SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
961 /*complexShapeFirst=*/true);
962 while ( smIt->more() && !is1DComputed )
964 SMESH_subMesh* sm = smIt->next();
965 if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
966 is1DComputed = sm->IsMeshComputed();
973 // try to project from the same face with different location
974 done = projectPartner( tgtFace, srcFace, tgtMesh, srcMesh, shape2ShapeMap );
978 // projection in case if the faces are similar in 2D space
979 done = projectBy2DSimilarity( tgtFace, srcFace, tgtMesh, srcMesh, shape2ShapeMap, is1DComputed);
982 SMESH_MesherHelper helper( theMesh );
983 helper.SetSubShape( tgtFace );
987 // --------------------
988 // Prepare to mapping
989 // --------------------
991 // Check if node projection to a face is needed
993 SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
995 for ( ; nbFaceNodes < 3 && faceIt->more(); ) {
996 const SMDS_MeshElement* face = faceIt->next();
997 SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
998 while ( nodeIt->more() ) {
999 const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1000 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1002 uvBox.Add( helper.GetNodeUV( srcFace, node ));
1006 const bool toProjectNodes =
1007 ( nbFaceNodes > 0 && ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN ));
1009 // Find the corresponding source and target vertex
1010 // and <theReverse> flag needed to call mapper.Apply()
1012 TopoDS_Vertex srcV1, tgtV1;
1013 bool reverse = false;
1015 if ( _sourceHypo->HasVertexAssociation() ) {
1016 srcV1 = _sourceHypo->GetSourceVertex(1);
1017 tgtV1 = _sourceHypo->GetTargetVertex(1);
1019 srcV1 = TopoDS::Vertex( TopExp_Explorer( srcFace, TopAbs_VERTEX ).Current() );
1020 tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1022 list< TopoDS_Edge > tgtEdges, srcEdges;
1023 list< int > nbEdgesInWires;
1024 SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1025 SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1027 if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1029 TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1030 TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1031 reverse = ( ! srcE1.IsSame( srcE1bis ));
1033 _sourceHypo->HasVertexAssociation() &&
1034 nbEdgesInWires.front() > 2 &&
1035 helper.IsRealSeam( tgtEdges.front() ))
1037 // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1038 // always puts a seam EDGE first (if possible) and as a result
1039 // we can't use only theReverse flag to correctly associate source
1040 // and target faces in the mapper. Thus we select srcV1 so that
1041 // GetOrderedEdges() to return EDGEs in a needed order
1042 list< TopoDS_Edge >::iterator edge = srcEdges.begin();
1043 for ( ; edge != srcEdges.end(); ++edge ) {
1044 if ( srcE1bis.IsSame( *edge )) {
1045 srcV1 = helper.IthVertex( 0, *edge );
1051 else if ( nbEdgesInWires.front() == 1 )
1053 // TODO::Compare orientation of curves in a sole edge
1054 //RETURN_BAD_RESULT("Not implemented case");
1058 RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1061 // Load pattern from the source face
1062 SMESH_Pattern mapper;
1063 mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1 );
1064 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1065 return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1067 // --------------------
1068 // Perform 2D mapping
1069 // --------------------
1071 // Compute mesh on a target face
1073 mapper.Apply( tgtFace, tgtV1, reverse );
1074 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1075 return error("Can't apply source mesh pattern to the face");
1079 const bool toCreatePolygons = false, toCreatePolyedrs = false;
1080 mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1081 if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1082 return error("Can't make mesh by source mesh pattern");
1084 // it will remove mesh built by pattern mapper on edges and vertices
1086 MeshCleaner cleaner( tgtSubMesh );
1088 // -------------------------------------------------------------------------
1089 // mapper doesn't take care of nodes already existing on edges and vertices,
1090 // so we must merge nodes created by it with existing ones
1091 // -------------------------------------------------------------------------
1093 SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1095 // Make groups of nodes to merge
1097 // loop on EDGE and VERTEX sub-meshes of a target FACE
1098 smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,/*complexShapeFirst=*/false);
1099 while ( smIt->more() )
1101 SMESH_subMesh* sm = smIt->next();
1102 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1103 if ( !smDS || smDS->NbNodes() == 0 )
1105 //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1108 if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1110 if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1112 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1113 SMESH_subMeshIteratorPtr smDegenIt
1114 = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1115 while ( smDegenIt->more() )
1116 if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1118 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1119 while ( nIt->more() )
1120 groupsOfNodes.back().push_back( nIt->next() );
1123 continue; // do not treat sm of degen VERTEX
1126 // Sort new and old nodes of a submesh separately
1128 bool isSeam = helper.IsRealSeam( sm->GetId() );
1130 enum { NEW_NODES = 0, OLD_NODES };
1131 map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1132 map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1133 set< const SMDS_MeshNode* > seamNodes;
1135 // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1136 if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1137 ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1139 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1140 while ( nIt->more() )
1142 const SMDS_MeshNode* node = nIt->next();
1143 bool isOld = isOldNode( node );
1145 if ( !isOld && isSeam ) { // new node on a seam edge
1146 if ( seamNodes.count( node ) )
1147 continue; // node is already in the map
1150 // sort nodes on edges by their position
1151 map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1152 switch ( node->GetPosition()->GetTypeOfPosition() )
1154 case SMDS_TOP_VERTEX: {
1155 if ( !is1DComputed && !pos2nodes.empty() )
1156 u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1158 pos2nodes.insert( make_pair( 0, node ));
1161 case SMDS_TOP_EDGE: {
1162 const SMDS_EdgePosition* pos =
1163 static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1164 pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1168 RETURN_BAD_RESULT("Wrong node position type: "<<
1169 node->GetPosition()->GetTypeOfPosition());
1172 const bool mergeNewToOld =
1173 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1174 const bool mergeSeamToNew =
1175 ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1177 if ( !mergeNewToOld )
1178 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1179 u2nodesMaps[ OLD_NODES ].size() > 0 )
1181 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1182 newEnd = u2nodesMaps[ OLD_NODES ].end();
1183 for ( ; u_oldNode != newEnd; ++u_oldNode )
1184 SMESH_Algo::addBadInputElement( u_oldNode->second );
1185 return error( COMPERR_BAD_INPUT_MESH,
1186 SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1187 << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1188 << " #" << sm->GetId() );
1190 if ( isSeam && !mergeSeamToNew ) {
1191 const TopoDS_Shape& seam = sm->GetSubShape();
1192 if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1193 u2nodesOnSeam.size() > 0 &&
1194 seam.ShapeType() == TopAbs_EDGE )
1196 int nbE1 = SMESH_MesherHelper::Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1197 int nbE2 = SMESH_MesherHelper::Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1198 if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1200 // find the 2 EDGEs of srcFace
1201 TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1202 for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1203 if ( seam.IsSame( src2tgtIt.Value() ))
1204 SMESH_Algo::addBadInputElements
1205 ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1206 return error( COMPERR_BAD_INPUT_MESH,
1207 "Different number of nodes on two edges projected to a seam edge" );
1212 // Make groups of nodes to merge
1214 u_oldNode = u2nodesMaps[ OLD_NODES ].begin();
1215 u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1216 newEnd = u2nodesMaps[ NEW_NODES ].end();
1217 u_newOnSeam = u2nodesOnSeam.begin();
1218 if ( mergeNewToOld )
1219 for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1221 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1222 groupsOfNodes.back().push_back( u_oldNode->second );
1223 groupsOfNodes.back().push_back( u_newNode->second );
1224 if ( mergeSeamToNew )
1225 groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1227 else if ( mergeSeamToNew )
1228 for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1230 groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1231 groupsOfNodes.back().push_back( u_newNode->second );
1232 groupsOfNodes.back().push_back( u_newOnSeam->second );
1235 } // loop on EDGE and VERTEX submeshes of a target FACE
1239 SMESH_MeshEditor editor( tgtMesh );
1240 int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1241 editor.MergeNodes( groupsOfNodes );
1242 int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1243 if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1244 return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1246 // ----------------------------------------------------------------
1247 // The mapper can't create quadratic elements, so convert if needed
1248 // ----------------------------------------------------------------
1250 faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1251 bool srcIsQuad = faceIt->next()->IsQuadratic();
1252 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1253 bool tgtIsQuad = faceIt->next()->IsQuadratic();
1254 if ( srcIsQuad && !tgtIsQuad )
1256 TIDSortedElemSet tgtFaces;
1257 faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1258 while ( faceIt->more() )
1259 tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1261 editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1264 cleaner.Release(); // not to remove mesh
1266 } // end of projection using Pattern mapping
1269 // ---------------------------
1270 // Check elements orientation
1271 // ---------------------------
1273 TopoDS_Face face = TopoDS::Face( theShape );
1274 if ( !theMesh.IsMainShape( tgtFace ))
1276 // find the main shape
1277 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1278 switch ( mainShape.ShapeType() ) {
1280 case TopAbs_SOLID: break;
1282 TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1283 for ( ; ancestIt.More(); ancestIt.Next() ) {
1284 TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1285 if ( type == TopAbs_SOLID ) {
1286 mainShape = ancestIt.Value();
1288 } else if ( type == TopAbs_SHELL ) {
1289 mainShape = ancestIt.Value();
1293 // find tgtFace in the main solid or shell to know it's true orientation.
1294 TopExp_Explorer exp( mainShape, TopAbs_FACE );
1295 for ( ; exp.More(); exp.Next() ) {
1296 if ( tgtFace.IsSame( exp.Current() )) {
1297 face = TopoDS::Face( exp.Current() );
1303 if ( helper.IsReversedSubMesh( face ))
1305 SMESH_MeshEditor editor( tgtMesh );
1306 SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1307 while ( eIt->more() ) {
1308 const SMDS_MeshElement* e = eIt->next();
1309 if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1310 RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1318 //=======================================================================
1319 //function : Evaluate
1321 //=======================================================================
1323 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
1324 const TopoDS_Shape& theShape,
1325 MapShapeNbElems& aResMap)
1330 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1331 SMESH_Mesh * tgtMesh = & theMesh;
1335 // ---------------------------
1336 // Make sub-shapes association
1337 // ---------------------------
1339 TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1340 TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1342 TAssocTool::TShapeShapeMap shape2ShapeMap;
1343 TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1344 if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1346 !shape2ShapeMap.IsBound( tgtFace ))
1347 return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1349 TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1351 // -------------------------------------------------------
1352 // Assure that mesh on a source Face is computed/evaluated
1353 // -------------------------------------------------------
1355 std::vector<int> aVec;
1357 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1358 if ( srcSubMesh->IsMeshComputed() )
1360 aVec.resize( SMDSEntity_Last, 0 );
1361 aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
1363 SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
1364 while ( elemIt->more() )
1365 aVec[ elemIt->next()->GetEntityType() ]++;
1369 MapShapeNbElems tmpResMap;
1370 MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
1371 if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
1372 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
1373 aVec = srcResMap[ srcSubMesh ];
1375 return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
1378 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1379 aResMap.insert(std::make_pair(sm,aVec));
1385 //=============================================================================
1387 * \brief Sets a default event listener to submesh of the source face
1388 * \param subMesh - submesh where algo is set
1390 * This method is called when a submesh gets HYP_OK algo_state.
1391 * After being set, event listener is notified on each event of a submesh.
1392 * Arranges that CLEAN event is translated from source submesh to
1395 //=============================================================================
1397 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
1399 TAssocTool::SetEventListener( subMesh,
1400 _sourceHypo->GetSourceFace(),
1401 _sourceHypo->GetSourceMesh() );