From ba4b36e294416ef8d7858be3508757a825f147b4 Mon Sep 17 00:00:00 2001
From: Mohcine Chraibi <m.chraibi@fz-juelich.de>
Date: Fri, 7 Jun 2019 20:09:25 +0200
Subject: [PATCH] Add mapper and actors to trains struct

avoids adding these things every time step
---
 src/SaxParser.cpp     | 12 +++----
 src/TimerCallback.cpp | 74 +++++++++++++++++++++++++++----------------
 src/train.h           |  5 +++
 3 files changed, 57 insertions(+), 34 deletions(-)

diff --git a/src/SaxParser.cpp b/src/SaxParser.cpp
index a0a2ca4..0354f50 100644
--- a/src/SaxParser.cpp
+++ b/src/SaxParser.cpp
@@ -1773,23 +1773,18 @@ bool SaxParser::LoadTrainTimetable(std::string Filename, std::map<int, std::shar
      for (TiXmlElement* e = xTTT->FirstChildElement("train"); e;
                     e = e->NextSiblingElement("train")) {
           std::shared_ptr<TrainTimeTable> TTT = parseTrainTimeTableNode(e);
-          std::cout << "SI BEFORE" << trainTimeTables.size() << "\n";
           if (TTT) { // todo: maybe get pointer to train
                if (trainTimeTables.count(TTT->id)!=0) {
                     Debug::Messages("WARNING: Duplicate id for train time table found [%d]",TTT->id);
                     exit(EXIT_FAILURE);
                }
-               std::cout << "parse " << TTT->id << "\n";
                trainTimeTables[TTT->id] = TTT;
           }
           else {
-          std:cout << "HHH\n" ;
+          std:cout << "too bad! \n" ;
 
-               }
-          std::cout << "SI AFTER" << trainTimeTables.size() << "\n";
+          }
      }
-
-
      return true;
 }
 
@@ -1880,6 +1875,9 @@ std::shared_ptr<TrainTimeTable> SaxParser::parseTrainTimeTableNode(TiXmlElement
                     platform_id,
                     false,
                     false,
+                    vtkSmartPointer<vtkPolyDataMapper>::New(),
+                    vtkSmartPointer<vtkActor>::New(),
+                    vtkSmartPointer<vtkTextActor3D>::New(),
                     });
 
      return trainTimeTab;
diff --git a/src/TimerCallback.cpp b/src/TimerCallback.cpp
index b776fa6..3411448 100644
--- a/src/TimerCallback.cpp
+++ b/src/TimerCallback.cpp
@@ -106,6 +106,9 @@
 
 using namespace std;
 
+static int once=1;
+
+
 TimerCallback* TimerCallback::New()
 {
     TimerCallback *cb = new TimerCallback;
@@ -163,10 +166,17 @@ void TimerCallback::Execute(vtkObject *caller, unsigned long eventId,
 
                     double now = frameNumber*iren->GetTimerDuration(tid)/1000;
 
+                    // {
+                    //      for (auto tab: extern_trainTimeTables)
+                    //      {
+                    //           VTK_CREATE(vtkPolyDataMapper, mapper);
+                    //           VTK_CREATE(vtkActor, actor);
+                    //      }
+
+                    // }
+                    int countTrains  = 0;
                     for (auto tab: extern_trainTimeTables)
                         {
-                             VTK_CREATE(vtkPolyDataMapper, mapper);
-                             VTK_CREATE(vtkActor, actor);
                              // VTK_CREATE(vtkTextActor, textActor);
                              VTK_CREATE(vtkTextActor3D, textActor);
                              auto trainType = tab.second->type;
@@ -178,43 +188,53 @@ void TimerCallback::Execute(vtkObject *caller, unsigned long eventId,
                              auto train = extern_trainTypes[trainType];
                              auto doors = train->doors;
                              std::vector<Point> doorPoints;
+                             auto mapper = tab.second->mapper;
+                             auto actor = tab.second->actor;
+                             auto txtActor = tab.second->textActor;
                              for(auto door: doors)
                              {
                                   doorPoints.push_back(door.GetPoint1());
                                   doorPoints.push_back(door.GetPoint2());
                              }//doors
-
-                             auto data = getTrainData(trainStart, trainEnd, doorPoints);
-                             mapper->SetInputData(data);
-                             actor->SetMapper(mapper);
-                             actor->GetProperty()->SetLineWidth(10);
-                             actor->GetProperty()->SetOpacity(0.1);//feels cool!
-                             // text
-                             textActor->GetTextProperty()->SetOpacity(0.1);
-                             double pos_x = mapper->GetInput()->GetPoint(0)[0];
-                             double pos_y = mapper->GetInput()->GetPoint(0)[1];
-
-                             textActor->SetPosition (pos_x, pos_y+2, 20);
-                             textActor->SetInput (trainType.c_str());
-                             textActor->GetTextProperty()->SetFontSize (30);
-                             textActor->SetVisibility(false);
-                             if((now >= tab.second->tin) && (now <= tab.second->tout))
+                             if(once)
                              {
+                                  auto data = getTrainData(trainStart, trainEnd, doorPoints);
+                                  mapper->SetInputData(data);
+                                  actor->SetMapper(mapper);
+                                  actor->GetProperty()->SetLineWidth(10);
+                                  actor->GetProperty()->SetOpacity(0.1);//feels cool!
                                   actor->GetProperty()->SetColor(0.1,.1,0.0);
-                                  textActor->GetTextProperty()->SetColor (1.0,0.0,0.0);
-                                  textActor->SetVisibility(true);
+                                  // text
+                                  txtActor->GetTextProperty()->SetOpacity(0.7);
+                                  double pos_x = 50*(trainStart._x + trainEnd._x);
+                                  double pos_y = 50*(trainStart._y + trainEnd._y);
+
+                                  txtActor->SetPosition (pos_x, pos_y+2, 20);
+                                  txtActor->SetInput (trainType.c_str());
+                                  txtActor->GetTextProperty()->SetFontSize (30);
+                                  txtActor->GetTextProperty()->SetBold (true);
+                                  txtActor->GetTextProperty()->SetColor (0.1,0.1,0.5);
+                                  txtActor->SetVisibility(false);
+                             }
+                             if((now >= tab.second->tin) && (now <= tab.second->tout))
+                             {
+                                  actor->SetVisibility(true);
+                                  txtActor->SetVisibility(true);
                              }
                              else
                              {
-                                  actor->GetProperty()->SetColor(renderer->GetBackground());
-                                  textActor->GetTextProperty()->SetColor(renderer->GetBackground());
+                                  actor->SetVisibility(false);
+                                  txtActor->SetVisibility(false);
+                             }
+                             if(once)
+                             {
+                                  renderer->AddActor(actor);
+                                  renderer->AddActor(txtActor);
+                                  if(countTrains == extern_trainTimeTables.size())
+                                       once = 0;
                              }
-                             renderer->AddActor(actor);
-                             renderer->AddActor(textActor);
-                             mapper=nullptr;
-                             actor=nullptr;
-                             textActor=nullptr;
 
+                             countTrains++;
                         }// time table
 
 
diff --git a/src/train.h b/src/train.h
index 4f090bf..804a2f9 100644
--- a/src/train.h
+++ b/src/train.h
@@ -3,6 +3,8 @@
 
 #include "geometry/Point.h"
 #include "geometry/Transition.h"
+#include <vtkPolyDataMapper.h>
+#include <vtkTextActor3D.h>
 
 struct TrainTimeTable
 {
@@ -19,6 +21,9 @@ struct TrainTimeTable
      int pid; // Platform id
      bool arrival;
      bool departure;
+     vtkSmartPointer<vtkPolyDataMapper> mapper;
+     vtkSmartPointer<vtkActor> actor;
+     vtkSmartPointer<vtkTextActor3D> textActor;
 };
 struct TrainType
 {
-- 
GitLab