dtkVisualizationDecoratorVectorStreamTracer.cpp 25.9 KB
Newer Older
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
1 2 3 4 5 6 7 8 9
// dtkVisualizationDecoratorVectorStreamTracer.cpp
//

#include "dtkVisualizationDecoratorVectorStreamTracer.h"

#include "dtkVisualizationDecoratorWithClut_p.h"
#include "dtkVisualizationMetaType.h"
#include "dtkVisualizationView2D.h"

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
10
#include <dtkVisualizationWidgets/dtkVisualizationWidgetsCategory>
11

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
12 13 14 15 16 17
#include <dtkLog>

#include <QtGui>
#include <QtWidgets>

#include <vtkActor.h>
18
#include <vtkBoundingBox.h>
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
19 20 21 22 23 24 25 26 27 28
#include <vtkCellCenters.h>
#include <vtkCellData.h>
#include <vtkCenterOfMass.h>
#include <vtkColorTransferFunction.h>
#include <vtkLineRepresentation.h>
#include <vtkLineSource.h>
#include <vtkLineWidget2.h>
#include <vtkLookupTable.h>
#include <vtkPiecewiseFunction.h>
#include <vtkPointData.h>
29
#include <vtkPointSource.h>
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
30 31
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
32
#include <vtkProperty.h>
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
33 34 35 36
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
37 38
#include <vtkSphereRepresentation.h>
#include <vtkSphereWidget2.h>
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
39 40 41 42 43 44 45
#include <vtkStreamTracer.h>
#include <vtkTubeFilter.h>

// /////////////////////////////////////////////////////////////////
// dtkStreamTracerObserver
// /////////////////////////////////////////////////////////////////

46
class dtkVisualizationDecoratorStreamTracerSphereObserver : public vtkCommand
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
47 48
{
private:
49
    dtkVisualizationDecoratorStreamTracerSphereObserver(void) {
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
50 51 52
    };

public:
53
    static dtkVisualizationDecoratorStreamTracerSphereObserver *New(void)
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
54
    {
55
        return new dtkVisualizationDecoratorStreamTracerSphereObserver;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
56 57
    };

58
    void Execute(vtkObject *caller, unsigned long event, void *) override
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
59 60
    {
        if (event == vtkCommand::InteractionEvent) {
61 62
            source_sphere->GetPolyData(source_data);
            stream_tracer->SetSourceData(source_data);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
63
            stream_tracer->Modified();
64 65 66 67 68 69 70 71 72

            vtkSphereWidget2 *source_sphere_widget = reinterpret_cast<vtkSphereWidget2*>(caller);
            vtkSphereRepresentation *sphere_representation = vtkSphereRepresentation::SafeDownCast(source_sphere_widget->GetRepresentation());
            radius->setValue(sphere_representation->GetRadius());
            double pos[3];
            sphere_representation->GetCenter(pos);
            pos_x->setValue(pos[0]);
            pos_y->setValue(pos[1]);
            pos_z->setValue(pos[2]);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
73 74 75 76
        }
    };

public:
77 78 79
    vtkSmartPointer<vtkPolyData> source_data;
    vtkSmartPointer<vtkSphereRepresentation> source_sphere;
    vtkSmartPointer<vtkStreamTracer> stream_tracer;
80 81 82 83
    dtkVisualizationWidgetsScalarControl *pos_x = nullptr;
    dtkVisualizationWidgetsScalarControl *pos_y = nullptr;
    dtkVisualizationWidgetsScalarControl *pos_z = nullptr;
    dtkVisualizationWidgetsScalarControl *radius = nullptr;
84 85 86 87 88 89 90 91 92 93 94 95 96 97
};

class dtkVisualizationDecoratorStreamTracerLineObserver : public vtkCommand
{
private:
    dtkVisualizationDecoratorStreamTracerLineObserver(void) {
    };

public:
    static dtkVisualizationDecoratorStreamTracerLineObserver *New(void)
    {
        return new dtkVisualizationDecoratorStreamTracerLineObserver;
    };

98
    void Execute(vtkObject *caller, unsigned long event, void *) override
99 100 101 102 103
    {
        if (event == vtkCommand::InteractionEvent) {
            source_line->GetPolyData(source_data);
            stream_tracer->SetSourceData(source_data);
            stream_tracer->Modified();
104 105 106 107 108 109 110 111 112 113 114 115 116

            vtkLineWidget2 *source_line_widget = reinterpret_cast<vtkLineWidget2*>(caller);
            vtkLineRepresentation *line_representation = vtkLineRepresentation::SafeDownCast(source_line_widget->GetLineRepresentation());
            double pos1[3];
            double pos2[3];
            line_representation->GetPoint1WorldPosition(pos1);
            line_representation->GetPoint2WorldPosition(pos2);
            pos1_x->setValue(pos1[0]);
            pos1_y->setValue(pos1[1]);
            pos1_z->setValue(pos1[2]);
            pos2_x->setValue(pos2[0]);
            pos2_y->setValue(pos2[1]);
            pos2_z->setValue(pos2[2]);
117 118 119 120 121 122
        }
    };

public:
    vtkSmartPointer<vtkPolyData> source_data;
    vtkSmartPointer<vtkLineRepresentation>   source_line;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
123
    vtkSmartPointer<vtkStreamTracer> stream_tracer;
124 125 126 127 128 129
    dtkVisualizationWidgetsScalarControl *pos1_x = nullptr;
    dtkVisualizationWidgetsScalarControl *pos1_y = nullptr;
    dtkVisualizationWidgetsScalarControl *pos1_z = nullptr;
    dtkVisualizationWidgetsScalarControl *pos2_x = nullptr;
    dtkVisualizationWidgetsScalarControl *pos2_y = nullptr;
    dtkVisualizationWidgetsScalarControl *pos2_z = nullptr;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
130 131 132 133 134 135 136 137 138
};

// ///////////////////////////////////////////////////////////////////
// dtkVisualizationDecoratorVectorStreamTracerPrivate declaration
// ///////////////////////////////////////////////////////////////////

class dtkVisualizationDecoratorVectorStreamTracerPrivate
{
public:
139 140
    vtkSmartPointer<dtkVisualizationDecoratorStreamTracerSphereObserver> source_sphere_observer;
    vtkSmartPointer<dtkVisualizationDecoratorStreamTracerLineObserver> source_line_observer;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
141
    vtkSmartPointer<vtkLineWidget2> source_line_widget;
142 143
    vtkSmartPointer<vtkSphereWidget2> source_sphere_widget;
    vtkSmartPointer<vtkPolyData> source_data;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
144
    vtkSmartPointer<vtkLineRepresentation> source_line;
145
    vtkSmartPointer<vtkSphereRepresentation> source_sphere;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
146 147 148

public:
    vtkSmartPointer<vtkStreamTracer> streamtracer;
149
    vtkSmartPointer<vtkTubeFilter> tube_filter;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
150 151 152 153
    vtkSmartPointer<vtkPolyDataMapper> mapper;
    vtkSmartPointer<vtkActor> actor;

public:
154 155
    QCheckBox *cb_show_seed = nullptr;
    QComboBox *cb_seed_type = nullptr;
156 157

public:
158 159 160 161
    QComboBox *cb_integrator_direction = nullptr;
    QComboBox *cb_integrator_type = nullptr;
    QSpinBox *sp_integrator_max_steps = nullptr;
    QDoubleSpinBox *sp_integrator_max_lengths = nullptr;
162 163

public:
164 165
    QDoubleSpinBox *sp_radius = nullptr;
    QSpinBox *sp_resolution = nullptr;
166 167

public:
168 169
    QVector<dtkVisualizationWidgetsScalarControl*> sphere_seed_controls;
    QVector<dtkVisualizationWidgetsScalarControl*> line_seed_controls;
170 171 172

public:
    void updateSeedControls(void);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
173 174
};

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
// ///////////////////////////////////////////////////////////////////

void dtkVisualizationDecoratorVectorStreamTracerPrivate::updateSeedControls(void)
{
    const bool is_sphere = (cb_seed_type->currentText() == "Sphere");
    for (auto* sphere_control : sphere_seed_controls) {
        sphere_control->setVisible(is_sphere);
    }
    for (auto* line_control : line_seed_controls) {
        line_control->setVisible(!is_sphere);
    }
    if (is_sphere) {
        source_sphere_observer->Execute(source_sphere_widget, vtkCommand::InteractionEvent, nullptr);
    } else {
        source_line_observer->Execute(source_line_widget, vtkCommand::InteractionEvent, nullptr);
    }
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
193 194 195 196 197 198
// ///////////////////////////////////////////////////////////////////
// dtkVisualizationDecoratorVectorStreamTracer implementation
// ///////////////////////////////////////////////////////////////////

dtkVisualizationDecoratorVectorStreamTracer::dtkVisualizationDecoratorVectorStreamTracer(void): dtkVisualizationDecoratorWithClut(), d(new dtkVisualizationDecoratorVectorStreamTracerPrivate())
{
199
    d->source_data = vtkSmartPointer<vtkPolyData>::New();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
200 201

    d->source_line = vtkSmartPointer<vtkLineRepresentation>::New();
202 203 204 205 206 207
    d->source_line->SetResolution(50);
    d->source_line->GetPolyData(d->source_data);

    d->source_sphere = vtkSmartPointer<vtkSphereRepresentation>::New();
    d->source_sphere->SetPhiResolution(7);
    d->source_sphere->SetThetaResolution(7);
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
208
    d->source_sphere->SetRadius(1.0);
209 210
    d->source_sphere->GetPolyData(d->source_data);
    d->source_sphere->SetRepresentationToWireframe();
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
211
    d->source_sphere->RadialLineOff();
212 213 214 215

    d->source_sphere_widget = vtkSmartPointer<vtkSphereWidget2>::New();
    d->source_sphere_widget->SetEnabled(false);
    d->source_sphere_widget->SetRepresentation(d->source_sphere);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
216 217 218 219 220 221

    d->source_line_widget = vtkSmartPointer<vtkLineWidget2>::New();
    d->source_line_widget->SetEnabled(false);
    d->source_line_widget->SetRepresentation(d->source_line);

    d->streamtracer = vtkSmartPointer<vtkStreamTracer>::New();
222
    d->streamtracer->SetSourceData(d->source_data);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
223 224

    d->streamtracer->SetIntegrationDirectionToBoth();
225
    d->streamtracer->SetIntegratorTypeToRungeKutta4();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
226 227 228 229 230 231
    d->streamtracer->SetIntegrationStepUnit(vtkStreamTracer::Units::CELL_LENGTH_UNIT);
    d->streamtracer->SetInitialIntegrationStep(0.2);
    d->streamtracer->SetMinimumIntegrationStep(0.01);
    d->streamtracer->SetMaximumIntegrationStep(0.5);

    d->streamtracer->SetMaximumNumberOfSteps(2000);
232
    d->streamtracer->SetMaximumPropagation(20); // To be set as the maximum size of the dataset
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
233 234 235 236
    d->streamtracer->SetTerminalSpeed(1.e-12);
    d->streamtracer->SetMaximumError(1.e-6);
    d->streamtracer->SetComputeVorticity(true);

237 238 239 240 241 242
    d->tube_filter = vtkSmartPointer<vtkTubeFilter>::New();
    d->tube_filter->SetInputConnection(d->streamtracer->GetOutputPort());
    d->tube_filter->SetNumberOfSides(8);
    d->tube_filter->SetRadius(.02);
    d->tube_filter->SetVaryRadius(0);

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
243
    d->mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
244
    d->mapper->SetInputConnection(d->tube_filter->GetOutputPort());
245
    d->mapper->SetScalarModeToUsePointFieldData();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
246 247 248 249

    d->actor = vtkSmartPointer<vtkActor>::New();
    d->actor->SetMapper(d->mapper);

250 251 252 253 254 255 256
    d->source_sphere_observer = vtkSmartPointer<dtkVisualizationDecoratorStreamTracerSphereObserver>::New();
    d->source_sphere_observer->source_data = d->source_data;
    d->source_sphere_observer->source_sphere = d->source_sphere;
    d->source_sphere_observer->stream_tracer = d->streamtracer;

    d->source_line_observer = vtkSmartPointer<dtkVisualizationDecoratorStreamTracerLineObserver>::New();
    d->source_line_observer->source_data = d->source_data;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
257 258 259 260
    d->source_line_observer->source_line = d->source_line;
    d->source_line_observer->stream_tracer = d->streamtracer;

    d->source_line_widget->AddObserver(vtkCommand::InteractionEvent, d->source_line_observer);
261
    d->source_sphere_widget->AddObserver(vtkCommand::InteractionEvent, d->source_sphere_observer);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
262 263 264 265

    //////////
    // Inspectors creation and setup

266 267
    d_func()->show_actor_cb->setObjectName("Streamlines");

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    d->cb_show_seed = new QCheckBox;
    d->cb_show_seed->setObjectName("Display");
    d->cb_show_seed->setChecked(true);

    d->cb_seed_type = new QComboBox;
    d->cb_seed_type->addItem("Line");
    d->cb_seed_type->addItem("Sphere");
    d->cb_seed_type->setCurrentIndex(1);
    d->cb_seed_type->setObjectName("Type");

    d->cb_integrator_direction = new QComboBox;
    d->cb_integrator_direction->addItem("Forward");
    d->cb_integrator_direction->addItem("Backward");
    d->cb_integrator_direction->addItem("Both");
    d->cb_integrator_direction->setCurrentIndex(2);
    d->cb_integrator_direction->setObjectName("Direction");

    d->cb_integrator_type = new QComboBox;
    d->cb_integrator_type->addItem("RK2");
    d->cb_integrator_type->addItem("RK4");
    d->cb_integrator_type->addItem("RK45");
289
    d->cb_integrator_type->setCurrentIndex(1);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    d->cb_integrator_type->setObjectName("Type");

    d->sp_integrator_max_steps = new QSpinBox;
    d->sp_integrator_max_steps->setMaximum(99999999);
    d->sp_integrator_max_steps->setValue(2000);
    d->sp_integrator_max_steps->setKeyboardTracking(false);
    d->sp_integrator_max_steps->setObjectName("Maximum Steps");

    d->sp_integrator_max_lengths = new QDoubleSpinBox;
    d->sp_integrator_max_lengths->setMaximum(999999999);
    d->sp_integrator_max_lengths->setValue(20);
    d->sp_integrator_max_lengths->setKeyboardTracking(false);
    d->sp_integrator_max_lengths->setObjectName("Max Stream Length");

    d->sp_radius = new QDoubleSpinBox;
    d->sp_radius->setValue(1.0);
    d->sp_radius->setSingleStep(0.1);
    d->sp_radius->setDecimals(4);
    d->sp_radius->setKeyboardTracking(false);
    d->sp_radius->setObjectName("Tube Radius");

    d->sp_resolution = new QSpinBox;
    d->sp_resolution->setMaximum(10000);
    d->sp_resolution->setValue(50);
    d->sp_resolution->setKeyboardTracking(false);
    d->sp_resolution->setObjectName("Resolution");
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    d->sphere_seed_controls = {
        new dtkVisualizationWidgetsScalarControl("Center X"),
        new dtkVisualizationWidgetsScalarControl("Center Y"),
        new dtkVisualizationWidgetsScalarControl("Center Z"),
        new dtkVisualizationWidgetsScalarPositiveControl("Radius", 1.3),
    };
    d->source_sphere_observer->pos_x = d->sphere_seed_controls[0];
    d->source_sphere_observer->pos_y = d->sphere_seed_controls[1];
    d->source_sphere_observer->pos_z = d->sphere_seed_controls[2];
    d->source_sphere_observer->radius = d->sphere_seed_controls[3];

    d->line_seed_controls = {
        new dtkVisualizationWidgetsScalarControl("Position 1 X"),
        new dtkVisualizationWidgetsScalarControl("Position 1 Y"),
        new dtkVisualizationWidgetsScalarControl("Position 1 Z"),
        new dtkVisualizationWidgetsScalarControl("Position 2 X"),
        new dtkVisualizationWidgetsScalarControl("Position 2 Y"),
        new dtkVisualizationWidgetsScalarControl("Position 2 Z"),
    };
    d->source_line_observer->pos1_x = d->line_seed_controls[0];
    d->source_line_observer->pos1_y = d->line_seed_controls[1];
    d->source_line_observer->pos1_z = d->line_seed_controls[2];
    d->source_line_observer->pos2_x = d->line_seed_controls[3];
    d->source_line_observer->pos2_y = d->line_seed_controls[4];
    d->source_line_observer->pos2_z = d->line_seed_controls[5];
342

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
343 344 345
    //////////
    // Inspectors connections

346
    connect(d->cb_show_seed, &QCheckBox::stateChanged, [=] (int state)
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
347
    {
348
        if (d->cb_seed_type->currentText() == "Sphere") {
349 350 351 352
            d->source_sphere_widget->SetEnabled(state == Qt::Checked);
        } else {
            d->source_line_widget->SetEnabled(state == Qt::Checked);
        }
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
353 354 355
        this->draw();
    });

356
    connect(d->cb_integrator_direction, QOverload<int>::of(&QComboBox::currentIndexChanged), [=] (int id)
357 358 359 360 361
    {
        d->streamtracer->SetIntegrationDirection(id);
        this->draw();
    });

362
    connect(d->cb_integrator_type, QOverload<int>::of(&QComboBox::currentIndexChanged), [=] (int id)
363 364 365 366 367
    {
        d->streamtracer->SetIntegratorType(id);
        this->draw();
    });

368
    connect(d->cb_seed_type, &QComboBox::currentTextChanged, [=] (const QString& text)
369 370 371 372 373 374 375 376
    {
        if (text == "Sphere") {
            d->source_line_widget->SetEnabled(false);
            d->source_sphere_widget->SetEnabled(true);
        } else {
            d->source_sphere_widget->SetEnabled(false);
            d->source_line_widget->SetEnabled(true);
        }
377
        d->updateSeedControls();
378
        this->touch();
379 380 381
        this->draw();
    });

382
    connect(d->sp_resolution, QOverload<int>::of(&QSpinBox::valueChanged), [=] (int val)
383 384 385 386 387 388 389 390 391
    {
        int sqrt_val = std::round(std::sqrt(val));
        d->source_sphere->SetPhiResolution(sqrt_val);
        d->source_sphere->SetThetaResolution(sqrt_val);
        d->source_line->SetResolution(val);
        this->touch();
        this->draw();
    });

392
    connect(d->sp_integrator_max_steps, QOverload<int>::of(&QSpinBox::valueChanged), [=] (int val)
393 394 395 396 397
    {
        d->streamtracer->SetMaximumNumberOfSteps(val);
        this->draw();
    });

398
    connect(d->sp_integrator_max_lengths, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=] (double val)
399 400 401 402 403
    {
        d->streamtracer->SetMaximumPropagation(val);
        this->draw();
    });

404
    connect(d->sp_radius, QOverload<double>::of(&QDoubleSpinBox::valueChanged), [=] (double val)
405 406 407 408 409
    {
        d->tube_filter->SetRadius(val);
        this->draw();
    });

410 411 412
    for (int i=0; i<6; ++i) {
        for (auto* seed_controls : { &d->sphere_seed_controls, &d->line_seed_controls }) {
            if (i >= seed_controls->size()) {
413
                continue;
414
            }
415 416 417 418
            dtkVisualizationWidgetsScalarControl *control = (*seed_controls)[i];
            const bool is_sphere = (seed_controls == &d->sphere_seed_controls);

            connect(control, QOverload<double>::of(&dtkVisualizationWidgetsScalarControl::valueChanged), [=] (double val)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            {
                vtkSmartPointer<vtkPolyData> data = vtkSmartPointer<vtkPolyData>::New();

                if (is_sphere) {
                    if (i == 3) {// radius
                        d->source_sphere->SetRadius(val);
                    } else {
                        double pos[3];
                        d->source_sphere->GetCenter(pos);
                        pos[i] = val;
                        d->source_sphere->SetCenter(pos);
                    }
                    d->source_sphere->GetPolyData(data);
                } else {
                    double pos[3];
                    const int axis = (i % 3);
                    const bool is_point1 = (i < 3);
                    if (is_point1) {
                        d->source_line->GetPoint1WorldPosition(pos);
                    } else {
                        d->source_line->GetPoint2WorldPosition(pos);
                    }
                    pos[axis] = val;
                    if (is_point1) {
                        d->source_line->SetPoint1WorldPosition(pos);
                    } else {
                        d->source_line->SetPoint2WorldPosition(pos);
                    }
                    d->source_line->GetPolyData(data);
                }

                d->streamtracer->SetSourceData(data);
                d->streamtracer->Modified();

                this->draw();
            });
455
        }
456 457
    }

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
458 459
    this->setObjectName("Vector Stream Tracer");

460 461
    QWidget *seed_box = new dtkVisualizationWidgetsCategory("Seed", {
            d->cb_show_seed,
462
            d->cb_seed_type,
463 464 465 466 467 468 469 470 471
            d->sphere_seed_controls[0],
            d->sphere_seed_controls[1],
            d->sphere_seed_controls[2],
            d->sphere_seed_controls[3],
            d->line_seed_controls[0],
            d->line_seed_controls[1],
            d->line_seed_controls[2],
            d->line_seed_controls[3],
            d->line_seed_controls[4],
472
            d->line_seed_controls[5]});
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
473

474 475 476 477 478
    QWidget *integrator_box = new dtkVisualizationWidgetsCategory("Integrator", {
            d->cb_integrator_direction,
            d->cb_integrator_type,
            d->sp_integrator_max_steps,
            d->sp_integrator_max_lengths});
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
479

480 481
    QWidget *styling_box = new dtkVisualizationWidgetsCategory("Styling", {
            d->sp_radius,
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
482 483 484
            d->sp_resolution,
            d_func()->opacity_widget
        });
485

486
    d_func()->inspectors << seed_box
487
                         << integrator_box
488
                         << styling_box;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
489 490 491 492 493 494 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 524 525 526 527
}

dtkVisualizationDecoratorVectorStreamTracer::~dtkVisualizationDecoratorVectorStreamTracer(void)
{
    dtkVisualizationDecoratorVectorStreamTracer::unsetCanvas();

    delete d;

    d = nullptr;
}

void dtkVisualizationDecoratorVectorStreamTracer::restoreSettings(void)
{
    dtkVisualizationDecoratorWithClut::restoreSettings();
}

void dtkVisualizationDecoratorVectorStreamTracer::setData(const QVariant& data)
{
    vtkDataSet *dataset = data.value<vtkDataSet *>();
    if (!dataset) {
        dtkWarn() << Q_FUNC_INFO << "vtkDataSet is expected. Input data is not stored.";
        return;
    }

    d_func()->clear();
    d_func()->retrieveVectorPoints(dataset);
    d_func()->retrieveVectorCells(dataset);

    if (!this->isDecorating()) {
        dtkWarn() << Q_FUNC_INFO << "vtkDataSet has no field to decorate. Nothing is done.";
        d_func()->clear();
        return;
    }

    d_func()->dataset = dataset;
    this->restoreSettings();

    double bounds[6];
    dataset->GetBounds(bounds);
528 529
    vtkBoundingBox box;
    box.SetBounds(bounds);
530
    double center[3]; box.GetCenter(center);
531 532 533
    double pos1[3]; pos1[0] = bounds[0]; pos1[1] = bounds[2]; pos1[2] = bounds[4];
    double pos2[3]; pos2[0] = bounds[1]; pos2[1] = bounds[3]; pos2[2] = bounds[5];

534 535
    d->sp_integrator_max_lengths->blockSignals(true);
    d->sp_integrator_max_lengths->setValue(box.GetDiagonalLength());
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
536
    d->streamtracer->SetMaximumPropagation(box.GetDiagonalLength());
537 538 539 540
    d->sp_integrator_max_lengths->blockSignals(false);

    d->sp_radius->blockSignals(true);
    d->sp_radius->setValue(box.GetDiagonalLength()/500.0);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
541
    d->tube_filter->SetRadius(box.GetDiagonalLength()/500.0);
542
    d->sp_radius->blockSignals(false);
543 544

    d->source_sphere->SetCenter(center);
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
545
    d->source_sphere->SetRadius(box.GetDiagonalLength()/20.0);
546
    d->source_sphere->Modified();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
547 548 549 550 551

    d->source_line->SetPoint1WorldPosition(pos1);
    d->source_line->SetPoint2WorldPosition(pos2);

    d->source_line->Modified();
552 553

    d->streamtracer->SetInputData(d->source_data);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
554 555 556 557 558 559 560
    d->streamtracer->Modified();

    d_func()->sortEligibleFields();
    this->setCurrentFieldName(d_func()->current_field_name);

    if (this->canvas()) {
        this->canvas()->renderer()->AddActor(d->actor);
561
        d->source_sphere_widget->SetInteractor(this->canvas()->renderer()->GetRenderWindow()->GetInteractor());
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
562
        d->source_line_widget->SetInteractor(this->canvas()->renderer()->GetRenderWindow()->GetInteractor());
563 564
        d->source_line_widget->On();
        d->source_sphere_widget->On();
565
        if (d->cb_seed_type->currentText() == "Sphere") {
566
            d->source_line_widget->SetEnabled(false);
567
        } else {
568
            d->source_sphere_widget->SetEnabled(false);
569
        }
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
570 571 572
    }

    d_func()->enableScalarBar();
573 574 575 576 577 578 579 580 581 582 583 584 585

    d->updateSeedControls();

    d->sphere_seed_controls[0]->setResetValue(center[0]);
    d->sphere_seed_controls[1]->setResetValue(center[1]);
    d->sphere_seed_controls[2]->setResetValue(center[2]);
    d->sphere_seed_controls[3]->setResetValue(d->source_sphere->GetRadius());
    d->line_seed_controls[0]->setResetValue(pos1[0]);
    d->line_seed_controls[1]->setResetValue(pos1[1]);
    d->line_seed_controls[2]->setResetValue(pos1[2]);
    d->line_seed_controls[3]->setResetValue(pos2[0]);
    d->line_seed_controls[4]->setResetValue(pos2[1]);
    d->line_seed_controls[5]->setResetValue(pos2[2]);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
}

void dtkVisualizationDecoratorVectorStreamTracer::setCanvas(dtkVisualizationCanvas *canvas)
{
    this->unsetCanvas();

    d_func()->view = dynamic_cast<dtkVisualizationView2D *>(canvas);
    if (!d_func()->view) {
        qWarning() << Q_FUNC_INFO << "View 2D or view 3D expected as canvas. Canvas is reset to nullptr.";
        return;
    }

    if (this->isDecorating()) {
        d_func()->view->renderer()->AddActor(d->actor);
        d->source_line_widget->SetInteractor(this->canvas()->renderer()->GetRenderWindow()->GetInteractor());
601
        d->source_sphere_widget->SetInteractor(this->canvas()->renderer()->GetRenderWindow()->GetInteractor());
602 603
        if (!d_func()->default_visibility) {
            d_func()->default_visibility = true;
604
        } else {
605 606
            d->source_line_widget->On();
            d->source_sphere_widget->On();
607
            if (d->cb_seed_type->currentText() == "Sphere") {
608
                d->source_line_widget->SetEnabled(false);
609
            } else {
610
                d->source_sphere_widget->SetEnabled(false);
611
            }
612
        }
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
613 614 615 616 617 618 619 620 621 622
    }

    d_func()->enableScalarBar();
}

void dtkVisualizationDecoratorVectorStreamTracer::unsetCanvas(void)
{
    if (d_func()->view) {
        d->source_line_widget->Off();
        d->source_line_widget->SetInteractor(nullptr);
623 624
        d->source_sphere_widget->Off();
        d->source_sphere_widget->SetInteractor(nullptr);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
625 626 627 628 629 630 631 632
        d_func()->view->renderer()->RemoveActor(d->actor);
    }

    d_func()->disableScalarBar();

    d_func()->view = nullptr;
}

633
void dtkVisualizationDecoratorVectorStreamTracer::touch(void)
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
634 635 636
{
    dtkVisualizationDecoratorWithClut::touch();

637
    if (d->cb_seed_type->currentText() == "Sphere") {
638 639 640 641 642
        d->source_sphere->GetPolyData(d->source_data);
    } else {
        d->source_line->GetPolyData(d->source_data);
    }
    d->streamtracer->SetSourceData(d->source_data);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
643 644 645 646 647 648 649
    d->streamtracer->Modified();
}

void dtkVisualizationDecoratorVectorStreamTracer::setVisibility(bool visible)
{
    dtkVisualizationDecoratorWithClut::setVisibility(visible);
    d->actor->SetVisibility(visible);
650 651 652 653 654 655
    if (d->cb_show_seed->isChecked()) {
        if (d->cb_seed_type->currentText() == "Sphere") {
            d->source_sphere_widget->SetEnabled(visible);
        } else {
            d->source_line_widget->SetEnabled(visible);
        }
656
    }
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
657 658
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
659 660 661 662 663 664
void dtkVisualizationDecoratorVectorStreamTracer::setOpacity(double opacity)
{
    d->actor->GetProperty()->SetOpacity(opacity);
    d->actor->Modified();
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
bool dtkVisualizationDecoratorVectorStreamTracer::setCurrentFieldName(const QString& field_name)
{
    if (field_name.isEmpty()) {
        dtkWarn() << Q_FUNC_INFO << "Field name is empty, nothing is done.";
        return false;
    }

    if (!d_func()->dataset) {
        dtkWarn() << Q_FUNC_INFO << "Before calling setCurrentFieldName, setDataSet must be called.";
        return false;
    }

    if(!d_func()->eligible_field_names.contains(field_name)) {
        dtkWarn() << Q_FUNC_INFO << "The field name :" << field_name << "that was specified doesn't match any of the eligible scalar field names";
        return false;
    }

    d_func()->current_field_name = field_name;

    using Support = dtkVisualizationDecoratorWithClut::Support;
    int support = d_func()->supports[field_name];
    if(support == Support::Point) {
        d->streamtracer->SetInputData(d_func()->dataset);
        d->streamtracer->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, qPrintable(field_name));
689

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
690
    } else if(support == Support::Cell) {
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
691
        d->streamtracer->SetInputData(d_func()->dataset);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
692 693 694 695 696 697 698 699 700 701 702
        d->streamtracer->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_CELLS, qPrintable(field_name));
    }
    d->streamtracer->Modified();

    return dtkVisualizationDecoratorWithClut::setCurrentFieldName(field_name);
}

void dtkVisualizationDecoratorVectorStreamTracer::setColorMap(const QMap<double, QColor>& new_colormap)
{
    dtkVisualizationDecoratorWithClut::setColorMap(new_colormap);

703 704 705 706 707
    d->mapper->SetLookupTable(d_func()->color_function);
    d->mapper->SelectColorArray(qPrintable(d_func()->current_field_name));
    auto&& range = d_func()->ranges[d_func()->current_field_name];
    d->mapper->SetScalarRange(range[0], range[1]);
    d->mapper->Modified();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
708 709 710 711
}

//
// dtkVisualizationDecoratorVectorStreamTracer.cpp ends here