Salome HOME
Re-fix regression of SALOME_TESTS/Grids/smesh/3D_mesh_Extrusion_01/B2
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_2D.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 // File      : StdMeshers_Projection_2D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28 //
29 #include "StdMeshers_Projection_2D.hxx"
30
31 #include "StdMeshers_ProjectionSource2D.hxx"
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "StdMeshers_FaceSide.hxx"
34
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"
49
50 #include <utilities.h>
51
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>
58 #include <TopExp.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
61 #include <TopTools_ListIteratorOfListOfShape.hxx>
62 #include <TopTools_MapOfShape.hxx>
63 #include <TopoDS.hxx>
64 #include <TopoDS_Solid.hxx>
65 #include <gp_Ax2.hxx>
66 #include <gp_Ax3.hxx>
67 #include <gp_GTrsf.hxx>
68
69
70 using namespace std;
71
72 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
73
74 namespace TAssocTool = StdMeshers_ProjectionUtils;
75 //typedef StdMeshers_ProjectionUtils TAssocTool;
76
77 //=======================================================================
78 //function : StdMeshers_Projection_2D
79 //purpose  : 
80 //=======================================================================
81
82 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
83   :SMESH_2D_Algo(hypId, studyId, gen)
84 {
85   _name = "Projection_2D";
86   _compatibleHypothesis.push_back("ProjectionSource2D");
87   _sourceHypo = 0;
88 }
89
90 //================================================================================
91 /*!
92  * \brief Destructor
93  */
94 //================================================================================
95
96 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
97 {}
98
99 //=======================================================================
100 //function : CheckHypothesis
101 //purpose  : 
102 //=======================================================================
103
104 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh&                          theMesh,
105                                                const TopoDS_Shape&                  theShape,
106                                                SMESH_Hypothesis::Hypothesis_Status& theStatus)
107 {
108   list <const SMESHDS_Hypothesis * >::const_iterator itl;
109
110   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
111   if ( hyps.size() == 0 )
112   {
113     theStatus = HYP_MISSING;
114     return false;  // can't work with no hypothesis
115   }
116
117   if ( hyps.size() > 1 )
118   {
119     theStatus = HYP_ALREADY_EXIST;
120     return false;
121   }
122
123   const SMESHDS_Hypothesis *theHyp = hyps.front();
124
125   string hypName = theHyp->GetName();
126
127   theStatus = HYP_OK;
128
129   if (hypName == "ProjectionSource2D")
130   {
131     _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
132
133     // Check hypo parameters
134
135     SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
136     SMESH_Mesh* tgtMesh = & theMesh;
137     if ( !srcMesh )
138       srcMesh = tgtMesh;
139
140     // check vertices
141     if ( _sourceHypo->HasVertexAssociation() )
142     {
143       // source vertices
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() ))
149       {
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() )));
155       }
156       else
157       {
158         // target vertices
159         edge = TAssocTool::GetEdgeByVertices
160           ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
161         if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
162         {
163           theStatus = HYP_BAD_PARAMETER;
164           error("Invalid target vertices");
165           SCRUTE((edge.IsNull()));
166           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
167         }
168         // PAL16203
169         else if ( !_sourceHypo->IsCompoundSource() &&
170                   !SMESH_MesherHelper::IsSubShape( edge, theShape ))
171         {
172           theStatus = HYP_BAD_PARAMETER;
173           error("Invalid target vertices");
174           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
175         }
176       }
177     }
178     // check a source face
179     if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
180          ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
181     {
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() ));
187     }
188   }
189   else
190   {
191     theStatus = HYP_INCOMPATIBLE;
192   }
193   return ( theStatus == HYP_OK );
194 }
195
196 namespace {
197
198   //================================================================================
199   /*!
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
203    */
204   //================================================================================
205
206   bool isOldNode( const SMDS_MeshNode* node )
207   {
208     // old nodes are shared by edges and new ones are shared
209     // only by faces created by mapper
210     //if ( is1DComputed )
211     {
212       bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
213       return isOld;
214     }
215     // else
216     // {
217     //   SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
218     //   bool isNew = invFace->more();
219     //   return !isNew;
220     // }
221   }
222
223   //================================================================================
224   /*!
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
228    */
229   //================================================================================
230
231   class MeshCleaner {
232     SMESH_subMesh* sm;
233   public:
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 )
238     {
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 );
250         }
251         // do not break but iterate over DependsOn()
252 //       }
253 //       default:
254         if ( !withSub ) return;
255         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
256         while ( smIt->more() )
257           Clean( smIt->next(), false );
258 //       }
259     }
260   };
261
262   //================================================================================
263   /*!
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
270    */
271   //================================================================================
272
273   bool getBoundaryNodes ( SMESH_subMesh*                        sm,
274                           const TopoDS_Face&                    face,
275                           map< double, const SMDS_MeshNode* > & u2nodes,
276                           set< const SMDS_MeshNode* > &         seamNodes)
277   {
278     u2nodes.clear();
279     seamNodes.clear();
280     if ( !sm || !sm->GetSubMeshDS() )
281       RETURN_BAD_RESULT("Null submesh");
282
283     SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
284     switch ( sm->GetSubShape().ShapeType() ) {
285
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 );
292         return true;
293       }
294       break;
295     }
296     case TopAbs_EDGE: {
297       
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");
307
308       const SMDS_MeshNode* nV1 = 0;
309       const SMDS_MeshNode* nE = 0;
310
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;
316         nV1 = node;
317
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() )
325             nbNodes /= 2;
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 );
331           if ( !nE ) {
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 );
336           }
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 );
340             else
341               nE = elem->GetNode( elem->NbNodes() - 1 );
342           }
343         }
344       }
345       if ( !nV1 )
346         RETURN_BAD_RESULT("No new node found on V1");
347       if ( !nE )
348         RETURN_BAD_RESULT("new node on edge not found");
349
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() );
356
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 ))
364           break;
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 );
372       }
373       if ( u2nodes.size() != seamNodes.size() )
374         RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
375                           ", " << u2nodes.size() << " != " << seamNodes.size() );
376       return true;
377     }
378     default:;
379     }
380     RETURN_BAD_RESULT ("Unexpected submesh type");
381
382   } // bool getBoundaryNodes()
383
384   //================================================================================
385   /*!
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
391    */
392   //================================================================================
393
394   bool are2dConnected( const TopoDS_Edge & E1,
395                        const TopoDS_Edge & E2,
396                        const TopoDS_Face & F )
397   {
398     double f,l;
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 );
402
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 ));
407
408     return (( uvFirst2.SquareDistance( uvFirst1 ) < tol2 ) ||
409             ( uvFirst2.SquareDistance( uvLast1  ) < tol2 ));
410   }
411
412   //================================================================================
413   /*!
414    * \brief Compose TSideVector for both FACEs keeping matching order of EDGEs
415    *        and fill src2tgtNodes map
416    */
417   //================================================================================
418
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,
427                   bool&                              is1DComputed)
428   {
429     src2tgtNodes.clear();
430
431     // get ordered src EDGEs
432     TError err;
433     srcWires = StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*skipMediumNodes=*/0, err);
434     if (( err && !err->IsOK() ) ||
435         ( srcWires.empty() ))
436       return err;
437
438     SMESH_MesherHelper srcHelper( *srcMesh );
439     srcHelper.SetSubShape( srcFace );
440
441     // make corresponding sequence of tgt EDGEs
442     tgtWires.resize( srcWires.size() );
443     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
444     {
445       StdMeshers_FaceSidePtr srcWire = srcWires[iW];
446
447       list< TopoDS_Edge > tgtEdges;
448       TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
449       for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
450       {
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() )
455           tgtE.Reverse();
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
459         {
460           // check which of edges to reverse, E or one already being in tgtEdges
461           if ( are2dConnected( tgtEdges.back(), tgtE, tgtFace ))
462           {
463             list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
464             std::advance( eIt, index-1 );
465             if ( are2dConnected( tgtEdges.back(), *eIt, tgtFace ))
466               eIt->Reverse();
467           }
468           else
469           {
470             tgtE.Reverse();
471           }
472         }
473         if ( srcWire->NbEdges() == 1 && tgtMesh == srcMesh ) // circle
474         {
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() )
479             tgtE = nE.second;
480         }
481         tgtEdges.push_back( tgtE );
482       }
483
484       tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
485                                                      /*theIsForward = */ true,
486                                                      /*theIgnoreMediumNodes = */false));
487       StdMeshers_FaceSidePtr tgtWire = tgtWires[ iW ];
488
489       // Fill map of src to tgt nodes with nodes on edges
490
491       for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
492       {
493         if ( srcMesh->GetSubMesh( srcWire->Edge(iE) )->IsEmpty() ||
494              tgtMesh->GetSubMesh( tgtWire->Edge(iE) )->IsEmpty() )
495         {
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 );
499           if ( srcN && tgtN )
500             src2tgtNodes.insert( make_pair( srcN, tgtN ));
501         }
502         else
503         {
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);
507           
508           vector< const SMDS_MeshNode* > srcNodes = srcEdge.GetOrderedNodes();
509           vector< const SMDS_MeshNode* > tgtNodes = tgtEdge.GetOrderedNodes();
510
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() )
515           {
516             vector< const SMDS_MeshNode* >::iterator tn = tgtNodes.begin();
517             //if ( srcWire->Edge(iE).Orientation() == tgtWire->Edge(iE).Orientation() )
518             {
519               vector< const SMDS_MeshNode* >::iterator sn = srcNodes.begin();
520               for ( ; tn != tgtNodes.end(); ++tn, ++sn)
521                 src2tgtNodes.insert( make_pair( *sn, *tn ));
522             }
523             // else
524             // {
525             //   vector< const SMDS_MeshNode* >::reverse_iterator sn = srcNodes.rbegin();
526             //   for ( ; tn != tgtNodes.end(); ++tn, ++sn)
527             //     src2tgtNodes.insert( make_pair( *sn, *tn ));
528             // }
529             is1DComputed = true;
530           }
531         }
532       } // loop on EDGEs of a WIRE
533
534     } // loop on WIREs
535
536     return TError();
537   }
538
539   //================================================================================
540   /*!
541    * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
542    * if projection by 3D transformation is possible
543    */
544   //================================================================================
545
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)
553   {
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 );
559
560     const double tol = 1.e-7 * srcMeshDS->getMaxDim();
561
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 ))
566     {
567       gp_GTrsf srcTrsf = srcFace.Location().Transformation();
568       gp_GTrsf tgtTrsf = tgtFace.Location().Transformation();
569       gp_GTrsf t = srcTrsf.Inverted().Multiplied( tgtTrsf );
570       trsf.Set( t );
571       // check
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 );
575       if ( !trsfIsOK )
576       {
577         trsf.Set( tgtTrsf.Inverted().Multiplied( srcTrsf ));
578         trsfIsOK = ( tgtP.Distance( trsf.Transform( srcP )) < tol );
579       }
580     }
581     if ( !trsfIsOK )
582     {
583       // Try to find the 3D transformation
584
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 )
591       {
592         const double minSegLen = srcWires[iW]->Length() / totNbSeg;
593         for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
594         {
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  )
601           {
602             srcPnts.push_back( srcWires[iW]->Value3d( srcU ).XYZ() );
603             tgtPnts.push_back( tgtWires[iW]->Value3d( tgtU ).XYZ() );
604             srcU += srcDu;
605             tgtU += tgtDu;
606             srcBC += srcPnts.back();
607             tgtBC += tgtPnts.back();
608           }
609         }
610       }
611       if ( !trsf.Solve( srcPnts, tgtPnts ))
612         return false;
613
614       // check trsf
615
616       const int nbTestPnt = 20;
617       const size_t  iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
618       // check boundary
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 )
622       {
623         gp_Pnt trsfTgt = trsf.Transform( srcPnts[i] );
624         trsfIsOK = ( trsfTgt.SquareDistance( tgtPnts[i] ) < tol*tol );
625       }
626       // check an in-FACE point
627       if ( trsfIsOK )
628       {
629         BRepAdaptor_Surface srcSurf( srcFace );
630         gp_Pnt srcP =
631           srcSurf.Value( 0.321 * ( srcSurf.FirstUParameter() + srcSurf.LastUParameter() ),
632                          0.123 * ( srcSurf.FirstVParameter() + srcSurf.LastVParameter() ));
633         gp_Pnt tgtTrsfP = trsf.Transform( srcP );
634         TopLoc_Location loc;
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 );
642       }
643       if ( !trsfIsOK )
644         return false;
645     }
646
647     // Make new faces
648
649     // prepare the helper to adding quadratic elements if necessary
650     //helper.SetSubShape( tgtFace );
651     helper.IsQuadraticSubMesh( tgtFace );
652
653     SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
654     if ( !is1DComputed && srcSubDS->NbElements() )
655       helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
656
657     SMESH_MesherHelper srcHelper( *srcMesh );
658     srcHelper.SetSubShape( srcFace );
659
660     const SMDS_MeshNode* nullNode = 0;
661     TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
662
663     // indices of nodes to create properly oriented faces
664     bool isReverse = ( !trsf.IsIdentity() );
665     int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
666     if ( isReverse )
667       std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
668
669     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
670     vector< const SMDS_MeshNode* > tgtNodes;
671     while ( elemIt->more() ) // loop on all mesh faces on srcFace
672     {
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
678       {
679         const SMDS_MeshNode* srcNode = elem->GetNode(i);
680         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
681         if ( srcN_tgtN->second == nullNode )
682         {
683           // create a new node
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() )
688           {
689           case SMDS_TOP_FACE:
690           {
691             gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode );
692             tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), srcUV.X(), srcUV.Y() );
693             break;
694           }
695           case SMDS_TOP_EDGE:
696           {
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 );
701             break;
702           }
703           case SMDS_TOP_VERTEX:
704           {
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 ));
708             break;
709           }
710           default:;
711           }
712         }
713         tgtNodes[i] = srcN_tgtN->second;
714       }
715       // create a new face
716       helper.SetElementsOnShape( true );
717       switch ( nbN )
718       {
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;
721       default:
722         if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
723         helper.AddPolygonalFace( tgtNodes );
724       }
725     }
726
727     // check node positions
728
729     if ( !tgtFace.IsPartner( srcFace ) )
730     {
731       SMESH_MesherHelper edgeHelper( *tgtMesh );
732       edgeHelper.ToFixNodeParameters( true );
733       helper.ToFixNodeParameters( true );
734
735       int nbOkPos = 0;
736       bool toCheck = true;
737       const double tol2d = 1e-12;
738       srcN_tgtN = src2tgtNodes.begin();
739       for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
740       {
741         const SMDS_MeshNode* n = srcN_tgtN->second;
742         switch ( n->GetPosition()->GetTypeOfPosition() )
743         {
744         case SMDS_TOP_FACE:
745         {
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 ))
750             ++nbOkPos;
751           else
752             nbOkPos = -((int) src2tgtNodes.size() );
753           break;
754         }
755         case SMDS_TOP_EDGE:
756         {
757           const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
758           edgeHelper.SetSubShape( tgtE );
759           edgeHelper.GetNodeU( tgtE, n, 0, &toCheck );
760           break;
761         }
762         default:;
763         }
764       }
765     }
766
767     return true;
768
769   } //   bool projectPartner()
770
771   //================================================================================
772   /*!
773    * \brief Preform projection in case if the faces are similar in 2D space
774    */
775   //================================================================================
776
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)
784   {
785     SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
786     SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
787
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
790
791     // 1) Find 2D transformation
792
793     StdMeshers_ProjectionUtils::TrsfFinder2D trsf;
794     {
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);
800
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 );
807       fromTgtCS.Invert();
808       trsf.Set( fromTgtCS * toSrcCS );
809
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 )
814       {
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 );
819       }
820
821       // Find trsf using a least-square approximation
822       if ( !trsfIsOK )
823       {
824         // find trsf
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 )
830         {
831           const double minSegLen = srcWires[iW]->Length() / totNbSeg;
832           for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
833           {
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  )
840             {
841               srcPnts.push_back( srcWires[iW]->Value2d( srcU ).XY() );
842               tgtPnts.push_back( tgtWires[iW]->Value2d( tgtU ).XY() );
843             }
844           }
845         }
846         if ( !trsf.Solve( srcPnts, tgtPnts ))
847           return false;
848
849         // check trsf
850
851         trsfIsOK = true;
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 )
855         {
856           gp_Pnt2d trsfTgt = trsf.Transform( srcPnts[i] );
857           trsfIsOK = ( trsfTgt.Distance( tgtPnts[i] ) < tol );
858         }
859         if ( !trsfIsOK )
860           return false;
861       }
862     } // "Find transformation" block
863
864     // 2) Projection
865
866     SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
867
868     SMESH_MesherHelper helper( *tgtMesh );
869     helper.SetSubShape( tgtFace );
870     if ( is1DComputed )
871       helper.IsQuadraticSubMesh( tgtFace );
872     else
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();
877
878     SMESH_MesherHelper srcHelper( *srcMesh );
879     srcHelper.SetSubShape( srcFace );
880
881     const SMDS_MeshNode* nullNode = 0;
882     TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
883
884     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
885     vector< const SMDS_MeshNode* > tgtNodes;
886     bool uvOK;
887     while ( elemIt->more() ) // loop on all mesh faces on srcFace
888     {
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
893       {
894         const SMDS_MeshNode* srcNode = elem->GetNode(i);
895         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
896         if ( srcN_tgtN->second == nullNode )
897         {
898           // create a new node
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() )
905           {
906           case SMDS_TOP_FACE: {
907             tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
908             break;
909           }
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 );
916             break;
917           }
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 ));
922             break;
923           }
924           default:;
925           }
926           srcN_tgtN->second = n;
927         }
928         tgtNodes[i] = srcN_tgtN->second;
929       }
930       // create a new face (with reversed orientation)
931       switch ( nbN )
932       {
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;
935       }
936     }  // loop on all mesh faces on srcFace
937
938     return true;
939   }
940
941   //================================================================================
942   /*!
943    * \brief Preform projection in case of quadrilateral faces
944    */
945   //================================================================================
946
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)
954   {
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();
959
960     // if ( srcWires[0]->NbEdges() != 4 )
961     //   return false;
962     // if ( !is1DComputed )
963     //   return false;
964     // for ( int iE = 0; iE < 4; ++iE )
965     // {
966     //   SMESHDS_SubMesh* sm = srcMeshDS->MeshElements( srcWires[0]->Edge( iE ));
967     //   if ( !sm ) return false;
968     //   if ( sm->NbNodes() + sm->NbElements() == 0 ) return false;
969     // }
970     // if ( BRepAdaptor_Surface( tgtFace ).GetType() != GeomAbs_Plane )
971     //   return false;
972     // // if ( BRepAdaptor_Surface( tgtFace ).GetType() == GeomAbs_Plane &&
973     // //      BRepAdaptor_Surface( srcFace ).GetType() == GeomAbs_Plane )
974     // //   return false; // too easy
975
976     // // load EDGEs to SMESH_Block
977
978     // SMESH_Block block;
979     // TopTools_IndexedMapOfOrientedShape blockSubShapes;
980     // {
981     //   const TopoDS_Solid& box = srcMesh->PseudoShape();
982     //   TopoDS_Shell shell = TopoDS::Shell( TopExp_Explorer( box, TopAbs_SHELL ).Current() );
983     //   TopoDS_Vertex v;
984     //   block.LoadBlockShapes( shell, v, v, blockSubShapes ); // fill all since operator[] is missing
985     // }
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 );
1001
1002     // // remember connectivity of new faces in terms of ( node-or-XY )
1003
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
1008
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) );
1013
1014     // SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
1015     // newFacesVec.resize( srcSubDS->NbElements() );
1016     // int iFaceSrc = 0;
1017
1018     // SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
1019     // while ( elemIt->more() ) // loop on all mesh faces on srcFace
1020     // {
1021     //   const SMDS_MeshElement* elem = elemIt->next();
1022     //   TFaceConn& tgtNodes = newFacesVec[ iFaceSrc++ ];
1023
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
1027     //   {
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
1032     //     {
1033     //       srcN_tgtN = src2tgtNodes.find( srcNode );
1034     //       if ( srcN_tgtN != src2tgtNodes.end() )
1035     //       {
1036     //         tgtNodeOrXY.first = srcN_tgtN->second; // tgt node exists
1037     //       }
1038     //       else 
1039     //       {
1040     //         // find XY of src node withing the quadrilateral srcFace
1041     //         if ( !block.ComputeParameters( SMESH_TNodeXYZ( srcNode ),
1042     //                                        tgtNodeOrXY.second, srcFaceBID ))
1043     //           return false;
1044     //       }
1045     //     }
1046     //     tgtNodes[ i ] = & tgtNodeOrXY;
1047     //   }
1048     // }
1049
1050     // // as all XY are computed, create tgt nodes and faces
1051
1052     // SMESH_MesherHelper helper( *tgtMesh );
1053     // helper.SetSubShape( tgtFace );
1054     // if ( is1DComputed )
1055     //   helper.IsQuadraticSubMesh( tgtFace );
1056     // else
1057     //   helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
1058     // helper.SetElementsOnShape( true );
1059     // Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
1060
1061     // SMESH_MesherHelper srcHelper( *srcMesh );
1062     // srcHelper.SetSubShape( srcFace );
1063
1064     // vector< const SMDS_MeshNode* > tgtNodes;
1065     // gp_XY uv;
1066
1067     // for ( size_t iFaceTgt = 0; iFaceTgt < newFacesVec.size(); ++iFaceTgt )
1068     // {
1069     //   TFaceConn& tgtConn = newFacesVec[ iFaceTgt ];
1070     //   tgtNodes.resize( tgtConn.size() );
1071     //   for ( size_t iN = 0; iN < tgtConn.size(); ++iN )
1072     //   {
1073     //     const SMDS_MeshNode* & tgtN = tgtConn[ iN ]->first;
1074     //     if ( !tgtN ) // create a node
1075     //     {
1076     //       if ( !block.FaceUV( tgtFaceBID, tgtConn[iN]->second, uv ))
1077     //         return false;
1078     //       gp_Pnt p = tgtSurface->Value( uv.X(), uv.Y() );
1079     //       tgtN = helper.AddNode( p.X(), p.Y(), p.Z(), uv.X(), uv.Y() );
1080     //     }
1081     //     tgtNodes[ tgtNodes.size() - iN - 1] = tgtN; // reversed orientation
1082     //   }
1083     //   switch ( tgtNodes.size() )
1084     //   {
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;
1087     //   default:
1088     //     if ( tgtNodes.size() > 4 )
1089     //       helper.AddPolygonalFace( tgtNodes );
1090     //   }
1091     // }
1092     return false; //true;
1093
1094   } // bool projectQuads(...)
1095
1096   //================================================================================
1097   /*!
1098    * \brief Fix bad faces by smoothing
1099    */
1100   //================================================================================
1101
1102   bool fixDistortedFaces( SMESH_MesherHelper& helper,
1103                           TSideVector&        tgtWires )
1104   {
1105     SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMesh( helper.GetSubShape() );
1106
1107     if ( helper.IsDistorted2D( faceSM, /*checkUV=*/true ))
1108     {
1109       SMESH_MeshEditor editor( helper.GetMesh() );
1110       SMESHDS_SubMesh* smDS = faceSM->GetSubMeshDS();
1111       const TopoDS_Face&  F = TopoDS::Face( faceSM->GetSubShape() );
1112
1113       TIDSortedElemSet faces;
1114       SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
1115       for ( faceIt = smDS->GetElements(); faceIt->more(); )
1116         faces.insert( faces.end(), faceIt->next() );
1117
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 )
1122       {
1123         TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
1124         for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
1125         {
1126           double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
1127                                           F,        tgtWires[iW]->FirstVertex( iE ));
1128           isConcaveBoundary = ( angle < -5. * M_PI / 180. );
1129
1130           prevEdge = tgtWires[iW]->Edge( iE );
1131         }
1132       }
1133       SMESH_MeshEditor:: SmoothMethod algo =
1134         isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
1135
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();
1140
1141       // smoothing
1142       set<const SMDS_MeshNode*> fixedNodes;
1143       editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
1144                      /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
1145
1146       helper.ToFixNodeParameters( true );
1147
1148       return !helper.IsDistorted2D( faceSM, /*checkUV=*/true );
1149     }
1150     return true;
1151   }
1152
1153   typedef list< pair< const SMDS_MeshNode*, const BRepMesh_Triangle* > > TNodeTriaList;
1154
1155   //================================================================================
1156   /*!
1157    * \brief Add in-FACE nodes surrounding a given node to a queue
1158    */
1159   //================================================================================
1160
1161   void addCloseNodes( const SMDS_MeshNode*     srcNode,
1162                       const BRepMesh_Triangle* bmTria,
1163                       const int                srcFaceID,
1164                       TNodeTriaList &          noTriQueue )
1165   {
1166     // find in-FACE nodes
1167     SMDS_ElemIteratorPtr elems = srcNode->GetInverseElementIterator(SMDSAbs_Face);
1168     while ( elems->more() )
1169     {
1170       const SMDS_MeshElement* elem = elems->next();
1171       if ( elem->getshapeId() == srcFaceID )
1172       {
1173         for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
1174         {
1175           const SMDS_MeshNode* n = elem->GetNode( i );
1176           if ( !n->isMarked() )
1177             noTriQueue.push_back( make_pair( n, bmTria ));
1178         }
1179       }
1180     }
1181   }
1182
1183   //================================================================================
1184   /*!
1185    * \brief Find a delauney triangle containing a given 2D point and return
1186    *        barycentric coordinates within the found triangle
1187    */
1188   //================================================================================
1189
1190   const BRepMesh_Triangle* findTriangle( const gp_XY&                            uv,
1191                                          const BRepMesh_Triangle*                bmTria,
1192                                          Handle(BRepMesh_DataStructureOfDelaun)& triaDS,
1193                                          double                                  bc[3] )
1194   {
1195     int   nodeIDs[3];
1196     gp_XY nodeUVs[3];
1197     int   linkIDs[3];
1198     Standard_Boolean ori[3];
1199
1200     while ( bmTria )
1201     {
1202       // check bmTria
1203
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();
1208
1209       SMESH_MeshAlgos::GetBarycentricCoords( uv,
1210                                              nodeUVs[0], nodeUVs[1], nodeUVs[2],
1211                                              bc[0], bc[1] );
1212       if ( bc[0] >= 0 && bc[1] >= 0 && bc[0] + bc[1] <= 1 )
1213       {
1214         bc[2] = 1 - bc[0] - bc[1];
1215         return bmTria;
1216       }
1217
1218       // look for a neighbor triangle, which is adjacent to a link intersected
1219       // by a segment( triangle center -> uv )
1220
1221       gp_XY gc = ( nodeUVs[0] + nodeUVs[1] + nodeUVs[2] ) / 3.;
1222       gp_XY seg = uv - gc;
1223
1224       bmTria->Edges( linkIDs, ori );
1225       int triaID = triaDS->IndexOf( *bmTria );
1226       bmTria = 0;
1227
1228       for ( int i = 0; i < 3; ++i )
1229       {
1230         const BRepMesh_PairOfIndex & triIDs = triaDS->ElementsConnectedTo( linkIDs[i] );
1231         if ( triIDs.Extent() < 2 )
1232           continue; // no neighbor triangle
1233
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
1240
1241         double crossSegLin = seg ^ lin;
1242         if ( Abs( crossSegLin ) < std::numeric_limits<double>::min() )
1243           continue; // parallel
1244
1245         double uSeg = ( uv1 - gc ) ^ lin / crossSegLin;
1246         if ( 0. <= uSeg && uSeg <= 1. )
1247         {
1248           bmTria = & triaDS->GetElement( triIDs.Index( 1 + ( triIDs.Index(1) == triaID )));
1249           break;
1250         }
1251       }
1252     }
1253     return bmTria;
1254   }
1255
1256   //================================================================================
1257   /*!
1258    * \brief Morph mesh on the target face to lie within FACE boundary w/o distortion
1259    *
1260    * algo:
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
1264    *   tgt triangle
1265    */
1266   //================================================================================
1267
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 )
1274   {
1275     if ( srcWires.size() != tgtWires.size() ) return false;
1276
1277     // count boundary points
1278     int iP = 1, nbP = 0;
1279     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1280       nbP += srcWires[iW]->NbPoints() - 1; // 1st and last points coincide
1281
1282     // fill boundary points
1283     BRepMesh::Array1OfVertexOfDelaun srcVert( 1, 1 + nbP ), tgtVert( 1, 1 + nbP );
1284     vector< const SMDS_MeshNode* > bndSrcNodes( nbP + 1 ); bndSrcNodes[0] = 0;
1285     BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
1286     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1287     {
1288       const UVPtStructVec& srcPnt = srcWires[iW]->GetUVPtStruct();
1289       const UVPtStructVec& tgtPnt = tgtWires[iW]->GetUVPtStruct();
1290       if ( srcPnt.size() != tgtPnt.size() ) return false;
1291
1292       for ( int i = 0, nb = srcPnt.size() - 1;  i < nb;  ++i, ++iP )
1293       {
1294         bndSrcNodes[ iP ]  = srcPnt[i].node;
1295         srcPnt[i].node->setIsMarked( true );
1296
1297         v.ChangeCoord() = srcPnt[i].UV();
1298         srcVert( iP )   = v;
1299         v.ChangeCoord() = tgtPnt[i].UV();
1300         tgtVert( iP )   = v;
1301       }
1302     }
1303     // triangulate the srcFace in 2D
1304     BRepMesh_Delaun delauney( srcVert );
1305     Handle(BRepMesh_DataStructureOfDelaun) triaDS = delauney.Result();
1306
1307     Handle(ShapeAnalysis_Surface) tgtSurface = tgtHelper.GetSurface( tgtFace );
1308     SMESHDS_Mesh* srcMesh = srcWires[0]->GetMesh()->GetMeshDS();
1309     SMESHDS_Mesh* tgtMesh = tgtHelper.GetMeshDS();
1310     const SMDS_MeshNode *srcNode, *tgtNode;
1311     const BRepMesh_Triangle *bmTria;
1312
1313     // un-mark internal src nodes; later we will mark moved nodes
1314     SMDS_NodeIteratorPtr nIt = srcMesh->MeshElements( srcFace )->GetNodes();
1315     if ( !nIt || !nIt->more() ) return true;
1316     while ( nIt->more() )
1317       ( srcNode = nIt->next() )->setIsMarked( false );
1318
1319     // initialize a queue of nodes with starting triangles
1320     const int srcFaceID = srcNode->getshapeId();
1321     TNodeTriaList noTriQueue;
1322     size_t iBndSrcN = 1;
1323     for ( ; iBndSrcN < bndSrcNodes.size() &&  noTriQueue.empty();  ++iBndSrcN )
1324     {
1325       // get a triangle
1326       const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1327       const BRepMesh_PairOfIndex &    triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1328       const BRepMesh_Triangle&           tria = triaDS->GetElement( triaIds.Index(1) );
1329
1330       addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1331     }
1332
1333     // Move tgt nodes
1334
1335     double bc[3]; // barycentric coordinates
1336     int    nodeIDs[3];
1337     bool   checkUV = true;
1338     const SMDS_FacePosition* pos;
1339
1340     while ( !noTriQueue.empty() )
1341     {
1342       srcNode = noTriQueue.front().first;
1343       bmTria  = noTriQueue.front().second;
1344       noTriQueue.pop_front();
1345       if ( srcNode->isMarked() )
1346         continue;
1347       srcNode->setIsMarked( true );
1348
1349       // find a delauney triangle containing the src node
1350       gp_XY uv = tgtHelper.GetNodeUV( srcFace, srcNode, NULL, &checkUV );
1351       bmTria = findTriangle( uv, bmTria, triaDS, bc );
1352       if ( !bmTria )
1353         continue;
1354
1355       // compute new coordinates for a corresponding tgt node
1356       gp_XY uvNew( 0., 0. ), nodeUV;
1357       triaDS->ElementNodes( *bmTria, nodeIDs );
1358       for ( int i = 0; i < 3; ++i )
1359         uvNew += bc[i] * tgtVert( nodeIDs[i]).Coord();
1360       gp_Pnt xyz = tgtSurface->Value( uvNew );
1361
1362       // find and move tgt node
1363       TAssocTool::TNodeNodeMap::const_iterator n2n = src2tgtNodes.find( srcNode );
1364       if ( n2n == src2tgtNodes.end() ) continue;
1365       tgtNode = n2n->second;
1366       tgtMesh->MoveNode( tgtNode, xyz.X(), xyz.Y(), xyz.Z() );
1367
1368       if (( pos = dynamic_cast< const SMDS_FacePosition* >( tgtNode->GetPosition() )))
1369         const_cast<SMDS_FacePosition*>( pos )->SetParameters( uvNew.X(), uvNew.Y() );
1370
1371       addCloseNodes( srcNode, bmTria, srcFaceID, noTriQueue );
1372
1373       // assure that all src nodes are visited
1374       for ( ; iBndSrcN < bndSrcNodes.size() &&  noTriQueue.empty();  ++iBndSrcN )
1375       {
1376         const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1377         const BRepMesh_PairOfIndex &    triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1378         const BRepMesh_Triangle&           tria = triaDS->GetElement( triaIds.Index(1) );
1379         addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1380       }
1381     }
1382
1383     return true;
1384   }
1385
1386   //=======================================================================
1387   /*
1388    * Set initial association of VERTEXes for the case of projection
1389    * from a quadrangle FACE to a closed FACE, where opposite src EDGEs
1390    * have different nb of segments
1391    */
1392   //=======================================================================
1393
1394   void initAssoc4Quad2Closed(const TopoDS_Shape&          tgtFace,
1395                              SMESH_MesherHelper&          tgtHelper,
1396                              const TopoDS_Shape&          srcFace,
1397                              SMESH_Mesh*                  srcMesh,
1398                              TAssocTool::TShapeShapeMap & assocMap)
1399   {
1400     if ( !tgtHelper.HasRealSeam() || srcFace.ShapeType() != TopAbs_FACE )
1401       return; // no seam edge
1402     list< TopoDS_Edge > tgtEdges, srcEdges;
1403     list< int > tgtNbEW, srcNbEW;
1404     int tgtNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( tgtFace ), tgtEdges, tgtNbEW );
1405     int srcNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( srcFace ), srcEdges, srcNbEW );
1406     if ( tgtNbW != 1 || srcNbW != 1 ||
1407          tgtNbEW.front() != 4 || srcNbEW.front() != 4 )
1408       return; // not quads
1409
1410     int srcNbSeg[4];
1411     list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1412     for ( int i = 0; edgeS != srcEdges.end(); ++i, ++edgeS )
1413       if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *edgeS ))
1414         srcNbSeg[ i ] = sm->NbNodes();
1415       else
1416         return; // not meshed
1417     if ( srcNbSeg[0] == srcNbSeg[2] && srcNbSeg[1] == srcNbSeg[3] )
1418       return; // same nb segments
1419     if ( srcNbSeg[0] != srcNbSeg[2] && srcNbSeg[1] != srcNbSeg[3] )
1420       return; // all different nb segments
1421
1422     edgeS = srcEdges.begin();
1423     if ( srcNbSeg[0] != srcNbSeg[2] )
1424       ++edgeS;
1425     TAssocTool::InsertAssociation( tgtHelper.IthVertex( 0,*edgeT ),
1426                                    tgtHelper.IthVertex( 0,*edgeS ), assocMap );
1427     TAssocTool::InsertAssociation( tgtHelper.IthVertex( 1,*edgeT ),
1428                                    tgtHelper.IthVertex( 1,*edgeS ), assocMap );
1429   }
1430
1431 } // namespace
1432
1433
1434 //=======================================================================
1435 //function : Compute
1436 //purpose  :
1437 //=======================================================================
1438
1439 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1440 {
1441   _src2tgtNodes.clear();
1442
1443   if ( !_sourceHypo )
1444     return false;
1445
1446   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1447   SMESH_Mesh * tgtMesh = & theMesh;
1448   if ( !srcMesh )
1449     srcMesh = tgtMesh;
1450
1451   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1452   SMESH_MesherHelper helper( theMesh );
1453
1454   // ---------------------------
1455   // Make sub-shapes association
1456   // ---------------------------
1457
1458   TopoDS_Face   tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1459   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1460
1461   helper.SetSubShape( tgtFace );
1462
1463   TAssocTool::TShapeShapeMap shape2ShapeMap;
1464   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1465   if ( shape2ShapeMap.IsEmpty() )
1466     initAssoc4Quad2Closed( tgtFace, helper, srcShape, srcMesh, shape2ShapeMap );
1467   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1468                                              shape2ShapeMap)  ||
1469        !shape2ShapeMap.IsBound( tgtFace ))
1470   {
1471     if ( srcShape.ShapeType() == TopAbs_FACE )
1472     {
1473       int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1474       int nbE2 = helper.Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1475       if ( nbE1 != nbE2 )
1476         return error(COMPERR_BAD_SHAPE,
1477                      SMESH_Comment("Different number of edges in source and target faces: ")
1478                      << nbE2 << " and " << nbE1 );
1479     }
1480     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1481   }
1482   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1483
1484   // ----------------------------------------------
1485   // Assure that mesh on a source Face is computed
1486   // ----------------------------------------------
1487
1488   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1489   SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1490
1491   string srcMeshError;
1492   if ( tgtMesh == srcMesh ) {
1493     if ( !TAssocTool::MakeComputed( srcSubMesh ))
1494       srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1495   }
1496   else {
1497     if ( !srcSubMesh->IsMeshComputed() )
1498       srcMeshError = TAssocTool::SourceNotComputedError();
1499   }
1500   if ( !srcMeshError.empty() )
1501     return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1502
1503   // ===========
1504   // Projection
1505   // ===========
1506
1507   // get ordered src and tgt EDGEs
1508   TSideVector srcWires, tgtWires;
1509   bool is1DComputed = false; // if any tgt EDGE is meshed
1510   TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh,
1511                          shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1512   if ( err && !err->IsOK() )
1513     return error( err );
1514
1515   bool projDone = false;
1516
1517   if ( !projDone )
1518   {
1519     // try to project from the same face with different location
1520     projDone = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1521                                shape2ShapeMap, _src2tgtNodes, is1DComputed );
1522   }
1523   if ( !projDone )
1524   {
1525     // projection in case if the faces are similar in 2D space
1526     projDone = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1527                                       shape2ShapeMap, _src2tgtNodes, is1DComputed );
1528   }
1529   if ( !projDone )
1530   {
1531     // projection in case of quadrilateral faces
1532     // NOT IMPLEMENTED, returns false
1533     projDone = projectQuads( tgtFace, srcFace, tgtWires, srcWires,
1534                              shape2ShapeMap, _src2tgtNodes, is1DComputed);
1535   }
1536
1537   // it will remove mesh built on edges and vertices in failure case
1538   MeshCleaner cleaner( tgtSubMesh );
1539
1540   if ( !projDone )
1541   {
1542     _src2tgtNodes.clear();
1543     // --------------------
1544     // Prepare to mapping 
1545     // --------------------
1546
1547     // Check if node projection to a face is needed
1548     Bnd_B2d uvBox;
1549     SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1550     set< const SMDS_MeshNode* > faceNodes;
1551     for ( ; faceNodes.size() < 3 && faceIt->more();  ) {
1552       const SMDS_MeshElement* face = faceIt->next();
1553       SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1554       while ( nodeIt->more() ) {
1555         const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1556         if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE &&
1557              faceNodes.insert( node ).second )
1558           uvBox.Add( helper.GetNodeUV( srcFace, node ));
1559       }
1560     }
1561     bool toProjectNodes = false;
1562     if ( faceNodes.size() == 1 )
1563       toProjectNodes = ( uvBox.IsVoid() || uvBox.CornerMin().IsEqual( gp_XY(0,0), 1e-12 ));
1564     else if ( faceNodes.size() > 1 )
1565       toProjectNodes = ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN );
1566
1567     // Find the corresponding source and target vertex
1568     // and <theReverse> flag needed to call mapper.Apply()
1569
1570     TopoDS_Vertex srcV1, tgtV1;
1571     bool reverse = false;
1572
1573     TopExp_Explorer vSrcExp( srcFace, TopAbs_VERTEX );
1574     srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1575     tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1576
1577     list< TopoDS_Edge > tgtEdges, srcEdges;
1578     list< int > nbEdgesInWires;
1579     SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1580     SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1581
1582     if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1583     {
1584       TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1585       TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1586       reverse = ( ! srcE1.IsSame( srcE1bis ));
1587       if ( reverse &&
1588            //_sourceHypo->HasVertexAssociation() &&
1589            nbEdgesInWires.front() > 2 &&
1590            helper.IsRealSeam( tgtEdges.front() ))
1591       {
1592         // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1593         // always puts a seam EDGE first (if possible) and as a result
1594         // we can't use only theReverse flag to correctly associate source
1595         // and target faces in the mapper. Thus we select srcV1 so that
1596         // GetOrderedEdges() to return EDGEs in a needed order
1597         TopoDS_Face tgtFaceBis = tgtFace;
1598         TopTools_MapOfShape checkedVMap( tgtEdges.size() );
1599         checkedVMap.Add ( srcV1 );
1600         for ( vSrcExp.Next(); vSrcExp.More(); )
1601         {
1602           tgtFaceBis.Reverse();
1603           tgtEdges.clear();
1604           SMESH_Block::GetOrderedEdges( tgtFaceBis, tgtEdges, nbEdgesInWires, tgtV1 );
1605           bool ok = true;
1606           list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1607           for ( ; edgeS != srcEdges.end() && ok ; ++edgeS, ++edgeT )
1608             ok = edgeT->IsSame( shape2ShapeMap( *edgeS, /*isSrc=*/true ));
1609           if ( ok )
1610             break; // FOUND!
1611
1612           reverse = !reverse;
1613           if ( reverse )
1614           {
1615             vSrcExp.Next();
1616             while ( vSrcExp.More() && !checkedVMap.Add( vSrcExp.Current() ))
1617               vSrcExp.Next();
1618           }
1619           else
1620           {
1621             srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1622             tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1623             srcEdges.clear();
1624             SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1625           }
1626         }
1627       }
1628       // for the case: project to a closed face from a non-closed face w/o vertex assoc;
1629       // avoid projecting to a seam from two EDGEs with different nb nodes on them
1630       // ( test mesh_Projection_2D_01/B1 )
1631       if ( !_sourceHypo->HasVertexAssociation() &&
1632            nbEdgesInWires.front() > 2 &&
1633            helper.IsRealSeam( tgtEdges.front() ))
1634       {
1635         TopoDS_Shape srcEdge1 = shape2ShapeMap( tgtEdges.front() );
1636         list< TopoDS_Edge >::iterator srcEdge2 =
1637           std::find( srcEdges.begin(), srcEdges.end(), srcEdge1);
1638         list< TopoDS_Edge >::iterator srcEdge3 =
1639           std::find( srcEdges.begin(), srcEdges.end(), srcEdge1.Reversed());
1640         if ( srcEdge2 == srcEdges.end() || srcEdge3 == srcEdges.end() ) // srcEdge1 is not a seam
1641         {
1642           // find srcEdge2 which also will be projected to tgtEdges.front()
1643           for ( srcEdge2 = srcEdges.begin(); srcEdge2 != srcEdges.end(); ++srcEdge2 )
1644             if ( !srcEdge1.IsSame( *srcEdge2 ) &&
1645                  tgtEdges.front().IsSame( shape2ShapeMap( *srcEdge2, /*isSrc=*/true )))
1646               break;
1647           // compare nb nodes on srcEdge1 and srcEdge2
1648           if ( srcEdge2 != srcEdges.end() )
1649           {
1650             int nbN1 = 0, nbN2 = 0;
1651             if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( srcEdge1 ))
1652               nbN1 = sm->NbNodes();
1653             if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *srcEdge2 ))
1654               nbN2 = sm->NbNodes();
1655             if ( nbN1 != nbN2 )
1656               srcV1 = helper.IthVertex( 1, srcEdges.front() );
1657           }
1658         }
1659       }
1660     }
1661     else if ( nbEdgesInWires.front() == 1 ) // a sole edge in a wire
1662     {
1663       TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1664       for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1665       {
1666         StdMeshers_FaceSidePtr srcWire = srcWires[iW];
1667         for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
1668           if ( srcE1.IsSame( srcWire->Edge( iE )))
1669           {
1670             reverse = ( tgtE1.Orientation() != tgtWires[iW]->Edge( iE ).Orientation() );
1671             break;
1672           }
1673       }
1674     }
1675     else
1676     {
1677       RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1678     }
1679
1680     // Load pattern from the source face
1681     SMESH_Pattern mapper;
1682     mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1, /*keepNodes=*/true );
1683     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1684       return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1685
1686     // --------------------
1687     // Perform 2D mapping
1688     // --------------------
1689
1690     // Compute mesh on a target face
1691
1692     mapper.Apply( tgtFace, tgtV1, reverse );
1693     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK ) {
1694       // std::ofstream file("/tmp/Pattern.smp" );
1695       // mapper.Save( file );
1696       return error("Can't apply source mesh pattern to the face");
1697     }
1698
1699     // Create the mesh
1700
1701     const bool toCreatePolygons = false, toCreatePolyedrs = false;
1702     mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1703     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1704       return error("Can't make mesh by source mesh pattern");
1705
1706     // fill _src2tgtNodes
1707     std::vector< const SMDS_MeshNode* > *srcNodes, *tgtNodes;
1708     mapper.GetInOutNodes( srcNodes, tgtNodes );
1709     size_t nbN = std::min( srcNodes->size(), tgtNodes->size() );
1710     for ( size_t i = 0; i < nbN; ++i )
1711       if ( (*srcNodes)[i] && (*tgtNodes)[i] )
1712         _src2tgtNodes.insert( make_pair( (*srcNodes)[i], (*tgtNodes)[i] ));
1713
1714
1715   } // end of projection using Pattern mapping
1716
1717   {
1718     // -------------------------------------------------------------------------
1719     // mapper doesn't take care of nodes already existing on edges and vertices,
1720     // so we must merge nodes created by it with existing ones
1721     // -------------------------------------------------------------------------
1722
1723     SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1724
1725     // Make groups of nodes to merge
1726
1727     // loop on EDGE and VERTEX sub-meshes of a target FACE
1728     SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1729                                                                      /*complexShapeFirst=*/false);
1730     while ( smIt->more() )
1731     {
1732       SMESH_subMesh*     sm = smIt->next();
1733       SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1734       if ( !smDS || smDS->NbNodes() == 0 )
1735         continue;
1736       //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1737       //  break;
1738
1739       if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1740       {
1741         if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1742         {
1743           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1744           SMESH_subMeshIteratorPtr smDegenIt
1745             = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1746           while ( smDegenIt->more() )
1747             if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1748             {
1749               SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1750               while ( nIt->more() )
1751                 groupsOfNodes.back().push_back( nIt->next() );
1752             }
1753         }
1754         continue; // do not treat sm of degen VERTEX
1755       }
1756
1757       // Sort new and old nodes of a sub-mesh separately
1758
1759       bool isSeam = helper.IsRealSeam( sm->GetId() );
1760
1761       enum { NEW_NODES = 0, OLD_NODES };
1762       map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1763       map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1764       set< const SMDS_MeshNode* > seamNodes;
1765
1766       // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1767       if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1768         ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1769
1770       SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1771       while ( nIt->more() )
1772       {
1773         const SMDS_MeshNode* node = nIt->next();
1774         bool isOld = isOldNode( node );
1775
1776         if ( !isOld && isSeam ) { // new node on a seam edge
1777           if ( seamNodes.count( node ) )
1778             continue; // node is already in the map
1779         }
1780
1781         // sort nodes on edges by their position
1782         map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1783         switch ( node->GetPosition()->GetTypeOfPosition() )
1784         {
1785         case  SMDS_TOP_VERTEX: {
1786           if ( !is1DComputed && !pos2nodes.empty() )
1787             u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1788           else
1789             pos2nodes.insert( make_pair( 0, node ));
1790           break;
1791         }
1792         case  SMDS_TOP_EDGE:   {
1793           const SMDS_EdgePosition* pos =
1794             static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1795           pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1796           break;
1797         }
1798         default:
1799           RETURN_BAD_RESULT("Wrong node position type: "<<
1800                             node->GetPosition()->GetTypeOfPosition());
1801         }
1802       }
1803       const bool mergeNewToOld =
1804         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1805       const bool mergeSeamToNew =
1806         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1807
1808       if ( !mergeNewToOld )
1809         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1810              u2nodesMaps[ OLD_NODES ].size() > 0 )
1811         {
1812           u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1813           newEnd    = u2nodesMaps[ OLD_NODES ].end();
1814           for ( ; u_oldNode != newEnd; ++u_oldNode )
1815             SMESH_Algo::addBadInputElement( u_oldNode->second );
1816           return error( COMPERR_BAD_INPUT_MESH,
1817                         SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1818                         << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1819                         << " #" << sm->GetId() );
1820         }
1821       if ( isSeam && !mergeSeamToNew ) {
1822         const TopoDS_Shape& seam = sm->GetSubShape();
1823         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1824              u2nodesOnSeam.size()            > 0 &&
1825              seam.ShapeType() == TopAbs_EDGE )
1826         {
1827           int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1828           int nbE2 = helper.Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1829           if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1830           {
1831             // find the 2 EDGEs of srcFace
1832             TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1833             for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1834               if ( seam.IsSame( src2tgtIt.Value() ))
1835                 SMESH_Algo::addBadInputElements
1836                   ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1837             return error( COMPERR_BAD_INPUT_MESH,
1838                           "Different number of nodes on two edges projected to a seam edge" );
1839           }
1840         }
1841       }
1842
1843       // Make groups of nodes to merge
1844
1845       u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1846       u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1847       newEnd    = u2nodesMaps[ NEW_NODES ].end();
1848       u_newOnSeam = u2nodesOnSeam.begin();
1849       if ( mergeNewToOld )
1850         for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1851         {
1852           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1853           groupsOfNodes.back().push_back( u_oldNode->second );
1854           groupsOfNodes.back().push_back( u_newNode->second );
1855           if ( mergeSeamToNew )
1856             groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1857         }
1858       else if ( mergeSeamToNew )
1859         for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1860         {
1861           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1862           groupsOfNodes.back().push_back( u_newNode->second );
1863           groupsOfNodes.back().push_back( u_newOnSeam->second );
1864         }
1865
1866     } // loop on EDGE and VERTEX submeshes of a target FACE
1867
1868     // Merge
1869
1870     SMESH_MeshEditor editor( tgtMesh );
1871     int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1872     editor.MergeNodes( groupsOfNodes );
1873     int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1874     if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1875       return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1876
1877     // ----------------------------------------------------------------
1878     // The mapper can't create quadratic elements, so convert if needed
1879     // ----------------------------------------------------------------
1880
1881     SMDS_ElemIteratorPtr faceIt;
1882     faceIt         = srcSubMesh->GetSubMeshDS()->GetElements();
1883     bool srcIsQuad = faceIt->next()->IsQuadratic();
1884     faceIt         = tgtSubMesh->GetSubMeshDS()->GetElements();
1885     bool tgtIsQuad = faceIt->next()->IsQuadratic();
1886     if ( srcIsQuad && !tgtIsQuad )
1887     {
1888       TIDSortedElemSet tgtFaces;
1889       faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1890       while ( faceIt->more() )
1891         tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1892
1893       editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1894     }
1895   } // end of coincident nodes and quadratic elements treatment
1896
1897
1898   if ( !projDone || is1DComputed )
1899     // ----------------------------------------------------------------
1900     // The mapper can create distorted faces by placing nodes out of the FACE
1901     // boundary, also bad face can be created if EDGEs already discretized
1902     // --> fix bad faces by smoothing
1903     // ----------------------------------------------------------------
1904     if ( helper.IsDistorted2D( tgtSubMesh, /*checkUV=*/false ))
1905     {
1906       morph( helper, tgtFace, srcFace, tgtWires, srcWires, _src2tgtNodes );
1907
1908       if ( !fixDistortedFaces( helper, tgtWires ))
1909         return error("Invalid mesh generated");
1910     }
1911   // ---------------------------
1912   // Check elements orientation
1913   // ---------------------------
1914
1915   TopoDS_Face face = TopoDS::Face( theShape );
1916   if ( !theMesh.IsMainShape( tgtFace ))
1917   {
1918     // find the main shape
1919     TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1920     switch ( mainShape.ShapeType() ) {
1921     case TopAbs_SHELL:
1922     case TopAbs_SOLID: break;
1923     default:
1924       TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1925       for ( ; ancestIt.More(); ancestIt.Next() ) {
1926         TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1927         if ( type == TopAbs_SOLID ) {
1928           mainShape = ancestIt.Value();
1929           break;
1930         } else if ( type == TopAbs_SHELL ) {
1931           mainShape = ancestIt.Value();
1932         }
1933       }
1934     }
1935     // find tgtFace in the main solid or shell to know it's true orientation.
1936     TopExp_Explorer exp( mainShape, TopAbs_FACE );
1937     for ( ; exp.More(); exp.Next() ) {
1938       if ( tgtFace.IsSame( exp.Current() )) {
1939         face = TopoDS::Face( exp.Current() );
1940         break;
1941       }
1942     }
1943   }
1944   // Fix orientation
1945   if ( helper.IsReversedSubMesh( face ))
1946   {
1947     SMESH_MeshEditor editor( tgtMesh );
1948     SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1949     while ( eIt->more() ) {
1950       const SMDS_MeshElement* e = eIt->next();
1951       if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1952         RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1953     }
1954   }
1955
1956   cleaner.Release(); // not to remove mesh
1957
1958   return true;
1959 }
1960
1961
1962 //=======================================================================
1963 //function : Evaluate
1964 //purpose  : 
1965 //=======================================================================
1966
1967 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh&         theMesh,
1968                                         const TopoDS_Shape& theShape,
1969                                         MapShapeNbElems&    aResMap)
1970 {
1971   if ( !_sourceHypo )
1972     return false;
1973
1974   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1975   SMESH_Mesh * tgtMesh = & theMesh;
1976   if ( !srcMesh )
1977     srcMesh = tgtMesh;
1978
1979   // ---------------------------
1980   // Make sub-shapes association
1981   // ---------------------------
1982
1983   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1984   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1985
1986   TAssocTool::TShapeShapeMap shape2ShapeMap;
1987   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1988   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1989                                              shape2ShapeMap)  ||
1990        !shape2ShapeMap.IsBound( tgtFace ))
1991     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1992
1993   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1994
1995   // -------------------------------------------------------
1996   // Assure that mesh on a source Face is computed/evaluated
1997   // -------------------------------------------------------
1998
1999   std::vector<int> aVec;
2000
2001   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
2002   if ( srcSubMesh->IsMeshComputed() )
2003   {
2004     aVec.resize( SMDSEntity_Last, 0 );
2005     aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
2006
2007     SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
2008     while ( elemIt->more() )
2009       aVec[ elemIt->next()->GetEntityType() ]++;
2010   }
2011   else
2012   {
2013     MapShapeNbElems  tmpResMap;
2014     MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
2015     if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
2016       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
2017     aVec = srcResMap[ srcSubMesh ];
2018     if ( aVec.empty() )
2019       return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
2020   }
2021
2022   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
2023   aResMap.insert(std::make_pair(sm,aVec));
2024
2025   return true;
2026 }
2027
2028
2029 //=============================================================================
2030 /*!
2031  * \brief Sets a default event listener to submesh of the source face
2032   * \param subMesh - submesh where algo is set
2033  *
2034  * This method is called when a submesh gets HYP_OK algo_state.
2035  * After being set, event listener is notified on each event of a submesh.
2036  * Arranges that CLEAN event is translated from source submesh to
2037  * the submesh
2038  */
2039 //=============================================================================
2040
2041 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
2042 {
2043   TAssocTool::SetEventListener( subMesh,
2044                                 _sourceHypo->GetSourceFace(),
2045                                 _sourceHypo->GetSourceMesh() );
2046 }