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