1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : StdMeshers_ViscousLayers.cxx
21 // Created : Wed Dec 1 15:15:34 2010
22 // Author : Edward AGAPOV (eap)
24 #include "StdMeshers_ViscousLayers.hxx"
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 "SMESHDS_Mesh.hxx"
34 #include "SMESH_Algo.hxx"
35 #include "SMESH_ComputeError.hxx"
36 #include "SMESH_ControlsDef.hxx"
37 #include "SMESH_Gen.hxx"
38 #include "SMESH_Group.hxx"
39 #include "SMESH_HypoFilter.hxx"
40 #include "SMESH_Mesh.hxx"
41 #include "SMESH_MeshAlgos.hxx"
42 #include "SMESH_MesherHelper.hxx"
43 #include "SMESH_ProxyMesh.hxx"
44 #include "SMESH_subMesh.hxx"
45 #include "SMESH_subMeshEventListener.hxx"
46 #include "StdMeshers_FaceSide.hxx"
47 #include "StdMeshers_ViscousLayers2D.hxx"
49 #include <Adaptor3d_HSurface.hxx>
50 #include <BRepAdaptor_Curve.hxx>
51 #include <BRepAdaptor_Curve2d.hxx>
52 #include <BRepAdaptor_Surface.hxx>
53 //#include <BRepLProp_CLProps.hxx>
54 #include <BRepLProp_SLProps.hxx>
55 #include <BRepOffsetAPI_MakeOffsetShape.hxx>
56 #include <BRep_Tool.hxx>
57 #include <Bnd_B2d.hxx>
58 #include <Bnd_B3d.hxx>
60 #include <GCPnts_AbscissaPoint.hxx>
61 #include <GCPnts_TangentialDeflection.hxx>
62 #include <Geom2d_Circle.hxx>
63 #include <Geom2d_Line.hxx>
64 #include <Geom2d_TrimmedCurve.hxx>
65 #include <GeomAdaptor_Curve.hxx>
66 #include <GeomLib.hxx>
67 #include <Geom_Circle.hxx>
68 #include <Geom_Curve.hxx>
69 #include <Geom_Line.hxx>
70 #include <Geom_TrimmedCurve.hxx>
71 #include <Precision.hxx>
72 #include <Standard_ErrorHandler.hxx>
73 #include <Standard_Failure.hxx>
74 #include <TColStd_Array1OfReal.hxx>
76 #include <TopExp_Explorer.hxx>
77 #include <TopTools_IndexedMapOfShape.hxx>
78 #include <TopTools_ListOfShape.hxx>
79 #include <TopTools_MapIteratorOfMapOfShape.hxx>
80 #include <TopTools_MapOfShape.hxx>
82 #include <TopoDS_Edge.hxx>
83 #include <TopoDS_Face.hxx>
84 #include <TopoDS_Vertex.hxx>
86 #include <gp_Cone.hxx>
87 #include <gp_Sphere.hxx>
99 //#define __NOT_INVALIDATE_BAD_SMOOTH
100 //#define __NODES_AT_POS
103 #define INCREMENTAL_SMOOTH // smooth only if min angle is too small
104 #define BLOCK_INFLATION // of individual _LayerEdge's
105 #define OLD_NEF_POLYGON
109 //================================================================================
114 enum UIndex { U_TGT = 1, U_SRC, LEN_TGT };
116 const double theMinSmoothCosin = 0.1;
117 const double theSmoothThickToElemSizeRatio = 0.6;
118 const double theMinSmoothTriaAngle = 30;
119 const double theMinSmoothQuadAngle = 45;
121 // what part of thickness is allowed till intersection
122 // (defined by SALOME_TESTS/Grids/smesh/viscous_layers_00/A5)
123 const double theThickToIntersection = 1.5;
125 bool needSmoothing( double cosin, double tgtThick, double elemSize )
127 return cosin * tgtThick > theSmoothThickToElemSizeRatio * elemSize;
129 double getSmoothingThickness( double cosin, double elemSize )
131 return theSmoothThickToElemSizeRatio * elemSize / cosin;
135 * \brief SMESH_ProxyMesh computed by _ViscousBuilder for a SOLID.
136 * It is stored in a SMESH_subMesh of the SOLID as SMESH_subMeshEventListenerData
138 struct _MeshOfSolid : public SMESH_ProxyMesh,
139 public SMESH_subMeshEventListenerData
141 bool _n2nMapComputed;
142 SMESH_ComputeErrorPtr _warning;
144 _MeshOfSolid( SMESH_Mesh* mesh)
145 :SMESH_subMeshEventListenerData( /*isDeletable=*/true),_n2nMapComputed(false)
147 SMESH_ProxyMesh::setMesh( *mesh );
150 // returns submesh for a geom face
151 SMESH_ProxyMesh::SubMesh* getFaceSubM(const TopoDS_Face& F, bool create=false)
153 TGeomID i = SMESH_ProxyMesh::shapeIndex(F);
154 return create ? SMESH_ProxyMesh::getProxySubMesh(i) : findProxySubMesh(i);
156 void setNode2Node(const SMDS_MeshNode* srcNode,
157 const SMDS_MeshNode* proxyNode,
158 const SMESH_ProxyMesh::SubMesh* subMesh)
160 SMESH_ProxyMesh::setNode2Node( srcNode,proxyNode,subMesh);
163 //--------------------------------------------------------------------------------
165 * \brief Listener of events of 3D sub-meshes computed with viscous layers.
166 * It is used to clear an inferior dim sub-meshes modified by viscous layers
168 class _ShrinkShapeListener : SMESH_subMeshEventListener
170 _ShrinkShapeListener()
171 : SMESH_subMeshEventListener(/*isDeletable=*/false,
172 "StdMeshers_ViscousLayers::_ShrinkShapeListener") {}
174 static SMESH_subMeshEventListener* Get() { static _ShrinkShapeListener l; return &l; }
175 virtual void ProcessEvent(const int event,
177 SMESH_subMesh* solidSM,
178 SMESH_subMeshEventListenerData* data,
179 const SMESH_Hypothesis* hyp)
181 if ( SMESH_subMesh::COMPUTE_EVENT == eventType && solidSM->IsEmpty() && data )
183 SMESH_subMeshEventListener::ProcessEvent(event,eventType,solidSM,data,hyp);
187 //--------------------------------------------------------------------------------
189 * \brief Listener of events of 3D sub-meshes computed with viscous layers.
190 * It is used to store data computed by _ViscousBuilder for a sub-mesh and to
191 * delete the data as soon as it has been used
193 class _ViscousListener : SMESH_subMeshEventListener
196 SMESH_subMeshEventListener(/*isDeletable=*/false,
197 "StdMeshers_ViscousLayers::_ViscousListener") {}
198 static SMESH_subMeshEventListener* Get() { static _ViscousListener l; return &l; }
200 virtual void ProcessEvent(const int event,
202 SMESH_subMesh* subMesh,
203 SMESH_subMeshEventListenerData* data,
204 const SMESH_Hypothesis* hyp)
206 if (( SMESH_subMesh::COMPUTE_EVENT == eventType ) &&
207 ( SMESH_subMesh::CHECK_COMPUTE_STATE != event &&
208 SMESH_subMesh::SUBMESH_COMPUTED != event ))
210 // delete SMESH_ProxyMesh containing temporary faces
211 subMesh->DeleteEventListener( this );
214 // Finds or creates proxy mesh of the solid
215 static _MeshOfSolid* GetSolidMesh(SMESH_Mesh* mesh,
216 const TopoDS_Shape& solid,
219 if ( !mesh ) return 0;
220 SMESH_subMesh* sm = mesh->GetSubMesh(solid);
221 _MeshOfSolid* data = (_MeshOfSolid*) sm->GetEventListenerData( Get() );
222 if ( !data && toCreate )
224 data = new _MeshOfSolid(mesh);
225 data->mySubMeshes.push_back( sm ); // to find SOLID by _MeshOfSolid
226 sm->SetEventListener( Get(), data, sm );
230 // Removes proxy mesh of the solid
231 static void RemoveSolidMesh(SMESH_Mesh* mesh, const TopoDS_Shape& solid)
233 mesh->GetSubMesh(solid)->DeleteEventListener( _ViscousListener::Get() );
237 //================================================================================
239 * \brief sets a sub-mesh event listener to clear sub-meshes of sub-shapes of
240 * the main shape when sub-mesh of the main shape is cleared,
241 * for example to clear sub-meshes of FACEs when sub-mesh of a SOLID
244 //================================================================================
246 void ToClearSubWithMain( SMESH_subMesh* sub, const TopoDS_Shape& main)
248 SMESH_subMesh* mainSM = sub->GetFather()->GetSubMesh( main );
249 SMESH_subMeshEventListenerData* data =
250 mainSM->GetEventListenerData( _ShrinkShapeListener::Get());
253 if ( find( data->mySubMeshes.begin(), data->mySubMeshes.end(), sub ) ==
254 data->mySubMeshes.end())
255 data->mySubMeshes.push_back( sub );
259 data = SMESH_subMeshEventListenerData::MakeData( /*dependent=*/sub );
260 sub->SetEventListener( _ShrinkShapeListener::Get(), data, /*whereToListenTo=*/mainSM );
264 //--------------------------------------------------------------------------------
266 * \brief Simplex (triangle or tetrahedron) based on 1 (tria) or 2 (tet) nodes of
267 * _LayerEdge and 2 nodes of the mesh surface beening smoothed.
268 * The class is used to check validity of face or volumes around a smoothed node;
269 * it stores only 2 nodes as the other nodes are stored by _LayerEdge.
273 const SMDS_MeshNode *_nPrev, *_nNext; // nodes on a smoothed mesh surface
274 const SMDS_MeshNode *_nOpp; // in 2D case, a node opposite to a smoothed node in QUAD
275 _Simplex(const SMDS_MeshNode* nPrev=0,
276 const SMDS_MeshNode* nNext=0,
277 const SMDS_MeshNode* nOpp=0)
278 : _nPrev(nPrev), _nNext(nNext), _nOpp(nOpp) {}
279 bool IsForward(const gp_XYZ* pntSrc, const gp_XYZ* pntTgt, double& vol) const
281 const double M[3][3] =
282 {{ _nNext->X() - pntSrc->X(), _nNext->Y() - pntSrc->Y(), _nNext->Z() - pntSrc->Z() },
283 { pntTgt->X() - pntSrc->X(), pntTgt->Y() - pntSrc->Y(), pntTgt->Z() - pntSrc->Z() },
284 { _nPrev->X() - pntSrc->X(), _nPrev->Y() - pntSrc->Y(), _nPrev->Z() - pntSrc->Z() }};
285 vol = ( + M[0][0] * M[1][1] * M[2][2]
286 + M[0][1] * M[1][2] * M[2][0]
287 + M[0][2] * M[1][0] * M[2][1]
288 - M[0][0] * M[1][2] * M[2][1]
289 - M[0][1] * M[1][0] * M[2][2]
290 - M[0][2] * M[1][1] * M[2][0]);
293 bool IsForward(const SMDS_MeshNode* nSrc, const gp_XYZ& pTgt, double& vol) const
295 SMESH_TNodeXYZ pSrc( nSrc );
296 return IsForward( &pSrc, &pTgt, vol );
298 bool IsForward(const gp_XY& tgtUV,
299 const SMDS_MeshNode* smoothedNode,
300 const TopoDS_Face& face,
301 SMESH_MesherHelper& helper,
302 const double refSign) const
304 gp_XY prevUV = helper.GetNodeUV( face, _nPrev, smoothedNode );
305 gp_XY nextUV = helper.GetNodeUV( face, _nNext, smoothedNode );
306 gp_Vec2d v1( tgtUV, prevUV ), v2( tgtUV, nextUV );
308 return d*refSign > 1e-100;
310 bool IsMinAngleOK( const gp_XYZ& pTgt, double& minAngle ) const
312 SMESH_TNodeXYZ pPrev( _nPrev ), pNext( _nNext );
313 if ( !_nOpp ) // triangle
315 gp_Vec tp( pPrev - pTgt ), pn( pNext - pPrev ), nt( pTgt - pNext );
316 double tp2 = tp.SquareMagnitude();
317 double pn2 = pn.SquareMagnitude();
318 double nt2 = nt.SquareMagnitude();
320 if ( tp2 < pn2 && tp2 < nt2 )
321 minAngle = ( nt * -pn ) * ( nt * -pn ) / nt2 / pn2;
322 else if ( pn2 < nt2 )
323 minAngle = ( tp * -nt ) * ( tp * -nt ) / tp2 / nt2;
325 minAngle = ( pn * -tp ) * ( pn * -tp ) / pn2 / tp2;
327 static double theMaxCos2 = ( Cos( theMinSmoothTriaAngle * M_PI / 180. ) *
328 Cos( theMinSmoothTriaAngle * M_PI / 180. ));
329 return minAngle < theMaxCos2;
333 SMESH_TNodeXYZ pOpp( _nOpp );
334 gp_Vec tp( pPrev - pTgt ), po( pOpp - pPrev ), on( pNext - pOpp), nt( pTgt - pNext );
335 double tp2 = tp.SquareMagnitude();
336 double po2 = po.SquareMagnitude();
337 double on2 = on.SquareMagnitude();
338 double nt2 = nt.SquareMagnitude();
339 minAngle = Max( Max((( tp * -nt ) * ( tp * -nt ) / tp2 / nt2 ),
340 (( po * -tp ) * ( po * -tp ) / po2 / tp2 )),
341 Max((( on * -po ) * ( on * -po ) / on2 / po2 ),
342 (( nt * -on ) * ( nt * -on ) / nt2 / on2 )));
344 static double theMaxCos2 = ( Cos( theMinSmoothQuadAngle * M_PI / 180. ) *
345 Cos( theMinSmoothQuadAngle * M_PI / 180. ));
346 return minAngle < theMaxCos2;
349 bool IsNeighbour(const _Simplex& other) const
351 return _nPrev == other._nNext || _nNext == other._nPrev;
353 bool Includes( const SMDS_MeshNode* node ) const { return _nPrev == node || _nNext == node; }
354 static void GetSimplices( const SMDS_MeshNode* node,
355 vector<_Simplex>& simplices,
356 const set<TGeomID>& ingnoreShapes,
357 const _SolidData* dataToCheckOri = 0,
358 const bool toSort = false);
359 static void SortSimplices(vector<_Simplex>& simplices);
361 //--------------------------------------------------------------------------------
363 * Structure used to take into account surface curvature while smoothing
368 double _k; // factor to correct node smoothed position
369 double _h2lenRatio; // avgNormProj / (2*avgDist)
370 gp_Pnt2d _uv; // UV used in putOnOffsetSurface()
372 static _Curvature* New( double avgNormProj, double avgDist )
375 if ( fabs( avgNormProj / avgDist ) > 1./200 )
378 c->_r = avgDist * avgDist / avgNormProj;
379 c->_k = avgDist * avgDist / c->_r / c->_r;
380 //c->_k = avgNormProj / c->_r;
381 c->_k *= ( c->_r < 0 ? 1/1.1 : 1.1 ); // not to be too restrictive
382 c->_h2lenRatio = avgNormProj / ( avgDist + avgDist );
384 c->_uv.SetCoord( 0., 0. );
388 double lenDelta(double len) const { return _k * ( _r + len ); }
389 double lenDeltaByDist(double dist) const { return dist * _h2lenRatio; }
391 //--------------------------------------------------------------------------------
395 struct _EdgesOnShape;
397 typedef map< const SMDS_MeshNode*, _LayerEdge*, TIDCompare > TNode2Edge;
399 //--------------------------------------------------------------------------------
401 * \brief Edge normal to surface, connecting a node on solid surface (_nodes[0])
402 * and a node of the most internal layer (_nodes.back())
406 typedef gp_XYZ (_LayerEdge::*PSmooFun)();
408 vector< const SMDS_MeshNode*> _nodes;
410 gp_XYZ _normal; // to boundary of solid
411 vector<gp_XYZ> _pos; // points computed during inflation
412 double _len; // length achieved with the last inflation step
413 double _maxLen; // maximal possible length
414 double _cosin; // of angle (_normal ^ surface)
415 double _minAngle; // of _simplices
416 double _lenFactor; // to compute _len taking _cosin into account
419 // simplices connected to the source node (_nodes[0]);
420 // used for smoothing and quality check of _LayerEdge's based on the FACE
421 vector<_Simplex> _simplices;
422 vector<_LayerEdge*> _neibors; // all surrounding _LayerEdge's
423 PSmooFun _smooFunction; // smoothing function
424 _Curvature* _curvature;
425 // data for smoothing of _LayerEdge's based on the EDGE
426 _2NearEdges* _2neibors;
428 enum EFlags { TO_SMOOTH = 0x0000001,
429 MOVED = 0x0000002, // set by _neibors[i]->SetNewLength()
430 SMOOTHED = 0x0000004, // set by _LayerEdge::Smooth()
431 DIFFICULT = 0x0000008, // near concave VERTEX
432 ON_CONCAVE_FACE = 0x0000010,
433 BLOCKED = 0x0000020, // not to inflate any more
434 INTERSECTED = 0x0000040, // close intersection with a face found
435 NORMAL_UPDATED = 0x0000080,
436 UPD_NORMAL_CONV = 0x0000100, // to update normal on boundary of concave FACE
437 MARKED = 0x0000200, // local usage
438 MULTI_NORMAL = 0x0000400, // a normal is invisible by some of surrounding faces
439 NEAR_BOUNDARY = 0x0000800, // is near FACE boundary forcing smooth
440 SMOOTHED_C1 = 0x0001000, // is on _eosC1
441 DISTORTED = 0x0002000, // was bad before smoothing
442 RISKY_SWOL = 0x0004000, // SWOL is parallel to a source FACE
443 SHRUNK = 0x0008000, // target node reached a tgt position while shrink()
444 UNUSED_FLAG = 0x0100000 // to add user flags after
446 bool Is ( int flag ) const { return _flags & flag; }
447 void Set ( int flag ) { _flags |= flag; }
448 void Unset( int flag ) { _flags &= ~flag; }
449 std::string DumpFlags() const; // debug
451 void SetNewLength( double len, _EdgesOnShape& eos, SMESH_MesherHelper& helper );
452 bool SetNewLength2d( Handle(Geom_Surface)& surface,
453 const TopoDS_Face& F,
455 SMESH_MesherHelper& helper );
456 void SetDataByNeighbors( const SMDS_MeshNode* n1,
457 const SMDS_MeshNode* n2,
458 const _EdgesOnShape& eos,
459 SMESH_MesherHelper& helper);
460 void Block( _SolidData& data );
461 void InvalidateStep( size_t curStep, const _EdgesOnShape& eos, bool restoreLength=false );
462 void ChooseSmooFunction(const set< TGeomID >& concaveVertices,
463 const TNode2Edge& n2eMap);
464 void SmoothPos( const vector< double >& segLen, const double tol );
465 int GetSmoothedPos( const double tol );
466 int Smooth(const int step, const bool isConcaveFace, bool findBest);
467 int Smooth(const int step, bool findBest, vector< _LayerEdge* >& toSmooth );
468 int CheckNeiborsOnBoundary(vector< _LayerEdge* >* badNeibors = 0, bool * needSmooth = 0 );
469 void SmoothWoCheck();
470 bool SmoothOnEdge(Handle(ShapeAnalysis_Surface)& surface,
471 const TopoDS_Face& F,
472 SMESH_MesherHelper& helper);
473 void MoveNearConcaVer( const _EdgesOnShape* eov,
474 const _EdgesOnShape* eos,
476 vector< _LayerEdge* > & badSmooEdges);
477 bool FindIntersection( SMESH_ElementSearcher& searcher,
479 const double& epsilon,
481 const SMDS_MeshElement** face = 0);
482 bool SegTriaInter( const gp_Ax1& lastSegment,
487 const double& epsilon) const;
488 bool SegTriaInter( const gp_Ax1& lastSegment,
489 const SMDS_MeshNode* n0,
490 const SMDS_MeshNode* n1,
491 const SMDS_MeshNode* n2,
493 const double& epsilon) const
494 { return SegTriaInter( lastSegment,
495 SMESH_TNodeXYZ( n0 ), SMESH_TNodeXYZ( n1 ), SMESH_TNodeXYZ( n2 ),
498 const gp_XYZ& PrevPos() const { return _pos[ _pos.size() - 2 ]; }
499 gp_XYZ PrevCheckPos( _EdgesOnShape* eos=0 ) const;
500 gp_Ax1 LastSegment(double& segLen, _EdgesOnShape& eos) const;
501 gp_XY LastUV( const TopoDS_Face& F, _EdgesOnShape& eos, int which=-1 ) const;
502 bool IsOnEdge() const { return _2neibors; }
503 bool IsOnFace() const { return ( _nodes[0]->GetPosition()->GetDim() == 2 ); }
504 int BaseShapeDim() const { return _nodes[0]->GetPosition()->GetDim(); }
505 gp_XYZ Copy( _LayerEdge& other, _EdgesOnShape& eos, SMESH_MesherHelper& helper );
506 void SetCosin( double cosin );
507 void SetNormal( const gp_XYZ& n ) { _normal = n; }
508 void SetMaxLen( double l ) { _maxLen = l; }
509 int NbSteps() const { return _pos.size() - 1; } // nb inlation steps
510 bool IsNeiborOnEdge( const _LayerEdge* edge ) const;
511 void SetSmooLen( double len ) { // set _len at which smoothing is needed
512 _cosin = len; // as for _LayerEdge's on FACE _cosin is not used
514 double GetSmooLen() { return _cosin; } // for _LayerEdge's on FACE _cosin is not used
516 gp_XYZ smoothLaplacian();
517 gp_XYZ smoothAngular();
518 gp_XYZ smoothLengthWeighted();
519 gp_XYZ smoothCentroidal();
520 gp_XYZ smoothNefPolygon();
522 enum { FUN_LAPLACIAN, FUN_LENWEIGHTED, FUN_CENTROIDAL, FUN_NEFPOLY, FUN_ANGULAR, FUN_NB };
523 static const int theNbSmooFuns = FUN_NB;
524 static PSmooFun _funs[theNbSmooFuns];
525 static const char* _funNames[theNbSmooFuns+1];
526 int smooFunID( PSmooFun fun=0) const;
528 _LayerEdge::PSmooFun _LayerEdge::_funs[theNbSmooFuns] = { &_LayerEdge::smoothLaplacian,
529 &_LayerEdge::smoothLengthWeighted,
530 &_LayerEdge::smoothCentroidal,
531 &_LayerEdge::smoothNefPolygon,
532 &_LayerEdge::smoothAngular };
533 const char* _LayerEdge::_funNames[theNbSmooFuns+1] = { "Laplacian",
541 bool operator () (const _LayerEdge* e1, const _LayerEdge* e2) const
543 const bool cmpNodes = ( e1 && e2 && e1->_nodes.size() && e2->_nodes.size() );
544 return cmpNodes ? ( e1->_nodes[0]->GetID() < e2->_nodes[0]->GetID()) : ( e1 < e2 );
547 //--------------------------------------------------------------------------------
549 * A 2D half plane used by _LayerEdge::smoothNefPolygon()
553 gp_XY _pos, _dir, _inNorm;
554 bool IsOut( const gp_XY p, const double tol ) const
556 return _inNorm * ( p - _pos ) < -tol;
558 bool FindIntersection( const _halfPlane& hp, gp_XY & intPnt )
560 //const double eps = 1e-10;
561 double D = _dir.Crossed( hp._dir );
562 if ( fabs(D) < std::numeric_limits<double>::min())
564 gp_XY vec21 = _pos - hp._pos;
565 double u = hp._dir.Crossed( vec21 ) / D;
566 intPnt = _pos + _dir * u;
570 //--------------------------------------------------------------------------------
572 * Structure used to smooth a _LayerEdge based on an EDGE.
576 double _wgt [2]; // weights of _nodes
577 _LayerEdge* _edges[2];
579 // normal to plane passing through _LayerEdge._normal and tangent of EDGE
582 _2NearEdges() { _edges[0]=_edges[1]=0; _plnNorm = 0; }
583 const SMDS_MeshNode* tgtNode(bool is2nd) {
584 return _edges[is2nd] ? _edges[is2nd]->_nodes.back() : 0;
586 const SMDS_MeshNode* srcNode(bool is2nd) {
587 return _edges[is2nd] ? _edges[is2nd]->_nodes[0] : 0;
590 std::swap( _wgt [0], _wgt [1] );
591 std::swap( _edges[0], _edges[1] );
593 void set( _LayerEdge* e1, _LayerEdge* e2, double w1, double w2 ) {
594 _edges[0] = e1; _edges[1] = e2; _wgt[0] = w1; _wgt[1] = w2;
596 bool include( const _LayerEdge* e ) {
597 return ( _edges[0] == e || _edges[1] == e );
602 //--------------------------------------------------------------------------------
604 * \brief Layers parameters got by averaging several hypotheses
608 AverageHyp( const StdMeshers_ViscousLayers* hyp = 0 )
609 :_nbLayers(0), _nbHyps(0), _method(0), _thickness(0), _stretchFactor(0)
613 void Add( const StdMeshers_ViscousLayers* hyp )
618 _nbLayers = hyp->GetNumberLayers();
619 //_thickness += hyp->GetTotalThickness();
620 _thickness = Max( _thickness, hyp->GetTotalThickness() );
621 _stretchFactor += hyp->GetStretchFactor();
622 _method = hyp->GetMethod();
625 double GetTotalThickness() const { return _thickness; /*_nbHyps ? _thickness / _nbHyps : 0;*/ }
626 double GetStretchFactor() const { return _nbHyps ? _stretchFactor / _nbHyps : 0; }
627 int GetNumberLayers() const { return _nbLayers; }
628 int GetMethod() const { return _method; }
630 bool UseSurfaceNormal() const
631 { return _method == StdMeshers_ViscousLayers::SURF_OFFSET_SMOOTH; }
632 bool ToSmooth() const
633 { return _method == StdMeshers_ViscousLayers::SURF_OFFSET_SMOOTH; }
634 bool IsOffsetMethod() const
635 { return _method == StdMeshers_ViscousLayers::FACE_OFFSET; }
638 int _nbLayers, _nbHyps, _method;
639 double _thickness, _stretchFactor;
642 //--------------------------------------------------------------------------------
644 * \brief _LayerEdge's on a shape and other shape data
648 vector< _LayerEdge* > _edges;
652 SMESH_subMesh * _subMesh;
653 // face or edge w/o layer along or near which _edges are inflated
655 bool _isRegularSWOL; // w/o singularities
656 // averaged StdMeshers_ViscousLayers parameters
659 _Smoother1D* _edgeSmoother;
660 vector< _EdgesOnShape* > _eosConcaVer; // edges at concave VERTEXes of a FACE
661 vector< _EdgesOnShape* > _eosC1; // to smooth together several C1 continues shapes
663 vector< gp_XYZ > _faceNormals; // if _shape is FACE
664 vector< _EdgesOnShape* > _faceEOS; // to get _faceNormals of adjacent FACEs
666 Handle(ShapeAnalysis_Surface) _offsetSurf;
667 _LayerEdge* _edgeForOffset;
669 _SolidData* _data; // parent SOLID
671 _LayerEdge* operator[](size_t i) const { return (_LayerEdge*) _edges[i]; }
672 size_t size() const { return _edges.size(); }
673 TopAbs_ShapeEnum ShapeType() const
674 { return _shape.IsNull() ? TopAbs_SHAPE : _shape.ShapeType(); }
675 TopAbs_ShapeEnum SWOLType() const
676 { return _sWOL.IsNull() ? TopAbs_SHAPE : _sWOL.ShapeType(); }
677 bool HasC1( const _EdgesOnShape* other ) const
678 { return std::find( _eosC1.begin(), _eosC1.end(), other ) != _eosC1.end(); }
679 bool GetNormal( const SMDS_MeshElement* face, gp_Vec& norm );
680 _SolidData& GetData() const { return *_data; }
682 _EdgesOnShape(): _shapeID(-1), _subMesh(0), _toSmooth(false), _edgeSmoother(0) {}
685 //--------------------------------------------------------------------------------
687 * \brief Convex FACE whose radius of curvature is less than the thickness of
688 * layers. It is used to detect distortion of prisms based on a convex
689 * FACE and to update normals to enable further increasing the thickness
695 // edges whose _simplices are used to detect prism distortion
696 vector< _LayerEdge* > _simplexTestEdges;
698 // map a sub-shape to _SolidData::_edgesOnShape
699 map< TGeomID, _EdgesOnShape* > _subIdToEOS;
703 bool _normalsFixedOnBorders; // used in putOnOffsetSurface()
705 double GetMaxCurvature( _SolidData& data,
707 BRepLProp_SLProps& surfProp,
708 SMESH_MesherHelper& helper);
710 bool GetCenterOfCurvature( _LayerEdge* ledge,
711 BRepLProp_SLProps& surfProp,
712 SMESH_MesherHelper& helper,
713 gp_Pnt & center ) const;
714 bool CheckPrisms() const;
717 //--------------------------------------------------------------------------------
719 * \brief Structure holding _LayerEdge's based on EDGEs that will collide
720 * at inflation up to the full thickness. A detected collision
721 * is fixed in updateNormals()
723 struct _CollisionEdges
726 vector< _LayerEdge* > _intEdges; // each pair forms an intersected quadrangle
727 const SMDS_MeshNode* nSrc(int i) const { return _intEdges[i]->_nodes[0]; }
728 const SMDS_MeshNode* nTgt(int i) const { return _intEdges[i]->_nodes.back(); }
731 //--------------------------------------------------------------------------------
733 * \brief Data of a SOLID
737 typedef const StdMeshers_ViscousLayers* THyp;
739 TopTools_MapOfShape _before; // SOLIDs to be computed before _solid
740 TGeomID _index; // SOLID id
741 _MeshOfSolid* _proxyMesh;
743 list< TopoDS_Shape > _hypShapes;
744 map< TGeomID, THyp > _face2hyp; // filled if _hyps.size() > 1
745 set< TGeomID > _reversedFaceIds;
746 set< TGeomID > _ignoreFaceIds; // WOL FACEs and FACEs of other SOLIDs
748 double _stepSize, _stepSizeCoeff, _geomSize;
749 const SMDS_MeshNode* _stepSizeNodes[2];
751 TNode2Edge _n2eMap; // nodes and _LayerEdge's based on them
753 // map to find _n2eMap of another _SolidData by a shrink shape shared by two _SolidData's
754 map< TGeomID, TNode2Edge* > _s2neMap;
755 // _LayerEdge's with underlying shapes
756 vector< _EdgesOnShape > _edgesOnShape;
758 // key: an id of shape (EDGE or VERTEX) shared by a FACE with
759 // layers and a FACE w/o layers
760 // value: the shape (FACE or EDGE) to shrink mesh on.
761 // _LayerEdge's basing on nodes on key shape are inflated along the value shape
762 map< TGeomID, TopoDS_Shape > _shrinkShape2Shape;
764 // Convex FACEs whose radius of curvature is less than the thickness of layers
765 map< TGeomID, _ConvexFace > _convexFaces;
767 // shapes (EDGEs and VERTEXes) srink from which is forbidden due to collisions with
768 // the adjacent SOLID
769 set< TGeomID > _noShrinkShapes;
771 int _nbShapesToSmooth;
773 vector< _CollisionEdges > _collisionEdges;
774 set< TGeomID > _concaveFaces;
776 double _maxThickness; // of all _hyps
777 double _minThickness; // of all _hyps
779 double _epsilon; // precision for SegTriaInter()
781 SMESH_MesherHelper* _helper;
783 _SolidData(const TopoDS_Shape& s=TopoDS_Shape(),
785 :_solid(s), _proxyMesh(m), _helper(0) {}
788 void SortOnEdge( const TopoDS_Edge& E, vector< _LayerEdge* >& edges);
789 void Sort2NeiborsOnEdge( vector< _LayerEdge* >& edges );
791 _ConvexFace* GetConvexFace( const TGeomID faceID ) {
792 map< TGeomID, _ConvexFace >::iterator id2face = _convexFaces.find( faceID );
793 return id2face == _convexFaces.end() ? 0 : & id2face->second;
795 _EdgesOnShape* GetShapeEdges(const TGeomID shapeID );
796 _EdgesOnShape* GetShapeEdges(const TopoDS_Shape& shape );
797 _EdgesOnShape* GetShapeEdges(const _LayerEdge* edge )
798 { return GetShapeEdges( edge->_nodes[0]->getshapeId() ); }
800 SMESH_MesherHelper& GetHelper() const { return *_helper; }
802 void UnmarkEdges( int flag = _LayerEdge::MARKED ) {
803 for ( size_t i = 0; i < _edgesOnShape.size(); ++i )
804 for ( size_t j = 0; j < _edgesOnShape[i]._edges.size(); ++j )
805 _edgesOnShape[i]._edges[j]->Unset( flag );
807 void AddShapesToSmooth( const set< _EdgesOnShape* >& shape,
808 const set< _EdgesOnShape* >* edgesNoAnaSmooth=0 );
810 void PrepareEdgesToSmoothOnFace( _EdgesOnShape* eof, bool substituteSrcNodes );
812 //--------------------------------------------------------------------------------
814 * \brief Offset plane used in getNormalByOffset()
820 int _faceIndexNext[2];
821 gp_Lin _lines[2]; // line of intersection with neighbor _OffsetPlane's
824 _isLineOK[0] = _isLineOK[1] = false; _faceIndexNext[0] = _faceIndexNext[1] = -1;
826 void ComputeIntersectionLine( _OffsetPlane& pln,
827 const TopoDS_Edge& E,
828 const TopoDS_Vertex& V );
829 gp_XYZ GetCommonPoint(bool& isFound, const TopoDS_Vertex& V) const;
830 int NbLines() const { return _isLineOK[0] + _isLineOK[1]; }
832 //--------------------------------------------------------------------------------
834 * \brief Container of centers of curvature at nodes on an EDGE bounding _ConvexFace
836 struct _CentralCurveOnEdge
839 vector< gp_Pnt > _curvaCenters;
840 vector< _LayerEdge* > _ledges;
841 vector< gp_XYZ > _normals; // new normal for each of _ledges
842 vector< double > _segLength2;
845 TopoDS_Face _adjFace;
846 bool _adjFaceToSmooth;
848 void Append( const gp_Pnt& center, _LayerEdge* ledge )
850 if ( ledge->Is( _LayerEdge::MULTI_NORMAL ))
852 if ( _curvaCenters.size() > 0 )
853 _segLength2.push_back( center.SquareDistance( _curvaCenters.back() ));
854 _curvaCenters.push_back( center );
855 _ledges.push_back( ledge );
856 _normals.push_back( ledge->_normal );
858 bool FindNewNormal( const gp_Pnt& center, gp_XYZ& newNormal );
859 void SetShapes( const TopoDS_Edge& edge,
860 const _ConvexFace& convFace,
862 SMESH_MesherHelper& helper);
864 //--------------------------------------------------------------------------------
866 * \brief Data of node on a shrinked FACE
870 const SMDS_MeshNode* _node;
871 vector<_Simplex> _simplices; // for quality check
873 enum SmoothType { LAPLACIAN, CENTROIDAL, ANGULAR, TFI };
875 bool Smooth(int& badNb,
876 Handle(Geom_Surface)& surface,
877 SMESH_MesherHelper& helper,
878 const double refSign,
882 gp_XY computeAngularPos(vector<gp_XY>& uv,
883 const gp_XY& uvToFix,
884 const double refSign );
887 //--------------------------------------------------------------------------------
889 * \brief Builder of viscous layers
891 class _ViscousBuilder
896 SMESH_ComputeErrorPtr Compute(SMESH_Mesh& mesh,
897 const TopoDS_Shape& shape);
898 // check validity of hypotheses
899 SMESH_ComputeErrorPtr CheckHypotheses( SMESH_Mesh& mesh,
900 const TopoDS_Shape& shape );
902 // restore event listeners used to clear an inferior dim sub-mesh modified by viscous layers
903 void RestoreListeners();
905 // computes SMESH_ProxyMesh::SubMesh::_n2n;
906 bool MakeN2NMap( _MeshOfSolid* pm );
910 bool findSolidsWithLayers();
911 bool setBefore( _SolidData& solidBefore, _SolidData& solidAfter );
912 bool findFacesWithLayers(const bool onlyWith=false);
913 void getIgnoreFaces(const TopoDS_Shape& solid,
914 const StdMeshers_ViscousLayers* hyp,
915 const TopoDS_Shape& hypShape,
916 set<TGeomID>& ignoreFaces);
917 bool makeLayer(_SolidData& data);
918 void setShapeData( _EdgesOnShape& eos, SMESH_subMesh* sm, _SolidData& data );
919 bool setEdgeData( _LayerEdge& edge, _EdgesOnShape& eos,
920 SMESH_MesherHelper& helper, _SolidData& data);
921 gp_XYZ getFaceNormal(const SMDS_MeshNode* n,
922 const TopoDS_Face& face,
923 SMESH_MesherHelper& helper,
925 bool shiftInside=false);
926 bool getFaceNormalAtSingularity(const gp_XY& uv,
927 const TopoDS_Face& face,
928 SMESH_MesherHelper& helper,
930 gp_XYZ getWeigthedNormal( const _LayerEdge* edge );
931 gp_XYZ getNormalByOffset( _LayerEdge* edge,
932 std::pair< TopoDS_Face, gp_XYZ > fId2Normal[],
934 bool lastNoOffset = false);
935 bool findNeiborsOnEdge(const _LayerEdge* edge,
936 const SMDS_MeshNode*& n1,
937 const SMDS_MeshNode*& n2,
940 void findSimplexTestEdges( _SolidData& data,
941 vector< vector<_LayerEdge*> >& edgesByGeom);
942 void computeGeomSize( _SolidData& data );
943 bool findShapesToSmooth( _SolidData& data);
944 void limitStepSizeByCurvature( _SolidData& data );
945 void limitStepSize( _SolidData& data,
946 const SMDS_MeshElement* face,
947 const _LayerEdge* maxCosinEdge );
948 void limitStepSize( _SolidData& data, const double minSize);
949 bool inflate(_SolidData& data);
950 bool smoothAndCheck(_SolidData& data, const int nbSteps, double & distToIntersection);
951 int invalidateBadSmooth( _SolidData& data,
952 SMESH_MesherHelper& helper,
953 vector< _LayerEdge* >& badSmooEdges,
954 vector< _EdgesOnShape* >& eosC1,
956 void makeOffsetSurface( _EdgesOnShape& eos, SMESH_MesherHelper& );
957 void putOnOffsetSurface( _EdgesOnShape& eos, int infStep,
958 vector< _EdgesOnShape* >& eosC1,
959 int smooStep=0, int moveAll=false );
960 void findCollisionEdges( _SolidData& data, SMESH_MesherHelper& helper );
961 void findEdgesToUpdateNormalNearConvexFace( _ConvexFace & convFace,
963 SMESH_MesherHelper& helper );
964 void limitMaxLenByCurvature( _SolidData& data, SMESH_MesherHelper& helper );
965 void limitMaxLenByCurvature( _LayerEdge* e1, _LayerEdge* e2,
966 _EdgesOnShape& eos1, _EdgesOnShape& eos2,
967 const bool isSmoothable );
968 bool updateNormals( _SolidData& data, SMESH_MesherHelper& helper, int stepNb, double stepSize );
969 bool updateNormalsOfConvexFaces( _SolidData& data,
970 SMESH_MesherHelper& helper,
972 void updateNormalsOfC1Vertices( _SolidData& data );
973 bool updateNormalsOfSmoothed( _SolidData& data,
974 SMESH_MesherHelper& helper,
976 const double stepSize );
977 bool isNewNormalOk( _SolidData& data,
979 const gp_XYZ& newNormal);
980 bool refine(_SolidData& data);
981 bool shrink(_SolidData& data);
982 bool prepareEdgeToShrink( _LayerEdge& edge, _EdgesOnShape& eos,
983 SMESH_MesherHelper& helper,
984 const SMESHDS_SubMesh* faceSubMesh );
985 void restoreNoShrink( _LayerEdge& edge ) const;
986 void fixBadFaces(const TopoDS_Face& F,
987 SMESH_MesherHelper& helper,
990 set<const SMDS_MeshNode*> * involvedNodes=NULL);
991 bool addBoundaryElements(_SolidData& data);
993 bool error( const string& text, int solidID=-1 );
994 SMESHDS_Mesh* getMeshDS() const { return _mesh->GetMeshDS(); }
997 void makeGroupOfLE();
1000 SMESH_ComputeErrorPtr _error;
1002 vector< _SolidData > _sdVec;
1003 TopTools_IndexedMapOfShape _solids; // to find _SolidData by a solid
1004 TopTools_MapOfShape _shrinkedFaces;
1009 //--------------------------------------------------------------------------------
1011 * \brief Shrinker of nodes on the EDGE
1015 TopoDS_Edge _geomEdge;
1016 vector<double> _initU;
1017 vector<double> _normPar;
1018 vector<const SMDS_MeshNode*> _nodes;
1019 const _LayerEdge* _edges[2];
1022 void AddEdge( const _LayerEdge* e, _EdgesOnShape& eos, SMESH_MesherHelper& helper );
1023 void Compute(bool set3D, SMESH_MesherHelper& helper);
1024 void RestoreParams();
1025 void SwapSrcTgtNodes(SMESHDS_Mesh* mesh);
1026 const TopoDS_Edge& GeomEdge() const { return _geomEdge; }
1027 const SMDS_MeshNode* TgtNode( bool is2nd ) const
1028 { return _edges[is2nd] ? _edges[is2nd]->_nodes.back() : 0; }
1029 const SMDS_MeshNode* SrcNode( bool is2nd ) const
1030 { return _edges[is2nd] ? _edges[is2nd]->_nodes[0] : 0; }
1032 //--------------------------------------------------------------------------------
1034 * \brief Smoother of _LayerEdge's on EDGE.
1038 struct OffPnt // point of the offsetted EDGE
1040 gp_XYZ _xyz; // coord of a point inflated from EDGE w/o smooth
1041 double _len; // length reached at previous inflation step
1042 double _param; // on EDGE
1043 _2NearEdges _2edges; // 2 neighbor _LayerEdge's
1044 gp_XYZ _edgeDir;// EDGE tangent at _param
1045 double Distance( const OffPnt& p ) const { return ( _xyz - p._xyz ).Modulus(); }
1047 vector< OffPnt > _offPoints;
1048 vector< double > _leParams; // normalized param of _eos._edges on EDGE
1049 Handle(Geom_Curve) _anaCurve; // for analytic smooth
1050 _LayerEdge _leOnV[2]; // _LayerEdge's holding normal to the EDGE at VERTEXes
1051 gp_XYZ _edgeDir[2]; // tangent at VERTEXes
1052 size_t _iSeg[2]; // index of segment where extreme tgt node is projected
1053 _EdgesOnShape& _eos;
1054 double _curveLen; // length of the EDGE
1055 std::pair<int,int> _eToSmooth[2]; // <from,to> indices of _LayerEdge's in _eos
1057 static Handle(Geom_Curve) CurveForSmooth( const TopoDS_Edge& E,
1059 SMESH_MesherHelper& helper);
1061 _Smoother1D( Handle(Geom_Curve) curveForSmooth,
1062 _EdgesOnShape& eos )
1063 : _anaCurve( curveForSmooth ), _eos( eos )
1066 bool Perform(_SolidData& data,
1067 Handle(ShapeAnalysis_Surface)& surface,
1068 const TopoDS_Face& F,
1069 SMESH_MesherHelper& helper );
1071 void prepare(_SolidData& data );
1073 void findEdgesToSmooth();
1075 bool isToSmooth( int iE );
1077 bool smoothAnalyticEdge( _SolidData& data,
1078 Handle(ShapeAnalysis_Surface)& surface,
1079 const TopoDS_Face& F,
1080 SMESH_MesherHelper& helper);
1081 bool smoothComplexEdge( _SolidData& data,
1082 Handle(ShapeAnalysis_Surface)& surface,
1083 const TopoDS_Face& F,
1084 SMESH_MesherHelper& helper);
1085 gp_XYZ getNormalNormal( const gp_XYZ & normal,
1086 const gp_XYZ& edgeDir);
1087 _LayerEdge* getLEdgeOnV( bool is2nd )
1089 return _eos._edges[ is2nd ? _eos._edges.size()-1 : 0 ]->_2neibors->_edges[ is2nd ];
1091 bool isAnalytic() const { return !_anaCurve.IsNull(); }
1093 void offPointsToPython() const; // debug
1095 //--------------------------------------------------------------------------------
1097 * \brief Class of temporary mesh face.
1098 * We can't use SMDS_FaceOfNodes since it's impossible to set it's ID which is
1099 * needed because SMESH_ElementSearcher internaly uses set of elements sorted by ID
1101 struct _TmpMeshFace : public SMDS_MeshElement
1103 vector<const SMDS_MeshNode* > _nn;
1104 _TmpMeshFace( const vector<const SMDS_MeshNode*>& nodes,
1105 int id, int faceID=-1, int idInFace=-1):
1106 SMDS_MeshElement(id), _nn(nodes) { setShapeId(faceID); setIdInShape(idInFace); }
1107 virtual const SMDS_MeshNode* GetNode(const int ind) const { return _nn[ind]; }
1108 virtual SMDSAbs_ElementType GetType() const { return SMDSAbs_Face; }
1109 virtual vtkIdType GetVtkType() const { return -1; }
1110 virtual SMDSAbs_EntityType GetEntityType() const { return SMDSEntity_Last; }
1111 virtual SMDSAbs_GeometryType GetGeomType() const
1112 { return _nn.size() == 3 ? SMDSGeom_TRIANGLE : SMDSGeom_QUADRANGLE; }
1113 virtual SMDS_ElemIteratorPtr elementsIterator(SMDSAbs_ElementType) const
1114 { return SMDS_ElemIteratorPtr( new SMDS_NodeVectorElemIterator( _nn.begin(), _nn.end()));}
1116 //--------------------------------------------------------------------------------
1118 * \brief Class of temporary mesh face storing _LayerEdge it's based on
1120 struct _TmpMeshFaceOnEdge : public _TmpMeshFace
1122 _LayerEdge *_le1, *_le2;
1123 _TmpMeshFaceOnEdge( _LayerEdge* le1, _LayerEdge* le2, int ID ):
1124 _TmpMeshFace( vector<const SMDS_MeshNode*>(4), ID ), _le1(le1), _le2(le2)
1126 _nn[0]=_le1->_nodes[0];
1127 _nn[1]=_le1->_nodes.back();
1128 _nn[2]=_le2->_nodes.back();
1129 _nn[3]=_le2->_nodes[0];
1131 gp_XYZ GetDir() const // return average direction of _LayerEdge's, normal to EDGE
1133 SMESH_TNodeXYZ p0s( _nn[0] );
1134 SMESH_TNodeXYZ p0t( _nn[1] );
1135 SMESH_TNodeXYZ p1t( _nn[2] );
1136 SMESH_TNodeXYZ p1s( _nn[3] );
1137 gp_XYZ v0 = p0t - p0s;
1138 gp_XYZ v1 = p1t - p1s;
1139 gp_XYZ v01 = p1s - p0s;
1140 gp_XYZ n = ( v0 ^ v01 ) + ( v1 ^ v01 );
1145 gp_XYZ GetDir(_LayerEdge* le1, _LayerEdge* le2) // return average direction of _LayerEdge's
1147 _nn[0]=le1->_nodes[0];
1148 _nn[1]=le1->_nodes.back();
1149 _nn[2]=le2->_nodes.back();
1150 _nn[3]=le2->_nodes[0];
1154 //--------------------------------------------------------------------------------
1156 * \brief Retriever of node coordinates either directly or from a surface by node UV.
1157 * \warning Location of a surface is ignored
1159 struct _NodeCoordHelper
1161 SMESH_MesherHelper& _helper;
1162 const TopoDS_Face& _face;
1163 Handle(Geom_Surface) _surface;
1164 gp_XYZ (_NodeCoordHelper::* _fun)(const SMDS_MeshNode* n) const;
1166 _NodeCoordHelper(const TopoDS_Face& F, SMESH_MesherHelper& helper, bool is2D)
1167 : _helper( helper ), _face( F )
1171 TopLoc_Location loc;
1172 _surface = BRep_Tool::Surface( _face, loc );
1174 if ( _surface.IsNull() )
1175 _fun = & _NodeCoordHelper::direct;
1177 _fun = & _NodeCoordHelper::byUV;
1179 gp_XYZ operator()(const SMDS_MeshNode* n) const { return (this->*_fun)( n ); }
1182 gp_XYZ direct(const SMDS_MeshNode* n) const
1184 return SMESH_TNodeXYZ( n );
1186 gp_XYZ byUV (const SMDS_MeshNode* n) const
1188 gp_XY uv = _helper.GetNodeUV( _face, n );
1189 return _surface->Value( uv.X(), uv.Y() ).XYZ();
1193 //================================================================================
1195 * \brief Check angle between vectors
1197 //================================================================================
1199 inline bool isLessAngle( const gp_Vec& v1, const gp_Vec& v2, const double cos )
1201 double dot = v1 * v2; // cos * |v1| * |v2|
1202 double l1 = v1.SquareMagnitude();
1203 double l2 = v2.SquareMagnitude();
1204 return (( dot * cos >= 0 ) &&
1205 ( dot * dot ) / l1 / l2 >= ( cos * cos ));
1208 } // namespace VISCOUS_3D
1212 //================================================================================
1213 // StdMeshers_ViscousLayers hypothesis
1215 StdMeshers_ViscousLayers::StdMeshers_ViscousLayers(int hypId, int studyId, SMESH_Gen* gen)
1216 :SMESH_Hypothesis(hypId, studyId, gen),
1217 _isToIgnoreShapes(1), _nbLayers(1), _thickness(1), _stretchFactor(1),
1218 _method( SURF_OFFSET_SMOOTH )
1220 _name = StdMeshers_ViscousLayers::GetHypType();
1221 _param_algo_dim = -3; // auxiliary hyp used by 3D algos
1222 } // --------------------------------------------------------------------------------
1223 void StdMeshers_ViscousLayers::SetBndShapes(const std::vector<int>& faceIds, bool toIgnore)
1225 if ( faceIds != _shapeIds )
1226 _shapeIds = faceIds, NotifySubMeshesHypothesisModification();
1227 if ( _isToIgnoreShapes != toIgnore )
1228 _isToIgnoreShapes = toIgnore, NotifySubMeshesHypothesisModification();
1229 } // --------------------------------------------------------------------------------
1230 void StdMeshers_ViscousLayers::SetTotalThickness(double thickness)
1232 if ( thickness != _thickness )
1233 _thickness = thickness, NotifySubMeshesHypothesisModification();
1234 } // --------------------------------------------------------------------------------
1235 void StdMeshers_ViscousLayers::SetNumberLayers(int nb)
1237 if ( _nbLayers != nb )
1238 _nbLayers = nb, NotifySubMeshesHypothesisModification();
1239 } // --------------------------------------------------------------------------------
1240 void StdMeshers_ViscousLayers::SetStretchFactor(double factor)
1242 if ( _stretchFactor != factor )
1243 _stretchFactor = factor, NotifySubMeshesHypothesisModification();
1244 } // --------------------------------------------------------------------------------
1245 void StdMeshers_ViscousLayers::SetMethod( ExtrusionMethod method )
1247 if ( _method != method )
1248 _method = method, NotifySubMeshesHypothesisModification();
1249 } // --------------------------------------------------------------------------------
1250 SMESH_ProxyMesh::Ptr
1251 StdMeshers_ViscousLayers::Compute(SMESH_Mesh& theMesh,
1252 const TopoDS_Shape& theShape,
1253 const bool toMakeN2NMap) const
1255 using namespace VISCOUS_3D;
1256 _ViscousBuilder builder;
1257 SMESH_ComputeErrorPtr err = builder.Compute( theMesh, theShape );
1258 if ( err && !err->IsOK() )
1259 return SMESH_ProxyMesh::Ptr();
1261 vector<SMESH_ProxyMesh::Ptr> components;
1262 TopExp_Explorer exp( theShape, TopAbs_SOLID );
1263 for ( ; exp.More(); exp.Next() )
1265 if ( _MeshOfSolid* pm =
1266 _ViscousListener::GetSolidMesh( &theMesh, exp.Current(), /*toCreate=*/false))
1268 if ( toMakeN2NMap && !pm->_n2nMapComputed )
1269 if ( !builder.MakeN2NMap( pm ))
1270 return SMESH_ProxyMesh::Ptr();
1271 components.push_back( SMESH_ProxyMesh::Ptr( pm ));
1272 pm->myIsDeletable = false; // it will de deleted by boost::shared_ptr
1274 if ( pm->_warning && !pm->_warning->IsOK() )
1276 SMESH_subMesh* sm = theMesh.GetSubMesh( exp.Current() );
1277 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1278 if ( !smError || smError->IsOK() )
1279 smError = pm->_warning;
1282 _ViscousListener::RemoveSolidMesh ( &theMesh, exp.Current() );
1284 switch ( components.size() )
1288 case 1: return components[0];
1290 default: return SMESH_ProxyMesh::Ptr( new SMESH_ProxyMesh( components ));
1292 return SMESH_ProxyMesh::Ptr();
1293 } // --------------------------------------------------------------------------------
1294 std::ostream & StdMeshers_ViscousLayers::SaveTo(std::ostream & save)
1296 save << " " << _nbLayers
1297 << " " << _thickness
1298 << " " << _stretchFactor
1299 << " " << _shapeIds.size();
1300 for ( size_t i = 0; i < _shapeIds.size(); ++i )
1301 save << " " << _shapeIds[i];
1302 save << " " << !_isToIgnoreShapes; // negate to keep the behavior in old studies.
1303 save << " " << _method;
1305 } // --------------------------------------------------------------------------------
1306 std::istream & StdMeshers_ViscousLayers::LoadFrom(std::istream & load)
1308 int nbFaces, faceID, shapeToTreat, method;
1309 load >> _nbLayers >> _thickness >> _stretchFactor >> nbFaces;
1310 while ( (int) _shapeIds.size() < nbFaces && load >> faceID )
1311 _shapeIds.push_back( faceID );
1312 if ( load >> shapeToTreat ) {
1313 _isToIgnoreShapes = !shapeToTreat;
1314 if ( load >> method )
1315 _method = (ExtrusionMethod) method;
1318 _isToIgnoreShapes = true; // old behavior
1321 } // --------------------------------------------------------------------------------
1322 bool StdMeshers_ViscousLayers::SetParametersByMesh(const SMESH_Mesh* theMesh,
1323 const TopoDS_Shape& theShape)
1327 } // --------------------------------------------------------------------------------
1328 SMESH_ComputeErrorPtr
1329 StdMeshers_ViscousLayers::CheckHypothesis(SMESH_Mesh& theMesh,
1330 const TopoDS_Shape& theShape,
1331 SMESH_Hypothesis::Hypothesis_Status& theStatus)
1333 VISCOUS_3D::_ViscousBuilder builder;
1334 SMESH_ComputeErrorPtr err = builder.CheckHypotheses( theMesh, theShape );
1335 if ( err && !err->IsOK() )
1336 theStatus = SMESH_Hypothesis::HYP_INCOMPAT_HYPS;
1338 theStatus = SMESH_Hypothesis::HYP_OK;
1342 // --------------------------------------------------------------------------------
1343 bool StdMeshers_ViscousLayers::IsShapeWithLayers(int shapeIndex) const
1346 ( std::find( _shapeIds.begin(), _shapeIds.end(), shapeIndex ) != _shapeIds.end() );
1347 return IsToIgnoreShapes() ? !isIn : isIn;
1349 // END StdMeshers_ViscousLayers hypothesis
1350 //================================================================================
1352 namespace VISCOUS_3D
1354 gp_XYZ getEdgeDir( const TopoDS_Edge& E, const TopoDS_Vertex& fromV )
1358 Handle(Geom_Curve) c = BRep_Tool::Curve( E, f, l );
1359 if ( c.IsNull() ) return gp_XYZ( Precision::Infinite(), 1e100, 1e100 );
1360 gp_Pnt p = BRep_Tool::Pnt( fromV );
1361 double distF = p.SquareDistance( c->Value( f ));
1362 double distL = p.SquareDistance( c->Value( l ));
1363 c->D1(( distF < distL ? f : l), p, dir );
1364 if ( distL < distF ) dir.Reverse();
1367 //--------------------------------------------------------------------------------
1368 gp_XYZ getEdgeDir( const TopoDS_Edge& E, const SMDS_MeshNode* atNode,
1369 SMESH_MesherHelper& helper)
1372 double f,l; gp_Pnt p;
1373 Handle(Geom_Curve) c = BRep_Tool::Curve( E, f, l );
1374 if ( c.IsNull() ) return gp_XYZ( Precision::Infinite(), 1e100, 1e100 );
1375 double u = helper.GetNodeU( E, atNode );
1379 //--------------------------------------------------------------------------------
1380 gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Vertex& fromV,
1381 const SMDS_MeshNode* node, SMESH_MesherHelper& helper, bool& ok,
1383 //--------------------------------------------------------------------------------
1384 gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Edge& fromE,
1385 const SMDS_MeshNode* node, SMESH_MesherHelper& helper, bool& ok)
1388 Handle(Geom_Curve) c = BRep_Tool::Curve( fromE, f, l );
1391 TopoDS_Vertex v = helper.IthVertex( 0, fromE );
1392 return getFaceDir( F, v, node, helper, ok );
1394 gp_XY uv = helper.GetNodeUV( F, node, 0, &ok );
1395 Handle(Geom_Surface) surface = BRep_Tool::Surface( F );
1396 gp_Pnt p; gp_Vec du, dv, norm;
1397 surface->D1( uv.X(),uv.Y(), p, du,dv );
1400 double u = helper.GetNodeU( fromE, node, 0, &ok );
1402 TopAbs_Orientation o = helper.GetSubShapeOri( F.Oriented(TopAbs_FORWARD), fromE);
1403 if ( o == TopAbs_REVERSED )
1406 gp_Vec dir = norm ^ du;
1408 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX &&
1409 helper.IsClosedEdge( fromE ))
1411 if ( fabs(u-f) < fabs(u-l)) c->D1( l, p, dv );
1412 else c->D1( f, p, dv );
1413 if ( o == TopAbs_REVERSED )
1415 gp_Vec dir2 = norm ^ dv;
1416 dir = dir.Normalized() + dir2.Normalized();
1420 //--------------------------------------------------------------------------------
1421 gp_XYZ getFaceDir( const TopoDS_Face& F, const TopoDS_Vertex& fromV,
1422 const SMDS_MeshNode* node, SMESH_MesherHelper& helper,
1423 bool& ok, double* cosin)
1425 TopoDS_Face faceFrw = F;
1426 faceFrw.Orientation( TopAbs_FORWARD );
1427 //double f,l; TopLoc_Location loc;
1428 TopoDS_Edge edges[2]; // sharing a vertex
1431 TopoDS_Vertex VV[2];
1432 TopExp_Explorer exp( faceFrw, TopAbs_EDGE );
1433 for ( ; exp.More() && nbEdges < 2; exp.Next() )
1435 const TopoDS_Edge& e = TopoDS::Edge( exp.Current() );
1436 if ( SMESH_Algo::isDegenerated( e )) continue;
1437 TopExp::Vertices( e, VV[0], VV[1], /*CumOri=*/true );
1438 if ( VV[1].IsSame( fromV )) {
1439 nbEdges += edges[ 0 ].IsNull();
1442 else if ( VV[0].IsSame( fromV )) {
1443 nbEdges += edges[ 1 ].IsNull();
1448 gp_XYZ dir(0,0,0), edgeDir[2];
1451 // get dirs of edges going fromV
1453 for ( size_t i = 0; i < nbEdges && ok; ++i )
1455 edgeDir[i] = getEdgeDir( edges[i], fromV );
1456 double size2 = edgeDir[i].SquareModulus();
1457 if (( ok = size2 > numeric_limits<double>::min() ))
1458 edgeDir[i] /= sqrt( size2 );
1460 if ( !ok ) return dir;
1462 // get angle between the 2 edges
1464 double angle = helper.GetAngle( edges[0], edges[1], faceFrw, fromV, &faceNormal );
1465 if ( Abs( angle ) < 5 * M_PI/180 )
1467 dir = ( faceNormal.XYZ() ^ edgeDir[0].Reversed()) + ( faceNormal.XYZ() ^ edgeDir[1] );
1471 dir = edgeDir[0] + edgeDir[1];
1476 double angle = gp_Vec( edgeDir[0] ).Angle( dir );
1477 *cosin = Cos( angle );
1480 else if ( nbEdges == 1 )
1482 dir = getFaceDir( faceFrw, edges[ edges[0].IsNull() ], node, helper, ok );
1483 if ( cosin ) *cosin = 1.;
1493 //================================================================================
1495 * \brief Finds concave VERTEXes of a FACE
1497 //================================================================================
1499 bool getConcaveVertices( const TopoDS_Face& F,
1500 SMESH_MesherHelper& helper,
1501 set< TGeomID >* vertices = 0)
1503 // check angles at VERTEXes
1505 TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, *helper.GetMesh(), 0, error );
1506 for ( size_t iW = 0; iW < wires.size(); ++iW )
1508 const int nbEdges = wires[iW]->NbEdges();
1509 if ( nbEdges < 2 && SMESH_Algo::isDegenerated( wires[iW]->Edge(0)))
1511 for ( int iE1 = 0; iE1 < nbEdges; ++iE1 )
1513 if ( SMESH_Algo::isDegenerated( wires[iW]->Edge( iE1 ))) continue;
1514 int iE2 = ( iE1 + 1 ) % nbEdges;
1515 while ( SMESH_Algo::isDegenerated( wires[iW]->Edge( iE2 )))
1516 iE2 = ( iE2 + 1 ) % nbEdges;
1517 TopoDS_Vertex V = wires[iW]->FirstVertex( iE2 );
1518 double angle = helper.GetAngle( wires[iW]->Edge( iE1 ),
1519 wires[iW]->Edge( iE2 ), F, V );
1520 if ( angle < -5. * M_PI / 180. )
1524 vertices->insert( helper.GetMeshDS()->ShapeToIndex( V ));
1528 return vertices ? !vertices->empty() : false;
1531 //================================================================================
1533 * \brief Returns true if a FACE is bound by a concave EDGE
1535 //================================================================================
1537 bool isConcave( const TopoDS_Face& F,
1538 SMESH_MesherHelper& helper,
1539 set< TGeomID >* vertices = 0 )
1541 bool isConcv = false;
1542 // if ( helper.Count( F, TopAbs_WIRE, /*useMap=*/false) > 1 )
1544 gp_Vec2d drv1, drv2;
1546 TopExp_Explorer eExp( F.Oriented( TopAbs_FORWARD ), TopAbs_EDGE );
1547 for ( ; eExp.More(); eExp.Next() )
1549 const TopoDS_Edge& E = TopoDS::Edge( eExp.Current() );
1550 if ( SMESH_Algo::isDegenerated( E )) continue;
1551 // check if 2D curve is concave
1552 BRepAdaptor_Curve2d curve( E, F );
1553 const int nbIntervals = curve.NbIntervals( GeomAbs_C2 );
1554 TColStd_Array1OfReal intervals(1, nbIntervals + 1 );
1555 curve.Intervals( intervals, GeomAbs_C2 );
1556 bool isConvex = true;
1557 for ( int i = 1; i <= nbIntervals && isConvex; ++i )
1559 double u1 = intervals( i );
1560 double u2 = intervals( i+1 );
1561 curve.D2( 0.5*( u1+u2 ), p, drv1, drv2 );
1562 double cross = drv1 ^ drv2;
1563 if ( E.Orientation() == TopAbs_REVERSED )
1565 isConvex = ( cross > -1e-9 ); // 0.1 );
1569 //cout << "Concave FACE " << helper.GetMeshDS()->ShapeToIndex( F ) << endl;
1578 // check angles at VERTEXes
1579 if ( getConcaveVertices( F, helper, vertices ))
1585 //================================================================================
1587 * \brief Computes mimimal distance of face in-FACE nodes from an EDGE
1588 * \param [in] face - the mesh face to treat
1589 * \param [in] nodeOnEdge - a node on the EDGE
1590 * \param [out] faceSize - the computed distance
1591 * \return bool - true if faceSize computed
1593 //================================================================================
1595 bool getDistFromEdge( const SMDS_MeshElement* face,
1596 const SMDS_MeshNode* nodeOnEdge,
1599 faceSize = Precision::Infinite();
1602 int nbN = face->NbCornerNodes();
1603 int iOnE = face->GetNodeIndex( nodeOnEdge );
1604 int iNext[2] = { SMESH_MesherHelper::WrapIndex( iOnE+1, nbN ),
1605 SMESH_MesherHelper::WrapIndex( iOnE-1, nbN ) };
1606 const SMDS_MeshNode* nNext[2] = { face->GetNode( iNext[0] ),
1607 face->GetNode( iNext[1] ) };
1608 gp_XYZ segVec, segEnd = SMESH_TNodeXYZ( nodeOnEdge ); // segment on EDGE
1609 double segLen = -1.;
1610 // look for two neighbor not in-FACE nodes of face
1611 for ( int i = 0; i < 2; ++i )
1613 if (( nNext[i]->GetPosition()->GetDim() != 2 ) &&
1614 ( nodeOnEdge->GetPosition()->GetDim() == 0 || nNext[i]->GetID() < nodeOnEdge->GetID() ))
1616 // look for an in-FACE node
1617 for ( int iN = 0; iN < nbN; ++iN )
1619 if ( iN == iOnE || iN == iNext[i] )
1621 SMESH_TNodeXYZ pInFace = face->GetNode( iN );
1622 gp_XYZ v = pInFace - segEnd;
1625 segVec = SMESH_TNodeXYZ( nNext[i] ) - segEnd;
1626 segLen = segVec.Modulus();
1628 double distToSeg = v.Crossed( segVec ).Modulus() / segLen;
1629 faceSize = Min( faceSize, distToSeg );
1637 //================================================================================
1639 * \brief Return direction of axis or revolution of a surface
1641 //================================================================================
1643 bool getRovolutionAxis( const Adaptor3d_Surface& surface,
1646 switch ( surface.GetType() ) {
1649 gp_Cone cone = surface.Cone();
1650 axis = cone.Axis().Direction();
1653 case GeomAbs_Sphere:
1655 gp_Sphere sphere = surface.Sphere();
1656 axis = sphere.Position().Direction();
1659 case GeomAbs_SurfaceOfRevolution:
1661 axis = surface.AxeOfRevolution().Direction();
1664 //case GeomAbs_SurfaceOfExtrusion:
1665 case GeomAbs_OffsetSurface:
1667 Handle(Adaptor3d_HSurface) base = surface.BasisSurface();
1668 return getRovolutionAxis( base->Surface(), axis );
1670 default: return false;
1675 //--------------------------------------------------------------------------------
1676 // DEBUG. Dump intermediate node positions into a python script
1677 // HOWTO use: run python commands written in a console to see
1678 // construction steps of viscous layers
1684 PyDump(SMESH_Mesh& m) {
1685 int tag = 3 + m.GetId();
1686 const char* fname = "/tmp/viscous.py";
1687 cout << "execfile('"<<fname<<"')"<<endl;
1688 py = _pyStream = new ofstream(fname);
1689 *py << "import SMESH" << endl
1690 << "from salome.smesh import smeshBuilder" << endl
1691 << "smesh = smeshBuilder.New(salome.myStudy)" << endl
1692 << "meshSO = smesh.GetCurrentStudy().FindObjectID('0:1:2:" << tag <<"')" << endl
1693 << "mesh = smesh.Mesh( meshSO.GetObject() )"<<endl;
1698 *py << "mesh.GroupOnFilter(SMESH.VOLUME,'Viscous Prisms',"
1699 "smesh.GetFilter(SMESH.VOLUME,SMESH.FT_ElemGeomType,'=',SMESH.Geom_PENTA))"<<endl;
1700 *py << "mesh.GroupOnFilter(SMESH.VOLUME,'Neg Volumes',"
1701 "smesh.GetFilter(SMESH.VOLUME,SMESH.FT_Volume3D,'<',0))"<<endl;
1705 ~PyDump() { Finish(); cout << "NB FUNCTIONS: " << theNbPyFunc << endl; }
1706 struct MyStream : public ostream
1708 template <class T> ostream & operator<<( const T &anything ) { return *this ; }
1710 void Pause() { py = &_mystream; }
1711 void Resume() { py = _pyStream; }
1715 #define dumpFunction(f) { _dumpFunction(f, __LINE__);}
1716 #define dumpMove(n) { _dumpMove(n, __LINE__);}
1717 #define dumpMoveComm(n,txt) { _dumpMove(n, __LINE__, txt);}
1718 #define dumpCmd(txt) { _dumpCmd(txt, __LINE__);}
1719 void _dumpFunction(const string& fun, int ln)
1720 { if (py) *py<< "def "<<fun<<"(): # "<< ln <<endl; cout<<fun<<"()"<<endl; ++theNbPyFunc; }
1721 void _dumpMove(const SMDS_MeshNode* n, int ln, const char* txt="")
1722 { if (py) *py<< " mesh.MoveNode( "<<n->GetID()<< ", "<< n->X()
1723 << ", "<<n->Y()<<", "<< n->Z()<< ")\t\t # "<< ln <<" "<< txt << endl; }
1724 void _dumpCmd(const string& txt, int ln)
1725 { if (py) *py<< " "<<txt<<" # "<< ln <<endl; }
1726 void dumpFunctionEnd()
1727 { if (py) *py<< " return"<< endl; }
1728 void dumpChangeNodes( const SMDS_MeshElement* f )
1729 { if (py) { *py<< " mesh.ChangeElemNodes( " << f->GetID()<<", [";
1730 for ( int i=1; i < f->NbNodes(); ++i ) *py << f->GetNode(i-1)->GetID()<<", ";
1731 *py << f->GetNode( f->NbNodes()-1 )->GetID() << " ])"<< endl; }}
1732 #define debugMsg( txt ) { cout << "# "<< txt << " (line: " << __LINE__ << ")" << endl; }
1736 struct PyDump { PyDump(SMESH_Mesh&) {} void Finish() {} void Pause() {} void Resume() {} };
1737 #define dumpFunction(f) f
1739 #define dumpMoveComm(n,txt)
1740 #define dumpCmd(txt)
1741 #define dumpFunctionEnd()
1742 #define dumpChangeNodes(f) { if(f) {} } // prevent "unused variable 'f'" warning
1743 #define debugMsg( txt ) {}
1748 using namespace VISCOUS_3D;
1750 //================================================================================
1752 * \brief Constructor of _ViscousBuilder
1754 //================================================================================
1756 _ViscousBuilder::_ViscousBuilder()
1758 _error = SMESH_ComputeError::New(COMPERR_OK);
1762 //================================================================================
1764 * \brief Stores error description and returns false
1766 //================================================================================
1768 bool _ViscousBuilder::error(const string& text, int solidId )
1770 const string prefix = string("Viscous layers builder: ");
1771 _error->myName = COMPERR_ALGO_FAILED;
1772 _error->myComment = prefix + text;
1775 SMESH_subMesh* sm = _mesh->GetSubMeshContaining( solidId );
1776 if ( !sm && !_sdVec.empty() )
1777 sm = _mesh->GetSubMeshContaining( solidId = _sdVec[0]._index );
1778 if ( sm && sm->GetSubShape().ShapeType() == TopAbs_SOLID )
1780 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1781 if ( smError && smError->myAlgo )
1782 _error->myAlgo = smError->myAlgo;
1784 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1786 // set KO to all solids
1787 for ( size_t i = 0; i < _sdVec.size(); ++i )
1789 if ( _sdVec[i]._index == solidId )
1791 sm = _mesh->GetSubMesh( _sdVec[i]._solid );
1792 if ( !sm->IsEmpty() )
1794 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1795 if ( !smError || smError->IsOK() )
1797 smError = SMESH_ComputeError::New( COMPERR_ALGO_FAILED, prefix + "failed");
1798 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
1802 makeGroupOfLE(); // debug
1807 //================================================================================
1809 * \brief At study restoration, restore event listeners used to clear an inferior
1810 * dim sub-mesh modified by viscous layers
1812 //================================================================================
1814 void _ViscousBuilder::RestoreListeners()
1819 //================================================================================
1821 * \brief computes SMESH_ProxyMesh::SubMesh::_n2n
1823 //================================================================================
1825 bool _ViscousBuilder::MakeN2NMap( _MeshOfSolid* pm )
1827 SMESH_subMesh* solidSM = pm->mySubMeshes.front();
1828 TopExp_Explorer fExp( solidSM->GetSubShape(), TopAbs_FACE );
1829 for ( ; fExp.More(); fExp.Next() )
1831 SMESHDS_SubMesh* srcSmDS = pm->GetMeshDS()->MeshElements( fExp.Current() );
1832 const SMESH_ProxyMesh::SubMesh* prxSmDS = pm->GetProxySubMesh( fExp.Current() );
1834 if ( !srcSmDS || !prxSmDS || !srcSmDS->NbElements() || !prxSmDS->NbElements() )
1836 if ( srcSmDS->GetElements()->next() == prxSmDS->GetElements()->next())
1839 if ( srcSmDS->NbElements() != prxSmDS->NbElements() )
1840 return error( "Different nb elements in a source and a proxy sub-mesh", solidSM->GetId());
1842 SMDS_ElemIteratorPtr srcIt = srcSmDS->GetElements();
1843 SMDS_ElemIteratorPtr prxIt = prxSmDS->GetElements();
1844 while( prxIt->more() )
1846 const SMDS_MeshElement* fSrc = srcIt->next();
1847 const SMDS_MeshElement* fPrx = prxIt->next();
1848 if ( fSrc->NbNodes() != fPrx->NbNodes())
1849 return error( "Different elements in a source and a proxy sub-mesh", solidSM->GetId());
1850 for ( int i = 0 ; i < fPrx->NbNodes(); ++i )
1851 pm->setNode2Node( fSrc->GetNode(i), fPrx->GetNode(i), prxSmDS );
1854 pm->_n2nMapComputed = true;
1858 //================================================================================
1860 * \brief Does its job
1862 //================================================================================
1864 SMESH_ComputeErrorPtr _ViscousBuilder::Compute(SMESH_Mesh& theMesh,
1865 const TopoDS_Shape& theShape)
1869 // check if proxy mesh already computed
1870 TopExp_Explorer exp( theShape, TopAbs_SOLID );
1872 return error("No SOLID's in theShape"), _error;
1874 if ( _ViscousListener::GetSolidMesh( _mesh, exp.Current(), /*toCreate=*/false))
1875 return SMESH_ComputeErrorPtr(); // everything already computed
1877 PyDump debugDump( theMesh );
1878 _pyDump = &debugDump;
1880 // TODO: ignore already computed SOLIDs
1881 if ( !findSolidsWithLayers())
1884 if ( !findFacesWithLayers() )
1887 for ( size_t i = 0; i < _sdVec.size(); ++i )
1890 for ( iSD = 0; iSD < _sdVec.size(); ++iSD ) // find next SOLID to compute
1891 if ( _sdVec[iSD]._before.IsEmpty() &&
1892 !_sdVec[iSD]._solid.IsNull() &&
1893 _sdVec[iSD]._n2eMap.empty() )
1896 if ( ! makeLayer(_sdVec[iSD]) ) // create _LayerEdge's
1899 if ( _sdVec[iSD]._n2eMap.size() == 0 ) // no layers in a SOLID
1901 _sdVec[iSD]._solid.Nullify();
1905 if ( ! inflate(_sdVec[iSD]) ) // increase length of _LayerEdge's
1908 if ( ! refine(_sdVec[iSD]) ) // create nodes and prisms
1911 if ( ! shrink(_sdVec[iSD]) ) // shrink 2D mesh on FACEs w/o layer
1914 addBoundaryElements(_sdVec[iSD]); // create quadrangles on prism bare sides
1916 const TopoDS_Shape& solid = _sdVec[iSD]._solid;
1917 for ( iSD = 0; iSD < _sdVec.size(); ++iSD )
1918 _sdVec[iSD]._before.Remove( solid );
1921 makeGroupOfLE(); // debug
1927 //================================================================================
1929 * \brief Check validity of hypotheses
1931 //================================================================================
1933 SMESH_ComputeErrorPtr _ViscousBuilder::CheckHypotheses( SMESH_Mesh& mesh,
1934 const TopoDS_Shape& shape )
1938 if ( _ViscousListener::GetSolidMesh( _mesh, shape, /*toCreate=*/false))
1939 return SMESH_ComputeErrorPtr(); // everything already computed
1942 findSolidsWithLayers();
1943 bool ok = findFacesWithLayers( true );
1945 // remove _MeshOfSolid's of _SolidData's
1946 for ( size_t i = 0; i < _sdVec.size(); ++i )
1947 _ViscousListener::RemoveSolidMesh( _mesh, _sdVec[i]._solid );
1952 return SMESH_ComputeErrorPtr();
1955 //================================================================================
1957 * \brief Finds SOLIDs to compute using viscous layers. Fills _sdVec
1959 //================================================================================
1961 bool _ViscousBuilder::findSolidsWithLayers()
1964 TopTools_IndexedMapOfShape allSolids;
1965 TopExp::MapShapes( _mesh->GetShapeToMesh(), TopAbs_SOLID, allSolids );
1966 _sdVec.reserve( allSolids.Extent());
1968 SMESH_HypoFilter filter;
1969 for ( int i = 1; i <= allSolids.Extent(); ++i )
1971 // find StdMeshers_ViscousLayers hyp assigned to the i-th solid
1972 SMESH_subMesh* sm = _mesh->GetSubMesh( allSolids(i) );
1973 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->NbElements() > 0 )
1974 continue; // solid is already meshed
1975 SMESH_Algo* algo = sm->GetAlgo();
1976 if ( !algo ) continue;
1977 // TODO: check if algo is hidden
1978 const list <const SMESHDS_Hypothesis *> & allHyps =
1979 algo->GetUsedHypothesis(*_mesh, allSolids(i), /*ignoreAuxiliary=*/false);
1980 _SolidData* soData = 0;
1981 list< const SMESHDS_Hypothesis *>::const_iterator hyp = allHyps.begin();
1982 const StdMeshers_ViscousLayers* viscHyp = 0;
1983 for ( ; hyp != allHyps.end(); ++hyp )
1984 if (( viscHyp = dynamic_cast<const StdMeshers_ViscousLayers*>( *hyp )))
1986 TopoDS_Shape hypShape;
1987 filter.Init( filter.Is( viscHyp ));
1988 _mesh->GetHypothesis( allSolids(i), filter, true, &hypShape );
1992 _MeshOfSolid* proxyMesh = _ViscousListener::GetSolidMesh( _mesh,
1995 _sdVec.push_back( _SolidData( allSolids(i), proxyMesh ));
1996 soData = & _sdVec.back();
1997 soData->_index = getMeshDS()->ShapeToIndex( allSolids(i));
1998 soData->_helper = new SMESH_MesherHelper( *_mesh );
1999 soData->_helper->SetSubShape( allSolids(i) );
2000 _solids.Add( allSolids(i) );
2002 soData->_hyps.push_back( viscHyp );
2003 soData->_hypShapes.push_back( hypShape );
2006 if ( _sdVec.empty() )
2008 ( SMESH_Comment(StdMeshers_ViscousLayers::GetHypType()) << " hypothesis not found",0);
2013 //================================================================================
2015 * \brief Set a _SolidData to be computed before another
2017 //================================================================================
2019 bool _ViscousBuilder::setBefore( _SolidData& solidBefore, _SolidData& solidAfter )
2021 // check possibility to set this order; get all solids before solidBefore
2022 TopTools_IndexedMapOfShape allSolidsBefore;
2023 allSolidsBefore.Add( solidBefore._solid );
2024 for ( int i = 1; i <= allSolidsBefore.Extent(); ++i )
2026 int iSD = _solids.FindIndex( allSolidsBefore(i) );
2029 TopTools_MapIteratorOfMapOfShape soIt( _sdVec[ iSD-1 ]._before );
2030 for ( ; soIt.More(); soIt.Next() )
2031 allSolidsBefore.Add( soIt.Value() );
2034 if ( allSolidsBefore.Contains( solidAfter._solid ))
2037 for ( int i = 1; i <= allSolidsBefore.Extent(); ++i )
2038 solidAfter._before.Add( allSolidsBefore(i) );
2043 //================================================================================
2047 //================================================================================
2049 bool _ViscousBuilder::findFacesWithLayers(const bool onlyWith)
2051 SMESH_MesherHelper helper( *_mesh );
2052 TopExp_Explorer exp;
2054 // collect all faces-to-ignore defined by hyp
2055 for ( size_t i = 0; i < _sdVec.size(); ++i )
2057 // get faces-to-ignore defined by each hyp
2058 typedef const StdMeshers_ViscousLayers* THyp;
2059 typedef std::pair< set<TGeomID>, THyp > TFacesOfHyp;
2060 list< TFacesOfHyp > ignoreFacesOfHyps;
2061 list< THyp >::iterator hyp = _sdVec[i]._hyps.begin();
2062 list< TopoDS_Shape >::iterator hypShape = _sdVec[i]._hypShapes.begin();
2063 for ( ; hyp != _sdVec[i]._hyps.end(); ++hyp, ++hypShape )
2065 ignoreFacesOfHyps.push_back( TFacesOfHyp( set<TGeomID>(), *hyp ));
2066 getIgnoreFaces( _sdVec[i]._solid, *hyp, *hypShape, ignoreFacesOfHyps.back().first );
2069 // fill _SolidData::_face2hyp and check compatibility of hypotheses
2070 const int nbHyps = _sdVec[i]._hyps.size();
2073 // check if two hypotheses define different parameters for the same FACE
2074 list< TFacesOfHyp >::iterator igFacesOfHyp;
2075 for ( exp.Init( _sdVec[i]._solid, TopAbs_FACE ); exp.More(); exp.Next() )
2077 const TGeomID faceID = getMeshDS()->ShapeToIndex( exp.Current() );
2079 igFacesOfHyp = ignoreFacesOfHyps.begin();
2080 for ( ; igFacesOfHyp != ignoreFacesOfHyps.end(); ++igFacesOfHyp )
2081 if ( ! igFacesOfHyp->first.count( faceID ))
2084 return error(SMESH_Comment("Several hypotheses define "
2085 "Viscous Layers on the face #") << faceID );
2086 hyp = igFacesOfHyp->second;
2089 _sdVec[i]._face2hyp.insert( make_pair( faceID, hyp ));
2091 _sdVec[i]._ignoreFaceIds.insert( faceID );
2094 // check if two hypotheses define different number of viscous layers for
2095 // adjacent faces of a solid
2096 set< int > nbLayersSet;
2097 igFacesOfHyp = ignoreFacesOfHyps.begin();
2098 for ( ; igFacesOfHyp != ignoreFacesOfHyps.end(); ++igFacesOfHyp )
2100 nbLayersSet.insert( igFacesOfHyp->second->GetNumberLayers() );
2102 if ( nbLayersSet.size() > 1 )
2104 for ( exp.Init( _sdVec[i]._solid, TopAbs_EDGE ); exp.More(); exp.Next() )
2106 PShapeIteratorPtr fIt = helper.GetAncestors( exp.Current(), *_mesh, TopAbs_FACE );
2107 THyp hyp1 = 0, hyp2 = 0;
2108 while( const TopoDS_Shape* face = fIt->next() )
2110 const TGeomID faceID = getMeshDS()->ShapeToIndex( *face );
2111 map< TGeomID, THyp >::iterator f2h = _sdVec[i]._face2hyp.find( faceID );
2112 if ( f2h != _sdVec[i]._face2hyp.end() )
2114 ( hyp1 ? hyp2 : hyp1 ) = f2h->second;
2117 if ( hyp1 && hyp2 &&
2118 hyp1->GetNumberLayers() != hyp2->GetNumberLayers() )
2120 return error("Two hypotheses define different number of "
2121 "viscous layers on adjacent faces");
2125 } // if ( nbHyps > 1 )
2128 _sdVec[i]._ignoreFaceIds.swap( ignoreFacesOfHyps.back().first );
2132 if ( onlyWith ) // is called to check hypotheses compatibility only
2135 // fill _SolidData::_reversedFaceIds
2136 for ( size_t i = 0; i < _sdVec.size(); ++i )
2138 exp.Init( _sdVec[i]._solid.Oriented( TopAbs_FORWARD ), TopAbs_FACE );
2139 for ( ; exp.More(); exp.Next() )
2141 const TopoDS_Face& face = TopoDS::Face( exp.Current() );
2142 const TGeomID faceID = getMeshDS()->ShapeToIndex( face );
2143 if ( //!sdVec[i]._ignoreFaceIds.count( faceID ) &&
2144 helper.NbAncestors( face, *_mesh, TopAbs_SOLID ) > 1 &&
2145 helper.IsReversedSubMesh( face ))
2147 _sdVec[i]._reversedFaceIds.insert( faceID );
2152 // Find FACEs to shrink mesh on (solution 2 in issue 0020832): fill in _shrinkShape2Shape
2153 TopTools_IndexedMapOfShape shapes;
2154 std::string structAlgoName = "Hexa_3D";
2155 for ( size_t i = 0; i < _sdVec.size(); ++i )
2158 TopExp::MapShapes(_sdVec[i]._solid, TopAbs_EDGE, shapes);
2159 for ( int iE = 1; iE <= shapes.Extent(); ++iE )
2161 const TopoDS_Shape& edge = shapes(iE);
2162 // find 2 FACEs sharing an EDGE
2164 PShapeIteratorPtr fIt = helper.GetAncestors(edge, *_mesh, TopAbs_FACE, &_sdVec[i]._solid);
2165 while ( fIt->more())
2167 const TopoDS_Shape* f = fIt->next();
2168 FF[ int( !FF[0].IsNull()) ] = *f;
2170 if( FF[1].IsNull() ) continue; // seam edge can be shared by 1 FACE only
2172 // check presence of layers on them
2174 for ( int j = 0; j < 2; ++j )
2175 ignore[j] = _sdVec[i]._ignoreFaceIds.count( getMeshDS()->ShapeToIndex( FF[j] ));
2176 if ( ignore[0] == ignore[1] )
2177 continue; // nothing interesting
2178 TopoDS_Shape fWOL = FF[ ignore[0] ? 0 : 1 ];
2181 if ( !fWOL.IsNull())
2183 TGeomID edgeInd = getMeshDS()->ShapeToIndex( edge );
2184 _sdVec[i]._shrinkShape2Shape.insert( make_pair( edgeInd, fWOL ));
2189 // Find the SHAPE along which to inflate _LayerEdge based on VERTEX
2191 for ( size_t i = 0; i < _sdVec.size(); ++i )
2194 TopExp::MapShapes(_sdVec[i]._solid, TopAbs_VERTEX, shapes);
2195 for ( int iV = 1; iV <= shapes.Extent(); ++iV )
2197 const TopoDS_Shape& vertex = shapes(iV);
2198 // find faces WOL sharing the vertex
2199 vector< TopoDS_Shape > facesWOL;
2200 size_t totalNbFaces = 0;
2201 PShapeIteratorPtr fIt = helper.GetAncestors(vertex, *_mesh, TopAbs_FACE, &_sdVec[i]._solid );
2202 while ( fIt->more())
2204 const TopoDS_Shape* f = fIt->next();
2206 const int fID = getMeshDS()->ShapeToIndex( *f );
2207 if ( _sdVec[i]._ignoreFaceIds.count ( fID ) /*&& !_sdVec[i]._noShrinkShapes.count( fID )*/)
2208 facesWOL.push_back( *f );
2210 if ( facesWOL.size() == totalNbFaces || facesWOL.empty() )
2211 continue; // no layers at this vertex or no WOL
2212 TGeomID vInd = getMeshDS()->ShapeToIndex( vertex );
2213 switch ( facesWOL.size() )
2217 helper.SetSubShape( facesWOL[0] );
2218 if ( helper.IsRealSeam( vInd )) // inflate along a seam edge?
2220 TopoDS_Shape seamEdge;
2221 PShapeIteratorPtr eIt = helper.GetAncestors(vertex, *_mesh, TopAbs_EDGE);
2222 while ( eIt->more() && seamEdge.IsNull() )
2224 const TopoDS_Shape* e = eIt->next();
2225 if ( helper.IsRealSeam( *e ) )
2228 if ( !seamEdge.IsNull() )
2230 _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, seamEdge ));
2234 _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, facesWOL[0] ));
2239 // find an edge shared by 2 faces
2240 PShapeIteratorPtr eIt = helper.GetAncestors(vertex, *_mesh, TopAbs_EDGE);
2241 while ( eIt->more())
2243 const TopoDS_Shape* e = eIt->next();
2244 if ( helper.IsSubShape( *e, facesWOL[0]) &&
2245 helper.IsSubShape( *e, facesWOL[1]))
2247 _sdVec[i]._shrinkShape2Shape.insert( make_pair( vInd, *e )); break;
2253 return error("Not yet supported case", _sdVec[i]._index);
2258 // Add to _noShrinkShapes sub-shapes of FACE's that can't be shrinked since
2259 // the algo of the SOLID sharing the FACE does not support it or for other reasons
2260 set< string > notSupportAlgos; notSupportAlgos.insert( structAlgoName );
2261 for ( size_t i = 0; i < _sdVec.size(); ++i )
2263 map< TGeomID, TopoDS_Shape >::iterator e2f = _sdVec[i]._shrinkShape2Shape.begin();
2264 for ( ; e2f != _sdVec[i]._shrinkShape2Shape.end(); ++e2f )
2266 const TopoDS_Shape& fWOL = e2f->second;
2267 const TGeomID edgeID = e2f->first;
2268 TGeomID faceID = getMeshDS()->ShapeToIndex( fWOL );
2269 TopoDS_Shape edge = getMeshDS()->IndexToShape( edgeID );
2270 if ( edge.ShapeType() != TopAbs_EDGE )
2271 continue; // shrink shape is VERTEX
2274 PShapeIteratorPtr soIt = helper.GetAncestors(fWOL, *_mesh, TopAbs_SOLID);
2275 while ( soIt->more() && solid.IsNull() )
2277 const TopoDS_Shape* so = soIt->next();
2278 if ( !so->IsSame( _sdVec[i]._solid ))
2281 if ( solid.IsNull() )
2284 bool noShrinkE = false;
2285 SMESH_Algo* algo = _mesh->GetSubMesh( solid )->GetAlgo();
2286 bool isStructured = ( algo && algo->GetName() == structAlgoName );
2287 size_t iSolid = _solids.FindIndex( solid ) - 1;
2288 if ( iSolid < _sdVec.size() && _sdVec[ iSolid ]._ignoreFaceIds.count( faceID ))
2290 // the adjacent SOLID has NO layers on fWOL;
2291 // shrink allowed if
2292 // - there are layers on the EDGE in the adjacent SOLID
2293 // - there are NO layers in the adjacent SOLID && algo is unstructured and computed later
2294 bool hasWLAdj = (_sdVec[iSolid]._shrinkShape2Shape.count( edgeID ));
2295 bool shrinkAllowed = (( hasWLAdj ) ||
2296 ( !isStructured && setBefore( _sdVec[ i ], _sdVec[ iSolid ] )));
2297 noShrinkE = !shrinkAllowed;
2299 else if ( iSolid < _sdVec.size() )
2301 // the adjacent SOLID has layers on fWOL;
2302 // check if SOLID's mesh is unstructured and then try to set it
2303 // to be computed after the i-th solid
2304 if ( isStructured || !setBefore( _sdVec[ i ], _sdVec[ iSolid ] ))
2305 noShrinkE = true; // don't shrink fWOL
2309 // the adjacent SOLID has NO layers at all
2310 noShrinkE = isStructured;
2315 _sdVec[i]._noShrinkShapes.insert( edgeID );
2317 // check if there is a collision with to-shrink-from EDGEs in iSolid
2318 // if ( iSolid < _sdVec.size() )
2321 // TopExp::MapShapes( fWOL, TopAbs_EDGE, shapes);
2322 // for ( int iE = 1; iE <= shapes.Extent(); ++iE )
2324 // const TopoDS_Edge& E = TopoDS::Edge( shapes( iE ));
2325 // const TGeomID eID = getMeshDS()->ShapeToIndex( E );
2326 // if ( eID == edgeID ||
2327 // !_sdVec[iSolid]._shrinkShape2Shape.count( eID ) ||
2328 // _sdVec[i]._noShrinkShapes.count( eID ))
2330 // for ( int is1st = 0; is1st < 2; ++is1st )
2332 // TopoDS_Vertex V = helper.IthVertex( is1st, E );
2333 // if ( _sdVec[i]._noShrinkShapes.count( getMeshDS()->ShapeToIndex( V ) ))
2335 // return error("No way to make a conformal mesh with "
2336 // "the given set of faces with layers", _sdVec[i]._index);
2343 // add VERTEXes of the edge in _noShrinkShapes, which is necessary if
2344 // _shrinkShape2Shape is different in the adjacent SOLID
2345 for ( TopoDS_Iterator vIt( edge ); vIt.More(); vIt.Next() )
2347 TGeomID vID = getMeshDS()->ShapeToIndex( vIt.Value() );
2348 bool noShrinkV = false, noShrinkIfAdjMeshed = false;
2350 if ( iSolid < _sdVec.size() )
2352 if ( _sdVec[ iSolid ]._ignoreFaceIds.count( faceID ))
2354 map< TGeomID, TopoDS_Shape >::iterator i2S, i2SAdj;
2355 i2S = _sdVec[i ]._shrinkShape2Shape.find( vID );
2356 i2SAdj = _sdVec[iSolid]._shrinkShape2Shape.find( vID );
2357 if ( i2SAdj == _sdVec[iSolid]._shrinkShape2Shape.end() )
2358 noShrinkV = (( isStructured ) ||
2359 ( noShrinkIfAdjMeshed = i2S->second.ShapeType() == TopAbs_EDGE ));
2361 noShrinkV = ( ! i2S->second.IsSame( i2SAdj->second ));
2365 noShrinkV = noShrinkE;
2370 // the adjacent SOLID has NO layers at all
2377 noShrinkV = noShrinkIfAdjMeshed =
2378 ( _sdVec[i]._shrinkShape2Shape[ vID ].ShapeType() == TopAbs_EDGE );
2382 if ( noShrinkV && noShrinkIfAdjMeshed )
2384 // noShrinkV if FACEs in the adjacent SOLID are meshed
2385 PShapeIteratorPtr fIt = helper.GetAncestors( _sdVec[i]._shrinkShape2Shape[ vID ],
2386 *_mesh, TopAbs_FACE, &solid );
2387 while ( fIt->more() )
2389 const TopoDS_Shape* f = fIt->next();
2390 if ( !f->IsSame( fWOL ))
2392 noShrinkV = ! _mesh->GetSubMesh( *f )->IsEmpty();
2398 _sdVec[i]._noShrinkShapes.insert( vID );
2401 } // loop on _sdVec[i]._shrinkShape2Shape
2402 } // loop on _sdVec to fill in _SolidData::_noShrinkShapes
2405 // add FACEs of other SOLIDs to _ignoreFaceIds
2406 for ( size_t i = 0; i < _sdVec.size(); ++i )
2409 TopExp::MapShapes(_sdVec[i]._solid, TopAbs_FACE, shapes);
2411 for ( exp.Init( _mesh->GetShapeToMesh(), TopAbs_FACE ); exp.More(); exp.Next() )
2413 if ( !shapes.Contains( exp.Current() ))
2414 _sdVec[i]._ignoreFaceIds.insert( getMeshDS()->ShapeToIndex( exp.Current() ));
2421 //================================================================================
2423 * \brief Finds FACEs w/o layers for a given SOLID by an hypothesis
2425 //================================================================================
2427 void _ViscousBuilder::getIgnoreFaces(const TopoDS_Shape& solid,
2428 const StdMeshers_ViscousLayers* hyp,
2429 const TopoDS_Shape& hypShape,
2430 set<TGeomID>& ignoreFaceIds)
2432 TopExp_Explorer exp;
2434 vector<TGeomID> ids = hyp->GetBndShapes();
2435 if ( hyp->IsToIgnoreShapes() ) // FACEs to ignore are given
2437 for ( size_t ii = 0; ii < ids.size(); ++ii )
2439 const TopoDS_Shape& s = getMeshDS()->IndexToShape( ids[ii] );
2440 if ( !s.IsNull() && s.ShapeType() == TopAbs_FACE )
2441 ignoreFaceIds.insert( ids[ii] );
2444 else // FACEs with layers are given
2446 exp.Init( solid, TopAbs_FACE );
2447 for ( ; exp.More(); exp.Next() )
2449 TGeomID faceInd = getMeshDS()->ShapeToIndex( exp.Current() );
2450 if ( find( ids.begin(), ids.end(), faceInd ) == ids.end() )
2451 ignoreFaceIds.insert( faceInd );
2455 // ignore internal FACEs if inlets and outlets are specified
2456 if ( hyp->IsToIgnoreShapes() )
2458 TopTools_IndexedDataMapOfShapeListOfShape solidsOfFace;
2459 TopExp::MapShapesAndAncestors( hypShape,
2460 TopAbs_FACE, TopAbs_SOLID, solidsOfFace);
2462 for ( exp.Init( solid, TopAbs_FACE ); exp.More(); exp.Next() )
2464 const TopoDS_Face& face = TopoDS::Face( exp.Current() );
2465 if ( SMESH_MesherHelper::NbAncestors( face, *_mesh, TopAbs_SOLID ) < 2 )
2468 int nbSolids = solidsOfFace.FindFromKey( face ).Extent();
2470 ignoreFaceIds.insert( getMeshDS()->ShapeToIndex( face ));
2475 //================================================================================
2477 * \brief Create the inner surface of the viscous layer and prepare data for infation
2479 //================================================================================
2481 bool _ViscousBuilder::makeLayer(_SolidData& data)
2483 // get all sub-shapes to make layers on
2484 set<TGeomID> subIds, faceIds;
2485 subIds = data._noShrinkShapes;
2486 TopExp_Explorer exp( data._solid, TopAbs_FACE );
2487 for ( ; exp.More(); exp.Next() )
2489 SMESH_subMesh* fSubM = _mesh->GetSubMesh( exp.Current() );
2490 if ( ! data._ignoreFaceIds.count( fSubM->GetId() ))
2491 faceIds.insert( fSubM->GetId() );
2494 // make a map to find new nodes on sub-shapes shared with other SOLID
2495 map< TGeomID, TNode2Edge* >::iterator s2ne;
2496 map< TGeomID, TopoDS_Shape >::iterator s2s = data._shrinkShape2Shape.begin();
2497 for (; s2s != data._shrinkShape2Shape.end(); ++s2s )
2499 TGeomID shapeInd = s2s->first;
2500 for ( size_t i = 0; i < _sdVec.size(); ++i )
2502 if ( _sdVec[i]._index == data._index ) continue;
2503 map< TGeomID, TopoDS_Shape >::iterator s2s2 = _sdVec[i]._shrinkShape2Shape.find( shapeInd );
2504 if ( s2s2 != _sdVec[i]._shrinkShape2Shape.end() &&
2505 *s2s == *s2s2 && !_sdVec[i]._n2eMap.empty() )
2507 data._s2neMap.insert( make_pair( shapeInd, &_sdVec[i]._n2eMap ));
2513 // Create temporary faces and _LayerEdge's
2515 dumpFunction(SMESH_Comment("makeLayers_")<<data._index);
2517 data._stepSize = Precision::Infinite();
2518 data._stepSizeNodes[0] = 0;
2520 SMESH_MesherHelper helper( *_mesh );
2521 helper.SetSubShape( data._solid );
2522 helper.SetElementsOnShape( true );
2524 vector< const SMDS_MeshNode*> newNodes; // of a mesh face
2525 TNode2Edge::iterator n2e2;
2527 // collect _LayerEdge's of shapes they are based on
2528 vector< _EdgesOnShape >& edgesByGeom = data._edgesOnShape;
2529 const int nbShapes = getMeshDS()->MaxShapeIndex();
2530 edgesByGeom.resize( nbShapes+1 );
2532 // set data of _EdgesOnShape's
2533 if ( SMESH_subMesh* sm = _mesh->GetSubMesh( data._solid ))
2535 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/false);
2536 while ( smIt->more() )
2539 if ( sm->GetSubShape().ShapeType() == TopAbs_FACE &&
2540 !faceIds.count( sm->GetId() ))
2542 setShapeData( edgesByGeom[ sm->GetId() ], sm, data );
2545 // make _LayerEdge's
2546 for ( set<TGeomID>::iterator id = faceIds.begin(); id != faceIds.end(); ++id )
2548 const TopoDS_Face& F = TopoDS::Face( getMeshDS()->IndexToShape( *id ));
2549 SMESH_subMesh* sm = _mesh->GetSubMesh( F );
2550 SMESH_ProxyMesh::SubMesh* proxySub =
2551 data._proxyMesh->getFaceSubM( F, /*create=*/true);
2553 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
2554 if ( !smDS ) return error(SMESH_Comment("Not meshed face ") << *id, data._index );
2556 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
2557 while ( eIt->more() )
2559 const SMDS_MeshElement* face = eIt->next();
2560 double faceMaxCosin = -1;
2561 _LayerEdge* maxCosinEdge = 0;
2562 int nbDegenNodes = 0;
2564 newNodes.resize( face->NbCornerNodes() );
2565 for ( size_t i = 0 ; i < newNodes.size(); ++i )
2567 const SMDS_MeshNode* n = face->GetNode( i );
2568 const int shapeID = n->getshapeId();
2569 const bool onDegenShap = helper.IsDegenShape( shapeID );
2570 const bool onDegenEdge = ( onDegenShap && n->GetPosition()->GetDim() == 1 );
2575 // substitute n on a degenerated EDGE with a node on a corresponding VERTEX
2576 const TopoDS_Shape& E = getMeshDS()->IndexToShape( shapeID );
2577 TopoDS_Vertex V = helper.IthVertex( 0, TopoDS::Edge( E ));
2578 if ( const SMDS_MeshNode* vN = SMESH_Algo::VertexNode( V, getMeshDS() )) {
2588 TNode2Edge::iterator n2e = data._n2eMap.insert( make_pair( n, (_LayerEdge*)0 )).first;
2589 if ( !(*n2e).second )
2592 _LayerEdge* edge = new _LayerEdge();
2593 edge->_nodes.push_back( n );
2595 edgesByGeom[ shapeID ]._edges.push_back( edge );
2596 const bool noShrink = data._noShrinkShapes.count( shapeID );
2598 SMESH_TNodeXYZ xyz( n );
2600 // set edge data or find already refined _LayerEdge and get data from it
2601 if (( !noShrink ) &&
2602 ( n->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE ) &&
2603 (( s2ne = data._s2neMap.find( shapeID )) != data._s2neMap.end() ) &&
2604 (( n2e2 = (*s2ne).second->find( n )) != s2ne->second->end() ))
2606 _LayerEdge* foundEdge = (*n2e2).second;
2607 gp_XYZ lastPos = edge->Copy( *foundEdge, edgesByGeom[ shapeID ], helper );
2608 foundEdge->_pos.push_back( lastPos );
2609 // location of the last node is modified and we restore it by foundEdge->_pos.back()
2610 const_cast< SMDS_MeshNode* >
2611 ( edge->_nodes.back() )->setXYZ( xyz.X(), xyz.Y(), xyz.Z() );
2617 edge->_nodes.push_back( helper.AddNode( xyz.X(), xyz.Y(), xyz.Z() ));
2619 if ( !setEdgeData( *edge, edgesByGeom[ shapeID ], helper, data ))
2622 if ( edge->_nodes.size() < 2 )
2623 edge->Block( data );
2624 //data._noShrinkShapes.insert( shapeID );
2626 dumpMove(edge->_nodes.back());
2628 if ( edge->_cosin > faceMaxCosin )
2630 faceMaxCosin = edge->_cosin;
2631 maxCosinEdge = edge;
2634 newNodes[ i ] = n2e->second->_nodes.back();
2637 data._n2eMap.insert( make_pair( face->GetNode( i ), n2e->second ));
2639 if ( newNodes.size() - nbDegenNodes < 2 )
2642 // create a temporary face
2643 const SMDS_MeshElement* newFace =
2644 new _TmpMeshFace( newNodes, --_tmpFaceID, face->getshapeId(), face->getIdInShape() );
2645 proxySub->AddElement( newFace );
2647 // compute inflation step size by min size of element on a convex surface
2648 if ( faceMaxCosin > theMinSmoothCosin )
2649 limitStepSize( data, face, maxCosinEdge );
2651 } // loop on 2D elements on a FACE
2652 } // loop on FACEs of a SOLID to create _LayerEdge's
2655 // Set _LayerEdge::_neibors
2656 TNode2Edge::iterator n2e;
2657 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
2659 _EdgesOnShape& eos = data._edgesOnShape[iS];
2660 for ( size_t i = 0; i < eos._edges.size(); ++i )
2662 _LayerEdge* edge = eos._edges[i];
2663 TIDSortedNodeSet nearNodes;
2664 SMDS_ElemIteratorPtr fIt = edge->_nodes[0]->GetInverseElementIterator(SMDSAbs_Face);
2665 while ( fIt->more() )
2667 const SMDS_MeshElement* f = fIt->next();
2668 if ( !data._ignoreFaceIds.count( f->getshapeId() ))
2669 nearNodes.insert( f->begin_nodes(), f->end_nodes() );
2671 nearNodes.erase( edge->_nodes[0] );
2672 edge->_neibors.reserve( nearNodes.size() );
2673 TIDSortedNodeSet::iterator node = nearNodes.begin();
2674 for ( ; node != nearNodes.end(); ++node )
2675 if (( n2e = data._n2eMap.find( *node )) != data._n2eMap.end() )
2676 edge->_neibors.push_back( n2e->second );
2680 data._epsilon = 1e-7;
2681 if ( data._stepSize < 1. )
2682 data._epsilon *= data._stepSize;
2684 if ( !findShapesToSmooth( data )) // _LayerEdge::_maxLen is computed here
2687 // limit data._stepSize depending on surface curvature and fill data._convexFaces
2688 limitStepSizeByCurvature( data ); // !!! it must be before node substitution in _Simplex
2690 // Set target nodes into _Simplex and _LayerEdge's to _2NearEdges
2691 const SMDS_MeshNode* nn[2];
2692 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
2694 _EdgesOnShape& eos = data._edgesOnShape[iS];
2695 for ( size_t i = 0; i < eos._edges.size(); ++i )
2697 _LayerEdge* edge = eos._edges[i];
2698 if ( edge->IsOnEdge() )
2700 // get neighbor nodes
2701 bool hasData = ( edge->_2neibors->_edges[0] );
2702 if ( hasData ) // _LayerEdge is a copy of another one
2704 nn[0] = edge->_2neibors->srcNode(0);
2705 nn[1] = edge->_2neibors->srcNode(1);
2707 else if ( !findNeiborsOnEdge( edge, nn[0],nn[1], eos, data ))
2711 // set neighbor _LayerEdge's
2712 for ( int j = 0; j < 2; ++j )
2714 if (( n2e = data._n2eMap.find( nn[j] )) == data._n2eMap.end() )
2715 return error("_LayerEdge not found by src node", data._index);
2716 edge->_2neibors->_edges[j] = n2e->second;
2719 edge->SetDataByNeighbors( nn[0], nn[1], eos, helper );
2722 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
2724 _Simplex& s = edge->_simplices[j];
2725 s._nNext = data._n2eMap[ s._nNext ]->_nodes.back();
2726 s._nPrev = data._n2eMap[ s._nPrev ]->_nodes.back();
2729 // For an _LayerEdge on a degenerated EDGE, copy some data from
2730 // a corresponding _LayerEdge on a VERTEX
2731 // (issue 52453, pb on a downloaded SampleCase2-Tet-netgen-mephisto.hdf)
2732 if ( helper.IsDegenShape( edge->_nodes[0]->getshapeId() ))
2734 // Generally we should not get here
2735 if ( eos.ShapeType() != TopAbs_EDGE )
2737 TopoDS_Vertex V = helper.IthVertex( 0, TopoDS::Edge( eos._shape ));
2738 const SMDS_MeshNode* vN = SMESH_Algo::VertexNode( V, getMeshDS() );
2739 if (( n2e = data._n2eMap.find( vN )) == data._n2eMap.end() )
2741 const _LayerEdge* vEdge = n2e->second;
2742 edge->_normal = vEdge->_normal;
2743 edge->_lenFactor = vEdge->_lenFactor;
2744 edge->_cosin = vEdge->_cosin;
2747 } // loop on data._edgesOnShape._edges
2748 } // loop on data._edgesOnShape
2750 // fix _LayerEdge::_2neibors on EDGEs to smooth
2751 // map< TGeomID,Handle(Geom_Curve)>::iterator e2c = data._edge2curve.begin();
2752 // for ( ; e2c != data._edge2curve.end(); ++e2c )
2753 // if ( !e2c->second.IsNull() )
2755 // if ( _EdgesOnShape* eos = data.GetShapeEdges( e2c->first ))
2756 // data.Sort2NeiborsOnEdge( eos->_edges );
2763 //================================================================================
2765 * \brief Compute inflation step size by min size of element on a convex surface
2767 //================================================================================
2769 void _ViscousBuilder::limitStepSize( _SolidData& data,
2770 const SMDS_MeshElement* face,
2771 const _LayerEdge* maxCosinEdge )
2774 double minSize = 10 * data._stepSize;
2775 const int nbNodes = face->NbCornerNodes();
2776 for ( int i = 0; i < nbNodes; ++i )
2778 const SMDS_MeshNode* nextN = face->GetNode( SMESH_MesherHelper::WrapIndex( i+1, nbNodes ));
2779 const SMDS_MeshNode* curN = face->GetNode( i );
2780 if ( nextN->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ||
2781 curN-> GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE )
2783 double dist = SMESH_TNodeXYZ( curN ).Distance( nextN );
2784 if ( dist < minSize )
2785 minSize = dist, iN = i;
2788 double newStep = 0.8 * minSize / maxCosinEdge->_lenFactor;
2789 if ( newStep < data._stepSize )
2791 data._stepSize = newStep;
2792 data._stepSizeCoeff = 0.8 / maxCosinEdge->_lenFactor;
2793 data._stepSizeNodes[0] = face->GetNode( iN );
2794 data._stepSizeNodes[1] = face->GetNode( SMESH_MesherHelper::WrapIndex( iN+1, nbNodes ));
2798 //================================================================================
2800 * \brief Compute inflation step size by min size of element on a convex surface
2802 //================================================================================
2804 void _ViscousBuilder::limitStepSize( _SolidData& data, const double minSize )
2806 if ( minSize < data._stepSize )
2808 data._stepSize = minSize;
2809 if ( data._stepSizeNodes[0] )
2812 SMESH_TNodeXYZ(data._stepSizeNodes[0]).Distance(data._stepSizeNodes[1]);
2813 data._stepSizeCoeff = data._stepSize / dist;
2818 //================================================================================
2820 * \brief Limit data._stepSize by evaluating curvature of shapes and fill data._convexFaces
2822 //================================================================================
2824 void _ViscousBuilder::limitStepSizeByCurvature( _SolidData& data )
2826 SMESH_MesherHelper helper( *_mesh );
2828 BRepLProp_SLProps surfProp( 2, 1e-6 );
2829 data._convexFaces.clear();
2831 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
2833 _EdgesOnShape& eof = data._edgesOnShape[iS];
2834 if ( eof.ShapeType() != TopAbs_FACE ||
2835 data._ignoreFaceIds.count( eof._shapeID ))
2838 TopoDS_Face F = TopoDS::Face( eof._shape );
2839 const TGeomID faceID = eof._shapeID;
2841 BRepAdaptor_Surface surface( F, false );
2842 surfProp.SetSurface( surface );
2844 _ConvexFace cnvFace;
2846 cnvFace._normalsFixed = false;
2847 cnvFace._isTooCurved = false;
2849 double maxCurvature = cnvFace.GetMaxCurvature( data, eof, surfProp, helper );
2850 if ( maxCurvature > 0 )
2852 limitStepSize( data, 0.9 / maxCurvature );
2853 findEdgesToUpdateNormalNearConvexFace( cnvFace, data, helper );
2855 if ( !cnvFace._isTooCurved ) continue;
2857 _ConvexFace & convFace =
2858 data._convexFaces.insert( make_pair( faceID, cnvFace )).first->second;
2860 // skip a closed surface (data._convexFaces is useful anyway)
2861 bool isClosedF = false;
2862 helper.SetSubShape( F );
2863 if ( helper.HasRealSeam() )
2865 // in the closed surface there must be a closed EDGE
2866 for ( TopExp_Explorer eIt( F, TopAbs_EDGE ); eIt.More() && !isClosedF; eIt.Next() )
2867 isClosedF = helper.IsClosedEdge( TopoDS::Edge( eIt.Current() ));
2871 // limit _LayerEdge::_maxLen on the FACE
2872 const double oriFactor = ( F.Orientation() == TopAbs_REVERSED ? +1. : -1. );
2873 const double minCurvature =
2874 1. / ( eof._hyp.GetTotalThickness() * ( 1 + theThickToIntersection ));
2875 map< TGeomID, _EdgesOnShape* >::iterator id2eos = cnvFace._subIdToEOS.find( faceID );
2876 if ( id2eos != cnvFace._subIdToEOS.end() )
2878 _EdgesOnShape& eos = * id2eos->second;
2879 for ( size_t i = 0; i < eos._edges.size(); ++i )
2881 _LayerEdge* ledge = eos._edges[ i ];
2882 gp_XY uv = helper.GetNodeUV( F, ledge->_nodes[0] );
2883 surfProp.SetParameters( uv.X(), uv.Y() );
2884 if ( surfProp.IsCurvatureDefined() )
2886 double curvature = Max( surfProp.MaxCurvature() * oriFactor,
2887 surfProp.MinCurvature() * oriFactor );
2888 if ( curvature > minCurvature )
2889 ledge->SetMaxLen( Min( ledge->_maxLen, 1. / curvature ));
2896 // Fill _ConvexFace::_simplexTestEdges. These _LayerEdge's are used to detect
2897 // prism distortion.
2898 map< TGeomID, _EdgesOnShape* >::iterator id2eos = convFace._subIdToEOS.find( faceID );
2899 if ( id2eos != convFace._subIdToEOS.end() && !id2eos->second->_edges.empty() )
2901 // there are _LayerEdge's on the FACE it-self;
2902 // select _LayerEdge's near EDGEs
2903 _EdgesOnShape& eos = * id2eos->second;
2904 for ( size_t i = 0; i < eos._edges.size(); ++i )
2906 _LayerEdge* ledge = eos._edges[ i ];
2907 for ( size_t j = 0; j < ledge->_simplices.size(); ++j )
2908 if ( ledge->_simplices[j]._nNext->GetPosition()->GetDim() < 2 )
2910 // do not select _LayerEdge's neighboring sharp EDGEs
2911 bool sharpNbr = false;
2912 for ( size_t iN = 0; iN < ledge->_neibors.size() && !sharpNbr; ++iN )
2913 sharpNbr = ( ledge->_neibors[iN]->_cosin > theMinSmoothCosin );
2915 convFace._simplexTestEdges.push_back( ledge );
2922 // where there are no _LayerEdge's on a _ConvexFace,
2923 // as e.g. on a fillet surface with no internal nodes - issue 22580,
2924 // so that collision of viscous internal faces is not detected by check of
2925 // intersection of _LayerEdge's with the viscous internal faces.
2927 set< const SMDS_MeshNode* > usedNodes;
2929 // look for _LayerEdge's with null _sWOL
2930 id2eos = convFace._subIdToEOS.begin();
2931 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
2933 _EdgesOnShape& eos = * id2eos->second;
2934 if ( !eos._sWOL.IsNull() )
2936 for ( size_t i = 0; i < eos._edges.size(); ++i )
2938 _LayerEdge* ledge = eos._edges[ i ];
2939 const SMDS_MeshNode* srcNode = ledge->_nodes[0];
2940 if ( !usedNodes.insert( srcNode ).second ) continue;
2942 for ( size_t i = 0; i < ledge->_simplices.size(); ++i )
2944 usedNodes.insert( ledge->_simplices[i]._nPrev );
2945 usedNodes.insert( ledge->_simplices[i]._nNext );
2947 convFace._simplexTestEdges.push_back( ledge );
2951 } // loop on FACEs of data._solid
2954 //================================================================================
2956 * \brief Detect shapes (and _LayerEdge's on them) to smooth
2958 //================================================================================
2960 bool _ViscousBuilder::findShapesToSmooth( _SolidData& data )
2962 // define allowed thickness
2963 computeGeomSize( data ); // compute data._geomSize and _LayerEdge::_maxLen
2966 // Find shapes needing smoothing; such a shape has _LayerEdge._normal on it's
2967 // boundary inclined to the shape at a sharp angle
2969 TopTools_MapOfShape edgesOfSmooFaces;
2970 SMESH_MesherHelper helper( *_mesh );
2973 vector< _EdgesOnShape >& edgesByGeom = data._edgesOnShape;
2974 data._nbShapesToSmooth = 0;
2976 for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS ) // check FACEs
2978 _EdgesOnShape& eos = edgesByGeom[iS];
2979 eos._toSmooth = false;
2980 if ( eos._edges.empty() || eos.ShapeType() != TopAbs_FACE )
2983 double tgtThick = eos._hyp.GetTotalThickness();
2984 SMESH_subMeshIteratorPtr subIt = eos._subMesh->getDependsOnIterator(/*includeSelf=*/false );
2985 while ( subIt->more() && !eos._toSmooth )
2987 TGeomID iSub = subIt->next()->GetId();
2988 const vector<_LayerEdge*>& eSub = edgesByGeom[ iSub ]._edges;
2989 if ( eSub.empty() ) continue;
2992 for ( size_t i = 0; i < eSub.size() && !eos._toSmooth; ++i )
2993 if ( eSub[i]->_cosin > theMinSmoothCosin )
2995 SMDS_ElemIteratorPtr fIt = eSub[i]->_nodes[0]->GetInverseElementIterator(SMDSAbs_Face);
2996 while ( fIt->more() && !eos._toSmooth )
2998 const SMDS_MeshElement* face = fIt->next();
2999 if ( face->getshapeId() == eos._shapeID &&
3000 getDistFromEdge( face, eSub[i]->_nodes[0], faceSize ))
3002 eos._toSmooth = needSmoothing( eSub[i]->_cosin,
3003 tgtThick * eSub[i]->_lenFactor,
3009 if ( eos._toSmooth )
3011 for ( TopExp_Explorer eExp( edgesByGeom[iS]._shape, TopAbs_EDGE ); eExp.More(); eExp.Next() )
3012 edgesOfSmooFaces.Add( eExp.Current() );
3014 data.PrepareEdgesToSmoothOnFace( &edgesByGeom[iS], /*substituteSrcNodes=*/false );
3016 data._nbShapesToSmooth += eos._toSmooth;
3020 for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS ) // check EDGEs
3022 _EdgesOnShape& eos = edgesByGeom[iS];
3023 eos._edgeSmoother = NULL;
3024 if ( eos._edges.empty() || eos.ShapeType() != TopAbs_EDGE ) continue;
3025 if ( !eos._hyp.ToSmooth() ) continue;
3027 const TopoDS_Edge& E = TopoDS::Edge( edgesByGeom[iS]._shape );
3028 if ( SMESH_Algo::isDegenerated( E ) || !edgesOfSmooFaces.Contains( E ))
3031 double tgtThick = eos._hyp.GetTotalThickness();
3032 for ( TopoDS_Iterator vIt( E ); vIt.More() && !eos._toSmooth; vIt.Next() )
3034 TGeomID iV = getMeshDS()->ShapeToIndex( vIt.Value() );
3035 vector<_LayerEdge*>& eV = edgesByGeom[ iV ]._edges;
3036 if ( eV.empty() || eV[0]->Is( _LayerEdge::MULTI_NORMAL )) continue;
3037 gp_Vec eDir = getEdgeDir( E, TopoDS::Vertex( vIt.Value() ));
3038 double angle = eDir.Angle( eV[0]->_normal );
3039 double cosin = Cos( angle );
3040 double cosinAbs = Abs( cosin );
3041 if ( cosinAbs > theMinSmoothCosin )
3043 // always smooth analytic EDGEs
3044 Handle(Geom_Curve) curve = _Smoother1D::CurveForSmooth( E, eos, helper );
3045 eos._toSmooth = ! curve.IsNull();
3047 // compare tgtThick with the length of an end segment
3048 SMDS_ElemIteratorPtr eIt = eV[0]->_nodes[0]->GetInverseElementIterator(SMDSAbs_Edge);
3049 while ( eIt->more() && !eos._toSmooth )
3051 const SMDS_MeshElement* endSeg = eIt->next();
3052 if ( endSeg->getshapeId() == (int) iS )
3055 SMESH_TNodeXYZ( endSeg->GetNode( 0 )).Distance( endSeg->GetNode( 1 ));
3056 eos._toSmooth = needSmoothing( cosinAbs, tgtThick * eV[0]->_lenFactor, segLen );
3059 if ( eos._toSmooth )
3061 eos._edgeSmoother = new _Smoother1D( curve, eos );
3063 // for ( size_t i = 0; i < eos._edges.size(); ++i )
3064 // eos._edges[i]->Set( _LayerEdge::TO_SMOOTH );
3068 data._nbShapesToSmooth += eos._toSmooth;
3072 // Reset _cosin if no smooth is allowed by the user
3073 for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS )
3075 _EdgesOnShape& eos = edgesByGeom[iS];
3076 if ( eos._edges.empty() ) continue;
3078 if ( !eos._hyp.ToSmooth() )
3079 for ( size_t i = 0; i < eos._edges.size(); ++i )
3080 //eos._edges[i]->SetCosin( 0 ); // keep _cosin to use in limitMaxLenByCurvature()
3081 eos._edges[i]->_lenFactor = 1;
3085 // Fill _eosC1 to make that C1 FACEs and EGDEs between them to be smoothed as a whole
3087 TopTools_MapOfShape c1VV;
3089 for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS ) // check FACEs
3091 _EdgesOnShape& eos = edgesByGeom[iS];
3092 if ( eos._edges.empty() ||
3093 eos.ShapeType() != TopAbs_FACE ||
3097 // check EDGEs of a FACE
3098 TopTools_MapOfShape checkedEE, allVV;
3099 list< SMESH_subMesh* > smQueue( 1, eos._subMesh ); // sm of FACEs
3100 while ( !smQueue.empty() )
3102 SMESH_subMesh* sm = smQueue.front();
3103 smQueue.pop_front();
3104 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/false);
3105 while ( smIt->more() )
3108 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
3109 allVV.Add( sm->GetSubShape() );
3110 if ( sm->GetSubShape().ShapeType() != TopAbs_EDGE ||
3111 !checkedEE.Add( sm->GetSubShape() ))
3114 _EdgesOnShape* eoe = data.GetShapeEdges( sm->GetId() );
3115 vector<_LayerEdge*>& eE = eoe->_edges;
3116 if ( eE.empty() || !eoe->_sWOL.IsNull() )
3119 bool isC1 = true; // check continuity along an EDGE
3120 for ( size_t i = 0; i < eE.size() && isC1; ++i )
3121 isC1 = ( Abs( eE[i]->_cosin ) < theMinSmoothCosin );
3125 // check that mesh faces are C1 as well
3127 gp_XYZ norm1, norm2;
3128 const SMDS_MeshNode* n = eE[ eE.size() / 2 ]->_nodes[0];
3129 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator(SMDSAbs_Face);
3130 if ( !SMESH_MeshAlgos::FaceNormal( fIt->next(), norm1, /*normalized=*/true ))
3132 while ( fIt->more() && isC1 )
3133 isC1 = ( SMESH_MeshAlgos::FaceNormal( fIt->next(), norm2, /*normalized=*/true ) &&
3134 Abs( norm1 * norm2 ) >= ( 1. - theMinSmoothCosin ));
3139 // add the EDGE and an adjacent FACE to _eosC1
3140 PShapeIteratorPtr fIt = helper.GetAncestors( sm->GetSubShape(), *_mesh, TopAbs_FACE );
3141 while ( const TopoDS_Shape* face = fIt->next() )
3143 _EdgesOnShape* eof = data.GetShapeEdges( *face );
3144 if ( !eof ) continue; // other solid
3145 if ( eos._shapeID == eof->_shapeID ) continue;
3146 if ( !eos.HasC1( eof ))
3149 eos._eosC1.push_back( eof );
3150 eof->_toSmooth = false;
3151 data.PrepareEdgesToSmoothOnFace( eof, /*substituteSrcNodes=*/false );
3152 smQueue.push_back( eof->_subMesh );
3154 if ( !eos.HasC1( eoe ))
3156 eos._eosC1.push_back( eoe );
3157 eoe->_toSmooth = false;
3158 data.PrepareEdgesToSmoothOnFace( eoe, /*substituteSrcNodes=*/false );
3163 if ( eos._eosC1.empty() )
3166 // check VERTEXes of C1 FACEs
3167 TopTools_MapIteratorOfMapOfShape vIt( allVV );
3168 for ( ; vIt.More(); vIt.Next() )
3170 _EdgesOnShape* eov = data.GetShapeEdges( vIt.Key() );
3171 if ( !eov || eov->_edges.empty() || !eov->_sWOL.IsNull() )
3174 bool isC1 = true; // check if all adjacent FACEs are in eos._eosC1
3175 PShapeIteratorPtr fIt = helper.GetAncestors( vIt.Key(), *_mesh, TopAbs_FACE );
3176 while ( const TopoDS_Shape* face = fIt->next() )
3178 _EdgesOnShape* eof = data.GetShapeEdges( *face );
3179 if ( !eof ) continue; // other solid
3180 isC1 = ( face->IsSame( eos._shape ) || eos.HasC1( eof ));
3186 eos._eosC1.push_back( eov );
3187 data.PrepareEdgesToSmoothOnFace( eov, /*substituteSrcNodes=*/false );
3188 c1VV.Add( eov->_shape );
3192 } // fill _eosC1 of FACEs
3197 vector< pair< _EdgesOnShape*, gp_XYZ > > dirOfEdges;
3199 for ( size_t iS = 0; iS < edgesByGeom.size(); ++iS ) // check VERTEXes
3201 _EdgesOnShape& eov = edgesByGeom[iS];
3202 if ( eov._edges.empty() ||
3203 eov.ShapeType() != TopAbs_VERTEX ||
3204 c1VV.Contains( eov._shape ))
3206 const TopoDS_Vertex& V = TopoDS::Vertex( eov._shape );
3208 // get directions of surrounding EDGEs
3210 PShapeIteratorPtr fIt = helper.GetAncestors( eov._shape, *_mesh, TopAbs_EDGE );
3211 while ( const TopoDS_Shape* e = fIt->next() )
3213 _EdgesOnShape* eoe = data.GetShapeEdges( *e );
3214 if ( !eoe ) continue; // other solid
3215 gp_XYZ eDir = getEdgeDir( TopoDS::Edge( *e ), V );
3216 if ( !Precision::IsInfinite( eDir.X() ))
3217 dirOfEdges.push_back( make_pair( eoe, eDir.Normalized() ));
3220 // find EDGEs with C1 directions
3221 for ( size_t i = 0; i < dirOfEdges.size(); ++i )
3222 for ( size_t j = i+1; j < dirOfEdges.size(); ++j )
3223 if ( dirOfEdges[i].first && dirOfEdges[j].first )
3225 double dot = dirOfEdges[i].second * dirOfEdges[j].second;
3226 bool isC1 = ( dot < - ( 1. - theMinSmoothCosin ));
3229 double maxEdgeLen = 3 * Min( eov._edges[0]->_maxLen, eov._hyp.GetTotalThickness() );
3230 double eLen1 = SMESH_Algo::EdgeLength( TopoDS::Edge( dirOfEdges[i].first->_shape ));
3231 double eLen2 = SMESH_Algo::EdgeLength( TopoDS::Edge( dirOfEdges[j].first->_shape ));
3232 if ( eLen1 < maxEdgeLen ) eov._eosC1.push_back( dirOfEdges[i].first );
3233 if ( eLen2 < maxEdgeLen ) eov._eosC1.push_back( dirOfEdges[j].first );
3234 dirOfEdges[i].first = 0;
3235 dirOfEdges[j].first = 0;
3238 } // fill _eosC1 of VERTEXes
3245 //================================================================================
3247 * \brief initialize data of _EdgesOnShape
3249 //================================================================================
3251 void _ViscousBuilder::setShapeData( _EdgesOnShape& eos,
3255 if ( !eos._shape.IsNull() ||
3256 sm->GetSubShape().ShapeType() == TopAbs_WIRE )
3259 SMESH_MesherHelper helper( *_mesh );
3262 eos._shapeID = sm->GetId();
3263 eos._shape = sm->GetSubShape();
3264 if ( eos.ShapeType() == TopAbs_FACE )
3265 eos._shape.Orientation( helper.GetSubShapeOri( data._solid, eos._shape ));
3266 eos._toSmooth = false;
3270 map< TGeomID, TopoDS_Shape >::const_iterator s2s =
3271 data._shrinkShape2Shape.find( eos._shapeID );
3272 if ( s2s != data._shrinkShape2Shape.end() )
3273 eos._sWOL = s2s->second;
3275 eos._isRegularSWOL = true;
3276 if ( eos.SWOLType() == TopAbs_FACE )
3278 const TopoDS_Face& F = TopoDS::Face( eos._sWOL );
3279 Handle(ShapeAnalysis_Surface) surface = helper.GetSurface( F );
3280 eos._isRegularSWOL = ( ! surface->HasSingularities( 1e-7 ));
3284 if ( data._hyps.size() == 1 )
3286 eos._hyp = data._hyps.back();
3290 // compute average StdMeshers_ViscousLayers parameters
3291 map< TGeomID, const StdMeshers_ViscousLayers* >::iterator f2hyp;
3292 if ( eos.ShapeType() == TopAbs_FACE )
3294 if (( f2hyp = data._face2hyp.find( eos._shapeID )) != data._face2hyp.end() )
3295 eos._hyp = f2hyp->second;
3299 PShapeIteratorPtr fIt = helper.GetAncestors( eos._shape, *_mesh, TopAbs_FACE );
3300 while ( const TopoDS_Shape* face = fIt->next() )
3302 TGeomID faceID = getMeshDS()->ShapeToIndex( *face );
3303 if (( f2hyp = data._face2hyp.find( faceID )) != data._face2hyp.end() )
3304 eos._hyp.Add( f2hyp->second );
3310 if ( ! eos._hyp.UseSurfaceNormal() )
3312 if ( eos.ShapeType() == TopAbs_FACE ) // get normals to elements on a FACE
3314 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
3315 if ( !smDS ) return;
3316 eos._faceNormals.resize( smDS->NbElements() );
3318 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
3319 for ( int iF = 0; eIt->more(); ++iF )
3321 const SMDS_MeshElement* face = eIt->next();
3322 if ( !SMESH_MeshAlgos::FaceNormal( face, eos._faceNormals[iF], /*normalized=*/true ))
3323 eos._faceNormals[iF].SetCoord( 0,0,0 );
3326 if ( !helper.IsReversedSubMesh( TopoDS::Face( eos._shape )))
3327 for ( size_t iF = 0; iF < eos._faceNormals.size(); ++iF )
3328 eos._faceNormals[iF].Reverse();
3330 else // find EOS of adjacent FACEs
3332 PShapeIteratorPtr fIt = helper.GetAncestors( eos._shape, *_mesh, TopAbs_FACE );
3333 while ( const TopoDS_Shape* face = fIt->next() )
3335 TGeomID faceID = getMeshDS()->ShapeToIndex( *face );
3336 eos._faceEOS.push_back( & data._edgesOnShape[ faceID ]);
3337 if ( eos._faceEOS.back()->_shape.IsNull() )
3338 // avoid using uninitialised _shapeID in GetNormal()
3339 eos._faceEOS.back()->_shapeID = faceID;
3345 //================================================================================
3347 * \brief Returns normal of a face
3349 //================================================================================
3351 bool _EdgesOnShape::GetNormal( const SMDS_MeshElement* face, gp_Vec& norm )
3354 const _EdgesOnShape* eos = 0;
3356 if ( face->getshapeId() == _shapeID )
3362 for ( size_t iF = 0; iF < _faceEOS.size() && !eos; ++iF )
3363 if ( face->getshapeId() == _faceEOS[ iF ]->_shapeID )
3364 eos = _faceEOS[ iF ];
3368 ( ok = ( face->getIdInShape() < (int) eos->_faceNormals.size() )))
3370 norm = eos->_faceNormals[ face->getIdInShape() ];
3374 debugMsg( "_EdgesOnShape::Normal() failed for face "<<face->GetID()
3375 << " on _shape #" << _shapeID );
3381 //================================================================================
3383 * \brief Set data of _LayerEdge needed for smoothing
3385 //================================================================================
3387 bool _ViscousBuilder::setEdgeData(_LayerEdge& edge,
3389 SMESH_MesherHelper& helper,
3392 const SMDS_MeshNode* node = edge._nodes[0]; // source node
3395 edge._maxLen = Precision::Infinite();
3398 edge._curvature = 0;
3401 // --------------------------
3402 // Compute _normal and _cosin
3403 // --------------------------
3406 edge._lenFactor = 1.;
3407 edge._normal.SetCoord(0,0,0);
3408 _Simplex::GetSimplices( node, edge._simplices, data._ignoreFaceIds, &data );
3410 int totalNbFaces = 0;
3412 std::pair< TopoDS_Face, gp_XYZ > face2Norm[20];
3416 const bool onShrinkShape = !eos._sWOL.IsNull();
3417 const bool useGeometry = (( eos._hyp.UseSurfaceNormal() ) ||
3418 ( eos.ShapeType() != TopAbs_FACE /*&& !onShrinkShape*/ ));
3420 // get geom FACEs the node lies on
3421 //if ( useGeometry )
3423 set<TGeomID> faceIds;
3424 if ( eos.ShapeType() == TopAbs_FACE )
3426 faceIds.insert( eos._shapeID );
3430 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
3431 while ( fIt->more() )
3432 faceIds.insert( fIt->next()->getshapeId() );
3434 set<TGeomID>::iterator id = faceIds.begin();
3435 for ( ; id != faceIds.end(); ++id )
3437 const TopoDS_Shape& s = getMeshDS()->IndexToShape( *id );
3438 if ( s.IsNull() || s.ShapeType() != TopAbs_FACE || data._ignoreFaceIds.count( *id ))
3440 F = TopoDS::Face( s );
3441 face2Norm[ totalNbFaces ].first = F;
3447 bool fromVonF = false;
3450 fromVonF = ( eos.ShapeType() == TopAbs_VERTEX &&
3451 eos.SWOLType() == TopAbs_FACE &&
3454 if ( onShrinkShape && !fromVonF ) // one of faces the node is on has no layers
3456 if ( eos.SWOLType() == TopAbs_EDGE )
3458 // inflate from VERTEX along EDGE
3459 edge._normal = getEdgeDir( TopoDS::Edge( eos._sWOL ), TopoDS::Vertex( eos._shape ));
3461 else if ( eos.ShapeType() == TopAbs_VERTEX )
3463 // inflate from VERTEX along FACE
3464 edge._normal = getFaceDir( TopoDS::Face( eos._sWOL ), TopoDS::Vertex( eos._shape ),
3465 node, helper, normOK, &edge._cosin);
3469 // inflate from EDGE along FACE
3470 edge._normal = getFaceDir( TopoDS::Face( eos._sWOL ), TopoDS::Edge( eos._shape ),
3471 node, helper, normOK);
3474 else // layers are on all FACEs of SOLID the node is on (or fromVonF)
3477 face2Norm[ totalNbFaces++ ].first = TopoDS::Face( eos._sWOL );
3480 for ( int iF = totalNbFaces - 1; iF >= 0; --iF )
3482 F = face2Norm[ iF ].first;
3483 geomNorm = getFaceNormal( node, F, helper, normOK );
3484 if ( !normOK ) continue;
3487 if ( helper.GetSubShapeOri( data._solid, F ) != TopAbs_REVERSED )
3489 face2Norm[ iF ].second = geomNorm.XYZ();
3490 edge._normal += geomNorm.XYZ();
3492 if ( nbOkNorms == 0 )
3493 return error(SMESH_Comment("Can't get normal to node ") << node->GetID(), data._index);
3495 if ( totalNbFaces >= 3 )
3497 edge._normal = getNormalByOffset( &edge, face2Norm, totalNbFaces, fromVonF );
3500 if ( edge._normal.Modulus() < 1e-3 && nbOkNorms > 1 )
3502 // opposite normals, re-get normals at shifted positions (IPAL 52426)
3503 edge._normal.SetCoord( 0,0,0 );
3504 for ( int iF = 0; iF < totalNbFaces - fromVonF; ++iF )
3506 const TopoDS_Face& F = face2Norm[iF].first;
3507 geomNorm = getFaceNormal( node, F, helper, normOK, /*shiftInside=*/true );
3508 if ( helper.GetSubShapeOri( data._solid, F ) != TopAbs_REVERSED )
3511 face2Norm[ iF ].second = geomNorm.XYZ();
3512 edge._normal += face2Norm[ iF ].second;
3517 else // !useGeometry - get _normal using surrounding mesh faces
3519 edge._normal = getWeigthedNormal( &edge );
3521 // set<TGeomID> faceIds;
3523 // SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
3524 // while ( fIt->more() )
3526 // const SMDS_MeshElement* face = fIt->next();
3527 // if ( eos.GetNormal( face, geomNorm ))
3529 // if ( onShrinkShape && !faceIds.insert( face->getshapeId() ).second )
3530 // continue; // use only one mesh face on FACE
3531 // edge._normal += geomNorm.XYZ();
3538 //if ( eos._hyp.UseSurfaceNormal() )
3540 switch ( eos.ShapeType() )
3547 TopoDS_Edge E = TopoDS::Edge( eos._shape );
3548 gp_Vec inFaceDir = getFaceDir( F, E, node, helper, normOK );
3549 double angle = inFaceDir.Angle( edge._normal ); // [0,PI]
3550 edge._cosin = Cos( angle );
3553 case TopAbs_VERTEX: {
3556 getFaceDir( TopoDS::Face( eos._sWOL ), TopoDS::Vertex( eos._shape ),
3557 node, helper, normOK, &edge._cosin );
3559 else if ( eos.SWOLType() != TopAbs_FACE ) // else _cosin is set by getFaceDir()
3561 TopoDS_Vertex V = TopoDS::Vertex( eos._shape );
3562 gp_Vec inFaceDir = getFaceDir( F, V, node, helper, normOK );
3563 double angle = inFaceDir.Angle( edge._normal ); // [0,PI]
3564 edge._cosin = Cos( angle );
3565 if ( totalNbFaces > 2 || helper.IsSeamShape( node->getshapeId() ))
3566 for ( int iF = 1; iF < totalNbFaces; ++iF )
3568 F = face2Norm[ iF ].first;
3569 inFaceDir = getFaceDir( F, V, node, helper, normOK=true );
3571 double angle = inFaceDir.Angle( edge._normal );
3572 double cosin = Cos( angle );
3573 if ( Abs( cosin ) > Abs( edge._cosin ))
3574 edge._cosin = cosin;
3581 return error(SMESH_Comment("Invalid shape position of node ")<<node, data._index);
3585 double normSize = edge._normal.SquareModulus();
3586 if ( normSize < numeric_limits<double>::min() )
3587 return error(SMESH_Comment("Bad normal at node ")<< node->GetID(), data._index );
3589 edge._normal /= sqrt( normSize );
3591 if ( edge.Is( _LayerEdge::MULTI_NORMAL ) && edge._nodes.size() == 2 )
3593 getMeshDS()->RemoveFreeNode( edge._nodes.back(), 0, /*fromGroups=*/false );
3594 edge._nodes.resize( 1 );
3595 edge._normal.SetCoord( 0,0,0 );
3596 edge.SetMaxLen( 0 );
3599 // Set the rest data
3600 // --------------------
3602 edge.SetCosin( edge._cosin ); // to update edge._lenFactor
3604 if ( onShrinkShape )
3606 const SMDS_MeshNode* tgtNode = edge._nodes.back();
3607 if ( SMESHDS_SubMesh* sm = getMeshDS()->MeshElements( data._solid ))
3608 sm->RemoveNode( tgtNode , /*isNodeDeleted=*/false );
3610 // set initial position which is parameters on _sWOL in this case
3611 if ( eos.SWOLType() == TopAbs_EDGE )
3613 double u = helper.GetNodeU( TopoDS::Edge( eos._sWOL ), node, 0, &normOK );
3614 edge._pos.push_back( gp_XYZ( u, 0, 0 ));
3615 if ( edge._nodes.size() > 1 )
3616 getMeshDS()->SetNodeOnEdge( tgtNode, TopoDS::Edge( eos._sWOL ), u );
3618 else // eos.SWOLType() == TopAbs_FACE
3620 gp_XY uv = helper.GetNodeUV( TopoDS::Face( eos._sWOL ), node, 0, &normOK );
3621 edge._pos.push_back( gp_XYZ( uv.X(), uv.Y(), 0));
3622 if ( edge._nodes.size() > 1 )
3623 getMeshDS()->SetNodeOnFace( tgtNode, TopoDS::Face( eos._sWOL ), uv.X(), uv.Y() );
3626 if ( edge._nodes.size() > 1 )
3628 // check if an angle between a FACE with layers and SWOL is sharp,
3629 // else the edge should not inflate
3631 for ( int iF = 0; iF < totalNbFaces && F.IsNull(); ++iF ) // find a FACE with VL
3632 if ( ! helper.IsSubShape( eos._sWOL, face2Norm[iF].first ))
3633 F = face2Norm[iF].first;
3636 geomNorm = getFaceNormal( node, F, helper, normOK );
3637 if ( helper.GetSubShapeOri( data._solid, F ) != TopAbs_REVERSED )
3638 geomNorm.Reverse(); // inside the SOLID
3639 if ( geomNorm * edge._normal < -0.001 )
3641 getMeshDS()->RemoveFreeNode( tgtNode, 0, /*fromGroups=*/false );
3642 edge._nodes.resize( 1 );
3644 else if ( edge._lenFactor > 3 )
3646 edge._lenFactor = 2;
3647 edge.Set( _LayerEdge::RISKY_SWOL );
3654 edge._pos.push_back( SMESH_TNodeXYZ( node ));
3656 if ( eos.ShapeType() == TopAbs_FACE )
3659 for ( size_t i = 0; i < edge._simplices.size(); ++i )
3661 edge._simplices[i].IsMinAngleOK( edge._pos.back(), angle );
3662 edge._minAngle = Max( edge._minAngle, angle ); // "angle" is actually cosine
3667 // Set neighbor nodes for a _LayerEdge based on EDGE
3669 if ( eos.ShapeType() == TopAbs_EDGE /*||
3670 ( onShrinkShape && posType == SMDS_TOP_VERTEX && fabs( edge._cosin ) < 1e-10 )*/)
3672 edge._2neibors = new _2NearEdges;
3673 // target nodes instead of source ones will be set later
3679 //================================================================================
3681 * \brief Return normal to a FACE at a node
3682 * \param [in] n - node
3683 * \param [in] face - FACE
3684 * \param [in] helper - helper
3685 * \param [out] isOK - true or false
3686 * \param [in] shiftInside - to find normal at a position shifted inside the face
3687 * \return gp_XYZ - normal
3689 //================================================================================
3691 gp_XYZ _ViscousBuilder::getFaceNormal(const SMDS_MeshNode* node,
3692 const TopoDS_Face& face,
3693 SMESH_MesherHelper& helper,
3700 // get a shifted position
3701 gp_Pnt p = SMESH_TNodeXYZ( node );
3702 gp_XYZ shift( 0,0,0 );
3703 TopoDS_Shape S = helper.GetSubShapeByNode( node, helper.GetMeshDS() );
3704 switch ( S.ShapeType() ) {
3707 shift = getFaceDir( face, TopoDS::Vertex( S ), node, helper, isOK );
3712 shift = getFaceDir( face, TopoDS::Edge( S ), node, helper, isOK );
3720 p.Translate( shift * 1e-5 );
3722 TopLoc_Location loc;
3723 GeomAPI_ProjectPointOnSurf& projector = helper.GetProjector( face, loc, 1e-7 );
3725 if ( !loc.IsIdentity() ) p.Transform( loc.Transformation().Inverted() );
3727 projector.Perform( p );
3728 if ( !projector.IsDone() || projector.NbPoints() < 1 )
3734 projector.LowerDistanceParameters(U,V);
3739 uv = helper.GetNodeUV( face, node, 0, &isOK );
3745 Handle(Geom_Surface) surface = BRep_Tool::Surface( face );
3747 if ( !shiftInside &&
3748 helper.IsDegenShape( node->getshapeId() ) &&
3749 getFaceNormalAtSingularity( uv, face, helper, normal ))
3752 return normal.XYZ();
3755 int pointKind = GeomLib::NormEstim( surface, uv, 1e-5, normal );
3756 enum { REGULAR = 0, QUASYSINGULAR, CONICAL, IMPOSSIBLE };
3758 if ( pointKind == IMPOSSIBLE &&
3759 node->GetPosition()->GetDim() == 2 ) // node inside the FACE
3761 // probably NormEstim() failed due to a too high tolerance
3762 pointKind = GeomLib::NormEstim( surface, uv, 1e-20, normal );
3763 isOK = ( pointKind < IMPOSSIBLE );
3765 if ( pointKind < IMPOSSIBLE )
3767 if ( pointKind != REGULAR &&
3769 node->GetPosition()->GetDim() < 2 ) // FACE boundary
3771 gp_XYZ normShift = getFaceNormal( node, face, helper, isOK, /*shiftInside=*/true );
3772 if ( normShift * normal.XYZ() < 0. )
3778 if ( !isOK ) // hard singularity, to call with shiftInside=true ?
3780 const TGeomID faceID = helper.GetMeshDS()->ShapeToIndex( face );
3782 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
3783 while ( fIt->more() )
3785 const SMDS_MeshElement* f = fIt->next();
3786 if ( f->getshapeId() == faceID )
3788 isOK = SMESH_MeshAlgos::FaceNormal( f, (gp_XYZ&) normal.XYZ(), /*normalized=*/true );
3791 TopoDS_Face ff = face;
3792 ff.Orientation( TopAbs_FORWARD );
3793 if ( helper.IsReversedSubMesh( ff ))
3800 return normal.XYZ();
3803 //================================================================================
3805 * \brief Try to get normal at a singularity of a surface basing on it's nature
3807 //================================================================================
3809 bool _ViscousBuilder::getFaceNormalAtSingularity( const gp_XY& uv,
3810 const TopoDS_Face& face,
3811 SMESH_MesherHelper& helper,
3814 BRepAdaptor_Surface surface( face );
3816 if ( !getRovolutionAxis( surface, axis ))
3819 double f,l, d, du, dv;
3820 f = surface.FirstUParameter();
3821 l = surface.LastUParameter();
3822 d = ( uv.X() - f ) / ( l - f );
3823 du = ( d < 0.5 ? +1. : -1 ) * 1e-5 * ( l - f );
3824 f = surface.FirstVParameter();
3825 l = surface.LastVParameter();
3826 d = ( uv.Y() - f ) / ( l - f );
3827 dv = ( d < 0.5 ? +1. : -1 ) * 1e-5 * ( l - f );
3830 gp_Pnt2d testUV = uv;
3831 enum { REGULAR = 0, QUASYSINGULAR, CONICAL, IMPOSSIBLE };
3833 Handle(Geom_Surface) geomsurf = surface.Surface().Surface();
3834 for ( int iLoop = 0; true ; ++iLoop )
3836 testUV.SetCoord( testUV.X() + du, testUV.Y() + dv );
3837 if ( GeomLib::NormEstim( geomsurf, testUV, tol, refDir ) == REGULAR )
3844 if ( axis * refDir < 0. )
3852 //================================================================================
3854 * \brief Return a normal at a node weighted with angles taken by faces
3856 //================================================================================
3858 gp_XYZ _ViscousBuilder::getWeigthedNormal( const _LayerEdge* edge )
3860 const SMDS_MeshNode* n = edge->_nodes[0];
3862 gp_XYZ resNorm(0,0,0);
3863 SMESH_TNodeXYZ p0( n ), pP, pN;
3864 for ( size_t i = 0; i < edge->_simplices.size(); ++i )
3866 pP.Set( edge->_simplices[i]._nPrev );
3867 pN.Set( edge->_simplices[i]._nNext );
3868 gp_Vec v0P( p0, pP ), v0N( p0, pN ), vPN( pP, pN ), norm = v0P ^ v0N;
3869 double l0P = v0P.SquareMagnitude();
3870 double l0N = v0N.SquareMagnitude();
3871 double lPN = vPN.SquareMagnitude();
3872 if ( l0P < std::numeric_limits<double>::min() ||
3873 l0N < std::numeric_limits<double>::min() ||
3874 lPN < std::numeric_limits<double>::min() )
3876 double lNorm = norm.SquareMagnitude();
3877 double sin2 = lNorm / l0P / l0N;
3878 double angle = ACos(( v0P * v0N ) / Sqrt( l0P ) / Sqrt( l0N ));
3880 double weight = sin2 * angle / lPN;
3881 resNorm += weight * norm.XYZ() / Sqrt( lNorm );
3887 //================================================================================
3889 * \brief Return a normal at a node by getting a common point of offset planes
3890 * defined by the FACE normals
3892 //================================================================================
3894 gp_XYZ _ViscousBuilder::getNormalByOffset( _LayerEdge* edge,
3895 std::pair< TopoDS_Face, gp_XYZ > f2Normal[],
3899 SMESH_TNodeXYZ p0 = edge->_nodes[0];
3901 gp_XYZ resNorm(0,0,0);
3902 TopoDS_Shape V = SMESH_MesherHelper::GetSubShapeByNode( p0._node, getMeshDS() );
3903 if ( V.ShapeType() != TopAbs_VERTEX || nbFaces < 3 )
3905 for ( int i = 0; i < nbFaces; ++i )
3906 resNorm += f2Normal[i].second;
3910 // prepare _OffsetPlane's
3911 vector< _OffsetPlane > pln( nbFaces );
3912 for ( int i = 0; i < nbFaces - lastNoOffset; ++i )
3914 pln[i]._faceIndex = i;
3915 pln[i]._plane = gp_Pln( p0 + f2Normal[i].second, f2Normal[i].second );
3919 pln[ nbFaces - 1 ]._faceIndex = nbFaces - 1;
3920 pln[ nbFaces - 1 ]._plane = gp_Pln( p0, f2Normal[ nbFaces - 1 ].second );
3923 // intersect neighboring OffsetPlane's
3924 PShapeIteratorPtr edgeIt = SMESH_MesherHelper::GetAncestors( V, *_mesh, TopAbs_EDGE );
3925 while ( const TopoDS_Shape* edge = edgeIt->next() )
3927 int f1 = -1, f2 = -1;
3928 for ( int i = 0; i < nbFaces && f2 < 0; ++i )
3929 if ( SMESH_MesherHelper::IsSubShape( *edge, f2Normal[i].first ))
3930 (( f1 < 0 ) ? f1 : f2 ) = i;
3933 pln[ f1 ].ComputeIntersectionLine( pln[ f2 ], TopoDS::Edge( *edge ), TopoDS::Vertex( V ));
3936 // get a common point
3937 gp_XYZ commonPnt( 0, 0, 0 );
3940 for ( int i = 0; i < nbFaces; ++i )
3942 commonPnt += pln[ i ].GetCommonPoint( isPointFound, TopoDS::Vertex( V ));
3943 nbPoints += isPointFound;
3945 gp_XYZ wgtNorm = getWeigthedNormal( edge );
3946 if ( nbPoints == 0 )
3949 commonPnt /= nbPoints;
3950 resNorm = commonPnt - p0;
3954 // choose the best among resNorm and wgtNorm
3955 resNorm.Normalize();
3956 wgtNorm.Normalize();
3957 double resMinDot = std::numeric_limits<double>::max();
3958 double wgtMinDot = std::numeric_limits<double>::max();
3959 for ( int i = 0; i < nbFaces - lastNoOffset; ++i )
3961 resMinDot = Min( resMinDot, resNorm * f2Normal[i].second );
3962 wgtMinDot = Min( wgtMinDot, wgtNorm * f2Normal[i].second );
3965 if ( Max( resMinDot, wgtMinDot ) < theMinSmoothCosin )
3967 edge->Set( _LayerEdge::MULTI_NORMAL );
3970 return ( resMinDot > wgtMinDot ) ? resNorm : wgtNorm;
3973 //================================================================================
3975 * \brief Compute line of intersection of 2 planes
3977 //================================================================================
3979 void _OffsetPlane::ComputeIntersectionLine( _OffsetPlane& pln,
3980 const TopoDS_Edge& E,
3981 const TopoDS_Vertex& V )
3983 int iNext = bool( _faceIndexNext[0] >= 0 );
3984 _faceIndexNext[ iNext ] = pln._faceIndex;
3986 gp_XYZ n1 = _plane.Axis().Direction().XYZ();
3987 gp_XYZ n2 = pln._plane.Axis().Direction().XYZ();
3989 gp_XYZ lineDir = n1 ^ n2;
3991 double x = Abs( lineDir.X() );
3992 double y = Abs( lineDir.Y() );
3993 double z = Abs( lineDir.Z() );
3995 int cooMax; // max coordinate
3997 if (x > z) cooMax = 1;
4001 if (y > z) cooMax = 2;
4006 if ( Abs( lineDir.Coord( cooMax )) < 0.05 )
4008 // parallel planes - intersection is an offset of the common EDGE
4009 gp_Pnt p = BRep_Tool::Pnt( V );
4010 linePos = 0.5 * (( p.XYZ() + n1 ) + ( p.XYZ() + n2 ));
4011 lineDir = getEdgeDir( E, V );
4015 // the constants in the 2 plane equations
4016 double d1 = - ( _plane.Axis().Direction().XYZ() * _plane.Location().XYZ() );
4017 double d2 = - ( pln._plane.Axis().Direction().XYZ() * pln._plane.Location().XYZ() );
4022 linePos.SetY(( d2*n1.Z() - d1*n2.Z()) / lineDir.X() );
4023 linePos.SetZ(( d1*n2.Y() - d2*n1.Y()) / lineDir.X() );
4026 linePos.SetX(( d1*n2.Z() - d2*n1.Z()) / lineDir.Y() );
4028 linePos.SetZ(( d2*n1.X() - d1*n2.X()) / lineDir.Y() );
4031 linePos.SetX(( d2*n1.Y() - d1*n2.Y()) / lineDir.Z() );
4032 linePos.SetY(( d1*n2.X() - d2*n1.X()) / lineDir.Z() );
4036 gp_Lin& line = _lines[ iNext ];
4037 line.SetDirection( lineDir );
4038 line.SetLocation ( linePos );
4040 _isLineOK[ iNext ] = true;
4043 iNext = bool( pln._faceIndexNext[0] >= 0 );
4044 pln._lines [ iNext ] = line;
4045 pln._faceIndexNext[ iNext ] = this->_faceIndex;
4046 pln._isLineOK [ iNext ] = true;
4049 //================================================================================
4051 * \brief Computes intersection point of two _lines
4053 //================================================================================
4055 gp_XYZ _OffsetPlane::GetCommonPoint(bool& isFound,
4056 const TopoDS_Vertex & V) const
4061 if ( NbLines() == 2 )
4063 gp_Vec lPerp0 = _lines[0].Direction().XYZ() ^ _plane.Axis().Direction().XYZ();
4064 double dot01 = lPerp0 * _lines[1].Direction().XYZ();
4065 if ( Abs( dot01 ) > 0.05 )
4067 gp_Vec l0l1 = _lines[1].Location().XYZ() - _lines[0].Location().XYZ();
4068 double u1 = - ( lPerp0 * l0l1 ) / dot01;
4069 p = ( _lines[1].Location().XYZ() + _lines[1].Direction().XYZ() * u1 );
4074 gp_Pnt pV ( BRep_Tool::Pnt( V ));
4075 gp_Vec lv0( _lines[0].Location(), pV ), lv1(_lines[1].Location(), pV );
4076 double dot0( lv0 * _lines[0].Direction() ), dot1( lv1 * _lines[1].Direction() );
4077 p += 0.5 * ( _lines[0].Location().XYZ() + _lines[0].Direction().XYZ() * dot0 );
4078 p += 0.5 * ( _lines[1].Location().XYZ() + _lines[1].Direction().XYZ() * dot1 );
4086 //================================================================================
4088 * \brief Find 2 neigbor nodes of a node on EDGE
4090 //================================================================================
4092 bool _ViscousBuilder::findNeiborsOnEdge(const _LayerEdge* edge,
4093 const SMDS_MeshNode*& n1,
4094 const SMDS_MeshNode*& n2,
4098 const SMDS_MeshNode* node = edge->_nodes[0];
4099 const int shapeInd = eos._shapeID;
4100 SMESHDS_SubMesh* edgeSM = 0;
4101 if ( eos.ShapeType() == TopAbs_EDGE )
4103 edgeSM = eos._subMesh->GetSubMeshDS();
4104 if ( !edgeSM || edgeSM->NbElements() == 0 )
4105 return error(SMESH_Comment("Not meshed EDGE ") << shapeInd, data._index);
4109 SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator(SMDSAbs_Edge);
4110 while ( eIt->more() && !n2 )
4112 const SMDS_MeshElement* e = eIt->next();
4113 const SMDS_MeshNode* nNeibor = e->GetNode( 0 );
4114 if ( nNeibor == node ) nNeibor = e->GetNode( 1 );
4117 if (!edgeSM->Contains(e)) continue;
4121 TopoDS_Shape s = SMESH_MesherHelper::GetSubShapeByNode( nNeibor, getMeshDS() );
4122 if ( !SMESH_MesherHelper::IsSubShape( s, eos._sWOL )) continue;
4124 ( iN++ ? n2 : n1 ) = nNeibor;
4127 return error(SMESH_Comment("Wrongly meshed EDGE ") << shapeInd, data._index);
4131 //================================================================================
4133 * \brief Set _curvature and _2neibors->_plnNorm by 2 neigbor nodes residing the same EDGE
4135 //================================================================================
4137 void _LayerEdge::SetDataByNeighbors( const SMDS_MeshNode* n1,
4138 const SMDS_MeshNode* n2,
4139 const _EdgesOnShape& eos,
4140 SMESH_MesherHelper& helper)
4142 if ( eos.ShapeType() != TopAbs_EDGE )
4144 if ( _curvature && Is( SMOOTHED_C1 ))
4147 gp_XYZ pos = SMESH_TNodeXYZ( _nodes[0] );
4148 gp_XYZ vec1 = pos - SMESH_TNodeXYZ( n1 );
4149 gp_XYZ vec2 = pos - SMESH_TNodeXYZ( n2 );
4153 double sumLen = vec1.Modulus() + vec2.Modulus();
4154 _2neibors->_wgt[0] = 1 - vec1.Modulus() / sumLen;
4155 _2neibors->_wgt[1] = 1 - vec2.Modulus() / sumLen;
4156 double avgNormProj = 0.5 * ( _normal * vec1 + _normal * vec2 );
4157 double avgLen = 0.5 * ( vec1.Modulus() + vec2.Modulus() );
4158 if ( _curvature ) delete _curvature;
4159 _curvature = _Curvature::New( avgNormProj, avgLen );
4160 // if ( _curvature )
4161 // debugMsg( _nodes[0]->GetID()
4162 // << " CURV r,k: " << _curvature->_r<<","<<_curvature->_k
4163 // << " proj = "<<avgNormProj<< " len = " << avgLen << "| lenDelta(0) = "
4164 // << _curvature->lenDelta(0) );
4168 if ( eos._sWOL.IsNull() )
4170 TopoDS_Edge E = TopoDS::Edge( eos._shape );
4171 // if ( SMESH_Algo::isDegenerated( E ))
4173 gp_XYZ dirE = getEdgeDir( E, _nodes[0], helper );
4174 gp_XYZ plnNorm = dirE ^ _normal;
4175 double proj0 = plnNorm * vec1;
4176 double proj1 = plnNorm * vec2;
4177 if ( fabs( proj0 ) > 1e-10 || fabs( proj1 ) > 1e-10 )
4179 if ( _2neibors->_plnNorm ) delete _2neibors->_plnNorm;
4180 _2neibors->_plnNorm = new gp_XYZ( plnNorm.Normalized() );
4185 //================================================================================
4187 * \brief Copy data from a _LayerEdge of other SOLID and based on the same node;
4188 * this and the other _LayerEdge are inflated along a FACE or an EDGE
4190 //================================================================================
4192 gp_XYZ _LayerEdge::Copy( _LayerEdge& other,
4194 SMESH_MesherHelper& helper )
4196 _nodes = other._nodes;
4197 _normal = other._normal;
4199 _lenFactor = other._lenFactor;
4200 _cosin = other._cosin;
4201 _2neibors = other._2neibors;
4202 _curvature = 0; std::swap( _curvature, other._curvature );
4203 _2neibors = 0; std::swap( _2neibors, other._2neibors );
4205 gp_XYZ lastPos( 0,0,0 );
4206 if ( eos.SWOLType() == TopAbs_EDGE )
4208 double u = helper.GetNodeU( TopoDS::Edge( eos._sWOL ), _nodes[0] );
4209 _pos.push_back( gp_XYZ( u, 0, 0));
4211 u = helper.GetNodeU( TopoDS::Edge( eos._sWOL ), _nodes.back() );
4216 gp_XY uv = helper.GetNodeUV( TopoDS::Face( eos._sWOL ), _nodes[0]);
4217 _pos.push_back( gp_XYZ( uv.X(), uv.Y(), 0));
4219 uv = helper.GetNodeUV( TopoDS::Face( eos._sWOL ), _nodes.back() );
4220 lastPos.SetX( uv.X() );
4221 lastPos.SetY( uv.Y() );
4226 //================================================================================
4228 * \brief Set _cosin and _lenFactor
4230 //================================================================================
4232 void _LayerEdge::SetCosin( double cosin )
4235 cosin = Abs( _cosin );
4236 //_lenFactor = ( cosin < 1.-1e-12 ) ? Min( 2., 1./sqrt(1-cosin*cosin )) : 1.0;
4237 _lenFactor = ( cosin < 1.-1e-12 ) ? 1./sqrt(1-cosin*cosin ) : 1.0;
4240 //================================================================================
4242 * \brief Check if another _LayerEdge is a neighbor on EDGE
4244 //================================================================================
4246 bool _LayerEdge::IsNeiborOnEdge( const _LayerEdge* edge ) const
4248 return (( this->_2neibors && this->_2neibors->include( edge )) ||
4249 ( edge->_2neibors && edge->_2neibors->include( this )));
4252 //================================================================================
4254 * \brief Fills a vector<_Simplex >
4256 //================================================================================
4258 void _Simplex::GetSimplices( const SMDS_MeshNode* node,
4259 vector<_Simplex>& simplices,
4260 const set<TGeomID>& ingnoreShapes,
4261 const _SolidData* dataToCheckOri,
4265 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
4266 while ( fIt->more() )
4268 const SMDS_MeshElement* f = fIt->next();
4269 const TGeomID shapeInd = f->getshapeId();
4270 if ( ingnoreShapes.count( shapeInd )) continue;
4271 const int nbNodes = f->NbCornerNodes();
4272 const int srcInd = f->GetNodeIndex( node );
4273 const SMDS_MeshNode* nPrev = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd-1, nbNodes ));
4274 const SMDS_MeshNode* nNext = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd+1, nbNodes ));
4275 const SMDS_MeshNode* nOpp = f->GetNode( SMESH_MesherHelper::WrapIndex( srcInd+2, nbNodes ));
4276 if ( dataToCheckOri && dataToCheckOri->_reversedFaceIds.count( shapeInd ))
4277 std::swap( nPrev, nNext );
4278 simplices.push_back( _Simplex( nPrev, nNext, ( nbNodes == 3 ? 0 : nOpp )));
4282 SortSimplices( simplices );
4285 //================================================================================
4287 * \brief Set neighbor simplices side by side
4289 //================================================================================
4291 void _Simplex::SortSimplices(vector<_Simplex>& simplices)
4293 vector<_Simplex> sortedSimplices( simplices.size() );
4294 sortedSimplices[0] = simplices[0];
4296 for ( size_t i = 1; i < simplices.size(); ++i )
4298 for ( size_t j = 1; j < simplices.size(); ++j )
4299 if ( sortedSimplices[i-1]._nNext == simplices[j]._nPrev )
4301 sortedSimplices[i] = simplices[j];
4306 if ( nbFound == simplices.size() - 1 )
4307 simplices.swap( sortedSimplices );
4310 //================================================================================
4312 * \brief DEBUG. Create groups contating temorary data of _LayerEdge's
4314 //================================================================================
4316 void _ViscousBuilder::makeGroupOfLE()
4319 for ( size_t i = 0 ; i < _sdVec.size(); ++i )
4321 if ( _sdVec[i]._n2eMap.empty() ) continue;
4323 dumpFunction( SMESH_Comment("make_LayerEdge_") << i );
4324 TNode2Edge::iterator n2e;
4325 for ( n2e = _sdVec[i]._n2eMap.begin(); n2e != _sdVec[i]._n2eMap.end(); ++n2e )
4327 _LayerEdge* le = n2e->second;
4328 // for ( size_t iN = 1; iN < le->_nodes.size(); ++iN )
4329 // dumpCmd(SMESH_Comment("mesh.AddEdge([ ") <<le->_nodes[iN-1]->GetID()
4330 // << ", " << le->_nodes[iN]->GetID() <<"])");
4332 dumpCmd(SMESH_Comment("mesh.AddEdge([ ") <<le->_nodes[0]->GetID()
4333 << ", " << le->_nodes.back()->GetID() <<"]) # " << le->_flags );
4338 dumpFunction( SMESH_Comment("makeNormals") << i );
4339 for ( n2e = _sdVec[i]._n2eMap.begin(); n2e != _sdVec[i]._n2eMap.end(); ++n2e )
4341 _LayerEdge* edge = n2e->second;
4342 SMESH_TNodeXYZ nXYZ( edge->_nodes[0] );
4343 nXYZ += edge->_normal * _sdVec[i]._stepSize;
4344 dumpCmd(SMESH_Comment("mesh.AddEdge([ ") << edge->_nodes[0]->GetID()
4345 << ", mesh.AddNode( "<< nXYZ.X()<<","<< nXYZ.Y()<<","<< nXYZ.Z()<<")])");
4349 dumpFunction( SMESH_Comment("makeTmpFaces_") << i );
4350 dumpCmd( "faceId1 = mesh.NbElements()" );
4351 TopExp_Explorer fExp( _sdVec[i]._solid, TopAbs_FACE );
4352 for ( ; fExp.More(); fExp.Next() )
4354 if ( const SMESHDS_SubMesh* sm = _sdVec[i]._proxyMesh->GetProxySubMesh( fExp.Current() ))
4356 if ( sm->NbElements() == 0 ) continue;
4357 SMDS_ElemIteratorPtr fIt = sm->GetElements();
4358 while ( fIt->more())
4360 const SMDS_MeshElement* e = fIt->next();
4361 SMESH_Comment cmd("mesh.AddFace([");
4362 for ( int j = 0; j < e->NbCornerNodes(); ++j )
4363 cmd << e->GetNode(j)->GetID() << (j+1 < e->NbCornerNodes() ? ",": "])");
4368 dumpCmd( "faceId2 = mesh.NbElements()" );
4369 dumpCmd( SMESH_Comment( "mesh.MakeGroup( 'tmpFaces_" ) << i << "',"
4370 << "SMESH.FACE, SMESH.FT_RangeOfIds,'=',"
4371 << "'%s-%s' % (faceId1+1, faceId2))");
4377 //================================================================================
4379 * \brief Find maximal _LayerEdge length (layer thickness) limited by geometry
4381 //================================================================================
4383 void _ViscousBuilder::computeGeomSize( _SolidData& data )
4385 data._geomSize = Precision::Infinite();
4386 double intersecDist;
4387 const SMDS_MeshElement* face;
4388 SMESH_MesherHelper helper( *_mesh );
4390 SMESHUtils::Deleter<SMESH_ElementSearcher> searcher
4391 ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(),
4392 data._proxyMesh->GetFaces( data._solid )));
4394 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4396 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
4397 if ( eos._edges.empty() )
4399 // get neighbor faces, intersection with which should not be considered since
4400 // collisions are avoided by means of smoothing
4401 set< TGeomID > neighborFaces;
4402 if ( eos._hyp.ToSmooth() )
4404 SMESH_subMeshIteratorPtr subIt =
4405 eos._subMesh->getDependsOnIterator(/*includeSelf=*/eos.ShapeType() != TopAbs_FACE );
4406 while ( subIt->more() )
4408 SMESH_subMesh* sm = subIt->next();
4409 PShapeIteratorPtr fIt = helper.GetAncestors( sm->GetSubShape(), *_mesh, TopAbs_FACE );
4410 while ( const TopoDS_Shape* face = fIt->next() )
4411 neighborFaces.insert( getMeshDS()->ShapeToIndex( *face ));
4414 // find intersections
4415 double thinkness = eos._hyp.GetTotalThickness();
4416 for ( size_t i = 0; i < eos._edges.size(); ++i )
4418 if ( eos._edges[i]->Is( _LayerEdge::BLOCKED )) continue;
4419 eos._edges[i]->SetMaxLen( thinkness );
4420 eos._edges[i]->FindIntersection( *searcher, intersecDist, data._epsilon, eos, &face );
4421 if ( intersecDist > 0 && face )
4423 data._geomSize = Min( data._geomSize, intersecDist );
4424 if ( !neighborFaces.count( face->getshapeId() ))
4425 eos[i]->SetMaxLen( Min( thinkness, intersecDist / ( face->GetID() < 0 ? 3. : 2. )));
4430 data._maxThickness = 0;
4431 data._minThickness = 1e100;
4432 list< const StdMeshers_ViscousLayers* >::iterator hyp = data._hyps.begin();
4433 for ( ; hyp != data._hyps.end(); ++hyp )
4435 data._maxThickness = Max( data._maxThickness, (*hyp)->GetTotalThickness() );
4436 data._minThickness = Min( data._minThickness, (*hyp)->GetTotalThickness() );
4439 // Limit inflation step size by geometry size found by intersecting
4440 // normals of _LayerEdge's with mesh faces
4441 if ( data._stepSize > 0.3 * data._geomSize )
4442 limitStepSize( data, 0.3 * data._geomSize );
4444 if ( data._stepSize > data._minThickness )
4445 limitStepSize( data, data._minThickness );
4448 // -------------------------------------------------------------------------
4449 // Detect _LayerEdge which can't intersect with opposite or neighbor layer,
4450 // so no need in detecting intersection at each inflation step
4451 // -------------------------------------------------------------------------
4453 int nbSteps = data._maxThickness / data._stepSize;
4454 if ( nbSteps < 3 || nbSteps * data._n2eMap.size() < 100000 )
4457 vector< const SMDS_MeshElement* > closeFaces;
4460 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4462 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
4463 if ( eos._edges.empty() || eos.ShapeType() != TopAbs_FACE )
4466 for ( size_t i = 0; i < eos.size(); ++i )
4468 SMESH_NodeXYZ p( eos[i]->_nodes[0] );
4469 double radius = data._maxThickness + 2 * eos[i]->_maxLen;
4471 searcher->GetElementsInSphere( p, radius, SMDSAbs_Face, closeFaces );
4473 bool toIgnore = true;
4474 for ( size_t iF = 0; iF < closeFaces.size() && toIgnore; ++iF )
4475 if ( !( toIgnore = ( closeFaces[ iF ]->getshapeId() == eos._shapeID ||
4476 data._ignoreFaceIds.count( closeFaces[ iF ]->getshapeId() ))))
4478 // check if a _LayerEdge will inflate in a direction opposite to a direction
4479 // toward a close face
4480 bool allBehind = true;
4481 for ( int iN = 0; iN < closeFaces[ iF ]->NbCornerNodes() && allBehind; ++iN )
4483 SMESH_NodeXYZ pi( closeFaces[ iF ]->GetNode( iN ));
4484 allBehind = (( pi - p ) * eos[i]->_normal < 0.1 * data._stepSize );
4486 toIgnore = allBehind;
4490 if ( toIgnore ) // no need to detect intersection
4492 eos[i]->Set( _LayerEdge::INTERSECTED );
4498 debugMsg( "Nb LE to intersect " << data._n2eMap.size()-nbDetected << ", ignore " << nbDetected );
4503 //================================================================================
4505 * \brief Increase length of _LayerEdge's to reach the required thickness of layers
4507 //================================================================================
4509 bool _ViscousBuilder::inflate(_SolidData& data)
4511 SMESH_MesherHelper helper( *_mesh );
4513 const double tgtThick = data._maxThickness;
4515 if ( data._stepSize < 1. )
4516 data._epsilon = data._stepSize * 1e-7;
4518 debugMsg( "-- geomSize = " << data._geomSize << ", stepSize = " << data._stepSize );
4521 findCollisionEdges( data, helper );
4523 limitMaxLenByCurvature( data, helper );
4527 // limit length of _LayerEdge's around MULTI_NORMAL _LayerEdge's
4528 for ( size_t i = 0; i < data._edgesOnShape.size(); ++i )
4529 if ( data._edgesOnShape[i].ShapeType() == TopAbs_VERTEX &&
4530 data._edgesOnShape[i]._edges.size() > 0 &&
4531 data._edgesOnShape[i]._edges[0]->Is( _LayerEdge::MULTI_NORMAL ))
4533 data._edgesOnShape[i]._edges[0]->Unset( _LayerEdge::BLOCKED );
4534 data._edgesOnShape[i]._edges[0]->Block( data );
4537 const double safeFactor = ( 2*data._maxThickness < data._geomSize ) ? 1 : theThickToIntersection;
4539 double avgThick = 0, curThick = 0, distToIntersection = Precision::Infinite();
4540 int nbSteps = 0, nbRepeats = 0;
4541 while ( avgThick < 0.99 )
4543 // new target length
4544 double prevThick = curThick;
4545 curThick += data._stepSize;
4546 if ( curThick > tgtThick )
4548 curThick = tgtThick + tgtThick*( 1.-avgThick ) * nbRepeats;
4552 double stepSize = curThick - prevThick;
4553 updateNormalsOfSmoothed( data, helper, nbSteps, stepSize ); // to ease smoothing
4555 // Elongate _LayerEdge's
4556 dumpFunction(SMESH_Comment("inflate")<<data._index<<"_step"<<nbSteps); // debug
4557 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4559 _EdgesOnShape& eos = data._edgesOnShape[iS];
4560 if ( eos._edges.empty() ) continue;
4562 const double shapeCurThick = Min( curThick, eos._hyp.GetTotalThickness() );
4563 for ( size_t i = 0; i < eos._edges.size(); ++i )
4565 eos._edges[i]->SetNewLength( shapeCurThick, eos, helper );
4570 if ( !updateNormals( data, helper, nbSteps, stepSize )) // to avoid collisions
4573 // Improve and check quality
4574 if ( !smoothAndCheck( data, nbSteps, distToIntersection ))
4578 #ifdef __NOT_INVALIDATE_BAD_SMOOTH
4579 debugMsg("NOT INVALIDATED STEP!");
4580 return error("Smoothing failed", data._index);
4582 dumpFunction(SMESH_Comment("invalidate")<<data._index<<"_step"<<nbSteps); // debug
4583 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4585 _EdgesOnShape& eos = data._edgesOnShape[iS];
4586 for ( size_t i = 0; i < eos._edges.size(); ++i )
4587 eos._edges[i]->InvalidateStep( nbSteps+1, eos );
4591 break; // no more inflating possible
4595 // Evaluate achieved thickness
4597 int nbActiveEdges = 0;
4598 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4600 _EdgesOnShape& eos = data._edgesOnShape[iS];
4601 if ( eos._edges.empty() ) continue;
4603 const double shapeTgtThick = eos._hyp.GetTotalThickness();
4604 for ( size_t i = 0; i < eos._edges.size(); ++i )
4606 if ( eos._edges[i]->_nodes.size() > 1 )
4607 avgThick += Min( 1., eos._edges[i]->_len / shapeTgtThick );
4609 avgThick += shapeTgtThick;
4610 nbActiveEdges += ( ! eos._edges[i]->Is( _LayerEdge::BLOCKED ));
4613 avgThick /= data._n2eMap.size();
4614 debugMsg( "-- Thickness " << curThick << " ("<< avgThick*100 << "%) reached" );
4616 #ifdef BLOCK_INFLATION
4617 if ( nbActiveEdges == 0 )
4619 debugMsg( "-- Stop inflation since all _LayerEdge's BLOCKED " );
4623 if ( distToIntersection < tgtThick * avgThick * safeFactor && avgThick < 0.9 )
4625 debugMsg( "-- Stop inflation since "
4626 << " distToIntersection( "<<distToIntersection<<" ) < avgThick( "
4627 << tgtThick * avgThick << " ) * " << safeFactor );
4633 limitStepSize( data, 0.25 * distToIntersection );
4634 if ( data._stepSizeNodes[0] )
4635 data._stepSize = data._stepSizeCoeff *
4636 SMESH_TNodeXYZ(data._stepSizeNodes[0]).Distance(data._stepSizeNodes[1]);
4638 } // while ( avgThick < 0.99 )
4641 return error("failed at the very first inflation step", data._index);
4643 if ( avgThick < 0.99 )
4645 if ( !data._proxyMesh->_warning || data._proxyMesh->_warning->IsOK() )
4647 data._proxyMesh->_warning.reset
4648 ( new SMESH_ComputeError (COMPERR_WARNING,
4649 SMESH_Comment("Thickness ") << tgtThick <<
4650 " of viscous layers not reached,"
4651 " average reached thickness is " << avgThick*tgtThick));
4655 // Restore position of src nodes moved by inflation on _noShrinkShapes
4656 dumpFunction(SMESH_Comment("restoNoShrink_So")<<data._index); // debug
4657 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4659 _EdgesOnShape& eos = data._edgesOnShape[iS];
4660 if ( !eos._edges.empty() && eos._edges[0]->_nodes.size() == 1 )
4661 for ( size_t i = 0; i < eos._edges.size(); ++i )
4663 restoreNoShrink( *eos._edges[ i ] );
4668 return safeFactor > 0; // == true (avoid warning: unused variable 'safeFactor')
4671 //================================================================================
4673 * \brief Improve quality of layer inner surface and check intersection
4675 //================================================================================
4677 bool _ViscousBuilder::smoothAndCheck(_SolidData& data,
4679 double & distToIntersection)
4681 if ( data._nbShapesToSmooth == 0 )
4682 return true; // no shapes needing smoothing
4684 bool moved, improved;
4686 vector< _LayerEdge* > movedEdges, badEdges;
4687 vector< _EdgesOnShape* > eosC1; // C1 continues shapes
4688 vector< bool > isConcaveFace;
4690 SMESH_MesherHelper helper(*_mesh);
4691 Handle(ShapeAnalysis_Surface) surface;
4694 for ( int isFace = 0; isFace < 2; ++isFace ) // smooth on [ EDGEs, FACEs ]
4696 const TopAbs_ShapeEnum shapeType = isFace ? TopAbs_FACE : TopAbs_EDGE;
4698 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4700 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
4701 if ( !eos._toSmooth ||
4702 eos.ShapeType() != shapeType ||
4703 eos._edges.empty() )
4706 // already smoothed?
4707 // bool toSmooth = ( eos._edges[ 0 ]->NbSteps() >= infStep+1 );
4708 // if ( !toSmooth ) continue;
4710 if ( !eos._hyp.ToSmooth() )
4712 // smooth disabled by the user; check validy only
4713 if ( !isFace ) continue;
4715 for ( size_t i = 0; i < eos._edges.size(); ++i )
4717 _LayerEdge* edge = eos._edges[i];
4718 for ( size_t iF = 0; iF < edge->_simplices.size(); ++iF )
4719 if ( !edge->_simplices[iF].IsForward( edge->_nodes[0], edge->_pos.back(), vol ))
4721 // debugMsg( "-- Stop inflation. Bad simplex ("
4722 // << " "<< edge->_nodes[0]->GetID()
4723 // << " "<< edge->_nodes.back()->GetID()
4724 // << " "<< edge->_simplices[iF]._nPrev->GetID()
4725 // << " "<< edge->_simplices[iF]._nNext->GetID() << " ) ");
4727 badEdges.push_back( edge );
4730 if ( !badEdges.empty() )
4734 int nbBad = invalidateBadSmooth( data, helper, badEdges, eosC1, infStep );
4738 continue; // goto the next EDGE or FACE
4742 if ( eos.SWOLType() == TopAbs_FACE )
4744 if ( !F.IsSame( eos._sWOL )) {
4745 F = TopoDS::Face( eos._sWOL );
4746 helper.SetSubShape( F );
4747 surface = helper.GetSurface( F );
4752 F.Nullify(); surface.Nullify();
4754 const TGeomID sInd = eos._shapeID;
4756 // perform smoothing
4758 if ( eos.ShapeType() == TopAbs_EDGE )
4760 dumpFunction(SMESH_Comment("smooth")<<data._index << "_Ed"<<sInd <<"_InfStep"<<infStep);
4762 if ( !eos._edgeSmoother->Perform( data, surface, F, helper ))
4764 // smooth on EDGE's (normally we should not get here)
4768 for ( size_t i = 0; i < eos._edges.size(); ++i )
4770 moved |= eos._edges[i]->SmoothOnEdge( surface, F, helper );
4772 dumpCmd( SMESH_Comment("# end step ")<<step);
4774 while ( moved && step++ < 5 );
4779 else // smooth on FACE
4782 eosC1.push_back( & eos );
4783 eosC1.insert( eosC1.end(), eos._eosC1.begin(), eos._eosC1.end() );
4786 isConcaveFace.resize( eosC1.size() );
4787 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4789 isConcaveFace[ iEOS ] = data._concaveFaces.count( eosC1[ iEOS ]->_shapeID );
4790 vector< _LayerEdge* > & edges = eosC1[ iEOS ]->_edges;
4791 for ( size_t i = 0; i < edges.size(); ++i )
4792 if ( edges[i]->Is( _LayerEdge::MOVED ) ||
4793 edges[i]->Is( _LayerEdge::NEAR_BOUNDARY ))
4794 movedEdges.push_back( edges[i] );
4796 makeOffsetSurface( *eosC1[ iEOS ], helper );
4799 int step = 0, stepLimit = 5, nbBad = 0;
4800 while (( ++step <= stepLimit ) || improved )
4802 dumpFunction(SMESH_Comment("smooth")<<data._index<<"_Fa"<<sInd
4803 <<"_InfStep"<<infStep<<"_"<<step); // debug
4804 int oldBadNb = nbBad;
4807 #ifdef INCREMENTAL_SMOOTH
4808 bool findBest = false; // ( step == stepLimit );
4809 for ( size_t i = 0; i < movedEdges.size(); ++i )
4811 movedEdges[i]->Unset( _LayerEdge::SMOOTHED );
4812 if ( movedEdges[i]->Smooth( step, findBest, movedEdges ) > 0 )
4813 badEdges.push_back( movedEdges[i] );
4816 bool findBest = ( step == stepLimit || isConcaveFace[ iEOS ]);
4817 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4819 vector< _LayerEdge* > & edges = eosC1[ iEOS ]->_edges;
4820 for ( size_t i = 0; i < edges.size(); ++i )
4822 edges[i]->Unset( _LayerEdge::SMOOTHED );
4823 if ( edges[i]->Smooth( step, findBest, false ) > 0 )
4824 badEdges.push_back( eos._edges[i] );
4828 nbBad = badEdges.size();
4831 debugMsg(SMESH_Comment("nbBad = ") << nbBad );
4833 if ( !badEdges.empty() && step >= stepLimit / 2 )
4835 if ( badEdges[0]->Is( _LayerEdge::ON_CONCAVE_FACE ))
4838 // resolve hard smoothing situation around concave VERTEXes
4839 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4841 vector< _EdgesOnShape* > & eosCoVe = eosC1[ iEOS ]->_eosConcaVer;
4842 for ( size_t i = 0; i < eosCoVe.size(); ++i )
4843 eosCoVe[i]->_edges[0]->MoveNearConcaVer( eosCoVe[i], eosC1[ iEOS ],
4846 // look for the best smooth of _LayerEdge's neighboring badEdges
4848 for ( size_t i = 0; i < badEdges.size(); ++i )
4850 _LayerEdge* ledge = badEdges[i];
4851 for ( size_t iN = 0; iN < ledge->_neibors.size(); ++iN )
4853 ledge->_neibors[iN]->Unset( _LayerEdge::SMOOTHED );
4854 nbBad += ledge->_neibors[iN]->Smooth( step, true, /*findBest=*/true );
4856 ledge->Unset( _LayerEdge::SMOOTHED );
4857 nbBad += ledge->Smooth( step, true, /*findBest=*/true );
4859 debugMsg(SMESH_Comment("nbBad = ") << nbBad );
4862 if ( nbBad == oldBadNb &&
4864 step < stepLimit ) // smooth w/o chech of validity
4867 dumpFunction(SMESH_Comment("smoothWoCheck")<<data._index<<"_Fa"<<sInd
4868 <<"_InfStep"<<infStep<<"_"<<step); // debug
4869 for ( size_t i = 0; i < movedEdges.size(); ++i )
4871 movedEdges[i]->SmoothWoCheck();
4873 if ( stepLimit < 9 )
4877 improved = ( nbBad < oldBadNb );
4881 if (( step % 3 == 1 ) || ( nbBad > 0 && step >= stepLimit / 2 ))
4882 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4884 putOnOffsetSurface( *eosC1[ iEOS ], infStep, eosC1, step, /*moveAll=*/step == 1 );
4887 } // smoothing steps
4889 // project -- to prevent intersections or fix bad simplices
4890 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4892 if ( ! eosC1[ iEOS ]->_eosConcaVer.empty() || nbBad > 0 )
4893 putOnOffsetSurface( *eosC1[ iEOS ], infStep, eosC1 );
4896 //if ( !badEdges.empty() )
4899 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
4901 for ( size_t i = 0; i < eosC1[ iEOS ]->_edges.size(); ++i )
4903 if ( !eosC1[ iEOS ]->_sWOL.IsNull() ) continue;
4905 _LayerEdge* edge = eosC1[ iEOS ]->_edges[i];
4906 edge->CheckNeiborsOnBoundary( & badEdges );
4907 if (( nbBad > 0 ) ||
4908 ( edge->Is( _LayerEdge::BLOCKED ) && edge->Is( _LayerEdge::NEAR_BOUNDARY )))
4910 SMESH_TNodeXYZ tgtXYZ = edge->_nodes.back();
4911 gp_XYZ prevXYZ = edge->PrevCheckPos();
4912 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
4913 if ( !edge->_simplices[j].IsForward( &prevXYZ, &tgtXYZ, vol ))
4915 debugMsg("Bad simplex ( " << edge->_nodes[0]->GetID()
4916 << " "<< tgtXYZ._node->GetID()
4917 << " "<< edge->_simplices[j]._nPrev->GetID()
4918 << " "<< edge->_simplices[j]._nNext->GetID() << " )" );
4919 badEdges.push_back( edge );
4926 // try to fix bad simplices by removing the last inflation step of some _LayerEdge's
4927 nbBad = invalidateBadSmooth( data, helper, badEdges, eosC1, infStep );
4933 } // // smooth on FACE's
4935 } // smooth on [ EDGEs, FACEs ]
4937 // Check orientation of simplices of _LayerEdge's on EDGEs and VERTEXes
4939 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4941 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
4942 if ( eos.ShapeType() == TopAbs_FACE ||
4943 eos._edges.empty() ||
4944 !eos._sWOL.IsNull() )
4948 for ( size_t i = 0; i < eos._edges.size(); ++i )
4950 _LayerEdge* edge = eos._edges[i];
4951 if ( edge->_nodes.size() < 2 ) continue;
4952 SMESH_TNodeXYZ tgtXYZ = edge->_nodes.back();
4953 //SMESH_TNodeXYZ prevXYZ = edge->_nodes[0];
4954 gp_XYZ prevXYZ = edge->PrevCheckPos( &eos );
4955 //const gp_XYZ& prevXYZ = edge->PrevPos();
4956 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
4957 if ( !edge->_simplices[j].IsForward( &prevXYZ, &tgtXYZ, vol ))
4959 debugMsg("Bad simplex on bnd ( " << edge->_nodes[0]->GetID()
4960 << " "<< tgtXYZ._node->GetID()
4961 << " "<< edge->_simplices[j]._nPrev->GetID()
4962 << " "<< edge->_simplices[j]._nNext->GetID() << " )" );
4963 badEdges.push_back( edge );
4968 // try to fix bad simplices by removing the last inflation step of some _LayerEdge's
4970 int nbBad = invalidateBadSmooth( data, helper, badEdges, eosC1, infStep );
4976 // Check if the last segments of _LayerEdge intersects 2D elements;
4977 // checked elements are either temporary faces or faces on surfaces w/o the layers
4979 SMESHUtils::Deleter<SMESH_ElementSearcher> searcher
4980 ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(),
4981 data._proxyMesh->GetFaces( data._solid )) );
4983 #ifdef BLOCK_INFLATION
4984 const bool toBlockInfaltion = true;
4986 const bool toBlockInfaltion = false;
4988 distToIntersection = Precision::Infinite();
4990 const SMDS_MeshElement* intFace = 0;
4991 const SMDS_MeshElement* closestFace = 0;
4993 bool is1stBlocked = true; // dbg
4994 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
4996 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
4997 if ( eos._edges.empty() || !eos._sWOL.IsNull() )
4999 for ( size_t i = 0; i < eos._edges.size(); ++i )
5001 if ( eos._edges[i]->Is( _LayerEdge::INTERSECTED ) ||
5002 eos._edges[i]->Is( _LayerEdge::MULTI_NORMAL ))
5004 if ( eos._edges[i]->FindIntersection( *searcher, dist, data._epsilon, eos, &intFace ))
5007 // commented due to "Illegal hash-positionPosition" error in NETGEN
5008 // on Debian60 on viscous_layers_01/B2 case
5009 // Collision; try to deflate _LayerEdge's causing it
5010 // badEdges.clear();
5011 // badEdges.push_back( eos._edges[i] );
5012 // eosC1[0] = & eos;
5013 // int nbBad = invalidateBadSmooth( data, helper, badEdges, eosC1, infStep );
5017 // badEdges.clear();
5018 // if ( _EdgesOnShape* eof = data.GetShapeEdges( intFace->getshapeId() ))
5020 // if ( const _TmpMeshFace* f = dynamic_cast< const _TmpMeshFace*>( intFace ))
5022 // const SMDS_MeshElement* srcFace =
5023 // eof->_subMesh->GetSubMeshDS()->GetElement( f->getIdInShape() );
5024 // SMDS_ElemIteratorPtr nIt = srcFace->nodesIterator();
5025 // while ( nIt->more() )
5027 // const SMDS_MeshNode* srcNode = static_cast<const SMDS_MeshNode*>( nIt->next() );
5028 // TNode2Edge::iterator n2e = data._n2eMap.find( srcNode );
5029 // if ( n2e != data._n2eMap.end() )
5030 // badEdges.push_back( n2e->second );
5033 // nbBad = invalidateBadSmooth( data, helper, badEdges, eosC1, infStep );
5038 // if ( eos._edges[i]->FindIntersection( *searcher, dist, data._epsilon, eos, &intFace ))
5045 SMESH_Comment msg("Invalid? normal at node "); msg << eos._edges[i]->_nodes[0]->GetID();
5050 const bool isShorterDist = ( distToIntersection > dist );
5051 if ( toBlockInfaltion || isShorterDist )
5053 // ignore intersection of a _LayerEdge based on a _ConvexFace with a face
5054 // lying on this _ConvexFace
5055 if ( _ConvexFace* convFace = data.GetConvexFace( intFace->getshapeId() ))
5056 if ( convFace->_isTooCurved && convFace->_subIdToEOS.count ( eos._shapeID ))
5059 // ignore intersection of a _LayerEdge based on a FACE with an element on this FACE
5060 // ( avoid limiting the thickness on the case of issue 22576)
5061 if ( intFace->getshapeId() == eos._shapeID )
5064 // ignore intersection with intFace of an adjacent FACE
5065 if ( dist > 0.1 * eos._edges[i]->_len )
5067 bool toIgnore = false;
5068 if ( eos._toSmooth )
5070 const TopoDS_Shape& S = getMeshDS()->IndexToShape( intFace->getshapeId() );
5071 if ( !S.IsNull() && S.ShapeType() == TopAbs_FACE )
5073 TopExp_Explorer sub( eos._shape,
5074 eos.ShapeType() == TopAbs_FACE ? TopAbs_EDGE : TopAbs_VERTEX );
5075 for ( ; !toIgnore && sub.More(); sub.Next() )
5076 // is adjacent - has a common EDGE or VERTEX
5077 toIgnore = ( helper.IsSubShape( sub.Current(), S ));
5079 if ( toIgnore ) // check angle between normals
5082 if ( SMESH_MeshAlgos::FaceNormal( intFace, normal, /*normalized=*/true ))
5083 toIgnore = ( normal * eos._edges[i]->_normal > -0.5 );
5087 if ( !toIgnore ) // check if the edge is a neighbor of intFace
5089 for ( size_t iN = 0; !toIgnore && iN < eos._edges[i]->_neibors.size(); ++iN )
5091 int nInd = intFace->GetNodeIndex( eos._edges[i]->_neibors[ iN ]->_nodes.back() );
5092 toIgnore = ( nInd >= 0 );
5099 // intersection not ignored
5101 if ( toBlockInfaltion &&
5102 dist < ( eos._edges[i]->_len * theThickToIntersection ))
5104 if ( is1stBlocked ) { is1stBlocked = false; // debug
5105 dumpFunction(SMESH_Comment("blockIntersected") <<data._index<<"_InfStep"<<infStep);
5107 eos._edges[i]->Set( _LayerEdge::INTERSECTED ); // not to intersect
5108 eos._edges[i]->Block( data ); // not to inflate
5110 if ( _EdgesOnShape* eof = data.GetShapeEdges( intFace->getshapeId() ))
5112 // block _LayerEdge's, on top of which intFace is
5113 if ( const _TmpMeshFace* f = dynamic_cast< const _TmpMeshFace*>( intFace ))
5115 const SMDS_MeshElement* srcFace =
5116 eof->_subMesh->GetSubMeshDS()->GetElement( f->getIdInShape() );
5117 SMDS_ElemIteratorPtr nIt = srcFace->nodesIterator();
5118 while ( nIt->more() )
5120 const SMDS_MeshNode* srcNode = static_cast<const SMDS_MeshNode*>( nIt->next() );
5121 TNode2Edge::iterator n2e = data._n2eMap.find( srcNode );
5122 if ( n2e != data._n2eMap.end() )
5123 n2e->second->Block( data );
5129 if ( isShorterDist )
5131 distToIntersection = dist;
5133 closestFace = intFace;
5136 } // if ( toBlockInfaltion || isShorterDist )
5137 } // loop on eos._edges
5138 } // loop on data._edgesOnShape
5140 if ( !is1stBlocked )
5143 if ( closestFace && le )
5146 SMDS_MeshElement::iterator nIt = closestFace->begin_nodes();
5147 cout << "#Shortest distance: _LayerEdge nodes: tgt " << le->_nodes.back()->GetID()
5148 << " src " << le->_nodes[0]->GetID()<< ", intersection with face ("
5149 << (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()
5150 << ") distance = " << distToIntersection<< endl;
5157 //================================================================================
5159 * \brief try to fix bad simplices by removing the last inflation step of some _LayerEdge's
5160 * \param [in,out] badSmooEdges - _LayerEdge's to fix
5161 * \return int - resulting nb of bad _LayerEdge's
5163 //================================================================================
5165 int _ViscousBuilder::invalidateBadSmooth( _SolidData& data,
5166 SMESH_MesherHelper& helper,
5167 vector< _LayerEdge* >& badSmooEdges,
5168 vector< _EdgesOnShape* >& eosC1,
5171 if ( badSmooEdges.empty() || infStep == 0 ) return 0;
5173 dumpFunction(SMESH_Comment("invalidateBadSmooth")<<"_S"<<eosC1[0]->_shapeID<<"_InfStep"<<infStep);
5176 INVALIDATED = _LayerEdge::UNUSED_FLAG,
5177 TO_INVALIDATE = _LayerEdge::UNUSED_FLAG * 2,
5178 ADDED = _LayerEdge::UNUSED_FLAG * 4
5180 data.UnmarkEdges( TO_INVALIDATE & INVALIDATED & ADDED );
5183 bool haveInvalidated = true;
5184 while ( haveInvalidated )
5186 haveInvalidated = false;
5187 for ( size_t i = 0; i < badSmooEdges.size(); ++i )
5189 _LayerEdge* edge = badSmooEdges[i];
5190 _EdgesOnShape* eos = data.GetShapeEdges( edge );
5192 bool invalidated = false;
5193 if ( edge->Is( TO_INVALIDATE ) && edge->NbSteps() > 1 )
5195 edge->InvalidateStep( edge->NbSteps(), *eos, /*restoreLength=*/true );
5196 edge->Block( data );
5197 edge->Set( INVALIDATED );
5198 edge->Unset( TO_INVALIDATE );
5200 haveInvalidated = true;
5203 // look for _LayerEdge's of bad _simplices
5205 SMESH_TNodeXYZ tgtXYZ = edge->_nodes.back();
5206 gp_XYZ prevXYZ1 = edge->PrevCheckPos( eos );
5207 //const gp_XYZ& prevXYZ2 = edge->PrevPos();
5208 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
5210 if (( edge->_simplices[j].IsForward( &prevXYZ1, &tgtXYZ, vol ))/* &&
5211 ( &prevXYZ1 == &prevXYZ2 || edge->_simplices[j].IsForward( &prevXYZ2, &tgtXYZ, vol ))*/)
5215 _LayerEdge* ee[2] = { 0,0 };
5216 for ( size_t iN = 0; iN < edge->_neibors.size() && !ee[1] ; ++iN )
5217 if ( edge->_simplices[j].Includes( edge->_neibors[iN]->_nodes.back() ))
5218 ee[ ee[0] != 0 ] = edge->_neibors[iN];
5220 int maxNbSteps = Max( ee[0]->NbSteps(), ee[1]->NbSteps() );
5221 while ( maxNbSteps > edge->NbSteps() && isBad )
5224 for ( int iE = 0; iE < 2; ++iE )
5226 if ( ee[ iE ]->NbSteps() > maxNbSteps &&
5227 ee[ iE ]->NbSteps() > 1 )
5229 _EdgesOnShape* eos = data.GetShapeEdges( ee[ iE ] );
5230 ee[ iE ]->InvalidateStep( ee[ iE ]->NbSteps(), *eos, /*restoreLength=*/true );
5231 ee[ iE ]->Block( data );
5232 ee[ iE ]->Set( INVALIDATED );
5233 haveInvalidated = true;
5236 if (( edge->_simplices[j].IsForward( &prevXYZ1, &tgtXYZ, vol )) /*&&
5237 ( &prevXYZ1 == &prevXYZ2 || edge->_simplices[j].IsForward( &prevXYZ2, &tgtXYZ, vol ))*/)
5241 if ( !ee[0]->Is( ADDED )) badSmooEdges.push_back( ee[0] );
5242 if ( !ee[1]->Is( ADDED )) badSmooEdges.push_back( ee[1] );
5243 ee[0]->Set( ADDED );
5244 ee[1]->Set( ADDED );
5247 ee[0]->Set( TO_INVALIDATE );
5248 ee[1]->Set( TO_INVALIDATE );
5252 if ( !invalidated && nbBad > 0 && edge->NbSteps() > 1 )
5254 _EdgesOnShape* eos = data.GetShapeEdges( edge );
5255 edge->InvalidateStep( edge->NbSteps(), *eos, /*restoreLength=*/true );
5256 edge->Block( data );
5257 edge->Set( INVALIDATED );
5258 edge->Unset( TO_INVALIDATE );
5259 haveInvalidated = true;
5261 } // loop on badSmooEdges
5262 } // while ( haveInvalidated )
5264 // re-smooth on analytical EDGEs
5265 for ( size_t i = 0; i < badSmooEdges.size(); ++i )
5267 _LayerEdge* edge = badSmooEdges[i];
5268 if ( !edge->Is( INVALIDATED )) continue;
5270 _EdgesOnShape* eos = data.GetShapeEdges( edge );
5271 if ( eos->ShapeType() == TopAbs_VERTEX )
5273 PShapeIteratorPtr eIt = helper.GetAncestors( eos->_shape, *_mesh, TopAbs_EDGE );
5274 while ( const TopoDS_Shape* e = eIt->next() )
5275 if ( _EdgesOnShape* eoe = data.GetShapeEdges( *e ))
5276 if ( eoe->_edgeSmoother && eoe->_edgeSmoother->isAnalytic() )
5278 // TopoDS_Face F; Handle(ShapeAnalysis_Surface) surface;
5279 // if ( eoe->SWOLType() == TopAbs_FACE ) {
5280 // F = TopoDS::Face( eoe->_sWOL );
5281 // surface = helper.GetSurface( F );
5283 // eoe->_edgeSmoother->Perform( data, surface, F, helper );
5284 eoe->_edgeSmoother->_anaCurve.Nullify();
5290 // check result of invalidation
5293 for ( size_t iEOS = 0; iEOS < eosC1.size(); ++iEOS )
5295 for ( size_t i = 0; i < eosC1[ iEOS ]->_edges.size(); ++i )
5297 if ( !eosC1[ iEOS ]->_sWOL.IsNull() ) continue;
5298 _LayerEdge* edge = eosC1[ iEOS ]->_edges[i];
5299 SMESH_TNodeXYZ tgtXYZ = edge->_nodes.back();
5300 gp_XYZ prevXYZ = edge->PrevCheckPos( eosC1[ iEOS ]);
5301 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
5302 if ( !edge->_simplices[j].IsForward( &prevXYZ, &tgtXYZ, vol ))
5305 debugMsg("Bad simplex remains ( " << edge->_nodes[0]->GetID()
5306 << " "<< tgtXYZ._node->GetID()
5307 << " "<< edge->_simplices[j]._nPrev->GetID()
5308 << " "<< edge->_simplices[j]._nNext->GetID() << " )" );
5317 //================================================================================
5319 * \brief Create an offset surface
5321 //================================================================================
5323 void _ViscousBuilder::makeOffsetSurface( _EdgesOnShape& eos, SMESH_MesherHelper& helper )
5325 if ( eos._offsetSurf.IsNull() ||
5326 eos._edgeForOffset == 0 ||
5327 eos._edgeForOffset->Is( _LayerEdge::BLOCKED ))
5330 Handle(ShapeAnalysis_Surface) baseSurface = helper.GetSurface( TopoDS::Face( eos._shape ));
5333 gp_Pnt tgtP = SMESH_TNodeXYZ( eos._edgeForOffset->_nodes.back() );
5334 /*gp_Pnt2d uv=*/baseSurface->ValueOfUV( tgtP, Precision::Confusion() );
5335 double offset = baseSurface->Gap();
5337 eos._offsetSurf.Nullify();
5341 BRepOffsetAPI_MakeOffsetShape offsetMaker;
5342 offsetMaker.PerformByJoin( eos._shape, -offset, Precision::Confusion() );
5343 if ( !offsetMaker.IsDone() ) return;
5345 TopExp_Explorer fExp( offsetMaker.Shape(), TopAbs_FACE );
5346 if ( !fExp.More() ) return;
5348 TopoDS_Face F = TopoDS::Face( fExp.Current() );
5349 Handle(Geom_Surface) surf = BRep_Tool::Surface( F );
5350 if ( surf.IsNull() ) return;
5352 eos._offsetSurf = new ShapeAnalysis_Surface( surf );
5354 catch ( Standard_Failure )
5359 //================================================================================
5361 * \brief Put nodes of a curved FACE to its offset surface
5363 //================================================================================
5365 void _ViscousBuilder::putOnOffsetSurface( _EdgesOnShape& eos,
5367 vector< _EdgesOnShape* >& eosC1,
5371 _EdgesOnShape * eof = & eos;
5372 if ( eos.ShapeType() != TopAbs_FACE ) // eos is a boundary of C1 FACE, look for the FACE eos
5375 for ( size_t i = 0; i < eosC1.size() && !eof; ++i )
5377 if ( eosC1[i]->_offsetSurf.IsNull() ||
5378 eosC1[i]->ShapeType() != TopAbs_FACE ||
5379 eosC1[i]->_edgeForOffset == 0 ||
5380 eosC1[i]->_edgeForOffset->Is( _LayerEdge::BLOCKED ))
5382 if ( SMESH_MesherHelper::IsSubShape( eos._shape, eosC1[i]->_shape ))
5387 eof->_offsetSurf.IsNull() ||
5388 eof->ShapeType() != TopAbs_FACE ||
5389 eof->_edgeForOffset == 0 ||
5390 eof->_edgeForOffset->Is( _LayerEdge::BLOCKED ))
5393 double preci = BRep_Tool::Tolerance( TopoDS::Face( eof->_shape )), vol;
5394 for ( size_t i = 0; i < eos._edges.size(); ++i )
5396 _LayerEdge* edge = eos._edges[i];
5397 edge->Unset( _LayerEdge::MARKED );
5398 if ( edge->Is( _LayerEdge::BLOCKED ) || !edge->_curvature )
5400 if ( moveAll == _LayerEdge::UPD_NORMAL_CONV )
5402 if ( !edge->Is( _LayerEdge::UPD_NORMAL_CONV ))
5405 else if ( !moveAll && !edge->Is( _LayerEdge::MOVED ))
5408 int nbBlockedAround = 0;
5409 for ( size_t iN = 0; iN < edge->_neibors.size(); ++iN )
5410 nbBlockedAround += edge->_neibors[iN]->Is( _LayerEdge::BLOCKED );
5411 if ( nbBlockedAround > 1 )
5414 gp_Pnt tgtP = SMESH_TNodeXYZ( edge->_nodes.back() );
5415 gp_Pnt2d uv = eof->_offsetSurf->NextValueOfUV( edge->_curvature->_uv, tgtP, preci );
5416 if ( eof->_offsetSurf->Gap() > edge->_len ) continue; // NextValueOfUV() bug
5417 edge->_curvature->_uv = uv;
5418 if ( eof->_offsetSurf->Gap() < 10 * preci ) continue; // same pos
5420 gp_XYZ newP = eof->_offsetSurf->Value( uv ).XYZ();
5421 gp_XYZ prevP = edge->PrevCheckPos();
5424 for ( size_t iS = 0; iS < edge->_simplices.size() && ok; ++iS )
5426 ok = edge->_simplices[iS].IsForward( &prevP, &newP, vol );
5430 SMDS_MeshNode* n = const_cast< SMDS_MeshNode* >( edge->_nodes.back() );
5431 n->setXYZ( newP.X(), newP.Y(), newP.Z());
5432 edge->_pos.back() = newP;
5434 edge->Set( _LayerEdge::MARKED );
5435 if ( moveAll == _LayerEdge::UPD_NORMAL_CONV )
5437 edge->_normal = ( newP - prevP ).Normalized();
5445 // dumpMove() for debug
5447 for ( ; i < eos._edges.size(); ++i )
5448 if ( eos._edges[i]->Is( _LayerEdge::MARKED ))
5450 if ( i < eos._edges.size() )
5452 dumpFunction(SMESH_Comment("putOnOffsetSurface_S") << eos._shapeID
5453 << "_InfStep" << infStep << "_" << smooStep );
5454 for ( ; i < eos._edges.size(); ++i )
5456 if ( eos._edges[i]->Is( _LayerEdge::MARKED ))
5457 dumpMove( eos._edges[i]->_nodes.back() );
5463 _ConvexFace* cnvFace;
5464 if ( moveAll != _LayerEdge::UPD_NORMAL_CONV &&
5465 eos.ShapeType() == TopAbs_FACE &&
5466 (cnvFace = eos.GetData().GetConvexFace( eos._shapeID )) &&
5467 !cnvFace->_normalsFixedOnBorders )
5469 // put on the surface nodes built on FACE boundaries
5470 SMESH_subMeshIteratorPtr smIt = eos._subMesh->getDependsOnIterator(/*includeSelf=*/false);
5471 while ( smIt->more() )
5473 SMESH_subMesh* sm = smIt->next();
5474 _EdgesOnShape* subEOS = eos.GetData().GetShapeEdges( sm->GetId() );
5475 if ( !subEOS->_sWOL.IsNull() ) continue;
5476 if ( std::find( eosC1.begin(), eosC1.end(), subEOS ) != eosC1.end() ) continue;
5478 putOnOffsetSurface( *subEOS, infStep, eosC1, smooStep, _LayerEdge::UPD_NORMAL_CONV );
5480 cnvFace->_normalsFixedOnBorders = true;
5484 //================================================================================
5486 * \brief Return a curve of the EDGE to be used for smoothing and arrange
5487 * _LayerEdge's to be in a consequent order
5489 //================================================================================
5491 Handle(Geom_Curve) _Smoother1D::CurveForSmooth( const TopoDS_Edge& E,
5493 SMESH_MesherHelper& helper)
5495 SMESHDS_SubMesh* smDS = eos._subMesh->GetSubMeshDS();
5497 TopLoc_Location loc; double f,l;
5499 Handle(Geom_Line) line;
5500 Handle(Geom_Circle) circle;
5501 bool isLine, isCirc;
5502 if ( eos._sWOL.IsNull() ) /////////////////////////////////////////// 3D case
5504 // check if the EDGE is a line
5505 Handle(Geom_Curve) curve = BRep_Tool::Curve( E, f, l);
5506 if ( curve->IsKind( STANDARD_TYPE( Geom_TrimmedCurve )))
5507 curve = Handle(Geom_TrimmedCurve)::DownCast( curve )->BasisCurve();
5509 line = Handle(Geom_Line)::DownCast( curve );
5510 circle = Handle(Geom_Circle)::DownCast( curve );
5511 isLine = (!line.IsNull());
5512 isCirc = (!circle.IsNull());
5514 if ( !isLine && !isCirc ) // Check if the EDGE is close to a line
5516 isLine = SMESH_Algo::IsStraight( E );
5519 line = new Geom_Line( gp::OX() ); // only type does matter
5521 if ( !isLine && !isCirc && eos._edges.size() > 2) // Check if the EDGE is close to a circle
5526 else //////////////////////////////////////////////////////////////////////// 2D case
5528 if ( !eos._isRegularSWOL ) // 23190
5531 const TopoDS_Face& F = TopoDS::Face( eos._sWOL );
5533 // check if the EDGE is a line
5534 Handle(Geom2d_Curve) curve = BRep_Tool::CurveOnSurface( E, F, f, l );
5535 if ( curve->IsKind( STANDARD_TYPE( Geom2d_TrimmedCurve )))
5536 curve = Handle(Geom2d_TrimmedCurve)::DownCast( curve )->BasisCurve();
5538 Handle(Geom2d_Line) line2d = Handle(Geom2d_Line)::DownCast( curve );
5539 Handle(Geom2d_Circle) circle2d = Handle(Geom2d_Circle)::DownCast( curve );
5540 isLine = (!line2d.IsNull());
5541 isCirc = (!circle2d.IsNull());
5543 if ( !isLine && !isCirc ) // Check if the EDGE is close to a line
5546 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
5547 while ( nIt->more() )
5548 bndBox.Add( helper.GetNodeUV( F, nIt->next() ));
5549 gp_XY size = bndBox.CornerMax() - bndBox.CornerMin();
5551 const double lineTol = 1e-2 * sqrt( bndBox.SquareExtent() );
5552 for ( int i = 0; i < 2 && !isLine; ++i )
5553 isLine = ( size.Coord( i+1 ) <= lineTol );
5555 if ( !isLine && !isCirc && eos._edges.size() > 2 ) // Check if the EDGE is close to a circle
5561 line = new Geom_Line( gp::OX() ); // only type does matter
5565 gp_Pnt2d p = circle2d->Location();
5566 gp_Ax2 ax( gp_Pnt( p.X(), p.Y(), 0), gp::DX());
5567 circle = new Geom_Circle( ax, 1.); // only center position does matter
5576 return Handle(Geom_Curve)();
5579 //================================================================================
5581 * \brief Smooth edges on EDGE
5583 //================================================================================
5585 bool _Smoother1D::Perform(_SolidData& data,
5586 Handle(ShapeAnalysis_Surface)& surface,
5587 const TopoDS_Face& F,
5588 SMESH_MesherHelper& helper )
5590 if ( _leParams.empty() || ( !isAnalytic() && _offPoints.empty() ))
5593 findEdgesToSmooth();
5595 return smoothAnalyticEdge( data, surface, F, helper );
5597 return smoothComplexEdge ( data, surface, F, helper );
5600 //================================================================================
5602 * \brief Find edges to smooth
5604 //================================================================================
5606 void _Smoother1D::findEdgesToSmooth()
5608 _LayerEdge* leOnV[2] = { getLEdgeOnV(0), getLEdgeOnV(1) };
5609 for ( int iEnd = 0; iEnd < 2; ++iEnd )
5610 if ( leOnV[iEnd]->Is( _LayerEdge::NORMAL_UPDATED ))
5611 _leOnV[iEnd]._cosin = Abs( _edgeDir[iEnd].Normalized() * leOnV[iEnd]->_normal );
5613 _eToSmooth[0].first = _eToSmooth[0].second = 0;
5615 for ( size_t i = 0; i < _eos.size(); ++i )
5617 if ( !_eos[i]->Is( _LayerEdge::TO_SMOOTH ))
5619 if ( needSmoothing( _leOnV[0]._cosin,
5620 _eos[i]->_len * leOnV[0]->_lenFactor, _curveLen * _leParams[i] ) ||
5623 _eos[i]->Set( _LayerEdge::TO_SMOOTH );
5627 _eToSmooth[0].second = i+1;
5630 _eToSmooth[1].first = _eToSmooth[1].second = _eos.size();
5632 for ( int i = _eos.size() - 1; i >= _eToSmooth[0].second; --i )
5634 if ( !_eos[i]->Is( _LayerEdge::TO_SMOOTH ))
5636 if ( needSmoothing( _leOnV[1]._cosin,
5637 _eos[i]->_len * leOnV[1]->_lenFactor, _curveLen * ( 1.-_leParams[i] )) ||
5639 _eos[i]->Set( _LayerEdge::TO_SMOOTH );
5643 _eToSmooth[1].first = i;
5647 //================================================================================
5649 * \brief Check if iE-th _LayerEdge needs smoothing
5651 //================================================================================
5653 bool _Smoother1D::isToSmooth( int iE )
5655 SMESH_NodeXYZ pi( _eos[iE]->_nodes[0] );
5656 SMESH_NodeXYZ p0( _eos[iE]->_2neibors->srcNode(0) );
5657 SMESH_NodeXYZ p1( _eos[iE]->_2neibors->srcNode(1) );
5658 gp_XYZ seg0 = pi - p0;
5659 gp_XYZ seg1 = p1 - pi;
5660 gp_XYZ tangent = seg0 + seg1;
5661 double tangentLen = tangent.Modulus();
5662 double segMinLen = Min( seg0.Modulus(), seg1.Modulus() );
5663 if ( tangentLen < std::numeric_limits<double>::min() )
5665 tangent /= tangentLen;
5667 for ( size_t i = 0; i < _eos[iE]->_neibors.size(); ++i )
5669 _LayerEdge* ne = _eos[iE]->_neibors[i];
5670 if ( !ne->Is( _LayerEdge::TO_SMOOTH ) ||
5671 ne->_nodes.size() < 2 ||
5672 ne->_nodes[0]->GetPosition()->GetDim() != 2 )
5674 gp_XYZ edgeVec = SMESH_NodeXYZ( ne->_nodes.back() ) - SMESH_NodeXYZ( ne->_nodes[0] );
5675 double proj = edgeVec * tangent;
5676 if ( needSmoothing( 1., proj, segMinLen ))
5682 //================================================================================
5684 * \brief smooth _LayerEdge's on a staight EDGE or circular EDGE
5686 //================================================================================
5688 bool _Smoother1D::smoothAnalyticEdge( _SolidData& data,
5689 Handle(ShapeAnalysis_Surface)& surface,
5690 const TopoDS_Face& F,
5691 SMESH_MesherHelper& helper)
5693 if ( !isAnalytic() ) return false;
5695 size_t iFrom = 0, iTo = _eos._edges.size();
5697 if ( _anaCurve->IsKind( STANDARD_TYPE( Geom_Line )))
5699 if ( F.IsNull() ) // 3D
5701 SMESH_TNodeXYZ pSrc0( _eos._edges[iFrom]->_2neibors->srcNode(0) );
5702 SMESH_TNodeXYZ pSrc1( _eos._edges[iTo-1]->_2neibors->srcNode(1) );
5703 //const gp_XYZ lineDir = pSrc1 - pSrc0;
5704 //_LayerEdge* vLE0 = getLEdgeOnV( 0 );
5705 //_LayerEdge* vLE1 = getLEdgeOnV( 1 );
5706 // bool shiftOnly = ( vLE0->Is( _LayerEdge::NORMAL_UPDATED ) ||
5707 // vLE0->Is( _LayerEdge::BLOCKED ) ||
5708 // vLE1->Is( _LayerEdge::NORMAL_UPDATED ) ||
5709 // vLE1->Is( _LayerEdge::BLOCKED ));
5710 for ( int iEnd = 0; iEnd < 2; ++iEnd )
5712 iFrom = _eToSmooth[ iEnd ].first, iTo = _eToSmooth[ iEnd ].second;
5713 if ( iFrom >= iTo ) continue;
5714 SMESH_TNodeXYZ p0( _eos[iFrom]->_2neibors->tgtNode(0) );
5715 SMESH_TNodeXYZ p1( _eos[iTo-1]->_2neibors->tgtNode(1) );
5716 double param0 = ( iFrom == 0 ) ? 0. : _leParams[ iFrom-1 ];
5717 double param1 = _leParams[ iTo ];
5718 for ( size_t i = iFrom; i < iTo; ++i )
5720 _LayerEdge* edge = _eos[i];
5721 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( edge->_nodes.back() );
5722 double param = ( _leParams[i] - param0 ) / ( param1 - param0 );
5723 gp_XYZ newPos = p0 * ( 1. - param ) + p1 * param;
5725 // if ( shiftOnly || edge->Is( _LayerEdge::NORMAL_UPDATED ))
5727 // gp_XYZ curPos = SMESH_TNodeXYZ ( tgtNode );
5728 // double shift = ( lineDir * ( newPos - pSrc0 ) -
5729 // lineDir * ( curPos - pSrc0 ));
5730 // newPos = curPos + lineDir * shift / lineDir.SquareModulus();
5732 if ( edge->Is( _LayerEdge::BLOCKED ))
5734 SMESH_TNodeXYZ pSrc( edge->_nodes[0] );
5735 double curThick = pSrc.SquareDistance( tgtNode );
5736 double newThink = ( pSrc - newPos ).SquareModulus();
5737 if ( newThink > curThick )
5740 edge->_pos.back() = newPos;
5741 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
5742 dumpMove( tgtNode );
5748 _LayerEdge* eV0 = getLEdgeOnV( 0 );
5749 _LayerEdge* eV1 = getLEdgeOnV( 1 );
5750 gp_XY uvV0 = eV0->LastUV( F, *data.GetShapeEdges( eV0 ));
5751 gp_XY uvV1 = eV1->LastUV( F, *data.GetShapeEdges( eV1 ));
5752 if ( eV0->_nodes.back() == eV1->_nodes.back() ) // closed edge
5754 int iPeriodic = helper.GetPeriodicIndex();
5755 if ( iPeriodic == 1 || iPeriodic == 2 )
5757 uvV1.SetCoord( iPeriodic, helper.GetOtherParam( uvV1.Coord( iPeriodic )));
5758 if ( uvV0.Coord( iPeriodic ) > uvV1.Coord( iPeriodic ))
5759 std::swap( uvV0, uvV1 );
5762 for ( int iEnd = 0; iEnd < 2; ++iEnd )
5764 iFrom = _eToSmooth[ iEnd ].first, iTo = _eToSmooth[ iEnd ].second;
5765 if ( iFrom >= iTo ) continue;
5766 _LayerEdge* e0 = _eos[iFrom]->_2neibors->_edges[0];
5767 _LayerEdge* e1 = _eos[iTo-1]->_2neibors->_edges[1];
5768 gp_XY uv0 = ( e0 == eV0 ) ? uvV0 : e0->LastUV( F, _eos );
5769 gp_XY uv1 = ( e1 == eV1 ) ? uvV1 : e1->LastUV( F, _eos );
5770 double param0 = ( iFrom == 0 ) ? 0. : _leParams[ iFrom-1 ];
5771 double param1 = _leParams[ iTo ];
5772 gp_XY rangeUV = uv1 - uv0;
5773 for ( size_t i = iFrom; i < iTo; ++i )
5775 if ( _eos[i]->Is( _LayerEdge::BLOCKED )) continue;
5776 double param = ( _leParams[i] - param0 ) / ( param1 - param0 );
5777 gp_XY newUV = uv0 + param * rangeUV;
5779 gp_Pnt newPos = surface->Value( newUV.X(), newUV.Y() );
5780 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _eos[i]->_nodes.back() );
5781 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
5782 dumpMove( tgtNode );
5784 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
5785 pos->SetUParameter( newUV.X() );
5786 pos->SetVParameter( newUV.Y() );
5788 gp_XYZ newUV0( newUV.X(), newUV.Y(), 0 );
5790 if ( !_eos[i]->Is( _LayerEdge::SMOOTHED ))
5792 _eos[i]->Set( _LayerEdge::SMOOTHED ); // to check in refine() (IPAL54237)
5793 if ( _eos[i]->_pos.size() > 2 )
5795 // modify previous positions to make _LayerEdge less sharply bent
5796 vector<gp_XYZ>& uvVec = _eos[i]->_pos;
5797 const gp_XYZ uvShift = newUV0 - uvVec.back();
5798 const double len2 = ( uvVec.back() - uvVec[ 0 ] ).SquareModulus();
5799 int iPrev = uvVec.size() - 2;
5802 double r = ( uvVec[ iPrev ] - uvVec[0] ).SquareModulus() / len2;
5803 uvVec[ iPrev ] += uvShift * r;
5808 _eos[i]->_pos.back() = newUV0;
5815 if ( _anaCurve->IsKind( STANDARD_TYPE( Geom_Circle )))
5817 Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast( _anaCurve );
5818 gp_Pnt center3D = circle->Location();
5820 if ( F.IsNull() ) // 3D
5822 if ( getLEdgeOnV( 0 )->_nodes.back() == getLEdgeOnV( 1 )->_nodes.back() )
5823 return true; // closed EDGE - nothing to do
5825 // circle is a real curve of EDGE
5826 gp_Circ circ = circle->Circ();
5828 // new center is shifted along its axis
5829 const gp_Dir& axis = circ.Axis().Direction();
5830 _LayerEdge* e0 = getLEdgeOnV(0);
5831 _LayerEdge* e1 = getLEdgeOnV(1);
5832 SMESH_TNodeXYZ p0 = e0->_nodes.back();
5833 SMESH_TNodeXYZ p1 = e1->_nodes.back();
5834 double shift1 = axis.XYZ() * ( p0 - center3D.XYZ() );
5835 double shift2 = axis.XYZ() * ( p1 - center3D.XYZ() );
5836 gp_Pnt newCenter = center3D.XYZ() + axis.XYZ() * 0.5 * ( shift1 + shift2 );
5838 double newRadius = 0.5 * ( newCenter.Distance( p0 ) + newCenter.Distance( p1 ));
5840 gp_Ax2 newAxis( newCenter, axis, gp_Vec( newCenter, p0 ));
5841 gp_Circ newCirc( newAxis, newRadius );
5842 gp_Vec vecC1 ( newCenter, p1 );
5844 double uLast = newAxis.XDirection().AngleWithRef( vecC1, newAxis.Direction() ); // -PI - +PI
5848 for ( size_t i = 0; i < _eos.size(); ++i )
5850 if ( _eos[i]->Is( _LayerEdge::BLOCKED )) continue;
5851 //if ( !_eos[i]->Is( _LayerEdge::TO_SMOOTH )) continue;
5852 double u = uLast * _leParams[i];
5853 gp_Pnt p = ElCLib::Value( u, newCirc );
5854 _eos._edges[i]->_pos.back() = p.XYZ();
5856 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _eos._edges[i]->_nodes.back() );
5857 tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
5858 dumpMove( tgtNode );
5864 const gp_XY center( center3D.X(), center3D.Y() );
5866 _LayerEdge* e0 = getLEdgeOnV(0);
5867 _LayerEdge* eM = _eos._edges[ 0 ];
5868 _LayerEdge* e1 = getLEdgeOnV(1);
5869 gp_XY uv0 = e0->LastUV( F, *data.GetShapeEdges( e0 ) );
5870 gp_XY uvM = eM->LastUV( F, *data.GetShapeEdges( eM ) );
5871 gp_XY uv1 = e1->LastUV( F, *data.GetShapeEdges( e1 ) );
5872 gp_Vec2d vec0( center, uv0 );
5873 gp_Vec2d vecM( center, uvM );
5874 gp_Vec2d vec1( center, uv1 );
5875 double uLast = vec0.Angle( vec1 ); // -PI - +PI
5876 double uMidl = vec0.Angle( vecM );
5877 if ( uLast * uMidl <= 0. )
5878 uLast += ( uMidl > 0 ? +2. : -2. ) * M_PI;
5879 const double radius = 0.5 * ( vec0.Magnitude() + vec1.Magnitude() );
5881 gp_Ax2d axis( center, vec0 );
5882 gp_Circ2d circ( axis, radius );
5883 for ( size_t i = 0; i < _eos.size(); ++i )
5885 if ( _eos[i]->Is( _LayerEdge::BLOCKED )) continue;
5886 //if ( !_eos[i]->Is( _LayerEdge::TO_SMOOTH )) continue;
5887 double newU = uLast * _leParams[i];
5888 gp_Pnt2d newUV = ElCLib::Value( newU, circ );
5889 _eos._edges[i]->_pos.back().SetCoord( newUV.X(), newUV.Y(), 0 );
5891 gp_Pnt newPos = surface->Value( newUV.X(), newUV.Y() );
5892 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _eos._edges[i]->_nodes.back() );
5893 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
5894 dumpMove( tgtNode );
5896 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
5897 pos->SetUParameter( newUV.X() );
5898 pos->SetVParameter( newUV.Y() );
5900 _eos[i]->Set( _LayerEdge::SMOOTHED ); // to check in refine() (IPAL54237)
5909 //================================================================================
5911 * \brief smooth _LayerEdge's on a an EDGE
5913 //================================================================================
5915 bool _Smoother1D::smoothComplexEdge( _SolidData& data,
5916 Handle(ShapeAnalysis_Surface)& surface,
5917 const TopoDS_Face& F,
5918 SMESH_MesherHelper& helper)
5920 if ( _offPoints.empty() )
5923 // ----------------------------------------------
5924 // move _offPoints along normals of _LayerEdge's
5925 // ----------------------------------------------
5927 _LayerEdge* e[2] = { getLEdgeOnV(0), getLEdgeOnV(1) };
5928 if ( e[0]->Is( _LayerEdge::NORMAL_UPDATED ))
5929 _leOnV[0]._normal = getNormalNormal( e[0]->_normal, _edgeDir[0] );
5930 if ( e[1]->Is( _LayerEdge::NORMAL_UPDATED ))
5931 _leOnV[1]._normal = getNormalNormal( e[1]->_normal, _edgeDir[1] );
5932 _leOnV[0]._len = e[0]->_len;
5933 _leOnV[1]._len = e[1]->_len;
5934 for ( size_t i = 0; i < _offPoints.size(); i++ )
5936 _LayerEdge* e0 = _offPoints[i]._2edges._edges[0];
5937 _LayerEdge* e1 = _offPoints[i]._2edges._edges[1];
5938 const double w0 = _offPoints[i]._2edges._wgt[0];
5939 const double w1 = _offPoints[i]._2edges._wgt[1];
5940 gp_XYZ avgNorm = ( e0->_normal * w0 + e1->_normal * w1 ).Normalized();
5941 double avgLen = ( e0->_len * w0 + e1->_len * w1 );
5942 double avgFact = ( e0->_lenFactor * w0 + e1->_lenFactor * w1 );
5943 if ( e0->Is( _LayerEdge::NORMAL_UPDATED ) ||
5944 e1->Is( _LayerEdge::NORMAL_UPDATED ))
5945 avgNorm = getNormalNormal( avgNorm, _offPoints[i]._edgeDir );
5947 _offPoints[i]._xyz += avgNorm * ( avgLen - _offPoints[i]._len ) * avgFact;
5948 _offPoints[i]._len = avgLen;
5952 if ( !surface.IsNull() ) // project _offPoints to the FACE
5954 fTol = 100 * BRep_Tool::Tolerance( F );
5955 //const double segLen = _offPoints[0].Distance( _offPoints[1] );
5957 gp_Pnt2d uv = surface->ValueOfUV( _offPoints[0]._xyz, fTol );
5958 //if ( surface->Gap() < 0.5 * segLen )
5959 _offPoints[0]._xyz = surface->Value( uv ).XYZ();
5961 for ( size_t i = 1; i < _offPoints.size(); ++i )
5963 uv = surface->NextValueOfUV( uv, _offPoints[i]._xyz, fTol );
5964 //if ( surface->Gap() < 0.5 * segLen )
5965 _offPoints[i]._xyz = surface->Value( uv ).XYZ();
5969 // -----------------------------------------------------------------
5970 // project tgt nodes of extreme _LayerEdge's to the offset segments
5971 // -----------------------------------------------------------------
5973 const int updatedOrBlocked = _LayerEdge::NORMAL_UPDATED | _LayerEdge::BLOCKED;
5974 if ( e[0]->Is( updatedOrBlocked )) _iSeg[0] = 0;
5975 if ( e[1]->Is( updatedOrBlocked )) _iSeg[1] = _offPoints.size()-2;
5977 gp_Pnt pExtreme[2], pProj[2];
5978 bool isProjected[2];
5979 for ( int is2nd = 0; is2nd < 2; ++is2nd )
5981 pExtreme[ is2nd ] = SMESH_TNodeXYZ( e[is2nd]->_nodes.back() );
5982 int i = _iSeg[ is2nd ];
5983 int di = is2nd ? -1 : +1;
5984 bool & projected = isProjected[ is2nd ];
5986 double uOnSeg, distMin = Precision::Infinite(), dist, distPrev = 0;
5989 gp_Vec v0p( _offPoints[i]._xyz, pExtreme[ is2nd ] );
5990 gp_Vec v01( _offPoints[i]._xyz, _offPoints[i+1]._xyz );
5991 uOnSeg = ( v0p * v01 ) / v01.SquareMagnitude(); // param [0,1] along v01
5992 projected = ( Abs( uOnSeg - 0.5 ) <= 0.5 );
5993 dist = pExtreme[ is2nd ].SquareDistance( _offPoints[ i + ( uOnSeg > 0.5 )]._xyz );
5994 if ( dist < distMin || projected )
5997 pProj[ is2nd ] = _offPoints[i]._xyz + ( v01 * uOnSeg ).XYZ();
6000 else if ( dist > distPrev )
6002 if ( ++nbWorse > 3 ) // avoid projection to the middle of a closed EDGE
6008 while ( !projected &&
6009 i >= 0 && i+1 < (int)_offPoints.size() );
6013 if (( is2nd && _iSeg[1] != _offPoints.size()-2 ) || ( !is2nd && _iSeg[0] != 0 ))
6016 _iSeg[1] = _offPoints.size()-2;
6017 debugMsg( "smoothComplexEdge() failed to project nodes of extreme _LayerEdge's" );
6022 if ( _iSeg[0] > _iSeg[1] )
6024 debugMsg( "smoothComplexEdge() incorrectly projected nodes of extreme _LayerEdge's" );
6028 // adjust length of extreme LE (test viscous_layers_01/B7)
6029 gp_Vec vDiv0( pExtreme[0], pProj[0] );
6030 gp_Vec vDiv1( pExtreme[1], pProj[1] );
6031 double d0 = vDiv0.Magnitude();
6032 double d1 = isProjected[1] ? vDiv1.Magnitude() : 0;
6033 if ( e[0]->Is( _LayerEdge::BLOCKED )) {
6034 if ( e[0]->_normal * vDiv0.XYZ() < 0 ) e[0]->_len += d0;
6035 else e[0]->_len -= d0;
6037 if ( e[1]->Is( _LayerEdge::BLOCKED )) {
6038 if ( e[1]->_normal * vDiv1.XYZ() < 0 ) e[1]->_len += d1;
6039 else e[1]->_len -= d1;
6042 // ---------------------------------------------------------------------------------
6043 // compute normalized length of the offset segments located between the projections
6044 // ---------------------------------------------------------------------------------
6046 // temporary replace extreme _offPoints by pExtreme
6047 gp_XYZ opXYZ[2] = { _offPoints[ _iSeg[0] ]._xyz,
6048 _offPoints[ _iSeg[1]+1 ]._xyz };
6049 _offPoints[ _iSeg[0] ]._xyz = pExtreme[0].XYZ();
6050 _offPoints[ _iSeg[1]+ 1]._xyz = pExtreme[1].XYZ();
6052 size_t iSeg = 0, nbSeg = _iSeg[1] - _iSeg[0] + 1;
6053 vector< double > len( nbSeg + 1 );
6055 len[ iSeg++ ] = pProj[ 0 ].Distance( _offPoints[ _iSeg[0]+1 ]._xyz );
6056 for ( size_t i = _iSeg[0]+1; i <= _iSeg[1]; ++i, ++iSeg )
6058 len[ iSeg ] = len[ iSeg-1 ] + _offPoints[i].Distance( _offPoints[i+1] );
6060 // if ( isProjected[ 1 ])
6061 // len[ nbSeg ] -= pProj[ 1 ].Distance( _offPoints[ _iSeg[1]+1 ]._xyz );
6063 // len[ nbSeg ] += pExtreme[ 1 ].Distance( _offPoints[ _iSeg[1]+1 ]._xyz );
6065 double fullLen = len.back() - d0 - d1;
6066 for ( iSeg = 0; iSeg < len.size(); ++iSeg )
6067 len[iSeg] = ( len[iSeg] - d0 ) / fullLen;
6069 // -------------------------------------------------------------
6070 // distribute tgt nodes of _LayerEdge's between the projections
6071 // -------------------------------------------------------------
6074 for ( size_t i = 0; i < _eos.size(); ++i )
6076 if ( _eos[i]->Is( _LayerEdge::BLOCKED )) continue;
6077 //if ( !_eos[i]->Is( _LayerEdge::TO_SMOOTH )) continue;
6078 while ( iSeg+2 < len.size() && _leParams[i] > len[ iSeg+1 ] )
6080 double r = ( _leParams[i] - len[ iSeg ]) / ( len[ iSeg+1 ] - len[ iSeg ]);
6081 gp_XYZ p = ( _offPoints[ iSeg + _iSeg[0] ]._xyz * ( 1 - r ) +
6082 _offPoints[ iSeg + _iSeg[0] + 1 ]._xyz * r );
6084 if ( surface.IsNull() )
6086 _eos[i]->_pos.back() = p;
6088 else // project a new node position to a FACE
6090 gp_Pnt2d uv ( _eos[i]->_pos.back().X(), _eos[i]->_pos.back().Y() );
6091 gp_Pnt2d uv2( surface->NextValueOfUV( uv, p, fTol ));
6093 p = surface->Value( uv2 ).XYZ();
6094 _eos[i]->_pos.back().SetCoord( uv2.X(), uv2.Y(), 0 );
6096 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _eos[i]->_nodes.back() );
6097 tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
6098 dumpMove( tgtNode );
6101 _offPoints[ _iSeg[0] ]._xyz = opXYZ[0];
6102 _offPoints[ _iSeg[1]+1 ]._xyz = opXYZ[1];
6107 //================================================================================
6109 * \brief Prepare for smoothing
6111 //================================================================================
6113 void _Smoother1D::prepare(_SolidData& data)
6115 const TopoDS_Edge& E = TopoDS::Edge( _eos._shape );
6116 _curveLen = SMESH_Algo::EdgeLength( E );
6118 // sort _LayerEdge's by position on the EDGE
6119 data.SortOnEdge( E, _eos._edges );
6121 // compute normalized param of _eos._edges on EDGE
6122 _leParams.resize( _eos._edges.size() + 1 );
6125 gp_Pnt pPrev = SMESH_TNodeXYZ( getLEdgeOnV( 0 )->_nodes[0] );
6127 for ( size_t i = 0; i < _eos._edges.size(); ++i )
6129 gp_Pnt p = SMESH_TNodeXYZ( _eos._edges[i]->_nodes[0] );
6130 curLen = p.Distance( pPrev );
6131 _leParams[i+1] = _leParams[i] + curLen;
6134 double fullLen = _leParams.back() + pPrev.Distance( SMESH_TNodeXYZ( getLEdgeOnV(1)->_nodes[0]));
6135 for ( size_t i = 0; i < _leParams.size()-1; ++i )
6136 _leParams[i] = _leParams[i+1] / fullLen;
6137 _leParams.back() = 1.;
6140 _LayerEdge* leOnV[2] = { getLEdgeOnV(0), getLEdgeOnV(1) };
6142 // get cosin to use in findEdgesToSmooth()
6143 _edgeDir[0] = getEdgeDir( E, leOnV[0]->_nodes[0], data.GetHelper() );
6144 _edgeDir[1] = getEdgeDir( E, leOnV[1]->_nodes[0], data.GetHelper() );
6145 _leOnV[0]._cosin = Abs( leOnV[0]->_cosin );
6146 _leOnV[1]._cosin = Abs( leOnV[1]->_cosin );
6147 if ( _eos._sWOL.IsNull() ) // 3D
6148 for ( int iEnd = 0; iEnd < 2; ++iEnd )
6149 _leOnV[iEnd]._cosin = Abs( _edgeDir[iEnd].Normalized() * leOnV[iEnd]->_normal );
6154 // divide E to have offset segments with low deflection
6155 BRepAdaptor_Curve c3dAdaptor( E );
6156 const double curDeflect = 0.1; //0.01; // Curvature deflection == |p1p2]*sin(p1p2,p1pM)
6157 const double angDeflect = 0.1; //0.09; // Angular deflection == sin(p1pM,pMp2)
6158 GCPnts_TangentialDeflection discret(c3dAdaptor, angDeflect, curDeflect);
6159 if ( discret.NbPoints() <= 2 )
6161 _anaCurve = new Geom_Line( gp::OX() ); // only type does matter
6165 const double u0 = c3dAdaptor.FirstParameter();
6166 gp_Pnt p; gp_Vec tangent;
6167 if ( discret.NbPoints() >= (int) _eos.size() + 2 )
6169 _offPoints.resize( discret.NbPoints() );
6170 for ( size_t i = 0; i < _offPoints.size(); i++ )
6172 double u = discret.Parameter( i+1 );
6173 c3dAdaptor.D1( u, p, tangent );
6174 _offPoints[i]._xyz = p.XYZ();
6175 _offPoints[i]._edgeDir = tangent.XYZ();
6176 _offPoints[i]._param = GCPnts_AbscissaPoint::Length( c3dAdaptor, u0, u ) / _curveLen;
6181 std::vector< double > params( _eos.size() + 2 );
6183 params[0] = data.GetHelper().GetNodeU( E, leOnV[0]->_nodes[0] );
6184 params.back() = data.GetHelper().GetNodeU( E, leOnV[1]->_nodes[0] );
6185 for ( size_t i = 0; i < _eos.size(); i++ )
6186 params[i+1] = data.GetHelper().GetNodeU( E, _eos[i]->_nodes[0] );
6188 if ( params[1] > params[ _eos.size() ] )
6189 std::reverse( params.begin() + 1, params.end() - 1 );
6191 _offPoints.resize( _eos.size() + 2 );
6192 for ( size_t i = 0; i < _offPoints.size(); i++ )
6194 const double u = params[i];
6195 c3dAdaptor.D1( u, p, tangent );
6196 _offPoints[i]._xyz = p.XYZ();
6197 _offPoints[i]._edgeDir = tangent.XYZ();
6198 _offPoints[i]._param = GCPnts_AbscissaPoint::Length( c3dAdaptor, u0, u ) / _curveLen;
6203 _offPoints [0]._2edges.set( &_leOnV[0], &_leOnV[0], 0.5, 0.5 );
6204 _offPoints.back()._2edges.set( &_leOnV[1], &_leOnV[1], 0.5, 0.5 );
6205 _2NearEdges tmp2edges;
6206 tmp2edges._edges[1] = _eos._edges[0];
6207 _leOnV[0]._2neibors = & tmp2edges;
6208 _leOnV[0]._nodes = leOnV[0]->_nodes;
6209 _leOnV[1]._nodes = leOnV[1]->_nodes;
6210 _LayerEdge* eNext, *ePrev = & _leOnV[0];
6211 for ( size_t iLE = 0, i = 1; i < _offPoints.size()-1; i++ )
6213 // find _LayerEdge's located before and after an offset point
6214 // (_eos._edges[ iLE ] is next after ePrev)
6215 while ( iLE < _eos._edges.size() && _offPoints[i]._param > _leParams[ iLE ] )
6216 ePrev = _eos._edges[ iLE++ ];
6217 eNext = ePrev->_2neibors->_edges[1];
6219 gp_Pnt p0 = SMESH_TNodeXYZ( ePrev->_nodes[0] );
6220 gp_Pnt p1 = SMESH_TNodeXYZ( eNext->_nodes[0] );
6221 double r = p0.Distance( _offPoints[i]._xyz ) / p0.Distance( p1 );
6222 _offPoints[i]._2edges.set( ePrev, eNext, 1-r, r );
6225 // replace _LayerEdge's on VERTEX by _leOnV in _offPoints._2edges
6226 for ( size_t i = 0; i < _offPoints.size(); i++ )
6227 if ( _offPoints[i]._2edges._edges[0] == leOnV[0] )
6228 _offPoints[i]._2edges._edges[0] = & _leOnV[0];
6230 for ( size_t i = _offPoints.size()-1; i > 0; i-- )
6231 if ( _offPoints[i]._2edges._edges[1] == leOnV[1] )
6232 _offPoints[i]._2edges._edges[1] = & _leOnV[1];
6235 // set _normal of _leOnV[0] and _leOnV[1] to be normal to the EDGE
6237 int iLBO = _offPoints.size() - 2; // last but one
6239 if ( leOnV[ 0 ]->Is( _LayerEdge::MULTI_NORMAL ))
6240 _leOnV[ 0 ]._normal = getNormalNormal( _eos._edges[1]->_normal, _edgeDir[0] );
6242 _leOnV[ 0 ]._normal = getNormalNormal( leOnV[0]->_normal, _edgeDir[0] );
6243 if ( leOnV[ 1 ]->Is( _LayerEdge::MULTI_NORMAL ))
6244 _leOnV[ 1 ]._normal = getNormalNormal( _eos._edges.back()->_normal, _edgeDir[1] );
6246 _leOnV[ 1 ]._normal = getNormalNormal( leOnV[1]->_normal, _edgeDir[1] );
6247 _leOnV[ 0 ]._len = 0;
6248 _leOnV[ 1 ]._len = 0;
6249 _leOnV[ 0 ]._lenFactor = _offPoints[1 ]._2edges._edges[1]->_lenFactor;
6250 _leOnV[ 1 ]._lenFactor = _offPoints[iLBO]._2edges._edges[0]->_lenFactor;
6253 _iSeg[1] = _offPoints.size()-2;
6255 // initialize OffPnt::_len
6256 for ( size_t i = 0; i < _offPoints.size(); ++i )
6257 _offPoints[i]._len = 0;
6259 if ( _eos._edges[0]->NbSteps() > 1 ) // already inflated several times, init _xyz
6261 _leOnV[0]._len = leOnV[0]->_len;
6262 _leOnV[1]._len = leOnV[1]->_len;
6263 for ( size_t i = 0; i < _offPoints.size(); i++ )
6265 _LayerEdge* e0 = _offPoints[i]._2edges._edges[0];
6266 _LayerEdge* e1 = _offPoints[i]._2edges._edges[1];
6267 const double w0 = _offPoints[i]._2edges._wgt[0];
6268 const double w1 = _offPoints[i]._2edges._wgt[1];
6269 double avgLen = ( e0->_len * w0 + e1->_len * w1 );
6270 gp_XYZ avgXYZ = ( SMESH_TNodeXYZ( e0->_nodes.back() ) * w0 +
6271 SMESH_TNodeXYZ( e1->_nodes.back() ) * w1 );
6272 _offPoints[i]._xyz = avgXYZ;
6273 _offPoints[i]._len = avgLen;
6278 //================================================================================
6280 * \brief return _normal of _leOnV[is2nd] normal to the EDGE
6282 //================================================================================
6284 gp_XYZ _Smoother1D::getNormalNormal( const gp_XYZ & normal,
6285 const gp_XYZ& edgeDir)
6287 gp_XYZ cross = normal ^ edgeDir;
6288 gp_XYZ norm = edgeDir ^ cross;
6289 double size = norm.Modulus();
6291 // if ( size == 0 ) // MULTI_NORMAL _LayerEdge
6292 // return gp_XYZ( 1e-100, 1e-100, 1e-100 );
6297 //================================================================================
6299 * \brief Writes a script creating a mesh composed of _offPoints
6301 //================================================================================
6303 void _Smoother1D::offPointsToPython() const
6305 const char* fname = "/tmp/offPoints.py";
6306 cout << "execfile('"<<fname<<"')"<<endl;
6308 py << "import SMESH" << endl
6309 << "from salome.smesh import smeshBuilder" << endl
6310 << "smesh = smeshBuilder.New(salome.myStudy)" << endl
6311 << "mesh = smesh.Mesh( 'offPoints' )"<<endl;
6312 for ( size_t i = 0; i < _offPoints.size(); i++ )
6314 py << "mesh.AddNode( "
6315 << _offPoints[i]._xyz.X() << ", "
6316 << _offPoints[i]._xyz.Y() << ", "
6317 << _offPoints[i]._xyz.Z() << " )" << endl;
6321 //================================================================================
6323 * \brief Sort _LayerEdge's by a parameter on a given EDGE
6325 //================================================================================
6327 void _SolidData::SortOnEdge( const TopoDS_Edge& E,
6328 vector< _LayerEdge* >& edges)
6330 map< double, _LayerEdge* > u2edge;
6331 for ( size_t i = 0; i < edges.size(); ++i )
6332 u2edge.insert( u2edge.end(),
6333 make_pair( _helper->GetNodeU( E, edges[i]->_nodes[0] ), edges[i] ));
6335 ASSERT( u2edge.size() == edges.size() );
6336 map< double, _LayerEdge* >::iterator u2e = u2edge.begin();
6337 for ( size_t i = 0; i < edges.size(); ++i, ++u2e )
6338 edges[i] = u2e->second;
6340 Sort2NeiborsOnEdge( edges );
6343 //================================================================================
6345 * \brief Set _2neibors according to the order of _LayerEdge on EDGE
6347 //================================================================================
6349 void _SolidData::Sort2NeiborsOnEdge( vector< _LayerEdge* >& edges )
6351 if ( edges.size() < 2 || !edges[0]->_2neibors ) return;
6353 for ( size_t i = 0; i < edges.size()-1; ++i )
6354 if ( edges[i]->_2neibors->tgtNode(1) != edges[i+1]->_nodes.back() )
6355 edges[i]->_2neibors->reverse();
6357 const size_t iLast = edges.size() - 1;
6358 if ( edges.size() > 1 &&
6359 edges[iLast]->_2neibors->tgtNode(0) != edges[iLast-1]->_nodes.back() )
6360 edges[iLast]->_2neibors->reverse();
6363 //================================================================================
6365 * \brief Return _EdgesOnShape* corresponding to the shape
6367 //================================================================================
6369 _EdgesOnShape* _SolidData::GetShapeEdges(const TGeomID shapeID )
6371 if ( shapeID < (int)_edgesOnShape.size() &&
6372 _edgesOnShape[ shapeID ]._shapeID == shapeID )
6373 return _edgesOnShape[ shapeID ]._subMesh ? & _edgesOnShape[ shapeID ] : 0;
6375 for ( size_t i = 0; i < _edgesOnShape.size(); ++i )
6376 if ( _edgesOnShape[i]._shapeID == shapeID )
6377 return _edgesOnShape[i]._subMesh ? & _edgesOnShape[i] : 0;
6382 //================================================================================
6384 * \brief Return _EdgesOnShape* corresponding to the shape
6386 //================================================================================
6388 _EdgesOnShape* _SolidData::GetShapeEdges(const TopoDS_Shape& shape )
6390 SMESHDS_Mesh* meshDS = _proxyMesh->GetMesh()->GetMeshDS();
6391 return GetShapeEdges( meshDS->ShapeToIndex( shape ));
6394 //================================================================================
6396 * \brief Prepare data of the _LayerEdge for smoothing on FACE
6398 //================================================================================
6400 void _SolidData::PrepareEdgesToSmoothOnFace( _EdgesOnShape* eos, bool substituteSrcNodes )
6402 SMESH_MesherHelper helper( *_proxyMesh->GetMesh() );
6404 set< TGeomID > vertices;
6406 if ( eos->ShapeType() == TopAbs_FACE )
6408 // check FACE concavity and get concave VERTEXes
6409 F = TopoDS::Face( eos->_shape );
6410 if ( isConcave( F, helper, &vertices ))
6411 _concaveFaces.insert( eos->_shapeID );
6413 // set eos._eosConcaVer
6414 eos->_eosConcaVer.clear();
6415 eos->_eosConcaVer.reserve( vertices.size() );
6416 for ( set< TGeomID >::iterator v = vertices.begin(); v != vertices.end(); ++v )
6418 _EdgesOnShape* eov = GetShapeEdges( *v );
6419 if ( eov && eov->_edges.size() == 1 )
6421 eos->_eosConcaVer.push_back( eov );
6422 for ( size_t i = 0; i < eov->_edges[0]->_neibors.size(); ++i )
6423 eov->_edges[0]->_neibors[i]->Set( _LayerEdge::DIFFICULT );
6427 // SetSmooLen() to _LayerEdge's on FACE
6428 // for ( size_t i = 0; i < eos->_edges.size(); ++i )
6430 // eos->_edges[i]->SetSmooLen( Precision::Infinite() );
6432 // SMESH_subMeshIteratorPtr smIt = eos->_subMesh->getDependsOnIterator(/*includeSelf=*/false);
6433 // while ( smIt->more() ) // loop on sub-shapes of the FACE
6435 // _EdgesOnShape* eoe = GetShapeEdges( smIt->next()->GetId() );
6436 // if ( !eoe ) continue;
6438 // vector<_LayerEdge*>& eE = eoe->_edges;
6439 // for ( size_t iE = 0; iE < eE.size(); ++iE ) // loop on _LayerEdge's on EDGE or VERTEX
6441 // if ( eE[iE]->_cosin <= theMinSmoothCosin )
6444 // SMDS_ElemIteratorPtr segIt = eE[iE]->_nodes[0]->GetInverseElementIterator(SMDSAbs_Edge);
6445 // while ( segIt->more() )
6447 // const SMDS_MeshElement* seg = segIt->next();
6448 // if ( !eos->_subMesh->DependsOn( seg->getshapeId() ))
6450 // if ( seg->GetNode(0) != eE[iE]->_nodes[0] )
6451 // continue; // not to check a seg twice
6452 // for ( size_t iN = 0; iN < eE[iE]->_neibors.size(); ++iN )
6454 // _LayerEdge* eN = eE[iE]->_neibors[iN];
6455 // if ( eN->_nodes[0]->getshapeId() != eos->_shapeID )
6457 // double dist = SMESH_MeshAlgos::GetDistance( seg, SMESH_TNodeXYZ( eN->_nodes[0] ));
6458 // double smooLen = getSmoothingThickness( eE[iE]->_cosin, dist );
6459 // eN->SetSmooLen( Min( smooLen, eN->GetSmooLen() ));
6460 // eN->Set( _LayerEdge::NEAR_BOUNDARY );
6465 } // if ( eos->ShapeType() == TopAbs_FACE )
6467 for ( size_t i = 0; i < eos->_edges.size(); ++i )
6469 eos->_edges[i]->_smooFunction = 0;
6470 eos->_edges[i]->Set( _LayerEdge::TO_SMOOTH );
6472 bool isCurved = false;
6473 for ( size_t i = 0; i < eos->_edges.size(); ++i )
6475 _LayerEdge* edge = eos->_edges[i];
6477 // get simplices sorted
6478 _Simplex::SortSimplices( edge->_simplices );
6480 // smoothing function
6481 edge->ChooseSmooFunction( vertices, _n2eMap );
6484 double avgNormProj = 0, avgLen = 0;
6485 for ( size_t iS = 0; iS < edge->_simplices.size(); ++iS )
6487 _Simplex& s = edge->_simplices[iS];
6489 gp_XYZ vec = edge->_pos.back() - SMESH_TNodeXYZ( s._nPrev );
6490 avgNormProj += edge->_normal * vec;
6491 avgLen += vec.Modulus();
6492 if ( substituteSrcNodes )
6494 s._nNext = _n2eMap[ s._nNext ]->_nodes.back();
6495 s._nPrev = _n2eMap[ s._nPrev ]->_nodes.back();
6498 avgNormProj /= edge->_simplices.size();
6499 avgLen /= edge->_simplices.size();
6500 if (( edge->_curvature = _Curvature::New( avgNormProj, avgLen )))
6502 edge->Set( _LayerEdge::SMOOTHED_C1 );
6504 SMDS_FacePosition* fPos = dynamic_cast<SMDS_FacePosition*>( edge->_nodes[0]->GetPosition() );
6506 for ( size_t iS = 0; iS < edge->_simplices.size() && !fPos; ++iS )
6507 fPos = dynamic_cast<SMDS_FacePosition*>( edge->_simplices[iS]._nPrev->GetPosition() );
6509 edge->_curvature->_uv.SetCoord( fPos->GetUParameter(), fPos->GetVParameter() );
6513 // prepare for putOnOffsetSurface()
6514 if (( eos->ShapeType() == TopAbs_FACE ) &&
6515 ( isCurved || !eos->_eosConcaVer.empty() ))
6517 eos->_offsetSurf = helper.GetSurface( TopoDS::Face( eos->_shape ));
6518 eos->_edgeForOffset = 0;
6520 double maxCosin = -1;
6521 for ( TopExp_Explorer eExp( eos->_shape, TopAbs_EDGE ); eExp.More(); eExp.Next() )
6523 _EdgesOnShape* eoe = GetShapeEdges( eExp.Current() );
6524 if ( !eoe || eoe->_edges.empty() ) continue;
6526 vector<_LayerEdge*>& eE = eoe->_edges;
6527 _LayerEdge* e = eE[ eE.size() / 2 ];
6528 if ( e->_cosin > maxCosin )
6530 eos->_edgeForOffset = e;
6531 maxCosin = e->_cosin;
6537 //================================================================================
6539 * \brief Add faces for smoothing
6541 //================================================================================
6543 void _SolidData::AddShapesToSmooth( const set< _EdgesOnShape* >& eosToSmooth,
6544 const set< _EdgesOnShape* >* edgesNoAnaSmooth )
6546 set< _EdgesOnShape * >::const_iterator eos = eosToSmooth.begin();
6547 for ( ; eos != eosToSmooth.end(); ++eos )
6549 if ( !*eos || (*eos)->_toSmooth ) continue;
6551 (*eos)->_toSmooth = true;
6553 if ( (*eos)->ShapeType() == TopAbs_FACE )
6555 PrepareEdgesToSmoothOnFace( *eos, /*substituteSrcNodes=*/false );
6556 (*eos)->_toSmooth = true;
6560 // avoid _Smoother1D::smoothAnalyticEdge() of edgesNoAnaSmooth
6561 if ( edgesNoAnaSmooth )
6562 for ( eos = edgesNoAnaSmooth->begin(); eos != edgesNoAnaSmooth->end(); ++eos )
6564 if ( (*eos)->_edgeSmoother )
6565 (*eos)->_edgeSmoother->_anaCurve.Nullify();
6569 //================================================================================
6571 * \brief Limit _LayerEdge::_maxLen according to local curvature
6573 //================================================================================
6575 void _ViscousBuilder::limitMaxLenByCurvature( _SolidData& data, SMESH_MesherHelper& helper )
6577 // find intersection of neighbor _LayerEdge's to limit _maxLen
6578 // according to local curvature (IPAL52648)
6580 // This method must be called after findCollisionEdges() where _LayerEdge's
6581 // get _lenFactor initialized in the case of eos._hyp.IsOffsetMethod()
6583 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
6585 _EdgesOnShape& eosI = data._edgesOnShape[iS];
6586 if ( eosI._edges.empty() ) continue;
6587 if ( !eosI._hyp.ToSmooth() )
6589 for ( size_t i = 0; i < eosI._edges.size(); ++i )
6591 _LayerEdge* eI = eosI._edges[i];
6592 for ( size_t iN = 0; iN < eI->_neibors.size(); ++iN )
6594 _LayerEdge* eN = eI->_neibors[iN];
6595 if ( eI->_nodes[0]->GetID() < eN->_nodes[0]->GetID() ) // treat this pair once
6597 _EdgesOnShape* eosN = data.GetShapeEdges( eN );
6598 limitMaxLenByCurvature( eI, eN, eosI, *eosN, eosI._hyp.ToSmooth() );
6603 else if ( eosI.ShapeType() == TopAbs_EDGE )
6605 const TopoDS_Edge& E = TopoDS::Edge( eosI._shape );
6606 if ( SMESH_Algo::IsStraight( E, /*degenResult=*/true )) continue;
6608 _LayerEdge* e0 = eosI._edges[0];
6609 for ( size_t i = 1; i < eosI._edges.size(); ++i )
6611 _LayerEdge* eI = eosI._edges[i];
6612 limitMaxLenByCurvature( eI, e0, eosI, eosI, eosI._hyp.ToSmooth() );
6619 //================================================================================
6621 * \brief Limit _LayerEdge::_maxLen according to local curvature
6623 //================================================================================
6625 void _ViscousBuilder::limitMaxLenByCurvature( _LayerEdge* e1,
6627 _EdgesOnShape& eos1,
6628 _EdgesOnShape& eos2,
6629 const bool isSmoothable )
6631 if (( e1->_nodes[0]->GetPosition()->GetDim() !=
6632 e2->_nodes[0]->GetPosition()->GetDim() ) &&
6633 ( e1->_cosin < 0.75 ))
6634 return; // angle > 90 deg at e1
6636 gp_XYZ plnNorm = e1->_normal ^ e2->_normal;
6637 double norSize = plnNorm.SquareModulus();
6638 if ( norSize < std::numeric_limits<double>::min() )
6639 return; // parallel normals
6641 // find closest points of skew _LayerEdge's
6642 SMESH_TNodeXYZ src1( e1->_nodes[0] ), src2( e2->_nodes[0] );
6643 gp_XYZ dir12 = src2 - src1;
6644 gp_XYZ perp1 = e1->_normal ^ plnNorm;
6645 gp_XYZ perp2 = e2->_normal ^ plnNorm;
6646 double dot1 = perp2 * e1->_normal;
6647 double dot2 = perp1 * e2->_normal;
6648 double u1 = ( perp2 * dir12 ) / dot1;
6649 double u2 = - ( perp1 * dir12 ) / dot2;
6650 if ( u1 > 0 && u2 > 0 )
6652 double ovl = ( u1 * e1->_normal * dir12 -
6653 u2 * e2->_normal * dir12 ) / dir12.SquareModulus();
6654 if ( ovl > theSmoothThickToElemSizeRatio )
6656 const double coef = 0.75;
6657 e1->SetMaxLen( Min( e1->_maxLen, coef * u1 / e1->_lenFactor ));
6658 e2->SetMaxLen( Min( e2->_maxLen, coef * u2 / e2->_lenFactor ));
6663 //================================================================================
6665 * \brief Fill data._collisionEdges
6667 //================================================================================
6669 void _ViscousBuilder::findCollisionEdges( _SolidData& data, SMESH_MesherHelper& helper )
6671 data._collisionEdges.clear();
6673 // set the full thickness of the layers to LEs
6674 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
6676 _EdgesOnShape& eos = data._edgesOnShape[iS];
6677 if ( eos._edges.empty() ) continue;
6678 if ( eos.ShapeType() != TopAbs_EDGE && eos.ShapeType() != TopAbs_VERTEX ) continue;
6680 for ( size_t i = 0; i < eos._edges.size(); ++i )
6682 if ( eos._edges[i]->Is( _LayerEdge::BLOCKED )) continue;
6683 double maxLen = eos._edges[i]->_maxLen;
6684 eos._edges[i]->_maxLen = Precision::Infinite(); // avoid blocking
6685 eos._edges[i]->SetNewLength( 1.5 * maxLen, eos, helper );
6686 eos._edges[i]->_maxLen = maxLen;
6690 // make temporary quadrangles got by extrusion of
6691 // mesh edges along _LayerEdge._normal's
6693 vector< const SMDS_MeshElement* > tmpFaces;
6695 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
6697 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
6698 if ( eos.ShapeType() != TopAbs_EDGE )
6700 if ( eos._edges.empty() )
6702 _LayerEdge* edge[2] = { 0, 0 }; // LE of 2 VERTEX'es
6703 SMESH_subMeshIteratorPtr smIt = eos._subMesh->getDependsOnIterator(/*includeSelf=*/false);
6704 while ( smIt->more() )
6705 if ( _EdgesOnShape* eov = data.GetShapeEdges( smIt->next()->GetId() ))
6706 if ( eov->_edges.size() == 1 )
6707 edge[ bool( edge[0]) ] = eov->_edges[0];
6711 _TmpMeshFaceOnEdge* f = new _TmpMeshFaceOnEdge( edge[0], edge[1], --_tmpFaceID );
6712 tmpFaces.push_back( f );
6715 for ( size_t i = 0; i < eos._edges.size(); ++i )
6717 _LayerEdge* edge = eos._edges[i];
6718 for ( int j = 0; j < 2; ++j ) // loop on _2NearEdges
6720 const SMDS_MeshNode* src2 = edge->_2neibors->srcNode(j);
6721 if ( src2->GetPosition()->GetDim() > 0 &&
6722 src2->GetID() < edge->_nodes[0]->GetID() )
6723 continue; // avoid using same segment twice
6725 // a _LayerEdge containg tgt2
6726 _LayerEdge* neiborEdge = edge->_2neibors->_edges[j];
6728 _TmpMeshFaceOnEdge* f = new _TmpMeshFaceOnEdge( edge, neiborEdge, --_tmpFaceID );
6729 tmpFaces.push_back( f );
6734 // Find _LayerEdge's intersecting tmpFaces.
6736 SMDS_ElemIteratorPtr fIt( new SMDS_ElementVectorIterator( tmpFaces.begin(),
6738 SMESHUtils::Deleter<SMESH_ElementSearcher> searcher
6739 ( SMESH_MeshAlgos::GetElementSearcher( *getMeshDS(), fIt ));
6741 double dist1, dist2, segLen, eps = 0.5;
6742 _CollisionEdges collEdges;
6743 vector< const SMDS_MeshElement* > suspectFaces;
6744 const double angle45 = Cos( 45. * M_PI / 180. );
6746 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
6748 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
6749 if ( eos.ShapeType() == TopAbs_FACE || !eos._sWOL.IsNull() )
6751 // find sub-shapes whose VL can influence VL on eos
6752 set< TGeomID > neighborShapes;
6753 PShapeIteratorPtr fIt = helper.GetAncestors( eos._shape, *_mesh, TopAbs_FACE );
6754 while ( const TopoDS_Shape* face = fIt->next() )
6756 TGeomID faceID = getMeshDS()->ShapeToIndex( *face );
6757 if ( _EdgesOnShape* eof = data.GetShapeEdges( faceID ))
6759 SMESH_subMeshIteratorPtr subIt = eof->_subMesh->getDependsOnIterator(/*includeSelf=*/false);
6760 while ( subIt->more() )
6761 neighborShapes.insert( subIt->next()->GetId() );
6764 if ( eos.ShapeType() == TopAbs_VERTEX )
6766 PShapeIteratorPtr eIt = helper.GetAncestors( eos._shape, *_mesh, TopAbs_EDGE );
6767 while ( const TopoDS_Shape* edge = eIt->next() )
6768 neighborShapes.erase( getMeshDS()->ShapeToIndex( *edge ));
6770 // find intersecting _LayerEdge's
6771 for ( size_t i = 0; i < eos._edges.size(); ++i )
6773 if ( eos._edges[i]->Is( _LayerEdge::MULTI_NORMAL )) continue;
6774 _LayerEdge* edge = eos._edges[i];
6775 gp_Ax1 lastSegment = edge->LastSegment( segLen, eos );
6778 gp_Vec eSegDir0, eSegDir1;
6779 if ( edge->IsOnEdge() )
6781 SMESH_TNodeXYZ eP( edge->_nodes[0] );
6782 eSegDir0 = SMESH_TNodeXYZ( edge->_2neibors->srcNode(0) ) - eP;
6783 eSegDir1 = SMESH_TNodeXYZ( edge->_2neibors->srcNode(1) ) - eP;
6785 suspectFaces.clear();
6786 searcher->GetElementsInSphere( SMESH_TNodeXYZ( edge->_nodes.back()), edge->_len * 2,
6787 SMDSAbs_Face, suspectFaces );
6788 collEdges._intEdges.clear();
6789 for ( size_t j = 0 ; j < suspectFaces.size(); ++j )
6791 const _TmpMeshFaceOnEdge* f = (const _TmpMeshFaceOnEdge*) suspectFaces[j];
6792 if ( f->_le1 == edge || f->_le2 == edge ) continue;
6793 if ( !neighborShapes.count( f->_le1->_nodes[0]->getshapeId() )) continue;
6794 if ( !neighborShapes.count( f->_le2->_nodes[0]->getshapeId() )) continue;
6795 if ( edge->IsOnEdge() ) {
6796 if ( edge->_2neibors->include( f->_le1 ) ||
6797 edge->_2neibors->include( f->_le2 )) continue;
6800 if (( f->_le1->IsOnEdge() && f->_le1->_2neibors->include( edge )) ||
6801 ( f->_le2->IsOnEdge() && f->_le2->_2neibors->include( edge ))) continue;
6803 dist1 = dist2 = Precision::Infinite();
6804 if ( !edge->SegTriaInter( lastSegment, f->_nn[0], f->_nn[1], f->_nn[2], dist1, eps ))
6805 dist1 = Precision::Infinite();
6806 if ( !edge->SegTriaInter( lastSegment, f->_nn[3], f->_nn[2], f->_nn[0], dist2, eps ))
6807 dist2 = Precision::Infinite();
6808 if (( dist1 > segLen ) && ( dist2 > segLen ))
6811 if ( edge->IsOnEdge() )
6813 // skip perpendicular EDGEs
6814 gp_Vec fSegDir = SMESH_TNodeXYZ( f->_nn[0] ) - SMESH_TNodeXYZ( f->_nn[3] );
6815 bool isParallel = ( isLessAngle( eSegDir0, fSegDir, angle45 ) ||
6816 isLessAngle( eSegDir1, fSegDir, angle45 ) ||
6817 isLessAngle( eSegDir0, fSegDir.Reversed(), angle45 ) ||
6818 isLessAngle( eSegDir1, fSegDir.Reversed(), angle45 ));
6823 // either limit inflation of edges or remember them for updating _normal
6824 // double dot = edge->_normal * f->GetDir();
6827 collEdges._intEdges.push_back( f->_le1 );
6828 collEdges._intEdges.push_back( f->_le2 );
6832 // double shortLen = 0.75 * ( Min( dist1, dist2 ) / edge->_lenFactor );
6833 // edge->SetMaxLen( Min( shortLen, edge->_maxLen ));
6837 if ( !collEdges._intEdges.empty() )
6839 collEdges._edge = edge;
6840 data._collisionEdges.push_back( collEdges );
6845 for ( size_t i = 0 ; i < tmpFaces.size(); ++i )
6848 // restore the zero thickness
6849 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
6851 _EdgesOnShape& eos = data._edgesOnShape[iS];
6852 if ( eos._edges.empty() ) continue;
6853 if ( eos.ShapeType() != TopAbs_EDGE && eos.ShapeType() != TopAbs_VERTEX ) continue;
6855 for ( size_t i = 0; i < eos._edges.size(); ++i )
6857 eos._edges[i]->InvalidateStep( 1, eos );
6858 eos._edges[i]->_len = 0;
6863 //================================================================================
6865 * \brief Find _LayerEdge's located on boundary of a convex FACE whose normal
6866 * will be updated at each inflation step
6868 //================================================================================
6870 void _ViscousBuilder::findEdgesToUpdateNormalNearConvexFace( _ConvexFace & convFace,
6872 SMESH_MesherHelper& helper )
6874 const TGeomID convFaceID = getMeshDS()->ShapeToIndex( convFace._face );
6875 const double preci = BRep_Tool::Tolerance( convFace._face );
6876 Handle(ShapeAnalysis_Surface) surface = helper.GetSurface( convFace._face );
6878 bool edgesToUpdateFound = false;
6880 map< TGeomID, _EdgesOnShape* >::iterator id2eos = convFace._subIdToEOS.begin();
6881 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
6883 _EdgesOnShape& eos = * id2eos->second;
6884 if ( !eos._sWOL.IsNull() ) continue;
6885 if ( !eos._hyp.ToSmooth() ) continue;
6886 for ( size_t i = 0; i < eos._edges.size(); ++i )
6888 _LayerEdge* ledge = eos._edges[ i ];
6889 if ( ledge->Is( _LayerEdge::UPD_NORMAL_CONV )) continue; // already checked
6890 if ( ledge->Is( _LayerEdge::MULTI_NORMAL )) continue; // not inflatable
6892 gp_XYZ tgtPos = ( SMESH_NodeXYZ( ledge->_nodes[0] ) +
6893 ledge->_normal * ledge->_lenFactor * ledge->_maxLen );
6895 // the normal must be updated if distance from tgtPos to surface is less than
6898 // find an initial UV for search of a projection of tgtPos to surface
6899 const SMDS_MeshNode* nodeInFace = 0;
6900 SMDS_ElemIteratorPtr fIt = ledge->_nodes[0]->GetInverseElementIterator(SMDSAbs_Face);
6901 while ( fIt->more() && !nodeInFace )
6903 const SMDS_MeshElement* f = fIt->next();
6904 if ( convFaceID != f->getshapeId() ) continue;
6906 SMDS_ElemIteratorPtr nIt = f->nodesIterator();
6907 while ( nIt->more() && !nodeInFace )
6909 const SMDS_MeshElement* n = nIt->next();
6910 if ( n->getshapeId() == convFaceID )
6911 nodeInFace = static_cast< const SMDS_MeshNode* >( n );
6916 gp_XY uv = helper.GetNodeUV( convFace._face, nodeInFace );
6919 surface->NextValueOfUV( uv, tgtPos, preci );
6920 double dist = surface->Gap();
6921 if ( dist < 0.95 * ledge->_maxLen )
6923 ledge->Set( _LayerEdge::UPD_NORMAL_CONV );
6924 if ( !ledge->_curvature ) ledge->_curvature = new _Curvature;
6925 ledge->_curvature->_uv.SetCoord( uv.X(), uv.Y() );
6926 edgesToUpdateFound = true;
6931 if ( !convFace._isTooCurved && edgesToUpdateFound )
6933 data._convexFaces.insert( make_pair( convFaceID, convFace )).first->second;
6937 //================================================================================
6939 * \brief Modify normals of _LayerEdge's on EDGE's to avoid intersection with
6940 * _LayerEdge's on neighbor EDGE's
6942 //================================================================================
6944 bool _ViscousBuilder::updateNormals( _SolidData& data,
6945 SMESH_MesherHelper& helper,
6949 updateNormalsOfC1Vertices( data );
6951 if ( stepNb > 0 && !updateNormalsOfConvexFaces( data, helper, stepNb ))
6954 // map to store new _normal and _cosin for each intersected edge
6955 map< _LayerEdge*, _LayerEdge, _LayerEdgeCmp > edge2newEdge;
6956 map< _LayerEdge*, _LayerEdge, _LayerEdgeCmp >::iterator e2neIt;
6957 _LayerEdge zeroEdge;
6958 zeroEdge._normal.SetCoord( 0,0,0 );
6959 zeroEdge._maxLen = Precision::Infinite();
6960 zeroEdge._nodes.resize(1); // to init _TmpMeshFaceOnEdge
6962 set< _EdgesOnShape* > shapesToSmooth, edgesNoAnaSmooth;
6964 double segLen, dist1, dist2, dist;
6965 vector< pair< _LayerEdge*, double > > intEdgesDist;
6966 _TmpMeshFaceOnEdge quad( &zeroEdge, &zeroEdge, 0 );
6968 for ( int iter = 0; iter < 5; ++iter )
6970 edge2newEdge.clear();
6972 for ( size_t iE = 0; iE < data._collisionEdges.size(); ++iE )
6974 _CollisionEdges& ce = data._collisionEdges[iE];
6975 _LayerEdge* edge1 = ce._edge;
6976 if ( !edge1 /*|| edge1->Is( _LayerEdge::BLOCKED )*/) continue;
6977 _EdgesOnShape* eos1 = data.GetShapeEdges( edge1 );
6978 if ( !eos1 ) continue;
6980 // detect intersections
6981 gp_Ax1 lastSeg = edge1->LastSegment( segLen, *eos1 );
6982 double testLen = 1.5 * edge1->_maxLen * edge1->_lenFactor;
6984 intEdgesDist.clear();
6985 double minIntDist = Precision::Infinite();
6986 for ( size_t i = 0; i < ce._intEdges.size(); i += 2 )
6988 if ( edge1->Is( _LayerEdge::BLOCKED ) &&
6989 ce._intEdges[i ]->Is( _LayerEdge::BLOCKED ) &&
6990 ce._intEdges[i+1]->Is( _LayerEdge::BLOCKED ))
6992 double dot = edge1->_normal * quad.GetDir( ce._intEdges[i], ce._intEdges[i+1] );
6993 double fact = ( 1.1 + dot * dot );
6994 SMESH_TNodeXYZ pSrc0( ce.nSrc(i) ), pSrc1( ce.nSrc(i+1) );
6995 SMESH_TNodeXYZ pTgt0( ce.nTgt(i) ), pTgt1( ce.nTgt(i+1) );
6996 gp_XYZ pLast0 = pSrc0 + ( pTgt0 - pSrc0 ) * fact;
6997 gp_XYZ pLast1 = pSrc1 + ( pTgt1 - pSrc1 ) * fact;
6998 dist1 = dist2 = Precision::Infinite();
6999 if ( !edge1->SegTriaInter( lastSeg, pSrc0, pLast0, pSrc1, dist1, eps ) &&
7000 !edge1->SegTriaInter( lastSeg, pSrc1, pLast1, pLast0, dist2, eps ))
7003 if ( dist > testLen || dist <= 0 )
7006 if ( dist > testLen || dist <= 0 )
7009 // choose a closest edge
7010 gp_Pnt intP( lastSeg.Location().XYZ() + lastSeg.Direction().XYZ() * ( dist + segLen ));
7011 double d1 = intP.SquareDistance( pSrc0 );
7012 double d2 = intP.SquareDistance( pSrc1 );
7013 int iClose = i + ( d2 < d1 );
7014 _LayerEdge* edge2 = ce._intEdges[iClose];
7015 edge2->Unset( _LayerEdge::MARKED );
7017 // choose a closest edge among neighbors
7018 gp_Pnt srcP( SMESH_TNodeXYZ( edge1->_nodes[0] ));
7019 d1 = srcP.SquareDistance( SMESH_TNodeXYZ( edge2->_nodes[0] ));
7020 for ( size_t j = 0; j < intEdgesDist.size(); ++j )
7022 _LayerEdge * edgeJ = intEdgesDist[j].first;
7023 if ( edge2->IsNeiborOnEdge( edgeJ ))
7025 d2 = srcP.SquareDistance( SMESH_TNodeXYZ( edgeJ->_nodes[0] ));
7026 ( d1 < d2 ? edgeJ : edge2 )->Set( _LayerEdge::MARKED );
7029 intEdgesDist.push_back( make_pair( edge2, dist ));
7030 // if ( Abs( d2 - d1 ) / Max( d2, d1 ) < 0.5 )
7032 // iClose = i + !( d2 < d1 );
7033 // intEdges.push_back( ce._intEdges[iClose] );
7034 // ce._intEdges[iClose]->Unset( _LayerEdge::MARKED );
7036 minIntDist = Min( edge1->_len * edge1->_lenFactor - segLen + dist, minIntDist );
7041 // compute new _normals
7042 for ( size_t i = 0; i < intEdgesDist.size(); ++i )
7044 _LayerEdge* edge2 = intEdgesDist[i].first;
7045 double distWgt = edge1->_len / intEdgesDist[i].second;
7046 // if ( edge1->Is( _LayerEdge::BLOCKED ) &&
7047 // edge2->Is( _LayerEdge::BLOCKED )) continue;
7048 if ( edge2->Is( _LayerEdge::MARKED )) continue;
7049 edge2->Set( _LayerEdge::MARKED );
7052 gp_XYZ dir1 = edge1->_normal, dir2 = edge2->_normal;
7054 double cos1 = Abs( edge1->_cosin ), cos2 = Abs( edge2->_cosin );
7055 double wgt1 = ( cos1 + 0.001 ) / ( cos1 + cos2 + 0.002 );
7056 double wgt2 = ( cos2 + 0.001 ) / ( cos1 + cos2 + 0.002 );
7057 // double cos1 = Abs( edge1->_cosin ), cos2 = Abs( edge2->_cosin );
7058 // double sgn1 = 0.1 * ( 1 + edge1->_cosin ), sgn2 = 0.1 * ( 1 + edge2->_cosin );
7059 // double wgt1 = ( cos1 + sgn1 ) / ( cos1 + cos2 + sgn1 + sgn2 );
7060 // double wgt2 = ( cos2 + sgn2 ) / ( cos1 + cos2 + sgn1 + sgn2 );
7061 gp_XYZ newNormal = wgt1 * dir1 + wgt2 * dir2;
7062 newNormal.Normalize();
7066 double sgn1 = edge1->_cosin / cos1, sgn2 = edge2->_cosin / cos2;
7067 if ( cos1 < theMinSmoothCosin )
7069 newCos = cos2 * sgn1;
7071 else if ( cos2 > theMinSmoothCosin ) // both cos1 and cos2 > theMinSmoothCosin
7073 newCos = ( wgt1 * cos1 + wgt2 * cos2 ) * edge1->_cosin / cos1;
7077 newCos = edge1->_cosin;
7080 e2neIt = edge2newEdge.insert( make_pair( edge1, zeroEdge )).first;
7081 e2neIt->second._normal += distWgt * newNormal;
7082 e2neIt->second._cosin = newCos;
7083 e2neIt->second.SetMaxLen( 0.7 * minIntDist / edge1->_lenFactor );
7084 if ( iter > 0 && sgn1 * sgn2 < 0 && edge1->_cosin < 0 )
7085 e2neIt->second._normal += dir2;
7087 e2neIt = edge2newEdge.insert( make_pair( edge2, zeroEdge )).first;
7088 e2neIt->second._normal += distWgt * newNormal;
7089 if ( Precision::IsInfinite( zeroEdge._maxLen ))
7091 e2neIt->second._cosin = edge2->_cosin;
7092 e2neIt->second.SetMaxLen( 1.3 * minIntDist / edge1->_lenFactor );
7094 if ( iter > 0 && sgn1 * sgn2 < 0 && edge2->_cosin < 0 )
7095 e2neIt->second._normal += dir1;
7099 if ( edge2newEdge.empty() )
7100 break; //return true;
7102 dumpFunction(SMESH_Comment("updateNormals")<< data._index << "_" << stepNb << "_it" << iter);
7104 // Update data of edges depending on a new _normal
7107 for ( e2neIt = edge2newEdge.begin(); e2neIt != edge2newEdge.end(); ++e2neIt )
7109 _LayerEdge* edge = e2neIt->first;
7110 _LayerEdge& newEdge = e2neIt->second;
7111 _EdgesOnShape* eos = data.GetShapeEdges( edge );
7112 if ( edge->Is( _LayerEdge::BLOCKED && newEdge._maxLen > edge->_len ))
7115 // Check if a new _normal is OK:
7116 newEdge._normal.Normalize();
7117 if ( !isNewNormalOk( data, *edge, newEdge._normal ))
7119 if ( newEdge._maxLen < edge->_len && iter > 0 ) // limit _maxLen
7121 edge->InvalidateStep( stepNb + 1, *eos, /*restoreLength=*/true );
7122 edge->SetMaxLen( newEdge._maxLen );
7123 edge->SetNewLength( newEdge._maxLen, *eos, helper );
7125 continue; // the new _normal is bad
7127 // the new _normal is OK
7129 // find shapes that need smoothing due to change of _normal
7130 if ( edge->_cosin < theMinSmoothCosin &&
7131 newEdge._cosin > theMinSmoothCosin )
7133 if ( eos->_sWOL.IsNull() )
7135 SMDS_ElemIteratorPtr fIt = edge->_nodes[0]->GetInverseElementIterator(SMDSAbs_Face);
7136 while ( fIt->more() )
7137 shapesToSmooth.insert( data.GetShapeEdges( fIt->next()->getshapeId() ));
7139 else // edge inflates along a FACE
7141 TopoDS_Shape V = helper.GetSubShapeByNode( edge->_nodes[0], getMeshDS() );
7142 PShapeIteratorPtr eIt = helper.GetAncestors( V, *_mesh, TopAbs_EDGE, &eos->_sWOL );
7143 while ( const TopoDS_Shape* E = eIt->next() )
7145 gp_Vec edgeDir = getEdgeDir( TopoDS::Edge( *E ), TopoDS::Vertex( V ));
7146 double angle = edgeDir.Angle( newEdge._normal ); // [0,PI]
7147 if ( angle < M_PI / 2 )
7148 shapesToSmooth.insert( data.GetShapeEdges( *E ));
7153 double len = edge->_len;
7154 edge->InvalidateStep( stepNb + 1, *eos, /*restoreLength=*/true );
7155 edge->SetNormal( newEdge._normal );
7156 edge->SetCosin( newEdge._cosin );
7157 edge->SetNewLength( len, *eos, helper );
7158 edge->Set( _LayerEdge::MARKED );
7159 edge->Set( _LayerEdge::NORMAL_UPDATED );
7160 edgesNoAnaSmooth.insert( eos );
7163 // Update normals and other dependent data of not intersecting _LayerEdge's
7164 // neighboring the intersecting ones
7166 for ( e2neIt = edge2newEdge.begin(); e2neIt != edge2newEdge.end(); ++e2neIt )
7168 _LayerEdge* edge1 = e2neIt->first;
7169 _EdgesOnShape* eos1 = data.GetShapeEdges( edge1 );
7170 if ( !edge1->Is( _LayerEdge::MARKED ))
7173 if ( edge1->IsOnEdge() )
7175 const SMDS_MeshNode * n1 = edge1->_2neibors->srcNode(0);
7176 const SMDS_MeshNode * n2 = edge1->_2neibors->srcNode(1);
7177 edge1->SetDataByNeighbors( n1, n2, *eos1, helper );
7180 if ( !edge1->_2neibors || !eos1->_sWOL.IsNull() )
7182 for ( int j = 0; j < 2; ++j ) // loop on 2 neighbors
7184 _LayerEdge* neighbor = edge1->_2neibors->_edges[j];
7185 if ( neighbor->Is( _LayerEdge::MARKED ) /*edge2newEdge.count ( neighbor )*/)
7186 continue; // j-th neighbor is also intersected
7187 _LayerEdge* prevEdge = edge1;
7188 const int nbSteps = 10;
7189 for ( int step = nbSteps; step; --step ) // step from edge1 in j-th direction
7191 if ( neighbor->Is( _LayerEdge::BLOCKED ) ||
7192 neighbor->Is( _LayerEdge::MARKED ))
7194 _EdgesOnShape* eos = data.GetShapeEdges( neighbor );
7195 if ( !eos ) continue;
7196 _LayerEdge* nextEdge = neighbor;
7197 if ( neighbor->_2neibors )
7200 nextEdge = neighbor->_2neibors->_edges[iNext];
7201 if ( nextEdge == prevEdge )
7202 nextEdge = neighbor->_2neibors->_edges[ ++iNext ];
7204 double r = double(step-1)/nbSteps/(iter+1);
7205 if ( !nextEdge->_2neibors )
7208 gp_XYZ newNorm = prevEdge->_normal * r + nextEdge->_normal * (1-r);
7209 newNorm.Normalize();
7210 if ( !isNewNormalOk( data, *neighbor, newNorm ))
7213 double len = neighbor->_len;
7214 neighbor->InvalidateStep( stepNb + 1, *eos, /*restoreLength=*/true );
7215 neighbor->SetNormal( newNorm );
7216 neighbor->SetCosin( prevEdge->_cosin * r + nextEdge->_cosin * (1-r) );
7217 if ( neighbor->_2neibors )
7218 neighbor->SetDataByNeighbors( prevEdge->_nodes[0], nextEdge->_nodes[0], *eos, helper );
7219 neighbor->SetNewLength( len, *eos, helper );
7220 neighbor->Set( _LayerEdge::MARKED );
7221 neighbor->Set( _LayerEdge::NORMAL_UPDATED );
7222 edgesNoAnaSmooth.insert( eos );
7224 if ( !neighbor->_2neibors )
7225 break; // neighbor is on VERTEX
7227 // goto the next neighbor
7228 prevEdge = neighbor;
7229 neighbor = nextEdge;
7236 data.AddShapesToSmooth( shapesToSmooth, &edgesNoAnaSmooth );
7241 //================================================================================
7243 * \brief Check if a new normal is OK
7245 //================================================================================
7247 bool _ViscousBuilder::isNewNormalOk( _SolidData& data,
7249 const gp_XYZ& newNormal)
7251 // check a min angle between the newNormal and surrounding faces
7252 vector<_Simplex> simplices;
7253 SMESH_TNodeXYZ n0( edge._nodes[0] ), n1, n2;
7254 _Simplex::GetSimplices( n0._node, simplices, data._ignoreFaceIds, &data );
7255 double newMinDot = 1, curMinDot = 1;
7256 for ( size_t i = 0; i < simplices.size(); ++i )
7258 n1.Set( simplices[i]._nPrev );
7259 n2.Set( simplices[i]._nNext );
7260 gp_XYZ normFace = ( n1 - n0 ) ^ ( n2 - n0 );
7261 double normLen2 = normFace.SquareModulus();
7262 if ( normLen2 < std::numeric_limits<double>::min() )
7264 normFace /= Sqrt( normLen2 );
7265 newMinDot = Min( newNormal * normFace, newMinDot );
7266 curMinDot = Min( edge._normal * normFace, curMinDot );
7269 if ( newMinDot < 0.5 )
7271 ok = ( newMinDot >= curMinDot * 0.9 );
7272 //return ( newMinDot >= ( curMinDot * ( 0.8 + 0.1 * edge.NbSteps() )));
7273 // double initMinDot2 = 1. - edge._cosin * edge._cosin;
7274 // return ( newMinDot * newMinDot ) >= ( 0.8 * initMinDot2 );
7280 //================================================================================
7282 * \brief Modify normals of _LayerEdge's on FACE to reflex smoothing
7284 //================================================================================
7286 bool _ViscousBuilder::updateNormalsOfSmoothed( _SolidData& data,
7287 SMESH_MesherHelper& helper,
7289 const double stepSize )
7291 if ( data._nbShapesToSmooth == 0 || nbSteps == 0 )
7292 return true; // no shapes needing smoothing
7294 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
7296 _EdgesOnShape& eos = data._edgesOnShape[ iS ];
7297 if ( //!eos._toSmooth || _eosC1 have _toSmooth == false
7298 !eos._hyp.ToSmooth() ||
7299 eos.ShapeType() != TopAbs_FACE ||
7300 eos._edges.empty() )
7303 bool toSmooth = ( eos._edges[ 0 ]->NbSteps() >= nbSteps+1 );
7304 if ( !toSmooth ) continue;
7306 for ( size_t i = 0; i < eos._edges.size(); ++i )
7308 _LayerEdge* edge = eos._edges[i];
7309 if ( !edge->Is( _LayerEdge::SMOOTHED ))
7311 if ( edge->Is( _LayerEdge::DIFFICULT ) && nbSteps != 1 )
7314 const gp_XYZ& pPrev = edge->PrevPos();
7315 const gp_XYZ& pLast = edge->_pos.back();
7316 gp_XYZ stepVec = pLast - pPrev;
7317 double realStepSize = stepVec.Modulus();
7318 if ( realStepSize < numeric_limits<double>::min() )
7321 edge->_lenFactor = realStepSize / stepSize;
7322 edge->_normal = stepVec / realStepSize;
7323 edge->Set( _LayerEdge::NORMAL_UPDATED );
7330 //================================================================================
7332 * \brief Modify normals of _LayerEdge's on C1 VERTEXes
7334 //================================================================================
7336 void _ViscousBuilder::updateNormalsOfC1Vertices( _SolidData& data )
7338 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
7340 _EdgesOnShape& eov = data._edgesOnShape[ iS ];
7341 if ( eov._eosC1.empty() ||
7342 eov.ShapeType() != TopAbs_VERTEX ||
7343 eov._edges.empty() )
7346 gp_XYZ newNorm = eov._edges[0]->_normal;
7347 double curThick = eov._edges[0]->_len * eov._edges[0]->_lenFactor;
7348 bool normChanged = false;
7350 for ( size_t i = 0; i < eov._eosC1.size(); ++i )
7352 _EdgesOnShape* eoe = eov._eosC1[i];
7353 const TopoDS_Edge& e = TopoDS::Edge( eoe->_shape );
7354 const double eLen = SMESH_Algo::EdgeLength( e );
7355 TopoDS_Shape oppV = SMESH_MesherHelper::IthVertex( 0, e );
7356 if ( oppV.IsSame( eov._shape ))
7357 oppV = SMESH_MesherHelper::IthVertex( 1, e );
7358 _EdgesOnShape* eovOpp = data.GetShapeEdges( oppV );
7359 if ( !eovOpp || eovOpp->_edges.empty() ) continue;
7360 if ( eov._edges[0]->Is( _LayerEdge::BLOCKED )) continue;
7362 double curThickOpp = eovOpp->_edges[0]->_len * eovOpp->_edges[0]->_lenFactor;
7363 if ( curThickOpp + curThick < eLen )
7366 double wgt = 2. * curThick / eLen;
7367 newNorm += wgt * eovOpp->_edges[0]->_normal;
7372 eov._edges[0]->SetNormal( newNorm.Normalized() );
7373 eov._edges[0]->Set( _LayerEdge::NORMAL_UPDATED );
7378 //================================================================================
7380 * \brief Modify normals of _LayerEdge's on _ConvexFace's
7382 //================================================================================
7384 bool _ViscousBuilder::updateNormalsOfConvexFaces( _SolidData& data,
7385 SMESH_MesherHelper& helper,
7388 SMESHDS_Mesh* meshDS = helper.GetMeshDS();
7391 map< TGeomID, _ConvexFace >::iterator id2face = data._convexFaces.begin();
7392 for ( ; id2face != data._convexFaces.end(); ++id2face )
7394 _ConvexFace & convFace = (*id2face).second;
7395 convFace._normalsFixedOnBorders = false; // to update at each inflation step
7397 if ( convFace._normalsFixed )
7398 continue; // already fixed
7399 if ( convFace.CheckPrisms() )
7400 continue; // nothing to fix
7402 convFace._normalsFixed = true;
7404 BRepAdaptor_Surface surface ( convFace._face, false );
7405 BRepLProp_SLProps surfProp( surface, 2, 1e-6 );
7407 // check if the convex FACE is of spherical shape
7409 Bnd_B3d centersBox; // bbox of centers of curvature of _LayerEdge's on VERTEXes
7413 map< TGeomID, _EdgesOnShape* >::iterator id2eos = convFace._subIdToEOS.begin();
7414 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
7416 _EdgesOnShape& eos = *(id2eos->second);
7417 if ( eos.ShapeType() == TopAbs_VERTEX )
7419 _LayerEdge* ledge = eos._edges[ 0 ];
7420 if ( convFace.GetCenterOfCurvature( ledge, surfProp, helper, center ))
7421 centersBox.Add( center );
7423 for ( size_t i = 0; i < eos._edges.size(); ++i )
7424 nodesBox.Add( SMESH_TNodeXYZ( eos._edges[ i ]->_nodes[0] ));
7426 if ( centersBox.IsVoid() )
7428 debugMsg( "Error: centersBox.IsVoid()" );
7431 const bool isSpherical =
7432 ( centersBox.SquareExtent() < 1e-6 * nodesBox.SquareExtent() );
7434 int nbEdges = helper.Count( convFace._face, TopAbs_EDGE, /*ignoreSame=*/false );
7435 vector < _CentralCurveOnEdge > centerCurves( nbEdges );
7439 // set _LayerEdge::_normal as average of all normals
7441 // WARNING: different density of nodes on EDGEs is not taken into account that
7442 // can lead to an improper new normal
7444 gp_XYZ avgNormal( 0,0,0 );
7446 id2eos = convFace._subIdToEOS.begin();
7447 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
7449 _EdgesOnShape& eos = *(id2eos->second);
7450 // set data of _CentralCurveOnEdge
7451 if ( eos.ShapeType() == TopAbs_EDGE )
7453 _CentralCurveOnEdge& ceCurve = centerCurves[ nbEdges++ ];
7454 ceCurve.SetShapes( TopoDS::Edge( eos._shape ), convFace, data, helper );
7455 if ( !eos._sWOL.IsNull() )
7456 ceCurve._adjFace.Nullify();
7458 ceCurve._ledges.insert( ceCurve._ledges.end(),
7459 eos._edges.begin(), eos._edges.end());
7461 // summarize normals
7462 for ( size_t i = 0; i < eos._edges.size(); ++i )
7463 avgNormal += eos._edges[ i ]->_normal;
7465 double normSize = avgNormal.SquareModulus();
7466 if ( normSize < 1e-200 )
7468 debugMsg( "updateNormalsOfConvexFaces(): zero avgNormal" );
7471 avgNormal /= Sqrt( normSize );
7473 // compute new _LayerEdge::_cosin on EDGEs
7474 double avgCosin = 0;
7477 for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
7479 _CentralCurveOnEdge& ceCurve = centerCurves[ iE ];
7480 if ( ceCurve._adjFace.IsNull() )
7482 for ( size_t iLE = 0; iLE < ceCurve._ledges.size(); ++iLE )
7484 const SMDS_MeshNode* node = ceCurve._ledges[ iLE ]->_nodes[0];
7485 inFaceDir = getFaceDir( ceCurve._adjFace, ceCurve._edge, node, helper, isOK );
7488 double angle = inFaceDir.Angle( avgNormal ); // [0,PI]
7489 ceCurve._ledges[ iLE ]->_cosin = Cos( angle );
7490 avgCosin += ceCurve._ledges[ iLE ]->_cosin;
7496 avgCosin /= nbCosin;
7498 // set _LayerEdge::_normal = avgNormal
7499 id2eos = convFace._subIdToEOS.begin();
7500 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
7502 _EdgesOnShape& eos = *(id2eos->second);
7503 if ( eos.ShapeType() != TopAbs_EDGE )
7504 for ( size_t i = 0; i < eos._edges.size(); ++i )
7505 eos._edges[ i ]->_cosin = avgCosin;
7507 for ( size_t i = 0; i < eos._edges.size(); ++i )
7509 eos._edges[ i ]->SetNormal( avgNormal );
7510 eos._edges[ i ]->Set( _LayerEdge::NORMAL_UPDATED );
7514 else // if ( isSpherical )
7516 // We suppose that centers of curvature at all points of the FACE
7517 // lie on some curve, let's call it "central curve". For all _LayerEdge's
7518 // having a common center of curvature we define the same new normal
7519 // as a sum of normals of _LayerEdge's on EDGEs among them.
7521 // get all centers of curvature for each EDGE
7523 helper.SetSubShape( convFace._face );
7524 _LayerEdge* vertexLEdges[2], **edgeLEdge, **edgeLEdgeEnd;
7526 TopExp_Explorer edgeExp( convFace._face, TopAbs_EDGE );
7527 for ( int iE = 0; edgeExp.More(); edgeExp.Next(), ++iE )
7529 const TopoDS_Edge& edge = TopoDS::Edge( edgeExp.Current() );
7531 // set adjacent FACE
7532 centerCurves[ iE ].SetShapes( edge, convFace, data, helper );
7534 // get _LayerEdge's of the EDGE
7535 TGeomID edgeID = meshDS->ShapeToIndex( edge );
7536 _EdgesOnShape* eos = data.GetShapeEdges( edgeID );
7537 if ( !eos || eos->_edges.empty() )
7539 // no _LayerEdge's on EDGE, use _LayerEdge's on VERTEXes
7540 for ( int iV = 0; iV < 2; ++iV )
7542 TopoDS_Vertex v = helper.IthVertex( iV, edge );
7543 TGeomID vID = meshDS->ShapeToIndex( v );
7544 eos = data.GetShapeEdges( vID );
7545 vertexLEdges[ iV ] = eos->_edges[ 0 ];
7547 edgeLEdge = &vertexLEdges[0];
7548 edgeLEdgeEnd = edgeLEdge + 2;
7550 centerCurves[ iE ]._adjFace.Nullify();
7554 if ( ! eos->_toSmooth )
7555 data.SortOnEdge( edge, eos->_edges );
7556 edgeLEdge = &eos->_edges[ 0 ];
7557 edgeLEdgeEnd = edgeLEdge + eos->_edges.size();
7558 vertexLEdges[0] = eos->_edges.front()->_2neibors->_edges[0];
7559 vertexLEdges[1] = eos->_edges.back() ->_2neibors->_edges[1];
7561 if ( ! eos->_sWOL.IsNull() )
7562 centerCurves[ iE ]._adjFace.Nullify();
7565 // Get curvature centers
7569 if ( edgeLEdge[0]->IsOnEdge() &&
7570 convFace.GetCenterOfCurvature( vertexLEdges[0], surfProp, helper, center ))
7572 centerCurves[ iE ].Append( center, vertexLEdges[0] );
7573 centersBox.Add( center );
7575 for ( ; edgeLEdge < edgeLEdgeEnd; ++edgeLEdge )
7576 if ( convFace.GetCenterOfCurvature( *edgeLEdge, surfProp, helper, center ))
7577 { // EDGE or VERTEXes
7578 centerCurves[ iE ].Append( center, *edgeLEdge );
7579 centersBox.Add( center );
7581 if ( edgeLEdge[-1]->IsOnEdge() &&
7582 convFace.GetCenterOfCurvature( vertexLEdges[1], surfProp, helper, center ))
7584 centerCurves[ iE ].Append( center, vertexLEdges[1] );
7585 centersBox.Add( center );
7587 centerCurves[ iE ]._isDegenerated =
7588 ( centersBox.IsVoid() || centersBox.SquareExtent() < 1e-6 * nodesBox.SquareExtent() );
7590 } // loop on EDGES of convFace._face to set up data of centerCurves
7592 // Compute new normals for _LayerEdge's on EDGEs
7594 double avgCosin = 0;
7597 for ( size_t iE1 = 0; iE1 < centerCurves.size(); ++iE1 )
7599 _CentralCurveOnEdge& ceCurve = centerCurves[ iE1 ];
7600 if ( ceCurve._isDegenerated )
7602 const vector< gp_Pnt >& centers = ceCurve._curvaCenters;
7603 vector< gp_XYZ > & newNormals = ceCurve._normals;
7604 for ( size_t iC1 = 0; iC1 < centers.size(); ++iC1 )
7607 for ( size_t iE2 = 0; iE2 < centerCurves.size() && !isOK; ++iE2 )
7610 isOK = centerCurves[ iE2 ].FindNewNormal( centers[ iC1 ], newNormals[ iC1 ]);
7612 if ( isOK && !ceCurve._adjFace.IsNull() )
7614 // compute new _LayerEdge::_cosin
7615 const SMDS_MeshNode* node = ceCurve._ledges[ iC1 ]->_nodes[0];
7616 inFaceDir = getFaceDir( ceCurve._adjFace, ceCurve._edge, node, helper, isOK );
7619 double angle = inFaceDir.Angle( newNormals[ iC1 ] ); // [0,PI]
7620 ceCurve._ledges[ iC1 ]->_cosin = Cos( angle );
7621 avgCosin += ceCurve._ledges[ iC1 ]->_cosin;
7627 // set new normals to _LayerEdge's of NOT degenerated central curves
7628 for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
7630 if ( centerCurves[ iE ]._isDegenerated )
7632 for ( size_t iLE = 0; iLE < centerCurves[ iE ]._ledges.size(); ++iLE )
7634 centerCurves[ iE ]._ledges[ iLE ]->SetNormal( centerCurves[ iE ]._normals[ iLE ]);
7635 centerCurves[ iE ]._ledges[ iLE ]->Set( _LayerEdge::NORMAL_UPDATED );
7638 // set new normals to _LayerEdge's of degenerated central curves
7639 for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
7641 if ( !centerCurves[ iE ]._isDegenerated ||
7642 centerCurves[ iE ]._ledges.size() < 3 )
7644 // new normal is an average of new normals at VERTEXes that
7645 // was computed on non-degenerated _CentralCurveOnEdge's
7646 gp_XYZ newNorm = ( centerCurves[ iE ]._ledges.front()->_normal +
7647 centerCurves[ iE ]._ledges.back ()->_normal );
7648 double sz = newNorm.Modulus();
7652 double newCosin = ( 0.5 * centerCurves[ iE ]._ledges.front()->_cosin +
7653 0.5 * centerCurves[ iE ]._ledges.back ()->_cosin );
7654 for ( size_t iLE = 1, nb = centerCurves[ iE ]._ledges.size() - 1; iLE < nb; ++iLE )
7656 centerCurves[ iE ]._ledges[ iLE ]->SetNormal( newNorm );
7657 centerCurves[ iE ]._ledges[ iLE ]->_cosin = newCosin;
7658 centerCurves[ iE ]._ledges[ iLE ]->Set( _LayerEdge::NORMAL_UPDATED );
7662 // Find new normals for _LayerEdge's based on FACE
7665 avgCosin /= nbCosin;
7666 const TGeomID faceID = meshDS->ShapeToIndex( convFace._face );
7667 map< TGeomID, _EdgesOnShape* >::iterator id2eos = convFace._subIdToEOS.find( faceID );
7668 if ( id2eos != convFace._subIdToEOS.end() )
7672 _EdgesOnShape& eos = * ( id2eos->second );
7673 for ( size_t i = 0; i < eos._edges.size(); ++i )
7675 _LayerEdge* ledge = eos._edges[ i ];
7676 if ( !convFace.GetCenterOfCurvature( ledge, surfProp, helper, center ))
7678 for ( size_t i = 0; i < centerCurves.size(); ++i, ++iE )
7680 iE = iE % centerCurves.size();
7681 if ( centerCurves[ iE ]._isDegenerated )
7683 newNorm.SetCoord( 0,0,0 );
7684 if ( centerCurves[ iE ].FindNewNormal( center, newNorm ))
7686 ledge->SetNormal( newNorm );
7687 ledge->_cosin = avgCosin;
7688 ledge->Set( _LayerEdge::NORMAL_UPDATED );
7695 } // not a quasi-spherical FACE
7697 // Update _LayerEdge's data according to a new normal
7699 dumpFunction(SMESH_Comment("updateNormalsOfConvexFaces")<<data._index
7700 <<"_F"<<meshDS->ShapeToIndex( convFace._face ));
7702 id2eos = convFace._subIdToEOS.begin();
7703 for ( ; id2eos != convFace._subIdToEOS.end(); ++id2eos )
7705 _EdgesOnShape& eos = * ( id2eos->second );
7706 for ( size_t i = 0; i < eos._edges.size(); ++i )
7708 _LayerEdge* & ledge = eos._edges[ i ];
7709 double len = ledge->_len;
7710 ledge->InvalidateStep( stepNb + 1, eos, /*restoreLength=*/true );
7711 ledge->SetCosin( ledge->_cosin );
7712 ledge->SetNewLength( len, eos, helper );
7714 if ( eos.ShapeType() != TopAbs_FACE )
7715 for ( size_t i = 0; i < eos._edges.size(); ++i )
7717 _LayerEdge* ledge = eos._edges[ i ];
7718 for ( size_t iN = 0; iN < ledge->_neibors.size(); ++iN )
7720 _LayerEdge* neibor = ledge->_neibors[iN];
7721 if ( neibor->_nodes[0]->GetPosition()->GetDim() == 2 )
7723 neibor->Set( _LayerEdge::NEAR_BOUNDARY );
7724 neibor->Set( _LayerEdge::MOVED );
7725 neibor->SetSmooLen( neibor->_len );
7729 } // loop on sub-shapes of convFace._face
7731 // Find FACEs adjacent to convFace._face that got necessity to smooth
7732 // as a result of normals modification
7734 set< _EdgesOnShape* > adjFacesToSmooth;
7735 for ( size_t iE = 0; iE < centerCurves.size(); ++iE )
7737 if ( centerCurves[ iE ]._adjFace.IsNull() ||
7738 centerCurves[ iE ]._adjFaceToSmooth )
7740 for ( size_t iLE = 0; iLE < centerCurves[ iE ]._ledges.size(); ++iLE )
7742 if ( centerCurves[ iE ]._ledges[ iLE ]->_cosin > theMinSmoothCosin )
7744 adjFacesToSmooth.insert( data.GetShapeEdges( centerCurves[ iE ]._adjFace ));
7749 data.AddShapesToSmooth( adjFacesToSmooth );
7754 } // loop on data._convexFaces
7759 //================================================================================
7761 * \brief Return max curvature of a FACE
7763 //================================================================================
7765 double _ConvexFace::GetMaxCurvature( _SolidData& data,
7767 BRepLProp_SLProps& surfProp,
7768 SMESH_MesherHelper& helper)
7770 double maxCurvature = 0;
7772 TopoDS_Face F = TopoDS::Face( eof._shape );
7774 const int nbTestPnt = 5;
7775 const double oriFactor = ( F.Orientation() == TopAbs_REVERSED ? +1. : -1. );
7776 SMESH_subMeshIteratorPtr smIt = eof._subMesh->getDependsOnIterator(/*includeSelf=*/true);
7777 while ( smIt->more() )
7779 SMESH_subMesh* sm = smIt->next();
7780 const TGeomID subID = sm->GetId();
7782 // find _LayerEdge's of a sub-shape
7784 if (( eos = data.GetShapeEdges( subID )))
7785 this->_subIdToEOS.insert( make_pair( subID, eos ));
7789 // check concavity and curvature and limit data._stepSize
7790 const double minCurvature =
7791 1. / ( eos->_hyp.GetTotalThickness() * ( 1 + theThickToIntersection ));
7792 size_t iStep = Max( 1, eos->_edges.size() / nbTestPnt );
7793 for ( size_t i = 0; i < eos->_edges.size(); i += iStep )
7795 gp_XY uv = helper.GetNodeUV( F, eos->_edges[ i ]->_nodes[0] );
7796 surfProp.SetParameters( uv.X(), uv.Y() );
7797 if ( surfProp.IsCurvatureDefined() )
7799 double curvature = Max( surfProp.MaxCurvature() * oriFactor,
7800 surfProp.MinCurvature() * oriFactor );
7801 maxCurvature = Max( maxCurvature, curvature );
7803 if ( curvature > minCurvature )
7804 this->_isTooCurved = true;
7807 } // loop on sub-shapes of the FACE
7809 return maxCurvature;
7812 //================================================================================
7814 * \brief Finds a center of curvature of a surface at a _LayerEdge
7816 //================================================================================
7818 bool _ConvexFace::GetCenterOfCurvature( _LayerEdge* ledge,
7819 BRepLProp_SLProps& surfProp,
7820 SMESH_MesherHelper& helper,
7821 gp_Pnt & center ) const
7823 gp_XY uv = helper.GetNodeUV( _face, ledge->_nodes[0] );
7824 surfProp.SetParameters( uv.X(), uv.Y() );
7825 if ( !surfProp.IsCurvatureDefined() )
7828 const double oriFactor = ( _face.Orientation() == TopAbs_REVERSED ? +1. : -1. );
7829 double surfCurvatureMax = surfProp.MaxCurvature() * oriFactor;
7830 double surfCurvatureMin = surfProp.MinCurvature() * oriFactor;
7831 if ( surfCurvatureMin > surfCurvatureMax )
7832 center = surfProp.Value().Translated( surfProp.Normal().XYZ() / surfCurvatureMin * oriFactor );
7834 center = surfProp.Value().Translated( surfProp.Normal().XYZ() / surfCurvatureMax * oriFactor );
7839 //================================================================================
7841 * \brief Check that prisms are not distorted
7843 //================================================================================
7845 bool _ConvexFace::CheckPrisms() const
7848 for ( size_t i = 0; i < _simplexTestEdges.size(); ++i )
7850 const _LayerEdge* edge = _simplexTestEdges[i];
7851 SMESH_TNodeXYZ tgtXYZ( edge->_nodes.back() );
7852 for ( size_t j = 0; j < edge->_simplices.size(); ++j )
7853 if ( !edge->_simplices[j].IsForward( edge->_nodes[0], tgtXYZ, vol ))
7855 debugMsg( "Bad simplex of _simplexTestEdges ("
7856 << " "<< edge->_nodes[0]->GetID()<< " "<< tgtXYZ._node->GetID()
7857 << " "<< edge->_simplices[j]._nPrev->GetID()
7858 << " "<< edge->_simplices[j]._nNext->GetID() << " )" );
7865 //================================================================================
7867 * \brief Try to compute a new normal by interpolating normals of _LayerEdge's
7868 * stored in this _CentralCurveOnEdge.
7869 * \param [in] center - curvature center of a point of another _CentralCurveOnEdge.
7870 * \param [in,out] newNormal - current normal at this point, to be redefined
7871 * \return bool - true if succeeded.
7873 //================================================================================
7875 bool _CentralCurveOnEdge::FindNewNormal( const gp_Pnt& center, gp_XYZ& newNormal )
7877 if ( this->_isDegenerated )
7880 // find two centers the given one lies between
7882 for ( size_t i = 0, nb = _curvaCenters.size()-1; i < nb; ++i )
7884 double sl2 = 1.001 * _segLength2[ i ];
7886 double d1 = center.SquareDistance( _curvaCenters[ i ]);
7890 double d2 = center.SquareDistance( _curvaCenters[ i+1 ]);
7891 if ( d2 > sl2 || d2 + d1 < 1e-100 )
7896 double r = d1 / ( d1 + d2 );
7897 gp_XYZ norm = (( 1. - r ) * _ledges[ i ]->_normal +
7898 ( r ) * _ledges[ i+1 ]->_normal );
7902 double sz = newNormal.Modulus();
7911 //================================================================================
7913 * \brief Set shape members
7915 //================================================================================
7917 void _CentralCurveOnEdge::SetShapes( const TopoDS_Edge& edge,
7918 const _ConvexFace& convFace,
7920 SMESH_MesherHelper& helper)
7924 PShapeIteratorPtr fIt = helper.GetAncestors( edge, *helper.GetMesh(), TopAbs_FACE );
7925 while ( const TopoDS_Shape* F = fIt->next())
7926 if ( !convFace._face.IsSame( *F ))
7928 _adjFace = TopoDS::Face( *F );
7929 _adjFaceToSmooth = false;
7930 // _adjFace already in a smoothing queue ?
7931 if ( _EdgesOnShape* eos = data.GetShapeEdges( _adjFace ))
7932 _adjFaceToSmooth = eos->_toSmooth;
7937 //================================================================================
7939 * \brief Looks for intersection of it's last segment with faces
7940 * \param distance - returns shortest distance from the last node to intersection
7942 //================================================================================
7944 bool _LayerEdge::FindIntersection( SMESH_ElementSearcher& searcher,
7946 const double& epsilon,
7948 const SMDS_MeshElement** intFace)
7950 vector< const SMDS_MeshElement* > suspectFaces;
7952 gp_Ax1 lastSegment = LastSegment( segLen, eos );
7953 searcher.GetElementsNearLine( lastSegment, SMDSAbs_Face, suspectFaces );
7955 bool segmentIntersected = false;
7956 distance = Precision::Infinite();
7957 int iFace = -1; // intersected face
7958 for ( size_t j = 0 ; j < suspectFaces.size() /*&& !segmentIntersected*/; ++j )
7960 const SMDS_MeshElement* face = suspectFaces[j];
7961 if ( face->GetNodeIndex( _nodes.back() ) >= 0 ||
7962 face->GetNodeIndex( _nodes[0] ) >= 0 )
7963 continue; // face sharing _LayerEdge node
7964 const int nbNodes = face->NbCornerNodes();
7965 bool intFound = false;
7967 SMDS_MeshElement::iterator nIt = face->begin_nodes();
7970 intFound = SegTriaInter( lastSegment, *nIt++, *nIt++, *nIt++, dist, epsilon );
7974 const SMDS_MeshNode* tria[3];
7977 for ( int n2 = 2; n2 < nbNodes && !intFound; ++n2 )
7980 intFound = SegTriaInter(lastSegment, tria[0], tria[1], tria[2], dist, epsilon );
7986 if ( dist < segLen*(1.01) && dist > -(_len*_lenFactor-segLen) )
7987 segmentIntersected = true;
7988 if ( distance > dist )
7989 distance = dist, iFace = j;
7992 if ( intFace ) *intFace = ( iFace != -1 ) ? suspectFaces[iFace] : 0;
7996 if ( segmentIntersected )
7999 SMDS_MeshElement::iterator nIt = suspectFaces[iFace]->begin_nodes();
8000 gp_XYZ intP( lastSegment.Location().XYZ() + lastSegment.Direction().XYZ() * ( distance+segLen ));
8001 cout << "nodes: tgt " << _nodes.back()->GetID() << " src " << _nodes[0]->GetID()
8002 << ", intersection with face ("
8003 << (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()<<" "<< (*nIt++)->GetID()
8004 << ") at point (" << intP.X() << ", " << intP.Y() << ", " << intP.Z()
8005 << ") distance = " << distance << endl;
8009 return segmentIntersected;
8012 //================================================================================
8014 * \brief Returns a point used to check orientation of _simplices
8016 //================================================================================
8018 gp_XYZ _LayerEdge::PrevCheckPos( _EdgesOnShape* eos ) const
8020 size_t i = Is( NORMAL_UPDATED ) && IsOnFace() ? _pos.size()-2 : 0;
8022 if ( !eos || eos->_sWOL.IsNull() )
8025 if ( eos->SWOLType() == TopAbs_EDGE )
8027 return BRepAdaptor_Curve( TopoDS::Edge( eos->_sWOL )).Value( _pos[i].X() ).XYZ();
8029 //else // TopAbs_FACE
8031 return BRepAdaptor_Surface( TopoDS::Face( eos->_sWOL )).Value(_pos[i].X(), _pos[i].Y() ).XYZ();
8034 //================================================================================
8036 * \brief Returns size and direction of the last segment
8038 //================================================================================
8040 gp_Ax1 _LayerEdge::LastSegment(double& segLen, _EdgesOnShape& eos) const
8042 // find two non-coincident positions
8043 gp_XYZ orig = _pos.back();
8045 int iPrev = _pos.size() - 2;
8046 //const double tol = ( _len > 0 ) ? 0.3*_len : 1e-100; // adjusted for IPAL52478 + PAL22576
8047 const double tol = ( _len > 0 ) ? ( 1e-6 * _len ) : 1e-100;
8048 while ( iPrev >= 0 )
8050 vec = orig - _pos[iPrev];
8051 if ( vec.SquareModulus() > tol*tol )
8061 segDir.SetLocation( SMESH_TNodeXYZ( _nodes[0] ));
8062 segDir.SetDirection( _normal );
8067 gp_Pnt pPrev = _pos[ iPrev ];
8068 if ( !eos._sWOL.IsNull() )
8070 TopLoc_Location loc;
8071 if ( eos.SWOLType() == TopAbs_EDGE )
8074 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( eos._sWOL ), loc, f,l);
8075 pPrev = curve->Value( pPrev.X() ).Transformed( loc );
8079 Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face( eos._sWOL ), loc );
8080 pPrev = surface->Value( pPrev.X(), pPrev.Y() ).Transformed( loc );
8082 vec = SMESH_TNodeXYZ( _nodes.back() ) - pPrev.XYZ();
8084 segDir.SetLocation( pPrev );
8085 segDir.SetDirection( vec );
8086 segLen = vec.Modulus();
8092 //================================================================================
8094 * \brief Return the last (or \a which) position of the target node on a FACE.
8095 * \param [in] F - the FACE this _LayerEdge is inflated along
8096 * \param [in] which - index of position
8097 * \return gp_XY - result UV
8099 //================================================================================
8101 gp_XY _LayerEdge::LastUV( const TopoDS_Face& F, _EdgesOnShape& eos, int which ) const
8103 if ( F.IsSame( eos._sWOL )) // F is my FACE
8104 return gp_XY( _pos.back().X(), _pos.back().Y() );
8106 if ( eos.SWOLType() != TopAbs_EDGE ) // wrong call
8107 return gp_XY( 1e100, 1e100 );
8109 // _sWOL is EDGE of F; _pos.back().X() is the last U on the EDGE
8110 double f, l, u = _pos[ which < 0 ? _pos.size()-1 : which ].X();
8111 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( TopoDS::Edge(eos._sWOL), F, f,l);
8112 if ( !C2d.IsNull() && f <= u && u <= l )
8113 return C2d->Value( u ).XY();
8115 return gp_XY( 1e100, 1e100 );
8118 //================================================================================
8120 * \brief Test intersection of the last segment with a given triangle
8121 * using Moller-Trumbore algorithm
8122 * Intersection is detected if distance to intersection is less than _LayerEdge._len
8124 //================================================================================
8126 bool _LayerEdge::SegTriaInter( const gp_Ax1& lastSegment,
8127 const gp_XYZ& vert0,
8128 const gp_XYZ& vert1,
8129 const gp_XYZ& vert2,
8131 const double& EPSILON) const
8133 const gp_Pnt& orig = lastSegment.Location();
8134 const gp_Dir& dir = lastSegment.Direction();
8136 /* calculate distance from vert0 to ray origin */
8137 //gp_XYZ tvec = orig.XYZ() - vert0;
8139 //if ( tvec * dir > EPSILON )
8140 // intersected face is at back side of the temporary face this _LayerEdge belongs to
8143 gp_XYZ edge1 = vert1 - vert0;
8144 gp_XYZ edge2 = vert2 - vert0;
8146 /* begin calculating determinant - also used to calculate U parameter */
8147 gp_XYZ pvec = dir.XYZ() ^ edge2;
8149 /* if determinant is near zero, ray lies in plane of triangle */
8150 double det = edge1 * pvec;
8152 const double ANGL_EPSILON = 1e-12;
8153 if ( det > -ANGL_EPSILON && det < ANGL_EPSILON )
8156 /* calculate distance from vert0 to ray origin */
8157 gp_XYZ tvec = orig.XYZ() - vert0;
8159 /* calculate U parameter and test bounds */
8160 double u = ( tvec * pvec ) / det;
8161 //if (u < 0.0 || u > 1.0)
8162 if ( u < -EPSILON || u > 1.0 + EPSILON )
8165 /* prepare to test V parameter */
8166 gp_XYZ qvec = tvec ^ edge1;
8168 /* calculate V parameter and test bounds */
8169 double v = (dir.XYZ() * qvec) / det;
8170 //if ( v < 0.0 || u + v > 1.0 )
8171 if ( v < -EPSILON || u + v > 1.0 + EPSILON )
8174 /* calculate t, ray intersects triangle */
8175 t = (edge2 * qvec) / det;
8181 //================================================================================
8183 * \brief _LayerEdge, located at a concave VERTEX of a FACE, moves target nodes of
8184 * neighbor _LayerEdge's by it's own inflation vector.
8185 * \param [in] eov - EOS of the VERTEX
8186 * \param [in] eos - EOS of the FACE
8187 * \param [in] step - inflation step
8188 * \param [in,out] badSmooEdges - tangled _LayerEdge's
8190 //================================================================================
8192 void _LayerEdge::MoveNearConcaVer( const _EdgesOnShape* eov,
8193 const _EdgesOnShape* eos,
8195 vector< _LayerEdge* > & badSmooEdges )
8197 // check if any of _neibors is in badSmooEdges
8198 if ( std::find_first_of( _neibors.begin(), _neibors.end(),
8199 badSmooEdges.begin(), badSmooEdges.end() ) == _neibors.end() )
8202 // get all edges to move
8204 set< _LayerEdge* > edges;
8206 // find a distance between _LayerEdge on VERTEX and its neighbors
8207 gp_XYZ curPosV = SMESH_TNodeXYZ( _nodes.back() );
8209 for ( size_t i = 0; i < _neibors.size(); ++i )
8211 _LayerEdge* nEdge = _neibors[i];
8212 if ( nEdge->_nodes[0]->getshapeId() == eos->_shapeID )
8214 edges.insert( nEdge );
8215 dist2 = Max( dist2, ( curPosV - nEdge->_pos.back() ).SquareModulus() );
8218 // add _LayerEdge's close to curPosV
8222 for ( set< _LayerEdge* >::iterator e = edges.begin(); e != edges.end(); ++e )
8224 _LayerEdge* edgeF = *e;
8225 for ( size_t i = 0; i < edgeF->_neibors.size(); ++i )
8227 _LayerEdge* nEdge = edgeF->_neibors[i];
8228 if ( nEdge->_nodes[0]->getshapeId() == eos->_shapeID &&
8229 dist2 > ( curPosV - nEdge->_pos.back() ).SquareModulus() )
8230 edges.insert( nEdge );
8234 while ( nbE < edges.size() );
8236 // move the target node of the got edges
8238 gp_XYZ prevPosV = PrevPos();
8239 if ( eov->SWOLType() == TopAbs_EDGE )
8241 BRepAdaptor_Curve curve ( TopoDS::Edge( eov->_sWOL ));
8242 prevPosV = curve.Value( prevPosV.X() ).XYZ();
8244 else if ( eov->SWOLType() == TopAbs_FACE )
8246 BRepAdaptor_Surface surface( TopoDS::Face( eov->_sWOL ));
8247 prevPosV = surface.Value( prevPosV.X(), prevPosV.Y() ).XYZ();
8250 SMDS_FacePosition* fPos;
8251 //double r = 1. - Min( 0.9, step / 10. );
8252 for ( set< _LayerEdge* >::iterator e = edges.begin(); e != edges.end(); ++e )
8254 _LayerEdge* edgeF = *e;
8255 const gp_XYZ prevVF = edgeF->PrevPos() - prevPosV;
8256 const gp_XYZ newPosF = curPosV + prevVF;
8257 SMDS_MeshNode* tgtNodeF = const_cast<SMDS_MeshNode*>( edgeF->_nodes.back() );
8258 tgtNodeF->setXYZ( newPosF.X(), newPosF.Y(), newPosF.Z() );
8259 edgeF->_pos.back() = newPosF;
8260 dumpMoveComm( tgtNodeF, "MoveNearConcaVer" ); // debug
8262 // set _curvature to make edgeF updated by putOnOffsetSurface()
8263 if ( !edgeF->_curvature )
8264 if (( fPos = dynamic_cast<SMDS_FacePosition*>( edgeF->_nodes[0]->GetPosition() )))
8266 edgeF->_curvature = new _Curvature;
8267 edgeF->_curvature->_r = 0;
8268 edgeF->_curvature->_k = 0;
8269 edgeF->_curvature->_h2lenRatio = 0;
8270 edgeF->_curvature->_uv.SetCoord( fPos->GetUParameter(), fPos->GetVParameter() );
8273 // gp_XYZ inflationVec( SMESH_TNodeXYZ( _nodes.back() ) -
8274 // SMESH_TNodeXYZ( _nodes[0] ));
8275 // for ( set< _LayerEdge* >::iterator e = edges.begin(); e != edges.end(); ++e )
8277 // _LayerEdge* edgeF = *e;
8278 // gp_XYZ newPos = SMESH_TNodeXYZ( edgeF->_nodes[0] ) + inflationVec;
8279 // SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( edgeF->_nodes.back() );
8280 // tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
8281 // edgeF->_pos.back() = newPosF;
8282 // dumpMoveComm( tgtNode, "MoveNearConcaVer" ); // debug
8285 // smooth _LayerEdge's around moved nodes
8286 //size_t nbBadBefore = badSmooEdges.size();
8287 for ( set< _LayerEdge* >::iterator e = edges.begin(); e != edges.end(); ++e )
8289 _LayerEdge* edgeF = *e;
8290 for ( size_t j = 0; j < edgeF->_neibors.size(); ++j )
8291 if ( edgeF->_neibors[j]->_nodes[0]->getshapeId() == eos->_shapeID )
8292 //&& !edges.count( edgeF->_neibors[j] ))
8294 _LayerEdge* edgeFN = edgeF->_neibors[j];
8295 edgeFN->Unset( SMOOTHED );
8296 int nbBad = edgeFN->Smooth( step, /*isConcaFace=*/true, /*findBest=*/true );
8299 // gp_XYZ newPos = SMESH_TNodeXYZ( edgeFN->_nodes[0] ) + inflationVec;
8300 // const gp_XYZ& prevPos = edgeFN->_pos[ edgeFN->_pos.size()-2 ];
8301 // int nbBadAfter = edgeFN->_simplices.size();
8303 // for ( size_t iS = 0; iS < edgeFN->_simplices.size(); ++iS )
8305 // nbBadAfter -= edgeFN->_simplices[iS].IsForward( &prevPos, &newPos, vol );
8307 // if ( nbBadAfter <= nbBad )
8309 // SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( edgeFN->_nodes.back() );
8310 // tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
8311 // edgeF->_pos.back() = newPosF;
8312 // dumpMoveComm( tgtNode, "MoveNearConcaVer 2" ); // debug
8313 // nbBad = nbBadAfter;
8317 badSmooEdges.push_back( edgeFN );
8320 // move a bit not smoothed around moved nodes
8321 // for ( size_t i = nbBadBefore; i < badSmooEdges.size(); ++i )
8323 // _LayerEdge* edgeF = badSmooEdges[i];
8324 // SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( edgeF->_nodes.back() );
8325 // gp_XYZ newPos1 = SMESH_TNodeXYZ( edgeF->_nodes[0] ) + inflationVec;
8326 // gp_XYZ newPos2 = 0.5 * ( newPos1 + SMESH_TNodeXYZ( tgtNode ));
8327 // tgtNode->setXYZ( newPos2.X(), newPos2.Y(), newPos2.Z() );
8328 // edgeF->_pos.back() = newPosF;
8329 // dumpMoveComm( tgtNode, "MoveNearConcaVer 2" ); // debug
8333 //================================================================================
8335 * \brief Perform smooth of _LayerEdge's based on EDGE's
8336 * \retval bool - true if node has been moved
8338 //================================================================================
8340 bool _LayerEdge::SmoothOnEdge(Handle(ShapeAnalysis_Surface)& surface,
8341 const TopoDS_Face& F,
8342 SMESH_MesherHelper& helper)
8344 ASSERT( IsOnEdge() );
8346 SMDS_MeshNode* tgtNode = const_cast<SMDS_MeshNode*>( _nodes.back() );
8347 SMESH_TNodeXYZ oldPos( tgtNode );
8348 double dist01, distNewOld;
8350 SMESH_TNodeXYZ p0( _2neibors->tgtNode(0));
8351 SMESH_TNodeXYZ p1( _2neibors->tgtNode(1));
8352 dist01 = p0.Distance( _2neibors->tgtNode(1) );
8354 gp_Pnt newPos = p0 * _2neibors->_wgt[0] + p1 * _2neibors->_wgt[1];
8355 double lenDelta = 0;
8358 //lenDelta = _curvature->lenDelta( _len );
8359 lenDelta = _curvature->lenDeltaByDist( dist01 );
8360 newPos.ChangeCoord() += _normal * lenDelta;
8363 distNewOld = newPos.Distance( oldPos );
8367 if ( _2neibors->_plnNorm )
8369 // put newPos on the plane defined by source node and _plnNorm
8370 gp_XYZ new2src = SMESH_TNodeXYZ( _nodes[0] ) - newPos.XYZ();
8371 double new2srcProj = (*_2neibors->_plnNorm) * new2src;
8372 newPos.ChangeCoord() += (*_2neibors->_plnNorm) * new2srcProj;
8374 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
8375 _pos.back() = newPos.XYZ();
8379 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
8380 gp_XY uv( Precision::Infinite(), 0 );
8381 helper.CheckNodeUV( F, tgtNode, uv, 1e-10, /*force=*/true );
8382 _pos.back().SetCoord( uv.X(), uv.Y(), 0 );
8384 newPos = surface->Value( uv );
8385 tgtNode->setXYZ( newPos.X(), newPos.Y(), newPos.Z() );
8388 // commented for IPAL0052478
8389 // if ( _curvature && lenDelta < 0 )
8391 // gp_Pnt prevPos( _pos[ _pos.size()-2 ]);
8392 // _len -= prevPos.Distance( oldPos );
8393 // _len += prevPos.Distance( newPos );
8395 bool moved = distNewOld > dist01/50;
8397 dumpMove( tgtNode ); // debug
8402 //================================================================================
8404 * \brief Perform 3D smooth of nodes inflated from FACE. No check of validity
8406 //================================================================================
8408 void _LayerEdge::SmoothWoCheck()
8410 if ( Is( DIFFICULT ))
8413 bool moved = Is( SMOOTHED );
8414 for ( size_t i = 0; i < _neibors.size() && !moved; ++i )
8415 moved = _neibors[i]->Is( SMOOTHED );
8419 gp_XYZ newPos = (this->*_smooFunction)(); // fun chosen by ChooseSmooFunction()
8421 SMDS_MeshNode* n = const_cast< SMDS_MeshNode* >( _nodes.back() );
8422 n->setXYZ( newPos.X(), newPos.Y(), newPos.Z());
8423 _pos.back() = newPos;
8425 dumpMoveComm( n, SMESH_Comment("No check - ") << _funNames[ smooFunID() ]);
8428 //================================================================================
8430 * \brief Checks validity of _neibors on EDGEs and VERTEXes
8432 //================================================================================
8434 int _LayerEdge::CheckNeiborsOnBoundary( vector< _LayerEdge* >* badNeibors, bool * needSmooth )
8436 if ( ! Is( NEAR_BOUNDARY ))
8441 for ( size_t iN = 0; iN < _neibors.size(); ++iN )
8443 _LayerEdge* eN = _neibors[iN];
8444 if ( eN->_nodes[0]->getshapeId() == _nodes[0]->getshapeId() )
8447 *needSmooth |= ( eN->Is( _LayerEdge::BLOCKED ) ||
8448 eN->Is( _LayerEdge::NORMAL_UPDATED ) ||
8449 eN->_pos.size() != _pos.size() );
8451 SMESH_TNodeXYZ curPosN ( eN->_nodes.back() );
8452 SMESH_TNodeXYZ prevPosN( eN->_nodes[0] );
8453 for ( size_t i = 0; i < eN->_simplices.size(); ++i )
8454 if ( eN->_nodes.size() > 1 &&
8455 eN->_simplices[i].Includes( _nodes.back() ) &&
8456 !eN->_simplices[i].IsForward( &prevPosN, &curPosN, vol ))
8461 badNeibors->push_back( eN );
8462 debugMsg("Bad boundary simplex ( "
8463 << " "<< eN->_nodes[0]->GetID()
8464 << " "<< eN->_nodes.back()->GetID()
8465 << " "<< eN->_simplices[i]._nPrev->GetID()
8466 << " "<< eN->_simplices[i]._nNext->GetID() << " )" );
8477 //================================================================================
8479 * \brief Perform 'smart' 3D smooth of nodes inflated from FACE
8480 * \retval int - nb of bad simplices around this _LayerEdge
8482 //================================================================================
8484 int _LayerEdge::Smooth(const int step, bool findBest, vector< _LayerEdge* >& toSmooth )
8486 if ( !Is( MOVED ) || Is( SMOOTHED ) || Is( BLOCKED ))
8487 return 0; // shape of simplices not changed
8488 if ( _simplices.size() < 2 )
8489 return 0; // _LayerEdge inflated along EDGE or FACE
8491 if ( Is( DIFFICULT )) // || Is( ON_CONCAVE_FACE )
8494 const gp_XYZ& curPos = _pos.back();
8495 const gp_XYZ& prevPos = _pos[0]; //PrevPos();
8497 // quality metrics (orientation) of tetras around _tgtNode
8499 double vol, minVolBefore = 1e100;
8500 for ( size_t i = 0; i < _simplices.size(); ++i )
8502 nbOkBefore += _simplices[i].IsForward( &prevPos, &curPos, vol );
8503 minVolBefore = Min( minVolBefore, vol );
8505 int nbBad = _simplices.size() - nbOkBefore;
8507 bool bndNeedSmooth = false;
8509 nbBad = CheckNeiborsOnBoundary( 0, & bndNeedSmooth );
8513 // evaluate min angle
8514 if ( nbBad == 0 && !findBest && !bndNeedSmooth )
8516 size_t nbGoodAngles = _simplices.size();
8518 for ( size_t i = 0; i < _simplices.size(); ++i )
8520 if ( !_simplices[i].IsMinAngleOK( curPos, angle ) && angle > _minAngle )
8523 if ( nbGoodAngles == _simplices.size() )
8529 if ( Is( ON_CONCAVE_FACE ))
8532 if ( step % 2 == 0 )
8535 if ( Is( ON_CONCAVE_FACE ) && !findBest ) // alternate FUN_CENTROIDAL and FUN_LAPLACIAN
8537 if ( _smooFunction == _funs[ FUN_LAPLACIAN ] )
8538 _smooFunction = _funs[ FUN_CENTROIDAL ];
8540 _smooFunction = _funs[ FUN_LAPLACIAN ];
8543 // compute new position for the last _pos using different _funs
8546 for ( int iFun = -1; iFun < theNbSmooFuns; ++iFun )
8549 newPos = (this->*_smooFunction)(); // fun chosen by ChooseSmooFunction()
8550 else if ( _funs[ iFun ] == _smooFunction )
8551 continue; // _smooFunction again
8552 else if ( step > 1 )
8553 newPos = (this->*_funs[ iFun ])(); // try other smoothing fun
8555 break; // let "easy" functions improve elements around distorted ones
8559 double delta = _curvature->lenDelta( _len );
8561 newPos += _normal * delta;
8564 double segLen = _normal * ( newPos - prevPos );
8565 if ( segLen + delta > 0 )
8566 newPos += _normal * delta;
8568 // double segLenChange = _normal * ( curPos - newPos );
8569 // newPos += 0.5 * _normal * segLenChange;
8573 double minVolAfter = 1e100;
8574 for ( size_t i = 0; i < _simplices.size(); ++i )
8576 nbOkAfter += _simplices[i].IsForward( &prevPos, &newPos, vol );
8577 minVolAfter = Min( minVolAfter, vol );
8580 if ( nbOkAfter < nbOkBefore )
8584 ( nbOkAfter == nbOkBefore ) &&
8585 ( minVolAfter <= minVolBefore ))
8588 nbBad = _simplices.size() - nbOkAfter;
8589 minVolBefore = minVolAfter;
8590 nbOkBefore = nbOkAfter;
8593 SMDS_MeshNode* n = const_cast< SMDS_MeshNode* >( _nodes.back() );
8594 n->setXYZ( newPos.X(), newPos.Y(), newPos.Z());
8595 _pos.back() = newPos;
8597 dumpMoveComm( n, SMESH_Comment( _funNames[ iFun < 0 ? smooFunID() : iFun ] )
8598 << (nbBad ? " --BAD" : ""));
8602 continue; // look for a better function
8608 } // loop on smoothing functions
8610 if ( moved ) // notify _neibors
8613 for ( size_t i = 0; i < _neibors.size(); ++i )
8614 if ( !_neibors[i]->Is( MOVED ))
8616 _neibors[i]->Set( MOVED );
8617 toSmooth.push_back( _neibors[i] );
8624 //================================================================================
8626 * \brief Perform 'smart' 3D smooth of nodes inflated from FACE
8627 * \retval int - nb of bad simplices around this _LayerEdge
8629 //================================================================================
8631 int _LayerEdge::Smooth(const int step, const bool isConcaveFace, bool findBest )
8633 if ( !_smooFunction )
8634 return 0; // _LayerEdge inflated along EDGE or FACE
8636 return 0; // not inflated
8638 const gp_XYZ& curPos = _pos.back();
8639 const gp_XYZ& prevPos = _pos[0]; //PrevCheckPos();
8641 // quality metrics (orientation) of tetras around _tgtNode
8643 double vol, minVolBefore = 1e100;
8644 for ( size_t i = 0; i < _simplices.size(); ++i )
8646 nbOkBefore += _simplices[i].IsForward( &prevPos, &curPos, vol );
8647 minVolBefore = Min( minVolBefore, vol );
8649 int nbBad = _simplices.size() - nbOkBefore;
8651 if ( isConcaveFace ) // alternate FUN_CENTROIDAL and FUN_LAPLACIAN
8653 if ( _smooFunction == _funs[ FUN_CENTROIDAL ] && step % 2 )
8654 _smooFunction = _funs[ FUN_LAPLACIAN ];
8655 else if ( _smooFunction == _funs[ FUN_LAPLACIAN ] && !( step % 2 ))
8656 _smooFunction = _funs[ FUN_CENTROIDAL ];
8659 // compute new position for the last _pos using different _funs
8661 for ( int iFun = -1; iFun < theNbSmooFuns; ++iFun )
8664 newPos = (this->*_smooFunction)(); // fun chosen by ChooseSmooFunction()
8665 else if ( _funs[ iFun ] == _smooFunction )
8666 continue; // _smooFunction again
8667 else if ( step > 1 )
8668 newPos = (this->*_funs[ iFun ])(); // try other smoothing fun
8670 break; // let "easy" functions improve elements around distorted ones
8674 double delta = _curvature->lenDelta( _len );
8676 newPos += _normal * delta;
8679 double segLen = _normal * ( newPos - prevPos );
8680 if ( segLen + delta > 0 )
8681 newPos += _normal * delta;
8683 // double segLenChange = _normal * ( curPos - newPos );
8684 // newPos += 0.5 * _normal * segLenChange;
8688 double minVolAfter = 1e100;
8689 for ( size_t i = 0; i < _simplices.size(); ++i )
8691 nbOkAfter += _simplices[i].IsForward( &prevPos, &newPos, vol );
8692 minVolAfter = Min( minVolAfter, vol );
8695 if ( nbOkAfter < nbOkBefore )
8697 if (( isConcaveFace || findBest ) &&
8698 ( nbOkAfter == nbOkBefore ) &&
8699 ( minVolAfter <= minVolBefore )
8703 nbBad = _simplices.size() - nbOkAfter;
8704 minVolBefore = minVolAfter;
8705 nbOkBefore = nbOkAfter;
8707 SMDS_MeshNode* n = const_cast< SMDS_MeshNode* >( _nodes.back() );
8708 n->setXYZ( newPos.X(), newPos.Y(), newPos.Z());
8709 _pos.back() = newPos;
8711 dumpMoveComm( n, SMESH_Comment( _funNames[ iFun < 0 ? smooFunID() : iFun ] )
8712 << ( nbBad ? "--BAD" : ""));
8714 // commented for IPAL0052478
8715 // _len -= prevPos.Distance(SMESH_TNodeXYZ( n ));
8716 // _len += prevPos.Distance(newPos);
8718 if ( iFun > -1 ) // findBest || the chosen _fun makes worse
8720 //_smooFunction = _funs[ iFun ];
8721 // cout << "# " << _funNames[ iFun ] << "\t N:" << _nodes.back()->GetID()
8722 // << "\t nbBad: " << _simplices.size() - nbOkAfter
8723 // << " minVol: " << minVolAfter
8724 // << " " << newPos.X() << " " << newPos.Y() << " " << newPos.Z()
8726 continue; // look for a better function
8732 } // loop on smoothing functions
8737 //================================================================================
8739 * \brief Chooses a smoothing technic giving a position most close to an initial one.
8740 * For a correct result, _simplices must contain nodes lying on geometry.
8742 //================================================================================
8744 void _LayerEdge::ChooseSmooFunction( const set< TGeomID >& concaveVertices,
8745 const TNode2Edge& n2eMap)
8747 if ( _smooFunction ) return;
8749 // use smoothNefPolygon() near concaveVertices
8750 if ( !concaveVertices.empty() )
8752 _smooFunction = _funs[ FUN_CENTROIDAL ];
8754 Set( ON_CONCAVE_FACE );
8756 for ( size_t i = 0; i < _simplices.size(); ++i )
8758 if ( concaveVertices.count( _simplices[i]._nPrev->getshapeId() ))
8760 _smooFunction = _funs[ FUN_NEFPOLY ];
8762 // set FUN_CENTROIDAL to neighbor edges
8763 for ( i = 0; i < _neibors.size(); ++i )
8765 if ( _neibors[i]->_nodes[0]->GetPosition()->GetDim() == 2 )
8767 _neibors[i]->_smooFunction = _funs[ FUN_CENTROIDAL ];
8774 // // this coice is done only if ( !concaveVertices.empty() ) for Grids/smesh/bugs_19/X1
8775 // // where the nodes are smoothed too far along a sphere thus creating
8776 // // inverted _simplices
8777 // double dist[theNbSmooFuns];
8778 // //double coef[theNbSmooFuns] = { 1., 1.2, 1.4, 1.4 };
8779 // double coef[theNbSmooFuns] = { 1., 1., 1., 1. };
8781 // double minDist = Precision::Infinite();
8782 // gp_Pnt p = SMESH_TNodeXYZ( _nodes[0] );
8783 // for ( int i = 0; i < FUN_NEFPOLY; ++i )
8785 // gp_Pnt newP = (this->*_funs[i])();
8786 // dist[i] = p.SquareDistance( newP );
8787 // if ( dist[i]*coef[i] < minDist )
8789 // _smooFunction = _funs[i];
8790 // minDist = dist[i]*coef[i];
8796 _smooFunction = _funs[ FUN_LAPLACIAN ];
8799 // for ( size_t i = 0; i < _simplices.size(); ++i )
8800 // minDim = Min( minDim, _simplices[i]._nPrev->GetPosition()->GetDim() );
8801 // if ( minDim == 0 )
8802 // _smooFunction = _funs[ FUN_CENTROIDAL ];
8803 // else if ( minDim == 1 )
8804 // _smooFunction = _funs[ FUN_CENTROIDAL ];
8808 // for ( int i = 0; i < FUN_NB; ++i )
8810 // //cout << dist[i] << " ";
8811 // if ( _smooFunction == _funs[i] ) {
8813 // //debugMsg( fNames[i] );
8817 // cout << _funNames[ iMin ] << "\t N:" << _nodes.back()->GetID() << endl;
8820 //================================================================================
8822 * \brief Returns a name of _SmooFunction
8824 //================================================================================
8826 int _LayerEdge::smooFunID( _LayerEdge::PSmooFun fun) const
8829 fun = _smooFunction;
8830 for ( int i = 0; i < theNbSmooFuns; ++i )
8831 if ( fun == _funs[i] )
8834 return theNbSmooFuns;
8837 //================================================================================
8839 * \brief Computes a new node position using Laplacian smoothing
8841 //================================================================================
8843 gp_XYZ _LayerEdge::smoothLaplacian()
8845 gp_XYZ newPos (0,0,0);
8846 for ( size_t i = 0; i < _simplices.size(); ++i )
8847 newPos += SMESH_TNodeXYZ( _simplices[i]._nPrev );
8848 newPos /= _simplices.size();
8853 //================================================================================
8855 * \brief Computes a new node position using angular-based smoothing
8857 //================================================================================
8859 gp_XYZ _LayerEdge::smoothAngular()
8861 vector< gp_Vec > edgeDir; edgeDir. reserve( _simplices.size() + 1 );
8862 vector< double > edgeSize; edgeSize.reserve( _simplices.size() );
8863 vector< gp_XYZ > points; points. reserve( _simplices.size() + 1 );
8865 gp_XYZ pPrev = SMESH_TNodeXYZ( _simplices.back()._nPrev );
8867 for ( size_t i = 0; i < _simplices.size(); ++i )
8869 gp_XYZ p = SMESH_TNodeXYZ( _simplices[i]._nPrev );
8870 edgeDir.push_back( p - pPrev );
8871 edgeSize.push_back( edgeDir.back().Magnitude() );
8872 if ( edgeSize.back() < numeric_limits<double>::min() )
8875 edgeSize.pop_back();
8879 edgeDir.back() /= edgeSize.back();
8880 points.push_back( p );
8885 edgeDir.push_back ( edgeDir[0] );
8886 edgeSize.push_back( edgeSize[0] );
8887 pN /= points.size();
8889 gp_XYZ newPos(0,0,0);
8891 for ( size_t i = 0; i < points.size(); ++i )
8893 gp_Vec toN = pN - points[i];
8894 double toNLen = toN.Magnitude();
8895 if ( toNLen < numeric_limits<double>::min() )
8900 gp_Vec bisec = edgeDir[i] + edgeDir[i+1];
8901 double bisecLen = bisec.SquareMagnitude();
8902 if ( bisecLen < numeric_limits<double>::min() )
8904 gp_Vec norm = edgeDir[i] ^ toN;
8905 bisec = norm ^ edgeDir[i];
8906 bisecLen = bisec.SquareMagnitude();
8908 bisecLen = Sqrt( bisecLen );
8912 gp_XYZ pNew = ( points[i] + bisec.XYZ() * toNLen ) * bisecLen;
8913 sumSize += bisecLen;
8915 gp_XYZ pNew = ( points[i] + bisec.XYZ() * toNLen ) * ( edgeSize[i] + edgeSize[i+1] );
8916 sumSize += ( edgeSize[i] + edgeSize[i+1] );
8922 // project newPos to an average plane
8924 gp_XYZ norm(0,0,0); // plane normal
8925 points.push_back( points[0] );
8926 for ( size_t i = 1; i < points.size(); ++i )
8928 gp_XYZ vec1 = points[ i-1 ] - pN;
8929 gp_XYZ vec2 = points[ i ] - pN;
8930 gp_XYZ cross = vec1 ^ vec2;
8933 if ( cross * norm < numeric_limits<double>::min() )
8934 norm += cross.Reversed();
8938 catch (Standard_Failure) { // if |cross| == 0.
8941 gp_XYZ vec = newPos - pN;
8942 double r = ( norm * vec ) / norm.SquareModulus(); // param [0,1] on norm
8943 newPos = newPos - r * norm;
8948 //================================================================================
8950 * \brief Computes a new node position using weigthed node positions
8952 //================================================================================
8954 gp_XYZ _LayerEdge::smoothLengthWeighted()
8956 vector< double > edgeSize; edgeSize.reserve( _simplices.size() + 1);
8957 vector< gp_XYZ > points; points. reserve( _simplices.size() );
8959 gp_XYZ pPrev = SMESH_TNodeXYZ( _simplices.back()._nPrev );
8960 for ( size_t i = 0; i < _simplices.size(); ++i )
8962 gp_XYZ p = SMESH_TNodeXYZ( _simplices[i]._nPrev );
8963 edgeSize.push_back( ( p - pPrev ).Modulus() );
8964 if ( edgeSize.back() < numeric_limits<double>::min() )
8966 edgeSize.pop_back();
8970 points.push_back( p );
8974 edgeSize.push_back( edgeSize[0] );
8976 gp_XYZ newPos(0,0,0);
8978 for ( size_t i = 0; i < points.size(); ++i )
8980 newPos += points[i] * ( edgeSize[i] + edgeSize[i+1] );
8981 sumSize += edgeSize[i] + edgeSize[i+1];
8987 //================================================================================
8989 * \brief Computes a new node position using angular-based smoothing
8991 //================================================================================
8993 gp_XYZ _LayerEdge::smoothCentroidal()
8995 gp_XYZ newPos(0,0,0);
8996 gp_XYZ pN = SMESH_TNodeXYZ( _nodes.back() );
8998 for ( size_t i = 0; i < _simplices.size(); ++i )
9000 gp_XYZ p1 = SMESH_TNodeXYZ( _simplices[i]._nPrev );
9001 gp_XYZ p2 = SMESH_TNodeXYZ( _simplices[i]._nNext );
9002 gp_XYZ gc = ( pN + p1 + p2 ) / 3.;
9003 double size = (( p1 - pN ) ^ ( p2 - pN )).Modulus();
9006 newPos += gc * size;
9013 //================================================================================
9015 * \brief Computes a new node position located inside a Nef polygon
9017 //================================================================================
9019 gp_XYZ _LayerEdge::smoothNefPolygon()
9020 #ifdef OLD_NEF_POLYGON
9022 gp_XYZ newPos(0,0,0);
9024 // get a plane to search a solution on
9026 vector< gp_XYZ > vecs( _simplices.size() + 1 );
9028 const double tol = numeric_limits<double>::min();
9029 gp_XYZ center(0,0,0);
9030 for ( i = 0; i < _simplices.size(); ++i )
9032 vecs[i] = ( SMESH_TNodeXYZ( _simplices[i]._nNext ) -
9033 SMESH_TNodeXYZ( _simplices[i]._nPrev ));
9034 center += SMESH_TNodeXYZ( _simplices[i]._nPrev );
9036 vecs.back() = vecs[0];
9037 center /= _simplices.size();
9039 gp_XYZ zAxis(0,0,0);
9040 for ( i = 0; i < _simplices.size(); ++i )
9041 zAxis += vecs[i] ^ vecs[i+1];
9044 for ( i = 0; i < _simplices.size(); ++i )
9047 if ( yAxis.SquareModulus() > tol )
9050 gp_XYZ xAxis = yAxis ^ zAxis;
9051 // SMESH_TNodeXYZ p0( _simplices[0]._nPrev );
9052 // const double tol = 1e-6 * ( p0.Distance( _simplices[1]._nPrev ) +
9053 // p0.Distance( _simplices[2]._nPrev ));
9054 // gp_XYZ center = smoothLaplacian();
9055 // gp_XYZ xAxis, yAxis, zAxis;
9056 // for ( i = 0; i < _simplices.size(); ++i )
9058 // xAxis = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9059 // if ( xAxis.SquareModulus() > tol*tol )
9062 // for ( i = 1; i < _simplices.size(); ++i )
9064 // yAxis = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9065 // zAxis = xAxis ^ yAxis;
9066 // if ( zAxis.SquareModulus() > tol*tol )
9069 // if ( i == _simplices.size() ) return newPos;
9071 yAxis = zAxis ^ xAxis;
9072 xAxis /= xAxis.Modulus();
9073 yAxis /= yAxis.Modulus();
9075 // get half-planes of _simplices
9077 vector< _halfPlane > halfPlns( _simplices.size() );
9079 for ( size_t i = 0; i < _simplices.size(); ++i )
9081 gp_XYZ OP1 = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9082 gp_XYZ OP2 = SMESH_TNodeXYZ( _simplices[i]._nNext ) - center;
9083 gp_XY p1( OP1 * xAxis, OP1 * yAxis );
9084 gp_XY p2( OP2 * xAxis, OP2 * yAxis );
9085 gp_XY vec12 = p2 - p1;
9086 double dist12 = vec12.Modulus();
9090 halfPlns[ nbHP ]._pos = p1;
9091 halfPlns[ nbHP ]._dir = vec12;
9092 halfPlns[ nbHP ]._inNorm.SetCoord( -vec12.Y(), vec12.X() );
9096 // intersect boundaries of half-planes, define state of intersection points
9097 // in relation to all half-planes and calculate internal point of a 2D polygon
9100 gp_XY newPos2D (0,0);
9102 enum { UNDEF = -1, NOT_OUT, IS_OUT, NO_INT };
9103 typedef std::pair< gp_XY, int > TIntPntState; // coord and isOut state
9104 TIntPntState undefIPS( gp_XY(1e100,1e100), UNDEF );
9106 vector< vector< TIntPntState > > allIntPnts( nbHP );
9107 for ( int iHP1 = 0; iHP1 < nbHP; ++iHP1 )
9109 vector< TIntPntState > & intPnts1 = allIntPnts[ iHP1 ];
9110 if ( intPnts1.empty() ) intPnts1.resize( nbHP, undefIPS );
9112 int iPrev = SMESH_MesherHelper::WrapIndex( iHP1 - 1, nbHP );
9113 int iNext = SMESH_MesherHelper::WrapIndex( iHP1 + 1, nbHP );
9116 const gp_XY* segEnds[2] = { 0, 0 }; // NOT_OUT points
9118 for ( int iHP2 = 0; iHP2 < nbHP; ++iHP2 )
9120 if ( iHP1 == iHP2 ) continue;
9122 TIntPntState & ips1 = intPnts1[ iHP2 ];
9123 if ( ips1.second == UNDEF )
9125 // find an intersection point of boundaries of iHP1 and iHP2
9127 if ( iHP2 == iPrev ) // intersection with neighbors is known
9128 ips1.first = halfPlns[ iHP1 ]._pos;
9129 else if ( iHP2 == iNext )
9130 ips1.first = halfPlns[ iHP2 ]._pos;
9131 else if ( !halfPlns[ iHP1 ].FindIntersection( halfPlns[ iHP2 ], ips1.first ))
9132 ips1.second = NO_INT;
9134 // classify the found intersection point
9135 if ( ips1.second != NO_INT )
9137 ips1.second = NOT_OUT;
9138 for ( int i = 0; i < nbHP && ips1.second == NOT_OUT; ++i )
9139 if ( i != iHP1 && i != iHP2 &&
9140 halfPlns[ i ].IsOut( ips1.first, tol ))
9141 ips1.second = IS_OUT;
9143 vector< TIntPntState > & intPnts2 = allIntPnts[ iHP2 ];
9144 if ( intPnts2.empty() ) intPnts2.resize( nbHP, undefIPS );
9145 TIntPntState & ips2 = intPnts2[ iHP1 ];
9148 if ( ips1.second == NOT_OUT )
9151 segEnds[ bool(segEnds[0]) ] = & ips1.first;
9155 // find a NOT_OUT segment of boundary which is located between
9156 // two NOT_OUT int points
9159 continue; // no such a segment
9163 // sort points along the boundary
9164 map< double, TIntPntState* > ipsByParam;
9165 for ( int iHP2 = 0; iHP2 < nbHP; ++iHP2 )
9167 TIntPntState & ips1 = intPnts1[ iHP2 ];
9168 if ( ips1.second != NO_INT )
9170 gp_XY op = ips1.first - halfPlns[ iHP1 ]._pos;
9171 double param = op * halfPlns[ iHP1 ]._dir;
9172 ipsByParam.insert( make_pair( param, & ips1 ));
9175 // look for two neighboring NOT_OUT points
9177 map< double, TIntPntState* >::iterator u2ips = ipsByParam.begin();
9178 for ( ; u2ips != ipsByParam.end(); ++u2ips )
9180 TIntPntState & ips1 = *(u2ips->second);
9181 if ( ips1.second == NOT_OUT )
9182 segEnds[ bool( nbNotOut++ ) ] = & ips1.first;
9183 else if ( nbNotOut >= 2 )
9190 if ( nbNotOut >= 2 )
9192 double len = ( *segEnds[0] - *segEnds[1] ).Modulus();
9195 newPos2D += 0.5 * len * ( *segEnds[0] + *segEnds[1] );
9202 newPos = center + xAxis * newPos2D.X() + yAxis * newPos2D.Y();
9211 #else // OLD_NEF_POLYGON
9212 { ////////////////////////////////// NEW
9213 gp_XYZ newPos(0,0,0);
9215 // get a plane to search a solution on
9218 gp_XYZ center(0,0,0);
9219 for ( i = 0; i < _simplices.size(); ++i )
9220 center += SMESH_TNodeXYZ( _simplices[i]._nPrev );
9221 center /= _simplices.size();
9223 vector< gp_XYZ > vecs( _simplices.size() + 1 );
9224 for ( i = 0; i < _simplices.size(); ++i )
9225 vecs[i] = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9226 vecs.back() = vecs[0];
9228 const double tol = numeric_limits<double>::min();
9229 gp_XYZ zAxis(0,0,0);
9230 for ( i = 0; i < _simplices.size(); ++i )
9232 gp_XYZ cross = vecs[i] ^ vecs[i+1];
9235 if ( cross * zAxis < tol )
9236 zAxis += cross.Reversed();
9240 catch (Standard_Failure) { // if |cross| == 0.
9245 for ( i = 0; i < _simplices.size(); ++i )
9248 if ( yAxis.SquareModulus() > tol )
9251 gp_XYZ xAxis = yAxis ^ zAxis;
9252 // SMESH_TNodeXYZ p0( _simplices[0]._nPrev );
9253 // const double tol = 1e-6 * ( p0.Distance( _simplices[1]._nPrev ) +
9254 // p0.Distance( _simplices[2]._nPrev ));
9255 // gp_XYZ center = smoothLaplacian();
9256 // gp_XYZ xAxis, yAxis, zAxis;
9257 // for ( i = 0; i < _simplices.size(); ++i )
9259 // xAxis = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9260 // if ( xAxis.SquareModulus() > tol*tol )
9263 // for ( i = 1; i < _simplices.size(); ++i )
9265 // yAxis = SMESH_TNodeXYZ( _simplices[i]._nPrev ) - center;
9266 // zAxis = xAxis ^ yAxis;
9267 // if ( zAxis.SquareModulus() > tol*tol )
9270 // if ( i == _simplices.size() ) return newPos;
9272 yAxis = zAxis ^ xAxis;
9273 xAxis /= xAxis.Modulus();
9274 yAxis /= yAxis.Modulus();
9276 // get half-planes of _simplices
9278 vector< _halfPlane > halfPlns( _simplices.size() );
9280 for ( size_t i = 0; i < _simplices.size(); ++i )
9282 const gp_XYZ& OP1 = vecs[ i ];
9283 const gp_XYZ& OP2 = vecs[ i+1 ];
9284 gp_XY p1( OP1 * xAxis, OP1 * yAxis );
9285 gp_XY p2( OP2 * xAxis, OP2 * yAxis );
9286 gp_XY vec12 = p2 - p1;
9287 double dist12 = vec12.Modulus();
9291 halfPlns[ nbHP ]._pos = p1;
9292 halfPlns[ nbHP ]._dir = vec12;
9293 halfPlns[ nbHP ]._inNorm.SetCoord( -vec12.Y(), vec12.X() );
9297 // intersect boundaries of half-planes, define state of intersection points
9298 // in relation to all half-planes and calculate internal point of a 2D polygon
9301 gp_XY newPos2D (0,0);
9303 enum { UNDEF = -1, NOT_OUT, IS_OUT, NO_INT };
9304 typedef std::pair< gp_XY, int > TIntPntState; // coord and isOut state
9305 TIntPntState undefIPS( gp_XY(1e100,1e100), UNDEF );
9307 vector< vector< TIntPntState > > allIntPnts( nbHP );
9308 for ( int iHP1 = 0; iHP1 < nbHP; ++iHP1 )
9310 vector< TIntPntState > & intPnts1 = allIntPnts[ iHP1 ];
9311 if ( intPnts1.empty() ) intPnts1.resize( nbHP, undefIPS );
9313 int iPrev = SMESH_MesherHelper::WrapIndex( iHP1 - 1, nbHP );
9314 int iNext = SMESH_MesherHelper::WrapIndex( iHP1 + 1, nbHP );
9317 const gp_XY* segEnds[2] = { 0, 0 }; // NOT_OUT points
9319 for ( int iHP2 = 0; iHP2 < nbHP; ++iHP2 )
9321 if ( iHP1 == iHP2 ) continue;
9323 TIntPntState & ips1 = intPnts1[ iHP2 ];
9324 if ( ips1.second == UNDEF )
9326 // find an intersection point of boundaries of iHP1 and iHP2
9328 if ( iHP2 == iPrev ) // intersection with neighbors is known
9329 ips1.first = halfPlns[ iHP1 ]._pos;
9330 else if ( iHP2 == iNext )
9331 ips1.first = halfPlns[ iHP2 ]._pos;
9332 else if ( !halfPlns[ iHP1 ].FindIntersection( halfPlns[ iHP2 ], ips1.first ))
9333 ips1.second = NO_INT;
9335 // classify the found intersection point
9336 if ( ips1.second != NO_INT )
9338 ips1.second = NOT_OUT;
9339 for ( int i = 0; i < nbHP && ips1.second == NOT_OUT; ++i )
9340 if ( i != iHP1 && i != iHP2 &&
9341 halfPlns[ i ].IsOut( ips1.first, tol ))
9342 ips1.second = IS_OUT;
9344 vector< TIntPntState > & intPnts2 = allIntPnts[ iHP2 ];
9345 if ( intPnts2.empty() ) intPnts2.resize( nbHP, undefIPS );
9346 TIntPntState & ips2 = intPnts2[ iHP1 ];
9349 if ( ips1.second == NOT_OUT )
9352 segEnds[ bool(segEnds[0]) ] = & ips1.first;
9356 // find a NOT_OUT segment of boundary which is located between
9357 // two NOT_OUT int points
9360 continue; // no such a segment
9364 // sort points along the boundary
9365 map< double, TIntPntState* > ipsByParam;
9366 for ( int iHP2 = 0; iHP2 < nbHP; ++iHP2 )
9368 TIntPntState & ips1 = intPnts1[ iHP2 ];
9369 if ( ips1.second != NO_INT )
9371 gp_XY op = ips1.first - halfPlns[ iHP1 ]._pos;
9372 double param = op * halfPlns[ iHP1 ]._dir;
9373 ipsByParam.insert( make_pair( param, & ips1 ));
9376 // look for two neighboring NOT_OUT points
9378 map< double, TIntPntState* >::iterator u2ips = ipsByParam.begin();
9379 for ( ; u2ips != ipsByParam.end(); ++u2ips )
9381 TIntPntState & ips1 = *(u2ips->second);
9382 if ( ips1.second == NOT_OUT )
9383 segEnds[ bool( nbNotOut++ ) ] = & ips1.first;
9384 else if ( nbNotOut >= 2 )
9391 if ( nbNotOut >= 2 )
9393 double len = ( *segEnds[0] - *segEnds[1] ).Modulus();
9396 newPos2D += 0.5 * len * ( *segEnds[0] + *segEnds[1] );
9403 newPos = center + xAxis * newPos2D.X() + yAxis * newPos2D.Y();
9412 #endif // OLD_NEF_POLYGON
9414 //================================================================================
9416 * \brief Add a new segment to _LayerEdge during inflation
9418 //================================================================================
9420 void _LayerEdge::SetNewLength( double len, _EdgesOnShape& eos, SMESH_MesherHelper& helper )
9425 if ( len > _maxLen )
9428 Block( eos.GetData() );
9430 const double lenDelta = len - _len;
9431 if ( lenDelta < len * 1e-3 )
9433 Block( eos.GetData() );
9437 SMDS_MeshNode* n = const_cast< SMDS_MeshNode*>( _nodes.back() );
9438 gp_XYZ oldXYZ = SMESH_TNodeXYZ( n );
9440 if ( eos._hyp.IsOffsetMethod() )
9444 SMDS_ElemIteratorPtr faceIt = _nodes[0]->GetInverseElementIterator( SMDSAbs_Face );
9445 while ( faceIt->more() )
9447 const SMDS_MeshElement* face = faceIt->next();
9448 if ( !eos.GetNormal( face, faceNorm ))
9451 // translate plane of a face
9452 gp_XYZ baryCenter = oldXYZ + faceNorm.XYZ() * lenDelta;
9454 // find point of intersection of the face plane located at baryCenter
9455 // and _normal located at newXYZ
9456 double d = -( faceNorm.XYZ() * baryCenter ); // d of plane equation ax+by+cz+d=0
9457 double dot = ( faceNorm.XYZ() * _normal );
9458 if ( dot < std::numeric_limits<double>::min() )
9459 dot = lenDelta * 1e-3;
9460 double step = -( faceNorm.XYZ() * newXYZ + d ) / dot;
9461 newXYZ += step * _normal;
9463 _lenFactor = _normal * ( newXYZ - oldXYZ ) / lenDelta; // _lenFactor is used in InvalidateStep()
9467 newXYZ = oldXYZ + _normal * lenDelta * _lenFactor;
9470 n->setXYZ( newXYZ.X(), newXYZ.Y(), newXYZ.Z() );
9471 _pos.push_back( newXYZ );
9473 if ( !eos._sWOL.IsNull() )
9477 if ( eos.SWOLType() == TopAbs_EDGE )
9479 double u = Precision::Infinite(); // to force projection w/o distance check
9480 uvOK = helper.CheckNodeU( TopoDS::Edge( eos._sWOL ), n, u,
9481 /*tol=*/2*lenDelta, /*force=*/true, distXYZ );
9482 _pos.back().SetCoord( u, 0, 0 );
9483 if ( _nodes.size() > 1 && uvOK )
9485 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( n->GetPosition() );
9486 pos->SetUParameter( u );
9491 gp_XY uv( Precision::Infinite(), 0 );
9492 uvOK = helper.CheckNodeUV( TopoDS::Face( eos._sWOL ), n, uv,
9493 /*tol=*/2*lenDelta, /*force=*/true, distXYZ );
9494 _pos.back().SetCoord( uv.X(), uv.Y(), 0 );
9495 if ( _nodes.size() > 1 && uvOK )
9497 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( n->GetPosition() );
9498 pos->SetUParameter( uv.X() );
9499 pos->SetVParameter( uv.Y() );
9504 n->setXYZ( distXYZ[1], distXYZ[2], distXYZ[3]);
9508 n->setXYZ( oldXYZ.X(), oldXYZ.Y(), oldXYZ.Z() );
9510 Block( eos.GetData() );
9518 if ( eos.ShapeType() != TopAbs_FACE )
9520 for ( size_t i = 0; i < _neibors.size(); ++i )
9521 //if ( _len > _neibors[i]->GetSmooLen() )
9522 _neibors[i]->Set( MOVED );
9526 dumpMove( n ); //debug
9529 //================================================================================
9531 * \brief Set BLOCKED flag and propagate limited _maxLen to _neibors
9533 //================================================================================
9535 void _LayerEdge::Block( _SolidData& data )
9537 //if ( Is( BLOCKED )) return;
9540 SMESH_Comment msg( "#BLOCK shape=");
9541 msg << data.GetShapeEdges( this )->_shapeID
9542 << ", nodes " << _nodes[0]->GetID() << ", " << _nodes.back()->GetID();
9543 dumpCmd( msg + " -- BEGIN");
9546 std::queue<_LayerEdge*> queue;
9549 gp_Pnt pSrc, pTgt, pSrcN, pTgtN;
9550 while ( !queue.empty() )
9552 _LayerEdge* edge = queue.front(); queue.pop();
9553 pSrc = SMESH_TNodeXYZ( edge->_nodes[0] );
9554 pTgt = SMESH_TNodeXYZ( edge->_nodes.back() );
9555 for ( size_t iN = 0; iN < edge->_neibors.size(); ++iN )
9557 _LayerEdge* neibor = edge->_neibors[iN];
9558 if ( neibor->_maxLen < edge->_maxLen * 1.01 )
9560 pSrcN = SMESH_TNodeXYZ( neibor->_nodes[0] );
9561 pTgtN = SMESH_TNodeXYZ( neibor->_nodes.back() );
9562 double minDist = pSrc.SquareDistance( pSrcN );
9563 minDist = Min( pTgt.SquareDistance( pTgtN ), minDist );
9564 minDist = Min( pSrc.SquareDistance( pTgtN ), minDist );
9565 minDist = Min( pTgt.SquareDistance( pSrcN ), minDist );
9566 double newMaxLen = edge->_maxLen + 0.5 * Sqrt( minDist );
9567 //if ( edge->_nodes[0]->getshapeId() == neibor->_nodes[0]->getshapeId() ) viscous_layers_00/A3
9569 //newMaxLen *= edge->_lenFactor / neibor->_lenFactor;
9570 // newMaxLen *= Min( edge->_lenFactor / neibor->_lenFactor,
9571 // neibor->_lenFactor / edge->_lenFactor );
9573 if ( neibor->_maxLen > newMaxLen )
9575 neibor->SetMaxLen( newMaxLen );
9576 if ( neibor->_maxLen < neibor->_len )
9578 _EdgesOnShape* eos = data.GetShapeEdges( neibor );
9579 int lastStep = neibor->Is( BLOCKED ) ? 1 : 0;
9580 while ( neibor->_len > neibor->_maxLen &&
9581 neibor->NbSteps() > lastStep )
9582 neibor->InvalidateStep( neibor->NbSteps(), *eos, /*restoreLength=*/true );
9583 neibor->SetNewLength( neibor->_maxLen, *eos, data.GetHelper() );
9584 //neibor->Block( data );
9586 queue.push( neibor );
9590 dumpCmd( msg + " -- END");
9593 //================================================================================
9595 * \brief Remove last inflation step
9597 //================================================================================
9599 void _LayerEdge::InvalidateStep( size_t curStep, const _EdgesOnShape& eos, bool restoreLength )
9601 if ( _pos.size() > curStep && _nodes.size() > 1 )
9603 _pos.resize( curStep );
9605 gp_Pnt nXYZ = _pos.back();
9606 SMDS_MeshNode* n = const_cast< SMDS_MeshNode*>( _nodes.back() );
9607 SMESH_TNodeXYZ curXYZ( n );
9608 if ( !eos._sWOL.IsNull() )
9610 TopLoc_Location loc;
9611 if ( eos.SWOLType() == TopAbs_EDGE )
9613 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( n->GetPosition() );
9614 pos->SetUParameter( nXYZ.X() );
9616 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( eos._sWOL ), loc, f,l);
9617 nXYZ = curve->Value( nXYZ.X() ).Transformed( loc );
9621 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( n->GetPosition() );
9622 pos->SetUParameter( nXYZ.X() );
9623 pos->SetVParameter( nXYZ.Y() );
9624 Handle(Geom_Surface) surface = BRep_Tool::Surface( TopoDS::Face(eos._sWOL), loc );
9625 nXYZ = surface->Value( nXYZ.X(), nXYZ.Y() ).Transformed( loc );
9628 n->setXYZ( nXYZ.X(), nXYZ.Y(), nXYZ.Z() );
9631 if ( restoreLength )
9633 if ( NbSteps() == 0 )
9635 else if ( IsOnFace() && Is( MOVED ))
9636 _len = ( nXYZ.XYZ() - SMESH_NodeXYZ( _nodes[0] )) * _normal;
9638 _len -= ( nXYZ.XYZ() - curXYZ ).Modulus() / _lenFactor;
9644 //================================================================================
9646 * \brief Return index of a _pos distant from _normal
9648 //================================================================================
9650 int _LayerEdge::GetSmoothedPos( const double tol )
9653 for ( size_t i = 1; i < _pos.size() && !iSmoothed; ++i )
9655 double normDist = ( _pos[i] - _pos[0] ).Crossed( _normal ).SquareModulus();
9656 if ( normDist > tol * tol )
9662 //================================================================================
9664 * \brief Smooth a path formed by _pos of a _LayerEdge smoothed on FACE
9666 //================================================================================
9668 void _LayerEdge::SmoothPos( const vector< double >& segLen, const double tol )
9670 if ( /*Is( NORMAL_UPDATED ) ||*/ _pos.size() <= 2 )
9673 // find the 1st smoothed _pos
9674 int iSmoothed = GetSmoothedPos( tol );
9675 if ( !iSmoothed ) return;
9677 gp_XYZ normal = _normal;
9678 if ( Is( NORMAL_UPDATED ))
9681 for ( size_t i = 0; i < _neibors.size(); ++i )
9683 if ( _neibors[i]->IsOnFace() )
9685 double dot = _normal * _neibors[i]->_normal;
9688 normal = _neibors[i]->_normal;
9694 for ( size_t i = 1; i < _pos.size(); ++i )
9696 normal = _pos[i] - _pos[0];
9697 double size = normal.Modulus();
9698 if ( size > RealSmall() )
9705 const double r = 0.2;
9706 for ( int iter = 0; iter < 50; ++iter )
9709 for ( size_t i = Max( 1, iSmoothed-1-iter ); i < _pos.size()-1; ++i )
9711 gp_XYZ midPos = 0.5 * ( _pos[i-1] + _pos[i+1] );
9712 gp_XYZ newPos = ( 1-r ) * midPos + r * _pos[i];
9714 double midLen = 0.5 * ( segLen[i-1] + segLen[i+1] );
9715 double newLen = ( 1-r ) * midLen + r * segLen[i];
9716 const_cast< double& >( segLen[i] ) = newLen;
9717 // check angle between normal and (_pos[i+1], _pos[i] )
9718 gp_XYZ posDir = _pos[i+1] - _pos[i];
9719 double size = posDir.SquareModulus();
9720 if ( size > RealSmall() )
9721 minDot = Min( minDot, ( normal * posDir ) * ( normal * posDir ) / size );
9723 if ( minDot > 0.5 * 0.5 )
9729 //================================================================================
9731 * \brief Print flags
9733 //================================================================================
9735 std::string _LayerEdge::DumpFlags() const
9738 for ( int flag = 1; flag < 0x1000000; flag *= 2 )
9739 if ( _flags & flag )
9741 EFlags f = (EFlags) flag;
9743 case TO_SMOOTH: dump << "TO_SMOOTH"; break;
9744 case MOVED: dump << "MOVED"; break;
9745 case SMOOTHED: dump << "SMOOTHED"; break;
9746 case DIFFICULT: dump << "DIFFICULT"; break;
9747 case ON_CONCAVE_FACE: dump << "ON_CONCAVE_FACE"; break;
9748 case BLOCKED: dump << "BLOCKED"; break;
9749 case INTERSECTED: dump << "INTERSECTED"; break;
9750 case NORMAL_UPDATED: dump << "NORMAL_UPDATED"; break;
9751 case UPD_NORMAL_CONV: dump << "UPD_NORMAL_CONV"; break;
9752 case MARKED: dump << "MARKED"; break;
9753 case MULTI_NORMAL: dump << "MULTI_NORMAL"; break;
9754 case NEAR_BOUNDARY: dump << "NEAR_BOUNDARY"; break;
9755 case SMOOTHED_C1: dump << "SMOOTHED_C1"; break;
9756 case DISTORTED: dump << "DISTORTED"; break;
9757 case RISKY_SWOL: dump << "RISKY_SWOL"; break;
9758 case SHRUNK: dump << "SHRUNK"; break;
9759 case UNUSED_FLAG: dump << "UNUSED_FLAG"; break;
9763 cout << dump << endl;
9768 //================================================================================
9770 * \brief Create layers of prisms
9772 //================================================================================
9774 bool _ViscousBuilder::refine(_SolidData& data)
9776 SMESH_MesherHelper& helper = data.GetHelper();
9777 helper.SetElementsOnShape(false);
9779 Handle(Geom_Curve) curve;
9780 Handle(ShapeAnalysis_Surface) surface;
9781 TopoDS_Edge geomEdge;
9782 TopoDS_Face geomFace;
9783 TopLoc_Location loc;
9786 vector< gp_XYZ > pos3D;
9787 bool isOnEdge, isTooConvexFace = false;
9788 TGeomID prevBaseId = -1;
9789 TNode2Edge* n2eMap = 0;
9790 TNode2Edge::iterator n2e;
9792 // Create intermediate nodes on each _LayerEdge
9794 for ( size_t iS = 0; iS < data._edgesOnShape.size(); ++iS )
9796 _EdgesOnShape& eos = data._edgesOnShape[iS];
9797 if ( eos._edges.empty() ) continue;
9799 if ( eos._edges[0]->_nodes.size() < 2 )
9800 continue; // on _noShrinkShapes
9802 // get data of a shrink shape
9804 geomEdge.Nullify(); geomFace.Nullify();
9805 curve.Nullify(); surface.Nullify();
9806 if ( !eos._sWOL.IsNull() )
9808 isOnEdge = ( eos.SWOLType() == TopAbs_EDGE );
9811 geomEdge = TopoDS::Edge( eos._sWOL );
9812 curve = BRep_Tool::Curve( geomEdge, loc, f,l);
9816 geomFace = TopoDS::Face( eos._sWOL );
9817 surface = helper.GetSurface( geomFace );
9820 else if ( eos.ShapeType() == TopAbs_FACE && eos._toSmooth )
9822 geomFace = TopoDS::Face( eos._shape );
9823 surface = helper.GetSurface( geomFace );
9824 // propagate _toSmooth back to _eosC1, which was unset in findShapesToSmooth()
9825 for ( size_t i = 0; i < eos._eosC1.size(); ++i )
9826 eos._eosC1[ i ]->_toSmooth = true;
9828 isTooConvexFace = false;
9829 if ( _ConvexFace* cf = data.GetConvexFace( eos._shapeID ))
9830 isTooConvexFace = cf->_isTooCurved;
9833 vector< double > segLen;
9834 for ( size_t i = 0; i < eos._edges.size(); ++i )
9836 _LayerEdge& edge = *eos._edges[i];
9837 if ( edge._pos.size() < 2 )
9840 // get accumulated length of segments
9841 segLen.resize( edge._pos.size() );
9843 if ( eos._sWOL.IsNull() )
9845 bool useNormal = true;
9846 bool usePos = false;
9847 bool smoothed = false;
9848 double preci = 0.1 * edge._len;
9849 if ( eos._toSmooth && edge._pos.size() > 2 )
9851 smoothed = edge.GetSmoothedPos( preci );
9855 if ( !surface.IsNull() && !isTooConvexFace ) // edge smoothed on FACE
9857 useNormal = usePos = false;
9858 gp_Pnt2d uv = helper.GetNodeUV( geomFace, edge._nodes[0] );
9859 for ( size_t j = 1; j < edge._pos.size() && !useNormal; ++j )
9861 uv = surface->NextValueOfUV( uv, edge._pos[j], preci );
9862 if ( surface->Gap() < 2. * edge._len )
9863 segLen[j] = surface->Gap();
9869 else if ( !edge.Is( _LayerEdge::NORMAL_UPDATED ))
9871 #ifndef __NODES_AT_POS
9872 useNormal = usePos = false;
9873 edge._pos[1] = edge._pos.back();
9874 edge._pos.resize( 2 );
9876 segLen[ 1 ] = edge._len;
9879 if ( useNormal && edge.Is( _LayerEdge::NORMAL_UPDATED ))
9881 useNormal = usePos = false;
9882 _LayerEdge tmpEdge; // get original _normal
9883 tmpEdge._nodes.push_back( edge._nodes[0] );
9884 if ( !setEdgeData( tmpEdge, eos, helper, data ))
9887 for ( size_t j = 1; j < edge._pos.size(); ++j )
9888 segLen[j] = ( edge._pos[j] - edge._pos[0] ) * tmpEdge._normal;
9892 for ( size_t j = 1; j < edge._pos.size(); ++j )
9893 segLen[j] = ( edge._pos[j] - edge._pos[0] ) * edge._normal;
9897 for ( size_t j = 1; j < edge._pos.size(); ++j )
9898 segLen[j] = segLen[j-1] + ( edge._pos[j-1] - edge._pos[j] ).Modulus();
9902 bool swapped = ( edge._pos.size() > 2 );
9906 for ( size_t j = 1; j < edge._pos.size()-1; ++j )
9907 if ( segLen[j] > segLen.back() )
9909 segLen.erase( segLen.begin() + j );
9910 edge._pos.erase( edge._pos.begin() + j );
9913 else if ( segLen[j] < segLen[j-1] )
9915 std::swap( segLen[j], segLen[j-1] );
9916 std::swap( edge._pos[j], edge._pos[j-1] );
9921 // smooth a path formed by edge._pos
9922 #ifndef __NODES_AT_POS
9923 if (( smoothed ) /*&&
9924 ( eos.ShapeType() == TopAbs_FACE || edge.Is( _LayerEdge::SMOOTHED_C1 ))*/)
9925 edge.SmoothPos( segLen, preci );
9928 else if ( eos._isRegularSWOL ) // usual SWOL
9930 if ( edge.Is( _LayerEdge::SMOOTHED ))
9932 SMESH_NodeXYZ p0( edge._nodes[0] );
9933 for ( size_t j = 1; j < edge._pos.size(); ++j )
9935 gp_XYZ pj = surface->Value( edge._pos[j].X(), edge._pos[j].Y() ).XYZ();
9936 segLen[j] = ( pj - p0 ) * edge._normal;
9941 for ( size_t j = 1; j < edge._pos.size(); ++j )
9942 segLen[j] = segLen[j-1] + (edge._pos[j-1] - edge._pos[j] ).Modulus();
9945 else if ( !surface.IsNull() ) // SWOL surface with singularities
9947 pos3D.resize( edge._pos.size() );
9948 for ( size_t j = 0; j < edge._pos.size(); ++j )
9949 pos3D[j] = surface->Value( edge._pos[j].X(), edge._pos[j].Y() ).XYZ();
9951 for ( size_t j = 1; j < edge._pos.size(); ++j )
9952 segLen[j] = segLen[j-1] + ( pos3D[j-1] - pos3D[j] ).Modulus();
9955 // allocate memory for new nodes if it is not yet refined
9956 const SMDS_MeshNode* tgtNode = edge._nodes.back();
9957 if ( edge._nodes.size() == 2 )
9959 #ifdef __NODES_AT_POS
9960 int nbNodes = edge._pos.size();
9962 int nbNodes = eos._hyp.GetNumberLayers() + 1;
9964 edge._nodes.resize( nbNodes, 0 );
9966 edge._nodes.back() = tgtNode;
9968 // restore shapePos of the last node by already treated _LayerEdge of another _SolidData
9969 const TGeomID baseShapeId = edge._nodes[0]->getshapeId();
9970 if ( baseShapeId != prevBaseId )
9972 map< TGeomID, TNode2Edge* >::iterator s2ne = data._s2neMap.find( baseShapeId );
9973 n2eMap = ( s2ne == data._s2neMap.end() ) ? 0 : s2ne->second;
9974 prevBaseId = baseShapeId;
9976 _LayerEdge* edgeOnSameNode = 0;
9977 bool useExistingPos = false;
9978 if ( n2eMap && (( n2e = n2eMap->find( edge._nodes[0] )) != n2eMap->end() ))
9980 edgeOnSameNode = n2e->second;
9981 useExistingPos = ( edgeOnSameNode->_len < edge._len );
9982 const gp_XYZ& otherTgtPos = edgeOnSameNode->_pos.back();
9983 SMDS_PositionPtr lastPos = tgtNode->GetPosition();
9986 SMDS_EdgePosition* epos = static_cast<SMDS_EdgePosition*>( lastPos );
9987 epos->SetUParameter( otherTgtPos.X() );
9991 SMDS_FacePosition* fpos = static_cast<SMDS_FacePosition*>( lastPos );
9992 fpos->SetUParameter( otherTgtPos.X() );
9993 fpos->SetVParameter( otherTgtPos.Y() );
9996 // calculate height of the first layer
9998 const double T = segLen.back(); //data._hyp.GetTotalThickness();
9999 const double f = eos._hyp.GetStretchFactor();
10000 const int N = eos._hyp.GetNumberLayers();
10001 const double fPowN = pow( f, N );
10002 if ( fPowN - 1 <= numeric_limits<double>::min() )
10005 h0 = T * ( f - 1 )/( fPowN - 1 );
10007 const double zeroLen = std::numeric_limits<double>::min();
10009 // create intermediate nodes
10010 double hSum = 0, hi = h0/f;
10012 for ( size_t iStep = 1; iStep < edge._nodes.size(); ++iStep )
10014 // compute an intermediate position
10017 while ( hSum > segLen[iSeg] && iSeg < segLen.size()-1 )
10019 int iPrevSeg = iSeg-1;
10020 while ( fabs( segLen[iPrevSeg] - segLen[iSeg]) <= zeroLen && iPrevSeg > 0 )
10022 double r = ( segLen[iSeg] - hSum ) / ( segLen[iSeg] - segLen[iPrevSeg] );
10023 gp_Pnt pos = r * edge._pos[iPrevSeg] + (1-r) * edge._pos[iSeg];
10024 #ifdef __NODES_AT_POS
10025 pos = edge._pos[ iStep ];
10027 SMDS_MeshNode*& node = const_cast< SMDS_MeshNode*& >( edge._nodes[ iStep ]);
10028 if ( !eos._sWOL.IsNull() )
10030 // compute XYZ by parameters <pos>
10035 pos = curve->Value( u ).Transformed(loc);
10037 else if ( eos._isRegularSWOL )
10039 uv.SetCoord( pos.X(), pos.Y() );
10041 pos = surface->Value( pos.X(), pos.Y() );
10045 uv.SetCoord( pos.X(), pos.Y() );
10046 gp_Pnt p = r * pos3D[ iPrevSeg ] + (1-r) * pos3D[ iSeg ];
10047 uv = surface->NextValueOfUV( uv, p, BRep_Tool::Tolerance( geomFace )).XY();
10049 pos = surface->Value( uv );
10052 // create or update the node
10055 node = helper.AddNode( pos.X(), pos.Y(), pos.Z());
10056 if ( !eos._sWOL.IsNull() )
10059 getMeshDS()->SetNodeOnEdge( node, geomEdge, u );
10061 getMeshDS()->SetNodeOnFace( node, geomFace, uv.X(), uv.Y() );
10065 getMeshDS()->SetNodeInVolume( node, helper.GetSubShapeID() );
10070 if ( !eos._sWOL.IsNull() )
10072 // make average pos from new and current parameters
10075 //u = 0.5 * ( u + helper.GetNodeU( geomEdge, node ));
10076 if ( useExistingPos )
10077 u = helper.GetNodeU( geomEdge, node );
10078 pos = curve->Value( u ).Transformed(loc);
10080 SMDS_EdgePosition* epos = static_cast<SMDS_EdgePosition*>( node->GetPosition() );
10081 epos->SetUParameter( u );
10085 //uv = 0.5 * ( uv + helper.GetNodeUV( geomFace, node ));
10086 if ( useExistingPos )
10087 uv = helper.GetNodeUV( geomFace, node );
10088 pos = surface->Value( uv );
10090 SMDS_FacePosition* fpos = static_cast<SMDS_FacePosition*>( node->GetPosition() );
10091 fpos->SetUParameter( uv.X() );
10092 fpos->SetVParameter( uv.Y() );
10095 node->setXYZ( pos.X(), pos.Y(), pos.Z() );
10097 } // loop on edge._nodes
10099 if ( !eos._sWOL.IsNull() ) // prepare for shrink()
10102 edge._pos.back().SetCoord( u, 0,0);
10104 edge._pos.back().SetCoord( uv.X(), uv.Y() ,0);
10106 if ( edgeOnSameNode )
10107 edgeOnSameNode->_pos.back() = edge._pos.back();
10110 } // loop on eos._edges to create nodes
10113 if ( !getMeshDS()->IsEmbeddedMode() )
10114 // Log node movement
10115 for ( size_t i = 0; i < eos._edges.size(); ++i )
10117 SMESH_TNodeXYZ p ( eos._edges[i]->_nodes.back() );
10118 getMeshDS()->MoveNode( p._node, p.X(), p.Y(), p.Z() );
10125 helper.SetElementsOnShape(true);
10127 vector< vector<const SMDS_MeshNode*>* > nnVec;
10128 set< vector<const SMDS_MeshNode*>* > nnSet;
10129 set< int > degenEdgeInd;
10130 vector<const SMDS_MeshElement*> degenVols;
10132 TopExp_Explorer exp( data._solid, TopAbs_FACE );
10133 for ( ; exp.More(); exp.Next() )
10135 const TGeomID faceID = getMeshDS()->ShapeToIndex( exp.Current() );
10136 if ( data._ignoreFaceIds.count( faceID ))
10138 const bool isReversedFace = data._reversedFaceIds.count( faceID );
10139 SMESHDS_SubMesh* fSubM = getMeshDS()->MeshElements( exp.Current() );
10140 SMDS_ElemIteratorPtr fIt = fSubM->GetElements();
10141 while ( fIt->more() )
10143 const SMDS_MeshElement* face = fIt->next();
10144 const int nbNodes = face->NbCornerNodes();
10145 nnVec.resize( nbNodes );
10147 degenEdgeInd.clear();
10148 size_t maxZ = 0, minZ = std::numeric_limits<size_t>::max();
10149 SMDS_NodeIteratorPtr nIt = face->nodeIterator();
10150 for ( int iN = 0; iN < nbNodes; ++iN )
10152 const SMDS_MeshNode* n = nIt->next();
10153 _LayerEdge* edge = data._n2eMap[ n ];
10154 const int i = isReversedFace ? nbNodes-1-iN : iN;
10155 nnVec[ i ] = & edge->_nodes;
10156 maxZ = std::max( maxZ, nnVec[ i ]->size() );
10157 minZ = std::min( minZ, nnVec[ i ]->size() );
10159 if ( helper.HasDegeneratedEdges() )
10160 nnSet.insert( nnVec[ i ]);
10165 if ( 0 < nnSet.size() && nnSet.size() < 3 )
10173 for ( size_t iZ = 1; iZ < minZ; ++iZ )
10174 helper.AddVolume( (*nnVec[0])[iZ-1], (*nnVec[1])[iZ-1], (*nnVec[2])[iZ-1],
10175 (*nnVec[0])[iZ], (*nnVec[1])[iZ], (*nnVec[2])[iZ]);
10177 for ( size_t iZ = minZ; iZ < maxZ; ++iZ )
10179 for ( int iN = 0; iN < nbNodes; ++iN )
10180 if ( nnVec[ iN ]->size() < iZ+1 )
10181 degenEdgeInd.insert( iN );
10183 if ( degenEdgeInd.size() == 1 ) // PYRAM
10185 int i2 = *degenEdgeInd.begin();
10186 int i0 = helper.WrapIndex( i2 - 1, nbNodes );
10187 int i1 = helper.WrapIndex( i2 + 1, nbNodes );
10188 helper.AddVolume( (*nnVec[i0])[iZ-1], (*nnVec[i1])[iZ-1],
10189 (*nnVec[i1])[iZ ], (*nnVec[i0])[iZ ], (*nnVec[i2]).back());
10193 int i3 = !degenEdgeInd.count(0) ? 0 : !degenEdgeInd.count(1) ? 1 : 2;
10194 helper.AddVolume( (*nnVec[ 0 ])[ i3 == 0 ? iZ-1 : nnVec[0]->size()-1 ],
10195 (*nnVec[ 1 ])[ i3 == 1 ? iZ-1 : nnVec[1]->size()-1 ],
10196 (*nnVec[ 2 ])[ i3 == 2 ? iZ-1 : nnVec[2]->size()-1 ],
10197 (*nnVec[ i3 ])[ iZ ]);
10205 for ( size_t iZ = 1; iZ < minZ; ++iZ )
10206 helper.AddVolume( (*nnVec[0])[iZ-1], (*nnVec[1])[iZ-1],
10207 (*nnVec[2])[iZ-1], (*nnVec[3])[iZ-1],
10208 (*nnVec[0])[iZ], (*nnVec[1])[iZ],
10209 (*nnVec[2])[iZ], (*nnVec[3])[iZ]);
10211 for ( size_t iZ = minZ; iZ < maxZ; ++iZ )
10213 for ( int iN = 0; iN < nbNodes; ++iN )
10214 if ( nnVec[ iN ]->size() < iZ+1 )
10215 degenEdgeInd.insert( iN );
10217 switch ( degenEdgeInd.size() )
10221 int i2 = *degenEdgeInd.begin();
10222 int i3 = *degenEdgeInd.rbegin();
10223 bool ok = ( i3 - i2 == 1 );
10224 if ( i2 == 0 && i3 == 3 ) { i2 = 3; i3 = 0; ok = true; }
10225 int i0 = helper.WrapIndex( i3 + 1, nbNodes );
10226 int i1 = helper.WrapIndex( i0 + 1, nbNodes );
10228 const SMDS_MeshElement* vol =
10229 helper.AddVolume( nnVec[i3]->back(), (*nnVec[i0])[iZ], (*nnVec[i0])[iZ-1],
10230 nnVec[i2]->back(), (*nnVec[i1])[iZ], (*nnVec[i1])[iZ-1]);
10232 degenVols.push_back( vol );
10236 default: // degen HEX
10238 const SMDS_MeshElement* vol =
10239 helper.AddVolume( nnVec[0]->size() > iZ-1 ? (*nnVec[0])[iZ-1] : nnVec[0]->back(),
10240 nnVec[1]->size() > iZ-1 ? (*nnVec[1])[iZ-1] : nnVec[1]->back(),
10241 nnVec[2]->size() > iZ-1 ? (*nnVec[2])[iZ-1] : nnVec[2]->back(),
10242 nnVec[3]->size() > iZ-1 ? (*nnVec[3])[iZ-1] : nnVec[3]->back(),
10243 nnVec[0]->size() > iZ ? (*nnVec[0])[iZ] : nnVec[0]->back(),
10244 nnVec[1]->size() > iZ ? (*nnVec[1])[iZ] : nnVec[1]->back(),
10245 nnVec[2]->size() > iZ ? (*nnVec[2])[iZ] : nnVec[2]->back(),
10246 nnVec[3]->size() > iZ ? (*nnVec[3])[iZ] : nnVec[3]->back());
10247 degenVols.push_back( vol );
10254 return error("Not supported type of element", data._index);
10256 } // switch ( nbNodes )
10257 } // while ( fIt->more() )
10260 if ( !degenVols.empty() )
10262 SMESH_ComputeErrorPtr& err = _mesh->GetSubMesh( data._solid )->GetComputeError();
10263 if ( !err || err->IsOK() )
10265 err.reset( new SMESH_ComputeError( COMPERR_WARNING,
10266 "Bad quality volumes created" ));
10267 err->myBadElements.insert( err->myBadElements.end(),
10268 degenVols.begin(),degenVols.end() );
10275 //================================================================================
10277 * \brief Shrink 2D mesh on faces to let space for inflated layers
10279 //================================================================================
10281 bool _ViscousBuilder::shrink(_SolidData& theData)
10283 // make map of (ids of FACEs to shrink mesh on) to (list of _SolidData containing
10284 // _LayerEdge's inflated along FACE or EDGE)
10285 map< TGeomID, list< _SolidData* > > f2sdMap;
10286 for ( size_t i = 0 ; i < _sdVec.size(); ++i )
10288 _SolidData& data = _sdVec[i];
10289 map< TGeomID, TopoDS_Shape >::iterator s2s = data._shrinkShape2Shape.begin();
10290 for (; s2s != data._shrinkShape2Shape.end(); ++s2s )
10291 if ( s2s->second.ShapeType() == TopAbs_FACE && !_shrinkedFaces.Contains( s2s->second ))
10293 f2sdMap[ getMeshDS()->ShapeToIndex( s2s->second )].push_back( &data );
10295 // Put mesh faces on the shrinked FACE to the proxy sub-mesh to avoid
10296 // usage of mesh faces made in addBoundaryElements() by the 3D algo or
10297 // by StdMeshers_QuadToTriaAdaptor
10298 if ( SMESHDS_SubMesh* smDS = getMeshDS()->MeshElements( s2s->second ))
10300 SMESH_ProxyMesh::SubMesh* proxySub =
10301 data._proxyMesh->getFaceSubM( TopoDS::Face( s2s->second ), /*create=*/true);
10302 if ( proxySub->NbElements() == 0 )
10304 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
10305 while ( fIt->more() )
10307 const SMDS_MeshElement* f = fIt->next();
10308 // as a result 3D algo will use elements from proxySub and not from smDS
10309 proxySub->AddElement( f );
10310 f->setIsMarked( true );
10312 // Mark nodes on the FACE to discriminate them from nodes
10313 // added by addBoundaryElements(); marked nodes are to be smoothed while shrink()
10314 for ( int iN = 0, nbN = f->NbNodes(); iN < nbN; ++iN )
10316 const SMDS_MeshNode* n = f->GetNode( iN );
10317 if ( n->GetPosition()->GetDim() == 2 )
10318 n->setIsMarked( true );
10326 SMESH_MesherHelper helper( *_mesh );
10327 helper.ToFixNodeParameters( true );
10330 map< TGeomID, _Shrinker1D > e2shrMap;
10331 vector< _EdgesOnShape* > subEOS;
10332 vector< _LayerEdge* > lEdges;
10334 // loop on FACEs to srink mesh on
10335 map< TGeomID, list< _SolidData* > >::iterator f2sd = f2sdMap.begin();
10336 for ( ; f2sd != f2sdMap.end(); ++f2sd )
10338 list< _SolidData* > & dataList = f2sd->second;
10339 if ( dataList.front()->_n2eMap.empty() ||
10340 dataList.back() ->_n2eMap.empty() )
10341 continue; // not yet computed
10342 if ( dataList.front() != &theData &&
10343 dataList.back() != &theData )
10346 _SolidData& data = *dataList.front();
10347 _SolidData* data2 = dataList.size() > 1 ? dataList.back() : 0;
10348 const TopoDS_Face& F = TopoDS::Face( getMeshDS()->IndexToShape( f2sd->first ));
10349 SMESH_subMesh* sm = _mesh->GetSubMesh( F );
10350 SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
10352 Handle(Geom_Surface) surface = BRep_Tool::Surface( F );
10354 _shrinkedFaces.Add( F );
10355 helper.SetSubShape( F );
10357 // ===========================
10358 // Prepare data for shrinking
10359 // ===========================
10361 // Collect nodes to smooth (they are marked at the beginning of this method)
10362 vector < const SMDS_MeshNode* > smoothNodes;
10364 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
10365 while ( nIt->more() )
10367 const SMDS_MeshNode* n = nIt->next();
10368 if ( n->isMarked() )
10369 smoothNodes.push_back( n );
10372 // Find out face orientation
10373 double refSign = 1;
10374 const set<TGeomID> ignoreShapes;
10376 if ( !smoothNodes.empty() )
10378 vector<_Simplex> simplices;
10379 _Simplex::GetSimplices( smoothNodes[0], simplices, ignoreShapes );
10380 helper.GetNodeUV( F, simplices[0]._nPrev, 0, &isOkUV ); // fix UV of simplex nodes
10381 helper.GetNodeUV( F, simplices[0]._nNext, 0, &isOkUV );
10382 gp_XY uv = helper.GetNodeUV( F, smoothNodes[0], 0, &isOkUV );
10383 if ( !simplices[0].IsForward(uv, smoothNodes[0], F, helper, refSign ))
10387 // Find _LayerEdge's inflated along F
10391 SMESH_subMeshIteratorPtr subIt = sm->getDependsOnIterator(/*includeSelf=*/false,
10392 /*complexFirst=*/true); //!!!
10393 while ( subIt->more() )
10395 const TGeomID subID = subIt->next()->GetId();
10396 if ( data._noShrinkShapes.count( subID ))
10398 _EdgesOnShape* eos = data.GetShapeEdges( subID );
10399 if ( !eos || eos->_sWOL.IsNull() )
10400 if ( data2 ) // check in adjacent SOLID
10402 eos = data2->GetShapeEdges( subID );
10403 if ( !eos || eos->_sWOL.IsNull() )
10406 subEOS.push_back( eos );
10408 for ( size_t i = 0; i < eos->_edges.size(); ++i )
10410 lEdges.push_back( eos->_edges[ i ] );
10411 prepareEdgeToShrink( *eos->_edges[ i ], *eos, helper, smDS );
10416 dumpFunction(SMESH_Comment("beforeShrinkFace")<<f2sd->first); // debug
10417 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
10418 while ( fIt->more() )
10419 if ( const SMDS_MeshElement* f = fIt->next() )
10420 dumpChangeNodes( f );
10423 // Replace source nodes by target nodes in mesh faces to shrink
10424 dumpFunction(SMESH_Comment("replNodesOnFace")<<f2sd->first); // debug
10425 const SMDS_MeshNode* nodes[20];
10426 for ( size_t iS = 0; iS < subEOS.size(); ++iS )
10428 _EdgesOnShape& eos = * subEOS[ iS ];
10429 for ( size_t i = 0; i < eos._edges.size(); ++i )
10431 _LayerEdge& edge = *eos._edges[i];
10432 const SMDS_MeshNode* srcNode = edge._nodes[0];
10433 const SMDS_MeshNode* tgtNode = edge._nodes.back();
10434 SMDS_ElemIteratorPtr fIt = srcNode->GetInverseElementIterator(SMDSAbs_Face);
10435 while ( fIt->more() )
10437 const SMDS_MeshElement* f = fIt->next();
10438 if ( !smDS->Contains( f ) || !f->isMarked() )
10440 SMDS_NodeIteratorPtr nIt = f->nodeIterator();
10441 for ( int iN = 0; nIt->more(); ++iN )
10443 const SMDS_MeshNode* n = nIt->next();
10444 nodes[iN] = ( n == srcNode ? tgtNode : n );
10446 helper.GetMeshDS()->ChangeElementNodes( f, nodes, f->NbNodes() );
10447 dumpChangeNodes( f );
10453 // find out if a FACE is concave
10454 const bool isConcaveFace = isConcave( F, helper );
10456 // Create _SmoothNode's on face F
10457 vector< _SmoothNode > nodesToSmooth( smoothNodes.size() );
10459 dumpFunction(SMESH_Comment("fixUVOnFace")<<f2sd->first); // debug
10460 const bool sortSimplices = isConcaveFace;
10461 for ( size_t i = 0; i < smoothNodes.size(); ++i )
10463 const SMDS_MeshNode* n = smoothNodes[i];
10464 nodesToSmooth[ i ]._node = n;
10465 // src nodes must be already replaced by tgt nodes to have tgt nodes in _simplices
10466 _Simplex::GetSimplices( n, nodesToSmooth[ i ]._simplices, ignoreShapes, 0, sortSimplices);
10467 // fix up incorrect uv of nodes on the FACE
10468 helper.GetNodeUV( F, n, 0, &isOkUV);
10473 //if ( nodesToSmooth.empty() ) continue;
10475 // Find EDGE's to shrink and set simpices to LayerEdge's
10476 set< _Shrinker1D* > eShri1D;
10478 for ( size_t iS = 0; iS < subEOS.size(); ++iS )
10480 _EdgesOnShape& eos = * subEOS[ iS ];
10481 if ( eos.SWOLType() == TopAbs_EDGE )
10483 SMESH_subMesh* edgeSM = _mesh->GetSubMesh( eos._sWOL );
10484 _Shrinker1D& srinker = e2shrMap[ edgeSM->GetId() ];
10485 eShri1D.insert( & srinker );
10486 srinker.AddEdge( eos._edges[0], eos, helper );
10487 VISCOUS_3D::ToClearSubWithMain( edgeSM, data._solid );
10488 // restore params of nodes on EGDE if the EDGE has been already
10489 // srinked while srinking other FACE
10490 srinker.RestoreParams();
10492 for ( size_t i = 0; i < eos._edges.size(); ++i )
10494 _LayerEdge& edge = * eos._edges[i];
10495 _Simplex::GetSimplices( /*tgtNode=*/edge._nodes.back(), edge._simplices, ignoreShapes );
10497 // additionally mark tgt node; only marked nodes will be used in SetNewLength2d()
10498 // not-marked nodes are those added by refine()
10499 edge._nodes.back()->setIsMarked( true );
10504 bool toFixTria = false; // to improve quality of trias by diagonal swap
10505 if ( isConcaveFace )
10507 const bool hasTria = _mesh->NbTriangles(), hasQuad = _mesh->NbQuadrangles();
10508 if ( hasTria != hasQuad ) {
10509 toFixTria = hasTria;
10512 set<int> nbNodesSet;
10513 SMDS_ElemIteratorPtr fIt = smDS->GetElements();
10514 while ( fIt->more() && nbNodesSet.size() < 2 )
10515 nbNodesSet.insert( fIt->next()->NbCornerNodes() );
10516 toFixTria = ( *nbNodesSet.begin() == 3 );
10520 // ==================
10521 // Perform shrinking
10522 // ==================
10524 bool shrinked = true;
10525 int nbBad, shriStep=0, smooStep=0;
10526 _SmoothNode::SmoothType smoothType
10527 = isConcaveFace ? _SmoothNode::ANGULAR : _SmoothNode::LAPLACIAN;
10528 SMESH_Comment errMsg;
10532 // Move boundary nodes (actually just set new UV)
10533 // -----------------------------------------------
10534 dumpFunction(SMESH_Comment("moveBoundaryOnF")<<f2sd->first<<"_st"<<shriStep ); // debug
10536 for ( size_t iS = 0; iS < subEOS.size(); ++iS )
10538 _EdgesOnShape& eos = * subEOS[ iS ];
10539 for ( size_t i = 0; i < eos._edges.size(); ++i )
10541 shrinked |= eos._edges[i]->SetNewLength2d( surface, F, eos, helper );
10546 // Move nodes on EDGE's
10547 // (XYZ is set as soon as a needed length reached in SetNewLength2d())
10548 set< _Shrinker1D* >::iterator shr = eShri1D.begin();
10549 for ( ; shr != eShri1D.end(); ++shr )
10550 (*shr)->Compute( /*set3D=*/false, helper );
10553 // -----------------
10554 int nbNoImpSteps = 0;
10557 while (( nbNoImpSteps < 5 && nbBad > 0) && moved)
10559 dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
10561 int oldBadNb = nbBad;
10564 // '% 5' minimizes NB FUNCTIONS on viscous_layers_00/B2 case
10565 _SmoothNode::SmoothType smooTy = ( smooStep % 5 ) ? smoothType : _SmoothNode::LAPLACIAN;
10566 for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
10568 moved |= nodesToSmooth[i].Smooth( nbBad, surface, helper, refSign,
10569 smooTy, /*set3D=*/isConcaveFace);
10571 if ( nbBad < oldBadNb )
10581 errMsg << "Can't shrink 2D mesh on face " << f2sd->first;
10582 if ( shriStep > 200 )
10583 errMsg << "Infinite loop at shrinking 2D mesh on face " << f2sd->first;
10584 if ( !errMsg.empty() )
10587 // Fix narrow triangles by swapping diagonals
10588 // ---------------------------------------
10591 set<const SMDS_MeshNode*> usedNodes;
10592 fixBadFaces( F, helper, /*is2D=*/true, shriStep, & usedNodes); // swap diagonals
10594 // update working data
10595 set<const SMDS_MeshNode*>::iterator n;
10596 for ( size_t i = 0; i < nodesToSmooth.size() && !usedNodes.empty(); ++i )
10598 n = usedNodes.find( nodesToSmooth[ i ]._node );
10599 if ( n != usedNodes.end())
10601 _Simplex::GetSimplices( nodesToSmooth[ i ]._node,
10602 nodesToSmooth[ i ]._simplices,
10603 ignoreShapes, NULL,
10604 /*sortSimplices=*/ smoothType == _SmoothNode::ANGULAR );
10605 usedNodes.erase( n );
10608 for ( size_t i = 0; i < lEdges.size() && !usedNodes.empty(); ++i )
10610 n = usedNodes.find( /*tgtNode=*/ lEdges[i]->_nodes.back() );
10611 if ( n != usedNodes.end())
10613 _Simplex::GetSimplices( lEdges[i]->_nodes.back(),
10614 lEdges[i]->_simplices,
10616 usedNodes.erase( n );
10620 // TODO: check effect of this additional smooth
10621 // additional laplacian smooth to increase allowed shrink step
10622 // for ( int st = 1; st; --st )
10624 // dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
10625 // for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
10627 // nodesToSmooth[i].Smooth( nbBad,surface,helper,refSign,
10628 // _SmoothNode::LAPLACIAN,/*set3D=*/false);
10632 } // while ( shrinked )
10634 if ( !errMsg.empty() ) // Try to re-compute the shrink FACE
10636 debugMsg( "Re-compute FACE " << f2sd->first << " because " << errMsg );
10639 SMESHDS_SubMesh* psm = data._proxyMesh->getFaceSubM( F );
10641 vector< const SMDS_MeshElement* > facesToRm;
10644 facesToRm.reserve( psm->NbElements() );
10645 for ( SMDS_ElemIteratorPtr ite = psm->GetElements(); ite->more(); )
10646 facesToRm.push_back( ite->next() );
10648 for ( size_t i = 0 ; i < _sdVec.size(); ++i )
10649 if (( psm = _sdVec[i]._proxyMesh->getFaceSubM( F )))
10652 for ( size_t i = 0; i < facesToRm.size(); ++i )
10653 getMeshDS()->RemoveFreeElement( facesToRm[i], smDS, /*fromGroups=*/false );
10657 TIDSortedNodeSet nodesToKeep; // nodes of _LayerEdge to keep
10658 for ( size_t iS = 0; iS < subEOS.size(); ++iS ) {
10659 for ( size_t i = 0; i < subEOS[iS]->_edges.size(); ++i )
10660 nodesToKeep.insert( ++( subEOS[iS]->_edges[i]->_nodes.begin() ),
10661 subEOS[iS]->_edges[i]->_nodes.end() );
10663 SMDS_NodeIteratorPtr itn = smDS->GetNodes();
10664 while ( itn->more() ) {
10665 const SMDS_MeshNode* n = itn->next();
10666 if ( !nodesToKeep.count( n ))
10667 getMeshDS()->RemoveFreeNode( n, smDS, /*fromGroups=*/false );
10670 // restore position and UV of target nodes
10672 for ( size_t iS = 0; iS < subEOS.size(); ++iS )
10673 for ( size_t i = 0; i < subEOS[iS]->_edges.size(); ++i )
10675 _LayerEdge* edge = subEOS[iS]->_edges[i];
10676 SMDS_MeshNode* tgtNode = const_cast< SMDS_MeshNode*& >( edge->_nodes.back() );
10677 if ( edge->_pos.empty() ||
10678 edge->Is( _LayerEdge::SHRUNK )) continue;
10679 if ( subEOS[iS]->SWOLType() == TopAbs_FACE )
10681 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
10682 pos->SetUParameter( edge->_pos[0].X() );
10683 pos->SetVParameter( edge->_pos[0].Y() );
10684 p = surface->Value( edge->_pos[0].X(), edge->_pos[0].Y() );
10688 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( tgtNode->GetPosition() );
10689 pos->SetUParameter( edge->_pos[0].Coord( U_TGT ));
10690 p = BRepAdaptor_Curve( TopoDS::Edge( subEOS[iS]->_sWOL )).Value( pos->GetUParameter() );
10692 tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
10693 dumpMove( tgtNode );
10695 // shrink EDGE sub-meshes and set proxy sub-meshes
10696 UVPtStructVec uvPtVec;
10697 set< _Shrinker1D* >::iterator shrIt = eShri1D.begin();
10698 for ( shrIt = eShri1D.begin(); shrIt != eShri1D.end(); ++shrIt )
10700 _Shrinker1D* shr = (*shrIt);
10701 shr->Compute( /*set3D=*/true, helper );
10703 // set proxy mesh of EDGEs w/o layers
10704 map< double, const SMDS_MeshNode* > nodes;
10705 SMESH_Algo::GetSortedNodesOnEdge( getMeshDS(), shr->GeomEdge(),/*skipMedium=*/true, nodes);
10706 // remove refinement nodes
10707 const SMDS_MeshNode* sn0 = shr->SrcNode(0), *sn1 = shr->SrcNode(1);
10708 const SMDS_MeshNode* tn0 = shr->TgtNode(0), *tn1 = shr->TgtNode(1);
10709 map< double, const SMDS_MeshNode* >::iterator u2n = nodes.begin();
10710 if ( u2n->second == sn0 || u2n->second == sn1 )
10712 while ( u2n->second != tn0 && u2n->second != tn1 )
10714 nodes.erase( nodes.begin(), u2n );
10716 u2n = --nodes.end();
10717 if ( u2n->second == sn0 || u2n->second == sn1 )
10719 while ( u2n->second != tn0 && u2n->second != tn1 )
10721 nodes.erase( ++u2n, nodes.end() );
10723 // set proxy sub-mesh
10724 uvPtVec.resize( nodes.size() );
10725 u2n = nodes.begin();
10726 BRepAdaptor_Curve2d curve( shr->GeomEdge(), F );
10727 for ( size_t i = 0; i < nodes.size(); ++i, ++u2n )
10729 uvPtVec[ i ].node = u2n->second;
10730 uvPtVec[ i ].param = u2n->first;
10731 uvPtVec[ i ].SetUV( curve.Value( u2n->first ).XY() );
10733 StdMeshers_FaceSide fSide( uvPtVec, F, shr->GeomEdge(), _mesh );
10734 StdMeshers_ViscousLayers2D::SetProxyMeshOfEdge( fSide );
10737 // set proxy mesh of EDGEs with layers
10738 vector< _LayerEdge* > edges;
10739 for ( size_t iS = 0; iS < subEOS.size(); ++iS )
10741 _EdgesOnShape& eos = * subEOS[ iS ];
10742 if ( eos.ShapeType() != TopAbs_EDGE ) continue;
10744 const TopoDS_Edge& E = TopoDS::Edge( eos._shape );
10745 data.SortOnEdge( E, eos._edges );
10748 if ( _EdgesOnShape* eov = data.GetShapeEdges( helper.IthVertex( 0, E, /*CumOri=*/false )))
10749 if ( !eov->_edges.empty() )
10750 edges.push_back( eov->_edges[0] ); // on 1st VERTEX
10752 edges.insert( edges.end(), eos._edges.begin(), eos._edges.end() );
10754 if ( _EdgesOnShape* eov = data.GetShapeEdges( helper.IthVertex( 1, E, /*CumOri=*/false )))
10755 if ( !eov->_edges.empty() )
10756 edges.push_back( eov->_edges[0] ); // on last VERTEX
10758 uvPtVec.resize( edges.size() );
10759 for ( size_t i = 0; i < edges.size(); ++i )
10761 uvPtVec[ i ].node = edges[i]->_nodes.back();
10762 uvPtVec[ i ].param = helper.GetNodeU( E, edges[i]->_nodes[0] );
10763 uvPtVec[ i ].SetUV( helper.GetNodeUV( F, edges[i]->_nodes.back() ));
10765 BRep_Tool::Range( E, uvPtVec[0].param, uvPtVec.back().param );
10766 StdMeshers_FaceSide fSide( uvPtVec, F, E, _mesh );
10767 StdMeshers_ViscousLayers2D::SetProxyMeshOfEdge( fSide );
10769 // temporary clear the FACE sub-mesh from faces made by refine()
10770 vector< const SMDS_MeshElement* > elems;
10771 elems.reserve( smDS->NbElements() + smDS->NbNodes() );
10772 for ( SMDS_ElemIteratorPtr ite = smDS->GetElements(); ite->more(); )
10773 elems.push_back( ite->next() );
10774 for ( SMDS_NodeIteratorPtr ite = smDS->GetNodes(); ite->more(); )
10775 elems.push_back( ite->next() );
10778 // compute the mesh on the FACE
10779 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
10780 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE_SUBMESH );
10782 // re-fill proxy sub-meshes of the FACE
10783 for ( size_t i = 0 ; i < _sdVec.size(); ++i )
10784 if (( psm = _sdVec[i]._proxyMesh->getFaceSubM( F )))
10785 for ( SMDS_ElemIteratorPtr ite = smDS->GetElements(); ite->more(); )
10786 psm->AddElement( ite->next() );
10789 for ( size_t i = 0; i < elems.size(); ++i )
10790 smDS->AddElement( elems[i] );
10792 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK )
10793 return error( errMsg );
10795 } // end of re-meshing in case of failed smoothing
10798 // No wrongly shaped faces remain; final smooth. Set node XYZ.
10799 bool isStructuredFixed = false;
10800 if ( SMESH_2D_Algo* algo = dynamic_cast<SMESH_2D_Algo*>( sm->GetAlgo() ))
10801 isStructuredFixed = algo->FixInternalNodes( *data._proxyMesh, F );
10802 if ( !isStructuredFixed )
10804 if ( isConcaveFace ) // fix narrow faces by swapping diagonals
10805 fixBadFaces( F, helper, /*is2D=*/false, ++shriStep );
10807 for ( int st = 3; st; --st )
10810 case 1: smoothType = _SmoothNode::LAPLACIAN; break;
10811 case 2: smoothType = _SmoothNode::LAPLACIAN; break;
10812 case 3: smoothType = _SmoothNode::ANGULAR; break;
10814 dumpFunction(SMESH_Comment("shrinkFace")<<f2sd->first<<"_st"<<++smooStep); // debug
10815 for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
10817 nodesToSmooth[i].Smooth( nbBad,surface,helper,refSign,
10818 smoothType,/*set3D=*/st==1 );
10823 if ( !getMeshDS()->IsEmbeddedMode() )
10824 // Log node movement
10825 for ( size_t i = 0; i < nodesToSmooth.size(); ++i )
10827 SMESH_TNodeXYZ p ( nodesToSmooth[i]._node );
10828 getMeshDS()->MoveNode( nodesToSmooth[i]._node, p.X(), p.Y(), p.Z() );
10832 // Set an event listener to clear FACE sub-mesh together with SOLID sub-mesh
10833 VISCOUS_3D::ToClearSubWithMain( sm, data._solid );
10835 VISCOUS_3D::ToClearSubWithMain( sm, data2->_solid );
10837 } // loop on FACES to srink mesh on
10840 // Replace source nodes by target nodes in shrinked mesh edges
10842 map< int, _Shrinker1D >::iterator e2shr = e2shrMap.begin();
10843 for ( ; e2shr != e2shrMap.end(); ++e2shr )
10844 e2shr->second.SwapSrcTgtNodes( getMeshDS() );
10849 //================================================================================
10851 * \brief Computes 2d shrink direction and finds nodes limiting shrinking
10853 //================================================================================
10855 bool _ViscousBuilder::prepareEdgeToShrink( _LayerEdge& edge,
10856 _EdgesOnShape& eos,
10857 SMESH_MesherHelper& helper,
10858 const SMESHDS_SubMesh* faceSubMesh)
10860 const SMDS_MeshNode* srcNode = edge._nodes[0];
10861 const SMDS_MeshNode* tgtNode = edge._nodes.back();
10863 if ( eos.SWOLType() == TopAbs_FACE )
10865 if ( tgtNode->GetPosition()->GetDim() != 2 ) // not inflated edge
10868 edge.Set( _LayerEdge::SHRUNK );
10869 return srcNode == tgtNode;
10871 gp_XY srcUV ( edge._pos[0].X(), edge._pos[0].Y() ); //helper.GetNodeUV( F, srcNode );
10872 gp_XY tgtUV = edge.LastUV( TopoDS::Face( eos._sWOL ), eos ); //helper.GetNodeUV( F, tgtNode );
10873 gp_Vec2d uvDir( srcUV, tgtUV );
10874 double uvLen = uvDir.Magnitude();
10876 edge._normal.SetCoord( uvDir.X(),uvDir.Y(), 0 );
10879 //edge._pos.resize(1);
10880 edge._pos[0].SetCoord( tgtUV.X(), tgtUV.Y(), 0 );
10882 // set UV of source node to target node
10883 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
10884 pos->SetUParameter( srcUV.X() );
10885 pos->SetVParameter( srcUV.Y() );
10887 else // _sWOL is TopAbs_EDGE
10889 if ( tgtNode->GetPosition()->GetDim() != 1 ) // not inflated edge
10892 edge.Set( _LayerEdge::SHRUNK );
10893 return srcNode == tgtNode;
10895 const TopoDS_Edge& E = TopoDS::Edge( eos._sWOL );
10896 SMESHDS_SubMesh* edgeSM = getMeshDS()->MeshElements( E );
10897 if ( !edgeSM || edgeSM->NbElements() == 0 )
10898 return error(SMESH_Comment("Not meshed EDGE ") << getMeshDS()->ShapeToIndex( E ));
10900 const SMDS_MeshNode* n2 = 0;
10901 SMDS_ElemIteratorPtr eIt = srcNode->GetInverseElementIterator(SMDSAbs_Edge);
10902 while ( eIt->more() && !n2 )
10904 const SMDS_MeshElement* e = eIt->next();
10905 if ( !edgeSM->Contains(e)) continue;
10906 n2 = e->GetNode( 0 );
10907 if ( n2 == srcNode ) n2 = e->GetNode( 1 );
10910 return error(SMESH_Comment("Wrongly meshed EDGE ") << getMeshDS()->ShapeToIndex( E ));
10912 double uSrc = helper.GetNodeU( E, srcNode, n2 );
10913 double uTgt = helper.GetNodeU( E, tgtNode, srcNode );
10914 double u2 = helper.GetNodeU( E, n2, srcNode );
10916 //edge._pos.clear();
10918 if ( fabs( uSrc-uTgt ) < 0.99 * fabs( uSrc-u2 ))
10920 // tgtNode is located so that it does not make faces with wrong orientation
10921 edge.Set( _LayerEdge::SHRUNK );
10924 //edge._pos.resize(1);
10925 edge._pos[0].SetCoord( U_TGT, uTgt );
10926 edge._pos[0].SetCoord( U_SRC, uSrc );
10927 edge._pos[0].SetCoord( LEN_TGT, fabs( uSrc-uTgt ));
10929 edge._simplices.resize( 1 );
10930 edge._simplices[0]._nPrev = n2;
10932 // set U of source node to the target node
10933 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( tgtNode->GetPosition() );
10934 pos->SetUParameter( uSrc );
10939 //================================================================================
10941 * \brief Restore position of a sole node of a _LayerEdge based on _noShrinkShapes
10943 //================================================================================
10945 void _ViscousBuilder::restoreNoShrink( _LayerEdge& edge ) const
10947 if ( edge._nodes.size() == 1 )
10952 const SMDS_MeshNode* srcNode = edge._nodes[0];
10953 TopoDS_Shape S = SMESH_MesherHelper::GetSubShapeByNode( srcNode, getMeshDS() );
10954 if ( S.IsNull() ) return;
10958 switch ( S.ShapeType() )
10963 TopLoc_Location loc;
10964 Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( S ), loc, f, l );
10965 if ( curve.IsNull() ) return;
10966 SMDS_EdgePosition* ePos = static_cast<SMDS_EdgePosition*>( srcNode->GetPosition() );
10967 p = curve->Value( ePos->GetUParameter() );
10970 case TopAbs_VERTEX:
10972 p = BRep_Tool::Pnt( TopoDS::Vertex( S ));
10977 getMeshDS()->MoveNode( srcNode, p.X(), p.Y(), p.Z() );
10978 dumpMove( srcNode );
10982 //================================================================================
10984 * \brief Try to fix triangles with high aspect ratio by swaping diagonals
10986 //================================================================================
10988 void _ViscousBuilder::fixBadFaces(const TopoDS_Face& F,
10989 SMESH_MesherHelper& helper,
10992 set<const SMDS_MeshNode*> * involvedNodes)
10994 SMESH::Controls::AspectRatio qualifier;
10995 SMESH::Controls::TSequenceOfXYZ points(3), points1(3), points2(3);
10996 const double maxAspectRatio = is2D ? 4. : 2;
10997 _NodeCoordHelper xyz( F, helper, is2D );
10999 // find bad triangles
11001 vector< const SMDS_MeshElement* > badTrias;
11002 vector< double > badAspects;
11003 SMESHDS_SubMesh* sm = helper.GetMeshDS()->MeshElements( F );
11004 SMDS_ElemIteratorPtr fIt = sm->GetElements();
11005 while ( fIt->more() )
11007 const SMDS_MeshElement * f = fIt->next();
11008 if ( f->NbCornerNodes() != 3 ) continue;
11009 for ( int iP = 0; iP < 3; ++iP ) points(iP+1) = xyz( f->GetNode(iP));
11010 double aspect = qualifier.GetValue( points );
11011 if ( aspect > maxAspectRatio )
11013 badTrias.push_back( f );
11014 badAspects.push_back( aspect );
11019 dumpFunction(SMESH_Comment("beforeSwapDiagonals_F")<<helper.GetSubShapeID());
11020 SMDS_ElemIteratorPtr fIt = sm->GetElements();
11021 while ( fIt->more() )
11023 const SMDS_MeshElement * f = fIt->next();
11024 if ( f->NbCornerNodes() == 3 )
11025 dumpChangeNodes( f );
11029 if ( badTrias.empty() )
11032 // find couples of faces to swap diagonal
11034 typedef pair < const SMDS_MeshElement* , const SMDS_MeshElement* > T2Trias;
11035 vector< T2Trias > triaCouples;
11037 TIDSortedElemSet involvedFaces, emptySet;
11038 for ( size_t iTia = 0; iTia < badTrias.size(); ++iTia )
11041 double aspRatio [3];
11044 if ( !involvedFaces.insert( badTrias[iTia] ).second )
11046 for ( int iP = 0; iP < 3; ++iP )
11047 points(iP+1) = xyz( badTrias[iTia]->GetNode(iP));
11049 // find triangles adjacent to badTrias[iTia] with better aspect ratio after diag-swaping
11050 int bestCouple = -1;
11051 for ( int iSide = 0; iSide < 3; ++iSide )
11053 const SMDS_MeshNode* n1 = badTrias[iTia]->GetNode( iSide );
11054 const SMDS_MeshNode* n2 = badTrias[iTia]->GetNode(( iSide+1 ) % 3 );
11055 trias [iSide].first = badTrias[iTia];
11056 trias [iSide].second = SMESH_MeshAlgos::FindFaceInSet( n1, n2, emptySet, involvedFaces,
11058 if (( ! trias[iSide].second ) ||
11059 ( trias[iSide].second->NbCornerNodes() != 3 ) ||
11060 ( ! sm->Contains( trias[iSide].second )))
11063 // aspect ratio of an adjacent tria
11064 for ( int iP = 0; iP < 3; ++iP )
11065 points2(iP+1) = xyz( trias[iSide].second->GetNode(iP));
11066 double aspectInit = qualifier.GetValue( points2 );
11068 // arrange nodes as after diag-swaping
11069 if ( helper.WrapIndex( i1+1, 3 ) == i2 )
11070 i3 = helper.WrapIndex( i1-1, 3 );
11072 i3 = helper.WrapIndex( i1+1, 3 );
11074 points1( 1+ iSide ) = points2( 1+ i3 );
11075 points2( 1+ i2 ) = points1( 1+ ( iSide+2 ) % 3 );
11077 // aspect ratio after diag-swaping
11078 aspRatio[ iSide ] = qualifier.GetValue( points1 ) + qualifier.GetValue( points2 );
11079 if ( aspRatio[ iSide ] > aspectInit + badAspects[ iTia ] )
11082 // prevent inversion of a triangle
11083 gp_Vec norm1 = gp_Vec( points1(1), points1(3) ) ^ gp_Vec( points1(1), points1(2) );
11084 gp_Vec norm2 = gp_Vec( points2(1), points2(3) ) ^ gp_Vec( points2(1), points2(2) );
11085 if ( norm1 * norm2 < 0. && norm1.Angle( norm2 ) > 70./180.*M_PI )
11088 if ( bestCouple < 0 || aspRatio[ bestCouple ] > aspRatio[ iSide ] )
11089 bestCouple = iSide;
11092 if ( bestCouple >= 0 )
11094 triaCouples.push_back( trias[bestCouple] );
11095 involvedFaces.insert ( trias[bestCouple].second );
11099 involvedFaces.erase( badTrias[iTia] );
11102 if ( triaCouples.empty() )
11107 SMESH_MeshEditor editor( helper.GetMesh() );
11108 dumpFunction(SMESH_Comment("beforeSwapDiagonals_F")<<helper.GetSubShapeID()<<"_"<<step);
11109 for ( size_t i = 0; i < triaCouples.size(); ++i )
11111 dumpChangeNodes( triaCouples[i].first );
11112 dumpChangeNodes( triaCouples[i].second );
11113 editor.InverseDiag( triaCouples[i].first, triaCouples[i].second );
11116 if ( involvedNodes )
11117 for ( size_t i = 0; i < triaCouples.size(); ++i )
11119 involvedNodes->insert( triaCouples[i].first->begin_nodes(),
11120 triaCouples[i].first->end_nodes() );
11121 involvedNodes->insert( triaCouples[i].second->begin_nodes(),
11122 triaCouples[i].second->end_nodes() );
11125 // just for debug dump resulting triangles
11126 dumpFunction(SMESH_Comment("swapDiagonals_F")<<helper.GetSubShapeID()<<"_"<<step);
11127 for ( size_t i = 0; i < triaCouples.size(); ++i )
11129 dumpChangeNodes( triaCouples[i].first );
11130 dumpChangeNodes( triaCouples[i].second );
11134 //================================================================================
11136 * \brief Move target node to it's final position on the FACE during shrinking
11138 //================================================================================
11140 bool _LayerEdge::SetNewLength2d( Handle(Geom_Surface)& surface,
11141 const TopoDS_Face& F,
11142 _EdgesOnShape& eos,
11143 SMESH_MesherHelper& helper )
11146 return false; // already at the target position
11148 SMDS_MeshNode* tgtNode = const_cast< SMDS_MeshNode*& >( _nodes.back() );
11150 if ( eos.SWOLType() == TopAbs_FACE )
11152 gp_XY curUV = helper.GetNodeUV( F, tgtNode );
11153 gp_Pnt2d tgtUV( _pos[0].X(), _pos[0].Y() );
11154 gp_Vec2d uvDir( _normal.X(), _normal.Y() );
11155 const double uvLen = tgtUV.Distance( curUV );
11156 const double kSafe = Max( 0.5, 1. - 0.1 * _simplices.size() );
11158 // Select shrinking step such that not to make faces with wrong orientation.
11159 double stepSize = 1e100;
11160 for ( size_t i = 0; i < _simplices.size(); ++i )
11162 if ( !_simplices[i]._nPrev->isMarked() ||
11163 !_simplices[i]._nNext->isMarked() )
11164 continue; // simplex of quadrangle created by addBoundaryElements()
11166 // find intersection of 2 lines: curUV-tgtUV and that connecting simplex nodes
11167 gp_XY uvN1 = helper.GetNodeUV( F, _simplices[i]._nPrev );
11168 gp_XY uvN2 = helper.GetNodeUV( F, _simplices[i]._nNext );
11169 gp_XY dirN = uvN2 - uvN1;
11170 double det = uvDir.Crossed( dirN );
11171 if ( Abs( det ) < std::numeric_limits<double>::min() ) continue;
11172 gp_XY dirN2Cur = curUV - uvN1;
11173 double step = dirN.Crossed( dirN2Cur ) / det;
11175 stepSize = Min( step, stepSize );
11178 if ( uvLen <= stepSize )
11184 else if ( stepSize > 0 )
11186 newUV = curUV + uvDir.XY() * stepSize * kSafe;
11192 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( tgtNode->GetPosition() );
11193 pos->SetUParameter( newUV.X() );
11194 pos->SetVParameter( newUV.Y() );
11197 gp_Pnt p = surface->Value( newUV.X(), newUV.Y() );
11198 tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
11199 dumpMove( tgtNode );
11202 else // _sWOL is TopAbs_EDGE
11204 const TopoDS_Edge& E = TopoDS::Edge( eos._sWOL );
11205 const SMDS_MeshNode* n2 = _simplices[0]._nPrev;
11206 SMDS_EdgePosition* tgtPos = static_cast<SMDS_EdgePosition*>( tgtNode->GetPosition() );
11208 const double u2 = helper.GetNodeU( E, n2, tgtNode );
11209 const double uSrc = _pos[0].Coord( U_SRC );
11210 const double lenTgt = _pos[0].Coord( LEN_TGT );
11212 double newU = _pos[0].Coord( U_TGT );
11213 if ( lenTgt < 0.99 * fabs( uSrc-u2 )) // n2 got out of src-tgt range
11215 Set( _LayerEdge::SHRUNK );
11220 newU = 0.1 * tgtPos->GetUParameter() + 0.9 * u2;
11222 tgtPos->SetUParameter( newU );
11224 gp_XY newUV = helper.GetNodeUV( F, tgtNode, _nodes[0]);
11225 gp_Pnt p = surface->Value( newUV.X(), newUV.Y() );
11226 tgtNode->setXYZ( p.X(), p.Y(), p.Z() );
11227 dumpMove( tgtNode );
11234 //================================================================================
11236 * \brief Perform smooth on the FACE
11237 * \retval bool - true if the node has been moved
11239 //================================================================================
11241 bool _SmoothNode::Smooth(int& nbBad,
11242 Handle(Geom_Surface)& surface,
11243 SMESH_MesherHelper& helper,
11244 const double refSign,
11248 const TopoDS_Face& face = TopoDS::Face( helper.GetSubShape() );
11250 // get uv of surrounding nodes
11251 vector<gp_XY> uv( _simplices.size() );
11252 for ( size_t i = 0; i < _simplices.size(); ++i )
11253 uv[i] = helper.GetNodeUV( face, _simplices[i]._nPrev, _node );
11255 // compute new UV for the node
11256 gp_XY newPos (0,0);
11257 if ( how == TFI && _simplices.size() == 4 )
11260 for ( size_t i = 0; i < _simplices.size(); ++i )
11261 if ( _simplices[i]._nOpp )
11262 corners[i] = helper.GetNodeUV( face, _simplices[i]._nOpp, _node );
11264 throw SALOME_Exception(LOCALIZED("TFI smoothing: _Simplex::_nOpp not set!"));
11266 newPos = helper.calcTFI ( 0.5, 0.5,
11267 corners[0], corners[1], corners[2], corners[3],
11268 uv[1], uv[2], uv[3], uv[0] );
11270 else if ( how == ANGULAR )
11272 newPos = computeAngularPos( uv, helper.GetNodeUV( face, _node ), refSign );
11274 else if ( how == CENTROIDAL && _simplices.size() > 3 )
11276 // average centers of diagonals wieghted with their reciprocal lengths
11277 if ( _simplices.size() == 4 )
11279 double w1 = 1. / ( uv[2]-uv[0] ).SquareModulus();
11280 double w2 = 1. / ( uv[3]-uv[1] ).SquareModulus();
11281 newPos = ( w1 * ( uv[2]+uv[0] ) + w2 * ( uv[3]+uv[1] )) / ( w1+w2 ) / 2;
11285 double sumWeight = 0;
11286 int nb = _simplices.size() == 4 ? 2 : _simplices.size();
11287 for ( int i = 0; i < nb; ++i )
11290 int iTo = i + _simplices.size() - 1;
11291 for ( int j = iFrom; j < iTo; ++j )
11293 int i2 = SMESH_MesherHelper::WrapIndex( j, _simplices.size() );
11294 double w = 1. / ( uv[i]-uv[i2] ).SquareModulus();
11296 newPos += w * ( uv[i]+uv[i2] );
11299 newPos /= 2 * sumWeight; // 2 is to get a middle between uv's
11304 // Laplacian smooth
11305 for ( size_t i = 0; i < _simplices.size(); ++i )
11307 newPos /= _simplices.size();
11310 // count quality metrics (orientation) of triangles around the node
11311 int nbOkBefore = 0;
11312 gp_XY tgtUV = helper.GetNodeUV( face, _node );
11313 for ( size_t i = 0; i < _simplices.size(); ++i )
11314 nbOkBefore += _simplices[i].IsForward( tgtUV, _node, face, helper, refSign );
11317 for ( size_t i = 0; i < _simplices.size(); ++i )
11318 nbOkAfter += _simplices[i].IsForward( newPos, _node, face, helper, refSign );
11320 if ( nbOkAfter < nbOkBefore )
11322 nbBad += _simplices.size() - nbOkBefore;
11326 SMDS_FacePosition* pos = static_cast<SMDS_FacePosition*>( _node->GetPosition() );
11327 pos->SetUParameter( newPos.X() );
11328 pos->SetVParameter( newPos.Y() );
11335 gp_Pnt p = surface->Value( newPos.X(), newPos.Y() );
11336 const_cast< SMDS_MeshNode* >( _node )->setXYZ( p.X(), p.Y(), p.Z() );
11340 nbBad += _simplices.size() - nbOkAfter;
11341 return ( (tgtUV-newPos).SquareModulus() > 1e-10 );
11344 //================================================================================
11346 * \brief Computes new UV using angle based smoothing technic
11348 //================================================================================
11350 gp_XY _SmoothNode::computeAngularPos(vector<gp_XY>& uv,
11351 const gp_XY& uvToFix,
11352 const double refSign)
11354 uv.push_back( uv.front() );
11356 vector< gp_XY > edgeDir ( uv.size() );
11357 vector< double > edgeSize( uv.size() );
11358 for ( size_t i = 1; i < edgeDir.size(); ++i )
11360 edgeDir [i-1] = uv[i] - uv[i-1];
11361 edgeSize[i-1] = edgeDir[i-1].Modulus();
11362 if ( edgeSize[i-1] < numeric_limits<double>::min() )
11363 edgeDir[i-1].SetX( 100 );
11365 edgeDir[i-1] /= edgeSize[i-1] * refSign;
11367 edgeDir.back() = edgeDir.front();
11368 edgeSize.back() = edgeSize.front();
11372 double sumSize = 0;
11373 for ( size_t i = 1; i < edgeDir.size(); ++i )
11375 if ( edgeDir[i-1].X() > 1. ) continue;
11377 while ( edgeDir[i].X() > 1. && ++i < edgeDir.size() );
11378 if ( i == edgeDir.size() ) break;
11380 gp_XY norm1( -edgeDir[i1].Y(), edgeDir[i1].X() );
11381 gp_XY norm2( -edgeDir[i].Y(), edgeDir[i].X() );
11382 gp_XY bisec = norm1 + norm2;
11383 double bisecSize = bisec.Modulus();
11384 if ( bisecSize < numeric_limits<double>::min() )
11386 bisec = -edgeDir[i1] + edgeDir[i];
11387 bisecSize = bisec.Modulus();
11389 bisec /= bisecSize;
11391 gp_XY dirToN = uvToFix - p;
11392 double distToN = dirToN.Modulus();
11393 if ( bisec * dirToN < 0 )
11394 distToN = -distToN;
11396 newPos += ( p + bisec * distToN ) * ( edgeSize[i1] + edgeSize[i] );
11398 sumSize += edgeSize[i1] + edgeSize[i];
11400 newPos /= /*nbEdges * */sumSize;
11404 //================================================================================
11406 * \brief Delete _SolidData
11408 //================================================================================
11410 _SolidData::~_SolidData()
11412 TNode2Edge::iterator n2e = _n2eMap.begin();
11413 for ( ; n2e != _n2eMap.end(); ++n2e )
11415 _LayerEdge* & e = n2e->second;
11418 delete e->_curvature;
11419 if ( e->_2neibors )
11420 delete e->_2neibors->_plnNorm;
11421 delete e->_2neibors;
11432 //================================================================================
11434 * \brief Keep a _LayerEdge inflated along the EDGE
11436 //================================================================================
11438 void _Shrinker1D::AddEdge( const _LayerEdge* e,
11439 _EdgesOnShape& eos,
11440 SMESH_MesherHelper& helper )
11443 if ( _nodes.empty() )
11445 _edges[0] = _edges[1] = 0;
11448 // check _LayerEdge
11449 if ( e == _edges[0] || e == _edges[1] || e->_nodes.size() < 2 )
11451 if ( eos.SWOLType() != TopAbs_EDGE )
11452 throw SALOME_Exception(LOCALIZED("Wrong _LayerEdge is added"));
11453 if ( _edges[0] && !_geomEdge.IsSame( eos._sWOL ))
11454 throw SALOME_Exception(LOCALIZED("Wrong _LayerEdge is added"));
11456 // store _LayerEdge
11457 _geomEdge = TopoDS::Edge( eos._sWOL );
11459 BRep_Tool::Range( _geomEdge, f,l );
11460 double u = helper.GetNodeU( _geomEdge, e->_nodes[0], e->_nodes.back());
11461 _edges[ u < 0.5*(f+l) ? 0 : 1 ] = e;
11465 const SMDS_MeshNode* tgtNode0 = TgtNode( 0 );
11466 const SMDS_MeshNode* tgtNode1 = TgtNode( 1 );
11468 if ( _nodes.empty() )
11470 SMESHDS_SubMesh * eSubMesh = helper.GetMeshDS()->MeshElements( _geomEdge );
11471 if ( !eSubMesh || eSubMesh->NbNodes() < 1 )
11473 TopLoc_Location loc;
11474 Handle(Geom_Curve) C = BRep_Tool::Curve( _geomEdge, loc, f,l );
11475 GeomAdaptor_Curve aCurve(C, f,l);
11476 const double totLen = GCPnts_AbscissaPoint::Length(aCurve, f, l);
11478 int nbExpectNodes = eSubMesh->NbNodes();
11479 _initU .reserve( nbExpectNodes );
11480 _normPar.reserve( nbExpectNodes );
11481 _nodes .reserve( nbExpectNodes );
11482 SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
11483 while ( nIt->more() )
11485 const SMDS_MeshNode* node = nIt->next();
11487 // skip refinement nodes
11488 if ( node->NbInverseElements(SMDSAbs_Edge) == 0 ||
11489 node == tgtNode0 || node == tgtNode1 )
11491 bool hasMarkedFace = false;
11492 SMDS_ElemIteratorPtr fIt = node->GetInverseElementIterator(SMDSAbs_Face);
11493 while ( fIt->more() && !hasMarkedFace )
11494 hasMarkedFace = fIt->next()->isMarked();
11495 if ( !hasMarkedFace )
11498 _nodes.push_back( node );
11499 _initU.push_back( helper.GetNodeU( _geomEdge, node ));
11500 double len = GCPnts_AbscissaPoint::Length(aCurve, f, _initU.back());
11501 _normPar.push_back( len / totLen );
11506 // remove target node of the _LayerEdge from _nodes
11507 size_t nbFound = 0;
11508 for ( size_t i = 0; i < _nodes.size(); ++i )
11509 if ( !_nodes[i] || _nodes[i] == tgtNode0 || _nodes[i] == tgtNode1 )
11510 _nodes[i] = 0, nbFound++;
11511 if ( nbFound == _nodes.size() )
11516 //================================================================================
11518 * \brief Move nodes on EDGE from ends where _LayerEdge's are inflated
11520 //================================================================================
11522 void _Shrinker1D::Compute(bool set3D, SMESH_MesherHelper& helper)
11524 if ( _done || _nodes.empty())
11526 const _LayerEdge* e = _edges[0];
11527 if ( !e ) e = _edges[1];
11530 _done = (( !_edges[0] || _edges[0]->Is( _LayerEdge::SHRUNK )) &&
11531 ( !_edges[1] || _edges[1]->Is( _LayerEdge::SHRUNK )));
11534 if ( set3D || _done )
11536 Handle(Geom_Curve) C = BRep_Tool::Curve(_geomEdge, f,l);
11537 GeomAdaptor_Curve aCurve(C, f,l);
11540 f = helper.GetNodeU( _geomEdge, _edges[0]->_nodes.back(), _nodes[0] );
11542 l = helper.GetNodeU( _geomEdge, _edges[1]->_nodes.back(), _nodes.back() );
11543 double totLen = GCPnts_AbscissaPoint::Length( aCurve, f, l );
11545 for ( size_t i = 0; i < _nodes.size(); ++i )
11547 if ( !_nodes[i] ) continue;
11548 double len = totLen * _normPar[i];
11549 GCPnts_AbscissaPoint discret( aCurve, len, f );
11550 if ( !discret.IsDone() )
11551 return throw SALOME_Exception(LOCALIZED("GCPnts_AbscissaPoint failed"));
11552 double u = discret.Parameter();
11553 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
11554 pos->SetUParameter( u );
11555 gp_Pnt p = C->Value( u );
11556 const_cast< SMDS_MeshNode*>( _nodes[i] )->setXYZ( p.X(), p.Y(), p.Z() );
11561 BRep_Tool::Range( _geomEdge, f,l );
11563 f = helper.GetNodeU( _geomEdge, _edges[0]->_nodes.back(), _nodes[0] );
11565 l = helper.GetNodeU( _geomEdge, _edges[1]->_nodes.back(), _nodes.back() );
11567 for ( size_t i = 0; i < _nodes.size(); ++i )
11569 if ( !_nodes[i] ) continue;
11570 double u = f * ( 1-_normPar[i] ) + l * _normPar[i];
11571 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
11572 pos->SetUParameter( u );
11577 //================================================================================
11579 * \brief Restore initial parameters of nodes on EDGE
11581 //================================================================================
11583 void _Shrinker1D::RestoreParams()
11586 for ( size_t i = 0; i < _nodes.size(); ++i )
11588 if ( !_nodes[i] ) continue;
11589 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( _nodes[i]->GetPosition() );
11590 pos->SetUParameter( _initU[i] );
11595 //================================================================================
11597 * \brief Replace source nodes by target nodes in shrinked mesh edges
11599 //================================================================================
11601 void _Shrinker1D::SwapSrcTgtNodes( SMESHDS_Mesh* mesh )
11603 const SMDS_MeshNode* nodes[3];
11604 for ( int i = 0; i < 2; ++i )
11606 if ( !_edges[i] ) continue;
11608 SMESHDS_SubMesh * eSubMesh = mesh->MeshElements( _geomEdge );
11609 if ( !eSubMesh ) return;
11610 const SMDS_MeshNode* srcNode = _edges[i]->_nodes[0];
11611 const SMDS_MeshNode* tgtNode = _edges[i]->_nodes.back();
11612 const SMDS_MeshNode* scdNode = _edges[i]->_nodes[1];
11613 SMDS_ElemIteratorPtr eIt = srcNode->GetInverseElementIterator(SMDSAbs_Edge);
11614 while ( eIt->more() )
11616 const SMDS_MeshElement* e = eIt->next();
11617 if ( !eSubMesh->Contains( e ) || e->GetNodeIndex( scdNode ) >= 0 )
11619 SMDS_ElemIteratorPtr nIt = e->nodesIterator();
11620 for ( int iN = 0; iN < e->NbNodes(); ++iN )
11622 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nIt->next() );
11623 nodes[iN] = ( n == srcNode ? tgtNode : n );
11625 mesh->ChangeElementNodes( e, nodes, e->NbNodes() );
11630 //================================================================================
11632 * \brief Creates 2D and 1D elements on boundaries of new prisms
11634 //================================================================================
11636 bool _ViscousBuilder::addBoundaryElements(_SolidData& data)
11638 SMESH_MesherHelper helper( *_mesh );
11640 vector< const SMDS_MeshNode* > faceNodes;
11642 //for ( size_t i = 0; i < _sdVec.size(); ++i )
11644 //_SolidData& data = _sdVec[i];
11645 TopTools_IndexedMapOfShape geomEdges;
11646 TopExp::MapShapes( data._solid, TopAbs_EDGE, geomEdges );
11647 for ( int iE = 1; iE <= geomEdges.Extent(); ++iE )
11649 const TopoDS_Edge& E = TopoDS::Edge( geomEdges(iE));
11650 const TGeomID edgeID = getMeshDS()->ShapeToIndex( E );
11651 if ( data._noShrinkShapes.count( edgeID ))
11654 // Get _LayerEdge's based on E
11656 map< double, const SMDS_MeshNode* > u2nodes;
11657 if ( !SMESH_Algo::GetSortedNodesOnEdge( getMeshDS(), E, /*ignoreMedium=*/false, u2nodes))
11660 vector< _LayerEdge* > ledges; ledges.reserve( u2nodes.size() );
11661 TNode2Edge & n2eMap = data._n2eMap;
11662 map< double, const SMDS_MeshNode* >::iterator u2n = u2nodes.begin();
11664 //check if 2D elements are needed on E
11665 TNode2Edge::iterator n2e = n2eMap.find( u2n->second );
11666 if ( n2e == n2eMap.end() ) continue; // no layers on vertex
11667 ledges.push_back( n2e->second );
11669 if (( n2e = n2eMap.find( u2n->second )) == n2eMap.end() )
11670 continue; // no layers on E
11671 ledges.push_back( n2eMap[ u2n->second ]);
11673 const SMDS_MeshNode* tgtN0 = ledges[0]->_nodes.back();
11674 const SMDS_MeshNode* tgtN1 = ledges[1]->_nodes.back();
11675 int nbSharedPyram = 0;
11676 SMDS_ElemIteratorPtr vIt = tgtN1->GetInverseElementIterator(SMDSAbs_Volume);
11677 while ( vIt->more() )
11679 const SMDS_MeshElement* v = vIt->next();
11680 nbSharedPyram += int( v->GetNodeIndex( tgtN0 ) >= 0 );
11682 if ( nbSharedPyram > 1 )
11683 continue; // not free border of the pyramid
11686 faceNodes.push_back( ledges[0]->_nodes[0] );
11687 faceNodes.push_back( ledges[1]->_nodes[0] );
11688 if ( ledges[0]->_nodes.size() > 1 ) faceNodes.push_back( ledges[0]->_nodes[1] );
11689 if ( ledges[1]->_nodes.size() > 1 ) faceNodes.push_back( ledges[1]->_nodes[1] );
11691 if ( getMeshDS()->FindElement( faceNodes, SMDSAbs_Face, /*noMedium=*/true))
11692 continue; // faces already created
11694 for ( ++u2n; u2n != u2nodes.end(); ++u2n )
11695 ledges.push_back( n2eMap[ u2n->second ]);
11697 // Find out orientation and type of face to create
11699 bool reverse = false, isOnFace;
11702 map< TGeomID, TopoDS_Shape >::iterator e2f = data._shrinkShape2Shape.find( edgeID );
11703 if (( isOnFace = ( e2f != data._shrinkShape2Shape.end() )))
11705 F = e2f->second.Oriented( TopAbs_FORWARD );
11706 reverse = ( helper.GetSubShapeOri( F, E ) == TopAbs_REVERSED );
11707 if ( helper.GetSubShapeOri( data._solid, F ) == TopAbs_REVERSED )
11708 reverse = !reverse, F.Reverse();
11709 if ( helper.IsReversedSubMesh( TopoDS::Face(F) ))
11710 reverse = !reverse;
11712 else if ( !data._ignoreFaceIds.count( e2f->first ))
11714 // find FACE with layers sharing E
11715 PShapeIteratorPtr fIt = helper.GetAncestors( E, *_mesh, TopAbs_FACE, &data._solid );
11717 F = *( fIt->next() );
11719 // Find the sub-mesh to add new faces
11720 SMESHDS_SubMesh* sm = 0;
11722 sm = getMeshDS()->MeshElements( F );
11724 sm = data._proxyMesh->getFaceSubM( TopoDS::Face(F), /*create=*/true );
11726 return error("error in addBoundaryElements()", data._index);
11728 // Find a proxy sub-mesh of the FACE of an adjacent SOLID, which will use the new boundary
11729 // faces for 3D meshing (PAL23414)
11730 SMESHDS_SubMesh* adjSM = 0;
11733 const TGeomID faceID = sm->GetID();
11734 PShapeIteratorPtr soIt = helper.GetAncestors( F, *_mesh, TopAbs_SOLID );
11735 while ( const TopoDS_Shape* solid = soIt->next() )
11736 if ( !solid->IsSame( data._solid ))
11738 size_t iData = _solids.FindIndex( *solid ) - 1;
11739 if ( iData < _sdVec.size() &&
11740 _sdVec[ iData ]._ignoreFaceIds.count( faceID ) &&
11741 _sdVec[ iData ]._shrinkShape2Shape.count( edgeID ) == 0 )
11743 SMESH_ProxyMesh::SubMesh* proxySub =
11744 _sdVec[ iData ]._proxyMesh->getFaceSubM( TopoDS::Face( F ), /*create=*/false);
11745 if ( proxySub && proxySub->NbElements() > 0 )
11752 const int dj1 = reverse ? 0 : 1;
11753 const int dj2 = reverse ? 1 : 0;
11754 vector< const SMDS_MeshElement*> ff; // new faces row
11755 SMESHDS_Mesh* m = getMeshDS();
11756 for ( size_t j = 1; j < ledges.size(); ++j )
11758 vector< const SMDS_MeshNode*>& nn1 = ledges[j-dj1]->_nodes;
11759 vector< const SMDS_MeshNode*>& nn2 = ledges[j-dj2]->_nodes;
11760 ff.resize( std::max( nn1.size(), nn2.size() ), NULL );
11761 if ( nn1.size() == nn2.size() )
11764 for ( size_t z = 1; z < nn1.size(); ++z )
11765 sm->AddElement( ff[z-1] = m->AddFace( nn1[z-1], nn2[z-1], nn2[z], nn1[z] ));
11767 for ( size_t z = 1; z < nn1.size(); ++z )
11768 sm->AddElement( new SMDS_FaceOfNodes( nn1[z-1], nn2[z-1], nn2[z], nn1[z] ));
11770 else if ( nn1.size() == 1 )
11773 for ( size_t z = 1; z < nn2.size(); ++z )
11774 sm->AddElement( ff[z-1] = m->AddFace( nn1[0], nn2[z-1], nn2[z] ));
11776 for ( size_t z = 1; z < nn2.size(); ++z )
11777 sm->AddElement( new SMDS_FaceOfNodes( nn1[0], nn2[z-1], nn2[z] ));
11782 for ( size_t z = 1; z < nn1.size(); ++z )
11783 sm->AddElement( ff[z-1] = m->AddFace( nn1[z-1], nn2[0], nn1[z] ));
11785 for ( size_t z = 1; z < nn1.size(); ++z )
11786 sm->AddElement( new SMDS_FaceOfNodes( nn1[z-1], nn2[0], nn2[z] ));
11789 if ( adjSM ) // add faces to a proxy SM of the adjacent SOLID
11791 for ( size_t z = 0; z < ff.size(); ++z )
11793 adjSM->AddElement( ff[ z ]);
11799 for ( int isFirst = 0; isFirst < 2; ++isFirst )
11801 _LayerEdge* edge = isFirst ? ledges.front() : ledges.back();
11802 _EdgesOnShape* eos = data.GetShapeEdges( edge );
11803 if ( eos && eos->SWOLType() == TopAbs_EDGE )
11805 vector< const SMDS_MeshNode*>& nn = edge->_nodes;
11806 if ( nn.size() < 2 || nn[1]->NbInverseElements( SMDSAbs_Edge ) >= 2 )
11808 helper.SetSubShape( eos->_sWOL );
11809 helper.SetElementsOnShape( true );
11810 for ( size_t z = 1; z < nn.size(); ++z )
11811 helper.AddEdge( nn[z-1], nn[z] );
11815 } // loop on EDGE's
11816 } // loop on _SolidData's