refactor program to work with a vector of layers instead

This commit is contained in:
Djairo Hougee 2024-05-05 12:05:01 +02:00
parent feec302053
commit 2f0cd19bc4
3 changed files with 71 additions and 34 deletions

View File

@ -40,39 +40,64 @@ 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() {
this->win = vtkSmartPointer<vtkRenderWindow>::New(); this->win = vtkSmartPointer<vtkRenderWindow>::New();
this->interact = vtkSmartPointer<vtkRenderWindowInteractor>::New(); this->interact = vtkSmartPointer<vtkRenderWindowInteractor>::New();
this->win->SetNumberOfLayers(3); this->win->SetNumberOfLayers(0);
this->win->AddRenderer(bg->getLayer());
this->win->AddRenderer(e->getLayer());
this->win->AddRenderer(l->getLayer());
setWinProperties(); setWinProperties();
setupTimer(); setupTimer();
} }
// Program::Program(Layer *bg, Layer *e, Layer *l) : background(bg), euler(e), lagrange(l), win(), interact() {
// this->win = vtkSmartPointer<vtkRenderWindow>::New();
// this->interact = vtkSmartPointer<vtkRenderWindowInteractor>::New();
//
// this->win->SetNumberOfLayers(3);
// this->win->AddRenderer(bg->getLayer());
// this->win->AddRenderer(e->getLayer());
// this->win->AddRenderer(l->getLayer());
// setWinProperties();
// setupTimer();
// }
void Program::setBackground(Layer *bg) { void Program::addLayer(Layer* layer) {
this->win->RemoveRenderer(this->background->getLayer()); this->layers.push_back(layer);
this->background = bg; this->win->AddRenderer(layer->getLayer());
this->win->AddRenderer(bg->getLayer()); this->win->SetNumberOfLayers(this->win->GetNumberOfLayers()+1);
}
void Program::removeLayer(Layer *layer) {
this->win->RemoveRenderer(layer->getLayer());
auto it = std::find(this->layers.begin(), this->layers.end(), layer);
if (it != this->layers.end()) {
this->layers.erase(it);
this->win->SetNumberOfLayers(this->win->GetNumberOfLayers()-1);
}
} }
void Program::setEuler(Layer *e) { // void Program::setBackground(Layer *bg) {
this->win->RemoveRenderer(this->euler->getLayer()); // this->win->RemoveRenderer(this->background->getLayer());
this->euler = e; // this->background = bg;
this->win->AddRenderer(e->getLayer()); // this->win->AddRenderer(bg->getLayer());
} //
// }
//
void Program::setLagrange(Layer *l) { //
this->win->RemoveRenderer(this->lagrange->getLayer()); // void Program::setEuler(Layer *e) {
this->lagrange = l; // this->win->RemoveRenderer(this->euler->getLayer());
this->win->AddRenderer(l->getLayer()); // this->euler = e;
} // this->win->AddRenderer(e->getLayer());
// }
//
//
// void Program::setLagrange(Layer *l) {
// this->win->RemoveRenderer(this->lagrange->getLayer());
// this->lagrange = l;
// this->win->AddRenderer(l->getLayer());
// // }
void Program::setLagrangeInteractor(SpawnPointCallback *cb){ void Program::setLagrangeInteractor(SpawnPointCallback *cb){
interact->AddObserver(vtkCommand::LeftButtonPressEvent, cb); interact->AddObserver(vtkCommand::LeftButtonPressEvent, cb);
@ -84,8 +109,11 @@ void Program::setLagrangeInteractor(SpawnPointCallback *cb){
void Program::updateData(int t) { void Program::updateData(int t) {
this->win->Render(); this->win->Render();
this->lagrange->updateData(t); for (Layer *l : layers) {
this->euler->updateData(t); l->updateData(t);
}
// this->lagrange->updateData(t);
// this->euler->updateData(t);
} }

View File

@ -10,9 +10,11 @@
class Program { class Program {
private: private:
Layer *background; // Layer *background;
Layer *euler; // Layer *euler;
Layer *lagrange; // Layer *lagrange;
std::vector<Layer *> layers;
vtkSmartPointer<vtkRenderWindow> win; vtkSmartPointer<vtkRenderWindow> win;
vtkSmartPointer<vtkRenderWindowInteractor> interact; vtkSmartPointer<vtkRenderWindowInteractor> interact;
@ -20,11 +22,15 @@ private:
void setupTimer(); void setupTimer();
public: public:
Program(Layer *bg, Layer *e, Layer *l); Program();
// Program(Layer *bg, Layer *e, Layer *l);
void setBackground(Layer *bg); void addLayer(Layer *layer);
void setEuler(Layer *e); void removeLayer(Layer *layer);
void setLagrange(Layer *l);
// void setBackground(Layer *bg);
// void setEuler(Layer *e);
// void setLagrange(Layer *l);
void setLagrangeInteractor(SpawnPointCallback *cb); void setLagrangeInteractor(SpawnPointCallback *cb);
// void addInteractionStyle(vtkInteractorStyle style); // void addInteractionStyle(vtkInteractorStyle style);

View File

@ -17,12 +17,15 @@ using namespace std;
int main() { int main() {
auto bg = new BackgroundImage("../../../../data/map_661-661.png");
auto e = new EGlyphLayer();
auto l = new LGlyphLayer(); auto l = new LGlyphLayer();
l->spoofPoints(); l->spoofPoints();
auto program = new Program(bg, e, l); Program *program = new Program();
program->addLayer(new BackgroundImage("../../../../data/map_661-661.png"));
program->addLayer(new EGlyphLayer());
program->addLayer(l);
// auto program = new Program(bg, e, l);
program->setLagrangeInteractor(l->createSpawnPointCallback()); program->setLagrangeInteractor(l->createSpawnPointCallback());
program->render(); program->render();