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_AutomaticLength.hxx"
41 #include "StdMeshers_Deflection1D.hxx"
42 #include "StdMeshers_Distribution.hxx"
43 #include "StdMeshers_FixedPoints1D.hxx"
44 #include "StdMeshers_LocalLength.hxx"
45 #include "StdMeshers_MaxLength.hxx"
46 #include "StdMeshers_NumberOfSegments.hxx"
47 #include "StdMeshers_Propagation.hxx"
48 #include "StdMeshers_SegmentLengthAroundVertex.hxx"
49 #include "StdMeshers_StartEndLength.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,
79 :SMESH_1D_Algo(hypId, studyId, gen)
81 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
83 _shapeType = (1 << TopAbs_EDGE);
86 _compatibleHypothesis.push_back("LocalLength");
87 _compatibleHypothesis.push_back("MaxLength");
88 _compatibleHypothesis.push_back("NumberOfSegments");
89 _compatibleHypothesis.push_back("StartEndLength");
90 _compatibleHypothesis.push_back("Deflection1D");
91 _compatibleHypothesis.push_back("Arithmetic1D");
92 _compatibleHypothesis.push_back("FixedPoints1D");
93 _compatibleHypothesis.push_back("AutomaticLength");
94 _compatibleHypothesis.push_back("Adaptive1D");
96 _compatibleHypothesis.push_back("QuadraticMesh"); // auxiliary !!!
97 _compatibleHypothesis.push_back("Propagation"); // auxiliary !!!
100 //=============================================================================
104 //=============================================================================
106 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
110 //=============================================================================
114 //=============================================================================
116 bool StdMeshers_Regular_1D::CheckHypothesis( SMESH_Mesh& aMesh,
117 const TopoDS_Shape& aShape,
118 Hypothesis_Status& aStatus )
121 _quadraticMesh = false;
122 _onlyUnaryInput = true;
124 const list <const SMESHDS_Hypothesis * > & hyps =
125 GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliaryHyps=*/false);
127 // find non-auxiliary hypothesis
128 const SMESHDS_Hypothesis *theHyp = 0;
129 list <const SMESHDS_Hypothesis * >::const_iterator h = hyps.begin();
130 for ( ; h != hyps.end(); ++h ) {
131 if ( static_cast<const SMESH_Hypothesis*>(*h)->IsAuxiliary() ) {
132 if ( strcmp( "QuadraticMesh", (*h)->GetName() ) == 0 )
133 _quadraticMesh = true;
137 theHyp = *h; // use only the first non-auxiliary hypothesis
143 aStatus = SMESH_Hypothesis::HYP_MISSING;
144 return false; // can't work without a hypothesis
147 string hypName = theHyp->GetName();
149 if (hypName == "LocalLength")
151 const StdMeshers_LocalLength * hyp =
152 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
154 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
155 _value[ PRECISION_IND ] = hyp->GetPrecision();
156 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
157 _hypType = LOCAL_LENGTH;
158 aStatus = SMESH_Hypothesis::HYP_OK;
161 else if (hypName == "MaxLength")
163 const StdMeshers_MaxLength * hyp =
164 dynamic_cast <const StdMeshers_MaxLength * >(theHyp);
166 _value[ BEG_LENGTH_IND ] = hyp->GetLength();
167 if ( hyp->GetUsePreestimatedLength() ) {
168 if ( int nbSeg = aMesh.GetGen()->GetBoundaryBoxSegmentation() )
169 _value[ BEG_LENGTH_IND ] = aMesh.GetShapeDiagonalSize() / nbSeg;
171 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
172 _hypType = MAX_LENGTH;
173 aStatus = SMESH_Hypothesis::HYP_OK;
176 else if (hypName == "NumberOfSegments")
178 const StdMeshers_NumberOfSegments * hyp =
179 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
181 _ivalue[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
182 ASSERT( _ivalue[ NB_SEGMENTS_IND ] > 0 );
183 _ivalue[ DISTR_TYPE_IND ] = (int) hyp->GetDistrType();
184 switch (_ivalue[ DISTR_TYPE_IND ])
186 case StdMeshers_NumberOfSegments::DT_Scale:
187 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
188 _revEdgesIDs = hyp->GetReversedEdges();
190 case StdMeshers_NumberOfSegments::DT_TabFunc:
191 _vvalue[ TAB_FUNC_IND ] = hyp->GetTableFunction();
192 _revEdgesIDs = hyp->GetReversedEdges();
194 case StdMeshers_NumberOfSegments::DT_ExprFunc:
195 _svalue[ EXPR_FUNC_IND ] = hyp->GetExpressionFunction();
196 _revEdgesIDs = hyp->GetReversedEdges();
198 case StdMeshers_NumberOfSegments::DT_Regular:
204 if (_ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_TabFunc ||
205 _ivalue[ DISTR_TYPE_IND ] == StdMeshers_NumberOfSegments::DT_ExprFunc)
206 _ivalue[ CONV_MODE_IND ] = hyp->ConversionMode();
207 _hypType = NB_SEGMENTS;
208 aStatus = SMESH_Hypothesis::HYP_OK;
211 else if (hypName == "Arithmetic1D")
213 const StdMeshers_Arithmetic1D * hyp =
214 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
216 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
217 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
218 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
219 _hypType = ARITHMETIC_1D;
221 _revEdgesIDs = hyp->GetReversedEdges();
223 aStatus = SMESH_Hypothesis::HYP_OK;
226 else if (hypName == "FixedPoints1D") {
227 _fpHyp = dynamic_cast <const StdMeshers_FixedPoints1D*>(theHyp);
229 _hypType = FIXED_POINTS_1D;
231 _revEdgesIDs = _fpHyp->GetReversedEdges();
233 aStatus = SMESH_Hypothesis::HYP_OK;
236 else if (hypName == "StartEndLength")
238 const StdMeshers_StartEndLength * hyp =
239 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
241 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
242 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
243 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
244 _hypType = BEG_END_LENGTH;
246 _revEdgesIDs = hyp->GetReversedEdges();
248 aStatus = SMESH_Hypothesis::HYP_OK;
251 else if (hypName == "Deflection1D")
253 const StdMeshers_Deflection1D * hyp =
254 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
256 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
257 ASSERT( _value[ DEFLECTION_IND ] > 0 );
258 _hypType = DEFLECTION;
259 aStatus = SMESH_Hypothesis::HYP_OK;
262 else if (hypName == "AutomaticLength")
264 StdMeshers_AutomaticLength * hyp = const_cast<StdMeshers_AutomaticLength *>
265 (dynamic_cast <const StdMeshers_AutomaticLength * >(theHyp));
267 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength( &aMesh, aShape );
268 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
269 _hypType = MAX_LENGTH;
270 aStatus = SMESH_Hypothesis::HYP_OK;
272 else if (hypName == "Adaptive1D")
274 _adaptiveHyp = dynamic_cast < const StdMeshers_Adaptive1D* >(theHyp);
275 ASSERT(_adaptiveHyp);
277 _onlyUnaryInput = false;
280 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
282 return ( _hypType != NONE );
285 static bool computeParamByFunc(Adaptor3d_Curve& C3d, double first, double last,
286 double length, bool theReverse,
287 int nbSeg, Function& func,
288 list<double>& theParams)
291 //OSD::SetSignal( true );
296 MESSAGE( "computeParamByFunc" );
298 int nbPnt = 1 + nbSeg;
299 vector<double> x(nbPnt, 0.);
301 if (!buildDistribution(func, 0.0, 1.0, nbSeg, x, 1E-4))
304 MESSAGE( "Points:\n" );
306 for ( int i=0; i<=nbSeg; i++ )
308 sprintf( buf, "%f\n", float(x[i] ) );
314 // apply parameters in range [0,1] to the space of the curve
315 double prevU = first;
322 for( int i = 1; i < nbSeg; i++ )
324 double curvLength = length * (x[i] - x[i-1]) * sign;
325 GCPnts_AbscissaPoint Discret( C3d, curvLength, prevU );
326 if ( !Discret.IsDone() )
328 double U = Discret.Parameter();
329 if ( U > first && U < last )
330 theParams.push_back( U );
341 //================================================================================
343 * \brief adjust internal node parameters so that the last segment length == an
344 * \param a1 - the first segment length
345 * \param an - the last segment length
346 * \param U1 - the first edge parameter
347 * \param Un - the last edge parameter
348 * \param length - the edge length
349 * \param C3d - the edge curve
350 * \param theParams - internal node parameters to adjust
351 * \param adjustNeighbors2an - to adjust length of segments next to the last one
352 * and not to remove parameters
354 //================================================================================
356 static void compensateError(double a1, double an,
357 double U1, double Un,
359 Adaptor3d_Curve& C3d,
360 list<double> & theParams,
361 bool adjustNeighbors2an = false)
363 int i, nPar = theParams.size();
364 if ( a1 + an <= length && nPar > 1 )
366 bool reverse = ( U1 > Un );
367 GCPnts_AbscissaPoint Discret(C3d, reverse ? an : -an, Un);
368 if ( !Discret.IsDone() )
370 double Utgt = Discret.Parameter(); // target value of the last parameter
371 list<double>::reverse_iterator itU = theParams.rbegin();
372 double Ul = *itU++; // real value of the last parameter
373 double dUn = Utgt - Ul; // parametric error of <an>
374 if ( Abs(dUn) <= Precision::Confusion() )
376 double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
377 if ( adjustNeighbors2an || Abs(dUn) < 0.5 * dU ) { // last segment is a bit shorter than it should
378 // move the last parameter to the edge beginning
380 else { // last segment is much shorter than it should -> remove the last param and
381 theParams.pop_back(); nPar--; // move the rest points toward the edge end
382 dUn = Utgt - theParams.back();
385 if ( !adjustNeighbors2an )
387 double q = dUn / ( Utgt - Un ); // (signed) factor of segment length change
388 for ( itU = theParams.rbegin(), i = 1; i < nPar; i++ ) {
392 dUn = q * (*itU - prevU) * (prevU-U1)/(Un-U1);
395 else if ( nPar == 1 )
397 theParams.back() += dUn;
401 double q = dUn / ( nPar - 1 );
402 theParams.back() += dUn;
403 double sign = reverse ? -1 : 1;
404 double prevU = theParams.back();
405 itU = theParams.rbegin();
406 for ( ++itU, i = 2; i < nPar; ++itU, i++ ) {
407 double newU = *itU + dUn;
408 if ( newU*sign < prevU*sign ) {
412 else { // set U between prevU and next valid param
413 list<double>::reverse_iterator itU2 = itU;
416 while ( (*itU2)*sign > prevU*sign ) {
419 dU = ( *itU2 - prevU ) / nb;
420 while ( itU != itU2 ) {
430 //================================================================================
432 * \brief Class used to clean mesh on edges when 0D hyp modified.
433 * Common approach doesn't work when 0D algo is missing because the 0D hyp is
434 * considered as not participating in computation whereas it is used by 1D algo.
436 //================================================================================
438 // struct VertexEventListener : public SMESH_subMeshEventListener
440 // VertexEventListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
443 // * \brief Clean mesh on edges
444 // * \param event - algo_event or compute_event itself (of SMESH_subMesh)
445 // * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
446 // * \param subMesh - the submesh where the event occures
448 // void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
449 // EventListenerData*, const SMESH_Hypothesis*)
451 // if ( eventType == SMESH_subMesh::ALGO_EVENT) // all algo events
453 // subMesh->ComputeStateEngine( SMESH_subMesh::MODIF_ALGO_STATE );
456 // }; // struct VertexEventListener
458 //=============================================================================
460 * \brief Sets event listener to vertex submeshes
461 * \param subMesh - submesh where algo is set
463 * This method is called when a submesh gets HYP_OK algo_state.
464 * After being set, event listener is notified on each event of a submesh.
466 //=============================================================================
468 void StdMeshers_Regular_1D::SetEventListener(SMESH_subMesh* subMesh)
470 StdMeshers_Propagation::SetPropagationMgr( subMesh );
473 //=============================================================================
476 * \param subMesh - restored submesh
478 * This method is called only if a submesh has HYP_OK algo_state.
480 //=============================================================================
482 void StdMeshers_Regular_1D::SubmeshRestored(SMESH_subMesh* subMesh)
486 //=============================================================================
488 * \brief Return StdMeshers_SegmentLengthAroundVertex assigned to vertex
490 //=============================================================================
492 const StdMeshers_SegmentLengthAroundVertex*
493 StdMeshers_Regular_1D::getVertexHyp(SMESH_Mesh & theMesh,
494 const TopoDS_Vertex & theV)
496 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName("SegmentAroundVertex_0D"));
497 if ( const SMESH_Hypothesis * h = theMesh.GetHypothesis( theV, filter, true ))
499 SMESH_Algo* algo = const_cast< SMESH_Algo* >( static_cast< const SMESH_Algo* > ( h ));
500 const list <const SMESHDS_Hypothesis *> & hypList = algo->GetUsedHypothesis( theMesh, theV, 0 );
501 if ( !hypList.empty() && string("SegmentLengthAroundVertex") == hypList.front()->GetName() )
502 return static_cast<const StdMeshers_SegmentLengthAroundVertex*>( hypList.front() );
507 //================================================================================
509 * \brief Tune parameters to fit "SegmentLengthAroundVertex" hypothesis
510 * \param theC3d - wire curve
511 * \param theLength - curve length
512 * \param theParameters - internal nodes parameters to modify
513 * \param theVf - 1st vertex
514 * \param theVl - 2nd vertex
516 //================================================================================
518 void StdMeshers_Regular_1D::redistributeNearVertices (SMESH_Mesh & theMesh,
519 Adaptor3d_Curve & theC3d,
521 std::list< double > & theParameters,
522 const TopoDS_Vertex & theVf,
523 const TopoDS_Vertex & theVl)
525 double f = theC3d.FirstParameter(), l = theC3d.LastParameter();
526 int nPar = theParameters.size();
527 for ( int isEnd1 = 0; isEnd1 < 2; ++isEnd1 )
529 const TopoDS_Vertex & V = isEnd1 ? theVf : theVl;
530 const StdMeshers_SegmentLengthAroundVertex* hyp = getVertexHyp (theMesh, V );
532 double vertexLength = hyp->GetLength();
533 if ( vertexLength > theLength / 2.0 )
535 if ( isEnd1 ) { // to have a segment of interest at end of theParameters
536 theParameters.reverse();
539 if ( _hypType == NB_SEGMENTS )
541 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
543 else if ( nPar <= 3 )
546 vertexLength = -vertexLength;
547 GCPnts_AbscissaPoint Discret(theC3d, vertexLength, l);
548 if ( Discret.IsDone() ) {
550 theParameters.push_back( Discret.Parameter());
552 double L = GCPnts_AbscissaPoint::Length( theC3d, theParameters.back(), l);
553 if ( vertexLength < L / 2.0 )
554 theParameters.push_back( Discret.Parameter());
556 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
562 // recompute params between the last segment and a middle one.
563 // find size of a middle segment
564 int nHalf = ( nPar-1 ) / 2;
565 list< double >::reverse_iterator itU = theParameters.rbegin();
566 std::advance( itU, nHalf );
568 double Lm = GCPnts_AbscissaPoint::Length( theC3d, Um, *itU);
569 double L = GCPnts_AbscissaPoint::Length( theC3d, *itU, l);
570 static StdMeshers_Regular_1D* auxAlgo = 0;
572 auxAlgo = new StdMeshers_Regular_1D( _gen->GetANewId(), _studyId, _gen );
573 auxAlgo->_hypType = BEG_END_LENGTH;
575 auxAlgo->_value[ BEG_LENGTH_IND ] = Lm;
576 auxAlgo->_value[ END_LENGTH_IND ] = vertexLength;
577 double from = *itU, to = l;
579 std::swap( from, to );
580 std::swap( auxAlgo->_value[ BEG_LENGTH_IND ], auxAlgo->_value[ END_LENGTH_IND ]);
583 if ( auxAlgo->computeInternalParameters( theMesh, theC3d, L, from, to, params, false ))
585 if ( isEnd1 ) params.reverse();
586 while ( 1 + nHalf-- )
587 theParameters.pop_back();
588 theParameters.splice( theParameters.end(), params );
592 compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
596 theParameters.reverse();
601 //=============================================================================
605 //=============================================================================
606 bool StdMeshers_Regular_1D::computeInternalParameters(SMESH_Mesh & theMesh,
607 Adaptor3d_Curve& theC3d,
611 list<double> & theParams,
612 const bool theReverse,
613 bool theConsiderPropagation)
617 double f = theFirstU, l = theLastU;
627 if ( _hypType == MAX_LENGTH )
629 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
631 nbseg = 1; // degenerated edge
632 eltSize = theLength / nbseg;
633 nbSegments = (int) nbseg;
635 else if ( _hypType == LOCAL_LENGTH )
637 // Local Length hypothesis
638 double nbseg = ceil(theLength / _value[ BEG_LENGTH_IND ]); // integer sup
641 bool isFound = false;
642 if (theConsiderPropagation && !_mainEdge.IsNull()) // propagated from some other edge
644 // Advanced processing to assure equal number of segments in case of Propagation
645 SMESH_subMesh* sm = theMesh.GetSubMeshContaining(_mainEdge);
647 bool computed = sm->IsMeshComputed();
649 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE) {
650 _gen->Compute( theMesh, _mainEdge, /*anUpward=*/true);
651 computed = sm->IsMeshComputed();
655 SMESHDS_SubMesh* smds = sm->GetSubMeshDS();
656 int nb_segments = smds->NbElements();
657 if (nbseg - 1 <= nb_segments && nb_segments <= nbseg + 1) {
664 if (!isFound) // not found by meshed edge in the propagation chain, use precision
666 double aPrecision = _value[ PRECISION_IND ];
667 double nbseg_prec = ceil((theLength / _value[ BEG_LENGTH_IND ]) - aPrecision);
668 if (nbseg_prec == (nbseg - 1)) nbseg--;
672 nbseg = 1; // degenerated edge
673 eltSize = theLength / nbseg;
674 nbSegments = (int) nbseg;
678 // Number Of Segments hypothesis
679 nbSegments = _ivalue[ NB_SEGMENTS_IND ];
680 if ( nbSegments < 1 ) return false;
681 if ( nbSegments == 1 ) return true;
683 switch (_ivalue[ DISTR_TYPE_IND ])
685 case StdMeshers_NumberOfSegments::DT_Scale:
687 double scale = _value[ SCALE_FACTOR_IND ];
689 if (fabs(scale - 1.0) < Precision::Confusion()) {
690 // special case to avoid division by zero
691 for (int i = 1; i < nbSegments; i++) {
692 double param = f + (l - f) * i / nbSegments;
693 theParams.push_back( param );
696 // general case of scale distribution
700 double alpha = pow(scale, 1.0 / (nbSegments - 1));
701 double factor = (l - f) / (1.0 - pow(alpha, nbSegments));
703 for (int i = 1; i < nbSegments; i++) {
704 double param = f + factor * (1.0 - pow(alpha, i));
705 theParams.push_back( param );
708 const double lenFactor = theLength/(l-f);
709 list<double>::iterator u = theParams.begin(), uEnd = theParams.end();
710 for ( ; u != uEnd; ++u )
712 GCPnts_AbscissaPoint Discret( theC3d, ((*u)-f) * lenFactor, f );
713 if ( Discret.IsDone() )
714 *u = Discret.Parameter();
719 case StdMeshers_NumberOfSegments::DT_TabFunc:
721 FunctionTable func(_vvalue[ TAB_FUNC_IND ], _ivalue[ CONV_MODE_IND ]);
722 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
723 _ivalue[ NB_SEGMENTS_IND ], func,
727 case StdMeshers_NumberOfSegments::DT_ExprFunc:
729 FunctionExpr func(_svalue[ EXPR_FUNC_IND ].c_str(), _ivalue[ CONV_MODE_IND ]);
730 return computeParamByFunc(theC3d, f, l, theLength, theReverse,
731 _ivalue[ NB_SEGMENTS_IND ], func,
735 case StdMeshers_NumberOfSegments::DT_Regular:
736 eltSize = theLength / nbSegments;
742 GCPnts_UniformAbscissa Discret(theC3d, eltSize, f, l);
743 if ( !Discret.IsDone() )
744 return error( "GCPnts_UniformAbscissa failed");
746 int NbPoints = Min( Discret.NbPoints(), nbSegments + 1 );
747 for ( int i = 2; i < NbPoints; i++ ) // skip 1st and last points
749 double param = Discret.Parameter(i);
750 theParams.push_back( param );
752 compensateError( eltSize, eltSize, f, l, theLength, theC3d, theParams, true ); // for PAL9899
756 case BEG_END_LENGTH: {
758 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = theLength
760 double a1 = _value[ BEG_LENGTH_IND ];
761 double an = _value[ END_LENGTH_IND ];
762 double q = ( theLength - a1 ) / ( theLength - an );
763 if ( q < theLength/1e6 || 1.01*theLength < a1 + an)
764 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
765 "for an edge of length "<<theLength);
767 double U1 = theReverse ? l : f;
768 double Un = theReverse ? f : l;
770 double eltSize = theReverse ? -a1 : a1;
772 // computes a point on a curve <theC3d> at the distance <eltSize>
773 // from the point of parameter <param>.
774 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
775 if ( !Discret.IsDone() ) break;
776 param = Discret.Parameter();
777 if ( f < param && param < l )
778 theParams.push_back( param );
783 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
784 if (theReverse) theParams.reverse(); // NPAL18025
788 case ARITHMETIC_1D: {
790 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = theLength
792 double a1 = _value[ BEG_LENGTH_IND ];
793 double an = _value[ END_LENGTH_IND ];
794 if ( 1.01*theLength < a1 + an)
795 return error ( SMESH_Comment("Invalid segment lengths (")<<a1<<" and "<<an<<") "<<
796 "for an edge of length "<<theLength);
798 double q = ( an - a1 ) / ( 2 *theLength/( a1 + an ) - 1 );
799 int n = int(fabs(q) > numeric_limits<double>::min() ? ( 1+( an-a1 )/q ) : ( 1+theLength/a1 ));
801 double U1 = theReverse ? l : f;
802 double Un = theReverse ? f : l;
809 while ( n-- > 0 && eltSize * ( Un - U1 ) > 0 ) {
810 // computes a point on a curve <theC3d> at the distance <eltSize>
811 // from the point of parameter <param>.
812 GCPnts_AbscissaPoint Discret( theC3d, eltSize, param );
813 if ( !Discret.IsDone() ) break;
814 param = Discret.Parameter();
815 if ( param > f && param < l )
816 theParams.push_back( param );
821 compensateError( a1, an, U1, Un, theLength, theC3d, theParams );
822 if (theReverse) theParams.reverse(); // NPAL18025
827 case FIXED_POINTS_1D: {
828 const std::vector<double>& aPnts = _fpHyp->GetPoints();
829 const std::vector<int>& nbsegs = _fpHyp->GetNbSegments();
831 TColStd_SequenceOfReal Params;
832 for(; i<aPnts.size(); i++) {
833 if( aPnts[i]<0.0001 || aPnts[i]>0.9999 ) continue;
835 bool IsExist = false;
836 for(; j<=Params.Length(); j++) {
837 if( fabs(aPnts[i]-Params.Value(j)) < 1e-4 ) {
841 if( aPnts[i]<Params.Value(j) ) break;
843 if(!IsExist) Params.InsertBefore(j,aPnts[i]);
845 double par2, par1, lp;
854 double eltSize, segmentSize = 0.;
855 double currAbscissa = 0;
856 for(i=0; i<Params.Length(); i++) {
857 int nbseg = ( i > nbsegs.size()-1 ) ? nbsegs[0] : nbsegs[i];
858 segmentSize = Params.Value(i+1)*theLength - currAbscissa;
859 currAbscissa += segmentSize;
860 GCPnts_AbscissaPoint APnt(theC3d, sign*segmentSize, par1);
862 return error( "GCPnts_AbscissaPoint failed");
863 par2 = APnt.Parameter();
864 eltSize = segmentSize/nbseg;
865 GCPnts_UniformAbscissa Discret(theC3d, eltSize, par1, par2);
867 Discret.Initialize(theC3d, eltSize, par2, par1);
869 Discret.Initialize(theC3d, eltSize, par1, par2);
870 if ( !Discret.IsDone() )
871 return error( "GCPnts_UniformAbscissa failed");
872 int NbPoints = Discret.NbPoints();
873 list<double> tmpParams;
874 for(int i=2; i<NbPoints; i++) {
875 double param = Discret.Parameter(i);
876 tmpParams.push_back( param );
879 compensateError( eltSize, eltSize, par2, par1, segmentSize, theC3d, tmpParams );
883 compensateError( eltSize, eltSize, par1, par2, segmentSize, theC3d, tmpParams );
885 list<double>::iterator itP = tmpParams.begin();
886 for(; itP != tmpParams.end(); itP++) {
887 theParams.push_back( *(itP) );
889 theParams.push_back( par2 );
894 int nbseg = ( nbsegs.size() > Params.Length() ) ? nbsegs[Params.Length()] : nbsegs[0];
895 segmentSize = theLength - currAbscissa;
896 eltSize = segmentSize/nbseg;
897 GCPnts_UniformAbscissa Discret;
899 Discret.Initialize(theC3d, eltSize, par1, lp);
901 Discret.Initialize(theC3d, eltSize, lp, par1);
902 if ( !Discret.IsDone() )
903 return error( "GCPnts_UniformAbscissa failed");
904 int NbPoints = Discret.NbPoints();
905 list<double> tmpParams;
906 for(int i=2; i<NbPoints; i++) {
907 double param = Discret.Parameter(i);
908 tmpParams.push_back( param );
911 compensateError( eltSize, eltSize, lp, par1, segmentSize, theC3d, tmpParams );
915 compensateError( eltSize, eltSize, par1, lp, segmentSize, theC3d, tmpParams );
917 list<double>::iterator itP = tmpParams.begin();
918 for(; itP != tmpParams.end(); itP++) {
919 theParams.push_back( *(itP) );
923 theParams.reverse(); // NPAL18025
930 GCPnts_UniformDeflection Discret(theC3d, _value[ DEFLECTION_IND ], f, l, true);
931 if ( !Discret.IsDone() )
934 int NbPoints = Discret.NbPoints();
935 for ( int i = 2; i < NbPoints; i++ )
937 double param = Discret.Parameter(i);
938 theParams.push_back( param );
949 //=============================================================================
953 //=============================================================================
955 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
957 if ( _hypType == NONE )
960 if ( _hypType == ADAPTIVE )
962 _adaptiveHyp->GetAlgo()->InitComputeError();
963 _adaptiveHyp->GetAlgo()->Compute( theMesh, theShape );
964 return error( _adaptiveHyp->GetAlgo()->GetComputeError() );
967 SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
969 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
970 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
971 int shapeID = meshDS->ShapeToIndex( E );
974 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
976 TopoDS_Vertex VFirst, VLast;
977 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
979 ASSERT(!VFirst.IsNull());
980 ASSERT(!VLast.IsNull());
981 const SMDS_MeshNode * idFirst = SMESH_Algo::VertexNode( VFirst, meshDS );
982 const SMDS_MeshNode * idLast = SMESH_Algo::VertexNode( VLast, meshDS );
983 if (!idFirst || !idLast)
984 return error( COMPERR_BAD_INPUT_MESH, "No node on vertex");
986 // remove elements created by e.g. patern mapping (PAL21999)
987 // CLEAN event is incorrectly ptopagated seemingly due to Propagation hyp
988 // so TEMPORARY solution is to clean the submesh manually
989 //theMesh.GetSubMesh(theShape)->ComputeStateEngine( SMESH_subMesh::CLEAN );
990 if (SMESHDS_SubMesh * subMeshDS = meshDS->MeshElements(theShape))
992 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
994 meshDS->RemoveFreeElement(ite->next(), subMeshDS);
995 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
996 while (itn->more()) {
997 const SMDS_MeshNode * node = itn->next();
998 if ( node->NbInverseElements() == 0 )
999 meshDS->RemoveFreeNode(node, subMeshDS);
1001 meshDS->RemoveNode(node);
1005 if (!Curve.IsNull())
1007 list< double > params;
1008 bool reversed = false;
1009 if ( theMesh.GetShapeToMesh().ShapeType() >= TopAbs_WIRE ) {
1010 // if the shape to mesh is WIRE or EDGE
1011 reversed = ( EE.Orientation() == TopAbs_REVERSED );
1013 if ( !_mainEdge.IsNull() ) {
1014 // take into account reversing the edge the hypothesis is propagated from
1015 reversed = ( _mainEdge.Orientation() == TopAbs_REVERSED );
1016 int mainID = meshDS->ShapeToIndex(_mainEdge);
1017 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), mainID) != _revEdgesIDs.end())
1018 reversed = !reversed;
1020 // take into account this edge reversing
1021 if ( std::find( _revEdgesIDs.begin(), _revEdgesIDs.end(), shapeID) != _revEdgesIDs.end())
1022 reversed = !reversed;
1024 BRepAdaptor_Curve C3d( E );
1025 double length = EdgeLength( E );
1026 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, reversed, true )) {
1029 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1031 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
1032 // only internal nodes receive an edge position with param on curve
1034 const SMDS_MeshNode * idPrev = idFirst;
1047 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++) {
1048 double param = *itU;
1049 gp_Pnt P = Curve->Value(param);
1051 //Add the Node in the DataStructure
1052 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1053 meshDS->SetNodeOnEdge(node, shapeID, param);
1055 if(_quadraticMesh) {
1056 // create medium node
1057 double prm = ( parPrev + param )/2;
1058 gp_Pnt PM = Curve->Value(prm);
1059 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1060 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1061 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1062 meshDS->SetMeshElementOnShape(edge, shapeID);
1065 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1066 meshDS->SetMeshElementOnShape(edge, shapeID);
1072 if(_quadraticMesh) {
1073 double prm = ( parPrev + parLast )/2;
1074 gp_Pnt PM = Curve->Value(prm);
1075 SMDS_MeshNode * NM = meshDS->AddNode(PM.X(), PM.Y(), PM.Z());
1076 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1077 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1078 meshDS->SetMeshElementOnShape(edge, shapeID);
1081 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
1082 meshDS->SetMeshElementOnShape(edge, shapeID);
1087 //MESSAGE("************* Degenerated edge! *****************");
1089 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1090 const int NbPoints = 5;
1091 BRep_Tool::Range( E, f, l ); // PAL15185
1092 double du = (l - f) / (NbPoints - 1);
1094 gp_Pnt P = BRep_Tool::Pnt(VFirst);
1096 const SMDS_MeshNode * idPrev = idFirst;
1097 for (int i = 2; i < NbPoints; i++) {
1098 double param = f + (i - 1) * du;
1099 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
1100 if(_quadraticMesh) {
1101 // create medium node
1102 double prm = param - du/2.;
1103 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1104 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1105 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node, NM);
1106 meshDS->SetMeshElementOnShape(edge, shapeID);
1109 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
1110 meshDS->SetMeshElementOnShape(edge, shapeID);
1112 meshDS->SetNodeOnEdge(node, shapeID, param);
1115 if(_quadraticMesh) {
1116 // create medium node
1117 double prm = l - du/2.;
1118 SMDS_MeshNode * NM = meshDS->AddNode(P.X(), P.Y(), P.Z());
1119 meshDS->SetNodeOnEdge(NM, shapeID, prm);
1120 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast, NM);
1121 meshDS->SetMeshElementOnShape(edge, shapeID);
1124 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
1125 meshDS->SetMeshElementOnShape(edge, shapeID);
1132 //=============================================================================
1136 //=============================================================================
1138 bool StdMeshers_Regular_1D::Evaluate(SMESH_Mesh & theMesh,
1139 const TopoDS_Shape & theShape,
1140 MapShapeNbElems& aResMap)
1142 if ( _hypType == NONE )
1145 if ( _hypType == ADAPTIVE )
1147 _adaptiveHyp->GetAlgo()->InitComputeError();
1148 _adaptiveHyp->GetAlgo()->Evaluate( theMesh, theShape, aResMap );
1149 return error( _adaptiveHyp->GetAlgo()->GetComputeError() );
1152 const TopoDS_Edge & EE = TopoDS::Edge(theShape);
1153 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
1156 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
1158 TopoDS_Vertex VFirst, VLast;
1159 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
1161 ASSERT(!VFirst.IsNull());
1162 ASSERT(!VLast.IsNull());
1164 std::vector<int> aVec(SMDSEntity_Last,0);
1166 if (!Curve.IsNull()) {
1167 list< double > params;
1169 BRepAdaptor_Curve C3d( E );
1170 double length = EdgeLength( E );
1171 if ( ! computeInternalParameters( theMesh, C3d, length, f, l, params, false, true )) {
1172 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1173 aResMap.insert(std::make_pair(sm,aVec));
1174 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1175 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,"Submesh can not be evaluated",this));
1178 redistributeNearVertices( theMesh, C3d, length, params, VFirst, VLast );
1180 if(_quadraticMesh) {
1181 aVec[SMDSEntity_Node] = 2*params.size() + 1;
1182 aVec[SMDSEntity_Quad_Edge] = params.size() + 1;
1185 aVec[SMDSEntity_Node] = params.size();
1186 aVec[SMDSEntity_Edge] = params.size() + 1;
1191 //MESSAGE("************* Degenerated edge! *****************");
1192 // Edge is a degenerated Edge : We put n = 5 points on the edge.
1193 if(_quadraticMesh) {
1194 aVec[SMDSEntity_Node] = 11;
1195 aVec[SMDSEntity_Quad_Edge] = 6;
1198 aVec[SMDSEntity_Node] = 5;
1199 aVec[SMDSEntity_Edge] = 6;
1203 SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1204 aResMap.insert(std::make_pair(sm,aVec));
1210 //=============================================================================
1212 * See comments in SMESH_Algo.cxx
1214 //=============================================================================
1216 const list <const SMESHDS_Hypothesis *> &
1217 StdMeshers_Regular_1D::GetUsedHypothesis(SMESH_Mesh & aMesh,
1218 const TopoDS_Shape & aShape,
1219 const bool ignoreAuxiliary)
1221 _usedHypList.clear();
1222 _mainEdge.Nullify();
1224 SMESH_HypoFilter auxiliaryFilter, compatibleFilter;
1225 auxiliaryFilter.Init( SMESH_HypoFilter::IsAuxiliary() );
1226 InitCompatibleHypoFilter( compatibleFilter, /*ignoreAux=*/true );
1228 // get non-auxiliary assigned directly to aShape
1229 int nbHyp = aMesh.GetHypotheses( aShape, compatibleFilter, _usedHypList, false );
1231 if (nbHyp == 0 && aShape.ShapeType() == TopAbs_EDGE)
1233 // Check, if propagated from some other edge
1234 _mainEdge = StdMeshers_Propagation::GetPropagationSource( aMesh, aShape );
1235 if ( !_mainEdge.IsNull() )
1237 // Propagation of 1D hypothesis from <aMainEdge> on this edge;
1238 // get non-auxiliary assigned to _mainEdge
1239 nbHyp = aMesh.GetHypotheses( _mainEdge, compatibleFilter, _usedHypList, true );
1243 if (nbHyp == 0) // nothing propagated nor assigned to aShape
1245 SMESH_Algo::GetUsedHypothesis( aMesh, aShape, ignoreAuxiliary );
1246 nbHyp = _usedHypList.size();
1250 // get auxiliary hyps from aShape
1251 aMesh.GetHypotheses( aShape, auxiliaryFilter, _usedHypList, true );
1253 if ( nbHyp > 1 && ignoreAuxiliary )
1254 _usedHypList.clear(); //only one compatible non-auxiliary hypothesis allowed
1256 return _usedHypList;
1259 //================================================================================
1261 * \brief Pass CancelCompute() to a child algorithm
1263 //================================================================================
1265 void StdMeshers_Regular_1D::CancelCompute()
1267 SMESH_Algo::CancelCompute();
1268 if ( _hypType == ADAPTIVE )
1269 _adaptiveHyp->GetAlgo()->CancelCompute();