Mesh: refactor MeshPy::writeInventor

This commit is contained in:
wmayer
2022-08-03 17:56:24 +02:00
parent 88e32eba9b
commit 322080d4f1
3 changed files with 33 additions and 26 deletions

View File

@@ -564,6 +564,36 @@ void MeshObject::load(std::istream& in)
#endif
}
void MeshObject::writeInventor(std::ostream& str, float creaseangle) const
{
const MeshCore::MeshPointArray& point = getKernel().GetPoints();
const MeshCore::MeshFacetArray& faces = getKernel().GetFacets();
std::vector<Base::Vector3f> coords;
coords.reserve(point.size());
std::copy(point.begin(), point.end(), std::back_inserter(coords));
std::vector<int> indices;
indices.reserve(4 * faces.size());
for (const auto& it : faces) {
indices.push_back(it._aulPoints[0]);
indices.push_back(it._aulPoints[1]);
indices.push_back(it._aulPoints[2]);
indices.push_back(-1);
}
Base::InventorBuilder builder(str);
builder.beginSeparator();
builder.addTransformation(getTransform());
builder.addShapeHints(creaseangle);
builder.beginPoints();
builder.addPoints(coords);
builder.endPoints();
builder.addIndexedFaceSet(indices);
builder.endSeparator();
builder.close();
}
void MeshObject::addFacet(const MeshCore::MeshGeomFacet& facet)
{
_kernel.AddFacet(facet);

View File

@@ -179,6 +179,7 @@ public:
// Save and load in internal format
void save(std::ostream&) const;
void load(std::istream&);
void writeInventor(std::ostream& str, float creaseangle=0.0f) const;
//@}
/** @name Manipulation */

View File

@@ -25,7 +25,6 @@
#include <Base/VectorPy.h>
#include <Base/Handle.h>
#include <Base/Builder3D.h>
#include <Base/Converter.h>
#include <Base/GeometryPyCXX.h>
#include <Base/MatrixPy.h>
@@ -311,32 +310,9 @@ PyObject* MeshPy::writeInventor(PyObject *args)
if (!PyArg_ParseTuple(args, "|f",&creaseangle))
return nullptr;
MeshObject* mesh = getMeshObjectPtr();
const MeshCore::MeshFacetArray& faces = mesh->getKernel().GetFacets();
std::vector<int> indices;
std::vector<Base::Vector3f> coords;
coords.reserve(mesh->countPoints());
for (MeshObject::const_point_iterator it = mesh->points_begin(); it != mesh->points_end(); ++it)
coords.emplace_back((float)it->x,(float)it->y,(float)it->z);
indices.reserve(4*faces.size());
for (MeshCore::MeshFacetArray::_TConstIterator it = faces.begin(); it != faces.end(); ++it) {
indices.push_back(it->_aulPoints[0]);
indices.push_back(it->_aulPoints[1]);
indices.push_back(it->_aulPoints[2]);
indices.push_back(-1);
}
std::stringstream result;
Base::InventorBuilder builder(result);
builder.beginSeparator();
builder.addShapeHints(creaseangle);
builder.beginPoints();
builder.addPoints(coords);
builder.endPoints();
builder.addIndexedFaceSet(indices);
builder.endSeparator();
builder.close();
MeshObject* mesh = getMeshObjectPtr();
mesh->writeInventor(result, creaseangle);
return Py::new_reference_to(Py::String(result.str()));
}