Commit c4fad44b authored by KLOCZKO Thibaud's avatar KLOCZKO Thibaud

Fix crash when no scalar field is available.

parent ce0c7951
......@@ -21,6 +21,8 @@
#include <dtkWidgets/dtkWidgetsOverlayPane>
#include <dtkWidgets/dtkWidgetsOverlayPaneItem>
#include <dtkLog>
#include <QColor>
#include <vtkActor.h>
......@@ -59,10 +61,10 @@ public:
vtkDataSet *dataset = nullptr;
public:
vtkSmartPointer<vtkActor> actor;
vtkSmartPointer<vtkDataSetMapper> mapper;
vtkSmartPointer<vtkPiecewiseFunction> opacity;
vtkSmartPointer<vtkScalarBarActor> scalar_bar;
vtkSmartPointer<vtkActor> actor;
vtkSmartPointer<vtkDataSetMapper> mapper;
vtkSmartPointer<vtkPiecewiseFunction> opacity;
vtkSmartPointer<vtkScalarBarActor> scalar_bar;
public:
QHash<QString, vtkSmartPointer<vtkColorTransferFunction>> color_transfer_functions;
......@@ -96,12 +98,12 @@ dtkVisualizationDecoratorScalarColorMap::~dtkVisualizationDecoratorScalarColorMa
void dtkVisualizationDecoratorScalarColorMap::draw(void)
{
if (!d->dataset || d->current_field_name.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Set dataset and field name before updating actor.";
dtkWarn() << Q_FUNC_INFO << "Set dataset and field name before updating actor.";
return;
}
if(!this->canvas()) {
qWarning() << Q_FUNC_INFO << "No canvas was set, call setCanvas to call draw on a canvas.";
dtkWarn() << Q_FUNC_INFO << "No canvas was set, call setCanvas to call draw on a canvas.";
return;
}
......@@ -121,7 +123,7 @@ void dtkVisualizationDecoratorScalarColorMap::draw(void)
d->actor->Modified();
if(this->canvas()->interactor()) {
if (this->canvas()->interactor()) {
this->canvas()->interactor()->Render();
}
}
......@@ -130,21 +132,21 @@ void dtkVisualizationDecoratorScalarColorMap::setData(const QVariant& data)
{
vtkDataSet *dataset = data.value<vtkDataSet *>();
if (!dataset) {
dtkWarn() << Q_FUNC_INFO << "vtkDataSet is expected. Input data is not stored.";
return;
}
dtkVisualizationDecorator::setData(data);
Q_ASSERT(dataset);
d->dataset = dataset;
d->current_field_name = QString();
d->current_field_name.clear();
d->eligible_field_names.clear();
d->color_transfer_functions.clear();
d->opacity_functions.clear();
d->ranges.clear();
d->supports.clear();
vtkIdType number_of_point_arrays = d->dataset->GetPointData()->GetNumberOfArrays();
vtkIdType number_of_cell_arrays = d->dataset->GetCellData()->GetNumberOfArrays();
for (vtkIdType i = 0; i < number_of_point_arrays; ++i) {
if (d->dataset->GetPointData()->GetArray(i)->GetNumberOfComponents() == 1) {
QString field_name = QString::fromUtf8(d->dataset->GetPointData()->GetArrayName(i));
......@@ -174,6 +176,7 @@ void dtkVisualizationDecoratorScalarColorMap::setData(const QVariant& data)
}
}
vtkIdType number_of_cell_arrays = d->dataset->GetCellData()->GetNumberOfArrays();
for (vtkIdType i = 0; i < number_of_cell_arrays; ++i) {
if (d->dataset->GetCellData()->GetArray(i)->GetNumberOfComponents() == 1) {
QString field_name = QString::fromUtf8(d->dataset->GetCellData()->GetArrayName(i));
......@@ -201,28 +204,32 @@ void dtkVisualizationDecoratorScalarColorMap::setData(const QVariant& data)
}
}
d->eligible_field_names.sort();
if(d->eligible_field_names.size() != 0) {
if (d->eligible_field_names.size() > 0) {
d->eligible_field_names.sort();
this->setCurrentFieldName(d->eligible_field_names.first());
}
d->mapper->SetInputData(d->dataset);
d->mapper->Modified();
}
void dtkVisualizationDecoratorScalarColorMap::setCanvas(dtkVisualizationCanvas *canvas)
{
dtkVisualizationView2D* old = dynamic_cast<dtkVisualizationView2D *>(this->canvas());
dtkVisualizationView2D *old = dynamic_cast<dtkVisualizationView2D *>(this->canvas());
if (old) {
old->renderer()->RemoveActor(d->actor);
}
if (dynamic_cast<dtkVisualizationView2D *>(canvas)) {
dtkVisualizationDecorator::setCanvas(canvas);
this->canvas()->renderer()->AddActor(d->actor);
dtkVisualizationView2D *view = dynamic_cast<dtkVisualizationView2D *>(canvas);
if (!view) {
qWarning() << Q_FUNC_INFO << "View 2D or view 3D expected as canvas. Canvas is reset to nullptr.";
dtkVisualizationDecorator::setCanvas(nullptr);
return;
}
dtkVisualizationDecorator::setCanvas(view);
view->renderer()->AddActor(d->actor);
dtkWidgetsOverlayPane *canvas_pane = canvas->overlay();
canvas_pane->setBound(500);
......@@ -257,12 +264,12 @@ QStringList dtkVisualizationDecoratorScalarColorMap::eligibleFieldNames(void)
return d->eligible_field_names;
}
const QString& dtkVisualizationDecoratorScalarColorMap::currentFieldName(void)
QString dtkVisualizationDecoratorScalarColorMap::currentFieldName(void)
{
return d->current_field_name;
}
const QPair<double, double>& dtkVisualizationDecoratorScalarColorMap::currentRange(void)
QPair<double, double> dtkVisualizationDecoratorScalarColorMap::currentRange(void)
{
return d->ranges[d->current_field_name];
}
......@@ -280,17 +287,17 @@ vtkSmartPointer<vtkPiecewiseFunction> dtkVisualizationDecoratorScalarColorMap::c
bool dtkVisualizationDecoratorScalarColorMap::setCurrentFieldName(const QString& scalar_field_name)
{
if (scalar_field_name.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Scalar field name is empty, nothing is done.";
dtkWarn() << Q_FUNC_INFO << "Scalar field name is empty, nothing is done.";
return false;
}
if (!d->dataset) {
qWarning() << Q_FUNC_INFO << "Before calling setScalarFieldName, setDataSet must be called.";
dtkWarn() << Q_FUNC_INFO << "Before calling setScalarFieldName, setDataSet must be called.";
return false;
}
if(!d->eligible_field_names.contains(scalar_field_name)) {
qWarning() << Q_FUNC_INFO << "The field name :" << scalar_field_name << "that was specified doesn't match any of the eligible scalar field names";
dtkWarn() << Q_FUNC_INFO << "The field name :" << scalar_field_name << "that was specified doesn't match any of the eligible scalar field names";
return false;
}
......@@ -326,10 +333,12 @@ bool dtkVisualizationDecoratorScalarColorMap::setCurrentFieldName(const QString&
return true;
}
void dtkVisualizationDecoratorScalarColorMap::setCurrentColorTransferFunction(vtkSmartPointer<vtkColorTransferFunction> color_function)
{
if (d->current_field_name.isEmpty()) {
return;
}
d->color_transfer_functions[d->current_field_name] = color_function;
d->mapper->SetLookupTable(color_function);
......@@ -348,6 +357,10 @@ void dtkVisualizationDecoratorScalarColorMap::setCurrentColorTransferFunction(vt
void dtkVisualizationDecoratorScalarColorMap::setCurrentOpacityTransferFunction(vtkSmartPointer<vtkPiecewiseFunction> opacity_function)
{
if (d->current_field_name.isEmpty()) {
return;
}
d->opacity_functions[d->current_field_name] = opacity_function;
//Need to be implemented, probably requires modifying the lut...
......@@ -361,6 +374,10 @@ void dtkVisualizationDecoratorScalarColorMap::setCurrentOpacityTransferFunction(
void dtkVisualizationDecoratorScalarColorMap::setCurrentRange(double min, double max)
{
if (d->current_field_name.isEmpty()) {
return;
}
d->ranges[d->current_field_name] = qMakePair(min, max);
d->mapper->SetScalarRange(min, max);
......
......@@ -41,8 +41,8 @@ public:
public:
QStringList eligibleFieldNames(void);
const QString& currentFieldName(void);
const QPair<double, double>& currentRange(void);
QString currentFieldName(void);
QPair<double, double> currentRange(void);
vtkSmartPointer<vtkColorTransferFunction> currentColorTransferFunction(void);
vtkSmartPointer<vtkPiecewiseFunction> currentOpacityTransferFunction(void);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment