Mesh: reduce the use of const_cast in Mesh module

This commit is contained in:
wmayer
2022-05-18 18:17:45 +02:00
committed by wwmayer
parent 9db32a3911
commit 0a4488462c
16 changed files with 104 additions and 135 deletions

View File

@@ -54,7 +54,7 @@ public:
int Index;
MeshCore::PointIndex PIndex[2];
MeshCore::FacetIndex NIndex[2];
Base::Reference<MeshObject> Mesh;
Base::Reference<const MeshObject> Mesh;
};
} // namespace Mesh

View File

@@ -31,7 +31,7 @@
using namespace Mesh;
Facet::Facet(const MeshCore::MeshFacet& face, MeshObject* obj, MeshCore::FacetIndex index)
Facet::Facet(const MeshCore::MeshFacet& face, const MeshObject* obj, MeshCore::FacetIndex index)
: Index(index), Mesh(obj)
{
for (int i=0; i<3; i++) {

View File

@@ -43,7 +43,7 @@ class MeshObject;
class MeshExport Facet : public MeshCore::MeshGeomFacet
{
public:
Facet(const MeshCore::MeshFacet& face = MeshCore::MeshFacet(), MeshObject* obj = nullptr, MeshCore::FacetIndex index = MeshCore::FACET_INDEX_MAX);
Facet(const MeshCore::MeshFacet& face = MeshCore::MeshFacet(), const MeshObject* obj = nullptr, MeshCore::FacetIndex index = MeshCore::FACET_INDEX_MAX);
Facet(const Facet& f);
~Facet();
@@ -54,7 +54,7 @@ public:
MeshCore::FacetIndex Index;
MeshCore::PointIndex PIndex[3];
MeshCore::FacetIndex NIndex[3];
Base::Reference<MeshObject> Mesh;
Base::Reference<const MeshObject> Mesh;
};
} // namespace Mesh

View File

@@ -168,7 +168,7 @@ Base::Matrix4D MeshObject::getTransform() const
Base::BoundBox3d MeshObject::getBoundBox()const
{
const_cast<MeshCore::MeshKernel&>(_kernel).RecalcBoundBox();
_kernel.RecalcBoundBox();
Base::BoundBox3f Bnd = _kernel.GetBoundBox();
Base::BoundBox3d Bnd2;
@@ -293,12 +293,17 @@ double MeshObject::getVolume() const
return _kernel.GetVolume();
}
MeshPoint MeshObject::getPoint(PointIndex index) const
Base::Vector3d MeshObject::getPoint(PointIndex index) const
{
Base::Vector3f vertf = _kernel.GetPoint(index);
Base::Vector3d vertd(vertf.x, vertf.y, vertf.z);
vertd = _Mtrx * vertd;
MeshPoint point(vertd, const_cast<MeshObject*>(this), index);
return vertd;
}
MeshPoint MeshObject::getMeshPoint(PointIndex index) const
{
MeshPoint point(getPoint(index), this, index);
return point;
}
@@ -311,10 +316,7 @@ void MeshObject::getPoints(std::vector<Base::Vector3d> &Points,
unsigned long ctpoints = _kernel.CountPoints();
Points.reserve(ctpoints);
for (unsigned long i=0; i<ctpoints; i++) {
Base::Vector3f vertf = _kernel.GetPoint(i);
Base::Vector3d vertd(vertf.x, vertf.y, vertf.z);
vertd = mat * vertd;
Points.push_back(vertd);
Points.push_back(getPoint(i));
}
// nullify translation part
@@ -331,9 +333,9 @@ void MeshObject::getPoints(std::vector<Base::Vector3d> &Points,
}
}
Mesh::Facet MeshObject::getFacet(FacetIndex index) const
Mesh::Facet MeshObject::getMeshFacet(FacetIndex index) const
{
Mesh::Facet face(_kernel.GetFacets()[index], const_cast<MeshObject*>(this), index);
Mesh::Facet face(_kernel.GetFacets()[index], this, index);
return face;
}
@@ -343,7 +345,7 @@ void MeshObject::getFaces(std::vector<Base::Vector3d> &Points,std::vector<Facet>
unsigned long ctpoints = _kernel.CountPoints();
Points.reserve(ctpoints);
for (unsigned long i=0; i<ctpoints; i++) {
Points.push_back(this->getPoint(i));
Points.push_back(getPoint(i));
}
unsigned long ctfacets = _kernel.CountFacets();
@@ -405,7 +407,7 @@ void MeshObject::save(const char* file, MeshCore::MeshIO::Format f,
aWriter.SetGroups(groups);
if (mat && mat->library.empty()) {
Base::FileInfo fi(file);
const_cast<MeshCore::Material*>(mat)->library = fi.fileNamePure() + ".mtl";
mat->library = fi.fileNamePure() + ".mtl";
}
aWriter.Transform(this->_Mtrx);
@@ -800,7 +802,7 @@ std::vector<PointIndex> MeshObject::getPointsFromFacets(const std::vector<FacetI
return _kernel.GetFacetPoints(facets);
}
void MeshObject::updateMesh(const std::vector<FacetIndex>& facets)
void MeshObject::updateMesh(const std::vector<FacetIndex>& facets) const
{
std::vector<PointIndex> points;
points = _kernel.GetFacetPoints(facets);
@@ -810,12 +812,12 @@ void MeshObject::updateMesh(const std::vector<FacetIndex>& facets)
alg.SetPointsFlag(points, MeshCore::MeshPoint::SEGMENT);
}
void MeshObject::updateMesh()
void MeshObject::updateMesh() const
{
MeshCore::MeshAlgorithm alg(_kernel);
alg.ResetFacetFlag(MeshCore::MeshFacet::SEGMENT);
alg.ResetPointFlag(MeshCore::MeshPoint::SEGMENT);
for (std::vector<Segment>::iterator it = this->_segments.begin();
for (std::vector<Segment>::const_iterator it = this->_segments.begin();
it != this->_segments.end(); ++it) {
std::vector<PointIndex> points;
points = _kernel.GetFacetPoints(it->getIndices());
@@ -1948,7 +1950,7 @@ std::vector<Segment> MeshObject::getSegmentsOfType(MeshObject::GeometryType type
const std::vector<MeshCore::MeshSegment>& data = surf->GetSegments();
for (std::vector<MeshCore::MeshSegment>::const_iterator it = data.begin(); it != data.end(); ++it) {
segm.emplace_back(const_cast<MeshObject*>(this), *it, false);
segm.emplace_back(this, *it, false);
}
}
@@ -1962,7 +1964,7 @@ MeshObject::const_point_iterator::const_point_iterator(const MeshObject* mesh, P
{
this->_p_it.Set(index);
this->_p_it.Transform(_mesh->_Mtrx);
this->_point.Mesh = const_cast<MeshObject*>(_mesh);
this->_point.Mesh = _mesh;
}
MeshObject::const_point_iterator::const_point_iterator(const MeshObject::const_point_iterator& fi)
@@ -2031,7 +2033,7 @@ MeshObject::const_facet_iterator::const_facet_iterator(const MeshObject* mesh, F
{
this->_f_it.Set(index);
this->_f_it.Transform(_mesh->_Mtrx);
this->_facet.Mesh = const_cast<MeshObject*>(_mesh);
this->_facet.Mesh = _mesh;
}
MeshObject::const_facet_iterator::const_facet_iterator(const MeshObject::const_facet_iterator& fi)

View File

@@ -42,7 +42,6 @@
#include "Core/Iterator.h"
#include "MeshPoint.h"
#include "Facet.h"
#include "MeshPoint.h"
#include "Segment.h"
namespace Py {
@@ -138,8 +137,9 @@ public:
unsigned long countEdges () const;
unsigned long countSegments() const;
bool isSolid() const;
MeshPoint getPoint(PointIndex) const;
Mesh::Facet getFacet(FacetIndex) const;
Base::Vector3d getPoint(PointIndex) const;
MeshPoint getMeshPoint(PointIndex) const;
Mesh::Facet getMeshFacet(FacetIndex) const;
double getSurface() const;
double getVolume() const;
/** Get points from object with given accuracy */
@@ -408,8 +408,8 @@ public:
private:
void deletedFacets(const std::vector<FacetIndex>& remFacets);
void updateMesh(const std::vector<FacetIndex>&);
void updateMesh();
void updateMesh(const std::vector<FacetIndex>&) const;
void updateMesh() const;
void swapKernel(MeshCore::MeshKernel& m, const std::vector<std::string>& g);
void copySegments(const MeshObject&);
void swapSegments(MeshObject&);

View File

@@ -75,13 +75,12 @@ void Feature::onChanged(const App::Property* prop)
{
// if the placement has changed apply the change to the mesh data as well
if (prop == &this->Placement) {
MeshObject& mesh = const_cast<MeshObject&>(this->Mesh.getValue());
mesh.setTransform(this->Placement.getValue().toMatrix());
this->Mesh.setTransform(this->Placement.getValue().toMatrix());
}
// if the mesh data has changed check and adjust the transformation as well
else if (prop == &this->Mesh) {
Base::Placement p;
p.fromMatrix(this->Mesh.getValue().getTransform());
p.fromMatrix(this->Mesh.getTransform());
if (p != this->Placement.getValue())
this->Placement.setValue(p);
}

View File

@@ -49,14 +49,14 @@ class MeshExport MeshPoint : public Vector3d
public:
/// simple constructor
MeshPoint(const Vector3d& vec = Vector3d(),MeshObject* obj = nullptr, unsigned int index = UINT_MAX)
MeshPoint(const Vector3d& vec = Vector3d(), const MeshObject* obj = nullptr, unsigned int index = UINT_MAX)
:Vector3d(vec),Index(index),Mesh(obj)
{}
bool isBound() const {return Index != UINT_MAX;}
unsigned int Index;
Base::Reference<MeshObject> Mesh;
Base::Reference<const MeshObject> Mesh;
};
} // namespace Mesh

View File

@@ -32,16 +32,6 @@ work!
</UserDocu>
</Documentation>
</Methode>
<Methode Name="move">
<Documentation>
<UserDocu>method move(Vector)
This method moves the point in the mesh along the
given vector. This affects the geometry of the mesh.
Be aware that after moving point(s) the mesh can
have self intersections!
</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Index" ReadOnly="true">
<Documentation>
<UserDocu>The index of this point in the MeshObject</UserDocu>
@@ -66,26 +56,23 @@ have self intersections!
</Documentation>
<Parameter Name="Normal" Type="Object"/>
</Attribute>
<Attribute Name="x" ReadOnly="false">
<Attribute Name="x" ReadOnly="true">
<Documentation>
<UserDocu>The X component of the point.
Setting this value also affects the mesh if this point is connected to it.</UserDocu>
<UserDocu>The X component of the point.</UserDocu>
</Documentation>
<Parameter Name="x" Type="Float"/>
</Attribute>
<Attribute Name="y" ReadOnly="false">
<Attribute Name="y" ReadOnly="true">
<Documentation>
<UserDocu>The Y component of the point.
Setting this value also affects the mesh if this point is connected to it.</UserDocu>
<UserDocu>The Y component of the point.</UserDocu>
</Documentation>
<Parameter Name="y" Type="Float"/>
</Attribute>
<Attribute Name="z" ReadOnly="false">
<Attribute Name="z" ReadOnly="true">
<Documentation>
<UserDocu>The Z component of the point.
Setting this value also affects the mesh if this point is connected to it.</UserDocu>
<UserDocu>The Z component of the point.</UserDocu>
</Documentation>
<Parameter Name="z" Type="Float"/>
</Attribute>
</PythonExport>
</GenerateModel>
</GenerateModel>

View File

@@ -86,42 +86,6 @@ PyObject* MeshPointPy::unbound(PyObject *args)
Py_Return;
}
PyObject* MeshPointPy::move(PyObject *args)
{
if (!getMeshPointPtr()->isBound()) {
PyErr_SetString(PyExc_RuntimeError, "This object is not bounded to a mesh, so no topological operation is possible!");
return nullptr;
}
if (getMeshPointPtr()->Mesh->countPoints() <= getMeshPointPtr()->Index) {
PyErr_SetString(PyExc_IndexError, "Index out of range");
return nullptr;
}
double x=0.0,y=0.0,z=0.0;
PyObject *object;
Base::Vector3d vec;
do {
if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {
vec.Set(x,y,z);
break;
}
PyErr_Clear(); // set by PyArg_ParseTuple()
if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
break;
}
PyErr_SetString(PyExc_TypeError, "Tuple of three floats or Vector expected");
return nullptr;
}
while (false);
getMeshPointPtr()->Mesh->movePoint(getMeshPointPtr()->Index,vec);
Py_Return;
}
Py::Long MeshPointPy::getIndex() const
{
return Py::Long((long) getMeshPointPtr()->Index);
@@ -167,17 +131,6 @@ Py::Float MeshPointPy::getx() const
return Py::Float(x);
}
void MeshPointPy::setx(Py::Float arg)
{
MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
ptr->x = (double)arg;
if (getMeshPointPtr()->isBound()) {
if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index)
getMeshPointPtr()->Mesh->setPoint(getMeshPointPtr()->Index,*ptr);
}
}
Py::Float MeshPointPy::gety() const
{
MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
@@ -191,17 +144,6 @@ Py::Float MeshPointPy::gety() const
return Py::Float(y);
}
void MeshPointPy::sety(Py::Float arg)
{
MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
ptr->y = (double)arg;
if (getMeshPointPtr()->isBound()) {
if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index)
getMeshPointPtr()->Mesh->setPoint(getMeshPointPtr()->Index,*ptr);
}
}
Py::Float MeshPointPy::getz() const
{
MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
@@ -215,17 +157,6 @@ Py::Float MeshPointPy::getz() const
return Py::Float(z);
}
void MeshPointPy::setz(Py::Float arg)
{
MeshPointPy::PointerType ptr = static_cast<MeshPointPy::PointerType>(_pcTwinPointer);
ptr->z = (double)arg;
if (getMeshPointPtr()->isBound()) {
if (getMeshPointPtr()->Mesh->countPoints() > getMeshPointPtr()->Index)
getMeshPointPtr()->Mesh->setPoint(getMeshPointPtr()->Index,*ptr);
}
}
PyObject *MeshPointPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;

View File

@@ -538,6 +538,16 @@ void PropertyMeshKernel::setPointIndices(const std::vector<std::pair<PointIndex,
hasSetValue();
}
void PropertyMeshKernel::setTransform(const Base::Matrix4D& rclTrf)
{
_meshObject->setTransform(rclTrf);
}
Base::Matrix4D PropertyMeshKernel::getTransform() const
{
return _meshObject->getTransform();
}
PyObject *PropertyMeshKernel::getPyObject()
{
if (!meshPyObject) {

View File

@@ -216,6 +216,8 @@ public:
/// Transform the real mesh data
void transformGeometry(const Base::Matrix4D &rclMat);
void setPointIndices( const std::vector<std::pair<PointIndex, Base::Vector3f> >& );
void setTransform(const Base::Matrix4D& rclTrf);
Base::Matrix4D getTransform() const;
//@}
/** @name Python interface */

View File

@@ -176,7 +176,17 @@ lines = mesh.section(mesh2, [ConnectLines=True, MinDist=0.0001])
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getPointNormals" Const="true">
<Methode Name="movePoint">
<Documentation>
<UserDocu>movePoint(int, Vector)
This method moves the point in the mesh along the
given vector. This affects the geometry of the mesh.
Be aware that after moving point(s) the mesh can
have self intersections!
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getPointNormals" Const="true">
<Documentation>
<UserDocu>
getPointNormals()
@@ -507,7 +517,7 @@ smooth([iteration=1,maxError=FLT_MAX])</UserDocu>
</Documentation>
</Methode>
<!-- End of hack -->
<Methode Name="nearestFacetOnRay" Const="true">
<Methode Name="nearestFacetOnRay" Const="true">
<Documentation>
<UserDocu>nearestFacetOnRay(tuple, tuple) -> dict
Get the index and intersection point of the nearest facet to a ray.

View File

@@ -828,6 +828,34 @@ PyObject* MeshPy::setPoint(PyObject *args)
Py_Return;
}
PyObject* MeshPy::movePoint(PyObject *args)
{
unsigned long index;
Base::Vector3d vec;
do {
double x=0.0,y=0.0,z=0.0;
if (PyArg_ParseTuple(args, "kddd", &index,&x,&y,&z)) {
vec.Set(x,y,z);
break;
}
PyErr_Clear(); // set by PyArg_ParseTuple()
PyObject *object;
if (PyArg_ParseTuple(args,"kO!", &index, &(Base::VectorPy::Type), &object)) {
vec = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
break;
}
PyErr_SetString(PyExc_TypeError, "Tuple of three floats or Vector expected");
return nullptr;
}
while (false);
getMeshObjectPtr()->movePoint(index, vec);
Py_Return;
}
PyObject* MeshPy::getPointNormals(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))

View File

@@ -36,14 +36,14 @@
using namespace Mesh;
Segment::Segment(MeshObject* mesh, bool mod)
Segment::Segment(const MeshObject* mesh, bool mod)
: _mesh(mesh)
, _save(false)
, _modifykernel(mod)
{
}
Segment::Segment(MeshObject* mesh, const std::vector<FacetIndex>& inds, bool mod)
Segment::Segment(const MeshObject* mesh, const std::vector<FacetIndex>& inds, bool mod)
: _mesh(mesh)
, _indices(inds)
, _save(false)
@@ -135,7 +135,7 @@ Segment::const_facet_iterator& Segment::const_facet_iterator::operator=(const Se
return *this;
}
void Segment::const_facet_iterator::dereference()
void Segment::const_facet_iterator::dereference() const
{
this->_f_it.Set(*_it);
this->_facet.MeshCore::MeshGeomFacet::operator = (*_f_it);
@@ -149,13 +149,13 @@ void Segment::const_facet_iterator::dereference()
const Facet& Segment::const_facet_iterator::operator*() const
{
const_cast<const_facet_iterator*>(this)->dereference();
this->dereference();
return this->_facet;
}
const Facet* Segment::const_facet_iterator::operator->() const
{
const_cast<const_facet_iterator*>(this)->dereference();
this->dereference();
return &(this->_facet);
}

View File

@@ -38,8 +38,8 @@ class MeshObject;
class MeshExport Segment
{
public:
Segment(MeshObject*, bool mod);
Segment(MeshObject*, const std::vector<FacetIndex>& inds, bool mod);
Segment(const MeshObject*, bool mod);
Segment(const MeshObject*, const std::vector<FacetIndex>& inds, bool mod);
void addIndices(const std::vector<FacetIndex>& inds);
void removeIndices(const std::vector<FacetIndex>& inds);
const std::vector<FacetIndex>& getIndices() const;
@@ -62,7 +62,7 @@ public:
friend class MeshObject;
private:
MeshObject* _mesh;
const MeshObject* _mesh;
std::vector<FacetIndex> _indices;
std::string _name;
std::string _color;
@@ -85,10 +85,10 @@ public:
const_facet_iterator& operator++();
const_facet_iterator& operator--();
private:
void dereference();
void dereference() const;
const Segment* _segment;
Facet _facet;
MeshCore::MeshFacetIterator _f_it;
mutable Facet _facet;
mutable MeshCore::MeshFacetIterator _f_it;
std::vector<FacetIndex>::const_iterator _it;
};

View File

@@ -1252,8 +1252,8 @@ std::vector<Mesh::FacetIndex> ViewProviderMesh::getFacetsOfRegion(const SbViewpo
SoSeparator* root = new SoSeparator();
root->ref();
root->addChild(camera);
root->addChild(const_cast<ViewProviderMesh*>(this)->getCoordNode());
root->addChild(const_cast<ViewProviderMesh*>(this)->getShapeNode());
root->addChild(this->getCoordNode());
root->addChild(this->getShapeNode());
Gui::SoGLSelectAction gl(region, select);
gl.apply(root);
root->unref();