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