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 "SMESH_Gen.hxx"
34 #include "SMESH_Mesh.hxx"
36 #include "StdMeshers_LocalLength.hxx"
37 #include "StdMeshers_NumberOfSegments.hxx"
38 #include "StdMeshers_Arithmetic1D.hxx"
39 #include "StdMeshers_StartEndLength.hxx"
40 #include "StdMeshers_Deflection1D.hxx"
42 #include "SMDS_MeshElement.hxx"
43 #include "SMDS_MeshNode.hxx"
44 #include "SMDS_EdgePosition.hxx"
45 #include "SMESH_subMesh.hxx"
47 #include "utilities.h"
49 #include <BRep_Tool.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <GeomAdaptor_Curve.hxx>
54 #include <GCPnts_AbscissaPoint.hxx>
55 #include <GCPnts_UniformAbscissa.hxx>
56 #include <GCPnts_UniformDeflection.hxx>
57 #include <Standard_ErrorHandler.hxx>
58 #include <Precision.hxx>
61 //#include <algorithm>
63 //=============================================================================
67 //=============================================================================
69 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
70 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
72 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
74 _shapeType = (1 << TopAbs_EDGE);
76 _compatibleHypothesis.push_back("LocalLength");
77 _compatibleHypothesis.push_back("NumberOfSegments");
78 _compatibleHypothesis.push_back("StartEndLength");
79 _compatibleHypothesis.push_back("Deflection1D");
80 _compatibleHypothesis.push_back("Arithmetic1D");
83 //=============================================================================
87 //=============================================================================
89 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
93 //=============================================================================
97 //=============================================================================
99 bool StdMeshers_Regular_1D::CheckHypothesis
101 const TopoDS_Shape& aShape,
102 SMESH_Hypothesis::Hypothesis_Status& aStatus)
106 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
107 if (hyps.size() == 0)
109 aStatus = SMESH_Hypothesis::HYP_MISSING;
110 return false; // can't work without a hypothesis
113 // use only the first hypothesis
114 const SMESHDS_Hypothesis *theHyp = hyps.front();
116 string hypName = theHyp->GetName();
118 if (hypName == "LocalLength")
120 const StdMeshers_LocalLength * hyp =
121 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
123 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
124 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
125 _hypType = LOCAL_LENGTH;
126 aStatus = SMESH_Hypothesis::HYP_OK;
129 else if (hypName == "NumberOfSegments")
131 const StdMeshers_NumberOfSegments * hyp =
132 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
134 _value[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
135 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
136 ASSERT( _value[ NB_SEGMENTS_IND ] > 0 );
137 _hypType = NB_SEGMENTS;
138 aStatus = SMESH_Hypothesis::HYP_OK;
141 else if (hypName == "Arithmetic1D")
143 const StdMeshers_Arithmetic1D * hyp =
144 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
146 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
147 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
148 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
149 _hypType = ARITHMETIC_1D;
150 aStatus = SMESH_Hypothesis::HYP_OK;
153 else if (hypName == "StartEndLength")
155 const StdMeshers_StartEndLength * hyp =
156 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
158 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
159 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
160 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
161 _hypType = BEG_END_LENGTH;
162 aStatus = SMESH_Hypothesis::HYP_OK;
165 else if (hypName == "Deflection1D")
167 const StdMeshers_Deflection1D * hyp =
168 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
170 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
171 ASSERT( _value[ DEFLECTION_IND ] > 0 );
172 _hypType = DEFLECTION;
173 aStatus = SMESH_Hypothesis::HYP_OK;
176 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
178 return ( _hypType != NONE );
181 //=======================================================================
182 //function : compensateError
183 //purpose : adjust theParams so that the last segment length == an
184 //=======================================================================
186 static void compensateError(double a1, double an,
187 double U1, double Un,
189 GeomAdaptor_Curve& C3d,
190 list<double> & theParams)
192 int i, nPar = theParams.size();
193 if ( a1 + an < length && nPar > 1 )
195 list<double>::reverse_iterator itU = theParams.rbegin();
197 // dist from the last point to the edge end <Un>, it should be equal <an>
198 double Ln = GCPnts_AbscissaPoint::Length( C3d, Ul, Un );
199 double dLn = an - Ln; // error of <an>
200 if ( Abs( dLn ) <= Precision::Confusion() )
202 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
203 double dUn = dLn * Abs( Un - U1 ) / length; // parametric error of <an>
204 if ( dUn < 0.5 * dU ) { // last segment is a bit shorter than it should
205 dUn = -dUn; // move the last parameter to the edge beginning
207 else { // last segment is much shorter than it should -> remove the last param and
208 theParams.pop_back(); nPar--; // move the rest points toward the edge end
209 Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
210 dUn = ( an - Ln ) * Abs( Un - U1 ) / length;
211 if ( dUn < 0.5 * dU )
216 double q = dUn / ( nPar - 1 );
217 for ( itU = theParams.rbegin(), i = 1; i < nPar; itU++, i++ ) {
224 //=============================================================================
228 //=============================================================================
229 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
230 list<double> & theParams,
231 const bool theReverse) const
236 Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
237 GeomAdaptor_Curve C3d(Curve);
239 double length = EdgeLength(theEdge);
247 if ( _hypType == LOCAL_LENGTH )
249 double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
251 nbseg = 1; // degenerated edge
252 eltSize = length / nbseg;
256 double epsilon = 0.001;
257 if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
259 double scale = _value[ SCALE_FACTOR_IND ];
262 double alpha = pow( scale , 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
263 double factor = (l - f) / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
265 int i, NbPoints = 1 + (int) _value[ NB_SEGMENTS_IND ];
266 for ( i = 2; i < NbPoints; i++ )
268 double param = f + factor * (1 - pow(alpha, i - 1));
269 theParams.push_back( param );
275 eltSize = length / _value[ NB_SEGMENTS_IND ];
279 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
280 if ( !Discret.IsDone() )
283 int NbPoints = Discret.NbPoints();
284 for ( int i = 2; i < NbPoints; i++ )
286 double param = Discret.Parameter(i);
287 theParams.push_back( param );
289 compensateError( eltSize, eltSize, f, l, length, C3d, theParams ); // for PAL9899
293 case BEG_END_LENGTH: {
295 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
297 double a1 = _value[ BEG_LENGTH_IND ];
298 double an = _value[ END_LENGTH_IND ];
299 double q = ( length - a1 ) / ( length - an );
301 double U1 = theReverse ? l : f;
302 double Un = theReverse ? f : l;
304 double eltSize = theReverse ? -a1 : a1;
306 // computes a point on a curve <C3d> at the distance <eltSize>
307 // from the point of parameter <param>.
308 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
309 if ( !Discret.IsDone() ) break;
310 param = Discret.Parameter();
311 if ( param > f && param < l )
312 theParams.push_back( param );
317 compensateError( a1, an, U1, Un, length, C3d, theParams );
321 case ARITHMETIC_1D: {
323 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
325 double a1 = _value[ BEG_LENGTH_IND ];
326 double an = _value[ END_LENGTH_IND ];
328 double q = ( an - a1 ) / ( 2 *length/( a1 + an ) - 1 );
329 int n = int( 1 + ( an - a1 ) / q );
331 double U1 = theReverse ? l : f;
332 double Un = theReverse ? f : l;
339 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
340 // computes a point on a curve <C3d> at the distance <eltSize>
341 // from the point of parameter <param>.
342 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
343 if ( !Discret.IsDone() ) break;
344 param = Discret.Parameter();
345 if ( param > f && param < l )
346 theParams.push_back( param );
351 compensateError( a1, an, U1, Un, length, C3d, theParams );
358 GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
359 if ( !Discret.IsDone() )
362 int NbPoints = Discret.NbPoints();
363 for ( int i = 2; i < NbPoints; i++ )
365 double param = Discret.Parameter(i);
366 theParams.push_back( param );
378 //=============================================================================
382 //=============================================================================
384 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
386 MESSAGE("StdMeshers_Regular_1D::Compute");
388 if ( _hypType == NONE )
391 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
392 aMesh.GetSubMesh(aShape);
394 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
395 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
398 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
400 TopoDS_Vertex VFirst, VLast;
401 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
403 ASSERT(!VFirst.IsNull());
404 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
407 MESSAGE (" NO NODE BUILT ON VERTEX ");
410 const SMDS_MeshNode * idFirst = lid->next();
412 ASSERT(!VLast.IsNull());
413 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
416 MESSAGE (" NO NODE BUILT ON VERTEX ");
419 const SMDS_MeshNode * idLast = lid->next();
423 list< double > params;
424 bool reversed = false;
425 if ( !_mainEdge.IsNull() )
426 reversed = aMesh.IsReversedInChain( EE, _mainEdge );
428 if ( ! computeInternalParameters( E, params, reversed ))
431 catch ( Standard_Failure ) {
435 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
436 // only internal nodes receive an edge position with param on curve
438 const SMDS_MeshNode * idPrev = idFirst;
440 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
443 gp_Pnt P = Curve->Value(param);
445 //Add the Node in the DataStructure
446 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
447 meshDS->SetNodeOnEdge(node, E);
449 // **** edgePosition associe au point = param.
450 SMDS_EdgePosition* epos =
451 dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
452 epos->SetUParameter(param);
454 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
455 meshDS->SetMeshElementOnShape(edge, E);
458 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
459 meshDS->SetMeshElementOnShape(edge, E);
463 // Edge is a degenerated Edge : We put n = 5 points on the edge.
465 BRep_Tool::Range(E, f, l);
466 double du = (l - f) / (NbPoints - 1);
467 //MESSAGE("************* Degenerated edge! *****************");
469 TopoDS_Vertex V1, V2;
470 TopExp::Vertices(E, V1, V2);
471 gp_Pnt P = BRep_Tool::Pnt(V1);
473 const SMDS_MeshNode * idPrev = idFirst;
474 for (int i = 2; i < NbPoints; i++)
476 double param = f + (i - 1) * du;
477 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
478 meshDS->SetNodeOnEdge(node, E);
480 SMDS_EdgePosition* epos =
481 dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
482 epos->SetUParameter(param);
484 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
485 meshDS->SetMeshElementOnShape(edge, E);
488 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
489 meshDS->SetMeshElementOnShape(edge, E);
494 //=============================================================================
496 * See comments in SMESH_Algo.cxx
498 //=============================================================================
500 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis(
501 SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
503 _usedHypList.clear();
504 _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
505 int nbHyp = _usedHypList.size();
509 // Check, if propagated from some other edge
510 if (aShape.ShapeType() == TopAbs_EDGE &&
511 aMesh.IsPropagatedHypothesis(aShape, _mainEdge))
513 // Propagation of 1D hypothesis from <aMainEdge> on this edge
514 //_usedHypList = GetAppliedHypothesis(aMesh, _mainEdge); // copy
515 // use a general method in order not to nullify _mainEdge
516 _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, _mainEdge); // copy
517 nbHyp = _usedHypList.size();
522 TopTools_ListIteratorOfListOfShape ancIt( aMesh.GetAncestors( aShape ));
523 for (; ancIt.More(); ancIt.Next())
525 const TopoDS_Shape& ancestor = ancIt.Value();
526 _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
527 nbHyp = _usedHypList.size();
533 _usedHypList.clear(); //only one compatible hypothesis allowed
537 //=============================================================================
541 //=============================================================================
543 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
548 //=============================================================================
552 //=============================================================================
554 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
559 //=============================================================================
563 //=============================================================================
565 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
567 return hyp.SaveTo( save );
570 //=============================================================================
574 //=============================================================================
576 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
578 return hyp.LoadFrom( load );