Salome HOME
2db02b2ce110fcd767728e8109252e6da13e4bcb
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_2D.cxx
1 // Copyright (C) 2007-2015  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_SubMesh.hxx"
39 #include "SMESH_Block.hxx"
40 #include "SMESH_Comment.hxx"
41 #include "SMESH_Gen.hxx"
42 #include "SMESH_Mesh.hxx"
43 #include "SMESH_MesherHelper.hxx"
44 #include "SMESH_Pattern.hxx"
45 #include "SMESH_subMesh.hxx"
46 #include "SMESH_subMeshEventListener.hxx"
47
48 #include "utilities.h"
49
50 #include <BRepAdaptor_Surface.hxx>
51 #include <BRep_Tool.hxx>
52 #include <Bnd_B2d.hxx>
53 #include <GeomAPI_ProjectPointOnSurf.hxx>
54 #include <GeomLib_IsPlanarSurface.hxx>
55 #include <TopExp.hxx>
56 #include <TopExp_Explorer.hxx>
57 #include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <TopoDS.hxx>
60 #include <gp_Ax2.hxx>
61 #include <gp_Ax3.hxx>
62
63
64 using namespace std;
65
66 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
67
68 namespace TAssocTool = StdMeshers_ProjectionUtils;
69 //typedef StdMeshers_ProjectionUtils TAssocTool;
70
71 //=======================================================================
72 //function : StdMeshers_Projection_2D
73 //purpose  : 
74 //=======================================================================
75
76 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
77   :SMESH_2D_Algo(hypId, studyId, gen)
78 {
79   _name = "Projection_2D";
80   _compatibleHypothesis.push_back("ProjectionSource2D");
81   _sourceHypo = 0;
82 }
83
84 //================================================================================
85 /*!
86  * \brief Destructor
87  */
88 //================================================================================
89
90 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
91 {}
92
93 //=======================================================================
94 //function : CheckHypothesis
95 //purpose  : 
96 //=======================================================================
97
98 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh&                          theMesh,
99                                                const TopoDS_Shape&                  theShape,
100                                                SMESH_Hypothesis::Hypothesis_Status& theStatus)
101 {
102   list <const SMESHDS_Hypothesis * >::const_iterator itl;
103
104   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
105   if ( hyps.size() == 0 )
106   {
107     theStatus = HYP_MISSING;
108     return false;  // can't work with no hypothesis
109   }
110
111   if ( hyps.size() > 1 )
112   {
113     theStatus = HYP_ALREADY_EXIST;
114     return false;
115   }
116
117   const SMESHDS_Hypothesis *theHyp = hyps.front();
118
119   string hypName = theHyp->GetName();
120
121   theStatus = HYP_OK;
122
123   if (hypName == "ProjectionSource2D")
124   {
125     _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
126
127     // Check hypo parameters
128
129     SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
130     SMESH_Mesh* tgtMesh = & theMesh;
131     if ( !srcMesh )
132       srcMesh = tgtMesh;
133
134     // check vertices
135     if ( _sourceHypo->HasVertexAssociation() )
136     {
137       // source vertices
138       TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
139         ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
140       if ( edge.IsNull() ||
141            !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
142            !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
143       {
144         theStatus = HYP_BAD_PARAMETER;
145         error("Invalid source vertices");
146         SCRUTE((edge.IsNull()));
147         SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
148         SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
149       }
150       else
151       {
152         // target vertices
153         edge = TAssocTool::GetEdgeByVertices
154           ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
155         if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
156         {
157           theStatus = HYP_BAD_PARAMETER;
158           error("Invalid target vertices");
159           SCRUTE((edge.IsNull()));
160           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
161         }
162         // PAL16203
163         else if ( !_sourceHypo->IsCompoundSource() &&
164                   !SMESH_MesherHelper::IsSubShape( edge, theShape ))
165         {
166           theStatus = HYP_BAD_PARAMETER;
167           error("Invalid target vertices");
168           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
169         }
170       }
171     }
172     // check a source face
173     if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
174          ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
175     {
176       theStatus = HYP_BAD_PARAMETER;
177       error("Invalid source face");
178       SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
179       SCRUTE((srcMesh == tgtMesh));
180       SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
181     }
182   }
183   else
184   {
185     theStatus = HYP_INCOMPATIBLE;
186   }
187   return ( theStatus == HYP_OK );
188 }
189
190 namespace {
191
192   //================================================================================
193   /*!
194    * \brief define if a node is new or old
195    * \param node - node to check
196    * \retval bool - true if the node existed before Compute() is called
197    */
198   //================================================================================
199
200   bool isOldNode( const SMDS_MeshNode* node )
201   {
202     // old nodes are shared by edges and new ones are shared
203     // only by faces created by mapper
204     //if ( is1DComputed )
205     {
206       bool isOld = node->NbInverseElements(SMDSAbs_Edge) > 0;
207       return isOld;
208     }
209     // else
210     // {
211     //   SMDS_ElemIteratorPtr invFace = node->GetInverseElementIterator(SMDSAbs_Face);
212     //   bool isNew = invFace->more();
213     //   return !isNew;
214     // }
215   }
216
217   //================================================================================
218   /*!
219    * \brief Class to remove mesh built by pattern mapper on edges
220    * and vertices in the case of failure of projection algo.
221    * It does it's job at destruction
222    */
223   //================================================================================
224
225   class MeshCleaner {
226     SMESH_subMesh* sm;
227   public:
228     MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
229     ~MeshCleaner() { Clean(sm); }
230     void Release() { sm = 0; } // mesh will not be removed
231     static void Clean( SMESH_subMesh* sm, bool withSub=true )
232     {
233       if ( !sm || !sm->GetSubMeshDS() ) return;
234       // PAL16567, 18920. Remove face nodes as well
235 //       switch ( sm->GetSubShape().ShapeType() ) {
236 //       case TopAbs_VERTEX:
237 //       case TopAbs_EDGE: {
238         SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
239         SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
240         while ( nIt->more() ) {
241           const SMDS_MeshNode* node = nIt->next();
242           if ( !isOldNode( node ) )
243             mesh->RemoveNode( node );
244         }
245         // do not break but iterate over DependsOn()
246 //       }
247 //       default:
248         if ( !withSub ) return;
249         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
250         while ( smIt->more() )
251           Clean( smIt->next(), false );
252 //       }
253     }
254   };
255
256   //================================================================================
257   /*!
258    * \brief find new nodes belonging to one free border of mesh on face
259     * \param sm - submesh on edge or vertex containg nodes to choose from
260     * \param face - the face bound by the submesh
261     * \param u2nodes - map to fill with nodes
262     * \param seamNodes - set of found nodes
263     * \retval bool - is a success
264    */
265   //================================================================================
266
267   bool getBoundaryNodes ( SMESH_subMesh*                        sm,
268                           const TopoDS_Face&                    face,
269                           map< double, const SMDS_MeshNode* > & u2nodes,
270                           set< const SMDS_MeshNode* > &         seamNodes)
271   {
272     u2nodes.clear();
273     seamNodes.clear();
274     if ( !sm || !sm->GetSubMeshDS() )
275       RETURN_BAD_RESULT("Null submesh");
276
277     SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
278     switch ( sm->GetSubShape().ShapeType() ) {
279
280     case TopAbs_VERTEX: {
281       while ( nIt->more() ) {
282         const SMDS_MeshNode* node = nIt->next();
283         if ( isOldNode( node ) ) continue;
284         u2nodes.insert( make_pair( 0., node ));
285         seamNodes.insert( node );
286         return true;
287       }
288       break;
289     }
290     case TopAbs_EDGE: {
291       
292       // Get submeshes of sub-vertices
293       const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
294       if ( subSM.size() != 2 )
295         RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
296                           " but we have " << subSM.size());
297       SMESH_subMesh* smV1 = subSM.begin()->second;
298       SMESH_subMesh* smV2 = subSM.rbegin()->second;
299       if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
300         RETURN_BAD_RESULT("Empty vertex submeshes");
301
302       const SMDS_MeshNode* nV1 = 0;
303       const SMDS_MeshNode* nE = 0;
304
305       // Look for nV1 - a new node on V1
306       nIt = smV1->GetSubMeshDS()->GetNodes();
307       while ( nIt->more() && !nE ) {
308         const SMDS_MeshNode* node = nIt->next();
309         if ( isOldNode( node ) ) continue;
310         nV1 = node;
311
312         // Find nE - a new node connected to nV1 and belonging to edge submesh;
313         SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
314         SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
315         while ( vElems->more() && !nE ) {
316           const SMDS_MeshElement* elem = vElems->next();
317           int nbNodes = elem->NbNodes();
318           if ( elem->IsQuadratic() )
319             nbNodes /= 2;
320           int iV1 = elem->GetNodeIndex( nV1 );
321           // try next after nV1
322           int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
323           if ( smDS->Contains( elem->GetNode( iE ) ))
324             nE = elem->GetNode( iE );
325           if ( !nE ) {
326             // try node before nV1
327             iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
328             if ( smDS->Contains( elem->GetNode( iE )))
329               nE = elem->GetNode( iE );
330           }
331           if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
332             if ( Abs( iV1 - iE ) == 1 )
333               nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
334             else
335               nE = elem->GetNode( elem->NbNodes() - 1 );
336           }
337         }
338       }
339       if ( !nV1 )
340         RETURN_BAD_RESULT("No new node found on V1");
341       if ( !nE )
342         RETURN_BAD_RESULT("new node on edge not found");
343
344       // Get the whole free border of a face
345       list< const SMDS_MeshNode* > bordNodes;
346       list< const SMDS_MeshElement* > bordFaces;
347       if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
348         RETURN_BAD_RESULT("free border of a face not found by nodes " <<
349                           nV1->GetID() << " " << nE->GetID() );
350
351       // Insert nodes of the free border to the map until node on V2 encountered
352       SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
353       list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
354       bordIt++; // skip nV1
355       for ( ; bordIt != bordNodes.end(); ++bordIt ) {
356         const SMDS_MeshNode* node = *bordIt;
357         if ( v2smDS->Contains( node ))
358           break;
359         if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
360           RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
361                             " pos type " << node->GetPosition()->GetTypeOfPosition());
362         const SMDS_EdgePosition* pos =
363           static_cast<const SMDS_EdgePosition*>(node->GetPosition());
364         u2nodes.insert( make_pair( pos->GetUParameter(), node ));
365         seamNodes.insert( node );
366       }
367       if ( u2nodes.size() != seamNodes.size() )
368         RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
369                           ", " << u2nodes.size() << " != " << seamNodes.size() );
370       return true;
371     }
372     default:;
373     }
374     RETURN_BAD_RESULT ("Unexpected submesh type");
375
376   } // bool getBoundaryNodes()
377
378   //================================================================================
379   /*!
380    * \brief Check if two consecutive EDGEs are connected in 2D
381    *  \param [in] E1 - a well oriented non-seam EDGE
382    *  \param [in] E2 - a possibly well oriented seam EDGE
383    *  \param [in] F - a FACE
384    *  \return bool - result
385    */
386   //================================================================================
387
388   bool are2dConnected( const TopoDS_Edge & E1,
389                        const TopoDS_Edge & E2,
390                        const TopoDS_Face & F )
391   {
392     double f,l;
393     Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( E1, F, f, l );
394     gp_Pnt2d uvLast1 = c1->Value( E1.Orientation() == TopAbs_REVERSED ? f : l );
395
396     Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( E2, F, f, l );
397     gp_Pnt2d uvFirst2 = c2->Value( f );
398     gp_Pnt2d uvLast2  = c2->Value( l );
399     double tol2 = 1e-5 * uvLast2.SquareDistance( uvFirst2 );
400
401     return (( uvLast1.SquareDistance( uvFirst2 ) < tol2 ) ||
402             ( uvLast1.SquareDistance( uvLast2 ) < tol2 ));
403   }
404
405   //================================================================================
406   /*!
407    * \brief Compose TSideVector for both FACEs keeping matching order of EDGEs
408    *        and fill src2tgtNodes map
409    */
410   //================================================================================
411
412   TError getWires(const TopoDS_Face&                 tgtFace,
413                   const TopoDS_Face&                 srcFace,
414                   SMESH_Mesh *                       tgtMesh,
415                   SMESH_Mesh *                       srcMesh,
416                   const TAssocTool::TShapeShapeMap&  shape2ShapeMap,
417                   TSideVector&                       srcWires,
418                   TSideVector&                       tgtWires,
419                   TAssocTool::TNodeNodeMap&          src2tgtNodes,
420                   bool&                              is1DComputed)
421   {
422     SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
423     SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
424
425     src2tgtNodes.clear();
426
427     // get ordered src EDGEs
428     TError err;
429     srcWires = StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*skipMediumNodes=*/0, err);
430     if ( err && !err->IsOK() || srcWires.empty() )
431       return err;
432
433     SMESH_MesherHelper srcHelper( *srcMesh );
434     srcHelper.SetSubShape( srcFace );
435
436     // make corresponding sequence of tgt EDGEs
437     tgtWires.resize( srcWires.size() );
438     for ( size_t iW = 0; iW < srcWires.size(); ++iW )
439     {
440       // check ori
441       bool reverse = false;
442       StdMeshers_FaceSidePtr srcWire = srcWires[iW];
443       // for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
444       // {
445       //   if ( srcHelper.IsRealSeam( srcWire->EdgeID( iE )))
446       //     continue;
447       //   TopoDS_Shape srcE = srcWire->Edge( iE );
448       //   TopoDS_Shape tgtE = shape2ShapeMap( srcE, /*isSrc=*/true);
449       //   if ( shape2ShapeMap._assocType == TShapeShapeMap::PROPAGATION ||
450       //        shape2ShapeMap._assocType == TShapeShapeMap::PROPAGATION)
451       //   {
452       //     reverse = false;
453       //   }
454       //   else if ( tgtMesh == srcMesh )
455       //   {
456       //     reverse = (( srcE.Orientation() == srcHelper.GetSubShapeOri( srcFace, srcE )) !=
457       //                ( tgtE.Orientation() == srcHelper.GetSubShapeOri( tgtFace, tgtE )));
458       //   }
459       //   else
460       //   {
461       //     TopoDS_Shape srcEbis = shape2ShapeMap( tgtE, /*isSrc=*/false );
462       //     reverse = ( srcE.Orientation() != srcEbis.Orientation() );
463       //   }
464       //   break;
465       // }
466
467       list< TopoDS_Edge > tgtEdges;
468       TopTools_IndexedMapOfShape edgeMap; // to detect seam edges
469       for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
470       {
471         TopoDS_Edge     srcE = srcWire->Edge( iE );
472         TopoDS_Edge     tgtE = TopoDS::Edge( shape2ShapeMap( srcE, /*isSrc=*/true));
473         TopoDS_Shape srcEbis = shape2ShapeMap( tgtE, /*isSrc=*/false );
474         if ( srcE.Orientation() != srcEbis.Orientation() )
475           //if ( reverse )
476           tgtE.Reverse();
477         // reverse a seam edge encountered for the second time
478         const int index = edgeMap.Add( tgtE );
479         if ( index < edgeMap.Extent() ) // E is a seam
480         {
481           // check which of edges to reverse, E or one already being in tgtEdges
482           if ( are2dConnected( tgtEdges.back(), tgtE, tgtFace ))
483           {
484             list< TopoDS_Edge >::iterator eIt = tgtEdges.begin();
485             std::advance( eIt, index-1 );
486             eIt->Reverse();
487           }
488           else
489           {
490             tgtE.Reverse();
491           }
492         }
493         if ( srcWire->NbEdges() == 1 && tgtMesh == srcMesh ) // circle
494         {
495           // try to verify ori by propagation
496           pair<int,TopoDS_Edge> nE =
497             StdMeshers_ProjectionUtils::GetPropagationEdge( srcMesh, tgtE, srcE );
498           if ( !nE.second.IsNull() )
499             tgtE = nE.second;
500         }
501         tgtEdges.push_back( tgtE );
502
503
504         // Fill map of src to tgt nodes with nodes on edges
505
506         if ( srcMesh->GetSubMesh( srcE )->IsEmpty() ||
507              tgtMesh->GetSubMesh( tgtE )->IsEmpty() )
508         {
509           // add nodes on VERTEXes for a case of not meshes EDGEs
510           const TopoDS_Shape&  srcV = SMESH_MesherHelper::IthVertex( 0, srcE );
511           const TopoDS_Shape&  tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
512           const SMDS_MeshNode* srcN = SMESH_Algo::VertexNode( TopoDS::Vertex( srcV ), srcMeshDS );
513           const SMDS_MeshNode* tgtN = SMESH_Algo::VertexNode( TopoDS::Vertex( tgtV ), tgtMeshDS );
514           if ( srcN && tgtN )
515             src2tgtNodes.insert( make_pair( srcN, tgtN ));
516         }
517         else
518         {
519           const bool skipMediumNodes = true;
520           map< double, const SMDS_MeshNode* > srcNodes, tgtNodes;
521           if ( !SMESH_Algo::GetSortedNodesOnEdge( srcMeshDS, srcE, skipMediumNodes, srcNodes) ||
522                !SMESH_Algo::GetSortedNodesOnEdge( tgtMeshDS, tgtE, skipMediumNodes, tgtNodes ))
523             return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
524                                             "Invalid node parameters on edges");
525
526           if (( srcNodes.size() != tgtNodes.size() ) && tgtNodes.size() > 0 )
527             return SMESH_ComputeError::New( COMPERR_BAD_INPUT_MESH,
528                                             "Different number of nodes on edges");
529           if ( !tgtNodes.empty() )
530           {
531             map< double, const SMDS_MeshNode* >::iterator u_tn = tgtNodes.begin();
532             if ( srcE.Orientation() == tgtE.Orientation() )
533             {
534               map< double, const SMDS_MeshNode* >::iterator u_sn = srcNodes.begin();
535               for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
536                 src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
537             }
538             else
539             {
540               map< double, const SMDS_MeshNode* >::reverse_iterator u_sn = srcNodes.rbegin();
541               for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
542                 src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
543             }
544             is1DComputed = true;
545           }
546         }
547       } // loop on EDGEs of a WIRE
548
549       tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
550                                                      /*theIsForward = */ true,
551                                                      /*theIgnoreMediumNodes = */false));
552     } // loop on WIREs
553
554     return TError();
555   }
556
557   //================================================================================
558   /*!
559    * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
560    * if projection by 3D transformation is possible
561    */
562   //================================================================================
563
564   bool projectPartner(const TopoDS_Face&                 tgtFace,
565                       const TopoDS_Face&                 srcFace,
566                       const TSideVector&                 tgtWires,
567                       const TSideVector&                 srcWires,
568                       const TAssocTool::TShapeShapeMap&  shape2ShapeMap,
569                       TAssocTool::TNodeNodeMap&          src2tgtNodes,
570                       const bool                         is1DComputed)
571   {
572     SMESH_Mesh *    tgtMesh = tgtWires[0]->GetMesh();
573     SMESH_Mesh *    srcMesh = srcWires[0]->GetMesh();
574     SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
575     SMESHDS_Mesh* srcMeshDS = srcMesh->GetMeshDS();
576     SMESH_MesherHelper helper( *tgtMesh );
577
578     const double tol = 1.e-7 * srcMeshDS->getMaxDim();
579
580     // transformation to get location of target nodes from source ones
581     StdMeshers_ProjectionUtils::TrsfFinder3D trsf;
582     if ( tgtFace.IsPartner( srcFace ))
583     {
584       gp_Trsf srcTrsf = srcFace.Location();
585       gp_Trsf tgtTrsf = tgtFace.Location();
586       trsf.Set( srcTrsf.Inverted() * tgtTrsf );
587       // check
588       gp_Pnt srcP = BRep_Tool::Pnt( srcWires[0]->FirstVertex() );
589       gp_Pnt tgtP = BRep_Tool::Pnt( tgtWires[0]->FirstVertex() );
590       if ( tgtP.Distance( trsf.Transform( srcP )) > tol )
591         trsf.Set( tgtTrsf.Inverted() * srcTrsf );
592     }
593     else
594     {
595       // Try to find the 3D transformation
596
597       const int totNbSeg = 50;
598       vector< gp_XYZ > srcPnts, tgtPnts;
599       srcPnts.reserve( totNbSeg );
600       tgtPnts.reserve( totNbSeg );
601       gp_XYZ srcBC( 0,0,0 ), tgtBC( 0,0,0 );
602       for ( size_t iW = 0; iW < srcWires.size(); ++iW )
603       {
604         const double minSegLen = srcWires[iW]->Length() / totNbSeg;
605         for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
606         {
607           int nbSeg    = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
608           double srcU  = srcWires[iW]->FirstParameter( iE );
609           double tgtU  = tgtWires[iW]->FirstParameter( iE );
610           double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
611           double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
612           for ( size_t i = 0; i < nbSeg; ++i  )
613           {
614             srcPnts.push_back( srcWires[iW]->Value3d( srcU ).XYZ() );
615             tgtPnts.push_back( tgtWires[iW]->Value3d( tgtU ).XYZ() );
616             srcU += srcDu;
617             tgtU += tgtDu;
618             srcBC += srcPnts.back();
619             tgtBC += tgtPnts.back();
620           }
621         }
622       }
623       if ( !trsf.Solve( srcPnts, tgtPnts ))
624         return false;
625
626       // check trsf
627
628       bool trsfIsOK = true;
629       const int nbTestPnt = 20;
630       const size_t  iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
631       // check boundary
632       gp_Pnt trsfTgt = trsf.Transform( srcBC / srcPnts.size() );
633       trsfIsOK = ( trsfTgt.SquareDistance( tgtBC / tgtPnts.size() ) < tol*tol );
634       for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
635       {
636         gp_Pnt trsfTgt = trsf.Transform( srcPnts[i] );
637         trsfIsOK = ( trsfTgt.SquareDistance( tgtPnts[i] ) < tol*tol );
638       }
639       // check an in-FACE point
640       if ( trsfIsOK )
641       {
642         BRepAdaptor_Surface srcSurf( srcFace );
643         gp_Pnt srcP =
644           srcSurf.Value( 0.321 * ( srcSurf.FirstUParameter() + srcSurf.LastUParameter() ),
645                          0.123 * ( srcSurf.FirstVParameter() + srcSurf.LastVParameter() ));
646         gp_Pnt tgtTrsfP = trsf.Transform( srcP );
647         TopLoc_Location loc;
648         GeomAPI_ProjectPointOnSurf& proj = helper.GetProjector( tgtFace, loc, 0.1*tol );
649         if ( !loc.IsIdentity() )
650           tgtTrsfP.Transform( loc.Transformation().Inverted() );
651         proj.Perform( tgtTrsfP );
652         trsfIsOK = ( proj.IsDone() &&
653                      proj.NbPoints() > 0 &&
654                      proj.LowerDistance() < tol );
655       }
656       if ( !trsfIsOK )
657         return false;
658     }
659
660     // Make new faces
661
662     // prepare the helper to adding quadratic elements if necessary
663     helper.SetSubShape( tgtFace );
664     helper.IsQuadraticSubMesh( tgtFace );
665
666     SMESHDS_SubMesh* srcSubDS = srcMeshDS->MeshElements( srcFace );
667     if ( !is1DComputed && srcSubDS->NbElements() )
668       helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
669
670     SMESH_MesherHelper srcHelper( *srcMesh );
671     srcHelper.SetSubShape( srcFace );
672
673     const SMDS_MeshNode* nullNode = 0;
674     TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
675
676     // indices of nodes to create properly oriented faces
677     bool isReverse = ( !trsf.IsIdentity() );
678     int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
679     if ( isReverse )
680       std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
681
682     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
683     vector< const SMDS_MeshNode* > tgtNodes;
684     while ( elemIt->more() ) // loop on all mesh faces on srcFace
685     {
686       const SMDS_MeshElement* elem = elemIt->next();
687       const int nbN = elem->NbCornerNodes(); 
688       tgtNodes.resize( nbN );
689       helper.SetElementsOnShape( false );
690       for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
691       {
692         const SMDS_MeshNode* srcNode = elem->GetNode(i);
693         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
694         if ( srcN_tgtN->second == nullNode )
695         {
696           // create a new node
697           gp_Pnt tgtP = trsf.Transform( SMESH_TNodeXYZ( srcNode ));
698           SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
699           srcN_tgtN->second = n;
700           switch ( srcNode->GetPosition()->GetTypeOfPosition() )
701           {
702           case SMDS_TOP_FACE:
703           {
704             gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode );
705             tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), srcUV.X(), srcUV.Y() );
706             break;
707           }
708           case SMDS_TOP_EDGE:
709           {
710             const TopoDS_Shape & srcE = srcMeshDS->IndexToShape( srcNode->getshapeId() );
711             const TopoDS_Shape & tgtE = shape2ShapeMap( srcE, /*isSrc=*/true );
712             double srcU = srcHelper.GetNodeU( TopoDS::Edge( srcE ), srcNode );
713             tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtE ), srcU );
714             break;
715           }
716           case SMDS_TOP_VERTEX:
717           {
718             const TopoDS_Shape & srcV = srcMeshDS->IndexToShape( srcNode->getshapeId() );
719             const TopoDS_Shape & tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
720             tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
721             break;
722           }
723           default:;
724           }
725         }
726         tgtNodes[i] = srcN_tgtN->second;
727       }
728       // create a new face
729       helper.SetElementsOnShape( true );
730       switch ( nbN )
731       {
732       case 3: helper.AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
733       case 4: helper.AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
734       default:
735         if ( isReverse ) std::reverse( tgtNodes.begin(), tgtNodes.end() );
736         helper.AddPolygonalFace( tgtNodes );
737       }
738     }
739
740     // check node positions
741
742     if ( !tgtFace.IsPartner( srcFace ) )
743     {
744       SMESH_MesherHelper edgeHelper( *tgtMesh );
745       edgeHelper.ToFixNodeParameters( true );
746       helper.ToFixNodeParameters( true );
747
748       int nbOkPos = 0;
749       bool toCheck = true;
750       const double tol2d = 1e-12;
751       srcN_tgtN = src2tgtNodes.begin();
752       for ( ; srcN_tgtN != src2tgtNodes.end(); ++srcN_tgtN )
753       {
754         const SMDS_MeshNode* n = srcN_tgtN->second;
755         switch ( n->GetPosition()->GetTypeOfPosition() )
756         {
757         case SMDS_TOP_FACE:
758         {
759           if ( nbOkPos > 10 ) break;
760           gp_XY uv = helper.GetNodeUV( tgtFace, n ), uvBis = uv;
761           if (( helper.CheckNodeUV( tgtFace, n, uv, tol )) &&
762               (( uv - uvBis ).SquareModulus() < tol2d ))
763             ++nbOkPos;
764           else
765             nbOkPos = -((int) src2tgtNodes.size() );
766           break;
767         }
768         case SMDS_TOP_EDGE:
769         {
770           const TopoDS_Edge & tgtE = TopoDS::Edge( tgtMeshDS->IndexToShape( n->getshapeId() ));
771           edgeHelper.SetSubShape( tgtE );
772           edgeHelper.GetNodeU( tgtE, n, 0, &toCheck );
773           break;
774         }
775         default:;
776         }
777       }
778     }
779
780     return true;
781
782   } //   bool projectPartner()
783
784   //================================================================================
785   /*!
786    * \brief Preform projection in case if the faces are similar in 2D space
787    */
788   //================================================================================
789
790   bool projectBy2DSimilarity(const TopoDS_Face&                 tgtFace,
791                              const TopoDS_Face&                 srcFace,
792                              const TSideVector&                 tgtWires,
793                              const TSideVector&                 srcWires,
794                              const TAssocTool::TShapeShapeMap&  shape2ShapeMap,
795                              TAssocTool::TNodeNodeMap&          src2tgtNodes,
796                              const bool                         is1DComputed)
797   {
798     SMESH_Mesh * tgtMesh = tgtWires[0]->GetMesh();
799     SMESH_Mesh * srcMesh = srcWires[0]->GetMesh();
800
801     // WARNING: we can have problems if the FACE is symmetrical in 2D,
802     // then the projection can be mirrored relating to what is expected
803
804     // 1) Find 2D transformation
805
806     StdMeshers_ProjectionUtils::TrsfFinder2D trsf;
807     {
808       // get 2 pairs of corresponding UVs
809       gp_Pnt2d srcP0 = srcWires[0]->Value2d(0.0);
810       gp_Pnt2d srcP1 = srcWires[0]->Value2d(0.333);
811       gp_Pnt2d tgtP0 = tgtWires[0]->Value2d(0.0);
812       gp_Pnt2d tgtP1 = tgtWires[0]->Value2d(0.333);
813
814       // make transformation
815       gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
816       gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
817       gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
818       toSrcCS  .SetTransformation( srcCS );
819       fromTgtCS.SetTransformation( tgtCS );
820       fromTgtCS.Invert();
821       trsf.Set( fromTgtCS * toSrcCS );
822
823       // check transformation
824       bool trsfIsOK = true;
825       const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
826       for ( double u = 0.12; ( u < 1. && trsfIsOK ); u += 0.1 )
827       {
828         gp_Pnt2d srcUV  = srcWires[0]->Value2d( u );
829         gp_Pnt2d tgtUV  = tgtWires[0]->Value2d( u );
830         gp_Pnt2d tgtUV2 = trsf.Transform( srcUV );
831         trsfIsOK = ( tgtUV.Distance( tgtUV2 ) < tol );
832       }
833
834       // Find trsf using a least-square approximation
835       if ( !trsfIsOK )
836       {
837         // find trsf
838         const int totNbSeg = 50;
839         vector< gp_XY > srcPnts, tgtPnts;
840         srcPnts.resize( totNbSeg );
841         tgtPnts.resize( totNbSeg );
842         for ( size_t iW = 0; iW < srcWires.size(); ++iW )
843         {
844           const double minSegLen = srcWires[iW]->Length() / totNbSeg;
845           for ( int iE = 0; iE < srcWires[iW]->NbEdges(); ++iE )
846           {
847             int nbSeg    = Max( 1, int( srcWires[iW]->EdgeLength( iE ) / minSegLen ));
848             double srcU  = srcWires[iW]->FirstParameter( iE );
849             double tgtU  = tgtWires[iW]->FirstParameter( iE );
850             double srcDu = ( srcWires[iW]->LastParameter( iE )- srcU ) / nbSeg;
851             double tgtDu = ( tgtWires[iW]->LastParameter( iE )- tgtU ) / nbSeg;
852             for ( size_t i = 0; i < nbSeg; ++i, srcU += srcDu, tgtU += tgtDu  )
853             {
854               srcPnts.push_back( srcWires[iW]->Value2d( srcU ).XY() );
855               tgtPnts.push_back( tgtWires[iW]->Value2d( tgtU ).XY() );
856             }
857           }
858         }
859         if ( !trsf.Solve( srcPnts, tgtPnts ))
860           return false;
861
862         // check trsf
863
864         trsfIsOK = true;
865         const int nbTestPnt = 10;
866         const size_t  iStep = Max( 1, int( srcPnts.size() / nbTestPnt ));
867         for ( size_t i = 0; ( i < srcPnts.size() && trsfIsOK ); i += iStep )
868         {
869           gp_Pnt2d trsfTgt = trsf.Transform( srcPnts[i] );
870           trsfIsOK = ( trsfTgt.Distance( tgtPnts[i] ) < tol );
871         }
872         if ( !trsfIsOK )
873           return false;
874       }
875     } // "Find transformation" block
876
877     // 2) Projection
878
879     SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
880
881     SMESH_MesherHelper helper( *tgtMesh );
882     helper.SetSubShape( tgtFace );
883     if ( is1DComputed )
884       helper.IsQuadraticSubMesh( tgtFace );
885     else
886       helper.SetIsQuadratic( srcSubDS->GetElements()->next()->IsQuadratic() );
887     helper.SetElementsOnShape( true );
888     Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
889     SMESHDS_Mesh* tgtMeshDS = tgtMesh->GetMeshDS();
890
891     SMESH_MesherHelper srcHelper( *srcMesh );
892     srcHelper.SetSubShape( srcFace );
893
894     const SMDS_MeshNode* nullNode = 0;
895     TAssocTool::TNodeNodeMap::iterator srcN_tgtN;
896
897     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
898     vector< const SMDS_MeshNode* > tgtNodes;
899     bool uvOK;
900     while ( elemIt->more() ) // loop on all mesh faces on srcFace
901     {
902       const SMDS_MeshElement* elem = elemIt->next();
903       const int nbN = elem->NbCornerNodes(); 
904       tgtNodes.resize( nbN );
905       for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
906       {
907         const SMDS_MeshNode* srcNode = elem->GetNode(i);
908         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
909         if ( srcN_tgtN->second == nullNode )
910         {
911           // create a new node
912           gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
913                                                 elem->GetNode( helper.WrapIndex(i+1,nbN)), &uvOK);
914           gp_Pnt2d   tgtUV = trsf.Transform( srcUV );
915           gp_Pnt      tgtP = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
916           SMDS_MeshNode* n = tgtMeshDS->AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
917           switch ( srcNode->GetPosition()->GetTypeOfPosition() )
918           {
919           case SMDS_TOP_FACE: {
920             tgtMeshDS->SetNodeOnFace( n, helper.GetSubShapeID(), tgtUV.X(), tgtUV.Y() );
921             break;
922           }
923           case SMDS_TOP_EDGE: {
924             TopoDS_Shape srcEdge = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
925             TopoDS_Edge  tgtEdge = TopoDS::Edge( shape2ShapeMap( srcEdge, /*isSrc=*/true ));
926             double U = Precision::Infinite();
927             helper.CheckNodeU( tgtEdge, n, U, Precision::PConfusion());
928             tgtMeshDS->SetNodeOnEdge( n, TopoDS::Edge( tgtEdge ), U );
929             break;
930           }
931           case SMDS_TOP_VERTEX: {
932             TopoDS_Shape srcV = srcHelper.GetSubShapeByNode( srcNode, srcHelper.GetMeshDS() );
933             TopoDS_Shape tgtV = shape2ShapeMap( srcV, /*isSrc=*/true );
934             tgtMeshDS->SetNodeOnVertex( n, TopoDS::Vertex( tgtV ));
935             break;
936           }
937           }
938           srcN_tgtN->second = n;
939         }
940         tgtNodes[i] = srcN_tgtN->second;
941       }
942       // create a new face (with reversed orientation)
943       switch ( nbN )
944       {
945       case 3: helper.AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
946       case 4: helper.AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
947       }
948     }
949     return true;
950
951   } // bool projectBy2DSimilarity(...)
952
953   //================================================================================
954   /*!
955    * \brief Fix bad faces by smoothing
956    */
957   //================================================================================
958
959   bool fixDistortedFaces( SMESH_MesherHelper& helper,
960                           TSideVector&        tgtWires )
961   {
962     SMESH_subMesh* faceSM = helper.GetMesh()->GetSubMesh( helper.GetSubShape() );
963
964     if ( helper.IsDistorted2D( faceSM, /*checkUV=*/false ))
965     {
966       SMESH_MeshEditor editor( helper.GetMesh() );
967       SMESHDS_SubMesh* smDS = faceSM->GetSubMeshDS();
968       const TopoDS_Face&  F = TopoDS::Face( faceSM->GetSubShape() );
969
970       TIDSortedElemSet faces;
971       SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
972       for ( faceIt = smDS->GetElements(); faceIt->more(); )
973         faces.insert( faces.end(), faceIt->next() );
974
975       // choose smoothing algo
976       //SMESH_MeshEditor:: SmoothMethod algo = SMESH_MeshEditor::CENTROIDAL;
977       bool isConcaveBoundary = false;
978       for ( size_t iW = 0; iW < tgtWires.size() && !isConcaveBoundary; ++iW )
979       {
980         TopoDS_Edge prevEdge = tgtWires[iW]->Edge( tgtWires[iW]->NbEdges() - 1 );
981         for ( int iE = 0; iE < tgtWires[iW]->NbEdges() && !isConcaveBoundary; ++iE )
982         {
983           double angle = helper.GetAngle( prevEdge, tgtWires[iW]->Edge( iE ),
984                                           F,        tgtWires[iW]->FirstVertex( iE ));
985           isConcaveBoundary = ( angle < -5. * M_PI / 180. );
986
987           prevEdge = tgtWires[iW]->Edge( iE );
988         }
989       }
990       SMESH_MeshEditor:: SmoothMethod algo =
991         isConcaveBoundary ? SMESH_MeshEditor::CENTROIDAL : SMESH_MeshEditor::LAPLACIAN;
992
993       // smooth in 2D or 3D?
994       TopLoc_Location loc;
995       Handle(Geom_Surface) surface = BRep_Tool::Surface( F, loc );
996       bool isPlanar = GeomLib_IsPlanarSurface( surface ).IsPlanar();
997
998       // smoothing
999       set<const SMDS_MeshNode*> fixedNodes;
1000       editor.Smooth( faces, fixedNodes, algo, /*nbIterations=*/ 10,
1001                      /*theTgtAspectRatio=*/1.0, /*the2D=*/!isPlanar);
1002
1003       helper.ToFixNodeParameters( true );
1004
1005       return !helper.IsDistorted2D( faceSM, /*checkUV=*/true );
1006     }
1007     return true;
1008   }
1009
1010 } // namespace
1011
1012
1013 //=======================================================================
1014 //function : Compute
1015 //purpose  :
1016 //=======================================================================
1017
1018 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
1019 {
1020   _src2tgtNodes.clear();
1021
1022   MESSAGE("Projection_2D Compute");
1023   if ( !_sourceHypo )
1024     return false;
1025
1026   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1027   SMESH_Mesh * tgtMesh = & theMesh;
1028   if ( !srcMesh )
1029     srcMesh = tgtMesh;
1030
1031   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1032   SMESH_MesherHelper helper( theMesh );
1033
1034   // ---------------------------
1035   // Make sub-shapes association
1036   // ---------------------------
1037
1038   TopoDS_Face   tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1039   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1040
1041   TAssocTool::TShapeShapeMap shape2ShapeMap;
1042   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1043   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1044                                              shape2ShapeMap)  ||
1045        !shape2ShapeMap.IsBound( tgtFace ))
1046   {
1047     if ( srcShape.ShapeType() == TopAbs_FACE )
1048     {
1049       int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1050       int nbE2 = helper.Count( srcShape, TopAbs_EDGE, /*ignoreSame=*/true );
1051       if ( nbE1 != nbE2 )
1052         return error(COMPERR_BAD_SHAPE,
1053                      SMESH_Comment("Different number of edges in source and target faces: ")
1054                      << nbE2 << " and " << nbE1 );
1055     }
1056     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1057   }
1058   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1059
1060   // orient faces
1061   // if ( srcMesh == tgtMesh )
1062   // {
1063   //   TopoDS_Shape solid =
1064   //     helper.GetCommonAncestor( srcFace, tgtFace, *tgtMesh, TopAbs_SOLID );
1065   //   if ( !solid.IsNull() )
1066   //   {
1067   //     srcFace.Orientation( helper.GetSubShapeOri( solid, srcFace ));
1068   //     tgtFace.Orientation( helper.GetSubShapeOri( solid, tgtFace ));
1069   //   }
1070   //   else if ( helper.NbAncestors( srcFace, *tgtMesh, TopAbs_SOLID ) == 1 &&
1071   //             helper.NbAncestors( tgtFace, *tgtMesh, TopAbs_SOLID ) == 1 )
1072   //   {
1073   //     srcFace.Orientation( helper.GetSubShapeOri( tgtMesh->GetShapeToMesh(), srcFace ));
1074   //     tgtFace.Orientation( helper.GetSubShapeOri( tgtMesh->GetShapeToMesh(), tgtFace ));
1075   //   }
1076   // }
1077   // ----------------------------------------------
1078   // Assure that mesh on a source Face is computed
1079   // ----------------------------------------------
1080
1081   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1082   SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
1083
1084   string srcMeshError;
1085   if ( tgtMesh == srcMesh ) {
1086     if ( !TAssocTool::MakeComputed( srcSubMesh ))
1087       srcMeshError = TAssocTool::SourceNotComputedError( srcSubMesh, this );
1088   }
1089   else {
1090     if ( !srcSubMesh->IsMeshComputed() )
1091       srcMeshError = TAssocTool::SourceNotComputedError();
1092   }
1093   if ( !srcMeshError.empty() )
1094     return error(COMPERR_BAD_INPUT_MESH, srcMeshError );
1095
1096   // ===========
1097   // Projection
1098   // ===========
1099
1100   // get ordered src and tgt EDGEs
1101   TSideVector srcWires, tgtWires;
1102   bool is1DComputed = false; // if any tgt EDGE is meshed
1103   TError err = getWires( tgtFace, srcFace, tgtMesh, srcMesh,
1104                          shape2ShapeMap, srcWires, tgtWires, _src2tgtNodes, is1DComputed );
1105   if ( err && !err->IsOK() )
1106     return error( err );
1107
1108   bool projDone = false;
1109
1110   if ( !projDone )
1111   {
1112     // try to project from the same face with different location
1113     projDone = projectPartner( tgtFace, srcFace, tgtWires, srcWires,
1114                                shape2ShapeMap, _src2tgtNodes, is1DComputed );
1115   }
1116   if ( !projDone )
1117   {
1118     // projection in case if the faces are similar in 2D space
1119     projDone = projectBy2DSimilarity( tgtFace, srcFace, tgtWires, srcWires,
1120                                       shape2ShapeMap, _src2tgtNodes, is1DComputed);
1121   }
1122
1123   helper.SetSubShape( tgtFace );
1124
1125   // it will remove mesh built on edges and vertices in failure case
1126   MeshCleaner cleaner( tgtSubMesh );
1127
1128   if ( !projDone )
1129   {
1130     _src2tgtNodes.clear();
1131     // --------------------
1132     // Prepare to mapping 
1133     // --------------------
1134
1135     // Check if node projection to a face is needed
1136     Bnd_B2d uvBox;
1137     SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
1138     set< const SMDS_MeshNode* > faceNodes;
1139     for ( ; faceNodes.size() < 3 && faceIt->more();  ) {
1140       const SMDS_MeshElement* face = faceIt->next();
1141       SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
1142       while ( nodeIt->more() ) {
1143         const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
1144         if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE &&
1145              faceNodes.insert( node ).second )
1146           uvBox.Add( helper.GetNodeUV( srcFace, node ));
1147       }
1148     }
1149     bool toProjectNodes = false;
1150     if ( faceNodes.size() == 1 )
1151       toProjectNodes = ( uvBox.IsVoid() || uvBox.CornerMin().IsEqual( gp_XY(0,0), 1e-12 ));
1152     else if ( faceNodes.size() > 1 )
1153       toProjectNodes = ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN );
1154
1155     // Find the corresponding source and target vertex
1156     // and <theReverse> flag needed to call mapper.Apply()
1157
1158     TopoDS_Vertex srcV1, tgtV1;
1159     bool reverse = false;
1160
1161     TopExp_Explorer vSrcExp( srcFace, TopAbs_VERTEX );
1162     srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1163     tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1164
1165     list< TopoDS_Edge > tgtEdges, srcEdges;
1166     list< int > nbEdgesInWires;
1167     SMESH_Block::GetOrderedEdges( tgtFace, tgtEdges, nbEdgesInWires, tgtV1 );
1168     SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1169
1170     if ( nbEdgesInWires.front() > 1 ) // possible to find out orientation
1171     {
1172       TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
1173       TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
1174       reverse = ( ! srcE1.IsSame( srcE1bis ));
1175       if ( reverse &&
1176            //_sourceHypo->HasVertexAssociation() &&
1177            nbEdgesInWires.front() > 2 &&
1178            helper.IsRealSeam( tgtEdges.front() ))
1179       {
1180         // projection to a face with seam EDGE; pb is that GetOrderedEdges()
1181         // always puts a seam EDGE first (if possible) and as a result
1182         // we can't use only theReverse flag to correctly associate source
1183         // and target faces in the mapper. Thus we select srcV1 so that
1184         // GetOrderedEdges() to return EDGEs in a needed order
1185         TopoDS_Face tgtFaceBis = tgtFace;
1186         for ( vSrcExp.Next(); vSrcExp.More(); )
1187         {
1188           tgtFaceBis.Reverse();
1189           tgtEdges.clear();
1190           SMESH_Block::GetOrderedEdges( tgtFaceBis, tgtEdges, nbEdgesInWires, tgtV1 );
1191           bool ok = true;
1192           list< TopoDS_Edge >::iterator edgeS = srcEdges.begin(), edgeT = tgtEdges.begin();
1193           for ( ; edgeS != srcEdges.end() && ok ; ++edgeS, ++edgeT )
1194             ok = edgeS->IsSame( shape2ShapeMap( *edgeT ));
1195           if ( ok )
1196             break; // FOUND!
1197
1198           reverse = !reverse;
1199           if ( reverse )
1200           {
1201             vSrcExp.Next();
1202           }
1203           else
1204           {
1205             srcV1 = TopoDS::Vertex( vSrcExp.Current() );
1206             tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1, /*isSrc=*/true ));
1207             srcEdges.clear();
1208             SMESH_Block::GetOrderedEdges( srcFace, srcEdges, nbEdgesInWires, srcV1 );
1209           }
1210         }
1211       }
1212     }
1213     else if ( nbEdgesInWires.front() == 1 )
1214     {
1215       // TODO::Compare orientation of curves in a sole edge
1216       //RETURN_BAD_RESULT("Not implemented case");
1217     }
1218     else
1219     {
1220       RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
1221     }
1222
1223     // Load pattern from the source face
1224     SMESH_Pattern mapper;
1225     mapper.Load( srcMesh, srcFace, toProjectNodes, srcV1 );
1226     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1227       return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
1228
1229     // --------------------
1230     // Perform 2D mapping
1231     // --------------------
1232
1233     // Compute mesh on a target face
1234
1235     mapper.Apply( tgtFace, tgtV1, reverse );
1236     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK ) {
1237       // std::ofstream file("/tmp/Pattern.smp" );
1238       // mapper.Save( file );
1239       return error("Can't apply source mesh pattern to the face");
1240     }
1241
1242     // Create the mesh
1243
1244     const bool toCreatePolygons = false, toCreatePolyedrs = false;
1245     mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
1246     if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
1247       return error("Can't make mesh by source mesh pattern");
1248
1249     // -------------------------------------------------------------------------
1250     // mapper doesn't take care of nodes already existing on edges and vertices,
1251     // so we must merge nodes created by it with existing ones
1252     // -------------------------------------------------------------------------
1253
1254     SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
1255
1256     // Make groups of nodes to merge
1257
1258     // loop on EDGE and VERTEX sub-meshes of a target FACE
1259     SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(/*includeSelf=*/false,
1260                                                                      /*complexShapeFirst=*/false);
1261     while ( smIt->more() )
1262     {
1263       SMESH_subMesh*     sm = smIt->next();
1264       SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
1265       if ( !smDS || smDS->NbNodes() == 0 )
1266         continue;
1267       //if ( !is1DComputed && sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1268       //  break;
1269
1270       if ( helper.IsDegenShape( sm->GetId() ) ) // to merge all nodes on degenerated
1271       {
1272         if ( sm->GetSubShape().ShapeType() == TopAbs_EDGE )
1273         {
1274           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1275           SMESH_subMeshIteratorPtr smDegenIt
1276             = sm->getDependsOnIterator(/*includeSelf=*/true,/*complexShapeFirst=*/false);
1277           while ( smDegenIt->more() )
1278             if (( smDS = smDegenIt->next()->GetSubMeshDS() ))
1279             {
1280               SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1281               while ( nIt->more() )
1282                 groupsOfNodes.back().push_back( nIt->next() );
1283             }
1284         }
1285         continue; // do not treat sm of degen VERTEX
1286       }
1287
1288       // Sort new and old nodes of a submesh separately
1289
1290       bool isSeam = helper.IsRealSeam( sm->GetId() );
1291
1292       enum { NEW_NODES = 0, OLD_NODES };
1293       map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
1294       map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
1295       set< const SMDS_MeshNode* > seamNodes;
1296
1297       // mapper changed, no more "mapper puts on a seam edge nodes from 2 edges"
1298       if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
1299         ;//RETURN_BAD_RESULT("getBoundaryNodes() failed");
1300
1301       SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
1302       while ( nIt->more() )
1303       {
1304         const SMDS_MeshNode* node = nIt->next();
1305         bool isOld = isOldNode( node );
1306
1307         if ( !isOld && isSeam ) { // new node on a seam edge
1308           if ( seamNodes.count( node ) )
1309             continue; // node is already in the map
1310         }
1311
1312         // sort nodes on edges by their position
1313         map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
1314         switch ( node->GetPosition()->GetTypeOfPosition() )
1315         {
1316         case  SMDS_TOP_VERTEX: {
1317           if ( !is1DComputed && !pos2nodes.empty() )
1318             u2nodesMaps[isOld ? NEW_NODES : OLD_NODES].insert( make_pair( 0, node ));
1319           else
1320             pos2nodes.insert( make_pair( 0, node ));
1321           break;
1322         }
1323         case  SMDS_TOP_EDGE:   {
1324           const SMDS_EdgePosition* pos =
1325             static_cast<const SMDS_EdgePosition*>(node->GetPosition());
1326           pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
1327           break;
1328         }
1329         default:
1330           RETURN_BAD_RESULT("Wrong node position type: "<<
1331                             node->GetPosition()->GetTypeOfPosition());
1332         }
1333       }
1334       const bool mergeNewToOld =
1335         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesMaps[ OLD_NODES ].size() );
1336       const bool mergeSeamToNew =
1337         ( u2nodesMaps[ NEW_NODES ].size() == u2nodesOnSeam.size() );
1338
1339       if ( !mergeNewToOld )
1340         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1341              u2nodesMaps[ OLD_NODES ].size() > 0 )
1342         {
1343           u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1344           newEnd    = u2nodesMaps[ OLD_NODES ].end();
1345           for ( ; u_oldNode != newEnd; ++u_oldNode )
1346             SMESH_Algo::addBadInputElement( u_oldNode->second );
1347           return error( COMPERR_BAD_INPUT_MESH,
1348                         SMESH_Comment( "Existing mesh mismatches the projected 2D mesh on " )
1349                         << ( sm->GetSubShape().ShapeType() == TopAbs_EDGE ? "edge" : "vertex" )
1350                         << " #" << sm->GetId() );
1351         }
1352       if ( isSeam && !mergeSeamToNew ) {
1353         const TopoDS_Shape& seam = sm->GetSubShape();
1354         if ( u2nodesMaps[ NEW_NODES ].size() > 0 &&
1355              u2nodesOnSeam.size()            > 0 &&
1356              seam.ShapeType() == TopAbs_EDGE )
1357         {
1358           int nbE1 = helper.Count( tgtFace, TopAbs_EDGE, /*ignoreSame=*/true );
1359           int nbE2 = helper.Count( srcFace, TopAbs_EDGE, /*ignoreSame=*/true );
1360           if ( nbE1 != nbE2 ) // 2 EDGEs are mapped to a seam EDGE
1361           {
1362             // find the 2 EDGEs of srcFace
1363             TopTools_DataMapIteratorOfDataMapOfShapeShape src2tgtIt( shape2ShapeMap._map2to1 );
1364             for ( ; src2tgtIt.More(); src2tgtIt.Next() )
1365               if ( seam.IsSame( src2tgtIt.Value() ))
1366                 SMESH_Algo::addBadInputElements
1367                   ( srcMesh->GetMeshDS()->MeshElements( src2tgtIt.Key() ));
1368             return error( COMPERR_BAD_INPUT_MESH,
1369                           "Different number of nodes on two edges projected to a seam edge" );
1370           }
1371         }
1372       }
1373
1374       // Make groups of nodes to merge
1375
1376       u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
1377       u_newNode = u2nodesMaps[ NEW_NODES ].begin();
1378       newEnd    = u2nodesMaps[ NEW_NODES ].end();
1379       u_newOnSeam = u2nodesOnSeam.begin();
1380       if ( mergeNewToOld )
1381         for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode )
1382         {
1383           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1384           groupsOfNodes.back().push_back( u_oldNode->second );
1385           groupsOfNodes.back().push_back( u_newNode->second );
1386           if ( mergeSeamToNew )
1387             groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
1388         }
1389       else if ( mergeSeamToNew )
1390         for ( ; u_newNode != newEnd; ++u_newNode, ++u_newOnSeam )
1391         {
1392           groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
1393           groupsOfNodes.back().push_back( u_newNode->second );
1394           groupsOfNodes.back().push_back( u_newOnSeam->second );
1395         }
1396
1397     } // loop on EDGE and VERTEX submeshes of a target FACE
1398
1399     // Merge
1400
1401     SMESH_MeshEditor editor( tgtMesh );
1402     int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1403     editor.MergeNodes( groupsOfNodes );
1404     int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
1405     if ( nbFaceBeforeMerge != nbFaceAtferMerge && !helper.HasDegeneratedEdges() )
1406       return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
1407
1408     // ----------------------------------------------------------------
1409     // The mapper can't create quadratic elements, so convert if needed
1410     // ----------------------------------------------------------------
1411
1412     faceIt         = srcSubMesh->GetSubMeshDS()->GetElements();
1413     bool srcIsQuad = faceIt->next()->IsQuadratic();
1414     faceIt         = tgtSubMesh->GetSubMeshDS()->GetElements();
1415     bool tgtIsQuad = faceIt->next()->IsQuadratic();
1416     if ( srcIsQuad && !tgtIsQuad )
1417     {
1418       TIDSortedElemSet tgtFaces;
1419       faceIt = tgtSubMesh->GetSubMeshDS()->GetElements();
1420       while ( faceIt->more() )
1421         tgtFaces.insert( tgtFaces.end(), faceIt->next() );
1422
1423       editor.ConvertToQuadratic(/*theForce3d=*/false, tgtFaces, false);
1424     }
1425
1426   } // end of projection using Pattern mapping
1427
1428
1429   if ( !projDone || is1DComputed )
1430     // ----------------------------------------------------------------
1431     // The mapper can create distorted faces by placing nodes out of the FACE
1432     // boundary, also bad face can be created if EDGEs already discretized
1433     // --> fix bad faces by smoothing
1434     // ----------------------------------------------------------------
1435     if ( !fixDistortedFaces( helper, tgtWires ))
1436       return error("Invalid mesh generated");
1437
1438   // ---------------------------
1439   // Check elements orientation
1440   // ---------------------------
1441
1442   TopoDS_Face face = TopoDS::Face( theShape );
1443   if ( !theMesh.IsMainShape( tgtFace ))
1444   {
1445     // find the main shape
1446     TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1447     switch ( mainShape.ShapeType() ) {
1448     case TopAbs_SHELL:
1449     case TopAbs_SOLID: break;
1450     default:
1451       TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
1452       for ( ; ancestIt.More(); ancestIt.Next() ) {
1453         TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
1454         if ( type == TopAbs_SOLID ) {
1455           mainShape = ancestIt.Value();
1456           break;
1457         } else if ( type == TopAbs_SHELL ) {
1458           mainShape = ancestIt.Value();
1459         }
1460       }
1461     }
1462     // find tgtFace in the main solid or shell to know it's true orientation.
1463     TopExp_Explorer exp( mainShape, TopAbs_FACE );
1464     for ( ; exp.More(); exp.Next() ) {
1465       if ( tgtFace.IsSame( exp.Current() )) {
1466         face = TopoDS::Face( exp.Current() );
1467         break;
1468       }
1469     }
1470   }
1471   // Fix orientation
1472   if ( helper.IsReversedSubMesh( face ))
1473   {
1474     SMESH_MeshEditor editor( tgtMesh );
1475     SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1476     while ( eIt->more() ) {
1477       const SMDS_MeshElement* e = eIt->next();
1478       if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1479         RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1480     }
1481   }
1482
1483   cleaner.Release(); // not to remove mesh
1484
1485   return true;
1486 }
1487
1488
1489 //=======================================================================
1490 //function : Evaluate
1491 //purpose  : 
1492 //=======================================================================
1493
1494 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh&         theMesh,
1495                                         const TopoDS_Shape& theShape,
1496                                         MapShapeNbElems&    aResMap)
1497 {
1498   if ( !_sourceHypo )
1499     return false;
1500
1501   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1502   SMESH_Mesh * tgtMesh = & theMesh;
1503   if ( !srcMesh )
1504     srcMesh = tgtMesh;
1505
1506   // ---------------------------
1507   // Make sub-shapes association
1508   // ---------------------------
1509
1510   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1511   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1512
1513   TAssocTool::TShapeShapeMap shape2ShapeMap;
1514   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
1515   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1516                                              shape2ShapeMap)  ||
1517        !shape2ShapeMap.IsBound( tgtFace ))
1518     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1519
1520   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1521
1522   // -------------------------------------------------------
1523   // Assure that mesh on a source Face is computed/evaluated
1524   // -------------------------------------------------------
1525
1526   std::vector<int> aVec;
1527
1528   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1529   if ( srcSubMesh->IsMeshComputed() )
1530   {
1531     aVec.resize( SMDSEntity_Last, 0 );
1532     aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
1533
1534     SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
1535     while ( elemIt->more() )
1536       aVec[ elemIt->next()->GetEntityType() ]++;
1537   }
1538   else
1539   {
1540     MapShapeNbElems  tmpResMap;
1541     MapShapeNbElems& srcResMap = (srcMesh == tgtMesh) ? aResMap : tmpResMap;
1542     if ( !_gen->Evaluate( *srcMesh, srcShape, srcResMap ))
1543       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
1544     aVec = srcResMap[ srcSubMesh ];
1545     if ( aVec.empty() )
1546       return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
1547   }
1548
1549   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1550   aResMap.insert(std::make_pair(sm,aVec));
1551
1552   return true;
1553 }
1554
1555
1556 //=============================================================================
1557 /*!
1558  * \brief Sets a default event listener to submesh of the source face
1559   * \param subMesh - submesh where algo is set
1560  *
1561  * This method is called when a submesh gets HYP_OK algo_state.
1562  * After being set, event listener is notified on each event of a submesh.
1563  * Arranges that CLEAN event is translated from source submesh to
1564  * the submesh
1565  */
1566 //=============================================================================
1567
1568 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
1569 {
1570   TAssocTool::SetEventListener( subMesh,
1571                                 _sourceHypo->GetSourceFace(),
1572                                 _sourceHypo->GetSourceMesh() );
1573 }