1 // Copyright (C) 2007-2011 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.
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 : implementaion of SMESH idl descriptions
24 // File : StdMeshers_CompositeSegment_1D.cxx
27 #include "StdMeshers_CompositeSegment_1D.hxx"
28 #include "StdMeshers_FaceSide.hxx"
29 #include "StdMeshers_AutomaticLength.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_subMesh.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36 #include "SMESH_Comment.hxx"
38 #include "SMDS_MeshElement.hxx"
39 #include "SMDS_MeshNode.hxx"
41 #include "utilities.h"
43 #include <BRepAdaptor_CompCurve.hxx>
44 #include <BRep_Builder.hxx>
45 #include <GCPnts_AbscissaPoint.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_MapOfShape.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Vertex.hxx>
53 #include <TopoDS_Wire.hxx>
56 #include <Standard_ErrorHandler.hxx>
57 #include <Standard_Failure.hxx>
59 typedef SMESH_Comment TComm;
66 //================================================================================
68 * \brief Search for an edge conjunct to the given one by the vertex
69 * Return NULL if more than 2 edges share the vertex or edges
70 * continuity is less than C1
72 //================================================================================
74 TopoDS_Edge nextC1Edge(TopoDS_Edge edge,
78 if (edge.Orientation() > TopAbs_REVERSED) // INTERNAL
79 edge.Orientation( TopAbs_FORWARD );
81 TopTools_MapOfShape edgeCounter;
82 edgeCounter.Add( edge );
83 TopoDS_Vertex v = forward ? TopExp::LastVertex(edge,true) : TopExp::FirstVertex(edge,true);
84 TopTools_ListIteratorOfListOfShape ancestIt = aMesh.GetAncestors( v );
85 for ( ; ancestIt.More(); ancestIt.Next() )
87 const TopoDS_Shape & ancestor = ancestIt.Value();
88 if ( ancestor.ShapeType() == TopAbs_EDGE && edgeCounter.Add( ancestor ))
89 eNext = TopoDS::Edge( ancestor );
91 if ( edgeCounter.Extent() < 3 && !eNext.IsNull() ) {
92 if ( SMESH_Algo::IsContinuous( edge, eNext )) {
93 // care of orientation
94 if (eNext.Orientation() > TopAbs_REVERSED) // INTERNAL
95 eNext.Orientation( TopAbs_FORWARD );
97 forward ? TopExp::FirstVertex(eNext,true) : TopExp::LastVertex(eNext,true);
98 bool reverse = (!v.IsSame(vn));
104 return TopoDS_Edge();
107 //================================================================================
109 * \brief Update submeshes state for all edges and internal vertices,
110 * make them look computed even if none edge or node is set on them
112 //================================================================================
114 void careOfSubMeshes( StdMeshers_FaceSide& side, EventListener* eListener)
116 if ( side.NbEdges() < 2)
118 for ( int iE = 0; iE < side.NbEdges(); ++iE )
120 // set listener and its data
121 EventListenerData * listenerData = new EventListenerData(true);
122 const TopoDS_Edge& edge = side.Edge( iE );
123 SMESH_subMesh * sm = side.GetMesh()->GetSubMesh( edge );
124 sm->SetEventListener( eListener, listenerData, sm );
125 // add edge submesh to the data
126 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
127 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK ) {
128 sm->SetIsAlwaysComputed( true );
129 listenerData->mySubMeshes.push_back( sm );
131 // add internal vertex submesh to the data
134 TopoDS_Vertex V = side.FirstVertex( iE );
135 sm = side.GetMesh()->GetSubMesh( V );
136 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
137 if ( sm->GetComputeState() != SMESH_subMesh::COMPUTE_OK )
138 sm->SetIsAlwaysComputed( true );
139 listenerData->mySubMeshes.push_back( sm );
144 //================================================================================
146 * \brief Class used to restore nodes on internal vertices of a complex side
147 * when StdMeshers_CompositeSegment_1D algorithm is removed
149 //================================================================================
151 struct VertexNodesRestoringListener : public SMESH_subMeshEventListener
153 VertexNodesRestoringListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
156 * \brief Restore nodes on internal vertices of a complex side
157 * \param event - algo_event or compute_event itself (of SMESH_subMesh)
158 * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
159 * \param subMesh - the submesh where the event occures
160 * \param data - listener data stored in the subMesh
161 * \param hyp - hypothesis, if eventType is algo_event
163 void ProcessEvent(const int event,
165 SMESH_subMesh* subMesh,
166 EventListenerData* data,
167 const SMESH_Hypothesis* /*hyp*/)
169 if ( data && eventType == SMESH_subMesh::ALGO_EVENT )
172 if ( subMesh->GetAlgoState() != SMESH_subMesh::HYP_OK )
175 SMESH_Gen * gen = subMesh->GetFather()->GetGen();
176 SMESH_Algo* algo = gen->GetAlgo( *subMesh->GetFather(), subMesh->GetSubShape() );
177 hypRemoved = ( string( algo->GetName() ) != StdMeshers_CompositeSegment_1D::AlgoName());
181 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
182 for ( ; smIt != data->mySubMeshes.end(); ++smIt )
183 if ( SMESH_subMesh* sm = *smIt ) {
184 sm->SetIsAlwaysComputed( false );
185 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
189 // at study restoration:
190 // check if edge submesh must have _alwaysComputed flag
191 else if ( event == SMESH_subMesh::SUBMESH_RESTORED &&
192 eventType == SMESH_subMesh::COMPUTE_EVENT )
194 if ( !subMesh->GetEventListenerData( this )) { // not yet checked
195 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
196 if ( meshDS->NbNodes() > 0 ) {
197 // check if there are nodes on all vertices
198 bool hasNodesOnVerext = true;
199 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
200 while ( hasNodesOnVerext && smIt->more() ) {
201 SMESH_subMesh* sm = smIt->next();
202 hasNodesOnVerext = ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->NbNodes() );
204 if ( !hasNodesOnVerext ) {
205 // check if an edge is a part of a complex side
207 TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
208 auto_ptr< StdMeshers_FaceSide > side
209 ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),
210 edge, face, false ));
211 if ( side->NbEdges() > 1 && side->NbSegments() )
212 careOfSubMeshes( *side, this );
218 }; // struct VertexNodesRestoringListener
221 //=============================================================================
225 //=============================================================================
227 StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D(int hypId,
230 :StdMeshers_Regular_1D(hypId, studyId, gen)
232 MESSAGE("StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D");
234 _EventListener = new VertexNodesRestoringListener();
237 //=======================================================================
238 //function : AlgoName
239 //purpose : Returns algo type name
240 //=======================================================================
242 std::string StdMeshers_CompositeSegment_1D::AlgoName()
244 return "CompositeSegment_1D";
246 //=============================================================================
250 //=============================================================================
252 StdMeshers_CompositeSegment_1D::~StdMeshers_CompositeSegment_1D()
254 delete _EventListener;
257 //=============================================================================
259 * \brief Sets event listener to submeshes if necessary
260 * \param subMesh - submesh where algo is set
262 * This method is called when a submesh gets HYP_OK algo_state.
263 * After being set, event listener is notified on each event of a submesh.
265 //=============================================================================
267 void StdMeshers_CompositeSegment_1D::SetEventListener(SMESH_subMesh* subMesh)
269 // issue 0020279. Set "_alwaysComputed" flag to the submeshes of internal
270 // vertices of composite edge in order to avoid creation of vertices on
271 // them for the sake of stability.
273 // check if "_alwaysComputed" is not yet set
274 bool isAlwaysComputed = false;
275 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
276 while ( !isAlwaysComputed && smIt->more() )
277 isAlwaysComputed = smIt->next()->IsAlwaysComputed();
279 if ( !isAlwaysComputed )
281 // check if an edge is a part of a complex side
283 TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
284 auto_ptr< StdMeshers_FaceSide > side
285 ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),edge, face, false ));
286 if ( side->NbEdges() > 1 ) { // complex
288 // set _alwaysComputed to vertices
289 for ( int iE = 1; iE < side->NbEdges(); ++iE )
291 TopoDS_Vertex V = side->FirstVertex( iE );
292 SMESH_subMesh* sm = side->GetMesh()->GetSubMesh( V );
293 sm->SetIsAlwaysComputed( true );
297 // set listener that will remove _alwaysComputed from submeshes at algorithm change
298 subMesh->SetEventListener( _EventListener, 0, subMesh);
299 StdMeshers_Regular_1D::SetEventListener( subMesh );
302 //=============================================================================
304 * \brief Return a face side the edge belongs to
306 //=============================================================================
308 StdMeshers_FaceSide *
309 StdMeshers_CompositeSegment_1D::GetFaceSide(SMESH_Mesh& aMesh,
310 const TopoDS_Edge& anEdge,
311 const TopoDS_Face& aFace,
312 const bool ignoreMeshed)
314 list< TopoDS_Edge > edges;
315 if ( anEdge.Orientation() <= TopAbs_REVERSED )
316 edges.push_back( anEdge );
318 edges.push_back( TopoDS::Edge( anEdge.Oriented( TopAbs_FORWARD ))); // PAL21718
320 list <const SMESHDS_Hypothesis *> hypList;
321 SMESH_Algo* theAlgo = aMesh.GetGen()->GetAlgo( aMesh, anEdge );
322 if ( theAlgo ) hypList = theAlgo->GetUsedHypothesis(aMesh, anEdge, false);
323 for ( int forward = 0; forward < 2; ++forward )
325 TopoDS_Edge eNext = nextC1Edge( edges.back(), aMesh, forward );
326 while ( !eNext.IsNull() ) {
327 if ( ignoreMeshed ) {
328 // eNext must not have computed mesh
329 if ( SMESHDS_SubMesh* sm = aMesh.GetMeshDS()->MeshElements(eNext) )
330 if ( sm->NbNodes() || sm->NbElements() )
333 // eNext must have same hypotheses
334 SMESH_Algo* algo = aMesh.GetGen()->GetAlgo( aMesh, eNext );
336 string(theAlgo->GetName()) != algo->GetName() ||
337 hypList != algo->GetUsedHypothesis(aMesh, eNext, false))
339 if ( std::find( edges.begin(), edges.end(), eNext ) != edges.end() )
342 edges.push_back( eNext );
344 edges.push_front( eNext );
345 eNext = nextC1Edge( eNext, aMesh, forward );
348 return new StdMeshers_FaceSide( aFace, edges, &aMesh, true, false );
351 //=============================================================================
355 //=============================================================================
357 bool StdMeshers_CompositeSegment_1D::Compute(SMESH_Mesh & aMesh,
358 const TopoDS_Shape & aShape)
360 TopoDS_Edge edge = TopoDS::Edge( aShape );
361 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
363 // Get edges to be discretized as a whole
364 TopoDS_Face nullFace;
365 auto_ptr< StdMeshers_FaceSide > side( GetFaceSide(aMesh, edge, nullFace, true ));
366 //side->dump("IN COMPOSITE SEG");
368 if ( side->NbEdges() < 2 )
369 return StdMeshers_Regular_1D::Compute( aMesh, aShape );
371 // update segment lenght computed by StdMeshers_AutomaticLength
372 const list <const SMESHDS_Hypothesis * > & hyps = GetUsedHypothesis(aMesh, aShape);
373 if ( !hyps.empty() ) {
374 StdMeshers_AutomaticLength * autoLenHyp = const_cast<StdMeshers_AutomaticLength *>
375 (dynamic_cast <const StdMeshers_AutomaticLength * >(hyps.front()));
377 _value[ BEG_LENGTH_IND ]= autoLenHyp->GetLength( &aMesh, side->Length() );
380 // Compute node parameters
381 auto_ptr< BRepAdaptor_CompCurve > C3d ( side->GetCurve3d() );
382 double f = C3d->FirstParameter(), l = C3d->LastParameter();
383 list< double > params;
384 if ( !computeInternalParameters ( aMesh, *C3d, side->Length(), f, l, params, false ))
387 // Redistribute parameters near ends
388 TopoDS_Vertex VFirst = side->FirstVertex();
389 TopoDS_Vertex VLast = side->LastVertex();
390 redistributeNearVertices( aMesh, *C3d, side->Length(), params, VFirst, VLast );
392 params.push_front(f);
394 int nbNodes = params.size();
398 const SMDS_MeshNode * nFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
399 const SMDS_MeshNode * nLast = SMESH_Algo::VertexNode( VLast, meshDS );
401 return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
402 <<meshDS->ShapeToIndex(VFirst));
404 return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
405 <<meshDS->ShapeToIndex(VLast));
407 vector<const SMDS_MeshNode*> nodes( nbNodes, (const SMDS_MeshNode*)0 );
408 nodes.front() = nFirst;
409 nodes.back() = nLast;
411 // create internal nodes
412 list< double >::iterator parIt = params.begin();
413 double prevPar = *parIt;
415 for ( int iN = 0; parIt != params.end(); ++iN, ++parIt)
417 if ( !nodes[ iN ] ) {
418 gp_Pnt p = C3d->Value( *parIt );
419 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
420 C3d->Edge( *parIt, edge, u );
421 meshDS->SetNodeOnEdge( n, edge, u );
422 // cout << "new NODE: par="<<*parIt<<" ePar="<<u<<" e="<<edge.TShape().operator->()
423 // << " " << n << endl;
428 double mPar = ( prevPar + *parIt )/2;
429 if ( _quadraticMesh ) {
430 // create medium node
431 double segLen = GCPnts_AbscissaPoint::Length(*C3d, prevPar, *parIt);
432 GCPnts_AbscissaPoint ruler( *C3d, segLen/2., prevPar );
433 if ( ruler.IsDone() )
434 mPar = ruler.Parameter();
435 gp_Pnt p = C3d->Value( mPar );
436 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
437 //cout << "new NODE "<< n << endl;
438 meshDS->SetNodeOnEdge( n, edge, u );
439 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ], n);
440 meshDS->SetMeshElementOnShape(seg, edge);
443 C3d->Edge( mPar, edge, u );
444 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ]);
445 meshDS->SetMeshElementOnShape(seg, edge);
451 // remove nodes on internal vertices
452 for ( int iE = 1; iE < side->NbEdges(); ++iE )
454 TopoDS_Vertex V = side->FirstVertex( iE );
455 while ( const SMDS_MeshNode * n = SMESH_Algo::VertexNode( V, meshDS ))
456 meshDS->RemoveNode( n );
459 // Update submeshes state for all edges and internal vertices,
460 // make them look computed even if none edge or node is set on them
461 careOfSubMeshes( *side, _EventListener );