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);
117 const SMESH_HypoFilter& StdMeshers_Propagation::GetFilter()
119 static SMESH_HypoFilter propagHypFilter;
120 if ( propagHypFilter.IsEmpty() )
123 Init( SMESH_HypoFilter::HasName( StdMeshers_Propagation::GetName ())).
124 Or ( SMESH_HypoFilter::HasName( StdMeshers_PropagOfDistribution::GetName ()));
126 return propagHypFilter;
128 //=============================================================================
129 //=============================================================================
130 // PROPAGATION MANAGEMENT
131 //=============================================================================
132 //=============================================================================
136 enum SubMeshState { WAIT_PROPAG_HYP, // propagation hyp or local 1D hyp is missing
137 HAS_PROPAG_HYP, // propag hyp on this submesh
138 IN_CHAIN, // submesh is in propagation chain
139 LAST_IN_CHAIN, // submesh with local 1D hyp breaking a chain
140 MEANINGLESS_LAST }; // meaningless
142 struct PropagationMgrData : public EventListenerData
144 bool myForward; //!< true if a curve of edge in chain is codirected with one of source edge
145 bool myIsPropagOfDistribution; //!< type of Propagation hyp
146 PropagationMgrData( SubMeshState state=WAIT_PROPAG_HYP ): EventListenerData(true) {
147 myType = state; myForward = true; myIsPropagOfDistribution = false;
150 myType = WAIT_PROPAG_HYP; mySubMeshes.clear(); myForward = true;
151 myIsPropagOfDistribution = false;
153 SubMeshState State() const {
154 return (SubMeshState) myType;
156 void SetState(SubMeshState state) {
159 void SetSource(SMESH_subMesh* sm ) {
160 mySubMeshes.clear(); if ( sm ) mySubMeshes.push_back( sm );
162 void AddSource(SMESH_subMesh* sm ) {
163 if ( sm ) mySubMeshes.push_back( sm );
165 void SetChain(list< SMESH_subMesh* >& chain ) {
166 mySubMeshes.clear(); mySubMeshes.splice( mySubMeshes.end(), chain );
168 SMESH_subMeshIteratorPtr GetChain() const;
169 SMESH_subMesh* GetSource() const;
172 //=============================================================================
174 * \brief return static PropagationMgr
176 PropagationMgr* PropagationMgr::GetListener()
178 static PropagationMgr theListener;
181 PropagationMgr* getListener()
183 return PropagationMgr::GetListener();
185 //=============================================================================
187 * \brief return PropagationMgrData found on a submesh
189 PropagationMgrData* findData(SMESH_subMesh* sm)
192 return static_cast< PropagationMgrData* >( sm->GetEventListenerData( getListener() ));
195 //=============================================================================
197 * \brief return PropagationMgrData found on theEdge submesh
199 PropagationMgrData* findData(SMESH_Mesh& theMesh, const TopoDS_Shape& theEdge)
201 if ( theEdge.ShapeType() == TopAbs_EDGE )
202 return findData( theMesh.GetSubMeshContaining( theEdge ) );
205 //=============================================================================
207 * \brief return existing or a new PropagationMgrData
209 PropagationMgrData* getData(SMESH_subMesh* sm)
211 PropagationMgrData* data = findData( sm );
213 data = new PropagationMgrData();
214 sm->SetEventListener( getListener(), data, sm );
218 //=============================================================================
220 * \brief Returns a local 1D hypothesis used for theEdge
222 const SMESH_Hypothesis* getLocal1DHyp (SMESH_subMesh* theSubMesh,
223 //const TopoDS_Shape& theEdge,
224 TopoDS_Shape* theSssignedTo=0)
226 static SMESH_HypoFilter hypo;
227 hypo.Init( hypo.HasDim( 1 )).
228 AndNot ( hypo.IsAlgo() ).
229 AndNot ( hypo.IsAssignedTo( theSubMesh->GetFather()->GetShapeToMesh() ));
231 return theSubMesh->GetFather()->GetHypothesis( theSubMesh, hypo, true, theSssignedTo );
233 //=============================================================================
235 * \brief Returns a propagation hypothesis assigned to theEdge
237 const SMESH_Hypothesis* getProagationHyp (SMESH_subMesh* theSubMesh)
239 return theSubMesh->GetFather()->GetHypothesis
240 ( theSubMesh, StdMeshers_Propagation::GetFilter(), true );
242 //================================================================================
244 * \brief Return an iterator on a list of submeshes
246 SMESH_subMeshIteratorPtr iterate( list<SMESH_subMesh*>::const_iterator from,
247 list<SMESH_subMesh*>::const_iterator to)
249 typedef SMESH_subMesh* TsubMesh;
250 typedef SMDS_SetIterator< TsubMesh, list< TsubMesh >::const_iterator > TIterator;
251 return SMESH_subMeshIteratorPtr ( new TIterator( from, to ));
253 //================================================================================
255 * \brief Build propagation chain
256 * \param theMainSubMesh - the submesh with Propagation hypothesis
258 bool buildPropagationChain ( SMESH_subMesh* theMainSubMesh )
260 DBGMSG( "buildPropagationChain from " << theMainSubMesh->GetId() );
261 const TopoDS_Shape& theMainEdge = theMainSubMesh->GetSubShape();
262 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
264 SMESH_Mesh* mesh = theMainSubMesh->GetFather();
266 TopoDS_Shape shapeOfHyp1D; // shape to which an hyp being propagated is assigned
267 const SMESH_Hypothesis* hyp1D = getLocal1DHyp( theMainSubMesh, &shapeOfHyp1D );
268 SMESH_HypoFilter moreLocalCheck( SMESH_HypoFilter::IsMoreLocalThan( shapeOfHyp1D, *mesh ));
270 PropagationMgrData* chainData = getData( theMainSubMesh );
271 chainData->SetState( HAS_PROPAG_HYP );
273 if ( const SMESH_Hypothesis * propagHyp = getProagationHyp( theMainSubMesh ))
274 chainData->myIsPropagOfDistribution =
275 ( StdMeshers_PropagOfDistribution::GetName() == propagHyp->GetName() );
277 // Edge submeshes, to which the 1D hypothesis will be propagated from theMainEdge
278 list<SMESH_subMesh*> & chain = chainData->mySubMeshes;
280 chain.push_back( theMainSubMesh );
282 TopTools_MapOfShape checkedShapes;
283 checkedShapes.Add( theMainEdge );
285 vector<TopoDS_Edge> edges;
287 list<SMESH_subMesh*>::iterator smIt = chain.begin();
288 for ( ; smIt != chain.end(); ++smIt )
290 const TopoDS_Edge& anE = TopoDS::Edge( (*smIt)->GetSubShape() );
291 PropagationMgrData* data = findData( *smIt );
292 if ( !data ) continue;
294 // Iterate on faces, having edge <anE>
295 TopTools_ListIteratorOfListOfShape itA (mesh->GetAncestors(anE));
296 for (; itA.More(); itA.Next())
298 // there are objects of different type among the ancestors of edge
299 if ( itA.Value().ShapeType() != TopAbs_WIRE /*|| !checkedShapes.Add( itA.Value() )*/)
302 // Get ordered edges and find index of anE in a sequence
304 BRepTools_WireExplorer aWE (TopoDS::Wire(itA.Value()));
306 for (; aWE.More(); aWE.Next()) {
307 TopoDS_Edge edge = aWE.Current();
308 edge.Orientation( aWE.Orientation() );
309 if ( edge.IsSame( anE ))
310 edgeIndex = edges.size();
311 edges.push_back( edge );
314 // Find an edge opposite to anE
316 if ( edges.size() < 4 ) {
317 continue; // too few edges
319 else if ( edges.size() == 4 ) {
320 int oppIndex = ( edgeIndex + 2 ) % 4;
321 anOppE = edges[ oppIndex ];
325 TopoDS_Edge prevEdge = anE;
326 int nbSide = 0, eIndex = edgeIndex + 1;
327 for ( int i = 0; i < edges.size(); ++i, ++eIndex )
329 if ( eIndex == edges.size() )
331 if ( !SMESH_Algo::IsContinuous( prevEdge, edges[ eIndex ])) {
335 // check that anE is not a part of a composite side
336 if ( anE.IsSame( prevEdge ) || anE.IsSame( edges[ eIndex ])) {
337 anOppE.Nullify(); break;
340 if ( nbSide == 2 ) { // opposite side
341 if ( !anOppE.IsNull() ) {
342 // composite opposite side -> stop propagation
343 anOppE.Nullify(); break;
345 anOppE = edges[ eIndex ];
348 anOppE.Nullify(); break; // too many sides
350 prevEdge = edges[ eIndex ];
352 if ( anOppE.IsNull() )
355 DBGMSG( nbSide << " sides in wire #" << mesh->GetMeshDS()->ShapeToIndex( itA.Value() ) << " - SKIP" );
359 if ( anOppE.IsNull() || !checkedShapes.Add( anOppE ))
361 SMESH_subMesh* oppSM = mesh->GetSubMesh( anOppE );
362 PropagationMgrData* oppData = getData( oppSM );
364 // Add anOppE to aChain if ...
365 if ( oppData->State() == WAIT_PROPAG_HYP ) // ... anOppE is not in any chain
367 oppData->SetSource( theMainSubMesh );
368 if ( ! (hyp1D = getLocal1DHyp( oppSM, &shapeOfHyp1D )) || //...no 1d hyp on anOppE
369 ! (moreLocalCheck.IsOk( hyp1D, shapeOfHyp1D ))) // ... or hyp1D is "more global"
371 oppData->myForward = data->myForward;
372 if ( edges[ edgeIndex ].Orientation() == anOppE.Orientation() )
373 oppData->myForward = !oppData->myForward;
374 chain.push_back( oppSM );
375 oppSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
376 oppData->SetState( IN_CHAIN );
377 DBGMSG( "set IN_CHAIN on " << oppSM->GetId() );
378 if ( oppSM->GetAlgoState() != SMESH_subMesh::HYP_OK )
379 // make oppSM check algo state
380 if ( SMESH_Algo* algo = oppSM->GetAlgo() )
381 oppSM->AlgoStateEngine(SMESH_subMesh::ADD_FATHER_ALGO, algo);
384 oppData->SetState( LAST_IN_CHAIN );
385 DBGMSG( "set LAST_IN_CHAIN on " << oppSM->GetId() );
388 else if ( oppData->State() == LAST_IN_CHAIN ) // anOppE breaks other chain
390 DBGMSG( "encounters LAST_IN_CHAIN on " << oppSM->GetId() );
391 oppData->AddSource( theMainSubMesh );
393 } // loop on face ancestors
394 } // loop on the chain
396 // theMainSubMesh must not be in a chain
401 //================================================================================
403 * \brief Clear propagation chain
405 bool clearPropagationChain( SMESH_subMesh* subMesh )
407 DBGMSG( "clearPropagationChain from " << subMesh->GetId() );
408 if ( PropagationMgrData* data = findData( subMesh ))
410 switch ( data->State() ) {
412 return clearPropagationChain( data->GetSource() );
414 case HAS_PROPAG_HYP: {
415 SMESH_subMeshIteratorPtr smIt = data->GetChain();
416 while ( smIt->more() ) {
417 SMESH_subMesh* sm = smIt->next();
418 getData( sm )->Init();
419 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
424 case LAST_IN_CHAIN: {
425 SMESH_subMeshIteratorPtr smIt = iterate( data->mySubMeshes.begin(),
426 data->mySubMeshes.end());
427 while ( smIt->more() )
428 clearPropagationChain( smIt->next() );
440 //================================================================================
442 * \brief Return an iterator on chain submeshes
444 //================================================================================
446 SMESH_subMeshIteratorPtr PropagationMgrData::GetChain() const
450 return iterate( mySubMeshes.begin(), mySubMeshes.end() );
452 if ( mySubMeshes.empty() ) break;
453 return getData( mySubMeshes.front() )->GetChain();
456 return iterate( mySubMeshes.end(), mySubMeshes.end() );
458 //================================================================================
460 * \brief Return a propagation source submesh
462 //================================================================================
464 SMESH_subMesh* PropagationMgrData::GetSource() const
466 if ( myType == IN_CHAIN )
467 if ( !mySubMeshes.empty() )
468 return mySubMeshes.front();
473 //================================================================================
477 //================================================================================
479 PropagationMgr::PropagationMgr()
480 : SMESH_subMeshEventListener( false, // won't be deleted by submesh
481 "StdMeshers_Propagation::PropagationMgr")
483 //================================================================================
485 * \brief Set PropagationMgr on a submesh
487 //================================================================================
489 void PropagationMgr::Set(SMESH_subMesh * submesh)
491 if ( findData( submesh )) return;
492 DBGMSG( "PropagationMgr::Set() on " << submesh->GetId() );
493 PropagationMgrData* data = new PropagationMgrData();
494 submesh->SetEventListener( getListener(), data, submesh );
496 const SMESH_Hypothesis * propagHyp =
497 getProagationHyp( submesh );
500 data->myIsPropagOfDistribution =
501 ( StdMeshers_PropagOfDistribution::GetName() == propagHyp->GetName() );
502 getListener()->ProcessEvent( SMESH_subMesh::ADD_HYP,
503 SMESH_subMesh::ALGO_EVENT,
509 //================================================================================
511 * \brief Return an edge from which hypotheses are propagated
513 //================================================================================
515 TopoDS_Edge PropagationMgr::GetSource(SMESH_subMesh * submesh,
516 bool& isPropagOfDistribution)
518 if ( PropagationMgrData* data = findData( submesh )) {
519 if ( data->State() == IN_CHAIN ) {
520 if ( SMESH_subMesh* sm = data->GetSource() )
522 TopoDS_Shape edge = sm->GetSubShape();
523 edge = edge.Oriented( data->myForward ? TopAbs_FORWARD : TopAbs_REVERSED );
524 DBGMSG( " GetSource() = edge " << sm->GetId() << " REV = " << (!data->myForward));
525 isPropagOfDistribution = false;
526 if ( PropagationMgrData* data = findData( sm ))
527 isPropagOfDistribution = data->myIsPropagOfDistribution;
528 if ( edge.ShapeType() == TopAbs_EDGE )
529 return TopoDS::Edge( edge );
533 return TopoDS_Edge();
535 //================================================================================
537 * \brief React on events on 1D submeshes
539 //================================================================================
541 void PropagationMgr::ProcessEvent(const int event,
543 SMESH_subMesh* subMesh,
544 SMESH_subMeshEventListenerData* listenerData,
545 const SMESH_Hypothesis* hyp)
549 if ( !hyp || hyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO || hyp->GetDim() != 1 )
551 if ( eventType != SMESH_subMesh::ALGO_EVENT )
553 DBGMSG( "PropagationMgr::ProcessEvent() on " << subMesh->GetId() );
555 bool isPropagHyp = ( StdMeshers_Propagation::GetName() == hyp->GetName() ||
556 StdMeshers_PropagOfDistribution::GetName() == hyp->GetName() );
558 PropagationMgrData* data = static_cast<PropagationMgrData*>( listenerData );
559 switch ( data->State() ) {
561 case WAIT_PROPAG_HYP: { // propagation hyp or local 1D hyp is missing
562 // --------------------------------------------------------
563 bool hasPropagHyp = ( isPropagHyp || getProagationHyp( subMesh ));
566 bool hasLocal1DHyp = getLocal1DHyp( subMesh );
567 if ( !hasLocal1DHyp )
569 if ( event == SMESH_subMesh::ADD_HYP ||
570 event == SMESH_subMesh::ADD_FATHER_HYP ) // add local or propagation hyp
572 DBGMSG( "ADD_HYP propagation to WAIT_PROPAG_HYP " << subMesh->GetId() );
573 // build propagation chain
574 buildPropagationChain( subMesh );
578 case HAS_PROPAG_HYP: { // propag hyp on this submesh
579 // --------------------------------------------------------
581 case SMESH_subMesh::REMOVE_HYP:
582 case SMESH_subMesh::REMOVE_FATHER_HYP: // remove propagation hyp
583 if ( isPropagHyp && !getProagationHyp( subMesh ))
585 DBGMSG( "REMOVE_HYP propagation from HAS_PROPAG_HYP " << subMesh->GetId() );
586 // clear propagation chain
587 clearPropagationChain( subMesh );
589 // return; -- hyp is modified any way
591 //case SMESH_subMesh::MODIF_HYP: // hyp modif
592 // clear mesh in a chain
593 DBGMSG( "MODIF_HYP on HAS_PROPAG_HYP " << subMesh->GetId() );
594 SMESH_subMeshIteratorPtr smIt = data->GetChain();
595 while ( smIt->more() ) {
596 SMESH_subMesh* smInChain = smIt->next();
597 smInChain->AlgoStateEngine( SMESH_subMesh::MODIF_HYP,
598 (SMESH_Hypothesis*) hyp );
604 case IN_CHAIN: { // submesh is in propagation chain
605 // --------------------------------------------------------
606 if ( event == SMESH_subMesh::ADD_HYP ) { // add local hypothesis
607 if ( isPropagHyp ) { // propagation hyp added
608 DBGMSG( "ADD_HYP propagation on IN_CHAIN " << subMesh->GetId() );
609 // collision - do nothing
611 else { // 1D hyp added
612 // rebuild propagation chain
613 DBGMSG( "ADD_HYP 1D on IN_CHAIN " << subMesh->GetId() );
614 SMESH_subMesh* sourceSM = data->GetSource();
615 clearPropagationChain( sourceSM );
616 buildPropagationChain( sourceSM );
621 case LAST_IN_CHAIN: { // submesh with local 1D hyp, breaking a chain
622 // --------------------------------------------------------
623 if ( event == SMESH_subMesh::REMOVE_HYP ) { // remove local hyp
624 // rebuild propagation chain
625 DBGMSG( "REMOVE_HYP 1D from LAST_IN_CHAIN " << subMesh->GetId() );
626 list<SMESH_subMesh*> sourceSM = data->mySubMeshes;
627 clearPropagationChain( subMesh );
628 SMESH_subMeshIteratorPtr smIt = iterate( sourceSM.begin(), sourceSM.end());
629 while ( smIt->more() )
630 buildPropagationChain( smIt->next() );
634 } // switch by SubMeshState