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