fixed vtk containerbox handler
This commit is contained in:
@@ -44,5 +44,5 @@ BOOST_AUTO_TEST_CASE(vtkStructuredGridTest) {
|
||||
viewer.Start();
|
||||
}
|
||||
|
||||
BOOST_CHECK(grid_viewer.GetWidget() != nullptr);
|
||||
BOOST_CHECK(grid_viewer.GetProp() != nullptr);
|
||||
}
|
||||
|
||||
54
src/Vtk/Math/vtkDense.h
Normal file
54
src/Vtk/Math/vtkDense.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef U_VTK_DENSE_H
|
||||
#define U_VTK_DENSE_H
|
||||
|
||||
#include "Math/Dense.h"
|
||||
#include <vtkMatrix4x4.h>
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::Matrix4f to an existing vtkMatrix4x4.
|
||||
* @param src The source Eigen matrix.
|
||||
* @param dst The destination vtkMatrix4x4.
|
||||
*/
|
||||
inline void Matrix4fToVtk(const Matrix4f& src, vtkMatrix4x4* dst) {
|
||||
if (!dst)
|
||||
return;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
dst->SetElement(i, j, src(i, j));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::Matrix4f to a new vtkMatrix4x4.
|
||||
* @param src The source Eigen matrix.
|
||||
* @return A new vtkMatrix4x4 (caller is responsible for management/deletion).
|
||||
*/
|
||||
inline vtkMatrix4x4* Matrix4fToVtk(const Matrix4f& src) {
|
||||
vtkMatrix4x4* dst = vtkMatrix4x4::New();
|
||||
Matrix4fToVtk(src, dst);
|
||||
return dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts a vtkMatrix4x4 to a uLib::Matrix4f.
|
||||
* @param src The source vtkMatrix4x4.
|
||||
* @return The converted uLib::Matrix4f matrix.
|
||||
*/
|
||||
inline Matrix4f VtkToMatrix4f(vtkMatrix4x4* src) {
|
||||
Matrix4f dst;
|
||||
if (!src) {
|
||||
dst.setIdentity();
|
||||
return dst;
|
||||
}
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
dst(i, j) = src->GetElement(i, j);
|
||||
return dst;
|
||||
}
|
||||
|
||||
} // namespace Vtk
|
||||
} // namespace uLib
|
||||
|
||||
#endif // U_VTK_DENSE_H
|
||||
@@ -30,6 +30,8 @@
|
||||
#include "Math/StructuredGrid.h"
|
||||
#include "Vtk/Math/vtkStructuredGrid.h"
|
||||
|
||||
#include "Vtk/Math/vtkDense.h"
|
||||
|
||||
namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
@@ -39,73 +41,64 @@ namespace Vtk {
|
||||
|
||||
vtkStructuredGrid::vtkStructuredGrid(Content &content)
|
||||
: m_Content(&content), m_Actor(vtkActor::New()),
|
||||
m_Widget(vtkBoxWidget::New()), m_Transform(vtkTransform::New()) {
|
||||
vtkSmartPointer<vtkWidgetCallback> callback =
|
||||
vtkSmartPointer<vtkWidgetCallback>::New();
|
||||
callback->SetGrid(this);
|
||||
m_Widget->AddObserver(vtkCommand::InteractionEvent, callback);
|
||||
m_Transform(vtkTransform::New()) {
|
||||
|
||||
this->InstallPipe();
|
||||
}
|
||||
|
||||
vtkStructuredGrid::~vtkStructuredGrid() {
|
||||
m_Actor->Delete();
|
||||
m_Widget->Delete();
|
||||
m_Transform->Delete();
|
||||
}
|
||||
|
||||
void vtkStructuredGrid::SetTransform(vtkTransform *t) {
|
||||
vtkMatrix4x4 *vmat = t->GetMatrix();
|
||||
Matrix4f mat;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
mat(i, j) = vmat->GetElement(i, j);
|
||||
Matrix4f mat = VtkToMatrix4f(vmat);
|
||||
m_Content->SetMatrix(mat);
|
||||
|
||||
vtkSmartPointer<vtkMatrix4x4> vmat2 = vtkSmartPointer<vtkMatrix4x4>::New();
|
||||
mat = m_Content->GetWorldMatrix();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
vmat2->SetElement(i, j, mat(i, j));
|
||||
Matrix4fToVtk(mat, vmat2);
|
||||
m_Transform->SetMatrix(vmat2);
|
||||
m_Transform->Update();
|
||||
this->Update();
|
||||
}
|
||||
|
||||
vtkBoxWidget *vtkStructuredGrid::GetWidget() { return m_Widget; }
|
||||
void vtkStructuredGrid::Update() {
|
||||
if (!m_Content) return;
|
||||
|
||||
void vtkStructuredGrid::Update() { m_Actor->GetMapper()->Update(); }
|
||||
vtkProp3D* actor = vtkProp3D::SafeDownCast(this->GetProp());
|
||||
if (!actor) return;
|
||||
|
||||
vtkMatrix4x4* vmat = actor->GetUserMatrix();
|
||||
if (!vmat) return;
|
||||
|
||||
Matrix4f transform = VtkToMatrix4f(vmat);
|
||||
m_Content->SetMatrix(transform);
|
||||
|
||||
m_Content->Updated(); // Notify others (like raytracer)
|
||||
}
|
||||
|
||||
void vtkStructuredGrid::InstallPipe() {
|
||||
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
|
||||
vtkSmartPointer<vtkTransformPolyDataFilter> filter =
|
||||
vtkSmartPointer<vtkTransformPolyDataFilter>::New();
|
||||
|
||||
vtkSmartPointer<vtkMatrix4x4> vmat = vtkSmartPointer<vtkMatrix4x4>::New();
|
||||
Matrix4f mat = m_Content->GetWorldMatrix();
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (int j = 0; j < 4; ++j)
|
||||
vmat->SetElement(i, j, mat(i, j));
|
||||
m_Transform->SetMatrix(vmat);
|
||||
filter->SetTransform(m_Transform);
|
||||
filter->SetInputConnection(cube->GetOutputPort());
|
||||
|
||||
|
||||
Vector3i dims = m_Content->GetDims();
|
||||
cube->SetBounds(0, dims(0), 0, dims(1), 0, dims(2));
|
||||
cube->Update();
|
||||
filter->Update();
|
||||
|
||||
vtkSmartPointer<vtkPolyDataMapper> mapper =
|
||||
vtkSmartPointer<vtkPolyDataMapper>::New();
|
||||
mapper->SetInputConnection(filter->GetOutputPort());
|
||||
mapper->SetInputConnection(cube->GetOutputPort());
|
||||
|
||||
m_Actor->SetMapper(mapper);
|
||||
m_Actor->GetProperty()->SetRepresentationToSurface();
|
||||
m_Actor->GetProperty()->SetEdgeVisibility(true);
|
||||
m_Actor->GetProperty()->SetOpacity(0.4);
|
||||
m_Actor->GetProperty()->SetAmbient(0.7);
|
||||
this->Update();
|
||||
m_Widget->SetProp3D(m_Actor);
|
||||
|
||||
vtkNew<vtkMatrix4x4> vmat;
|
||||
Matrix4fToVtk(m_Content->GetWorldMatrix(), vmat);
|
||||
m_Actor->SetUserMatrix(vmat);
|
||||
|
||||
this->SetProp(m_Actor);
|
||||
}
|
||||
|
||||
@@ -56,32 +56,12 @@ public:
|
||||
|
||||
void SetTransform(class vtkTransform *t);
|
||||
|
||||
class vtkBoxWidget *GetWidget();
|
||||
|
||||
void Update();
|
||||
virtual void Update() override;
|
||||
|
||||
private:
|
||||
void InstallPipe();
|
||||
|
||||
class vtkWidgetCallback : public vtkCommand {
|
||||
public:
|
||||
static vtkWidgetCallback *New() { return new vtkWidgetCallback; }
|
||||
|
||||
void SetGrid(uLib::Vtk::vtkStructuredGrid *grid) { this->grid = grid; }
|
||||
|
||||
virtual void Execute(vtkObject *caller, unsigned long, void *) {
|
||||
vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
|
||||
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget *>(caller);
|
||||
widget->GetTransform(t);
|
||||
grid->SetTransform(t);
|
||||
}
|
||||
|
||||
private:
|
||||
uLib::Vtk::vtkStructuredGrid *grid;
|
||||
};
|
||||
|
||||
vtkActor *m_Actor;
|
||||
vtkBoxWidget *m_Widget;
|
||||
StructuredGrid *m_Content;
|
||||
vtkTransform *m_Transform;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user