From da17aa5cfaa5bbf2a954b97847a823bb258bf654 Mon Sep 17 00:00:00 2001 From: djairoh Date: Fri, 3 May 2024 15:23:34 +0200 Subject: [PATCH] refactor of VtkCallbackCommand to be its' own class --- vtk/src/CMakeLists.txt | 2 ++ ...nd (conflicted copy 2024-05-03 143152).cpp | 22 +++++++++++++ ...mand (conflicted copy 2024-05-03 151842).h | 22 +++++++++++++ vtk/src/commands/TimerCallbackCommand.cpp | 30 +++++++++++++++++ vtk/src/commands/TimerCallbackCommand.h | 22 +++++++++++++ vtk/src/helperClasses/EGlyphLayer.cpp | 24 +++++++++----- vtk/src/helperClasses/EGlyphLayer.h | 3 ++ vtk/src/helperClasses/LGlyphLayer.cpp | 2 +- vtk/src/helperClasses/Program.cpp | 33 ++++++++++++++----- vtk/src/helperClasses/Program.h | 2 +- 10 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 143152).cpp create mode 100644 vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 151842).h create mode 100644 vtk/src/commands/TimerCallbackCommand.cpp create mode 100644 vtk/src/commands/TimerCallbackCommand.h diff --git a/vtk/src/CMakeLists.txt b/vtk/src/CMakeLists.txt index f4446c2..b7601ad 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 + commands/TimerCallbackCommand.h + commands/TimerCallbackCommand.cpp ) execute_process( diff --git a/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 143152).cpp b/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 143152).cpp new file mode 100644 index 0000000..2a5391d --- /dev/null +++ b/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 143152).cpp @@ -0,0 +1,22 @@ +#include "TimerCallbackCommand.h" +#include "../helperClasses/Program.h" + + +// TimerCallbackCommand::TimerCallbackCommand() : dt(3600), maxTime(3600*24*365), time(0) {} + +TimerCallbackCommand *New() { + TimerCallbackCommand *cb = new TimerCallbackCommand(); + cb->setDefaults(); + return cb; +} + +void TimerCallbackCommand::Execute(vtkObject *caller, long unsigned int eventId, void* clientData, void* callData) { + this->time += this->dt; + + if (this->time >= this->maxTime) { + return; + // TODO: how do we deal with reaching the end of the simulated dataset? Do we just stop simulating, loop back around? What about the location of the particles in this case? Just some ideas to consider, but we should iron this out pretty soon. + } + + ((Program *)clientData)->updateData(this->time); +} diff --git a/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 151842).h b/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 151842).h new file mode 100644 index 0000000..76ece9e --- /dev/null +++ b/vtk/src/commands/TimerCallbackCommand (conflicted copy 2024-05-03 151842).h @@ -0,0 +1,22 @@ +#ifndef TIMERCALLBACKCOMMAND_H +#define TIMERCALLBACKCOMMAND_H + +#include +#include "../helperClasses/Program.h" + +class TimerCallbackCommand : public vtkCallbackCommand { +public: + TimerCallbackCommand(); + static TimerCallbackCommand* New(Program *program); + void Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) override; + + void setDefaults(); + + +private: + int time; + int dt; + int maxTime; +}; + +#endif diff --git a/vtk/src/commands/TimerCallbackCommand.cpp b/vtk/src/commands/TimerCallbackCommand.cpp new file mode 100644 index 0000000..4c43b74 --- /dev/null +++ b/vtk/src/commands/TimerCallbackCommand.cpp @@ -0,0 +1,30 @@ +#include "TimerCallbackCommand.h" +#include "../helperClasses/Program.h" + + +TimerCallbackCommand::TimerCallbackCommand() : dt(3600), maxTime(3600*24*365), time(0) {} + +TimerCallbackCommand* TimerCallbackCommand::New(Program *program) { + TimerCallbackCommand *cb = new TimerCallbackCommand(); + cb->setProgram(program); + return cb; +} + +void TimerCallbackCommand::Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) { + cout << this->time << " " << this->maxTime << endl; + + this->time += this->dt; + + if (this->time >= this->maxTime) { + return; + // TODO: how do we deal with reaching the end of the simulated dataset? Do we just stop simulating, loop back around? What about the location of the particles in this case? Just some ideas to consider, but we should iron this out pretty soon. + } + + this->program->updateData(this->time); +} + + + +void TimerCallbackCommand::setProgram(Program *program) { + this->program = program; +} diff --git a/vtk/src/commands/TimerCallbackCommand.h b/vtk/src/commands/TimerCallbackCommand.h new file mode 100644 index 0000000..ff478b8 --- /dev/null +++ b/vtk/src/commands/TimerCallbackCommand.h @@ -0,0 +1,22 @@ +#ifndef TIMERCALLBACKCOMMAND_H +#define TIMERCALLBACKCOMMAND_H + +#include +#include "../helperClasses/Program.h" + +class TimerCallbackCommand : public vtkCallbackCommand { +public: + TimerCallbackCommand(); + static TimerCallbackCommand* New(Program *program); + void Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) override; + + void setProgram(Program *program); + +private: + int time; + int dt; + int maxTime; + Program *program; +}; + +#endif diff --git a/vtk/src/helperClasses/EGlyphLayer.cpp b/vtk/src/helperClasses/EGlyphLayer.cpp index 7146dc5..4e4b9c0 100644 --- a/vtk/src/helperClasses/EGlyphLayer.cpp +++ b/vtk/src/helperClasses/EGlyphLayer.cpp @@ -48,6 +48,8 @@ EGlyphLayer::EGlyphLayer() { this->ren->InteractiveOff(); this->data = vtkSmartPointer::New(); + this->direction = vtkSmartPointer::New(); + this->direction->SetName("direction"); readCoordinates(); } @@ -55,22 +57,23 @@ EGlyphLayer::EGlyphLayer() { void EGlyphLayer::readCoordinates() { vtkNew points; auto [times, lats, lons] = readGrid(); // FIXME: import Robin's readData function and use it - vtkNew direction; - direction->SetName("direction"); - direction->SetNumberOfComponents(3); - direction->SetNumberOfTuples(67*116); //FIXME: use robins function to get num of points - points->Allocate(67*116); + this->numLats = lats.size(); + this->numLons = lons.size(); + + this->direction->SetNumberOfComponents(3); + this->direction->SetNumberOfTuples(numLats*numLons); + points->Allocate(numLats*numLons); int i = 0; for (double lat : lats) { for (double lon : lons) { - direction->SetTuple3(i, 0.45, 0.90, 0); //FIXME: read this info from file; figure out how to update it dynamically + direction->SetTuple3(i, 0.45, 0.90, 0); //FIXME: read this info from file points->InsertPoint(i++, (lat*1000-46125)/25, (lon*1000+15875)/43.5, 0); // FIXME: counts on fixed window geometry to map properly; refactor to make use of active window geometry. // see also https://vtk.org/doc/nightly/html/classvtkPolyDataMapper2D.html } } this->data->SetPoints(points); - this->data->GetPointData()->AddArray(direction); + this->data->GetPointData()->AddArray(this->direction); this->data->GetPointData()->SetActiveVectors("direction"); vtkNew arrowSource; @@ -101,10 +104,13 @@ void EGlyphLayer::readCoordinates() { actor->GetProperty()->SetColor(0,0,0); actor->GetProperty()->SetOpacity(0.2); - this->ren->AddActor(actor); + this->ren->AddActor(actor) ; } void EGlyphLayer::updateData(int t) { - + for (int i=0; i < numLats*numLons; i++) { + this->direction->SetTuple3(i, std::cos(t), std::sin(t), 0); // FIXME: fetch data from file. + } + this->direction->Modified(); } diff --git a/vtk/src/helperClasses/EGlyphLayer.h b/vtk/src/helperClasses/EGlyphLayer.h index 04d2559..8631f32 100644 --- a/vtk/src/helperClasses/EGlyphLayer.h +++ b/vtk/src/helperClasses/EGlyphLayer.h @@ -10,6 +10,9 @@ class EGlyphLayer : public Layer { private: vtkSmartPointer data; + vtkSmartPointer direction; + int numLats; + int numLons; /** This private function sets up the initial coordinates for the glyphs in the dataset. * It also reads some initial data to actually display. diff --git a/vtk/src/helperClasses/LGlyphLayer.cpp b/vtk/src/helperClasses/LGlyphLayer.cpp index bec5866..c0cfbec 100644 --- a/vtk/src/helperClasses/LGlyphLayer.cpp +++ b/vtk/src/helperClasses/LGlyphLayer.cpp @@ -57,7 +57,7 @@ void LGlyphLayer::spoofPoints() { // returns new coords for a point; used to test the updateData function std::pair advect(int time, double lat, double lon) { - return {lat+0.001, lon+0.001} ; + return {lat+0.1, lon+0.1} ; } diff --git a/vtk/src/helperClasses/Program.cpp b/vtk/src/helperClasses/Program.cpp index 8f5a578..66bb818 100644 --- a/vtk/src/helperClasses/Program.cpp +++ b/vtk/src/helperClasses/Program.cpp @@ -1,9 +1,23 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include "Program.h" - +#include "../commands/TimerCallbackCommand.h" void Program::setWinProperties() { this->win->SetWindowName("Simulation"); @@ -15,15 +29,8 @@ void Program::setWinProperties() { } -void Program::CallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData) { - ((Program *)clientData)->lagrange->updateData(1); - ((Program *)clientData)->win->Render(); -} - - void Program::setupTimer() { - vtkNew callback; - callback->SetCallback(this->CallbackFunction); + auto callback = vtkSmartPointer::New(this); callback->SetClientData(this); this->interact->AddObserver(vtkCommand::TimerEvent, callback); this->interact->CreateRepeatingTimer(17); // 60 fps == 1000 / 60 == 16.7 ms per frame @@ -67,6 +74,14 @@ void Program::setLagrange(Layer *l) { // void Program::addInteractionStyle(vtkInteractorStyle style); + +void Program::updateData(int t) { + this->win->Render(); + this->lagrange->updateData(t); + this->euler->updateData(t); +} + + void Program::render() { this->win->Render(); this->interact->Start(); diff --git a/vtk/src/helperClasses/Program.h b/vtk/src/helperClasses/Program.h index 35013d5..e18f945 100644 --- a/vtk/src/helperClasses/Program.h +++ b/vtk/src/helperClasses/Program.h @@ -16,7 +16,6 @@ private: vtkSmartPointer interact; void setWinProperties(); - static void CallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData); void setupTimer(); public: @@ -27,6 +26,7 @@ public: void setLagrange(Layer *l); // void addInteractionStyle(vtkInteractorStyle style); + void updateData(int t); void render(); };