1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : StdMeshers_Regular_1D.cxx
24 // Moved here from SMESH_Regular_1D.cxx
25 // Author : Paul RASCLE, EDF
28 #include "StdMeshers_Regular_1D.hxx"
30 #include "SMDS_MeshElement.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMESH_Comment.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_HypoFilter.hxx"
35 #include "SMESH_Mesh.hxx"
36 #include "SMESH_subMesh.hxx"
37 #include "SMESH_subMeshEventListener.hxx"
38 #include "StdMeshers_Adaptive1D.hxx"
39 #include "StdMeshers_Arithmetic1D.hxx"
40 #include "StdMeshers_Geometric1D.hxx"
41 #include "StdMeshers_AutomaticLength.hxx"
42 #include "StdMeshers_Deflection1D.hxx"
43 #include "StdMeshers_Distribution.hxx"
44 #include "StdMeshers_FixedPoints1D.hxx"
45 #include "StdMeshers_LocalLength.hxx"
46 #include "StdMeshers_MaxLength.hxx"
47 #include "StdMeshers_NumberOfSegments.hxx"
48 #include "StdMeshers_Propagation.hxx"
49 #include "StdMeshers_SegmentLengthAroundVertex.hxx"
50 #include "StdMeshers_StartEndLength.hxx"
52 #include "Utils_SALOME_Exception.hxx"
53 #include "utilities.h"
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>
62 #include <TopExp_Explorer.hxx>
64 #include <TopoDS_Edge.hxx>
65 #include <TopoDS_Vertex.hxx>
72 //=============================================================================
76 //=============================================================================
78 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
80 :SMESH_1D_Algo(hypId, studyId, gen)
82 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
84 _shapeType = (1 << TopAbs_EDGE);
87 _compatibleHypothesis.push_back("LocalLength");
88 _compatibleHypothesis.push_back("MaxLength");
89 _compatibleHypothesis.push_back("NumberOfSegments");
90 _compatibleHypothesis.push_back("StartEndLength");
91 _compatibleHypothesis.push_back("Deflection1D");
92 _compatibleHypothesis.push_back("Arithmetic1D");
93 _compatibleHypothesis.push_back("GeometricProgression");
94 _compatibleHypothesis.push_back("FixedPoints1D");
95 _compatibleHypothesis.push_back("AutomaticLength");
96 _compatibleHypothesis.push_back("Adaptive1D");
98 _compatibleHypothesis.push_back("QuadraticMesh");
99 _compatibleHypothesis.push_back("Propagation");
100 _compatibleHypothesis.push_back("PropagOfDistribution");
103 //=============================================================================
107 //=============================================================================
109 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
113 //=============================================================================
117 //=============================================================================
119 bool StdMeshers_Regular_1D::CheckHypothesis( SMESH_Mesh& aMesh,
120 const TopoDS_Shape& aShape,
121 Hypothesis_Status& aStatus )
124 _quadraticMesh = false;
125 _onlyUnaryInput = true;
127 const list <const SMESHDS_Hypothesis * > & hyps =
128 GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliaryHyps=*/false);
130 // find non-auxiliary hypothesis
131 const SMESHDS_Hypothesis *theHyp = 0;
132 list <const SMESHDS_Hypothesis * >::const_iterator h = hyps.begin();
133 for ( ; h != hyps.end(); ++h ) {
134 if ( static_cast<const SMESH_Hypothesis*>(*h)->IsAuxiliary() ) {
135 if ( strcmp( "QuadraticMesh", (*h)->GetName() ) == 0 )
136 _quadraticMesh = true;
140 theHyp = *h; // use only the first non-auxiliary hypothesis
146 aStatus = SMESH_Hypothesis::HYP_MISSING;
147 return false; // can't work without a hypothesis
150 string hypName = theHyp->GetName();
152 if (hypName == "LocalLength")
154 const StdMeshers_LocalLength * hyp =
155 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
157 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
158 _value[ PRECISION_IND ] = hyp->GetPrecision();
159 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
160 _hypType = LOCAL_LENGTH;
161 aStatus = SMESH_Hypothesis::HYP_OK;
164 else if (hypName == "MaxLength")
166 const StdMeshers_MaxLength * hyp =
167 dynamic_cast <const StdMeshers_MaxLength * >(theHyp);
169 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
170 if ( hyp->GetUsePreestimatedLength() ) {
171 if ( int nbSeg = aMesh.GetGen()->GetBoundaryBoxSegmentation() )
172 _value[ BEG_LENGTH_IND ] = aMesh.GetShapeDiagonalSize() / nbSeg;
174 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
175 _hypType = MAX_LENGTH;
176 aStatus = SMESH_Hypothesis::HYP_OK;
179 else if (hypName == "NumberOfSegments")
181 const StdMeshers_NumberOfSegments * hyp =
182 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
184 _ivalue[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
185 ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
186 _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
187 switch (_ivalue[ DISTR_TYPE_IND ])
189 case StdMeshers_NumberOfSegments::DT_Scale:
190 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
191 _revEdgesIDs = hyp->GetReversedEdges();
193 case StdMeshers_NumberOfSegments::DT_TabFunc:
194 _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
195 _revEdgesIDs = hyp->GetReversedEdges();
197 case StdMeshers_NumberOfSegments::DT_ExprFunc:
198 _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
199 _revEdgesIDs = hyp->GetReversedEdges();
201 case StdMeshers_NumberOfSegments::DT_Regular:
207 if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
208 _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
209 _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
210 _hypType = NB_SEGMENTS;
211 aStatus = SMESH_Hypothesis::HYP_OK;
214 else if (hypName == "Arithmetic1D")
216 const StdMeshers_Arithmetic1D * hyp =
217 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
219 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
220 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
221 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
222 _hypType = ARITHMETIC_1D;
224 _revEdgesIDs = hyp->GetReversedEdges();
226 aStatus = SMESH_Hypothesis::HYP_OK;
229 else if (hypName == "GeometricProgression")
231 const StdMeshers_Geometric1D * hyp =
232 dynamic_cast <const StdMeshers_Geometric1D * >(theHyp);
234 _value[ BEG_LENGTH_IND ] = hyp->GetStartLength();
235 _value[ END_LENGTH_IND ] = hyp->GetCommonRatio();
236 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
237 _hypType = GEOMETRIC_1D;
239 _revEdgesIDs = hyp->GetReversedEdges();
241 aStatus = SMESH_Hypothesis::HYP_OK;
244 else if (hypName == "FixedPoints1D") {
245 _fpHyp = dynamic_cast <const StdMeshers_FixedPoints1D*>(theHyp);
247 _hypType = FIXED_POINTS_1D;
249 _revEdgesIDs = _fpHyp->GetReversedEdges();
251 aStatus = SMESH_Hypothesis::HYP_OK;
254 else if (hypName == "StartEndLength")
256 const StdMeshers_StartEndLength * hyp =
257 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
259 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
260 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
261 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
262 _hypType = BEG_END_LENGTH;
264 _revEdgesIDs = hyp->GetReversedEdges();
266 aStatus = SMESH_Hypothesis::HYP_OK;
269 else if (hypName == "Deflection1D")
271 const StdMeshers_Deflection1D * hyp =
272 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
274 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
275 ASSERT( _value[ DEFLECTION_IND ] > 0 );
276 _hypType = DEFLECTION;
277 aStatus = SMESH_Hypothesis::HYP_OK;
280 else if (hypName == "AutomaticLength")
282 StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
283 (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
285 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
286 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
287 _hypType = MAX_LENGTH;
288 aStatus = SMESH_Hypothesis::HYP_OK;
290 else if (hypName == "Adaptive1D")
292 _adaptiveHyp = dynamic_cast < const StdMeshers_Adaptive1D* >(theHyp);
293 ASSERT(_adaptiveHyp);
295 _onlyUnaryInput = false;
298 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
300 return ( _hypType != NONE );
303 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
304 double length, bool theReverse,
305 int nbSeg, Function& func,
306 list<double>& theParams)
309 //OSD::SetSignal( true );
314 MESSAGE( "computeParamByFunc" );
316 int nbPnt = 1 + nbSeg;
317 vector<double> x(nbPnt, 0.);
319 if (!buildDistribution(func, 0.0, 1.0, nbSeg, x, 1E-4))
322 MESSAGE( "Points:\n" );
324 for ( int i=0; i<=nbSeg; i++ )
326 sprintf( buf, "%f\n", float(x[i] ) );
332 // apply parameters in range [0,1] to the space of the curve
333 double prevU = first;
340 for( int i = 1; i < nbSeg; i++ )
342 double curvLength = length * (x[i] - x[i-1]) * sign;
343 GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
344 if ( !Discret.IsDone() )
346 double U = Discret.Parameter();
347 if ( U > first && U < last )
348 theParams.push_back( U );
359 //================================================================================
361 * \brief adjust internal node parameters so that the last segment length == an
362 * \param a1 - the first segment length
363 * \param an - the last segment length
364 * \param U1 - the first edge parameter
365 * \param Un - the last edge parameter
366 * \param length - the edge length
367 * \param C3d - the edge curve
368 * \param theParams - internal node parameters to adjust
369 * \param adjustNeighbors2an - to adjust length of segments next to the last one
370 * and not to remove parameters
372 //================================================================================
374 static void compensateError(double a1, double an,
375 double U1, double Un,
377 Adaptor3d_Curve& C3d,
378 list<double> & theParams,
379 bool adjustNeighbors2an = false)
381 int i, nPar = theParams.size();
382 if ( a1 + an <= length && nPar > 1 )
384 bool reverse = ( U1 > Un );
385 GCPnts_AbscissaPoint Discret(C3d, reverse ? an : -an, Un);
386 if ( !Discret.IsDone() )
388 double Utgt = Discret.Parameter(); // target value of the last parameter
389 list<double>::reverse_iterator itU = theParams.rbegin();
390 double Ul = *itU++; // real value of the last parameter
391 double dUn = Utgt - Ul; // parametric error of <an>
392 if ( Abs(dUn) <= Precision::Confusion() )
394 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
395 if ( adjustNeighbors2an || Abs(dUn) < 0.5 * dU ) { // last segment is a bit shorter than it should
396 // move the last parameter to the edge beginning
398 else { // last segment is much shorter than it should -> remove the last param and
399 theParams.pop_back(); nPar--; // move the rest points toward the edge end
400 dUn = Utgt - theParams.back();
403 if ( !adjustNeighbors2an )
405 double q = dUn / ( Utgt - Un ); // (signed) factor of segment length change
406 for ( itU = theParams.rbegin(), i = 1; i < nPar; i++ ) {
410 dUn = q * (*itU - prevU) * (prevU-U1)/(Un-U1);
413 else if ( nPar == 1 )
415 theParams.back() += dUn;
419 double q = dUn / ( nPar - 1 );
420 theParams.back() += dUn;
421 double sign = reverse ? -1 : 1;
422 double prevU = theParams.back();
423 itU = theParams.rbegin();
424 for ( ++itU, i = 2; i < nPar; ++itU, i++ ) {
425 double newU = *itU + dUn;
426 if ( newU*sign < prevU*sign ) {
430 else { // set U between prevU and next valid param
431 list<double>::reverse_iterator itU2 = itU;
434 while ( (*itU2)*sign > prevU*sign ) {
437 dU = ( *itU2 - prevU ) / nb;
438 while ( itU != itU2 ) {
448 //================================================================================
450 * \brief Class used to clean mesh on edges when 0D hyp modified.
451 * Common approach doesn't work when 0D algo is missing because the 0D hyp is
452 * considered as not participating in computation whereas it is used by 1D algo.
454 //================================================================================
456 // struct VertexEventListener : public SMESH_subMeshEventListener
458 // VertexEventListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
461 // * \brief Clean mesh on edges
462 // * \param event - algo_event or compute_event itself (of SMESH_subMesh)
463 // * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
464 // * \param subMesh - the submesh where the event occures
466 // void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
467 // EventListenerData*, const SMESH_Hypothesis*)
469 // if ( eventType == SMESH_subMesh::ALGO_EVENT) // all algo events
471 // subMesh->ComputeStateEngine( SMESH_subMesh::MODIF_ALGO_STATE );
474 // }; // struct VertexEventListener
476 //=============================================================================
478 * \brief Sets event listener to vertex submeshes
479 * \param subMesh - submesh where algo is set
481 * This method is called when a submesh gets HYP_OK algo_state.
482 * After being set, event listener is notified on each event of a submesh.
484 //=============================================================================
486 void StdMeshers_Regular_1D::SetEventListener(SMESH_subMesh* subMesh)
488 StdMeshers_Propagation::SetPropagationMgr( subMesh );
491 //=============================================================================
494 * \param subMesh - restored submesh
496 * This method is called only if a submesh has HYP_OK algo_state.
498 //=============================================================================
500 void StdMeshers_Regular_1D::SubmeshRestored(SMESH_subMesh* subMesh)
504 //=============================================================================
506 * \brief Return StdMeshers_SegmentLengthAroundVertex assigned to vertex
508 //=============================================================================
510 const StdMeshers_SegmentLengthAroundVertex*
511 StdMeshers_Regular_1D::getVertexHyp(SMESH_Mesh & theMesh,
512 const TopoDS_Vertex & theV)
514 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName("SegmentAroundVertex_0D"));
515 if ( const SMESH_Hypothesis * h = theMesh.GetHypothesis( theV, filter, true ))
517 SMESH_Algo* algo = const_cast< SMESH_Algo* >( static_cast< const SMESH_Algo* > ( h ));
518 const list <const SMESHDS_Hypothesis *> & hypList = algo->GetUsedHypothesis( theMesh, theV, 0 );
519 if ( !hypList.empty() && string("SegmentLengthAroundVertex") == hypList.front()->GetName() )
520 return static_cast<const StdMeshers_SegmentLengthAroundVertex*>( hypList.front() );
525 //================================================================================
527 * \brief Tune parameters to fit "SegmentLengthAroundVertex" hypothesis
528 * \param theC3d - wire curve
529 * \param theLength - curve length
530 * \param theParameters - internal nodes parameters to modify
531 * \param theVf - 1st vertex
532 * \param theVl - 2nd vertex
534 //================================================================================
536 void StdMeshers_Regular_1D::redistributeNearVertices (SMESH_Mesh & theMesh,
537 Adaptor3d_Curve & theC3d,
539 std::list< double > & theParameters,
540 const TopoDS_Vertex & theVf,
541 const TopoDS_Vertex & theVl)
543 double f = theC3d.FirstParameter(), l = theC3d.LastParameter();
544 int nPar = theParameters.size();
545 for ( int isEnd1 = 0; isEnd1 < 2; ++isEnd1 )
547 const TopoDS_Vertex & V = isEnd1 ? theVf : theVl;
548 const StdMeshers_SegmentLengthAroundVertex* hyp = getVertexHyp (theMesh, V );
550 double vertexLength = hyp->GetLength();
551 if ( vertexLength > theLength / 2.0 )
553 if ( isEnd1 ) { // to have a segment of interest at end of theParameters
554 theParameters.reverse();
557 if ( _hypType == NB_SEGMENTS )
559 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
561 else if ( nPar <= 3 )
564 vertexLength = -vertexLength;
565 GCPnts_AbscissaPoint Discret(theC3d, vertexLength, l);
566 if ( Discret.IsDone() ) {
568 theParameters.push_back( Discret.Parameter());
570 double L = GCPnts_AbscissaPoint::Length( theC3d, theParameters.back(), l);
571 if ( vertexLength < L / 2.0 )
572 theParameters.push_back( Discret.Parameter());
574 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
580 // recompute params between the last segment and a middle one.
581 // find size of a middle segment
582 int nHalf = ( nPar-1 ) / 2;
583 list< double >::reverse_iterator itU = theParameters.rbegin();
584 std::advance( itU, nHalf );
586 double Lm = GCPnts_AbscissaPoint::Length( theC3d, Um, *itU);
587 double L = GCPnts_AbscissaPoint::Length( theC3d, *itU, l);
588 static StdMeshers_Regular_1D* auxAlgo = 0;
590 auxAlgo = new StdMeshers_Regular_1D( _gen->GetANewId(), _studyId, _gen );
591 auxAlgo->_hypType = BEG_END_LENGTH;
593 auxAlgo->_value[ BEG_LENGTH_IND ] = Lm;
594 auxAlgo->_value[ END_LENGTH_IND ] = vertexLength;
595 double from = *itU, to = l;
597 std::swap( from, to );
598 std::swap( auxAlgo->_value[ BEG_LENGTH_IND ], auxAlgo->_value[ END_LENGTH_IND ]);
601 if ( auxAlgo->computeInternalParameters( theMesh, theC3d, L, from, to, params, false ))
603 if ( isEnd1 ) params.reverse();
604 while ( 1 + nHalf-- )
605 theParameters.pop_back();
606 theParameters.splice( theParameters.end(), params );
610 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
614 theParameters.reverse();
619 //=============================================================================
623 //=============================================================================
624 bool StdMeshers_Regular_1D::computeInternalParameters(SMESH_Mesh & theMesh,
625 Adaptor3d_Curve& theC3d,
629 list<double> & theParams,
630 const bool theReverse,
631 bool theConsiderPropagation)
635 double f = theFirstU, l = theLastU;
637 // Propagation Of Distribution
639 if ( !_mainEdge.IsNull() && _isPropagOfDistribution )
641 TopoDS_Edge mainEdge = TopoDS::Edge( _mainEdge ); // should not be a reference!
642 _gen->Compute( theMesh, mainEdge, /*aShapeOnly=*/true, /*anUpward=*/true);
644 SMESHDS_SubMesh* smDS = theMesh.GetMeshDS()->MeshElements( mainEdge );
646 return error("No mesh on the source edge of Propagation Of Distribution");
647 if ( smDS->NbNodes() < 1 )
648 return true; // 1 segment
650 vector< double > mainEdgeParams;
651 if ( ! SMESH_Algo::GetNodeParamOnEdge( theMesh.GetMeshDS(), mainEdge, mainEdgeParams ))
652 return error("Bad node parameters on the source edge of Propagation Of Distribution");
654 vector< double > segLen( mainEdgeParams.size() - 1 );
656 BRepAdaptor_Curve mainEdgeCurve( mainEdge );
657 for ( size_t i = 1; i < mainEdgeParams.size(); ++i )
659 segLen[ i-1 ] = GCPnts_AbscissaPoint::Length( mainEdgeCurve,
662 totalLen += segLen[ i-1 ];
664 for ( size_t i = 0; i < segLen.size(); ++i )
665 segLen[ i ] *= theLength / totalLen;
667 size_t iSeg = theReverse ? segLen.size()-1 : 0;
668 size_t dSeg = theReverse ? -1 : +1;
669 double param = theFirstU;
671 for ( int i = 0, nb = segLen.size()-1; i < nb; ++i, iSeg += dSeg )
673 GCPnts_AbscissaPoint Discret( theC3d, segLen[ iSeg ], param );
674 if ( !Discret.IsDone() ) break;
675 param = Discret.Parameter();
676 theParams.push_back( param );
679 if ( nbParams != segLen.size()-1 )
680 return error( SMESH_Comment("Can't divide into ") << segLen.size() << " segements");
682 compensateError( segLen[ theReverse ? segLen.size()-1 : 0 ],
683 segLen[ theReverse ? 0 : segLen.size()-1 ],
684 f, l, theLength, theC3d, theParams, true );
697 if ( _hypType == MAX_LENGTH )
699 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
701 nbseg = 1; // degenerated edge
702 eltSize = theLength / nbseg;
703 nbSegments = (int) nbseg;
705 else if ( _hypType == LOCAL_LENGTH )
707 // Local Length hypothesis
708 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
711 bool isFound = false;
712 if (theConsiderPropagation && !_mainEdge.IsNull()) // propagated from some other edge
714 // Advanced processing to assure equal number of segments in case of Propagation
715 SMESH_subMesh* sm = theMesh.GetSubMeshContaining(_mainEdge);
717 bool computed = sm->IsMeshComputed();
719 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
720 _gen->Compute( theMesh, _mainEdge, /*anUpward=*/true);
721 computed = sm->IsMeshComputed();
725 SMESHDS_SubMesh* smds = sm->GetSubMeshDS();
726 int nb_segments = smds->NbElements();
727 if (nbseg - 1 <= nb_segments && nb_segments <= nbseg + 1) {
734 if (!isFound) // not found by meshed edge in the propagation chain, use precision
736 double aPrecision = _value[ PRECISION_IND ];
737 double nbseg_prec = ceil((theLength / _value[ BEG_LENGTH_IND ]) - aPrecision);
738 if (nbseg_prec == (nbseg - 1)) nbseg--;
742 nbseg = 1; // degenerated edge
743 eltSize = theLength / nbseg;
744 nbSegments = (int) nbseg;
748 // Number Of Segments hypothesis
749 nbSegments = _ivalue[ NB_SEGMENTS_IND ];
750 if ( nbSegments < 1 ) return false;
751 if ( nbSegments == 1 ) return true;
753 switch (_ivalue[ DISTR_TYPE_IND ])
755 case StdMeshers_NumberOfSegments::DT_Scale:
757 double scale = _value[ SCALE_FACTOR_IND ];
759 if (fabs(scale - 1.0) < Precision::Confusion()) {
760 // special case to avoid division by zero
761 for (int i = 1; i < nbSegments; i++) {
762 double param = f + (l - f) * i / nbSegments;
763 theParams.push_back( param );
766 // general case of scale distribution
770 double alpha = pow(scale, 1.0 / (nbSegments - 1));
771 double factor = (l - f) / (1.0 - pow(alpha, nbSegments));
773 for (int i = 1; i < nbSegments; i++) {
774 double param = f + factor * (1.0 - pow(alpha, i));
775 theParams.push_back( param );
778 const double lenFactor = theLength/(l-f);
779 list<double>::iterator u = theParams.begin(), uEnd = theParams.end();
780 for ( ; u != uEnd; ++u )
782 GCPnts_AbscissaPoint Discret( theC3d, ((*u)-f) * lenFactor, f );
783 if ( Discret.IsDone() )
784 *u = Discret.Parameter();
789 case StdMeshers_NumberOfSegments::DT_TabFunc:
791 FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
792 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
793 _ivalue[ NB_SEGMENTS_IND ], func,
797 case StdMeshers_NumberOfSegments::DT_ExprFunc:
799 FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
800 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
801 _ivalue[ NB_SEGMENTS_IND ], func,
805 case StdMeshers_NumberOfSegments::DT_Regular:
806 eltSize = theLength / nbSegments;
812 GCPnts_UniformAbscissa Discret(theC3d, eltSize, f, l);
813 if ( !Discret.IsDone() )
814 return error( "GCPnts_UniformAbscissa failed");
816 int NbPoints = Min( Discret.NbPoints(), nbSegments + 1 );
817 for ( int i = 2; i < NbPoints; i++ ) // skip 1st and last points
819 double param = Discret.Parameter(i);
820 theParams.push_back( param );
822 compensateError( eltSize, eltSize, f, l, theLength, theC3d, theParams, true ); // for PAL9899
826 case BEG_END_LENGTH: {
828 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = theLength
830 double a1 = _value[ BEG_LENGTH_IND ];
831 double an = _value[ END_LENGTH_IND ];
832 double q = ( theLength - a1 ) / ( theLength - an );
833 if ( q < theLength/1e6 || 1.01*theLength < a1 + an)
834 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
835 "for an edge of length "<<theLength);
837 double U1 = theReverse ? l : f;
838 double Un = theReverse ? f : l;
840 double eltSize = theReverse ? -a1 : a1;
842 // computes a point on a curve <theC3d> at the distance <eltSize>
843 // from the point of parameter <param>.
844 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
845 if ( !Discret.IsDone() ) break;
846 param = Discret.Parameter();
847 if ( f < param && param < l )
848 theParams.push_back( param );
853 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
854 if (theReverse) theParams.reverse(); // NPAL18025
858 case ARITHMETIC_1D: {
860 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = theLength
862 double a1 = _value[ BEG_LENGTH_IND ];
863 double an = _value[ END_LENGTH_IND ];
864 if ( 1.01*theLength < a1 + an)
865 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
866 "for an edge of length "<<theLength);
868 double q = ( an - a1 ) / ( 2 *theLength/( a1 + an ) - 1 );
869 int n = int(fabs(q) > numeric_limits<double>::min() ? ( 1+( an-a1 )/q ) : ( 1+theLength/a1 ));
871 double U1 = theReverse ? l : f;
872 double Un = theReverse ? f : l;
879 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
880 // computes a point on a curve <theC3d> at the distance <eltSize>
881 // from the point of parameter <param>.
882 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
883 if ( !Discret.IsDone() ) break;
884 param = Discret.Parameter();
885 if ( param > f && param < l )
886 theParams.push_back( param );
891 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
892 if (theReverse) theParams.reverse(); // NPAL18025
899 double a1 = _value[ BEG_LENGTH_IND ], an;
900 double q = _value[ END_LENGTH_IND ];
902 double U1 = theReverse ? l : f;
903 double Un = theReverse ? f : l;
911 // computes a point on a curve <theC3d> at the distance <eltSize>
912 // from the point of parameter <param>.
913 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
914 if ( !Discret.IsDone() ) break;
915 param = Discret.Parameter();
916 if ( f < param && param < l )
917 theParams.push_back( param );
926 if ( Abs( param - Un ) < 0.2 * Abs( param - theParams.back() ))
928 compensateError( a1, eltSize, U1, Un, theLength, theC3d, theParams );
930 else if ( Abs( Un - theParams.back() ) <
931 0.2 * Abs( theParams.back() - *(--theParams.rbegin())))
933 theParams.pop_back();
934 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
937 if (theReverse) theParams.reverse(); // NPAL18025
942 case FIXED_POINTS_1D: {
943 const std::vector<double>& aPnts = _fpHyp->GetPoints();
944 const std::vector<int>& nbsegs = _fpHyp->GetNbSegments();
946 TColStd_SequenceOfReal Params;
947 for(; i<aPnts.size(); i++) {
948 if( aPnts[i]<0.0001 || aPnts[i]>0.9999 ) continue;
950 bool IsExist = false;
951 for(; j<=Params.Length(); j++) {
952 if( fabs(aPnts[i]-Params.Value(j)) < 1e-4 ) {
956 if( aPnts[i]<Params.Value(j) ) break;
958 if(!IsExist) Params.InsertBefore(j,aPnts[i]);
960 double par2, par1, lp;
969 double eltSize, segmentSize = 0.;
970 double currAbscissa = 0;
971 for(i=0; i<Params.Length(); i++) {
972 int nbseg = ( i > nbsegs.size()-1 ) ? nbsegs[0] : nbsegs[i];
973 segmentSize = Params.Value(i+1)*theLength - currAbscissa;
974 currAbscissa += segmentSize;
975 GCPnts_AbscissaPoint APnt(theC3d, sign*segmentSize, par1);
977 return error( "GCPnts_AbscissaPoint failed");
978 par2 = APnt.Parameter();
979 eltSize = segmentSize/nbseg;
980 GCPnts_UniformAbscissa Discret(theC3d, eltSize, par1, par2);
982 Discret.Initialize(theC3d, eltSize, par2, par1);
984 Discret.Initialize(theC3d, eltSize, par1, par2);
985 if ( !Discret.IsDone() )
986 return error( "GCPnts_UniformAbscissa failed");
987 int NbPoints = Discret.NbPoints();
988 list<double> tmpParams;
989 for(int i=2; i<NbPoints; i++) {
990 double param = Discret.Parameter(i);
991 tmpParams.push_back( param );
994 compensateError( eltSize, eltSize, par2, par1, segmentSize, theC3d, tmpParams );
998 compensateError( eltSize, eltSize, par1, par2, segmentSize, theC3d, tmpParams );
1000 list<double>::iterator itP = tmpParams.begin();
1001 for(; itP != tmpParams.end(); itP++) {
1002 theParams.push_back( *(itP) );
1004 theParams.push_back( par2 );
1009 int nbseg = ( nbsegs.size() > Params.Length() ) ? nbsegs[Params.Length()] : nbsegs[0];
1010 segmentSize = theLength - currAbscissa;
1011 eltSize = segmentSize/nbseg;
1012 GCPnts_UniformAbscissa Discret;
1014 Discret.Initialize(theC3d, eltSize, par1, lp);
1016 Discret.Initialize(theC3d, eltSize, lp, par1);
1017 if ( !Discret.IsDone() )
1018 return error( "GCPnts_UniformAbscissa failed");
1019 int NbPoints = Discret.NbPoints();
1020 list<double> tmpParams;
1021 for(int i=2; i<NbPoints; i++) {
1022 double param = Discret.Parameter(i);
1023 tmpParams.push_back( param );
1026 compensateError( eltSize, eltSize, lp, par1, segmentSize, theC3d, tmpParams );
1027 tmpParams.reverse();
1030 compensateError( eltSize, eltSize, par1, lp, segmentSize, theC3d, tmpParams );
1032 list<double>::iterator itP = tmpParams.begin();
1033 for(; itP != tmpParams.end(); itP++) {
1034 theParams.push_back( *(itP) );
1038 theParams.reverse(); // NPAL18025
1045 GCPnts_UniformDeflection Discret(theC3d, _value[ DEFLECTION_IND ], f, l, true);
1046 if ( !Discret.IsDone() )
1049 int NbPoints = Discret.NbPoints();
1050 for ( int i = 2; i < NbPoints; i++ )
1052 double param = Discret.Parameter(i);
1053 theParams.push_back( param );
1064 //=============================================================================
1068 //=============================================================================
1070 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
1072 if ( _hypType == NONE )
1075 if ( _hypType == ADAPTIVE )
1077 _adaptiveHyp->GetAlgo()->InitComputeError();
1078 _adaptiveHyp->GetAlgo()->Compute( theMesh, theShape );
1079 return error( _adaptiveHyp->GetAlgo()->GetComputeError() );
1082 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1084 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
1085 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
1086 int shapeID = meshDS->ShapeToIndex( E );
1089 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
1091 TopoDS_Vertex VFirst, VLast;
1092 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
1094 ASSERT(!VFirst.IsNull());
1095 ASSERT(!VLast.IsNull());
1096 const SMDS_MeshNode * idFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
1097 const SMDS_MeshNode * idLast = SMESH_Algo::VertexNode( VLast, meshDS );
1098 if (!idFirst || !idLast)
1099 return error( COMPERR_BAD_INPUT_MESH, "No node on vertex");
1101 // remove elements created by e.g. patern mapping (PAL21999)
1102 // CLEAN event is incorrectly ptopagated seemingly due to Propagation hyp
1103 // so TEMPORARY solution is to clean the submesh manually
1104 //theMesh.GetSubMesh(theShape)->ComputeStateEngine( SMESH_subMesh::CLEAN );
1105 if (SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(theShape))
1107 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1109 meshDS->RemoveFreeElement(ite->next(), subMeshDS);
1110 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1111 while (itn->more()) {
1112 const SMDS_MeshNode * node = itn->next();
1113 if ( node->NbInverseElements() == 0 )
1114 meshDS->RemoveFreeNode(node, subMeshDS);
1116 meshDS->RemoveNode(node);
1120 if (!Curve.IsNull())
1122 list< double > params;
1123 bool reversed = false;
1124 if ( theMesh.GetShapeToMesh().ShapeType() >= TopAbs_WIRE ) {
1125 // if the shape to mesh is WIRE or EDGE
1126 reversed = ( EE.Orientation() == TopAbs_REVERSED );
1128 if ( !_mainEdge.IsNull() ) {
1129 // take into account reversing the edge the hypothesis is propagated from
1130 reversed = ( _mainEdge.Orientation() == TopAbs_REVERSED );
1131 int mainID = meshDS->ShapeToIndex(_mainEdge);
1132 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), mainID) != _revEdgesIDs.end())
1133 reversed = !reversed;
1135 // take into account this edge reversing
1136 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), shapeID) != _revEdgesIDs.end())
1137 reversed = !reversed;
1139 BRepAdaptor_Curve C3d( E );
1140 double length = EdgeLength( E );
1141 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, reversed, true )) {
1144 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1146 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
1147 // only internal nodes receive an edge position with param on curve
1149 const SMDS_MeshNode * idPrev = idFirst;
1162 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++) {
1163 double param = *itU;
1164 gp_Pnt P = Curve->Value(param);
1166 //Add the Node in the DataStructure
1167 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1168 meshDS->SetNodeOnEdge(node, shapeID, param);
1170 if(_quadraticMesh) {
1171 // create medium node
1172 double prm = ( parPrev + param )/2;
1173 gp_Pnt PM = Curve->Value(prm);
1174 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1175 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1176 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1177 meshDS->SetMeshElementOnShape(edge, shapeID);
1180 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1181 meshDS->SetMeshElementOnShape(edge, shapeID);
1187 if(_quadraticMesh) {
1188 double prm = ( parPrev + parLast )/2;
1189 gp_Pnt PM = Curve->Value(prm);
1190 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1191 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1192 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1193 meshDS->SetMeshElementOnShape(edge, shapeID);
1196 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
1197 meshDS->SetMeshElementOnShape(edge, shapeID);
1202 //MESSAGE("************* Degenerated edge! *****************");
1204 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1205 const int NbPoints = 5;
1206 BRep_Tool::Range( E, f, l ); // PAL15185
1207 double du = (l - f) / (NbPoints - 1);
1209 gp_Pnt P = BRep_Tool::Pnt(VFirst);
1211 const SMDS_MeshNode * idPrev = idFirst;
1212 for (int i = 2; i < NbPoints; i++) {
1213 double param = f + (i - 1) * du;
1214 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1215 if(_quadraticMesh) {
1216 // create medium node
1217 double prm = param - du/2.;
1218 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1219 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1220 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1221 meshDS->SetMeshElementOnShape(edge, shapeID);
1224 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1225 meshDS->SetMeshElementOnShape(edge, shapeID);
1227 meshDS->SetNodeOnEdge(node, shapeID, param);
1230 if(_quadraticMesh) {
1231 // create medium node
1232 double prm = l - du/2.;
1233 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1234 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1235 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1236 meshDS->SetMeshElementOnShape(edge, shapeID);
1239 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
1240 meshDS->SetMeshElementOnShape(edge, shapeID);
1247 //=============================================================================
1251 //=============================================================================
1253 bool StdMeshers_Regular_1D::Evaluate(SMESH_Mesh & theMesh,
1254 const TopoDS_Shape & theShape,
1255 MapShapeNbElems& aResMap)
1257 if ( _hypType == NONE )
1260 if ( _hypType == ADAPTIVE )
1262 _adaptiveHyp->GetAlgo()->InitComputeError();
1263 _adaptiveHyp->GetAlgo()->Evaluate( theMesh, theShape, aResMap );
1264 return error( _adaptiveHyp->GetAlgo()->GetComputeError() );
1267 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
1268 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
1271 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
1273 TopoDS_Vertex VFirst, VLast;
1274 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
1276 ASSERT(!VFirst.IsNull());
1277 ASSERT(!VLast.IsNull());
1279 std::vector<int> aVec(SMDSEntity_Last,0);
1281 if (!Curve.IsNull()) {
1282 list< double > params;
1284 BRepAdaptor_Curve C3d( E );
1285 double length = EdgeLength( E );
1286 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, false, true )) {
1287 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1288 aResMap.insert(std::make_pair(sm,aVec));
1289 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1290 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
1293 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1295 if(_quadraticMesh) {
1296 aVec[SMDSEntity_Node] = 2*params.size() + 1;
1297 aVec[SMDSEntity_Quad_Edge] = params.size() + 1;
1300 aVec[SMDSEntity_Node] = params.size();
1301 aVec[SMDSEntity_Edge] = params.size() + 1;
1306 //MESSAGE("************* Degenerated edge! *****************");
1307 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1308 if(_quadraticMesh) {
1309 aVec[SMDSEntity_Node] = 11;
1310 aVec[SMDSEntity_Quad_Edge] = 6;
1313 aVec[SMDSEntity_Node] = 5;
1314 aVec[SMDSEntity_Edge] = 6;
1318 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1319 aResMap.insert(std::make_pair(sm,aVec));
1325 //=============================================================================
1327 * See comments in SMESH_Algo.cxx
1329 //=============================================================================
1331 const list <const SMESHDS_Hypothesis *> &
1332 StdMeshers_Regular_1D::GetUsedHypothesis(SMESH_Mesh & aMesh,
1333 const TopoDS_Shape & aShape,
1334 const bool ignoreAuxiliary)
1336 _usedHypList.clear();
1337 _mainEdge.Nullify();
1339 SMESH_HypoFilter auxiliaryFilter, compatibleFilter;
1340 auxiliaryFilter.Init( SMESH_HypoFilter::IsAuxiliary() );
1341 InitCompatibleHypoFilter( compatibleFilter, /*ignoreAux=*/true );
1343 // get non-auxiliary assigned directly to aShape
1344 int nbHyp = aMesh.GetHypotheses( aShape, compatibleFilter, _usedHypList, false );
1346 if (nbHyp == 0 && aShape.ShapeType() == TopAbs_EDGE)
1348 // Check, if propagated from some other edge
1349 _mainEdge = StdMeshers_Propagation::GetPropagationSource( aMesh, aShape,
1350 _isPropagOfDistribution );
1351 if ( !_mainEdge.IsNull() )
1353 // Propagation of 1D hypothesis from <aMainEdge> on this edge;
1354 // get non-auxiliary assigned to _mainEdge
1355 nbHyp = aMesh.GetHypotheses( _mainEdge, compatibleFilter, _usedHypList, true );
1359 if (nbHyp == 0) // nothing propagated nor assigned to aShape
1361 SMESH_Algo::GetUsedHypothesis( aMesh, aShape, ignoreAuxiliary );
1362 nbHyp = _usedHypList.size();
1366 // get auxiliary hyps from aShape
1367 aMesh.GetHypotheses( aShape, auxiliaryFilter, _usedHypList, true );
1369 if ( nbHyp > 1 && ignoreAuxiliary )
1370 _usedHypList.clear(); //only one compatible non-auxiliary hypothesis allowed
1372 return _usedHypList;
1375 //================================================================================
1377 * \brief Pass CancelCompute() to a child algorithm
1379 //================================================================================
1381 void StdMeshers_Regular_1D::CancelCompute()
1383 SMESH_Algo::CancelCompute();
1384 if ( _hypType == ADAPTIVE )
1385 _adaptiveHyp->GetAlgo()->CancelCompute();