Salome HOME
PAL8539. Fix case NB_SEGMENTS for scale != 1.0 and first edge parameter != 0.0
[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
42 #include "SMDS_MeshElement.hxx"
43 #include "SMDS_MeshNode.hxx"
44 #include "SMDS_EdgePosition.hxx"
45 #include "SMESH_subMesh.hxx"
46
47 #include "utilities.h"
48
49 #include <BRep_Tool.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <GeomAdaptor_Curve.hxx>
54 #include <GCPnts_AbscissaPoint.hxx>
55 #include <GCPnts_UniformAbscissa.hxx>
56 #include <GCPnts_UniformDeflection.hxx>
57 #include <Standard_ErrorHandler.hxx>
58 #include <Precision.hxx>
59
60 #include <string>
61 //#include <algorithm>
62
63 //=============================================================================
64 /*!
65  *  
66  */
67 //=============================================================================
68
69 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
70         SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
71 {
72         MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
73         _name = "Regular_1D";
74         _shapeType = (1 << TopAbs_EDGE);
75
76         _compatibleHypothesis.push_back("LocalLength");
77         _compatibleHypothesis.push_back("NumberOfSegments");
78         _compatibleHypothesis.push_back("StartEndLength");
79         _compatibleHypothesis.push_back("Deflection1D");
80         _compatibleHypothesis.push_back("Arithmetic1D");
81 }
82
83 //=============================================================================
84 /*!
85  *  
86  */
87 //=============================================================================
88
89 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
90 {
91 }
92
93 //=============================================================================
94 /*!
95  *  
96  */
97 //=============================================================================
98
99 bool StdMeshers_Regular_1D::CheckHypothesis
100                          (SMESH_Mesh& aMesh,
101                           const TopoDS_Shape& aShape,
102                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
103 {
104   _hypType = NONE;
105
106   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
107   if (hyps.size() == 0)
108   {
109     aStatus = SMESH_Hypothesis::HYP_MISSING;
110     return false;  // can't work without a hypothesis
111   }
112
113   // use only the first hypothesis
114   const SMESHDS_Hypothesis *theHyp = hyps.front();
115
116   string hypName = theHyp->GetName();
117
118   if (hypName == "LocalLength")
119   {
120     const StdMeshers_LocalLength * hyp =
121       dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
122     ASSERT(hyp);
123     _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
124     ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
125     _hypType = LOCAL_LENGTH;
126     aStatus = SMESH_Hypothesis::HYP_OK;
127   }
128
129   else if (hypName == "NumberOfSegments")
130   {
131     const StdMeshers_NumberOfSegments * hyp =
132       dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
133     ASSERT(hyp);
134     _value[ NB_SEGMENTS_IND  ] = hyp->GetNumberOfSegments();
135     _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
136     ASSERT( _value[ NB_SEGMENTS_IND ] > 0 );
137     _hypType = NB_SEGMENTS;
138     aStatus = SMESH_Hypothesis::HYP_OK;
139   }
140
141   else if (hypName == "Arithmetic1D")
142   {
143     const StdMeshers_Arithmetic1D * hyp =
144       dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
145     ASSERT(hyp);
146     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
147     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
148     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
149     _hypType = ARITHMETIC_1D;
150     aStatus = SMESH_Hypothesis::HYP_OK;
151   }
152
153   else if (hypName == "StartEndLength")
154   {
155     const StdMeshers_StartEndLength * hyp =
156       dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
157     ASSERT(hyp);
158     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
159     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
160     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
161     _hypType = BEG_END_LENGTH;
162     aStatus = SMESH_Hypothesis::HYP_OK;
163   }
164
165   else if (hypName == "Deflection1D")
166   {
167     const StdMeshers_Deflection1D * hyp =
168       dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
169     ASSERT(hyp);
170     _value[ DEFLECTION_IND ] = hyp->GetDeflection();
171     ASSERT( _value[ DEFLECTION_IND ] > 0 );
172     _hypType = DEFLECTION;
173     aStatus = SMESH_Hypothesis::HYP_OK;
174   }
175   else
176     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
177
178   return ( _hypType != NONE );
179 }
180
181 //=======================================================================
182 //function : compensateError
183 //purpose  : adjust theParams so that the last segment length == an
184 //=======================================================================
185
186 static void compensateError(double a1, double an,
187                             double U1, double Un,
188                             double             length,
189                             GeomAdaptor_Curve& C3d,
190                             list<double> &     theParams)
191 {
192   int i, nPar = theParams.size();
193   if ( a1 + an < length && nPar > 1 )
194   {
195     list<double>::reverse_iterator itU = theParams.rbegin();
196     double Ul = *itU++;
197     // dist from the last point to the edge end <Un>, it should be equal <an>
198     double Ln = GCPnts_AbscissaPoint::Length( C3d, Ul, Un );
199     double dLn = an - Ln; // error of <an>
200     if ( Abs( dLn ) <= Precision::Confusion() )
201       return;
202     double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
203     double dUn = dLn * Abs( Un - U1 ) / length; // parametric error of <an>
204     if ( dUn < 0.5 * dU ) { // last segment is a bit shorter than it should
205       dUn = -dUn; // move the last parameter to the edge beginning
206     }
207     else {  // last segment is much shorter than it should -> remove the last param and
208       theParams.pop_back(); nPar--; // move the rest points toward the edge end
209       Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
210       dUn = ( an - Ln ) * Abs( Un - U1 ) / length;
211       if ( dUn < 0.5 * dU )
212         dUn = -dUn;
213     }
214     if ( U1 > Un )
215       dUn = -dUn;
216     double q  = dUn / ( nPar - 1 );
217     for ( itU = theParams.rbegin(), i = 1; i < nPar; itU++, i++ ) {
218       (*itU) += dUn;
219       dUn -= q;
220     }
221   }
222 }
223
224 //=============================================================================
225 /*!
226  *  
227  */
228 //=============================================================================
229 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
230                                                       list<double> &     theParams,
231                                                       const bool         theReverse) const
232 {
233   theParams.clear();
234
235   double f, l;
236   Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
237   GeomAdaptor_Curve C3d(Curve);
238
239   double length = EdgeLength(theEdge);
240
241   switch( _hypType )
242   {
243   case LOCAL_LENGTH:
244   case NB_SEGMENTS: {
245
246     double eltSize = 1;
247     if ( _hypType == LOCAL_LENGTH )
248     {
249       double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
250       if (nbseg <= 0)
251         nbseg = 1;                        // degenerated edge
252       eltSize = length / nbseg;
253     }
254     else
255     {
256       double epsilon = 0.001;
257       if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
258       {
259         double scale = _value[ SCALE_FACTOR_IND ];
260         if ( theReverse )
261           scale = 1. / scale;
262         double alpha = pow( scale , 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
263         double factor = (l - f) / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
264
265         int i, NbPoints = 1 + (int) _value[ NB_SEGMENTS_IND ];
266         for ( i = 2; i < NbPoints; i++ )
267         {
268           double param = f + factor * (1 - pow(alpha, i - 1));
269           theParams.push_back( param );
270         }
271         return true;
272       }
273       else
274       {
275         eltSize = length / _value[ NB_SEGMENTS_IND ];
276       }
277     }
278
279     GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
280     if ( !Discret.IsDone() )
281       return false;
282
283     int NbPoints = Discret.NbPoints();
284     for ( int i = 2; i < NbPoints; i++ )
285     {
286       double param = Discret.Parameter(i);
287       theParams.push_back( param );
288     }
289     return true;
290   }
291
292   case BEG_END_LENGTH: {
293
294     // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
295
296     double a1 = _value[ BEG_LENGTH_IND ];
297     double an = _value[ END_LENGTH_IND ];
298     double q  = ( length - a1 ) / ( length - an );
299
300     double U1 = theReverse ? l : f;
301     double Un = theReverse ? f : l;
302     double param = U1;
303     double eltSize = theReverse ? -a1 : a1;
304     while ( 1 ) {
305       // computes a point on a curve <C3d> at the distance <eltSize>
306       // from the point of parameter <param>.
307       GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
308       if ( !Discret.IsDone() ) break;
309       param = Discret.Parameter();
310       if ( param > f && param < l )
311         theParams.push_back( param );
312       else
313         break;
314       eltSize *= q;
315     }
316     compensateError( a1, an, U1, Un, length, C3d, theParams );
317     return true;
318   }
319
320   case ARITHMETIC_1D: {
321
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  q = ( an - a1 ) / ( 2 *length/( a1 + an ) - 1 );
328     int     n = int( 1 + ( an - a1 ) / q );
329
330     double U1 = theReverse ? l : f;
331     double Un = theReverse ? f : l;
332     double param = U1;
333     double eltSize = a1;
334     if ( theReverse ) {
335       eltSize = -eltSize;
336       q = -q;
337     }
338     while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
339       // computes a point on a curve <C3d> at the distance <eltSize>
340       // from the point of parameter <param>.
341       GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
342       if ( !Discret.IsDone() ) break;
343       param = Discret.Parameter();
344       if ( param > f && param < l )
345         theParams.push_back( param );
346       else
347         break;
348       eltSize += q;
349     }
350     compensateError( a1, an, U1, Un, length, C3d, theParams );
351
352     return true;
353   }
354
355   case DEFLECTION: {
356
357     GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
358     if ( !Discret.IsDone() )
359       return false;
360
361     int NbPoints = Discret.NbPoints();
362     for ( int i = 2; i < NbPoints; i++ )
363     {
364       double param = Discret.Parameter(i);
365       theParams.push_back( param );
366     }
367     return true;
368     
369   }
370
371   default:;
372   }
373
374   return false;
375 }
376
377 //=============================================================================
378 /*!
379  *  
380  */
381 //=============================================================================
382
383 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
384 {
385   MESSAGE("StdMeshers_Regular_1D::Compute");
386
387   if ( _hypType == NONE )
388     return false;
389
390   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
391   aMesh.GetSubMesh(aShape);
392
393   const TopoDS_Edge & EE = TopoDS::Edge(aShape);
394   TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
395
396   double f, l;
397   Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
398
399   TopoDS_Vertex VFirst, VLast;
400   TopExp::Vertices(E, VFirst, VLast);   // Vfirst corresponds to f and Vlast to l
401
402   ASSERT(!VFirst.IsNull());
403   SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
404   if (!lid->more())
405   {
406     MESSAGE (" NO NODE BUILT ON VERTEX ");
407     return false;
408   }
409   const SMDS_MeshNode * idFirst = lid->next();
410
411   ASSERT(!VLast.IsNull());
412   lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
413   if (!lid->more())
414   {
415     MESSAGE (" NO NODE BUILT ON VERTEX ");
416     return false;
417   }
418   const SMDS_MeshNode * idLast = lid->next();
419
420   if (!Curve.IsNull())
421   {
422     list< double > params;
423     bool reversed = false;
424     if ( !_mainEdge.IsNull() )
425       reversed = aMesh.IsReversedInChain( EE, _mainEdge );
426     try {
427       if ( ! computeInternalParameters( E, params, reversed ))
428         return false;
429     }
430     catch ( Standard_Failure ) {
431       return false;
432     }
433
434     // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
435     // only internal nodes receive an edge position with param on curve
436
437     const SMDS_MeshNode * idPrev = idFirst;
438     
439     for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
440     {
441       double param = *itU;
442       gp_Pnt P = Curve->Value(param);
443
444       //Add the Node in the DataStructure
445       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
446       meshDS->SetNodeOnEdge(node, E);
447
448       // **** edgePosition associe au point = param. 
449       SMDS_EdgePosition* epos =
450         dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
451       epos->SetUParameter(param);
452
453       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
454       meshDS->SetMeshElementOnShape(edge, E);
455       idPrev = node;
456     }
457     SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
458     meshDS->SetMeshElementOnShape(edge, E);
459   }
460   else
461   {
462     // Edge is a degenerated Edge : We put n = 5 points on the edge.
463     int NbPoints = 5;
464     BRep_Tool::Range(E, f, l);
465     double du = (l - f) / (NbPoints - 1);
466     //MESSAGE("************* Degenerated edge! *****************");
467
468     TopoDS_Vertex V1, V2;
469     TopExp::Vertices(E, V1, V2);
470     gp_Pnt P = BRep_Tool::Pnt(V1);
471
472     const SMDS_MeshNode * idPrev = idFirst;
473     for (int i = 2; i < NbPoints; i++)
474     {
475       double param = f + (i - 1) * du;
476       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
477       meshDS->SetNodeOnEdge(node, E);
478
479       SMDS_EdgePosition* epos =
480         dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
481       epos->SetUParameter(param);
482
483       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
484       meshDS->SetMeshElementOnShape(edge, E);
485       idPrev = node;
486     }
487     SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
488     meshDS->SetMeshElementOnShape(edge, E);
489   }
490   return true;
491 }
492
493 //=============================================================================
494 /*!
495  *  See comments in SMESH_Algo.cxx
496  */
497 //=============================================================================
498
499 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis(
500         SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
501 {
502   _usedHypList.clear();
503   _usedHypList = GetAppliedHypothesis(aMesh, aShape);   // copy
504   int nbHyp = _usedHypList.size();
505   _mainEdge.Nullify();
506   if (nbHyp == 0)
507   {
508     // Check, if propagated from some other edge
509     if (aShape.ShapeType() == TopAbs_EDGE &&
510         aMesh.IsPropagatedHypothesis(aShape, _mainEdge))
511     {
512       // Propagation of 1D hypothesis from <aMainEdge> on this edge
513       //_usedHypList = GetAppliedHypothesis(aMesh, _mainEdge);  // copy
514       // use a general method in order not to nullify _mainEdge
515       _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, _mainEdge);   // copy
516       nbHyp = _usedHypList.size();
517     }
518   }
519   if (nbHyp == 0)
520   {
521     TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
522     for (; ancIt.More(); ancIt.Next())
523     {
524       const TopoDS_Shape& ancestor = ancIt.Value();
525       _usedHypList = GetAppliedHypothesis(aMesh, ancestor);     // copy
526       nbHyp = _usedHypList.size();
527       if (nbHyp == 1)
528         break;
529     }
530   }
531   if (nbHyp > 1)
532     _usedHypList.clear();       //only one compatible hypothesis allowed
533   return _usedHypList;
534 }
535
536 //=============================================================================
537 /*!
538  *  
539  */
540 //=============================================================================
541
542 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
543 {
544   return save;
545 }
546
547 //=============================================================================
548 /*!
549  *  
550  */
551 //=============================================================================
552
553 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
554 {
555   return load;
556 }
557
558 //=============================================================================
559 /*!
560  *  
561  */
562 //=============================================================================
563
564 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
565 {
566   return hyp.SaveTo( save );
567 }
568
569 //=============================================================================
570 /*!
571  *  
572  */
573 //=============================================================================
574
575 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
576 {
577   return hyp.LoadFrom( load );
578 }