Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / StdMeshers / StdMeshers_CompositeSegment_1D.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_CompositeSegment_1D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_CompositeSegment_1D.hxx"
28 #include "StdMeshers_FaceSide.hxx"
29 #include "StdMeshers_AutomaticLength.hxx"
30
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESH_subMesh.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36 #include "SMESH_Comment.hxx"
37
38 #include "SMDS_MeshElement.hxx"
39 #include "SMDS_MeshNode.hxx"
40
41 #include "utilities.h"
42
43 #include <BRepAdaptor_CompCurve.hxx>
44 #include <BRep_Builder.hxx>
45 #include <GCPnts_AbscissaPoint.hxx>
46 #include <TopExp.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49 #include <TopTools_MapOfShape.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Vertex.hxx>
53 #include <TopoDS_Wire.hxx>
54 #include <gp_Pnt.hxx>
55
56 #include <Standard_ErrorHandler.hxx>
57 #include <Standard_Failure.hxx>
58
59 typedef SMESH_Comment TComm;
60
61 using namespace std;
62
63
64 namespace {
65
66   //================================================================================
67   /*!
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
71    */
72   //================================================================================
73
74   TopoDS_Edge nextC1Edge(TopoDS_Edge  edge,
75                          SMESH_Mesh & aMesh,
76                          const bool   forward)
77   {
78     if (edge.Orientation() > TopAbs_REVERSED) // INTERNAL
79       edge.Orientation( TopAbs_FORWARD );
80     TopoDS_Edge eNext;
81     TopTools_MapOfShape edgeCounter;
82     edgeCounter.Add( edge );
83     TopoDS_Vertex v = forward ? TopExp::LastVertex(edge,true) : TopExp::FirstVertex(edge,true);
84     TopTools_ListIteratorOfListOfShape ancestIt = aMesh.GetAncestors( v );
85     for ( ; ancestIt.More(); ancestIt.Next() )
86     {
87       const TopoDS_Shape & ancestor = ancestIt.Value();
88       if ( ancestor.ShapeType() == TopAbs_EDGE && edgeCounter.Add( ancestor ))
89         eNext = TopoDS::Edge( ancestor );
90     }
91     if ( edgeCounter.Extent() < 3 && !eNext.IsNull() ) {
92       if ( SMESH_Algo::IsContinuous( edge, eNext )) {
93         // care of orientation
94         if (eNext.Orientation() > TopAbs_REVERSED) // INTERNAL
95           eNext.Orientation( TopAbs_FORWARD );
96         TopoDS_Vertex vn =
97           forward ? TopExp::FirstVertex(eNext,true) : TopExp::LastVertex(eNext,true);
98         bool reverse = (!v.IsSame(vn));
99         if ( reverse )
100           eNext.Reverse();
101         return eNext;
102       }
103     }
104     return TopoDS_Edge();
105   }
106
107   //================================================================================
108   /*!
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
111    */
112   //================================================================================
113
114   void careOfSubMeshes( StdMeshers_FaceSide& side, EventListener* eListener)
115   {
116     if ( side.NbEdges() < 2)
117       return;
118     for ( int iE = 0; iE < side.NbEdges(); ++iE )
119     {
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 );
130       }
131       // add internal vertex submesh to the data
132       if ( iE )
133       {
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 );
140       }
141     }
142   }
143
144   //================================================================================
145   /*!
146    * \brief Class used to restore nodes on internal vertices of a complex side
147    *  when StdMeshers_CompositeSegment_1D algorithm is removed
148    */
149   //================================================================================
150
151   struct VertexNodesRestoringListener : public SMESH_subMeshEventListener
152   {
153     VertexNodesRestoringListener():
154       SMESH_subMeshEventListener(0, // won't be deleted by submesh
155                                  "StdMeshers_CompositeSegment_1D::VertexNodesRestoringListener")
156     {}
157   /*!
158    * \brief Restore nodes on internal vertices of a complex side
159    * \param event - algo_event or compute_event itself (of SMESH_subMesh)
160    * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
161    * \param subMesh - the submesh where the event occures
162    * \param data - listener data stored in the subMesh
163    * \param hyp - hypothesis, if eventType is algo_event
164    */
165     void ProcessEvent(const int          event,
166                       const int          eventType,
167                       SMESH_subMesh*     subMesh,
168                       EventListenerData* data,
169                       const SMESH_Hypothesis*  /*hyp*/)
170     {
171       if ( data && eventType == SMESH_subMesh::ALGO_EVENT )
172       {
173         bool hypRemoved;
174         if ( subMesh->GetAlgoState() != SMESH_subMesh::HYP_OK )
175           hypRemoved = true;
176         else {
177           SMESH_Algo* algo = subMesh->GetAlgo();
178           hypRemoved = ( string( algo->GetName() ) != StdMeshers_CompositeSegment_1D::AlgoName());
179         }
180         if ( hypRemoved )
181         {
182           list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
183           for ( ; smIt != data->mySubMeshes.end(); ++smIt )
184             if ( SMESH_subMesh* sm = *smIt ) {
185               sm->SetIsAlwaysComputed( false );
186               sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
187             }
188         }
189       }
190       // at study restoration:
191       // check if edge submesh must have _alwaysComputed flag
192       else if ( event     == SMESH_subMesh::SUBMESH_RESTORED &&
193                 eventType == SMESH_subMesh::COMPUTE_EVENT )
194       {
195         if ( !subMesh->GetEventListenerData( this )) { // not yet checked
196           SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
197           if ( meshDS->NbNodes() > 0 ) {
198             // check if there are nodes on all vertices
199             bool hasNodesOnVerext = true;
200             SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
201             while ( hasNodesOnVerext && smIt->more() ) {
202               SMESH_subMesh* sm = smIt->next();
203               hasNodesOnVerext = ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->NbNodes() );
204             }
205             if ( !hasNodesOnVerext ) {
206               // check if an edge is a part of a complex side
207               TopoDS_Face face;
208               TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
209               auto_ptr< StdMeshers_FaceSide > side
210                 ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),
211                                                               edge, face, false ));
212               if ( side->NbEdges() > 1 && side->NbSegments() )
213                 careOfSubMeshes( *side, this );
214             }
215           }
216         }
217       }
218     }
219   }; // struct VertexNodesRestoringListener
220 }
221
222 //=============================================================================
223 /*!
224  *  
225  */
226 //=============================================================================
227
228 StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D(int         hypId,
229                                                                int         studyId,
230                                                                SMESH_Gen * gen)
231   :StdMeshers_Regular_1D(hypId, studyId, gen)
232 {
233   MESSAGE("StdMeshers_CompositeSegment_1D::StdMeshers_CompositeSegment_1D");
234   _name = AlgoName();
235   _EventListener = new VertexNodesRestoringListener();
236 }
237
238 //=======================================================================
239 //function : AlgoName
240 //purpose  : Returns algo type name
241 //=======================================================================
242
243 std::string StdMeshers_CompositeSegment_1D::AlgoName()
244 {
245   return "CompositeSegment_1D";
246 }
247 //=============================================================================
248 /*!
249  *  
250  */
251 //=============================================================================
252
253 StdMeshers_CompositeSegment_1D::~StdMeshers_CompositeSegment_1D()
254 {
255   delete _EventListener;
256 }
257
258 //=============================================================================
259 /*!
260  * \brief Sets event listener to submeshes if necessary
261  * \param subMesh - submesh where algo is set
262  *
263  * This method is called when a submesh gets HYP_OK algo_state.
264  * After being set, event listener is notified on each event of a submesh.
265  */
266 //=============================================================================
267
268 void StdMeshers_CompositeSegment_1D::SetEventListener(SMESH_subMesh* subMesh)
269 {
270   // issue 0020279. Set "_alwaysComputed" flag to the submeshes of internal
271   // vertices of composite edge in order to avoid creation of vertices on
272   // them for the sake of stability.
273
274   // check if "_alwaysComputed" is not yet set
275   bool isAlwaysComputed = false;
276   SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
277   while ( !isAlwaysComputed && smIt->more() )
278     isAlwaysComputed = smIt->next()->IsAlwaysComputed();
279
280   if ( !isAlwaysComputed )
281   {
282     // check if an edge is a part of a complex side
283     TopoDS_Face face;
284     TopoDS_Edge edge = TopoDS::Edge( subMesh->GetSubShape() );
285     auto_ptr< StdMeshers_FaceSide > side
286       ( StdMeshers_CompositeSegment_1D::GetFaceSide(*subMesh->GetFather(),edge, face, false ));
287     if ( side->NbEdges() > 1 ) { // complex
288
289       // set _alwaysComputed to vertices
290       for ( int iE = 1; iE < side->NbEdges(); ++iE )
291       {
292         TopoDS_Vertex V = side->FirstVertex( iE );
293         SMESH_subMesh* sm = side->GetMesh()->GetSubMesh( V );
294         sm->SetIsAlwaysComputed( true );
295       }
296     }
297   }
298   // set listener that will remove _alwaysComputed from submeshes at algorithm change
299   subMesh->SetEventListener( _EventListener, 0, subMesh);
300   StdMeshers_Regular_1D::SetEventListener( subMesh );
301 }
302
303 //=============================================================================
304 /*!
305  * \brief Return a face side the edge belongs to
306  */
307 //=============================================================================
308
309 StdMeshers_FaceSide *
310 StdMeshers_CompositeSegment_1D::GetFaceSide(SMESH_Mesh&        aMesh,
311                                             const TopoDS_Edge& anEdge,
312                                             const TopoDS_Face& aFace,
313                                             const bool         ignoreMeshed)
314 {
315   list< TopoDS_Edge > edges;
316   if ( anEdge.Orientation() <= TopAbs_REVERSED )
317     edges.push_back( anEdge );
318   else
319     edges.push_back( TopoDS::Edge( anEdge.Oriented( TopAbs_FORWARD ))); // PAL21718
320
321   list <const SMESHDS_Hypothesis *> hypList;
322   SMESH_Algo* theAlgo = aMesh.GetGen()->GetAlgo( aMesh, anEdge );
323   if ( theAlgo ) hypList = theAlgo->GetUsedHypothesis(aMesh, anEdge, false);
324   for ( int forward = 0; forward < 2; ++forward )
325   {
326     TopoDS_Edge eNext = nextC1Edge( edges.back(), aMesh, forward );
327     while ( !eNext.IsNull() ) {
328       if ( ignoreMeshed ) {
329         // eNext must not have computed mesh
330         if ( SMESHDS_SubMesh* sm = aMesh.GetMeshDS()->MeshElements(eNext) )
331           if ( sm->NbNodes() || sm->NbElements() )
332             break;
333       }
334       // eNext must have same hypotheses
335       SMESH_Algo* algo = aMesh.GetGen()->GetAlgo( aMesh, eNext );
336       if ( !algo ||
337            string(theAlgo->GetName()) != algo->GetName() ||
338            hypList != algo->GetUsedHypothesis(aMesh, eNext, false))
339         break;
340       if ( std::find( edges.begin(), edges.end(), eNext ) != edges.end() )
341         break;
342       if ( forward )
343         edges.push_back( eNext );
344       else
345         edges.push_front( eNext );
346       eNext = nextC1Edge( eNext, aMesh, forward );
347     }
348   }
349   return new StdMeshers_FaceSide( aFace, edges, &aMesh, true, false );
350 }
351
352 //=============================================================================
353 /*!
354  *  
355  */
356 //=============================================================================
357
358 bool StdMeshers_CompositeSegment_1D::Compute(SMESH_Mesh &         aMesh,
359                                              const TopoDS_Shape & aShape)
360 {
361   TopoDS_Edge edge = TopoDS::Edge( aShape );
362   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
363
364   // Get edges to be discretized as a whole
365   TopoDS_Face nullFace;
366   auto_ptr< StdMeshers_FaceSide > side( GetFaceSide(aMesh, edge, nullFace, true ));
367   //side->dump("IN COMPOSITE SEG");
368
369   if ( side->NbEdges() < 2 )
370     return StdMeshers_Regular_1D::Compute( aMesh, aShape );
371
372   // update segment lenght computed by StdMeshers_AutomaticLength
373   const list <const SMESHDS_Hypothesis * > & hyps = GetUsedHypothesis(aMesh, aShape);
374   if ( !hyps.empty() ) {
375     StdMeshers_AutomaticLength * autoLenHyp = const_cast<StdMeshers_AutomaticLength *>
376       (dynamic_cast <const StdMeshers_AutomaticLength * >(hyps.front()));
377     if ( autoLenHyp )
378       _value[ BEG_LENGTH_IND ]= autoLenHyp->GetLength( &aMesh, side->Length() );
379   }
380
381   // Compute node parameters
382   auto_ptr< BRepAdaptor_CompCurve > C3d ( side->GetCurve3d() );
383   double f = C3d->FirstParameter(), l = C3d->LastParameter();
384   list< double > params;
385   if ( !computeInternalParameters ( aMesh, *C3d, side->Length(), f, l, params, false ))
386     return false;
387
388   // Redistribute parameters near ends
389   TopoDS_Vertex VFirst = side->FirstVertex();
390   TopoDS_Vertex VLast  = side->LastVertex();
391   redistributeNearVertices( aMesh, *C3d, side->Length(), params, VFirst, VLast );
392
393   params.push_front(f);
394   params.push_back(l);
395   int nbNodes = params.size();
396
397   // Create mesh
398
399   const SMDS_MeshNode * nFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
400   const SMDS_MeshNode * nLast  = SMESH_Algo::VertexNode( VLast, meshDS );
401   if (!nFirst)
402     return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
403                  <<meshDS->ShapeToIndex(VFirst));
404   if (!nLast)
405     return error(COMPERR_BAD_INPUT_MESH, TComm("No node on vertex ")
406                  <<meshDS->ShapeToIndex(VLast));
407
408   vector<const SMDS_MeshNode*> nodes( nbNodes, (const SMDS_MeshNode*)0 );
409   nodes.front() = nFirst;
410   nodes.back()  = nLast;
411
412   // create internal nodes
413   list< double >::iterator parIt = params.begin();
414   double prevPar = *parIt;
415   Standard_Real u;
416   for ( int iN = 0; parIt != params.end(); ++iN, ++parIt)
417   {
418     if ( !nodes[ iN ] ) {
419       gp_Pnt p = C3d->Value( *parIt );
420       SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
421       C3d->Edge( *parIt, edge, u );
422       meshDS->SetNodeOnEdge( n, edge, u );
423 //       cout << "new NODE: par="<<*parIt<<" ePar="<<u<<" e="<<edge.TShape().operator->()
424 //            << " " << n << endl;
425       nodes[ iN ] = n;
426     }
427     // create edges
428     if ( iN ) {
429       double mPar = ( prevPar + *parIt )/2;
430       if ( _quadraticMesh ) {
431         // create medium node
432         double segLen = GCPnts_AbscissaPoint::Length(*C3d, prevPar, *parIt);
433         GCPnts_AbscissaPoint ruler( *C3d, segLen/2., prevPar );
434         if ( ruler.IsDone() )
435           mPar = ruler.Parameter();
436         gp_Pnt p = C3d->Value( mPar );
437         SMDS_MeshNode* n = meshDS->AddNode( p.X(), p.Y(), p.Z());
438         //cout << "new NODE "<< n << endl;
439         meshDS->SetNodeOnEdge( n, edge, u );
440         SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ], n);
441         meshDS->SetMeshElementOnShape(seg, edge);
442       }
443       else {
444         C3d->Edge( mPar, edge, u );
445         SMDS_MeshEdge * seg = meshDS->AddEdge(nodes[ iN-1 ], nodes[ iN ]);
446         meshDS->SetMeshElementOnShape(seg, edge);
447       }
448     }
449     prevPar = *parIt;
450   }
451
452   // remove nodes on internal vertices
453   for ( int iE = 1; iE < side->NbEdges(); ++iE )
454   {
455     TopoDS_Vertex V = side->FirstVertex( iE );
456     while ( const SMDS_MeshNode * n = SMESH_Algo::VertexNode( V, meshDS ))
457       meshDS->RemoveNode( n );
458   }
459
460   // Update submeshes state for all edges and internal vertices,
461   // make them look computed even if none edge or node is set on them
462   careOfSubMeshes( *side, _EventListener );
463
464   return true;
465 }