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
32 #include "StdMeshers_Regular_1D.hxx"
33 #include "StdMeshers_Distribution.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_Mesh.hxx"
39 #include "StdMeshers_LocalLength.hxx"
40 #include "StdMeshers_NumberOfSegments.hxx"
41 #include "StdMeshers_Arithmetic1D.hxx"
42 #include "StdMeshers_StartEndLength.hxx"
43 #include "StdMeshers_Deflection1D.hxx"
44 #include <StdMeshers_AutomaticLength.hxx>
46 #include "SMDS_MeshElement.hxx"
47 #include "SMDS_MeshNode.hxx"
48 #include "SMDS_EdgePosition.hxx"
49 #include "SMESH_subMesh.hxx"
51 #include "Utils_SALOME_Exception.hxx"
52 #include "utilities.h"
54 #include <BRep_Tool.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <GeomAdaptor_Curve.hxx>
59 #include <GCPnts_AbscissaPoint.hxx>
60 #include <GCPnts_UniformAbscissa.hxx>
61 #include <GCPnts_UniformDeflection.hxx>
62 #include <Standard_ErrorHandler.hxx>
63 #include <Precision.hxx>
64 #include <Expr_GeneralExpression.hxx>
65 #include <Expr_NamedUnknown.hxx>
66 #include <Expr_Array1OfNamedUnknown.hxx>
67 #include <TColStd_Array1OfReal.hxx>
68 #include <ExprIntrp_GenExp.hxx>
73 //=============================================================================
77 //=============================================================================
79 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
80 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
82 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
84 _shapeType = (1 << TopAbs_EDGE);
86 _compatibleHypothesis.push_back("LocalLength");
87 _compatibleHypothesis.push_back("NumberOfSegments");
88 _compatibleHypothesis.push_back("StartEndLength");
89 _compatibleHypothesis.push_back("Deflection1D");
90 _compatibleHypothesis.push_back("Arithmetic1D");
91 _compatibleHypothesis.push_back("AutomaticLength");
94 //=============================================================================
98 //=============================================================================
100 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
104 //=============================================================================
108 //=============================================================================
110 bool StdMeshers_Regular_1D::CheckHypothesis
112 const TopoDS_Shape& aShape,
113 SMESH_Hypothesis::Hypothesis_Status& aStatus)
117 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
118 if (hyps.size() == 0)
120 aStatus = SMESH_Hypothesis::HYP_MISSING;
121 return false; // can't work without a hypothesis
124 // use only the first hypothesis
125 const SMESHDS_Hypothesis *theHyp = hyps.front();
127 string hypName = theHyp->GetName();
129 if (hypName == "LocalLength")
131 const StdMeshers_LocalLength * hyp =
132 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
134 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
135 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
136 _hypType = LOCAL_LENGTH;
137 aStatus = SMESH_Hypothesis::HYP_OK;
140 else if (hypName == "NumberOfSegments")
142 const StdMeshers_NumberOfSegments * hyp =
143 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
145 _ivalue[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
146 ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
147 _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
148 switch (_ivalue[ DISTR_TYPE_IND ])
150 case StdMeshers_NumberOfSegments::DT_Scale:
151 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
153 case StdMeshers_NumberOfSegments::DT_TabFunc:
154 _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
156 case StdMeshers_NumberOfSegments::DT_ExprFunc:
157 _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
159 case StdMeshers_NumberOfSegments::DT_Regular:
165 if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
166 _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
167 _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
168 _hypType = NB_SEGMENTS;
169 aStatus = SMESH_Hypothesis::HYP_OK;
172 else if (hypName == "Arithmetic1D")
174 const StdMeshers_Arithmetic1D * hyp =
175 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
177 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
178 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
179 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
180 _hypType = ARITHMETIC_1D;
181 aStatus = SMESH_Hypothesis::HYP_OK;
184 else if (hypName == "StartEndLength")
186 const StdMeshers_StartEndLength * hyp =
187 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
189 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
190 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
191 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
192 _hypType = BEG_END_LENGTH;
193 aStatus = SMESH_Hypothesis::HYP_OK;
196 else if (hypName == "Deflection1D")
198 const StdMeshers_Deflection1D * hyp =
199 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
201 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
202 ASSERT( _value[ DEFLECTION_IND ] > 0 );
203 _hypType = DEFLECTION;
204 aStatus = SMESH_Hypothesis::HYP_OK;
207 else if (hypName == "AutomaticLength")
209 StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
210 (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
212 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
213 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
214 _hypType = LOCAL_LENGTH;
215 aStatus = SMESH_Hypothesis::HYP_OK;
218 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
220 return ( _hypType != NONE );
223 //=======================================================================
224 //function : compensateError
225 //purpose : adjust theParams so that the last segment length == an
226 //=======================================================================
228 static void compensateError(double a1, double an,
229 double U1, double Un,
231 GeomAdaptor_Curve& C3d,
232 list<double> & theParams)
234 int i, nPar = theParams.size();
235 if ( a1 + an < length && nPar > 1 )
237 list<double>::reverse_iterator itU = theParams.rbegin();
239 // dist from the last point to the edge end <Un>, it should be equal <an>
240 double Ln = GCPnts_AbscissaPoint::Length( C3d, Ul, Un );
241 double dLn = an - Ln; // error of <an>
242 if ( Abs( dLn ) <= Precision::Confusion() )
244 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
245 double dUn = dLn * Abs( Un - U1 ) / length; // parametric error of <an>
246 if ( dUn < 0.5 * dU ) { // last segment is a bit shorter than it should
247 dUn = -dUn; // move the last parameter to the edge beginning
249 else { // last segment is much shorter than it should -> remove the last param and
250 theParams.pop_back(); nPar--; // move the rest points toward the edge end
251 Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
252 dUn = ( an - Ln ) * Abs( Un - U1 ) / length;
253 if ( dUn < 0.5 * dU )
258 double q = dUn / ( nPar - 1 );
259 for ( itU = theParams.rbegin(), i = 1; i < nPar; itU++, i++ ) {
266 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
267 double length, bool theReverse,
268 int nbSeg, Function& func,
269 list<double>& theParams)
271 OSD::SetSignal( true );
276 MESSAGE( "computeParamByFunc" );
278 int nbPnt = 1 + nbSeg;
279 vector<double> x(nbPnt, 0.);
281 if( !buildDistribution( func, 0.0, 1.0, nbSeg, x, 1E-4 ) )
284 MESSAGE( "Points:\n" );
286 for( int i=0; i<=nbSeg; i++ )
288 sprintf( buf, "%f\n", float(x[i] ) );
294 // apply parameters in range [0,1] to the space of the curve
295 double prevU = first;
302 for( int i = 1; i < nbSeg; i++ )
304 double curvLength = length * (x[i] - x[i-1]) * sign;
305 GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
306 if ( !Discret.IsDone() )
308 double U = Discret.Parameter();
309 if ( U > first && U < last )
310 theParams.push_back( U );
318 //=============================================================================
322 //=============================================================================
323 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
324 list<double> & theParams,
325 const bool theReverse) const
330 Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
331 GeomAdaptor_Curve C3d(Curve);
333 double length = EdgeLength(theEdge);
341 if ( _hypType == LOCAL_LENGTH )
343 // Local Length hypothesis
344 double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
346 nbseg = 1; // degenerated edge
347 eltSize = length / nbseg;
351 // Number Of Segments hypothesis
352 switch (_ivalue[ DISTR_TYPE_IND ])
354 case StdMeshers_NumberOfSegments::DT_Scale:
356 double scale = _value[ SCALE_FACTOR_IND ];
359 double alpha = pow( scale , 1.0 / (_ivalue[ NB_SEGMENTS_IND ] - 1));
360 double factor = (l - f) / (1 - pow( alpha,_ivalue[ NB_SEGMENTS_IND ]));
362 int i, NbPoints = 1 + _ivalue[ NB_SEGMENTS_IND ];
363 for ( i = 2; i < NbPoints; i++ )
365 double param = f + factor * (1 - pow(alpha, i - 1));
366 theParams.push_back( param );
371 case StdMeshers_NumberOfSegments::DT_TabFunc:
373 FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
374 return computeParamByFunc(C3d, f, l, length, theReverse,
375 _ivalue[ NB_SEGMENTS_IND ], func,
379 case StdMeshers_NumberOfSegments::DT_ExprFunc:
381 FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
382 return computeParamByFunc(C3d, f, l, length, theReverse,
383 _ivalue[ NB_SEGMENTS_IND ], func,
387 case StdMeshers_NumberOfSegments::DT_Regular:
388 eltSize = length / _ivalue[ NB_SEGMENTS_IND ];
395 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
396 if ( !Discret.IsDone() )
399 int NbPoints = Discret.NbPoints();
400 for ( int i = 2; i < NbPoints; i++ )
402 double param = Discret.Parameter(i);
403 theParams.push_back( param );
405 compensateError( eltSize, eltSize, f, l, length, C3d, theParams ); // for PAL9899
409 case BEG_END_LENGTH: {
411 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
413 double a1 = _value[ BEG_LENGTH_IND ];
414 double an = _value[ END_LENGTH_IND ];
415 double q = ( length - a1 ) / ( length - an );
417 double U1 = theReverse ? l : f;
418 double Un = theReverse ? f : l;
420 double eltSize = theReverse ? -a1 : a1;
422 // computes a point on a curve <C3d> at the distance <eltSize>
423 // from the point of parameter <param>.
424 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
425 if ( !Discret.IsDone() ) break;
426 param = Discret.Parameter();
427 if ( param > f && param < l )
428 theParams.push_back( param );
433 compensateError( a1, an, U1, Un, length, C3d, theParams );
437 case ARITHMETIC_1D: {
439 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
441 double a1 = _value[ BEG_LENGTH_IND ];
442 double an = _value[ END_LENGTH_IND ];
444 double q = ( an - a1 ) / ( 2 *length/( a1 + an ) - 1 );
445 int n = int( 1 + ( an - a1 ) / q );
447 double U1 = theReverse ? l : f;
448 double Un = theReverse ? f : l;
455 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
456 // computes a point on a curve <C3d> at the distance <eltSize>
457 // from the point of parameter <param>.
458 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
459 if ( !Discret.IsDone() ) break;
460 param = Discret.Parameter();
461 if ( param > f && param < l )
462 theParams.push_back( param );
467 compensateError( a1, an, U1, Un, length, C3d, theParams );
474 GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
475 if ( !Discret.IsDone() )
478 int NbPoints = Discret.NbPoints();
479 for ( int i = 2; i < NbPoints; i++ )
481 double param = Discret.Parameter(i);
482 theParams.push_back( param );
494 //=============================================================================
498 //=============================================================================
500 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
502 MESSAGE("StdMeshers_Regular_1D::Compute");
504 if ( _hypType == NONE )
507 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
508 aMesh.GetSubMesh(aShape);
510 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
511 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
512 int shapeID = meshDS->ShapeToIndex( E );
515 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
517 TopoDS_Vertex VFirst, VLast;
518 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
520 ASSERT(!VFirst.IsNull());
521 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
524 MESSAGE (" NO NODE BUILT ON VERTEX ");
527 const SMDS_MeshNode * idFirst = lid->next();
529 ASSERT(!VLast.IsNull());
530 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
533 MESSAGE (" NO NODE BUILT ON VERTEX ");
536 const SMDS_MeshNode * idLast = lid->next();
540 list< double > params;
541 bool reversed = false;
542 if ( !_mainEdge.IsNull() )
543 reversed = aMesh.IsReversedInChain( EE, _mainEdge );
545 if ( ! computeInternalParameters( E, params, reversed ))
548 catch ( Standard_Failure ) {
552 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
553 // only internal nodes receive an edge position with param on curve
555 const SMDS_MeshNode * idPrev = idFirst;
557 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
560 gp_Pnt P = Curve->Value(param);
562 //Add the Node in the DataStructure
563 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
564 meshDS->SetNodeOnEdge(node, shapeID, param);
566 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
567 meshDS->SetMeshElementOnShape(edge, shapeID);
570 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
571 meshDS->SetMeshElementOnShape(edge, shapeID);
575 // Edge is a degenerated Edge : We put n = 5 points on the edge.
577 BRep_Tool::Range(E, f, l);
578 double du = (l - f) / (NbPoints - 1);
579 //MESSAGE("************* Degenerated edge! *****************");
581 TopoDS_Vertex V1, V2;
582 TopExp::Vertices(E, V1, V2);
583 gp_Pnt P = BRep_Tool::Pnt(V1);
585 const SMDS_MeshNode * idPrev = idFirst;
586 for (int i = 2; i < NbPoints; i++)
588 double param = f + (i - 1) * du;
589 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
590 meshDS->SetNodeOnEdge(node, shapeID, param);
592 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
593 meshDS->SetMeshElementOnShape(edge, shapeID);
596 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
597 meshDS->SetMeshElementOnShape(edge, shapeID);
602 //=============================================================================
604 * See comments in SMESH_Algo.cxx
606 //=============================================================================
608 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis(
609 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
611 _usedHypList.clear();
612 _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
613 int nbHyp = _usedHypList.size();
617 // Check, if propagated from some other edge
618 if (aShape.ShapeType() == TopAbs_EDGE &&
619 aMesh.IsPropagatedHypothesis(aShape, _mainEdge))
621 // Propagation of 1D hypothesis from <aMainEdge> on this edge
622 //_usedHypList = GetAppliedHypothesis(aMesh, _mainEdge); // copy
623 // use a general method in order not to nullify _mainEdge
624 _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, _mainEdge); // copy
625 nbHyp = _usedHypList.size();
630 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
631 for (; ancIt.More(); ancIt.Next())
633 const TopoDS_Shape& ancestor = ancIt.Value();
634 _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
635 nbHyp = _usedHypList.size();
641 _usedHypList.clear(); //only one compatible hypothesis allowed
645 //=============================================================================
649 //=============================================================================
651 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
656 //=============================================================================
660 //=============================================================================
662 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
667 //=============================================================================
671 //=============================================================================
673 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
675 return hyp.SaveTo( save );
678 //=============================================================================
682 //=============================================================================
684 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
686 return hyp.LoadFrom( load );