1 // Copyright (C) 2007-2010 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_Propagation.cxx
27 #include "StdMeshers_Propagation.hxx"
29 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_subMesh.hxx"
37 #include <BRepTools_WireExplorer.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_MapOfShape.hxx>
43 // cout << txt << endl;
49 // =======================================================================
51 * \brief Listener managing propagation of 1D hypotheses
53 // =======================================================================
55 class PropagationMgr: public SMESH_subMeshEventListener
58 static PropagationMgr* GetListener();
60 * \brief Set listener on edge submesh
62 static void Set(SMESH_subMesh * submesh);
64 * \brief Return an edge from which hypotheses are propagated from
66 static TopoDS_Edge GetSource(SMESH_subMesh * submesh);
68 * \brief Does it's main job
70 void ProcessEvent(const int event,
72 SMESH_subMesh* subMesh,
73 SMESH_subMeshEventListenerData* data,
74 const SMESH_Hypothesis* hyp = 0);
80 //=============================================================================
82 * StdMeshers_Propagation Implementation
84 //=============================================================================
86 StdMeshers_Propagation::StdMeshers_Propagation (int hypId, int studyId, SMESH_Gen * gen)
87 : SMESH_Hypothesis(hypId, studyId, gen)
90 _param_algo_dim = -1; // 1D auxiliary
92 StdMeshers_Propagation::~StdMeshers_Propagation() {}
93 string StdMeshers_Propagation::GetName () { return "Propagation"; }
94 ostream & StdMeshers_Propagation::SaveTo (ostream & save) { return save; }
95 istream & StdMeshers_Propagation::LoadFrom (istream & load) { return load; }
96 ostream & operator << (ostream & save, StdMeshers_Propagation & hyp) { return hyp.SaveTo(save); }
97 istream & operator >> (istream & load, StdMeshers_Propagation & hyp) { return hyp.LoadFrom(load); }
98 bool StdMeshers_Propagation::SetParametersByMesh(const SMESH_Mesh*,
99 const TopoDS_Shape& ) { return false; }
100 bool StdMeshers_Propagation::SetParametersByDefaults(const TDefaults&,const SMESH_Mesh*) { return false; }
101 void StdMeshers_Propagation::SetPropagationMgr(SMESH_subMesh* subMesh) { PropagationMgr::Set( subMesh ); }
103 * \brief Return an edge from which hypotheses are propagated from
105 TopoDS_Edge StdMeshers_Propagation::GetPropagationSource(SMESH_Mesh& theMesh,
106 const TopoDS_Shape& theEdge)
108 return PropagationMgr::GetSource(theMesh.GetSubMeshContaining( theEdge ));
111 //=============================================================================
112 //=============================================================================
113 // PROPAGATION MANAGEMENT
114 //=============================================================================
115 //=============================================================================
119 enum SubMeshState { WAIT_PROPAG_HYP, // propagation hyp or local 1D hyp is missing
120 HAS_PROPAG_HYP, // propag hyp on this submesh
121 IN_CHAIN, // submesh is in propagation chain
122 LAST_IN_CHAIN, // submesh with local 1D hyp breaking a chain
123 MEANINGLESS_LAST }; // meaningless
125 struct PropagationMgrData : public EventListenerData
127 bool myForward; //!< true if a curve of edge in chain is codirected with one of source edge
128 PropagationMgrData( SubMeshState state=WAIT_PROPAG_HYP ): EventListenerData(true) {
129 myType = state; myForward = true;
132 myType = WAIT_PROPAG_HYP; mySubMeshes.clear(); myForward = true;
134 SubMeshState State() const {
135 return (SubMeshState) myType;
137 void SetState(SubMeshState state) {
140 void SetSource(SMESH_subMesh* sm ) {
141 mySubMeshes.clear(); if ( sm ) mySubMeshes.push_back( sm );
143 void AddSource(SMESH_subMesh* sm ) {
144 if ( sm ) mySubMeshes.push_back( sm );
146 void SetChain(list< SMESH_subMesh* >& chain ) {
147 mySubMeshes.clear(); mySubMeshes.splice( mySubMeshes.end(), chain );
149 SMESH_subMeshIteratorPtr GetChain() const;
150 SMESH_subMesh* GetSource() const;
153 //=============================================================================
155 * \brief return static PropagationMgr
157 PropagationMgr* PropagationMgr::GetListener()
159 static PropagationMgr theListener;
162 PropagationMgr* getListener()
164 return PropagationMgr::GetListener();
166 //=============================================================================
168 * \brief return PropagationMgrData found on a submesh
170 PropagationMgrData* findData(SMESH_subMesh* sm)
173 return static_cast< PropagationMgrData* >( sm->GetEventListenerData( getListener() ));
176 //=============================================================================
178 * \brief return PropagationMgrData found on theEdge submesh
180 PropagationMgrData* findData(SMESH_Mesh& theMesh, const TopoDS_Shape& theEdge)
182 if ( theEdge.ShapeType() == TopAbs_EDGE )
183 return findData( theMesh.GetSubMeshContaining( theEdge ) );
186 //=============================================================================
188 * \brief return existing or a new PropagationMgrData
190 PropagationMgrData* getData(SMESH_subMesh* sm)
192 PropagationMgrData* data = findData( sm );
194 data = new PropagationMgrData();
195 sm->SetEventListener( getListener(), data, sm );
199 //=============================================================================
201 * \brief Returns a local 1D hypothesis used for theEdge
203 const SMESH_Hypothesis* getLocal1DHyp (SMESH_Mesh& theMesh,
204 const TopoDS_Shape& theEdge)
206 static SMESH_HypoFilter hypo;
207 hypo.Init( hypo.HasDim( 1 )).
208 AndNot ( hypo.IsAlgo() ).
209 AndNot ( hypo.IsAssignedTo( theMesh.GetMeshDS()->ShapeToMesh() ));
210 return theMesh.GetHypothesis( theEdge, hypo, true );
212 //=============================================================================
214 * \brief Returns a propagation hypothesis assigned to theEdge
216 const SMESH_Hypothesis* getProagationHyp (SMESH_Mesh& theMesh,
217 const TopoDS_Shape& theEdge)
219 static SMESH_HypoFilter propagHypFilter
220 ( SMESH_HypoFilter::HasName( StdMeshers_Propagation::GetName ()));
221 return theMesh.GetHypothesis( theEdge, propagHypFilter, true );
223 //================================================================================
225 * \brief Return an iterator on a list of submeshes
227 SMESH_subMeshIteratorPtr iterate( list<SMESH_subMesh*>::const_iterator from,
228 list<SMESH_subMesh*>::const_iterator to)
230 typedef SMESH_subMesh* TsubMesh;
231 typedef SMDS_SetIterator< TsubMesh, list< TsubMesh >::const_iterator > TIterator;
232 return SMESH_subMeshIteratorPtr ( new TIterator( from, to ));
234 //================================================================================
236 * \brief Build propagation chain
237 * \param theMainSubMesh - the submesh with Propagation hypothesis
239 bool buildPropagationChain ( SMESH_subMesh* theMainSubMesh )
241 DBGMSG( "buildPropagationChain from " << theMainSubMesh->GetId() );
242 const TopoDS_Shape& theMainEdge = theMainSubMesh->GetSubShape();
243 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
245 SMESH_Mesh* mesh = theMainSubMesh->GetFather();
247 PropagationMgrData* chainData = getData( theMainSubMesh );
248 chainData->SetState( HAS_PROPAG_HYP );
250 // Edge submeshes, to which the 1D hypothesis will be propagated from theMainEdge
251 list<SMESH_subMesh*> & chain = chainData->mySubMeshes;
253 chain.push_back( theMainSubMesh );
255 TopTools_MapOfShape checkedShapes;
256 checkedShapes.Add( theMainEdge );
258 list<SMESH_subMesh*>::iterator smIt = chain.begin();
259 for ( ; smIt != chain.end(); ++smIt )
261 const TopoDS_Edge& anE = TopoDS::Edge( (*smIt)->GetSubShape() );
262 PropagationMgrData* data = findData( *smIt );
263 if ( !data ) continue;
265 // Iterate on faces, having edge <anE>
266 TopTools_ListIteratorOfListOfShape itA (mesh->GetAncestors(anE));
267 for (; itA.More(); itA.Next())
269 // there are objects of different type among the ancestors of edge
270 if ( itA.Value().ShapeType() != TopAbs_WIRE || !checkedShapes.Add( itA.Value() ))
273 // Get ordered edges and find index of anE in a sequence
274 BRepTools_WireExplorer aWE (TopoDS::Wire(itA.Value()));
275 vector<TopoDS_Edge> edges;
278 for (; aWE.More(); aWE.Next()) {
279 TopoDS_Edge edge = aWE.Current();
280 edge.Orientation( aWE.Orientation() );
281 if ( edge.IsSame( anE ))
282 edgeIndex = edges.size();
283 edges.push_back( edge );
286 // Find an edge opposite to anE
288 if ( edges.size() < 4 ) {
289 continue; // too few edges
291 else if ( edges.size() == 4 ) {
292 int oppIndex = edgeIndex + 2;
293 if ( oppIndex > 3 ) oppIndex -= 4;
294 anOppE = edges[ oppIndex ];
298 TopoDS_Edge prevEdge = anE;
299 int nbSide = 0, eIndex = edgeIndex + 1;
300 for ( int i = 0; i < edges.size(); ++i, ++eIndex )
302 if ( eIndex == edges.size() )
304 if ( !SMESH_Algo::IsContinuous( prevEdge, edges[ eIndex ])) {
308 // check that anE is not a part of a composite side
309 if ( anE.IsSame( prevEdge ) || anE.IsSame( edges[ eIndex ])) {
310 anOppE.Nullify(); break;
313 if ( nbSide == 2 ) { // opposite side
314 if ( !anOppE.IsNull() ) {
315 // composite opposite side -> stop propagation
316 anOppE.Nullify(); break;
318 anOppE = edges[ eIndex ];
321 anOppE.Nullify(); break; // too many sides
323 prevEdge = edges[ eIndex ];
325 if ( anOppE.IsNull() )
328 DBGMSG( nbSide << " sides in wire #" << mesh->GetMeshDS()->ShapeToIndex( itA.Value() ) << " - SKIP" );
332 if ( anOppE.IsNull() || !checkedShapes.Add( anOppE ))
334 SMESH_subMesh* oppSM = mesh->GetSubMesh( anOppE );
335 PropagationMgrData* oppData = getData( oppSM );
337 // Add anOppE to aChain if ...
338 if ( oppData->State() == WAIT_PROPAG_HYP ) // ... anOppE is not in any chain
340 oppData->SetSource( theMainSubMesh );
341 if ( !getLocal1DHyp( *mesh, anOppE )) // ... no 1d hyp on anOppE
343 oppData->myForward = data->myForward;
344 if ( edges[ edgeIndex ].Orientation() == anOppE.Orientation() )
345 oppData->myForward = !oppData->myForward;
346 chain.push_back( oppSM );
347 oppSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
348 oppData->SetState( IN_CHAIN );
349 DBGMSG( "set IN_CHAIN on " << oppSM->GetId() );
352 oppData->SetState( LAST_IN_CHAIN );
353 DBGMSG( "set LAST_IN_CHAIN on " << oppSM->GetId() );
356 else if ( oppData->State() == LAST_IN_CHAIN ) // anOppE breaks other chain
358 DBGMSG( "encounters LAST_IN_CHAIN on " << oppSM->GetId() );
359 oppData->AddSource( theMainSubMesh );
361 } // loop on face ancestors
362 } // loop on the chain
364 // theMainSubMesh must not be in a chain
369 //================================================================================
371 * \brief Clear propagation chain
373 bool clearPropagationChain( SMESH_subMesh* subMesh )
375 DBGMSG( "clearPropagationChain from " << subMesh->GetId() );
376 if ( PropagationMgrData* data = findData( subMesh ))
378 switch ( data->State() ) {
380 return clearPropagationChain( data->GetSource() );
382 case HAS_PROPAG_HYP: {
383 SMESH_subMeshIteratorPtr smIt = data->GetChain();
384 while ( smIt->more() ) {
385 SMESH_subMesh* sm = smIt->next();
386 getData( sm )->Init();
387 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
392 case LAST_IN_CHAIN: {
393 SMESH_subMeshIteratorPtr smIt = iterate( data->mySubMeshes.begin(),
394 data->mySubMeshes.end());
395 while ( smIt->more() )
396 clearPropagationChain( smIt->next() );
408 //================================================================================
410 * \brief Return an iterator on chain submeshes
412 //================================================================================
414 SMESH_subMeshIteratorPtr PropagationMgrData::GetChain() const
418 return iterate( mySubMeshes.begin(), mySubMeshes.end() );
420 if ( mySubMeshes.empty() ) break;
421 return getData( mySubMeshes.front() )->GetChain();
424 return iterate( mySubMeshes.end(), mySubMeshes.end() );
426 //================================================================================
428 * \brief Return a propagation source submesh
430 //================================================================================
432 SMESH_subMesh* PropagationMgrData::GetSource() const
434 if ( myType == IN_CHAIN )
435 if ( !mySubMeshes.empty() )
436 return mySubMeshes.front();
441 //================================================================================
445 //================================================================================
447 PropagationMgr::PropagationMgr()
448 : SMESH_subMeshEventListener( false ) // won't be deleted by submesh
450 //================================================================================
452 * \brief Set PropagationMgr on a submesh
454 //================================================================================
456 void PropagationMgr::Set(SMESH_subMesh * submesh)
458 DBGMSG( "PropagationMgr::Set() on " << submesh->GetId() );
459 EventListenerData* data = new PropagationMgrData();
460 submesh->SetEventListener( getListener(), data, submesh );
462 const SMESH_Hypothesis * propagHyp =
463 getProagationHyp( *submesh->GetFather(), submesh->GetSubShape() );
465 getListener()->ProcessEvent( SMESH_subMesh::ADD_HYP,
466 SMESH_subMesh::ALGO_EVENT,
471 //================================================================================
473 * \brief Return an edge from which hypotheses are propagated
475 //================================================================================
477 TopoDS_Edge PropagationMgr::GetSource(SMESH_subMesh * submesh)
479 if ( PropagationMgrData* data = findData( submesh )) {
480 if ( data->State() == IN_CHAIN ) {
481 if ( SMESH_subMesh* sm = data->GetSource() )
483 TopoDS_Shape edge = sm->GetSubShape();
484 edge = edge.Oriented( data->myForward ? TopAbs_FORWARD : TopAbs_REVERSED );
485 DBGMSG( " GetSource() = edge " << sm->GetId() << " REV = " << (!data->myForward));
486 if ( edge.ShapeType() == TopAbs_EDGE )
487 return TopoDS::Edge( edge );
491 return TopoDS_Edge();
493 //================================================================================
495 * \brief React on events on 1D submeshes
497 //================================================================================
499 void PropagationMgr::ProcessEvent(const int event,
501 SMESH_subMesh* subMesh,
502 SMESH_subMeshEventListenerData* listenerData,
503 const SMESH_Hypothesis* hyp)
507 if ( !hyp || hyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO || hyp->GetDim() != 1 )
509 if ( eventType != SMESH_subMesh::ALGO_EVENT )
511 DBGMSG( "PropagationMgr::ProcessEvent() on " << subMesh->GetId() );
513 bool isPropagHyp = ( StdMeshers_Propagation::GetName() == hyp->GetName() );
515 PropagationMgrData* data = static_cast<PropagationMgrData*>( listenerData );
516 switch ( data->State() ) {
518 case WAIT_PROPAG_HYP: { // propagation hyp or local 1D hyp is missing
519 // --------------------------------------------------------
520 bool hasPropagHyp = ( isPropagHyp ||
521 getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) );
524 bool hasLocal1DHyp = getLocal1DHyp( *subMesh->GetFather(), subMesh->GetSubShape());
525 if ( !hasLocal1DHyp )
527 if ( event == SMESH_subMesh::ADD_HYP ||
528 event == SMESH_subMesh::ADD_FATHER_HYP ) // add local or propagation hyp
530 DBGMSG( "ADD_HYP propagation to WAIT_PROPAG_HYP " << subMesh->GetId() );
531 // build propagation chain
532 buildPropagationChain( subMesh );
536 case HAS_PROPAG_HYP: { // propag hyp on this submesh
537 // --------------------------------------------------------
539 case SMESH_subMesh::REMOVE_HYP:
540 case SMESH_subMesh::REMOVE_FATHER_HYP: // remove propagation hyp
541 if ( isPropagHyp && !getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) )
543 DBGMSG( "REMOVE_HYP propagation from HAS_PROPAG_HYP " << subMesh->GetId() );
544 // clear propagation chain
545 clearPropagationChain( subMesh );
547 // return; -- hyp is modified any way
549 //case SMESH_subMesh::MODIF_HYP: // hyp modif
550 // clear mesh in a chain
551 DBGMSG( "MODIF_HYP on HAS_PROPAG_HYP " << subMesh->GetId() );
552 SMESH_subMeshIteratorPtr smIt = data->GetChain();
553 while ( smIt->more() ) {
554 SMESH_subMesh* smInChain = smIt->next();
555 smInChain->AlgoStateEngine( SMESH_subMesh::MODIF_HYP,
556 (SMESH_Hypothesis*) hyp );
562 case IN_CHAIN: { // submesh is in propagation chain
563 // --------------------------------------------------------
564 if ( event == SMESH_subMesh::ADD_HYP ) { // add local hypothesis
565 if ( isPropagHyp ) { // propagation hyp added
566 DBGMSG( "ADD_HYP propagation on IN_CHAIN " << subMesh->GetId() );
567 // collision - do nothing
569 else { // 1D hyp added
570 // rebuild propagation chain
571 DBGMSG( "ADD_HYP 1D on IN_CHAIN " << subMesh->GetId() );
572 SMESH_subMesh* sourceSM = data->GetSource();
573 clearPropagationChain( sourceSM );
574 buildPropagationChain( sourceSM );
579 case LAST_IN_CHAIN: { // submesh with local 1D hyp, breaking a chain
580 // --------------------------------------------------------
581 if ( event == SMESH_subMesh::REMOVE_HYP ) { // remove local hyp
582 // rebuild propagation chain
583 DBGMSG( "REMOVE_HYP 1D from LAST_IN_CHAIN " << subMesh->GetId() );
584 list<SMESH_subMesh*> sourceSM = data->mySubMeshes;
585 clearPropagationChain( subMesh );
586 SMESH_subMeshIteratorPtr smIt = iterate( sourceSM.begin(), sourceSM.end());
587 while ( smIt->more() )
588 buildPropagationChain( smIt->next() );
592 } // switch by SubMeshState