1 // Copyright (C) 2007-2008 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
22 // SMESH SMESH : implementaion of SMESH idl descriptions
23 // 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 void StdMeshers_Propagation::SetPropagationMgr(SMESH_subMesh* subMesh) { PropagationMgr::Set( subMesh ); }
102 * \brief Return an edge from which hypotheses are propagated from
104 TopoDS_Edge StdMeshers_Propagation::GetPropagationSource(SMESH_Mesh& theMesh,
105 const TopoDS_Shape& theEdge)
107 return PropagationMgr::GetSource(theMesh.GetSubMeshContaining( theEdge ));
110 //=============================================================================
111 //=============================================================================
112 // PROPAGATION MANAGEMENT
113 //=============================================================================
114 //=============================================================================
118 enum SubMeshState { WAIT_PROPAG_HYP, // propagation hyp or local 1D hyp is missing
119 HAS_PROPAG_HYP, // propag hyp on this submesh
120 IN_CHAIN, // submesh is in propagation chain
121 LAST_IN_CHAIN, // submesh with local 1D hyp breaking a chain
122 MEANINGLESS_LAST }; // meaningless
124 struct PropagationMgrData : public EventListenerData
126 bool myForward; //!< true if a curve of edge in chain is codirected with one of source edge
127 PropagationMgrData( SubMeshState state=WAIT_PROPAG_HYP ): EventListenerData(true) {
128 myType = state; myForward = true;
131 myType = WAIT_PROPAG_HYP; mySubMeshes.clear(); myForward = true;
133 SubMeshState State() const {
134 return (SubMeshState) myType;
136 void SetState(SubMeshState state) {
139 void SetSource(SMESH_subMesh* sm ) {
140 mySubMeshes.clear(); if ( sm ) mySubMeshes.push_back( sm );
142 void AddSource(SMESH_subMesh* sm ) {
143 if ( sm ) mySubMeshes.push_back( sm );
145 void SetChain(list< SMESH_subMesh* >& chain ) {
146 mySubMeshes.clear(); mySubMeshes.splice( mySubMeshes.end(), chain );
148 SMESH_subMeshIteratorPtr GetChain() const;
149 SMESH_subMesh* GetSource() const;
152 //=============================================================================
154 * \brief return static PropagationMgr
156 PropagationMgr* PropagationMgr::GetListener()
158 static PropagationMgr theListener;
161 PropagationMgr* getListener()
163 return PropagationMgr::GetListener();
165 //=============================================================================
167 * \brief return PropagationMgrData found on a submesh
169 PropagationMgrData* findData(SMESH_subMesh* sm)
172 return static_cast< PropagationMgrData* >( sm->GetEventListenerData( getListener() ));
175 //=============================================================================
177 * \brief return PropagationMgrData found on theEdge submesh
179 PropagationMgrData* findData(SMESH_Mesh& theMesh, const TopoDS_Shape& theEdge)
181 if ( theEdge.ShapeType() == TopAbs_EDGE )
182 return findData( theMesh.GetSubMeshContaining( theEdge ) );
185 //=============================================================================
187 * \brief return existing or a new PropagationMgrData
189 PropagationMgrData* getData(SMESH_subMesh* sm)
191 PropagationMgrData* data = findData( sm );
193 data = new PropagationMgrData();
194 sm->SetEventListener( getListener(), data, sm );
198 //=============================================================================
200 * \brief Returns a local 1D hypothesis used for theEdge
202 const SMESH_Hypothesis* getLocal1DHyp (SMESH_Mesh& theMesh,
203 const TopoDS_Shape& theEdge)
205 static SMESH_HypoFilter hypo;
206 hypo.Init( hypo.HasDim( 1 )).
207 AndNot ( hypo.IsAlgo() ).
208 AndNot ( hypo.IsAssignedTo( theMesh.GetMeshDS()->ShapeToMesh() ));
209 return theMesh.GetHypothesis( theEdge, hypo, true );
211 //=============================================================================
213 * \brief Returns a propagation hypothesis assigned to theEdge
215 const SMESH_Hypothesis* getProagationHyp (SMESH_Mesh& theMesh,
216 const TopoDS_Shape& theEdge)
218 static SMESH_HypoFilter propagHypFilter
219 ( SMESH_HypoFilter::HasName( StdMeshers_Propagation::GetName ()));
220 return theMesh.GetHypothesis( theEdge, propagHypFilter, true );
222 //================================================================================
224 * \brief Return an iterator on a list of submeshes
226 SMESH_subMeshIteratorPtr iterate( list<SMESH_subMesh*>::const_iterator from,
227 list<SMESH_subMesh*>::const_iterator to)
229 typedef SMESH_subMesh* TsubMesh;
230 typedef SMDS_SetIterator< TsubMesh, list< TsubMesh >::const_iterator > TIterator;
231 return SMESH_subMeshIteratorPtr ( new TIterator( from, to ));
233 //================================================================================
235 * \brief Build propagation chain
236 * \param theMainSubMesh - the submesh with Propagation hypothesis
238 bool buildPropagationChain ( SMESH_subMesh* theMainSubMesh )
240 DBGMSG( "buildPropagationChain from " << theMainSubMesh->GetId() );
241 const TopoDS_Shape& theMainEdge = theMainSubMesh->GetSubShape();
242 if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
244 SMESH_Mesh* mesh = theMainSubMesh->GetFather();
246 PropagationMgrData* chainData = getData( theMainSubMesh );
247 chainData->SetState( HAS_PROPAG_HYP );
249 // Edge submeshes, to which the 1D hypothesis will be propagated from theMainEdge
250 list<SMESH_subMesh*> & chain = chainData->mySubMeshes;
252 chain.push_back( theMainSubMesh );
254 TopTools_MapOfShape checkedShapes;
255 checkedShapes.Add( theMainEdge );
257 list<SMESH_subMesh*>::iterator smIt = chain.begin();
258 for ( ; smIt != chain.end(); ++smIt )
260 const TopoDS_Edge& anE = TopoDS::Edge( (*smIt)->GetSubShape() );
261 PropagationMgrData* data = findData( *smIt );
262 if ( !data ) continue;
264 // Iterate on faces, having edge <anE>
265 TopTools_ListIteratorOfListOfShape itA (mesh->GetAncestors(anE));
266 for (; itA.More(); itA.Next())
268 // there are objects of different type among the ancestors of edge
269 if ( itA.Value().ShapeType() != TopAbs_WIRE || !checkedShapes.Add( itA.Value() ))
272 // Get ordered edges and find index of anE in a sequence
273 BRepTools_WireExplorer aWE (TopoDS::Wire(itA.Value()));
274 vector<TopoDS_Edge> edges;
277 for (; aWE.More(); aWE.Next()) {
278 TopoDS_Edge edge = aWE.Current();
279 edge.Orientation( aWE.Orientation() );
280 if ( edge.IsSame( anE ))
281 edgeIndex = edges.size();
282 edges.push_back( edge );
285 // Find an edge opposite to anE
287 if ( edges.size() < 4 ) {
288 continue; // too few edges
290 else if ( edges.size() == 4 ) {
291 int oppIndex = edgeIndex + 2;
292 if ( oppIndex > 3 ) oppIndex -= 4;
293 anOppE = edges[ oppIndex ];
297 TopoDS_Edge prevEdge = anE;
298 int nbSide = 0, eIndex = edgeIndex + 1;
299 for ( int i = 0; i < edges.size(); ++i, ++eIndex )
301 if ( eIndex == edges.size() )
303 if ( !SMESH_Algo::IsContinuous( prevEdge, edges[ eIndex ])) {
307 // check that anE is not a part of a composite side
308 if ( anE.IsSame( prevEdge ) || anE.IsSame( edges[ eIndex ])) {
309 anOppE.Nullify(); break;
312 if ( nbSide == 2 ) { // opposite side
313 if ( !anOppE.IsNull() ) {
314 // composite opposite side -> stop propagation
315 anOppE.Nullify(); break;
317 anOppE = edges[ eIndex ];
320 anOppE.Nullify(); break; // too many sides
322 prevEdge = edges[ eIndex ];
324 if ( anOppE.IsNull() )
327 DBGMSG( nbSide << " sides in wire #" << mesh->GetMeshDS()->ShapeToIndex( itA.Value() ) << " - SKIP" );
331 if ( anOppE.IsNull() || !checkedShapes.Add( anOppE ))
333 SMESH_subMesh* oppSM = mesh->GetSubMesh( anOppE );
334 PropagationMgrData* oppData = getData( oppSM );
336 // Add anOppE to aChain if ...
337 if ( oppData->State() == WAIT_PROPAG_HYP ) // ... anOppE is not in any chain
339 oppData->SetSource( theMainSubMesh );
340 if ( !getLocal1DHyp( *mesh, anOppE )) // ... no 1d hyp on anOppE
342 oppData->myForward = data->myForward;
343 if ( edges[ edgeIndex ].Orientation() == anOppE.Orientation() )
344 oppData->myForward = !oppData->myForward;
345 chain.push_back( oppSM );
346 oppSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
347 oppData->SetState( IN_CHAIN );
348 DBGMSG( "set IN_CHAIN on " << oppSM->GetId() );
351 oppData->SetState( LAST_IN_CHAIN );
352 DBGMSG( "set LAST_IN_CHAIN on " << oppSM->GetId() );
355 else if ( oppData->State() == LAST_IN_CHAIN ) // anOppE breaks other chain
357 DBGMSG( "encounters LAST_IN_CHAIN on " << oppSM->GetId() );
358 oppData->AddSource( theMainSubMesh );
360 } // loop on face ancestors
361 } // loop on the chain
363 // theMainSubMesh must not be in a chain
368 //================================================================================
370 * \brief Clear propagation chain
372 bool clearPropagationChain( SMESH_subMesh* subMesh )
374 DBGMSG( "clearPropagationChain from " << subMesh->GetId() );
375 if ( PropagationMgrData* data = findData( subMesh ))
377 switch ( data->State() ) {
379 return clearPropagationChain( data->GetSource() );
381 case HAS_PROPAG_HYP: {
382 SMESH_subMeshIteratorPtr smIt = data->GetChain();
383 while ( smIt->more() ) {
384 SMESH_subMesh* sm = smIt->next();
385 getData( sm )->Init();
386 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
391 case LAST_IN_CHAIN: {
392 SMESH_subMeshIteratorPtr smIt = iterate( data->mySubMeshes.begin(),
393 data->mySubMeshes.end());
394 while ( smIt->more() )
395 clearPropagationChain( smIt->next() );
407 //================================================================================
409 * \brief Return an iterator on chain submeshes
411 //================================================================================
413 SMESH_subMeshIteratorPtr PropagationMgrData::GetChain() const
417 return iterate( mySubMeshes.begin(), mySubMeshes.end() );
419 if ( mySubMeshes.empty() ) break;
420 return getData( mySubMeshes.front() )->GetChain();
423 return iterate( mySubMeshes.end(), mySubMeshes.end() );
425 //================================================================================
427 * \brief Return a propagation source submesh
429 //================================================================================
431 SMESH_subMesh* PropagationMgrData::GetSource() const
433 if ( myType == IN_CHAIN )
434 if ( !mySubMeshes.empty() )
435 return mySubMeshes.front();
440 //================================================================================
444 //================================================================================
446 PropagationMgr::PropagationMgr()
447 : SMESH_subMeshEventListener( false ) // won't be deleted by submesh
449 //================================================================================
451 * \brief Set PropagationMgr on a submesh
453 //================================================================================
455 void PropagationMgr::Set(SMESH_subMesh * submesh)
457 DBGMSG( "PropagationMgr::Set() on " << submesh->GetId() );
458 EventListenerData* data = new PropagationMgrData();
459 submesh->SetEventListener( getListener(), data, submesh );
461 const SMESH_Hypothesis * propagHyp =
462 getProagationHyp( *submesh->GetFather(), submesh->GetSubShape() );
464 getListener()->ProcessEvent( SMESH_subMesh::ADD_HYP,
465 SMESH_subMesh::ALGO_EVENT,
470 //================================================================================
472 * \brief Return an edge from which hypotheses are propagated
474 //================================================================================
476 TopoDS_Edge PropagationMgr::GetSource(SMESH_subMesh * submesh)
478 if ( PropagationMgrData* data = findData( submesh )) {
479 if ( data->State() == IN_CHAIN ) {
480 if ( SMESH_subMesh* sm = data->GetSource() )
482 TopoDS_Shape edge = sm->GetSubShape();
483 edge = edge.Oriented( data->myForward ? TopAbs_FORWARD : TopAbs_REVERSED );
484 DBGMSG( " GetSource() = edge " << sm->GetId() << " REV = " << (!data->myForward));
485 if ( edge.ShapeType() == TopAbs_EDGE )
486 return TopoDS::Edge( edge );
490 return TopoDS_Edge();
492 //================================================================================
494 * \brief React on events on 1D submeshes
496 //================================================================================
498 void PropagationMgr::ProcessEvent(const int event,
500 SMESH_subMesh* subMesh,
501 SMESH_subMeshEventListenerData* listenerData,
502 const SMESH_Hypothesis* hyp)
506 if ( !hyp || hyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO || hyp->GetDim() != 1 )
508 if ( eventType != SMESH_subMesh::ALGO_EVENT )
510 DBGMSG( "PropagationMgr::ProcessEvent() on " << subMesh->GetId() );
512 bool isPropagHyp = ( StdMeshers_Propagation::GetName() == hyp->GetName() );
514 PropagationMgrData* data = static_cast<PropagationMgrData*>( listenerData );
515 switch ( data->State() ) {
517 case WAIT_PROPAG_HYP: { // propagation hyp or local 1D hyp is missing
518 // --------------------------------------------------------
519 bool hasPropagHyp = ( isPropagHyp ||
520 getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) );
523 bool hasLocal1DHyp = getLocal1DHyp( *subMesh->GetFather(), subMesh->GetSubShape());
524 if ( !hasLocal1DHyp )
526 if ( event == SMESH_subMesh::ADD_HYP ||
527 event == SMESH_subMesh::ADD_FATHER_HYP ) // add local or propagation hyp
529 DBGMSG( "ADD_HYP propagation to WAIT_PROPAG_HYP " << subMesh->GetId() );
530 // build propagation chain
531 buildPropagationChain( subMesh );
535 case HAS_PROPAG_HYP: { // propag hyp on this submesh
536 // --------------------------------------------------------
538 case SMESH_subMesh::REMOVE_HYP:
539 case SMESH_subMesh::REMOVE_FATHER_HYP: // remove propagation hyp
540 if ( isPropagHyp && !getProagationHyp( *subMesh->GetFather(), subMesh->GetSubShape()) )
542 DBGMSG( "REMOVE_HYP propagation from HAS_PROPAG_HYP " << subMesh->GetId() );
543 // clear propagation chain
544 clearPropagationChain( subMesh );
546 // return; -- hyp is modified any way
548 //case SMESH_subMesh::MODIF_HYP: // hyp modif
549 // clear mesh in a chain
550 DBGMSG( "MODIF_HYP on HAS_PROPAG_HYP " << subMesh->GetId() );
551 SMESH_subMeshIteratorPtr smIt = data->GetChain();
552 while ( smIt->more() ) {
553 SMESH_subMesh* smInChain = smIt->next();
554 smInChain->AlgoStateEngine( SMESH_subMesh::MODIF_HYP,
555 (SMESH_Hypothesis*) hyp );
561 case IN_CHAIN: { // submesh is in propagation chain
562 // --------------------------------------------------------
563 if ( event == SMESH_subMesh::ADD_HYP ) { // add local hypothesis
564 if ( isPropagHyp ) { // propagation hyp added
565 DBGMSG( "ADD_HYP propagation on IN_CHAIN " << subMesh->GetId() );
566 // collision - do nothing
568 else { // 1D hyp added
569 // rebuild propagation chain
570 DBGMSG( "ADD_HYP 1D on IN_CHAIN " << subMesh->GetId() );
571 SMESH_subMesh* sourceSM = data->GetSource();
572 clearPropagationChain( sourceSM );
573 buildPropagationChain( sourceSM );
578 case LAST_IN_CHAIN: { // submesh with local 1D hyp, breaking a chain
579 // --------------------------------------------------------
580 if ( event == SMESH_subMesh::REMOVE_HYP ) { // remove local hyp
581 // rebuild propagation chain
582 DBGMSG( "REMOVE_HYP 1D from LAST_IN_CHAIN " << subMesh->GetId() );
583 list<SMESH_subMesh*> sourceSM = data->mySubMeshes;
584 clearPropagationChain( subMesh );
585 SMESH_subMeshIteratorPtr smIt = iterate( sourceSM.begin(), sourceSM.end());
586 while ( smIt->more() )
587 buildPropagationChain( smIt->next() );
591 } // switch by SubMeshState