fixed most ( still units error )

This commit is contained in:
AndreaRigoni
2026-03-27 15:02:17 +00:00
parent 93e5602562
commit 038c6f99f4
22 changed files with 411 additions and 277 deletions

View File

@@ -48,6 +48,8 @@
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include "Math/Transform.h"
#include "Vtk/Math/vtkDense.h"
namespace uLib {
namespace Vtk {
@@ -62,20 +64,23 @@ struct HandlerWidgetData {
vtkSmartPointer<::vtkActor> m_RotCam; // Camera ring
vtkSmartPointer<::vtkActor> m_ScaleX, m_ScaleY, m_ScaleZ; // Cubes
// cut plane to see only half of rotation handles
vtkSmartPointer<::vtkPlane> m_ClipPlane;
// picker to select the gizmo
vtkSmartPointer<::vtkCellPicker> m_Picker;
// initial transform of the object
vtkSmartPointer<::vtkTransform> m_InitialTransform;
std::vector<vtkSmartPointer<::vtkTransform>> m_TransformChain;
vtkSmartPointer<::vtkMatrix4x4> m_BaseMatrix;
// undo stack
std::vector<uLib::TRS> m_UndoStack;
HandlerWidgetData() {
m_Picker = vtkSmartPointer<::vtkCellPicker>::New();
m_InitialTransform = vtkSmartPointer<::vtkTransform>::New();
m_ClipPlane = vtkSmartPointer<::vtkPlane>::New();
m_OverlayRenderer = vtkSmartPointer<::vtkRenderer>::New();
m_BaseMatrix = vtkSmartPointer<::vtkMatrix4x4>::New();
m_HighlightedProp = nullptr;
}
};
@@ -95,7 +100,6 @@ vtkHandlerWidget::vtkHandlerWidget() : d(new HandlerWidgetData()) {
this->m_TranslationEnabled = true;
this->m_RotationEnabled = true;
this->m_ScalingEnabled = true;
d->m_BaseMatrix->Identity();
this->CreateGizmos();
}
@@ -108,19 +112,14 @@ vtkHandlerWidget::~vtkHandlerWidget() {
return d->m_OverlayRenderer;
}
void vtkHandlerWidget::SetProp3D(::vtkProp3D *prop) {
if (this->Prop3D == prop) {
return;
}
this->Prop3D = prop;
if (this->Prop3D) {
// Initialize d->m_BaseMatrix from the object's current matrix
if (this->Prop3D->GetUserMatrix()) {
this->d->m_BaseMatrix->DeepCopy(this->Prop3D->GetUserMatrix());
} else {
this->d->m_BaseMatrix->Identity();
}
this->d->m_TransformChain.clear(); // Clear any previous transform chain
this->d->m_UndoStack.clear(); // Clear history when selecting new object
this->UpdateGizmoPosition();
}
this->Modified();
@@ -247,20 +246,19 @@ void vtkHandlerWidget::OnKeyPress() {
bool ctrl = (this->Interactor->GetControlKey() != 0);
if (ctrl && key == "z") {
if (!this->d->m_TransformChain.empty()) {
if (!this->d->m_UndoStack.empty()) {
std::cout << "Undoing last transform action..." << std::endl;
this->d->m_TransformChain.pop_back();
uLib::TRS target = this->d->m_UndoStack.back();
this->d->m_UndoStack.pop_back();
// Update object from chain
vtkNew<vtkTransform> total;
total->PostMultiply();
total->SetMatrix(this->d->m_BaseMatrix.GetPointer());
for (auto& t : d->m_TransformChain) {
total->Concatenate(t);
}
if (this->Prop3D && this->Prop3D->GetUserMatrix()) {
this->Prop3D->GetUserMatrix()->DeepCopy(total->GetMatrix());
if (this->Prop3D) {
this->Prop3D->SetPosition(target.position.x(), target.position.y(), target.position.z());
// Convert Model Radians to VTK Degrees
this->Prop3D->SetOrientation(target.rotation.x() / CLHEP::degree,
target.rotation.y() / CLHEP::degree,
target.rotation.z() / CLHEP::degree);
this->Prop3D->SetScale(target.scaling.x(), target.scaling.y(), target.scaling.z());
this->Prop3D->SetUserMatrix(nullptr);
this->Prop3D->Modified();
this->UpdateGizmoPosition();
this->InvokeEvent(::vtkCommand::InteractionEvent, nullptr);
@@ -311,21 +309,12 @@ void vtkHandlerWidget::OnLeftButtonDown() {
this->StartEventPosition[0] = X;
this->StartEventPosition[1] = Y;
if (this->Prop3D) {
if (!this->Prop3D->GetUserMatrix()) {
vtkNew<vtkMatrix4x4> vmat;
this->Prop3D->SetUserMatrix(vmat);
}
// If the chain is empty, initialize base from current state?
// Actually, if we just started selecting this object, we should have initialized BaseMatrix.
// For now, let's keep d->m_InitialTransform as the state BEFORE this drag
vtkNew<vtkTransform> current;
current->PostMultiply();
current->SetMatrix(this->d->m_BaseMatrix.GetPointer());
for (auto& t : d->m_TransformChain) {
current->Concatenate(t);
}
this->d->m_InitialTransform->SetMatrix(current->GetMatrix());
// Capture current state for Undo
this->d->m_UndoStack.push_back(uLib::TRS(uLib::Vtk::VtkToMatrix4f(this->Prop3D->GetMatrix())));
if (this->d->m_UndoStack.size() > 50) this->d->m_UndoStack.erase(this->d->m_UndoStack.begin());
// Use the prop's total matrix for calculation baseline
this->d->m_InitialTransform->SetMatrix(this->Prop3D->GetMatrix());
}
this->EventCallbackCommand->SetAbortFlag(1);
this->InvokeEvent(::vtkCommand::StartInteractionEvent, nullptr);
@@ -337,27 +326,6 @@ void vtkHandlerWidget::OnLeftButtonUp() {
if (this->Interaction == IDLE)
return;
// Finalize the current interaction into the chain
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
// We need to re-calculate the final 'op' to store it
// Actually, we could have stored it in OnMouseMove, but let's re-calculate or
// just capture the delta between d->m_InitialTransform and current UserMatrix.
if (this->Prop3D && this->Prop3D->GetUserMatrix()) {
vtkNew<vtkMatrix4x4> inv;
vtkMatrix4x4::Invert(this->d->m_InitialTransform->GetMatrix(), inv);
vtkNew<vtkMatrix4x4> final_op_mat;
vtkMatrix4x4::Multiply4x4(this->Prop3D->GetUserMatrix(), inv, final_op_mat);
vtkNew<vtkTransform> final_op;
final_op->SetMatrix(final_op_mat);
this->d->m_TransformChain.push_back(final_op);
std::cout << "Action finalized. Chain size: " << this->d->m_TransformChain.size() << std::endl;
}
this->Interaction = IDLE;
this->EventCallbackCommand->SetAbortFlag(1);
this->InvokeEvent(::vtkCommand::EndInteractionEvent, nullptr);
@@ -578,9 +546,17 @@ void vtkHandlerWidget::OnMouseMove() {
total->SetMatrix(this->d->m_InitialTransform->GetMatrix()); // d->m_InitialTransform is already Base*Chain
total->Concatenate(op);
vtkMatrix4x4* targetMat = this->Prop3D->GetUserMatrix();
if (targetMat) {
targetMat->DeepCopy(total->GetMatrix());
if (this->Prop3D) {
double p[3], r[3], s[3];
total->GetPosition(p);
total->GetOrientation(r);
total->GetScale(s);
this->Prop3D->SetPosition(p);
// VTK GetOrientation already returned degrees, so r is in degrees.
// We apply it directly back to VTK.
this->Prop3D->SetOrientation(r);
this->Prop3D->SetScale(s);
this->Prop3D->SetUserMatrix(nullptr);
}
this->Prop3D->Modified();
@@ -671,7 +647,7 @@ void vtkHandlerWidget::SetTransform(::vtkTransform *t) {
void vtkHandlerWidget::GetTransform(::vtkTransform *t) {
if (!t || !this->Prop3D)
return;
t->SetMatrix(this->Prop3D->GetUserMatrix());
t->SetMatrix(this->Prop3D->GetMatrix());
}
void vtkHandlerWidget::CreateGizmos() {