Salome HOME
Rever incorrect integration!!! from 13.09.2009 into the V5_1_main
[modules/smesh.git] / src / StdMeshers / StdMeshers_Regular_1D.cxx
1 //  Copyright (C) 2007-2010  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_Regular_1D.cxx
25 //           Moved here from SMESH_Regular_1D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //
29 #include "StdMeshers_Regular_1D.hxx"
30 #include "StdMeshers_Distribution.hxx"
31
32 #include "StdMeshers_Arithmetic1D.hxx"
33 #include "StdMeshers_AutomaticLength.hxx"
34 #include "StdMeshers_Deflection1D.hxx"
35 #include "StdMeshers_LocalLength.hxx"
36 #include "StdMeshers_MaxLength.hxx"
37 #include "StdMeshers_NumberOfSegments.hxx"
38 #include "StdMeshers_Propagation.hxx"
39 #include "StdMeshers_SegmentLengthAroundVertex.hxx"
40 #include "StdMeshers_StartEndLength.hxx"
41
42 #include "SMESH_Gen.hxx"
43 #include "SMESH_Mesh.hxx"
44 #include "SMESH_HypoFilter.hxx"
45 #include "SMESH_subMesh.hxx"
46 #include "SMESH_subMeshEventListener.hxx"
47 #include "SMESH_Comment.hxx"
48
49 #include "SMDS_MeshElement.hxx"
50 #include "SMDS_MeshNode.hxx"
51
52 #include "Utils_SALOME_Exception.hxx"
53 #include "utilities.h"
54
55 #include <BRepAdaptor_Curve.hxx>
56 #include <BRep_Tool.hxx>
57 #include <GCPnts_AbscissaPoint.hxx>
58 #include <GCPnts_UniformAbscissa.hxx>
59 #include <GCPnts_UniformDeflection.hxx>
60 #include <Precision.hxx>
61 #include <TopExp.hxx>
62 #include <TopExp_Explorer.hxx>
63 #include <TopoDS.hxx>
64 #include <TopoDS_Edge.hxx>
65
66 #include <string>
67 #include <limits>
68
69 using namespace std;
70
71 //=============================================================================
72 /*!
73  *  
74  */
75 //=============================================================================
76
77 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
78         SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
79 {
80         MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
81         _name = "Regular_1D";
82         _shapeType = (1 << TopAbs_EDGE);
83         _fpHyp = 0;
84
85         _compatibleHypothesis.push_back("LocalLength");
86         _compatibleHypothesis.push_back("MaxLength");
87         _compatibleHypothesis.push_back("NumberOfSegments");
88         _compatibleHypothesis.push_back("StartEndLength");
89         _compatibleHypothesis.push_back("Deflection1D");
90         _compatibleHypothesis.push_back("Arithmetic1D");
91         _compatibleHypothesis.push_back("FixedPoints1D");
92         _compatibleHypothesis.push_back("AutomaticLength");
93
94         _compatibleHypothesis.push_back("QuadraticMesh"); // auxiliary !!!
95         _compatibleHypothesis.push_back("Propagation"); // auxiliary !!!
96 }
97
98 //=============================================================================
99 /*!
100  *  
101  */
102 //=============================================================================
103
104 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
105 {
106 }
107
108 //=============================================================================
109 /*!
110  *  
111  */
112 //=============================================================================
113
114 bool StdMeshers_Regular_1D::CheckHypothesis
115                          (SMESH_Mesh&                          aMesh,
116                           const TopoDS_Shape&                  aShape,
117                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
118 {
119   _hypType = NONE;
120   _quadraticMesh = false;
121
122   const bool ignoreAuxiliaryHyps = false;
123   const list <const SMESHDS_Hypothesis * > & hyps =
124     GetUsedHypothesis(aMesh, aShape, ignoreAuxiliaryHyps);
125
126   // find non-auxiliary hypothesis
127   const SMESHDS_Hypothesis *theHyp = 0;
128   list <const SMESHDS_Hypothesis * >::const_iterator h = hyps.begin();
129   for ( ; h != hyps.end(); ++h ) {
130     if ( static_cast<const SMESH_Hypothesis*>(*h)->IsAuxiliary() ) {
131       if ( strcmp( "QuadraticMesh", (*h)->GetName() ) == 0 )
132         _quadraticMesh = true;
133     }
134     else {
135       if ( !theHyp )
136         theHyp = *h; // use only the first non-auxiliary hypothesis
137     }
138   }
139
140   if ( !theHyp )
141   {
142     aStatus = SMESH_Hypothesis::HYP_MISSING;
143     return false;  // can't work without a hypothesis
144   }
145
146   string hypName = theHyp->GetName();
147
148   if (hypName == "LocalLength")
149   {
150     const StdMeshers_LocalLength * hyp =
151       dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
152     ASSERT(hyp);
153     _value[ BEG_LENGTH_IND ] = hyp->GetLength();
154     _value[ PRECISION_IND ] = hyp->GetPrecision();
155     ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
156     _hypType = LOCAL_LENGTH;
157     aStatus = SMESH_Hypothesis::HYP_OK;
158   }
159
160   else if (hypName == "MaxLength")
161   {
162     const StdMeshers_MaxLength * hyp =
163       dynamic_cast <const StdMeshers_MaxLength * >(theHyp);
164     ASSERT(hyp);
165     _value[ BEG_LENGTH_IND ] = hyp->GetLength();
166     if ( hyp->GetUsePreestimatedLength() ) {
167       if ( int nbSeg = aMesh.GetGen()->GetBoundaryBoxSegmentation() )
168         _value[ BEG_LENGTH_IND ] = aMesh.GetShapeDiagonalSize() / nbSeg;
169     }
170     ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
171     _hypType = MAX_LENGTH;
172     aStatus = SMESH_Hypothesis::HYP_OK;
173   }
174
175   else if (hypName == "NumberOfSegments")
176   {
177     const StdMeshers_NumberOfSegments * hyp =
178       dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
179     ASSERT(hyp);
180     _ivalue[ NB_SEGMENTS_IND  ] = hyp->GetNumberOfSegments();
181     ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
182     _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
183     switch (_ivalue[ DISTR_TYPE_IND ])
184     {
185     case StdMeshers_NumberOfSegments::DT_Scale:
186       _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
187       _revEdgesIDs = hyp->GetReversedEdges();
188       break;
189     case StdMeshers_NumberOfSegments::DT_TabFunc:
190       _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
191       _revEdgesIDs = hyp->GetReversedEdges();
192       break;
193     case StdMeshers_NumberOfSegments::DT_ExprFunc:
194       _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
195       _revEdgesIDs = hyp->GetReversedEdges();
196       break;
197     case StdMeshers_NumberOfSegments::DT_Regular:
198       break;
199     default:
200       ASSERT(0);
201       break;
202     }
203     if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
204         _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
205         _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
206     _hypType = NB_SEGMENTS;
207     aStatus = SMESH_Hypothesis::HYP_OK;
208   }
209
210   else if (hypName == "Arithmetic1D")
211   {
212     const StdMeshers_Arithmetic1D * hyp =
213       dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
214     ASSERT(hyp);
215     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
216     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
217     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
218     _hypType = ARITHMETIC_1D;
219
220     _revEdgesIDs = hyp->GetReversedEdges();
221
222     aStatus = SMESH_Hypothesis::HYP_OK;
223   }
224
225   else if (hypName == "FixedPoints1D") {
226     _fpHyp = dynamic_cast <const StdMeshers_FixedPoints1D*>(theHyp);
227     ASSERT(_fpHyp);
228     _hypType = FIXED_POINTS_1D;
229
230     _revEdgesIDs = _fpHyp->GetReversedEdges();
231
232     aStatus = SMESH_Hypothesis::HYP_OK;
233   }
234
235   else if (hypName == "StartEndLength")
236   {
237     const StdMeshers_StartEndLength * hyp =
238       dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
239     ASSERT(hyp);
240     _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
241     _value[ END_LENGTH_IND ] = hyp->GetLength( false );
242     ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
243     _hypType = BEG_END_LENGTH;
244
245     _revEdgesIDs = hyp->GetReversedEdges();
246
247     aStatus = SMESH_Hypothesis::HYP_OK;
248   }
249
250   else if (hypName == "Deflection1D")
251   {
252     const StdMeshers_Deflection1D * hyp =
253       dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
254     ASSERT(hyp);
255     _value[ DEFLECTION_IND ] = hyp->GetDeflection();
256     ASSERT( _value[ DEFLECTION_IND ] > 0 );
257     _hypType = DEFLECTION;
258     aStatus = SMESH_Hypothesis::HYP_OK;
259   }
260
261   else if (hypName == "AutomaticLength")
262   {
263     StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
264       (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
265     ASSERT(hyp);
266     _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
267 //     _value[ BEG_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
268 //     _value[ END_LENGTH_IND ] = Precision::Confusion(); // ?? or set to zero?
269     ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
270     _hypType = MAX_LENGTH;
271     aStatus = SMESH_Hypothesis::HYP_OK;
272   }
273   else
274     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
275
276   return ( _hypType != NONE );
277 }
278
279 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
280                                double length, bool theReverse,
281                                int nbSeg, Function& func,
282                                list<double>& theParams)
283 {
284   // never do this way
285   //OSD::SetSignal( true );
286
287   if (nbSeg <= 0)
288     return false;
289
290   MESSAGE( "computeParamByFunc" );
291
292   int nbPnt = 1 + nbSeg;
293   vector<double> x(nbPnt, 0.);
294
295   if (!buildDistribution(func, 0.0, 1.0, nbSeg, x, 1E-4))
296      return false;
297
298   MESSAGE( "Points:\n" );
299   char buf[1024];
300   for ( int i=0; i<=nbSeg; i++ )
301   {
302     sprintf(  buf, "%f\n", float(x[i] ) );
303     MESSAGE( buf );
304   }
305
306
307
308   // apply parameters in range [0,1] to the space of the curve
309   double prevU = first;
310   double sign = 1.;
311   if (theReverse)
312   {
313     prevU = last;
314     sign = -1.;
315   }
316   for( int i = 1; i < nbSeg; i++ )
317   {
318     double curvLength = length * (x[i] - x[i-1]) * sign;
319     GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
320     if ( !Discret.IsDone() )
321       return false;
322     double U = Discret.Parameter();
323     if ( U > first && U < last )
324       theParams.push_back( U );
325     else
326       return false;
327     prevU = U;
328   }
329   if ( theReverse )
330     theParams.reverse();
331   return true;
332 }
333
334
335 //================================================================================
336 /*!
337  * \brief adjust internal node parameters so that the last segment length == an
338   * \param a1 - the first segment length
339   * \param an - the last segment length
340   * \param U1 - the first edge parameter
341   * \param Un - the last edge parameter
342   * \param length - the edge length
343   * \param C3d - the edge curve
344   * \param theParams - internal node parameters to adjust
345   * \param adjustNeighbors2an - to adjust length of segments next to the last one
346   *  and not to remove parameters
347  */
348 //================================================================================
349
350 static void compensateError(double a1, double an,
351                             double U1, double Un,
352                             double            length,
353                             Adaptor3d_Curve&  C3d,
354                             list<double> &    theParams,
355                             bool              adjustNeighbors2an = false)
356 {
357   int i, nPar = theParams.size();
358   if ( a1 + an < length && nPar > 1 )
359   {
360     bool reverse = ( U1 > Un );
361     GCPnts_AbscissaPoint Discret(C3d, reverse ? an : -an, Un);
362     if ( !Discret.IsDone() )
363       return;
364     double Utgt = Discret.Parameter(); // target value of the last parameter
365     list<double>::reverse_iterator itU = theParams.rbegin();
366     double Ul = *itU++; // real value of the last parameter
367     double dUn = Utgt - Ul; // parametric error of <an>
368     if ( Abs(dUn) <= Precision::Confusion() )
369       return;
370     double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
371     if ( adjustNeighbors2an || Abs(dUn) < 0.5 * dU ) { // last segment is a bit shorter than it should
372       // move the last parameter to the edge beginning
373     }
374     else {  // last segment is much shorter than it should -> remove the last param and
375       theParams.pop_back(); nPar--; // move the rest points toward the edge end
376       dUn = Utgt - theParams.back();
377     }
378
379     double q  = dUn / ( nPar - 1 );
380     if ( !adjustNeighbors2an ) {
381       for ( itU = theParams.rbegin(), i = 1; i < nPar; itU++, i++ ) {
382         (*itU) += dUn;
383         dUn -= q;
384       }
385     }
386     else {
387       theParams.back() += dUn;
388       double sign = reverse ? -1 : 1;
389       double prevU = theParams.back();
390       itU = theParams.rbegin();
391       for ( ++itU, i = 2; i < nPar; ++itU, i++ ) {
392         double newU = *itU + dUn;
393         if ( newU*sign < prevU*sign ) {
394           prevU = *itU = newU;
395           dUn -= q;
396         }
397         else { // set U between prevU and next valid param
398           list<double>::reverse_iterator itU2 = itU;
399           ++itU2;
400           int nb = 2;
401           while ( (*itU2)*sign > prevU*sign ) {
402             ++itU2; ++nb;
403           }
404           dU = ( *itU2 - prevU ) / nb;
405           while ( itU != itU2 ) {
406             *itU += dU; ++itU;
407           }
408           break;
409         }
410       }
411     }
412   }
413 }
414
415 //================================================================================
416 /*!
417  * \brief Class used to clean mesh on edges when 0D hyp modified.
418  * Common approach doesn't work when 0D algo is missing because the 0D hyp is
419  * considered as not participating in computation whereas it is used by 1D algo.
420  */
421 //================================================================================
422
423 // struct VertexEventListener : public SMESH_subMeshEventListener
424 // {
425 //   VertexEventListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
426 //   {}
427 //   /*!
428 //    * \brief Clean mesh on edges
429 //    * \param event - algo_event or compute_event itself (of SMESH_subMesh)
430 //    * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
431 //    * \param subMesh - the submesh where the event occures
432 //    */
433 //   void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
434 //                     EventListenerData*, const SMESH_Hypothesis*)
435 //   {
436 //     if ( eventType == SMESH_subMesh::ALGO_EVENT) // all algo events
437 //     {
438 //       subMesh->ComputeStateEngine( SMESH_subMesh::MODIF_ALGO_STATE );
439 //     }
440 //   }
441 // }; // struct VertexEventListener
442
443 //=============================================================================
444 /*!
445  * \brief Sets event listener to vertex submeshes
446  * \param subMesh - submesh where algo is set
447  *
448  * This method is called when a submesh gets HYP_OK algo_state.
449  * After being set, event listener is notified on each event of a submesh.
450  */
451 //=============================================================================
452
453 void StdMeshers_Regular_1D::SetEventListener(SMESH_subMesh* subMesh)
454 {
455   StdMeshers_Propagation::SetPropagationMgr( subMesh );
456 }
457
458 //=============================================================================
459 /*!
460  * \brief Do nothing
461  * \param subMesh - restored submesh
462  *
463  * This method is called only if a submesh has HYP_OK algo_state.
464  */
465 //=============================================================================
466
467 void StdMeshers_Regular_1D::SubmeshRestored(SMESH_subMesh* subMesh)
468 {
469 }
470
471 //=============================================================================
472 /*!
473  * \brief Return StdMeshers_SegmentLengthAroundVertex assigned to vertex
474  */
475 //=============================================================================
476
477 const StdMeshers_SegmentLengthAroundVertex*
478 StdMeshers_Regular_1D::getVertexHyp(SMESH_Mesh &          theMesh,
479                                     const TopoDS_Vertex & theV)
480 {
481   static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName("SegmentAroundVertex_0D"));
482   if ( const SMESH_Hypothesis * h = theMesh.GetHypothesis( theV, filter, true ))
483   {
484     SMESH_Algo* algo = const_cast< SMESH_Algo* >( static_cast< const SMESH_Algo* > ( h ));
485     const list <const SMESHDS_Hypothesis *> & hypList = algo->GetUsedHypothesis( theMesh, theV, 0 );
486     if ( !hypList.empty() && string("SegmentLengthAroundVertex") == hypList.front()->GetName() )
487       return static_cast<const StdMeshers_SegmentLengthAroundVertex*>( hypList.front() );
488   }
489   return 0;
490 }
491
492 //================================================================================
493 /*!
494  * \brief Tune parameters to fit "SegmentLengthAroundVertex" hypothesis
495   * \param theC3d - wire curve
496   * \param theLength - curve length
497   * \param theParameters - internal nodes parameters to modify
498   * \param theVf - 1st vertex
499   * \param theVl - 2nd vertex
500  */
501 //================================================================================
502
503 void StdMeshers_Regular_1D::redistributeNearVertices (SMESH_Mesh &          theMesh,
504                                                       Adaptor3d_Curve &     theC3d,
505                                                       double                theLength,
506                                                       std::list< double > & theParameters,
507                                                       const TopoDS_Vertex & theVf,
508                                                       const TopoDS_Vertex & theVl)
509 {
510   double f = theC3d.FirstParameter(), l = theC3d.LastParameter();
511   int nPar = theParameters.size();
512   for ( int isEnd1 = 0; isEnd1 < 2; ++isEnd1 )
513   {
514     const TopoDS_Vertex & V = isEnd1 ? theVf : theVl;
515     const StdMeshers_SegmentLengthAroundVertex* hyp = getVertexHyp (theMesh, V );
516     if ( hyp ) {
517       double vertexLength = hyp->GetLength();
518       if ( vertexLength > theLength / 2.0 )
519         continue;
520       if ( isEnd1 ) { // to have a segment of interest at end of theParameters
521         theParameters.reverse();
522         std::swap( f, l );
523       }
524       if ( _hypType == NB_SEGMENTS )
525       {
526         compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
527       }
528       else if ( nPar <= 3 )
529       {
530         if ( !isEnd1 )
531           vertexLength = -vertexLength;
532         GCPnts_AbscissaPoint Discret(theC3d, vertexLength, l);
533         if ( Discret.IsDone() ) {
534           if ( nPar == 0 )
535             theParameters.push_back( Discret.Parameter());
536           else {
537             double L = GCPnts_AbscissaPoint::Length( theC3d, theParameters.back(), l);
538             if ( vertexLength < L / 2.0 )
539               theParameters.push_back( Discret.Parameter());
540             else
541               compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
542           }
543         }
544       }
545       else
546       {
547         // recompute params between the last segment and a middle one.
548         // find size of a middle segment
549         int nHalf = ( nPar-1 ) / 2;
550         list< double >::reverse_iterator itU = theParameters.rbegin();
551         std::advance( itU, nHalf );
552         double Um = *itU++;
553         double Lm = GCPnts_AbscissaPoint::Length( theC3d, Um, *itU);
554         double L = GCPnts_AbscissaPoint::Length( theC3d, *itU, l);
555         StdMeshers_Regular_1D algo( *this );
556         algo._hypType = BEG_END_LENGTH;
557         algo._value[ BEG_LENGTH_IND ] = Lm;
558         algo._value[ END_LENGTH_IND ] = vertexLength;
559         double from = *itU, to = l;
560         if ( isEnd1 ) {
561           std::swap( from, to );
562           std::swap( algo._value[ BEG_LENGTH_IND ], algo._value[ END_LENGTH_IND ]);
563         }
564         list<double> params;
565         if ( algo.computeInternalParameters( theMesh, theC3d, L, from, to, params, false ))
566         {
567           if ( isEnd1 ) params.reverse();
568           while ( 1 + nHalf-- )
569             theParameters.pop_back();
570           theParameters.splice( theParameters.end(), params );
571         }
572         else
573         {
574           compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
575         }
576       }
577       if ( isEnd1 )
578         theParameters.reverse();
579     }
580   }
581 }
582
583 //=============================================================================
584 /*!
585  *  
586  */
587 //=============================================================================
588 bool StdMeshers_Regular_1D::computeInternalParameters(SMESH_Mesh &     theMesh,
589                                                       Adaptor3d_Curve& theC3d,
590                                                       double           theLength,
591                                                       double           theFirstU,
592                                                       double           theLastU,
593                                                       list<double> &   theParams,
594                                                       const bool       theReverse,
595                                                       bool             theConsiderPropagation)
596 {
597   theParams.clear();
598
599   double f = theFirstU, l = theLastU;
600
601   switch( _hypType )
602   {
603   case LOCAL_LENGTH:
604   case MAX_LENGTH:
605   case NB_SEGMENTS: {
606
607     double eltSize = 1;
608     if ( _hypType == MAX_LENGTH )
609     {
610       double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
611       if (nbseg <= 0)
612         nbseg = 1;                        // degenerated edge
613       eltSize = theLength / nbseg;
614     }
615     else if ( _hypType == LOCAL_LENGTH )
616     {
617       // Local Length hypothesis
618       double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
619
620       // NPAL17873:
621       bool isFound = false;
622       if (theConsiderPropagation && !_mainEdge.IsNull()) // propagated from some other edge
623       {
624         // Advanced processing to assure equal number of segments in case of Propagation
625         SMESH_subMesh* sm = theMesh.GetSubMeshContaining(_mainEdge);
626         if (sm) {
627           bool computed = sm->IsMeshComputed();
628           if (!computed) {
629             if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
630               _gen->Compute( theMesh, _mainEdge, /*anUpward=*/true);
631               computed = sm->IsMeshComputed();
632             }
633           }
634           if (computed) {
635             SMESHDS_SubMesh* smds = sm->GetSubMeshDS();
636             int nb_segments = smds->NbElements();
637             if (nbseg - 1 <= nb_segments && nb_segments <= nbseg + 1) {
638               isFound = true;
639               nbseg = nb_segments;
640             }
641           }
642         }
643       }
644       if (!isFound) // not found by meshed edge in the propagation chain, use precision
645       {
646         double aPrecision = _value[ PRECISION_IND ];
647         double nbseg_prec = ceil((theLength / _value[ BEG_LENGTH_IND ]) - aPrecision);
648         if (nbseg_prec == (nbseg - 1)) nbseg--;
649       }
650
651       if (nbseg <= 0)
652         nbseg = 1;                        // degenerated edge
653       eltSize = theLength / nbseg;
654     }
655     else
656     {
657       // Number Of Segments hypothesis
658       int NbSegm = _ivalue[ NB_SEGMENTS_IND ];
659       if ( NbSegm < 1 )  return false;
660       if ( NbSegm == 1 ) return true;
661
662       switch (_ivalue[ DISTR_TYPE_IND ])
663       {
664       case StdMeshers_NumberOfSegments::DT_Scale:
665         {
666           double scale = _value[ SCALE_FACTOR_IND ];
667
668           if (fabs(scale - 1.0) < Precision::Confusion()) {
669             // special case to avoid division by zero
670             for (int i = 1; i < NbSegm; i++) {
671               double param = f + (l - f) * i / NbSegm;
672               theParams.push_back( param );
673             }
674           } else {
675             // general case of scale distribution
676             if ( theReverse )
677               scale = 1.0 / scale;
678
679             double alpha = pow(scale, 1.0 / (NbSegm - 1));
680             double factor = (l - f) / (1.0 - pow(alpha, NbSegm));
681
682             for (int i = 1; i < NbSegm; i++) {
683               double param = f + factor * (1.0 - pow(alpha, i));
684               theParams.push_back( param );
685             }
686           }
687           return true;
688         }
689         break;
690       case StdMeshers_NumberOfSegments::DT_TabFunc:
691         {
692           FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
693           return computeParamByFunc(theC3d, f, l, theLength, theReverse,
694                                     _ivalue[ NB_SEGMENTS_IND ], func,
695                                     theParams);
696         }
697         break;
698       case StdMeshers_NumberOfSegments::DT_ExprFunc:
699         {
700           FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
701           return computeParamByFunc(theC3d, f, l, theLength, theReverse,
702                                     _ivalue[ NB_SEGMENTS_IND ], func,
703                                     theParams);
704         }
705         break;
706       case StdMeshers_NumberOfSegments::DT_Regular:
707         eltSize = theLength / _ivalue[ NB_SEGMENTS_IND ];
708         break;
709       default:
710         return false;
711       }
712     }
713     GCPnts_UniformAbscissa Discret(theC3d, eltSize, f, l);
714     if ( !Discret.IsDone() )
715       return error( "GCPnts_UniformAbscissa failed");
716
717     int NbPoints = Discret.NbPoints();
718     for ( int i = 2; i < NbPoints; i++ )
719     {
720       double param = Discret.Parameter(i);
721       theParams.push_back( param );
722     }
723     compensateError( eltSize, eltSize, f, l, theLength, theC3d, theParams ); // for PAL9899
724     return true;
725   }
726
727   case BEG_END_LENGTH: {
728
729     // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = theLength
730
731     double a1 = _value[ BEG_LENGTH_IND ];
732     double an = _value[ END_LENGTH_IND ];
733     double q  = ( theLength - a1 ) / ( theLength - an );
734     if ( q < theLength/1e6 || 1.01*theLength < a1 + an)
735       return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
736                      "for an edge of length "<<theLength);
737
738     double U1 = theReverse ? l : f;
739     double Un = theReverse ? f : l;
740     double param = U1;
741     double eltSize = theReverse ? -a1 : a1;
742     while ( 1 ) {
743       // computes a point on a curve <theC3d> at the distance <eltSize>
744       // from the point of parameter <param>.
745       GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
746       if ( !Discret.IsDone() ) break;
747       param = Discret.Parameter();
748       if ( f < param && param < l )
749         theParams.push_back( param );
750       else
751         break;
752       eltSize *= q;
753     }
754     compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
755     if (theReverse) theParams.reverse(); // NPAL18025
756     return true;
757   }
758
759   case ARITHMETIC_1D: {
760
761     // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = theLength
762
763     double a1 = _value[ BEG_LENGTH_IND ];
764     double an = _value[ END_LENGTH_IND ];
765     if ( 1.01*theLength < a1 + an)
766       return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
767                      "for an edge of length "<<theLength);
768
769     double  q = ( an - a1 ) / ( 2 *theLength/( a1 + an ) - 1 );
770     int n = int(fabs(q) > numeric_limits<double>::min() ? ( 1+( an-a1 )/q ) : ( 1+theLength/a1 ));
771
772     double U1 = theReverse ? l : f;
773     double Un = theReverse ? f : l;
774     double param = U1;
775     double eltSize = a1;
776     if ( theReverse ) {
777       eltSize = -eltSize;
778       q = -q;
779     }
780     while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
781       // computes a point on a curve <theC3d> at the distance <eltSize>
782       // from the point of parameter <param>.
783       GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
784       if ( !Discret.IsDone() ) break;
785       param = Discret.Parameter();
786       if ( param > f && param < l )
787         theParams.push_back( param );
788       else
789         break;
790       eltSize += q;
791     }
792     compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
793     if (theReverse) theParams.reverse(); // NPAL18025
794
795     return true;
796   }
797
798   case FIXED_POINTS_1D: {
799     const std::vector<double>& aPnts = _fpHyp->GetPoints();
800     const std::vector<int>& nbsegs = _fpHyp->GetNbSegments();
801     int i = 0;
802     TColStd_SequenceOfReal Params;
803     for(; i<aPnts.size(); i++) {
804       if( aPnts[i]<0.0001 || aPnts[i]>0.9999 ) continue;
805       int j=1;
806       bool IsExist = false;
807       for(; j<=Params.Length(); j++) {
808         if( fabs(aPnts[i]-Params.Value(j)) < 1e-4 ) {
809           IsExist = true;
810           break;
811         }
812         if( aPnts[i]<Params.Value(j) ) break;
813       }
814       if(!IsExist) Params.InsertBefore(j,aPnts[i]);
815     }
816     double par2, par1, lp;
817     par1 = f;
818     lp = l;
819     double sign = 1.0;
820     if(theReverse) {
821       par1 = l;
822       lp = f;
823       sign = -1.0;
824     }
825     double eltSize, segmentSize = 0.;
826     double currAbscissa = 0;
827     for(i=0; i<Params.Length(); i++) {
828       int nbseg = ( i > nbsegs.size()-1 ) ? nbsegs[0] : nbsegs[i];
829       segmentSize = Params.Value(i+1)*theLength - currAbscissa;
830       currAbscissa += segmentSize;
831       GCPnts_AbscissaPoint APnt(theC3d, sign*segmentSize, par1);
832       if( !APnt.IsDone() )
833         return error( "GCPnts_AbscissaPoint failed");
834       par2 = APnt.Parameter();
835       eltSize = segmentSize/nbseg;
836       GCPnts_UniformAbscissa Discret(theC3d, eltSize, par1, par2);
837       if(theReverse)
838         Discret.Initialize(theC3d, eltSize, par2, par1);
839       else
840         Discret.Initialize(theC3d, eltSize, par1, par2);
841       if ( !Discret.IsDone() )
842         return error( "GCPnts_UniformAbscissa failed");
843       int NbPoints = Discret.NbPoints();
844       list<double> tmpParams;
845       for(int i=2; i<NbPoints; i++) {
846         double param = Discret.Parameter(i);
847         tmpParams.push_back( param );
848       }
849       if (theReverse) {
850         compensateError( eltSize, eltSize, par2, par1, segmentSize, theC3d, tmpParams );
851         tmpParams.reverse();
852       }
853       else {
854         compensateError( eltSize, eltSize, par1, par2, segmentSize, theC3d, tmpParams );
855       }
856       list<double>::iterator itP = tmpParams.begin();
857       for(; itP != tmpParams.end(); itP++) {
858         theParams.push_back( *(itP) );
859       }
860       theParams.push_back( par2 );
861
862       par1 = par2;
863     }
864     // add for last
865     int nbseg = ( nbsegs.size() > Params.Length() ) ? nbsegs[Params.Length()] : nbsegs[0];
866     segmentSize = theLength - currAbscissa;
867     eltSize = segmentSize/nbseg;
868     GCPnts_UniformAbscissa Discret;
869     if(theReverse)
870       Discret.Initialize(theC3d, eltSize, par1, lp);
871     else
872       Discret.Initialize(theC3d, eltSize, lp, par1);
873     if ( !Discret.IsDone() )
874       return error( "GCPnts_UniformAbscissa failed");
875     int NbPoints = Discret.NbPoints();
876     list<double> tmpParams;
877     for(int i=2; i<NbPoints; i++) {
878       double param = Discret.Parameter(i);
879       tmpParams.push_back( param );
880     }
881     if (theReverse) {
882       compensateError( eltSize, eltSize, lp, par1, segmentSize, theC3d, tmpParams );
883       tmpParams.reverse();
884     }
885     else {
886       compensateError( eltSize, eltSize, par1, lp, segmentSize, theC3d, tmpParams );
887     }
888     list<double>::iterator itP = tmpParams.begin();
889     for(; itP != tmpParams.end(); itP++) {
890       theParams.push_back( *(itP) );
891     }
892
893     if (theReverse) {
894       theParams.reverse(); // NPAL18025
895     }
896     return true;
897   }
898
899   case DEFLECTION: {
900
901     GCPnts_UniformDeflection Discret(theC3d, _value[ DEFLECTION_IND ], f, l, true);
902     if ( !Discret.IsDone() )
903       return false;
904
905     int NbPoints = Discret.NbPoints();
906     for ( int i = 2; i < NbPoints; i++ )
907     {
908       double param = Discret.Parameter(i);
909       theParams.push_back( param );
910     }
911     return true;
912   }
913
914   default:;
915   }
916
917   return false;
918 }
919
920 //=============================================================================
921 /*!
922  *  
923  */
924 //=============================================================================
925
926 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
927 {
928   if ( _hypType == NONE )
929     return false;
930
931   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
932
933   const TopoDS_Edge & EE = TopoDS::Edge(theShape);
934   TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
935   int shapeID = meshDS->ShapeToIndex( E );
936
937   double f, l;
938   Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
939
940   TopoDS_Vertex VFirst, VLast;
941   TopExp::Vertices(E, VFirst, VLast);   // Vfirst corresponds to f and Vlast to l
942
943   ASSERT(!VFirst.IsNull());
944   ASSERT(!VLast.IsNull());
945   const SMDS_MeshNode * idFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
946   const SMDS_MeshNode * idLast = SMESH_Algo::VertexNode( VLast, meshDS );
947   if (!idFirst || !idLast)
948     return error( COMPERR_BAD_INPUT_MESH, "No node on vertex");
949
950   if (!Curve.IsNull())
951   {
952     list< double > params;
953     bool reversed = false;
954     if ( theMesh.GetShapeToMesh().ShapeType() >= TopAbs_WIRE ) {
955       reversed = ( EE.Orientation() == TopAbs_REVERSED );
956     }
957     if ( !_mainEdge.IsNull() ) {
958       reversed = ( _mainEdge.Orientation() == TopAbs_REVERSED );
959     }
960     else if ( _revEdgesIDs.size() > 0 ) {
961       for ( int i = 0; i < _revEdgesIDs.size(); i++) {
962         if ( _revEdgesIDs[i] == shapeID ) {
963           reversed = !reversed;
964         }
965       }
966     }
967
968     BRepAdaptor_Curve C3d( E );
969     double length = EdgeLength( E );
970     if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, reversed, true )) {
971       return false;
972     }
973     redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
974
975     // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
976     // only internal nodes receive an edge position with param on curve
977
978     const SMDS_MeshNode * idPrev = idFirst;
979     double parPrev = f;
980     double parLast = l;
981
982     /* NPAL18025
983     if (reversed) {
984       idPrev = idLast;
985       idLast = idFirst;
986       idFirst = idPrev;
987       parPrev = l;
988       parLast = f;
989     }
990     */
991
992     for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++) {
993       double param = *itU;
994       gp_Pnt P = Curve->Value(param);
995
996       //Add the Node in the DataStructure
997       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
998       meshDS->SetNodeOnEdge(node, shapeID, param);
999
1000       if(_quadraticMesh) {
1001         // create medium node
1002         double prm = ( parPrev + param )/2;
1003         gp_Pnt PM = Curve->Value(prm);
1004         SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1005         meshDS->SetNodeOnEdge(NM, shapeID, prm);
1006         SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1007         meshDS->SetMeshElementOnShape(edge, shapeID);
1008       }
1009       else {
1010         SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1011         meshDS->SetMeshElementOnShape(edge, shapeID);
1012       }
1013
1014       idPrev = node;
1015       parPrev = param;
1016     }
1017     if(_quadraticMesh) {
1018       double prm = ( parPrev + parLast )/2;
1019       gp_Pnt PM = Curve->Value(prm);
1020       SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1021       meshDS->SetNodeOnEdge(NM, shapeID, prm);
1022       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1023       meshDS->SetMeshElementOnShape(edge, shapeID);
1024     }
1025     else {
1026       SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
1027       meshDS->SetMeshElementOnShape(edge, shapeID);
1028     }
1029   }
1030   else
1031   {
1032     //MESSAGE("************* Degenerated edge! *****************");
1033
1034     // Edge is a degenerated Edge : We put n = 5 points on the edge.
1035     const int NbPoints = 5;
1036     BRep_Tool::Range( E, f, l ); // PAL15185
1037     double du = (l - f) / (NbPoints - 1);
1038
1039     gp_Pnt P = BRep_Tool::Pnt(VFirst);
1040
1041     const SMDS_MeshNode * idPrev = idFirst;
1042     for (int i = 2; i < NbPoints; i++) {
1043       double param = f + (i - 1) * du;
1044       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1045       if(_quadraticMesh) {
1046         // create medium node
1047         double prm = param - du/2.;
1048         SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1049         meshDS->SetNodeOnEdge(NM, shapeID, prm);
1050         SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1051         meshDS->SetMeshElementOnShape(edge, shapeID);
1052       }
1053       else {
1054         SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1055         meshDS->SetMeshElementOnShape(edge, shapeID);
1056       }
1057       meshDS->SetNodeOnEdge(node, shapeID, param);
1058       idPrev = node;
1059     }
1060     if(_quadraticMesh) {
1061       // create medium node
1062       double prm = l - du/2.;
1063       SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1064       meshDS->SetNodeOnEdge(NM, shapeID, prm);
1065       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1066       meshDS->SetMeshElementOnShape(edge, shapeID);
1067     }
1068     else {
1069       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
1070       meshDS->SetMeshElementOnShape(edge, shapeID);
1071     }
1072   }
1073   return true;
1074 }
1075
1076
1077 //=============================================================================
1078 /*!
1079  *  
1080  */
1081 //=============================================================================
1082
1083 bool StdMeshers_Regular_1D::Evaluate(SMESH_Mesh & theMesh,
1084                                      const TopoDS_Shape & theShape,
1085                                      MapShapeNbElems& aResMap)
1086 {
1087   if ( _hypType == NONE )
1088     return false;
1089
1090   //SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1091
1092   const TopoDS_Edge & EE = TopoDS::Edge(theShape);
1093   TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
1094   //  int shapeID = meshDS->ShapeToIndex( E );
1095
1096   double f, l;
1097   Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
1098
1099   TopoDS_Vertex VFirst, VLast;
1100   TopExp::Vertices(E, VFirst, VLast);   // Vfirst corresponds to f and Vlast to l
1101
1102   ASSERT(!VFirst.IsNull());
1103   ASSERT(!VLast.IsNull());
1104
1105   std::vector<int> aVec(SMDSEntity_Last,0);
1106
1107   if (!Curve.IsNull()) {
1108     list< double > params;
1109
1110     BRepAdaptor_Curve C3d( E );
1111     double length = EdgeLength( E );
1112     if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, false, true )) {
1113       SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1114       aResMap.insert(std::make_pair(sm,aVec));
1115       SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1116       smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
1117       return false;
1118     }
1119     redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1120
1121     if(_quadraticMesh) {
1122       aVec[SMDSEntity_Node] = 2*params.size() + 1;
1123       aVec[SMDSEntity_Quad_Edge] = params.size() + 1;
1124     }
1125     else {
1126       aVec[SMDSEntity_Node] = params.size();
1127       aVec[SMDSEntity_Edge] = params.size() + 1;
1128     }
1129     
1130   }
1131   else {
1132     //MESSAGE("************* Degenerated edge! *****************");
1133     // Edge is a degenerated Edge : We put n = 5 points on the edge.
1134     if(_quadraticMesh) {
1135       aVec[SMDSEntity_Node] = 11;
1136       aVec[SMDSEntity_Quad_Edge] = 6;
1137     }
1138     else {
1139       aVec[SMDSEntity_Node] = 5;
1140       aVec[SMDSEntity_Edge] = 6;
1141     }
1142   }
1143
1144   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1145   aResMap.insert(std::make_pair(sm,aVec));
1146
1147   return true;
1148 }
1149
1150
1151 //=============================================================================
1152 /*!
1153  *  See comments in SMESH_Algo.cxx
1154  */
1155 //=============================================================================
1156
1157 const list <const SMESHDS_Hypothesis *> &
1158 StdMeshers_Regular_1D::GetUsedHypothesis(SMESH_Mesh &         aMesh,
1159                                          const TopoDS_Shape & aShape,
1160                                          const bool           ignoreAuxiliary)
1161 {
1162   _usedHypList.clear();
1163   _mainEdge.Nullify();
1164
1165   SMESH_HypoFilter auxiliaryFilter, compatibleFilter;
1166   auxiliaryFilter.Init( SMESH_HypoFilter::IsAuxiliary() );
1167   const bool ignoreAux = true;
1168   InitCompatibleHypoFilter( compatibleFilter, ignoreAux );
1169
1170   // get non-auxiliary assigned to aShape
1171   int nbHyp = aMesh.GetHypotheses( aShape, compatibleFilter, _usedHypList, false );
1172
1173   if (nbHyp == 0 && aShape.ShapeType() == TopAbs_EDGE)
1174   {
1175     // Check, if propagated from some other edge
1176     _mainEdge = StdMeshers_Propagation::GetPropagationSource( aMesh, aShape );
1177     if ( !_mainEdge.IsNull() )
1178     {
1179       // Propagation of 1D hypothesis from <aMainEdge> on this edge;
1180       // get non-auxiliary assigned to _mainEdge
1181       nbHyp = aMesh.GetHypotheses( _mainEdge, compatibleFilter, _usedHypList, true );
1182     }
1183   }
1184
1185   if (nbHyp == 0) // nothing propagated nor assigned to aShape
1186   {
1187     SMESH_Algo::GetUsedHypothesis( aMesh, aShape, ignoreAuxiliary );
1188     nbHyp = _usedHypList.size();
1189   }
1190   else
1191   {
1192     // get auxiliary hyps from aShape
1193     aMesh.GetHypotheses( aShape, auxiliaryFilter, _usedHypList, true );
1194   }
1195   if ( nbHyp > 1 && ignoreAuxiliary )
1196     _usedHypList.clear(); //only one compatible non-auxiliary hypothesis allowed
1197
1198   return _usedHypList;
1199 }