Salome HOME
fa4b13639e741badaa1804b465692c73f198f060
[modules/smesh.git] / src / StdMeshers / StdMeshers_ViscousLayers.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File      : StdMeshers_ViscousLayers.cxx
21 // Created   : Wed Dec  1 15:15:34 2010
22 // Author    : Edward AGAPOV (eap)
23
24 #include "StdMeshers_ViscousLayers.hxx"
25
26 #include "SMDS_EdgePosition.hxx"
27 #include "SMDS_FaceOfNodes.hxx"
28 #include "SMDS_FacePosition.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Group.hxx"
32 #include "SMESHDS_Hypothesis.hxx"
33 #include "SMESH_Algo.hxx"
34 #include "SMESH_ComputeError.hxx"
35 #include "SMESH_ControlsDef.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MeshAlgos.hxx"
41 #include "SMESH_MesherHelper.hxx"
42 #include "SMESH_ProxyMesh.hxx"
43 #include "SMESH_subMesh.hxx"
44 #include "SMESH_subMeshEventListener.hxx"
45 #include "StdMeshers_FaceSide.hxx"
46
47 #include <BRepAdaptor_Curve2d.hxx>
48 #include <BRepAdaptor_Surface.hxx>
49 #include <BRepLProp_SLProps.hxx>
50 #include <BRep_Tool.hxx>
51 #include <Bnd_B2d.hxx>
52 #include <Bnd_B3d.hxx>
53 #include <ElCLib.hxx>
54 #include <GCPnts_AbscissaPoint.hxx>
55 #include <Geom2d_Circle.hxx>
56 #include <Geom2d_Line.hxx>
57 #include <Geom2d_TrimmedCurve.hxx>
58 #include <GeomAdaptor_Curve.hxx>
59 #include <GeomLib.hxx>
60 #include <Geom_Circle.hxx>
61 #include <Geom_Curve.hxx>
62 #include <Geom_Line.hxx>
63 #include <Geom_TrimmedCurve.hxx>
64 #include <Precision.hxx>
65 #include <Standard_ErrorHandler.hxx>
66 #include <Standard_Failure.hxx>
67 #include <TColStd_Array1OfReal.hxx>
68 #include <TopExp.hxx>
69 #include <TopExp_Explorer.hxx>
70 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <TopTools_ListOfShape.hxx>
72 #include <TopTools_MapOfShape.hxx>
73 #include <TopoDS.hxx>
74 #include <TopoDS_Edge.hxx>
75 #include <TopoDS_Face.hxx>
76 #include <TopoDS_Vertex.hxx>
77 #include <gp_Ax1.hxx>
78 #include <gp_Vec.hxx>
79 #include <gp_XY.hxx>
80
81 #include <list>
82 #include <string>
83 #include <cmath>
84 #include <limits>
85
86 //#define __myDEBUG
87
88 using namespace std;
89
90 //================================================================================
91 namespace VISCOUS_3D
92 {
93   typedef int TGeomID;
94
95   enum UIndex { U_TGT = 1, U_SRC, LEN_TGT };
96
97   const double theMinSmoothCosin = 0.1;
98
99   /*!
100    * \brief SMESH_ProxyMesh computed by _ViscousBuilder for a SOLID.
101    * It is stored in a SMESH_subMesh of the SOLID as SMESH_subMeshEventListenerData
102    */
103   struct _MeshOfSolid : public SMESH_ProxyMesh,
104                         public SMESH_subMeshEventListenerData
105   {
106     bool _n2nMapComputed;
107
108     _MeshOfSolid( SMESH_Mesh* mesh)
109       :SMESH_subMeshEventListenerData( /*isDeletable=*/true),_n2nMapComputed(false)
110     {
111       SMESH_ProxyMesh::setMesh( *mesh );
112     }
113
114     // returns submesh for a geom face
115     SMESH_ProxyMesh::SubMesh* getFaceSubM(const TopoDS_Face& F, bool create=false)
116     {
117       TGeomID i = SMESH_ProxyMesh::shapeIndex(F);
118       return create ? SMESH_ProxyMesh::getProxySubMesh(i) : findProxySubMesh(i);
119     }
120     void setNode2Node(const SMDS_MeshNode*                 srcNode,
121                       const SMDS_MeshNode*                 proxyNode,
122                       const SMESH_ProxyMesh::SubMesh* subMesh)
123     {
124       SMESH_ProxyMesh::setNode2Node( srcNode,proxyNode,subMesh);
125     }
126   };
127   //--------------------------------------------------------------------------------
128   /*!
129    * \brief Listener of events of 3D sub-meshes computed with viscous layers.
130    * It is used to clear an inferior dim sub-meshes modified by viscous layers
131    */
132   class _ShrinkShapeListener : SMESH_subMeshEventListener
133   {
134     _ShrinkShapeListener()
135       : SMESH_subMeshEventListener(/*isDeletable=*/false,
136                                    "StdMeshers_ViscousLayers::_ShrinkShapeListener") {}
137   public:
138     static SMESH_subMeshEventListener* Get() { static _ShrinkShapeListener l; return &l; }
139     virtual void ProcessEvent(const int                       event,
140                               const int                       eventType,
141                               SMESH_subMesh*                  solidSM,
142                               SMESH_subMeshEventListenerData* data,
143                               const SMESH_Hypothesis*         hyp)
144     {
145       if ( SMESH_subMesh::COMPUTE_EVENT == eventType && solidSM->IsEmpty() && data )
146       {
147         SMESH_subMeshEventListener::ProcessEvent(event,eventType,solidSM,data,hyp);
148       }
149     }
150   };
151   //--------------------------------------------------------------------------------
152   /*!
153    * \brief Listener of events of 3D sub-meshes computed with viscous layers.
154    * It is used to store data computed by _ViscousBuilder for a sub-mesh and to
155    * delete the data as soon as it has been used
156    */
157   class _ViscousListener : SMESH_subMeshEventListener
158   {
159     _ViscousListener():
160       SMESH_subMeshEventListener(/*isDeletable=*/false,
161                                  "StdMeshers_ViscousLayers::_ViscousListener") {}
162     static SMESH_subMeshEventListener* Get() { static _ViscousListener l; return &l; }
163   public:
164     virtual void ProcessEvent(const int                       event,
165                               const int                       eventType,
166                               SMESH_subMesh*                  subMesh,
167                               SMESH_subMeshEventListenerData* data,
168                               const SMESH_Hypothesis*         hyp)
169     {
170       if ( SMESH_subMesh::COMPUTE_EVENT == eventType )
171       {
172         // delete SMESH_ProxyMesh containing temporary faces
173         subMesh->DeleteEventListener( this );
174       }
175     }
176     // Finds or creates proxy mesh of the solid
177     static _MeshOfSolid* GetSolidMesh(SMESH_Mesh*         mesh,
178                                       const TopoDS_Shape& solid,
179                                       bool                toCreate=false)
180     {
181       if ( !mesh ) return 0;
182       SMESH_subMesh* sm = mesh->GetSubMesh(solid);
183       _MeshOfSolid* data = (_MeshOfSolid*) sm->GetEventListenerData( Get() );
184       if ( !data && toCreate )
185       {
186         data = new _MeshOfSolid(mesh);
187         data->mySubMeshes.push_back( sm ); // to find SOLID by _MeshOfSolid
188         sm->SetEventListener( Get(), data, sm );
189       }
190       return data;
191     }
192     // Removes proxy mesh of the solid
193     static void RemoveSolidMesh(SMESH_Mesh* mesh, const TopoDS_Shape& solid)
194     {
195       mesh->GetSubMesh(solid)->DeleteEventListener( _ViscousListener::Get() );
196     }
197   };
198   
199   //================================================================================
200   /*!
201    * \brief sets a sub-mesh event listener to clear sub-meshes of sub-shapes of
202    * the main shape when sub-mesh of the main shape is cleared,
203    * for example to clear sub-meshes of FACEs when sub-mesh of a SOLID
204    * is cleared
205    */
206   //================================================================================
207
208   void ToClearSubWithMain( SMESH_subMesh* sub, const TopoDS_Shape& main)
209   {
210     SMESH_subMesh* mainSM = sub->GetFather()->GetSubMesh( main );
211     SMESH_subMeshEventListenerData* data =
212       mainSM->GetEventListenerData( _ShrinkShapeListener::Get());
213     if ( data )
214     {
215       if ( find( data->mySubMeshes.begin(), data->mySubMeshes.end(), sub ) ==
216            data->mySubMeshes.end())
217         data->mySubMeshes.push_back( sub );
218     }
219     else
220     {
221       data = SMESH_subMeshEventListenerData::MakeData( /*dependent=*/sub );
222       sub->SetEventListener( _ShrinkShapeListener::Get(), data, /*whereToListenTo=*/mainSM );
223     }
224   }
225   //--------------------------------------------------------------------------------
226   /*!
227    * \brief Simplex (triangle or tetrahedron) based on 1 (tria) or 2 (tet) nodes of
228    * _LayerEdge and 2 nodes of the mesh surface beening smoothed.
229    * The class is used to check validity of face or volumes around a smoothed node;
230    * it stores only 2 nodes as the other nodes are stored by _LayerEdge.
231    */
232   struct _Simplex
233   {
234     const SMDS_MeshNode *_nPrev, *_nNext; // nodes on a smoothed mesh surface
235     const SMDS_MeshNode *_nOpp; // in 2D case, a node opposite to a smoothed node in QUAD
236     _Simplex(const SMDS_MeshNode* nPrev=0,
237              const SMDS_MeshNode* nNext=0,
238              const SMDS_MeshNode* nOpp=0)
239       : _nPrev(nPrev), _nNext(nNext), _nOpp(nOpp) {}
240     bool IsForward(const SMDS_MeshNode* nSrc, const gp_XYZ* pntTgt) const
241     {
242       const double M[3][3] =
243         {{ _nNext->X() - nSrc->X(), _nNext->Y() - nSrc->Y(), _nNext->Z() - nSrc->Z() },
244          { pntTgt->X() - nSrc->X(), pntTgt->Y() - nSrc->Y(), pntTgt->Z() - nSrc->Z() },
245          { _nPrev->X() - nSrc->X(), _nPrev->Y() - nSrc->Y(), _nPrev->Z() - nSrc->Z() }};
246       double determinant = ( + M[0][0]*M[1][1]*M[2][2]
247                              + M[0][1]*M[1][2]*M[2][0]
248                              + M[0][2]*M[1][0]*M[2][1]
249                              - M[0][0]*M[1][2]*M[2][1]
250                              - M[0][1]*M[1][0]*M[2][2]
251                              - M[0][2]*M[1][1]*M[2][0]);
252       return determinant > 1e-100;
253     }
254     bool IsForward(const gp_XY&         tgtUV,
255                    const SMDS_MeshNode* smoothedNode,
256                    const TopoDS_Face&   face,
257                    SMESH_MesherHelper&  helper,
258                    const double         refSign) const
259     {
260       gp_XY prevUV = helper.GetNodeUV( face, _nPrev, smoothedNode );
261       gp_XY nextUV = helper.GetNodeUV( face, _nNext, smoothedNode );
262       gp_Vec2d v1( tgtUV, prevUV ), v2( tgtUV, nextUV );
263       double d = v1 ^ v2;
264       return d*refSign > 1e-100;
265     }
266     bool IsNeighbour(const _Simplex& other) const
267     {
268       return _nPrev == other._nNext || _nNext == other._nPrev;
269     }
270   };
271   //--------------------------------------------------------------------------------
272   /*!
273    * Structure used to take into account surface curvature while smoothing
274    */
275   struct _Curvature
276   {
277     double _r; // radius
278     double _k; // factor to correct node smoothed position
279     double _h2lenRatio; // avgNormProj / (2*avgDist)
280   public:
281     static _Curvature* New( double avgNormProj, double avgDist )
282     {
283       _Curvature* c = 0;
284       if ( fabs( avgNormProj / avgDist ) > 1./200 )
285       {
286         c = new _Curvature;
287         c->_r = avgDist * avgDist / avgNormProj;
288         c->_k = avgDist * avgDist / c->_r / c->_r;
289         //c->_k = avgNormProj / c->_r;
290         c->_k *= ( c->_r < 0 ? 1/1.1 : 1.1 ); // not to be too restrictive
291         c->_h2lenRatio = avgNormProj / ( avgDist + avgDist );
292       }
293       return c;
294     }
295     double lenDelta(double len) const { return _k * ( _r + len ); }
296     double lenDeltaByDist(double dist) const { return dist * _h2lenRatio; }
297   };
298   struct _2NearEdges;
299   //--------------------------------------------------------------------------------
300   /*!
301    * \brief Edge normal to surface, connecting a node on solid surface (_nodes[0])
302    * and a node of the most internal layer (_nodes.back())
303    */
304   struct _LayerEdge
305   {
306     vector< const SMDS_MeshNode*> _nodes;
307
308     gp_XYZ              _normal; // to solid surface
309     vector<gp_XYZ>      _pos; // points computed during inflation
310     double              _len; // length achived with the last inflation step
311     double              _cosin; // of angle (_normal ^ surface)
312     double              _lenFactor; // to compute _len taking _cosin into account
313
314     // face or edge w/o layer along or near which _LayerEdge is inflated
315     TopoDS_Shape        _sWOL;
316     // simplices connected to the source node (_nodes[0]);
317     // used for smoothing and quality check of _LayerEdge's based on the FACE
318     vector<_Simplex>    _simplices;
319     // data for smoothing of _LayerEdge's based on the EDGE
320     _2NearEdges*        _2neibors;
321
322     _Curvature*         _curvature;
323     // TODO:: detele _Curvature, _plnNorm
324
325     void SetNewLength( double len, SMESH_MesherHelper& helper );
326     bool SetNewLength2d( Handle(Geom_Surface)& surface,
327                          const TopoDS_Face&    F,
328                          SMESH_MesherHelper&   helper );
329     void SetDataByNeighbors( const SMDS_MeshNode* n1,
330                              const SMDS_MeshNode* n2,
331                              SMESH_MesherHelper&  helper);
332     void InvalidateStep( int curStep, bool restoreLength=false );
333     bool Smooth(int& badNb);
334     bool SmoothOnEdge(Handle(Geom_Surface)& surface,
335                       const TopoDS_Face&    F,
336                       SMESH_MesherHelper&   helper);
337     bool FindIntersection( SMESH_ElementSearcher&   searcher,
338                            double &                 distance,
339                            const double&            epsilon,
340                            const SMDS_MeshElement** face = 0);
341     bool SegTriaInter( const gp_Ax1&        lastSegment,
342                        const SMDS_MeshNode* n0,
343                        const SMDS_MeshNode* n1,
344                        const SMDS_MeshNode* n2,
345                        double&              dist,
346                        const double&        epsilon) const;
347     gp_Ax1 LastSegment(double& segLen) const;
348     gp_XY  LastUV( const TopoDS_Face& F ) const;
349     bool   IsOnEdge() const { return _2neibors; }
350     gp_XYZ Copy( _LayerEdge& other, SMESH_MesherHelper& helper );
351     void   SetCosin( double cosin );
352   };
353   struct _LayerEdgeCmp
354   {
355     bool operator () (const _LayerEdge* e1, const _LayerEdge* e2) const
356     {
357       const bool cmpNodes = ( e1 && e2 && e1->_nodes.size() && e2->_nodes.size() );
358       return cmpNodes ? ( e1->_nodes[0]->GetID() < e2->_nodes[0]->GetID()) : ( e1 < e2 );
359     }
360   };
361   struct _LayerEdge;
362   //--------------------------------------------------------------------------------
363   /*!
364    * Structure used to smooth a _LayerEdge based on an EDGE.
365    */
366   struct _2NearEdges
367   {
368     double               _wgt  [2]; // weights of _nodes
369     _LayerEdge*          _edges[2];
370
371      // normal to plane passing through _LayerEdge._normal and tangent of EDGE
372     gp_XYZ*              _plnNorm;
373
374     _2NearEdges() { _edges[0]=_edges[1]=0; _plnNorm = 0; }
375     const SMDS_MeshNode* tgtNode(bool is2nd) {
376       return _edges[is2nd] ? _edges[is2nd]->_nodes.back() : 0;
377     }
378     const SMDS_MeshNode* srcNode(bool is2nd) {
379       return _edges[is2nd] ? _edges[is2nd]->_nodes[0] : 0;
380     }
381     void reverse() {
382       std::swap( _wgt  [0], _wgt  [1] );
383       std::swap( _edges[0], _edges[1] );
384     }
385   };
386   //--------------------------------------------------------------------------------
387   /*!
388    * \brief Convex FACE whose radius of curvature is less than the thickness of 
389    *        layers. It is used to detect distortion of prisms based on a convex
390    *        FACE and to update normals to enable further increasing the thickness
391    */
392   struct _ConvexFace
393   {
394     TopoDS_Face                     _face;
395
396     // edges whose _simplices are used to detect prism destorsion
397     vector< _LayerEdge* >           _simplexTestEdges;
398
399     // map a sub-shape to it's index in _SolidData::_endEdgeOnShape vector
400     map< TGeomID, int >             _subIdToEdgeEnd;
401
402     bool                            _normalsFixed;
403
404     bool GetCenterOfCurvature( _LayerEdge*         ledge,
405                                BRepLProp_SLProps&  surfProp,
406                                SMESH_MesherHelper& helper,
407                                gp_Pnt &            center ) const;
408     bool CheckPrisms() const;
409   };
410
411   //--------------------------------------------------------------------------------
412
413   typedef map< const SMDS_MeshNode*, _LayerEdge*, TIDCompare > TNode2Edge;
414   
415   //--------------------------------------------------------------------------------
416   /*!
417    * \brief Data of a SOLID
418    */
419   struct _SolidData
420   {
421     TopoDS_Shape                    _solid;
422     const StdMeshers_ViscousLayers* _hyp;
423     TopoDS_Shape                    _hypShape;
424     _MeshOfSolid*                   _proxyMesh;
425     set<TGeomID>                    _reversedFaceIds;
426     set<TGeomID>                    _ignoreFaceIds; // WOL FACEs and FACEs of other SOLIDS
427
428     double                          _stepSize, _stepSizeCoeff;
429     const SMDS_MeshNode*            _stepSizeNodes[2];
430
431     TNode2Edge                      _n2eMap; // nodes and _LayerEdge's based on them
432
433     // map to find _n2eMap of another _SolidData by a shrink shape shared by two _SolidData's
434     map< TGeomID, TNode2Edge* >     _s2neMap;
435     // edges of _n2eMap. We keep same data in two containers because
436     // iteration over the map is 5 time longer than over the vector
437     vector< _LayerEdge* >           _edges;
438
439     // key:   an id of shape (EDGE or VERTEX) shared by a FACE with
440     //        layers and a FACE w/o layers
441     // value: the shape (FACE or EDGE) to shrink mesh on.
442     //       _LayerEdge's basing on nodes on key shape are inflated along the value shape
443     map< TGeomID, TopoDS_Shape >     _shrinkShape2Shape;
444
445     // Convex FACEs whose radius of curvature is less than the thickness of layers
446     map< TGeomID, _ConvexFace >      _convexFaces;
447
448     // shapes (EDGEs and VERTEXes) srink from which is forbiden due to collisions with
449     // the adjacent SOLID
450     set< TGeomID >                   _noShrinkShapes;
451
452     // <EDGE to smooth on> to <it's curve> -- for analytic smooth
453     map< TGeomID,Handle(Geom_Curve)> _edge2curve;
454
455     // end indices in _edges of _LayerEdge on each shape, first go shapes to smooth
456     vector< int >                    _endEdgeOnShape;
457     int                              _nbShapesToSmooth;
458
459     double                           _epsilon; // precision for SegTriaInter()
460
461     TGeomID                          _index; // SOLID id, for debug
462
463     _SolidData(const TopoDS_Shape&             s=TopoDS_Shape(),
464                const StdMeshers_ViscousLayers* h=0,
465                const TopoDS_Shape&             hs=TopoDS_Shape(),
466                _MeshOfSolid*                   m=0)
467       :_solid(s), _hyp(h), _hypShape(hs), _proxyMesh(m) {}
468     ~_SolidData();
469
470     Handle(Geom_Curve) CurveForSmooth( const TopoDS_Edge&    E,
471                                        const int             iFrom,
472                                        const int             iTo,
473                                        Handle(Geom_Surface)& surface,
474                                        const TopoDS_Face&    F,
475                                        SMESH_MesherHelper&   helper);
476
477     void SortOnEdge( const TopoDS_Edge&  E,
478                      const int           iFrom,
479                      const int           iTo,
480                      SMESH_MesherHelper& helper);
481
482     _ConvexFace* GetConvexFace( const TGeomID faceID )
483     {
484       map< TGeomID, _ConvexFace >::iterator id2face = _convexFaces.find( faceID );
485       return id2face == _convexFaces.end() ? 0 : & id2face->second;
486     }
487     void GetEdgesOnShape( size_t end, int &  iBeg, int &  iEnd )
488     {
489       iBeg = end > 0 ? _endEdgeOnShape[ end-1 ] : 0;
490       iEnd = _endEdgeOnShape[ end ];
491     }
492
493     bool GetShapeEdges(const TGeomID shapeID, size_t& edgeEnd, int* iBeg=0, int* iEnd=0 ) const;
494
495     void AddShapesToSmooth( const set< TGeomID >& shapeIDs );
496   };
497   //--------------------------------------------------------------------------------
498   /*!
499    * \brief Container of centers of curvature at nodes on an EDGE bounding _ConvexFace
500    */
501   struct _CentralCurveOnEdge
502   {
503     bool                  _isDegenerated;
504     vector< gp_Pnt >      _curvaCenters;
505     vector< _LayerEdge* > _ledges;
506     vector< gp_XYZ >      _normals; // new normal for each of _ledges
507     vector< double >      _segLength2;
508
509     TopoDS_Edge           _edge;
510     TopoDS_Face           _adjFace;
511     bool                  _adjFaceToSmooth;
512
513     void Append( const gp_Pnt& center, _LayerEdge* ledge )
514     {
515       if ( _curvaCenters.size() > 0 )
516         _segLength2.push_back( center.SquareDistance( _curvaCenters.back() ));
517       _curvaCenters.push_back( center );
518       _ledges.push_back( ledge );
519       _normals.push_back( ledge->_normal );
520     }
521     bool FindNewNormal( const gp_Pnt& center, gp_XYZ& newNormal );
522     void SetShapes( const TopoDS_Edge&  edge,
523                     const _ConvexFace&  convFace,
524                     const _SolidData&   data,
525                     SMESH_MesherHelper& helper);
526   };
527   //--------------------------------------------------------------------------------
528   /*!
529    * \brief Data of node on a shrinked FACE
530    */
531   struct _SmoothNode
532   {
533     const SMDS_MeshNode*         _node;
534     vector<_Simplex>             _simplices; // for quality check
535
536     enum SmoothType { LAPLACIAN, CENTROIDAL, ANGULAR, TFI };
537
538     bool Smooth(int&                  badNb,
539                 Handle(Geom_Surface)& surface,
540                 SMESH_MesherHelper&   helper,
541                 const double          refSign,
542                 SmoothType            how,
543                 bool                  set3D);
544
545     gp_XY computeAngularPos(vector<gp_XY>& uv,
546                             const gp_XY&   uvToFix,
547                             const double   refSign );
548   };
549   //--------------------------------------------------------------------------------
550   /*!
551    * \brief Builder of viscous layers
552    */
553   class _ViscousBuilder
554   {
555   public:
556     _ViscousBuilder();
557     // does it's job
558     SMESH_ComputeErrorPtr Compute(SMESH_Mesh&         mesh,
559                                   const TopoDS_Shape& shape);
560
561     // restore event listeners used to clear an inferior dim sub-mesh modified by viscous layers
562     void RestoreListeners();
563
564     // computes SMESH_ProxyMesh::SubMesh::_n2n;
565     bool MakeN2NMap( _MeshOfSolid* pm );
566
567   private:
568
569     bool findSolidsWithLayers();
570     bool findFacesWithLayers();
571     bool makeLayer(_SolidData& data);
572     bool setEdgeData(_LayerEdge& edge, const set<TGeomID>& subIds,
573                      SMESH_MesherHelper& helper, _SolidData& data);
574     gp_XYZ getFaceNormal(const SMDS_MeshNode* n,
575                          const TopoDS_Face&   face,
576                          SMESH_MesherHelper&  helper,
577                          bool&                isOK,
578                          bool                 shiftInside=false);
579     gp_XYZ getWeigthedNormal( const SMDS_MeshNode*         n,
580                               std::pair< TGeomID, gp_XYZ > fId2Normal[],
581                               const int                    nbFaces );
582     bool findNeiborsOnEdge(const _LayerEdge*     edge,
583                            const SMDS_MeshNode*& n1,
584                            const SMDS_MeshNode*& n2,
585                            _SolidData&           data);
586     void getSimplices( const SMDS_MeshNode* node, vector<_Simplex>& simplices,
587                        const set<TGeomID>& ingnoreShapes,
588                        const _SolidData*   dataToCheckOri = 0,
589                        const bool          toSort = false);
590     void findSimplexTestEdges( _SolidData&                    data,
591                                vector< vector<_LayerEdge*> >& edgesByGeom);
592     bool sortEdges( _SolidData&                    data,
593                     vector< vector<_LayerEdge*> >& edgesByGeom);
594     void limitStepSizeByCurvature( _SolidData&  data );
595     void limitStepSize( _SolidData&             data,
596                         const SMDS_MeshElement* face,
597                         const _LayerEdge*       maxCosinEdge );
598     void limitStepSize( _SolidData& data, const double minSize);
599     bool inflate(_SolidData& data);
600     bool smoothAndCheck(_SolidData& data, const int nbSteps, double & distToIntersection);
601     bool smoothAnalyticEdge( _SolidData&           data,
602                              const int             iFrom,
603                              const int             iTo,
604                              Handle(Geom_Surface)& surface,
605                              const TopoDS_Face&    F,
606                              SMESH_MesherHelper&   helper);
607     bool updateNormals( _SolidData& data, SMESH_MesherHelper& helper, int stepNb );
608     bool updateNormalsOfConvexFaces( _SolidData&         data,
609                                      SMESH_MesherHelper& helper,
610                                      int                 stepNb );
611     bool refine(_SolidData& data);
612     bool shrink();
613     bool prepareEdgeToShrink( _LayerEdge& edge, const TopoDS_Face& F,
614                               SMESH_MesherHelper& helper,
615                               const SMESHDS_SubMesh* faceSubMesh );
616     void restoreNoShrink( _LayerEdge& edge ) const;
617     void fixBadFaces(const TopoDS_Face&          F,
618                      SMESH_MesherHelper&         helper,
619                      const bool                  is2D,
620                      const int                   step,
621                      set<const SMDS_MeshNode*> * involvedNodes=NULL);
622     bool addBoundaryElements();
623
624     bool error( const string& text, int solidID=-1 );
625     SMESHDS_Mesh* getMeshDS() const { return _mesh->GetMeshDS(); }
626
627     // debug
628     void makeGroupOfLE();
629
630     SMESH_Mesh*           _mesh;
631     SMESH_ComputeErrorPtr _error;
632
633     vector< _SolidData >  _sdVec;
634     int                   _tmpFaceID;
635   };
636   //--------------------------------------------------------------------------------
637   /*!
638    * \brief Shrinker of nodes on the EDGE
639    */
640   class _Shrinker1D
641   {
642     vector<double>                _initU;
643     vector<double>                _normPar;
644     vector<const SMDS_MeshNode*>  _nodes;
645     const _LayerEdge*             _edges[2];
646     bool                          _done;
647   public:
648     void AddEdge( const _LayerEdge* e, SMESH_MesherHelper& helper );
649     void Compute(bool set3D, SMESH_MesherHelper& helper);
650     void RestoreParams();
651     void SwapSrcTgtNodes(SMESHDS_Mesh* mesh);
652   };
653   //--------------------------------------------------------------------------------
654   /*!
655    * \brief Class of temporary mesh face.
656    * We can't use SMDS_FaceOfNodes since it's impossible to set it's ID which is
657    * needed because SMESH_ElementSearcher internaly uses set of elements sorted by ID
658    */
659   struct _TmpMeshFace : public SMDS_MeshElement
660   {
661     vector<const SMDS_MeshNode* > _nn;
662     _TmpMeshFace( const vector<const SMDS_MeshNode*>& nodes, int id, int faceID=-1):
663       SMDS_MeshElement(id), _nn(nodes) { setShapeId(faceID); }
664     virtual const SMDS_MeshNode* GetNode(const int ind) const { return _nn[ind]; }
665     virtual SMDSAbs_ElementType  GetType() const              { return SMDSAbs_Face; }
666     virtual vtkIdType GetVtkType() const                      { return -1; }
667     virtual SMDSAbs_EntityType   GetEntityType() const        { return SMDSEntity_Last; }
668     virtual SMDSAbs_GeometryType GetGeomType() const
669     { return _nn.size() == 3 ? SMDSGeom_TRIANGLE : SMDSGeom_QUADRANGLE; }
670     virtual SMDS_ElemIteratorPtr elementsIterator(SMDSAbs_ElementType) const
671     { return SMDS_ElemIteratorPtr( new SMDS_NodeVectorElemIterator( _nn.begin(), _nn.end()));}
672   };
673   //--------------------------------------------------------------------------------
674   /*!
675    * \brief Class of temporary mesh face storing _LayerEdge it's based on
676    */
677   struct _TmpMeshFaceOnEdge : public _TmpMeshFace
678   {
679     _LayerEdge *_le1, *_le2;
680     _TmpMeshFaceOnEdge( _LayerEdge* le1, _LayerEdge* le2, int ID ):
681       _TmpMeshFace( vector<const SMDS_MeshNode*>(4), ID ), _le1(le1), _le2(le2)
682     {
683       _nn[0]=_le1->_nodes[0];
684       _nn[1]=_le1->_nodes.back();
685       _nn[2]=_le2->_nodes.back();
686       _nn[3]=_le2->_nodes[0];
687     }
688   };
689   //--------------------------------------------------------------------------------
690   /*!
691    * \brief Retriever of node coordinates either directly of from a surface by node UV.
692    * \warning Location of a surface is ignored
693    */
694   struct _NodeCoordHelper
695   {
696     SMESH_MesherHelper&        _helper;
697     const TopoDS_Face&         _face;
698     Handle(Geom_Surface)       _surface;
699     gp_XYZ (_NodeCoordHelper::* _fun)(const SMDS_MeshNode* n) const;
700
701     _NodeCoordHelper(const TopoDS_Face& F, SMESH_MesherHelper& helper, bool is2D)
702       : _helper( helper ), _face( F )
703     {
704       if ( is2D )
705       {
706         TopLoc_Location loc;
707         _surface = BRep_Tool::Surface( _face, loc );
708       }
709       if ( _surface.IsNull() )
710         _fun = & _NodeCoordHelper::direct;
711       else
712         _fun = & _NodeCoordHelper::byUV;
713     }
714     gp_XYZ operator()(const SMDS_MeshNode* n) const { return (this->*_fun)( n ); }
715
716   private:
717     gp_XYZ direct(const SMDS_MeshNode* n) const
718     {
719       return SMESH_TNodeXYZ( n );
720     }
721     gp_XYZ byUV  (const SMDS_MeshNode* n) const
722     {
723       gp_XY uv = _helper.GetNodeUV( _face, n );
724       return _surface->Value( uv.X(), uv.Y() ).XYZ();
725     }
726   };
727 } // namespace VISCOUS_3D
728
729
730
731 //================================================================================
732 // StdMeshers_ViscousLayers hypothesis
733 //
734 StdMeshers_ViscousLayers::StdMeshers_ViscousLayers(int hypId, int studyId, SMESH_Gen* gen)
735   :SMESH_Hypothesis(hypId, studyId, gen),
736    _isToIgnoreShapes(1), _nbLayers(1), _thickness(1), _stretchFactor(1)
737 {
738   _name = StdMeshers_ViscousLayers::GetHypType();
739   _param_algo_dim = -3; // auxiliary hyp used by 3D algos
740 } // --------------------------------------------------------------------------------
741 void StdMeshers_ViscousLayers::SetBndShapes(const std::vector<int>& faceIds, bool toIgnore)
742 {
743   if ( faceIds != _shapeIds )
744     _shapeIds = faceIds, NotifySubMeshesHypothesisModification();
745   if ( _isToIgnoreShapes != toIgnore )
746     _isToIgnoreShapes = toIgnore, NotifySubMeshesHypothesisModification();
747 } // --------------------------------------------------------------------------------
748 void StdMeshers_ViscousLayers::SetTotalThickness(double thickness)
749 {
750   if ( thickness != _thickness )
751     _thickness = thickness, NotifySubMeshesHypothesisModification();
752 } // --------------------------------------------------------------------------------
753 void StdMeshers_ViscousLayers::SetNumberLayers(int nb)
754 {
755   if ( _nbLayers != nb )
756     _nbLayers = nb, NotifySubMeshesHypothesisModification();
757 } // --------------------------------------------------------------------------------
758 void StdMeshers_ViscousLayers::SetStretchFactor(double factor)
759 {
760   if ( _stretchFactor != factor )
761     _stretchFactor = factor, NotifySubMeshesHypothesisModification();
762 } // --------------------------------------------------------------------------------
763 SMESH_ProxyMesh::Ptr
764 StdMeshers_ViscousLayers::Compute(SMESH_Mesh&         theMesh,
765                                   const TopoDS_Shape& theShape,
766                                   const bool          toMakeN2NMap) const
767 {
768   using namespace VISCOUS_3D;
769   _ViscousBuilder bulder;
770   SMESH_ComputeErrorPtr err = bulder.Compute( theMesh, theShape );
771   if ( err && !err->IsOK() )
772     return SMESH_ProxyMesh::Ptr();
773
774   vector<SMESH_ProxyMesh::Ptr> components;
775   TopExp_Explorer exp( theShape, TopAbs_SOLID );
776   for ( ; exp.More(); exp.Next() )
777   {
778     if ( _MeshOfSolid* pm =
779          _ViscousListener::GetSolidMesh( &theMesh, exp.Current(), /*toCreate=*/false))
780     {
781       if ( toMakeN2NMap && !pm->_n2nMapComputed )
782         if ( !bulder.MakeN2NMap( pm ))
783           return SMESH_ProxyMesh::Ptr();
784       components.push_back( SMESH_ProxyMesh::Ptr( pm ));
785       pm->myIsDeletable = false; // it will de deleted by boost::shared_ptr
786     }
787     _ViscousListener::RemoveSolidMesh ( &theMesh, exp.Current() );
788   }
789   switch ( components.size() )
790   {
791   case 0: break;
792
793   case 1: return components[0];
794
795   default: return SMESH_ProxyMesh::Ptr( new SMESH_ProxyMesh( components ));
796   }
797   return SMESH_ProxyMesh::Ptr();
798 } // --------------------------------------------------------------------------------
799 std::ostream & StdMeshers_ViscousLayers::SaveTo(std::ostream & save)
800 {
801   save << " " << _nbLayers
802        << " " << _thickness
803        << " " << _stretchFactor
804        << " " << _shapeIds.size();
805   for ( size_t i = 0; i < _shapeIds.size(); ++i )
806     save << " " << _shapeIds[i];
807   save << " " << !_isToIgnoreShapes; // negate to keep the behavior in old studies.
808   return save;
809 } // --------------------------------------------------------------------------------
810 std::istream & StdMeshers_ViscousLayers::LoadFrom(std::istream & load)
811 {
812   int nbFaces, faceID, shapeToTreat;
813   load >> _nbLayers >> _thickness >> _stretchFactor >> nbFaces;
814   while ( _shapeIds.size() < nbFaces && load >> faceID )
815     _shapeIds.push_back( faceID );
816   if ( load >> shapeToTreat )
817     _isToIgnoreShapes = !shapeToTreat;
818   else
819     _isToIgnoreShapes = true; // old behavior
820   return load;
821 } // --------------------------------------------------------------------------------
822 bool StdMeshers_ViscousLayers::SetParametersByMesh(const SMESH_Mesh*   theMesh,
823                                                    const TopoDS_Shape& theShape)
824 {
825   // TODO
826   return false;
827 }
828 // END StdMeshers_ViscousLayers hypothesis
829 //================================================================================
830
831 namespace
832 {
833   gp_XYZ getEdgeDir( const TopoDS_Edge& E, const TopoDS_Vertex& fromV )
834   {
835     gp_Vec dir;
836     double f,l;
837     Handle(Geom_Curve) c = BRep_Tool::Curve( E, f, l );
838     gp_Pnt p = BRep_Tool::Pnt( fromV );
839     double distF = p.SquareDistance( c->Value( f ));
840     double distL = p.SquareDistance( c->Value( l ));
841     c->D1(( distF < distL ? f : l), p, dir );
842     if ( distL < distF ) dir.Reverse();
843     return dir.XYZ();
844   }
845   //--------------------------------------------------------------------------------
846   gp_XYZ getEdgeDir( const TopoDS_Edge& E, const SMDS_MeshNode* atNode,
847                      SMESH_MesherHelper& helper)
848   {
849     gp_Vec dir;
850     double f,l; gp_Pnt p;
851     Handle(Geom_Curve) c = BRep_Tool::Curve( E, f, l );
852     double u = helper.GetNodeU( E, atNode );
853     c->D1( u, p, dir );
854     return dir.XYZ();
855   }
856   //--------------------------------------------------------------------------------
857   gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Vertex& fromV,
858                      const SMDS_MeshNode* node, SMESH_MesherHelper& helper, bool& ok,
859                      double* cosin=0);
860   //--------------------------------------------------------------------------------
861   gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Edge& fromE,
862                      const SMDS_MeshNode* node, SMESH_MesherHelper& helper, bool& ok)
863   {
864     double f,l;
865     Handle(Geom_Curve) c = BRep_Tool::Curve( fromE, f, l );
866     if ( c.IsNull() )
867     {
868       TopoDS_Vertex v = helper.IthVertex( 0, fromE );
869       return getFaceDir( F, v, node, helper, ok );
870     }
871     gp_XY uv = helper.GetNodeUV( F, node, 0, &ok );
872     Handle(Geom_Surface) surface = BRep_Tool::Surface( F );
873     gp_Pnt p; gp_Vec du, dv, norm;
874     surface->D1( uv.X(),uv.Y(), p, du,dv );
875     norm = du ^ dv;
876
877     double u = helper.GetNodeU( fromE, node, 0, &ok );
878     c->D1( u, p, du );
879     TopAbs_Orientation o = helper.GetSubShapeOri( F.Oriented(TopAbs_FORWARD), fromE);
880     if ( o == TopAbs_REVERSED )
881       du.Reverse();
882
883     gp_Vec dir = norm ^ du;
884
885     if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX &&
886          helper.IsClosedEdge( fromE ))
887     {
888       if ( fabs(u-f) < fabs(u-l)) c->D1( l, p, dv );
889       else                        c->D1( f, p, dv );
890       if ( o == TopAbs_REVERSED )
891         dv.Reverse();
892       gp_Vec dir2 = norm ^ dv;
893       dir = dir.Normalized() + dir2.Normalized();
894     }
895     return dir.XYZ();
896   }
897   //--------------------------------------------------------------------------------
898   gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Vertex& fromV,
899                      const SMDS_MeshNode* node, SMESH_MesherHelper& helper,
900                      bool& ok, double* cosin)
901   {
902     TopoDS_Face faceFrw = F;
903     faceFrw.Orientation( TopAbs_FORWARD );
904     double f,l; TopLoc_Location loc;
905     TopoDS_Edge edges[2]; // sharing a vertex
906     int nbEdges = 0;
907     {
908       TopoDS_Vertex VV[2];
909       TopExp_Explorer exp( faceFrw, TopAbs_EDGE );
910       for ( ; exp.More() && nbEdges < 2; exp.Next() )
911       {
912         const TopoDS_Edge& e = TopoDS::Edge( exp.Current() );
913         if ( SMESH_Algo::isDegenerated( e )) continue;
914         TopExp::Vertices( e, VV[0], VV[1], /*CumOri=*/true );
915         if ( VV[1].IsSame( fromV )) {
916           nbEdges += edges[ 0 ].IsNull();
917           edges[ 0 ] = e;
918         }
919         else if ( VV[0].IsSame( fromV )) {
920           nbEdges += edges[ 1 ].IsNull();
921           edges[ 1 ] = e;
922         }
923       }
924     }
925     gp_XYZ dir(0,0,0), edgeDir[2];
926     if ( nbEdges == 2 )
927     {
928       // get dirs of edges going fromV
929       ok = true;
930       for ( size_t i = 0; i < nbEdges && ok; ++i )
931       {
932         edgeDir[i] = getEdgeDir( edges[i], fromV );
933         double size2 = edgeDir[i].SquareModulus();
934         if (( ok = size2 > numeric_limits<double>::min() ))
935           edgeDir[i] /= sqrt( size2 );
936       }
937       if ( !ok ) return dir;
938
939       // get angle between the 2 edges
940       gp_Vec faceNormal;
941       double angle = helper.GetAngle( edges[0], edges[1], faceFrw, fromV, &faceNormal );
942       if ( Abs( angle ) < 5 * M_PI/180 )
943       {
944         dir = ( faceNormal.XYZ() ^ edgeDir[0].Reversed()) + ( faceNormal.XYZ() ^ edgeDir[1] );
945       }
946       else
947       {
948         dir = edgeDir[0] + edgeDir[1];
949         if ( angle < 0 )
950           dir.Reverse();
951       }
952       if ( cosin ) {
953         double angle = gp_Vec( edgeDir[0] ).Angle( dir );
954         *cosin = Cos( angle );
955       }
956     }
957     else if ( nbEdges == 1 )
958     {
959       dir = getFaceDir( faceFrw, edges[ edges[0].IsNull() ], node, helper, ok );
960       if ( cosin ) *cosin = 1.;
961     }
962     else
963     {
964       ok = false;
965     }
966
967     return dir;
968   }
969   //================================================================================
970   /*!
971    * \brief Returns true if a FACE is bound by a concave EDGE
972    */
973   //================================================================================
974
975   bool isConcave( const TopoDS_Face& F, SMESH_MesherHelper& helper )
976   {
977     // if ( helper.Count( F, TopAbs_WIRE, /*useMap=*/false) > 1 )
978     //   return true;
979     gp_Vec2d drv1, drv2;
980     gp_Pnt2d p;
981     TopExp_Explorer eExp( F.Oriented( TopAbs_FORWARD ), TopAbs_EDGE );
982     for ( ; eExp.More(); eExp.Next() )
983     {
984       const TopoDS_Edge& E = TopoDS::Edge( eExp.Current() );
985       if ( SMESH_Algo::isDegenerated( E )) continue;
986       // check if 2D curve is concave
987       BRepAdaptor_Curve2d curve( E, F );
988       const int nbIntervals = curve.NbIntervals( GeomAbs_C2 );
989       TColStd_Array1OfReal intervals(1, nbIntervals + 1 );
990       curve.Intervals( intervals, GeomAbs_C2 );
991       bool isConvex = true;
992       for ( int i = 1; i <= nbIntervals && isConvex; ++i )
993       {
994         double u1 = intervals( i );
995         double u2 = intervals( i+1 );
996         curve.D2( 0.5*( u1+u2 ), p, drv1, drv2 );
997         double cross = drv2 ^ drv1;
998         if ( E.Orientation() == TopAbs_REVERSED )
999           cross = -cross;
1000         isConvex = ( cross > 0.1 ); //-1e-9 );
1001       }
1002       if ( !isConvex )
1003       {
1004         //cout << "Concave FACE " << helper.GetMeshDS()->ShapeToIndex( F ) << endl;
1005         return true;
1006       }
1007     }
1008     // check angles at VERTEXes
1009     TError error;
1010     TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, *helper.GetMesh(), 0, error );
1011     for ( size_t iW = 0; iW < wires.size(); ++iW )
1012     {
1013       const int nbEdges = wires[iW]->NbEdges();
1014       if ( nbEdges < 2 && SMESH_Algo::isDegenerated( wires[iW]->Edge(0)))
1015         continue;
1016       for ( int iE1 = 0; iE1 < nbEdges; ++iE1 )
1017       {
1018         if ( SMESH_Algo::isDegenerated( wires[iW]->Edge( iE1 ))) continue;
1019         int iE2 = ( iE1 + 1 ) % nbEdges;
1020         while ( SMESH_Algo::isDegenerated( wires[iW]->Edge( iE2 )))
1021           iE2 = ( iE2 + 1 ) % nbEdges;
1022         double angle = helper.GetAngle( wires[iW]->Edge( iE1 ),
1023                                         wires[iW]->Edge( iE2 ), F,
1024                                         wires[iW]->FirstVertex( iE2 ));
1025         if ( angle < -5. * M_PI / 180. )
1026           return true;
1027       }
1028     }
1029     return false;
1030   }
1031   //--------------------------------------------------------------------------------
1032   // DEBUG. Dump intermediate node positions into a python script
1033   // HOWTO use: run python commands written in a console to see
1034   //  construction steps of viscous layers
1035 #ifdef __myDEBUG
1036   ofstream* py;
1037   int       theNbFunc;
1038   struct PyDump {
1039     PyDump() {
1040       const char* fname = "/tmp/viscous.py";
1041       cout << "execfile('"<<fname<<"')"<<endl;
1042       py = new ofstream(fname);
1043       *py << "import SMESH" << endl
1044           << "from salome.smesh import smeshBuilder" << endl
1045           << "smesh  = smeshBuilder.New(salome.myStudy)" << endl
1046           << "meshSO = smesh.GetCurrentStudy().FindObjectID('0:1:2:3')" << endl
1047           << "mesh   = smesh.Mesh( meshSO.GetObject() )"<<endl;
1048       theNbFunc = 0;
1049     }
1050     void Finish() {
1051       if (py) {
1052         *py << "mesh.GroupOnFilter(SMESH.VOLUME,'Viscous Prisms',"
1053           "smesh.GetFilter(SMESH.VOLUME,SMESH.FT_ElemGeomType,'=',SMESH.Geom_PENTA))"<<endl;
1054         *py << "mesh.GroupOnFilter(SMESH.VOLUME,'Neg Volumes',"
1055           "smesh.GetFilter(SMESH.VOLUME,SMESH.FT_Volume3D,'<',0))"<<endl;
1056       }
1057       delete py; py=0;
1058     }
1059     ~PyDump() { Finish(); cout << "NB FUNCTIONS: " << theNbFunc << endl; }
1060   };
1061 #define dumpFunction(f) { _dumpFunction(f, __LINE__);}
1062 #define dumpMove(n)     { _dumpMove(n, __LINE__);}
1063 #define dumpCmd(txt)    { _dumpCmd(txt, __LINE__);}
1064   void _dumpFunction(const string& fun, int ln)
1065   { if (py) *py<< "def "<<fun<<"(): # "<< ln <<endl; cout<<fun<<"()"<<endl; ++theNbFunc; }
1066   void _dumpMove(const SMDS_MeshNode* n, int ln)
1067   { if (py) *py<< "  mesh.MoveNode( "<<n->GetID()<< ", "<< n->X()
1068                << ", "<<n->Y()<<", "<< n->Z()<< ")\t\t # "<< ln <<endl; }
1069   void _dumpCmd(const string& txt, int ln)
1070   { if (py) *py<< "  "<<txt<<" # "<< ln <<endl; }
1071   void dumpFunctionEnd()
1072   { if (py) *py<< "  return"<< endl; }
1073   void dumpChangeNodes( const SMDS_MeshElement* f )
1074   { if (py) { *py<< "  mesh.ChangeElemNodes( " << f->GetID()<<", [";
1075       for ( int i=1; i < f->NbNodes(); ++i ) *py << f->GetNode(i-1)->GetID()<<", ";
1076       *py << f->GetNode( f->NbNodes()-1 )->GetID() << " ])"<< endl; }}
1077 #define debugMsg( txt ) { cout << txt << " (line: " << __LINE__ << ")" << endl; }
1078 #else
1079   struct PyDump { void Finish() {} };
1080 #define dumpFunction(f) f
1081 #define dumpMove(n)
1082 #define dumpCmd(txt)
1083 #define dumpFunctionEnd()
1084 #define dumpChangeNodes(f)
1085 #define debugMsg( txt ) {}
1086 #endif
1087 }
1088
1089 using namespace VISCOUS_3D;
1090
1091 //================================================================================
1092 /*!
1093  * \brief Constructor of _ViscousBuilder
1094  */
1095 //================================================================================
1096
1097 _ViscousBuilder::_ViscousBuilder()
1098 {
1099   _error = SMESH_ComputeError::New(COMPERR_OK);
1100   _tmpFaceID = 0;
1101 }
1102
1103 //================================================================================
1104 /*!
1105  * \brief Stores error description and returns false
1106  */
1107 //================================================================================
1108
1109 bool _ViscousBuilder::error(const string& text, int solidId )
1110 {
1111   const string prefix = string("Viscous layers builder: ");
1112   _error->myName    = COMPERR_ALGO_FAILED;
1113   _error->myComment = prefix + text;
1114   if ( _mesh )
1115   {
1116     SMESH_subMesh* sm = _mesh->GetSubMeshContaining( solidId );
1117     if ( !sm && !_sdVec.empty() )
1118       sm = _mesh->GetSubMeshContaining( solidId = _sdVec[0]._index );
1119     if ( sm && sm->GetSubShape().ShapeType() == TopAbs_SOLID )
1120     {
1121       SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1122       if ( smError && smError->myAlgo )
1123         _error->myAlgo = smError->myAlgo;
1124       smError = _error;
1125       sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1126     }
1127     // set KO to all solids
1128     for ( size_t i = 0; i < _sdVec.size(); ++i )
1129     {
1130       if ( _sdVec[i]._index == solidId )
1131         continue;
1132       sm = _mesh->GetSubMesh( _sdVec[i]._solid );
1133       if ( !sm->IsEmpty() )
1134         continue;
1135       SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1136       if ( !smError || smError->IsOK() )
1137       {
1138         smError = SMESH_ComputeError::New( COMPERR_ALGO_FAILED, prefix + "failed");
1139         sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1140       }
1141     }
1142   }
1143   makeGroupOfLE(); // debug
1144
1145   return false;
1146 }
1147
1148 //================================================================================
1149 /*!
1150  * \brief At study restoration, restore event listeners used to clear an inferior
1151  *  dim sub-mesh modified by viscous layers
1152  */
1153 //================================================================================
1154
1155 void _ViscousBuilder::RestoreListeners()
1156 {
1157   // TODO
1158 }
1159
1160 //================================================================================
1161 /*!
1162  * \brief computes SMESH_ProxyMesh::SubMesh::_n2n
1163  */
1164 //================================================================================
1165
1166 bool _ViscousBuilder::MakeN2NMap( _MeshOfSolid* pm )
1167 {
1168   SMESH_subMesh* solidSM = pm->mySubMeshes.front();
1169   TopExp_Explorer fExp( solidSM->GetSubShape(), TopAbs_FACE );
1170   for ( ; fExp.More(); fExp.Next() )
1171   {
1172     SMESHDS_SubMesh* srcSmDS = pm->GetMeshDS()->MeshElements( fExp.Current() );
1173     const SMESH_ProxyMesh::SubMesh* prxSmDS = pm->GetProxySubMesh( fExp.Current() );
1174
1175     if ( !srcSmDS || !prxSmDS || !srcSmDS->NbElements() || !prxSmDS->NbElements() )
1176       continue;
1177     if ( srcSmDS->GetElements()->next() == prxSmDS->GetElements()->next())
1178       continue;
1179
1180     if ( srcSmDS->NbElements() != prxSmDS->NbElements() )
1181       return error( "Different nb elements in a source and a proxy sub-mesh", solidSM->GetId());
1182
1183     SMDS_ElemIteratorPtr srcIt = srcSmDS->GetElements();
1184     SMDS_ElemIteratorPtr prxIt = prxSmDS->GetElements();
1185     while( prxIt->more() )
1186     {
1187       const SMDS_MeshElement* fSrc = srcIt->next();
1188       const SMDS_MeshElement* fPrx = prxIt->next();
1189       if ( fSrc->NbNodes() != fPrx->NbNodes())
1190         return error( "Different elements in a source and a proxy sub-mesh", solidSM->GetId());
1191       for ( int i = 0 ; i < fPrx->NbNodes(); ++i )
1192         pm->setNode2Node( fSrc->GetNode(i), fPrx->GetNode(i), prxSmDS );
1193     }
1194   }
1195   pm->_n2nMapComputed = true;
1196   return true;
1197 }
1198
1199 //================================================================================
1200 /*!
1201  * \brief Does its job
1202  */
1203 //================================================================================
1204
1205 SMESH_ComputeErrorPtr _ViscousBuilder::Compute(SMESH_Mesh&         theMesh,
1206                                                const TopoDS_Shape& theShape)
1207 {
1208   // TODO: set priority of solids during Gen::Compute()
1209
1210   _mesh = & theMesh;
1211
1212   // check if proxy mesh already computed
1213   TopExp_Explorer exp( theShape, TopAbs_SOLID );
1214   if ( !exp.More() )
1215     return error("No SOLID's in theShape"), _error;
1216
1217   if ( _ViscousListener::GetSolidMesh( _mesh, exp.Current(), /*toCreate=*/false))
1218     return SMESH_ComputeErrorPtr(); // everything already computed
1219
1220   PyDump debugDump;
1221
1222   // TODO: ignore already computed SOLIDs 
1223   if ( !findSolidsWithLayers())
1224     return _error;
1225
1226   if ( !findFacesWithLayers() )
1227     return _error;
1228
1229   for ( size_t i = 0; i < _sdVec.size(); ++i )
1230   {
1231     if ( ! makeLayer(_sdVec[i]) )
1232       return _error;
1233
1234     if ( _sdVec[i]._edges.size() == 0 )
1235       continue;
1236     
1237     if ( ! inflate(_sdVec[i]) )
1238       return _error;
1239
1240     if ( ! refine(_sdVec[i]) )
1241       return _error;
1242   }
1243   if ( !shrink() )
1244     return _error;
1245
1246   addBoundaryElements();
1247
1248   makeGroupOfLE(); // debug
1249   debugDump.Finish();
1250
1251   return _error;
1252 }
1253
1254 //================================================================================
1255 /*!
1256  * \brief Finds SOLIDs to compute using viscous layers. Fills _sdVec
1257  */
1258 //================================================================================
1259
1260 bool _ViscousBuilder::findSolidsWithLayers()
1261 {
1262   // get all solids
1263   TopTools_IndexedMapOfShape allSolids;
1264   TopExp::MapShapes( _mesh->GetShapeToMesh(), TopAbs_SOLID, allSolids );
1265   _sdVec.reserve( allSolids.Extent());
1266
1267   SMESH_Gen* gen = _mesh->GetGen();
1268   SMESH_HypoFilter filter;
1269   for ( int i = 1; i <= allSolids.Extent(); ++i )
1270   {
1271     // find StdMeshers_ViscousLayers hyp assigned to the i-th solid
1272     SMESH_Algo* algo = gen->GetAlgo( *_mesh, allSolids(i) );
1273     if ( !algo ) continue;
1274     // TODO: check if algo is hidden
1275     const list <const SMESHDS_Hypothesis *> & allHyps =
1276       algo->GetUsedHypothesis(*_mesh, allSolids(i), /*ignoreAuxiliary=*/false);
1277     list< const SMESHDS_Hypothesis *>::const_iterator hyp = allHyps.begin();
1278     const StdMeshers_ViscousLayers* viscHyp = 0;
1279     for ( ; hyp != allHyps.end() && !viscHyp; ++hyp )
1280       viscHyp = dynamic_cast<const StdMeshers_ViscousLayers*>( *hyp );
1281     if ( viscHyp )
1282     {
1283       TopoDS_Shape hypShape;
1284       filter.Init( filter.Is( viscHyp ));
1285       _mesh->GetHypothesis( allSolids(i), filter, true, &hypShape );
1286
1287       _MeshOfSolid* proxyMesh = _ViscousListener::GetSolidMesh( _mesh,
1288                                                                 allSolids(i),
1289                                                                 /*toCreate=*/true);
1290       _sdVec.push_back( _SolidData( allSolids(i), viscHyp, hypShape, proxyMesh ));
1291       _sdVec.back()._index = getMeshDS()->ShapeToIndex( allSolids(i));
1292     }
1293   }
1294   if ( _sdVec.empty() )
1295     return error
1296       ( SMESH_Comment(StdMeshers_ViscousLayers::GetHypType()) << " hypothesis not found",0);
1297
1298   return true;
1299 }
1300
1301 //================================================================================
1302 /*!
1303  * \brief 
1304  */
1305 //================================================================================
1306
1307 bool _ViscousBuilder::findFacesWithLayers()
1308 {
1309   SMESH_MesherHelper helper( *_mesh );
1310   TopExp_Explorer exp;
1311   TopTools_IndexedMapOfShape solids;
1312
1313   // collect all faces to ignore defined by hyp
1314   for ( size_t i = 0; i < _sdVec.size(); ++i )
1315   {
1316     solids.Add( _sdVec[i]._solid );
1317
1318     vector<TGeomID> ids = _sdVec[i]._hyp->GetBndShapes();
1319     if ( _sdVec[i]._hyp->IsToIgnoreShapes() ) // FACEs to ignore are given
1320     {
1321       for ( size_t ii = 0; ii < ids.size(); ++ii )
1322       {
1323         const TopoDS_Shape& s = getMeshDS()->IndexToShape( ids[ii] );
1324         if ( !s.IsNull() && s.ShapeType() == TopAbs_FACE )
1325           _sdVec[i]._ignoreFaceIds.insert( ids[ii] );
1326       }
1327     }
1328     else // FACEs with layers are given
1329     {
1330       exp.Init( _sdVec[i]._solid, TopAbs_FACE );
1331       for ( ; exp.More(); exp.Next() )
1332       {
1333         TGeomID faceInd = getMeshDS()->ShapeToIndex( exp.Current() );
1334         if ( find( ids.begin(), ids.end(), faceInd ) == ids.end() )
1335           _sdVec[i]._ignoreFaceIds.insert( faceInd );
1336       }
1337     }
1338
1339     // ignore internal FACEs if inlets and outlets are specified
1340     {
1341       TopTools_IndexedDataMapOfShapeListOfShape solidsOfFace;
1342       if ( _sdVec[i]._hyp->IsToIgnoreShapes() )
1343         TopExp::MapShapesAndAncestors( _sdVec[i]._hypShape,
1344                                        TopAbs_FACE, TopAbs_SOLID, solidsOfFace);
1345
1346       exp.Init( _sdVec[i]._solid.Oriented( TopAbs_FORWARD ), TopAbs_FACE );
1347       for ( ; exp.More(); exp.Next() )
1348       {
1349         const TopoDS_Face& face = TopoDS::Face( exp.Current() );
1350         if ( helper.NbAncestors( face, *_mesh, TopAbs_SOLID ) < 2 )
1351           continue;
1352
1353         const TGeomID faceInd = getMeshDS()->ShapeToIndex( face );
1354         if ( _sdVec[i]._hyp->IsToIgnoreShapes() )
1355         {
1356           int nbSolids = solidsOfFace.FindFromKey( face ).Extent();
1357           if ( nbSolids > 1 )
1358             _sdVec[i]._ignoreFaceIds.insert( faceInd );
1359         }
1360
1361         if ( helper.IsReversedSubMesh( face ))
1362         {
1363           _sdVec[i]._reversedFaceIds.insert( faceInd );
1364         }
1365       }
1366     }
1367   }
1368
1369   // Find faces to shrink mesh on (solution 2 in issue 0020832);
1370   TopTools_IndexedMapOfShape shapes;
1371   for ( size_t i = 0; i < _sdVec.size(); ++i )
1372   {
1373     shapes.Clear();
1374     TopExp::MapShapes(_sdVec[i]._solid, TopAbs_EDGE, shapes);
1375     for ( int iE = 1; iE <= shapes.Extent(); ++iE )
1376     {
1377       const TopoDS_Shape& edge = shapes(iE);
1378       // find 2 faces sharing an edge
1379       TopoDS_Shape FF[2];
1380       PShapeIteratorPtr fIt = helper.GetAncestors(edge, *_mesh, TopAbs_FACE);
1381       while ( fIt->more())
1382       {
1383         const TopoDS_Shape* f = fIt->next();
1384         if ( helper.IsSubShape( *f, _sdVec[i]._solid))
1385           FF[ int( !FF[0].IsNull()) ] = *f;
1386       }
1387       if( FF[1].IsNull() ) continue; // seam edge can be shared by 1 FACE only
1388       // check presence of layers on them
1389       int ignore[2];
1390       for ( int j = 0; j < 2; ++j )
1391         ignore[j] = _sdVec[i]._ignoreFaceIds.count ( getMeshDS()->ShapeToIndex( FF[j] ));
1392       if ( ignore[0] == ignore[1] )
1393         continue; // nothing interesting
1394       TopoDS_Shape fWOL = FF[ ignore[0] ? 0 : 1 ];
1395       // check presence of layers on fWOL within an adjacent SOLID
1396       bool collision = false;
1397       PShapeIteratorPtr sIt = helper.GetAncestors( fWOL, *_mesh, TopAbs_SOLID );
1398       while ( const TopoDS_Shape* solid = sIt->next() )
1399         if ( !solid->IsSame( _sdVec[i]._solid ))
1400         {
1401           int iSolid = solids.FindIndex( *solid );
1402           int  iFace = getMeshDS()->ShapeToIndex( fWOL );
1403           if ( iSolid > 0 && !_sdVec[ iSolid-1 ]._ignoreFaceIds.count( iFace ))
1404           {
1405             //_sdVec[i]._noShrinkShapes.insert( iFace );
1406             //fWOL.Nullify();
1407             collision = true;
1408           }
1409         }
1410       // add edge to maps
1411       if ( !fWOL.IsNull())
1412       {
1413         TGeomID edgeInd = getMeshDS()->ShapeToIndex( edge );
1414         _sdVec[i]._shrinkShape2Shape.insert( make_pair( edgeInd, fWOL ));
1415         if ( collision )
1416         {
1417           // _shrinkShape2Shape will be used to temporary inflate _LayerEdge's based
1418           // on the edge but shrink won't be performed
1419           _sdVec[i]._noShrinkShapes.insert( edgeInd );
1420         }
1421       }
1422     }
1423   }
1424   // Exclude from _shrinkShape2Shape FACE's that can't be shrinked since
1425   // the algo of the SOLID sharing the FACE does not support it
1426   set< string > notSupportAlgos; notSupportAlgos.insert("Hexa_3D");
1427   for ( size_t i = 0; i < _sdVec.size(); ++i )
1428   {
1429     map< TGeomID, TopoDS_Shape >::iterator e2f = _sdVec[i]._shrinkShape2Shape.begin();
1430     for ( ; e2f != _sdVec[i]._shrinkShape2Shape.end(); ++e2f )
1431     {
1432       const TopoDS_Shape& fWOL = e2f->second;
1433       const TGeomID     edgeID = e2f->first;
1434       bool notShrinkFace = false;
1435       PShapeIteratorPtr soIt = helper.GetAncestors(fWOL, *_mesh, TopAbs_SOLID);
1436       while ( soIt->more() )
1437       {
1438         const TopoDS_Shape* solid = soIt->next();
1439         if ( _sdVec[i]._solid.IsSame( *solid )) continue;
1440         SMESH_Algo* algo = _mesh->GetGen()->GetAlgo( *_mesh, *solid );
1441         if ( !algo || !notSupportAlgos.count( algo->GetName() )) continue;
1442         notShrinkFace = true;
1443         size_t iSolid = 0;
1444         for ( ; iSolid < _sdVec.size(); ++iSolid )
1445         {
1446           if ( _sdVec[iSolid]._solid.IsSame( *solid ) ) {
1447             if ( _sdVec[iSolid]._shrinkShape2Shape.count( edgeID ))
1448               notShrinkFace = false;
1449             break;
1450           }
1451         }
1452         if ( notShrinkFace )
1453         {
1454           _sdVec[i]._noShrinkShapes.insert( edgeID );
1455
1456           // add VERTEXes of the edge in _noShrinkShapes
1457           TopoDS_Shape edge = getMeshDS()->IndexToShape( edgeID );
1458           for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
1459             _sdVec[i]._noShrinkShapes.insert( getMeshDS()->ShapeToIndex( vIt.Value() ));
1460
1461           // check if there is a collision with to-shrink-from EDGEs in iSolid
1462           if ( iSolid == _sdVec.size() )
1463             continue; // no VL in the solid
1464           shapes.Clear();
1465           TopExp::MapShapes( fWOL, TopAbs_EDGE, shapes);
1466           for ( int iE = 1; iE <= shapes.Extent(); ++iE )
1467           {
1468             const TopoDS_Edge& E = TopoDS::Edge( shapes( iE ));
1469             const TGeomID    eID = getMeshDS()->ShapeToIndex( E );
1470             if ( eID == edgeID ||
1471                  !_sdVec[iSolid]._shrinkShape2Shape.count( eID ) ||
1472                  _sdVec[i]._noShrinkShapes.count( eID ))
1473               continue;
1474             for ( int is1st = 0; is1st < 2; ++is1st )
1475             {
1476               TopoDS_Vertex V = helper.IthVertex( is1st, E );
1477               if ( _sdVec[i]._noShrinkShapes.count( getMeshDS()->ShapeToIndex( V ) ))
1478               {
1479                 // _sdVec[i]._noShrinkShapes.insert( eID );
1480                 // V = helper.IthVertex( !is1st, E );
1481                 // _sdVec[i]._noShrinkShapes.insert( getMeshDS()->ShapeToIndex( V ));
1482                 //iE = 0; // re-start the loop on EDGEs of fWOL
1483                 return error("No way to make a conformal mesh with "
1484                              "the given set of faces with layers", _sdVec[i]._index);
1485               }
1486             }
1487           }
1488         }
1489
1490       } // while ( soIt->more() )
1491     } // loop on _sdVec[i]._shrinkShape2Shape
1492   } // loop on _sdVec to fill in _SolidData::_noShrinkShapes
1493
1494   // Find the SHAPE along which to inflate _LayerEdge based on VERTEX
1495
1496   for ( size_t i = 0; i < _sdVec.size(); ++i )
1497   {
1498     shapes.Clear();
1499     TopExp::MapShapes(_sdVec[i]._solid, TopAbs_VERTEX, shapes);
1500     for ( int iV = 1; iV <= shapes.Extent(); ++iV )
1501     {
1502       const TopoDS_Shape& vertex = shapes(iV);
1503       // find faces WOL sharing the vertex
1504       vector< TopoDS_Shape > facesWOL;
1505       int totalNbFaces = 0;
1506       PShapeIteratorPtr fIt = helper.GetAncestors(vertex, *_mesh, TopAbs_FACE);
1507       while ( fIt->more())
1508       {
1509         const TopoDS_Shape* f = fIt->next();
1510         if ( helper.IsSubShape( *f, _sdVec[i]._solid ) )
1511         {
1512           totalNbFaces++;
1513           const int fID = getMeshDS()->ShapeToIndex( *f );
1514           if ( _sdVec[i]._ignoreFaceIds.count ( fID ) /*&&
1515                !_sdVec[i]._noShrinkShapes.count( fID )*/)
1516             facesWOL.push_back( *f );
1517         }
1518       }
1519       if ( facesWOL.size() == totalNbFaces || facesWOL.empty() )
1520         continue; // no layers at this vertex or no WOL
1521       TGeomID vInd = getMeshDS()->ShapeToIndex( vertex );
1522       switch ( facesWOL.size() )
1523       {
1524       case 1:
1525       {
1526         helper.SetSubShape( facesWOL[0] );
1527         if ( helper.IsRealSeam( vInd )) // inflate along a seam edge?
1528         {
1529           TopoDS_Shape seamEdge;
1530           PShapeIteratorPtr eIt = helper.GetAncestors(vertex, *_mesh, TopAbs_EDGE);
1531           while ( eIt->more() && seamEdge.IsNull() )
1532           {
1533             const TopoDS_Shape* e = eIt->next();
1534             if ( helper.IsRealSeam( *e ) )
1535               seamEdge = *e;
1536           }
1537           if ( !seamEdge.IsNull() )
1538           {
1539             _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, seamEdge ));
1540             break;
1541           }
1542         }
1543         _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, facesWOL[0] ));
1544         break;
1545       }
1546       case 2:
1547       {
1548         // find an edge shared by 2 faces
1549         PShapeIteratorPtr eIt = helper.GetAncestors(vertex, *_mesh, TopAbs_EDGE);
1550         while ( eIt->more())
1551         {
1552           const TopoDS_Shape* e = eIt->next();
1553           if ( helper.IsSubShape( *e, facesWOL[0]) &&
1554                helper.IsSubShape( *e, facesWOL[1]))
1555           {
1556             _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, *e )); break;
1557           }
1558         }
1559         break;
1560       }
1561       default:
1562         return error("Not yet supported case", _sdVec[i]._index);
1563       }
1564     }
1565   }
1566
1567   // add FACEs of other SOLIDs to _ignoreFaceIds
1568   for ( size_t i = 0; i < _sdVec.size(); ++i )
1569   {
1570     shapes.Clear();
1571     TopExp::MapShapes(_sdVec[i]._solid, TopAbs_FACE, shapes);
1572
1573     for ( exp.Init( _mesh->GetShapeToMesh(), TopAbs_FACE ); exp.More(); exp.Next() )
1574     {
1575       if ( !shapes.Contains( exp.Current() ))
1576         _sdVec[i]._ignoreFaceIds.insert( getMeshDS()->ShapeToIndex( exp.Current() ));
1577     }
1578   }
1579
1580   return true;
1581 }
1582
1583 //================================================================================
1584 /*!
1585  * \brief Create the inner surface of the viscous layer and prepare data for infation
1586  */
1587 //================================================================================
1588
1589 bool _ViscousBuilder::makeLayer(_SolidData& data)
1590 {
1591   // get all sub-shapes to make layers on
1592   set<TGeomID> subIds, faceIds;
1593   subIds = data._noShrinkShapes;
1594   TopExp_Explorer exp( data._solid, TopAbs_FACE );
1595   for ( ; exp.More(); exp.Next() )
1596     {
1597       SMESH_subMesh* fSubM = _mesh->GetSubMesh( exp.Current() );
1598       if ( ! data._ignoreFaceIds.count( fSubM->GetId() ))
1599         faceIds.insert( fSubM->GetId() );
1600       SMESH_subMeshIteratorPtr subIt = fSubM->getDependsOnIterator(/*includeSelf=*/true);
1601       while ( subIt->more() )
1602         subIds.insert( subIt->next()->GetId() );
1603     }
1604
1605   // make a map to find new nodes on sub-shapes shared with other SOLID
1606   map< TGeomID, TNode2Edge* >::iterator s2ne;
1607   map< TGeomID, TopoDS_Shape >::iterator s2s = data._shrinkShape2Shape.begin();
1608   for (; s2s != data._shrinkShape2Shape.end(); ++s2s )
1609   {
1610     TGeomID shapeInd = s2s->first;
1611     for ( size_t i = 0; i < _sdVec.size(); ++i )
1612     {
1613       if ( _sdVec[i]._index == data._index ) continue;
1614       map< TGeomID, TopoDS_Shape >::iterator s2s2 = _sdVec[i]._shrinkShape2Shape.find( shapeInd );
1615       if ( s2s2 != _sdVec[i]._shrinkShape2Shape.end() &&
1616            *s2s == *s2s2 && !_sdVec[i]._n2eMap.empty() )
1617       {
1618         data._s2neMap.insert( make_pair( shapeInd, &_sdVec[i]._n2eMap ));
1619         break;
1620       }
1621     }
1622   }
1623
1624   // Create temporary faces and _LayerEdge's
1625
1626   dumpFunction(SMESH_Comment("makeLayers_")<<data._index); 
1627
1628   data._stepSize = Precision::Infinite();
1629   data._stepSizeNodes[0] = 0;
1630
1631   SMESH_MesherHelper helper( *_mesh );
1632   helper.SetSubShape( data._solid );
1633   helper.SetElementsOnShape( true );
1634
1635   vector< const SMDS_MeshNode*> newNodes; // of a mesh face
1636   TNode2Edge::iterator n2e2;
1637
1638   // collect _LayerEdge's of shapes they are based on
1639   const int nbShapes = getMeshDS()->MaxShapeIndex();
1640   vector< vector<_LayerEdge*> > edgesByGeom( nbShapes+1 );
1641
1642   for ( set<TGeomID>::iterator id = faceIds.begin(); id != faceIds.end(); ++id )
1643   {
1644     SMESHDS_SubMesh* smDS = getMeshDS()->MeshElements( *id );
1645     if ( !smDS ) return error(SMESH_Comment("Not meshed face ") << *id, data._index );
1646
1647     const TopoDS_Face& F = TopoDS::Face( getMeshDS()->IndexToShape( *id ));
1648     SMESH_ProxyMesh::SubMesh* proxySub =
1649       data._proxyMesh->getFaceSubM( F, /*create=*/true);
1650
1651     SMDS_ElemIteratorPtr eIt = smDS->GetElements();
1652     while ( eIt->more() )
1653     {
1654       const SMDS_MeshElement* face = eIt->next();
1655       newNodes.resize( face->NbCornerNodes() );
1656       double faceMaxCosin = -1;
1657       _LayerEdge* maxCosinEdge = 0;
1658       for ( int i = 0 ; i < face->NbCornerNodes(); ++i )
1659       {
1660         const SMDS_MeshNode* n = face->GetNode(i);
1661         TNode2Edge::iterator n2e = data._n2eMap.insert( make_pair( n, (_LayerEdge*)0 )).first;
1662         if ( !(*n2e).second )
1663         {
1664           // add a _LayerEdge
1665           _LayerEdge* edge = new _LayerEdge();
1666           n2e->second = edge;
1667           edge->_nodes.push_back( n );
1668           const int   shapeID = n->getshapeId();
1669           const bool noShrink = data._noShrinkShapes.count( shapeID );
1670           edgesByGeom[ shapeID ].push_back( edge );
1671
1672           SMESH_TNodeXYZ xyz( n );
1673
1674           // set edge data or find already refined _LayerEdge and get data from it
1675           if (( !noShrink                                                     ) &&
1676               ( n->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE        ) &&
1677               (( s2ne = data._s2neMap.find( shapeID )) != data._s2neMap.end() ) &&
1678               (( n2e2 = (*s2ne).second->find( n )) != s2ne->second->end()     ))
1679           {
1680             _LayerEdge* foundEdge = (*n2e2).second;
1681             gp_XYZ        lastPos = edge->Copy( *foundEdge, helper );
1682             foundEdge->_pos.push_back( lastPos );
1683             // location of the last node is modified and we restore it by foundEdge->_pos.back()
1684             const_cast< SMDS_MeshNode* >
1685               ( edge->_nodes.back() )->setXYZ( xyz.X(), xyz.Y(), xyz.Z() );
1686           }
1687           else
1688           {
1689             if ( !noShrink )
1690             {
1691               edge->_nodes.push_back( helper.AddNode( xyz.X(), xyz.Y(), xyz.Z() ));
1692             }
1693             if ( !setEdgeData( *edge, subIds, helper, data ))
1694               return false;
1695           }
1696           dumpMove(edge->_nodes.back());
1697
1698           if ( edge->_cosin > faceMaxCosin )
1699           {
1700             faceMaxCosin = edge->_cosin;
1701             maxCosinEdge = edge;
1702           }
1703         }
1704         newNodes[ i ] = n2e->second->_nodes.back();
1705       }
1706       // create a temporary face
1707       const SMDS_MeshElement* newFace =
1708         new _TmpMeshFace( newNodes, --_tmpFaceID, face->getshapeId() );
1709       proxySub->AddElement( newFace );
1710
1711       // compute inflation step size by min size of element on a convex surface
1712       if ( faceMaxCosin > theMinSmoothCosin )
1713         limitStepSize( data, face, maxCosinEdge );
1714     } // loop on 2D elements on a FACE
1715   } // loop on FACEs of a SOLID
1716
1717   data._epsilon = 1e-7;
1718   if ( data._stepSize < 1. )
1719     data._epsilon *= data._stepSize;
1720
1721   // Put _LayerEdge's into the vector data._edges
1722   if ( !sortEdges( data, edgesByGeom ))
1723     return false;
1724
1725   // limit data._stepSize depending on surface curvature and fill data._convexFaces
1726   limitStepSizeByCurvature( data ); // !!! it must be before node substitution in _Simplex
1727
1728   // Set target nodes into _Simplex and _LayerEdge's to _2NearEdges
1729   TNode2Edge::iterator n2e;
1730   const SMDS_MeshNode* nn[2];
1731   for ( size_t i = 0; i < data._edges.size(); ++i )
1732   {
1733     if ( data._edges[i]->IsOnEdge() )
1734     {
1735       // get neighbor nodes
1736       bool hasData = ( data._edges[i]->_2neibors->_edges[0] );
1737       if ( hasData ) // _LayerEdge is a copy of another one
1738       {
1739         nn[0] = data._edges[i]->_2neibors->srcNode(0);
1740         nn[1] = data._edges[i]->_2neibors->srcNode(1);
1741       }
1742       else if ( !findNeiborsOnEdge( data._edges[i], nn[0],nn[1], data ))
1743       {
1744         return false;
1745       }
1746       // set neighbor _LayerEdge's
1747       for ( int j = 0; j < 2; ++j )
1748       {
1749         if (( n2e = data._n2eMap.find( nn[j] )) == data._n2eMap.end() )
1750           return error("_LayerEdge not found by src node", data._index);
1751         data._edges[i]->_2neibors->_edges[j] = n2e->second;
1752       }
1753       if ( !hasData )
1754         data._edges[i]->SetDataByNeighbors( nn[0], nn[1], helper);
1755     }
1756
1757     for ( size_t j = 0; j < data._edges[i]->_simplices.size(); ++j )
1758     {
1759       _Simplex& s = data._edges[i]->_simplices[j];
1760       s._nNext = data._n2eMap[ s._nNext ]->_nodes.back();
1761       s._nPrev = data._n2eMap[ s._nPrev ]->_nodes.back();
1762     }
1763   }
1764
1765   dumpFunctionEnd();
1766   return true;
1767 }
1768
1769 //================================================================================
1770 /*!
1771  * \brief Compute inflation step size by min size of element on a convex surface
1772  */
1773 //================================================================================
1774
1775 void _ViscousBuilder::limitStepSize( _SolidData&             data,
1776                                      const SMDS_MeshElement* face,
1777                                      const _LayerEdge*       maxCosinEdge )
1778 {
1779   int iN = 0;
1780   double minSize = 10 * data._stepSize;
1781   const int nbNodes = face->NbCornerNodes();
1782   for ( int i = 0; i < nbNodes; ++i )
1783   {
1784     const SMDS_MeshNode* nextN = face->GetNode( SMESH_MesherHelper::WrapIndex( i+1, nbNodes ));
1785     const SMDS_MeshNode*  curN = face->GetNode( i );
1786     if ( nextN->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ||
1787          curN-> GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
1788     {
1789       double dist = SMESH_TNodeXYZ( curN ).Distance( nextN );
1790       if ( dist < minSize )
1791         minSize = dist, iN = i;
1792     }
1793   }
1794   double newStep = 0.8 * minSize / maxCosinEdge->_lenFactor;
1795   if ( newStep < data._stepSize )
1796   {
1797     data._stepSize = newStep;
1798     data._stepSizeCoeff = 0.8 / maxCosinEdge->_lenFactor;
1799     data._stepSizeNodes[0] = face->GetNode( iN );
1800     data._stepSizeNodes[1] = face->GetNode( SMESH_MesherHelper::WrapIndex( iN+1, nbNodes ));
1801   }
1802 }
1803
1804 //================================================================================
1805 /*!
1806  * \brief Compute inflation step size by min size of element on a convex surface
1807  */
1808 //================================================================================
1809
1810 void _ViscousBuilder::limitStepSize( _SolidData& data, const double minSize )
1811 {
1812   if ( minSize < data._stepSize )
1813   {
1814     data._stepSize = minSize;
1815     if ( data._stepSizeNodes[0] )
1816     {
1817       double dist =
1818         SMESH_TNodeXYZ(data._stepSizeNodes[0]).Distance(data._stepSizeNodes[1]);
1819       data._stepSizeCoeff = data._stepSize / dist;
1820     }
1821   }
1822 }
1823
1824 //================================================================================
1825 /*!
1826  * \brief Limit data._stepSize by evaluating curvature of shapes and fill data._convexFaces
1827  */
1828 //================================================================================
1829
1830 void _ViscousBuilder::limitStepSizeByCurvature( _SolidData& data )
1831 {
1832   const int nbTestPnt = 5; // on a FACE sub-shape
1833   const double minCurvature = 0.9 / data._hyp->GetTotalThickness();
1834
1835   BRepLProp_SLProps surfProp( 2, 1e-6 );
1836   SMESH_MesherHelper helper( *_mesh );
1837
1838   data._convexFaces.clear();
1839
1840   TopExp_Explorer face( data._solid, TopAbs_FACE );
1841   for ( ; face.More(); face.Next() )
1842   {
1843     const TopoDS_Face& F = TopoDS::Face( face.Current() );
1844     SMESH_subMesh *   sm = _mesh->GetSubMesh( F );
1845     const TGeomID faceID = sm->GetId();
1846     if ( data._ignoreFaceIds.count( faceID )) continue;
1847
1848     BRepAdaptor_Surface surface( F, false );
1849     surfProp.SetSurface( surface );
1850
1851     bool isTooCurved = false;
1852     int iBeg, iEnd;
1853
1854     _ConvexFace cnvFace;
1855     const double        oriFactor = ( F.Orientation() == TopAbs_REVERSED ? +1. : -1. );
1856     SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true);
1857     while ( smIt->more() )
1858     {
1859       sm = smIt->next();
1860       const TGeomID subID = sm->GetId();
1861       // find _LayerEdge's of a sub-shape
1862       size_t edgesEnd;
1863       if ( data.GetShapeEdges( subID, edgesEnd, &iBeg, &iEnd ))
1864         cnvFace._subIdToEdgeEnd.insert( make_pair( subID, edgesEnd ));
1865       else
1866         continue;
1867       // check concavity and curvature and limit data._stepSize
1868       int nbLEdges = iEnd - iBeg;
1869       int iStep     = Max( 1, nbLEdges / nbTestPnt );
1870       for ( ; iBeg < iEnd; iBeg += iStep )
1871       {
1872         gp_XY uv = helper.GetNodeUV( F, data._edges[ iBeg ]->_nodes[0] );
1873         surfProp.SetParameters( uv.X(), uv.Y() );
1874         if ( !surfProp.IsCurvatureDefined() )
1875           continue;
1876         if ( surfProp.MaxCurvature() * oriFactor > minCurvature )
1877         {
1878           limitStepSize( data, 0.9 / surfProp.MaxCurvature() * oriFactor );
1879           isTooCurved = true;
1880         }
1881         if ( surfProp.MinCurvature() * oriFactor > minCurvature )
1882         {
1883           limitStepSize( data, 0.9 / surfProp.MinCurvature() * oriFactor );
1884           isTooCurved = true;
1885         }
1886       }
1887     } // loop on sub-shapes of the FACE
1888
1889     if ( !isTooCurved ) continue;
1890
1891     _ConvexFace & convFace =
1892       data._convexFaces.insert( make_pair( faceID, cnvFace )).first->second;
1893
1894     convFace._face = F;
1895     convFace._normalsFixed = false;
1896
1897     // Fill _ConvexFace::_simplexTestEdges. These _LayerEdge's are used to detect
1898     // prism distortion.
1899     map< TGeomID, int >::iterator id2end = convFace._subIdToEdgeEnd.find( faceID );
1900     if ( id2end != convFace._subIdToEdgeEnd.end() )
1901     {
1902       // there are _LayerEdge's on the FACE it-self;
1903       // select _LayerEdge's near EDGEs
1904       data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
1905       for ( ; iBeg < iEnd; ++iBeg )
1906       {
1907         _LayerEdge* ledge = data._edges[ iBeg ];
1908         for ( size_t j = 0; j < ledge->_simplices.size(); ++j )
1909           if ( ledge->_simplices[j]._nNext->GetPosition()->GetDim() < 2 )
1910           {
1911             convFace._simplexTestEdges.push_back( ledge );
1912             break;
1913           }
1914       }
1915     }
1916     else
1917     {
1918       // where there are no _LayerEdge's on a _ConvexFace,
1919       // as e.g. on a fillet surface with no internal nodes - issue 22580,
1920       // so that collision of viscous internal faces is not detected by check of
1921       // intersection of _LayerEdge's with the viscous internal faces.
1922
1923       set< const SMDS_MeshNode* > usedNodes;
1924
1925       // look for _LayerEdge's with null _sWOL
1926       map< TGeomID, int >::iterator id2end = convFace._subIdToEdgeEnd.begin();
1927       for ( ; id2end != convFace._subIdToEdgeEnd.end(); ++id2end )
1928       {
1929         data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
1930         if ( iBeg >= iEnd || !data._edges[ iBeg ]->_sWOL.IsNull() )
1931           continue;
1932         for ( ; iBeg < iEnd; ++iBeg )
1933         {
1934           _LayerEdge* ledge = data._edges[ iBeg ];
1935           const SMDS_MeshNode* srcNode = ledge->_nodes[0];
1936           if ( !usedNodes.insert( srcNode ).second ) continue;
1937
1938           getSimplices( srcNode, ledge->_simplices, data._ignoreFaceIds, &data );
1939           for ( size_t i = 0; i < ledge->_simplices.size(); ++i )
1940           {
1941             usedNodes.insert( ledge->_simplices[i]._nPrev );
1942             usedNodes.insert( ledge->_simplices[i]._nNext );
1943           }
1944           convFace._simplexTestEdges.push_back( ledge );
1945         }
1946       }
1947     }
1948   } // loop on FACEs of data._solid
1949 }
1950
1951 //================================================================================
1952 /*!
1953  * \brief Separate shapes (and _LayerEdge's on them) to smooth from the rest ones
1954  */
1955 //================================================================================
1956
1957 bool _ViscousBuilder::sortEdges( _SolidData&                    data,
1958                                  vector< vector<_LayerEdge*> >& edgesByGeom)
1959 {
1960   // Find shapes needing smoothing; such a shape has _LayerEdge._normal on it's
1961   // boundry inclined at a sharp angle to the shape
1962
1963   list< TGeomID > shapesToSmooth;
1964   
1965   SMESH_MesherHelper helper( *_mesh );
1966   bool ok = true;
1967
1968   for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS )
1969   {
1970     vector<_LayerEdge*>& eS = edgesByGeom[iS];
1971     if ( eS.empty() ) continue;
1972     const TopoDS_Shape& S = getMeshDS()->IndexToShape( iS );
1973     bool needSmooth = false;
1974     switch ( S.ShapeType() )
1975     {
1976     case TopAbs_EDGE: {
1977
1978       bool isShrinkEdge = !eS[0]->_sWOL.IsNull();
1979       for ( TopoDS_Iterator vIt( S ); vIt.More() && !needSmooth; vIt.Next() )
1980       {
1981         TGeomID iV = getMeshDS()->ShapeToIndex( vIt.Value() );
1982         vector<_LayerEdge*>& eV = edgesByGeom[ iV ];
1983         if ( eV.empty() ) continue;
1984         // double cosin = eV[0]->_cosin;
1985         // bool badCosin =
1986         //   ( !eV[0]->_sWOL.IsNull() && ( eV[0]->_sWOL.ShapeType() == TopAbs_EDGE || !isShrinkEdge));
1987         // if ( badCosin )
1988         // {
1989         //   gp_Vec dir1, dir2;
1990         //   if ( eV[0]->_sWOL.ShapeType() == TopAbs_EDGE )
1991         //     dir1 = getEdgeDir( TopoDS::Edge( eV[0]->_sWOL ), TopoDS::Vertex( vIt.Value() ));
1992         //   else
1993         //     dir1 = getFaceDir( TopoDS::Face( eV[0]->_sWOL ), TopoDS::Vertex( vIt.Value() ),
1994         //                        eV[0]->_nodes[0], helper, ok);
1995         //   dir2 = getEdgeDir( TopoDS::Edge( S ), TopoDS::Vertex( vIt.Value() ));
1996         //   double angle = dir1.Angle( dir2 );
1997         //   cosin = cos( angle );
1998         // }
1999         gp_Vec eDir = getEdgeDir( TopoDS::Edge( S ), TopoDS::Vertex( vIt.Value() ));
2000         double angle = eDir.Angle( eV[0]->_normal );
2001         double cosin = Cos( angle );
2002         needSmooth = ( cosin > theMinSmoothCosin );
2003       }
2004       break;
2005     }
2006     case TopAbs_FACE: {
2007
2008       for ( TopExp_Explorer eExp( S, TopAbs_EDGE ); eExp.More() && !needSmooth; eExp.Next() )
2009       {
2010         TGeomID iE = getMeshDS()->ShapeToIndex( eExp.Current() );
2011         vector<_LayerEdge*>& eE = edgesByGeom[ iE ];
2012         if ( eE.empty() ) continue;
2013         if ( eE[0]->_sWOL.IsNull() )
2014         {
2015           for ( size_t i = 0; i < eE.size() && !needSmooth; ++i )
2016             needSmooth = ( eE[i]->_cosin > theMinSmoothCosin );
2017         }
2018         else
2019         {
2020           const TopoDS_Face& F1 = TopoDS::Face( S );
2021           const TopoDS_Face& F2 = TopoDS::Face( eE[0]->_sWOL );
2022           const TopoDS_Edge& E  = TopoDS::Edge( eExp.Current() );
2023           for ( size_t i = 0; i < eE.size() && !needSmooth; ++i )
2024           {
2025             gp_Vec dir1 = getFaceDir( F1, E, eE[i]->_nodes[0], helper, ok );
2026             gp_Vec dir2 = getFaceDir( F2, E, eE[i]->_nodes[0], helper, ok );
2027             double angle = dir1.Angle( dir2 );
2028             double cosin = cos( angle );
2029             needSmooth = ( cosin > theMinSmoothCosin );
2030           }
2031         }
2032       }
2033       break;
2034     }
2035     case TopAbs_VERTEX:
2036       continue;
2037     default:;
2038     }
2039     if ( needSmooth )
2040     {
2041       if ( S.ShapeType() == TopAbs_EDGE ) shapesToSmooth.push_front( iS );
2042       else                                shapesToSmooth.push_back ( iS );
2043     }
2044
2045   } // loop on edgesByGeom
2046
2047   data._edges.reserve( data._n2eMap.size() );
2048   data._endEdgeOnShape.clear();
2049
2050   // first we put _LayerEdge's on shapes to smooth
2051   data._nbShapesToSmooth = 0;
2052   list< TGeomID >::iterator gIt = shapesToSmooth.begin();
2053   for ( ; gIt != shapesToSmooth.end(); ++gIt )
2054   {
2055     vector<_LayerEdge*>& eVec = edgesByGeom[ *gIt ];
2056     if ( eVec.empty() ) continue;
2057     data._edges.insert( data._edges.end(), eVec.begin(), eVec.end() );
2058     data._endEdgeOnShape.push_back( data._edges.size() );
2059     data._nbShapesToSmooth++;
2060     eVec.clear();
2061   }
2062
2063   // then the rest _LayerEdge's
2064   for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS )
2065   {
2066     vector<_LayerEdge*>& eVec = edgesByGeom[iS];
2067     if ( eVec.empty() ) continue;
2068     data._edges.insert( data._edges.end(), eVec.begin(), eVec.end() );
2069     data._endEdgeOnShape.push_back( data._edges.size() );
2070     //eVec.clear();
2071   }
2072
2073   return ok;
2074 }
2075
2076 //================================================================================
2077 /*!
2078  * \brief Set data of _LayerEdge needed for smoothing
2079  *  \param subIds - ids of sub-shapes of a SOLID to take into account faces from
2080  */
2081 //================================================================================
2082
2083 bool _ViscousBuilder::setEdgeData(_LayerEdge&         edge,
2084                                   const set<TGeomID>& subIds,
2085                                   SMESH_MesherHelper& helper,
2086                                   _SolidData&         data)
2087 {
2088   SMESH_MeshEditor editor(_mesh);
2089
2090   const SMDS_MeshNode* node = edge._nodes[0]; // source node
2091   SMDS_TypeOfPosition posType = node->GetPosition()->GetTypeOfPosition();
2092
2093   edge._len       = 0;
2094   edge._2neibors  = 0;
2095   edge._curvature = 0;
2096
2097   // --------------------------
2098   // Compute _normal and _cosin
2099   // --------------------------
2100
2101   edge._cosin = 0;
2102   edge._normal.SetCoord(0,0,0);
2103
2104   int totalNbFaces = 0;
2105   gp_Vec geomNorm;
2106   bool normOK = true;
2107
2108   const TGeomID shapeInd = node->getshapeId();
2109   map< TGeomID, TopoDS_Shape >::const_iterator s2s = data._shrinkShape2Shape.find( shapeInd );
2110   const bool onShrinkShape ( s2s != data._shrinkShape2Shape.end() );
2111
2112   if ( onShrinkShape ) // one of faces the node is on has no layers
2113   {
2114     TopoDS_Shape vertEdge = getMeshDS()->IndexToShape( s2s->first ); // vertex or edge
2115     if ( s2s->second.ShapeType() == TopAbs_EDGE )
2116     {
2117       // inflate from VERTEX along EDGE
2118       edge._normal = getEdgeDir( TopoDS::Edge( s2s->second ), TopoDS::Vertex( vertEdge ));
2119     }
2120     else if ( vertEdge.ShapeType() == TopAbs_VERTEX )
2121     {
2122       // inflate from VERTEX along FACE
2123       edge._normal = getFaceDir( TopoDS::Face( s2s->second ), TopoDS::Vertex( vertEdge ),
2124                                  node, helper, normOK, &edge._cosin);
2125     }
2126     else
2127     {
2128       // inflate from EDGE along FACE
2129       edge._normal = getFaceDir( TopoDS::Face( s2s->second ), TopoDS::Edge( vertEdge ),
2130                                  node, helper, normOK);
2131     }
2132   }
2133   else // layers are on all faces of SOLID the node is on
2134   {
2135     // find indices of geom faces the node lies on
2136     set<TGeomID> faceIds;
2137     if  ( posType == SMDS_TOP_FACE )
2138     {
2139       faceIds.insert( node->getshapeId() );
2140     }
2141     else
2142     {
2143       SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
2144       while ( fIt->more() )
2145         faceIds.insert( editor.FindShape(fIt->next()));
2146     }
2147
2148     set<TGeomID>::iterator id = faceIds.begin();
2149     TopoDS_Face F;
2150     std::pair< TGeomID, gp_XYZ > id2Norm[20];
2151     for ( ; id != faceIds.end(); ++id )
2152     {
2153       const TopoDS_Shape& s = getMeshDS()->IndexToShape( *id );
2154       if ( s.IsNull() || s.ShapeType() != TopAbs_FACE || !subIds.count( *id ))
2155         continue;
2156       F = TopoDS::Face( s );
2157       geomNorm = getFaceNormal( node, F, helper, normOK );
2158       if ( !normOK ) continue;
2159
2160       if ( helper.GetSubShapeOri( data._solid, F ) != TopAbs_REVERSED )
2161         geomNorm.Reverse();
2162       id2Norm[ totalNbFaces ].first  = *id;
2163       id2Norm[ totalNbFaces ].second = geomNorm.XYZ();
2164       totalNbFaces++;
2165       edge._normal += geomNorm.XYZ();
2166     }
2167     if ( totalNbFaces == 0 )
2168       return error(SMESH_Comment("Can't get normal to node ") << node->GetID(), data._index);
2169
2170     if ( normOK && edge._normal.Modulus() < 1e-3 && totalNbFaces > 1 )
2171     {
2172       // opposite normals, re-get normals at shifted positions (IPAL 52426)
2173       edge._normal.SetCoord( 0,0,0 );
2174       for ( int i = 0; i < totalNbFaces; ++i )
2175       {
2176         const TopoDS_Face& F = TopoDS::Face( getMeshDS()->IndexToShape( id2Norm[i].first ));
2177         geomNorm = getFaceNormal( node, F, helper, normOK, /*shiftInside=*/true );
2178         if ( helper.GetSubShapeOri( data._solid, F ) != TopAbs_REVERSED )
2179           geomNorm.Reverse();
2180         if ( normOK )
2181           id2Norm[ i ].second = geomNorm.XYZ();
2182         edge._normal += id2Norm[ i ].second;
2183       }
2184     }
2185
2186     if ( totalNbFaces < 3 )
2187     {
2188       //edge._normal /= totalNbFaces;
2189     }
2190     else
2191     {
2192       edge._normal = getWeigthedNormal( node, id2Norm, totalNbFaces );
2193     }
2194
2195     switch ( posType )
2196     {
2197     case SMDS_TOP_FACE:
2198       edge._cosin = 0; break;
2199
2200     case SMDS_TOP_EDGE: {
2201       TopoDS_Edge E = TopoDS::Edge( helper.GetSubShapeByNode( node, getMeshDS()));
2202       gp_Vec inFaceDir = getFaceDir( F, E, node, helper, normOK );
2203       double angle = inFaceDir.Angle( edge._normal ); // [0,PI]
2204       edge._cosin = cos( angle );
2205       //cout << "Cosin on EDGE " << edge._cosin << " node " << node->GetID() << endl;
2206       break;
2207     }
2208     case SMDS_TOP_VERTEX: {
2209       TopoDS_Vertex V = TopoDS::Vertex( helper.GetSubShapeByNode( node, getMeshDS()));
2210       gp_Vec inFaceDir = getFaceDir( F, V, node, helper, normOK );
2211       double angle = inFaceDir.Angle( edge._normal ); // [0,PI]
2212       edge._cosin = cos( angle );
2213       //cout << "Cosin on VERTEX " << edge._cosin << " node " << node->GetID() << endl;
2214       break;
2215     }
2216     default:
2217       return error(SMESH_Comment("Invalid shape position of node ")<<node, data._index);
2218     }
2219   } // case _sWOL.IsNull()
2220
2221   double normSize = edge._normal.SquareModulus();
2222   if ( normSize < numeric_limits<double>::min() )
2223     return error(SMESH_Comment("Bad normal at node ")<< node->GetID(), data._index );
2224
2225   edge._normal /= sqrt( normSize );
2226
2227   // TODO: if ( !normOK ) then get normal by mesh faces
2228
2229   // Set the rest data
2230   // --------------------
2231   if ( onShrinkShape )
2232   {
2233     edge._sWOL = (*s2s).second;
2234
2235     SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( edge._nodes.back() );
2236     if ( SMESHDS_SubMesh* sm = getMeshDS()->MeshElements( data._solid ))
2237       sm->RemoveNode( tgtNode , /*isNodeDeleted=*/false );
2238
2239     // set initial position which is parameters on _sWOL in this case
2240     if ( edge._sWOL.ShapeType() == TopAbs_EDGE )
2241     {
2242       double u = helper.GetNodeU( TopoDS::Edge( edge._sWOL ), node, 0, &normOK );
2243       edge._pos.push_back( gp_XYZ( u, 0, 0 ));
2244       if ( edge._nodes.size() > 1 )
2245         getMeshDS()->SetNodeOnEdge( tgtNode, TopoDS::Edge( edge._sWOL ), u );
2246     }
2247     else // TopAbs_FACE
2248     {
2249       gp_XY uv = helper.GetNodeUV( TopoDS::Face( edge._sWOL ), node, 0, &normOK );
2250       edge._pos.push_back( gp_XYZ( uv.X(), uv.Y(), 0));
2251       if ( edge._nodes.size() > 1 )
2252         getMeshDS()->SetNodeOnFace( tgtNode, TopoDS::Face( edge._sWOL ), uv.X(), uv.Y() );
2253     }
2254   }
2255   else
2256   {
2257     edge._pos.push_back( SMESH_TNodeXYZ( node ));
2258
2259     if ( posType == SMDS_TOP_FACE )
2260     {
2261       getSimplices( node, edge._simplices, data._ignoreFaceIds, &data );
2262       double avgNormProj = 0, avgLen = 0;
2263       for ( size_t i = 0; i < edge._simplices.size(); ++i )
2264       {
2265         gp_XYZ vec = edge._pos.back() - SMESH_TNodeXYZ( edge._simplices[i]._nPrev );
2266         avgNormProj += edge._normal * vec;
2267         avgLen += vec.Modulus();
2268       }
2269       avgNormProj /= edge._simplices.size();
2270       avgLen /= edge._simplices.size();
2271       edge._curvature = _Curvature::New( avgNormProj, avgLen );
2272     }
2273   }
2274
2275   // Set neighbour nodes for a _LayerEdge based on EDGE
2276
2277   if ( posType == SMDS_TOP_EDGE /*||
2278        ( onShrinkShape && posType == SMDS_TOP_VERTEX && fabs( edge._cosin ) < 1e-10 )*/)
2279   {
2280     edge._2neibors = new _2NearEdges;
2281     // target node instead of source ones will be set later
2282     // if ( ! findNeiborsOnEdge( &edge,
2283     //                           edge._2neibors->_nodes[0],
2284     //                           edge._2neibors->_nodes[1],
2285     //                           data))
2286     //   return false;
2287     // edge.SetDataByNeighbors( edge._2neibors->_nodes[0],
2288     //                          edge._2neibors->_nodes[1],
2289     //                          helper);
2290   }
2291
2292   edge.SetCosin( edge._cosin ); // to update edge._lenFactor
2293
2294   return true;
2295 }
2296
2297 //================================================================================
2298 /*!
2299  * \brief Return normal to a FACE at a node
2300  *  \param [in] n - node
2301  *  \param [in] face - FACE
2302  *  \param [in] helper - helper
2303  *  \param [out] isOK - true or false
2304  *  \param [in] shiftInside - to find normal at a position shifted inside the face
2305  *  \return gp_XYZ - normal
2306  */
2307 //================================================================================
2308
2309 gp_XYZ _ViscousBuilder::getFaceNormal(const SMDS_MeshNode* node,
2310                                       const TopoDS_Face&   face,
2311                                       SMESH_MesherHelper&  helper,
2312                                       bool&                isOK,
2313                                       bool                 shiftInside)
2314 {
2315   gp_XY uv;
2316   if ( shiftInside )
2317   {
2318     // get a shifted position
2319     gp_Pnt p = SMESH_TNodeXYZ( node );
2320     gp_XYZ shift( 0,0,0 );
2321     TopoDS_Shape S = helper.GetSubShapeByNode( node, helper.GetMeshDS() );
2322     switch ( S.ShapeType() ) {
2323     case TopAbs_VERTEX:
2324     {
2325       shift = getFaceDir( face, TopoDS::Vertex( S ), node, helper, isOK );
2326       break;
2327     }
2328     case TopAbs_EDGE:
2329     {
2330       shift = getFaceDir( face, TopoDS::Edge( S ), node, helper, isOK );
2331       break;
2332     }
2333     default:
2334       isOK = false;
2335     }
2336     if ( isOK )
2337       shift.Normalize();
2338     p.Translate( shift * 1e-5 );
2339
2340     TopLoc_Location loc;
2341     GeomAPI_ProjectPointOnSurf& projector = helper.GetProjector( face, loc, 1e-7 );
2342
2343     if ( !loc.IsIdentity() ) p.Transform( loc.Transformation().Inverted() );
2344     
2345     projector.Perform( p );
2346     if ( !projector.IsDone() || projector.NbPoints() < 1 )
2347     {
2348       isOK = false;
2349       return p.XYZ();
2350     }
2351     Quantity_Parameter U,V;
2352     projector.LowerDistanceParameters(U,V);
2353     uv.SetCoord( U,V );
2354   }
2355   else
2356   {
2357     uv = helper.GetNodeUV( face, node, 0, &isOK );
2358   }
2359
2360   gp_Dir normal;
2361   isOK = false;
2362
2363   Handle(Geom_Surface) surface = BRep_Tool::Surface( face );
2364   int pointKind = GeomLib::NormEstim( surface, uv, 1e-5, normal );
2365   enum { REGULAR = 0, QUASYSINGULAR, CONICAL, IMPOSSIBLE };
2366
2367   if ( pointKind == IMPOSSIBLE &&
2368        node->GetPosition()->GetDim() == 2 ) // node inside the FACE
2369   {
2370     // probably NormEstim() failed due to a too high tolerance
2371     pointKind = GeomLib::NormEstim( surface, uv, 1e-20, normal );
2372     isOK = ( pointKind < IMPOSSIBLE );
2373   }
2374   if ( pointKind < IMPOSSIBLE )
2375   {
2376     if ( pointKind != REGULAR &&
2377          !shiftInside &&
2378          node->GetPosition()->GetDim() < 2 ) // FACE boundary
2379     {
2380       gp_XYZ normShift = getFaceNormal( node, face, helper, isOK, /*shiftInside=*/true );
2381       if ( normShift * normal.XYZ() < 0. )
2382         normal = normShift;
2383     }
2384     isOK = true;
2385   }
2386
2387   if ( !isOK ) // hard singularity, to call with shiftInside=true ?
2388   {
2389     const TGeomID faceID = helper.GetMeshDS()->ShapeToIndex( face );
2390
2391     SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
2392     while ( fIt->more() )
2393     {
2394       const SMDS_MeshElement* f = fIt->next();
2395       if ( f->getshapeId() == faceID )
2396       {
2397         isOK = SMESH_MeshAlgos::FaceNormal( f, (gp_XYZ&) normal.XYZ(), /*normalized=*/true );
2398         if ( isOK )
2399         {
2400           TopoDS_Face ff = face;
2401           ff.Orientation( TopAbs_FORWARD );
2402           if ( helper.IsReversedSubMesh( ff ))
2403             normal.Reverse();
2404           break;
2405         }
2406       }
2407     }
2408   }
2409   return normal.XYZ();
2410 }
2411
2412 //================================================================================
2413 /*!
2414  * \brief Return a normal at a node weighted with angles taken by FACEs
2415  *  \param [in] n - the node
2416  *  \param [in] fId2Normal - FACE ids and normals
2417  *  \param [in] nbFaces - nb of FACEs meeting at the node
2418  *  \return gp_XYZ - computed normal
2419  */
2420 //================================================================================
2421
2422 gp_XYZ _ViscousBuilder::getWeigthedNormal( const SMDS_MeshNode*         n,
2423                                            std::pair< TGeomID, gp_XYZ > fId2Normal[],
2424                                            const int                    nbFaces )
2425 {
2426   gp_XYZ resNorm(0,0,0);
2427   TopoDS_Shape V = SMESH_MesherHelper::GetSubShapeByNode( n, getMeshDS() );
2428   if ( V.ShapeType() != TopAbs_VERTEX )
2429   {
2430     for ( int i = 0; i < nbFaces; ++i )
2431       resNorm += fId2Normal[i].second / nbFaces ;
2432     return resNorm;
2433   }
2434
2435   double angles[30];
2436   for ( int i = 0; i < nbFaces; ++i )
2437   {
2438     const TopoDS_Face& F = TopoDS::Face( getMeshDS()->IndexToShape( fId2Normal[i].first ));
2439
2440     // look for two EDGEs shared by F and other FACEs within fId2Normal
2441     TopoDS_Edge ee[2];
2442     int nbE = 0;
2443     PShapeIteratorPtr eIt = SMESH_MesherHelper::GetAncestors( V, *_mesh, TopAbs_EDGE );
2444     while ( const TopoDS_Shape* E = eIt->next() )
2445     {
2446       if ( !SMESH_MesherHelper::IsSubShape( *E, F ))
2447         continue;
2448       bool isSharedEdge = false;
2449       for ( int j = 0; j < nbFaces && !isSharedEdge; ++j )
2450       {
2451         if ( i == j ) continue;
2452         const TopoDS_Shape& otherF = getMeshDS()->IndexToShape( fId2Normal[j].first );
2453         isSharedEdge = SMESH_MesherHelper::IsSubShape( *E, otherF );
2454       }
2455       if ( !isSharedEdge )
2456         continue;
2457       ee[ nbE ] = TopoDS::Edge( *E );
2458       ee[ nbE ].Orientation( SMESH_MesherHelper::GetSubShapeOri( F, *E ));
2459       if ( ++nbE == 2 )
2460         break;
2461     }
2462
2463     // get an angle between the two EDGEs
2464     angles[i] = 0;
2465     if ( nbE < 1 ) continue;
2466     if ( nbE == 1 )
2467     {
2468       ee[ 1 ] == ee[ 0 ];
2469     }
2470     else
2471     {
2472       if ( !V.IsSame( SMESH_MesherHelper::IthVertex( 0, ee[ 1 ] )))
2473         std::swap( ee[0], ee[1] );
2474     }
2475     angles[i] = SMESH_MesherHelper::GetAngle( ee[0], ee[1], F, TopoDS::Vertex( V ));
2476   }
2477
2478   // compute a weighted normal
2479   double sumAngle = 0;
2480   for ( int i = 0; i < nbFaces; ++i )
2481   {
2482     angles[i] = ( angles[i] > 2*M_PI )  ?  0  :  M_PI - angles[i];
2483     sumAngle += angles[i];
2484   }
2485   for ( int i = 0; i < nbFaces; ++i )
2486     resNorm += angles[i] / sumAngle * fId2Normal[i].second;
2487
2488   return resNorm;
2489 }
2490
2491 //================================================================================
2492 /*!
2493  * \brief Find 2 neigbor nodes of a node on EDGE
2494  */
2495 //================================================================================
2496
2497 bool _ViscousBuilder::findNeiborsOnEdge(const _LayerEdge*     edge,
2498                                         const SMDS_MeshNode*& n1,
2499                                         const SMDS_MeshNode*& n2,
2500                                         _SolidData&           data)
2501 {
2502   const SMDS_MeshNode* node = edge->_nodes[0];
2503   const int        shapeInd = node->getshapeId();
2504   SMESHDS_SubMesh*   edgeSM = 0;
2505   if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_EDGE )
2506   {
2507     edgeSM = getMeshDS()->MeshElements( shapeInd );
2508     if ( !edgeSM || edgeSM->NbElements() == 0 )
2509       return error(SMESH_Comment("Not meshed EDGE ") << shapeInd, data._index);
2510   }
2511   int iN = 0;
2512   n2 = 0;
2513   SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Edge);
2514   while ( eIt->more() && !n2 )
2515   {
2516     const SMDS_MeshElement* e = eIt->next();
2517     const SMDS_MeshNode*   nNeibor = e->GetNode( 0 );
2518     if ( nNeibor == node ) nNeibor = e->GetNode( 1 );
2519     if ( edgeSM )
2520     {
2521       if (!edgeSM->Contains(e)) continue;
2522     }
2523     else
2524     {
2525       TopoDS_Shape s = SMESH_MesherHelper::GetSubShapeByNode(nNeibor, getMeshDS() );
2526       if ( !SMESH_MesherHelper::IsSubShape( s, edge->_sWOL )) continue;
2527     }
2528     ( iN++ ? n2 : n1 ) = nNeibor;
2529   }
2530   if ( !n2 )
2531     return error(SMESH_Comment("Wrongly meshed EDGE ") << shapeInd, data._index);
2532   return true;
2533 }
2534
2535 //================================================================================
2536 /*!
2537  * \brief Set _curvature and _2neibors->_plnNorm by 2 neigbor nodes residing the same EDGE
2538  */
2539 //================================================================================
2540
2541 void _LayerEdge::SetDataByNeighbors( const SMDS_MeshNode* n1,
2542                                      const SMDS_MeshNode* n2,
2543                                      SMESH_MesherHelper&  helper)
2544 {
2545   if ( _nodes[0]->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
2546     return;
2547
2548   gp_XYZ pos = SMESH_TNodeXYZ( _nodes[0] );
2549   gp_XYZ vec1 = pos - SMESH_TNodeXYZ( n1 );
2550   gp_XYZ vec2 = pos - SMESH_TNodeXYZ( n2 );
2551
2552   // Set _curvature
2553
2554   double sumLen = vec1.Modulus() + vec2.Modulus();
2555   _2neibors->_wgt[0] = 1 - vec1.Modulus() / sumLen;
2556   _2neibors->_wgt[1] = 1 - vec2.Modulus() / sumLen;
2557   double avgNormProj = 0.5 * ( _normal * vec1 + _normal * vec2 );
2558   double avgLen = 0.5 * ( vec1.Modulus() + vec2.Modulus() );
2559   if ( _curvature ) delete _curvature;
2560   _curvature = _Curvature::New( avgNormProj, avgLen );
2561   // if ( _curvature )
2562   //   debugMsg( _nodes[0]->GetID()
2563   //             << " CURV r,k: " << _curvature->_r<<","<<_curvature->_k
2564   //             << " proj = "<<avgNormProj<< " len = " << avgLen << "| lenDelta(0) = "
2565   //             << _curvature->lenDelta(0) );
2566
2567   // Set _plnNorm
2568
2569   if ( _sWOL.IsNull() )
2570   {
2571     TopoDS_Shape S = helper.GetSubShapeByNode( _nodes[0], helper.GetMeshDS() );
2572     gp_XYZ dirE = getEdgeDir( TopoDS::Edge( S ), _nodes[0], helper );
2573     gp_XYZ plnNorm = dirE ^ _normal;
2574     double proj0 = plnNorm * vec1;
2575     double proj1 = plnNorm * vec2;
2576     if ( fabs( proj0 ) > 1e-10 || fabs( proj1 ) > 1e-10 )
2577     {
2578       if ( _2neibors->_plnNorm ) delete _2neibors->_plnNorm;
2579       _2neibors->_plnNorm = new gp_XYZ( plnNorm.Normalized() );
2580     }
2581   }
2582 }
2583
2584 //================================================================================
2585 /*!
2586  * \brief Copy data from a _LayerEdge of other SOLID and based on the same node;
2587  * this and other _LayerEdge's are inflated along a FACE or an EDGE
2588  */
2589 //================================================================================
2590
2591 gp_XYZ _LayerEdge::Copy( _LayerEdge& other, SMESH_MesherHelper& helper )
2592 {
2593   _nodes     = other._nodes;
2594   _normal    = other._normal;
2595   _len       = 0;
2596   _lenFactor = other._lenFactor;
2597   _cosin     = other._cosin;
2598   _sWOL      = other._sWOL;
2599   _2neibors  = other._2neibors;
2600   _curvature = 0; std::swap( _curvature, other._curvature );
2601   _2neibors  = 0; std::swap( _2neibors,  other._2neibors );
2602
2603   gp_XYZ lastPos( 0,0,0 );
2604   if ( _sWOL.ShapeType() == TopAbs_EDGE )
2605   {
2606     double u = helper.GetNodeU( TopoDS::Edge( _sWOL ), _nodes[0] );
2607     _pos.push_back( gp_XYZ( u, 0, 0));
2608
2609     u = helper.GetNodeU( TopoDS::Edge( _sWOL ), _nodes.back() );
2610     lastPos.SetX( u );
2611   }
2612   else // TopAbs_FACE
2613   {
2614     gp_XY uv = helper.GetNodeUV( TopoDS::Face( _sWOL ), _nodes[0]);
2615     _pos.push_back( gp_XYZ( uv.X(), uv.Y(), 0));
2616
2617     uv = helper.GetNodeUV( TopoDS::Face( _sWOL ), _nodes.back() );
2618     lastPos.SetX( uv.X() );
2619     lastPos.SetY( uv.Y() );
2620   }
2621   return lastPos;
2622 }
2623
2624 //================================================================================
2625 /*!
2626  * \brief Set _cosin and _lenFactor
2627  */
2628 //================================================================================
2629
2630 void _LayerEdge::SetCosin( double cosin )
2631 {
2632   _cosin = cosin;
2633   cosin = Abs( _cosin );
2634   _lenFactor = ( /*0.1 < cosin &&*/ cosin < 1-1e-12 ) ?  1./sqrt(1-cosin*cosin) : 1.0;
2635 }
2636
2637 //================================================================================
2638 /*!
2639  * \brief Fills a vector<_Simplex > 
2640  */
2641 //================================================================================
2642
2643 void _ViscousBuilder::getSimplices( const SMDS_MeshNode* node,
2644                                     vector<_Simplex>&    simplices,
2645                                     const set<TGeomID>&  ingnoreShapes,
2646                                     const _SolidData*    dataToCheckOri,
2647                                     const bool           toSort)
2648 {
2649   simplices.clear();
2650   SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
2651   while ( fIt->more() )
2652   {
2653     const SMDS_MeshElement* f = fIt->next();
2654     const TGeomID    shapeInd = f->getshapeId();
2655     if ( ingnoreShapes.count( shapeInd )) continue;
2656     const int nbNodes = f->NbCornerNodes();
2657     const int  srcInd = f->GetNodeIndex( node );
2658     const SMDS_MeshNode* nPrev = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd-1, nbNodes ));
2659     const SMDS_MeshNode* nNext = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd+1, nbNodes ));
2660     const SMDS_MeshNode* nOpp  = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd+2, nbNodes ));
2661     if ( dataToCheckOri && dataToCheckOri->_reversedFaceIds.count( shapeInd ))
2662       std::swap( nPrev, nNext );
2663     simplices.push_back( _Simplex( nPrev, nNext, nOpp ));
2664   }
2665
2666   if ( toSort )
2667   {
2668     vector<_Simplex> sortedSimplices( simplices.size() );
2669     sortedSimplices[0] = simplices[0];
2670     int nbFound = 0;
2671     for ( size_t i = 1; i < simplices.size(); ++i )
2672     {
2673       for ( size_t j = 1; j < simplices.size(); ++j )
2674         if ( sortedSimplices[i-1]._nNext == simplices[j]._nPrev )
2675         {
2676           sortedSimplices[i] = simplices[j];
2677           nbFound++;
2678           break;
2679         }
2680     }
2681     if ( nbFound == simplices.size() - 1 )
2682       simplices.swap( sortedSimplices );
2683   }
2684 }
2685
2686 //================================================================================
2687 /*!
2688  * \brief DEBUG. Create groups contating temorary data of _LayerEdge's
2689  */
2690 //================================================================================
2691
2692 void _ViscousBuilder::makeGroupOfLE()
2693 {
2694 #ifdef _DEBUG_
2695   for ( size_t i = 0 ; i < _sdVec.size(); ++i )
2696   {
2697     if ( _sdVec[i]._edges.empty() ) continue;
2698
2699     dumpFunction( SMESH_Comment("make_LayerEdge_") << i );
2700     for ( size_t j = 0 ; j < _sdVec[i]._edges.size(); ++j )
2701     {
2702       _LayerEdge* le = _sdVec[i]._edges[j];
2703       for ( size_t iN = 1; iN < le->_nodes.size(); ++iN )
2704         dumpCmd(SMESH_Comment("mesh.AddEdge([ ") <<le->_nodes[iN-1]->GetID()
2705                 << ", " << le->_nodes[iN]->GetID() <<"])");
2706     }
2707     dumpFunctionEnd();
2708
2709     dumpFunction( SMESH_Comment("makeNormals") << i );
2710     for ( size_t j = 0 ; j < _sdVec[i]._edges.size(); ++j )
2711     {
2712       _LayerEdge& edge = *_sdVec[i]._edges[j];
2713       SMESH_TNodeXYZ nXYZ( edge._nodes[0] );
2714       nXYZ += edge._normal * _sdVec[i]._stepSize;
2715       dumpCmd(SMESH_Comment("mesh.AddEdge([ ") <<edge._nodes[0]->GetID()
2716               << ", mesh.AddNode( " << nXYZ.X()<<","<< nXYZ.Y()<<","<< nXYZ.Z()<<")])");
2717     }
2718     dumpFunctionEnd();
2719
2720     dumpFunction( SMESH_Comment("makeTmpFaces_") << i );
2721     TopExp_Explorer fExp( _sdVec[i]._solid, TopAbs_FACE );
2722     for ( ; fExp.More(); fExp.Next() )
2723     {
2724       if (const SMESHDS_SubMesh* sm = _sdVec[i]._proxyMesh->GetProxySubMesh( fExp.Current()))
2725       {
2726         SMDS_ElemIteratorPtr fIt = sm->GetElements();
2727         while ( fIt->more())
2728         {
2729           const SMDS_MeshElement* e = fIt->next();
2730           SMESH_Comment cmd("mesh.AddFace([");
2731           for ( int j=0; j < e->NbCornerNodes(); ++j )
2732             cmd << e->GetNode(j)->GetID() << (j+1<e->NbCornerNodes() ? ",": "])");
2733           dumpCmd( cmd );
2734         }
2735       }
2736     }
2737     dumpFunctionEnd();
2738   }
2739 #endif
2740 }
2741
2742 //================================================================================
2743 /*!
2744  * \brief Increase length of _LayerEdge's to reach the required thickness of layers
2745  */
2746 //================================================================================
2747
2748 bool _ViscousBuilder::inflate(_SolidData& data)
2749 {
2750   SMESH_MesherHelper helper( *_mesh );
2751
2752   // Limit inflation step size by geometry size found by itersecting
2753   // normals of _LayerEdge's with mesh faces
2754   double geomSize = Precision::Infinite(), intersecDist;
2755   auto_ptr<SMESH_ElementSearcher> searcher
2756     ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(),
2757                                            data._proxyMesh->GetFaces( data._solid )) );
2758   for ( size_t i = 0; i < data._edges.size(); ++i )
2759   {
2760     if ( data._edges[i]->IsOnEdge() ) continue;
2761     data._edges[i]->FindIntersection( *searcher, intersecDist, data._epsilon );
2762     if ( geomSize > intersecDist && intersecDist > 0 )
2763       geomSize = intersecDist;
2764   }
2765   if ( data._stepSize > 0.3 * geomSize )
2766     limitStepSize( data, 0.3 * geomSize );
2767
2768   const double tgtThick = data._hyp->GetTotalThickness();
2769   if ( data._stepSize > tgtThick )
2770     limitStepSize( data, tgtThick );
2771
2772   if ( data._stepSize < 1. )
2773     data._epsilon = data._stepSize * 1e-7;
2774
2775   debugMsg( "-- geomSize = " << geomSize << ", stepSize = " << data._stepSize );
2776
2777   double avgThick = 0, curThick = 0, distToIntersection = Precision::Infinite();
2778   int nbSteps = 0, nbRepeats = 0;
2779   while ( 1.01 * avgThick < tgtThick )
2780   {
2781     // new target length
2782     curThick += data._stepSize;
2783     if ( curThick > tgtThick )
2784     {
2785       curThick = tgtThick + ( tgtThick-avgThick ) * nbRepeats;
2786       nbRepeats++;
2787     }
2788
2789     // Elongate _LayerEdge's
2790     dumpFunction(SMESH_Comment("inflate")<<data._index<<"_step"<<nbSteps); // debug
2791     for ( size_t i = 0; i < data._edges.size(); ++i )
2792     {
2793       data._edges[i]->SetNewLength( curThick, helper );
2794     }
2795     dumpFunctionEnd();
2796
2797     if ( !updateNormals( data, helper, nbSteps ))
2798       return false;
2799
2800     // Improve and check quality
2801     if ( !smoothAndCheck( data, nbSteps, distToIntersection ))
2802     {
2803       if ( nbSteps > 0 )
2804       {
2805         dumpFunction(SMESH_Comment("invalidate")<<data._index<<"_step"<<nbSteps); // debug
2806         for ( size_t i = 0; i < data._edges.size(); ++i )
2807         {
2808           data._edges[i]->InvalidateStep( nbSteps+1 );
2809         }
2810         dumpFunctionEnd();
2811       }
2812       break; // no more inflating possible
2813     }
2814     nbSteps++;
2815
2816     // Evaluate achieved thickness
2817     avgThick = 0;
2818     for ( size_t i = 0; i < data._edges.size(); ++i )
2819       avgThick += data._edges[i]->_len;
2820     avgThick /= data._edges.size();
2821     debugMsg( "-- Thickness " << avgThick << " reached" );
2822
2823     if ( distToIntersection < avgThick*1.5 )
2824     {
2825       debugMsg( "-- Stop inflation since "
2826                 << " distToIntersection( "<<distToIntersection<<" ) < avgThick( "
2827                 << avgThick << " ) * 1.5" );
2828       break;
2829     }
2830     // new step size
2831     limitStepSize( data, 0.25 * distToIntersection );
2832     if ( data._stepSizeNodes[0] )
2833       data._stepSize = data._stepSizeCoeff *
2834         SMESH_TNodeXYZ(data._stepSizeNodes[0]).Distance(data._stepSizeNodes[1]);
2835
2836   } // while ( 1.01 * avgThick < tgtThick )
2837
2838   if (nbSteps == 0 )
2839     return error("failed at the very first inflation step", data._index);
2840
2841   if ( 1.01 * avgThick < tgtThick )
2842     if ( SMESH_subMesh* sm = _mesh->GetSubMeshContaining( data._index ))
2843     {
2844       SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
2845       if ( !smError || smError->IsOK() )
2846         smError.reset
2847           ( new SMESH_ComputeError (COMPERR_WARNING,
2848                                     SMESH_Comment("Thickness ") << tgtThick <<
2849                                     " of viscous layers not reached,"
2850                                     " average reached thickness is " << avgThick ));
2851     }
2852
2853
2854   // Restore position of src nodes moved by infaltion on _noShrinkShapes
2855   dumpFunction(SMESH_Comment("restoNoShrink_So")<<data._index); // debug
2856   int iBeg, iEnd = 0;
2857   for ( int iS = 0; iS < data._endEdgeOnShape.size(); ++iS )
2858   {
2859     iBeg = iEnd;
2860     iEnd = data._endEdgeOnShape[ iS ];
2861     if ( data._edges[ iBeg ]->_nodes.size() == 1 )
2862       for ( ; iBeg < iEnd; ++iBeg )
2863       {
2864         restoreNoShrink( *data._edges[ iBeg ] );
2865       }
2866   }
2867   dumpFunctionEnd();
2868
2869   return true;
2870 }
2871
2872 //================================================================================
2873 /*!
2874  * \brief Improve quality of layer inner surface and check intersection
2875  */
2876 //================================================================================
2877
2878 bool _ViscousBuilder::smoothAndCheck(_SolidData& data,
2879                                      const int   nbSteps,
2880                                      double &    distToIntersection)
2881 {
2882   if ( data._nbShapesToSmooth == 0 )
2883     return true; // no shapes needing smoothing
2884
2885   bool moved, improved;
2886
2887   SMESH_MesherHelper helper(*_mesh);
2888   Handle(Geom_Surface) surface;
2889   TopoDS_Face F;
2890
2891   int iBeg, iEnd = 0;
2892   for ( int iS = 0; iS < data._nbShapesToSmooth; ++iS )
2893   {
2894     iBeg = iEnd;
2895     iEnd = data._endEdgeOnShape[ iS ];
2896
2897     if ( !data._edges[ iBeg ]->_sWOL.IsNull() &&
2898          data._edges[ iBeg ]->_sWOL.ShapeType() == TopAbs_FACE )
2899     {
2900       if ( !F.IsSame( data._edges[ iBeg ]->_sWOL )) {
2901         F = TopoDS::Face( data._edges[ iBeg ]->_sWOL );
2902         helper.SetSubShape( F );
2903         surface = BRep_Tool::Surface( F );
2904       }
2905     }
2906     else
2907     {
2908       F.Nullify(); surface.Nullify();
2909     }
2910     TGeomID sInd = data._edges[ iBeg ]->_nodes[0]->getshapeId();
2911
2912     if ( data._edges[ iBeg ]->IsOnEdge() )
2913     { 
2914       dumpFunction(SMESH_Comment("smooth")<<data._index << "_Ed"<<sInd <<"_InfStep"<<nbSteps);
2915
2916       // try a simple solution on an analytic EDGE
2917       if ( !smoothAnalyticEdge( data, iBeg, iEnd, surface, F, helper ))
2918       {
2919         // smooth on EDGE's
2920         int step = 0;
2921         do {
2922           moved = false;
2923           for ( int i = iBeg; i < iEnd; ++i )
2924           {
2925             moved |= data._edges[i]->SmoothOnEdge(surface, F, helper);
2926           }
2927           dumpCmd( SMESH_Comment("# end step ")<<step);
2928         }
2929         while ( moved && step++ < 5 );
2930       }
2931       dumpFunctionEnd();
2932     }
2933     else
2934     {
2935       // smooth on FACE's
2936       int step = 0, stepLimit = 5, badNb = 0; moved = true;
2937       while (( ++step <= stepLimit && moved ) || improved )
2938       {
2939         dumpFunction(SMESH_Comment("smooth")<<data._index<<"_Fa"<<sInd
2940                      <<"_InfStep"<<nbSteps<<"_"<<step); // debug
2941         int oldBadNb = badNb;
2942         badNb = 0;
2943         moved = false;
2944         if ( step % 2 )
2945           for ( int i = iBeg; i < iEnd; ++i )
2946             moved |= data._edges[i]->Smooth(badNb);
2947         else
2948           for ( int i = iEnd-1; i >= iBeg; --i )
2949             moved |= data._edges[i]->Smooth(badNb);
2950         improved = ( badNb < oldBadNb );
2951
2952         // issue 22576 -- no bad faces but still there are intersections to fix
2953         if ( improved && badNb == 0 )
2954           stepLimit = step + 3;
2955
2956         dumpFunctionEnd();
2957       }
2958       if ( badNb > 0 )
2959       {
2960 #ifdef __myDEBUG
2961         for ( int i = iBeg; i < iEnd; ++i )
2962         {
2963           _LayerEdge* edge = data._edges[i];
2964           SMESH_TNodeXYZ tgtXYZ( edge->_nodes.back() );
2965           for ( size_t j = 0; j < edge->_simplices.size(); ++j )
2966             if ( !edge->_simplices[j].IsForward( edge->_nodes[0], &tgtXYZ ))
2967             {
2968               cout << "Bad simplex ( " << edge->_nodes[0]->GetID()<< " "<< tgtXYZ._node->GetID()
2969                    << " "<< edge->_simplices[j]._nPrev->GetID()
2970                    << " "<< edge->_simplices[j]._nNext->GetID() << " )" << endl;
2971               return false;
2972             }
2973         }
2974 #endif
2975         return false;
2976       }
2977     }
2978   } // loop on shapes to smooth
2979
2980   // Check orientation of simplices of _ConvexFace::_simplexTestEdges
2981   map< TGeomID, _ConvexFace >::iterator id2face = data._convexFaces.begin();
2982   for ( ; id2face != data._convexFaces.end(); ++id2face )
2983   {
2984     _ConvexFace & convFace = (*id2face).second;
2985     if ( !convFace._simplexTestEdges.empty() &&
2986          convFace._simplexTestEdges[0]->_nodes[0]->GetPosition()->GetDim() == 2 )
2987       continue; // _simplexTestEdges are based on FACE -- already checked while smoothing
2988
2989     if ( !convFace.CheckPrisms() )
2990       return false;
2991   }
2992
2993   // Check if the last segments of _LayerEdge intersects 2D elements;
2994   // checked elements are either temporary faces or faces on surfaces w/o the layers
2995
2996   auto_ptr<SMESH_ElementSearcher> searcher
2997     ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(),
2998                                            data._proxyMesh->GetFaces( data._solid )) );
2999
3000   distToIntersection = Precision::Infinite();
3001   double dist;
3002   const SMDS_MeshElement* intFace = 0;
3003   const SMDS_MeshElement* closestFace = 0;
3004   int iLE = 0;
3005   for ( size_t i = 0; i < data._edges.size(); ++i )
3006   {
3007     if ( !data._edges[i]->_sWOL.IsNull() )
3008       continue;
3009     if ( data._edges[i]->FindIntersection( *searcher, dist, data._epsilon, &intFace ))
3010       return false;
3011     if ( distToIntersection > dist )
3012     {
3013       // ignore intersection of a _LayerEdge based on a _ConvexFace with a face
3014       // lying on this _ConvexFace
3015       if ( _ConvexFace* convFace = data.GetConvexFace( intFace->getshapeId() ))
3016         if ( convFace->_subIdToEdgeEnd.count ( data._edges[i]->_nodes[0]->getshapeId() ))
3017           continue;
3018
3019       distToIntersection = dist;
3020       iLE = i;
3021       closestFace = intFace;
3022     }
3023   }
3024 #ifdef __myDEBUG
3025   if ( closestFace )
3026   {
3027     SMDS_MeshElement::iterator nIt = closestFace->begin_nodes();
3028     cout << "Shortest distance: _LayerEdge nodes: tgt " << data._edges[iLE]->_nodes.back()->GetID()
3029          << " src " << data._edges[iLE]->_nodes[0]->GetID()<< ", intersection with face ("
3030          << (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()
3031          << ") distance = " << distToIntersection<< endl;
3032   }
3033 #endif
3034
3035   return true;
3036 }
3037
3038 //================================================================================
3039 /*!
3040  * \brief Return a curve of the EDGE to be used for smoothing and arrange
3041  *        _LayerEdge's to be in a consequent order
3042  */
3043 //================================================================================
3044
3045 Handle(Geom_Curve) _SolidData::CurveForSmooth( const TopoDS_Edge&    E,
3046                                                const int             iFrom,
3047                                                const int             iTo,
3048                                                Handle(Geom_Surface)& surface,
3049                                                const TopoDS_Face&    F,
3050                                                SMESH_MesherHelper&   helper)
3051 {
3052   TGeomID eIndex = helper.GetMeshDS()->ShapeToIndex( E );
3053
3054   map< TGeomID, Handle(Geom_Curve)>::iterator i2curve = _edge2curve.find( eIndex );
3055
3056   if ( i2curve == _edge2curve.end() )
3057   {
3058     // sort _LayerEdge's by position on the EDGE
3059     SortOnEdge( E, iFrom, iTo, helper );
3060
3061     SMESHDS_SubMesh* smDS = helper.GetMeshDS()->MeshElements( eIndex );
3062
3063     TopLoc_Location loc; double f,l;
3064
3065     Handle(Geom_Line)   line;
3066     Handle(Geom_Circle) circle;
3067     bool isLine, isCirc;
3068     if ( F.IsNull() ) // 3D case
3069     {
3070       // check if the EDGE is a line
3071       Handle(Geom_Curve) curve = BRep_Tool::Curve( E, loc, f, l);
3072       if ( curve->IsKind( STANDARD_TYPE( Geom_TrimmedCurve )))
3073         curve = Handle(Geom_TrimmedCurve)::DownCast( curve )->BasisCurve();
3074
3075       line   = Handle(Geom_Line)::DownCast( curve );
3076       circle = Handle(Geom_Circle)::DownCast( curve );
3077       isLine = (!line.IsNull());
3078       isCirc = (!circle.IsNull());
3079
3080       if ( !isLine && !isCirc ) // Check if the EDGE is close to a line
3081       {
3082         Bnd_B3d bndBox;
3083         SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
3084         while ( nIt->more() )
3085           bndBox.Add( SMESH_TNodeXYZ( nIt->next() ));
3086         gp_XYZ size = bndBox.CornerMax() - bndBox.CornerMin();
3087
3088         SMESH_TNodeXYZ p0( _edges[iFrom]->_2neibors->tgtNode(0) );
3089         SMESH_TNodeXYZ p1( _edges[iFrom]->_2neibors->tgtNode(1) );
3090         const double lineTol = 1e-2 * ( p0 - p1 ).Modulus();
3091         for ( int i = 0; i < 3 && !isLine; ++i )
3092           isLine = ( size.Coord( i+1 ) <= lineTol );
3093       }
3094       if ( !isLine && !isCirc && iTo-iFrom > 2) // Check if the EDGE is close to a circle
3095       {
3096         // TODO
3097       }
3098     }
3099     else // 2D case
3100     {
3101       // check if the EDGE is a line
3102       Handle(Geom2d_Curve) curve = BRep_Tool::CurveOnSurface( E, F, f, l);
3103       if ( curve->IsKind( STANDARD_TYPE( Geom2d_TrimmedCurve )))
3104         curve = Handle(Geom2d_TrimmedCurve)::DownCast( curve )->BasisCurve();
3105
3106       Handle(Geom2d_Line)   line2d   = Handle(Geom2d_Line)::DownCast( curve );
3107       Handle(Geom2d_Circle) circle2d = Handle(Geom2d_Circle)::DownCast( curve );
3108       isLine = (!line2d.IsNull());
3109       isCirc = (!circle2d.IsNull());
3110
3111       if ( !isLine && !isCirc) // Check if the EDGE is close to a line
3112       {
3113         Bnd_B2d bndBox;
3114         SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
3115         while ( nIt->more() )
3116           bndBox.Add( helper.GetNodeUV( F, nIt->next() ));
3117         gp_XY size = bndBox.CornerMax() - bndBox.CornerMin();
3118
3119         const double lineTol = 1e-2 * sqrt( bndBox.SquareExtent() );
3120         for ( int i = 0; i < 2 && !isLine; ++i )
3121           isLine = ( size.Coord( i+1 ) <= lineTol );
3122       }
3123       if ( !isLine && !isCirc && iTo-iFrom > 2) // Check if the EDGE is close to a circle
3124       {
3125         // TODO
3126       }
3127       if ( isLine )
3128       {
3129         line = new Geom_Line( gp::OX() ); // only type does matter
3130       }
3131       else if ( isCirc )
3132       {
3133         gp_Pnt2d p = circle2d->Location();
3134         gp_Ax2 ax( gp_Pnt( p.X(), p.Y(), 0), gp::DX());
3135         circle = new Geom_Circle( ax, 1.); // only center position does matter
3136       }
3137     }
3138
3139     Handle(Geom_Curve)& res = _edge2curve[ eIndex ];
3140     if ( isLine )
3141       res = line;
3142     else if ( isCirc )
3143       res = circle;
3144
3145     return res;
3146   }
3147   return i2curve->second;
3148 }
3149
3150 //================================================================================
3151 /*!
3152  * \brief Sort _LayerEdge's by a parameter on a given EDGE
3153  */
3154 //================================================================================
3155
3156 void _SolidData::SortOnEdge( const TopoDS_Edge&  E,
3157                              const int           iFrom,
3158                              const int           iTo,
3159                              SMESH_MesherHelper& helper)
3160 {
3161   map< double, _LayerEdge* > u2edge;
3162   for ( int i = iFrom; i < iTo; ++i )
3163     u2edge.insert( make_pair( helper.GetNodeU( E, _edges[i]->_nodes[0] ), _edges[i] ));
3164
3165   ASSERT( u2edge.size() == iTo - iFrom );
3166   map< double, _LayerEdge* >::iterator u2e = u2edge.begin();
3167   for ( int i = iFrom; i < iTo; ++i, ++u2e )
3168     _edges[i] = u2e->second;
3169
3170   // set _2neibors according to the new order
3171   for ( int i = iFrom; i < iTo-1; ++i )
3172     if ( _edges[i]->_2neibors->tgtNode(1) != _edges[i+1]->_nodes.back() )
3173       _edges[i]->_2neibors->reverse();
3174   if ( u2edge.size() > 1 &&
3175        _edges[iTo-1]->_2neibors->tgtNode(0) != _edges[iTo-2]->_nodes.back() )
3176     _edges[iTo-1]->_2neibors->reverse();
3177 }
3178
3179 //================================================================================
3180 /*!
3181  * \brief Return index corresponding to the shape in _endEdgeOnShape
3182  */
3183 //================================================================================
3184
3185 bool _SolidData::GetShapeEdges(const TGeomID shapeID,
3186                                size_t &      edgesEnd,
3187                                int*          iBeg,
3188                                int*          iEnd ) const
3189 {
3190   int beg = 0, end = 0;
3191   for ( edgesEnd = 0; edgesEnd < _endEdgeOnShape.size(); ++edgesEnd )
3192   {
3193     end = _endEdgeOnShape[ edgesEnd ];
3194     TGeomID sID = _edges[ beg ]->_nodes[0]->getshapeId();
3195     if ( sID == shapeID )
3196     {
3197       if ( iBeg ) *iBeg = beg;
3198       if ( iEnd ) *iEnd = end;
3199       return true;
3200     }
3201     beg = end;
3202   }
3203   return false;
3204 }
3205
3206 //================================================================================
3207 /*!
3208  * \brief Add faces for smoothing
3209  */
3210 //================================================================================
3211
3212 void _SolidData::AddShapesToSmooth( const set< TGeomID >& faceIDs )
3213 {
3214   // convert faceIDs to indices in _endEdgeOnShape
3215   set< size_t > iEnds;
3216   size_t end;
3217   set< TGeomID >::const_iterator fId = faceIDs.begin();
3218   for ( ; fId != faceIDs.end(); ++fId )
3219     if ( GetShapeEdges( *fId, end ) && end >= _nbShapesToSmooth )
3220       iEnds.insert( end );
3221
3222   set< size_t >::iterator endsIt = iEnds.begin();
3223
3224   // "add" by move of _nbShapesToSmooth
3225   int nbFacesToAdd = iEnds.size();
3226   while ( endsIt != iEnds.end() && *endsIt == _nbShapesToSmooth )
3227   {
3228     ++endsIt;
3229     ++_nbShapesToSmooth;
3230     --nbFacesToAdd;
3231   }
3232   if ( endsIt == iEnds.end() )
3233     return;
3234
3235   // Move _LayerEdge's on FACEs just after _nbShapesToSmooth
3236
3237   vector< _LayerEdge* > nonSmoothLE, smoothLE;
3238   size_t lastSmooth = *iEnds.rbegin();
3239   int iBeg, iEnd;
3240   for ( size_t i = _nbShapesToSmooth; i <= lastSmooth; ++i )
3241   {
3242     vector< _LayerEdge* > & edgesVec = iEnds.count(i) ? smoothLE : nonSmoothLE;
3243     iBeg = i ? _endEdgeOnShape[ i-1 ] : 0;
3244     iEnd = _endEdgeOnShape[ i ];
3245     edgesVec.insert( edgesVec.end(), _edges.begin() + iBeg, _edges.begin() + iEnd ); 
3246   }
3247
3248   iBeg = _nbShapesToSmooth ? _endEdgeOnShape[ _nbShapesToSmooth-1 ] : 0;
3249   std::copy( smoothLE.begin(),    smoothLE.end(),    &_edges[ iBeg ] );
3250   std::copy( nonSmoothLE.begin(), nonSmoothLE.end(), &_edges[ iBeg + smoothLE.size()]);
3251
3252   // update _endEdgeOnShape
3253   for ( size_t i = _nbShapesToSmooth; i < _endEdgeOnShape.size(); ++i )
3254   {
3255     TGeomID curShape = _edges[ iBeg ]->_nodes[0]->getshapeId();
3256     while ( ++iBeg < _edges.size() &&
3257             curShape == _edges[ iBeg ]->_nodes[0]->getshapeId() );
3258
3259     _endEdgeOnShape[ i ] = iBeg;
3260   }
3261
3262   _nbShapesToSmooth += nbFacesToAdd;
3263 }
3264
3265 //================================================================================
3266 /*!
3267  * \brief smooth _LayerEdge's on a staight EDGE or circular EDGE
3268  */
3269 //================================================================================
3270
3271 bool _ViscousBuilder::smoothAnalyticEdge( _SolidData&           data,
3272                                           const int             iFrom,
3273                                           const int             iTo,
3274                                           Handle(Geom_Surface)& surface,
3275                                           const TopoDS_Face&    F,
3276                                           SMESH_MesherHelper&   helper)
3277 {
3278   TopoDS_Shape S = helper.GetSubShapeByNode( data._edges[ iFrom ]->_nodes[0],
3279                                              helper.GetMeshDS());
3280   TopoDS_Edge E = TopoDS::Edge( S );
3281
3282   Handle(Geom_Curve) curve = data.CurveForSmooth( E, iFrom, iTo, surface, F, helper );
3283   if ( curve.IsNull() ) return false;
3284
3285   // compute a relative length of segments
3286   vector< double > len( iTo-iFrom+1 );
3287   {
3288     double curLen, prevLen = len[0] = 1.0;
3289     for ( int i = iFrom; i < iTo; ++i )
3290     {
3291       curLen = prevLen * data._edges[i]->_2neibors->_wgt[0] / data._edges[i]->_2neibors->_wgt[1];
3292       len[i-iFrom+1] = len[i-iFrom] + curLen;
3293       prevLen = curLen;
3294     }
3295   }
3296
3297   if ( curve->IsKind( STANDARD_TYPE( Geom_Line )))
3298   {
3299     if ( F.IsNull() ) // 3D
3300     {
3301       SMESH_TNodeXYZ p0( data._edges[iFrom]->_2neibors->tgtNode(0));
3302       SMESH_TNodeXYZ p1( data._edges[iTo-1]->_2neibors->tgtNode(1));
3303       for ( int i = iFrom; i < iTo; ++i )
3304       {
3305         double r = len[i-iFrom] / len.back();
3306         gp_XYZ newPos = p0 * ( 1. - r ) + p1 * r;
3307         data._edges[i]->_pos.back() = newPos;
3308         SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( data._edges[i]->_nodes.back() );
3309         tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
3310         dumpMove( tgtNode );
3311       }
3312     }
3313     else
3314     {
3315       // gp_XY uv0 = helper.GetNodeUV( F, data._edges[iFrom]->_2neibors->tgtNode(0));
3316       // gp_XY uv1 = helper.GetNodeUV( F, data._edges[iTo-1]->_2neibors->tgtNode(1));
3317       gp_XY uv0 = data._edges[iFrom]->_2neibors->_edges[0]->LastUV( F );
3318       gp_XY uv1 = data._edges[iTo-1]->_2neibors->_edges[1]->LastUV( F );
3319       if ( data._edges[iFrom]->_2neibors->tgtNode(0) ==
3320            data._edges[iTo-1]->_2neibors->tgtNode(1) ) // closed edge
3321       {
3322         int iPeriodic = helper.GetPeriodicIndex();
3323         if ( iPeriodic == 1 || iPeriodic == 2 )
3324         {
3325           uv1.SetCoord( iPeriodic, helper.GetOtherParam( uv1.Coord( iPeriodic )));
3326           if ( uv0.Coord( iPeriodic ) > uv1.Coord( iPeriodic ))
3327             std::swap( uv0, uv1 );
3328         }
3329       }
3330       const gp_XY rangeUV = uv1 - uv0;
3331       for ( int i = iFrom; i < iTo; ++i )
3332       {
3333         double r = len[i-iFrom] / len.back();
3334         gp_XY newUV = uv0 + r * rangeUV;
3335         data._edges[i]->_pos.back().SetCoord( newUV.X(), newUV.Y(), 0 );
3336
3337         gp_Pnt newPos = surface->Value( newUV.X(), newUV.Y() );
3338         SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( data._edges[i]->_nodes.back() );
3339         tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
3340         dumpMove( tgtNode );
3341
3342         SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
3343         pos->SetUParameter( newUV.X() );
3344         pos->SetVParameter( newUV.Y() );
3345       }
3346     }
3347     return true;
3348   }
3349
3350   if ( curve->IsKind( STANDARD_TYPE( Geom_Circle )))
3351   {
3352     Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast( curve );
3353     gp_Pnt center3D = circle->Location();
3354
3355     if ( F.IsNull() ) // 3D
3356     {
3357       if ( data._edges[iFrom]->_2neibors->tgtNode(0) ==
3358            data._edges[iTo-1]->_2neibors->tgtNode(1) )
3359         return true; // closed EDGE - nothing to do
3360
3361       return false; // TODO ???
3362     }
3363     else // 2D
3364     {
3365       const gp_XY center( center3D.X(), center3D.Y() );
3366
3367       gp_XY uv0 = data._edges[iFrom]->_2neibors->_edges[0]->LastUV( F );
3368       gp_XY uvM = data._edges[iFrom]->LastUV( F );
3369       gp_XY uv1 = data._edges[iTo-1]->_2neibors->_edges[1]->LastUV( F );
3370       // gp_XY uv0 = helper.GetNodeUV( F, data._edges[iFrom]->_2neibors->tgtNode(0));
3371       // gp_XY uvM = helper.GetNodeUV( F, data._edges[iFrom]->_nodes.back());
3372       // gp_XY uv1 = helper.GetNodeUV( F, data._edges[iTo-1]->_2neibors->tgtNode(1));
3373       gp_Vec2d vec0( center, uv0 );
3374       gp_Vec2d vecM( center, uvM );
3375       gp_Vec2d vec1( center, uv1 );
3376       double uLast = vec0.Angle( vec1 ); // -PI - +PI
3377       double uMidl = vec0.Angle( vecM );
3378       if ( uLast * uMidl <= 0. )
3379         uLast += ( uMidl > 0 ? +2. : -2. ) * M_PI;
3380       const double radius = 0.5 * ( vec0.Magnitude() + vec1.Magnitude() );
3381
3382       gp_Ax2d   axis( center, vec0 );
3383       gp_Circ2d circ( axis, radius );
3384       for ( int i = iFrom; i < iTo; ++i )
3385       {
3386         double    newU = uLast * len[i-iFrom] / len.back();
3387         gp_Pnt2d newUV = ElCLib::Value( newU, circ );
3388         data._edges[i]->_pos.back().SetCoord( newUV.X(), newUV.Y(), 0 );
3389
3390         gp_Pnt newPos = surface->Value( newUV.X(), newUV.Y() );
3391         SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( data._edges[i]->_nodes.back() );
3392         tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
3393         dumpMove( tgtNode );
3394
3395         SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
3396         pos->SetUParameter( newUV.X() );
3397         pos->SetVParameter( newUV.Y() );
3398       }
3399     }
3400     return true;
3401   }
3402
3403   return false;
3404 }
3405
3406 //================================================================================
3407 /*!
3408  * \brief Modify normals of _LayerEdge's on EDGE's to avoid intersection with
3409  * _LayerEdge's on neighbor EDGE's
3410  */
3411 //================================================================================
3412
3413 bool _ViscousBuilder::updateNormals( _SolidData&         data,
3414                                      SMESH_MesherHelper& helper,
3415                                      int                 stepNb )
3416 {
3417   if ( stepNb > 0 )
3418     return updateNormalsOfConvexFaces( data, helper, stepNb );
3419
3420   // make temporary quadrangles got by extrusion of
3421   // mesh edges along _LayerEdge._normal's
3422
3423   vector< const SMDS_MeshElement* > tmpFaces;
3424   {
3425     set< SMESH_TLink > extrudedLinks; // contains target nodes
3426     vector< const SMDS_MeshNode*> nodes(4); // of a tmp mesh face
3427
3428     dumpFunction(SMESH_Comment("makeTmpFacesOnEdges")<<data._index);
3429     for ( size_t i = 0; i < data._edges.size(); ++i )
3430     {
3431       _LayerEdge* edge = data._edges[i];
3432       if ( !edge->IsOnEdge() || !edge->_sWOL.IsNull() ) continue;
3433       const SMDS_MeshNode* tgt1 = edge->_nodes.back();
3434       for ( int j = 0; j < 2; ++j ) // loop on _2NearEdges
3435       {
3436         const SMDS_MeshNode* tgt2 = edge->_2neibors->tgtNode(j);
3437         pair< set< SMESH_TLink >::iterator, bool > link_isnew =
3438           extrudedLinks.insert( SMESH_TLink( tgt1, tgt2 ));
3439         if ( !link_isnew.second )
3440         {
3441           extrudedLinks.erase( link_isnew.first );
3442           continue; // already extruded and will no more encounter
3443         }
3444         // a _LayerEdge containg tgt2
3445         _LayerEdge* neiborEdge = edge->_2neibors->_edges[j];
3446
3447         _TmpMeshFaceOnEdge* f = new _TmpMeshFaceOnEdge( edge, neiborEdge, --_tmpFaceID );
3448         tmpFaces.push_back( f );
3449
3450         dumpCmd(SMESH_Comment("mesh.AddFace([ ")
3451                 <<f->_nn[0]->GetID()<<", "<<f->_nn[1]->GetID()<<", "
3452                 <<f->_nn[2]->GetID()<<", "<<f->_nn[3]->GetID()<<" ])");
3453       }
3454     }
3455     dumpFunctionEnd();
3456   }
3457   // Check if _LayerEdge's based on EDGE's intersects tmpFaces.
3458   // Perform two loops on _LayerEdge on EDGE's:
3459   // 1) to find and fix intersection
3460   // 2) to check that no new intersection appears as result of 1)
3461
3462   SMDS_ElemIteratorPtr fIt( new SMDS_ElementVectorIterator( tmpFaces.begin(),
3463                                                             tmpFaces.end()));
3464   auto_ptr<SMESH_ElementSearcher> searcher
3465     ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(), fIt ));
3466
3467   // 1) Find intersections
3468   double dist;
3469   const SMDS_MeshElement* face;
3470   typedef map< _LayerEdge*, set< _LayerEdge*, _LayerEdgeCmp >, _LayerEdgeCmp > TLEdge2LEdgeSet;
3471   TLEdge2LEdgeSet edge2CloseEdge;
3472
3473   const double eps = data._epsilon * data._epsilon;
3474   for ( size_t i = 0; i < data._edges.size(); ++i )
3475   {
3476     _LayerEdge* edge = data._edges[i];
3477     if (( !edge->IsOnEdge() ) &&
3478         ( edge->_sWOL.IsNull() || edge->_sWOL.ShapeType() != TopAbs_FACE ))
3479       continue;
3480     if ( edge->FindIntersection( *searcher, dist, eps, &face ))
3481     {
3482       const _TmpMeshFaceOnEdge* f = (const _TmpMeshFaceOnEdge*) face;
3483       set< _LayerEdge*, _LayerEdgeCmp > & ee = edge2CloseEdge[ edge ];
3484       ee.insert( f->_le1 );
3485       ee.insert( f->_le2 );
3486       if ( f->_le1->IsOnEdge() && f->_le1->_sWOL.IsNull() ) 
3487         edge2CloseEdge[ f->_le1 ].insert( edge );
3488       if ( f->_le2->IsOnEdge() && f->_le2->_sWOL.IsNull() ) 
3489         edge2CloseEdge[ f->_le2 ].insert( edge );
3490     }
3491   }
3492
3493   // Set _LayerEdge._normal
3494
3495   if ( !edge2CloseEdge.empty() )
3496   {
3497     dumpFunction(SMESH_Comment("updateNormals")<<data._index);
3498
3499     set< TGeomID > shapesToSmooth;
3500
3501     // vector to store new _normal and _cosin for each edge in edge2CloseEdge
3502     vector< pair< _LayerEdge*, _LayerEdge > > edge2newEdge( edge2CloseEdge.size() );
3503
3504     TLEdge2LEdgeSet::iterator e2ee = edge2CloseEdge.begin();
3505     for ( size_t iE = 0; e2ee != edge2CloseEdge.end(); ++e2ee, ++iE )
3506     {
3507       _LayerEdge* edge1 = e2ee->first;
3508       _LayerEdge* edge2 = 0;
3509       set< _LayerEdge*, _LayerEdgeCmp >& ee = e2ee->second;
3510
3511       edge2newEdge[ iE ].first = NULL;
3512
3513       // find EDGEs the edges reside
3514       // TopoDS_Edge E1, E2;
3515       // TopoDS_Shape S = helper.GetSubShapeByNode( edge1->_nodes[0], getMeshDS() );
3516       // if ( S.ShapeType() != TopAbs_EDGE )
3517       //   continue; // TODO: find EDGE by VERTEX
3518       // E1 = TopoDS::Edge( S );
3519       set< _LayerEdge*, _LayerEdgeCmp >::iterator eIt = ee.begin();
3520       for ( ; !edge2 && eIt != ee.end(); ++eIt )
3521       {
3522         if ( edge1->_sWOL == (*eIt)->_sWOL )
3523           edge2 = *eIt;
3524       }
3525       if ( !edge2 ) continue;
3526
3527       edge2newEdge[ iE ].first = edge1;
3528       _LayerEdge& newEdge = edge2newEdge[ iE ].second;
3529       // while ( E2.IsNull() && eIt != ee.end())
3530       // {
3531       //   _LayerEdge* e2 = *eIt++;
3532       //   TopoDS_Shape S = helper.GetSubShapeByNode( e2->_nodes[0], getMeshDS() );
3533       //   if ( S.ShapeType() == TopAbs_EDGE )
3534       //     E2 = TopoDS::Edge( S ), edge2 = e2;
3535       // }
3536       // if ( E2.IsNull() ) continue; // TODO: find EDGE by VERTEX
3537
3538       // find 3 FACEs sharing 2 EDGEs
3539
3540       // TopoDS_Face FF1[2], FF2[2];
3541       // PShapeIteratorPtr fIt = helper.GetAncestors(E1, *_mesh, TopAbs_FACE);
3542       // while ( fIt->more() && FF1[1].IsNull() )
3543       // {
3544       //   const TopoDS_Face *F = (const TopoDS_Face*) fIt->next();
3545       //   if ( helper.IsSubShape( *F, data._solid))
3546       //     FF1[ FF1[0].IsNull() ? 0 : 1 ] = *F;
3547       // }
3548       // fIt = helper.GetAncestors(E2, *_mesh, TopAbs_FACE);
3549       // while ( fIt->more() && FF2[1].IsNull())
3550       // {
3551       //   const TopoDS_Face *F = (const TopoDS_Face*) fIt->next();
3552       //   if ( helper.IsSubShape( *F, data._solid))
3553       //     FF2[ FF2[0].IsNull() ? 0 : 1 ] = *F;
3554       // }
3555       // // exclude a FACE common to E1 and E2 (put it to FFn[1] )
3556       // if ( FF1[0].IsSame( FF2[0]) || FF1[0].IsSame( FF2[1]))
3557       //   std::swap( FF1[0], FF1[1] );
3558       // if ( FF2[0].IsSame( FF1[0]) )
3559       //   std::swap( FF2[0], FF2[1] );
3560       // if ( FF1[0].IsNull() || FF2[0].IsNull() )
3561       //   continue;
3562
3563       // get a new normal for edge1
3564       //bool ok;
3565       gp_Vec dir1 = edge1->_normal, dir2 = edge2->_normal;
3566       // if ( edge1->_cosin < 0 )
3567       //   dir1 = getFaceDir( FF1[0], E1, edge1->_nodes[0], helper, ok ).Normalized();
3568       // if ( edge2->_cosin < 0 )
3569       //   dir2 = getFaceDir( FF2[0], E2, edge2->_nodes[0], helper, ok ).Normalized();
3570
3571       double cos1 = Abs( edge1->_cosin ), cos2 = Abs( edge2->_cosin );
3572       double wgt1 = ( cos1 + 0.001 ) / ( cos1 + cos2 + 0.002 );
3573       double wgt2 = ( cos2 + 0.001 ) / ( cos1 + cos2 + 0.002 );
3574       newEdge._normal = ( wgt1 * dir1 + wgt2 * dir2 ).XYZ();
3575       newEdge._normal.Normalize();
3576
3577       // cout << edge1->_nodes[0]->GetID() << " "
3578       //      << edge2->_nodes[0]->GetID() << " NORM: "
3579       //      << newEdge._normal.X() << ", " << newEdge._normal.Y() << ", " << newEdge._normal.Z() << endl;
3580
3581       // get new cosin
3582       if ( cos1 < theMinSmoothCosin )
3583       {
3584         newEdge._cosin = edge2->_cosin;
3585       }
3586       else if ( cos2 > theMinSmoothCosin ) // both cos1 and cos2 > theMinSmoothCosin
3587       {
3588         // gp_Vec dirInFace;
3589         // if ( edge1->_cosin < 0 )
3590         //   dirInFace = dir1;
3591         // else
3592         //   dirInFace = getFaceDir( FF1[0], E1, edge1->_nodes[0], helper, ok );
3593         // double angle = dirInFace.Angle( edge1->_normal ); // [0,PI]
3594         // edge1->SetCosin( Cos( angle ));
3595         //newEdge._cosin = 0; // ???????????
3596         newEdge._cosin = ( wgt1 * cos1 + wgt2 * cos2 ) * edge1->_cosin / cos1;
3597       }
3598       else
3599       {
3600         newEdge._cosin = edge1->_cosin;
3601       }
3602
3603       // find shapes that need smoothing due to change of _normal
3604       if ( edge1->_cosin  < theMinSmoothCosin &&
3605            newEdge._cosin > theMinSmoothCosin )
3606       {
3607         if ( edge1->_sWOL.IsNull() )
3608         {
3609           SMDS_ElemIteratorPtr fIt = edge1->_nodes[0]->GetInverseElementIterator(SMDSAbs_Face);
3610           while ( fIt->more() )
3611             shapesToSmooth.insert( fIt->next()->getshapeId() );
3612           //limitStepSize( data, fIt->next(), edge1->_cosin ); // too late
3613         }
3614         else // edge1 inflates along a FACE
3615         {
3616           TopoDS_Shape V = helper.GetSubShapeByNode( edge1->_nodes[0], getMeshDS() );
3617           PShapeIteratorPtr eIt = helper.GetAncestors( V, *_mesh, TopAbs_EDGE );
3618           while ( const TopoDS_Shape* E = eIt->next() )
3619           {
3620             if ( !helper.IsSubShape( *E, /*FACE=*/edge1->_sWOL ))
3621               continue;
3622             gp_Vec edgeDir = getEdgeDir( TopoDS::Edge( *E ), TopoDS::Vertex( V ));
3623             double   angle = edgeDir.Angle( newEdge._normal ); // [0,PI]
3624             if ( angle < M_PI / 2 )
3625               shapesToSmooth.insert( getMeshDS()->ShapeToIndex( *E ));
3626           }
3627         }
3628       }
3629     }
3630
3631     data.AddShapesToSmooth( shapesToSmooth );
3632
3633     // Update data of edges depending on a new _normal
3634
3635     for ( size_t iE = 0; iE < edge2newEdge.size(); ++iE )
3636     {
3637       _LayerEdge*   edge1 = edge2newEdge[ iE ].first;
3638       _LayerEdge& newEdge = edge2newEdge[ iE ].second;
3639       if ( !edge1 ) continue;
3640
3641       edge1->_normal = newEdge._normal;
3642       edge1->SetCosin( newEdge._cosin );
3643       edge1->InvalidateStep( 1 );
3644       edge1->_len = 0;
3645       edge1->SetNewLength( data._stepSize, helper );
3646       if ( edge1->IsOnEdge() )
3647       {
3648         const SMDS_MeshNode * n1 = edge1->_2neibors->srcNode(0);
3649         const SMDS_MeshNode * n2 = edge1->_2neibors->srcNode(1);
3650         edge1->SetDataByNeighbors( n1, n2, helper );
3651       }
3652
3653       // Update normals and other dependent data of not intersecting _LayerEdge's
3654       // neighboring the intersecting ones
3655
3656       if ( !edge1->_2neibors )
3657         continue;
3658       for ( int j = 0; j < 2; ++j ) // loop on 2 neighbors
3659       {
3660         _LayerEdge* neighbor = edge1->_2neibors->_edges[j];
3661         if ( edge2CloseEdge.count ( neighbor ))
3662           continue; // j-th neighbor is also intersected
3663         _LayerEdge* prevEdge = edge1;
3664         const int nbSteps = 10;
3665         for ( int step = nbSteps; step; --step ) // step from edge1 in j-th direction
3666         {
3667           if ( !neighbor->_2neibors )
3668             break; // neighbor is on VERTEX
3669           int iNext = 0;
3670           _LayerEdge* nextEdge = neighbor->_2neibors->_edges[iNext];
3671           if ( nextEdge == prevEdge )
3672             nextEdge = neighbor->_2neibors->_edges[ ++iNext ];
3673           double r = double(step-1)/nbSteps;
3674           if ( !nextEdge->_2neibors )
3675             r = 0.5;
3676
3677           gp_XYZ newNorm = prevEdge->_normal * r + nextEdge->_normal * (1-r);
3678           newNorm.Normalize();
3679
3680           neighbor->_normal = newNorm;
3681           neighbor->SetCosin( prevEdge->_cosin * r + nextEdge->_cosin * (1-r) );
3682           neighbor->SetDataByNeighbors( prevEdge->_nodes[0], nextEdge->_nodes[0], helper );
3683
3684           neighbor->InvalidateStep( 1 );
3685           neighbor->_len = 0;
3686           neighbor->SetNewLength( data._stepSize, helper );
3687
3688           // goto the next neighbor
3689           prevEdge = neighbor;
3690           neighbor = nextEdge;
3691         }
3692       }
3693     }
3694     dumpFunctionEnd();
3695   }
3696   // 2) Check absence of intersections
3697   // TODO?
3698
3699   for ( size_t i = 0 ; i < tmpFaces.size(); ++i )
3700     delete tmpFaces[i];
3701
3702   return true;
3703 }
3704
3705 //================================================================================
3706 /*!
3707  * \brief Modify normals of _LayerEdge's on _ConvexFace's
3708  */
3709 //================================================================================
3710
3711 bool _ViscousBuilder::updateNormalsOfConvexFaces( _SolidData&         data,
3712                                                   SMESH_MesherHelper& helper,
3713                                                   int                 stepNb )
3714 {
3715   SMESHDS_Mesh* meshDS = helper.GetMeshDS();
3716   bool isOK;
3717
3718   map< TGeomID, _ConvexFace >::iterator id2face = data._convexFaces.begin();
3719   for ( ; id2face != data._convexFaces.end(); ++id2face )
3720   {
3721     _ConvexFace & convFace = (*id2face).second;
3722     if ( convFace._normalsFixed )
3723       continue; // already fixed
3724     if ( convFace.CheckPrisms() )
3725       continue; // nothing to fix
3726
3727     convFace._normalsFixed = true;
3728
3729     BRepAdaptor_Surface surface ( convFace._face, false );
3730     BRepLProp_SLProps   surfProp( surface, 2, 1e-6 );
3731
3732     // check if the convex FACE is of spherical shape
3733
3734     Bnd_B3d centersBox; // bbox of centers of curvature of _LayerEdge's on VERTEXes
3735     Bnd_B3d nodesBox;
3736     gp_Pnt  center;
3737     int     iBeg, iEnd;
3738
3739     map< TGeomID, int >::iterator id2end = convFace._subIdToEdgeEnd.begin();
3740     for ( ; id2end != convFace._subIdToEdgeEnd.end(); ++id2end )
3741     {
3742       data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
3743
3744       if ( meshDS->IndexToShape( id2end->first ).ShapeType() == TopAbs_VERTEX )
3745       {
3746         _LayerEdge* ledge = data._edges[ iBeg ];
3747         if ( convFace.GetCenterOfCurvature( ledge, surfProp, helper, center ))
3748           centersBox.Add( center );
3749       }
3750       for ( ; iBeg < iEnd; ++iBeg )
3751         nodesBox.Add( SMESH_TNodeXYZ( data._edges[ iBeg ]->_nodes[0] ));
3752     }
3753     if ( centersBox.IsVoid() )
3754     {
3755       debugMsg( "Error: centersBox.IsVoid()" );
3756       return false;
3757     }
3758     const bool isSpherical =
3759       ( centersBox.SquareExtent() < 1e-6 * nodesBox.SquareExtent() );
3760
3761     int nbEdges = helper.Count( convFace._face, TopAbs_EDGE, /*ignoreSame=*/false );
3762     vector < _CentralCurveOnEdge > centerCurves( nbEdges );
3763
3764     if ( isSpherical )
3765     {
3766       // set _LayerEdge::_normal as average of all normals
3767
3768       // WARNING: different density of nodes on EDGEs is not taken into account that
3769       // can lead to an improper new normal
3770
3771       gp_XYZ avgNormal( 0,0,0 );
3772       nbEdges = 0;
3773       id2end = convFace._subIdToEdgeEnd.begin();
3774       for ( ; id2end != convFace._subIdToEdgeEnd.end(); ++id2end )
3775       {
3776         data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
3777         // set data of _CentralCurveOnEdge
3778         const TopoDS_Shape& S = meshDS->IndexToShape( id2end->first );
3779         if ( S.ShapeType() == TopAbs_EDGE )
3780         {
3781           _CentralCurveOnEdge& ceCurve = centerCurves[ nbEdges++ ];
3782           ceCurve.SetShapes( TopoDS::Edge(S), convFace, data, helper );
3783           if ( !data._edges[ iBeg ]->_sWOL.IsNull() )
3784             ceCurve._adjFace.Nullify();
3785           else
3786             ceCurve._ledges.insert( ceCurve._ledges.end(),
3787                                     &data._edges[ iBeg ], &data._edges[ iEnd ]);
3788         }
3789         // summarize normals
3790         for ( ; iBeg < iEnd; ++iBeg )
3791           avgNormal += data._edges[ iBeg ]->_normal;
3792       }
3793       double normSize = avgNormal.SquareModulus();
3794       if ( normSize < 1e-200 )
3795       {
3796         debugMsg( "updateNormalsOfConvexFaces(): zero avgNormal" );
3797         return false;
3798       }
3799       avgNormal /= Sqrt( normSize );
3800
3801       // compute new _LayerEdge::_cosin on EDGEs
3802       double avgCosin = 0;
3803       int     nbCosin = 0;
3804       gp_Vec inFaceDir;
3805       for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
3806       {
3807         _CentralCurveOnEdge& ceCurve = centerCurves[ iE ];
3808         if ( ceCurve._adjFace.IsNull() )
3809           continue;
3810         for ( size_t iLE = 0; iLE < ceCurve._ledges.size(); ++iLE )
3811         {
3812           const SMDS_MeshNode* node = ceCurve._ledges[ iLE ]->_nodes[0];
3813           inFaceDir = getFaceDir( ceCurve._adjFace, ceCurve._edge, node, helper, isOK );
3814           if ( isOK )
3815           {
3816             double angle = inFaceDir.Angle( avgNormal ); // [0,PI]
3817             ceCurve._ledges[ iLE ]->_cosin = Cos( angle );
3818             avgCosin += ceCurve._ledges[ iLE ]->_cosin;
3819             nbCosin++;
3820           }
3821         }
3822       }
3823       if ( nbCosin > 0 )
3824         avgCosin /= nbCosin;
3825
3826       // set _LayerEdge::_normal = avgNormal
3827       id2end = convFace._subIdToEdgeEnd.begin();
3828       for ( ; id2end != convFace._subIdToEdgeEnd.end(); ++id2end )
3829       {
3830         data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
3831         const TopoDS_Shape& S = meshDS->IndexToShape( id2end->first );
3832         if ( S.ShapeType() != TopAbs_EDGE )
3833           for ( int i = iBeg; i < iEnd; ++i )
3834             data._edges[ i ]->_cosin = avgCosin;
3835
3836         for ( ; iBeg < iEnd; ++iBeg )
3837           data._edges[ iBeg ]->_normal = avgNormal;
3838       }
3839     }
3840     else // if ( isSpherical )
3841     {
3842       // We suppose that centers of curvature at all points of the FACE
3843       // lie on some curve, let's call it "central curve". For all _LayerEdge's
3844       // having a common center of curvature we define the same new normal
3845       // as a sum of normals of _LayerEdge's on EDGEs among them.
3846
3847       // get all centers of curvature for each EDGE
3848
3849       helper.SetSubShape( convFace._face );
3850       _LayerEdge* vertexLEdges[2], **edgeLEdge, **edgeLEdgeEnd;
3851
3852       TopExp_Explorer edgeExp( convFace._face, TopAbs_EDGE );
3853       for ( int iE = 0; edgeExp.More(); edgeExp.Next(), ++iE )
3854       {
3855         const TopoDS_Edge& edge = TopoDS::Edge( edgeExp.Current() );
3856
3857         // set adjacent FACE
3858         centerCurves[ iE ].SetShapes( edge, convFace, data, helper );
3859
3860         // get _LayerEdge's of the EDGE
3861         TGeomID edgeID = meshDS->ShapeToIndex( edge );
3862         id2end = convFace._subIdToEdgeEnd.find( edgeID );
3863         if ( id2end == convFace._subIdToEdgeEnd.end() )
3864         {
3865           // no _LayerEdge's on EDGE, use _LayerEdge's on VERTEXes
3866           for ( int iV = 0; iV < 2; ++iV )
3867           {
3868             TopoDS_Vertex v = helper.IthVertex( iV, edge );
3869             TGeomID     vID = meshDS->ShapeToIndex( v );
3870             int  end = convFace._subIdToEdgeEnd[ vID ];
3871             int iBeg = end > 0 ? data._endEdgeOnShape[ end-1 ] : 0;
3872             vertexLEdges[ iV ] = data._edges[ iBeg ];
3873           }
3874           edgeLEdge    = &vertexLEdges[0];
3875           edgeLEdgeEnd = edgeLEdge + 2;
3876
3877           centerCurves[ iE ]._adjFace.Nullify();
3878         }
3879         else
3880         {
3881           data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
3882           if ( id2end->second >= data._nbShapesToSmooth )
3883             data.SortOnEdge( edge, iBeg, iEnd, helper );
3884           edgeLEdge    = &data._edges[ iBeg ];
3885           edgeLEdgeEnd = edgeLEdge + iEnd - iBeg;
3886           vertexLEdges[0] = data._edges[ iBeg   ]->_2neibors->_edges[0];
3887           vertexLEdges[1] = data._edges[ iEnd-1 ]->_2neibors->_edges[1];
3888
3889           if ( ! data._edges[ iBeg ]->_sWOL.IsNull() )
3890             centerCurves[ iE ]._adjFace.Nullify();
3891         }
3892
3893         // Get curvature centers
3894
3895         centersBox.Clear();
3896
3897         if ( edgeLEdge[0]->IsOnEdge() &&
3898              convFace.GetCenterOfCurvature( vertexLEdges[0], surfProp, helper, center ))
3899         { // 1st VERTEX
3900           centerCurves[ iE ].Append( center, vertexLEdges[0] );
3901           centersBox.Add( center );
3902         }
3903         for ( ; edgeLEdge < edgeLEdgeEnd; ++edgeLEdge )
3904           if ( convFace.GetCenterOfCurvature( *edgeLEdge, surfProp, helper, center ))
3905           { // EDGE or VERTEXes
3906             centerCurves[ iE ].Append( center, *edgeLEdge );
3907             centersBox.Add( center );
3908           }
3909         if ( edgeLEdge[-1]->IsOnEdge() &&
3910              convFace.GetCenterOfCurvature( vertexLEdges[1], surfProp, helper, center ))
3911         { // 2nd VERTEX
3912           centerCurves[ iE ].Append( center, vertexLEdges[1] );
3913           centersBox.Add( center );
3914         }
3915         centerCurves[ iE ]._isDegenerated =
3916           ( centersBox.IsVoid() || centersBox.SquareExtent() < 1e-6 * nodesBox.SquareExtent() );
3917
3918       } // loop on EDGES of convFace._face to set up data of centerCurves
3919
3920       // Compute new normals for _LayerEdge's on EDGEs
3921
3922       double avgCosin = 0;
3923       int     nbCosin = 0;
3924       gp_Vec inFaceDir;
3925       for ( size_t iE1 = 0; iE1 < centerCurves.size(); ++iE1 )
3926       {
3927         _CentralCurveOnEdge& ceCurve = centerCurves[ iE1 ];
3928         if ( ceCurve._isDegenerated )
3929           continue;
3930         const vector< gp_Pnt >& centers = ceCurve._curvaCenters;
3931         vector< gp_XYZ > &   newNormals = ceCurve._normals;
3932         for ( size_t iC1 = 0; iC1 < centers.size(); ++iC1 )
3933         {
3934           isOK = false;
3935           for ( size_t iE2 = 0; iE2 < centerCurves.size() && !isOK; ++iE2 )
3936           {
3937             if ( iE1 != iE2 )
3938               isOK = centerCurves[ iE2 ].FindNewNormal( centers[ iC1 ], newNormals[ iC1 ]);
3939           }
3940           if ( isOK && !ceCurve._adjFace.IsNull() )
3941           {
3942             // compute new _LayerEdge::_cosin
3943             const SMDS_MeshNode* node = ceCurve._ledges[ iC1 ]->_nodes[0];
3944             inFaceDir = getFaceDir( ceCurve._adjFace, ceCurve._edge, node, helper, isOK );
3945             if ( isOK )
3946             {
3947               double angle = inFaceDir.Angle( newNormals[ iC1 ] ); // [0,PI]
3948               ceCurve._ledges[ iC1 ]->_cosin = Cos( angle );
3949               avgCosin += ceCurve._ledges[ iC1 ]->_cosin;
3950               nbCosin++;
3951             }
3952           }
3953         }
3954       }
3955       // set new normals to _LayerEdge's of NOT degenerated central curves
3956       for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
3957       {
3958         if ( centerCurves[ iE ]._isDegenerated )
3959           continue;
3960         for ( size_t iLE = 0; iLE < centerCurves[ iE ]._ledges.size(); ++iLE )
3961           centerCurves[ iE ]._ledges[ iLE ]->_normal = centerCurves[ iE ]._normals[ iLE ];
3962       }
3963       // set new normals to _LayerEdge's of     degenerated central curves
3964       for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
3965       {
3966         if ( !centerCurves[ iE ]._isDegenerated ||
3967              centerCurves[ iE ]._ledges.size() < 3 )
3968           continue;
3969         // new normal is an average of new normals at VERTEXes that
3970         // was computed on non-degenerated _CentralCurveOnEdge's
3971         gp_XYZ newNorm = ( centerCurves[ iE ]._ledges.front()->_normal +
3972                            centerCurves[ iE ]._ledges.back ()->_normal );
3973         double sz = newNorm.Modulus();
3974         if ( sz < 1e-200 )
3975           continue;
3976         newNorm /= sz;
3977         double newCosin = ( 0.5 * centerCurves[ iE ]._ledges.front()->_cosin +
3978                             0.5 * centerCurves[ iE ]._ledges.back ()->_cosin );
3979         for ( size_t iLE = 1, nb = centerCurves[ iE ]._ledges.size() - 1; iLE < nb; ++iLE )
3980         {
3981           centerCurves[ iE ]._ledges[ iLE ]->_normal = newNorm;
3982           centerCurves[ iE ]._ledges[ iLE ]->_cosin  = newCosin;
3983         }
3984       }
3985
3986       // Find new normals for _LayerEdge's based on FACE
3987
3988       if ( nbCosin > 0 )
3989         avgCosin /= nbCosin;
3990       const TGeomID faceID = meshDS->ShapeToIndex( convFace._face );
3991       map< TGeomID, int >::iterator id2end = convFace._subIdToEdgeEnd.find( faceID );
3992       if ( id2end != convFace._subIdToEdgeEnd.end() )
3993       {
3994         int iE = 0;
3995         gp_XYZ newNorm;
3996         data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
3997         for ( ; iBeg < iEnd; ++iBeg )
3998         {
3999           _LayerEdge* ledge = data._edges[ iBeg ];
4000           if ( !convFace.GetCenterOfCurvature( ledge, surfProp, helper, center ))
4001             continue;
4002           for ( size_t i = 0; i < centerCurves.size(); ++i, ++iE )
4003           {
4004             iE = iE % centerCurves.size();
4005             if ( centerCurves[ iE ]._isDegenerated )
4006               continue;
4007             newNorm.SetCoord( 0,0,0 );
4008             if ( centerCurves[ iE ].FindNewNormal( center, newNorm ))
4009             {
4010               ledge->_normal = newNorm;
4011               ledge->_cosin  = avgCosin;
4012               break;
4013             }
4014           }
4015         }
4016       }
4017
4018     } // not a quasi-spherical FACE
4019
4020     // Update _LayerEdge's data according to a new normal
4021
4022     dumpFunction(SMESH_Comment("updateNormalsOfConvexFaces")<<data._index
4023                  <<"_F"<<meshDS->ShapeToIndex( convFace._face ));
4024
4025     id2end = convFace._subIdToEdgeEnd.begin();
4026     for ( ; id2end != convFace._subIdToEdgeEnd.end(); ++id2end )
4027     {
4028       data.GetEdgesOnShape( id2end->second, iBeg, iEnd );
4029       for ( ; iBeg < iEnd; ++iBeg )
4030       {
4031         _LayerEdge* & ledge = data._edges[ iBeg ];
4032         double len = ledge->_len;
4033         ledge->InvalidateStep( stepNb + 1, /*restoreLength=*/true );
4034         ledge->SetCosin( ledge->_cosin );
4035         ledge->SetNewLength( len, helper );
4036       }
4037
4038     } // loop on sub-shapes of convFace._face
4039
4040     // Find FACEs adjacent to convFace._face that got necessity to smooth
4041     // as a result of normals modification
4042
4043     set< TGeomID > adjFacesToSmooth;
4044     for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
4045     {
4046       if ( centerCurves[ iE ]._adjFace.IsNull() ||
4047            centerCurves[ iE ]._adjFaceToSmooth )
4048         continue;
4049       for ( size_t iLE = 0; iLE < centerCurves[ iE ]._ledges.size(); ++iLE )
4050       {
4051         if ( centerCurves[ iE ]._ledges[ iLE ]->_cosin > theMinSmoothCosin )
4052         {
4053           adjFacesToSmooth.insert( meshDS->ShapeToIndex( centerCurves[ iE ]._adjFace ));
4054           break;
4055         }
4056       }
4057     }
4058     data.AddShapesToSmooth( adjFacesToSmooth );
4059
4060     dumpFunctionEnd();
4061
4062
4063   } // loop on data._convexFaces
4064
4065   return true;
4066 }
4067
4068 //================================================================================
4069 /*!
4070  * \brief Finds a center of curvature of a surface at a _LayerEdge
4071  */
4072 //================================================================================
4073
4074 bool _ConvexFace::GetCenterOfCurvature( _LayerEdge*         ledge,
4075                                         BRepLProp_SLProps&  surfProp,
4076                                         SMESH_MesherHelper& helper,
4077                                         gp_Pnt &            center ) const
4078 {
4079   gp_XY uv = helper.GetNodeUV( _face, ledge->_nodes[0] );
4080   surfProp.SetParameters( uv.X(), uv.Y() );
4081   if ( !surfProp.IsCurvatureDefined() )
4082     return false;
4083
4084   const double oriFactor = ( _face.Orientation() == TopAbs_REVERSED ? +1. : -1. );
4085   double surfCurvatureMax = surfProp.MaxCurvature() * oriFactor;
4086   double surfCurvatureMin = surfProp.MinCurvature() * oriFactor;
4087   if ( surfCurvatureMin > surfCurvatureMax )
4088     center = surfProp.Value().Translated( surfProp.Normal().XYZ() / surfCurvatureMin * oriFactor );
4089   else
4090     center = surfProp.Value().Translated( surfProp.Normal().XYZ() / surfCurvatureMax * oriFactor );
4091
4092   return true;
4093 }
4094
4095 //================================================================================
4096 /*!
4097  * \brief Check that prisms are not distorted
4098  */
4099 //================================================================================
4100
4101 bool _ConvexFace::CheckPrisms() const
4102 {
4103   for ( size_t i = 0; i < _simplexTestEdges.size(); ++i )
4104   {
4105     const _LayerEdge* edge = _simplexTestEdges[i];
4106     SMESH_TNodeXYZ tgtXYZ( edge->_nodes.back() );
4107     for ( size_t j = 0; j < edge->_simplices.size(); ++j )
4108       if ( !edge->_simplices[j].IsForward( edge->_nodes[0], &tgtXYZ ))
4109       {
4110         debugMsg( "Bad simplex of _simplexTestEdges ("
4111                   << " "<< edge->_nodes[0]->GetID()<< " "<< tgtXYZ._node->GetID()
4112                   << " "<< edge->_simplices[j]._nPrev->GetID()
4113                   << " "<< edge->_simplices[j]._nNext->GetID() << " )" );
4114         return false;
4115       }
4116   }
4117   return true;
4118 }
4119
4120 //================================================================================
4121 /*!
4122  * \brief Try to compute a new normal by interpolating normals of _LayerEdge's
4123  *        stored in this _CentralCurveOnEdge.
4124  *  \param [in] center - curvature center of a point of another _CentralCurveOnEdge.
4125  *  \param [in,out] newNormal - current normal at this point, to be redefined
4126  *  \return bool - true if succeeded.
4127  */
4128 //================================================================================
4129
4130 bool _CentralCurveOnEdge::FindNewNormal( const gp_Pnt& center, gp_XYZ& newNormal )
4131 {
4132   if ( this->_isDegenerated )
4133     return false;
4134
4135   // find two centers the given one lies between
4136
4137   for ( size_t i = 0, nb = _curvaCenters.size()-1;  i < nb;  ++i )
4138   {
4139     double sl2 = 1.001 * _segLength2[ i ];
4140
4141     double d1 = center.SquareDistance( _curvaCenters[ i ]);
4142     if ( d1 > sl2 )
4143       continue;
4144     
4145     double d2 = center.SquareDistance( _curvaCenters[ i+1 ]);
4146     if ( d2 > sl2 || d2 + d1 < 1e-100 )
4147       continue;
4148
4149     d1 = Sqrt( d1 );
4150     d2 = Sqrt( d2 );
4151     double r = d1 / ( d1 + d2 );
4152     gp_XYZ norm = (( 1. - r ) * _ledges[ i   ]->_normal +
4153                    (      r ) * _ledges[ i+1 ]->_normal );
4154     norm.Normalize();
4155
4156     newNormal += norm;
4157     double sz = newNormal.Modulus();
4158     if ( sz < 1e-200 )
4159       break;
4160     newNormal /= sz;
4161     return true;
4162   }
4163   return false;
4164 }
4165
4166 //================================================================================
4167 /*!
4168  * \brief Set shape members
4169  */
4170 //================================================================================
4171
4172 void _CentralCurveOnEdge::SetShapes( const TopoDS_Edge&  edge,
4173                                      const _ConvexFace&  convFace,
4174                                      const _SolidData&   data,
4175                                      SMESH_MesherHelper& helper)
4176 {
4177   _edge = edge;
4178
4179   PShapeIteratorPtr fIt = helper.GetAncestors( edge, *helper.GetMesh(), TopAbs_FACE );
4180   while ( const TopoDS_Shape* F = fIt->next())
4181     if ( !convFace._face.IsSame( *F ))
4182     {
4183       _adjFace = TopoDS::Face( *F );
4184       _adjFaceToSmooth = false;
4185       // _adjFace already in a smoothing queue ?
4186       size_t end;
4187       TGeomID adjFaceID = helper.GetMeshDS()->ShapeToIndex( *F );
4188       if ( data.GetShapeEdges( adjFaceID, end ))
4189         _adjFaceToSmooth = ( end < data._nbShapesToSmooth );
4190       break;
4191     }
4192 }
4193
4194 //================================================================================
4195 /*!
4196  * \brief Looks for intersection of it's last segment with faces
4197  *  \param distance - returns shortest distance from the last node to intersection
4198  */
4199 //================================================================================
4200
4201 bool _LayerEdge::FindIntersection( SMESH_ElementSearcher&   searcher,
4202                                    double &                 distance,
4203                                    const double&            epsilon,
4204                                    const SMDS_MeshElement** face)
4205 {
4206   vector< const SMDS_MeshElement* > suspectFaces;
4207   double segLen;
4208   gp_Ax1 lastSegment = LastSegment(segLen);
4209   searcher.GetElementsNearLine( lastSegment, SMDSAbs_Face, suspectFaces );
4210
4211   bool segmentIntersected = false;
4212   distance = Precision::Infinite();
4213   int iFace = -1; // intersected face
4214   for ( size_t j = 0 ; j < suspectFaces.size() /*&& !segmentIntersected*/; ++j )
4215   {
4216     const SMDS_MeshElement* face = suspectFaces[j];
4217     if ( face->GetNodeIndex( _nodes.back() ) >= 0 ||
4218          face->GetNodeIndex( _nodes[0]     ) >= 0 )
4219       continue; // face sharing _LayerEdge node
4220     const int nbNodes = face->NbCornerNodes();
4221     bool intFound = false;
4222     double dist;
4223     SMDS_MeshElement::iterator nIt = face->begin_nodes();
4224     if ( nbNodes == 3 )
4225     {
4226       intFound = SegTriaInter( lastSegment, *nIt++, *nIt++, *nIt++, dist, epsilon );
4227     }
4228     else
4229     {
4230       const SMDS_MeshNode* tria[3];
4231       tria[0] = *nIt++;
4232       tria[1] = *nIt++;
4233       for ( int n2 = 2; n2 < nbNodes && !intFound; ++n2 )
4234       {
4235         tria[2] = *nIt++;
4236         intFound = SegTriaInter(lastSegment, tria[0], tria[1], tria[2], dist, epsilon );
4237         tria[1] = tria[2];
4238       }
4239     }
4240     if ( intFound )
4241     {
4242       if ( dist < segLen*(1.01) && dist > -(_len*_lenFactor-segLen) )
4243         segmentIntersected = true;
4244       if ( distance > dist )
4245         distance = dist, iFace = j;
4246     }
4247   }
4248   if ( iFace != -1 && face ) *face = suspectFaces[iFace];
4249
4250   if ( segmentIntersected )
4251   {
4252 #ifdef __myDEBUG
4253     SMDS_MeshElement::iterator nIt = suspectFaces[iFace]->begin_nodes();
4254     gp_XYZ intP( lastSegment.Location().XYZ() + lastSegment.Direction().XYZ() * distance );
4255     cout << "nodes: tgt " << _nodes.back()->GetID() << " src " << _nodes[0]->GetID()
4256          << ", intersection with face ("
4257          << (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()
4258          << ") at point (" << intP.X() << ", " << intP.Y() << ", " << intP.Z()
4259          << ") distance = " << distance - segLen<< endl;
4260 #endif
4261   }
4262
4263   distance -= segLen;
4264
4265   return segmentIntersected;
4266 }
4267
4268 //================================================================================
4269 /*!
4270  * \brief Returns size and direction of the last segment
4271  */
4272 //================================================================================
4273
4274 gp_Ax1 _LayerEdge::LastSegment(double& segLen) const
4275 {
4276   // find two non-coincident positions
4277   gp_XYZ orig = _pos.back();
4278   gp_XYZ dir;
4279   int iPrev = _pos.size() - 2;
4280   while ( iPrev >= 0 )
4281   {
4282     dir = orig - _pos[iPrev];
4283     if ( dir.SquareModulus() > 1e-100 )
4284       break;
4285     else
4286       iPrev--;
4287   }
4288
4289   // make gp_Ax1
4290   gp_Ax1 segDir;
4291   if ( iPrev < 0 )
4292   {
4293     segDir.SetLocation( SMESH_TNodeXYZ( _nodes[0] ));
4294     segDir.SetDirection( _normal );
4295     segLen = 0;
4296   }
4297   else
4298   {
4299     gp_Pnt pPrev = _pos[ iPrev ];
4300     if ( !_sWOL.IsNull() )
4301     {
4302       TopLoc_Location loc;
4303       if ( _sWOL.ShapeType() == TopAbs_EDGE )
4304       {
4305         double f,l;
4306         Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( _sWOL ), loc, f,l);
4307         pPrev = curve->Value( pPrev.X() ).Transformed( loc );
4308       }
4309       else
4310       {
4311         Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face(_sWOL), loc );
4312         pPrev = surface->Value( pPrev.X(), pPrev.Y() ).Transformed( loc );
4313       }
4314       dir = SMESH_TNodeXYZ( _nodes.back() ) - pPrev.XYZ();
4315     }
4316     segDir.SetLocation( pPrev );
4317     segDir.SetDirection( dir );
4318     segLen = dir.Modulus();
4319   }
4320
4321   return segDir;
4322 }
4323
4324 //================================================================================
4325 /*!
4326  * \brief Return the last position of the target node on a FACE. 
4327  *  \param [in] F - the FACE this _LayerEdge is inflated along
4328  *  \return gp_XY - result UV
4329  */
4330 //================================================================================
4331
4332 gp_XY _LayerEdge::LastUV( const TopoDS_Face& F ) const
4333 {
4334   if ( F.IsSame( _sWOL )) // F is my FACE
4335     return gp_XY( _pos.back().X(), _pos.back().Y() );
4336
4337   if ( _sWOL.IsNull() || _sWOL.ShapeType() != TopAbs_EDGE ) // wrong call
4338     return gp_XY( 1e100, 1e100 );
4339
4340   // _sWOL is EDGE of F; _pos.back().X() is the last U on the EDGE
4341   double f, l, u = _pos.back().X();
4342   Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( TopoDS::Edge(_sWOL), F, f,l);
4343   if ( !C2d.IsNull() && f <= u && u <= l )
4344     return C2d->Value( u ).XY();
4345
4346   return gp_XY( 1e100, 1e100 );
4347 }
4348
4349 //================================================================================
4350 /*!
4351  * \brief Test intersection of the last segment with a given triangle
4352  *   using Moller-Trumbore algorithm
4353  * Intersection is detected if distance to intersection is less than _LayerEdge._len
4354  */
4355 //================================================================================
4356
4357 bool _LayerEdge::SegTriaInter( const gp_Ax1&        lastSegment,
4358                                const SMDS_MeshNode* n0,
4359                                const SMDS_MeshNode* n1,
4360                                const SMDS_MeshNode* n2,
4361                                double&              t,
4362                                const double&        EPSILON) const
4363 {
4364   //const double EPSILON = 1e-6;
4365
4366   gp_XYZ orig = lastSegment.Location().XYZ();
4367   gp_XYZ dir  = lastSegment.Direction().XYZ();
4368
4369   SMESH_TNodeXYZ vert0( n0 );
4370   SMESH_TNodeXYZ vert1( n1 );
4371   SMESH_TNodeXYZ vert2( n2 );
4372
4373   /* calculate distance from vert0 to ray origin */
4374   gp_XYZ tvec = orig - vert0;
4375
4376   //if ( tvec * dir > EPSILON )
4377     // intersected face is at back side of the temporary face this _LayerEdge belongs to
4378     //return false;
4379
4380   gp_XYZ edge1 = vert1 - vert0;
4381   gp_XYZ edge2 = vert2 - vert0;
4382
4383   /* begin calculating determinant - also used to calculate U parameter */
4384   gp_XYZ pvec = dir ^ edge2;
4385
4386   /* if determinant is near zero, ray lies in plane of triangle */
4387   double det = edge1 * pvec;
4388
4389   if (det > -EPSILON && det < EPSILON)
4390     return false;
4391   double inv_det = 1.0 / det;
4392
4393   /* calculate U parameter and test bounds */
4394   double u = ( tvec * pvec ) * inv_det;
4395   //if (u < 0.0 || u > 1.0)
4396   if (u < -EPSILON || u > 1.0 + EPSILON)
4397     return false;
4398
4399   /* prepare to test V parameter */
4400   gp_XYZ qvec = tvec ^ edge1;
4401
4402   /* calculate V parameter and test bounds */
4403   double v = (dir * qvec) * inv_det;
4404   //if ( v < 0.0 || u + v > 1.0 )
4405   if ( v < -EPSILON || u + v > 1.0 + EPSILON)
4406     return false;
4407
4408   /* calculate t, ray intersects triangle */
4409   t = (edge2 * qvec) * inv_det;
4410
4411   //return true;
4412   return t > 0.;
4413 }
4414
4415 //================================================================================
4416 /*!
4417  * \brief Perform smooth of _LayerEdge's based on EDGE's
4418  *  \retval bool - true if node has been moved
4419  */
4420 //================================================================================
4421
4422 bool _LayerEdge::SmoothOnEdge(Handle(Geom_Surface)& surface,
4423                               const TopoDS_Face&    F,
4424                               SMESH_MesherHelper&   helper)
4425 {
4426   ASSERT( IsOnEdge() );
4427
4428   SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _nodes.back() );
4429   SMESH_TNodeXYZ oldPos( tgtNode );
4430   double dist01, distNewOld;
4431   
4432   SMESH_TNodeXYZ p0( _2neibors->tgtNode(0));
4433   SMESH_TNodeXYZ p1( _2neibors->tgtNode(1));
4434   dist01 = p0.Distance( _2neibors->tgtNode(1) );
4435
4436   gp_Pnt newPos = p0 * _2neibors->_wgt[0] + p1 * _2neibors->_wgt[1];
4437   double lenDelta = 0;
4438   if ( _curvature )
4439   {
4440     //lenDelta = _curvature->lenDelta( _len );
4441     lenDelta = _curvature->lenDeltaByDist( dist01 );
4442     newPos.ChangeCoord() += _normal * lenDelta;
4443   }
4444
4445   distNewOld = newPos.Distance( oldPos );
4446
4447   if ( F.IsNull() )
4448   {
4449     if ( _2neibors->_plnNorm )
4450     {
4451       // put newPos on the plane defined by source node and _plnNorm
4452       gp_XYZ new2src = SMESH_TNodeXYZ( _nodes[0] ) - newPos.XYZ();
4453       double new2srcProj = (*_2neibors->_plnNorm) * new2src;
4454       newPos.ChangeCoord() += (*_2neibors->_plnNorm) * new2srcProj;
4455     }
4456     tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
4457     _pos.back() = newPos.XYZ();
4458   }
4459   else
4460   {
4461     tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
4462     gp_XY uv( Precision::Infinite(), 0 );
4463     helper.CheckNodeUV( F, tgtNode, uv, 1e-10, /*force=*/true );
4464     _pos.back().SetCoord( uv.X(), uv.Y(), 0 );
4465
4466     newPos = surface->Value( uv.X(), uv.Y() );
4467     tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
4468   }
4469
4470   if ( _curvature && lenDelta < 0 )
4471   {
4472     gp_Pnt prevPos( _pos[ _pos.size()-2 ]);
4473     _len -= prevPos.Distance( oldPos );
4474     _len += prevPos.Distance( newPos );
4475   }
4476   bool moved = distNewOld > dist01/50;
4477   //if ( moved )
4478   dumpMove( tgtNode ); // debug
4479
4480   return moved;
4481 }
4482
4483 //================================================================================
4484 /*!
4485  * \brief Perform laplacian smooth in 3D of nodes inflated from FACE
4486  *  \retval bool - true if _tgtNode has been moved
4487  */
4488 //================================================================================
4489
4490 bool _LayerEdge::Smooth(int& badNb)
4491 {
4492   if ( _simplices.size() < 2 )
4493     return false; // _LayerEdge inflated along EDGE or FACE
4494
4495   // compute new position for the last _pos
4496   gp_XYZ newPos (0,0,0);
4497   for ( size_t i = 0; i < _simplices.size(); ++i )
4498     newPos += SMESH_TNodeXYZ( _simplices[i]._nPrev );
4499   newPos /= _simplices.size();
4500
4501   const gp_XYZ& curPos ( _pos.back() );
4502   const gp_Pnt  prevPos( _pos[ _pos.size()-2 ]);
4503   if ( _curvature )
4504   {
4505     double delta  = _curvature->lenDelta( _len );
4506     if ( delta > 0 )
4507       newPos += _normal * delta;
4508     else
4509     {
4510       double segLen = _normal * ( newPos - prevPos.XYZ() );
4511       if ( segLen + delta > 0 )
4512         newPos += _normal * delta;
4513     }
4514     // double segLenChange = _normal * ( curPos - newPos );
4515     // newPos += 0.5 * _normal * segLenChange;
4516   }
4517
4518   // count quality metrics (orientation) of tetras around _tgtNode
4519   int nbOkBefore = 0;
4520   for ( size_t i = 0; i < _simplices.size(); ++i )
4521     nbOkBefore += _simplices[i].IsForward( _nodes[0], &curPos );
4522
4523   int nbOkAfter = 0;
4524   for ( size_t i = 0; i < _simplices.size(); ++i )
4525     nbOkAfter += _simplices[i].IsForward( _nodes[0], &newPos );
4526
4527   if ( nbOkAfter < nbOkBefore )
4528     return false;
4529
4530   SMDS_MeshNode* n = const_cast< SMDS_MeshNode* >( _nodes.back() );
4531
4532   _len -= prevPos.Distance(SMESH_TNodeXYZ( n ));
4533   _len += prevPos.Distance(newPos);
4534
4535   n->setXYZ( newPos.X(), newPos.Y(), newPos.Z());
4536   _pos.back() = newPos;
4537
4538   badNb += _simplices.size() - nbOkAfter;
4539
4540   dumpMove( n );
4541
4542   return true;
4543 }
4544
4545 //================================================================================
4546 /*!
4547  * \brief Add a new segment to _LayerEdge during inflation
4548  */
4549 //================================================================================
4550
4551 void _LayerEdge::SetNewLength( double len, SMESH_MesherHelper& helper )
4552 {
4553   if ( _len - len > -1e-6 )
4554   {
4555     _pos.push_back( _pos.back() );
4556     return;
4557   }
4558
4559   SMDS_MeshNode* n = const_cast< SMDS_MeshNode*>( _nodes.back() );
4560   SMESH_TNodeXYZ oldXYZ( n );
4561   gp_XYZ nXYZ = oldXYZ + _normal * ( len - _len ) * _lenFactor;
4562   n->setXYZ( nXYZ.X(), nXYZ.Y(), nXYZ.Z() );
4563
4564   _pos.push_back( nXYZ );
4565   _len = len;
4566   if ( !_sWOL.IsNull() )
4567   {
4568     double distXYZ[4];
4569     if ( _sWOL.ShapeType() == TopAbs_EDGE )
4570     {
4571       double u = Precision::Infinite(); // to force projection w/o distance check
4572       helper.CheckNodeU( TopoDS::Edge( _sWOL ), n, u, 1e-10, /*force=*/true, distXYZ );
4573       _pos.back().SetCoord( u, 0, 0 );
4574       if ( _nodes.size() > 1 )
4575       {
4576         SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( n->GetPosition() );
4577         pos->SetUParameter( u );
4578       }
4579     }
4580     else //  TopAbs_FACE
4581     {
4582       gp_XY uv( Precision::Infinite(), 0 );
4583       helper.CheckNodeUV( TopoDS::Face( _sWOL ), n, uv, 1e-10, /*force=*/true, distXYZ );
4584       _pos.back().SetCoord( uv.X(), uv.Y(), 0 );
4585       if ( _nodes.size() > 1 )
4586       {
4587         SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( n->GetPosition() );
4588         pos->SetUParameter( uv.X() );
4589         pos->SetVParameter( uv.Y() );
4590       }
4591     }
4592     n->setXYZ( distXYZ[1], distXYZ[2], distXYZ[3]);
4593   }
4594   dumpMove( n ); //debug
4595 }
4596
4597 //================================================================================
4598 /*!
4599  * \brief Remove last inflation step
4600  */
4601 //================================================================================
4602
4603 void _LayerEdge::InvalidateStep( int curStep, bool restoreLength )
4604 {
4605   if ( _pos.size() > curStep )
4606   {
4607     if ( restoreLength )
4608       _len -= ( _pos[ curStep-1 ] - _pos.back() ).Modulus();
4609
4610     _pos.resize( curStep );
4611     gp_Pnt nXYZ = _pos.back();
4612     SMDS_MeshNode* n = const_cast< SMDS_MeshNode*>( _nodes.back() );
4613     if ( !_sWOL.IsNull() )
4614     {
4615       TopLoc_Location loc;
4616       if ( _sWOL.ShapeType() == TopAbs_EDGE )
4617       {
4618         SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( n->GetPosition() );
4619         pos->SetUParameter( nXYZ.X() );
4620         double f,l;
4621         Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( _sWOL ), loc, f,l);
4622         nXYZ = curve->Value( nXYZ.X() ).Transformed( loc );
4623       }
4624       else
4625       {
4626         SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( n->GetPosition() );
4627         pos->SetUParameter( nXYZ.X() );
4628         pos->SetVParameter( nXYZ.Y() );
4629         Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face(_sWOL), loc );
4630         nXYZ = surface->Value( nXYZ.X(), nXYZ.Y() ).Transformed( loc );
4631       }
4632     }
4633     n->setXYZ( nXYZ.X(), nXYZ.Y(), nXYZ.Z() );
4634     dumpMove( n );
4635   }
4636 }
4637
4638 //================================================================================
4639 /*!
4640  * \brief Create layers of prisms
4641  */
4642 //================================================================================
4643
4644 bool _ViscousBuilder::refine(_SolidData& data)
4645 {
4646   SMESH_MesherHelper helper( *_mesh );
4647   helper.SetSubShape( data._solid );
4648   helper.SetElementsOnShape(false);
4649
4650   Handle(Geom_Curve) curve;
4651   Handle(Geom_Surface) surface;
4652   TopoDS_Edge geomEdge;
4653   TopoDS_Face geomFace;
4654   TopoDS_Shape prevSWOL;
4655   TopLoc_Location loc;
4656   double f,l, u;
4657   gp_XY uv;
4658   bool isOnEdge;
4659   TGeomID prevBaseId = -1;
4660   TNode2Edge* n2eMap = 0;
4661   TNode2Edge::iterator n2e;
4662
4663   // Create intermediate nodes on each _LayerEdge
4664
4665   for ( size_t i = 0; i < data._edges.size(); ++i )
4666   {
4667     _LayerEdge& edge = *data._edges[i];
4668
4669     if ( edge._nodes.size() < 2 )
4670       continue; // on _noShrinkShapes
4671
4672     // get accumulated length of segments
4673     vector< double > segLen( edge._pos.size() );
4674     segLen[0] = 0.0;
4675     for ( size_t j = 1; j < edge._pos.size(); ++j )
4676       segLen[j] = segLen[j-1] + (edge._pos[j-1] - edge._pos[j] ).Modulus();
4677
4678     // allocate memory for new nodes if it is not yet refined
4679     const SMDS_MeshNode* tgtNode = edge._nodes.back();
4680     if ( edge._nodes.size() == 2 )
4681     {
4682       edge._nodes.resize( data._hyp->GetNumberLayers() + 1, 0 );
4683       edge._nodes[1] = 0;
4684       edge._nodes.back() = tgtNode;
4685     }
4686     // get data of a shrink shape
4687     if ( !edge._sWOL.IsNull() && edge._sWOL != prevSWOL )
4688     {
4689       isOnEdge = ( edge._sWOL.ShapeType() == TopAbs_EDGE );
4690       if ( isOnEdge )
4691       {
4692         geomEdge = TopoDS::Edge( edge._sWOL );
4693         curve    = BRep_Tool::Curve( geomEdge, loc, f,l);
4694       }
4695       else
4696       {
4697         geomFace = TopoDS::Face( edge._sWOL );
4698         surface  = BRep_Tool::Surface( geomFace, loc );
4699       }
4700       prevSWOL = edge._sWOL;
4701     }
4702     // restore shapePos of the last node by already treated _LayerEdge of another _SolidData
4703     const TGeomID baseShapeId = edge._nodes[0]->getshapeId();
4704     if ( baseShapeId != prevBaseId )
4705     {
4706       map< TGeomID, TNode2Edge* >::iterator s2ne = data._s2neMap.find( baseShapeId );
4707       n2eMap = ( s2ne == data._s2neMap.end() ) ? 0 : n2eMap = s2ne->second;
4708       prevBaseId = baseShapeId;
4709     }
4710     _LayerEdge* edgeOnSameNode = 0;
4711     if ( n2eMap && (( n2e = n2eMap->find( edge._nodes[0] )) != n2eMap->end() ))
4712     {
4713       edgeOnSameNode = n2e->second;
4714       const gp_XYZ& otherTgtPos = edgeOnSameNode->_pos.back();
4715       SMDS_PositionPtr  lastPos = tgtNode->GetPosition();
4716       if ( isOnEdge )
4717       {
4718         SMDS_EdgePosition* epos = static_cast<SMDS_EdgePosition*>( lastPos );
4719         epos->SetUParameter( otherTgtPos.X() );
4720       }
4721       else
4722       {
4723         SMDS_FacePosition* fpos = static_cast<SMDS_FacePosition*>( lastPos );
4724         fpos->SetUParameter( otherTgtPos.X() );
4725         fpos->SetVParameter( otherTgtPos.Y() );
4726       }
4727     }
4728     // calculate height of the first layer
4729     double h0;
4730     const double T = segLen.back(); //data._hyp.GetTotalThickness();
4731     const double f = data._hyp->GetStretchFactor();
4732     const int    N = data._hyp->GetNumberLayers();
4733     const double fPowN = pow( f, N );
4734     if ( fPowN - 1 <= numeric_limits<double>::min() )
4735       h0 = T / N;
4736     else
4737       h0 = T * ( f - 1 )/( fPowN - 1 );
4738
4739     const double zeroLen = std::numeric_limits<double>::min();
4740
4741     // create intermediate nodes
4742     double hSum = 0, hi = h0/f;
4743     size_t iSeg = 1;
4744     for ( size_t iStep = 1; iStep < edge._nodes.size(); ++iStep )
4745     {
4746       // compute an intermediate position
4747       hi *= f;
4748       hSum += hi;
4749       while ( hSum > segLen[iSeg] && iSeg < segLen.size()-1)
4750         ++iSeg;
4751       int iPrevSeg = iSeg-1;
4752       while ( fabs( segLen[iPrevSeg] - segLen[iSeg]) <= zeroLen && iPrevSeg > 0 )
4753         --iPrevSeg;
4754       double r = ( segLen[iSeg] - hSum ) / ( segLen[iSeg] - segLen[iPrevSeg] );
4755       gp_Pnt pos = r * edge._pos[iPrevSeg] + (1-r) * edge._pos[iSeg];
4756
4757       SMDS_MeshNode*& node = const_cast< SMDS_MeshNode*& >( edge._nodes[ iStep ]);
4758       if ( !edge._sWOL.IsNull() )
4759       {
4760         // compute XYZ by parameters <pos>
4761         if ( isOnEdge )
4762         {
4763           u = pos.X();
4764           if ( !node )
4765             pos = curve->Value( u ).Transformed(loc);
4766         }
4767         else
4768         {
4769           uv.SetCoord( pos.X(), pos.Y() );
4770           if ( !node )
4771             pos = surface->Value( pos.X(), pos.Y() ).Transformed(loc);
4772         }
4773       }
4774       // create or update the node
4775       if ( !node )
4776       {
4777         node = helper.AddNode( pos.X(), pos.Y(), pos.Z());
4778         if ( !edge._sWOL.IsNull() )
4779         {
4780           if ( isOnEdge )
4781             getMeshDS()->SetNodeOnEdge( node, geomEdge, u );
4782           else
4783             getMeshDS()->SetNodeOnFace( node, geomFace, uv.X(), uv.Y() );
4784         }
4785         else
4786         {
4787           getMeshDS()->SetNodeInVolume( node, helper.GetSubShapeID() );
4788         }
4789       }
4790       else
4791       {
4792         if ( !edge._sWOL.IsNull() )
4793         {
4794           // make average pos from new and current parameters
4795           if ( isOnEdge )
4796           {
4797             u = 0.5 * ( u + helper.GetNodeU( geomEdge, node ));
4798             pos = curve->Value( u ).Transformed(loc);
4799
4800             SMDS_EdgePosition* epos = static_cast<SMDS_EdgePosition*>( node->GetPosition() );
4801             epos->SetUParameter( u );
4802           }
4803           else
4804           {
4805             uv = 0.5 * ( uv + helper.GetNodeUV( geomFace, node ));
4806             pos = surface->Value( uv.X(), uv.Y()).Transformed(loc);
4807
4808             SMDS_FacePosition* fpos = static_cast<SMDS_FacePosition*>( node->GetPosition() );
4809             fpos->SetUParameter( uv.X() );
4810             fpos->SetVParameter( uv.Y() );
4811           }
4812         }
4813         node->setXYZ( pos.X(), pos.Y(), pos.Z() );
4814       }
4815     } // loop on edge._nodes
4816
4817     if ( !edge._sWOL.IsNull() ) // prepare for shrink()
4818     {
4819       if ( isOnEdge )
4820         edge._pos.back().SetCoord( u, 0,0);
4821       else
4822         edge._pos.back().SetCoord( uv.X(), uv.Y() ,0);
4823
4824       if ( edgeOnSameNode )
4825         edgeOnSameNode->_pos.back() = edge._pos.back();
4826     }
4827
4828   } // loop on data._edges to create nodes
4829
4830   if ( !getMeshDS()->IsEmbeddedMode() )
4831     // Log node movement
4832     for ( size_t i = 0; i < data._edges.size(); ++i )
4833     {
4834       _LayerEdge& edge = *data._edges[i];
4835       SMESH_TNodeXYZ p ( edge._nodes.back() );
4836       getMeshDS()->MoveNode( p._node, p.X(), p.Y(), p.Z() );
4837     }
4838
4839   // Create volumes
4840
4841   helper.SetElementsOnShape(true);
4842
4843   vector< vector<const SMDS_MeshNode*>* > nnVec;
4844   set< int > degenEdgeInd;
4845   vector<const SMDS_MeshElement*> degenVols;
4846
4847   TopExp_Explorer exp( data._solid, TopAbs_FACE );
4848   for ( ; exp.More(); exp.Next() )
4849   {
4850     if ( data._ignoreFaceIds.count( getMeshDS()->ShapeToIndex( exp.Current() )))
4851       continue;
4852     SMESHDS_SubMesh*   fSubM = getMeshDS()->MeshElements( exp.Current() );
4853     SMDS_ElemIteratorPtr fIt = fSubM->GetElements();
4854     while ( fIt->more() )
4855     {
4856       const SMDS_MeshElement* face = fIt->next();
4857       const int            nbNodes = face->NbCornerNodes();
4858       nnVec.resize( nbNodes );
4859       degenEdgeInd.clear();
4860       int nbZ = 0;
4861       SMDS_ElemIteratorPtr nIt = face->nodesIterator();
4862       for ( int iN = 0; iN < nbNodes; ++iN )
4863       {
4864         const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nIt->next() );
4865         nnVec[ iN ] = & data._n2eMap[ n ]->_nodes;
4866         if ( nnVec[ iN ]->size() < 2 )
4867           degenEdgeInd.insert( iN );
4868         else
4869           nbZ = nnVec[ iN ]->size();
4870       }
4871       if ( nbZ == 0 )
4872         continue;
4873
4874       switch ( nbNodes )
4875       {
4876       case 3:
4877         switch ( degenEdgeInd.size() )
4878         {
4879         case 0: // PENTA
4880         {
4881           for ( int iZ = 1; iZ < nbZ; ++iZ )
4882             helper.AddVolume( (*nnVec[0])[iZ-1], (*nnVec[1])[iZ-1], (*nnVec[2])[iZ-1],
4883                               (*nnVec[0])[iZ],   (*nnVec[1])[iZ],   (*nnVec[2])[iZ]);
4884           break;
4885         }
4886         case 1: // PYRAM
4887         {
4888           int i2 = *degenEdgeInd.begin();
4889           int i0 = helper.WrapIndex( i2 - 1, nbNodes );
4890           int i1 = helper.WrapIndex( i2 + 1, nbNodes );
4891           for ( int iZ = 1; iZ < nbZ; ++iZ )
4892             helper.AddVolume( (*nnVec[i0])[iZ-1], (*nnVec[i1])[iZ-1],
4893                               (*nnVec[i1])[iZ],   (*nnVec[i0])[iZ],   (*nnVec[i2])[0]);
4894           break;
4895         }
4896         case 2: // TETRA
4897         {
4898           int i3 = !degenEdgeInd.count(0) ? 0 : !degenEdgeInd.count(1) ? 1 : 2;
4899           for ( int iZ = 1; iZ < nbZ; ++iZ )
4900             helper.AddVolume( (*nnVec[0])[iZ-1], (*nnVec[1])[iZ-1], (*nnVec[2])[iZ-1],
4901                               (*nnVec[i3])[iZ]);
4902           break;
4903         }
4904         }
4905         break;
4906
4907       case 4:
4908         switch ( degenEdgeInd.size() )
4909         {
4910         case 0: // HEX
4911         {
4912           for ( int iZ = 1; iZ < nbZ; ++iZ )
4913             helper.AddVolume( (*nnVec[0])[iZ-1], (*nnVec[1])[iZ-1],
4914                               (*nnVec[2])[iZ-1], (*nnVec[3])[iZ-1],
4915                               (*nnVec[0])[iZ],   (*nnVec[1])[iZ],
4916                               (*nnVec[2])[iZ],   (*nnVec[3])[iZ]);
4917           break;
4918         }
4919         case 2: // PENTA?
4920         {
4921           int i2 = *degenEdgeInd.begin();
4922           int i3 = *degenEdgeInd.rbegin();
4923           bool ok = ( i3 - i2 == 1 );
4924           if ( i2 == 0 && i3 == 3 ) { i2 = 3; i3 = 0; ok = true; }
4925           int i0 = helper.WrapIndex( i3 + 1, nbNodes );
4926           int i1 = helper.WrapIndex( i0 + 1, nbNodes );
4927           for ( int iZ = 1; iZ < nbZ; ++iZ )
4928           {
4929             const SMDS_MeshElement* vol =
4930               helper.AddVolume( (*nnVec[i3])[0], (*nnVec[i0])[iZ], (*nnVec[i0])[iZ-1],
4931                                 (*nnVec[i2])[0], (*nnVec[i1])[iZ], (*nnVec[i1])[iZ-1]);
4932             if ( !ok && vol )
4933               degenVols.push_back( vol );
4934           }
4935           break;
4936         }
4937         case 3: // degen HEX
4938         {
4939           const SMDS_MeshNode* nn[8];
4940           for ( int iZ = 1; iZ < nbZ; ++iZ )
4941           {
4942             const SMDS_MeshElement* vol =
4943               helper.AddVolume( nnVec[0]->size() > 1 ? (*nnVec[0])[iZ-1] : (*nnVec[0])[0],
4944                                 nnVec[1]->size() > 1 ? (*nnVec[1])[iZ-1] : (*nnVec[1])[0],
4945                                 nnVec[2]->size() > 1 ? (*nnVec[2])[iZ-1] : (*nnVec[2])[0],
4946                                 nnVec[3]->size() > 1 ? (*nnVec[3])[iZ-1] : (*nnVec[3])[0],
4947                                 nnVec[0]->size() > 1 ? (*nnVec[0])[iZ]   : (*nnVec[0])[0],
4948                                 nnVec[1]->size() > 1 ? (*nnVec[1])[iZ]   : (*nnVec[1])[0],
4949                                 nnVec[2]->size() > 1 ? (*nnVec[2])[iZ]   : (*nnVec[2])[0],
4950                                 nnVec[3]->size() > 1 ? (*nnVec[3])[iZ]   : (*nnVec[3])[0]);
4951             degenVols.push_back( vol );
4952           }
4953         }
4954         break;
4955         }
4956         break;
4957
4958       default:
4959         return error("Not supported type of element", data._index);
4960
4961       } // switch ( nbNodes )
4962     } // while ( fIt->more() )
4963   } // loop on FACEs
4964
4965   if ( !degenVols.empty() )
4966   {
4967     SMESH_ComputeErrorPtr& err = _mesh->GetSubMesh( data._solid )->GetComputeError();
4968     if ( !err || err->IsOK() )
4969     {
4970       err.reset( new SMESH_ComputeError( COMPERR_WARNING,
4971                                          "Degenerated volumes created" ));
4972       err->myBadElements.insert( err->myBadElements.end(),
4973                                  degenVols.begin(),degenVols.end() );
4974     }
4975   }
4976
4977   return true;
4978 }
4979
4980 //================================================================================
4981 /*!
4982  * \brief Shrink 2D mesh on faces to let space for inflated layers
4983  */
4984 //================================================================================
4985
4986 bool _ViscousBuilder::shrink()
4987 {
4988   // make map of (ids of FACEs to shrink mesh on) to (_SolidData containing _LayerEdge's
4989   // inflated along FACE or EDGE)
4990   map< TGeomID, _SolidData* > f2sdMap;
4991   for ( size_t i = 0 ; i < _sdVec.size(); ++i )
4992   {
4993     _SolidData& data = _sdVec[i];
4994     TopTools_MapOfShape FFMap;
4995     map< TGeomID, TopoDS_Shape >::iterator s2s = data._shrinkShape2Shape.begin();
4996     for (; s2s != data._shrinkShape2Shape.end(); ++s2s )
4997       if ( s2s->second.ShapeType() == TopAbs_FACE )
4998       {
4999         f2sdMap.insert( make_pair( getMeshDS()->ShapeToIndex( s2s->second ), &data ));
5000
5001         if ( FFMap.Add( (*s2s).second ))
5002           // Put mesh faces on the shrinked FACE to the proxy sub-mesh to avoid
5003           // usage of mesh faces made in addBoundaryElements() by the 3D algo or
5004           // by StdMeshers_QuadToTriaAdaptor
5005           if ( SMESHDS_SubMesh* smDS = getMeshDS()->MeshElements( s2s->second ))
5006           {
5007             SMESH_ProxyMesh::SubMesh* proxySub =
5008               data._proxyMesh->getFaceSubM( TopoDS::Face( s2s->second ), /*create=*/true);
5009             SMDS_ElemIteratorPtr fIt = smDS->GetElements();
5010             while ( fIt->more() )
5011               proxySub->AddElement( fIt->next() );
5012             // as a result 3D algo will use elements from proxySub and not from smDS
5013           }
5014       }
5015   }
5016
5017   SMESH_MesherHelper helper( *_mesh );
5018   helper.ToFixNodeParameters( true );
5019
5020   // EDGE's to shrink
5021   map< TGeomID, _Shrinker1D > e2shrMap;
5022   vector< _LayerEdge* > lEdges;
5023
5024   // loop on FACES to srink mesh on
5025   map< TGeomID, _SolidData* >::iterator f2sd = f2sdMap.begin();
5026   for ( ; f2sd != f2sdMap.end(); ++f2sd )
5027   {
5028     _SolidData&      data = *f2sd->second;
5029     const TopoDS_Face&  F = TopoDS::Face( getMeshDS()->IndexToShape( f2sd->first ));
5030     SMESH_subMesh*     sm = _mesh->GetSubMesh( F );
5031     SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
5032
5033     Handle(Geom_Surface) surface = BRep_Tool::Surface(F);
5034
5035     helper.SetSubShape(F);
5036
5037     // ===========================
5038     // Prepare data for shrinking
5039     // ===========================
5040
5041     // Collect nodes to smooth, as src nodes are not yet replaced by tgt ones
5042     // and thus all nodes on a FACE connected to 2d elements are to be smoothed
5043     vector < const SMDS_MeshNode* > smoothNodes;
5044     {
5045       SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
5046       while ( nIt->more() )
5047       {
5048         const SMDS_MeshNode* n = nIt->next();
5049         if ( n->NbInverseElements( SMDSAbs_Face ) > 0 )
5050           smoothNodes.push_back( n );
5051       }
5052     }
5053     // Find out face orientation
5054     double refSign = 1;
5055     const set<TGeomID> ignoreShapes;
5056     bool isOkUV;
5057     if ( !smoothNodes.empty() )
5058     {
5059       vector<_Simplex> simplices;
5060       getSimplices( smoothNodes[0], simplices, ignoreShapes );
5061       helper.GetNodeUV( F, simplices[0]._nPrev, 0, &isOkUV ); // fix UV of silpmex nodes
5062       helper.GetNodeUV( F, simplices[0]._nNext, 0, &isOkUV );
5063       gp_XY uv = helper.GetNodeUV( F, smoothNodes[0], 0, &isOkUV );
5064       if ( !simplices[0].IsForward(uv, smoothNodes[0], F, helper,refSign) )
5065         refSign = -1;
5066     }
5067
5068     // Find _LayerEdge's inflated along F
5069     lEdges.clear();
5070     {
5071       set< TGeomID > subIDs;
5072       SMESH_subMeshIteratorPtr subIt = sm->getDependsOnIterator(/*includeSelf=*/false);
5073       while ( subIt->more() )
5074         subIDs.insert( subIt->next()->GetId() );
5075
5076       int iBeg, iEnd = 0;
5077       for ( int iS = 0; iS < data._endEdgeOnShape.size() && !subIDs.empty(); ++iS )
5078       {
5079         iBeg = iEnd;
5080         iEnd = data._endEdgeOnShape[ iS ];
5081         TGeomID shapeID = data._edges[ iBeg ]->_nodes[0]->getshapeId();
5082         set< TGeomID >::iterator idIt = subIDs.find( shapeID );
5083         if ( idIt == subIDs.end() ||
5084              data._edges[ iBeg ]->_sWOL.IsNull() ) continue;
5085         subIDs.erase( idIt );
5086
5087         if ( !data._noShrinkShapes.count( shapeID ))
5088           for ( ; iBeg < iEnd; ++iBeg )
5089           {
5090             lEdges.push_back( data._edges[ iBeg ] );
5091             prepareEdgeToShrink( *data._edges[ iBeg ], F, helper, smDS );
5092           }
5093       }
5094     }
5095
5096     dumpFunction(SMESH_Comment("beforeShrinkFace")<<f2sd->first); // debug
5097     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
5098     while ( fIt->more() )
5099       if ( const SMDS_MeshElement* f = fIt->next() )
5100         dumpChangeNodes( f );
5101
5102     // Replace source nodes by target nodes in mesh faces to shrink
5103     dumpFunction(SMESH_Comment("replNodesOnFace")<<f2sd->first); // debug
5104     const SMDS_MeshNode* nodes[20];
5105     for ( size_t i = 0; i < lEdges.size(); ++i )
5106     {
5107       _LayerEdge& edge = *lEdges[i];
5108       const SMDS_MeshNode* srcNode = edge._nodes[0];
5109       const SMDS_MeshNode* tgtNode = edge._nodes.back();
5110       SMDS_ElemIteratorPtr fIt = srcNode->GetInverseElementIterator(SMDSAbs_Face);
5111       while ( fIt->more() )
5112       {
5113         const SMDS_MeshElement* f = fIt->next();
5114         if ( !smDS->Contains( f ))
5115           continue;
5116         SMDS_NodeIteratorPtr nIt = f->nodeIterator();
5117         for ( int iN = 0; nIt->more(); ++iN )
5118         {
5119           const SMDS_MeshNode* n = nIt->next();
5120           nodes[iN] = ( n == srcNode ? tgtNode : n );
5121         }
5122         helper.GetMeshDS()->ChangeElementNodes( f, nodes, f->NbNodes() );
5123         dumpChangeNodes( f );
5124       }
5125     }
5126
5127     // find out if a FACE is concave
5128     const bool isConcaveFace = isConcave( F, helper );
5129
5130     // Create _SmoothNode's on face F
5131     vector< _SmoothNode > nodesToSmooth( smoothNodes.size() );
5132     {
5133       dumpFunction(SMESH_Comment("fixUVOnFace")<<f2sd->first); // debug
5134       const bool sortSimplices = isConcaveFace;
5135       for ( size_t i = 0; i < smoothNodes.size(); ++i )
5136       {
5137         const SMDS_MeshNode* n = smoothNodes[i];
5138         nodesToSmooth[ i ]._node = n;
5139         // src nodes must be replaced by tgt nodes to have tgt nodes in _simplices
5140         getSimplices( n, nodesToSmooth[ i ]._simplices, ignoreShapes, NULL, sortSimplices );
5141         // fix up incorrect uv of nodes on the FACE
5142         helper.GetNodeUV( F, n, 0, &isOkUV);
5143         dumpMove( n );
5144       }
5145     }
5146     //if ( nodesToSmooth.empty() ) continue;
5147
5148     // Find EDGE's to shrink and set simpices to LayerEdge's
5149     set< _Shrinker1D* > eShri1D;
5150     {
5151       for ( size_t i = 0; i < lEdges.size(); ++i )
5152       {
5153         _LayerEdge* edge = lEdges[i];
5154         if ( edge->_sWOL.ShapeType() == TopAbs_EDGE )
5155         {
5156           TGeomID edgeIndex = getMeshDS()->ShapeToIndex( edge->_sWOL );
5157           _Shrinker1D& srinker = e2shrMap[ edgeIndex ];
5158           eShri1D.insert( & srinker );
5159           srinker.AddEdge( edge, helper );
5160           VISCOUS_3D::ToClearSubWithMain( _mesh->GetSubMesh( edge->_sWOL ), data._solid );
5161           // restore params of nodes on EGDE if the EDGE has been already
5162           // srinked while srinking another FACE
5163           srinker.RestoreParams();
5164         }
5165         getSimplices( /*tgtNode=*/edge->_nodes.back(), edge->_simplices, ignoreShapes );
5166       }
5167     }
5168
5169     bool toFixTria = false; // to improve quality of trias by diagonal swap
5170     if ( isConcaveFace )
5171     {
5172       const bool hasTria = _mesh->NbTriangles(), hasQuad = _mesh->NbQuadrangles();
5173       if ( hasTria != hasQuad ) {
5174         toFixTria = hasTria;
5175       }
5176       else {
5177         set<int> nbNodesSet;
5178         SMDS_ElemIteratorPtr fIt = smDS->GetElements();
5179         while ( fIt->more() && nbNodesSet.size() < 2 )
5180           nbNodesSet.insert( fIt->next()->NbCornerNodes() );
5181         toFixTria = ( *nbNodesSet.begin() == 3 );
5182       }
5183     }
5184
5185     // ==================
5186     // Perform shrinking
5187     // ==================
5188
5189     bool shrinked = true;
5190     int badNb, shriStep=0, smooStep=0;
5191     _SmoothNode::SmoothType smoothType
5192       = isConcaveFace ? _SmoothNode::ANGULAR : _SmoothNode::LAPLACIAN;
5193     while ( shrinked )
5194     {
5195       shriStep++;
5196       // Move boundary nodes (actually just set new UV)
5197       // -----------------------------------------------
5198       dumpFunction(SMESH_Comment("moveBoundaryOnF")<<f2sd->first<<"_st"<<shriStep ); // debug
5199       shrinked = false;
5200       for ( size_t i = 0; i < lEdges.size(); ++i )
5201       {
5202         shrinked |= lEdges[i]->SetNewLength2d( surface,F,helper );
5203       }
5204       dumpFunctionEnd();
5205
5206       // Move nodes on EDGE's
5207       // (XYZ is set as soon as a needed length reached in SetNewLength2d())
5208       set< _Shrinker1D* >::iterator shr = eShri1D.begin();
5209       for ( ; shr != eShri1D.end(); ++shr )
5210         (*shr)->Compute( /*set3D=*/false, helper );
5211
5212       // Smoothing in 2D
5213       // -----------------
5214       int nbNoImpSteps = 0;
5215       bool       moved = true;
5216       badNb = 1;
5217       while (( nbNoImpSteps < 5 && badNb > 0) && moved)
5218       {
5219         dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
5220
5221         int oldBadNb = badNb;
5222         badNb = 0;
5223         moved = false;
5224         for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
5225         {
5226           moved |= nodesToSmooth[i].Smooth( badNb,surface,helper,refSign,
5227                                             smoothType, /*set3D=*/isConcaveFace);
5228         }
5229         if ( badNb < oldBadNb )
5230           nbNoImpSteps = 0;
5231         else
5232           nbNoImpSteps++;
5233
5234         dumpFunctionEnd();
5235       }
5236       if ( badNb > 0 )
5237         return error(SMESH_Comment("Can't shrink 2D mesh on face ") << f2sd->first );
5238       if ( shriStep > 200 )
5239         return error(SMESH_Comment("Infinite loop at shrinking 2D mesh on face ") << f2sd->first );
5240
5241       // Fix narrow triangles by swapping diagonals
5242       // ---------------------------------------
5243       if ( toFixTria )
5244       {
5245         set<const SMDS_MeshNode*> usedNodes;
5246         fixBadFaces( F, helper, /*is2D=*/true, shriStep, & usedNodes); // swap diagonals
5247
5248         // update working data
5249         set<const SMDS_MeshNode*>::iterator n;
5250         for ( size_t i = 0; i < nodesToSmooth.size() && !usedNodes.empty(); ++i )
5251         {
5252           n = usedNodes.find( nodesToSmooth[ i ]._node );
5253           if ( n != usedNodes.end())
5254           {
5255             getSimplices( nodesToSmooth[ i ]._node,
5256                           nodesToSmooth[ i ]._simplices,
5257                           ignoreShapes, NULL,
5258                           /*sortSimplices=*/ smoothType == _SmoothNode::ANGULAR );
5259             usedNodes.erase( n );
5260           }
5261         }
5262         for ( size_t i = 0; i < lEdges.size() && !usedNodes.empty(); ++i )
5263         {
5264           n = usedNodes.find( /*tgtNode=*/ lEdges[i]->_nodes.back() );
5265           if ( n != usedNodes.end())
5266           {
5267             getSimplices( lEdges[i]->_nodes.back(),
5268                           lEdges[i]->_simplices,
5269                           ignoreShapes );
5270             usedNodes.erase( n );
5271           }
5272         }
5273       }
5274       // TODO: check effect of this additional smooth
5275       // additional laplacian smooth to increase allowed shrink step
5276       // for ( int st = 1; st; --st )
5277       // {
5278       //   dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
5279       //   for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
5280       //   {
5281       //     nodesToSmooth[i].Smooth( badNb,surface,helper,refSign,
5282       //                              _SmoothNode::LAPLACIAN,/*set3D=*/false);
5283       //   }
5284       // }
5285     } // while ( shrinked )
5286
5287     // No wrongly shaped faces remain; final smooth. Set node XYZ.
5288     bool isStructuredFixed = false;
5289     if ( SMESH_2D_Algo* algo = dynamic_cast<SMESH_2D_Algo*>( sm->GetAlgo() ))
5290       isStructuredFixed = algo->FixInternalNodes( *data._proxyMesh, F );
5291     if ( !isStructuredFixed )
5292     {
5293       if ( isConcaveFace ) // fix narrow faces by swapping diagonals
5294         fixBadFaces( F, helper, /*is2D=*/false, ++shriStep );
5295
5296       for ( int st = 3; st; --st )
5297       {
5298         switch( st ) {
5299         case 1: smoothType = _SmoothNode::LAPLACIAN; break;
5300         case 2: smoothType = _SmoothNode::LAPLACIAN; break;
5301         case 3: smoothType = _SmoothNode::ANGULAR; break;
5302         }
5303         dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
5304         for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
5305         {
5306           nodesToSmooth[i].Smooth( badNb,surface,helper,refSign,
5307                                    smoothType,/*set3D=*/st==1 );
5308         }
5309         dumpFunctionEnd();
5310       }
5311     }
5312     // Set an event listener to clear FACE sub-mesh together with SOLID sub-mesh
5313     VISCOUS_3D::ToClearSubWithMain( sm, data._solid );
5314
5315     if ( !getMeshDS()->IsEmbeddedMode() )
5316       // Log node movement
5317       for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
5318       {
5319         SMESH_TNodeXYZ p ( nodesToSmooth[i]._node );
5320         getMeshDS()->MoveNode( nodesToSmooth[i]._node, p.X(), p.Y(), p.Z() );
5321       }
5322
5323   } // loop on FACES to srink mesh on
5324
5325
5326   // Replace source nodes by target nodes in shrinked mesh edges
5327
5328   map< int, _Shrinker1D >::iterator e2shr = e2shrMap.begin();
5329   for ( ; e2shr != e2shrMap.end(); ++e2shr )
5330     e2shr->second.SwapSrcTgtNodes( getMeshDS() );
5331
5332   return true;
5333 }
5334
5335 //================================================================================
5336 /*!
5337  * \brief Computes 2d shrink direction and finds nodes limiting shrinking
5338  */
5339 //================================================================================
5340
5341 bool _ViscousBuilder::prepareEdgeToShrink( _LayerEdge&            edge,
5342                                            const TopoDS_Face&     F,
5343                                            SMESH_MesherHelper&    helper,
5344                                            const SMESHDS_SubMesh* faceSubMesh)
5345 {
5346   const SMDS_MeshNode* srcNode = edge._nodes[0];
5347   const SMDS_MeshNode* tgtNode = edge._nodes.back();
5348
5349   if ( edge._sWOL.ShapeType() == TopAbs_FACE )
5350   {
5351     gp_XY srcUV( edge._pos[0].X(), edge._pos[0].Y() );//helper.GetNodeUV( F, srcNode );
5352     gp_XY tgtUV = edge.LastUV( F );                   //helper.GetNodeUV( F, tgtNode );
5353     gp_Vec2d uvDir( srcUV, tgtUV );
5354     double uvLen = uvDir.Magnitude();
5355     uvDir /= uvLen;
5356     edge._normal.SetCoord( uvDir.X(),uvDir.Y(), 0 );
5357     edge._len = uvLen;
5358
5359     edge._pos.resize(1);
5360     edge._pos[0].SetCoord( tgtUV.X(), tgtUV.Y(), 0 );
5361
5362     // set UV of source node to target node
5363     SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
5364     pos->SetUParameter( srcUV.X() );
5365     pos->SetVParameter( srcUV.Y() );
5366   }
5367   else // _sWOL is TopAbs_EDGE
5368   {
5369     const TopoDS_Edge&    E = TopoDS::Edge( edge._sWOL );
5370     SMESHDS_SubMesh* edgeSM = getMeshDS()->MeshElements( E );
5371     if ( !edgeSM || edgeSM->NbElements() == 0 )
5372       return error(SMESH_Comment("Not meshed EDGE ") << getMeshDS()->ShapeToIndex( E ));
5373
5374     const SMDS_MeshNode* n2 = 0;
5375     SMDS_ElemIteratorPtr eIt = srcNode->GetInverseElementIterator(SMDSAbs_Edge);
5376     while ( eIt->more() && !n2 )
5377     {
5378       const SMDS_MeshElement* e = eIt->next();
5379       if ( !edgeSM->Contains(e)) continue;
5380       n2 = e->GetNode( 0 );
5381       if ( n2 == srcNode ) n2 = e->GetNode( 1 );
5382     }
5383     if ( !n2 )
5384       return error(SMESH_Comment("Wrongly meshed EDGE ") << getMeshDS()->ShapeToIndex( E ));
5385
5386     double uSrc = helper.GetNodeU( E, srcNode, n2 );
5387     double uTgt = helper.GetNodeU( E, tgtNode, srcNode );
5388     double u2   = helper.GetNodeU( E, n2, srcNode );
5389
5390     edge._pos.clear();
5391
5392     if ( fabs( uSrc-uTgt ) < 0.99 * fabs( uSrc-u2 ))
5393     {
5394       // tgtNode is located so that it does not make faces with wrong orientation
5395       return true;
5396     }
5397     edge._pos.resize(1);
5398     edge._pos[0].SetCoord( U_TGT, uTgt );
5399     edge._pos[0].SetCoord( U_SRC, uSrc );
5400     edge._pos[0].SetCoord( LEN_TGT, fabs( uSrc-uTgt ));
5401
5402     edge._simplices.resize( 1 );
5403     edge._simplices[0]._nPrev = n2;
5404
5405     // set U of source node to the target node
5406     SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( tgtNode->GetPosition() );
5407     pos->SetUParameter( uSrc );
5408   }
5409   return true;
5410 }
5411
5412 //================================================================================
5413 /*!
5414  * \brief Restore position of a sole node of a _LayerEdge based on _noShrinkShapes
5415  */
5416 //================================================================================
5417
5418 void _ViscousBuilder::restoreNoShrink( _LayerEdge& edge ) const
5419 {
5420   if ( edge._nodes.size() == 1 )
5421   {
5422     edge._pos.clear();
5423     edge._len = 0;
5424
5425     const SMDS_MeshNode* srcNode = edge._nodes[0];
5426     TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( srcNode, getMeshDS() );
5427     if ( S.IsNull() ) return;
5428
5429     gp_Pnt p;
5430
5431     switch ( S.ShapeType() )
5432     {
5433     case TopAbs_EDGE:
5434     {
5435       double f,l;
5436       TopLoc_Location loc;
5437       Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( S ), loc, f, l );
5438       if ( curve.IsNull() ) return;
5439       SMDS_EdgePosition* ePos = static_cast<SMDS_EdgePosition*>( srcNode->GetPosition() );
5440       p = curve->Value( ePos->GetUParameter() );
5441       break;
5442     }
5443     case TopAbs_VERTEX:
5444     {
5445       p = BRep_Tool::Pnt( TopoDS::Vertex( S ));
5446       break;
5447     }
5448     default: return;
5449     }
5450     getMeshDS()->MoveNode( srcNode, p.X(), p.Y(), p.Z() );
5451     dumpMove( srcNode );
5452   }
5453 }
5454
5455 //================================================================================
5456 /*!
5457  * \brief Try to fix triangles with high aspect ratio by swaping diagonals
5458  */
5459 //================================================================================
5460
5461 void _ViscousBuilder::fixBadFaces(const TopoDS_Face&          F,
5462                                   SMESH_MesherHelper&         helper,
5463                                   const bool                  is2D,
5464                                   const int                   step,
5465                                   set<const SMDS_MeshNode*> * involvedNodes)
5466 {
5467   SMESH::Controls::AspectRatio qualifier;
5468   SMESH::Controls::TSequenceOfXYZ points(3), points1(3), points2(3);
5469   const double maxAspectRatio = is2D ? 4. : 2;
5470   _NodeCoordHelper xyz( F, helper, is2D );
5471
5472   // find bad triangles
5473
5474   vector< const SMDS_MeshElement* > badTrias;
5475   vector< double >                  badAspects;
5476   SMESHDS_SubMesh*      sm = helper.GetMeshDS()->MeshElements( F );
5477   SMDS_ElemIteratorPtr fIt = sm->GetElements();
5478   while ( fIt->more() )
5479   {
5480     const SMDS_MeshElement * f = fIt->next();
5481     if ( f->NbCornerNodes() != 3 ) continue;
5482     for ( int iP = 0; iP < 3; ++iP ) points(iP+1) = xyz( f->GetNode(iP));
5483     double aspect = qualifier.GetValue( points );
5484     if ( aspect > maxAspectRatio )
5485     {
5486       badTrias.push_back( f );
5487       badAspects.push_back( aspect );
5488     }
5489   }
5490   if ( step == 1 )
5491   {
5492     dumpFunction(SMESH_Comment("beforeSwapDiagonals_F")<<helper.GetSubShapeID());
5493     SMDS_ElemIteratorPtr fIt = sm->GetElements();
5494     while ( fIt->more() )
5495     {
5496       const SMDS_MeshElement * f = fIt->next();
5497       if ( f->NbCornerNodes() == 3 )
5498         dumpChangeNodes( f );
5499     }
5500     dumpFunctionEnd();
5501   }
5502   if ( badTrias.empty() )
5503     return;
5504
5505   // find couples of faces to swap diagonal
5506
5507   typedef pair < const SMDS_MeshElement* , const SMDS_MeshElement* > T2Trias;
5508   vector< T2Trias > triaCouples; 
5509
5510   TIDSortedElemSet involvedFaces, emptySet;
5511   for ( size_t iTia = 0; iTia < badTrias.size(); ++iTia )
5512   {
5513     T2Trias trias    [3];
5514     double  aspRatio [3];
5515     int i1, i2, i3;
5516
5517     if ( !involvedFaces.insert( badTrias[iTia] ).second )
5518       continue;
5519     for ( int iP = 0; iP < 3; ++iP )
5520       points(iP+1) = xyz( badTrias[iTia]->GetNode(iP));
5521
5522     // find triangles adjacent to badTrias[iTia] with better aspect ratio after diag-swaping
5523     int bestCouple = -1;
5524     for ( int iSide = 0; iSide < 3; ++iSide )
5525     {
5526       const SMDS_MeshNode* n1 = badTrias[iTia]->GetNode( iSide );
5527       const SMDS_MeshNode* n2 = badTrias[iTia]->GetNode(( iSide+1 ) % 3 );
5528       trias [iSide].first  = badTrias[iTia];
5529       trias [iSide].second = SMESH_MeshAlgos::FindFaceInSet( n1, n2, emptySet, involvedFaces,
5530                                                              & i1, & i2 );
5531       if (( ! trias[iSide].second ) ||
5532           ( trias[iSide].second->NbCornerNodes() != 3 ) ||
5533           ( ! sm->Contains( trias[iSide].second )))
5534         continue;
5535
5536       // aspect ratio of an adjacent tria
5537       for ( int iP = 0; iP < 3; ++iP )
5538         points2(iP+1) = xyz( trias[iSide].second->GetNode(iP));
5539       double aspectInit = qualifier.GetValue( points2 );
5540
5541       // arrange nodes as after diag-swaping
5542       if ( helper.WrapIndex( i1+1, 3 ) == i2 )
5543         i3 = helper.WrapIndex( i1-1, 3 );
5544       else
5545         i3 = helper.WrapIndex( i1+1, 3 );
5546       points1 = points;
5547       points1( 1+ iSide ) = points2( 1+ i3 );
5548       points2( 1+ i2    ) = points1( 1+ ( iSide+2 ) % 3 );
5549
5550       // aspect ratio after diag-swaping
5551       aspRatio[ iSide ] = qualifier.GetValue( points1 ) + qualifier.GetValue( points2 );
5552       if ( aspRatio[ iSide ] > aspectInit + badAspects[ iTia ] )
5553         continue;
5554
5555       // prevent inversion of a triangle
5556       gp_Vec norm1 = gp_Vec( points1(1), points1(3) ) ^ gp_Vec( points1(1), points1(2) );
5557       gp_Vec norm2 = gp_Vec( points2(1), points2(3) ) ^ gp_Vec( points2(1), points2(2) );
5558       if ( norm1 * norm2 < 0. && norm1.Angle( norm2 ) > 70./180.*M_PI )
5559         continue;
5560
5561       if ( bestCouple < 0 || aspRatio[ bestCouple ] > aspRatio[ iSide ] )
5562         bestCouple = iSide;
5563     }
5564
5565     if ( bestCouple >= 0 )
5566     {
5567       triaCouples.push_back( trias[bestCouple] );
5568       involvedFaces.insert ( trias[bestCouple].second );
5569     }
5570     else
5571     {
5572       involvedFaces.erase( badTrias[iTia] );
5573     }
5574   }
5575   if ( triaCouples.empty() )
5576     return;
5577
5578   // swap diagonals
5579
5580   SMESH_MeshEditor editor( helper.GetMesh() );
5581   dumpFunction(SMESH_Comment("beforeSwapDiagonals_F")<<helper.GetSubShapeID()<<"_"<<step);
5582   for ( size_t i = 0; i < triaCouples.size(); ++i )
5583   {
5584     dumpChangeNodes( triaCouples[i].first );
5585     dumpChangeNodes( triaCouples[i].second );
5586     editor.InverseDiag( triaCouples[i].first, triaCouples[i].second );
5587   }
5588
5589   if ( involvedNodes )
5590     for ( size_t i = 0; i < triaCouples.size(); ++i )
5591     {
5592       involvedNodes->insert( triaCouples[i].first->begin_nodes(),
5593                              triaCouples[i].first->end_nodes() );
5594       involvedNodes->insert( triaCouples[i].second->begin_nodes(),
5595                              triaCouples[i].second->end_nodes() );
5596     }
5597
5598   // just for debug dump resulting triangles
5599   dumpFunction(SMESH_Comment("swapDiagonals_F")<<helper.GetSubShapeID()<<"_"<<step);
5600   for ( size_t i = 0; i < triaCouples.size(); ++i )
5601   {
5602     dumpChangeNodes( triaCouples[i].first );
5603     dumpChangeNodes( triaCouples[i].second );
5604   }
5605 }
5606
5607 //================================================================================
5608 /*!
5609  * \brief Move target node to it's final position on the FACE during shrinking
5610  */
5611 //================================================================================
5612
5613 bool _LayerEdge::SetNewLength2d( Handle(Geom_Surface)& surface,
5614                                  const TopoDS_Face&    F,
5615                                  SMESH_MesherHelper&   helper )
5616 {
5617   if ( _pos.empty() )
5618     return false; // already at the target position
5619
5620   SMDS_MeshNode* tgtNode = const_cast< SMDS_MeshNode*& >( _nodes.back() );
5621
5622   if ( _sWOL.ShapeType() == TopAbs_FACE )
5623   {
5624     gp_XY    curUV = helper.GetNodeUV( F, tgtNode );
5625     gp_Pnt2d tgtUV( _pos[0].X(), _pos[0].Y() );
5626     gp_Vec2d uvDir( _normal.X(), _normal.Y() );
5627     const double uvLen = tgtUV.Distance( curUV );
5628     const double kSafe = Max( 0.5, 1. - 0.1 * _simplices.size() );
5629
5630     // Select shrinking step such that not to make faces with wrong orientation.
5631     double stepSize = 1e100;
5632     for ( size_t i = 0; i < _simplices.size(); ++i )
5633     {
5634       // find intersection of 2 lines: curUV-tgtUV and that connecting simplex nodes
5635       gp_XY uvN1 = helper.GetNodeUV( F, _simplices[i]._nPrev );
5636       gp_XY uvN2 = helper.GetNodeUV( F, _simplices[i]._nNext );
5637       gp_XY dirN = uvN2 - uvN1;
5638       double det = uvDir.Crossed( dirN );
5639       if ( Abs( det )  < std::numeric_limits<double>::min() ) continue;
5640       gp_XY dirN2Cur = curUV - uvN1;
5641       double step = dirN.Crossed( dirN2Cur ) / det;
5642       if ( step > 0 )
5643         stepSize = Min( step, stepSize );
5644     }
5645     gp_Pnt2d newUV;
5646     if ( uvLen <= stepSize )
5647     {
5648       newUV = tgtUV;
5649       _pos.clear();
5650     }
5651     else if ( stepSize > 0 )
5652     {
5653       newUV = curUV + uvDir.XY() * stepSize * kSafe;
5654     }
5655     else
5656     {
5657       return true;
5658     }
5659     SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
5660     pos->SetUParameter( newUV.X() );
5661     pos->SetVParameter( newUV.Y() );
5662
5663 #ifdef __myDEBUG
5664     gp_Pnt p = surface->Value( newUV.X(), newUV.Y() );
5665     tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
5666     dumpMove( tgtNode );
5667 #endif
5668   }
5669   else // _sWOL is TopAbs_EDGE
5670   {
5671     const TopoDS_Edge&      E = TopoDS::Edge( _sWOL );
5672     const SMDS_MeshNode*   n2 = _simplices[0]._nPrev;
5673     SMDS_EdgePosition* tgtPos = static_cast<SMDS_EdgePosition*>( tgtNode->GetPosition() );
5674
5675     const double u2     = helper.GetNodeU( E, n2, tgtNode );
5676     const double uSrc   = _pos[0].Coord( U_SRC );
5677     const double lenTgt = _pos[0].Coord( LEN_TGT );
5678
5679     double newU = _pos[0].Coord( U_TGT );
5680     if ( lenTgt < 0.99 * fabs( uSrc-u2 )) // n2 got out of src-tgt range
5681     {
5682       _pos.clear();
5683     }
5684     else
5685     {
5686       newU = 0.1 * tgtPos->GetUParameter() + 0.9 * u2;
5687     }
5688     tgtPos->SetUParameter( newU );
5689 #ifdef __myDEBUG
5690     gp_XY newUV = helper.GetNodeUV( F, tgtNode, _nodes[0]);
5691     gp_Pnt p = surface->Value( newUV.X(), newUV.Y() );
5692     tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
5693     dumpMove( tgtNode );
5694 #endif
5695   }
5696   return true;
5697 }
5698
5699 //================================================================================
5700 /*!
5701  * \brief Perform smooth on the FACE
5702  *  \retval bool - true if the node has been moved
5703  */
5704 //================================================================================
5705
5706 bool _SmoothNode::Smooth(int&                  badNb,
5707                          Handle(Geom_Surface)& surface,
5708                          SMESH_MesherHelper&   helper,
5709                          const double          refSign,
5710                          SmoothType            how,
5711                          bool                  set3D)
5712 {
5713   const TopoDS_Face& face = TopoDS::Face( helper.GetSubShape() );
5714
5715   // get uv of surrounding nodes
5716   vector<gp_XY> uv( _simplices.size() );
5717   for ( size_t i = 0; i < _simplices.size(); ++i )
5718     uv[i] = helper.GetNodeUV( face, _simplices[i]._nPrev, _node );
5719
5720   // compute new UV for the node
5721   gp_XY newPos (0,0);
5722   if ( how == TFI && _simplices.size() == 4 )
5723   {
5724     gp_XY corners[4];
5725     for ( size_t i = 0; i < _simplices.size(); ++i )
5726       if ( _simplices[i]._nOpp )
5727         corners[i] = helper.GetNodeUV( face, _simplices[i]._nOpp, _node );
5728       else
5729         throw SALOME_Exception(LOCALIZED("TFI smoothing: _Simplex::_nOpp not set!"));
5730
5731     newPos = helper.calcTFI ( 0.5, 0.5,
5732                               corners[0], corners[1], corners[2], corners[3],
5733                               uv[1], uv[2], uv[3], uv[0] );
5734   }
5735   else if ( how == ANGULAR )
5736   {
5737     newPos = computeAngularPos( uv, helper.GetNodeUV( face, _node ), refSign );
5738   }
5739   else if ( how == CENTROIDAL && _simplices.size() > 3 )
5740   {
5741     // average centers of diagonals wieghted with their reciprocal lengths
5742     if ( _simplices.size() == 4 )
5743     {
5744       double w1 = 1. / ( uv[2]-uv[0] ).SquareModulus();
5745       double w2 = 1. / ( uv[3]-uv[1] ).SquareModulus();
5746       newPos = ( w1 * ( uv[2]+uv[0] ) + w2 * ( uv[3]+uv[1] )) / ( w1+w2 ) / 2;
5747     }
5748     else
5749     {
5750       double sumWeight = 0;
5751       int nb = _simplices.size() == 4 ? 2 : _simplices.size();
5752       for ( int i = 0; i < nb; ++i )
5753       {
5754         int iFrom = i + 2;
5755         int iTo   = i + _simplices.size() - 1;
5756         for ( int j = iFrom; j < iTo; ++j )
5757         {
5758           int i2 = SMESH_MesherHelper::WrapIndex( j, _simplices.size() );
5759           double w = 1. / ( uv[i]-uv[i2] ).SquareModulus();
5760           sumWeight += w;
5761           newPos += w * ( uv[i]+uv[i2] );
5762         }
5763       }
5764       newPos /= 2 * sumWeight; // 2 is to get a middle between uv's
5765     }
5766   }
5767   else
5768   {
5769     // Laplacian smooth
5770     for ( size_t i = 0; i < _simplices.size(); ++i )
5771       newPos += uv[i];
5772     newPos /= _simplices.size();
5773   }
5774
5775   // count quality metrics (orientation) of triangles around the node
5776   int nbOkBefore = 0;
5777   gp_XY tgtUV = helper.GetNodeUV( face, _node );
5778   for ( size_t i = 0; i < _simplices.size(); ++i )
5779     nbOkBefore += _simplices[i].IsForward( tgtUV, _node, face, helper, refSign );
5780
5781   int nbOkAfter = 0;
5782   for ( size_t i = 0; i < _simplices.size(); ++i )
5783     nbOkAfter += _simplices[i].IsForward( newPos, _node, face, helper, refSign );
5784
5785   if ( nbOkAfter < nbOkBefore )
5786   {
5787     badNb += _simplices.size() - nbOkBefore;
5788     return false;
5789   }
5790
5791   SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( _node->GetPosition() );
5792   pos->SetUParameter( newPos.X() );
5793   pos->SetVParameter( newPos.Y() );
5794
5795 #ifdef __myDEBUG
5796   set3D = true;
5797 #endif
5798   if ( set3D )
5799   {
5800     gp_Pnt p = surface->Value( newPos.X(), newPos.Y() );
5801     const_cast< SMDS_MeshNode* >( _node )->setXYZ( p.X(), p.Y(), p.Z() );
5802     dumpMove( _node );
5803   }
5804
5805   badNb += _simplices.size() - nbOkAfter;
5806   return ( (tgtUV-newPos).SquareModulus() > 1e-10 );
5807 }
5808
5809 //================================================================================
5810 /*!
5811  * \brief Computes new UV using angle based smoothing technic
5812  */
5813 //================================================================================
5814
5815 gp_XY _SmoothNode::computeAngularPos(vector<gp_XY>& uv,
5816                                      const gp_XY&   uvToFix,
5817                                      const double   refSign)
5818 {
5819   uv.push_back( uv.front() );
5820
5821   vector< gp_XY >  edgeDir ( uv.size() );
5822   vector< double > edgeSize( uv.size() );
5823   for ( size_t i = 1; i < edgeDir.size(); ++i )
5824   {
5825     edgeDir [i-1] = uv[i] - uv[i-1];
5826     edgeSize[i-1] = edgeDir[i-1].Modulus();
5827     if ( edgeSize[i-1] < numeric_limits<double>::min() )
5828       edgeDir[i-1].SetX( 100 );
5829     else
5830       edgeDir[i-1] /= edgeSize[i-1] * refSign;
5831   }
5832   edgeDir.back()  = edgeDir.front();
5833   edgeSize.back() = edgeSize.front();
5834
5835   gp_XY  newPos(0,0);
5836   int    nbEdges = 0;
5837   double sumSize = 0;
5838   for ( size_t i = 1; i < edgeDir.size(); ++i )
5839   {
5840     if ( edgeDir[i-1].X() > 1. ) continue;
5841     int i1 = i-1;
5842     while ( edgeDir[i].X() > 1. && ++i < edgeDir.size() );
5843     if ( i == edgeDir.size() ) break;
5844     gp_XY p = uv[i];
5845     gp_XY norm1( -edgeDir[i1].Y(), edgeDir[i1].X() );
5846     gp_XY norm2( -edgeDir[i].Y(),  edgeDir[i].X() );
5847     gp_XY bisec = norm1 + norm2;
5848     double bisecSize = bisec.Modulus();
5849     if ( bisecSize < numeric_limits<double>::min() )
5850     {
5851       bisec = -edgeDir[i1] + edgeDir[i];
5852       bisecSize = bisec.Modulus();
5853     }
5854     bisec /= bisecSize;
5855
5856     gp_XY  dirToN  = uvToFix - p;
5857     double distToN = dirToN.Modulus();
5858     if ( bisec * dirToN < 0 )
5859       distToN = -distToN;
5860
5861     newPos += ( p + bisec * distToN ) * ( edgeSize[i1] + edgeSize[i] );
5862     ++nbEdges;
5863     sumSize += edgeSize[i1] + edgeSize[i];
5864   }
5865   newPos /= /*nbEdges * */sumSize;
5866   return newPos;
5867 }
5868
5869 //================================================================================
5870 /*!
5871  * \brief Delete _SolidData
5872  */
5873 //================================================================================
5874
5875 _SolidData::~_SolidData()
5876 {
5877   for ( size_t i = 0; i < _edges.size(); ++i )
5878   {
5879     if ( _edges[i] && _edges[i]->_2neibors )
5880       delete _edges[i]->_2neibors;
5881     delete _edges[i];
5882   }
5883   _edges.clear();
5884 }
5885 //================================================================================
5886 /*!
5887  * \brief Add a _LayerEdge inflated along the EDGE
5888  */
5889 //================================================================================
5890
5891 void _Shrinker1D::AddEdge( const _LayerEdge* e, SMESH_MesherHelper& helper )
5892 {
5893   // init
5894   if ( _nodes.empty() )
5895   {
5896     _edges[0] = _edges[1] = 0;
5897     _done = false;
5898   }
5899   // check _LayerEdge
5900   if ( e == _edges[0] || e == _edges[1] )
5901     return;
5902   if ( e->_sWOL.IsNull() || e->_sWOL.ShapeType() != TopAbs_EDGE )
5903     throw SALOME_Exception(LOCALIZED("Wrong _LayerEdge is added"));
5904   if ( _edges[0] && _edges[0]->_sWOL != e->_sWOL )
5905     throw SALOME_Exception(LOCALIZED("Wrong _LayerEdge is added"));
5906
5907   // store _LayerEdge
5908   const TopoDS_Edge& E = TopoDS::Edge( e->_sWOL );
5909   double f,l;
5910   BRep_Tool::Range( E, f,l );
5911   double u = helper.GetNodeU( E, e->_nodes[0], e->_nodes.back());
5912   _edges[ u < 0.5*(f+l) ? 0 : 1 ] = e;
5913
5914   // Update _nodes
5915
5916   const SMDS_MeshNode* tgtNode0 = _edges[0] ? _edges[0]->_nodes.back() : 0;
5917   const SMDS_MeshNode* tgtNode1 = _edges[1] ? _edges[1]->_nodes.back() : 0;
5918
5919   if ( _nodes.empty() )
5920   {
5921     SMESHDS_SubMesh * eSubMesh = helper.GetMeshDS()->MeshElements( E );
5922     if ( !eSubMesh || eSubMesh->NbNodes() < 1 )
5923       return;
5924     TopLoc_Location loc;
5925     Handle(Geom_Curve) C = BRep_Tool::Curve(E, loc, f,l);
5926     GeomAdaptor_Curve aCurve(C, f,l);
5927     const double totLen = GCPnts_AbscissaPoint::Length(aCurve, f, l);
5928
5929     int nbExpectNodes = eSubMesh->NbNodes();
5930     _initU  .reserve( nbExpectNodes );
5931     _normPar.reserve( nbExpectNodes );
5932     _nodes  .reserve( nbExpectNodes );
5933     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
5934     while ( nIt->more() )
5935     {
5936       const SMDS_MeshNode* node = nIt->next();
5937       if ( node->NbInverseElements(SMDSAbs_Edge) == 0 ||
5938            node == tgtNode0 || node == tgtNode1 )
5939         continue; // refinement nodes
5940       _nodes.push_back( node );
5941       _initU.push_back( helper.GetNodeU( E, node ));
5942       double len = GCPnts_AbscissaPoint::Length(aCurve, f, _initU.back());
5943       _normPar.push_back(  len / totLen );
5944     }
5945   }
5946   else
5947   {
5948     // remove target node of the _LayerEdge from _nodes
5949     int nbFound = 0;
5950     for ( size_t i = 0; i < _nodes.size(); ++i )
5951       if ( !_nodes[i] || _nodes[i] == tgtNode0 || _nodes[i] == tgtNode1 )
5952         _nodes[i] = 0, nbFound++;
5953     if ( nbFound == _nodes.size() )
5954       _nodes.clear();
5955   }
5956 }
5957
5958 //================================================================================
5959 /*!
5960  * \brief Move nodes on EDGE from ends where _LayerEdge's are inflated
5961  */
5962 //================================================================================
5963
5964 void _Shrinker1D::Compute(bool set3D, SMESH_MesherHelper& helper)
5965 {
5966   if ( _done || _nodes.empty())
5967     return;
5968   const _LayerEdge* e = _edges[0];
5969   if ( !e ) e = _edges[1];
5970   if ( !e ) return;
5971
5972   _done =  (( !_edges[0] || _edges[0]->_pos.empty() ) &&
5973             ( !_edges[1] || _edges[1]->_pos.empty() ));
5974
5975   const TopoDS_Edge& E = TopoDS::Edge( e->_sWOL );
5976   double f,l;
5977   if ( set3D || _done )
5978   {
5979     Handle(Geom_Curve) C = BRep_Tool::Curve(E, f,l);
5980     GeomAdaptor_Curve aCurve(C, f,l);
5981
5982     if ( _edges[0] )
5983       f = helper.GetNodeU( E, _edges[0]->_nodes.back(), _nodes[0] );
5984     if ( _edges[1] )
5985       l = helper.GetNodeU( E, _edges[1]->_nodes.back(), _nodes.back() );
5986     double totLen = GCPnts_AbscissaPoint::Length( aCurve, f, l );
5987
5988     for ( size_t i = 0; i < _nodes.size(); ++i )
5989     {
5990       if ( !_nodes[i] ) continue;
5991       double len = totLen * _normPar[i];
5992       GCPnts_AbscissaPoint discret( aCurve, len, f );
5993       if ( !discret.IsDone() )
5994         return throw SALOME_Exception(LOCALIZED("GCPnts_AbscissaPoint failed"));
5995       double u = discret.Parameter();
5996       SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
5997       pos->SetUParameter( u );
5998       gp_Pnt p = C->Value( u );
5999       const_cast< SMDS_MeshNode*>( _nodes[i] )->setXYZ( p.X(), p.Y(), p.Z() );
6000     }
6001   }
6002   else
6003   {
6004     BRep_Tool::Range( E, f,l );
6005     if ( _edges[0] )
6006       f = helper.GetNodeU( E, _edges[0]->_nodes.back(), _nodes[0] );
6007     if ( _edges[1] )
6008       l = helper.GetNodeU( E, _edges[1]->_nodes.back(), _nodes.back() );
6009     
6010     for ( size_t i = 0; i < _nodes.size(); ++i )
6011     {
6012       if ( !_nodes[i] ) continue;
6013       double u = f * ( 1-_normPar[i] ) + l * _normPar[i];
6014       SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
6015       pos->SetUParameter( u );
6016     }
6017   }
6018 }
6019
6020 //================================================================================
6021 /*!
6022  * \brief Restore initial parameters of nodes on EDGE
6023  */
6024 //================================================================================
6025
6026 void _Shrinker1D::RestoreParams()
6027 {
6028   if ( _done )
6029     for ( size_t i = 0; i < _nodes.size(); ++i )
6030     {
6031       if ( !_nodes[i] ) continue;
6032       SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
6033       pos->SetUParameter( _initU[i] );
6034     }
6035   _done = false;
6036 }
6037
6038 //================================================================================
6039 /*!
6040  * \brief Replace source nodes by target nodes in shrinked mesh edges
6041  */
6042 //================================================================================
6043
6044 void _Shrinker1D::SwapSrcTgtNodes( SMESHDS_Mesh* mesh )
6045 {
6046   const SMDS_MeshNode* nodes[3];
6047   for ( int i = 0; i < 2; ++i )
6048   {
6049     if ( !_edges[i] ) continue;
6050
6051     SMESHDS_SubMesh * eSubMesh = mesh->MeshElements( _edges[i]->_sWOL );
6052     if ( !eSubMesh ) return;
6053     const SMDS_MeshNode* srcNode = _edges[i]->_nodes[0];
6054     const SMDS_MeshNode* tgtNode = _edges[i]->_nodes.back();
6055     SMDS_ElemIteratorPtr eIt = srcNode->GetInverseElementIterator(SMDSAbs_Edge);
6056     while ( eIt->more() )
6057     {
6058       const SMDS_MeshElement* e = eIt->next();
6059       if ( !eSubMesh->Contains( e ))
6060           continue;
6061       SMDS_ElemIteratorPtr nIt = e->nodesIterator();
6062       for ( int iN = 0; iN < e->NbNodes(); ++iN )
6063       {
6064         const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nIt->next() );
6065         nodes[iN] = ( n == srcNode ? tgtNode : n );
6066       }
6067       mesh->ChangeElementNodes( e, nodes, e->NbNodes() );
6068     }
6069   }
6070 }
6071
6072 //================================================================================
6073 /*!
6074  * \brief Creates 2D and 1D elements on boundaries of new prisms
6075  */
6076 //================================================================================
6077
6078 bool _ViscousBuilder::addBoundaryElements()
6079 {
6080   SMESH_MesherHelper helper( *_mesh );
6081
6082   for ( size_t i = 0; i < _sdVec.size(); ++i )
6083   {
6084     _SolidData& data = _sdVec[i];
6085     TopTools_IndexedMapOfShape geomEdges;
6086     TopExp::MapShapes( data._solid, TopAbs_EDGE, geomEdges );
6087     for ( int iE = 1; iE <= geomEdges.Extent(); ++iE )
6088     {
6089       const TopoDS_Edge& E = TopoDS::Edge( geomEdges(iE));
6090       if ( data._noShrinkShapes.count( getMeshDS()->ShapeToIndex( E )))
6091         continue;
6092
6093       // Get _LayerEdge's based on E
6094
6095       map< double, const SMDS_MeshNode* > u2nodes;
6096       if ( !SMESH_Algo::GetSortedNodesOnEdge( getMeshDS(), E, /*ignoreMedium=*/false, u2nodes))
6097         continue;
6098
6099       vector< _LayerEdge* > ledges; ledges.reserve( u2nodes.size() );
6100       TNode2Edge & n2eMap = data._n2eMap;
6101       map< double, const SMDS_MeshNode* >::iterator u2n = u2nodes.begin();
6102       {
6103         //check if 2D elements are needed on E
6104         TNode2Edge::iterator n2e = n2eMap.find( u2n->second );
6105         if ( n2e == n2eMap.end() ) continue; // no layers on vertex
6106         ledges.push_back( n2e->second );
6107         u2n++;
6108         if (( n2e = n2eMap.find( u2n->second )) == n2eMap.end() )
6109           continue; // no layers on E
6110         ledges.push_back( n2eMap[ u2n->second ]);
6111
6112         const SMDS_MeshNode* tgtN0 = ledges[0]->_nodes.back();
6113         const SMDS_MeshNode* tgtN1 = ledges[1]->_nodes.back();
6114         int nbSharedPyram = 0;
6115         SMDS_ElemIteratorPtr vIt = tgtN0->GetInverseElementIterator(SMDSAbs_Volume);
6116         while ( vIt->more() )
6117         {
6118           const SMDS_MeshElement* v = vIt->next();
6119           nbSharedPyram += int( v->GetNodeIndex( tgtN1 ) >= 0 );
6120         }
6121         if ( nbSharedPyram > 1 )
6122           continue; // not free border of the pyramid
6123
6124         if ( getMeshDS()->FindFace( ledges[0]->_nodes[0], ledges[0]->_nodes[1],
6125                                     ledges[1]->_nodes[0], ledges[1]->_nodes[1]))
6126           continue; // faces already created
6127       }
6128       for ( ++u2n; u2n != u2nodes.end(); ++u2n )
6129         ledges.push_back( n2eMap[ u2n->second ]);
6130
6131       // Find out orientation and type of face to create
6132
6133       bool reverse = false, isOnFace;
6134       
6135       map< TGeomID, TopoDS_Shape >::iterator e2f =
6136         data._shrinkShape2Shape.find( getMeshDS()->ShapeToIndex( E ));
6137       TopoDS_Shape F;
6138       if (( isOnFace = ( e2f != data._shrinkShape2Shape.end() )))
6139       {
6140         F = e2f->second.Oriented( TopAbs_FORWARD );
6141         reverse = ( helper.GetSubShapeOri( F, E ) == TopAbs_REVERSED );
6142         if ( helper.GetSubShapeOri( data._solid, F ) == TopAbs_REVERSED )
6143           reverse = !reverse, F.Reverse();
6144         if ( helper.IsReversedSubMesh( TopoDS::Face(F) ))
6145           reverse = !reverse;
6146       }
6147       else
6148       {
6149         // find FACE with layers sharing E
6150         PShapeIteratorPtr fIt = helper.GetAncestors( E, *_mesh, TopAbs_FACE );
6151         while ( fIt->more() && F.IsNull() )
6152         {
6153           const TopoDS_Shape* pF = fIt->next();
6154           if ( helper.IsSubShape( *pF, data._solid) &&
6155                !data._ignoreFaceIds.count( e2f->first ))
6156             F = *pF;
6157         }
6158       }
6159       // Find the sub-mesh to add new faces
6160       SMESHDS_SubMesh* sm = 0;
6161       if ( isOnFace )
6162         sm = getMeshDS()->MeshElements( F );
6163       else
6164         sm = data._proxyMesh->getFaceSubM( TopoDS::Face(F), /*create=*/true );
6165       if ( !sm )
6166         return error("error in addBoundaryElements()", data._index);
6167
6168       // Make faces
6169       const int dj1 = reverse ? 0 : 1;
6170       const int dj2 = reverse ? 1 : 0;
6171       for ( size_t j = 1; j < ledges.size(); ++j )
6172       {
6173         vector< const SMDS_MeshNode*>&  nn1 = ledges[j-dj1]->_nodes;
6174         vector< const SMDS_MeshNode*>&  nn2 = ledges[j-dj2]->_nodes;
6175         if ( nn1.size() == nn2.size() )
6176         {
6177           if ( isOnFace )
6178             for ( size_t z = 1; z < nn1.size(); ++z )
6179               sm->AddElement( getMeshDS()->AddFace( nn1[z-1], nn2[z-1], nn2[z], nn1[z] ));
6180           else
6181             for ( size_t z = 1; z < nn1.size(); ++z )
6182               sm->AddElement( new SMDS_FaceOfNodes( nn1[z-1], nn2[z-1], nn2[z], nn1[z] ));
6183         }
6184         else if ( nn1.size() == 1 )
6185         {
6186           if ( isOnFace )
6187             for ( size_t z = 1; z < nn2.size(); ++z )
6188               sm->AddElement( getMeshDS()->AddFace( nn1[0], nn2[z-1], nn2[z] ));
6189           else
6190             for ( size_t z = 1; z < nn2.size(); ++z )
6191               sm->AddElement( new SMDS_FaceOfNodes( nn1[0], nn2[z-1], nn2[z] ));
6192         }
6193         else
6194         {
6195           if ( isOnFace )
6196             for ( size_t z = 1; z < nn1.size(); ++z )
6197               sm->AddElement( getMeshDS()->AddFace( nn1[z-1], nn2[0], nn1[z] ));
6198           else
6199             for ( size_t z = 1; z < nn1.size(); ++z )
6200               sm->AddElement( new SMDS_FaceOfNodes( nn1[z-1], nn2[0], nn2[z] ));
6201         }
6202       }
6203
6204       // Make edges
6205       for ( int isFirst = 0; isFirst < 2; ++isFirst )
6206       {
6207         _LayerEdge* edge = isFirst ? ledges.front() : ledges.back();
6208         if ( !edge->_sWOL.IsNull() && edge->_sWOL.ShapeType() == TopAbs_EDGE )
6209         {
6210           vector< const SMDS_MeshNode*>&  nn = edge->_nodes;
6211           if ( nn.size() < 2 || nn[1]->GetInverseElementIterator( SMDSAbs_Edge )->more() )
6212             continue;
6213           helper.SetSubShape( edge->_sWOL );
6214           helper.SetElementsOnShape( true );
6215           for ( size_t z = 1; z < nn.size(); ++z )
6216             helper.AddEdge( nn[z-1], nn[z] );
6217         }
6218       }
6219     }
6220   }
6221
6222   return true;
6223 }