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