1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 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
24 // File : StdMeshers_Regular_1D.cxx
25 // Moved here from SMESH_Regular_1D.cxx
26 // Author : Paul RASCLE, EDF
30 #include "StdMeshers_CompositeSegment_1D.hxx"
31 #include "StdMeshers_FaceSide.hxx"
32 #include "StdMeshers_AutomaticLength.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_HypoFilter.hxx"
37 #include "SMESH_subMesh.hxx"
38 #include "SMESH_subMeshEventListener.hxx"
40 #include "SMDS_MeshElement.hxx"
41 #include "SMDS_MeshNode.hxx"
43 #include "utilities.h"
45 #include <BRepAdaptor_CompCurve.hxx>
46 #include <BRep_Builder.hxx>
47 #include <GCPnts_AbscissaPoint.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_MapOfShape.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Vertex.hxx>
55 #include <TopoDS_Wire.hxx>
58 #include <Standard_ErrorHandler.hxx>
59 #include <Standard_Failure.hxx>
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(const TopoDS_Edge& edge,
79 TopTools_MapOfShape edgeCounter;
80 edgeCounter.Add( edge );
82 v = forward ? TopExp::LastVertex( edge,1 ) : TopExp::FirstVertex( edge,1 );
83 TopTools_ListIteratorOfListOfShape ancestIt = aMesh.GetAncestors( v );
84 for ( ; ancestIt.More(); ancestIt.Next() )
86 const TopoDS_Shape & ancestor = ancestIt.Value();
87 if ( ancestor.ShapeType() == TopAbs_EDGE && edgeCounter.Add( ancestor ))
88 eNext = TopoDS::Edge( ancestor );
90 if ( edgeCounter.Extent() < 3 && !eNext.IsNull() ) {
91 GeomAbs_Shape cont = SMESH_Algo::Continuity( edge, eNext );
92 if (cont >= GeomAbs_G1) {
93 // care of orientation
96 reverse = ( !v.IsSame( TopExp::FirstVertex( eNext, true )));
98 reverse = ( !v.IsSame( TopExp::LastVertex( eNext, true )));
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 SMESH_Hypothesis* /*hyp*/)
169 bool hypRemoved = ( eventType == SMESH_subMesh::ALGO_EVENT &&
170 subMesh->GetAlgoState() != SMESH_subMesh::HYP_OK );
171 if ( hypRemoved && data )
173 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
174 for ( ; smIt != data->mySubMeshes.end(); ++smIt )
176 if ( SMESH_subMesh* sm = *smIt ) {
177 sm->SetIsAlwaysComputed( false );
178 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
179 sm->GetFather()->GetGen()->Compute( *sm );
181 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
185 // at study restoration:
186 // check if edge submesh must have _alwaysComputed flag
187 else if ( eventType == SMESH_subMesh::COMPUTE_EVENT &&
188 event == SMESH_subMesh::SUBMESH_RESTORED )
190 if ( !subMesh->GetEventListenerData( this )) { // not yet checked
191 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
192 TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
193 TopoDS_Vertex V1, V2;
194 TopExp::Vertices( edge, V1, V2 );
195 bool noVertexNode1 = ( !SMESH_Algo::VertexNode( V1, meshDS ));
196 bool noVertexNode2 = ( !SMESH_Algo::VertexNode( V2, meshDS ));
197 if ( noVertexNode1 || noVertexNode2 ) {
199 auto_ptr< StdMeshers_FaceSide > side
200 ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),
201 edge, face, false ));
202 if ( side->NbSegments() )
203 careOfSubMeshes( *side, this );
208 }; // struct VertexNodesRestoringListener
211 //=============================================================================
215 //=============================================================================
217 StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D(int hypId,
220 :StdMeshers_Regular_1D(hypId, studyId, gen)
222 MESSAGE("StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D");
223 _name = "CompositeSegment_1D";
224 _EventListener = new VertexNodesRestoringListener();
227 //=============================================================================
231 //=============================================================================
233 StdMeshers_CompositeSegment_1D::~StdMeshers_CompositeSegment_1D()
235 delete _EventListener;
238 //=============================================================================
240 * \brief Sets event listener to submeshes if necessary
241 * \param subMesh - submesh where algo is set
243 * This method is called when a submesh gets HYP_OK algo_state.
244 * After being set, event listener is notified on each event of a submesh.
246 //=============================================================================
248 void StdMeshers_CompositeSegment_1D::SetEventListener(SMESH_subMesh* subMesh)
250 subMesh->SetEventListener( _EventListener, 0, subMesh);
251 StdMeshers_Regular_1D::SetEventListener( subMesh );
254 //=============================================================================
256 * \brief Return a face side the edge belongs to
258 //=============================================================================
260 StdMeshers_FaceSide *
261 StdMeshers_CompositeSegment_1D::GetFaceSide(SMESH_Mesh& aMesh,
262 const TopoDS_Edge& anEdge,
263 const TopoDS_Face& aFace,
264 const bool ignoreMeshed)
266 list< TopoDS_Edge > edges;
267 edges.push_back( anEdge );
269 list <const SMESHDS_Hypothesis *> hypList;
270 SMESH_Algo* theAlgo = aMesh.GetGen()->GetAlgo( aMesh, anEdge );
271 if ( theAlgo ) hypList = theAlgo->GetUsedHypothesis(aMesh, anEdge, false);
272 for ( int forward = 0; forward < 2; ++forward )
274 TopoDS_Edge eNext = nextC1Edge( anEdge, aMesh, forward );
275 while ( !eNext.IsNull() ) {
276 if ( ignoreMeshed ) {
277 // eNext must not have computed mesh
278 if ( SMESHDS_SubMesh* sm = aMesh.GetMeshDS()->MeshElements(eNext) )
279 if ( sm->NbNodes() || sm->NbElements() )
282 // eNext must have same hypotheses
283 SMESH_Algo* algo = aMesh.GetGen()->GetAlgo( aMesh, eNext );
285 string(theAlgo->GetName()) != algo->GetName() ||
286 hypList != algo->GetUsedHypothesis(aMesh, eNext, false))
289 edges.push_back( eNext );
291 edges.push_front( eNext );
292 eNext = nextC1Edge( eNext, aMesh, forward );
295 return new StdMeshers_FaceSide( aFace, edges, &aMesh, true, false );
298 //=============================================================================
302 //=============================================================================
304 bool StdMeshers_CompositeSegment_1D::Compute(SMESH_Mesh & aMesh,
305 const TopoDS_Shape & aShape)
307 TopoDS_Edge edge = TopoDS::Edge( aShape );
308 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
310 // Get edges to be discretized as a whole
311 TopoDS_Face nullFace;
312 auto_ptr< StdMeshers_FaceSide > side( GetFaceSide(aMesh, edge, nullFace, true ));
313 //side->dump("IN COMPOSITE SEG");
315 if ( side->NbEdges() < 2 )
316 return StdMeshers_Regular_1D::Compute( aMesh, aShape );
318 // update segment lenght computed by StdMeshers_AutomaticLength
319 const list <const SMESHDS_Hypothesis * > & hyps = GetUsedHypothesis(aMesh, aShape);
320 if ( !hyps.empty() ) {
321 StdMeshers_AutomaticLength * autoLenHyp = const_cast<StdMeshers_AutomaticLength *>
322 (dynamic_cast <const StdMeshers_AutomaticLength * >(hyps.front()));
324 _value[ BEG_LENGTH_IND ]= autoLenHyp->GetLength( &aMesh, side->Length() );
327 // Compute node parameters
328 auto_ptr< BRepAdaptor_CompCurve > C3d ( side->GetCurve3d() );
329 double f = C3d->FirstParameter(), l = C3d->LastParameter();
330 list< double > params;
331 if ( !computeInternalParameters ( *C3d, side->Length(), f, l, params, false ))
334 // Redistribute parameters near ends
335 TopoDS_Vertex VFirst = side->FirstVertex();
336 TopoDS_Vertex VLast = side->LastVertex();
337 redistributeNearVertices( aMesh, *C3d, side->Length(), params, VFirst, VLast );
339 params.push_front(f);
341 int nbNodes = params.size();
345 const SMDS_MeshNode * nFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
347 MESSAGE (" NO NODE BUILT ON VERTEX ");
350 const SMDS_MeshNode * nLast = SMESH_Algo::VertexNode( VLast, meshDS );
352 MESSAGE (" NO NODE BUILT ON VERTEX ");
355 vector<const SMDS_MeshNode*> nodes( nbNodes, (const SMDS_MeshNode*)0 );
356 nodes.front() = nFirst;
357 nodes.back() = nLast;
359 // create internal nodes
360 list< double >::iterator parIt = params.begin();
361 double prevPar = *parIt;
363 for ( int iN = 0; parIt != params.end(); ++iN, ++parIt)
365 if ( !nodes[ iN ] ) {
366 gp_Pnt p = C3d->Value( *parIt );
367 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
368 C3d->Edge( *parIt, edge, u );
369 meshDS->SetNodeOnEdge( n, edge, u );
370 // cout << "new NODE: par="<<*parIt<<" ePar="<<u<<" e="<<edge.TShape().operator->()
371 // << " " << n << endl;
376 double mPar = ( prevPar + *parIt )/2;
377 if ( _quadraticMesh ) {
378 // create medium node
379 double segLen = GCPnts_AbscissaPoint::Length(*C3d, prevPar, *parIt);
380 GCPnts_AbscissaPoint ruler( *C3d, segLen/2., prevPar );
381 if ( ruler.IsDone() )
382 mPar = ruler.Parameter();
383 gp_Pnt p = C3d->Value( mPar );
384 SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
385 //cout << "new NODE "<< n << endl;
386 meshDS->SetNodeOnEdge( n, edge, u );
387 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ], n);
388 meshDS->SetMeshElementOnShape(seg, edge);
391 C3d->Edge( mPar, edge, u );
392 SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ]);
393 meshDS->SetMeshElementOnShape(seg, edge);
399 // remove nodes on internal vertices
400 for ( int iE = 0; iE < side->NbEdges()-1; ++iE )
402 TopoDS_Vertex V = side->LastVertex( iE );
403 while ( const SMDS_MeshNode * n = SMESH_Algo::VertexNode( V, meshDS ))
404 meshDS->RemoveNode( n );
407 // Update submeshes state for all edges and internal vertices,
408 // make them look computed even if none edge or node is set on them
409 careOfSubMeshes( *side, _EventListener );