Explorar o código

Set axes to their bounds when adding scalarstocolors plots

Julien Finet %!s(int64=14) %!d(string=hai) anos
pai
achega
92434715c4

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsViewTest1.cpp

@@ -47,7 +47,7 @@ int ctkVTKScalarsToColorsViewTest1(int argc, char * argv [] )
 
   ctkVTKScalarsToColorsView view(0);
   view.addColorTransferFunction(ctf);
-  view.fitAxesToBounds();
+  view.setAxesToChartBounds();
   view.show();
 
   ctf->AddRGBPoint(0.6, 0.9686,0.0,0.6941);

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsViewTest2.cpp

@@ -110,7 +110,7 @@ int ctkVTKScalarsToColorsViewTest2(int argc, char * argv [] )
   // add histogram item
   view.addPlot(histogramPlot);
   view.chart()->SetBarWidthFraction(1.);
-  view.fitAxesToBounds();
+  view.setAxesToChartBounds();
   view.show();
 
   if (argc < 2 || QString(argv[1]) != "-I")

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsViewTest3.cpp

@@ -63,7 +63,7 @@ int ctkVTKScalarsToColorsViewTest3(int argc, char * argv [] )
   ctkVTKScalarsToColorsView view(0);
   // add transfer function item
   view.addCompositeFunction(ctf, opacityFunction);
-  view.fitAxesToBounds();
+  view.setAxesToChartBounds();
   view.show();
 
   if (argc < 2 || QString(argv[1]) != "-I")

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsWidgetTest1.cpp

@@ -55,7 +55,7 @@ int ctkVTKScalarsToColorsWidgetTest1(int argc, char * argv [] )
   // add transfer function item
   vtkPlot* plot = widget.view()->addOpacityFunction(opacityFunction);
   plot->SetColor(0, 67,  247, 255);
-  widget.view()->fitAxesToBounds();
+  widget.view()->setAxesToChartBounds();
   widget.show();
 
   if (argc < 2 || QString(argv[1]) != "-I")

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsWidgetTest2.cpp

@@ -64,7 +64,7 @@ int ctkVTKScalarsToColorsWidgetTest2(int argc, char * argv [] )
   ctkVTKScalarsToColorsWidget widget(0);
   // add transfer function item
   widget.view()->addCompositeFunction(ctf, opacityFunction);
-  widget.view()->fitAxesToBounds();
+  widget.view()->setAxesToChartBounds();
   widget.show();
 
   if (argc < 2 || QString(argv[1]) != "-I")

+ 1 - 1
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkVTKScalarsToColorsWidgetTest3.cpp

@@ -56,7 +56,7 @@ int ctkVTKScalarsToColorsWidgetTest3(int argc, char * argv [] )
   ctkVTKScalarsToColorsWidget widget(0);
   // add transfer function item
   widget.view()->addColorTransferFunction(ctf);
-  widget.view()->fitAxesToBounds();
+  widget.view()->setAxesToChartBounds();
   widget.show();
 
   if (argc < 2 || QString(argv[1]) != "-I")

+ 72 - 41
Libs/Visualization/VTK/Widgets/ctkVTKChartView.cpp

@@ -19,6 +19,7 @@
 =========================================================================*/
 
 // Qt includes
+#include <QDebug>
 #include <QMouseEvent>
 
 // CTK includes
@@ -131,95 +132,125 @@ void ctkVTKChartView::addPlot(vtkPlot* plot)
   Q_D(ctkVTKChartView);
   d->Chart->AddPlot(plot);
   emit this->plotAdded(plot);
+  this->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------
-void ctkVTKChartView::fitAxesToBounds()
+void ctkVTKChartView::onChartUpdated()
 {
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::chartBounds(double* bounds)
+{
+  bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
+  bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
   vtkChartXY* chart = this->chart();
   const vtkIdType plotCount = chart->GetNumberOfPlots();
-  double extremaBounds[8] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
-                             VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
-                             VTK_DOUBLE_MAX, VTK_DOUBLE_MIN};
   for (vtkIdType i = 0; i < plotCount; ++i)
     {
     vtkPlot* plot = chart->GetPlot(i);
 
     int corner = chart->GetPlotCorner(plot);
-    double bounds[4];
-    plot->GetBounds(bounds);
+    double plotBounds[4];
+    plot->GetBounds(plotBounds);
     switch (corner)
       {
       // bottom left
       case 0:
         // x
-        extremaBounds[2] = extremaBounds[2] > bounds[0] ?
-          bounds[0] : extremaBounds[2];
-        extremaBounds[3] = extremaBounds[3] < bounds[1] ?
-          bounds[1] : extremaBounds[3];
+        bounds[2] = bounds[2] > plotBounds[0] ?
+          plotBounds[0] : bounds[2];
+        bounds[3] = bounds[3] < plotBounds[1] ?
+          plotBounds[1] : bounds[3];
         // y
-        extremaBounds[0] = extremaBounds[0] > bounds[2] ?
-          bounds[2] : extremaBounds[0];
-        extremaBounds[1] = extremaBounds[1] < bounds[3] ?
-          bounds[3] : extremaBounds[1];
+        bounds[0] = bounds[0] > plotBounds[2] ?
+          plotBounds[2] : bounds[0];
+        bounds[1] = bounds[1] < plotBounds[3] ?
+          plotBounds[3] : bounds[1];
         break;
       // bottom right
       case 1:
         // x
-        extremaBounds[2] = extremaBounds[2] > bounds[0] ?
-          bounds[0] : extremaBounds[2];
-        extremaBounds[3] = extremaBounds[3] < bounds[1] ?
-          bounds[1] : extremaBounds[3];
+        bounds[2] = bounds[2] > plotBounds[0] ?
+          plotBounds[0] : bounds[2];
+        bounds[3] = bounds[3] < plotBounds[1] ?
+          plotBounds[1] : bounds[3];
         // y
-        extremaBounds[4] = extremaBounds[4] > bounds[2] ?
-          bounds[2] : extremaBounds[4];
-        extremaBounds[5] = extremaBounds[5] < bounds[3] ?
-          bounds[3] : extremaBounds[5];
+        bounds[4] = bounds[4] > plotBounds[2] ?
+          plotBounds[2] : bounds[4];
+        bounds[5] = bounds[5] < plotBounds[3] ?
+          plotBounds[3] : bounds[5];
         break;
       // top right
       case 2:
         // x
-        extremaBounds[6] = extremaBounds[6] > bounds[0] ?
-          bounds[0] : extremaBounds[6];
-        extremaBounds[7] = extremaBounds[7] < bounds[1] ?
-          bounds[1] : extremaBounds[7];
+        bounds[6] = bounds[6] > plotBounds[0] ?
+          plotBounds[0] : bounds[6];
+        bounds[7] = bounds[7] < plotBounds[1] ?
+          plotBounds[1] : bounds[7];
         // y
-        extremaBounds[4] = extremaBounds[4] > bounds[2] ?
-          bounds[2] : extremaBounds[4];
-        extremaBounds[5] = extremaBounds[5] < bounds[3] ?
-          bounds[3] : extremaBounds[5];
+        bounds[4] = bounds[4] > plotBounds[2] ?
+          plotBounds[2] : bounds[4];
+        bounds[5] = bounds[5] < plotBounds[3] ?
+          plotBounds[3] : bounds[5];
         break;
       // top left
       case 3:
         // x
-        extremaBounds[6] = extremaBounds[6] > bounds[0] ?
-          bounds[0] : extremaBounds[6];
-        extremaBounds[7] = extremaBounds[7] < bounds[1] ?
-          bounds[1] : extremaBounds[7];
+        bounds[6] = bounds[6] > plotBounds[0] ?
+          plotBounds[0] : bounds[6];
+        bounds[7] = bounds[7] < plotBounds[1] ?
+          plotBounds[1] : bounds[7];
         // y
-        extremaBounds[0] = extremaBounds[0] > bounds[2] ?
-          bounds[2] : extremaBounds[1];
-        extremaBounds[1] = extremaBounds[0] < bounds[3] ?
-          bounds[3] : extremaBounds[1];
+        bounds[0] = bounds[0] > plotBounds[2] ?
+          plotBounds[2] : bounds[1];
+        bounds[1] = bounds[0] < plotBounds[3] ?
+          plotBounds[3] : bounds[1];
         break;
       }
     }
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::setAxesToChartBounds()
+{
+  vtkChartXY* chart = this->chart();
+  double bounds[8];
+  this->chartBounds(bounds);
   for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
     {
-    if (extremaBounds[2*i] != VTK_DOUBLE_MAX)
+    if (bounds[2*i] != VTK_DOUBLE_MAX)
       {
-      chart->GetAxis(i)->SetRange(extremaBounds[2*i], extremaBounds[2*i+1]);
+      chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);
       chart->GetAxis(i)->SetBehavior(1);
       }
     }
 }
 
 // ----------------------------------------------------------------------------
+void ctkVTKChartView::boundAxesToChartBounds()
+{
+  vtkChartXY* chart = this->chart();
+  double bounds[8];
+  this->chartBounds(bounds);
+  for (int i = 0; i < chart->GetNumberOfAxes(); ++i)
+    {
+    if (bounds[2*i] != VTK_DOUBLE_MAX)
+      {
+      chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);
+      chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);
+      }
+    }
+  emit boundsChanged();
+}
+
+// ----------------------------------------------------------------------------
 void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event)
 {
   if (event->button() == Qt::MidButton)
     {
-    this->fitAxesToBounds();
+    this->setAxesToChartBounds();
     }
   this->QVTKWidget::mouseDoubleClickEvent(event);
 }

+ 6 - 1
Libs/Visualization/VTK/Widgets/ctkVTKChartView.h

@@ -54,14 +54,19 @@ public:
   QString title()const;
   void setTitle(const QString& title);
 
-  void fitAxesToBounds();
+  /// Compute the bounds for the 4 chart axes
+  void chartBounds(double* bounds);
+  void setAxesToChartBounds();
+  void boundAxesToChartBounds();
 
 signals:
   void plotAdded(vtkPlot* plot);
+  void boundsChanged();
 
 protected:
   QScopedPointer<ctkVTKChartViewPrivate> d_ptr;
   virtual void mouseDoubleClickEvent(QMouseEvent* event);
+  virtual void onChartUpdated();
 
 private:
   Q_DECLARE_PRIVATE(ctkVTKChartView);

+ 14 - 17
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsView.cpp

@@ -51,7 +51,7 @@ protected:
 public:
   ctkVTKScalarsToColorsViewPrivate(ctkVTKScalarsToColorsView& object);
   void init();
-  void updateChart();
+  void updateBounds();
 };
 
 // ----------------------------------------------------------------------------
@@ -73,15 +73,8 @@ void ctkVTKScalarsToColorsViewPrivate::init()
   for (int i = 0; i < 4; ++i)
     {
     chart->GetAxis(i)->SetVisible(false);
-    chart->GetAxis(i)->SetMinimumLimit(0.);
-    chart->GetAxis(i)->SetMaximumLimit(1.);
     }
-}
-
-// ----------------------------------------------------------------------------
-void ctkVTKScalarsToColorsViewPrivate::updateChart()
-{
-
+  q->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------
@@ -113,14 +106,20 @@ void ctkVTKScalarsToColorsView::addPlot(vtkPlot* plot)
 }
 
 // ----------------------------------------------------------------------------
+void ctkVTKScalarsToColorsView::onChartUpdated()
+{
+  this->Superclass::onChartUpdated();
+  this->boundAxesToChartBounds();
+  this->setAxesToChartBounds();
+}
+
+// ----------------------------------------------------------------------------
 vtkPlot* ctkVTKScalarsToColorsView::addLookupTable(vtkLookupTable* lut)
 {
-  Q_D(ctkVTKScalarsToColorsView);
   vtkSmartPointer<vtkLookupTableItem> item =
     vtkSmartPointer<vtkLookupTableItem>::New();
   item->SetLookupTable(lut);
   this->addPlot(item);
-  d->updateChart();
   return item;
 }
 // ----------------------------------------------------------------------------
@@ -128,7 +127,6 @@ vtkPlot* ctkVTKScalarsToColorsView
 ::addColorTransferFunction(vtkColorTransferFunction* colorTF,
                            bool editable)
 {
-  Q_D(ctkVTKScalarsToColorsView);
   vtkSmartPointer<vtkColorTransferFunctionItem> item =
     vtkSmartPointer<vtkColorTransferFunctionItem>::New();
   item->SetColorTransferFunction(colorTF);
@@ -137,7 +135,6 @@ vtkPlot* ctkVTKScalarsToColorsView
     {
     this->addColorTransferFunctionControlPoints(colorTF);
     }
-  d->updateChart();
   return item;
 }
 
@@ -154,7 +151,6 @@ vtkPlot* ctkVTKScalarsToColorsView
 ::addPiecewiseFunction(vtkPiecewiseFunction* piecewiseTF,
                        bool editable)
 {
-  Q_D(ctkVTKScalarsToColorsView);
   vtkSmartPointer<vtkPiecewiseFunctionItem> item =
     vtkSmartPointer<vtkPiecewiseFunctionItem>::New();
   item->SetPiecewiseFunction(piecewiseTF);
@@ -166,7 +162,6 @@ vtkPlot* ctkVTKScalarsToColorsView
     {
     this->addPiecewiseFunctionControlPoints(piecewiseTF);
     }
-  d->updateChart();
   return item;
 }
 
@@ -176,7 +171,6 @@ vtkPlot* ctkVTKScalarsToColorsView
                        vtkPiecewiseFunction* opacityTF,
                        bool colorTFEditable, bool opacityTFEditable)
 {
-  Q_D(ctkVTKScalarsToColorsView);
   vtkSmartPointer<vtkCompositeTransferFunctionItem> item =
     vtkSmartPointer<vtkCompositeTransferFunctionItem>::New();
   item->SetColorTransferFunction(colorTF);
@@ -195,7 +189,6 @@ vtkPlot* ctkVTKScalarsToColorsView
     {
     this->addOpacityFunctionControlPoints(opacityTF);
     }
-  d->updateChart();
   return item;
 }
 
@@ -384,6 +377,7 @@ void ctkVTKScalarsToColorsView::setLookuptTableToPlots(vtkLookupTable* lut)
     {
     plot->SetLookupTable(lut);
     }
+  this->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------
@@ -400,6 +394,7 @@ void ctkVTKScalarsToColorsView
     {
     plot->SetColorTransferFunction(colorTF);
     }
+  this->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------
@@ -417,6 +412,7 @@ void ctkVTKScalarsToColorsView
     {
     plot->SetOpacityFunction(opacityTF);
     }
+  this->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------
@@ -433,6 +429,7 @@ void ctkVTKScalarsToColorsView
     {
     plot->SetPiecewiseFunction(piecewiseTF);
     }
+  this->onChartUpdated();
 }
 
 // ----------------------------------------------------------------------------

+ 3 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsView.h

@@ -45,6 +45,7 @@ public:
   virtual ~ctkVTKScalarsToColorsView();
 
   virtual void addPlot(vtkPlot* plot);
+
   vtkPlot* addLookupTable(vtkLookupTable* lut);
   vtkPlot* addColorTransferFunction(vtkColorTransferFunction* colorTF, bool editable = true);
   vtkPlot* addOpacityFunction(vtkPiecewiseFunction* opacityTF, bool editable = true);
@@ -81,6 +82,8 @@ public slots:
 protected:
   QScopedPointer<ctkVTKScalarsToColorsViewPrivate> d_ptr;
 
+  virtual void onChartUpdated();
+
 private:
   Q_DECLARE_PRIVATE(ctkVTKScalarsToColorsView);
   Q_DISABLE_COPY(ctkVTKScalarsToColorsView);

+ 15 - 1
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsWidget.cpp

@@ -76,6 +76,9 @@ void ctkVTKScalarsToColorsWidgetPrivate::setupUi(QWidget* widget)
   this->Ui_ctkVTKScalarsToColorsWidget::setupUi(widget);
   QObject::connect(this->View, SIGNAL(plotAdded(vtkPlot*)),
                    q, SLOT(onPlotAdded(vtkPlot*)));
+  QObject::connect(this->View, SIGNAL(boundsChanged()),
+                   q, SLOT(onBoundsChanged()));
+
   this->PointIdSpinBox->setSpecialValueText("None");
   QObject::connect(this->PointIdSpinBox, SIGNAL(valueChanged(int)),
                    q, SLOT(onCurrentPointChanged(int)));
@@ -167,6 +170,18 @@ void ctkVTKScalarsToColorsWidget::onPlotAdded(vtkPlot* plot)
 }
 
 // ----------------------------------------------------------------------------
+void ctkVTKScalarsToColorsWidget::onBoundsChanged()
+{
+  Q_D(ctkVTKScalarsToColorsWidget);
+  double bounds[8];
+  d->View->chartBounds(bounds);
+  d->XRangeSlider->setMinimum(bounds[vtkAxis::BOTTOM * 2]);
+  d->XRangeSlider->setMaximum(bounds[vtkAxis::BOTTOM * 2 + 1]);
+  d->YRangeSlider->setMinimum(bounds[vtkAxis::LEFT * 2]);
+  d->YRangeSlider->setMaximum(bounds[vtkAxis::LEFT * 2 + 1]);
+}
+
+// ----------------------------------------------------------------------------
 void ctkVTKScalarsToColorsWidget::setCurrentPoint(vtkObject* caller, void* callData)
 {
   vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
@@ -374,7 +389,6 @@ void ctkVTKScalarsToColorsWidget::onYRangeChanged(double min, double max)
 void ctkVTKScalarsToColorsWidget::onAxesModified()
 {
   Q_D(ctkVTKScalarsToColorsWidget);
-  bool modified = false;
   vtkAxis* xAxis = d->CurrentControlPointsItem ?
     d->CurrentControlPointsItem->GetXAxis() : d->View->chart()->GetAxis(vtkAxis::BOTTOM);
   Q_ASSERT(xAxis);

+ 1 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsWidget.h

@@ -59,6 +59,7 @@ public slots:
 
 protected slots:
   void onPlotAdded(vtkPlot*);
+  void onBoundsChanged();
   void setCurrentPoint(vtkObject* controlPointsItem, void* pointId);
   void updateCurrentPoint();
   void onCurrentPointChanged(int pointId);