1 // Copyright (C) 2007-2014 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 : implementaion of SMESH idl descriptions
24 // File : StdMeshers_Propagation.cxx
27 #include "StdMeshers_Propagation.hxx"
29 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_HypoFilter.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_subMesh.hxx"
38 #include <BRepTools_WireExplorer.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <TopTools_MapOfShape.hxx>
44 // cout << txt << endl;
50 // =======================================================================
52 * \brief Listener managing propagation of 1D hypotheses
54 // =======================================================================
56 class PropagationMgr: public SMESH_subMeshEventListener
59 static PropagationMgr* GetListener();
61 * \brief Set listener on edge submesh
63 static void Set(SMESH_subMesh * submesh);
65 * \brief Return an edge from which hypotheses are propagated from
67 static TopoDS_Edge GetSource(SMESH_subMesh * submesh,
68 bool& isPropagOfDistribution);
70 * \brief Does it's main job
72 void ProcessEvent(const int event,
74 SMESH_subMesh* subMesh,
75 SMESH_subMeshEventListenerData* data,
76 const SMESH_Hypothesis* hyp = 0);
82 //=============================================================================
84 * StdMeshers_Propagation Implementation
86 //=============================================================================
88 StdMeshers_Propagation::StdMeshers_Propagation (int hypId, int studyId, SMESH_Gen * gen)
89 : SMESH_Hypothesis(hypId, studyId, gen)
92 _param_algo_dim = -1; // 1D auxiliary
94 StdMeshers_PropagOfDistribution::StdMeshers_PropagOfDistribution (int hypId,
97 : StdMeshers_Propagation(hypId, studyId, gen) { _name = GetName(); }
98 StdMeshers_Propagation::~StdMeshers_Propagation() {}
99 string StdMeshers_Propagation::GetName () { return "Propagation"; }
100 string StdMeshers_PropagOfDistribution::GetName () { return "PropagOfDistribution"; }
101 ostream & StdMeshers_Propagation::SaveTo (ostream & save) { return save; }
102 istream & StdMeshers_Propagation::LoadFrom (istream & load) { return load; }
103 bool StdMeshers_Propagation::SetParametersByMesh(const SMESH_Mesh*,
104 const TopoDS_Shape& ) { return false; }
105 bool StdMeshers_Propagation::SetParametersByDefaults(const TDefaults&,const SMESH_Mesh*) { return false; }
106 void StdMeshers_Propagation::SetPropagationMgr(SMESH_subMesh* subMesh) { PropagationMgr::Set( subMesh ); }
108 * \brief Return an edge from which hypotheses are propagated
110 TopoDS_Edge StdMeshers_Propagation::GetPropagationSource(SMESH_Mesh& theMesh,
111 const TopoDS_Shape& theEdge,
112 bool& isPropagOfDistribution)
114 return PropagationMgr::GetSource( theMesh.GetSubMeshContaining( theEdge ),
115 isPropagOfDistribution);
118 //=============================================================================
119 //=============================================================================
120 // PROPAGATION MANAGEMENT
121 //=============================================================================
122 //=============================================================================
126 enum SubMeshState { WAIT_PROPAG_HYP, // propagation hyp or local 1D hyp is missing
127 HAS_PROPAG_HYP, // propag hyp on this submesh
128 IN_CHAIN, // submesh is in propagation chain
129 LAST_IN_CHAIN, // submesh with local 1D hyp breaking a chain
130 MEANINGLESS_LAST }; // meaningless
132 struct PropagationMgrData : public EventListenerData
134 bool myForward; //!< true if a curve of edge in chain is codirected with one of source edge
135 bool myIsPropagOfDistribution; //!< type of Propagation hyp
136 PropagationMgrData( SubMeshState state=WAIT_PROPAG_HYP ): EventListenerData(true) {
137 myType = state; myForward = true; myIsPropagOfDistribution = false;
140 myType = WAIT_PROPAG_HYP; mySubMeshes.clear(); myForward = true;
141 myIsPropagOfDistribution = false;
143 SubMeshState State() const {
144 return (SubMeshState) myType;
146 void SetState(SubMeshState state) {
149 void SetSource(SMESH_subMesh* sm ) {
150 mySubMeshes.clear(); if ( sm ) mySubMeshes.push_back( sm );
152 void AddSource(SMESH_subMesh* sm ) {
153 if ( sm ) mySubMeshes.push_back( sm );
155 void SetChain(list< SMESH_subMesh* >& chain ) {
156 mySubMeshes.clear(); mySubMeshes.splice( mySubMeshes.end(), chain );
158 SMESH_subMeshIteratorPtr GetChain() const;
159 SMESH_subMesh* GetSource() const;
162 //=============================================================================
164 * \brief return static PropagationMgr
166 PropagationMgr* PropagationMgr::GetListener()
168 static PropagationMgr theListener;
171 PropagationMgr* getListener()
173 return PropagationMgr::GetListener();
175 //=============================================================================
177 * \brief return PropagationMgrData found on a submesh
179 PropagationMgrData* findData(SMESH_subMesh* sm)
182 return static_cast< PropagationMgrData* >( sm->GetEventListenerData( getListener() ));
185 //=============================================================================
187 * \brief return PropagationMgrData found on theEdge submesh
189 PropagationMgrData* findData(SMESH_Mesh& theMesh, const TopoDS_Shape& theEdge)
191 if ( theEdge.ShapeType() == TopAbs_EDGE )
192 return findData( theMesh.GetSubMeshContaining( theEdge ) );
195 //=============================================================================
197 * \brief return existing or a new PropagationMgrData
199 PropagationMgrData* getData(SMESH_subMesh* sm)
201 PropagationMgrData* data = findData( sm );
203 data = new PropagationMgrData();
204 sm->SetEventListener( getListener(), data, sm );
208 //=============================================================================
210 * \brief Returns a local 1D hypothesis used for theEdge
212 const SMESH_Hypothesis* getLocal1DHyp (SMESH_Mesh& theMesh,
213 const TopoDS_Shape& theEdge,
214 TopoDS_Shape* theSssignedTo=0)
216 static SMESH_HypoFilter hypo;
217 hypo.Init( hypo.HasDim( 1 )).
218 AndNot ( hypo.IsAlgo() ).
219 AndNot ( hypo.IsAssignedTo( theMesh.GetShapeToMesh() ));
221 return theMesh.GetHypothesis( theEdge, hypo, true, theSssignedTo );
223 //=============================================================================
225 * \brief Returns a propagation hypothesis assigned to theEdge
227 const SMESH_Hypothesis* getProagationHyp (SMESH_Mesh& theMesh,
228 const TopoDS_Shape& theEdge)
230 static SMESH_HypoFilter propagHypFilter;
231 if ( propagHypFilter.IsEmpty() )
234 Init( SMESH_HypoFilter::HasName( StdMeshers_Propagation::GetName ())).
235 Or ( SMESH_HypoFilter::HasName( StdMeshers_PropagOfDistribution::GetName ()));
237 return theMesh.GetHypothesis( theEdge, propagHypFilter, true );
239 //================================================================================
241 * \brief Return an iterator on a list of submeshes
243 SMESH_subMeshIteratorPtr iterate( list<SMESH_subMesh*>::const_iterator from,
244 list<SMESH_subMesh*>::const_iterator to)
246 typedef SMESH_subMesh* TsubMesh;
247 typedef SMDS_SetIterator< TsubMesh, list< TsubMesh >::const_iterator > TIterator;
248 return SMESH_subMeshIteratorPtr ( new TIterator( from, to ));
250 //================================================================================
252 * \brief Build propagation chain
253 * \param theMainSubMesh - the submesh with Propagation hypothesis
255 bool buildPropagationChain ( SMESH_subMesh* theMainSubMesh )
257 DBGMSG( "buildPropagationChain from " << theMainSubMesh->GetId() );
258 const TopoDS_Shape& theMainEdge = theMainSubMesh->GetSubShape();
259 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
261 SMESH_Mesh* mesh = theMainSubMesh->GetFather();
263 TopoDS_Shape shapeOfHyp1D; // shape to which an hyp being propagated is assigned
264 const SMESH_Hypothesis* hyp1D = getLocal1DHyp( *mesh, theMainEdge, &shapeOfHyp1D );
265 SMESH_HypoFilter moreLocalCheck( SMESH_HypoFilter::IsMoreLocalThan( shapeOfHyp1D, *mesh ));
267 PropagationMgrData* chainData = getData( theMainSubMesh );
268 chainData->SetState( HAS_PROPAG_HYP );
270 if ( const SMESH_Hypothesis * propagHyp = getProagationHyp( *mesh, theMainEdge ))
271 chainData->myIsPropagOfDistribution =
272 ( StdMeshers_PropagOfDistribution::GetName() == propagHyp->GetName() );
274 // Edge submeshes, to which the 1D hypothesis will be propagated from theMainEdge
275 list<SMESH_subMesh*> & chain = chainData->mySubMeshes;
277 chain.push_back( theMainSubMesh );
279 TopTools_MapOfShape checkedShapes;
280 checkedShapes.Add( theMainEdge );
282 list<SMESH_subMesh*>::iterator smIt = chain.begin();
283 for ( ; smIt != chain.end(); ++smIt )
285 const TopoDS_Edge& anE = TopoDS::Edge( (*smIt)->GetSubShape() );
286 PropagationMgrData* data = findData( *smIt );
287 if ( !data ) continue;
289 // Iterate on faces, having edge <anE>
290 TopTools_ListIteratorOfListOfShape itA (mesh->GetAncestors(anE));
291 for (; itA.More(); itA.Next())
293 // there are objects of different type among the ancestors of edge
294 if ( itA.Value().ShapeType() != TopAbs_WIRE /*|| !checkedShapes.Add( itA.Value() )*/)
297 // Get ordered edges and find index of anE in a sequence
298 BRepTools_WireExplorer aWE (TopoDS::Wire(itA.Value()));
299 vector<TopoDS_Edge> edges;
302 for (; aWE.More(); aWE.Next()) {
303 TopoDS_Edge edge = aWE.Current();
304 edge.Orientation( aWE.Orientation() );
305 if ( edge.IsSame( anE ))
306 edgeIndex = edges.size();
307 edges.push_back( edge );
310 // Find an edge opposite to anE
312 if ( edges.size() < 4 ) {
313 continue; // too few edges
315 else if ( edges.size() == 4 ) {
316 int oppIndex = ( edgeIndex + 2 ) % 4;
317 anOppE = edges[ oppIndex ];
321 TopoDS_Edge prevEdge = anE;
322 int nbSide = 0, eIndex = edgeIndex + 1;
323 for ( int i = 0; i < edges.size(); ++i, ++eIndex )
325 if ( eIndex == edges.size() )
327 if ( !SMESH_Algo::IsContinuous( prevEdge, edges[ eIndex ])) {
331 // check that anE is not a part of a composite side
332 if ( anE.IsSame( prevEdge ) || anE.IsSame( edges[ eIndex ])) {
333 anOppE.Nullify(); break;
336 if ( nbSide == 2 ) { // opposite side
337 if ( !anOppE.IsNull() ) {
338 // composite opposite side -> stop propagation
339 anOppE.Nullify(); break;
341 anOppE = edges[ eIndex ];
344 anOppE.Nullify(); break; // too many sides
346 prevEdge = edges[ eIndex ];
348 if ( anOppE.IsNull() )
351 DBGMSG( nbSide << " sides in wire #" << mesh->GetMeshDS()->ShapeToIndex( itA.Value() ) << " - SKIP" );
355 if ( anOppE.IsNull() || !checkedShapes.Add( anOppE ))
357 SMESH_subMesh* oppSM = mesh->GetSubMesh( anOppE );
358 PropagationMgrData* oppData = getData( oppSM );
360 // Add anOppE to aChain if ...
361 if ( oppData->State() == WAIT_PROPAG_HYP ) // ... anOppE is not in any chain
363 oppData->SetSource( theMainSubMesh );
364 if ( ! (hyp1D = getLocal1DHyp( *mesh, anOppE, &shapeOfHyp1D )) || //...no 1d hyp on anOppE
365 ! (moreLocalCheck.IsOk( hyp1D, shapeOfHyp1D ))) // ... or hyp1D is "more global"
367 oppData->myForward = data->myForward;
368 if ( edges[ edgeIndex ].Orientation() == anOppE.Orientation() )
369 oppData->myForward = !oppData->myForward;
370 chain.push_back( oppSM );
371 oppSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
372 oppData->SetState( IN_CHAIN );
373 DBGMSG( "set IN_CHAIN on " << oppSM->GetId() );
374 if ( oppSM->GetAlgoState() != SMESH_subMesh::HYP_OK )
375 // make oppSM check algo state
376 if ( SMESH_Algo* algo = mesh->GetGen()->GetAlgo( *mesh, anOppE ))
377 oppSM->AlgoStateEngine(SMESH_subMesh::ADD_FATHER_ALGO,algo);
380 oppData->SetState( LAST_IN_CHAIN );
381 DBGMSG( "set LAST_IN_CHAIN on " << oppSM->GetId() );
384 else if ( oppData->State() == LAST_IN_CHAIN ) // anOppE breaks other chain
386 DBGMSG( "encounters LAST_IN_CHAIN on " << oppSM->GetId() );
387 oppData->AddSource( theMainSubMesh );
389 } // loop on face ancestors
390 } // loop on the chain
392 // theMainSubMesh must not be in a chain
397 //================================================================================
399 * \brief Clear propagation chain
401 bool clearPropagationChain( SMESH_subMesh* subMesh )
403 DBGMSG( "clearPropagationChain from " << subMesh->GetId() );
404 if ( PropagationMgrData* data = findData( subMesh ))
406 switch ( data->State() ) {
408 return clearPropagationChain( data->GetSource() );
410 case HAS_PROPAG_HYP: {
411 SMESH_subMeshIteratorPtr smIt = data->GetChain();
412 while ( smIt->more() ) {
413 SMESH_subMesh* sm = smIt->next();
414 getData( sm )->Init();
415 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
420 case LAST_IN_CHAIN: {
421 SMESH_subMeshIteratorPtr smIt = iterate( data->mySubMeshes.begin(),
422 data->mySubMeshes.end());
423 while ( smIt->more() )
424 clearPropagationChain( smIt->next() );
436 //================================================================================
438 * \brief Return an iterator on chain submeshes
440 //================================================================================
442 SMESH_subMeshIteratorPtr PropagationMgrData::GetChain() const
446 return iterate( mySubMeshes.begin(), mySubMeshes.end() );
448 if ( mySubMeshes.empty() ) break;
449 return getData( mySubMeshes.front() )->GetChain();
452 return iterate( mySubMeshes.end(), mySubMeshes.end() );
454 //================================================================================
456 * \brief Return a propagation source submesh
458 //================================================================================
460 SMESH_subMesh* PropagationMgrData::GetSource() const
462 if ( myType == IN_CHAIN )
463 if ( !mySubMeshes.empty() )
464 return mySubMeshes.front();
469 //================================================================================
473 //================================================================================
475 PropagationMgr::PropagationMgr()
476 : SMESH_subMeshEventListener( false, // won't be deleted by submesh
477 "StdMeshers_Propagation::PropagationMgr")
479 //================================================================================
481 * \brief Set PropagationMgr on a submesh
483 //================================================================================
485 void PropagationMgr::Set(SMESH_subMesh * submesh)
487 if ( findData( submesh )) return;
488 DBGMSG( "PropagationMgr::Set() on " << submesh->GetId() );
489 PropagationMgrData* data = new PropagationMgrData();
490 submesh->SetEventListener( getListener(), data, submesh );
492 const SMESH_Hypothesis * propagHyp =
493 getProagationHyp( *submesh->GetFather(), submesh->GetSubShape() );
496 data->myIsPropagOfDistribution =
497 ( StdMeshers_PropagOfDistribution::GetName() == propagHyp->GetName() );
498 getListener()->ProcessEvent( SMESH_subMesh::ADD_HYP,
499 SMESH_subMesh::ALGO_EVENT,
505 //================================================================================
507 * \brief Return an edge from which hypotheses are propagated
509 //================================================================================
511 TopoDS_Edge PropagationMgr::GetSource(SMESH_subMesh * submesh,
512 bool& isPropagOfDistribution)
514 if ( PropagationMgrData* data = findData( submesh )) {
515 if ( data->State() == IN_CHAIN ) {
516 if ( SMESH_subMesh* sm = data->GetSource() )
518 TopoDS_Shape edge = sm->GetSubShape();
519 edge = edge.Oriented( data->myForward ? TopAbs_FORWARD : TopAbs_REVERSED );
520 DBGMSG( " GetSource() = edge " << sm->GetId() << " REV = " << (!data->myForward));
521 isPropagOfDistribution = false;
522 if ( PropagationMgrData* data = findData( sm ))
523 isPropagOfDistribution = data->myIsPropagOfDistribution;
524 if ( edge.ShapeType() == TopAbs_EDGE )
525 return TopoDS::Edge( edge );
529 return TopoDS_Edge();
531 //================================================================================
533 * \brief React on events on 1D submeshes
535 //================================================================================
537 void PropagationMgr::ProcessEvent(const int event,
539 SMESH_subMesh* subMesh,
540 SMESH_subMeshEventListenerData* listenerData,
541 const SMESH_Hypothesis* hyp)
545 if ( !hyp || hyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO || hyp->GetDim() != 1 )
547 if ( eventType != SMESH_subMesh::ALGO_EVENT )
549 DBGMSG( "PropagationMgr::ProcessEvent() on " << subMesh->GetId() );
551 bool isPropagHyp = ( StdMeshers_Propagation::GetName() == hyp->GetName() ||
552 StdMeshers_PropagOfDistribution::GetName() == hyp->GetName() );
554 PropagationMgrData* data = static_cast<PropagationMgrData*>( listenerData );
555 switch ( data->State() ) {
557 case WAIT_PROPAG_HYP: { // propagation hyp or local 1D hyp is missing
558 // --------------------------------------------------------
559 bool hasPropagHyp = ( isPropagHyp ||
560 getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) );
563 bool hasLocal1DHyp = getLocal1DHyp( *subMesh->GetFather(), subMesh->GetSubShape());
564 if ( !hasLocal1DHyp )
566 if ( event == SMESH_subMesh::ADD_HYP ||
567 event == SMESH_subMesh::ADD_FATHER_HYP ) // add local or propagation hyp
569 DBGMSG( "ADD_HYP propagation to WAIT_PROPAG_HYP " << subMesh->GetId() );
570 // build propagation chain
571 buildPropagationChain( subMesh );
575 case HAS_PROPAG_HYP: { // propag hyp on this submesh
576 // --------------------------------------------------------
578 case SMESH_subMesh::REMOVE_HYP:
579 case SMESH_subMesh::REMOVE_FATHER_HYP: // remove propagation hyp
580 if ( isPropagHyp && !getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) )
582 DBGMSG( "REMOVE_HYP propagation from HAS_PROPAG_HYP " << subMesh->GetId() );
583 // clear propagation chain
584 clearPropagationChain( subMesh );
586 // return; -- hyp is modified any way
588 //case SMESH_subMesh::MODIF_HYP: // hyp modif
589 // clear mesh in a chain
590 DBGMSG( "MODIF_HYP on HAS_PROPAG_HYP " << subMesh->GetId() );
591 SMESH_subMeshIteratorPtr smIt = data->GetChain();
592 while ( smIt->more() ) {
593 SMESH_subMesh* smInChain = smIt->next();
594 smInChain->AlgoStateEngine( SMESH_subMesh::MODIF_HYP,
595 (SMESH_Hypothesis*) hyp );
601 case IN_CHAIN: { // submesh is in propagation chain
602 // --------------------------------------------------------
603 if ( event == SMESH_subMesh::ADD_HYP ) { // add local hypothesis
604 if ( isPropagHyp ) { // propagation hyp added
605 DBGMSG( "ADD_HYP propagation on IN_CHAIN " << subMesh->GetId() );
606 // collision - do nothing
608 else { // 1D hyp added
609 // rebuild propagation chain
610 DBGMSG( "ADD_HYP 1D on IN_CHAIN " << subMesh->GetId() );
611 SMESH_subMesh* sourceSM = data->GetSource();
612 clearPropagationChain( sourceSM );
613 buildPropagationChain( sourceSM );
618 case LAST_IN_CHAIN: { // submesh with local 1D hyp, breaking a chain
619 // --------------------------------------------------------
620 if ( event == SMESH_subMesh::REMOVE_HYP ) { // remove local hyp
621 // rebuild propagation chain
622 DBGMSG( "REMOVE_HYP 1D from LAST_IN_CHAIN " << subMesh->GetId() );
623 list<SMESH_subMesh*> sourceSM = data->mySubMeshes;
624 clearPropagationChain( subMesh );
625 SMESH_subMeshIteratorPtr smIt = iterate( sourceSM.begin(), sourceSM.end());
626 while ( smIt->more() )
627 buildPropagationChain( smIt->next() );
631 } // switch by SubMeshState