3 Commits

Author SHA1 Message Date
AndreaRigoni
1374821344 added gizmo but not yet working 2026-03-08 10:21:38 +00:00
AndreaRigoni
2548582036 attach a widget (not working well yet) 2026-03-08 09:42:28 +00:00
AndreaRigoni
32a1104769 detector chamber in vtk 2026-03-08 08:46:21 +00:00
12 changed files with 1024 additions and 241 deletions

View File

@@ -1,10 +1,14 @@
set(HEADERS uLibVtkInterface.h
uLibVtkViewer.h
vtkContainerBox.h)
vtkContainerBox.h
vtkHandlerWidget.h
)
set(SOURCES uLibVtkInterface.cxx
uLibVtkViewer.cpp
vtkContainerBox.cpp)
vtkContainerBox.cpp
vtkHandlerWidget.cpp
)
## Pull in Math VTK wrappers (sets MATH_SOURCES / MATH_HEADERS)
add_subdirectory(Math)

View File

@@ -9,11 +9,13 @@
set(HEP_DETECTORS_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonScatter.cxx
${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonEvent.cxx
${CMAKE_CURRENT_SOURCE_DIR}/vtkDetectorChamber.cxx
PARENT_SCOPE)
set(HEP_DETECTORS_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonScatter.h
${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonEvent.h
${CMAKE_CURRENT_SOURCE_DIR}/vtkDetectorChamber.h
PARENT_SCOPE)
if(BUILD_TESTING)

View File

@@ -1,6 +1,7 @@
# TESTS
set(TESTS
vtkMuonScatterTest
vtkDetectorChamberTest
)
set(LIBRARIES

View File

@@ -0,0 +1,58 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
#include "HEP/Detectors/DetectorChamber.h"
#include "Vtk/uLibVtkViewer.h"
#define BOOST_TEST_MODULE vtkDetectorChamberTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(vtkDetectorChamberTest) {
uLib::DetectorChamber d1, d2;
d1.SetSize(uLib::Vector3f(10, 20, 2));
d1.SetPosition(uLib::Vector3f(0, 0, 0));
d2.SetSize(uLib::Vector3f(10, 20, 2));
d2.SetPosition(uLib::Vector3f(0, 0, 20));
uLib::Vtk::vtkDetectorChamber vtkDetectorChamber(&d1);
uLib::Vtk::vtkDetectorChamber vtkDetectorChamber2(&d2);
if (!vtkDetectorChamber.GetProp()) {
BOOST_FAIL("vtkDetectorChamber::GetProp() returned NULL");
}
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
uLib::Vtk::Viewer viewer;
viewer.AddPuppet(vtkDetectorChamber);
viewer.AddPuppet(vtkDetectorChamber2);
viewer.Start();
}
BOOST_CHECK(true); // reached here without crash
}

View File

@@ -0,0 +1,199 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include <vtkAxes.h>
#include <vtkCubeSource.h>
#include <vtkLineSource.h>
#include <vtkMatrix4x4.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
#include "Vtk/vtkHandlerWidget.h"
namespace uLib {
namespace Vtk {
vtkDetectorChamber::vtkDetectorChamber(DetectorChamber *content)
: vtkContainerBox(content) {
m_Widget = vtkHandlerWidget::New();
m_Callback = vtkWidgetCallback::New();
m_PickerCallback = vtkSelectionCallback::New();
m_Callback->SetChamber(this);
m_PickerCallback->SetChamber(this);
m_Widget->AddObserver(vtkCommand::InteractionEvent, m_Callback);
}
vtkDetectorChamber::~vtkDetectorChamber() {
m_Widget->Delete();
m_Callback->Delete();
m_PickerCallback->Delete();
}
DetectorChamber *vtkDetectorChamber::GetContent() {
return static_cast<DetectorChamber *>(m_Content);
}
void vtkDetectorChamber::PrintSelf(std::ostream &o) const {
vtkContainerBox::PrintSelf(o);
}
void vtkDetectorChamber::ConnectInteractor(
vtkRenderWindowInteractor *interactor) {
if (!interactor)
return;
m_Widget->SetInteractor(interactor);
m_Widget->SetProp3D(m_Cube);
m_Widget->PlaceWidget();
interactor->AddObserver(vtkCommand::LeftButtonPressEvent, m_PickerCallback);
}
void vtkDetectorChamber::Update() {
if (!m_Content)
return;
Matrix4f mat = static_cast<Content *>(m_Content)->GetWorldMatrix();
vtkSmartPointer<vtkMatrix4x4> vmat = vtkSmartPointer<vtkMatrix4x4>::New();
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
vmat->SetElement(i, j, mat(i, j));
m_Cube->SetUserMatrix(vmat);
m_Axes->SetUserMatrix(vmat);
m_Pivot->SetUserMatrix(vmat);
}
void vtkDetectorChamber::InstallPipe() {
if (!m_Content)
return;
// Cube: unit box (0,0,0) -> (1,1,1)
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
cube->SetBounds(0, 1, 0, 1, 0, 1);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cube->GetOutputPort());
m_Cube->SetMapper(mapper);
m_Cube->GetProperty()->SetRepresentationToWireframe();
m_Cube->GetProperty()->SetAmbient(0.7);
// Axes: corner origin
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(0, 0, 0);
axes->SetScaleFactor(0.2);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
m_Axes->SetMapper(mapper);
m_Axes->GetProperty()->SetLineWidth(3);
m_Axes->GetProperty()->SetAmbient(0.4);
// Pivot: center of unit box
vtkSmartPointer<vtkAxes> pivot = vtkSmartPointer<vtkAxes>::New();
pivot->SetOrigin(0.5, 0.5, 0.5);
pivot->SetScaleFactor(0.1);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(pivot->GetOutputPort());
m_Pivot->SetMapper(mapper);
m_Pivot->GetProperty()->SetLineWidth(3);
m_Pivot->GetProperty()->SetAmbient(0.4);
this->SetProp(m_Cube);
this->SetProp(m_Axes);
this->SetProp(m_Pivot);
this->Update();
}
void vtkDetectorChamber::vtkWidgetCallback::Execute(vtkObject *caller,
unsigned long, void *) {
vtkHandlerWidget *widget = reinterpret_cast<vtkHandlerWidget *>(caller);
vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
widget->GetTransform(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);
chamber->GetContent()->SetMatrix(mat);
// Reset internal matrix to identity to avoid double-transformation
vtkProp3D *prop = widget->GetProp3D();
if (prop) {
prop->SetPosition(0, 0, 0);
prop->SetOrientation(0, 0, 0);
prop->SetScale(1, 1, 1);
}
chamber->Update();
}
void vtkDetectorChamber::vtkSelectionCallback::Execute(vtkObject *caller,
unsigned long, void *) {
vtkRenderWindowInteractor *interactor =
reinterpret_cast<vtkRenderWindowInteractor *>(caller);
vtkSmartPointer<vtkPropPicker> picker = vtkSmartPointer<vtkPropPicker>::New();
int *pos = interactor->GetEventPosition();
picker->Pick(
pos[0], pos[1], 0,
interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
vtkProp *picked = picker->GetViewProp();
if (picked == chamber->m_Cube || picked == chamber->m_Axes ||
picked == chamber->m_Pivot) {
if (!chamber->m_Widget->GetEnabled()) {
// Register current position/rotation/scale from content //
Matrix4f mat = chamber->GetContent()->GetWorldMatrix();
vtkSmartPointer<vtkMatrix4x4> vmat = vtkSmartPointer<vtkMatrix4x4>::New();
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
vmat->SetElement(i, j, mat(i, j));
vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New();
t->SetMatrix(vmat);
chamber->m_Widget->SetCurrentRenderer(
interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
chamber->m_Widget->SetTransform(t);
chamber->m_Widget->PlaceWidget();
chamber->m_Widget->On();
}
} else {
if (chamber->m_Widget->GetEnabled()) {
chamber->m_Widget->Off();
}
}
}
} // namespace Vtk
} // namespace uLib

View File

@@ -0,0 +1,91 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#ifndef VTK_DETECTOR_CHAMBER_H
#define VTK_DETECTOR_CHAMBER_H
#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include "HEP/Detectors/DetectorChamber.h"
#include "Math/Dense.h"
#include "Vtk/uLibVtkInterface.h"
#include "Vtk/vtkContainerBox.h"
namespace uLib {
namespace Vtk {
class vtkHandlerWidget;
class vtkDetectorChamber : public vtkContainerBox {
typedef DetectorChamber Content;
public:
vtkDetectorChamber(DetectorChamber *content);
~vtkDetectorChamber();
Content *GetContent();
void Update();
void ConnectInteractor(vtkRenderWindowInteractor *interactor) override;
void PrintSelf(std::ostream &o) const;
protected:
void InstallPipe() override;
private:
class vtkWidgetCallback : public vtkCommand {
public:
static vtkWidgetCallback *New() { return new vtkWidgetCallback; }
void SetChamber(uLib::Vtk::vtkDetectorChamber *ch) { this->chamber = ch; }
virtual void Execute(vtkObject *caller, unsigned long, void *) override;
private:
uLib::Vtk::vtkDetectorChamber *chamber;
};
class vtkSelectionCallback : public vtkCommand {
public:
static vtkSelectionCallback *New() { return new vtkSelectionCallback; }
void SetChamber(uLib::Vtk::vtkDetectorChamber *ch) { this->chamber = ch; }
virtual void Execute(vtkObject *caller, unsigned long, void *) override;
private:
uLib::Vtk::vtkDetectorChamber *chamber;
};
vtkHandlerWidget *m_Widget;
vtkWidgetCallback *m_Callback;
vtkSelectionCallback *m_PickerCallback;
};
} // namespace Vtk
} // namespace uLib
#endif // VTK_DETECTOR_CHAMBER_H

View File

@@ -23,37 +23,32 @@
//////////////////////////////////////////////////////////////////////////////*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <vtkSmartPointer.h>
#include <vtkRendererCollection.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRendererCollection.h>
#include <vtkSmartPointer.h>
#include <vtkTextProperty.h>
#include <vtkAxesActor.h>
#include <vtkCamera.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkTextProperty.h>
#include "uLibVtkViewer.h"
// Custom interactor style: disables spin/inertia so the scene only
// rotates while the mouse is actively being moved with the button held.
class vtkInteractorStyleNoSpin : public vtkInteractorStyleTrackballCamera
{
class vtkInteractorStyleNoSpin : public vtkInteractorStyleTrackballCamera {
public:
static vtkInteractorStyleNoSpin *New();
vtkTypeMacro(vtkInteractorStyleNoSpin, vtkInteractorStyleTrackballCamera);
// Override: when the left button is released, immediately stop any
// ongoing motion (rotation/spin) so no momentum is carried over.
void OnLeftButtonUp() override
{
void OnLeftButtonUp() override {
this->StopState();
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}
@@ -63,23 +58,18 @@ vtkStandardNewMacro(vtkInteractorStyleNoSpin);
namespace uLib {
namespace Vtk {
////////////////////////////////////////////////////////////////////////////////
///// VTK VIEWER //////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Viewer::Viewer() :
m_RenderWindow(vtkRenderWindow::New()),
m_Renderer(vtkRenderer::New()),
Viewer::Viewer()
: m_RenderWindow(vtkRenderWindow::New()), m_Renderer(vtkRenderer::New()),
m_Annotation(vtkCornerAnnotation::New()),
m_Marker(vtkOrientationMarkerWidget::New())
{
m_Marker(vtkOrientationMarkerWidget::New()) {
InstallPipe();
}
Viewer::~Viewer()
{
Viewer::~Viewer() {
UninstallPipe();
m_Annotation->Delete();
@@ -88,8 +78,7 @@ Viewer::~Viewer()
m_RenderWindow->Delete();
}
void Viewer::InstallPipe()
{
void Viewer::InstallPipe() {
m_RenderWindow->AddRenderer(m_Renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
@@ -101,19 +90,18 @@ void Viewer::InstallPipe()
renderWindowInteractor->SetInteractorStyle(style);
// annotation //
m_Annotation->GetTextProperty()->SetColor(1,1,1);
m_Annotation->GetTextProperty()->SetColor(1, 1, 1);
m_Annotation->GetTextProperty()->SetFontFamilyToArial();
m_Annotation->GetTextProperty()->SetOpacity(0.5);
m_Annotation->SetMaximumFontSize(10);
m_Annotation->SetText(0,"uLib VTK Viewer - OpenCMT all right reserved.");
m_Annotation->SetText(0, "uLib VTK Viewer - OpenCMT all right reserved.");
m_Renderer->AddViewProp(m_Annotation);
// orientation marker //
vtkSmartPointer<vtkAxesActor> axes =
vtkSmartPointer<vtkAxesActor>::New();
vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
m_Marker->SetInteractor(renderWindowInteractor);
m_Marker->SetOrientationMarker(axes);
m_Marker->SetViewport(0.0,0.0,0.2,0.2);
m_Marker->SetViewport(0.0, 0.0, 0.2, 0.2);
m_Marker->SetEnabled(true);
m_Marker->InteractiveOff();
@@ -121,64 +109,51 @@ void Viewer::InstallPipe()
m_RenderWindow->Render();
}
void Viewer::UninstallPipe()
{
void Viewer::UninstallPipe() {
m_Renderer->RemoveAllViewProps();
m_Renderer->Clear();
}
void Viewer::addProp(vtkProp *prop)
{
void Viewer::addProp(vtkProp *prop) {
m_Renderer->AddActor(prop);
m_Renderer->Render();
}
void Viewer::RemoveProp(vtkProp *prop)
{
void Viewer::RemoveProp(vtkProp *prop) {
m_Renderer->RemoveViewProp(prop);
m_Renderer->Render();
}
void Viewer::AddPuppet(Puppet &prop)
{
void Viewer::AddPuppet(Puppet &prop) {
prop.ConnectRenderer(m_Renderer);
prop.ConnectInteractor(this->GetInteractor());
m_Renderer->Render();
}
void Viewer::RemovePuppet(Puppet &prop)
{
void Viewer::RemovePuppet(Puppet &prop) {
prop.DisconnectRenderer(m_Renderer);
m_Renderer->Render();
}
void Viewer::Start()
{
m_RenderWindow->GetInteractor()->Start();
}
void Viewer::Start() { m_RenderWindow->GetInteractor()->Start(); }
vtkCornerAnnotation *Viewer::GetAnnotation()
{
return m_Annotation;
}
vtkCornerAnnotation *Viewer::GetAnnotation() { return m_Annotation; }
vtkRenderer *Viewer::GetRenderer()
{
return m_Renderer;
}
vtkRenderer *Viewer::GetRenderer() { return m_Renderer; }
vtkRenderWindowInteractor *Viewer::GetInteractor()
{
vtkRenderWindowInteractor *Viewer::GetInteractor() {
return m_RenderWindow->GetInteractor();
}
void Viewer::Reset()
{
m_Renderer->ResetCameraClippingRange();
m_Renderer->ResetCamera();
void Viewer::Reset() {
this->ZoomAuto();
m_Renderer->Render();
}
void Viewer::ZoomAuto() {
m_Renderer->ResetCameraClippingRange();
m_Renderer->ResetCamera();
}
} // vtk
} // uLib
} // namespace Vtk
} // namespace uLib

View File

@@ -23,18 +23,16 @@
//////////////////////////////////////////////////////////////////////////////*/
#ifndef ULIBVTKVIEWER_H
#define ULIBVTKVIEWER_H
#include <iostream>
#include "uLibVtkInterface.h"
#include <iostream>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkCornerAnnotation.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
class vtkProp;
class vtk3DWidget;
@@ -45,23 +43,19 @@ class vtkRenderer;
namespace uLib {
namespace Vtk {
template <class T>
class Tie {
template <class T> class Tie {
public:
void DoAction() {
std::cout << "Tie::DoAction -> generic Tie does nothing\n";
}
};
class Viewer {
public:
Viewer();
~Viewer();
void AddPuppet(Puppet &prop);
void RemovePuppet(Puppet &prop);
@@ -69,14 +63,15 @@ public:
void AddWidget(vtk3DWidget *widget);
void Reset();
void ZoomAuto();
void Start();
vtkCornerAnnotation *GetAnnotation();
vtkRenderer * GetRenderer();
vtkRenderer *GetRenderer();
vtkRenderWindowInteractor * GetInteractor();
vtkRenderWindowInteractor *GetInteractor();
void addProp(vtkProp *prop);
@@ -92,16 +87,12 @@ private:
vtkOrientationMarkerWidget *m_Marker;
};
template <>
class Tie<Viewer> {
template <> class Tie<Viewer> {
public:
void DoAction() {
std::cout << " VIEWER TIE !!! \n";
}
void DoAction() { std::cout << " VIEWER TIE !!! \n"; }
};
} // vtk
} // uLib
} // namespace Vtk
} // namespace uLib
#endif // ULIBVTKVIEWER_H

View File

@@ -23,65 +23,55 @@
//////////////////////////////////////////////////////////////////////////////*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkActor.h>
#include <vtkAxes.h>
#include <vtkAssembly.h>
#include <vtkAxes.h>
#include <vtkCubeSource.h>
#include <vtkLineSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkLineSource.h>
#include <vtkSmartPointer.h>
#include "vtkContainerBox.h"
namespace uLib {
namespace Vtk {
vtkContainerBox::vtkContainerBox(vtkContainerBox::Content &content) :
m_Cube(vtkActor::New()),
m_Axes(vtkActor::New()),
m_Pivot(vtkActor::New()),
m_Content(&content)
{
vtkContainerBox::vtkContainerBox(vtkContainerBox::Content *content)
: m_Cube(vtkActor::New()), m_Axes(vtkActor::New()),
m_Pivot(vtkActor::New()), m_Content(content) {
this->InstallPipe();
}
vtkContainerBox::~vtkContainerBox()
{
vtkContainerBox::~vtkContainerBox() {
m_Cube->Delete();
m_Axes->Delete();
m_Pivot->Delete();
}
vtkPolyData *vtkContainerBox::GetPolyData() const
{
vtkPolyData *vtkContainerBox::GetPolyData() const {
// TODO
return NULL;
}
void vtkContainerBox::InstallPipe()
{
if(!m_Content) return;
void vtkContainerBox::InstallPipe() {
if (!m_Content)
return;
Content *c = m_Content;
// CUBE
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
Vector3f p = c->GetPosition();
cube->SetCenter(p(0),p(1),p(2));
Vector4f p1 = c->GetWorldPoint(HPoint3f(0,0,0));
Vector4f p2 = c->GetWorldPoint(HPoint3f(1,1,1));
cube->SetCenter(p(0), p(1), p(2));
Vector4f p1 = c->GetWorldPoint(HPoint3f(0, 0, 0));
Vector4f p2 = c->GetWorldPoint(HPoint3f(1, 1, 1));
vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New();
line->SetPoint1(p1(0),p1(1),p1(2));
line->SetPoint2(p2(0),p2(1),p2(2));
line->SetPoint1(p1(0), p1(1), p1(2));
line->SetPoint2(p2(0), p2(1), p2(2));
line->Update();
cube->SetBounds(line->GetOutput()->GetBounds());
vtkSmartPointer<vtkPolyDataMapper> mapper =
@@ -94,7 +84,7 @@ void vtkContainerBox::InstallPipe()
// AXES //
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(p1(0),p1(1),p1(2));
axes->SetOrigin(p1(0), p1(1), p1(2));
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
@@ -107,7 +97,7 @@ void vtkContainerBox::InstallPipe()
// PIVOT //
axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(p(0),p(1),p(2));
axes->SetOrigin(p(0), p(1), p(2));
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
@@ -123,7 +113,5 @@ void vtkContainerBox::InstallPipe()
this->SetProp(m_Pivot);
}
} // vtk
} // uLib
} // namespace Vtk
} // namespace uLib

View File

@@ -23,29 +23,26 @@
//////////////////////////////////////////////////////////////////////////////*/
#ifndef U_VTKCONTAINERBOX_H
#define U_VTKCONTAINERBOX_H
#include "uLibVtkInterface.h"
#include "Math/ContainerBox.h"
#include "uLibVtkInterface.h"
namespace uLib {
namespace Vtk {
class vtkContainerBox : public Puppet, public Polydata {
typedef ContainerBox Content;
public:
vtkContainerBox(Content &content);
vtkContainerBox(Content *content);
~vtkContainerBox();
virtual class vtkPolyData *GetPolyData() const;
private:
void InstallPipe();
protected:
virtual void InstallPipe();
vtkActor *m_Cube;
vtkActor *m_Axes;
@@ -53,7 +50,7 @@ private:
vtkContainerBox::Content *m_Content;
};
} // vtk
} // uLib
} // namespace Vtk
} // namespace uLib
#endif // VTKCONTAINERBOX_H

View File

@@ -0,0 +1,366 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include "vtkHandlerWidget.h"
#include <vtkArcSource.h>
#include <vtkArrowSource.h>
#include <vtkCallbackCommand.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkCubeSource.h>
#include <vtkInteractorObserver.h>
#include <vtkMath.h>
#include <vtkMatrix4x4.h>
#include <vtkObjectFactory.h>
#include <vtkPolyDataMapper.h>
#include <vtkProp3D.h>
#include <vtkPropPicker.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
namespace uLib {
namespace Vtk {
vtkStandardNewMacro(vtkHandlerWidget);
vtkHandlerWidget::vtkHandlerWidget() {
this->Interaction = IDLE;
this->m_Picker = vtkSmartPointer<::vtkPropPicker>::New();
this->m_InitialTransform = vtkSmartPointer<::vtkTransform>::New();
this->EventCallbackCommand->SetCallback(vtkHandlerWidget::ProcessEvents);
this->EventCallbackCommand->SetClientData(this);
this->CreateGizmos();
}
vtkHandlerWidget::~vtkHandlerWidget() {}
void vtkHandlerWidget::SetEnabled(int enabling) {
if (enabling) {
if (this->Enabled)
return;
if (!this->Interactor) {
vtkErrorMacro(
<< "The interactor must be set prior to enabling the widget");
return;
}
if (!this->CurrentRenderer) {
this->CurrentRenderer = this->Interactor->FindPokedRenderer(
this->Interactor->GetLastEventPosition()[0],
this->Interactor->GetLastEventPosition()[1]);
}
if (!this->CurrentRenderer) {
return;
}
this->Enabled = 1;
// Add observers
::vtkRenderWindowInteractor *i = this->Interactor;
i->AddObserver(::vtkCommand::LeftButtonPressEvent,
this->EventCallbackCommand, this->Priority);
i->AddObserver(::vtkCommand::LeftButtonReleaseEvent,
this->EventCallbackCommand, this->Priority);
i->AddObserver(::vtkCommand::MouseMoveEvent, this->EventCallbackCommand,
this->Priority);
this->UpdateGizmoPosition();
this->CurrentRenderer->AddActor(m_AxesX);
this->CurrentRenderer->AddActor(m_AxesY);
this->CurrentRenderer->AddActor(m_AxesZ);
this->CurrentRenderer->AddActor(m_RotX);
this->CurrentRenderer->AddActor(m_RotY);
this->CurrentRenderer->AddActor(m_RotZ);
this->CurrentRenderer->AddActor(m_ScaleX);
this->CurrentRenderer->AddActor(m_ScaleY);
this->CurrentRenderer->AddActor(m_ScaleZ);
this->InvokeEvent(::vtkCommand::EnableEvent, nullptr);
} else {
if (!this->Enabled)
return;
this->Enabled = 0;
this->Interactor->RemoveObserver(this->EventCallbackCommand);
if (this->CurrentRenderer) {
this->CurrentRenderer->RemoveActor(m_AxesX);
this->CurrentRenderer->RemoveActor(m_AxesY);
this->CurrentRenderer->RemoveActor(m_AxesZ);
this->CurrentRenderer->RemoveActor(m_RotX);
this->CurrentRenderer->RemoveActor(m_RotY);
this->CurrentRenderer->RemoveActor(m_RotZ);
this->CurrentRenderer->RemoveActor(m_ScaleX);
this->CurrentRenderer->RemoveActor(m_ScaleY);
this->CurrentRenderer->RemoveActor(m_ScaleZ);
}
this->InvokeEvent(::vtkCommand::DisableEvent, nullptr);
}
if (this->Interactor) {
this->Interactor->Render();
}
}
void vtkHandlerWidget::ProcessEvents(::vtkObject *caller, unsigned long event,
void *clientdata, void *calldata) {
(void)caller;
(void)calldata;
vtkHandlerWidget *self = reinterpret_cast<vtkHandlerWidget *>(clientdata);
switch (event) {
case ::vtkCommand::LeftButtonPressEvent:
self->OnLeftButtonDown();
break;
case ::vtkCommand::LeftButtonReleaseEvent:
self->OnLeftButtonUp();
break;
case ::vtkCommand::MouseMoveEvent:
self->OnMouseMove();
break;
}
}
void vtkHandlerWidget::OnLeftButtonDown() {
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
if (!this->CurrentRenderer) {
this->CurrentRenderer = this->Interactor->FindPokedRenderer(X, Y);
}
this->m_Picker->Pick(X, Y, 0.0, this->CurrentRenderer);
::vtkProp *prop = this->m_Picker->GetViewProp();
if (!prop)
return;
this->Interaction = IDLE;
if (prop == m_AxesX)
this->Interaction = TRANS_X;
else if (prop == m_AxesY)
this->Interaction = TRANS_Y;
else if (prop == m_AxesZ)
this->Interaction = TRANS_Z;
else if (prop == m_RotX)
this->Interaction = ROT_X;
else if (prop == m_RotY)
this->Interaction = ROT_Y;
else if (prop == m_RotZ)
this->Interaction = ROT_Z;
else if (prop == m_ScaleX)
this->Interaction = SCALE_X;
else if (prop == m_ScaleY)
this->Interaction = SCALE_Y;
else if (prop == m_ScaleZ)
this->Interaction = SCALE_Z;
if (this->Interaction != IDLE) {
this->StartEventPosition[0] = X;
this->StartEventPosition[1] = Y;
if (this->Prop3D) {
this->m_InitialTransform->SetMatrix(this->Prop3D->GetMatrix());
}
this->EventCallbackCommand->SetAbortFlag(1);
this->InvokeEvent(::vtkCommand::StartInteractionEvent, nullptr);
this->Interactor->Render();
}
}
void vtkHandlerWidget::OnLeftButtonUp() {
if (this->Interaction == IDLE)
return;
this->Interaction = IDLE;
this->InvokeEvent(::vtkCommand::EndInteractionEvent, nullptr);
this->Interactor->Render();
}
void vtkHandlerWidget::OnMouseMove() {
if (this->Interaction == IDLE || !this->Prop3D)
return;
int X = this->Interactor->GetEventPosition()[0];
int Y = this->Interactor->GetEventPosition()[1];
double dx = X - this->StartEventPosition[0];
double dy = Y - this->StartEventPosition[1];
vtkSmartPointer<::vtkTransform> t = vtkSmartPointer<::vtkTransform>::New();
t->PostMultiply();
t->SetMatrix(this->m_InitialTransform->GetMatrix());
double factor = 0.01;
switch (this->Interaction) {
case TRANS_X:
t->Translate(dx * factor, 0, 0);
break;
case TRANS_Y:
t->Translate(0, dy * factor, 0);
break;
case TRANS_Z:
t->Translate(0, 0, dy * factor);
break;
case ROT_X:
t->RotateX(dy);
break;
case ROT_Y:
t->RotateY(dx);
break;
case ROT_Z:
t->RotateZ(dx);
break;
case SCALE_X:
t->Scale(std::max(0.1, 1.0 + dx * factor), 1.0, 1.0);
break;
case SCALE_Y:
t->Scale(1.0, std::max(0.1, 1.0 + dy * factor), 1.0);
break;
case SCALE_Z:
t->Scale(1.0, 1.0, std::max(0.1, 1.0 + dy * factor));
break;
}
this->Prop3D->SetUserMatrix(t->GetMatrix());
this->UpdateGizmoPosition();
this->InvokeEvent(::vtkCommand::InteractionEvent, nullptr);
this->Interactor->Render();
}
void vtkHandlerWidget::PlaceWidget(double bounds[6]) {
(void)bounds;
this->UpdateGizmoPosition();
}
void vtkHandlerWidget::PlaceWidget() { this->UpdateGizmoPosition(); }
void vtkHandlerWidget::SetTransform(::vtkTransform *t) {
if (!t || !this->Prop3D)
return;
this->Prop3D->SetUserMatrix(t->GetMatrix());
this->UpdateGizmoPosition();
}
void vtkHandlerWidget::GetTransform(::vtkTransform *t) {
if (!t || !this->Prop3D)
return;
t->SetMatrix(this->Prop3D->GetUserMatrix());
}
void vtkHandlerWidget::CreateGizmos() {
auto create_arrow = [](double dir[3], double color[3]) {
auto arrow = vtkSmartPointer<::vtkArrowSource>::New();
auto mapper = vtkSmartPointer<::vtkPolyDataMapper>::New();
mapper->SetInputConnection(arrow->GetOutputPort());
auto actor = vtkSmartPointer<::vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(color);
auto t = vtkSmartPointer<::vtkTransform>::New();
if (dir[1] > 0)
t->RotateZ(90);
else if (dir[2] > 0)
t->RotateY(-90);
actor->SetUserTransform(t);
return actor;
};
auto create_ring = [](int axis, double color[3]) {
auto arc = vtkSmartPointer<::vtkArcSource>::New();
arc->SetCenter(0, 0, 0);
arc->SetResolution(64);
if (axis == 0) {
arc->SetPoint1(0, 1, 0);
arc->SetPoint2(0, -1, 0);
} else if (axis == 1) {
arc->SetPoint1(1, 0, 0);
arc->SetPoint2(-1, 0, 0);
} else if (axis == 2) {
arc->SetPoint1(1, 0, 0);
arc->SetPoint2(-1, 0, 0);
}
auto mapper = vtkSmartPointer<::vtkPolyDataMapper>::New();
mapper->SetInputConnection(arc->GetOutputPort());
auto actor = vtkSmartPointer<::vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(color);
actor->GetProperty()->SetLineWidth(3);
return actor;
};
double red[] = {0.8, 0.1, 0.1}, green[] = {0.1, 0.8, 0.1},
blue[] = {0.1, 0.1, 0.8};
double x[] = {1, 0, 0}, y[] = {0, 1, 0}, z[] = {0, 0, 1};
m_AxesX = create_arrow(x, red);
m_AxesY = create_arrow(y, green);
m_AxesZ = create_arrow(z, blue);
m_RotX = create_ring(0, red);
m_RotY = create_ring(1, green);
m_RotZ = create_ring(2, blue);
auto create_cube = [](double pos[3], double color[3]) {
auto cube = vtkSmartPointer<::vtkCubeSource>::New();
cube->SetCenter(pos[0], pos[1], pos[2]);
cube->SetXLength(0.12);
cube->SetYLength(0.12);
cube->SetZLength(0.12);
auto mapper = vtkSmartPointer<::vtkPolyDataMapper>::New();
mapper->SetInputConnection(cube->GetOutputPort());
auto actor = vtkSmartPointer<::vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(color);
return actor;
};
double px[] = {1.2, 0, 0}, py[] = {0, 1.2, 0}, pz[] = {0, 0, 1.2};
m_ScaleX = create_cube(px, red);
m_ScaleY = create_cube(py, green);
m_ScaleZ = create_cube(pz, blue);
}
void vtkHandlerWidget::UpdateGizmoPosition() {
if (!this->Prop3D)
return;
::vtkMatrix4x4 *mat = this->Prop3D->GetMatrix();
m_AxesX->SetUserMatrix(mat);
m_AxesY->SetUserMatrix(mat);
m_AxesZ->SetUserMatrix(mat);
m_RotX->SetUserMatrix(mat);
m_RotY->SetUserMatrix(mat);
m_RotZ->SetUserMatrix(mat);
m_ScaleX->SetUserMatrix(mat);
m_ScaleY->SetUserMatrix(mat);
m_ScaleZ->SetUserMatrix(mat);
}
} // namespace Vtk
} // namespace uLib

111
src/Vtk/vtkHandlerWidget.h Normal file
View File

@@ -0,0 +1,111 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#ifndef U_VTKHANDLERWIDGET_H
#define U_VTKHANDLERWIDGET_H
#include "Math/Dense.h"
#include "uLibVtkInterface.h"
#include <vtk3DWidget.h>
#include <vtkSmartPointer.h>
// Forward declarations of VTK classes in global namespace
class vtkActor;
class vtkCallbackCommand;
class vtkPropPicker;
class vtkTransform;
class vtkObject;
class vtkRenderWindowInteractor;
namespace uLib {
namespace Vtk {
/**
* @class vtkHandlerWidget
* @brief A Blender-like transform gizmo for move, rotate, and scale.
*/
class vtkHandlerWidget : public ::vtk3DWidget {
public:
static vtkHandlerWidget *New();
vtkTypeMacro(vtkHandlerWidget, ::vtk3DWidget);
vtkHandlerWidget();
virtual ~vtkHandlerWidget();
virtual void SetEnabled(int enabling) override;
static void ProcessEvents(::vtkObject *caller, unsigned long event,
void *clientdata, void *calldata);
void OnLeftButtonDown();
void OnLeftButtonUp();
void OnMouseMove();
enum InteractionState {
IDLE = 0,
TRANS_X,
TRANS_Y,
TRANS_Z,
ROT_X,
ROT_Y,
ROT_Z,
SCALE_X,
SCALE_Y,
SCALE_Z
};
using ::vtk3DWidget::PlaceWidget;
virtual void PlaceWidget(double bounds[6]) override;
virtual void PlaceWidget() override;
// Transform handling //
void SetTransform(::vtkTransform *t);
void GetTransform(::vtkTransform *t);
protected:
void CreateGizmos();
void UpdateGizmoPosition();
int Interaction;
// Visual components //
vtkSmartPointer<::vtkActor> m_AxesX, m_AxesY, m_AxesZ; // Arrows
vtkSmartPointer<::vtkActor> m_RotX, m_RotY, m_RotZ; // Rings
vtkSmartPointer<::vtkActor> m_ScaleX, m_ScaleY, m_ScaleZ; // Cubes
vtkSmartPointer<::vtkPropPicker> m_Picker;
double StartEventPosition[2];
vtkSmartPointer<::vtkTransform> m_InitialTransform;
private:
vtkHandlerWidget(const vtkHandlerWidget &) = delete;
void operator=(const vtkHandlerWidget &) = delete;
};
} // namespace Vtk
} // namespace uLib
#endif // U_VTKHANDLERWIDGET_H