1 // Copyright (C) 2007-2014 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_ViscousLayers2D.cxx
21 // Created : 23 Jul 2012
22 // Author : Edward AGAPOV (eap)
24 #include "StdMeshers_ViscousLayers2D.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 "SMESH_Algo.hxx"
34 #include "SMESH_ComputeError.hxx"
35 #include "SMESH_ControlsDef.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_ProxyMesh.hxx"
42 #include "SMESH_Quadtree.hxx"
43 #include "SMESH_subMesh.hxx"
44 #include "SMESH_subMeshEventListener.hxx"
45 #include "StdMeshers_FaceSide.hxx"
47 #include "utilities.h"
49 #include <BRepAdaptor_Curve.hxx>
50 #include <BRepAdaptor_Curve2d.hxx>
51 #include <BRep_Tool.hxx>
52 #include <Bnd_B2d.hxx>
53 #include <Bnd_B3d.hxx>
55 #include <GCPnts_AbscissaPoint.hxx>
56 #include <Geom2dAdaptor_Curve.hxx>
57 #include <Geom2dInt_GInter.hxx>
58 #include <Geom2d_Circle.hxx>
59 #include <Geom2d_Line.hxx>
60 #include <Geom2d_TrimmedCurve.hxx>
61 #include <GeomAdaptor_Curve.hxx>
62 #include <Geom_Circle.hxx>
63 #include <Geom_Curve.hxx>
64 #include <Geom_Line.hxx>
65 #include <Geom_TrimmedCurve.hxx>
66 #include <IntRes2d_IntersectionPoint.hxx>
67 #include <Precision.hxx>
68 #include <Standard_ErrorHandler.hxx>
69 #include <TColStd_Array1OfReal.hxx>
71 #include <TopExp_Explorer.hxx>
72 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
73 #include <TopTools_IndexedMapOfShape.hxx>
74 #include <TopTools_ListIteratorOfListOfShape.hxx>
75 #include <TopTools_ListOfShape.hxx>
76 #include <TopTools_MapOfShape.hxx>
78 #include <TopoDS_Edge.hxx>
79 #include <TopoDS_Face.hxx>
80 #include <TopoDS_Vertex.hxx>
96 //================================================================================
101 //--------------------------------------------------------------------------------
103 * \brief Proxy Mesh of FACE with viscous layers. It's needed only to
104 * redefine newSubmesh().
106 struct _ProxyMeshOfFace : public SMESH_ProxyMesh
108 //---------------------------------------------------
109 // Proxy sub-mesh of an EDGE. It contains nodes in _uvPtStructVec.
110 struct _EdgeSubMesh : public SMESH_ProxyMesh::SubMesh
112 _EdgeSubMesh(int index=0): SubMesh(index) {}
113 //virtual int NbElements() const { return _elements.size()+1; }
114 virtual int NbNodes() const { return Max( 0, _uvPtStructVec.size()-2 ); }
115 void SetUVPtStructVec(UVPtStructVec& vec) { _uvPtStructVec.swap( vec ); }
117 _ProxyMeshOfFace(const SMESH_Mesh& mesh): SMESH_ProxyMesh(mesh) {}
118 _EdgeSubMesh* GetEdgeSubMesh(int ID) { return (_EdgeSubMesh*) getProxySubMesh(ID); }
119 virtual SubMesh* newSubmesh(int index=0) const { return new _EdgeSubMesh(index); }
121 //--------------------------------------------------------------------------------
123 * \brief SMESH_subMeshEventListener used to store _ProxyMeshOfFace, computed
124 * by _ViscousBuilder2D, in a SMESH_subMesh of the FACE.
125 * This is to delete _ProxyMeshOfFace when StdMeshers_ViscousLayers2D
126 * hypothesis is modified
128 struct _ProxyMeshHolder : public SMESH_subMeshEventListener
130 _ProxyMeshHolder( const TopoDS_Face& face,
131 SMESH_ProxyMesh::Ptr& mesh)
132 : SMESH_subMeshEventListener( /*deletable=*/true, Name() )
134 SMESH_subMesh* faceSM = mesh->GetMesh()->GetSubMesh( face );
135 faceSM->SetEventListener( this, new _Data( mesh ), faceSM );
137 // Finds a proxy mesh of face
138 static SMESH_ProxyMesh::Ptr FindProxyMeshOfFace( const TopoDS_Shape& face,
141 SMESH_ProxyMesh::Ptr proxy;
142 SMESH_subMesh* faceSM = mesh.GetSubMesh( face );
143 if ( EventListenerData* ld = faceSM->GetEventListenerData( Name() ))
144 proxy = static_cast< _Data* >( ld )->_mesh;
148 void ProcessEvent(const int event,
150 SMESH_subMesh* subMesh,
151 EventListenerData* data,
152 const SMESH_Hypothesis* /*hyp*/)
154 if ( event == SMESH_subMesh::CLEAN && eventType == SMESH_subMesh::COMPUTE_EVENT)
155 ((_Data*) data)->_mesh.reset();
158 // holder of a proxy mesh
159 struct _Data : public SMESH_subMeshEventListenerData
161 SMESH_ProxyMesh::Ptr _mesh;
162 _Data( SMESH_ProxyMesh::Ptr& mesh )
163 :SMESH_subMeshEventListenerData( /*isDeletable=*/true), _mesh( mesh )
166 // Returns identifier string
167 static const char* Name() { return "VISCOUS_2D::_ProxyMeshHolder"; }
171 //--------------------------------------------------------------------------------
173 * \brief Segment connecting inner ends of two _LayerEdge's.
177 const gp_XY* _uv[2]; // poiter to _LayerEdge::_uvIn
178 int _indexInLine; // position in _PolyLine
181 _Segment(const gp_XY& p1, const gp_XY& p2):_indexInLine(-1) { _uv[0] = &p1; _uv[1] = &p2; }
182 const gp_XY& p1() const { return *_uv[0]; }
183 const gp_XY& p2() const { return *_uv[1]; }
185 //--------------------------------------------------------------------------------
187 * \brief Tree of _Segment's used for a faster search of _Segment's.
189 struct _SegmentTree : public SMESH_Quadtree
191 typedef boost::shared_ptr< _SegmentTree > Ptr;
193 _SegmentTree( const vector< _Segment >& segments );
194 void GetSegmentsNear( const _Segment& seg, vector< const _Segment* >& found );
195 void GetSegmentsNear( const gp_Ax2d& ray, vector< const _Segment* >& found );
198 _SegmentTree* newChild() const { return new _SegmentTree; }
199 void buildChildrenData();
200 Bnd_B2d* buildRootBox();
202 static int maxNbSegInLeaf() { return 5; }
205 const _Segment* _seg;
207 void Set( const _Segment& seg )
210 _iMin[0] = ( seg._uv[1]->X() < seg._uv[0]->X() );
211 _iMin[1] = ( seg._uv[1]->Y() < seg._uv[0]->Y() );
213 bool IsOut( const _Segment& seg ) const;
214 bool IsOut( const gp_Ax2d& ray ) const;
216 vector< _SegBox > _segments;
218 //--------------------------------------------------------------------------------
220 * \brief Edge normal to FACE boundary, connecting a point on EDGE (_uvOut)
221 * and a point of a layer internal boundary (_uvIn)
225 gp_XY _uvOut; // UV on the FACE boundary
226 gp_XY _uvIn; // UV inside the FACE
227 double _length2D; // distance between _uvOut and _uvIn
229 bool _isBlocked;// is more inflation possible or not
231 gp_XY _normal2D; // to curve
232 double _len2dTo3dRatio; // to pass 2D <--> 3D
233 gp_Ax2d _ray; // a ray starting at _uvOut
235 vector<gp_XY> _uvRefined; // divisions by layers
237 bool SetNewLength( const double length );
243 //--------------------------------------------------------------------------------
245 * \brief Poly line composed of _Segment's of one EDGE.
246 * It's used to detect intersection of inflated layers by intersecting
251 StdMeshers_FaceSide* _wire;
252 int _edgeInd; // index of my EDGE in _wire
253 bool _advancable; // true if there is a viscous layer on my EDGE
254 bool _isStraight2D;// pcurve type
255 _PolyLine* _leftLine; // lines of neighbour EDGE's
256 _PolyLine* _rightLine;
257 int _firstPntInd; // index in vector<UVPtStruct> of _wire
260 vector< _LayerEdge > _lEdges; /* _lEdges[0] is usually is not treated
261 as it is equal to the last one of the _leftLine */
262 vector< _Segment > _segments; // segments connecting _uvIn's of _lEdges
263 _SegmentTree::Ptr _segTree;
265 vector< _PolyLine* > _reachableLines; // lines able to interfere with my layer
267 vector< const SMDS_MeshNode* > _leftNodes; // nodes built from a left VERTEX
268 vector< const SMDS_MeshNode* > _rightNodes; // nodes built from a right VERTEX
270 typedef vector< _Segment >::iterator TSegIterator;
271 typedef vector< _LayerEdge >::iterator TEdgeIterator;
273 TIDSortedElemSet _newFaces; // faces generated from this line
275 bool IsCommonEdgeShared( const _PolyLine& other );
276 size_t FirstLEdge() const
278 return ( _leftLine->_advancable && _lEdges.size() > 2 ) ? 1 : 0;
280 bool IsAdjacent( const _Segment& seg, const _LayerEdge* LE=0 ) const
282 if ( LE /*&& seg._indexInLine < _lEdges.size()*/ )
283 return ( seg._uv[0] == & LE->_uvIn ||
284 seg._uv[1] == & LE->_uvIn );
285 return ( & seg == &_leftLine->_segments.back() ||
286 & seg == &_rightLine->_segments[0] );
288 bool IsConcave() const;
290 //--------------------------------------------------------------------------------
292 * \brief Intersector of _Segment's
294 struct _SegmentIntersection
296 gp_XY _vec1, _vec2; // Vec( _seg.p1(), _seg.p2() )
297 gp_XY _vec21; // Vec( _seg2.p1(), _seg1.p1() )
298 double _D; // _vec1.Crossed( _vec2 )
299 double _param1, _param2; // intersection param on _seg1 and _seg2
301 bool Compute(const _Segment& seg1, const _Segment& seg2, bool seg2IsRay = false )
303 // !!! If seg2IsRay, returns true at any _param2 !!!
304 const double eps = 1e-10;
305 _vec1 = seg1.p2() - seg1.p1();
306 _vec2 = seg2.p2() - seg2.p1();
307 _vec21 = seg1.p1() - seg2.p1();
308 _D = _vec1.Crossed(_vec2);
309 if ( fabs(_D) < std::numeric_limits<double>::min())
311 _param1 = _vec2.Crossed(_vec21) / _D;
312 if (_param1 < -eps || _param1 > 1 + eps )
314 _param2 = _vec1.Crossed(_vec21) / _D;
315 return seg2IsRay || ( _param2 > -eps && _param2 < 1 + eps );
317 bool Compute( const _Segment& seg1, const gp_Ax2d& ray )
319 gp_XY segEnd = ray.Location().XY() + ray.Direction().XY();
320 _Segment seg2( ray.Location().XY(), segEnd );
321 return Compute( seg1, seg2, true );
323 //gp_XY GetPoint() { return _seg1.p1() + _param1 * _vec1; }
325 //--------------------------------------------------------------------------------
327 typedef map< const SMDS_MeshNode*, _LayerEdge*, TIDCompare > TNode2Edge;
329 //--------------------------------------------------------------------------------
331 * \brief Builder of viscous layers
333 class _ViscousBuilder2D
336 _ViscousBuilder2D(SMESH_Mesh& theMesh,
337 const TopoDS_Face& theFace,
338 const StdMeshers_ViscousLayers2D* theHyp);
339 SMESH_ComputeErrorPtr GetError() const { return _error; }
341 SMESH_ProxyMesh::Ptr Compute(const TopoDS_Shape& theShapeHypAssignedTo);
345 bool findEdgesWithLayers(const TopoDS_Shape& theShapeHypAssignedTo);
346 bool makePolyLines();
348 bool fixCollisions();
352 bool toShrinkForAdjacent( const TopoDS_Face& adjFace,
353 const TopoDS_Edge& E,
354 const TopoDS_Vertex& V);
355 void setLenRatio( _LayerEdge& LE, const gp_Pnt& pOut );
356 void setLayerEdgeData( _LayerEdge& lEdge,
358 Handle(Geom2d_Curve)& pcurve,
359 Handle(Geom_Curve)& curve,
362 GeomAPI_ProjectPointOnSurf* faceProj);
363 void adjustCommonEdge( _PolyLine& LL, _PolyLine& LR );
364 void calcLayersHeight(const double totalThick,
365 vector<double>& heights);
366 bool removeMeshFaces(const TopoDS_Shape& face);
368 bool error( const string& text );
369 SMESHDS_Mesh* getMeshDS() { return _mesh->GetMeshDS(); }
370 _ProxyMeshOfFace* getProxyMesh();
373 //void makeGroupOfLE();
380 const StdMeshers_ViscousLayers2D* _hyp;
383 SMESH_ProxyMesh::Ptr _proxyMesh;
384 SMESH_ComputeErrorPtr _error;
387 Handle(Geom_Surface) _surface;
388 SMESH_MesherHelper _helper;
389 TSideVector _faceSideVec; // wires (StdMeshers_FaceSide) of _face
390 vector<_PolyLine> _polyLineVec; // fronts to advance
391 bool _is2DIsotropic; // is same U and V resoulution of _face
392 vector<TopoDS_Face> _clearedFaces; // FACEs whose mesh was removed by shrink()
394 double _fPowN; // to compute thickness of layers
395 double _thickness; // required or possible layers thickness
397 // sub-shapes of _face
398 set<TGeomID> _ignoreShapeIds; // ids of EDGEs w/o layers
399 set<TGeomID> _noShrinkVert; // ids of VERTEXes that are extremities
400 // of EDGEs along which _LayerEdge can't be inflated because no viscous layers
401 // defined on neighbour FACEs sharing an EDGE. Nonetheless _LayerEdge's
402 // are inflated along such EDGEs but then such _LayerEdge's are turned into
403 // a node on VERTEX, i.e. all nodes on a _LayerEdge are melded into one node.
405 int _nbLE; // for DEBUG
408 //================================================================================
410 * \brief Returns StdMeshers_ViscousLayers2D for the FACE
412 const StdMeshers_ViscousLayers2D* findHyp(SMESH_Mesh& theMesh,
413 const TopoDS_Face& theFace,
414 TopoDS_Shape* assignedTo=0)
416 SMESH_HypoFilter hypFilter
417 ( SMESH_HypoFilter::HasName( StdMeshers_ViscousLayers2D::GetHypType() ));
418 const SMESH_Hypothesis * hyp =
419 theMesh.GetHypothesis( theFace, hypFilter, /*ancestors=*/true, assignedTo );
420 return dynamic_cast< const StdMeshers_ViscousLayers2D* > ( hyp );
423 //================================================================================
425 * \brief Returns ids of EDGEs not to create Viscous Layers on
426 * \param [in] theHyp - the hypothesis, holding edges either to ignore or not to.
427 * \param [in] theFace - the FACE whose EDGEs are checked.
428 * \param [in] theMesh - the mesh.
429 * \param [in,out] theEdgeIds - container returning EDGEs to ignore.
430 * \return int - number of found EDGEs of the FACE.
432 //================================================================================
434 int getEdgesToIgnore( const StdMeshers_ViscousLayers2D* theHyp,
435 const TopoDS_Shape& theFace,
436 const SMESHDS_Mesh* theMesh,
437 set< int > & theEdgeIds)
439 int nbToEdgesIgnore = 0;
440 vector<TGeomID> ids = theHyp->GetBndShapes();
441 if ( theHyp->IsToIgnoreShapes() ) // EDGEs to ignore are given
443 for ( size_t i = 0; i < ids.size(); ++i )
445 const TopoDS_Shape& E = theMesh->IndexToShape( ids[i] );
447 E.ShapeType() == TopAbs_EDGE &&
448 SMESH_MesherHelper::IsSubShape( E, theFace ))
450 theEdgeIds.insert( ids[i] );
455 else // EDGEs to make the Viscous Layers on are given
457 TopExp_Explorer E( theFace, TopAbs_EDGE );
458 for ( ; E.More(); E.Next(), ++nbToEdgesIgnore )
459 theEdgeIds.insert( theMesh->ShapeToIndex( E.Current() ));
461 for ( size_t i = 0; i < ids.size(); ++i )
462 nbToEdgesIgnore -= theEdgeIds.erase( ids[i] );
464 return nbToEdgesIgnore;
467 } // namespace VISCOUS_2D
469 //================================================================================
470 // StdMeshers_ViscousLayers hypothesis
472 StdMeshers_ViscousLayers2D::StdMeshers_ViscousLayers2D(int hypId, int studyId, SMESH_Gen* gen)
473 :StdMeshers_ViscousLayers(hypId, studyId, gen)
475 _name = StdMeshers_ViscousLayers2D::GetHypType();
476 _param_algo_dim = -2; // auxiliary hyp used by 2D algos
478 // --------------------------------------------------------------------------------
479 bool StdMeshers_ViscousLayers2D::SetParametersByMesh(const SMESH_Mesh* theMesh,
480 const TopoDS_Shape& theShape)
485 // --------------------------------------------------------------------------------
487 StdMeshers_ViscousLayers2D::Compute(SMESH_Mesh& theMesh,
488 const TopoDS_Face& theFace)
490 SMESH_ProxyMesh::Ptr pm;
492 TopoDS_Shape hypAssignedTo;
493 const StdMeshers_ViscousLayers2D* vlHyp = VISCOUS_2D::findHyp( theMesh, theFace, &hypAssignedTo );
496 VISCOUS_2D::_ViscousBuilder2D builder( theMesh, theFace, vlHyp );
497 pm = builder.Compute( hypAssignedTo );
498 SMESH_ComputeErrorPtr error = builder.GetError();
499 if ( error && !error->IsOK() )
500 theMesh.GetSubMesh( theFace )->GetComputeError() = error;
502 pm.reset( new SMESH_ProxyMesh( theMesh ));
503 if ( getenv("__ONLY__VL2D__"))
508 pm.reset( new SMESH_ProxyMesh( theMesh ));
512 // --------------------------------------------------------------------------------
513 void StdMeshers_ViscousLayers2D::RestoreListeners() const
515 StudyContextStruct* sc = _gen->GetStudyContext( _studyId );
516 std::map < int, SMESH_Mesh * >::iterator i_smesh = sc->mapMesh.begin();
517 for ( ; i_smesh != sc->mapMesh.end(); ++i_smesh )
519 SMESH_Mesh* smesh = i_smesh->second;
521 !smesh->HasShapeToMesh() ||
522 !smesh->GetMeshDS() ||
523 !smesh->GetMeshDS()->IsUsedHypothesis( this ))
526 // set event listeners to EDGE's of FACE where this hyp is used
527 TopoDS_Shape shape = i_smesh->second->GetShapeToMesh();
528 for ( TopExp_Explorer face( shape, TopAbs_FACE); face.More(); face.Next() )
529 if ( SMESH_Algo* algo = _gen->GetAlgo( *smesh, face.Current() ))
531 const std::list <const SMESHDS_Hypothesis *> & usedHyps =
532 algo->GetUsedHypothesis( *smesh, face.Current(), /*ignoreAuxiliary=*/false );
533 if ( std::find( usedHyps.begin(), usedHyps.end(), this ) != usedHyps.end() )
534 for ( TopExp_Explorer edge( face.Current(), TopAbs_EDGE); edge.More(); edge.Next() )
535 VISCOUS_3D::ToClearSubWithMain( smesh->GetSubMesh( edge.Current() ), face.Current() );
539 // END StdMeshers_ViscousLayers2D hypothesis
540 //================================================================================
542 using namespace VISCOUS_2D;
544 //================================================================================
546 * \brief Constructor of _ViscousBuilder2D
548 //================================================================================
550 _ViscousBuilder2D::_ViscousBuilder2D(SMESH_Mesh& theMesh,
551 const TopoDS_Face& theFace,
552 const StdMeshers_ViscousLayers2D* theHyp):
553 _mesh( &theMesh ), _face( theFace ), _hyp( theHyp ), _helper( theMesh )
555 _helper.SetSubShape( _face );
556 _helper.SetElementsOnShape( true );
558 _face.Orientation( TopAbs_FORWARD ); // 2D logic works only in this case
559 _surface = BRep_Tool::Surface( _face );
562 _fPowN = pow( _hyp->GetStretchFactor(), _hyp->GetNumberLayers() );
567 //================================================================================
569 * \brief Stores error description and returns false
571 //================================================================================
573 bool _ViscousBuilder2D::error(const string& text )
575 _error->myName = COMPERR_ALGO_FAILED;
576 _error->myComment = string("Viscous layers builder 2D: ") + text;
577 if ( SMESH_subMesh* sm = _mesh->GetSubMesh( _face ) )
579 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
580 if ( smError && smError->myAlgo )
581 _error->myAlgo = smError->myAlgo;
585 cout << "_ViscousBuilder2D::error " << text << endl;
590 //================================================================================
592 * \brief Does its job
594 //================================================================================
596 SMESH_ProxyMesh::Ptr _ViscousBuilder2D::Compute(const TopoDS_Shape& theShapeHypAssignedTo)
598 _error = SMESH_ComputeError::New(COMPERR_OK);
599 _faceSideVec = StdMeshers_FaceSide::GetFaceWires( _face, *_mesh, true, _error );
600 if ( !_error->IsOK() )
603 if ( !findEdgesWithLayers(theShapeHypAssignedTo) ) // analysis of a shape
606 if ( ! makePolyLines() ) // creation of fronts
609 if ( ! inflate() ) // advance fronts
612 // remove elements and nodes from _face
613 removeMeshFaces( _face );
615 if ( !shrink() ) // shrink segments on edges w/o layers
618 if ( ! refine() ) // make faces
626 //================================================================================
628 * \brief Finds EDGE's to make viscous layers on.
630 //================================================================================
632 bool _ViscousBuilder2D::findEdgesWithLayers(const TopoDS_Shape& theShapeHypAssignedTo)
634 // collect all EDGEs to ignore defined by hyp
635 int nbMyEdgesIgnored = getEdgesToIgnore( _hyp, _face, getMeshDS(), _ignoreShapeIds );
637 // get all shared EDGEs
638 TopTools_MapOfShape sharedEdges;
639 TopTools_IndexedDataMapOfShapeListOfShape facesOfEdgeMap;
640 TopExp::MapShapesAndAncestors( theShapeHypAssignedTo,
641 TopAbs_EDGE, TopAbs_FACE, facesOfEdgeMap);
642 for ( int iE = 1; iE <= facesOfEdgeMap.Extent(); ++iE )
643 if ( facesOfEdgeMap( iE ).Extent() > 1 )
644 sharedEdges.Add( facesOfEdgeMap.FindKey( iE ));
646 // check all EDGEs of the _face
647 int totalNbEdges = 0;
648 for ( size_t iWire = 0; iWire < _faceSideVec.size(); ++iWire )
650 StdMeshers_FaceSidePtr wire = _faceSideVec[ iWire ];
651 totalNbEdges += wire->NbEdges();
652 for ( int iE = 0; iE < wire->NbEdges(); ++iE )
654 if ( sharedEdges.Contains( wire->Edge( iE )))
656 // ignore internal EDGEs (shared by several FACEs)
657 const TGeomID edgeID = wire->EdgeID( iE );
658 _ignoreShapeIds.insert( edgeID );
660 // check if ends of an EDGE are to be added to _noShrinkVert
661 const TopTools_ListOfShape& faceList = facesOfEdgeMap.FindFromKey( wire->Edge( iE ));
662 TopTools_ListIteratorOfListOfShape faceIt( faceList );
663 for ( ; faceIt.More(); faceIt.Next() )
665 const TopoDS_Shape& neighbourFace = faceIt.Value();
666 if ( neighbourFace.IsSame( _face )) continue;
667 SMESH_Algo* algo = _mesh->GetGen()->GetAlgo( *_mesh, neighbourFace );
668 if ( !algo ) continue;
670 const StdMeshers_ViscousLayers2D* viscHyp = 0;
671 const list <const SMESHDS_Hypothesis *> & allHyps =
672 algo->GetUsedHypothesis(*_mesh, neighbourFace, /*noAuxiliary=*/false);
673 list< const SMESHDS_Hypothesis *>::const_iterator hyp = allHyps.begin();
674 for ( ; hyp != allHyps.end() && !viscHyp; ++hyp )
675 viscHyp = dynamic_cast<const StdMeshers_ViscousLayers2D*>( *hyp );
677 set<TGeomID> neighbourIgnoreEdges;
679 getEdgesToIgnore( viscHyp, neighbourFace, getMeshDS(), neighbourIgnoreEdges );
681 for ( int iV = 0; iV < 2; ++iV )
683 TopoDS_Vertex vertex = iV ? wire->LastVertex(iE) : wire->FirstVertex(iE);
685 _noShrinkVert.insert( getMeshDS()->ShapeToIndex( vertex ));
688 PShapeIteratorPtr edgeIt = _helper.GetAncestors( vertex, *_mesh, TopAbs_EDGE );
689 while ( const TopoDS_Shape* edge = edgeIt->next() )
690 if ( !edge->IsSame( wire->Edge( iE )) &&
691 _helper.IsSubShape( *edge, neighbourFace ) &&
692 ( neighbourIgnoreEdges.count( getMeshDS()->ShapeToIndex( *edge )) ||
693 sharedEdges.Contains( *edge )))
695 _noShrinkVert.insert( getMeshDS()->ShapeToIndex( vertex ));
705 // add VERTEXes w/o layers to _ignoreShapeIds (this is used by toShrinkForAdjacent())
706 for ( size_t iWire = 0; iWire < _faceSideVec.size(); ++iWire )
708 StdMeshers_FaceSidePtr wire = _faceSideVec[ iWire ];
709 for ( int iE = 0; iE < wire->NbEdges(); ++iE )
711 TGeomID edge1 = wire->EdgeID( iE );
712 TGeomID edge2 = wire->EdgeID( iE+1 );
713 if ( _ignoreShapeIds.count( edge1 ) && _ignoreShapeIds.count( edge2 ))
714 _ignoreShapeIds.insert( getMeshDS()->ShapeToIndex( wire->LastVertex( iE )));
718 return ( nbMyEdgesIgnored < totalNbEdges );
721 //================================================================================
723 * \brief Create the inner front of the viscous layers and prepare data for inflation
725 //================================================================================
727 bool _ViscousBuilder2D::makePolyLines()
729 // Create _PolyLines and _LayerEdge's
731 // count total nb of EDGEs to allocate _polyLineVec
733 for ( size_t iWire = 0; iWire < _faceSideVec.size(); ++iWire )
735 StdMeshers_FaceSidePtr wire = _faceSideVec[ iWire ];
736 nbEdges += wire->NbEdges();
737 if ( wire->GetUVPtStruct().empty() && wire->NbPoints() > 0 )
738 return error("Invalid node parameters on some EDGE");
740 _polyLineVec.resize( nbEdges );
742 // check if 2D normal should be computed by 3D one by means of projection
743 GeomAPI_ProjectPointOnSurf* faceProj = 0;
747 const UVPtStruct& uv = _faceSideVec[0]->GetUVPtStruct()[0];
748 gp_Pnt p = SMESH_TNodeXYZ( uv.node );
749 tmpLE._uvOut.SetCoord( uv.u, uv.v );
750 tmpLE._normal2D.SetCoord( 1., 0. );
751 setLenRatio( tmpLE, p );
752 const double r1 = tmpLE._len2dTo3dRatio;
753 tmpLE._normal2D.SetCoord( 0., 1. );
754 setLenRatio( tmpLE, p );
755 const double r2 = tmpLE._len2dTo3dRatio;
756 // projection is needed if two _len2dTo3dRatio's differ too much
757 const double maxR = Max( r2, r1 );
758 if ( Abs( r2-r1 )/maxR > 0.2*maxR )
759 faceProj = & _helper.GetProjector( _face, loc );
761 _is2DIsotropic = !faceProj;
763 // Assign data to _PolyLine's
764 // ---------------------------
767 for ( size_t iWire = 0; iWire < _faceSideVec.size(); ++iWire )
769 StdMeshers_FaceSidePtr wire = _faceSideVec[ iWire ];
770 const vector<UVPtStruct>& points = wire->GetUVPtStruct();
772 for ( int iE = 0; iE < wire->NbEdges(); ++iE )
774 _PolyLine& L = _polyLineVec[ iPoLine++ ];
775 L._wire = wire.get();
777 L._advancable = !_ignoreShapeIds.count( wire->EdgeID( iE ));
779 int iRight = iPoLine - (( iE+1 < wire->NbEdges() ) ? 0 : wire->NbEdges() );
780 L._rightLine = &_polyLineVec[ iRight ];
781 _polyLineVec[ iRight ]._leftLine = &L;
783 L._firstPntInd = iPnt;
784 double lastNormPar = wire->LastParameter( iE ) - 1e-10;
785 while ( points[ iPnt ].normParam < lastNormPar )
787 L._lastPntInd = iPnt;
788 L._lEdges.resize( Max( 3, L._lastPntInd - L._firstPntInd + 1 )); // 3 edges minimum
790 // TODO: add more _LayerEdge's to strongly curved EDGEs
791 // in order not to miss collisions
794 Handle(Geom_Curve) curve = BRep_Tool::Curve( L._wire->Edge( iE ), loc, u, u );
795 Handle(Geom2d_Curve) pcurve = L._wire->Curve2d( L._edgeInd );
796 const bool reverse = (( L._wire->Edge( iE ).Orientation() == TopAbs_REVERSED ) ^
797 (_face.Orientation() == TopAbs_REVERSED ));
798 for ( int i = L._firstPntInd; i <= L._lastPntInd; ++i )
800 _LayerEdge& lEdge = L._lEdges[ i - L._firstPntInd ];
801 u = ( i == L._firstPntInd ? wire->FirstU(iE) : points[ i ].param );
802 p = SMESH_TNodeXYZ( points[ i ].node );
803 setLayerEdgeData( lEdge, u, pcurve, curve, p, reverse, faceProj );
804 setLenRatio( lEdge, p );
806 if ( L._lastPntInd - L._firstPntInd + 1 < 3 ) // add 3-d _LayerEdge in the middle
808 L._lEdges[2] = L._lEdges[1];
809 u = 0.5 * ( wire->FirstU(iE) + wire->LastU(iE) );
810 if ( !curve.IsNull() )
811 p = curve->Value( u );
813 p = 0.5 * ( SMESH_TNodeXYZ( points[ L._firstPntInd ].node ) +
814 SMESH_TNodeXYZ( points[ L._lastPntInd ].node ));
815 setLayerEdgeData( L._lEdges[1], u, pcurve, curve, p, reverse, faceProj );
816 setLenRatio( L._lEdges[1], p );
821 // Fill _PolyLine's with _segments
822 // --------------------------------
824 double maxLen2dTo3dRatio = 0;
825 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
827 _PolyLine& L = _polyLineVec[ iPoLine ];
828 L._segments.resize( L._lEdges.size() - 1 );
829 for ( size_t i = 1; i < L._lEdges.size(); ++i )
831 _Segment & S = L._segments[i-1];
832 S._uv[0] = & L._lEdges[i-1]._uvIn;
833 S._uv[1] = & L._lEdges[i ]._uvIn;
834 S._indexInLine = i-1;
835 if ( maxLen2dTo3dRatio < L._lEdges[i]._len2dTo3dRatio )
836 maxLen2dTo3dRatio = L._lEdges[i]._len2dTo3dRatio;
838 // // connect _PolyLine's with segments, the 1st _LayerEdge of every _PolyLine
839 // // becomes not connected to any segment
840 // if ( L._leftLine->_advancable )
841 // L._segments[0]._uv[0] = & L._leftLine->_lEdges.back()._uvIn;
843 L._segTree.reset( new _SegmentTree( L._segments ));
846 // Evaluate max possible _thickness if required layers thickness seems too high
847 // ----------------------------------------------------------------------------
849 _thickness = _hyp->GetTotalThickness();
850 _SegmentTree::box_type faceBndBox2D;
851 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
852 faceBndBox2D.Add( *_polyLineVec[ iPoLine]._segTree->getBox() );
853 double boxTol = 1e-3 * sqrt( faceBndBox2D.SquareExtent() );
855 if ( _thickness * maxLen2dTo3dRatio > sqrt( faceBndBox2D.SquareExtent() ) / 10 )
857 vector< const _Segment* > foundSegs;
858 double maxPossibleThick = 0;
859 _SegmentIntersection intersection;
860 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
862 _PolyLine& L1 = _polyLineVec[ iL1 ];
863 _SegmentTree::box_type boxL1 = * L1._segTree->getBox();
864 boxL1.Enlarge( boxTol );
865 // consider case of a circle as well!
866 for ( size_t iL2 = iL1; iL2 < _polyLineVec.size(); ++iL2 )
868 _PolyLine& L2 = _polyLineVec[ iL2 ];
869 _SegmentTree::box_type boxL2 = * L2._segTree->getBox();
870 boxL2.Enlarge( boxTol );
871 if ( boxL1.IsOut( boxL2 ))
873 for ( size_t iLE = 1; iLE < L1._lEdges.size(); ++iLE )
876 L2._segTree->GetSegmentsNear( L1._lEdges[iLE]._ray, foundSegs );
877 for ( size_t i = 0; i < foundSegs.size(); ++i )
878 if ( intersection.Compute( *foundSegs[i], L1._lEdges[iLE]._ray ))
880 double distToL2 = intersection._param2 / L1._lEdges[iLE]._len2dTo3dRatio;
881 double psblThick = distToL2 / ( 1 + L1._advancable + L2._advancable );
882 maxPossibleThick = Max( psblThick, maxPossibleThick );
887 if ( maxPossibleThick > 0. )
888 _thickness = Min( _hyp->GetTotalThickness(), maxPossibleThick );
891 // Adjust _LayerEdge's at _PolyLine's extremities
892 // -----------------------------------------------
894 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
896 _PolyLine& LL = _polyLineVec[ iPoLine ];
897 _PolyLine& LR = *LL._rightLine;
898 adjustCommonEdge( LL, LR );
900 // recreate _segments if some _LayerEdge's have been removed by adjustCommonEdge()
901 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
903 _PolyLine& L = _polyLineVec[ iPoLine ];
904 // if ( L._segments.size() == L._lEdges.size() - 1 )
906 L._segments.resize( L._lEdges.size() - 1 );
907 for ( size_t i = 1; i < L._lEdges.size(); ++i )
909 _Segment & S = L._segments[i-1];
910 S._uv[0] = & L._lEdges[i-1]._uvIn;
911 S._uv[1] = & L._lEdges[i ]._uvIn;
912 S._indexInLine = i-1;
914 L._segTree.reset( new _SegmentTree( L._segments ));
916 // connect _PolyLine's with segments, the 1st _LayerEdge of every _PolyLine
917 // becomes not connected to any segment
918 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
920 _PolyLine& L = _polyLineVec[ iPoLine ];
921 if ( L._leftLine->_advancable )
922 L._segments[0]._uv[0] = & L._leftLine->_lEdges.back()._uvIn;
925 // Fill _reachableLines.
926 // ----------------------
928 // compute bnd boxes taking into account the layers total thickness
929 vector< _SegmentTree::box_type > lineBoxes( _polyLineVec.size() );
930 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
932 lineBoxes[ iPoLine ] = *_polyLineVec[ iPoLine ]._segTree->getBox();
933 lineBoxes[ iPoLine ].Enlarge( maxLen2dTo3dRatio * _thickness *
934 ( _polyLineVec[ iPoLine ]._advancable ? 2. : 1.2 ));
937 for ( iPoLine = 0; iPoLine < _polyLineVec.size(); ++iPoLine )
939 _PolyLine& L1 = _polyLineVec[ iPoLine ];
940 for ( size_t iL2 = 0; iL2 < _polyLineVec.size(); ++iL2 )
942 _PolyLine& L2 = _polyLineVec[ iL2 ];
943 if ( iPoLine == iL2 || lineBoxes[ iPoLine ].IsOut( lineBoxes[ iL2 ]))
945 if ( !L1._advancable && ( L1._leftLine == &L2 || L1._rightLine == &L2 ))
947 // check reachability by _LayerEdge's
948 int iDelta = 1; //Max( 1, L1._lEdges.size() / 100 );
949 for ( size_t iLE = 1; iLE < L1._lEdges.size(); iLE += iDelta )
951 _LayerEdge& LE = L1._lEdges[iLE];
952 if ( !lineBoxes[ iL2 ].IsOut ( LE._uvOut,
953 LE._uvOut + LE._normal2D *_thickness * LE._len2dTo3dRatio ))
955 L1._reachableLines.push_back( & L2 );
960 // add self to _reachableLines
961 Geom2dAdaptor_Curve pcurve( L1._wire->Curve2d( L1._edgeInd ));
962 L1._isStraight2D = ( pcurve.GetType() == GeomAbs_Line );
963 if ( !L1._isStraight2D )
965 // TODO: check carefully
966 L1._reachableLines.push_back( & L1 );
973 //================================================================================
975 * \brief adjust common _LayerEdge of two adjacent _PolyLine's
976 * \param LL - left _PolyLine
977 * \param LR - right _PolyLine
979 //================================================================================
981 void _ViscousBuilder2D::adjustCommonEdge( _PolyLine& LL, _PolyLine& LR )
983 int nbAdvancableL = LL._advancable + LR._advancable;
984 if ( nbAdvancableL == 0 )
987 _LayerEdge& EL = LL._lEdges.back();
988 _LayerEdge& ER = LR._lEdges.front();
989 gp_XY normL = EL._normal2D;
990 gp_XY normR = ER._normal2D;
991 gp_XY tangL ( normL.Y(), -normL.X() );
993 // set common direction to a VERTEX _LayerEdge shared by two _PolyLine's
994 gp_XY normCommon = ( normL * int( LL._advancable ) +
995 normR * int( LR._advancable )).Normalized();
996 EL._normal2D = normCommon;
997 EL._ray.SetLocation ( EL._uvOut );
998 EL._ray.SetDirection( EL._normal2D );
999 if ( nbAdvancableL == 1 ) { // _normal2D is true normal (not average)
1000 EL._isBlocked = true; // prevent intersecting with _Segments of _advancable line
1003 // update _LayerEdge::_len2dTo3dRatio according to a new direction
1004 const vector<UVPtStruct>& points = LL._wire->GetUVPtStruct();
1005 setLenRatio( EL, SMESH_TNodeXYZ( points[ LL._lastPntInd ].node ));
1009 const double dotNormTang = normR * tangL;
1010 const bool largeAngle = Abs( dotNormTang ) > 0.2;
1011 if ( largeAngle ) // not 180 degrees
1013 // recompute _len2dTo3dRatio to take into account angle between EDGEs
1014 gp_Vec2d oldNorm( LL._advancable ? normL : normR );
1015 double angleFactor = 1. / Max( 0.3, Cos( oldNorm.Angle( normCommon )));
1016 EL._len2dTo3dRatio *= angleFactor;
1017 ER._len2dTo3dRatio = EL._len2dTo3dRatio;
1019 gp_XY normAvg = ( normL + normR ).Normalized(); // average normal at VERTEX
1021 if ( dotNormTang < 0. ) // ---------------------------- CONVEX ANGLE
1023 // Remove _LayerEdge's intersecting the normAvg to avoid collisions
1024 // during inflate().
1026 // find max length of the VERTEX-based _LayerEdge whose direction is normAvg
1027 double maxLen2D = _thickness * EL._len2dTo3dRatio;
1028 const gp_XY& pCommOut = ER._uvOut;
1029 gp_XY pCommIn = pCommOut + normAvg * maxLen2D;
1030 _Segment segCommon( pCommOut, pCommIn );
1031 _SegmentIntersection intersection;
1032 vector< const _Segment* > foundSegs;
1033 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
1035 _PolyLine& L1 = _polyLineVec[ iL1 ];
1036 const _SegmentTree::box_type* boxL1 = L1._segTree->getBox();
1037 if ( boxL1->IsOut ( pCommOut, pCommIn ))
1039 for ( size_t iLE = 1; iLE < L1._lEdges.size(); ++iLE )
1042 L1._segTree->GetSegmentsNear( segCommon, foundSegs );
1043 for ( size_t i = 0; i < foundSegs.size(); ++i )
1044 if ( intersection.Compute( *foundSegs[i], segCommon ) &&
1045 intersection._param2 > 1e-10 )
1047 double len2D = intersection._param2 * maxLen2D / ( 2 + L1._advancable );
1048 if ( len2D < maxLen2D ) {
1050 pCommIn = pCommOut + normAvg * maxLen2D; // here length of segCommon changes
1056 // remove _LayerEdge's intersecting segCommon
1057 for ( int isR = 0; isR < 2; ++isR ) // loop on [ LL, LR ]
1059 _PolyLine& L = isR ? LR : LL;
1060 _PolyLine::TEdgeIterator eIt = isR ? L._lEdges.begin()+1 : L._lEdges.end()-2;
1061 int dIt = isR ? +1 : -1;
1062 if ( nbAdvancableL == 1 && L._advancable && normL * normR > -0.01 )
1063 continue; // obtuse internal angle
1064 // at least 3 _LayerEdge's should remain in a _PolyLine
1065 if ( L._lEdges.size() < 4 ) continue;
1067 _SegmentIntersection lastIntersection;
1068 for ( ; iLE < L._lEdges.size(); ++iLE, eIt += dIt )
1070 gp_XY uvIn = eIt->_uvOut + eIt->_normal2D * _thickness * eIt->_len2dTo3dRatio;
1071 _Segment segOfEdge( eIt->_uvOut, uvIn );
1072 if ( !intersection.Compute( segCommon, segOfEdge ))
1074 lastIntersection._param1 = intersection._param1;
1075 lastIntersection._param2 = intersection._param2;
1077 if ( iLE >= L._lEdges.size() - 1 )
1079 // all _LayerEdge's intersect the segCommon, limit inflation
1080 // of remaining 3 _LayerEdge's
1081 vector< _LayerEdge > newEdgeVec( Min( 3, L._lEdges.size() ));
1082 newEdgeVec.front() = L._lEdges.front();
1083 newEdgeVec.back() = L._lEdges.back();
1084 if ( newEdgeVec.size() == 3 )
1086 newEdgeVec[1] = L._lEdges[ isR ? (L._lEdges.size() - 2) : 1 ];
1087 newEdgeVec[1]._len2dTo3dRatio *= lastIntersection._param2;
1089 L._lEdges.swap( newEdgeVec );
1090 if ( !isR ) std::swap( lastIntersection._param1 , lastIntersection._param2 );
1091 L._lEdges.front()._len2dTo3dRatio *= lastIntersection._param1; // ??
1092 L._lEdges.back ()._len2dTo3dRatio *= lastIntersection._param2;
1094 else if ( iLE != 1 )
1096 // eIt points to the _LayerEdge not intersecting with segCommon
1098 LR._lEdges.erase( LR._lEdges.begin()+1, eIt );
1100 LL._lEdges.erase( eIt, --LL._lEdges.end() );
1101 // eIt = isR ? L._lEdges.begin()+1 : L._lEdges.end()-2;
1102 // for ( size_t i = 1; i < iLE; ++i, eIt += dIt )
1103 // eIt->_isBlocked = true;
1107 else // ------------------------------------------ CONCAVE ANGLE
1109 if ( nbAdvancableL == 1 )
1111 // make that the _LayerEdge at VERTEX is not shared by LL and LR:
1112 // different normals is a sign that they are not shared
1113 _LayerEdge& notSharedEdge = LL._advancable ? LR._lEdges[0] : LL._lEdges.back();
1114 _LayerEdge& sharedEdge = LR._advancable ? LR._lEdges[0] : LL._lEdges.back();
1116 notSharedEdge._normal2D.SetCoord( 0.,0. );
1117 sharedEdge._normal2D = normAvg;
1118 sharedEdge._isBlocked = false;
1119 notSharedEdge._isBlocked = true;
1125 //================================================================================
1127 * \brief initialize data of a _LayerEdge
1129 //================================================================================
1131 void _ViscousBuilder2D::setLayerEdgeData( _LayerEdge& lEdge,
1133 Handle(Geom2d_Curve)& pcurve,
1134 Handle(Geom_Curve)& curve,
1137 GeomAPI_ProjectPointOnSurf* faceProj)
1140 if ( faceProj && !curve.IsNull() )
1142 uv = pcurve->Value( u );
1143 gp_Vec tangent; gp_Pnt p; gp_Vec du, dv;
1144 curve->D1( u, p, tangent );
1147 _surface->D1( uv.X(), uv.Y(), p, du, dv );
1148 gp_Vec faceNorm = du ^ dv;
1149 gp_Vec normal = faceNorm ^ tangent;
1151 p = pOut.XYZ() + normal.XYZ() * /*1e-2 * */_hyp->GetTotalThickness() / _hyp->GetNumberLayers();
1152 faceProj->Perform( p );
1153 if ( !faceProj->IsDone() || faceProj->NbPoints() < 1 )
1154 return setLayerEdgeData( lEdge, u, pcurve, curve, p, reverse, NULL );
1155 Quantity_Parameter U,V;
1156 faceProj->LowerDistanceParameters(U,V);
1157 lEdge._normal2D.SetCoord( U - uv.X(), V - uv.Y() );
1158 lEdge._normal2D.Normalize();
1163 pcurve->D1( u, uv, tangent );
1164 tangent.Normalize();
1167 lEdge._normal2D.SetCoord( -tangent.Y(), tangent.X() );
1169 lEdge._uvOut = lEdge._uvIn = uv.XY();
1170 lEdge._ray.SetLocation ( lEdge._uvOut );
1171 lEdge._ray.SetDirection( lEdge._normal2D );
1172 lEdge._isBlocked = false;
1173 lEdge._length2D = 0;
1175 lEdge._ID = _nbLE++;
1179 //================================================================================
1181 * \brief Compute and set _LayerEdge::_len2dTo3dRatio
1183 //================================================================================
1185 void _ViscousBuilder2D::setLenRatio( _LayerEdge& LE, const gp_Pnt& pOut )
1187 const double probeLen2d = 1e-3;
1189 gp_Pnt2d p2d = LE._uvOut + LE._normal2D * probeLen2d;
1190 gp_Pnt p3d = _surface->Value( p2d.X(), p2d.Y() );
1191 double len3d = p3d.Distance( pOut );
1192 if ( len3d < std::numeric_limits<double>::min() )
1193 LE._len2dTo3dRatio = std::numeric_limits<double>::min();
1195 LE._len2dTo3dRatio = probeLen2d / len3d;
1198 //================================================================================
1200 * \brief Increase length of _LayerEdge's to reach the required thickness of layers
1202 //================================================================================
1204 bool _ViscousBuilder2D::inflate()
1206 // Limit size of inflation step by geometry size found by
1207 // itersecting _LayerEdge's with _Segment's
1208 double minSize = _thickness, maxSize = 0;
1209 vector< const _Segment* > foundSegs;
1210 _SegmentIntersection intersection;
1211 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
1213 _PolyLine& L1 = _polyLineVec[ iL1 ];
1214 for ( size_t iL2 = 0; iL2 < L1._reachableLines.size(); ++iL2 )
1216 _PolyLine& L2 = * L1._reachableLines[ iL2 ];
1217 for ( size_t iLE = 1; iLE < L1._lEdges.size(); ++iLE )
1220 L2._segTree->GetSegmentsNear( L1._lEdges[iLE]._ray, foundSegs );
1221 for ( size_t i = 0; i < foundSegs.size(); ++i )
1222 if ( ! L1.IsAdjacent( *foundSegs[i], & L1._lEdges[iLE] ) &&
1223 intersection.Compute( *foundSegs[i], L1._lEdges[iLE]._ray ))
1225 double distToL2 = intersection._param2 / L1._lEdges[iLE]._len2dTo3dRatio;
1226 double size = distToL2 / ( 1 + L1._advancable + L2._advancable );
1227 if ( 1e-10 < size && size < minSize )
1229 if ( size > maxSize )
1235 if ( minSize > maxSize ) // no collisions possible
1236 maxSize = _thickness;
1238 cout << "-- minSize = " << minSize << ", maxSize = " << maxSize << endl;
1241 double curThick = 0, stepSize = minSize;
1243 if ( maxSize > _thickness )
1244 maxSize = _thickness;
1245 while ( curThick < maxSize )
1247 curThick += stepSize * 1.25;
1248 if ( curThick > _thickness )
1249 curThick = _thickness;
1251 // Elongate _LayerEdge's
1252 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
1254 _PolyLine& L = _polyLineVec[ iL ];
1255 if ( !L._advancable ) continue;
1256 bool lenChange = false;
1257 for ( size_t iLE = L.FirstLEdge(); iLE < L._lEdges.size(); ++iLE )
1258 lenChange |= L._lEdges[iLE].SetNewLength( curThick );
1259 // for ( int k=0; k<L._segments.size(); ++k)
1260 // cout << "( " << L._segments[k].p1().X() << ", " <<L._segments[k].p1().Y() << " ) "
1261 // << "( " << L._segments[k].p2().X() << ", " <<L._segments[k].p2().Y() << " ) "
1264 L._segTree.reset( new _SegmentTree( L._segments ));
1267 // Avoid intersection of _Segment's
1268 bool allBlocked = fixCollisions();
1271 break; // no more inflating possible
1273 stepSize = Max( stepSize , _thickness / 10. );
1277 // if (nbSteps == 0 )
1278 // return error("failed at the very first inflation step");
1281 // remove _LayerEdge's of one line intersecting with each other
1282 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
1284 _PolyLine& L = _polyLineVec[ iL ];
1285 if ( !L._advancable ) continue;
1287 // replace an inactive (1st) _LayerEdge with an active one of a neighbour _PolyLine
1288 if ( /*!L._leftLine->_advancable &&*/ L.IsCommonEdgeShared( *L._leftLine ) ) {
1289 L._lEdges[0] = L._leftLine->_lEdges.back();
1291 if ( !L._rightLine->_advancable && L.IsCommonEdgeShared( *L._rightLine ) ) {
1292 L._lEdges.back() = L._rightLine->_lEdges[0];
1295 _SegmentIntersection intersection;
1296 for ( int isR = 0; ( isR < 2 && L._lEdges.size() > 2 ); ++isR )
1298 int nbRemove = 0, deltaIt = isR ? -1 : +1;
1299 _PolyLine::TEdgeIterator eIt = isR ? L._lEdges.end()-1 : L._lEdges.begin();
1300 if ( eIt->_length2D == 0 ) continue;
1301 _Segment seg1( eIt->_uvOut, eIt->_uvIn );
1302 for ( eIt += deltaIt; nbRemove < L._lEdges.size()-1; eIt += deltaIt )
1304 _Segment seg2( eIt->_uvOut, eIt->_uvIn );
1305 if ( !intersection.Compute( seg1, seg2 ))
1309 if ( nbRemove > 0 ) {
1310 if ( nbRemove == L._lEdges.size()-1 ) // 1st and last _LayerEdge's intersect
1313 _LayerEdge& L0 = L._lEdges.front();
1314 _LayerEdge& L1 = L._lEdges.back();
1315 L0._length2D *= intersection._param1 * 0.5;
1316 L1._length2D *= intersection._param2 * 0.5;
1317 L0._uvIn = L0._uvOut + L0._normal2D * L0._length2D;
1318 L1._uvIn = L1._uvOut + L1._normal2D * L1._length2D;
1319 if ( L.IsCommonEdgeShared( *L._leftLine ))
1320 L._leftLine->_lEdges.back() = L0;
1323 L._lEdges.erase( L._lEdges.end()-nbRemove-1,
1324 L._lEdges.end()-nbRemove );
1326 L._lEdges.erase( L._lEdges.begin()+1,
1327 L._lEdges.begin()+1+nbRemove );
1334 //================================================================================
1336 * \brief Remove intersection of _PolyLine's
1338 //================================================================================
1340 bool _ViscousBuilder2D::fixCollisions()
1342 // look for intersections of _Segment's by intersecting _LayerEdge's with
1344 vector< const _Segment* > foundSegs;
1345 _SegmentIntersection intersection;
1347 list< pair< _LayerEdge*, double > > edgeLenLimitList;
1348 list< _LayerEdge* > blockedEdgesList;
1350 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
1352 _PolyLine& L1 = _polyLineVec[ iL1 ];
1353 //if ( !L1._advancable ) continue;
1354 for ( size_t iL2 = 0; iL2 < L1._reachableLines.size(); ++iL2 )
1356 _PolyLine& L2 = * L1._reachableLines[ iL2 ];
1357 for ( size_t iLE = L1.FirstLEdge(); iLE < L1._lEdges.size(); ++iLE )
1359 _LayerEdge& LE1 = L1._lEdges[iLE];
1360 if ( LE1._isBlocked ) continue;
1362 L2._segTree->GetSegmentsNear( LE1._ray, foundSegs );
1363 for ( size_t i = 0; i < foundSegs.size(); ++i )
1365 if ( ! L1.IsAdjacent( *foundSegs[i], &LE1 ) &&
1366 intersection.Compute( *foundSegs[i], LE1._ray ))
1368 const double dist2DToL2 = intersection._param2;
1369 double newLen2D = dist2DToL2 / 2;
1370 if ( newLen2D < 1.1 * LE1._length2D ) // collision!
1372 if ( newLen2D > 0 || !L1._advancable )
1374 blockedEdgesList.push_back( &LE1 );
1375 if ( L1._advancable && newLen2D > 0 )
1377 edgeLenLimitList.push_back( make_pair( &LE1, newLen2D ));
1378 blockedEdgesList.push_back( &L2._lEdges[ foundSegs[i]->_indexInLine ]);
1379 blockedEdgesList.push_back( &L2._lEdges[ foundSegs[i]->_indexInLine + 1 ]);
1381 else // here dist2DToL2 < 0 and LE1._length2D == 0
1383 _LayerEdge* LE2[2] = { & L2._lEdges[ foundSegs[i]->_indexInLine ],
1384 & L2._lEdges[ foundSegs[i]->_indexInLine + 1 ] };
1385 _Segment outSeg2( LE2[0]->_uvOut, LE2[1]->_uvOut );
1386 intersection.Compute( outSeg2, LE1._ray );
1387 newLen2D = intersection._param2 / 2;
1390 edgeLenLimitList.push_back( make_pair( LE2[0], newLen2D ));
1391 edgeLenLimitList.push_back( make_pair( LE2[1], newLen2D ));
1402 // limit length of _LayerEdge's that are extrema of _PolyLine's
1403 // to avoid intersection of these _LayerEdge's
1404 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
1406 _PolyLine& L = _polyLineVec[ iL1 ];
1407 if ( L._lEdges.size() < 4 ) // all intermediate _LayerEdge's intersect with extremum ones
1409 _LayerEdge& LEL = L._leftLine->_lEdges.back();
1410 _LayerEdge& LER = L._lEdges.back();
1411 _Segment segL( LEL._uvOut, LEL._uvIn );
1412 _Segment segR( LER._uvOut, LER._uvIn );
1413 double newLen2DL, newLen2DR;
1414 if ( intersection.Compute( segL, LER._ray ))
1416 newLen2DR = intersection._param2 / 2;
1417 newLen2DL = LEL._length2D * intersection._param1 / 2;
1419 else if ( intersection.Compute( segR, LEL._ray ))
1421 newLen2DL = intersection._param2 / 2;
1422 newLen2DR = LER._length2D * intersection._param1 / 2;
1428 if ( newLen2DL > 0 && newLen2DR > 0 )
1430 if ( newLen2DL < 1.1 * LEL._length2D )
1431 edgeLenLimitList.push_back( make_pair( &LEL, newLen2DL ));
1432 if ( newLen2DR < 1.1 * LER._length2D )
1433 edgeLenLimitList.push_back( make_pair( &LER, newLen2DR ));
1438 // set limited length to _LayerEdge's
1439 list< pair< _LayerEdge*, double > >::iterator edge2Len = edgeLenLimitList.begin();
1440 for ( ; edge2Len != edgeLenLimitList.end(); ++edge2Len )
1442 _LayerEdge* LE = edge2Len->first;
1443 if ( LE->_length2D > edge2Len->second )
1445 LE->_isBlocked = false;
1446 LE->SetNewLength( edge2Len->second / LE->_len2dTo3dRatio );
1448 LE->_isBlocked = true;
1451 // block inflation of _LayerEdge's
1452 list< _LayerEdge* >::iterator edge = blockedEdgesList.begin();
1453 for ( ; edge != blockedEdgesList.end(); ++edge )
1454 (*edge)->_isBlocked = true;
1456 // find a not blocked _LayerEdge
1457 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
1459 _PolyLine& L = _polyLineVec[ iL ];
1460 if ( !L._advancable ) continue;
1461 for ( size_t iLE = L.FirstLEdge(); iLE < L._lEdges.size(); ++iLE )
1462 if ( !L._lEdges[ iLE ]._isBlocked )
1469 //================================================================================
1471 * \brief Create new edges and shrink edges existing on a non-advancable _PolyLine
1472 * adjacent to an advancable one.
1474 //================================================================================
1476 bool _ViscousBuilder2D::shrink()
1478 gp_Pnt2d uv; //gp_Vec2d tangent;
1479 _SegmentIntersection intersection;
1482 for ( size_t iL1 = 0; iL1 < _polyLineVec.size(); ++iL1 )
1484 _PolyLine& L = _polyLineVec[ iL1 ]; // line with no layers
1485 if ( L._advancable )
1487 const int nbAdvancable = ( L._rightLine->_advancable + L._leftLine->_advancable );
1488 if ( nbAdvancable == 0 )
1491 const TopoDS_Vertex& V1 = L._wire->FirstVertex( L._edgeInd );
1492 const TopoDS_Vertex& V2 = L._wire->LastVertex ( L._edgeInd );
1493 const int v1ID = getMeshDS()->ShapeToIndex( V1 );
1494 const int v2ID = getMeshDS()->ShapeToIndex( V2 );
1495 const bool isShrinkableL = ! _noShrinkVert.count( v1ID ) && L._leftLine->_advancable;
1496 const bool isShrinkableR = ! _noShrinkVert.count( v2ID ) && L._rightLine->_advancable;
1497 if ( !isShrinkableL && !isShrinkableR )
1500 const TopoDS_Edge& E = L._wire->Edge ( L._edgeInd );
1501 const int edgeID = L._wire->EdgeID ( L._edgeInd );
1502 const double edgeLen = L._wire->EdgeLength ( L._edgeInd );
1503 Handle(Geom2d_Curve) pcurve = L._wire->Curve2d ( L._edgeInd );
1504 const bool edgeReversed = ( E.Orientation() == TopAbs_REVERSED );
1506 SMESH_MesherHelper helper( *_mesh ); // to create nodes and edges on E
1507 helper.SetSubShape( E );
1508 helper.SetElementsOnShape( true );
1510 // Check a FACE adjacent to _face by E
1511 bool existingNodesFound = false;
1512 TopoDS_Face adjFace;
1513 PShapeIteratorPtr faceIt = _helper.GetAncestors( E, *_mesh, TopAbs_FACE );
1514 while ( const TopoDS_Shape* f = faceIt->next() )
1515 if ( !_face.IsSame( *f ))
1517 adjFace = TopoDS::Face( *f );
1518 SMESH_ProxyMesh::Ptr pm = _ProxyMeshHolder::FindProxyMeshOfFace( adjFace, *_mesh );
1519 if ( !pm || pm->NbProxySubMeshes() == 0 /*|| !pm->GetProxySubMesh( E )*/)
1521 // There are no viscous layers on an adjacent FACE, clear it's 2D mesh
1522 removeMeshFaces( adjFace );
1523 // if ( removeMeshFaces( adjFace ))
1524 // _clearedFaces.push_back( adjFace ); // to re-compute after all
1528 // There are viscous layers on the adjacent FACE; shrink must be already done;
1532 const vector<UVPtStruct>& points = L._wire->GetUVPtStruct();
1533 int iPFrom = L._firstPntInd, iPTo = L._lastPntInd;
1534 if ( isShrinkableL )
1536 vector<gp_XY>& uvVec = L._lEdges.front()._uvRefined;
1537 for ( int i = 0; i < _hyp->GetNumberLayers(); ++i ) {
1538 const UVPtStruct& uvPt = points[ iPFrom + i + 1 ];
1539 L._leftNodes.push_back( uvPt.node );
1540 uvVec.push_back ( pcurve->Value( uvPt.param ).XY() );
1543 if ( isShrinkableR )
1545 vector<gp_XY>& uvVec = L._lEdges.back()._uvRefined;
1546 for ( int i = 0; i < _hyp->GetNumberLayers(); ++i ) {
1547 const UVPtStruct& uvPt = points[ iPTo - i - 1 ];
1548 L._rightNodes.push_back( uvPt.node );
1549 uvVec.push_back ( pcurve->Value( uvPt.param ).XY() );
1552 // make proxy sub-mesh data of present nodes
1554 if ( isShrinkableL ) iPFrom += _hyp->GetNumberLayers();
1555 if ( isShrinkableR ) iPTo -= _hyp->GetNumberLayers();
1556 UVPtStructVec nodeDataVec( & points[ iPFrom ], & points[ iPTo + 1 ]);
1558 double normSize = nodeDataVec.back().normParam - nodeDataVec.front().normParam;
1559 for ( int iP = nodeDataVec.size()-1; iP >= 0 ; --iP )
1560 nodeDataVec[iP].normParam =
1561 ( nodeDataVec[iP].normParam - nodeDataVec[0].normParam ) / normSize;
1563 const SMDS_MeshNode* n = nodeDataVec.front().node;
1564 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
1565 nodeDataVec.front().param = L._wire->FirstU( L._edgeInd );
1566 n = nodeDataVec.back().node;
1567 if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
1568 nodeDataVec.back().param = L._wire->LastU( L._edgeInd );
1570 _ProxyMeshOfFace::_EdgeSubMesh* myEdgeSM = getProxyMesh()->GetEdgeSubMesh( edgeID );
1571 myEdgeSM->SetUVPtStructVec( nodeDataVec );
1573 existingNodesFound = true;
1576 } // loop on FACEs sharing E
1578 // Commented as a case with a seam EDGE (issue 0052461) is hard to support
1579 // because SMESH_ProxyMesh can't hold different sub-meshes for two
1580 // 2D representations of the seam. But such a case is not a real practice one.
1581 // Check if L is an already shrinked seam
1582 // if ( adjFace.IsNull() && _helper.IsRealSeam( edgeID ))
1584 // for ( int iL2 = iL1-1; iL2 > -1; --iL2 )
1586 // _PolyLine& L2 = _polyLineVec[ iL2 ];
1587 // if ( edgeID == L2._wire->EdgeID( L2._edgeInd ))
1589 // // copy layer nodes
1590 // const int seamPar = _helper.GetPeriodicIndex();
1591 // vector<gp_XY>& uvVec = L._lEdges.front()._uvRefined;
1592 // if ( isShrinkableL )
1594 // L._leftNodes = L2._rightNodes;
1595 // uvVec = L2._lEdges.back()._uvRefined;
1597 // if ( isShrinkableR )
1599 // L._rightNodes = L2._leftNodes;
1600 // uvVec = L2._lEdges.front()._uvRefined;
1602 // for ( size_t i = 0; i < uvVec.size(); ++i )
1604 // gp_XY & uv = uvVec[i];
1605 // uv.SetCoord( seamPar, _helper.GetOtherParam( uv.Coord( seamPar )));
1608 // existingNodesFound = true;
1614 if ( existingNodesFound )
1615 continue; // nothing more to do in this case
1617 double u1 = L._wire->FirstU( L._edgeInd ), uf = u1;
1618 double u2 = L._wire->LastU ( L._edgeInd ), ul = u2;
1620 // a ratio to pass 2D <--> 1D
1621 const double len1D = 1e-3;
1622 const double len2D = pcurve->Value(uf).Distance( pcurve->Value(uf+len1D));
1623 double len1dTo2dRatio = len1D / len2D;
1625 // create a vector of proxy nodes
1626 const vector<UVPtStruct>& points = L._wire->GetUVPtStruct();
1627 UVPtStructVec nodeDataVec( & points[ L._firstPntInd ],
1628 & points[ L._lastPntInd + 1 ]);
1629 nodeDataVec.front().param = u1; // U on vertex is correct on only one of shared edges
1630 nodeDataVec.back ().param = u2;
1631 nodeDataVec.front().normParam = 0;
1632 nodeDataVec.back ().normParam = 1;
1634 // Get length of existing segments (from an edge start to a node) and their nodes
1635 vector< double > segLengths( nodeDataVec.size() - 1 );
1636 BRepAdaptor_Curve curve( E );
1637 for ( size_t iP = 1; iP < nodeDataVec.size(); ++iP )
1639 const double len = GCPnts_AbscissaPoint::Length( curve, uf, nodeDataVec[iP].param );
1640 segLengths[ iP-1 ] = len;
1643 // Move first and last parameters on EDGE (U of n1) according to layers' thickness
1644 // and create nodes of layers on EDGE ( -x-x-x )
1648 // x-----x-----x-----x-----
1653 // x-x-x-x-----x-----x----
1656 int isRShrinkedForAdjacent;
1657 UVPtStructVec nodeDataForAdjacent;
1658 for ( int isR = 0; isR < 2; ++isR )
1660 _PolyLine* L2 = isR ? L._rightLine : L._leftLine; // line with layers
1661 if ( !L2->_advancable &&
1662 !toShrinkForAdjacent( adjFace, E, L._wire->FirstVertex( L._edgeInd + isR )))
1664 if ( isR ? !isShrinkableR : !isShrinkableL )
1667 double & u = isR ? u2 : u1; // param to move
1668 double u0 = isR ? ul : uf; // init value of the param to move
1669 int iPEnd = isR ? nodeDataVec.size() - 1 : 0;
1671 _LayerEdge& nearLE = isR ? L._lEdges.back() : L._lEdges.front();
1672 _LayerEdge& farLE = isR ? L._lEdges.front() : L._lEdges.back();
1674 // try to find length of advancement along L by intersecting L with
1675 // an adjacent _Segment of L2
1677 double& length2D = nearLE._length2D;
1678 double length1D = 0;
1679 sign = ( isR ^ edgeReversed ) ? -1. : 1.;
1681 bool isConvex = false;
1682 if ( L2->_advancable )
1684 const uvPtStruct& tang2P1 = points[ isR ? L2->_firstPntInd : L2->_lastPntInd ];
1685 const uvPtStruct& tang2P2 = points[ isR ? L2->_firstPntInd+1 : L2->_lastPntInd-1 ];
1686 gp_XY seg2Dir( tang2P2.u - tang2P1.u,
1687 tang2P2.v - tang2P1.v );
1688 int iFSeg2 = isR ? 0 : L2->_segments.size() - 1;
1689 int iLSeg2 = isR ? 1 : L2->_segments.size() - 2;
1690 gp_XY uvLSeg2In = L2->_lEdges[ iLSeg2 ]._uvIn;
1691 Handle(Geom2d_Line) seg2Line = new Geom2d_Line( uvLSeg2In, seg2Dir );
1693 Geom2dAdaptor_Curve edgeCurve( pcurve, Min( uf, ul ), Max( uf, ul ));
1694 Geom2dAdaptor_Curve seg2Curve( seg2Line );
1695 Geom2dInt_GInter curveInt( edgeCurve, seg2Curve, 1e-7, 1e-7 );
1696 isConvex = ( curveInt.IsDone() && !curveInt.IsEmpty() );
1699 length1D = Abs( u - curveInt.Point( 1 ).ParamOnFirst() );
1700 double maxDist2d = 2 * L2->_lEdges[ iLSeg2 ]._length2D;
1701 isConvex = ( length1D < maxDist2d * len1dTo2dRatio );
1708 if ( !isConvex ) { /* concave VERTEX */ /* o-----o---
1714 length2D = L2->_lEdges[ iFSeg2 ]._length2D;
1715 //if ( L2->_advancable ) continue;
1718 else // L2 is advancable but in the face adjacent by L
1720 length2D = farLE._length2D;
1721 if ( length2D == 0 ) {
1722 _LayerEdge& neighborLE =
1723 ( isR ? L._leftLine->_lEdges.back() : L._rightLine->_lEdges.front() );
1724 length2D = neighborLE._length2D;
1725 if ( length2D == 0 )
1726 length2D = _thickness * nearLE._len2dTo3dRatio;
1730 // move u to the internal boundary of layers
1732 // x-x-x-x-----x-----x----
1733 double maxLen3D = Min( _thickness, edgeLen / ( 1 + nbAdvancable ));
1734 double maxLen2D = maxLen3D * nearLE._len2dTo3dRatio;
1735 if ( !length2D ) length2D = length1D / len1dTo2dRatio;
1736 if ( Abs( length2D ) > maxLen2D )
1737 length2D = maxLen2D;
1738 nearLE._uvIn = nearLE._uvOut + nearLE._normal2D * length2D;
1740 u += length2D * len1dTo2dRatio * sign;
1741 nodeDataVec[ iPEnd ].param = u;
1743 gp_Pnt2d newUV = pcurve->Value( u );
1744 nodeDataVec[ iPEnd ].u = newUV.X();
1745 nodeDataVec[ iPEnd ].v = newUV.Y();
1747 // compute params of layers on L
1748 vector<double> heights;
1749 calcLayersHeight( u - u0, heights );
1751 vector< double > params( heights.size() );
1752 for ( size_t i = 0; i < params.size(); ++i )
1753 params[ i ] = u0 + heights[ i ];
1755 // create nodes of layers and edges between them
1757 vector< const SMDS_MeshNode* >& layersNode = isR ? L._rightNodes : L._leftNodes;
1758 vector<gp_XY>& nodeUV = ( isR ? L._lEdges.back() : L._lEdges[0] )._uvRefined;
1759 nodeUV.resize ( _hyp->GetNumberLayers() );
1760 layersNode.resize( _hyp->GetNumberLayers() );
1761 const SMDS_MeshNode* vertexNode = nodeDataVec[ iPEnd ].node;
1762 const SMDS_MeshNode * prevNode = vertexNode;
1763 for ( size_t i = 0; i < params.size(); ++i )
1765 gp_Pnt p = curve.Value( params[i] );
1766 layersNode[ i ] = helper.AddNode( p.X(), p.Y(), p.Z(), /*id=*/0, params[i] );
1767 nodeUV [ i ] = pcurve->Value( params[i] ).XY();
1768 helper.AddEdge( prevNode, layersNode[ i ] );
1769 prevNode = layersNode[ i ];
1772 // store data of layer nodes made for adjacent FACE
1773 if ( !L2->_advancable )
1775 isRShrinkedForAdjacent = isR;
1776 nodeDataForAdjacent.resize( _hyp->GetNumberLayers() );
1778 size_t iFrw = 0, iRev = nodeDataForAdjacent.size()-1, *i = isR ? &iRev : &iFrw;
1779 nodeDataForAdjacent[ *i ] = points[ isR ? L._lastPntInd : L._firstPntInd ];
1780 nodeDataForAdjacent[ *i ].param = u0;
1781 nodeDataForAdjacent[ *i ].normParam = isR;
1782 for ( ++iFrw, --iRev; iFrw < layersNode.size(); ++iFrw, --iRev )
1784 nodeDataForAdjacent[ *i ].node = layersNode[ iFrw - 1 ];
1785 nodeDataForAdjacent[ *i ].u = nodeUV [ iFrw - 1 ].X();
1786 nodeDataForAdjacent[ *i ].v = nodeUV [ iFrw - 1 ].Y();
1787 nodeDataForAdjacent[ *i ].param = params [ iFrw - 1 ];
1790 // replace a node on vertex by a node of last (most internal) layer
1791 // in a segment on E
1792 SMDS_ElemIteratorPtr segIt = vertexNode->GetInverseElementIterator( SMDSAbs_Edge );
1793 const SMDS_MeshNode* segNodes[3];
1794 while ( segIt->more() )
1796 const SMDS_MeshElement* segment = segIt->next();
1797 if ( segment->getshapeId() != edgeID ) continue;
1799 const int nbNodes = segment->NbNodes();
1800 for ( int i = 0; i < nbNodes; ++i )
1802 const SMDS_MeshNode* n = segment->GetNode( i );
1803 segNodes[ i ] = ( n == vertexNode ? layersNode.back() : n );
1805 getMeshDS()->ChangeElementNodes( segment, segNodes, nbNodes );
1808 nodeDataVec[ iPEnd ].node = layersNode.back();
1810 } // loop on the extremities of L
1812 // Shrink edges to fit in between the layers at EDGE ends
1814 double newLength = GCPnts_AbscissaPoint::Length( curve, u1, u2 );
1815 double lenRatio = newLength / edgeLen * ( edgeReversed ? -1. : 1. );
1816 for ( size_t iP = 1; iP < nodeDataVec.size()-1; ++iP )
1818 const SMDS_MeshNode* oldNode = nodeDataVec[iP].node;
1820 GCPnts_AbscissaPoint discret( curve, segLengths[iP-1] * lenRatio, u1 );
1821 if ( !discret.IsDone() )
1822 throw SALOME_Exception(LOCALIZED("GCPnts_AbscissaPoint failed"));
1824 nodeDataVec[iP].param = discret.Parameter();
1825 if ( oldNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
1826 throw SALOME_Exception(SMESH_Comment("ViscousBuilder2D: not SMDS_TOP_EDGE node position: ")
1827 << oldNode->GetPosition()->GetTypeOfPosition()
1828 << " of node " << oldNode->GetID());
1829 SMDS_EdgePosition* pos = static_cast<SMDS_EdgePosition*>( oldNode->GetPosition() );
1830 pos->SetUParameter( nodeDataVec[iP].param );
1832 gp_Pnt newP = curve.Value( nodeDataVec[iP].param );
1833 getMeshDS()->MoveNode( oldNode, newP.X(), newP.Y(), newP.Z() );
1835 gp_Pnt2d newUV = pcurve->Value( nodeDataVec[iP].param ).XY();
1836 nodeDataVec[iP].u = newUV.X();
1837 nodeDataVec[iP].v = newUV.Y();
1838 nodeDataVec[iP].normParam = segLengths[iP-1] / edgeLen;
1839 // nodeDataVec[iP].x = segLengths[iP-1] / edgeLen;
1840 // nodeDataVec[iP].y = segLengths[iP-1] / edgeLen;
1843 // Add nodeDataForAdjacent to nodeDataVec
1845 if ( !nodeDataForAdjacent.empty() )
1847 const double par1 = isRShrinkedForAdjacent ? u2 : uf;
1848 const double par2 = isRShrinkedForAdjacent ? ul : u1;
1849 const double shrinkLen = GCPnts_AbscissaPoint::Length( curve, par1, par2 );
1851 // compute new normParam for nodeDataVec
1852 for ( size_t iP = 0; iP < nodeDataVec.size()-1; ++iP )
1853 nodeDataVec[iP+1].normParam = segLengths[iP] / ( edgeLen + shrinkLen );
1854 double normDelta = 1 - nodeDataVec.back().normParam;
1855 if ( !isRShrinkedForAdjacent )
1856 for ( size_t iP = 0; iP < nodeDataVec.size(); ++iP )
1857 nodeDataVec[iP].normParam += normDelta;
1859 // compute new normParam for nodeDataForAdjacent
1860 const double deltaR = isRShrinkedForAdjacent ? nodeDataVec.back().normParam : 0;
1861 for ( size_t iP = !isRShrinkedForAdjacent; iP < nodeDataForAdjacent.size(); ++iP )
1863 double lenFromPar1 =
1864 GCPnts_AbscissaPoint::Length( curve, par1, nodeDataForAdjacent[iP].param );
1865 nodeDataForAdjacent[iP].normParam = deltaR + normDelta * lenFromPar1 / shrinkLen;
1867 // concatenate nodeDataVec and nodeDataForAdjacent
1868 nodeDataVec.insert(( isRShrinkedForAdjacent ? nodeDataVec.end() : nodeDataVec.begin() ),
1869 nodeDataForAdjacent.begin(), nodeDataForAdjacent.end() );
1872 // Extend nodeDataVec by a node located at the end of not shared _LayerEdge
1873 /* n - to add to nodeDataVec
1882 for ( int isR = 0; isR < 2; ++isR )
1884 _PolyLine& L2 = *( isR ? L._rightLine : L._leftLine ); // line with layers
1885 if ( ! L2._advancable || L.IsCommonEdgeShared( L2 ) )
1887 vector< const SMDS_MeshNode* >& layerNodes2 = isR ? L2._leftNodes : L2._rightNodes;
1888 _LayerEdge& LE2 = isR ? L2._lEdges.front() : L2._lEdges.back();
1889 if ( layerNodes2.empty() )
1891 // refine the not shared _LayerEdge
1892 vector<double> layersHeight;
1893 calcLayersHeight( LE2._length2D, layersHeight );
1895 vector<gp_XY>& nodeUV2 = LE2._uvRefined;
1896 nodeUV2.resize ( _hyp->GetNumberLayers() );
1897 layerNodes2.resize( _hyp->GetNumberLayers() );
1898 for ( size_t i = 0; i < layersHeight.size(); ++i )
1900 gp_XY uv = LE2._uvOut + LE2._normal2D * layersHeight[i];
1901 gp_Pnt p = _surface->Value( uv.X(), uv.Y() );
1903 layerNodes2[ i ] = _helper.AddNode( p.X(), p.Y(), p.Z(), /*id=*/0, uv.X(), uv.Y() );
1906 UVPtStruct ptOfNode;
1907 ptOfNode.u = LE2._uvRefined.back().X();
1908 ptOfNode.v = LE2._uvRefined.back().Y();
1909 ptOfNode.node = layerNodes2.back();
1910 ptOfNode.param = isR ? ul : uf;
1911 ptOfNode.normParam = isR ? 1 : 0;
1913 nodeDataVec.insert(( isR ? nodeDataVec.end() : nodeDataVec.begin() ), ptOfNode );
1915 // recompute normParam of nodes in nodeDataVec
1916 newLength = GCPnts_AbscissaPoint::Length( curve,
1917 nodeDataVec.front().param,
1918 nodeDataVec.back().param);
1919 for ( size_t iP = 1; iP < nodeDataVec.size(); ++iP )
1921 const double len = GCPnts_AbscissaPoint::Length( curve,
1922 nodeDataVec.front().param,
1923 nodeDataVec[iP].param );
1924 nodeDataVec[iP].normParam = len / newLength;
1928 // create a proxy sub-mesh containing the moved nodes
1929 _ProxyMeshOfFace::_EdgeSubMesh* edgeSM = getProxyMesh()->GetEdgeSubMesh( edgeID );
1930 edgeSM->SetUVPtStructVec( nodeDataVec );
1932 // set a sub-mesh event listener to remove just created edges when
1933 // "ViscousLayers2D" hypothesis is modified
1934 VISCOUS_3D::ToClearSubWithMain( _mesh->GetSubMesh( E ), _face );
1936 } // loop on _polyLineVec
1941 //================================================================================
1943 * \brief Returns true if there will be a shrinked mesh on EDGE E of FACE adjFace
1946 //================================================================================
1948 bool _ViscousBuilder2D::toShrinkForAdjacent( const TopoDS_Face& adjFace,
1949 const TopoDS_Edge& E,
1950 const TopoDS_Vertex& V)
1952 if ( _noShrinkVert.count( getMeshDS()->ShapeToIndex( V )) || adjFace.IsNull() )
1955 TopoDS_Shape hypAssignedTo;
1956 if ( const StdMeshers_ViscousLayers2D* vlHyp = findHyp( *_mesh, adjFace, &hypAssignedTo ))
1958 VISCOUS_2D::_ViscousBuilder2D builder( *_mesh, adjFace, vlHyp );
1959 builder._faceSideVec = StdMeshers_FaceSide::GetFaceWires( adjFace, *_mesh, true, _error );
1960 builder.findEdgesWithLayers( hypAssignedTo );
1962 PShapeIteratorPtr edgeIt = _helper.GetAncestors( V, *_mesh, TopAbs_EDGE );
1963 while ( const TopoDS_Shape* edgeAtV = edgeIt->next() )
1965 if ( !edgeAtV->IsSame( E ) &&
1966 _helper.IsSubShape( *edgeAtV, adjFace ) &&
1967 !builder._ignoreShapeIds.count( getMeshDS()->ShapeToIndex( *edgeAtV )))
1976 //================================================================================
1980 //================================================================================
1982 bool _ViscousBuilder2D::refine()
1984 // find out orientation of faces to create
1986 ( _helper.GetSubShapeOri( _mesh->GetShapeToMesh(), _face ) == TopAbs_REVERSED );
1988 // store a proxyMesh in a sub-mesh
1989 // make faces on each _PolyLine
1990 vector< double > layersHeight;
1991 double prevLen2D = -1;
1992 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
1994 _PolyLine& L = _polyLineVec[ iL ];
1995 if ( !L._advancable ) continue;
1997 // replace an inactive (1st) _LayerEdge with an active one of a neighbour _PolyLine
1998 //size_t iLE = 0, nbLE = L._lEdges.size();
1999 const bool leftEdgeShared = L.IsCommonEdgeShared( *L._leftLine );
2000 const bool rightEdgeShared = L.IsCommonEdgeShared( *L._rightLine );
2001 if ( /*!L._leftLine->_advancable &&*/ leftEdgeShared )
2003 L._lEdges[0] = L._leftLine->_lEdges.back();
2004 //iLE += int( !L._leftLine->_advancable );
2006 if ( !L._rightLine->_advancable && rightEdgeShared )
2008 L._lEdges.back() = L._rightLine->_lEdges[0];
2012 // limit length of neighbour _LayerEdge's to avoid sharp change of layers thickness
2014 vector< double > segLen( L._lEdges.size() );
2017 // check if length modification is usefull: look for _LayerEdge's
2018 // with length limited due to collisions
2019 bool lenLimited = false;
2020 for ( size_t iLE = 1; ( iLE < L._lEdges.size()-1 && !lenLimited ); ++iLE )
2021 lenLimited = L._lEdges[ iLE ]._isBlocked;
2025 for ( size_t i = 1; i < segLen.size(); ++i )
2027 // accumulate length of segments
2028 double sLen = (L._lEdges[i-1]._uvOut - L._lEdges[i]._uvOut ).Modulus();
2029 segLen[i] = segLen[i-1] + sLen;
2031 const double totSegLen = segLen.back();
2032 // normalize the accumulated length
2033 for ( size_t iS = 1; iS < segLen.size(); ++iS )
2034 segLen[iS] /= totSegLen;
2036 for ( int isR = 0; isR < 2; ++isR )
2038 size_t iF = 0, iL = L._lEdges.size()-1;
2039 size_t *i = isR ? &iL : &iF;
2040 _LayerEdge* prevLE = & L._lEdges[ *i ];
2042 for ( ++iF, --iL; iF < L._lEdges.size()-1; ++iF, --iL )
2044 _LayerEdge& LE = L._lEdges[*i];
2045 if ( prevLE->_length2D > 0 )
2047 gp_XY tangent ( LE._normal2D.Y(), -LE._normal2D.X() );
2048 weight += Abs( tangent * ( prevLE->_uvIn - LE._uvIn )) / totSegLen;
2049 // gp_XY prevTang( LE._uvOut - prevLE->_uvOut );
2050 // gp_XY prevNorm( -prevTang.Y(), prevTang.X() );
2051 gp_XY prevNorm = LE._normal2D;
2052 double prevProj = prevNorm * ( prevLE->_uvIn - prevLE->_uvOut );
2053 if ( prevProj > 0 ) {
2054 prevProj /= prevNorm.Modulus();
2055 if ( LE._length2D < prevProj )
2056 weight += 0.75 * ( 1 - weight ); // length decrease is more preferable
2057 LE._length2D = weight * LE._length2D + ( 1 - weight ) * prevProj;
2058 LE._uvIn = LE._uvOut + LE._normal2D * LE._length2D;
2065 // DEBUG: to see _uvRefined. cout can be redirected to hide NETGEN output
2066 // cerr << "import smesh" << endl << "mesh = smesh.Mesh()"<< endl;
2068 const vector<UVPtStruct>& points = L._wire->GetUVPtStruct();
2070 // analyse extremities of the _PolyLine to find existing nodes
2071 const TopoDS_Vertex& V1 = L._wire->FirstVertex( L._edgeInd );
2072 const TopoDS_Vertex& V2 = L._wire->LastVertex ( L._edgeInd );
2073 const int v1ID = getMeshDS()->ShapeToIndex( V1 );
2074 const int v2ID = getMeshDS()->ShapeToIndex( V2 );
2075 const bool isShrinkableL = ! _noShrinkVert.count( v1ID );
2076 const bool isShrinkableR = ! _noShrinkVert.count( v2ID );
2078 bool hasLeftNode = ( !L._leftLine->_rightNodes.empty() && leftEdgeShared );
2079 bool hasRightNode = ( !L._rightLine->_leftNodes.empty() && rightEdgeShared );
2080 bool hasOwnLeftNode = ( !L._leftNodes.empty() );
2081 bool hasOwnRightNode = ( !L._rightNodes.empty() );
2082 bool isClosedEdge = ( points[ L._firstPntInd ].node == points[ L._lastPntInd ].node );
2084 nbN = L._lastPntInd - L._firstPntInd + 1,
2085 iN0 = ( hasLeftNode || hasOwnLeftNode || isClosedEdge || !isShrinkableL ),
2086 iNE = nbN - ( hasRightNode || hasOwnRightNode || !isShrinkableR );
2088 // update _uvIn of end _LayerEdge's by existing nodes
2089 const SMDS_MeshNode *nL = 0, *nR = 0;
2090 if ( hasOwnLeftNode ) nL = L._leftNodes.back();
2091 else if ( hasLeftNode ) nL = L._leftLine->_rightNodes.back();
2092 if ( hasOwnRightNode ) nR = L._rightNodes.back();
2093 else if ( hasRightNode ) nR = L._rightLine->_leftNodes.back();
2095 L._lEdges[0]._uvIn = _helper.GetNodeUV( _face, nL, points[ L._firstPntInd + 1 ].node );
2097 L._lEdges.back()._uvIn = _helper.GetNodeUV( _face, nR, points[ L._lastPntInd - 1 ].node );
2099 // compute normalized [0;1] node parameters of nodes on a _PolyLine
2100 vector< double > normPar( nbN );
2102 normF = L._wire->FirstParameter( L._edgeInd ),
2103 normL = L._wire->LastParameter ( L._edgeInd ),
2104 normDist = normL - normF;
2105 for ( int i = L._firstPntInd; i <= L._lastPntInd; ++i )
2106 normPar[ i - L._firstPntInd ] = ( points[i].normParam - normF ) / normDist;
2108 // Calculate UV of most inner nodes
2110 vector< gp_XY > innerUV( nbN );
2112 // check if innerUV should be interpolated between _LayerEdge::_uvIn's
2113 const size_t nbLE = L._lEdges.size();
2114 bool needInterpol = ( nbN != nbLE );
2115 if ( !needInterpol )
2117 // more check: compare length of inner and outer end segments
2118 double lenIn, lenOut;
2119 for ( int isR = 0; isR < 2 && !needInterpol; ++isR )
2121 const _Segment& segIn = isR ? L._segments.back() : L._segments[0];
2122 const gp_XY& uvIn1 = segIn.p1();
2123 const gp_XY& uvIn2 = segIn.p2();
2124 const gp_XY& uvOut1 = L._lEdges[ isR ? nbLE-1 : 0 ]._uvOut;
2125 const gp_XY& uvOut2 = L._lEdges[ isR ? nbLE-2 : 1 ]._uvOut;
2126 if ( _is2DIsotropic )
2128 lenIn = ( uvIn1 - uvIn2 ).Modulus();
2129 lenOut = ( uvOut1 - uvOut2 ).Modulus();
2133 lenIn = _surface->Value( uvIn1.X(), uvIn1.Y() )
2134 .Distance( _surface->Value( uvIn2.X(), uvIn2.Y() ));
2135 lenOut = _surface->Value( uvOut1.X(), uvOut1.Y() )
2136 .Distance( _surface->Value( uvOut2.X(), uvOut2.Y() ));
2138 needInterpol = ( lenIn < 0.66 * lenOut );
2144 // compute normalized accumulated length of inner segments
2146 if ( _is2DIsotropic )
2147 for ( iS = 1; iS < segLen.size(); ++iS )
2149 double sLen = ( L._lEdges[iS-1]._uvIn - L._lEdges[iS]._uvIn ).Modulus();
2150 segLen[iS] = segLen[iS-1] + sLen;
2153 for ( iS = 1; iS < segLen.size(); ++iS )
2155 const gp_XY& uv1 = L._lEdges[iS-1]._uvIn;
2156 const gp_XY& uv2 = L._lEdges[iS ]._uvIn;
2157 gp_Pnt p1 = _surface->Value( uv1.X(), uv1.Y() );
2158 gp_Pnt p2 = _surface->Value( uv2.X(), uv2.Y() );
2159 double sLen = p1.Distance( p2 );
2160 segLen[iS] = segLen[iS-1] + sLen;
2162 // normalize the accumulated length
2163 for ( iS = 1; iS < segLen.size(); ++iS )
2164 segLen[iS] /= segLen.back();
2166 // calculate UV of most inner nodes according to the normalized node parameters
2168 for ( size_t i = 0; i < innerUV.size(); ++i )
2170 while ( normPar[i] > segLen[iS+1] )
2172 double r = ( normPar[i] - segLen[iS] ) / ( segLen[iS+1] - segLen[iS] );
2173 innerUV[ i ] = r * L._lEdges[iS+1]._uvIn + (1-r) * L._lEdges[iS]._uvIn;
2176 else // ! needInterpol
2178 for ( size_t i = 0; i < nbLE; ++i )
2179 innerUV[ i ] = L._lEdges[i]._uvIn;
2182 // normalized height of layers
2183 calcLayersHeight( 1., layersHeight );
2185 // Create layers of faces
2187 // nodes to create 1 layer of faces
2188 vector< const SMDS_MeshNode* > outerNodes( nbN );
2189 vector< const SMDS_MeshNode* > innerNodes( nbN );
2191 // initialize outerNodes by nodes of the L._wire
2192 for ( int i = L._firstPntInd; i <= L._lastPntInd; ++i )
2193 outerNodes[ i-L._firstPntInd ] = points[i].node;
2195 L._leftNodes .reserve( _hyp->GetNumberLayers() );
2196 L._rightNodes.reserve( _hyp->GetNumberLayers() );
2197 int cur = 0, prev = -1; // to take into account orientation of _face
2198 if ( isReverse ) std::swap( cur, prev );
2199 for ( int iF = 0; iF < _hyp->GetNumberLayers(); ++iF ) // loop on layers of faces
2201 // create innerNodes of a current layer
2202 for ( size_t i = iN0; i < iNE; ++i )
2204 gp_XY uvOut = points[ L._firstPntInd + i ].UV();
2205 gp_XY& uvIn = innerUV[ i ];
2206 gp_XY uv = layersHeight[ iF ] * uvIn + ( 1.-layersHeight[ iF ]) * uvOut;
2207 gp_Pnt p = _surface->Value( uv.X(), uv.Y() );
2208 innerNodes[i] = _helper.AddNode( p.X(), p.Y(), p.Z(), /*id=*/0, uv.X(), uv.Y() );
2210 // use nodes created for adjacent _PolyLine's
2211 if ( hasOwnLeftNode ) innerNodes.front() = L._leftNodes [ iF ];
2212 else if ( hasLeftNode ) innerNodes.front() = L._leftLine->_rightNodes[ iF ];
2213 if ( hasOwnRightNode ) innerNodes.back() = L._rightNodes[ iF ];
2214 else if ( hasRightNode ) innerNodes.back() = L._rightLine->_leftNodes[ iF ];
2215 if ( isClosedEdge ) innerNodes.front() = innerNodes.back(); // circle
2216 if ( !isShrinkableL ) innerNodes.front() = outerNodes.front();
2217 if ( !isShrinkableR ) innerNodes.back() = outerNodes.back();
2218 if ( !hasOwnLeftNode ) L._leftNodes.push_back( innerNodes.front() );
2219 if ( !hasOwnRightNode ) L._rightNodes.push_back( innerNodes.back() );
2222 for ( size_t i = 1; i < innerNodes.size(); ++i )
2223 if ( SMDS_MeshElement* f = _helper.AddFace( outerNodes[ i+prev ], outerNodes[ i+cur ],
2224 innerNodes[ i+cur ], innerNodes[ i+prev ]))
2225 L._newFaces.insert( L._newFaces.end(), f );
2227 outerNodes.swap( innerNodes );
2230 // faces between not shared _LayerEdge's (at concave VERTEX)
2231 for ( int isR = 0; isR < 2; ++isR )
2233 if ( isR ? rightEdgeShared : leftEdgeShared )
2235 vector< const SMDS_MeshNode* > &
2236 lNodes = (isR ? L._rightNodes : L._leftLine->_rightNodes ),
2237 rNodes = (isR ? L._rightLine->_leftNodes : L._leftNodes );
2238 if ( lNodes.empty() || rNodes.empty() || lNodes.size() != rNodes.size() )
2241 for ( size_t i = 1; i < lNodes.size(); ++i )
2242 _helper.AddFace( lNodes[ i+prev ], rNodes[ i+prev ],
2243 rNodes[ i+cur ], lNodes[ i+cur ]);
2245 const UVPtStruct& ptOnVertex = points[ isR ? L._lastPntInd : L._firstPntInd ];
2247 _helper.AddFace( ptOnVertex.node, lNodes[ 0 ], rNodes[ 0 ]);
2249 _helper.AddFace( ptOnVertex.node, rNodes[ 0 ], lNodes[ 0 ]);
2252 // Fill the _ProxyMeshOfFace
2254 UVPtStructVec nodeDataVec( outerNodes.size() ); // outerNodes swapped with innerNodes
2255 for ( size_t i = 0; i < outerNodes.size(); ++i )
2257 gp_XY uv = _helper.GetNodeUV( _face, outerNodes[i] );
2258 nodeDataVec[i].u = uv.X();
2259 nodeDataVec[i].v = uv.Y();
2260 nodeDataVec[i].node = outerNodes[i];
2261 nodeDataVec[i].param = points [i + L._firstPntInd].param;
2262 nodeDataVec[i].normParam = normPar[i];
2263 nodeDataVec[i].x = normPar[i];
2264 nodeDataVec[i].y = normPar[i];
2266 nodeDataVec.front().param = L._wire->FirstU( L._edgeInd );
2267 nodeDataVec.back() .param = L._wire->LastU ( L._edgeInd );
2269 _ProxyMeshOfFace::_EdgeSubMesh* edgeSM
2270 = getProxyMesh()->GetEdgeSubMesh( L._wire->EdgeID( L._edgeInd ));
2271 edgeSM->SetUVPtStructVec( nodeDataVec );
2273 } // loop on _PolyLine's
2275 // re-compute FACEs whose mesh was removed by shrink()
2276 for ( size_t i = 0; i < _clearedFaces.size(); ++i )
2278 SMESH_subMesh* sm = _mesh->GetSubMesh( _clearedFaces[i] );
2279 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE )
2280 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
2286 //================================================================================
2288 * \brief Improve quality of the created mesh elements
2290 //================================================================================
2292 bool _ViscousBuilder2D::improve()
2297 // fixed nodes on EDGE's
2298 std::set<const SMDS_MeshNode*> fixedNodes;
2299 for ( size_t iWire = 0; iWire < _faceSideVec.size(); ++iWire )
2301 StdMeshers_FaceSidePtr wire = _faceSideVec[ iWire ];
2302 const vector<UVPtStruct>& points = wire->GetUVPtStruct();
2303 for ( size_t i = 0; i < points.size(); ++i )
2304 fixedNodes.insert( fixedNodes.end(), points[i].node );
2306 // fixed proxy nodes
2307 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
2309 _PolyLine& L = _polyLineVec[ iL ];
2310 const TopoDS_Edge& E = L._wire->Edge( L._edgeInd );
2311 if ( const SMESH_ProxyMesh::SubMesh* sm = _proxyMesh->GetProxySubMesh( E ))
2313 const UVPtStructVec& points = sm->GetUVPtStructVec();
2314 for ( size_t i = 0; i < points.size(); ++i )
2315 fixedNodes.insert( fixedNodes.end(), points[i].node );
2317 for ( size_t i = 0; i < L._rightNodes.size(); ++i )
2318 fixedNodes.insert( fixedNodes.end(), L._rightNodes[i] );
2322 SMESH_MeshEditor editor( _mesh );
2323 for ( size_t iL = 0; iL < _polyLineVec.size(); ++iL )
2325 _PolyLine& L = _polyLineVec[ iL ];
2326 if ( L._isStraight2D ) continue;
2327 // SMESH_MeshEditor::SmoothMethod how =
2328 // L._isStraight2D ? SMESH_MeshEditor::LAPLACIAN : SMESH_MeshEditor::CENTROIDAL;
2329 //editor.Smooth( L._newFaces, fixedNodes, how, /*nbIt = */3 );
2330 //editor.Smooth( L._newFaces, fixedNodes, SMESH_MeshEditor::LAPLACIAN, /*nbIt = */1 );
2331 editor.Smooth( L._newFaces, fixedNodes, SMESH_MeshEditor::CENTROIDAL, /*nbIt = */3 );
2336 //================================================================================
2338 * \brief Remove elements and nodes from a face
2340 //================================================================================
2342 bool _ViscousBuilder2D::removeMeshFaces(const TopoDS_Shape& face)
2344 // we don't use SMESH_subMesh::ComputeStateEngine() because of a listener
2345 // which clears EDGEs together with _face.
2346 bool thereWereElems = false;
2347 SMESH_subMesh* sm = _mesh->GetSubMesh( face );
2348 if ( SMESHDS_SubMesh* smDS = sm->GetSubMeshDS() )
2350 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
2351 thereWereElems = eIt->more();
2352 while ( eIt->more() ) getMeshDS()->RemoveFreeElement( eIt->next(), smDS );
2353 SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
2354 while ( nIt->more() ) getMeshDS()->RemoveFreeNode( nIt->next(), smDS );
2356 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
2358 return thereWereElems;
2361 //================================================================================
2363 * \brief Creates a _ProxyMeshOfFace and store it in a sub-mesh of FACE
2365 //================================================================================
2367 _ProxyMeshOfFace* _ViscousBuilder2D::getProxyMesh()
2369 if ( _proxyMesh.get() )
2370 return (_ProxyMeshOfFace*) _proxyMesh.get();
2372 _ProxyMeshOfFace* proxyMeshOfFace = new _ProxyMeshOfFace( *_mesh );
2373 _proxyMesh.reset( proxyMeshOfFace );
2374 new _ProxyMeshHolder( _face, _proxyMesh );
2376 return proxyMeshOfFace;
2379 //================================================================================
2381 * \brief Calculate height of layers for the given thickness. Height is measured
2382 * from the outer boundary
2384 //================================================================================
2386 void _ViscousBuilder2D::calcLayersHeight(const double totalThick,
2387 vector<double>& heights)
2389 heights.resize( _hyp->GetNumberLayers() );
2391 if ( _fPowN - 1 <= numeric_limits<double>::min() )
2392 h0 = totalThick / _hyp->GetNumberLayers();
2394 h0 = totalThick * ( _hyp->GetStretchFactor() - 1 )/( _fPowN - 1 );
2396 double hSum = 0, hi = h0;
2397 for ( int i = 0; i < _hyp->GetNumberLayers(); ++i )
2400 heights[ i ] = hSum;
2401 hi *= _hyp->GetStretchFactor();
2405 //================================================================================
2407 * \brief Elongate this _LayerEdge
2409 //================================================================================
2411 bool _LayerEdge::SetNewLength( const double length3D )
2413 if ( _isBlocked ) return false;
2415 //_uvInPrev = _uvIn;
2416 _length2D = length3D * _len2dTo3dRatio;
2417 _uvIn = _uvOut + _normal2D * _length2D;
2421 //================================================================================
2423 * \brief Return true if _LayerEdge at a common VERTEX between EDGEs with
2424 * and w/o layer is common to the both _PolyLine's. If this is true, nodes
2425 * of this _LayerEdge are inflated along a _PolyLine w/o layer, else the nodes
2426 * are inflated along _normal2D of _LayerEdge of EDGE with layer
2428 //================================================================================
2430 bool _PolyLine::IsCommonEdgeShared( const _PolyLine& other )
2432 const double tol = 1e-30;
2434 if ( & other == _leftLine )
2435 return _lEdges[0]._normal2D.IsEqual( _leftLine->_lEdges.back()._normal2D, tol );
2437 if ( & other == _rightLine )
2438 return _lEdges.back()._normal2D.IsEqual( _rightLine->_lEdges[0]._normal2D, tol );
2443 //================================================================================
2445 * \brief Return \c true if the EDGE of this _PolyLine is concave
2447 //================================================================================
2449 bool _PolyLine::IsConcave() const
2451 if ( _lEdges.size() < 2 )
2454 gp_Vec2d v1( _lEdges[0]._uvOut, _lEdges[1]._uvOut );
2455 gp_Vec2d v2( _lEdges[0]._uvOut, _lEdges[2]._uvOut );
2456 const double size2 = v2.Magnitude();
2458 return ( v1 ^ v2 ) / size2 < -1e-3 * size2;
2461 //================================================================================
2463 * \brief Constructor of SegmentTree
2465 //================================================================================
2467 _SegmentTree::_SegmentTree( const vector< _Segment >& segments ):
2470 _segments.resize( segments.size() );
2471 for ( size_t i = 0; i < segments.size(); ++i )
2472 _segments[i].Set( segments[i] );
2477 //================================================================================
2479 * \brief Return the maximal bnd box
2481 //================================================================================
2483 _SegmentTree::box_type* _SegmentTree::buildRootBox()
2485 _SegmentTree::box_type* box = new _SegmentTree::box_type;
2486 for ( size_t i = 0; i < _segments.size(); ++i )
2488 box->Add( *_segments[i]._seg->_uv[0] );
2489 box->Add( *_segments[i]._seg->_uv[1] );
2494 //================================================================================
2496 * \brief Redistrubute _segments among children
2498 //================================================================================
2500 void _SegmentTree::buildChildrenData()
2502 for ( int i = 0; i < _segments.size(); ++i )
2503 for (int j = 0; j < nbChildren(); j++)
2504 if ( !myChildren[j]->getBox()->IsOut( *_segments[i]._seg->_uv[0],
2505 *_segments[i]._seg->_uv[1] ))
2506 ((_SegmentTree*)myChildren[j])->_segments.push_back( _segments[i]);
2508 SMESHUtils::FreeVector( _segments ); // = _elements.clear() + free memory
2510 for (int j = 0; j < nbChildren(); j++)
2512 _SegmentTree* child = static_cast<_SegmentTree*>( myChildren[j]);
2513 child->myIsLeaf = ( child->_segments.size() <= maxNbSegInLeaf() );
2517 //================================================================================
2519 * \brief Return elements which can include the point
2521 //================================================================================
2523 void _SegmentTree::GetSegmentsNear( const _Segment& seg,
2524 vector< const _Segment* >& found )
2526 if ( getBox()->IsOut( *seg._uv[0], *seg._uv[1] ))
2531 for ( int i = 0; i < _segments.size(); ++i )
2532 if ( !_segments[i].IsOut( seg ))
2533 found.push_back( _segments[i]._seg );
2537 for (int i = 0; i < nbChildren(); i++)
2538 ((_SegmentTree*) myChildren[i])->GetSegmentsNear( seg, found );
2543 //================================================================================
2545 * \brief Return segments intersecting a ray
2547 //================================================================================
2549 void _SegmentTree::GetSegmentsNear( const gp_Ax2d& ray,
2550 vector< const _Segment* >& found )
2552 if ( getBox()->IsOut( ray ))
2557 for ( int i = 0; i < _segments.size(); ++i )
2558 if ( !_segments[i].IsOut( ray ))
2559 found.push_back( _segments[i]._seg );
2563 for (int i = 0; i < nbChildren(); i++)
2564 ((_SegmentTree*) myChildren[i])->GetSegmentsNear( ray, found );
2568 //================================================================================
2570 * \brief Classify a _Segment
2572 //================================================================================
2574 bool _SegmentTree::_SegBox::IsOut( const _Segment& seg ) const
2576 const double eps = std::numeric_limits<double>::min();
2577 for ( int iC = 0; iC < 2; ++iC )
2579 if ( seg._uv[0]->Coord(iC+1) < _seg->_uv[ _iMin[iC]]->Coord(iC+1)+eps &&
2580 seg._uv[1]->Coord(iC+1) < _seg->_uv[ _iMin[iC]]->Coord(iC+1)+eps )
2582 if ( seg._uv[0]->Coord(iC+1) > _seg->_uv[ 1-_iMin[iC]]->Coord(iC+1)-eps &&
2583 seg._uv[1]->Coord(iC+1) > _seg->_uv[ 1-_iMin[iC]]->Coord(iC+1)-eps )
2589 //================================================================================
2591 * \brief Classify a ray
2593 //================================================================================
2595 bool _SegmentTree::_SegBox::IsOut( const gp_Ax2d& ray ) const
2597 double distBoxCenter2Ray =
2598 ray.Direction().XY() ^ ( ray.Location().XY() - 0.5 * (*_seg->_uv[0] + *_seg->_uv[1]));
2600 double boxSectionDiam =
2601 Abs( ray.Direction().X() ) * ( _seg->_uv[1-_iMin[1]]->Y() - _seg->_uv[_iMin[1]]->Y() ) +
2602 Abs( ray.Direction().Y() ) * ( _seg->_uv[1-_iMin[0]]->X() - _seg->_uv[_iMin[0]]->X() );
2604 return Abs( distBoxCenter2Ray ) > 0.5 * boxSectionDiam;