From abc5413b34eb340d191ace64bfe07effe07e2892 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 17 May 2022 11:54:11 +0200 Subject: [PATCH] Mesh: avoid problematic const_cast --- src/Mod/Mesh/App/Core/Elements.h | 30 ++++++++++++++-------------- src/Mod/Mesh/App/Core/Iterator.h | 9 +++++---- src/Mod/Mesh/App/Core/MeshKernel.cpp | 2 +- src/Mod/Mesh/App/Core/MeshKernel.h | 23 ++++++++++++++++----- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/Mod/Mesh/App/Core/Elements.h b/src/Mod/Mesh/App/Core/Elements.h index 5f176bc649..97971623ab 100644 --- a/src/Mod/Mesh/App/Core/Elements.h +++ b/src/Mod/Mesh/App/Core/Elements.h @@ -121,9 +121,9 @@ public: */ //@{ void SetFlag (TFlagType tF) const - { const_cast(this)->_ucFlag |= static_cast(tF); } + { _ucFlag |= static_cast(tF); } void ResetFlag (TFlagType tF) const - { const_cast(this)->_ucFlag &= ~static_cast(tF); } + { _ucFlag &= ~static_cast(tF); } bool IsFlag (TFlagType tF) const { return (_ucFlag & static_cast(tF)) == static_cast(tF); } void ResetInvalid () const @@ -133,7 +133,7 @@ public: bool IsValid () const { return !IsFlag(INVALID); } void SetProperty(unsigned long uP) const - { const_cast(this)->_ulProp = uP; } + { _ulProp = uP; } //@} // Assignment @@ -145,8 +145,8 @@ public: inline bool operator < (const MeshPoint &rclPt) const; public: - unsigned char _ucFlag; /**< Flag member */ - unsigned long _ulProp; /**< Free usable property */ + mutable unsigned char _ucFlag; /**< Flag member */ + mutable unsigned long _ulProp; /**< Free usable property */ }; /** @@ -250,15 +250,15 @@ public: */ //@{ void SetFlag (TFlagType tF) const - { const_cast(this)->_ucFlag |= static_cast(tF); } + { _ucFlag |= static_cast(tF); } void ResetFlag (TFlagType tF) const - { const_cast(this)->_ucFlag &= ~static_cast(tF); } + { _ucFlag &= ~static_cast(tF); } bool IsFlag (TFlagType tF) const { return (_ucFlag & static_cast(tF)) == static_cast(tF); } void ResetInvalid () const { ResetFlag(INVALID); } void SetProperty(unsigned long uP) const - { const_cast(this)->_ulProp = uP; } + { _ulProp = uP; } /** * Marks a facet as invalid. Should be used only temporary from within an algorithm * (e.g. deletion of several facets) but must not be set permanently. @@ -346,8 +346,8 @@ public: } public: - unsigned char _ucFlag; /**< Flag member. */ - unsigned long _ulProp; /**< Free usable property. */ + mutable unsigned char _ucFlag; /**< Flag member. */ + mutable unsigned long _ulProp; /**< Free usable property. */ PointIndex _aulPoints[3]; /**< Indices of corner points. */ FacetIndex _aulNeighbours[3]; /**< Indices of neighbour facets. */ }; @@ -427,7 +427,7 @@ public: /** * Calculates the facet normal for storing internally. */ - inline void CalcNormal (); + inline void CalcNormal () const; /** * Arrange the facet normal so the both vectors have the same orientation. */ @@ -552,8 +552,8 @@ public: bool IsCoplanar(const MeshGeomFacet &facet) const; protected: - Base::Vector3f _clNormal; /**< Normal of the facet. */ - bool _bNormalCalculated; /**< True if the normal is already calculated. */ + mutable Base::Vector3f _clNormal; /**< Normal of the facet. */ + mutable bool _bNormalCalculated; /**< True if the normal is already calculated. */ public: Base::Vector3f _aclPoints[3]; /**< Geometric corner points. */ @@ -795,7 +795,7 @@ inline float MeshGeomFacet::DistancePlaneToPoint (const Base::Vector3f &rclPoint return float(fabs(rclPoint.DistanceToPlane(_aclPoints[0], GetNormal()))); } -inline void MeshGeomFacet::CalcNormal () +inline void MeshGeomFacet::CalcNormal () const { _clNormal = (_aclPoints[1] - _aclPoints[0]) % (_aclPoints[2] - _aclPoints[0]); _clNormal.Normalize(); @@ -805,7 +805,7 @@ inline void MeshGeomFacet::CalcNormal () inline Base::Vector3f MeshGeomFacet::GetNormal () const { if (_bNormalCalculated == false) - const_cast(this)->CalcNormal(); + CalcNormal(); return _clNormal; } diff --git a/src/Mod/Mesh/App/Core/Iterator.h b/src/Mod/Mesh/App/Core/Iterator.h index 07e74ed2f2..e687b323f4 100644 --- a/src/Mod/Mesh/App/Core/Iterator.h +++ b/src/Mod/Mesh/App/Core/Iterator.h @@ -259,7 +259,7 @@ protected: protected: const MeshKernel& _rclMesh; const MeshPointArray& _rclPAry; - MeshPoint _clPoint; + mutable MeshPoint _clPoint; MeshPointArray::_TConstIterator _clIter; bool _bApply; Base::Matrix4D _clTrf; @@ -443,10 +443,11 @@ inline void MeshPointIterator::Transform( const Base::Matrix4D& rclTrf ) } inline const MeshPoint& MeshPointIterator::Dereference () const -{ // We change only the value of the point but not the actual iterator - const_cast(this)->_clPoint = *_clIter; +{ + // We change only the value of the point but not the actual iterator + _clPoint = *_clIter; if ( _bApply ) - const_cast(this)->_clPoint = _clTrf * _clPoint; + _clPoint = _clTrf * _clPoint; return _clPoint; } diff --git a/src/Mod/Mesh/App/Core/MeshKernel.cpp b/src/Mod/Mesh/App/Core/MeshKernel.cpp index cca4dd085f..d30b8c329c 100644 --- a/src/Mod/Mesh/App/Core/MeshKernel.cpp +++ b/src/Mod/Mesh/App/Core/MeshKernel.cpp @@ -1053,7 +1053,7 @@ void MeshKernel::Smooth(int iterations, float stepsize) LaplaceSmoothing(*this).Smooth(iterations); } -void MeshKernel::RecalcBoundBox () +void MeshKernel::RecalcBoundBox () const { _clBoundBox.SetVoid(); for (MeshPointArray::_TConstIterator pI = _aclPointArray.begin(); pI != _aclPointArray.end(); pI++) diff --git a/src/Mod/Mesh/App/Core/MeshKernel.h b/src/Mod/Mesh/App/Core/MeshKernel.h index fc7b4abb32..6c9a032060 100644 --- a/src/Mod/Mesh/App/Core/MeshKernel.h +++ b/src/Mod/Mesh/App/Core/MeshKernel.h @@ -100,7 +100,7 @@ public: /** Forces a recalculation of the bounding box. This method should be called after * the removal of points.or after a transformation of the data structure. */ - void RecalcBoundBox (); + void RecalcBoundBox () const; /** Returns the point at the given index. This method is rather slow and should be * called occasionally only. For fast access the MeshPointIterator interfsce should @@ -124,6 +124,9 @@ public: /** Returns the point indices of the given facet index. */ inline void GetFacetPoints (FacetIndex ulFaIndex, PointIndex &rclP0, PointIndex &rclP1, PointIndex &rclP2) const; + /** Returns the point indices of the given facet index. */ + inline void SetFacetPoints (FacetIndex ulFaIndex, PointIndex rclP0, + PointIndex rclP1, PointIndex rclP2); /** Returns the point indices of the given facet indices. */ std::vector GetFacetPoints(const std::vector&) const; /** Returns the facet indices that share the given point indices. */ @@ -446,7 +449,7 @@ protected: MeshPointArray _aclPointArray; /**< Holds the array of geometric points. */ MeshFacetArray _aclFacetArray; /**< Holds the array of facets. */ - Base::BoundBox3f _clBoundBox; /**< The current calculated bounding box. */ + mutable Base::BoundBox3f _clBoundBox; /**< The current calculated bounding box. */ bool _bValid; /**< Current state of validality. */ // friends @@ -555,9 +558,19 @@ inline void MeshKernel::GetFacetPoints (FacetIndex ulFaIndex, PointIndex &rclP0, { assert(ulFaIndex < _aclFacetArray.size()); const MeshFacet& rclFacet = _aclFacetArray[ulFaIndex]; - rclP0 = rclFacet._aulPoints[0]; - rclP1 = rclFacet._aulPoints[1]; - rclP2 = rclFacet._aulPoints[2]; + rclP0 = rclFacet._aulPoints[0]; + rclP1 = rclFacet._aulPoints[1]; + rclP2 = rclFacet._aulPoints[2]; +} + +inline void MeshKernel::SetFacetPoints (FacetIndex ulFaIndex, PointIndex rclP0, + PointIndex rclP1, PointIndex rclP2) +{ + assert(ulFaIndex < _aclFacetArray.size()); + MeshFacet& rclFacet = _aclFacetArray[ulFaIndex]; + rclFacet._aulPoints[0] = rclP0; + rclFacet._aulPoints[1] = rclP1; + rclFacet._aulPoints[2] = rclP2; }