add triangle mesh affine transform

This commit is contained in:
AndreaRigoni
2026-03-19 13:11:49 +00:00
parent 4cb4560921
commit ca5f576b99
6 changed files with 96 additions and 21 deletions

View File

@@ -41,6 +41,16 @@ BOOST_AUTO_TEST_CASE(vtkTriangleMeshConstruction) {
mesh.AddTriangle(Vector3i(0, 1, 2));
Vtk::vtkTriangleMesh v_mesh(mesh);
Object::connect(&mesh, &TriangleMesh::Updated, [&mesh]() {
Vector3f points[3];
points[0] = mesh.GetPoint(0);
points[1] = mesh.GetPoint(1);
points[2] = mesh.GetPoint(2);
std::cout << "mesh updated: " << points[0].transpose() << " " << points[1].transpose()
<< " " << points[2].transpose() << std::endl;
});
v_mesh.Update();
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {

View File

@@ -40,6 +40,9 @@
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkMatrix4x4.h>
#include <vtkNew.h>
#include "Math/vtkDense.h"
#include "Vtk/Math/vtkTriangleMesh.h"
#include <iostream>
@@ -55,12 +58,10 @@ void vtkTriangleMesh::vtk2uLib_update() {
<< "number of polys = " << number_of_triangles << "\n"
<< "//////\n";
m_content.Points().resize(number_of_points);
m_content.Points().clear();
for (int i = 0; i < number_of_points; ++i) {
double *point = m_Poly->GetPoint(i);
m_content.Points()[i](0) = point[0];
m_content.Points()[i](1) = point[1];
m_content.Points()[i](2) = point[2];
m_content.Points().push_back(Vector3f(point[0], point[1], point[2]));
}
m_content.Triangles().resize(number_of_triangles);
@@ -83,11 +84,8 @@ void vtkTriangleMesh::uLib2vtk_update() {
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->SetNumberOfPoints(number_of_points);
for (vtkIdType i = 0; i < number_of_points; i++) {
double x, y, z;
x = m_content.Points().at(i)(0);
y = m_content.Points().at(i)(1);
z = m_content.Points().at(i)(2);
points->SetPoint(i, x, y, z);
Vector3f p = m_content.Points().at(i);
points->SetPoint(i, p(0), p(1), p(2));
}
vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New();
@@ -105,7 +103,33 @@ void vtkTriangleMesh::uLib2vtk_update() {
m_Poly->SetPoints(points);
m_Poly->SetPolys(polys);
m_Poly->Modified();
}
void vtkTriangleMesh::contentUpdate() {
vtkMatrix4x4 *vmat = m_Actor->GetUserMatrix();
if (!vmat) {
vtkNew<vtkMatrix4x4> mat;
m_Actor->SetUserMatrix(mat);
vmat = mat;
}
Matrix4f transform = m_content.GetWorldMatrix();
Matrix4fToVtk(transform, vmat);
uLib2vtk_update();
m_Poly->Modified();
m_Actor->GetMapper()->Update();
Puppet::Update();
}
void vtkTriangleMesh::Update() {
vtkMatrix4x4 *vmat = m_Actor->GetUserMatrix();
if (!vmat) return;
Matrix4f transform = VtkToMatrix4f(vmat);
m_content.SetMatrix(transform);
m_content.Updated();
}
// -------------------------------------------------------------------------- //
@@ -116,10 +140,18 @@ vtkTriangleMesh::vtkTriangleMesh(vtkTriangleMesh::Content &content)
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(m_Poly);
m_Actor->SetMapper(mapper);
vtkNew<vtkMatrix4x4> vmat;
Matrix4fToVtk(m_content.GetWorldMatrix(), vmat);
m_Actor->SetUserMatrix(vmat);
this->SetProp(m_Actor);
Object::connect(&m_content, &Content::Updated, this, &vtkTriangleMesh::contentUpdate);
this->contentUpdate();
}
vtkTriangleMesh::~vtkTriangleMesh() {
Object::disconnect(&m_content, &Content::Updated, this, &vtkTriangleMesh::contentUpdate);
m_Poly->Delete();
m_Actor->Delete();
}
@@ -160,7 +192,5 @@ void vtkTriangleMesh::ReadFromStlFile(const char *filename) {
vtkPolyData *vtkTriangleMesh::GetPolyData() const { return m_Poly; }
void vtkTriangleMesh::Update() { uLib2vtk_update(); }
} // namespace Vtk
} // namespace uLib

View File

@@ -53,7 +53,9 @@ public:
virtual class vtkPolyData *GetPolyData() const;
void Update();
virtual void contentUpdate();
virtual void Update();
private:
void vtk2uLib_update();