dtkVisualizationDecoratorVectorStreamTracer.cpp 14.2 KB
Newer Older
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
// dtkVisualizationDecoratorVectorStreamTracer.cpp
//

#include "dtkVisualizationDecoratorVectorStreamTracer.h"

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

#include <dtkLog>

#include <QtGui>
#include <QtWidgets>

#include <vtkActor.h>
#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>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkStreamTracer.h>
#include <vtkTubeFilter.h>

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

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

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

    virtual void Execute(vtkObject *caller, unsigned long event, void *)
    {
        if (event == vtkCommand::InteractionEvent) {
            source_line->GetPolyData(source_line_data);
            stream_tracer->SetSourceData(source_line_data);
            stream_tracer->Modified();
        }
    };

public:
    vtkSmartPointer<vtkPolyData> source_line_data;
    vtkSmartPointer<vtkLineRepresentation> source_line;
    vtkSmartPointer<vtkStreamTracer> stream_tracer;
};

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

class dtkVisualizationDecoratorVectorStreamTracerPrivate
{
public:
    vtkSmartPointer<dtkVisualizationDecoratorStreamTracerObserver> source_line_observer;
    vtkSmartPointer<vtkLineWidget2> source_line_widget;
    vtkSmartPointer<vtkPolyData> source_line_data;
    vtkSmartPointer<vtkLineRepresentation> source_line;

public:
    vtkSmartPointer<vtkStreamTracer> streamtracer;
80
    vtkSmartPointer<vtkTubeFilter> tube_filter;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
81 82 83 84 85 86 87 88 89
    vtkSmartPointer<vtkPolyDataMapper> mapper;
    vtkSmartPointer<vtkActor> actor;

public:
    vtkSmartPointer<vtkCellCenters> cell_centers;

public:
    QCheckBox *show_actor_cb = nullptr;
    QCheckBox *show_source_actor_cb = nullptr;
90 91 92 93 94 95 96 97 98

public:
    QComboBox *cb_integrator_direction = nullptr;
    QComboBox *cb_integrator_type = nullptr;
    QSpinBox *sp_integrator_max_steps = nullptr;
    QDoubleSpinBox *sp_integrator_max_lengths = nullptr;

public:
    QDoubleSpinBox *sp_radius = nullptr;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
};

// ///////////////////////////////////////////////////////////////////
// dtkVisualizationDecoratorVectorStreamTracer implementation
// ///////////////////////////////////////////////////////////////////

dtkVisualizationDecoratorVectorStreamTracer::dtkVisualizationDecoratorVectorStreamTracer(void): dtkVisualizationDecoratorWithClut(), d(new dtkVisualizationDecoratorVectorStreamTracerPrivate())
{

    d->source_line_data = vtkSmartPointer<vtkPolyData> ::New();

    d->source_line = vtkSmartPointer<vtkLineRepresentation>::New();
    d->source_line->SetResolution(1000);
    d->source_line->GetPolyData(d->source_line_data);

    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();
    d->streamtracer->SetSourceData(d->source_line_data);

    d->streamtracer->SetIntegrationDirectionToBoth();
122
    d->streamtracer->SetIntegratorTypeToRungeKutta45();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
123 124 125 126 127 128
    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);
129
    d->streamtracer->SetMaximumPropagation(20); // To be set as the maximum size of the dataset
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
130 131 132 133
    d->streamtracer->SetTerminalSpeed(1.e-12);
    d->streamtracer->SetMaximumError(1.e-6);
    d->streamtracer->SetComputeVorticity(true);

134 135 136 137 138 139
    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
140
    d->mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
141
    d->mapper->SetInputConnection(d->tube_filter->GetOutputPort());
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

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

    d->cell_centers = vtkSmartPointer<vtkCellCenters>::New();

    d->source_line_observer = vtkSmartPointer<dtkVisualizationDecoratorStreamTracerObserver>::New();
    d->source_line_observer->source_line_data = d->source_line_data;
    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);

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

    d->show_actor_cb = new QCheckBox;
    d->show_source_actor_cb = new QCheckBox;

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    d->cb_integrator_direction = new QComboBox;
    d->cb_integrator_direction->addItem(QStringLiteral("Forward"));
    d->cb_integrator_direction->addItem(QStringLiteral("Backward"));
    d->cb_integrator_direction->addItem(QStringLiteral("Both"));
    d->cb_integrator_direction->setCurrentIndex(2);

    d->cb_integrator_type = new QComboBox;
    d->cb_integrator_type->addItem(QStringLiteral("RK2"));
    d->cb_integrator_type->addItem(QStringLiteral("RK4"));
    d->cb_integrator_type->addItem(QStringLiteral("RK45"));
    d->cb_integrator_type->setCurrentIndex(2);

    d->sp_integrator_max_steps = new QSpinBox;
    d->sp_integrator_max_steps->setMaximum(999999);
    d->sp_integrator_max_steps->setValue(2000);
    d->sp_integrator_max_steps->setKeyboardTracking(false);

    d->sp_integrator_max_lengths = new QDoubleSpinBox;
    d->sp_integrator_max_lengths->setMaximum(9999999);
    d->sp_integrator_max_lengths->setValue(20);
    d->sp_integrator_max_lengths->setKeyboardTracking(false);

    d->sp_radius = new QDoubleSpinBox;

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
185
    d->show_source_actor_cb->setChecked(true);
186 187 188 189
    d->sp_radius->setValue(0.01);
    d->sp_radius->setSingleStep(0.1);
    d->sp_radius->setDecimals(4);
    d->sp_radius->setKeyboardTracking(false);
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    //////////
    // Inspectors connections

    connect(d->show_actor_cb, &QCheckBox::stateChanged, [=] (int state)
    {
        this->saveSettings("visibility",state == Qt::Checked);
        this->setVisibility(state == Qt::Checked);
        this->draw();
    });

    connect(d->show_source_actor_cb, &QCheckBox::stateChanged, [=] (int state)
    {
        d->source_line_widget->SetEnabled(state == Qt::Checked);
        this->draw();
    });

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
    connect(d->cb_integrator_direction, QOverload<int>::of(&QComboBox::currentIndexChanged), [=] (int id)
    {
        d->streamtracer->SetIntegrationDirection(id);
        this->draw();
    });

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

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

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

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

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
237 238 239 240
    this->setObjectName("Vector Stream Tracer");

    d->show_actor_cb->setObjectName("Display");
    d->show_source_actor_cb->setObjectName("Display Source");
241 242 243 244 245 246 247 248 249
    d->cb_integrator_direction->setObjectName("Integration Direction");
    d->cb_integrator_type->setObjectName("Integrator Type");
    d->sp_integrator_max_steps->setObjectName("Maximum Steps");
    d->sp_integrator_max_lengths->setObjectName("Maximum Streamline Length");
    d->sp_radius->setObjectName("Tube Radius");

    d_func()->inspectors << d->cb_integrator_direction << d->cb_integrator_type << d->sp_integrator_max_steps
                         << d->sp_integrator_max_lengths << d->sp_radius
                         << d->show_actor_cb << d->show_source_actor_cb;
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 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 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
}

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

    delete d;

    d = nullptr;
}

void dtkVisualizationDecoratorVectorStreamTracer::restoreSettings(void)
{
    if (this->objectName().isEmpty()) {
        return;
    }
    dtkVisualizationDecoratorWithClut::restoreSettings();
    d->actor->SetVisibility(d_func()->default_visibility);
    d->show_actor_cb->blockSignals(true);
    d->show_actor_cb->setChecked(d_func()->default_visibility);
    d->show_actor_cb->blockSignals(false);
}

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();

    d->cell_centers->SetInputData(dataset);

    double bounds[6];
    dataset->GetBounds(bounds);
    double pos1[3]; pos1[0] = bounds[0]; pos1[1] = bounds[1]; pos1[2] = bounds[2];
    double pos2[3]; pos2[0] = bounds[3]; pos2[1] = bounds[4]; pos2[2] = bounds[5];

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

    d->source_line->Modified();
    d->source_line->GetPolyData(d->source_line_data);
    d->streamtracer->SetInputData(d->source_line_data);
    d->streamtracer->Modified();

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

    if (this->canvas()) {
        this->canvas()->renderer()->AddActor(d->actor);
        d->source_line_widget->SetInteractor(this->canvas()->renderer()->GetRenderWindow()->GetInteractor());
        d->source_line_widget->On();
    }

    d_func()->enableScalarBar();
}

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());
        d->source_line_widget->On();
    }

    d_func()->enableScalarBar();
}

void dtkVisualizationDecoratorVectorStreamTracer::unsetCanvas(void)
{
    if (d_func()->view) {
        d->source_line_widget->Off();
        d->source_line_widget->SetInteractor(nullptr);
        d_func()->view->renderer()->RemoveActor(d->actor);
    }

    d_func()->disableScalarBar();

    d_func()->view = nullptr;
}

void dtkVisualizationDecoratorVectorStreamTracer::touch()
{
    dtkVisualizationDecoratorWithClut::touch();

    d->source_line->GetPolyData(d->source_line_data);
    d->streamtracer->SetSourceData(d->source_line_data);
    d->streamtracer->Modified();
}

void dtkVisualizationDecoratorVectorStreamTracer::setVisibility(bool visible)
{
    dtkVisualizationDecoratorWithClut::setVisibility(visible);
    d->actor->SetVisibility(visible);
    d->source_line_widget->SetEnabled(visible);
}

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));
393

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
394 395 396 397 398 399 400 401 402 403 404 405 406
    } else if(support == Support::Cell) {
        d->streamtracer->SetInputConnection(d->cell_centers->GetOutputPort());
        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);

407 408 409 410 411 412 413
    d_func()->color_function->SetVectorModeToMagnitude();
    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->SetScalarModeToUsePointFieldData();
    d->mapper->SetScalarRange(range[0], range[1]);
    d->mapper->Modified();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
414 415 416 417
}

//
// dtkVisualizationDecoratorVectorStreamTracer.cpp ends here