Salome HOME
Zcracks plugin adaptation to new Zcracks tool
[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     if ( srcWires.size() == 1 ) return false; // tmp
1277
1278     // count boundary points
1279     int iP = 1, nbP = 0;
1280     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1281       nbP += srcWires[iW]->NbPoints() - 1; // 1st and last points coincide
1282
1283     // fill boundary points
1284     BRepMesh::Array1OfVertexOfDelaun srcVert( 1, 1 + nbP ), tgtVert( 1, 1 + nbP );
1285     vector< const SMDS_MeshNode* > bndSrcNodes( nbP + 1 ); bndSrcNodes[0] = 0;
1286     BRepMesh_Vertex v( 0, 0, BRepMesh_Frontier );
1287     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1288     {
1289       const UVPtStructVec& srcPnt = srcWires[iW]->GetUVPtStruct();
1290       const UVPtStructVec& tgtPnt = tgtWires[iW]->GetUVPtStruct();
1291       if ( srcPnt.size() != tgtPnt.size() ) return false;
1292
1293       for ( int i = 0, nb = srcPnt.size() - 1;  i < nb;  ++i, ++iP )
1294       {
1295         bndSrcNodes[ iP ]  = srcPnt[i].node;
1296         srcPnt[i].node->setIsMarked( true );
1297
1298         v.ChangeCoord() = srcPnt[i].UV();
1299         srcVert( iP )   = v;
1300         v.ChangeCoord() = tgtPnt[i].UV();
1301         tgtVert( iP )   = v;
1302       }
1303     }
1304     // triangulate the srcFace in 2D
1305     BRepMesh_Delaun delauney( srcVert );
1306     Handle(BRepMesh_DataStructureOfDelaun) triaDS = delauney.Result();
1307
1308     Handle(ShapeAnalysis_Surface) tgtSurface = tgtHelper.GetSurface( tgtFace );
1309     SMESHDS_Mesh* srcMesh = srcWires[0]->GetMesh()->GetMeshDS();
1310     SMESHDS_Mesh* tgtMesh = tgtHelper.GetMeshDS();
1311     const SMDS_MeshNode *srcNode, *tgtNode;
1312     const BRepMesh_Triangle *bmTria;
1313
1314     // un-mark internal src nodes; later we will mark moved nodes
1315     SMDS_NodeIteratorPtr nIt = srcMesh->MeshElements( srcFace )->GetNodes();
1316     if ( !nIt || !nIt->more() ) return true;
1317     while ( nIt->more() )
1318       ( srcNode = nIt->next() )->setIsMarked( false );
1319
1320     // initialize a queue of nodes with starting triangles
1321     const int srcFaceID = srcNode->getshapeId();
1322     TNodeTriaList noTriQueue;
1323     size_t iBndSrcN = 1;
1324     for ( ; iBndSrcN < bndSrcNodes.size() &&  noTriQueue.empty();  ++iBndSrcN )
1325     {
1326       // get a triangle
1327       const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1328       const BRepMesh_PairOfIndex &    triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1329       const BRepMesh_Triangle&           tria = triaDS->GetElement( triaIds.Index(1) );
1330
1331       addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1332     }
1333
1334     // Move tgt nodes
1335
1336     double bc[3]; // barycentric coordinates
1337     int    nodeIDs[3];
1338     bool   checkUV = true;
1339     const SMDS_FacePosition* pos;
1340
1341     while ( !noTriQueue.empty() )
1342     {
1343       srcNode = noTriQueue.front().first;
1344       bmTria  = noTriQueue.front().second;
1345       noTriQueue.pop_front();
1346       if ( srcNode->isMarked() )
1347         continue;
1348       srcNode->setIsMarked( true );
1349
1350       // find a delauney triangle containing the src node
1351       gp_XY uv = tgtHelper.GetNodeUV( srcFace, srcNode, NULL, &checkUV );
1352       bmTria = findTriangle( uv, bmTria, triaDS, bc );
1353       if ( !bmTria )
1354         continue;
1355
1356       // compute new coordinates for a corresponding tgt node
1357       gp_XY uvNew( 0., 0. ), nodeUV;
1358       triaDS->ElementNodes( *bmTria, nodeIDs );
1359       for ( int i = 0; i < 3; ++i )
1360         uvNew += bc[i] * tgtVert( nodeIDs[i]).Coord();
1361       gp_Pnt xyz = tgtSurface->Value( uvNew );
1362
1363       // find and move tgt node
1364       TAssocTool::TNodeNodeMap::const_iterator n2n = src2tgtNodes.find( srcNode );
1365       if ( n2n == src2tgtNodes.end() ) continue;
1366       tgtNode = n2n->second;
1367       tgtMesh->MoveNode( tgtNode, xyz.X(), xyz.Y(), xyz.Z() );
1368
1369       if (( pos = dynamic_cast< const SMDS_FacePosition* >( tgtNode->GetPosition() )))
1370         const_cast<SMDS_FacePosition*>( pos )->SetParameters( uvNew.X(), uvNew.Y() );
1371
1372       addCloseNodes( srcNode, bmTria, srcFaceID, noTriQueue );
1373
1374       // assure that all src nodes are visited
1375       for ( ; iBndSrcN < bndSrcNodes.size() &&  noTriQueue.empty();  ++iBndSrcN )
1376       {
1377         const BRepMesh::ListOfInteger & linkIds = triaDS->LinksConnectedTo( iBndSrcN );
1378         const BRepMesh_PairOfIndex &    triaIds = triaDS->ElementsConnectedTo( linkIds.First() );
1379         const BRepMesh_Triangle&           tria = triaDS->GetElement( triaIds.Index(1) );
1380         addCloseNodes( bndSrcNodes[ iBndSrcN ], &tria, srcFaceID, noTriQueue );
1381       }
1382     }
1383
1384     return true;
1385   }
1386
1387   //=======================================================================
1388   /*
1389    * Set initial association of VERTEXes for the case of projection
1390    * from a quadrangle FACE to a closed FACE, where opposite src EDGEs
1391    * have different nb of segments
1392    */
1393   //=======================================================================
1394
1395   void initAssoc4Quad2Closed(const TopoDS_Shape&          tgtFace,
1396                              SMESH_MesherHelper&          tgtHelper,
1397                              const TopoDS_Shape&          srcFace,
1398                              SMESH_Mesh*                  srcMesh,
1399                              TAssocTool::TShapeShapeMap & assocMap)
1400   {
1401     if ( !tgtHelper.HasRealSeam() || srcFace.ShapeType() != TopAbs_FACE )
1402       return; // no seam edge
1403     list< TopoDS_Edge > tgtEdges, srcEdges;
1404     list< int > tgtNbEW, srcNbEW;
1405     int tgtNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( tgtFace ), tgtEdges, tgtNbEW );
1406     int srcNbW = SMESH_Block::GetOrderedEdges( TopoDS::Face( srcFace ), srcEdges, srcNbEW );
1407     if ( tgtNbW != 1 || srcNbW != 1 ||
1408          tgtNbEW.front() != 4 || srcNbEW.front() != 4 )
1409       return; // not quads
1410
1411     int srcNbSeg[4];
1412     list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1413     for ( int i = 0; edgeS != srcEdges.end(); ++i, ++edgeS )
1414       if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *edgeS ))
1415         srcNbSeg[ i ] = sm->NbNodes();
1416       else
1417         return; // not meshed
1418     if ( srcNbSeg[0] == srcNbSeg[2] && srcNbSeg[1] == srcNbSeg[3] )
1419       return; // same nb segments
1420     if ( srcNbSeg[0] != srcNbSeg[2] && srcNbSeg[1] != srcNbSeg[3] )
1421       return; // all different nb segments
1422
1423     edgeS = srcEdges.begin();
1424     if ( srcNbSeg[0] != srcNbSeg[2] )
1425       ++edgeS;
1426     TAssocTool::InsertAssociation( tgtHelper.IthVertex( 0,*edgeT ),
1427                                    tgtHelper.IthVertex( 0,*edgeS ), assocMap );
1428     TAssocTool::InsertAssociation( tgtHelper.IthVertex( 1,*edgeT ),
1429                                    tgtHelper.IthVertex( 1,*edgeS ), assocMap );
1430   }
1431
1432 } // namespace
1433
1434
1435 //=======================================================================
1436 //function : Compute
1437 //purpose  :
1438 //=======================================================================
1439
1440 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1441 {
1442   _src2tgtNodes.clear();
1443
1444   if ( !_sourceHypo )
1445     return false;
1446
1447   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1448   SMESH_Mesh * tgtMesh = & theMesh;
1449   if ( !srcMesh )
1450     srcMesh = tgtMesh;
1451
1452   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1453   SMESH_MesherHelper helper( theMesh );
1454
1455   // ---------------------------
1456   // Make sub-shapes association
1457   // ---------------------------
1458
1459   TopoDS_Face   tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1460   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1461
1462   helper.SetSubShape( tgtFace );
1463
1464   TAssocTool::TShapeShapeMap shape2ShapeMap;
1465   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1466   if ( shape2ShapeMap.IsEmpty() )
1467     initAssoc4Quad2Closed( tgtFace, helper, srcShape, srcMesh, shape2ShapeMap );
1468   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1469                                              shape2ShapeMap)  ||
1470        !shape2ShapeMap.IsBound( tgtFace ))
1471   {
1472     if ( srcShape.ShapeType() == TopAbs_FACE )
1473     {
1474       int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1475       int nbE2 = helper.Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1476       if ( nbE1 != nbE2 )
1477         return error(COMPERR_BAD_SHAPE,
1478                      SMESH_Comment("Different number of edges in source and target faces: ")
1479                      << nbE2 << " and " << nbE1 );
1480     }
1481     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1482   }
1483   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1484
1485   // ----------------------------------------------
1486   // Assure that mesh on a source Face is computed
1487   // ----------------------------------------------
1488
1489   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1490   SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1491
1492   string srcMeshError;
1493   if ( tgtMesh == srcMesh ) {
1494     if ( !TAssocTool::MakeComputed( srcSubMesh ))
1495       srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1496   }
1497   else {
1498     if ( !srcSubMesh->IsMeshComputed() )
1499       srcMeshError = TAssocTool::SourceNotComputedError();
1500   }
1501   if ( !srcMeshError.empty() )
1502     return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1503
1504   // ===========
1505   // Projection
1506   // ===========
1507
1508   // get ordered src and tgt EDGEs
1509   TSideVector srcWires, tgtWires;
1510   bool is1DComputed = false; // if any tgt EDGE is meshed
1511   TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh,
1512                          shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1513   if ( err && !err->IsOK() )
1514     return error( err );
1515
1516   bool projDone = false;
1517
1518   if ( !projDone )
1519   {
1520     // try to project from the same face with different location
1521     projDone = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1522                                shape2ShapeMap, _src2tgtNodes, is1DComputed );
1523   }
1524   if ( !projDone )
1525   {
1526     // projection in case if the faces are similar in 2D space
1527     projDone = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1528                                       shape2ShapeMap, _src2tgtNodes, is1DComputed );
1529   }
1530   if ( !projDone )
1531   {
1532     // projection in case of quadrilateral faces
1533     // NOT IMPLEMENTED, returns false
1534     projDone = projectQuads( tgtFace, srcFace, tgtWires, srcWires,
1535                              shape2ShapeMap, _src2tgtNodes, is1DComputed);
1536   }
1537
1538   // it will remove mesh built on edges and vertices in failure case
1539   MeshCleaner cleaner( tgtSubMesh );
1540
1541   if ( !projDone )
1542   {
1543     _src2tgtNodes.clear();
1544     // --------------------
1545     // Prepare to mapping 
1546     // --------------------
1547
1548     // Check if node projection to a face is needed
1549     Bnd_B2d uvBox;
1550     SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1551     set< const SMDS_MeshNode* > faceNodes;
1552     for ( ; faceNodes.size() < 3 && faceIt->more();  ) {
1553       const SMDS_MeshElement* face = faceIt->next();
1554       SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1555       while ( nodeIt->more() ) {
1556         const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1557         if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE &&
1558              faceNodes.insert( node ).second )
1559           uvBox.Add( helper.GetNodeUV( srcFace, node ));
1560       }
1561     }
1562     bool toProjectNodes = false;
1563     if ( faceNodes.size() == 1 )
1564       toProjectNodes = ( uvBox.IsVoid() || uvBox.CornerMin().IsEqual( gp_XY(0,0), 1e-12 ));
1565     else if ( faceNodes.size() > 1 )
1566       toProjectNodes = ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN );
1567
1568     // Find the corresponding source and target vertex
1569     // and <theReverse> flag needed to call mapper.Apply()
1570
1571     TopoDS_Vertex srcV1, tgtV1;
1572     bool reverse = false;
1573
1574     TopExp_Explorer vSrcExp( srcFace, TopAbs_VERTEX );
1575     srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1576     tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1577
1578     list< TopoDS_Edge > tgtEdges, srcEdges;
1579     list< int > nbEdgesInWires;
1580     SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1581     SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1582
1583     if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1584     {
1585       TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1586       TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1587       reverse = ( ! srcE1.IsSame( srcE1bis ));
1588       if ( reverse &&
1589            //_sourceHypo->HasVertexAssociation() &&
1590            nbEdgesInWires.front() > 2 &&
1591            helper.IsRealSeam( tgtEdges.front() ))
1592       {
1593         // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1594         // always puts a seam EDGE first (if possible) and as a result
1595         // we can't use only theReverse flag to correctly associate source
1596         // and target faces in the mapper. Thus we select srcV1 so that
1597         // GetOrderedEdges() to return EDGEs in a needed order
1598         TopoDS_Face tgtFaceBis = tgtFace;
1599         TopTools_MapOfShape checkedVMap( tgtEdges.size() );
1600         checkedVMap.Add ( srcV1 );
1601         for ( vSrcExp.Next(); vSrcExp.More(); )
1602         {
1603           tgtFaceBis.Reverse();
1604           tgtEdges.clear();
1605           SMESH_Block::GetOrderedEdges( tgtFaceBis, tgtEdges, nbEdgesInWires, tgtV1 );
1606           bool ok = true;
1607           list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1608           for ( ; edgeS != srcEdges.end() && ok ; ++edgeS, ++edgeT )
1609             ok = edgeT->IsSame( shape2ShapeMap( *edgeS, /*isSrc=*/true ));
1610           if ( ok )
1611             break; // FOUND!
1612
1613           reverse = !reverse;
1614           if ( reverse )
1615           {
1616             vSrcExp.Next();
1617             while ( vSrcExp.More() && !checkedVMap.Add( vSrcExp.Current() ))
1618               vSrcExp.Next();
1619           }
1620           else
1621           {
1622             srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1623             tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1624             srcEdges.clear();
1625             SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1626           }
1627         }
1628       }
1629       // for the case: project to a closed face from a non-closed face w/o vertex assoc;
1630       // avoid projecting to a seam from two EDGEs with different nb nodes on them
1631       // ( test mesh_Projection_2D_01/B1 )
1632       if ( !_sourceHypo->HasVertexAssociation() &&
1633            nbEdgesInWires.front() > 2 &&
1634            helper.IsRealSeam( tgtEdges.front() ))
1635       {
1636         TopoDS_Shape srcEdge1 = shape2ShapeMap( tgtEdges.front() );
1637         list< TopoDS_Edge >::iterator srcEdge2 =
1638           std::find( srcEdges.begin(), srcEdges.end(), srcEdge1);
1639         list< TopoDS_Edge >::iterator srcEdge3 =
1640           std::find( srcEdges.begin(), srcEdges.end(), srcEdge1.Reversed());
1641         if ( srcEdge2 == srcEdges.end() || srcEdge3 == srcEdges.end() ) // srcEdge1 is not a seam
1642         {
1643           // find srcEdge2 which also will be projected to tgtEdges.front()
1644           for ( srcEdge2 = srcEdges.begin(); srcEdge2 != srcEdges.end(); ++srcEdge2 )
1645             if ( !srcEdge1.IsSame( *srcEdge2 ) &&
1646                  tgtEdges.front().IsSame( shape2ShapeMap( *srcEdge2, /*isSrc=*/true )))
1647               break;
1648           // compare nb nodes on srcEdge1 and srcEdge2
1649           if ( srcEdge2 != srcEdges.end() )
1650           {
1651             int nbN1 = 0, nbN2 = 0;
1652             if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( srcEdge1 ))
1653               nbN1 = sm->NbNodes();
1654             if ( SMESHDS_SubMesh* sm = srcMesh->GetMeshDS()->MeshElements( *srcEdge2 ))
1655               nbN2 = sm->NbNodes();
1656             if ( nbN1 != nbN2 )
1657               srcV1 = helper.IthVertex( 1, srcEdges.front() );
1658           }
1659         }
1660       }
1661     }
1662     else if ( nbEdgesInWires.front() == 1 ) // a sole edge in a wire
1663     {
1664       TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1665       for ( size_t iW = 0; iW < srcWires.size(); ++iW )
1666       {
1667         StdMeshers_FaceSidePtr srcWire = srcWires[iW];
1668         for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
1669           if ( srcE1.IsSame( srcWire->Edge( iE )))
1670           {
1671             reverse = ( tgtE1.Orientation() != tgtWires[iW]->Edge( iE ).Orientation() );
1672             break;
1673           }
1674       }
1675     }
1676     else
1677     {
1678       RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1679     }
1680
1681     // Load pattern from the source face
1682     SMESH_Pattern mapper;
1683     mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1, /*keepNodes=*/true );
1684     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1685       return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1686
1687     // --------------------
1688     // Perform 2D mapping
1689     // --------------------
1690
1691     // Compute mesh on a target face
1692
1693     mapper.Apply( tgtFace, tgtV1, reverse );
1694     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK ) {
1695       // std::ofstream file("/tmp/Pattern.smp" );
1696       // mapper.Save( file );
1697       return error("Can't apply source mesh pattern to the face");
1698     }
1699
1700     // Create the mesh
1701
1702     const bool toCreatePolygons = false, toCreatePolyedrs = false;
1703     mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1704     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1705       return error("Can't make mesh by source mesh pattern");
1706
1707     // fill _src2tgtNodes
1708     std::vector< const SMDS_MeshNode* > *srcNodes, *tgtNodes;
1709     mapper.GetInOutNodes( srcNodes, tgtNodes );
1710     size_t nbN = std::min( srcNodes->size(), tgtNodes->size() );
1711     for ( size_t i = 0; i < nbN; ++i )
1712       if ( (*srcNodes)[i] && (*tgtNodes)[i] )
1713         _src2tgtNodes.insert( make_pair( (*srcNodes)[i], (*tgtNodes)[i] ));
1714
1715
1716   } // end of projection using Pattern mapping
1717
1718   {
1719     // -------------------------------------------------------------------------
1720     // mapper doesn't take care of nodes already existing on edges and vertices,
1721     // so we must merge nodes created by it with existing ones
1722     // -------------------------------------------------------------------------
1723
1724     SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1725
1726     // Make groups of nodes to merge
1727
1728     // loop on EDGE and VERTEX sub-meshes of a target FACE
1729     SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1730                                                                      /*complexShapeFirst=*/false);
1731     while ( smIt->more() )
1732     {
1733       SMESH_subMesh*     sm = smIt->next();
1734       SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1735       if ( !smDS || smDS->NbNodes() == 0 )
1736         continue;
1737       //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1738       //  break;
1739
1740       if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1741       {
1742         if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1743         {
1744           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1745           SMESH_subMeshIteratorPtr smDegenIt
1746             = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1747           while ( smDegenIt->more() )
1748             if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1749             {
1750               SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1751               while ( nIt->more() )
1752                 groupsOfNodes.back().push_back( nIt->next() );
1753             }
1754         }
1755         continue; // do not treat sm of degen VERTEX
1756       }
1757
1758       // Sort new and old nodes of a sub-mesh separately
1759
1760       bool isSeam = helper.IsRealSeam( sm->GetId() );
1761
1762       enum { NEW_NODES = 0, OLD_NODES };
1763       map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1764       map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1765       set< const SMDS_MeshNode* > seamNodes;
1766
1767       // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1768       if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1769         ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1770
1771       SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1772       while ( nIt->more() )
1773       {
1774         const SMDS_MeshNode* node = nIt->next();
1775         bool isOld = isOldNode( node );
1776
1777         if ( !isOld && isSeam ) { // new node on a seam edge
1778           if ( seamNodes.count( node ) )
1779             continue; // node is already in the map
1780         }
1781
1782         // sort nodes on edges by their position
1783         map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1784         switch ( node->GetPosition()->GetTypeOfPosition() )
1785         {
1786         case  SMDS_TOP_VERTEX: {
1787           if ( !is1DComputed && !pos2nodes.empty() )
1788             u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1789           else
1790             pos2nodes.insert( make_pair( 0, node ));
1791           break;
1792         }
1793         case  SMDS_TOP_EDGE:   {
1794           const SMDS_EdgePosition* pos =
1795             static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1796           pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1797           break;
1798         }
1799         default:
1800           RETURN_BAD_RESULT("Wrong node position type: "<<
1801                             node->GetPosition()->GetTypeOfPosition());
1802         }
1803       }
1804       const bool mergeNewToOld =
1805         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1806       const bool mergeSeamToNew =
1807         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1808
1809       if ( !mergeNewToOld )
1810         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1811              u2nodesMaps[ OLD_NODES ].size() > 0 )
1812         {
1813           u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1814           newEnd    = u2nodesMaps[ OLD_NODES ].end();
1815           for ( ; u_oldNode != newEnd; ++u_oldNode )
1816             SMESH_Algo::addBadInputElement( u_oldNode->second );
1817           return error( COMPERR_BAD_INPUT_MESH,
1818                         SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1819                         << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1820                         << " #" << sm->GetId() );
1821         }
1822       if ( isSeam && !mergeSeamToNew ) {
1823         const TopoDS_Shape& seam = sm->GetSubShape();
1824         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1825              u2nodesOnSeam.size()            > 0 &&
1826              seam.ShapeType() == TopAbs_EDGE )
1827         {
1828           int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1829           int nbE2 = helper.Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1830           if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1831           {
1832             // find the 2 EDGEs of srcFace
1833             TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1834             for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1835               if ( seam.IsSame( src2tgtIt.Value() ))
1836                 SMESH_Algo::addBadInputElements
1837                   ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1838             return error( COMPERR_BAD_INPUT_MESH,
1839                           "Different number of nodes on two edges projected to a seam edge" );
1840           }
1841         }
1842       }
1843
1844       // Make groups of nodes to merge
1845
1846       u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1847       u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1848       newEnd    = u2nodesMaps[ NEW_NODES ].end();
1849       u_newOnSeam = u2nodesOnSeam.begin();
1850       if ( mergeNewToOld )
1851         for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1852         {
1853           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1854           groupsOfNodes.back().push_back( u_oldNode->second );
1855           groupsOfNodes.back().push_back( u_newNode->second );
1856           if ( mergeSeamToNew )
1857             groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1858         }
1859       else if ( mergeSeamToNew )
1860         for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1861         {
1862           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1863           groupsOfNodes.back().push_back( u_newNode->second );
1864           groupsOfNodes.back().push_back( u_newOnSeam->second );
1865         }
1866
1867     } // loop on EDGE and VERTEX submeshes of a target FACE
1868
1869     // Merge
1870
1871     SMESH_MeshEditor editor( tgtMesh );
1872     int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1873     editor.MergeNodes( groupsOfNodes );
1874     int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1875     if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1876       return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1877
1878     // ----------------------------------------------------------------
1879     // The mapper can't create quadratic elements, so convert if needed
1880     // ----------------------------------------------------------------
1881
1882     SMDS_ElemIteratorPtr faceIt;
1883     faceIt         = srcSubMesh->GetSubMeshDS()->GetElements();
1884     bool srcIsQuad = faceIt->next()->IsQuadratic();
1885     faceIt         = tgtSubMesh->GetSubMeshDS()->GetElements();
1886     bool tgtIsQuad = faceIt->next()->IsQuadratic();
1887     if ( srcIsQuad && !tgtIsQuad )
1888     {
1889       TIDSortedElemSet tgtFaces;
1890       faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1891       while ( faceIt->more() )
1892         tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1893
1894       editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1895     }
1896   } // end of coincident nodes and quadratic elements treatment
1897
1898
1899   if ( !projDone || is1DComputed )
1900     // ----------------------------------------------------------------
1901     // The mapper can create distorted faces by placing nodes out of the FACE
1902     // boundary, also bad face can be created if EDGEs already discretized
1903     // --> fix bad faces by smoothing
1904     // ----------------------------------------------------------------
1905     if ( helper.IsDistorted2D( tgtSubMesh, /*checkUV=*/false ))
1906     {
1907       morph( helper, tgtFace, srcFace, tgtWires, srcWires, _src2tgtNodes );
1908
1909       if ( !fixDistortedFaces( helper, tgtWires ))
1910         return error("Invalid mesh generated");
1911     }
1912   // ---------------------------
1913   // Check elements orientation
1914   // ---------------------------
1915
1916   TopoDS_Face face = TopoDS::Face( theShape );
1917   if ( !theMesh.IsMainShape( tgtFace ))
1918   {
1919     // find the main shape
1920     TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1921     switch ( mainShape.ShapeType() ) {
1922     case TopAbs_SHELL:
1923     case TopAbs_SOLID: break;
1924     default:
1925       TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1926       for ( ; ancestIt.More(); ancestIt.Next() ) {
1927         TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1928         if ( type == TopAbs_SOLID ) {
1929           mainShape = ancestIt.Value();
1930           break;
1931         } else if ( type == TopAbs_SHELL ) {
1932           mainShape = ancestIt.Value();
1933         }
1934       }
1935     }
1936     // find tgtFace in the main solid or shell to know it's true orientation.
1937     TopExp_Explorer exp( mainShape, TopAbs_FACE );
1938     for ( ; exp.More(); exp.Next() ) {
1939       if ( tgtFace.IsSame( exp.Current() )) {
1940         face = TopoDS::Face( exp.Current() );
1941         break;
1942       }
1943     }
1944   }
1945   // Fix orientation
1946   if ( helper.IsReversedSubMesh( face ))
1947   {
1948     SMESH_MeshEditor editor( tgtMesh );
1949     SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1950     while ( eIt->more() ) {
1951       const SMDS_MeshElement* e = eIt->next();
1952       if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1953         RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1954     }
1955   }
1956
1957   cleaner.Release(); // not to remove mesh
1958
1959   return true;
1960 }
1961
1962
1963 //=======================================================================
1964 //function : Evaluate
1965 //purpose  : 
1966 //=======================================================================
1967
1968 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh&         theMesh,
1969                                         const TopoDS_Shape& theShape,
1970                                         MapShapeNbElems&    aResMap)
1971 {
1972   if ( !_sourceHypo )
1973     return false;
1974
1975   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1976   SMESH_Mesh * tgtMesh = & theMesh;
1977   if ( !srcMesh )
1978     srcMesh = tgtMesh;
1979
1980   // ---------------------------
1981   // Make sub-shapes association
1982   // ---------------------------
1983
1984   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1985   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1986
1987   TAssocTool::TShapeShapeMap shape2ShapeMap;
1988   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1989   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1990                                              shape2ShapeMap)  ||
1991        !shape2ShapeMap.IsBound( tgtFace ))
1992     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1993
1994   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1995
1996   // -------------------------------------------------------
1997   // Assure that mesh on a source Face is computed/evaluated
1998   // -------------------------------------------------------
1999
2000   std::vector<int> aVec;
2001
2002   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
2003   if ( srcSubMesh->IsMeshComputed() )
2004   {
2005     aVec.resize( SMDSEntity_Last, 0 );
2006     aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
2007
2008     SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
2009     while ( elemIt->more() )
2010       aVec[ elemIt->next()->GetEntityType() ]++;
2011   }
2012   else
2013   {
2014     MapShapeNbElems  tmpResMap;
2015     MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
2016     if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
2017       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
2018     aVec = srcResMap[ srcSubMesh ];
2019     if ( aVec.empty() )
2020       return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
2021   }
2022
2023   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
2024   aResMap.insert(std::make_pair(sm,aVec));
2025
2026   return true;
2027 }
2028
2029
2030 //=============================================================================
2031 /*!
2032  * \brief Sets a default event listener to submesh of the source face
2033   * \param subMesh - submesh where algo is set
2034  *
2035  * This method is called when a submesh gets HYP_OK algo_state.
2036  * After being set, event listener is notified on each event of a submesh.
2037  * Arranges that CLEAN event is translated from source submesh to
2038  * the submesh
2039  */
2040 //=============================================================================
2041
2042 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
2043 {
2044   TAssocTool::SetEventListener( subMesh,
2045                                 _sourceHypo->GetSourceFace(),
2046                                 _sourceHypo->GetSourceMesh() );
2047 }