+
+//================================================================================
+/*!
+ * \brief adjust internal node parameters so that the last segment length == an
+ * \param a1 - the first segment length
+ * \param an - the last segment length
+ * \param U1 - the first edge parameter
+ * \param Un - the last edge parameter
+ * \param length - the edge length
+ * \param C3d - the edge curve
+ * \param theParams - internal node parameters to adjust
+ * \param adjustNeighbors2an - to adjust length of segments next to the last one
+ * and not to remove parameters
+ */
+//================================================================================
+
+static void compensateError(double a1, double an,
+ double U1, double Un,
+ double length,
+ Adaptor3d_Curve& C3d,
+ list<double> & theParams,
+ bool adjustNeighbors2an = false)
+{
+ int i, nPar = theParams.size();
+ if ( a1 + an <= length && nPar > 1 )
+ {
+ bool reverse = ( U1 > Un );
+ GCPnts_AbscissaPoint Discret(C3d, reverse ? an : -an, Un);
+ if ( !Discret.IsDone() )
+ return;
+ double Utgt = Discret.Parameter(); // target value of the last parameter
+ list<double>::reverse_iterator itU = theParams.rbegin();
+ double Ul = *itU++; // real value of the last parameter
+ double dUn = Utgt - Ul; // parametric error of <an>
+ if ( Abs(dUn) <= Precision::Confusion() )
+ return;
+ double dU = Abs( Ul - *itU ); // parametric length of the last but one segment
+ if ( adjustNeighbors2an || Abs(dUn) < 0.5 * dU ) { // last segment is a bit shorter than it should
+ // move the last parameter to the edge beginning
+ }
+ else { // last segment is much shorter than it should -> remove the last param and
+ theParams.pop_back(); nPar--; // move the rest points toward the edge end
+ dUn = Utgt - theParams.back();
+ }
+
+ if ( !adjustNeighbors2an )
+ {
+ double q = dUn / ( Utgt - Un ); // (signed) factor of segment length change
+ for ( itU = theParams.rbegin(), i = 1; i < nPar; i++ ) {
+ double prevU = *itU;
+ (*itU) += dUn;
+ ++itU;
+ dUn = q * (*itU - prevU) * (prevU-U1)/(Un-U1);
+ }
+ }
+ else if ( nPar == 1 )
+ {
+ theParams.back() += dUn;
+ }
+ else
+ {
+ double q = dUn / ( nPar - 1 );
+ theParams.back() += dUn;
+ double sign = reverse ? -1 : 1;
+ double prevU = theParams.back();
+ itU = theParams.rbegin();
+ for ( ++itU, i = 2; i < nPar; ++itU, i++ ) {
+ double newU = *itU + dUn;
+ if ( newU*sign < prevU*sign ) {
+ prevU = *itU = newU;
+ dUn -= q;
+ }
+ else { // set U between prevU and next valid param
+ list<double>::reverse_iterator itU2 = itU;
+ ++itU2;
+ int nb = 2;
+ while ( (*itU2)*sign > prevU*sign ) {
+ ++itU2; ++nb;
+ }
+ dU = ( *itU2 - prevU ) / nb;
+ while ( itU != itU2 ) {
+ *itU += dU; ++itU;
+ }
+ break;
+ }
+ }
+ }
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Class used to clean mesh on edges when 0D hyp modified.
+ * Common approach doesn't work when 0D algo is missing because the 0D hyp is
+ * considered as not participating in computation whereas it is used by 1D algo.
+ */
+//================================================================================
+
+// struct VertexEventListener : public SMESH_subMeshEventListener
+// {
+// VertexEventListener():SMESH_subMeshEventListener(0) // won't be deleted by submesh
+// {}
+// /*!
+// * \brief Clean mesh on edges
+// * \param event - algo_event or compute_event itself (of SMESH_subMesh)
+// * \param eventType - ALGO_EVENT or COMPUTE_EVENT (of SMESH_subMesh)
+// * \param subMesh - the submesh where the event occures
+// */
+// void ProcessEvent(const int event, const int eventType, SMESH_subMesh* subMesh,
+// EventListenerData*, const SMESH_Hypothesis*)
+// {
+// if ( eventType == SMESH_subMesh::ALGO_EVENT) // all algo events
+// {
+// subMesh->ComputeStateEngine( SMESH_subMesh::MODIF_ALGO_STATE );
+// }
+// }
+// }; // struct VertexEventListener
+
+//=============================================================================
+/*!
+ * \brief Sets event listener to vertex submeshes
+ * \param subMesh - submesh where algo is set
+ *
+ * This method is called when a submesh gets HYP_OK algo_state.
+ * After being set, event listener is notified on each event of a submesh.
+ */
+//=============================================================================
+
+void StdMeshers_Regular_1D::SetEventListener(SMESH_subMesh* subMesh)
+{
+ StdMeshers_Propagation::SetPropagationMgr( subMesh );
+}
+
+//=============================================================================
+/*!
+ * \brief Do nothing
+ * \param subMesh - restored submesh
+ *
+ * This method is called only if a submesh has HYP_OK algo_state.
+ */
+//=============================================================================
+
+void StdMeshers_Regular_1D::SubmeshRestored(SMESH_subMesh* subMesh)
+{
+}
+
+//=============================================================================
+/*!
+ * \brief Return StdMeshers_SegmentLengthAroundVertex assigned to vertex
+ */
+//=============================================================================
+
+const StdMeshers_SegmentLengthAroundVertex*
+StdMeshers_Regular_1D::getVertexHyp(SMESH_Mesh & theMesh,
+ const TopoDS_Vertex & theV)
+{
+ static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName("SegmentAroundVertex_0D"));
+ if ( const SMESH_Hypothesis * h = theMesh.GetHypothesis( theV, filter, true ))
+ {
+ SMESH_Algo* algo = const_cast< SMESH_Algo* >( static_cast< const SMESH_Algo* > ( h ));
+ const list <const SMESHDS_Hypothesis *> & hypList = algo->GetUsedHypothesis( theMesh, theV, 0 );
+ if ( !hypList.empty() && string("SegmentLengthAroundVertex") == hypList.front()->GetName() )
+ return static_cast<const StdMeshers_SegmentLengthAroundVertex*>( hypList.front() );
+ }
+ return 0;
+}
+
+//================================================================================
+/*!
+ * \brief Tune parameters to fit "SegmentLengthAroundVertex" hypothesis
+ * \param theC3d - wire curve
+ * \param theLength - curve length
+ * \param theParameters - internal nodes parameters to modify
+ * \param theVf - 1st vertex
+ * \param theVl - 2nd vertex
+ */
+//================================================================================
+
+void StdMeshers_Regular_1D::redistributeNearVertices (SMESH_Mesh & theMesh,
+ Adaptor3d_Curve & theC3d,
+ double theLength,
+ std::list< double > & theParameters,
+ const TopoDS_Vertex & theVf,
+ const TopoDS_Vertex & theVl)
+{
+ double f = theC3d.FirstParameter(), l = theC3d.LastParameter();
+ int nPar = theParameters.size();
+ for ( int isEnd1 = 0; isEnd1 < 2; ++isEnd1 )
+ {
+ const TopoDS_Vertex & V = isEnd1 ? theVf : theVl;
+ const StdMeshers_SegmentLengthAroundVertex* hyp = getVertexHyp (theMesh, V );
+ if ( hyp ) {
+ double vertexLength = hyp->GetLength();
+ if ( vertexLength > theLength / 2.0 )
+ continue;
+ if ( isEnd1 ) { // to have a segment of interest at end of theParameters
+ theParameters.reverse();
+ std::swap( f, l );
+ }
+ if ( _hypType == NB_SEGMENTS )
+ {
+ compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
+ }
+ else if ( nPar <= 3 )
+ {
+ if ( !isEnd1 )
+ vertexLength = -vertexLength;
+ GCPnts_AbscissaPoint Discret(theC3d, vertexLength, l);
+ if ( Discret.IsDone() ) {
+ if ( nPar == 0 )
+ theParameters.push_back( Discret.Parameter());
+ else {
+ double L = GCPnts_AbscissaPoint::Length( theC3d, theParameters.back(), l);
+ if ( vertexLength < L / 2.0 )
+ theParameters.push_back( Discret.Parameter());
+ else
+ compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
+ }
+ }
+ }
+ else
+ {
+ // recompute params between the last segment and a middle one.
+ // find size of a middle segment
+ int nHalf = ( nPar-1 ) / 2;
+ list< double >::reverse_iterator itU = theParameters.rbegin();
+ std::advance( itU, nHalf );
+ double Um = *itU++;
+ double Lm = GCPnts_AbscissaPoint::Length( theC3d, Um, *itU);
+ double L = GCPnts_AbscissaPoint::Length( theC3d, *itU, l);
+ static StdMeshers_Regular_1D* auxAlgo = 0;
+ if ( !auxAlgo ) {
+ auxAlgo = new StdMeshers_Regular_1D( _gen->GetANewId(), _studyId, _gen );
+ auxAlgo->_hypType = BEG_END_LENGTH;
+ }
+ auxAlgo->_value[ BEG_LENGTH_IND ] = Lm;
+ auxAlgo->_value[ END_LENGTH_IND ] = vertexLength;
+ double from = *itU, to = l;
+ if ( isEnd1 ) {
+ std::swap( from, to );
+ std::swap( auxAlgo->_value[ BEG_LENGTH_IND ], auxAlgo->_value[ END_LENGTH_IND ]);
+ }
+ list<double> params;
+ if ( auxAlgo->computeInternalParameters( theMesh, theC3d, L, from, to, params, false ))
+ {
+ if ( isEnd1 ) params.reverse();
+ while ( 1 + nHalf-- )
+ theParameters.pop_back();
+ theParameters.splice( theParameters.end(), params );
+ }
+ else
+ {
+ compensateError(0, vertexLength, f, l, theLength, theC3d, theParameters, true );
+ }
+ }
+ if ( isEnd1 )
+ theParameters.reverse();
+ }
+ }
+}
+