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