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