1 // Copyright (C) 2007-2012 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"
29 #include "StdMeshers_Distribution.hxx"
31 #include "StdMeshers_Arithmetic1D.hxx"
32 #include "StdMeshers_AutomaticLength.hxx"
33 #include "StdMeshers_Deflection1D.hxx"
34 #include "StdMeshers_LocalLength.hxx"
35 #include "StdMeshers_MaxLength.hxx"
36 #include "StdMeshers_NumberOfSegments.hxx"
37 #include "StdMeshers_Propagation.hxx"
38 #include "StdMeshers_SegmentLengthAroundVertex.hxx"
39 #include "StdMeshers_StartEndLength.hxx"
41 #include "SMESH_Gen.hxx"
42 #include "SMESH_Mesh.hxx"
43 #include "SMESH_HypoFilter.hxx"
44 #include "SMESH_subMesh.hxx"
45 #include "SMESH_subMeshEventListener.hxx"
46 #include "SMESH_Comment.hxx"
48 #include "SMDS_MeshElement.hxx"
49 #include "SMDS_MeshNode.hxx"
51 #include "Utils_SALOME_Exception.hxx"
52 #include "utilities.h"
54 #include <BRepAdaptor_Curve.hxx>
55 #include <BRep_Tool.hxx>
56 #include <GCPnts_AbscissaPoint.hxx>
57 #include <GCPnts_UniformAbscissa.hxx>
58 #include <GCPnts_UniformDeflection.hxx>
59 #include <Precision.hxx>
61 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Vertex.hxx>
71 //=============================================================================
75 //=============================================================================
77 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
78 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
80 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
82 _shapeType = (1 << TopAbs_EDGE);
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");
94 _compatibleHypothesis.push_back("QuadraticMesh"); // auxiliary !!!
95 _compatibleHypothesis.push_back("Propagation"); // auxiliary !!!
98 //=============================================================================
102 //=============================================================================
104 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
108 //=============================================================================
112 //=============================================================================
114 bool StdMeshers_Regular_1D::CheckHypothesis
116 const TopoDS_Shape& aShape,
117 SMESH_Hypothesis::Hypothesis_Status& aStatus)
120 _quadraticMesh = false;
122 const list <const SMESHDS_Hypothesis * > & hyps =
123 GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliaryHyps=*/false);
125 // find non-auxiliary hypothesis
126 const SMESHDS_Hypothesis *theHyp = 0;
127 list <const SMESHDS_Hypothesis * >::const_iterator h = hyps.begin();
128 for ( ; h != hyps.end(); ++h ) {
129 if ( static_cast<const SMESH_Hypothesis*>(*h)->IsAuxiliary() ) {
130 if ( strcmp( "QuadraticMesh", (*h)->GetName() ) == 0 )
131 _quadraticMesh = true;
135 theHyp = *h; // use only the first non-auxiliary hypothesis
141 aStatus = SMESH_Hypothesis::HYP_MISSING;
142 return false; // can't work without a hypothesis
145 string hypName = theHyp->GetName();
147 if (hypName == "LocalLength")
149 const StdMeshers_LocalLength * hyp =
150 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
152 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
153 _value[ PRECISION_IND ] = hyp->GetPrecision();
154 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
155 _hypType = LOCAL_LENGTH;
156 aStatus = SMESH_Hypothesis::HYP_OK;
159 else if (hypName == "MaxLength")
161 const StdMeshers_MaxLength * hyp =
162 dynamic_cast <const StdMeshers_MaxLength * >(theHyp);
164 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
165 if ( hyp->GetUsePreestimatedLength() ) {
166 if ( int nbSeg = aMesh.GetGen()->GetBoundaryBoxSegmentation() )
167 _value[ BEG_LENGTH_IND ] = aMesh.GetShapeDiagonalSize() / nbSeg;
169 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
170 _hypType = MAX_LENGTH;
171 aStatus = SMESH_Hypothesis::HYP_OK;
174 else if (hypName == "NumberOfSegments")
176 const StdMeshers_NumberOfSegments * hyp =
177 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
179 _ivalue[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
180 ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
181 _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
182 switch (_ivalue[ DISTR_TYPE_IND ])
184 case StdMeshers_NumberOfSegments::DT_Scale:
185 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
186 _revEdgesIDs = hyp->GetReversedEdges();
188 case StdMeshers_NumberOfSegments::DT_TabFunc:
189 _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
190 _revEdgesIDs = hyp->GetReversedEdges();
192 case StdMeshers_NumberOfSegments::DT_ExprFunc:
193 _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
194 _revEdgesIDs = hyp->GetReversedEdges();
196 case StdMeshers_NumberOfSegments::DT_Regular:
202 if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
203 _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
204 _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
205 _hypType = NB_SEGMENTS;
206 aStatus = SMESH_Hypothesis::HYP_OK;
209 else if (hypName == "Arithmetic1D")
211 const StdMeshers_Arithmetic1D * hyp =
212 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
214 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
215 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
216 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
217 _hypType = ARITHMETIC_1D;
219 _revEdgesIDs = hyp->GetReversedEdges();
221 aStatus = SMESH_Hypothesis::HYP_OK;
224 else if (hypName == "FixedPoints1D") {
225 _fpHyp = dynamic_cast <const StdMeshers_FixedPoints1D*>(theHyp);
227 _hypType = FIXED_POINTS_1D;
229 _revEdgesIDs = _fpHyp->GetReversedEdges();
231 aStatus = SMESH_Hypothesis::HYP_OK;
234 else if (hypName == "StartEndLength")
236 const StdMeshers_StartEndLength * hyp =
237 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
239 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
240 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
241 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
242 _hypType = BEG_END_LENGTH;
244 _revEdgesIDs = hyp->GetReversedEdges();
246 aStatus = SMESH_Hypothesis::HYP_OK;
249 else if (hypName == "Deflection1D")
251 const StdMeshers_Deflection1D * hyp =
252 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
254 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
255 ASSERT( _value[ DEFLECTION_IND ] > 0 );
256 _hypType = DEFLECTION;
257 aStatus = SMESH_Hypothesis::HYP_OK;
260 else if (hypName == "AutomaticLength")
262 StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
263 (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
265 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
266 // _value[ BEG_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
267 // _value[ END_LENGTH_IND ] = Precision::Confusion(); // ?? or set to zero?
268 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
269 _hypType = MAX_LENGTH;
270 aStatus = SMESH_Hypothesis::HYP_OK;
273 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
275 return ( _hypType != NONE );
278 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
279 double length, bool theReverse,
280 int nbSeg, Function& func,
281 list<double>& theParams)
284 //OSD::SetSignal( true );
289 MESSAGE( "computeParamByFunc" );
291 int nbPnt = 1 + nbSeg;
292 vector<double> x(nbPnt, 0.);
294 if (!buildDistribution(func, 0.0, 1.0, nbSeg, x, 1E-4))
297 MESSAGE( "Points:\n" );
299 for ( int i=0; i<=nbSeg; i++ )
301 sprintf( buf, "%f\n", float(x[i] ) );
307 // apply parameters in range [0,1] to the space of the curve
308 double prevU = first;
315 for( int i = 1; i < nbSeg; i++ )
317 double curvLength = length * (x[i] - x[i-1]) * sign;
318 GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
319 if ( !Discret.IsDone() )
321 double U = Discret.Parameter();
322 if ( U > first && U < last )
323 theParams.push_back( U );
334 //================================================================================
336 * \brief adjust internal node parameters so that the last segment length == an
337 * \param a1 - the first segment length
338 * \param an - the last segment length
339 * \param U1 - the first edge parameter
340 * \param Un - the last edge parameter
341 * \param length - the edge length
342 * \param C3d - the edge curve
343 * \param theParams - internal node parameters to adjust
344 * \param adjustNeighbors2an - to adjust length of segments next to the last one
345 * and not to remove parameters
347 //================================================================================
349 static void compensateError(double a1, double an,
350 double U1, double Un,
352 Adaptor3d_Curve& C3d,
353 list<double> & theParams,
354 bool adjustNeighbors2an = false)
356 int i, nPar = theParams.size();
357 if ( a1 + an <= length && nPar > 1 )
359 bool reverse = ( U1 > Un );
360 GCPnts_AbscissaPoint Discret(C3d, reverse ? an : -an, Un);
361 if ( !Discret.IsDone() )
363 double Utgt = Discret.Parameter(); // target value of the last parameter
364 list<double>::reverse_iterator itU = theParams.rbegin();
365 double Ul = *itU++; // real value of the last parameter
366 double dUn = Utgt - Ul; // parametric error of <an>
367 if ( Abs(dUn) <= Precision::Confusion() )
369 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
370 if ( adjustNeighbors2an || Abs(dUn) < 0.5 * dU ) { // last segment is a bit shorter than it should
371 // move the last parameter to the edge beginning
373 else { // last segment is much shorter than it should -> remove the last param and
374 theParams.pop_back(); nPar--; // move the rest points toward the edge end
375 dUn = Utgt - theParams.back();
378 if ( !adjustNeighbors2an )
380 double q = dUn / ( Utgt - Un ); // (signed) factor of segment length change
381 for ( itU = theParams.rbegin(), i = 1; i < nPar; i++ ) {
385 dUn = q * (*itU - prevU) * (prevU-U1)/(Un-U1);
388 else if ( nPar == 1 )
390 theParams.back() += dUn;
394 double q = dUn / ( nPar - 1 );
395 theParams.back() += dUn;
396 double sign = reverse ? -1 : 1;
397 double prevU = theParams.back();
398 itU = theParams.rbegin();
399 for ( ++itU, i = 2; i < nPar; ++itU, i++ ) {
400 double newU = *itU + dUn;
401 if ( newU*sign < prevU*sign ) {
405 else { // set U between prevU and next valid param
406 list<double>::reverse_iterator itU2 = itU;
409 while ( (*itU2)*sign > prevU*sign ) {
412 dU = ( *itU2 - prevU ) / nb;
413 while ( itU != itU2 ) {
423 //================================================================================
425 * \brief Class used to clean mesh on edges when 0D hyp modified.
426 * Common approach doesn't work when 0D algo is missing because the 0D hyp is
427 * considered as not participating in computation whereas it is used by 1D algo.
429 //================================================================================
431 // struct VertexEventListener : public SMESH_subMeshEventListener
433 // VertexEventListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
436 // * \brief Clean mesh on edges
437 // * \param event - algo_event or compute_event itself (of SMESH_subMesh)
438 // * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
439 // * \param subMesh - the submesh where the event occures
441 // void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
442 // EventListenerData*, const SMESH_Hypothesis*)
444 // if ( eventType == SMESH_subMesh::ALGO_EVENT) // all algo events
446 // subMesh->ComputeStateEngine( SMESH_subMesh::MODIF_ALGO_STATE );
449 // }; // struct VertexEventListener
451 //=============================================================================
453 * \brief Sets event listener to vertex submeshes
454 * \param subMesh - submesh where algo is set
456 * This method is called when a submesh gets HYP_OK algo_state.
457 * After being set, event listener is notified on each event of a submesh.
459 //=============================================================================
461 void StdMeshers_Regular_1D::SetEventListener(SMESH_subMesh* subMesh)
463 StdMeshers_Propagation::SetPropagationMgr( subMesh );
466 //=============================================================================
469 * \param subMesh - restored submesh
471 * This method is called only if a submesh has HYP_OK algo_state.
473 //=============================================================================
475 void StdMeshers_Regular_1D::SubmeshRestored(SMESH_subMesh* subMesh)
479 //=============================================================================
481 * \brief Return StdMeshers_SegmentLengthAroundVertex assigned to vertex
483 //=============================================================================
485 const StdMeshers_SegmentLengthAroundVertex*
486 StdMeshers_Regular_1D::getVertexHyp(SMESH_Mesh & theMesh,
487 const TopoDS_Vertex & theV)
489 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName("SegmentAroundVertex_0D"));
490 if ( const SMESH_Hypothesis * h = theMesh.GetHypothesis( theV, filter, true ))
492 SMESH_Algo* algo = const_cast< SMESH_Algo* >( static_cast< const SMESH_Algo* > ( h ));
493 const list <const SMESHDS_Hypothesis *> & hypList = algo->GetUsedHypothesis( theMesh, theV, 0 );
494 if ( !hypList.empty() && string("SegmentLengthAroundVertex") == hypList.front()->GetName() )
495 return static_cast<const StdMeshers_SegmentLengthAroundVertex*>( hypList.front() );
500 //================================================================================
502 * \brief Tune parameters to fit "SegmentLengthAroundVertex" hypothesis
503 * \param theC3d - wire curve
504 * \param theLength - curve length
505 * \param theParameters - internal nodes parameters to modify
506 * \param theVf - 1st vertex
507 * \param theVl - 2nd vertex
509 //================================================================================
511 void StdMeshers_Regular_1D::redistributeNearVertices (SMESH_Mesh & theMesh,
512 Adaptor3d_Curve & theC3d,
514 std::list< double > & theParameters,
515 const TopoDS_Vertex & theVf,
516 const TopoDS_Vertex & theVl)
518 double f = theC3d.FirstParameter(), l = theC3d.LastParameter();
519 int nPar = theParameters.size();
520 for ( int isEnd1 = 0; isEnd1 < 2; ++isEnd1 )
522 const TopoDS_Vertex & V = isEnd1 ? theVf : theVl;
523 const StdMeshers_SegmentLengthAroundVertex* hyp = getVertexHyp (theMesh, V );
525 double vertexLength = hyp->GetLength();
526 if ( vertexLength > theLength / 2.0 )
528 if ( isEnd1 ) { // to have a segment of interest at end of theParameters
529 theParameters.reverse();
532 if ( _hypType == NB_SEGMENTS )
534 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
536 else if ( nPar <= 3 )
539 vertexLength = -vertexLength;
540 GCPnts_AbscissaPoint Discret(theC3d, vertexLength, l);
541 if ( Discret.IsDone() ) {
543 theParameters.push_back( Discret.Parameter());
545 double L = GCPnts_AbscissaPoint::Length( theC3d, theParameters.back(), l);
546 if ( vertexLength < L / 2.0 )
547 theParameters.push_back( Discret.Parameter());
549 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
555 // recompute params between the last segment and a middle one.
556 // find size of a middle segment
557 int nHalf = ( nPar-1 ) / 2;
558 list< double >::reverse_iterator itU = theParameters.rbegin();
559 std::advance( itU, nHalf );
561 double Lm = GCPnts_AbscissaPoint::Length( theC3d, Um, *itU);
562 double L = GCPnts_AbscissaPoint::Length( theC3d, *itU, l);
563 static StdMeshers_Regular_1D* auxAlgo = 0;
565 auxAlgo = new StdMeshers_Regular_1D( _gen->GetANewId(), _studyId, _gen );
566 auxAlgo->_hypType = BEG_END_LENGTH;
568 auxAlgo->_value[ BEG_LENGTH_IND ] = Lm;
569 auxAlgo->_value[ END_LENGTH_IND ] = vertexLength;
570 double from = *itU, to = l;
572 std::swap( from, to );
573 std::swap( auxAlgo->_value[ BEG_LENGTH_IND ], auxAlgo->_value[ END_LENGTH_IND ]);
576 if ( auxAlgo->computeInternalParameters( theMesh, theC3d, L, from, to, params, false ))
578 if ( isEnd1 ) params.reverse();
579 while ( 1 + nHalf-- )
580 theParameters.pop_back();
581 theParameters.splice( theParameters.end(), params );
585 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
589 theParameters.reverse();
594 //=============================================================================
598 //=============================================================================
599 bool StdMeshers_Regular_1D::computeInternalParameters(SMESH_Mesh & theMesh,
600 Adaptor3d_Curve& theC3d,
604 list<double> & theParams,
605 const bool theReverse,
606 bool theConsiderPropagation)
610 double f = theFirstU, l = theLastU;
620 if ( _hypType == MAX_LENGTH )
622 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
624 nbseg = 1; // degenerated edge
625 eltSize = theLength / nbseg;
626 nbSegments = (int) nbseg;
628 else if ( _hypType == LOCAL_LENGTH )
630 // Local Length hypothesis
631 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
634 bool isFound = false;
635 if (theConsiderPropagation && !_mainEdge.IsNull()) // propagated from some other edge
637 // Advanced processing to assure equal number of segments in case of Propagation
638 SMESH_subMesh* sm = theMesh.GetSubMeshContaining(_mainEdge);
640 bool computed = sm->IsMeshComputed();
642 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
643 _gen->Compute( theMesh, _mainEdge, /*anUpward=*/true);
644 computed = sm->IsMeshComputed();
648 SMESHDS_SubMesh* smds = sm->GetSubMeshDS();
649 int nb_segments = smds->NbElements();
650 if (nbseg - 1 <= nb_segments && nb_segments <= nbseg + 1) {
657 if (!isFound) // not found by meshed edge in the propagation chain, use precision
659 double aPrecision = _value[ PRECISION_IND ];
660 double nbseg_prec = ceil((theLength / _value[ BEG_LENGTH_IND ]) - aPrecision);
661 if (nbseg_prec == (nbseg - 1)) nbseg--;
665 nbseg = 1; // degenerated edge
666 eltSize = theLength / nbseg;
667 nbSegments = (int) nbseg;
671 // Number Of Segments hypothesis
672 nbSegments = _ivalue[ NB_SEGMENTS_IND ];
673 if ( nbSegments < 1 ) return false;
674 if ( nbSegments == 1 ) return true;
676 switch (_ivalue[ DISTR_TYPE_IND ])
678 case StdMeshers_NumberOfSegments::DT_Scale:
680 double scale = _value[ SCALE_FACTOR_IND ];
682 if (fabs(scale - 1.0) < Precision::Confusion()) {
683 // special case to avoid division by zero
684 for (int i = 1; i < nbSegments; i++) {
685 double param = f + (l - f) * i / nbSegments;
686 theParams.push_back( param );
689 // general case of scale distribution
693 double alpha = pow(scale, 1.0 / (nbSegments - 1));
694 double factor = (l - f) / (1.0 - pow(alpha, nbSegments));
696 for (int i = 1; i < nbSegments; i++) {
697 double param = f + factor * (1.0 - pow(alpha, i));
698 theParams.push_back( param );
701 const double lenFactor = theLength/(l-f);
702 list<double>::iterator u = theParams.begin(), uEnd = theParams.end();
703 for ( ; u != uEnd; ++u )
705 GCPnts_AbscissaPoint Discret( theC3d, ((*u)-f) * lenFactor, f );
706 if ( Discret.IsDone() )
707 *u = Discret.Parameter();
712 case StdMeshers_NumberOfSegments::DT_TabFunc:
714 FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
715 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
716 _ivalue[ NB_SEGMENTS_IND ], func,
720 case StdMeshers_NumberOfSegments::DT_ExprFunc:
722 FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
723 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
724 _ivalue[ NB_SEGMENTS_IND ], func,
728 case StdMeshers_NumberOfSegments::DT_Regular:
729 eltSize = theLength / nbSegments;
735 GCPnts_UniformAbscissa Discret(theC3d, eltSize, f, l);
736 if ( !Discret.IsDone() )
737 return error( "GCPnts_UniformAbscissa failed");
739 int NbPoints = Min( Discret.NbPoints(), nbSegments + 1 );
740 for ( int i = 2; i < NbPoints; i++ ) // skip 1st and last points
742 double param = Discret.Parameter(i);
743 theParams.push_back( param );
745 compensateError( eltSize, eltSize, f, l, theLength, theC3d, theParams, true ); // for PAL9899
749 case BEG_END_LENGTH: {
751 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = theLength
753 double a1 = _value[ BEG_LENGTH_IND ];
754 double an = _value[ END_LENGTH_IND ];
755 double q = ( theLength - a1 ) / ( theLength - an );
756 if ( q < theLength/1e6 || 1.01*theLength < a1 + an)
757 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
758 "for an edge of length "<<theLength);
760 double U1 = theReverse ? l : f;
761 double Un = theReverse ? f : l;
763 double eltSize = theReverse ? -a1 : a1;
765 // computes a point on a curve <theC3d> at the distance <eltSize>
766 // from the point of parameter <param>.
767 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
768 if ( !Discret.IsDone() ) break;
769 param = Discret.Parameter();
770 if ( f < param && param < l )
771 theParams.push_back( param );
776 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
777 if (theReverse) theParams.reverse(); // NPAL18025
781 case ARITHMETIC_1D: {
783 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = theLength
785 double a1 = _value[ BEG_LENGTH_IND ];
786 double an = _value[ END_LENGTH_IND ];
787 if ( 1.01*theLength < a1 + an)
788 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
789 "for an edge of length "<<theLength);
791 double q = ( an - a1 ) / ( 2 *theLength/( a1 + an ) - 1 );
792 int n = int(fabs(q) > numeric_limits<double>::min() ? ( 1+( an-a1 )/q ) : ( 1+theLength/a1 ));
794 double U1 = theReverse ? l : f;
795 double Un = theReverse ? f : l;
802 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
803 // computes a point on a curve <theC3d> at the distance <eltSize>
804 // from the point of parameter <param>.
805 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
806 if ( !Discret.IsDone() ) break;
807 param = Discret.Parameter();
808 if ( param > f && param < l )
809 theParams.push_back( param );
814 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
815 if (theReverse) theParams.reverse(); // NPAL18025
820 case FIXED_POINTS_1D: {
821 const std::vector<double>& aPnts = _fpHyp->GetPoints();
822 const std::vector<int>& nbsegs = _fpHyp->GetNbSegments();
824 TColStd_SequenceOfReal Params;
825 for(; i<aPnts.size(); i++) {
826 if( aPnts[i]<0.0001 || aPnts[i]>0.9999 ) continue;
828 bool IsExist = false;
829 for(; j<=Params.Length(); j++) {
830 if( fabs(aPnts[i]-Params.Value(j)) < 1e-4 ) {
834 if( aPnts[i]<Params.Value(j) ) break;
836 if(!IsExist) Params.InsertBefore(j,aPnts[i]);
838 double par2, par1, lp;
847 double eltSize, segmentSize = 0.;
848 double currAbscissa = 0;
849 for(i=0; i<Params.Length(); i++) {
850 int nbseg = ( i > nbsegs.size()-1 ) ? nbsegs[0] : nbsegs[i];
851 segmentSize = Params.Value(i+1)*theLength - currAbscissa;
852 currAbscissa += segmentSize;
853 GCPnts_AbscissaPoint APnt(theC3d, sign*segmentSize, par1);
855 return error( "GCPnts_AbscissaPoint failed");
856 par2 = APnt.Parameter();
857 eltSize = segmentSize/nbseg;
858 GCPnts_UniformAbscissa Discret(theC3d, eltSize, par1, par2);
860 Discret.Initialize(theC3d, eltSize, par2, par1);
862 Discret.Initialize(theC3d, eltSize, par1, par2);
863 if ( !Discret.IsDone() )
864 return error( "GCPnts_UniformAbscissa failed");
865 int NbPoints = Discret.NbPoints();
866 list<double> tmpParams;
867 for(int i=2; i<NbPoints; i++) {
868 double param = Discret.Parameter(i);
869 tmpParams.push_back( param );
872 compensateError( eltSize, eltSize, par2, par1, segmentSize, theC3d, tmpParams );
876 compensateError( eltSize, eltSize, par1, par2, segmentSize, theC3d, tmpParams );
878 list<double>::iterator itP = tmpParams.begin();
879 for(; itP != tmpParams.end(); itP++) {
880 theParams.push_back( *(itP) );
882 theParams.push_back( par2 );
887 int nbseg = ( nbsegs.size() > Params.Length() ) ? nbsegs[Params.Length()] : nbsegs[0];
888 segmentSize = theLength - currAbscissa;
889 eltSize = segmentSize/nbseg;
890 GCPnts_UniformAbscissa Discret;
892 Discret.Initialize(theC3d, eltSize, par1, lp);
894 Discret.Initialize(theC3d, eltSize, lp, par1);
895 if ( !Discret.IsDone() )
896 return error( "GCPnts_UniformAbscissa failed");
897 int NbPoints = Discret.NbPoints();
898 list<double> tmpParams;
899 for(int i=2; i<NbPoints; i++) {
900 double param = Discret.Parameter(i);
901 tmpParams.push_back( param );
904 compensateError( eltSize, eltSize, lp, par1, segmentSize, theC3d, tmpParams );
908 compensateError( eltSize, eltSize, par1, lp, segmentSize, theC3d, tmpParams );
910 list<double>::iterator itP = tmpParams.begin();
911 for(; itP != tmpParams.end(); itP++) {
912 theParams.push_back( *(itP) );
916 theParams.reverse(); // NPAL18025
923 GCPnts_UniformDeflection Discret(theC3d, _value[ DEFLECTION_IND ], f, l, true);
924 if ( !Discret.IsDone() )
927 int NbPoints = Discret.NbPoints();
928 for ( int i = 2; i < NbPoints; i++ )
930 double param = Discret.Parameter(i);
931 theParams.push_back( param );
942 //=============================================================================
946 //=============================================================================
948 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
950 if ( _hypType == NONE )
953 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
955 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
956 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
957 int shapeID = meshDS->ShapeToIndex( E );
960 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
962 TopoDS_Vertex VFirst, VLast;
963 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
965 ASSERT(!VFirst.IsNull());
966 ASSERT(!VLast.IsNull());
967 const SMDS_MeshNode * idFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
968 const SMDS_MeshNode * idLast = SMESH_Algo::VertexNode( VLast, meshDS );
969 if (!idFirst || !idLast)
970 return error( COMPERR_BAD_INPUT_MESH, "No node on vertex");
972 // remove elements created by e.g. patern mapping (PAL21999)
973 // CLEAN event is incorrectly ptopagated seemingly due to Propagation hyp
974 // so TEMPORARY solution is to clean the submesh manually
975 //theMesh.GetSubMesh(theShape)->ComputeStateEngine( SMESH_subMesh::CLEAN );
976 if (SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(theShape))
978 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
980 meshDS->RemoveFreeElement(ite->next(), subMeshDS);
981 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
982 while (itn->more()) {
983 const SMDS_MeshNode * node = itn->next();
984 if ( node->NbInverseElements() == 0 )
985 meshDS->RemoveFreeNode(node, subMeshDS);
987 meshDS->RemoveNode(node);
993 list< double > params;
994 bool reversed = false;
995 if ( theMesh.GetShapeToMesh().ShapeType() >= TopAbs_WIRE ) {
996 // if the shape to mesh is WIRE or EDGE
997 reversed = ( EE.Orientation() == TopAbs_REVERSED );
999 if ( !_mainEdge.IsNull() ) {
1000 // take into account reversing the edge the hypothesis is propagated from
1001 reversed = ( _mainEdge.Orientation() == TopAbs_REVERSED );
1002 int mainID = meshDS->ShapeToIndex(_mainEdge);
1003 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), mainID) != _revEdgesIDs.end())
1004 reversed = !reversed;
1006 // take into account this edge reversing
1007 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), shapeID) != _revEdgesIDs.end())
1008 reversed = !reversed;
1010 BRepAdaptor_Curve C3d( E );
1011 double length = EdgeLength( E );
1012 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, reversed, true )) {
1015 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1017 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
1018 // only internal nodes receive an edge position with param on curve
1020 const SMDS_MeshNode * idPrev = idFirst;
1033 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++) {
1034 double param = *itU;
1035 gp_Pnt P = Curve->Value(param);
1037 //Add the Node in the DataStructure
1038 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1039 meshDS->SetNodeOnEdge(node, shapeID, param);
1041 if(_quadraticMesh) {
1042 // create medium node
1043 double prm = ( parPrev + param )/2;
1044 gp_Pnt PM = Curve->Value(prm);
1045 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1046 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1047 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1048 meshDS->SetMeshElementOnShape(edge, shapeID);
1051 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1052 meshDS->SetMeshElementOnShape(edge, shapeID);
1058 if(_quadraticMesh) {
1059 double prm = ( parPrev + parLast )/2;
1060 gp_Pnt PM = Curve->Value(prm);
1061 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1062 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1063 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1064 meshDS->SetMeshElementOnShape(edge, shapeID);
1067 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
1068 meshDS->SetMeshElementOnShape(edge, shapeID);
1073 //MESSAGE("************* Degenerated edge! *****************");
1075 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1076 const int NbPoints = 5;
1077 BRep_Tool::Range( E, f, l ); // PAL15185
1078 double du = (l - f) / (NbPoints - 1);
1080 gp_Pnt P = BRep_Tool::Pnt(VFirst);
1082 const SMDS_MeshNode * idPrev = idFirst;
1083 for (int i = 2; i < NbPoints; i++) {
1084 double param = f + (i - 1) * du;
1085 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1086 if(_quadraticMesh) {
1087 // create medium node
1088 double prm = param - du/2.;
1089 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1090 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1091 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1092 meshDS->SetMeshElementOnShape(edge, shapeID);
1095 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1096 meshDS->SetMeshElementOnShape(edge, shapeID);
1098 meshDS->SetNodeOnEdge(node, shapeID, param);
1101 if(_quadraticMesh) {
1102 // create medium node
1103 double prm = l - du/2.;
1104 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1105 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1106 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1107 meshDS->SetMeshElementOnShape(edge, shapeID);
1110 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
1111 meshDS->SetMeshElementOnShape(edge, shapeID);
1118 //=============================================================================
1122 //=============================================================================
1124 bool StdMeshers_Regular_1D::Evaluate(SMESH_Mesh & theMesh,
1125 const TopoDS_Shape & theShape,
1126 MapShapeNbElems& aResMap)
1128 if ( _hypType == NONE )
1131 //SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
1133 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
1134 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
1135 // int shapeID = meshDS->ShapeToIndex( E );
1138 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
1140 TopoDS_Vertex VFirst, VLast;
1141 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
1143 ASSERT(!VFirst.IsNull());
1144 ASSERT(!VLast.IsNull());
1146 std::vector<int> aVec(SMDSEntity_Last,0);
1148 if (!Curve.IsNull()) {
1149 list< double > params;
1151 BRepAdaptor_Curve C3d( E );
1152 double length = EdgeLength( E );
1153 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, false, true )) {
1154 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1155 aResMap.insert(std::make_pair(sm,aVec));
1156 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1157 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
1160 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1162 if(_quadraticMesh) {
1163 aVec[SMDSEntity_Node] = 2*params.size() + 1;
1164 aVec[SMDSEntity_Quad_Edge] = params.size() + 1;
1167 aVec[SMDSEntity_Node] = params.size();
1168 aVec[SMDSEntity_Edge] = params.size() + 1;
1173 //MESSAGE("************* Degenerated edge! *****************");
1174 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1175 if(_quadraticMesh) {
1176 aVec[SMDSEntity_Node] = 11;
1177 aVec[SMDSEntity_Quad_Edge] = 6;
1180 aVec[SMDSEntity_Node] = 5;
1181 aVec[SMDSEntity_Edge] = 6;
1185 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1186 aResMap.insert(std::make_pair(sm,aVec));
1192 //=============================================================================
1194 * See comments in SMESH_Algo.cxx
1196 //=============================================================================
1198 const list <const SMESHDS_Hypothesis *> &
1199 StdMeshers_Regular_1D::GetUsedHypothesis(SMESH_Mesh & aMesh,
1200 const TopoDS_Shape & aShape,
1201 const bool ignoreAuxiliary)
1203 _usedHypList.clear();
1204 _mainEdge.Nullify();
1206 SMESH_HypoFilter auxiliaryFilter, compatibleFilter;
1207 auxiliaryFilter.Init( SMESH_HypoFilter::IsAuxiliary() );
1208 InitCompatibleHypoFilter( compatibleFilter, /*ignoreAux=*/true );
1210 // get non-auxiliary assigned directly to aShape
1211 int nbHyp = aMesh.GetHypotheses( aShape, compatibleFilter, _usedHypList, false );
1213 if (nbHyp == 0 && aShape.ShapeType() == TopAbs_EDGE)
1215 // Check, if propagated from some other edge
1216 _mainEdge = StdMeshers_Propagation::GetPropagationSource( aMesh, aShape );
1217 if ( !_mainEdge.IsNull() )
1219 // Propagation of 1D hypothesis from <aMainEdge> on this edge;
1220 // get non-auxiliary assigned to _mainEdge
1221 nbHyp = aMesh.GetHypotheses( _mainEdge, compatibleFilter, _usedHypList, true );
1225 if (nbHyp == 0) // nothing propagated nor assigned to aShape
1227 SMESH_Algo::GetUsedHypothesis( aMesh, aShape, ignoreAuxiliary );
1228 nbHyp = _usedHypList.size();
1232 // get auxiliary hyps from aShape
1233 aMesh.GetHypotheses( aShape, auxiliaryFilter, _usedHypList, true );
1235 if ( nbHyp > 1 && ignoreAuxiliary )
1236 _usedHypList.clear(); //only one compatible non-auxiliary hypothesis allowed
1238 return _usedHypList;