1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshers_Regular_1D.cxx
25 // Moved here from SMESH_Regular_1D.cxx
26 // Author : Paul RASCLE, EDF
30 #include "StdMeshers_Regular_1D.hxx"
31 #include "StdMeshers_Distribution.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
35 #include "StdMeshers_LocalLength.hxx"
36 #include "StdMeshers_NumberOfSegments.hxx"
37 #include "StdMeshers_Arithmetic1D.hxx"
38 #include "StdMeshers_StartEndLength.hxx"
39 #include "StdMeshers_Deflection1D.hxx"
40 #include "StdMeshers_AutomaticLength.hxx"
42 #include "SMDS_MeshElement.hxx"
43 #include "SMDS_MeshNode.hxx"
44 #include "SMDS_EdgePosition.hxx"
45 #include "SMESH_subMesh.hxx"
47 #include "Utils_SALOME_Exception.hxx"
48 #include "utilities.h"
50 #include <BRep_Tool.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <GeomAdaptor_Curve.hxx>
55 #include <GCPnts_AbscissaPoint.hxx>
56 #include <GCPnts_UniformAbscissa.hxx>
57 #include <GCPnts_UniformDeflection.hxx>
58 #include <Standard_ErrorHandler.hxx>
59 #include <Precision.hxx>
60 #include <Expr_GeneralExpression.hxx>
61 #include <Expr_NamedUnknown.hxx>
62 #include <Expr_Array1OfNamedUnknown.hxx>
63 #include <TColStd_Array1OfReal.hxx>
64 #include <ExprIntrp_GenExp.hxx>
72 //=============================================================================
76 //=============================================================================
78 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
79 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
81 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
83 _shapeType = (1 << TopAbs_EDGE);
85 _compatibleHypothesis.push_back("LocalLength");
86 _compatibleHypothesis.push_back("NumberOfSegments");
87 _compatibleHypothesis.push_back("StartEndLength");
88 _compatibleHypothesis.push_back("Deflection1D");
89 _compatibleHypothesis.push_back("Arithmetic1D");
90 _compatibleHypothesis.push_back("AutomaticLength");
93 //=============================================================================
97 //=============================================================================
99 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
103 //=============================================================================
107 //=============================================================================
109 bool StdMeshers_Regular_1D::CheckHypothesis
111 const TopoDS_Shape& aShape,
112 SMESH_Hypothesis::Hypothesis_Status& aStatus)
116 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
117 if (hyps.size() == 0)
119 aStatus = SMESH_Hypothesis::HYP_MISSING;
120 return false; // can't work without a hypothesis
123 // use only the first hypothesis
124 const SMESHDS_Hypothesis *theHyp = hyps.front();
126 string hypName = theHyp->GetName();
128 if (hypName == "LocalLength")
130 const StdMeshers_LocalLength * hyp =
131 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
133 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
134 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
135 _hypType = LOCAL_LENGTH;
136 aStatus = SMESH_Hypothesis::HYP_OK;
139 else if (hypName == "NumberOfSegments")
141 const StdMeshers_NumberOfSegments * hyp =
142 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
144 _ivalue[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
145 ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
146 _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
147 switch (_ivalue[ DISTR_TYPE_IND ])
149 case StdMeshers_NumberOfSegments::DT_Scale:
150 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
152 case StdMeshers_NumberOfSegments::DT_TabFunc:
153 _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
155 case StdMeshers_NumberOfSegments::DT_ExprFunc:
156 _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
158 case StdMeshers_NumberOfSegments::DT_Regular:
164 if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
165 _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
166 _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
167 _hypType = NB_SEGMENTS;
168 aStatus = SMESH_Hypothesis::HYP_OK;
171 else if (hypName == "Arithmetic1D")
173 const StdMeshers_Arithmetic1D * hyp =
174 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
176 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
177 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
178 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
179 _hypType = ARITHMETIC_1D;
180 aStatus = SMESH_Hypothesis::HYP_OK;
183 else if (hypName == "StartEndLength")
185 const StdMeshers_StartEndLength * hyp =
186 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
188 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
189 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
190 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
191 _hypType = BEG_END_LENGTH;
192 aStatus = SMESH_Hypothesis::HYP_OK;
195 else if (hypName == "Deflection1D")
197 const StdMeshers_Deflection1D * hyp =
198 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
200 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
201 ASSERT( _value[ DEFLECTION_IND ] > 0 );
202 _hypType = DEFLECTION;
203 aStatus = SMESH_Hypothesis::HYP_OK;
206 else if (hypName == "AutomaticLength")
208 StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
209 (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
211 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
212 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
213 _hypType = LOCAL_LENGTH;
214 aStatus = SMESH_Hypothesis::HYP_OK;
217 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
219 return ( _hypType != NONE );
222 //=======================================================================
223 //function : compensateError
224 //purpose : adjust theParams so that the last segment length == an
225 //=======================================================================
227 static void compensateError(double a1, double an,
228 double U1, double Un,
230 GeomAdaptor_Curve& C3d,
231 list<double> & theParams)
233 int i, nPar = theParams.size();
234 if ( a1 + an < length && nPar > 1 )
236 list<double>::reverse_iterator itU = theParams.rbegin();
238 // dist from the last point to the edge end <Un>, it should be equal <an>
239 double Ln = GCPnts_AbscissaPoint::Length( C3d, Ul, Un );
240 double dLn = an - Ln; // error of <an>
241 if ( Abs( dLn ) <= Precision::Confusion() )
243 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
244 double dUn = dLn * Abs( Un - U1 ) / length; // parametric error of <an>
245 if ( dUn < 0.5 * dU ) { // last segment is a bit shorter than it should
246 dUn = -dUn; // move the last parameter to the edge beginning
248 else { // last segment is much shorter than it should -> remove the last param and
249 theParams.pop_back(); nPar--; // move the rest points toward the edge end
250 Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
251 dUn = ( an - Ln ) * Abs( Un - U1 ) / length;
252 if ( dUn < 0.5 * dU )
257 double q = dUn / ( nPar - 1 );
258 for ( itU = theParams.rbegin(), i = 1; i < nPar; itU++, i++ ) {
265 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
266 double length, bool theReverse,
267 int nbSeg, Function& func,
268 list<double>& theParams)
270 OSD::SetSignal( true );
275 MESSAGE( "computeParamByFunc" );
277 int nbPnt = 1 + nbSeg;
278 vector<double> x(nbPnt, 0.);
280 if( !buildDistribution( func, 0.0, 1.0, nbSeg, x, 1E-4 ) )
283 MESSAGE( "Points:\n" );
285 for( int i=0; i<=nbSeg; i++ )
287 sprintf( buf, "%f\n", float(x[i] ) );
293 // apply parameters in range [0,1] to the space of the curve
294 double prevU = first;
301 for( int i = 1; i < nbSeg; i++ )
303 double curvLength = length * (x[i] - x[i-1]) * sign;
304 GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
305 if ( !Discret.IsDone() )
307 double U = Discret.Parameter();
308 if ( U > first && U < last )
309 theParams.push_back( U );
317 //=============================================================================
321 //=============================================================================
322 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
323 list<double> & theParams,
324 const bool theReverse) const
329 Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
330 GeomAdaptor_Curve C3d(Curve);
332 double length = EdgeLength(theEdge);
340 if ( _hypType == LOCAL_LENGTH )
342 // Local Length hypothesis
343 double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
345 nbseg = 1; // degenerated edge
346 eltSize = length / nbseg;
350 // Number Of Segments hypothesis
351 switch (_ivalue[ DISTR_TYPE_IND ])
353 case StdMeshers_NumberOfSegments::DT_Scale:
355 double scale = _value[ SCALE_FACTOR_IND ];
358 double alpha = pow( scale , 1.0 / (_ivalue[ NB_SEGMENTS_IND ] - 1));
359 double factor = (l - f) / (1 - pow( alpha,_ivalue[ NB_SEGMENTS_IND ]));
361 int i, NbPoints = 1 + _ivalue[ NB_SEGMENTS_IND ];
362 for ( i = 2; i < NbPoints; i++ )
364 double param = f + factor * (1 - pow(alpha, i - 1));
365 theParams.push_back( param );
370 case StdMeshers_NumberOfSegments::DT_TabFunc:
372 FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
373 return computeParamByFunc(C3d, f, l, length, theReverse,
374 _ivalue[ NB_SEGMENTS_IND ], func,
378 case StdMeshers_NumberOfSegments::DT_ExprFunc:
380 FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
381 return computeParamByFunc(C3d, f, l, length, theReverse,
382 _ivalue[ NB_SEGMENTS_IND ], func,
386 case StdMeshers_NumberOfSegments::DT_Regular:
387 eltSize = length / _ivalue[ NB_SEGMENTS_IND ];
394 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
395 if ( !Discret.IsDone() )
398 int NbPoints = Discret.NbPoints();
399 for ( int i = 2; i < NbPoints; i++ )
401 double param = Discret.Parameter(i);
402 theParams.push_back( param );
404 compensateError( eltSize, eltSize, f, l, length, C3d, theParams ); // for PAL9899
408 case BEG_END_LENGTH: {
410 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
412 double a1 = _value[ BEG_LENGTH_IND ];
413 double an = _value[ END_LENGTH_IND ];
414 double q = ( length - a1 ) / ( length - an );
416 double U1 = theReverse ? l : f;
417 double Un = theReverse ? f : l;
419 double eltSize = theReverse ? -a1 : a1;
421 // computes a point on a curve <C3d> at the distance <eltSize>
422 // from the point of parameter <param>.
423 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
424 if ( !Discret.IsDone() ) break;
425 param = Discret.Parameter();
426 if ( param > f && param < l )
427 theParams.push_back( param );
432 compensateError( a1, an, U1, Un, length, C3d, theParams );
436 case ARITHMETIC_1D: {
438 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
440 double a1 = _value[ BEG_LENGTH_IND ];
441 double an = _value[ END_LENGTH_IND ];
443 double q = ( an - a1 ) / ( 2 *length/( a1 + an ) - 1 );
444 int n = int( 1 + ( an - a1 ) / q );
446 double U1 = theReverse ? l : f;
447 double Un = theReverse ? f : l;
454 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
455 // computes a point on a curve <C3d> at the distance <eltSize>
456 // from the point of parameter <param>.
457 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
458 if ( !Discret.IsDone() ) break;
459 param = Discret.Parameter();
460 if ( param > f && param < l )
461 theParams.push_back( param );
466 compensateError( a1, an, U1, Un, length, C3d, theParams );
473 GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
474 if ( !Discret.IsDone() )
477 int NbPoints = Discret.NbPoints();
478 for ( int i = 2; i < NbPoints; i++ )
480 double param = Discret.Parameter(i);
481 theParams.push_back( param );
493 //=============================================================================
497 //=============================================================================
499 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
501 MESSAGE("StdMeshers_Regular_1D::Compute");
503 if ( _hypType == NONE )
506 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
507 aMesh.GetSubMesh(aShape);
509 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
510 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
511 int shapeID = meshDS->ShapeToIndex( E );
514 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
516 TopoDS_Vertex VFirst, VLast;
517 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
519 ASSERT(!VFirst.IsNull());
520 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
523 MESSAGE (" NO NODE BUILT ON VERTEX ");
526 const SMDS_MeshNode * idFirst = lid->next();
528 ASSERT(!VLast.IsNull());
529 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
532 MESSAGE (" NO NODE BUILT ON VERTEX ");
535 const SMDS_MeshNode * idLast = lid->next();
539 list< double > params;
540 bool reversed = false;
541 if ( !_mainEdge.IsNull() )
542 reversed = aMesh.IsReversedInChain( EE, _mainEdge );
544 if ( ! computeInternalParameters( E, params, reversed ))
547 catch ( Standard_Failure ) {
551 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
552 // only internal nodes receive an edge position with param on curve
554 const SMDS_MeshNode * idPrev = idFirst;
556 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
559 gp_Pnt P = Curve->Value(param);
561 //Add the Node in the DataStructure
562 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
563 meshDS->SetNodeOnEdge(node, shapeID, param);
565 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
566 meshDS->SetMeshElementOnShape(edge, shapeID);
569 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
570 meshDS->SetMeshElementOnShape(edge, shapeID);
574 // Edge is a degenerated Edge : We put n = 5 points on the edge.
576 BRep_Tool::Range(E, f, l);
577 double du = (l - f) / (NbPoints - 1);
578 //MESSAGE("************* Degenerated edge! *****************");
580 TopoDS_Vertex V1, V2;
581 TopExp::Vertices(E, V1, V2);
582 gp_Pnt P = BRep_Tool::Pnt(V1);
584 const SMDS_MeshNode * idPrev = idFirst;
585 for (int i = 2; i < NbPoints; i++)
587 double param = f + (i - 1) * du;
588 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
589 meshDS->SetNodeOnEdge(node, shapeID, param);
591 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
592 meshDS->SetMeshElementOnShape(edge, shapeID);
595 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
596 meshDS->SetMeshElementOnShape(edge, shapeID);
601 //=============================================================================
603 * See comments in SMESH_Algo.cxx
605 //=============================================================================
607 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis(
608 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
610 _usedHypList.clear();
611 _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
612 int nbHyp = _usedHypList.size();
616 // Check, if propagated from some other edge
617 if (aShape.ShapeType() == TopAbs_EDGE &&
618 aMesh.IsPropagatedHypothesis(aShape, _mainEdge))
620 // Propagation of 1D hypothesis from <aMainEdge> on this edge
621 //_usedHypList = GetAppliedHypothesis(aMesh, _mainEdge); // copy
622 // use a general method in order not to nullify _mainEdge
623 _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, _mainEdge); // copy
624 nbHyp = _usedHypList.size();
629 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
630 for (; ancIt.More(); ancIt.Next())
632 const TopoDS_Shape& ancestor = ancIt.Value();
633 _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
634 nbHyp = _usedHypList.size();
640 _usedHypList.clear(); //only one compatible hypothesis allowed
644 //=============================================================================
648 //=============================================================================
650 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
655 //=============================================================================
659 //=============================================================================
661 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
666 //=============================================================================
670 //=============================================================================
672 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
674 return hyp.SaveTo( save );
677 //=============================================================================
681 //=============================================================================
683 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
685 return hyp.LoadFrom( load );