dtkVisualizationViewer.cpp 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include "dtkVisualizationViewer.h"
16
#include "dtkVisualizationViewerStack.h"
17

18 19
#include <dtkCore>

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
20 21 22
#include <dtkWidgets>

#include <dtkVisualization>
23

24
#include <dtkVisualizationWidgets/dtkVisualizationWidgetsClutEditor>
25

26
#include <vtkCellData.h>
27
#include <vtkDataSet.h>
28
#include <vtkDataArray.h>
CABEL Tristan's avatar
CABEL Tristan committed
29
#include <vtkDoubleArray.h>
30
#include <vtkPointData.h>
CABEL Tristan's avatar
CABEL Tristan committed
31 32
#include <vtkSmartPointer.h>

33 34
#include <QtCore>

CABEL Tristan's avatar
CABEL Tristan committed
35
#include <math.h>
36

37 38 39 40
// ///////////////////////////////////////////////////////////////////
// dtkVisualizationViewerPrivate
// ///////////////////////////////////////////////////////////////////

41 42 43
class dtkVisualizationViewerPrivate
{
public:
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
44
    dtkVisualizationViewerStack *stack = nullptr;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
45
    dtkVisualizationDecoratorCollection collection;
46
    vtkDataSet *dataset = nullptr;
CABEL Tristan's avatar
CABEL Tristan committed
47
    double v_max = 100.;
CABEL Tristan's avatar
CABEL Tristan committed
48 49 50

public:
    void createPlot2DData(dtkVisualizationPlot2D *plot);
51
    void drunkData(double time);
52 53
};

CABEL Tristan's avatar
CABEL Tristan committed
54

CABEL Tristan's avatar
CABEL Tristan committed
55
void dtkVisualizationViewerPrivate::drunkData(double val)
56 57 58 59 60
{
    if(!dataset) {
        qInfo() << "import a dataset before calling this method";
    }

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
61
    //qDebug() << "val" << val;
62

CABEL Tristan's avatar
CABEL Tristan committed
63
    if(val == 1.) {
64 65 66 67
        qDebug() << "nb points array" << dataset->GetPointData()->GetNumberOfArrays();
        qDebug() << "nb cells  array" << dataset->GetCellData()->GetNumberOfArrays();
    }

CABEL Tristan's avatar
CABEL Tristan committed
68 69 70 71 72 73 74 75
    vtkIdType nb_points = dataset->GetNumberOfPoints();
    vtkIdType nb_cells = dataset->GetNumberOfCells();

    double xyz[3];
    double new_value[3] = {val, 1.5*val, -1.*val};

    double bounds[6]; //x_min , x_max , y_min , y_max , z_min , z_max
    dataset->GetBounds(bounds);
76 77 78 79 80 81 82

    {
        vtkPointData *point_data = dataset->GetPointData();
        vtkIdType nb_arrays = point_data->GetNumberOfArrays();

        for (vtkIdType i = 0; i < nb_arrays; ++i) {
            vtkDataArray *array = point_data->GetArray(i);
CABEL Tristan's avatar
CABEL Tristan committed
83
            vtkIdType nb_tuples = array->GetNumberOfTuples();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            vtkIdType nb_cmpts = array->GetNumberOfComponents();

            if (nb_cmpts == 1) {
                for(vtkIdType t=0; t < nb_tuples; ++t) {
                    dataset->GetPoint(t, xyz);
                    double value = val * std::cos(val + ((xyz[0] - bounds[0])/bounds[1]));
                    array->SetTuple1(t, value);
                }
            } else if (nb_cmpts == 3) {
                for(vtkIdType t=0; t < nb_tuples; ++t) {
                    dataset->GetPoint(t, xyz);
                    new_value[0] = val * std::cos(val + ((xyz[0] - bounds[0])/bounds[1]));
                    new_value[1] = val * std::cos(val + ((xyz[1] - bounds[2])/bounds[3]));
                    new_value[2] = val * std::cos(val + ((xyz[2] - bounds[4])/bounds[5]));
                    array->SetTuple3(t, new_value[0], new_value[1], new_value[2]);
                }
100 101 102 103 104 105 106 107 108
            }
        }
    }
    {
        vtkCellData *cell_data = dataset->GetCellData();
        vtkIdType nb_arrays = cell_data->GetNumberOfArrays();

        for (vtkIdType i = 0; i < nb_arrays; ++i) {
            vtkDataArray *array = cell_data->GetArray(i);
CABEL Tristan's avatar
CABEL Tristan committed
109
            vtkIdType nb_tuples = array->GetNumberOfTuples();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
110 111 112 113
            vtkIdType nb_cmpts = array->GetNumberOfComponents();

            if (nb_cmpts == 1) {
                for(vtkIdType t=0; t < nb_tuples; ++t) {
CABEL Tristan's avatar
CABEL Tristan committed
114 115 116
                    double cell_bounds[6];
                    dataset->GetCell(t)->GetBounds(cell_bounds);
                    double value = val * std::cos(val + ((cell_bounds[0] - bounds[0])/bounds[1]));
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
117 118 119 120
                    array->SetTuple1(t, value);
                }
            } else if (nb_cmpts == 3) {
                for(vtkIdType t=0; t < nb_tuples; ++t) {
CABEL Tristan's avatar
CABEL Tristan committed
121 122 123 124 125 126
                    double cell_bounds[6];
                    dataset->GetCell(t)->GetBounds(cell_bounds);

                    new_value[0] = val * std::cos(val + ((cell_bounds[0] - bounds[0])/bounds[1]));
                    new_value[1] = val * std::cos(val + ((cell_bounds[2] - bounds[2])/bounds[3]));
                    new_value[2] = val * std::cos(val + ((cell_bounds[4] - bounds[4])/bounds[5]));
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
127 128
                    array->SetTuple3(t, new_value[0], new_value[1], new_value[2]);
                }
129 130 131 132 133
            }
        }
    }
}

CABEL Tristan's avatar
CABEL Tristan committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
void dtkVisualizationViewerPrivate::createPlot2DData(dtkVisualizationPlot2D *plot)
{
    static bool plot_is_init = false;
    if(plot_is_init)
        return;

    int MAX = 5;
    double pi = 3.1516;
    vtkSmartPointer<vtkDoubleArray> field_1 = vtkSmartPointer<vtkDoubleArray>::New();
    field_1->SetName("Linear");
    field_1->SetNumberOfComponents(1);
    field_1->SetNumberOfTuples(MAX * 1e2);

    vtkSmartPointer<vtkDoubleArray> field_2 = vtkSmartPointer<vtkDoubleArray>::New();
    field_2->SetName("Fake EEG");
    field_2->SetNumberOfComponents(1);
    field_2->SetNumberOfTuples(MAX * 1e2);

    vtkSmartPointer<vtkDoubleArray> field_3 = vtkSmartPointer<vtkDoubleArray>::New();
    field_3->SetName("third_field");
    field_3->SetNumberOfComponents(1);
    field_3->SetNumberOfTuples(MAX * 1e2);

    for(double i = 0; i < MAX; i += 1e-2) {
        field_1->SetValue(i * 1e2, i);
        field_2->SetValue(i * 1e2, cos(M_PI*6*i) + 2*sin(M_PI*2*i));
        field_3->SetValue(i * 1e2, rand() % 10);
    }

    plot->addFieldX(field_1);
    plot->addFieldY(field_2, true, vtkChart::LINE, vtkPlotPoints::NONE);
    plot->addFieldY(field_3);

    // insert in menu
    dtkWidgetsMenuBar *menu_bar = dtkApp->window()->menubar();
    dtkWidgetsMenu *plot_menu = menu_bar->menu("View " + plot->objectName());

    if(!plot_menu) {
        plot_menu = menu_bar->addMenu(0, "View " + plot->objectName());
    }

    dtkWidgetsMenu *plot_group = plot_menu->addMenu(0, "Views");

    for(auto inspector : plot->inspectors()) {
        inspector->setVisible(true);
        dtkWidgetsMenuItemDIY *item = new dtkWidgetsMenuItemDIY(inspector->objectName(), inspector);
        plot_group->addItem(item);
    }

    menu_bar->touch();

    plot_is_init = true;
}

188 189 190 191
// ///////////////////////////////////////////////////////////////////
// dtkVisualizationViewer
// ///////////////////////////////////////////////////////////////////

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
192
dtkVisualizationViewer::dtkVisualizationViewer(QWidget *parent) : dtkWidgetsMainWindow(parent), d(new dtkVisualizationViewerPrivate)
193
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
194
    dtk::visualization::initialize();
LE BRETON Come's avatar
LE BRETON Come committed
195

196 197
    QWidget *widget = new QWidget(this);

198
    d->stack = new dtkVisualizationViewerStack(this);
199

200 201
    QHBoxLayout *layout = new QHBoxLayout(widget);
    layout->setContentsMargins(this->menubar()->width(), 0, 0, 0);
202
    layout->setSpacing(0);
203

204 205
    layout->addWidget(d->stack);

LE BRETON Come's avatar
LE BRETON Come committed
206 207
    dtkWidgetsParameterMenuBarGenerator menubar_generator(":menu.json", ":parameters.json");
    menubar_generator.populate(this->menubar());
LE BRETON Come's avatar
LE BRETON Come committed
208 209
    this->populate();

LE BRETON Come's avatar
LE BRETON Come committed
210
    this->menubar()->touch();
211

CABEL Tristan's avatar
CABEL Tristan committed
212
    QHash<QString, dtkCoreParameter *> parameters = menubar_generator.parameters();
213

214 215 216 217 218 219 220 221
    // Restore last path used from the settings
    QSettings settings;
    settings.beginGroup("viewer");
    QString default_path = settings.value("last_import_dir").toString();
    if (!default_path.isEmpty())
        parameters["open"]->setValue(default_path);
    settings.endGroup();

CABEL Tristan's avatar
CABEL Tristan committed
222 223 224 225
    dtkCoreParameter *open_parameter = parameters["open"];
    if(open_parameter) {
        open_parameter->connect([=] (QVariant v) {
                QString path = v.value<dtk::d_path>().path();
226
                if(path.isEmpty()) return;
LE BRETON Come's avatar
LE BRETON Come committed
227 228 229
                importDataSet(v.value<dtk::d_path>().path());
            });
    }
WINTZ Julien's avatar
WINTZ Julien committed
230

CABEL Tristan's avatar
CABEL Tristan committed
231 232 233 234 235 236 237 238 239 240
    dtkCoreParameter *plotdata_parameter = parameters["plotdata"];
    if(plotdata_parameter) {
        plotdata_parameter->connect([=] (QVariant v) {
                dtkVisualizationPlot2D *plot = dynamic_cast<dtkVisualizationPlot2D *>(d->stack->layout()->current()->view());

                if(plot)
                    d->createPlot2DData(plot);
            });
    }

241 242

    //for animating data
CABEL Tristan's avatar
CABEL Tristan committed
243
    // 20s to go from 0 to d->v_max
CABEL Tristan's avatar
CABEL Tristan committed
244
    QTimeLine *timeline = new QTimeLine(20000, this); // 20 s
CABEL Tristan's avatar
CABEL Tristan committed
245
    timeline->setFrameRange(0, d->v_max);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    connect(timeline, &QTimeLine::frameChanged,
            [=](int time) {
                d->drunkData(time);
                emit updated();
            });


    dtkCoreParameter *drunkdata_parameter = parameters["drunkdata"];
    if(drunkdata_parameter) {
        drunkdata_parameter->connect([=] (QVariant v) {
                 if(v.value<dtk::d_bool>().value())
                     timeline->start();
            });
    }

261
    this->setAttribute(Qt::WA_DeleteOnClose);
262
    this->setCentralWidget(widget);
263
    this->setup();
264 265 266 267
}

dtkVisualizationViewer::~dtkVisualizationViewer(void)
{
268 269
    this->setdw();

270 271 272
    delete d;
}

273 274
void dtkVisualizationViewer::importDataSet(const QString& path)
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
275 276
    d->collection.clear();

277 278 279 280 281 282 283
    QFileInfo info(path);

    QSettings settings;
    settings.beginGroup("viewer");
    settings.setValue("last_import_dir", info.absoluteDir().path());
    settings.endGroup();

LE BRETON Come's avatar
LE BRETON Come committed
284
    dtkVisualizationCanvas *canvas = dynamic_cast<dtkVisualizationCanvas *>(d->stack->layout()->current()->view());
285

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
286 287
    if(!canvas)
        return;
288

289
    d->dataset = dtkVisualizationVTKReader(path);
290 291 292 293 294 295 296 297

    if (!d->dataset) {
        QMessageBox msgBox;
        msgBox.setText("Fail to read file, check file extension");
        msgBox.exec();
        return;
    }

298
    QVariant data = dtk::variantFromValue(d->dataset);
299

Pierre's avatar
Pierre committed
300 301 302 303 304 305
    dtkVisualizationDecoratorInfo              *decorator_info               = new dtkVisualizationDecoratorInfo;
    dtkVisualizationDecoratorDelaunay2D        *decorator_delaunay_2D        = new dtkVisualizationDecoratorDelaunay2D;
    dtkVisualizationDecoratorPoints            *decorator_points             = new dtkVisualizationDecoratorPoints;
    dtkVisualizationDecoratorScalarColorMap    *decorator_scalar_colormap    = new dtkVisualizationDecoratorScalarColorMap;
    dtkVisualizationDecoratorScalarGlyphs      *decorator_scalar_glyphs      = new dtkVisualizationDecoratorScalarGlyphs;
    dtkVisualizationDecoratorScalarIsolines    *decorator_scalar_isolines    = new dtkVisualizationDecoratorScalarIsolines;
306
    dtkVisualizationDecoratorSnapshot          *decorator_snapshot           = new dtkVisualizationDecoratorSnapshot;
LE BRETON Come's avatar
LE BRETON Come committed
307
    dtkVisualizationDecoratorVectorStreamlines *decorator_vector_streamlines = new dtkVisualizationDecoratorVectorStreamlines;
308
    dtkVisualizationDecoratorVolume            *decorator_volume             = new dtkVisualizationDecoratorVolume;
CABEL Tristan's avatar
CABEL Tristan committed
309

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
310 311 312 313 314 315
    QList<dtkVisualizationDecorator *> decorators;
    decorators << decorator_points
               << decorator_delaunay_2D
               << decorator_scalar_colormap
               << decorator_scalar_isolines
               << decorator_scalar_glyphs
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
316
               << decorator_vector_streamlines
317
               << decorator_volume
CABEL Tristan's avatar
CABEL Tristan committed
318
               << decorator_snapshot
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
319
        ;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
320 321 322 323 324 325 326 327 328 329 330 331

    for (auto dec : decorators) {
        dec->setData(data);
        if (dec->isDecorating()) {
            d->collection << dec;
        } else {
            delete dec;
        }
    }
    decorators.clear();

    d->collection.setCanvas(canvas);
LE BRETON Come's avatar
LE BRETON Come committed
332

333
    decorator_info->setData(data);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
334
    decorator_info->setCanvas(canvas);
335
    decorator_info->setFileInfo(info);
LE BRETON Come's avatar
LE BRETON Come committed
336

337 338 339 340 341 342
    dtkWidgetsMenuBar *menu_bar = dtkApp->window()->menubar();

    dtkWidgetsMenu *canvas_menu = menu_bar->menu("View " + canvas->objectName());

    Q_ASSERT(canvas_menu);

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
343 344 345
    if (decorator_info->isDecorating()) {
        for(auto inspector : decorator_info->inspectors()) {
            dtkWidgetsMenuItemDIY *item = new dtkWidgetsMenuItemDIY(inspector->objectName(), inspector);
346

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
347
            item->setSizePolicy(QSizePolicy::Expanding);
348

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
349 350
            canvas_menu->addItem(item);
        }
351
    }
352

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
353
    for(auto decorator : d->collection) {
354

355 356 357 358 359 360
        dtkWidgetsMenu *menu = canvas_menu->menu(decorator->objectName());

        if(!menu) {
            menu = canvas_menu->addMenu(0, decorator->objectName());
        }

361 362
        for(auto inspector : decorator->inspectors()) {
            dtkWidgetsMenuItemDIY *item = new dtkWidgetsMenuItemDIY(inspector->objectName(), inspector);
363 364 365 366 367 368 369 370

            menu->addItem(item);
        }

    }

    menu_bar->touch();

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
371
    d->collection.draw();
372 373


374
    //TODO complete
375 376
    connect(this, &dtkVisualizationViewer::updated,
            [=] (void) {
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
377
                d->collection.draw();
378
            });
379 380
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void dtkVisualizationViewer::setup(void)
{
    QSettings settings;
    settings.beginGroup("viewer");
    this->move(settings.value("position").toPoint());
    this->resize(settings.value("size").toSize());
    settings.endGroup();
}

void dtkVisualizationViewer::setdw(void)
{
    QSettings settings;
    settings.beginGroup("viewer");
    settings.setValue("position", this->pos());
    settings.setValue("size", this->size());
    settings.endGroup();
}

399 400 401 402 403 404 405
void dtkVisualizationViewer::keyPressEvent(QKeyEvent *event)
{
    QMainWindow::keyPressEvent(event);
}

void dtkVisualizationViewer::resizeEvent(QResizeEvent *event)
{
406
    dtkWidgetsMainWindow::resizeEvent(event);
407 408
}

409 410 411 412 413 414 415 416
void dtkVisualizationViewer::mouseMoveEvent(QMouseEvent *event)
{
    // do not show title bar when over the menu. use hardcoded values of menu :-(
    if (event->pos().x() < 300 + 32 + 12)
        return;

    dtkWidgetsMainWindow::mouseMoveEvent(event);
}
417 418
//
// dtkVisualizationViewer.cpp ends here