diff --git a/.gitignore b/.gitignore index 5ca0973..4befed3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .DS_Store - +.idea diff --git a/vtk/src/CMakeLists.txt b/vtk/src/CMakeLists.txt index f4446c2..97f6e2e 100644 --- a/vtk/src/CMakeLists.txt +++ b/vtk/src/CMakeLists.txt @@ -42,6 +42,8 @@ add_executable(VtkBase MACOSX_BUNDLE main.cpp helperClasses/LGlyphLayer.h helperClasses/Program.cpp helperClasses/Program.h + helperClasses/SpawnPointCallback.h + helperClasses/SpawnPointCallback.cpp ) execute_process( diff --git a/vtk/src/helperClasses/LGlyphLayer.cpp b/vtk/src/helperClasses/LGlyphLayer.cpp index bec5866..005439d 100644 --- a/vtk/src/helperClasses/LGlyphLayer.cpp +++ b/vtk/src/helperClasses/LGlyphLayer.cpp @@ -1,4 +1,5 @@ #include "LGlyphLayer.h" +#include "SpawnPointCallback.h" #include #include #include @@ -6,6 +7,17 @@ #include #include #include +#include +#include + + +vtkSmartPointer LGlyphLayer::createSpawnPointCallback() { + auto newPointCallBack = vtkSmartPointer::New(); + newPointCallBack->setData(data); + newPointCallBack->setPoints(points); + return newPointCallBack; +} + // TODO: add interactionStyle functionality // TODO: add timer + advection (probably from the program class not here) diff --git a/vtk/src/helperClasses/LGlyphLayer.h b/vtk/src/helperClasses/LGlyphLayer.h index e13257c..edc751d 100644 --- a/vtk/src/helperClasses/LGlyphLayer.h +++ b/vtk/src/helperClasses/LGlyphLayer.h @@ -2,7 +2,9 @@ #define LGLYPHLAYER_H #include "Layer.h" +#include "SpawnPointCallback.h" #include +#include /** Implements the Layer class for the case of a Lagrangian visualization. * Specifically, this class models the Lagrangian particles in the simulation using the 'glyph' mark and 'transparency' channel to denote age. @@ -13,6 +15,7 @@ private: vtkSmartPointer data; + public: /** Constructor. */ @@ -27,6 +30,7 @@ public: */ void updateData(int t) override; + vtkSmartPointer createSpawnPointCallback(); }; #endif diff --git a/vtk/src/helperClasses/Program.cpp b/vtk/src/helperClasses/Program.cpp index 8f5a578..1451cee 100644 --- a/vtk/src/helperClasses/Program.cpp +++ b/vtk/src/helperClasses/Program.cpp @@ -1,8 +1,10 @@ #include #include #include +#include #include "Program.h" +#include "SpawnPointCallback.h" void Program::setWinProperties() { @@ -13,6 +15,8 @@ void Program::setWinProperties() { this->interact->SetRenderWindow(this->win); this->interact->Initialize(); + vtkNew style; + interact->SetInteractorStyle(style); } void Program::CallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData) { @@ -20,7 +24,6 @@ void Program::CallbackFunction(vtkObject* caller, long unsigned int eventId, voi ((Program *)clientData)->win->Render(); } - void Program::setupTimer() { vtkNew callback; callback->SetCallback(this->CallbackFunction); @@ -29,7 +32,6 @@ void Program::setupTimer() { this->interact->CreateRepeatingTimer(17); // 60 fps == 1000 / 60 == 16.7 ms per frame } - Program::Program(Layer *bg, Layer *e, Layer *l) : background(bg), euler(e), lagrange(l), win(), interact() { this->win = vtkSmartPointer::New(); this->interact = vtkSmartPointer::New(); @@ -64,7 +66,11 @@ void Program::setLagrange(Layer *l) { this->win->AddRenderer(l->getLayer()); } -// void Program::addInteractionStyle(vtkInteractorStyle style); +void Program::setLagrangeInteractor(SpawnPointCallback *cb){ + interact->AddObserver(vtkCommand::LeftButtonPressEvent, cb); + interact->AddObserver(vtkCommand::LeftButtonReleaseEvent, cb); + interact->AddObserver(vtkCommand::MouseMoveEvent, cb); +} void Program::render() { diff --git a/vtk/src/helperClasses/Program.h b/vtk/src/helperClasses/Program.h index 35013d5..002c977 100644 --- a/vtk/src/helperClasses/Program.h +++ b/vtk/src/helperClasses/Program.h @@ -6,6 +6,7 @@ #include #include "Layer.h" +#include "SpawnPointCallback.h" class Program { private: @@ -25,8 +26,9 @@ public: void setBackground(Layer *bg); void setEuler(Layer *e); void setLagrange(Layer *l); + void setLagrangeInteractor(SpawnPointCallback *cb); - // void addInteractionStyle(vtkInteractorStyle style); +// void addObserver(vtkInteractorStyle *style); void render(); }; diff --git a/vtk/src/helperClasses/SpawnPointCallback.cpp b/vtk/src/helperClasses/SpawnPointCallback.cpp new file mode 100644 index 0000000..14dbc49 --- /dev/null +++ b/vtk/src/helperClasses/SpawnPointCallback.cpp @@ -0,0 +1,60 @@ +#include "SpawnPointCallback.h" + +#include +#include +#include +#include +#include +#include + +void convertDisplayToWorld(vtkRenderer* renderer, int x, int y, double *worldPos) { + double displayPos[3] = {static_cast(x), static_cast(y), 0.0}; + renderer->SetDisplayPoint(displayPos); + renderer->DisplayToWorld(); + renderer->GetWorldPoint(worldPos); +} + +void SpawnPointCallback::Execute(vtkObject *caller, unsigned long evId, void *callData) { + // Note the use of reinterpret_cast to cast the caller to the expected type. + auto interactor = reinterpret_cast(caller); + + if (evId == vtkCommand::LeftButtonPressEvent) { + dragging = true; + } + if (evId == vtkCommand::LeftButtonReleaseEvent) { + dragging = false; + } + if (!dragging) { + return; + } + + int x, y; + interactor->GetEventPosition(x, y); + + vtkIdType id = points->InsertNextPoint(x, y, 0); + data->SetPoints(points); + + vtkSmartPointer vertex = vtkSmartPointer::New(); + vertex->GetPointIds()->SetId(0, id); + + vtkSmartPointer vertices = vtkSmartPointer::New(); + vertices->InsertNextCell(vertex); + data->SetVerts(vertices); +// data->Modified(); // These might be needed im not sure. +// ren->GetRenderWindow()->Render(); +} + + +SpawnPointCallback::SpawnPointCallback() : data(nullptr), points(nullptr) {} + +SpawnPointCallback *SpawnPointCallback::New() { + return new SpawnPointCallback; +} + +void SpawnPointCallback::setData(const vtkSmartPointer &data) { + this->data = data; +} + +void SpawnPointCallback::setPoints(const vtkSmartPointer &points) { + this->points = points; +} \ No newline at end of file diff --git a/vtk/src/helperClasses/SpawnPointCallback.h b/vtk/src/helperClasses/SpawnPointCallback.h new file mode 100644 index 0000000..99ab160 --- /dev/null +++ b/vtk/src/helperClasses/SpawnPointCallback.h @@ -0,0 +1,31 @@ +#ifndef VTKBASE_SPAWNPOINTCALLBACK_H +#define VTKBASE_SPAWNPOINTCALLBACK_H + + +#include +#include +#include +#include + +class SpawnPointCallback : public vtkCallbackCommand { + +public: + static SpawnPointCallback *New(); + SpawnPointCallback(); + + void setPoints(const vtkSmartPointer &points); + + void setData(const vtkSmartPointer &data); + +private: + vtkSmartPointer data; + vtkSmartPointer points; +public: + +private: + void Execute(vtkObject *caller, unsigned long evId, void *callData) override; + bool dragging = false; +}; + + +#endif //VTKBASE_SPAWNPOINTCALLBACK_H diff --git a/vtk/src/main.cpp b/vtk/src/main.cpp index d5aa31d..78b1a3a 100644 --- a/vtk/src/main.cpp +++ b/vtk/src/main.cpp @@ -23,6 +23,7 @@ int main() { l->spoofPoints(); auto program = new Program(bg, e, l); + program->setLagrangeInteractor(l->createSpawnPointCallback()); program->render(); return EXIT_SUCCESS;