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