Mesh: avoid problematic const_cast

This commit is contained in:
wmayer
2022-05-17 11:54:11 +02:00
parent 6a3281438f
commit abc5413b34
4 changed files with 39 additions and 25 deletions

View File

@@ -121,9 +121,9 @@ public:
*/
//@{
void SetFlag (TFlagType tF) const
{ const_cast<MeshPoint*>(this)->_ucFlag |= static_cast<unsigned char>(tF); }
{ _ucFlag |= static_cast<unsigned char>(tF); }
void ResetFlag (TFlagType tF) const
{ const_cast<MeshPoint*>(this)->_ucFlag &= ~static_cast<unsigned char>(tF); }
{ _ucFlag &= ~static_cast<unsigned char>(tF); }
bool IsFlag (TFlagType tF) const
{ return (_ucFlag & static_cast<unsigned char>(tF)) == static_cast<unsigned char>(tF); }
void ResetInvalid () const
@@ -133,7 +133,7 @@ public:
bool IsValid () const
{ return !IsFlag(INVALID); }
void SetProperty(unsigned long uP) const
{ const_cast<MeshPoint*>(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<MeshFacet*>(this)->_ucFlag |= static_cast<unsigned char>(tF); }
{ _ucFlag |= static_cast<unsigned char>(tF); }
void ResetFlag (TFlagType tF) const
{ const_cast<MeshFacet*>(this)->_ucFlag &= ~static_cast<unsigned char>(tF); }
{ _ucFlag &= ~static_cast<unsigned char>(tF); }
bool IsFlag (TFlagType tF) const
{ return (_ucFlag & static_cast<unsigned char>(tF)) == static_cast<unsigned char>(tF); }
void ResetInvalid () const
{ ResetFlag(INVALID); }
void SetProperty(unsigned long uP) const
{ const_cast<MeshFacet*>(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<MeshGeomFacet*>(this)->CalcNormal();
CalcNormal();
return _clNormal;
}

View File

@@ -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<MeshPointIterator*>(this)->_clPoint = *_clIter;
{
// We change only the value of the point but not the actual iterator
_clPoint = *_clIter;
if ( _bApply )
const_cast<MeshPointIterator*>(this)->_clPoint = _clTrf * _clPoint;
_clPoint = _clTrf * _clPoint;
return _clPoint;
}

View File

@@ -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++)

View File

@@ -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<PointIndex> GetFacetPoints(const std::vector<FacetIndex>&) 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;
}