ThreadVisualisation.cpp 27.8 KB
Newer Older
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * @file ThreadDataTransfert.cpp
 * @author   Ulrich Kemloh <kemlohulrich@gmail.com>
 * @version 0.1
 * Copyright (C) <2009-2010>
 *
 * @section LICENSE
 * This file is part of OpenPedSim.
 *
 * OpenPedSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * OpenPedSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenPedSim. If not, see <http://www.gnu.org/licenses/>.
 *
 * @section DESCRIPTION
 * This class implements the thread that manages the main visulisation
 *
 * \brief visualise the data
 *
 *
 *
 *  Created on: 22.05.2009
 *
 */


#include <QThread>
#include <QString>
#include <QObject>
#include <QMessageBox>

Mohcine Chraibi's avatar
Mohcine Chraibi committed
40 41
// we don't need this when compiling with cmake
// http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Factories_now_require_defines
42
// all modules need to be initialized in vtk6
Mohcine Chraibi's avatar
Mohcine Chraibi committed
43 44
//#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingFreeTypeOpenGL,vtkRenderingOpenGL)
//#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
45

46

Ulrich Kemloh's avatar
Ulrich Kemloh committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkAssembly.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkAxisActor.h>
#include <vtkAxesActor.h>
#include <vtkTextActor.h>
#include <vtkLight.h>
#include <vtkScalarBarActor.h>
#include <vtkFloatArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkLookupTable.h>
#include <vtkTextProperty.h>
#include <vtkProperty.h>
64 65
#include <vtkOutputWindow.h>
#include <vtkFileOutputWindow.h>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
66 67 68 69
#include <vtkCallbackCommand.h>
#include <vtkSmartPointer.h>
#include <vtkActor2DCollection.h>
#include <vtkRegularPolygonSource.h>
70 71
#include <vtkLabeledDataMapper.h>
#include <vtkDiskSource.h>
72 73
#include <vtkTriangleFilter.h>
#include <vtkStripper.h>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
74 75 76 77
#include <vtkSphereSource.h>
#include <vtkCylinderSource.h>
#include <vtkWindowToImageFilter.h>
#include <vtkActor.h>
78
#include <vtkLightKit.h>
79
#include <vtkPolyLine.h>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
80 81

#include "geometry/FacilityGeometry.h"
82
#include "geometry/GeometryFactory.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
83 84 85 86 87 88 89 90 91 92
#include "geometry/LinePlotter2D.h"

#include "ThreadVisualisation.h"
#include "Pedestrian.h"
#include "TimerCallback.h"
#include "Frame.h"
#include "TrajectoryPoint.h"
#include "SyncData.h"
#include "InteractorStyle.h"
#include "SystemSettings.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
93
#include "geometry/PointPlotter.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
94 95 96 97
#include "Debug.h"


#define VTK_CREATE(type, name) \
98
    vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
Ulrich Kemloh's avatar
Ulrich Kemloh committed
99

100

Ulrich Kemloh's avatar
Ulrich Kemloh committed
101
ThreadVisualisation::ThreadVisualisation(QObject *parent):
Ulrich Kemloh's avatar
Ulrich Kemloh committed
102
    QThread(parent)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
103
{
104 105 106 107 108 109 110 111
    _renderer=NULL;
    _renderWindow=NULL;
    _renderWinInteractor=NULL;
    _runningTime=vtkTextActor::New();;
    _framePerSecond=25;
    _axis=NULL;
    _winTitle="header without room caption";
    //_geometry=new FacilityGeometry();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
112 113
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
114 115 116
ThreadVisualisation::~ThreadVisualisation()
{

117 118
    if(_axis)
        _axis->Delete();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
119

Ulrich Kemloh's avatar
Ulrich Kemloh committed
120 121 122 123 124
    if(extern_glyphs_pedestrians_3D) extern_glyphs_pedestrians_3D->Delete();
    if(extern_glyphs_pedestrians_actor_3D) extern_glyphs_pedestrians_actor_3D->Delete();
    if(extern_glyphs_pedestrians) extern_glyphs_pedestrians->Delete();
    if(extern_glyphs_pedestrians_actor_2D) extern_glyphs_pedestrians_actor_2D->Delete();
    if(extern_pedestrians_labels) extern_pedestrians_labels->Delete();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
125

126
    _runningTime->Delete();
127

Ulrich Kemloh's avatar
Ulrich Kemloh committed
128 129
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
130 131
void ThreadVisualisation::setFullsreen(bool status)
{
132
    _renderWindow->SetFullScreen(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
133 134
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
135 136
void ThreadVisualisation::slotSetFrameRate(float fps)
{
137
    _framePerSecond=fps;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
138 139 140
}


Ulrich Kemloh's avatar
Ulrich Kemloh committed
141 142
void ThreadVisualisation::run()
{
143
    //deactivate the output windows
144
    vtkObject::GlobalWarningDisplayOff();
145

Ulrich Kemloh's avatar
Ulrich Kemloh committed
146
    //emit signalStatusMessage("running");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
147

148

Ulrich Kemloh's avatar
Ulrich Kemloh committed
149
    // Create the renderer
150
    _renderer = vtkRenderer::New();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
151 152
    // set the background
    //renderer->SetBackground(.00,.00,.00);
153
    _renderer->SetBackground(1.0,1.0,1.0);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
154
    //add the geometry
155 156 157 158
    _geometry.Init(_renderer);
    //_geometry->CreateActors();
    //_renderer->AddActor(_geometry->getActor2D());
    //_renderer->AddActor(_geometry->getActor3D());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
159

160
    initGlyphs2D();
161
    initGlyphs3D();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
162

Ulrich Kemloh's avatar
Ulrich Kemloh committed
163 164
    //create the trails
    extern_trail_plotter = new PointPlotter();
165
    _renderer->AddActor(extern_trail_plotter->getActor());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
166

Ulrich Kemloh's avatar
Ulrich Kemloh committed
167
    // add axis
Ulrich Kemloh's avatar
Ulrich Kemloh committed
168
    //axis= vtkAxesActor::New();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
169 170 171 172 173
    //axis->SetScale(10);
    //axis->SetConeRadius(30);
    //axis->SetXAxisLabelText("1 Meter");
    //axis->SetYAxisLabelText("1 Meter");
    //axis->SetZAxisLabelText("1 Meter");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
174
    //axis->AxisLabelsOff();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
175 176
    //axis->SetCylinderRadius(100);
    //axis->set
Ulrich Kemloh's avatar
Ulrich Kemloh committed
177 178
    //axis->SetTotalLength(1000,1000,1000);
    //axis->SetVisibility(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
179 180 181
    //renderer->AddActor(axis);

    //add big circle at null point
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    // {
    //      vtkSphereSource* org = vtkSphereSource::New();
    //     org->SetRadius(10);
    //     vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
    //     mapper->SetInputConnection(org->GetOutputPort());
    //     org->Delete();
    //     //------
    //     // create actor
    //     vtkActor* actor = vtkActor::New();
    //     actor->SetMapper(mapper);
    //     mapper->Delete();
    //     actor->GetProperty()->SetColor(.1,.10,0.0);
    //     _renderer->AddActor(actor);
    //     actor->Delete();
    //      mysphere;
    //     //
    // }
    // {
    //     vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
    //     mapper->SetInputConnection(extern_mysphere->GetOutputPort());
    //     //------
    //     // create actor
    //     vtkActor* actor = vtkActor::New();
    //     actor->SetMapper(mapper);
    //     mapper->Delete();
    //     actor->GetProperty()->SetColor(.1,.10,0.0);
    //     _renderer->AddActor(actor);
    //     actor->Delete();
    //     //
    // }

    // {
    //      // Train
    //      double p0[3] = {1.0*100, 4.0*100, 0.0};
    //      double p1[3] = {3.0*100, 4.0*100, 0.0};
    //      double p2[3] = {3.0*100, 5.0*100, 0.0};
    //      double p3[3] = {1.0*100, 5.0*100, 0.0};
    //      double p4[3] = {1.0*100, 4.0*100, 0.0};
    //      // Create a vtkPoints object and store the points in it
    //      VTK_CREATE(vtkPoints, points);

    //      points->InsertNextPoint(p0);
    //      points->InsertNextPoint(p1);
    //      points->InsertNextPoint(p2);
    //      points->InsertNextPoint(p3);
    //      points->InsertNextPoint(p4);

    //      VTK_CREATE(vtkPolyLine, polyLine);

    //      polyLine->GetPointIds()->SetNumberOfIds(5);
    //      for(unsigned int i = 0; i < 5; i++)
    //      {
    //           polyLine->GetPointIds()->SetId(i,i);
    //      }

    //      // Create a cell array to store the lines in and add the lines to it
    //      VTK_CREATE(vtkCellArray, cells);
    //      cells->InsertNextCell(polyLine);

    //      // Create a polydata to store everything in
    //      VTK_CREATE(vtkPolyData, polyData);

    //      // Add the points to the dataset
    //      polyData->SetPoints(points);

    //      // Add the lines to the dataset
    //      polyData->SetLines(cells);

    //      VTK_CREATE(vtkPolyDataMapper, mapper);
    //      mapper->SetInputData(polyData);


    //      VTK_CREATE(vtkActor, actor);
    //      actor->SetMapper(mapper);
    //      actor->GetProperty()->SetColor(.1,.10,0.0);
    //     _renderer->AddActor(actor);
    // }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
259 260

    // Create the render window
261 262 263
    _renderWindow = vtkRenderWindow::New();
    _renderWindow->AddRenderer( _renderer );
    _renderWindow->SetSize(960, 800);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
264 265 266
    //renderWindow->SetSize(640, 480);

    // add the legend
267 268
    //if(SystemSettings::getShowLegend())
    //	initLegend();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
269

Ulrich Kemloh's avatar
Ulrich Kemloh committed
270
    //add the running time frame
271
    _runningTime->SetTextScaleModeToViewport();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
272 273 274 275 276
    //runningTime->SetTextScaleModeToProp();
    //runningTime->SetMinimumSize(10,10);
    //runningTime->SetMinimumSize(180,80);
    //runningTime->SetInput(txt);
    //runningTime->SetDisplayPosition(500,700);
277
    _runningTime->SetVisibility(SystemSettings::getOnScreenInfos());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
278 279

    // set the properties of the caption
280
    vtkTextProperty* tprop = _runningTime->GetTextProperty();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
281 282 283 284 285 286
    //tprop->SetFontFamilyToArial();
    //tprop->BoldOn();
    //tprop->SetLineSpacing(1.0);
    tprop->SetFontSize(10);
    tprop->SetColor(1.0,0.0,0.0);

287
    _renderer->AddActor2D(_runningTime);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
288 289 290

    //CAUTION: this is necessary for WIN32 to update the window name
    // but his will freeze your system on linux
291
#if defined(_WIN32) || defined(__APPLE__)
292
    _renderWindow->Render();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
293 294
#endif

295
    _renderWindow->SetWindowName(_winTitle.toStdString().c_str());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
296

Ulrich Kemloh's avatar
Ulrich Kemloh committed
297
    // Create an interactor
298 299 300
    _renderWinInteractor = vtkRenderWindowInteractor::New();
    _renderWindow->SetInteractor( _renderWinInteractor );
    _renderWinInteractor->Initialize();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
301

302
    //add a light kit
303
    {
304
        //VTK_CREATE(vtkLightKit, lightKit);
305 306 307 308 309 310 311 312
        //lightKit->SetKeyLightIntensity(1);

        //lightKit->SetKeyLightWarmth(5);
        //lightKit->SetBackLightAngle(30,40);
        //lightKit->SetKeyLightWarmth(2.0);
        //lightKit->SetMaintainLuminance(true);
        //lightKit->AddLightsToRenderer(renderer);
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
313 314


315
    if(SystemSettings::get2D()) {
316 317 318
        _renderer->GetActiveCamera()->OrthogonalizeViewUp();
        _renderer->GetActiveCamera()->ParallelProjectionOn();
        _renderer->ResetCamera();
319
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
320

Ulrich Kemloh's avatar
Ulrich Kemloh committed
321 322
    //create a timer for rendering the window
    TimerCallback *renderingTimer = new TimerCallback();
323
    int timer= _renderWinInteractor->CreateRepeatingTimer(1000.0/_framePerSecond);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
324
    renderingTimer->SetRenderTimerId(timer);
325 326
    renderingTimer->setTextActor(_runningTime);
    _renderWinInteractor->AddObserver(vtkCommand::TimerEvent,renderingTimer);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
327

328 329 330 331




Ulrich Kemloh's avatar
Ulrich Kemloh committed
332 333 334
    //create the necessary connections
    QObject::connect(renderingTimer, SIGNAL(signalRunningTime(unsigned long )),
                     this->parent(), SLOT(slotRunningTime(unsigned long )));
Ulrich Kemloh's avatar
Ulrich Kemloh committed
335

336 337
    QObject::connect(renderingTimer, SIGNAL(signalFrameNumber(unsigned long, unsigned long )),
                     this->parent(), SLOT(slotFrameNumber(unsigned long, unsigned long )));
Ulrich Kemloh's avatar
Ulrich Kemloh committed
338

Ulrich Kemloh's avatar
Ulrich Kemloh committed
339 340
    QObject::connect(renderingTimer, SIGNAL(signalRenderingTime(int)),
                     this->parent(), SLOT(slotRenderingTime(int)));
Mohcine Chraibi's avatar
Mohcine Chraibi committed
341

Ulrich Kemloh's avatar
Ulrich Kemloh committed
342 343
    // Create my interactor style
    InteractorStyle* style = InteractorStyle::New();
344
    _renderWinInteractor->SetInteractorStyle( style );
Ulrich Kemloh's avatar
Ulrich Kemloh committed
345
    style->Delete();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
346 347


Ulrich Kemloh's avatar
Ulrich Kemloh committed
348 349 350
    //create special camera for the virtual reality feeling
    //renderer->GetActiveCamera()->SetRoll(90);
    //renderer->GetRenderers()->GetFirstRenderer()->GetActiveCamera();
351 352 353

    //Pedestrian::setCamera(renderer->GetActiveCamera());
    //renderer->ResetCamera();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
354

Ulrich Kemloh's avatar
Ulrich Kemloh committed
355 356 357
    //renderWinInteractor->Initialize();
    // Initialize and enter interactive mode
    // should be called after the observer has been added
Ulrich Kemloh's avatar
Ulrich Kemloh committed
358 359
    //renderWindow->Modified();

360 361
    //style->SetKeyPressActivationValue('R');
    //style->SetKeyPressActivation(true);
362
    //renderWindow->SetCurrentCursor(VTK_CURSOR_CROSSHAIR);
363

364 365 366
    //save the top view  camera
    _topViewCamera=vtkCamera::New();
    //renderer->GetActiveCamera()->Modified();
367
    _topViewCamera->DeepCopy(_renderer->GetActiveCamera());
368

369
    //update all (restored) system settings
370 371
    setGeometryVisibility2D(SystemSettings::get2D());
    setGeometryVisibility3D(!SystemSettings::get2D());
372 373 374
    setGeometryVisibility(SystemSettings::getShowGeometry());
    setOnscreenInformationVisibility(SystemSettings::getOnScreenInfos());
    showFloor(SystemSettings::getShowFloor());
375 376 377 378
    showWalls(SystemSettings::getShowWalls());
    showObstacle(SystemSettings::getShowObstacles());
    showDoors(SystemSettings::getShowExits());
    showNavLines(SystemSettings::getShowNavLines());
379 380 381
    setGeometryLabelsVisibility(SystemSettings::getShowGeometryCaptions());
    setBackgroundColor(SystemSettings::getBackgroundColor());
    setWallsColor(SystemSettings::getWallsColor());
382
    setObstacleColor(SystemSettings::getObstacleColor());
383 384 385
    setFloorColor(SystemSettings::getFloorColor());
    setExitsColor(SystemSettings::getExitsColor());
    setNavLinesColor(SystemSettings::getNavLinesColor());
386 387
    //FIXME:
    showGradientField(SystemSettings::getShowGradientField());
388
    _renderWinInteractor->Start();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
389

390 391

#ifdef __APPLE__
392
    //InitMultiThreading();
393

394 395 396 397
    //dispatch_async(main_q, ^(void){
    //          is_main_thread(); //Unfortunately not
    //          std::cout << "now spinning the visualizer" << std::endl;
    //                  renderWinInteractor->Start();
398

399
    //});
400 401 402 403 404
    //[[NSThread new] start];
    //#include <objc/objc.h>
    //NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(workerThreadFunction:) object:nil];
    //[myThread start];
#endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
405
    //emit signalStatusMessage("Idle");
406
    emit signal_controlSequences("CONTROL_RESET");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
407 408


409
    // still debugging. TODO, check the source of the leak while using cocoa
Ulrich Kemloh's avatar
Ulrich Kemloh committed
410
#ifndef __APPLE__
Ulrich Kemloh's avatar
Ulrich Kemloh committed
411
    //clear some stuffs
412
    //delete extern_trail_plotter;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
413
    finalize();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
414

415 416 417
    _renderer->Delete();
    _renderWindow->Delete();
    _renderWinInteractor->Delete();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
418
    _topViewCamera->Delete();
419
    _renderer=NULL;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
420 421
    delete renderingTimer;
#endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
422

Ulrich Kemloh's avatar
Ulrich Kemloh committed
423 424 425
}


Ulrich Kemloh's avatar
Ulrich Kemloh committed
426 427
void ThreadVisualisation::slotControlSequence(const char* para)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
428

Ulrich Kemloh's avatar
Ulrich Kemloh committed
429
    //cout <<"control sequence received: " <<sex<<endl;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
430 431
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
432 433
void ThreadVisualisation::setGeometryVisibility( bool status)
{
434 435 436 437 438 439

    if(SystemSettings::get2D())
    {
        _geometry.Set2D(status);
    } else
    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
440
       _geometry.Set3D(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
441
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
442 443 444
}

/// show / hide the walls
Ulrich Kemloh's avatar
Ulrich Kemloh committed
445 446
void ThreadVisualisation::showWalls(bool status)
{
447
    _geometry.ShowWalls(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
448 449 450
}

/// show/ hide the exits
Ulrich Kemloh's avatar
Ulrich Kemloh committed
451 452
void ThreadVisualisation::showDoors(bool status)
{
453
    _geometry.ShowDoors(status);
454 455 456 457
}

void ThreadVisualisation::showNavLines(bool status)
{
458
    _geometry.ShowNavLines(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
459 460
}

461 462
void ThreadVisualisation::showFloor(bool status)
{
463
    _geometry.ShowFloor(status);
464
}
465

466 467 468 469
void ThreadVisualisation::showObstacle(bool status)
{
    _geometry.ShowObstacles(status);
}
470 471 472 473
void ThreadVisualisation::showGradientField(bool status)
{
    _geometry.ShowGradientField(status);
}
474 475
void  ThreadVisualisation::initGlyphs2D()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
476 477 478
    if(extern_glyphs_pedestrians) extern_glyphs_pedestrians->Delete();
    if(extern_glyphs_pedestrians_actor_2D) extern_glyphs_pedestrians_actor_2D->Delete();
    if(extern_pedestrians_labels) extern_pedestrians_labels->Delete();
479

480
    extern_glyphs_pedestrians = vtkTensorGlyph::New();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
481 482
    extern_glyphs_pedestrians_actor_2D = vtkActor::New();
    extern_pedestrians_labels =  vtkActor2D::New();
483

484
    //glyphs with ellipsoids
485 486 487 488
    //    VTK_CREATE (vtkSphereSource, agentShape);
    //    agentShape->SetRadius(30);
    //    agentShape->SetPhiResolution(20);
    //    agentShape->SetThetaResolution(20);
489 490

    //now create the glyphs with ellipses
491 492 493 494
    VTK_CREATE (vtkDiskSource, agentShape);
    agentShape->SetCircumferentialResolution(20);
    agentShape->SetInnerRadius(0);
    agentShape->SetOuterRadius(30);
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    //    {
    //        //personal space
    //        VTK_CREATE (vtkDiskSource, perSpace);
    //        perSpace->SetCircumferentialResolution(20);
    //        perSpace->SetInnerRadius(0);
    //        perSpace->SetOuterRadius(30);
    //        //forehead
    //        perSpace->SetCircumferentialResolution(20);
    //        perSpace->SetInnerRadius(0);
    //        perSpace->SetOuterRadius(30);
    //        //backhead

    //        //Append the two meshes
    //        VTK_CREATE (vtkAppendPolyData, appendFilter);
    //#if VTK_MAJOR_VERSION <= 5
    //        appendFilter->AddInputConnection(perSpace->GetProducerPort());
    //        appendFilter->AddInputConnection(input2->GetProducerPort());
    //#else
    //        appendFilter->AddInputData(perSpace);
    //        appendFilter->AddInputData(input2);
    //#endif
    //        appendFilter->Update();

    //        // Remove any duplicate points.
    //        VTK_CREATE (vtkCleanPolyData, cleanFilter);
    //        cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
    //        cleanFilter->Update();
    //    }
524

525
    //speed the rendering using triangles stripers
Ulrich Kemloh's avatar
Ulrich Kemloh committed
526 527
    //vtkTriangleFilter *tris = vtkTriangleFilter::New();
    VTK_CREATE(vtkTriangleFilter, tris);
528 529 530
    tris->SetInputConnection(agentShape->GetOutputPort());
    //tris->GetOutput()->ReleaseData();

Ulrich Kemloh's avatar
Ulrich Kemloh committed
531 532
    //vtkStripper *strip = vtkStripper::New();
    VTK_CREATE(vtkStripper, strip);
533 534 535 536
    strip->SetInputConnection(tris->GetOutputPort());
    //strip->GetOutput()->ReleaseData();

    extern_glyphs_pedestrians->SetSourceConnection(strip->GetOutputPort());
537
    //extern_glyphs_pedestrians->SetSourceConnection(agentShape->GetOutputPort());
538

539
    //first frame
540 541 542 543
    auto&& frames=extern_trajectories_firstSet.GetFrames();
    if(frames.empty()) return;

    Frame * frame = frames.begin()->second;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
544
//    std::cout << "FRAME " << frames.begin()->first << "\n";
545
    vtkPolyData* pData=NULL;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
546

547
    if(frame) pData=frame->GetPolyData2D();
548

549
#if VTK_MAJOR_VERSION <= 5
550
    extern_glyphs_pedestrians->SetSource(agentShape->GetOutput());
551
    if (frame )extern_glyphs_pedestrians->SetInput(pData);
552
#else
553
    extern_glyphs_pedestrians->SetInputConnection(agentShape->GetOutputPort());
554
    if (frame) extern_glyphs_pedestrians->SetInputData(pData);
555
#endif
556

557 558
    extern_glyphs_pedestrians->ThreeGlyphsOff();
    extern_glyphs_pedestrians->ExtractEigenvaluesOff();
559 560
    //_agents->SymmetricOn();
    //_agents->Update();
561

562 563 564
    VTK_CREATE(vtkPolyDataMapper, mapper);
    mapper->SetInputConnection(extern_glyphs_pedestrians->GetOutputPort());
    //improve the performance
565

566 567 568 569 570 571 572 573
    VTK_CREATE(vtkLookupTable, lut);
    lut->SetHueRange(0.0,0.470);
    //lut->SetSaturationRange(0,0);
    lut->SetValueRange(1.0,1.0);
    lut->SetNanColor(0.2,0.2,0.2,0.5);
    lut->SetNumberOfTableValues(256);
    lut->Build();
    mapper->SetLookupTable(lut);
574

575
    extern_glyphs_pedestrians_actor_2D->SetMapper(mapper);
576
    //extern_glyphs_pedestrians_actor_2D->GetProperty()->BackfaceCullingOn();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
577

578
    //if(extern_trajectories_firstSet.getNumberOfAgents()>0)
579
    _renderer->AddActor(extern_glyphs_pedestrians_actor_2D);
580

581 582 583 584 585
    // structure for the labels
    VTK_CREATE(vtkLabeledDataMapper, labelMapper);
    extern_pedestrians_labels->SetMapper(labelMapper);
    labelMapper->SetFieldDataName("labels");
    labelMapper->SetLabelModeToLabelFieldData();
586
    _renderer->AddActor2D(extern_pedestrians_labels);
587
    extern_pedestrians_labels->SetVisibility(false);
588
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
589

590 591
void ThreadVisualisation::initGlyphs3D()
{
592

Ulrich Kemloh's avatar
Ulrich Kemloh committed
593 594 595
    if(extern_glyphs_pedestrians_3D) extern_glyphs_pedestrians_3D->Delete();
    if(extern_glyphs_pedestrians_actor_3D) extern_glyphs_pedestrians_actor_3D->Delete();

596
    extern_glyphs_pedestrians_3D = vtkTensorGlyph::New();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
597
    extern_glyphs_pedestrians_actor_3D = vtkActor::New();
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    //now create the glyphs with zylinders
    VTK_CREATE (vtkCylinderSource, agentShape);
    agentShape->SetHeight(160);
    agentShape->SetRadius(20);
    //agentShape->SetCenter(0,0,80);
    agentShape->SetResolution(20);
    /*
        //VTK_CREATE (vtkAssembly, agentShape);
        vtk3DSImporter* importer = vtk3DSImporter::New();
        importer->SetFileName("data/140404_charles.3ds");
        importer->Read();
        importer->Update();
        //importer->GetRenderer()->GetLights();
        //importer->GetRenderWindow()->GetInteractor()->Start();

        ////collect all the elements from the 3ds
615
        vtkActorCollection* collection=importer->GetRenderer()x->GetActors();
616 617 618 619 620 621 622 623 624 625 626 627 628
        vtkActor *actorCharlie= collection->GetLastActor();
        actorCharlie->InitPathTraversal();
        vtkMapper *mapperCharlie=actorCharlie->GetMapper();
        mapperCharlie->Update();
        //_agents3D->SetColorGlyphs(false);
        vtkPolyData *dataCharlie=vtkPolyData::SafeDownCast(mapperCharlie->GetInput());

        //strip the data, again
        //speed the rendering using triangles stripers
        VTK_CREATE(vtkTriangleFilter, tris);
        tris->SetInputData(dataCharlie);
        VTK_CREATE(vtkStripper, agentShape);
        agentShape->SetInputConnection(tris->GetOutputPort());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
629
    */
Ulrich Kemloh's avatar
Ulrich Kemloh committed
630

Ulrich Kemloh's avatar
Ulrich Kemloh committed
631 632 633 634 635 636 637 638 639 640 641 642
    //speed the rendering using triangles stripers
    //vtkTriangleFilter *tris = vtkTriangleFilter::New();
    VTK_CREATE(vtkTriangleFilter, tris);
    tris->SetInputConnection(agentShape->GetOutputPort());
    //tris->GetOutput()->ReleaseData();

    //vtkStripper *strip = vtkStripper::New();
    VTK_CREATE(vtkStripper, strip);
    strip->SetInputConnection(tris->GetOutputPort());
    //strip->GetOutput()->ReleaseData();

    extern_glyphs_pedestrians_3D->SetSourceConnection(strip->GetOutputPort());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
643

644
    //first frame
645 646 647 648 649
    auto&& frames=extern_trajectories_firstSet.GetFrames();
    if(frames.empty()) return;

    Frame * frame = frames.begin()->second;

650 651 652
    vtkPolyData* pData=NULL;
    if(frame) pData=frame->GetPolyData2D();

653
#if VTK_MAJOR_VERSION <= 5
Ulrich Kemloh's avatar
Ulrich Kemloh committed
654
    extern_glyphs_pedestrians_3D->SetSource(strip->GetOutput());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
655
    if (frame )extern_glyphs_pedestrians_3D->SetInput(pData);
656
#else
Ulrich Kemloh's avatar
Ulrich Kemloh committed
657
    extern_glyphs_pedestrians_3D->SetInputConnection(strip->GetOutputPort());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
658
    if (frame )extern_glyphs_pedestrians_3D->SetInputData(pData);
659
#endif
Ulrich Kemloh's avatar
Ulrich Kemloh committed
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
    extern_glyphs_pedestrians_3D->ThreeGlyphsOff();
    extern_glyphs_pedestrians_3D->ExtractEigenvaluesOff();

    VTK_CREATE(vtkPolyDataMapper, mapper);
    mapper->SetInputConnection(extern_glyphs_pedestrians_3D->GetOutputPort());

    VTK_CREATE(vtkLookupTable, lut);
    lut->SetHueRange(0.0,0.470);
    //lut->SetSaturationRange(0,0);
    lut->SetValueRange(1.0,1.0);
    lut->SetNanColor(0.2,0.2,0.2,0.5);
    lut->SetNumberOfTableValues(256);
    lut->Build();
    mapper->SetLookupTable(lut);

676
    extern_glyphs_pedestrians_actor_3D->SetMapper(mapper);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
677
    extern_glyphs_pedestrians_actor_3D->GetProperty()->BackfaceCullingOn();
678
    //if(extern_trajectories_firstSet.getNumberOfAgents()>0)
679
    _renderer->AddActor(extern_glyphs_pedestrians_actor_3D);
680

Ulrich Kemloh's avatar
Ulrich Kemloh committed
681
    extern_glyphs_pedestrians_actor_3D->SetVisibility(false);
682
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
683

Ulrich Kemloh's avatar
Ulrich Kemloh committed
684 685 686
void  ThreadVisualisation::init()
{
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
687

Ulrich Kemloh's avatar
Ulrich Kemloh committed
688 689
void ThreadVisualisation::finalize()
{
690

Ulrich Kemloh's avatar
Ulrich Kemloh committed
691 692
}

693 694 695 696 697 698
void ThreadVisualisation::QcolorToDouble(const QColor &col, double *rgb)
{
    rgb[0]=(double)col.red()/255.0;
    rgb[1]=(double)col.green()/255.0;
    rgb[2]=(double)col.blue()/255.0;
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
699

Ulrich Kemloh's avatar
Ulrich Kemloh committed
700 701 702 703 704 705 706 707 708 709
void ThreadVisualisation::initLegend(/*std::vector scalars*/)
{
    //lookup table
    vtkLookupTable* lut =  vtkLookupTable::New();
    lut->SetHueRange(0.0,0.566);
    lut->SetTableRange(20.0, 50.0);
    //lut->SetSaturationRange(0,0);
    //lut->SetValueRange(0.0,1.0);
    lut->SetNumberOfTableValues(50);
    lut->Build();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
710

Ulrich Kemloh's avatar
Ulrich Kemloh committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    //vtkPolyData* polyData = vtkPolyData::New();
    //polyData->sets
    //polyData->SetPoints(pts);
    //polyData->GetPointData()->SetScalars(scalars);
    //vtkPolyDataMapper* mapper =vtkPolyDataMapper::New();
    //mapper->SetLookupTable(lut);
    //  mapper->SetInput(polyData->GetPolyDataOutput());
    //  mapper->SetScalarRange(randomColors->GetPolyDataOutput()->GetScalarRange());

    vtkTextProperty* titleProp = vtkTextProperty::New();
    titleProp->SetFontSize(14);

    vtkTextProperty* labelProp = vtkTextProperty::New();
    labelProp->SetFontSize(10);

    //	cerr<<"label: " <<labelProp->GetFontSize()<<endl;
    //	cerr<<"     : " <<labelProp->GetFontFamilyAsString()<<endl;
    //
    //	cerr<<"title: " <<titleProp->GetFontSize()<<endl;
    //	cerr<<"     : " <<titleProp->GetFontFamilyAsString()<<endl;

    vtkScalarBarActor* scalarBar = vtkScalarBarActor::New();
    scalarBar->SetLookupTable(lut);
    //scalarBar->SetLookupTable(mapper->GetLookupTable());
    scalarBar->SetTitle("Velocities ( cm/s )");
    scalarBar->SetTitleTextProperty(titleProp);
    scalarBar->SetLabelTextProperty (labelProp);
    scalarBar->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
    //	scalarBar->GetPositionCoordinate()->SetValue(0.1, 0.05);
    scalarBar->GetPositionCoordinate()->SetValue(0.005, 0.90075);
    scalarBar->SetOrientationToVertical();
    scalarBar->SetNumberOfLabels(7);
    scalarBar->SetMaximumNumberOfColors(20);
    scalarBar->SetWidth(0.105);
    scalarBar->SetHeight(0.10);
    //scalarBar->SetPickable(1);
    //scalarBar->SetTextPositionToPrecedeScalarBar();
    //scalarBar->SetLabelFormat("%-#5.1f");
749 750
    _renderer->AddActor2D(scalarBar);
    _renderer->Render();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
751 752 753 754

}


Ulrich Kemloh's avatar
Ulrich Kemloh committed
755 756
void ThreadVisualisation::setAxisVisible(bool status)
{
757
    _axis->SetVisibility(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
758 759
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
760 761
void ThreadVisualisation::setCameraPerspective(int mode)
{
762
    if(_renderer==NULL) return;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
763

Ulrich Kemloh's avatar
Ulrich Kemloh committed
764
    switch (mode) {
765
    case 1: //TOP oder RESET
766
        _renderer->GetActiveCamera()->DeepCopy(_topViewCamera);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
767
        break;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
768

Ulrich Kemloh's avatar
Ulrich Kemloh committed
769
    case 2://SIDE
Ulrich Kemloh's avatar
Ulrich Kemloh committed
770

Ulrich Kemloh's avatar
Ulrich Kemloh committed
771
        break;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
772

Ulrich Kemloh's avatar
Ulrich Kemloh committed
773 774 775
    case 3:
        //FRONT
        break;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
776

Ulrich Kemloh's avatar
Ulrich Kemloh committed
777 778 779 780 781 782 783 784
    case 4: { // agent virtual reality
        //vtkCamera *camera = renderer->GetActiveCamera();
        //camera->SetRoll(-90);
        //std::cout <<"roll       [ "<< camera->GetRoll()<<" ]"<<std::endl;
        //double roll=camera->GetRoll();
        //camera->Roll(-90-camera->GetRoll());
        //camera->Modified();
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
785

786
        break;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
787
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
788 789
}

790
void ThreadVisualisation::setBackgroundColor(const QColor& col)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
791
{
792 793
    double  bgcolor[3];
    QcolorToDouble(col,bgcolor);
794 795
    if (_renderer!=NULL)
        _renderer->SetBackground(bgcolor);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
796 797
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
798 799 800
void ThreadVisualisation::setWindowTitle(QString title)
{
    if(title.isEmpty())return;
801
    _winTitle=title;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
802 803 804
}

/// @todo check this construct
Ulrich Kemloh's avatar
Ulrich Kemloh committed
805 806
void ThreadVisualisation::setGeometry(FacilityGeometry* geometry)
{
807 808 809
    //this->_geometry=geometry;
    cout<<"dont call me"<<endl;
    exit(0);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
810 811
}

812
GeometryFactory &ThreadVisualisation::getGeometry()
Ulrich Kemloh's avatar
Ulrich Kemloh committed
813
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
814
    //delete the old object
815 816 817
    //delete _geometry;
    //_geometry=new FacilityGeometry();
    return _geometry;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
818 819
}

820
void ThreadVisualisation::setWallsColor(const QColor &color)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
821
{
822 823
    double  rbgColor[3];
    QcolorToDouble(color,rbgColor);
824
    _geometry.ChangeWallsColor(rbgColor);
825 826
}

827
void ThreadVisualisation::setFloorColor(const QColor &color)
828
{
829 830
    double  rbgColor[3];
    QcolorToDouble(color,rbgColor);
831
    _geometry.ChangeFloorColor(rbgColor);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
832 833
}

834 835 836 837 838 839 840
void ThreadVisualisation::setObstacleColor(const QColor &color)
{
    double  rbgColor[3];
    QcolorToDouble(color,rbgColor);
    _geometry.ChangeObstaclesColor(rbgColor);
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
841 842
void ThreadVisualisation::setGeometryLabelsVisibility(int v)
{
843
    _geometry.ShowGeometryLabels(v);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
844 845
}

846
void ThreadVisualisation::setExitsColor(const QColor &color)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
847
{
848 849
    double  rbgColor[3];
    QcolorToDouble(color,rbgColor);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
850 851
    // std::cout << "ENTER: " << rbgColor[0] << ", " << rbgColor[1] << ", " << rbgColor[2] << "\n";
// HH
852
    _geometry.ChangeExitsColor(rbgColor);
853 854
}

855
void ThreadVisualisation::setNavLinesColor(const QColor &color)
856
{
857 858
    double  rbgColor[3];
    QcolorToDouble(color,rbgColor);
859
    _geometry.ChangeNavLinesColor(rbgColor);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
860 861 862 863
}

/// enable/disable 2D
/// todo: consider disabling the 2d option in the 3d, and vice-versa
Ulrich Kemloh's avatar
Ulrich Kemloh committed
864 865
void ThreadVisualisation::setGeometryVisibility2D(bool status)
{
866
    _geometry.Set2D(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
867 868 869 870
}

/// enable/disable 3D
/// todo: consider disabling the 2d option in the 3d, and vice-versa
Ulrich Kemloh's avatar
Ulrich Kemloh committed
871 872
void ThreadVisualisation::setGeometryVisibility3D(bool status)
{
873
    _geometry.Set3D(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
874 875
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
876 877
void ThreadVisualisation::setOnscreenInformationVisibility(bool show)
{
878
    _runningTime->SetVisibility(show);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
879 880
}

881 882 883 884
void ThreadVisualisation::Create2dAgent()
{

}