spawning points

This commit is contained in:
robin 2024-05-03 14:15:27 +02:00
parent b83f49b4ad
commit 63892ca363
9 changed files with 123 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.DS_Store .DS_Store
.idea

View File

@ -42,6 +42,8 @@ add_executable(VtkBase MACOSX_BUNDLE main.cpp
helperClasses/LGlyphLayer.h helperClasses/LGlyphLayer.h
helperClasses/Program.cpp helperClasses/Program.cpp
helperClasses/Program.h helperClasses/Program.h
helperClasses/SpawnPointCallback.h
helperClasses/SpawnPointCallback.cpp
) )
execute_process( execute_process(

View File

@ -1,4 +1,5 @@
#include "LGlyphLayer.h" #include "LGlyphLayer.h"
#include "SpawnPointCallback.h"
#include <vtkActor2D.h> #include <vtkActor2D.h>
#include <vtkGlyph2D.h> #include <vtkGlyph2D.h>
#include <vtkGlyphSource2D.h> #include <vtkGlyphSource2D.h>
@ -6,6 +7,17 @@
#include <vtkPolyDataMapper2D.h> #include <vtkPolyDataMapper2D.h>
#include <vtkProperty2D.h> #include <vtkProperty2D.h>
#include <vtkVertexGlyphFilter.h> #include <vtkVertexGlyphFilter.h>
#include <vtkInteractorStyle.h>
#include <vtkInteractorStyleUser.h>
vtkSmartPointer<SpawnPointCallback> LGlyphLayer::createSpawnPointCallback() {
auto newPointCallBack = vtkSmartPointer<SpawnPointCallback>::New();
newPointCallBack->setData(data);
newPointCallBack->setPoints(points);
return newPointCallBack;
}
// TODO: add interactionStyle functionality // TODO: add interactionStyle functionality
// TODO: add timer + advection (probably from the program class not here) // TODO: add timer + advection (probably from the program class not here)

View File

@ -2,7 +2,9 @@
#define LGLYPHLAYER_H #define LGLYPHLAYER_H
#include "Layer.h" #include "Layer.h"
#include "SpawnPointCallback.h"
#include <vtkPolyData.h> #include <vtkPolyData.h>
#include <vtkInteractorStyle.h>
/** Implements the Layer class for the case of a Lagrangian visualization. /** 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. * 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<vtkPolyData> data; vtkSmartPointer<vtkPolyData> data;
public: public:
/** Constructor. /** Constructor.
*/ */
@ -27,6 +30,7 @@ public:
*/ */
void updateData(int t) override; void updateData(int t) override;
vtkSmartPointer<SpawnPointCallback> createSpawnPointCallback();
}; };
#endif #endif

View File

@ -1,8 +1,10 @@
#include <vtkRenderWindow.h> #include <vtkRenderWindow.h>
#include <vtkNew.h> #include <vtkNew.h>
#include <vtkCallbackCommand.h> #include <vtkCallbackCommand.h>
#include <vtkInteractorStyleUser.h>
#include "Program.h" #include "Program.h"
#include "SpawnPointCallback.h"
void Program::setWinProperties() { void Program::setWinProperties() {
@ -13,6 +15,8 @@ void Program::setWinProperties() {
this->interact->SetRenderWindow(this->win); this->interact->SetRenderWindow(this->win);
this->interact->Initialize(); this->interact->Initialize();
vtkNew<vtkInteractorStyleUser> style;
interact->SetInteractorStyle(style);
} }
void Program::CallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData) { 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(); ((Program *)clientData)->win->Render();
} }
void Program::setupTimer() { void Program::setupTimer() {
vtkNew<vtkCallbackCommand> callback; vtkNew<vtkCallbackCommand> callback;
callback->SetCallback(this->CallbackFunction); callback->SetCallback(this->CallbackFunction);
@ -29,7 +32,6 @@ void Program::setupTimer() {
this->interact->CreateRepeatingTimer(17); // 60 fps == 1000 / 60 == 16.7 ms per frame 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() { Program::Program(Layer *bg, Layer *e, Layer *l) : background(bg), euler(e), lagrange(l), win(), interact() {
this->win = vtkSmartPointer<vtkRenderWindow>::New(); this->win = vtkSmartPointer<vtkRenderWindow>::New();
this->interact = vtkSmartPointer<vtkRenderWindowInteractor>::New(); this->interact = vtkSmartPointer<vtkRenderWindowInteractor>::New();
@ -64,7 +66,11 @@ void Program::setLagrange(Layer *l) {
this->win->AddRenderer(l->getLayer()); 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() { void Program::render() {

View File

@ -6,6 +6,7 @@
#include <vtkRenderer.h> #include <vtkRenderer.h>
#include "Layer.h" #include "Layer.h"
#include "SpawnPointCallback.h"
class Program { class Program {
private: private:
@ -25,8 +26,9 @@ public:
void setBackground(Layer *bg); void setBackground(Layer *bg);
void setEuler(Layer *e); void setEuler(Layer *e);
void setLagrange(Layer *l); void setLagrange(Layer *l);
void setLagrangeInteractor(SpawnPointCallback *cb);
// void addInteractionStyle(vtkInteractorStyle style); // void addObserver(vtkInteractorStyle *style);
void render(); void render();
}; };

View File

@ -0,0 +1,60 @@
#include "SpawnPointCallback.h"
#include <vtkVertex.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkCommand.h>
#include <vtkRenderWindow.h>
void convertDisplayToWorld(vtkRenderer* renderer, int x, int y, double *worldPos) {
double displayPos[3] = {static_cast<double>(x), static_cast<double>(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<vtkRenderWindowInteractor *>(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<vtkVertex> vertex = vtkSmartPointer<vtkVertex>::New();
vertex->GetPointIds()->SetId(0, id);
vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::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<vtkPolyData> &data) {
this->data = data;
}
void SpawnPointCallback::setPoints(const vtkSmartPointer<vtkPoints> &points) {
this->points = points;
}

View File

@ -0,0 +1,31 @@
#ifndef VTKBASE_SPAWNPOINTCALLBACK_H
#define VTKBASE_SPAWNPOINTCALLBACK_H
#include <vtkCallbackCommand.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
class SpawnPointCallback : public vtkCallbackCommand {
public:
static SpawnPointCallback *New();
SpawnPointCallback();
void setPoints(const vtkSmartPointer<vtkPoints> &points);
void setData(const vtkSmartPointer<vtkPolyData> &data);
private:
vtkSmartPointer<vtkPolyData> data;
vtkSmartPointer<vtkPoints> points;
public:
private:
void Execute(vtkObject *caller, unsigned long evId, void *callData) override;
bool dragging = false;
};
#endif //VTKBASE_SPAWNPOINTCALLBACK_H

View File

@ -23,6 +23,7 @@ int main() {
l->spoofPoints(); l->spoofPoints();
auto program = new Program(bg, e, l); auto program = new Program(bg, e, l);
program->setLagrangeInteractor(l->createSpawnPointCallback());
program->render(); program->render();
return EXIT_SUCCESS; return EXIT_SUCCESS;