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