dtkVisualizationView3D.cpp 12.2 KB
Newer Older
Julien Wintz's avatar
Julien Wintz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

15
#include "dtkVisualizationView3D.h"
Julien Wintz's avatar
Julien Wintz committed
16

Julien Wintz's avatar
Julien Wintz committed
17 18 19
#include <dtkWidgets/dtkWidgetsHUD>
#include <dtkWidgets/dtkWidgetsHUDItem>
#include <dtkWidgets/dtkWidgetsOverlayPane>
Julien Wintz's avatar
Julien Wintz committed
20

21
#include <dtkVisualizationWidgets/dtkVisualizationWidgetsActorList>
22 23
#include <dtkVisualizationWidgets/dtkVisualizationWidgetsClutEditor>

24
#include <vtkCamera.h>
25 26
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkRenderer.h>
Julien Wintz's avatar
Julien Wintz committed
27
#include <vtkRenderWindowInteractor.h>
28 29
#include <vtkSmartPointer.h>

Julien Wintz's avatar
Julien Wintz committed
30 31 32
#include <QVTKOpenGLWidget.h>

// ///////////////////////////////////////////////////////////////////
33
// dtkVisualizationView3DPrivate
Julien Wintz's avatar
Julien Wintz committed
34 35
// ///////////////////////////////////////////////////////////////////

36
class dtkVisualizationView3DPrivate : public QVTKOpenGLWidget
Julien Wintz's avatar
Julien Wintz committed
37 38 39 40
{
    Q_OBJECT

public:
41 42
     dtkVisualizationView3DPrivate(QWidget *parent = Q_NULLPTR);
    ~dtkVisualizationView3DPrivate(void);
43

44 45 46 47
public slots:
    void enableInteractor(void);
    void disableInteractor(void);

48 49 50 51 52 53
public:
    QSize sizeHint(void) const;

public:
    vtkSmartPointer<vtkGenericOpenGLRenderWindow> window;
    vtkSmartPointer<vtkRenderer> renderer;
54 55 56

protected:
    void mousePressEvent(QMouseEvent *event);
Julien Wintz's avatar
Julien Wintz committed
57
    void resizeEvent(QResizeEvent *event);
58 59

public:
Julien Wintz's avatar
Julien Wintz committed
60
    dtkVisualizationView3D *q = nullptr;
Julien Wintz's avatar
Julien Wintz committed
61 62

public:
Julien Wintz's avatar
Julien Wintz committed
63 64
    dtkWidgetsHUD *hud;
    dtkWidgetsOverlayPane *overlay;
65 66

public:
67
    dtkVisualizationWidgetsActorList *actors;
68
    dtkVisualizationWidgetsClutEditor *clut;
Julien Wintz's avatar
Julien Wintz committed
69 70
};

71
dtkVisualizationView3DPrivate::dtkVisualizationView3DPrivate(QWidget *parent) : QVTKOpenGLWidget(parent)
Julien Wintz's avatar
Julien Wintz committed
72
{
73
    static int count = 1;
74

75 76
    this->renderer = vtkSmartPointer<vtkRenderer>::New();
    this->renderer->SetBackground(0.290, 0.295, 0.300);
Julien Wintz's avatar
Julien Wintz committed
77

78 79 80 81 82
    this->window = vtkGenericOpenGLRenderWindow::New();
    this->window->AddRenderer(this->renderer);

    this->SetRenderWindow(this->window);
    this->setEnableHiDPI(true);
83

84
    this->setObjectName(QString("View3D - %1").arg(count++));
Julien Wintz's avatar
Julien Wintz committed
85

Julien Wintz's avatar
Julien Wintz committed
86
    this->hud = new dtkWidgetsHUD(this);
87 88 89

    this->clut = new dtkVisualizationWidgetsClutEditor(this);

90 91 92 93 94 95 96 97
    this->actors = new dtkVisualizationWidgetsActorList(this);
    this->actors->setRenderer(this->renderer);

    dtkWidgetsOverlayPaneItem *actors_item = new dtkWidgetsOverlayPaneItem(this);
    actors_item->setTitle("Actors");
    actors_item->layout()->setContentsMargins(0, 0, 0, 0);
    actors_item->addWidget(this->actors);

98 99 100 101 102
    dtkWidgetsOverlayPaneItem *clut_item = new dtkWidgetsOverlayPaneItem(this);
    clut_item->setTitle("Color Lookup Table");
    clut_item->layout()->setContentsMargins(0, 0, 0, 0);
    clut_item->addWidget(this->clut);

Julien Wintz's avatar
Julien Wintz committed
103
    this->overlay = new dtkWidgetsOverlayPane(this);
104
    this->overlay->addWidget(actors_item);
105
    this->overlay->addWidget(clut_item);
Julien Wintz's avatar
Julien Wintz committed
106 107
    this->overlay->toggle();

108 109 110 111 112 113 114
    connect(this->overlay, &dtkWidgetsOverlayPane::entered, [=] () {
        this->GetInteractor()->Disable();
    });

    connect(this->overlay, &dtkWidgetsOverlayPane::left, [=] () {
        this->GetInteractor()->Enable();
    });
Julien Wintz's avatar
Julien Wintz committed
115 116
}

117
dtkVisualizationView3DPrivate::~dtkVisualizationView3DPrivate(void)
Julien Wintz's avatar
Julien Wintz committed
118 119 120 121
{

}

122 123 124 125 126 127 128 129 130 131
void dtkVisualizationView3DPrivate::enableInteractor(void)
{
    this->GetInteractor()->Enable();
}

void dtkVisualizationView3DPrivate::disableInteractor(void)
{
    this->GetInteractor()->Disable();
}

132
QSize dtkVisualizationView3DPrivate::sizeHint(void) const
133 134 135 136
{
    return QSize(800, 600);
}

137
void dtkVisualizationView3DPrivate::mousePressEvent(QMouseEvent *event)
138 139 140
{
    q->emit focused();

141 142 143 144 145
    if(this->overlay->rect().contains(event->pos())) {
        event->accept();
        return;
    }

146 147 148
    QVTKOpenGLWidget::mousePressEvent(event);
}

149
void dtkVisualizationView3DPrivate::resizeEvent(QResizeEvent *event)
Julien Wintz's avatar
Julien Wintz committed
150 151 152 153 154 155 156 157
{
    QVTKOpenGLWidget::resizeEvent(event);

    this->hud->resize(event->size());

    this->overlay->setFixedHeight(event->size().height());
}

Julien Wintz's avatar
Julien Wintz committed
158
// ///////////////////////////////////////////////////////////////////
159
// dtkVisualizationView3D
Julien Wintz's avatar
Julien Wintz committed
160 161
// ///////////////////////////////////////////////////////////////////

162
dtkVisualizationView3D::dtkVisualizationView3D(QWidget *parent) : dtkWidgetsWidget(parent)
Julien Wintz's avatar
Julien Wintz committed
163
{
164
    d = new dtkVisualizationView3DPrivate(this);
165
    d->q = this;
166 167 168 169

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(d);
Julien Wintz's avatar
Julien Wintz committed
170

Julien Wintz's avatar
Julien Wintz committed
171 172 173
    dtkWidgetsHUDItem *settings = d->hud->addItem(fa::sliders);
    dtkWidgetsHUDItem *viewport = d->hud->addItem(fa::compass);
    dtkWidgetsHUDItem *anchored = d->hud->addItem(fa::anchor);
174 175 176

    settings->setToolTip("Settings");
    viewport->setToolTip("Viewport");
177
    anchored->setToolTip("Unlink");
178 179

    connect(settings, SIGNAL(clicked()), d->overlay, SLOT(toggle()));
180
    connect(viewport, SIGNAL(clicked()), this, SLOT(switchCameraView()));
181
    connect(anchored, SIGNAL(clicked()), this, SLOT(unlink()));
182 183

    this->setAcceptDrops(true);
Julien Wintz's avatar
Julien Wintz committed
184 185
}

186
dtkVisualizationView3D::~dtkVisualizationView3D(void)
Julien Wintz's avatar
Julien Wintz committed
187
{
Julien Wintz's avatar
Julien Wintz committed
188
    delete d;
Julien Wintz's avatar
Julien Wintz committed
189 190
}

191
QWidget *dtkVisualizationView3D::widget(void)
192 193 194 195
{
    return d;
}

196
void dtkVisualizationView3D::link(dtkVisualizationView3D *other)
197 198 199
{
    d->renderer->SetActiveCamera(other->d->renderer->GetActiveCamera());

200
    other->d->GetRenderWindow()->AddObserver(vtkCommand::RenderEvent, this, &dtkVisualizationView3D::update);
201

202 203 204 205 206
    d->hud->addInfo("Linked");

    this->update();
}

207
void dtkVisualizationView3D::unlink(void)
208 209 210 211 212 213 214
{
    vtkSmartPointer<vtkCamera> camera = vtkCamera::New();
    camera->ShallowCopy(d->renderer->GetActiveCamera());

    d->renderer->SetActiveCamera(camera);

    d->hud->addInfo("Unlinked");
215 216 217 218

    this->update();
}

219
void dtkVisualizationView3D::update(void)
220
{
221
    // d->actors->update();
222

Julien Wintz's avatar
Julien Wintz committed
223 224 225 226 227
    bool enabled = d->GetInteractor()->GetEnabled();

    if(!enabled)
        d->GetInteractor()->Enable();

228
    d->GetInteractor()->Render();
Julien Wintz's avatar
Julien Wintz committed
229 230 231

    if(!enabled)
        d->GetInteractor()->Disable();
232 233
}

234
void dtkVisualizationView3D::importVTI(const QString& path)
235 236 237 238
{
    qDebug() << Q_FUNC_INFO << path;
}

239
void dtkVisualizationView3D::importVTP(const QString& path)
240 241 242 243
{
    qDebug() << Q_FUNC_INFO << path;
}

244
void dtkVisualizationView3D::importVTR(const QString& path)
245 246 247 248
{
    qDebug() << Q_FUNC_INFO << path;
}

249
void dtkVisualizationView3D::importVTS(const QString& path)
250 251 252 253
{
    qDebug() << Q_FUNC_INFO << path;
}

254 255 256 257 258
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkXMLUnstructuredGridReader.h>

259
void dtkVisualizationView3D::importVTU(const QString& path)
260
{
261 262
    vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
    reader->SetFileName(qPrintable(path));
263

264 265 266 267 268 269 270 271 272 273 274 275 276
    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());
    mapper->ScalarVisibilityOff();

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->EdgeVisibilityOn();
    actor->GetProperty()->SetLineWidth(2.0);

    d->renderer->AddActor(actor);
    d->renderer->ResetCamera();

    this->update();
277 278
}

279
void dtkVisualizationView3D::switchCameraView(void)
Julien Wintz's avatar
Julien Wintz committed
280
{
281 282 283
    qDebug() << Q_FUNC_INFO << "to be implemented as an iteration over 3D/2D+X/2D-X/2D+Y/2D-Y/2D+Z/@D-Z";
}

284
void dtkVisualizationView3D::setCameraViewNegativeX(void)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[1]-bounds[0]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0]+side, center[1], center[2]);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 1, 0);

    d->renderer->ResetCamera();

    this->update();
}

305
void dtkVisualizationView3D::setCameraViewPositiveX(void)
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[1]-bounds[0]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0]-side, center[1], center[2]);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 1, 0);

    d->renderer->ResetCamera();

    this->update();
}

326
void dtkVisualizationView3D::setCameraViewNegativeY(void)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[3]-bounds[2]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0], center[1]+side, center[2]);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 0, 1);

    d->renderer->ResetCamera();

    this->update();
}

347
void dtkVisualizationView3D::setCameraViewPositiveY(void)
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[3]-bounds[2]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0], center[1]-side, center[2]);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 0, 1);

    d->renderer->ResetCamera();

    this->update();
}

368
void dtkVisualizationView3D::setCameraViewNegativeZ(void)
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[5]-bounds[4]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0], center[1], center[2]+side);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 1, 0);

    d->renderer->ResetCamera();

    this->update();
}

389
void dtkVisualizationView3D::setCameraViewPositiveZ(void)
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
{
    double bounds[6]; d->renderer->ComputeVisiblePropBounds(bounds);

    double side = qAbs(bounds[5]-bounds[4]);

    double center[3];
    center[0] = (bounds[1]+bounds[0])/2;
    center[1] = (bounds[3]+bounds[2])/2;
    center[2] = (bounds[5]+bounds[4])/2;

    vtkCamera *camera = d->renderer->GetActiveCamera();
    camera->SetPosition(center[0], center[1], center[2]-side);
    camera->SetFocalPoint(center[0], center[1], center[2]);
    camera->SetViewUp(0, 1, 0);

    d->renderer->ResetCamera();

    this->update();
Julien Wintz's avatar
Julien Wintz committed
408 409
}

Julien Wintz's avatar
Julien Wintz committed
410 411 412 413 414
dtkWidgetsOverlayPane *dtkVisualizationView3D::overlay(void)
{
    return d->overlay;
}

415 416 417 418 419
dtkWidgetsHUD *dtkVisualizationView3D::hud(void)
{
    return d->hud;
}

Julien Wintz's avatar
Julien Wintz committed
420 421 422 423 424
vtkRenderer *dtkVisualizationView3D::renderer(void)
{
    return d->renderer;
}

Julien Wintz's avatar
Julien Wintz committed
425 426 427 428 429
vtkRenderWindowInteractor *dtkVisualizationView3D::interactor(void)
{
    return d->GetInteractor();
}

430
void dtkVisualizationView3D::dragEnterEvent(QDragEnterEvent *event)
431 432 433 434 435 436 437
{
    if (event->mimeData()->hasText())
        event->accept();
    else
        event->ignore();
}

438
void dtkVisualizationView3D::dragLeaveEvent(QDragLeaveEvent *event)
439 440 441 442
{
    event->accept();
}

443
void dtkVisualizationView3D::dragMoveEvent(QDragMoveEvent *event)
444 445 446 447 448 449 450
{
    if (event->mimeData()->hasText())
        event->accept();
    else
        event->ignore();
}

451
void dtkVisualizationView3D::dropEvent(QDropEvent *event)
452 453 454 455 456 457 458 459 460 461 462
{
    if (event->mimeData()->hasText()) {

        QString path = event->mimeData()->text();

        if(!path.startsWith("file://"))
            return;

        path.remove("file://");

        if (path.endsWith(".vti"))
463
            this->importVTI(path);
464 465

        if (path.endsWith(".vtp"))
466
            this->importVTP(path);
467 468

        if (path.endsWith(".vtr"))
469
            this->importVTR(path);
470 471

        if (path.endsWith(".vts"))
472
            this->importVTS(path);
473 474

        if (path.endsWith(".vtu"))
475
            this->importVTU(path);
476 477 478

        event->accept();
    }
479 480
}

481
void dtkVisualizationView3D::keyPressEvent(QKeyEvent *event)
482 483
{
    event->ignore();
484

Julien Wintz's avatar
Julien Wintz committed
485
    // dtkWidgetsWidget::keyPressEvent(event);
486 487
}

Julien Wintz's avatar
Julien Wintz committed
488 489
// ///////////////////////////////////////////////////////////////////

490
#include "dtkVisualizationView3D.moc"
Julien Wintz's avatar
Julien Wintz committed
491 492

//
493
// dtkVisualizationView3D.cpp ends here