ctkVTKLookupTable.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00022 #include <QColor>
00023 #include <QDebug>
00024 
00026 #include "ctkVTKLookupTable.h"
00027 
00029 #include <vtkLookupTable.h>
00030 #include <vtkSmartPointer.h>
00031 
00032 //-----------------------------------------------------------------------------
00033 class ctkVTKLookupTablePrivate: public ctkPrivate<ctkVTKLookupTable>
00034 {
00035 public:
00036   vtkSmartPointer<vtkLookupTable> LookupTable;
00037 };
00038 
00039 //-----------------------------------------------------------------------------
00040 ctkVTKLookupTable::ctkVTKLookupTable(QObject* parentObject)
00041   :ctkTransferFunction(parentObject)
00042 {
00043   CTK_INIT_PRIVATE(ctkVTKLookupTable);
00044 }
00045 
00046 //-----------------------------------------------------------------------------
00047 ctkVTKLookupTable::ctkVTKLookupTable(vtkLookupTable* lookupTable, 
00048                                      QObject* parentObject)
00049   :ctkTransferFunction(parentObject)
00050 {
00051   CTK_INIT_PRIVATE(ctkVTKLookupTable);
00052   this->setLookupTable(lookupTable);
00053 }
00054 
00055 //-----------------------------------------------------------------------------
00056 ctkVTKLookupTable::~ctkVTKLookupTable()
00057 {
00058 }
00059 
00060 //-----------------------------------------------------------------------------
00061 int ctkVTKLookupTable::count()const
00062 {
00063   CTK_D(const ctkVTKLookupTable);
00064   if (d->LookupTable.GetPointer() == 0)
00065     {
00066     Q_ASSERT(d->LookupTable.GetPointer());
00067     return -1;
00068     }
00069   return d->LookupTable->GetNumberOfColors();
00070 }
00071 
00072 //-----------------------------------------------------------------------------
00073 bool ctkVTKLookupTable::isDiscrete()const
00074 {
00075   return true;
00076 }
00077 
00078 //-----------------------------------------------------------------------------
00079 bool ctkVTKLookupTable::isEditable()const
00080 {
00081   return true;
00082 }
00083 
00084 //-----------------------------------------------------------------------------
00085 void ctkVTKLookupTable::range(qreal& minRange, qreal& maxRange)const
00086 {
00087   CTK_D(const ctkVTKLookupTable);
00088   if (d->LookupTable.GetPointer() == 0)
00089     {
00090     Q_ASSERT(d->LookupTable.GetPointer());
00091     minRange = 1.; // set incorrect values
00092     maxRange = 0.;
00093     return;
00094     }
00095   double rangeValues[2];
00096   d->LookupTable->GetTableRange(rangeValues);
00097   minRange = rangeValues[0];
00098   maxRange = rangeValues[1];
00099 }
00100 
00101 //-----------------------------------------------------------------------------
00102 QVariant ctkVTKLookupTable::minValue()const
00103 {
00104   CTK_D(const ctkVTKLookupTable);
00105   if (d->LookupTable.GetPointer() == 0)
00106     {
00107     Q_ASSERT(d->LookupTable.GetPointer());
00108     return QColor();
00109     }
00110   QColor minValue = QColor::fromHsvF(
00111     d->LookupTable->GetHueRange()[0],
00112     d->LookupTable->GetSaturationRange()[0],
00113     d->LookupTable->GetValueRange()[0],
00114     d->LookupTable->GetAlphaRange()[0]);
00115   return minValue;
00116 }
00117 
00118 //-----------------------------------------------------------------------------
00119 QVariant ctkVTKLookupTable::maxValue()const
00120 {
00121   CTK_D(const ctkVTKLookupTable);
00122   if (d->LookupTable.GetPointer() == 0)
00123     {
00124     Q_ASSERT(d->LookupTable.GetPointer());
00125     return QColor();
00126     }
00127   QColor maxValue = QColor::fromHsvF(
00128     d->LookupTable->GetHueRange()[1],
00129     d->LookupTable->GetSaturationRange()[1],
00130     d->LookupTable->GetValueRange()[1],
00131     d->LookupTable->GetAlphaRange()[1]);
00132   return maxValue;
00133 }
00134 
00135 //-----------------------------------------------------------------------------
00136 qreal ctkVTKLookupTable::indexToPos(int index)const
00137 {
00138   CTK_D(const ctkVTKLookupTable);
00139   double* range = d->LookupTable->GetRange();
00140   return range[0] + index * ((range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1));
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 int ctkVTKLookupTable::posToIndex(qreal pos)const
00145 {
00146   CTK_D(const ctkVTKLookupTable);
00147   double* range = d->LookupTable->GetRange();
00148   return (pos - range[0]) / ((range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1));
00149 }
00150 
00151 //-----------------------------------------------------------------------------
00152 ctkControlPoint* ctkVTKLookupTable::controlPoint(int index)const
00153 {
00154   CTK_D(const ctkVTKLookupTable);
00155   ctkControlPoint* cp = new ctkControlPoint();
00156   cp->P.X = this->indexToPos(index);
00157   cp->P.Value = this->value(cp->P.X);
00158   return cp;
00159 }
00160 
00161 //-----------------------------------------------------------------------------
00162 QVariant ctkVTKLookupTable::value(qreal pos)const
00163 {
00164   CTK_D(const ctkVTKLookupTable);
00165   Q_ASSERT(d->LookupTable.GetPointer());
00166   double rgb[3];
00167   d->LookupTable->GetColor(pos, rgb);
00168   double alpha = d->LookupTable->GetOpacity(pos);
00169   return QColor::fromRgbF(rgb[0], rgb[1], rgb[2], alpha);
00170 }
00171 
00172 //-----------------------------------------------------------------------------
00173 int ctkVTKLookupTable::insertControlPoint(const ctkControlPoint& cp)
00174 {
00175   CTK_D(ctkVTKLookupTable);
00176   qDebug() << "ctkVTKLookupTable doesn't support insertControlPoint";
00177   return -1;
00178 }
00179 
00180 //-----------------------------------------------------------------------------
00181 // insert point with value = 0
00182 int ctkVTKLookupTable::insertControlPoint(qreal pos)
00183 {
00184   // nothing
00185   int index = 0;
00186 
00187   return index;
00188 }
00189 //-----------------------------------------------------------------------------
00190 void ctkVTKLookupTable::setControlPointPos(int index, qreal pos)
00191 {
00192   CTK_D(ctkVTKLookupTable);
00193   // TODO, inform that nothing is done here.
00194   qDebug() << "ctkVTKLookupTable doesn't support setControlPointPos";
00195   return;
00196 }
00197 
00198 //-----------------------------------------------------------------------------
00199 void ctkVTKLookupTable::setControlPointValue(int index, const QVariant& value)
00200 {
00201   CTK_D(ctkVTKLookupTable);
00202   Q_ASSERT(value.value<QColor>().isValid());
00203   QColor rgba = value.value<QColor>();
00204   d->LookupTable->SetTableValue(index, rgba.redF(), rgba.greenF(), rgba.blueF(), rgba.alphaF());
00205 }
00206 
00207 //-----------------------------------------------------------------------------
00208 void ctkVTKLookupTable::setLookupTable(vtkLookupTable* lookupTable)
00209 {
00210   CTK_D(ctkVTKLookupTable);
00211   d->LookupTable = lookupTable;
00212   this->qvtkReconnect(d->LookupTable,vtkCommand::ModifiedEvent,
00213                       this, SIGNAL(changed()));
00214   emit changed();
00215 }
00216 
00217 //-----------------------------------------------------------------------------
00218 vtkLookupTable* ctkVTKLookupTable::lookupTable()const
00219 {
00220   CTK_D(const ctkVTKLookupTable);
00221   return d->LookupTable;
00222 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1