1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementation of SMESH idl descriptions
24 // File : StdMeshers_CompositeSegment_1D.cxx
27 #include "StdMeshers_CompositeSegment_1D.hxx"
29 #include "SMDS_MeshElement.hxx"
30 #include "SMDS_MeshNode.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Comment.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_HypoFilter.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_TypeDefs.hxx"
37 #include "SMESH_subMesh.hxx"
38 #include "SMESH_subMeshEventListener.hxx"
39 #include "StdMeshers_AutomaticLength.hxx"
40 #include "StdMeshers_FaceSide.hxx"
42 #include "utilities.h"
44 #include <BRepAdaptor_CompCurve.hxx>
45 #include <BRep_Builder.hxx>
46 #include <GCPnts_AbscissaPoint.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_MapOfShape.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopoDS_Wire.hxx>
57 #include <Standard_ErrorHandler.hxx>
58 #include <Standard_Failure.hxx>
60 typedef SMESH_Comment TComm;
67 void careOfSubMeshes( StdMeshers_FaceSide& side );
69 //================================================================================
71 * \brief Search for an edge conjunct to the given one by the vertex
72 * Return NULL if more than 2 edges share the vertex or edges
73 * continuity is less than C1
75 //================================================================================
77 TopoDS_Edge nextC1Edge(TopoDS_Edge edge,
81 if (edge.Orientation() > TopAbs_REVERSED) // INTERNAL
82 edge.Orientation( TopAbs_FORWARD );
84 TopTools_MapOfShape edgeCounter;
85 edgeCounter.Add( edge );
86 TopoDS_Vertex v = forward ? TopExp::LastVertex(edge,true) : TopExp::FirstVertex(edge,true);
87 TopTools_ListIteratorOfListOfShape ancestIt = aMesh.GetAncestors( v );
88 for ( ; ancestIt.More(); ancestIt.Next() )
90 const TopoDS_Shape & ancestor = ancestIt.Value();
91 if ( ancestor.ShapeType() == TopAbs_EDGE && edgeCounter.Add( ancestor ))
92 eNext = TopoDS::Edge( ancestor );
94 if ( edgeCounter.Extent() < 3 && !eNext.IsNull() ) {
95 if ( SMESH_Algo::IsContinuous( edge, eNext )) {
96 // care of orientation
97 if (eNext.Orientation() > TopAbs_REVERSED) // INTERNAL
98 eNext.Orientation( TopAbs_FORWARD );
100 forward ? TopExp::FirstVertex(eNext,true) : TopExp::LastVertex(eNext,true);
101 bool reverse = (!v.IsSame(vn));
107 return TopoDS_Edge();
110 //================================================================================
112 * \brief Class used to restore nodes on internal vertices of a complex side
113 * when StdMeshers_CompositeSegment_1D algorithm is removed
115 //================================================================================
117 struct VertexNodesRestoringListener : public SMESH_subMeshEventListener
119 VertexNodesRestoringListener():
120 SMESH_subMeshEventListener(1, // will be deleted by sub-mesh
121 "StdMeshers_CompositeSegment_1D::VertexNodesRestoringListener")
123 static VertexNodesRestoringListener* New() { return new VertexNodesRestoringListener(); }
126 * \brief Restore nodes on internal vertices of a complex side
127 * \param event - algo_event or compute_event itself (of SMESH_subMesh)
128 * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
129 * \param subMesh - the submesh where the event occurs
130 * \param data - listener data stored in the subMesh
131 * \param hyp - hypothesis, if eventType is algo_event
133 void ProcessEvent(const int event,
135 SMESH_subMesh* subMesh,
136 EventListenerData* data,
137 const SMESH_Hypothesis* /*hyp*/)
139 if ( data && eventType == SMESH_subMesh::ALGO_EVENT )
142 if ( subMesh->GetAlgoState() != SMESH_subMesh::HYP_OK )
145 SMESH_Algo* algo = subMesh->GetAlgo();
146 hypRemoved = ( string( algo->GetName() ) != StdMeshers_CompositeSegment_1D::AlgoName());
150 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
151 for ( ; smIt != data->mySubMeshes.end(); ++smIt )
152 if ( SMESH_subMesh* sm = *smIt ) {
153 sm->SetIsAlwaysComputed( false );
154 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
158 // at study restoration:
159 // check if edge submesh must have _alwaysComputed flag
160 else if ( event == SMESH_subMesh::SUBMESH_RESTORED &&
161 eventType == SMESH_subMesh::COMPUTE_EVENT )
163 if ( !subMesh->GetEventListenerData( this )) { // not yet checked
164 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
165 if ( meshDS->NbNodes() > 0 ) {
166 // check if there are nodes on all vertices
167 bool hasNodesOnVerext = true;
168 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
169 while ( hasNodesOnVerext && smIt->more() ) {
170 SMESH_subMesh* sm = smIt->next();
171 hasNodesOnVerext = ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->NbNodes() );
173 if ( !hasNodesOnVerext ) {
174 // check if an edge is a part of a complex side
176 TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
177 SMESHUtils::Deleter< StdMeshers_FaceSide > side
178 ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),
179 edge, face, false ));
180 if ( side->NbEdges() > 1 && side->NbSegments() )
181 careOfSubMeshes( *side );
186 // clean all EDGEs of a complex side if one EDGE is cleaned
187 else if ( event == SMESH_subMesh::CLEAN &&
188 eventType == SMESH_subMesh::COMPUTE_EVENT )
190 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(/*includeSelf=*/false);
191 while ( smIt->more() ) // loop on VERTEX sub-meshes
193 SMESH_subMesh* sm = smIt->next();
194 if ( sm->IsAlwaysComputed() ) // it's an internal node sub-mesh
195 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
199 }; // struct VertexNodesRestoringListener
201 //================================================================================
203 * \brief Update submeshes state for all edges and internal vertices,
204 * make them look computed even if none edge or node is set on them
206 //================================================================================
208 void careOfSubMeshes( StdMeshers_FaceSide& side )
210 if ( side.NbEdges() < 2)
212 for ( int iE = 0; iE < side.NbEdges(); ++iE )
214 // set listener and its data
215 EventListenerData * listenerData = new EventListenerData(true);
216 const TopoDS_Edge& edge = side.Edge( iE );
217 SMESH_subMesh * sm = side.GetMesh()->GetSubMesh( edge );
218 sm->SetEventListener( new VertexNodesRestoringListener(), listenerData, sm );
219 // add edge submesh to the data
220 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
221 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK ) {
222 sm->SetIsAlwaysComputed( true );
223 listenerData->mySubMeshes.push_back( sm );
225 // add internal vertex submesh to the data
228 TopoDS_Vertex V = side.FirstVertex( iE );
229 sm = side.GetMesh()->GetSubMesh( V );
230 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
231 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK )
232 sm->SetIsAlwaysComputed( true );
233 listenerData->mySubMeshes.push_back( sm );
239 //=============================================================================
243 //=============================================================================
245 StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D(int hypId,
247 :StdMeshers_Regular_1D(hypId, gen)
252 //=======================================================================
253 //function : AlgoName
254 //purpose : Returns algo type name
255 //=======================================================================
257 std::string StdMeshers_CompositeSegment_1D::AlgoName()
259 return "CompositeSegment_1D";
262 //=============================================================================
264 * \brief Sets event listener to submeshes if necessary
265 * \param subMesh - submesh where algo is set
267 * This method is called when a submesh gets HYP_OK algo_state.
268 * After being set, event listener is notified on each event of a submesh.
270 //=============================================================================
272 void StdMeshers_CompositeSegment_1D::SetEventListener(SMESH_subMesh* subMesh)
274 // issue 0020279. Set "_alwaysComputed" flag to the submeshes of internal
275 // vertices of composite edge in order to avoid creation of vertices on
276 // them for the sake of stability.
278 // check if "_alwaysComputed" is not yet set
279 bool isAlwaysComputed = false;
280 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
281 while ( !isAlwaysComputed && smIt->more() )
282 isAlwaysComputed = smIt->next()->IsAlwaysComputed();
284 if ( !isAlwaysComputed )
286 // check if an edge is a part of a complex side
288 TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
289 SMESHUtils::Deleter< StdMeshers_FaceSide > side
290 ( StdMeshers_CompositeSegment_1D::GetFaceSide( *subMesh->GetFather(), edge, face, false ));
291 if ( side->NbEdges() > 1 ) { // complex
293 // set _alwaysComputed to vertices
294 for ( int iE = 1; iE < side->NbEdges(); ++iE )
296 TopoDS_Vertex V = side->FirstVertex( iE );
297 SMESH_subMesh* sm = side->GetMesh()->GetSubMesh( V );
298 sm->SetIsAlwaysComputed( true );
302 // set listener that will remove _alwaysComputed from submeshes at algorithm change
303 subMesh->SetEventListener( new VertexNodesRestoringListener(), 0, subMesh );
304 StdMeshers_Regular_1D::SetEventListener( subMesh );
307 //=============================================================================
309 * \brief Return a face side the edge belongs to
311 //=============================================================================
313 StdMeshers_FaceSide *
314 StdMeshers_CompositeSegment_1D::GetFaceSide(SMESH_Mesh& aMesh,
315 const TopoDS_Edge& anEdge,
316 const TopoDS_Face& aFace,
317 const bool ignoreMeshed)
319 list< TopoDS_Edge > edges;
320 if ( anEdge.Orientation() <= TopAbs_REVERSED )
321 edges.push_back( anEdge );
323 edges.push_back( TopoDS::Edge( anEdge.Oriented( TopAbs_FORWARD ))); // PAL21718
325 list <const SMESHDS_Hypothesis *> hypList;
326 SMESH_Algo* theAlgo = aMesh.GetGen()->GetAlgo( aMesh, anEdge );
327 if ( theAlgo ) hypList = theAlgo->GetUsedHypothesis(aMesh, anEdge, false);
328 for ( int forward = 0; forward < 2; ++forward )
330 TopoDS_Edge eNext = nextC1Edge( edges.back(), aMesh, forward );
331 while ( !eNext.IsNull() ) {
332 if ( ignoreMeshed ) {
333 // eNext must not have computed mesh
334 if ( SMESHDS_SubMesh* sm = aMesh.GetMeshDS()->MeshElements(eNext) )
335 if ( sm->NbNodes() || sm->NbElements() )
338 // eNext must have same hypotheses
339 SMESH_Algo* algo = aMesh.GetGen()->GetAlgo( aMesh, eNext );
341 string(theAlgo->GetName()) != algo->GetName() ||
342 hypList != algo->GetUsedHypothesis(aMesh, eNext, false))
344 if ( std::find( edges.begin(), edges.end(), eNext ) != edges.end() )
347 edges.push_back( eNext );
349 edges.push_front( eNext );
350 eNext = nextC1Edge( eNext, aMesh, forward );
353 return new StdMeshers_FaceSide( aFace, edges, &aMesh, true, false );
356 //=============================================================================
360 //=============================================================================
362 bool StdMeshers_CompositeSegment_1D::Compute(SMESH_Mesh & aMesh,
363 const TopoDS_Shape & aShape)
365 TopoDS_Edge edge = TopoDS::Edge( aShape );
366 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
368 // Get edges to be discretized as a whole
369 TopoDS_Face nullFace;
370 SMESHUtils::Deleter< StdMeshers_FaceSide > side( GetFaceSide(aMesh, edge, nullFace, true ));
371 //side->dump("IN COMPOSITE SEG");
373 if ( side->NbEdges() < 2 )
374 return StdMeshers_Regular_1D::Compute( aMesh, aShape );
376 // update segment length computed by StdMeshers_AutomaticLength
377 const list <const SMESHDS_Hypothesis * > & hyps = GetUsedHypothesis(aMesh, aShape);
378 if ( !hyps.empty() ) {
379 StdMeshers_AutomaticLength * autoLenHyp = const_cast<StdMeshers_AutomaticLength *>
380 (dynamic_cast <const StdMeshers_AutomaticLength * >(hyps.front()));
382 _value[ BEG_LENGTH_IND ]= autoLenHyp->GetLength( &aMesh, side->Length() );
385 // Compute node parameters
386 SMESHUtils::Deleter< BRepAdaptor_CompCurve > C3d ( side->GetCurve3d() );
387 double f = C3d->FirstParameter(), l = C3d->LastParameter();
388 list< double > params;
389 if ( !computeInternalParameters ( aMesh, *C3d, side->Length(), f, l, params, false ))
392 // Redistribute parameters near ends
393 TopoDS_Vertex VFirst = side->FirstVertex();
394 TopoDS_Vertex VLast = side->LastVertex();
395 redistributeNearVertices( aMesh, *C3d, side->Length(), params, VFirst, VLast );
397 params.push_front(f);
399 int nbNodes = params.size();
403 // compute and get nodes on extremity VERTEX'es
404 SMESH_subMesh* smVFirst = aMesh.GetSubMesh( VFirst );
405 smVFirst->SetIsAlwaysComputed( false );
406 smVFirst->ComputeStateEngine( SMESH_subMesh::COMPUTE );
408 SMESH_subMesh* smVLast = aMesh.GetSubMesh( VLast );
409 smVLast->SetIsAlwaysComputed( false );
410 smVLast->ComputeStateEngine( SMESH_subMesh::COMPUTE );
412 const SMDS_MeshNode * nFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
413 const SMDS_MeshNode * nLast = SMESH_Algo::VertexNode( VLast, meshDS );
415 return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
416 <<meshDS->ShapeToIndex(VFirst));
418 return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
419 <<meshDS->ShapeToIndex(VLast));
421 vector<const SMDS_MeshNode*> nodes( nbNodes, (const SMDS_MeshNode*)0 );
422 nodes.front() = nFirst;
423 nodes.back() = nLast;
425 // create internal nodes
426 list< double >::iterator parIt = params.begin();
427 double prevPar = *parIt;
429 for ( int iN = 0; parIt != params.end(); ++iN, ++parIt)
431 if ( !nodes[ iN ] ) {
432 gp_Pnt p = C3d->Value( *parIt );
433 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
434 C3d->Edge( *parIt, edge, u );
435 meshDS->SetNodeOnEdge( n, edge, u );
436 // cout << "new NODE: par="<<*parIt<<" ePar="<<u<<" e="<<edge.TShape().operator->()
437 // << " " << n << endl;
442 double mPar = ( prevPar + *parIt )/2;
443 if ( _quadraticMesh ) {
444 // create medium node
445 double segLen = GCPnts_AbscissaPoint::Length(*C3d, prevPar, *parIt);
446 GCPnts_AbscissaPoint ruler( *C3d, segLen/2., prevPar );
447 if ( ruler.IsDone() )
448 mPar = ruler.Parameter();
449 gp_Pnt p = C3d->Value( mPar );
450 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
451 //cout << "new NODE "<< n << endl;
452 meshDS->SetNodeOnEdge( n, edge, u );
453 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ], n);
454 meshDS->SetMeshElementOnShape(seg, edge);
457 C3d->Edge( mPar, edge, u );
458 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ]);
459 meshDS->SetMeshElementOnShape(seg, edge);
465 // remove nodes on internal vertices
466 for ( int iE = 1; iE < side->NbEdges(); ++iE )
468 TopoDS_Vertex V = side->FirstVertex( iE );
469 while ( const SMDS_MeshNode * n = SMESH_Algo::VertexNode( V, meshDS ))
470 meshDS->RemoveNode( n );
473 // Update submeshes state for all edges and internal vertices,
474 // make them look computed even if none edge or node is set on them
475 careOfSubMeshes( *side );