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