Forráskód Böngészése

ENH: Add ctkVTKLookupTable to support vtkLookupTable

Julien Finet 15 éve
szülő
commit
c364d1cbaa

+ 3 - 0
Libs/Visualization/VTK/Core/CMakeLists.txt

@@ -29,6 +29,8 @@ SET(KIT_SRCS
   ctkVTKCommandOptions.h
   ctkVTKConnection.cpp
   ctkVTKConnection.h
+  ctkVTKLookupTable.cpp
+  ctkVTKLookupTable.h
   ctkVTKObject.h
   ctkVTKObjectEventsObserver.cpp
   ctkVTKObjectEventsObserver.h
@@ -38,6 +40,7 @@ SET(KIT_SRCS
 SET(KIT_MOC_SRCS
   ctkVTKColorTransferFunction.h
   ctkVTKConnection.h
+  ctkVTKLookupTable.h
   ctkVTKObjectEventsObserver.h
   )
 

+ 192 - 0
Libs/Visualization/VTK/Core/ctkVTKLookupTable.cpp

@@ -0,0 +1,192 @@
+/*=========================================================================
+
+  Library:   CTK
+ 
+  Copyright (c) 2010  Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ 
+=========================================================================*/
+
+/// Qt includes
+#include <QColor>
+#include <QDebug>
+
+/// CTK includes
+#include "ctkVTKLookupTable.h"
+
+/// VTK includes
+#include <vtkLookupTable.h>
+#include <vtkSmartPointer.h>
+
+//-----------------------------------------------------------------------------
+class ctkVTKLookupTablePrivate: public ctkPrivate<ctkVTKLookupTable>
+{
+public:
+  vtkSmartPointer<vtkLookupTable> LookupTable;
+};
+
+//-----------------------------------------------------------------------------
+ctkVTKLookupTable::ctkVTKLookupTable(QObject* parentObject)
+  :ctkTransferFunction(parentObject)
+{
+  CTK_INIT_PRIVATE(ctkVTKLookupTable);
+}
+
+//-----------------------------------------------------------------------------
+ctkVTKLookupTable::ctkVTKLookupTable(vtkLookupTable* lookupTable, 
+                                     QObject* parentObject)
+  :ctkTransferFunction(parentObject)
+{
+  CTK_INIT_PRIVATE(ctkVTKLookupTable);
+  this->setLookupTable(lookupTable);
+}
+
+//-----------------------------------------------------------------------------
+ctkVTKLookupTable::~ctkVTKLookupTable()
+{
+}
+
+//-----------------------------------------------------------------------------
+int ctkVTKLookupTable::count()const
+{
+  CTK_D(const ctkVTKLookupTable);
+  if (d->LookupTable.GetPointer() == 0)
+    {
+    Q_ASSERT(d->LookupTable.GetPointer());
+    return -1;
+    }
+  return d->LookupTable->GetNumberOfColors();
+}
+
+//-----------------------------------------------------------------------------
+bool ctkVTKLookupTable::isDiscrete()const
+{
+  return true;
+}
+
+//-----------------------------------------------------------------------------
+void ctkVTKLookupTable::range(qreal& minRange, qreal& maxRange)const
+{
+  CTK_D(const ctkVTKLookupTable);
+  if (d->LookupTable.GetPointer() == 0)
+    {
+    Q_ASSERT(d->LookupTable.GetPointer());
+    minRange = 1.; // set incorrect values
+    maxRange = 0.;
+    return;
+    }
+  double rangeValues[2];
+  d->LookupTable->GetTableRange(rangeValues);
+  minRange = rangeValues[0];
+  maxRange = rangeValues[1];
+}
+
+//-----------------------------------------------------------------------------
+QVariant ctkVTKLookupTable::minValue()const
+{
+  CTK_D(const ctkVTKLookupTable);
+  if (d->LookupTable.GetPointer() == 0)
+    {
+    Q_ASSERT(d->LookupTable.GetPointer());
+    return QColor();
+    }
+  QColor minValue = QColor::fromHsvF(
+    d->LookupTable->GetHueRange()[0],
+    d->LookupTable->GetSaturationRange()[0],
+    d->LookupTable->GetValueRange()[0],
+    d->LookupTable->GetAlphaRange()[0]);
+  return minValue;
+}
+
+//-----------------------------------------------------------------------------
+QVariant ctkVTKLookupTable::maxValue()const
+{
+  CTK_D(const ctkVTKLookupTable);
+  if (d->LookupTable.GetPointer() == 0)
+    {
+    Q_ASSERT(d->LookupTable.GetPointer());
+    return QColor();
+    }
+  QColor maxValue = QColor::fromHsvF(
+    d->LookupTable->GetHueRange()[1],
+    d->LookupTable->GetSaturationRange()[1],
+    d->LookupTable->GetValueRange()[1],
+    d->LookupTable->GetAlphaRange()[1]);
+  return maxValue;
+}
+
+//-----------------------------------------------------------------------------
+ctkControlPoint* ctkVTKLookupTable::controlPoint(int index)const
+{
+  CTK_D(const ctkVTKLookupTable);
+  double* range = d->LookupTable->GetRange();
+  ctkControlPoint* cp = new ctkControlPoint();
+  cp->P.X = range[0] + index * (range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1);
+  cp->P.Value = this->value(cp->P.X);
+  return cp;
+}
+
+//-----------------------------------------------------------------------------
+QVariant ctkVTKLookupTable::value(qreal pos)const
+{
+  CTK_D(const ctkVTKLookupTable);
+  Q_ASSERT(d->LookupTable.GetPointer());
+  double rgb[3];
+  d->LookupTable->GetColor(pos, rgb);
+  double alpha = d->LookupTable->GetOpacity(pos);
+  return QColor::fromRgbF(rgb[0], rgb[1], rgb[2], alpha);;
+}
+
+//-----------------------------------------------------------------------------
+int ctkVTKLookupTable::insertControlPoint(const ctkControlPoint& cp)
+{
+  CTK_D(ctkVTKLookupTable);
+  qDebug() << "ctkVTKLookupTable doesn't support insertControlPoint";
+  return -1;
+}
+
+//-----------------------------------------------------------------------------
+void ctkVTKLookupTable::setControlPointPos(int index, qreal pos)
+{
+  CTK_D(ctkVTKLookupTable);
+  // TODO, inform that nothing is done here.
+  qDebug() << "ctkVTKLookupTable doesn't support setControlPointPos";
+  return;
+}
+
+//-----------------------------------------------------------------------------
+void ctkVTKLookupTable::setControlPointValue(int index, const QVariant& value)
+{
+  CTK_D(ctkVTKLookupTable);
+  Q_ASSERT(value.value<QColor>().isValid());
+  QColor rgba = value.value<QColor>();
+  d->LookupTable->SetTableValue(index, rgba.redF(), rgba.greenF(), rgba.blueF(), rgba.alphaF());
+}
+
+//-----------------------------------------------------------------------------
+void ctkVTKLookupTable::setLookupTable(vtkLookupTable* lookupTable)
+{
+  CTK_D(ctkVTKLookupTable);
+  d->LookupTable = lookupTable;
+  this->qvtkReconnect(d->LookupTable,vtkCommand::ModifiedEvent,
+                      this, SIGNAL(changed()));
+  emit changed();
+}
+
+//-----------------------------------------------------------------------------
+vtkLookupTable* ctkVTKLookupTable::lookupTable()const
+{
+  CTK_D(const ctkVTKLookupTable);
+  return d->LookupTable;
+}

+ 64 - 0
Libs/Visualization/VTK/Core/ctkVTKLookupTable.h

@@ -0,0 +1,64 @@
+/*=========================================================================
+
+  Library:   CTK
+ 
+  Copyright (c) 2010  Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ 
+=========================================================================*/
+
+#ifndef __ctkVTKLookupTable_h
+#define __ctkVTKLookupTable_h
+
+// CTK includes
+#include "ctkTransferFunction.h"
+#include "ctkPimpl.h"
+#include "CTKVisualizationVTKCoreExport.h"
+#include "ctkVTKObject.h"
+
+class vtkLookupTable;
+class ctkVTKLookupTablePrivate;
+
+///
+/// Transfer function for a vtkColorTransferFunction. 
+/// The value is an RGB QColor (no alpha supported)
+class CTK_VISUALIZATION_VTK_CORE_EXPORT ctkVTKLookupTable: public ctkTransferFunction
+{
+  Q_OBJECT;
+  QVTK_OBJECT;
+public:
+  ctkVTKLookupTable(QObject* parent = 0);
+  ctkVTKLookupTable(vtkLookupTable* lookupTable, QObject* parent = 0);
+  virtual ~ctkVTKLookupTable();
+  
+  virtual ctkControlPoint* controlPoint(int index)const;
+  virtual QVariant value(qreal pos)const;
+  virtual int count()const;
+  virtual bool isDiscrete()const;
+
+  virtual void range(qreal& minRange, qreal& maxRange)const;
+  virtual QVariant minValue()const;
+  virtual QVariant maxValue()const;
+
+  virtual int insertControlPoint(const ctkControlPoint& cp);
+  virtual void setControlPointPos(int index, qreal pos);
+  virtual void setControlPointValue(int index, const QVariant& value);
+
+  void setLookupTable(vtkLookupTable* lookupTable);
+  vtkLookupTable* lookupTable()const;
+private:
+  CTK_DECLARE_PRIVATE(ctkVTKLookupTable);
+};
+
+#endif

+ 2 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/CMakeLists.txt

@@ -2,6 +2,7 @@ SET(KIT ${PROJECT_NAME})
 
 CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
   ctkTransferFunctionWidgetTest1.cpp
+  ctkTransferFunctionWidgetTest2.cpp
   #EXTRA_INCLUDE TestingMacros.h
   )
 
@@ -29,3 +30,4 @@ ENDMACRO( SIMPLE_TEST  )
 
 #SIMPLE_TEST( ctkVTKObjectTest1 )
 SIMPLE_TEST( ctkTransferFunctionWidgetTest1 )
+SIMPLE_TEST( ctkTransferFunctionWidgetTest2 )

+ 64 - 0
Libs/Visualization/VTK/Widgets/Testing/Cpp/ctkTransferFunctionWidgetTest2.cpp

@@ -0,0 +1,64 @@
+/*=========================================================================
+
+  Library:   CTK
+ 
+  Copyright (c) 2010  Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ 
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QSharedPointer>
+#include <QTimer>
+
+// CTK includes
+#include "ctkTransferFunction.h"
+#include "ctkTransferFunctionWidget.h"
+#include "ctkVTKLookupTable.h"
+
+// VTK includes
+#include <vtkLookupTable.h>
+#include <vtkSmartPointer.h>
+
+// STD includes
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkTransferFunctionWidgetTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  
+  vtkSmartPointer<vtkLookupTable> ctf = 
+    vtkSmartPointer<vtkLookupTable>::New();
+
+  ctf->SetNumberOfTableValues(32);
+  ctf->SetHueRange(0.,1.);
+  ctf->SetSaturationRange(1.,1.);
+  ctf->SetValueRange(1.,1.);
+  ctf->SetAlphaRange(1.,1.);
+  ctf->Build();
+
+  QSharedPointer<ctkTransferFunction> transferFunction = 
+    QSharedPointer<ctkTransferFunction>(new ctkVTKLookupTable(ctf));
+  ctkTransferFunctionWidget transferFunctionWidget(transferFunction.data(), 0);
+  // the widget is not really shown here, only when app.exec() is called
+  transferFunctionWidget.show();
+
+  QTimer autoExit;
+  QObject::connect(&autoExit, SIGNAL(timeout()), &app, SLOT(quit()));
+  autoExit.start(100000);
+
+  return app.exec();
+}