Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / StdMeshers / StdMeshers_Regular_1D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : StdMeshers_Regular_1D.cxx
25 //           Moved here from SMESH_Regular_1D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 using namespace std;
31
32 #include "StdMeshers_Regular_1D.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_Mesh.hxx"
35
36 #include "StdMeshers_LocalLength.hxx"
37 #include "StdMeshers_NumberOfSegments.hxx"
38 #include "StdMeshers_Arithmetic1D.hxx"
39 #include "StdMeshers_StartEndLength.hxx"
40 #include "StdMeshers_Deflection1D.hxx"
41 #include "StdMeshers_Propagation.hxx"
42
43 #include "SMDS_MeshElement.hxx"
44 #include "SMDS_MeshNode.hxx"
45 #include "SMDS_EdgePosition.hxx"
46
47 #include "utilities.h"
48
49 #include <BRep_Tool.hxx>
50 #include <BRepTools_WireExplorer.hxx>
51
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopTools_Array1OfShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
56
57 #include <GeomAdaptor_Curve.hxx>
58 #include <GCPnts_AbscissaPoint.hxx>
59 #include <GCPnts_UniformAbscissa.hxx>
60 #include <GCPnts_UniformDeflection.hxx>
61
62 #include <Standard_ErrorHandler.hxx>
63 #include <Precision.hxx>
64
65 #include <string>
66 //#include <algorithm>
67
68 //=============================================================================
69 /*!
70  *
71  */
72 //=============================================================================
73
74 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
75         SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
76 {
77         MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
78         _name = "Regular_1D";
79         _shapeType = (1 << TopAbs_EDGE);
80
81         _compatibleHypothesis.push_back("LocalLength");
82         _compatibleHypothesis.push_back("NumberOfSegments");
83         _compatibleHypothesis.push_back("StartEndLength");
84         _compatibleHypothesis.push_back("Deflection1D");
85         _compatibleHypothesis.push_back("Arithmetic1D");
86 }
87
88 //=============================================================================
89 /*!
90  *
91  */
92 //=============================================================================
93
94 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
95 {
96 }
97
98 //=============================================================================
99 /*!
100  *
101  */
102 //=============================================================================
103
104 bool StdMeshers_Regular_1D::CheckHypothesis
105                          (SMESH_Mesh& aMesh,
106                           const TopoDS_Shape& aShape,
107                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
108 {
109   _hypType = NONE;
110
111   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
112   if (hyps.size() == 0)
113   {
114     aStatus = SMESH_Hypothesis::HYP_MISSING;
115     return false;  // can't work without a hypothesis
116   }
117
118   // use only the first hypothesis
119   const SMESHDS_Hypothesis *theHyp = hyps.front();
120
121   string hypName = theHyp->GetName();
122
123   if (hypName == "LocalLength")
124   {
125     const StdMeshers_LocalLength * hyp =
126       dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
127     ASSERT(hyp);
128     _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
129     ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
130     _hypType = LOCAL_LENGTH;
131     aStatus = SMESH_Hypothesis::HYP_OK;
132   }
133
134   else if (hypName == "NumberOfSegments")
135   {
136     const StdMeshers_NumberOfSegments * hyp =
137       dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
138     ASSERT(hyp);
139     _value[ NB_SEGMENTS_IND  ] = hyp->GetNumberOfSegments();
140     _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
141     ASSERT( _value[ NB_SEGMENTS_IND ] > 0 );
142     _hypType = NB_SEGMENTS;
143     aStatus = SMESH_Hypothesis::HYP_OK;
144   }
145
146   else if (hypName == "Arithmetic1D")
147   {
148     const StdMeshers_Arithmetic1D * hyp =
149       dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
150     ASSERT(hyp);
151     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
152     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
153     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
154     _hypType = ARITHMETIC_1D;
155     aStatus = SMESH_Hypothesis::HYP_OK;
156   }
157
158   else if (hypName == "StartEndLength")
159   {
160     const StdMeshers_StartEndLength * hyp =
161       dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
162     ASSERT(hyp);
163     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
164     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
165     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
166     _hypType = BEG_END_LENGTH;
167     aStatus = SMESH_Hypothesis::HYP_OK;
168   }
169
170   else if (hypName == "Deflection1D")
171   {
172     const StdMeshers_Deflection1D * hyp =
173       dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
174     ASSERT(hyp);
175     _value[ DEFLECTION_IND ] = hyp->GetDeflection();
176     ASSERT( _value[ DEFLECTION_IND ] > 0 );
177     _hypType = DEFLECTION;
178     aStatus = SMESH_Hypothesis::HYP_OK;
179   }
180   else
181     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
182
183   return ( _hypType != NONE );
184 }
185
186 //=============================================================================
187 /*!
188  *
189  */
190 //=============================================================================
191 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
192                                                       list<double> &     theParams) const
193 {
194   theParams.clear();
195
196   double f, l;
197   Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
198   GeomAdaptor_Curve C3d(Curve);
199
200   double length = EdgeLength(theEdge);
201   //SCRUTE(length);
202
203   switch( _hypType )
204   {
205   case LOCAL_LENGTH:
206   case NB_SEGMENTS: {
207
208     double eltSize = 1;
209     if ( _hypType == LOCAL_LENGTH )
210     {
211       double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
212       if (nbseg <= 0)
213         nbseg = 1;                        // degenerated edge
214       eltSize = length / nbseg;
215     }
216     else
217     {
218       double epsilon = 0.001;
219       if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
220       {
221         double alpha =
222           pow( _value[ SCALE_FACTOR_IND ], 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
223         double factor =
224           length / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
225
226         int i, NbPoints = (int) _value[ NB_SEGMENTS_IND ];
227         for ( int i = 2; i < NbPoints; i++ )
228         {
229           double param = factor * (1 - pow(alpha, i - 1));
230           theParams.push_back( param );
231         }
232         return true;
233       }
234       else
235       {
236         eltSize = length / _value[ NB_SEGMENTS_IND ];
237       }
238     }
239
240     GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
241     if ( !Discret.IsDone() )
242       return false;
243
244     int NbPoints = Discret.NbPoints();
245     for ( int i = 2; i < NbPoints; i++ )
246     {
247       double param = Discret.Parameter(i);
248       theParams.push_back( param );
249     }
250     return true;
251   }
252
253   case BEG_END_LENGTH: {
254
255     // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
256
257     double a1 = _value[ BEG_LENGTH_IND ];
258     double an = _value[ END_LENGTH_IND ];
259     double q  = ( length - a1 ) / ( length - an );
260
261     double U1 = Min ( f, l );
262     double Un = Max ( f, l );
263     double param = U1;
264     double eltSize = a1;
265     while ( 1 ) {
266       // computes a point on a curve <C3d> at the distance <eltSize>
267       // from the point of parameter <param>.
268       GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
269       if ( !Discret.IsDone() ) break;
270       param = Discret.Parameter();
271       if ( param < Un )
272         theParams.push_back( param );
273       else
274         break;
275       eltSize *= q;
276     }
277     if ( a1 + an < length ) {
278       // compensate error
279       double Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
280       double dLn = an - Ln;
281       if ( dLn < 0.5 * an )
282         dLn = -dLn;
283       else {
284         theParams.pop_back();
285         Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
286         dLn = an - Ln;
287         if ( dLn < 0.5 * an )
288           dLn = -dLn;
289       }
290       double dUn = dLn * ( Un - U1 ) / length;
291 //       SCRUTE( Ln );
292 //       SCRUTE( dLn );
293 //       SCRUTE( dUn );
294       list<double>::reverse_iterator itU = theParams.rbegin();
295       int i, n = theParams.size();
296       for ( i = 1 ; i < n; itU++, i++ ) {
297         (*itU) += dUn;
298         dUn /= q;
299       }
300     }
301
302     return true;
303   }
304
305   case DEFLECTION: {
306
307     GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
308     if ( !Discret.IsDone() )
309       return false;
310
311     int NbPoints = Discret.NbPoints();
312     for ( int i = 2; i < NbPoints; i++ )
313     {
314       double param = Discret.Parameter(i);
315       theParams.push_back( param );
316     }
317     return true;
318
319   }
320
321   case ARITHMETIC_1D: {
322         // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
323
324     double a1 = _value[ BEG_LENGTH_IND ];
325     double an = _value[ END_LENGTH_IND ];
326
327     double nd = (2 * length) / (an + a1) - 1;
328     int n = int(nd);
329     if(n != nd)
330       n++;
331
332     double q = ((2 * length) / (n + 1) - 2 * a1) / n;
333     double U1 = Min ( f, l );
334     double Un = Max ( f, l );
335     double param = U1;
336     double eltSize = a1;
337
338     double L=0;
339     while ( 1 ) {
340       L+=eltSize;
341       // computes a point on a curve <C3d> at the distance <eltSize>
342       // from the point of parameter <param>.
343       GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
344       if ( !Discret.IsDone() ) break;
345       param = Discret.Parameter();
346       if ( fabs(param - Un) > Precision::Confusion() && param < Un) {
347         theParams.push_back( param );
348       }
349       else
350         break;
351       eltSize += q;
352     }
353
354     return true;
355   }
356
357   default:;
358   }
359
360   return false;
361 }
362
363 //=============================================================================
364 /*!
365  *
366  */
367 //=============================================================================
368
369 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
370 {
371   MESSAGE("StdMeshers_Regular_1D::Compute");
372
373   if ( _hypType == NONE )
374     return false;
375
376   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
377   SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
378
379   const TopoDS_Edge & EE = TopoDS::Edge(aShape);
380   TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
381
382   double f, l;
383   Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
384
385   TopoDS_Vertex VFirst, VLast;
386   TopExp::Vertices(E, VFirst, VLast);   // Vfirst corresponds to f and Vlast to l
387
388   ASSERT(!VFirst.IsNull());
389   SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
390   if (!lid->more())
391   {
392     MESSAGE (" NO NODE BUILT ON VERTEX ");
393     return false;
394   }
395   const SMDS_MeshNode * idFirst = lid->next();
396
397   ASSERT(!VLast.IsNull());
398   lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
399   if (!lid->more())
400   {
401     MESSAGE (" NO NODE BUILT ON VERTEX ");
402     return false;
403   }
404   const SMDS_MeshNode * idLast = lid->next();
405
406   if (!Curve.IsNull())
407   {
408     list< double > params;
409     try {
410       if ( ! computeInternalParameters( E, params ))
411         return false;
412     }
413     catch ( Standard_Failure ) {
414       return false;
415     }
416
417     // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
418     // only internal nodes receive an edge position with param on curve
419
420     const SMDS_MeshNode * idPrev = idFirst;
421
422     for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
423     {
424       double param = *itU;
425       gp_Pnt P = Curve->Value(param);
426
427       //Add the Node in the DataStructure
428       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
429       meshDS->SetNodeOnEdge(node, E);
430
431       // **** edgePosition associe au point = param.
432       SMDS_EdgePosition* epos =
433         dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
434       epos->SetUParameter(param);
435
436       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
437       meshDS->SetMeshElementOnShape(edge, E);
438       idPrev = node;
439     }
440     SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
441     meshDS->SetMeshElementOnShape(edge, E);
442   }
443   else
444   {
445     // Edge is a degenerated Edge : We put n = 5 points on the edge.
446     int NbPoints = 5;
447     BRep_Tool::Range(E, f, l);
448     double du = (l - f) / (NbPoints - 1);
449     //MESSAGE("************* Degenerated edge! *****************");
450
451     TopoDS_Vertex V1, V2;
452     TopExp::Vertices(E, V1, V2);
453     gp_Pnt P = BRep_Tool::Pnt(V1);
454
455     const SMDS_MeshNode * idPrev = idFirst;
456     for (int i = 2; i < NbPoints; i++)
457     {
458       double param = f + (i - 1) * du;
459       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
460       meshDS->SetNodeOnEdge(node, E);
461
462       SMDS_EdgePosition* epos =
463         dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
464       epos->SetUParameter(param);
465
466       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
467       meshDS->SetMeshElementOnShape(edge, E);
468       idPrev = node;
469     }
470     SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
471     meshDS->SetMeshElementOnShape(edge, E);
472   }
473   return true;
474 }
475
476 //=============================================================================
477 /*!
478  *  GetUsedHypothesis
479  */
480 //=============================================================================
481
482 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis
483   (SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
484 {
485   _usedHypList.clear();
486   _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
487   int nbHyp = _usedHypList.size();
488
489   // try to find being propagated hypothesis
490   string propName = StdMeshers_Propagation::GetName();
491   if (nbHyp == 0) {
492     // Get all opposite edges
493     TopTools_ListOfShape anOppositeEdges;
494     TopoDS_Shape mainShape = aMesh.GetMeshDS()->ShapeToMesh();
495     GetOppositeEdges(mainShape, aShape, anOppositeEdges);
496     TopTools_ListIteratorOfListOfShape oppIt (anOppositeEdges);
497     for (; oppIt.More(); oppIt.Next()) {
498       const TopoDS_Shape& oppE = oppIt.Value();
499
500       // Find Propagation hypothesis on the opposite edge
501       if (IsPropagated(aMesh, oppE)) {
502
503         // Get hypothesis, used by the opposite edge
504         _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, oppE);
505         nbHyp = _usedHypList.size();
506         if (nbHyp == 1)
507           break;
508       }
509     }
510   }
511
512   // try to find relevant 1D hypothesis on ancestors
513   if (nbHyp == 0) {
514     TopTools_ListIteratorOfListOfShape ancIt (aMesh.GetAncestors(aShape));
515     for (; ancIt.More(); ancIt.Next()) {
516       const TopoDS_Shape& ancestor = ancIt.Value();
517       _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
518       nbHyp = _usedHypList.size();
519       if (nbHyp == 1)
520         break;
521     }
522   }
523
524   if (nbHyp > 1)
525     _usedHypList.clear(); //only one compatible hypothesis allowed
526   return _usedHypList;
527 }
528
529 //=============================================================================
530 /*!
531  *  Is Propagation hypothesis assigned to theShape or its ancestors
532  */
533 //=============================================================================
534 Standard_Boolean StdMeshers_Regular_1D::IsPropagated (SMESH_Mesh         & theMesh,
535                                                       const TopoDS_Shape & theShape)
536 {
537   const SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
538
539   // try to find Propagation hypothesis on theShape
540   const list<const SMESHDS_Hypothesis*> & listHyp = meshDS->GetHypothesis(theShape);
541
542   list<const SMESHDS_Hypothesis*>::const_iterator it = listHyp.begin();
543   for (; it != listHyp.end(); it++) {
544     const SMESHDS_Hypothesis *anHyp = *it;
545     if (anHyp->GetName() == StdMeshers_Propagation::GetName())
546       return Standard_True;
547   }
548
549   // try to find Propagation hypothesis on ancestors
550   TopTools_ListIteratorOfListOfShape ancIt (theMesh.GetAncestors(theShape));
551   for (; ancIt.More(); ancIt.Next()) {
552     const TopoDS_Shape& ancestor = ancIt.Value();
553     const list<const SMESHDS_Hypothesis*> & listAncHyp = meshDS->GetHypothesis(ancestor);
554
555     list<const SMESHDS_Hypothesis*>::const_iterator itAnc = listAncHyp.begin();
556     for (; itAnc != listAncHyp.end(); itAnc++) {
557       const SMESHDS_Hypothesis *anHyp = *itAnc;
558       if (anHyp->GetName() == StdMeshers_Propagation::GetName())
559         return Standard_True;
560     }
561   }
562
563   return Standard_False;
564 }
565
566 //=============================================================================
567 /*!
568  * GetOppositeEdges() - get all edges of theShape,
569  * laying on any quadrangle face in front of theEdge
570  */
571 //=============================================================================
572 void StdMeshers_Regular_1D::GetOppositeEdges (const TopoDS_Shape&   theShape,
573                                               const TopoDS_Shape&   theEdge,
574                                               TopTools_ListOfShape& theOppositeEdges) const
575 {
576   TopExp_Explorer aWires (theShape, TopAbs_WIRE);
577   for (; aWires.More(); aWires.Next()) {
578     const TopoDS_Shape& aWire = aWires.Current();
579     BRepTools_WireExplorer aWE (TopoDS::Wire(aWire));
580     Standard_Integer nb = 1, found = 0;
581     TopTools_Array1OfShape anEdges (1,4);
582     for (; aWE.More(); aWE.Next(), nb++) {
583       if (nb > 4) {
584         found = 0;
585         break;
586       }
587       anEdges(nb) = aWE.Current();
588       if (anEdges(nb).IsSame(theEdge))
589         found = nb;
590     }
591     if (nb == 5 && found > 0) {
592       Standard_Integer opp = found + 2;
593       if (opp > 4) opp -= 4;
594       theOppositeEdges.Append(anEdges(opp));
595     }
596   }
597 }
598
599 //=============================================================================
600 /*!
601  *
602  */
603 //=============================================================================
604
605 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
606 {
607   return save;
608 }
609
610 //=============================================================================
611 /*!
612  *
613  */
614 //=============================================================================
615
616 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
617 {
618   return load;
619 }
620
621 //=============================================================================
622 /*!
623  *
624  */
625 //=============================================================================
626
627 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
628 {
629   return hyp.SaveTo( save );
630 }
631
632 //=============================================================================
633 /*!
634  *
635  */
636 //=============================================================================
637
638 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
639 {
640   return hyp.LoadFrom( load );
641 }