ctkVTKColorTransferFunction.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 "ctkVTKColorTransferFunction.h"
00027 
00029 #include <vtkColorTransferFunction.h>
00030 #include <vtkSmartPointer.h>
00031 
00032 class ctkVTKColorTransferFunctionPrivate: public ctkPrivate<ctkVTKColorTransferFunction>
00033 {
00034 public:
00035   vtkSmartPointer<vtkColorTransferFunction> ColorTransferFunction;
00036 };
00037 
00038 //-----------------------------------------------------------------------------
00039 ctkVTKColorTransferFunction::ctkVTKColorTransferFunction(vtkColorTransferFunction* colorTransferFunction, 
00040                                                          QObject* parentObject)
00041   :ctkTransferFunction(parentObject)
00042 {
00043   CTK_INIT_PRIVATE(ctkVTKColorTransferFunction);
00044   this->setColorTransferFunction(colorTransferFunction);
00045 }
00046 
00047 //-----------------------------------------------------------------------------
00048 ctkVTKColorTransferFunction::~ctkVTKColorTransferFunction()
00049 {
00050 }
00051 
00052 //-----------------------------------------------------------------------------
00053 int ctkVTKColorTransferFunction::count()const
00054 {
00055   CTK_D(const ctkVTKColorTransferFunction);
00056   if (d->ColorTransferFunction.GetPointer() == 0)
00057     {
00058     Q_ASSERT(d->ColorTransferFunction.GetPointer());
00059     return -1;
00060     }
00061   return d->ColorTransferFunction->GetSize();
00062 }
00063 
00064 //-----------------------------------------------------------------------------
00065 void ctkVTKColorTransferFunction::range(qreal& minRange, qreal& maxRange)const
00066 {
00067   CTK_D(const ctkVTKColorTransferFunction);
00068   if (d->ColorTransferFunction.GetPointer() == 0)
00069     {
00070     Q_ASSERT(d->ColorTransferFunction.GetPointer());
00071     minRange = 1.;
00072     maxRange = 0.;
00073     return;
00074     }
00075   double rangeValues[2];
00076   d->ColorTransferFunction->GetRange(rangeValues);
00077   minRange = rangeValues[0];
00078   maxRange = rangeValues[1];
00079 }
00080 
00081 //-----------------------------------------------------------------------------
00082 QVariant ctkVTKColorTransferFunction::minValue()const
00083 {
00084   CTK_D(const ctkVTKColorTransferFunction);
00085   if (d->ColorTransferFunction.GetPointer() == 0)
00086     {
00087     Q_ASSERT(d->ColorTransferFunction.GetPointer());
00088     return -1;
00089     }
00090   double rgb[3];
00091   QColor minValue = QColor::fromRgbF(1.,1.,1.);
00092   for (int i = 0; i < this->count(); ++i)
00093     {
00094     d->ColorTransferFunction->GetColor(i, rgb);
00095     Q_ASSERT(rgb[0] >= 0. && rgb[0] <= 1. &&
00096              rgb[1] >= 0. && rgb[1] <= 1. &&
00097              rgb[2] >= 0. && rgb[2] <= 1.);
00098     QColor color = QColor::fromRgbF(rgb[0], rgb[1], rgb[2]);
00099     if ( qGray(color.red(), color.green(), color.blue()) <
00100          qGray(minValue.red(), minValue.green(), minValue.blue()))
00101       {
00102       minValue = color;
00103       }
00104     }
00105   return minValue;
00106 }
00107 
00108 //-----------------------------------------------------------------------------
00109 QVariant ctkVTKColorTransferFunction::maxValue()const
00110 {
00111   CTK_D(const ctkVTKColorTransferFunction);
00112   if (d->ColorTransferFunction.GetPointer() == 0)
00113     {
00114     Q_ASSERT(d->ColorTransferFunction.GetPointer());
00115     return -1;
00116     }
00117   double rgb[3];
00118   QColor minValue = QColor::fromRgbF(0.,0.,0.);
00119   for (int i = 0; i < this->count(); ++i)
00120     {
00121     d->ColorTransferFunction->GetColor(i, rgb);
00122     Q_ASSERT(rgb[0] >= 0. && rgb[0] <= 1. &&
00123              rgb[1] >= 0. && rgb[1] <= 1. &&
00124              rgb[2] >= 0. && rgb[2] <= 1.);
00125     QColor color = QColor::fromRgbF(rgb[0], rgb[1], rgb[2]);
00126     if ( qGray(color.red(), color.green(), color.blue()) >
00127          qGray(minValue.red(), minValue.green(), minValue.blue()))
00128       {
00129       minValue = color;
00130       }
00131     }
00132   return minValue;
00133 }
00134 
00135 //-----------------------------------------------------------------------------
00136 ctkControlPoint* ctkVTKColorTransferFunction::controlPoint(int index)const
00137 {
00138   CTK_D(const ctkVTKColorTransferFunction);
00139   Q_ASSERT(index >= 0 && index < this->count());
00140   double values[6];
00141   d->ColorTransferFunction->GetNodeValue(index, values);
00142   Q_ASSERT(values[0] >= d->ColorTransferFunction->GetRange()[0] &&
00143            values[0] <= d->ColorTransferFunction->GetRange()[1] &&
00144            values[1] >= 0. && values[1] <= 1. &&  // Red
00145            values[2] >= 0. && values[2] <= 1. &&  // Green
00146            values[3] >= 0. && values[3] <= 1. &&  // Blue
00147            values[4] >= 0. && values[4] <= 1. &&  // MidPoint
00148            values[5] >= 0. && values[5] <= 1.);   // Sharpness
00149   QColor rgb = QColor::fromRgbF(values[1], values[2], values[3]);
00150   if (index + 1 >= this->count())
00151     {
00152     ctkControlPoint* cp = new ctkControlPoint();
00153     cp->P.X = values[0];
00154     cp->P.Value = rgb;
00155     return cp;
00156     }
00157   ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
00158   cp->P.X = values[0];
00159   cp->P.Value = rgb;
00160   d->ColorTransferFunction->GetNodeValue(index + 1, values);
00161   Q_ASSERT(values[0] >= d->ColorTransferFunction->GetRange()[0] &&
00162            values[0] <= d->ColorTransferFunction->GetRange()[1] &&
00163            values[1] >= 0. && values[1] <= 1. &&  // Red
00164            values[2] >= 0. && values[2] <= 1. &&  // Green
00165            values[3] >= 0. && values[3] <= 1. &&  // Blue
00166            values[4] >= 0. && values[4] <= 1. &&  // MidPoint
00167            values[5] >= 0. && values[5] <= 1.);   // Sharpness
00168   double subPoints[30];
00169   d->ColorTransferFunction->GetTable(cp->x(), values[0], 10, subPoints);
00170   qreal interval = (values[0] - cp->x()) / 9.;
00171   for(int i = 0; i < 10; ++i)
00172     {
00173     
00174     QColor rgb = QColor::fromRgbF(subPoints[3*i], 
00175                                   subPoints[3*i+1],
00176                                   subPoints[3*i+2]);
00177     cp->SubPoints << ctkPoint(cp->x() + interval*i, rgb);
00178     }
00179   return cp;
00180 }
00181 
00182 //-----------------------------------------------------------------------------
00183 QVariant ctkVTKColorTransferFunction::value(qreal pos)const
00184 {
00185   CTK_D(const ctkVTKColorTransferFunction);
00186   Q_ASSERT(d->ColorTransferFunction.GetPointer());
00187   double rgb[3];
00188   d->ColorTransferFunction->GetColor(pos, rgb);
00189   QColor color = QColor::fromRgbF(rgb[0], rgb[1], rgb[2]);
00190   return color;
00191 }
00192 
00193 //-----------------------------------------------------------------------------
00194 int ctkVTKColorTransferFunction::insertControlPoint(const ctkControlPoint& cp)
00195 {
00196   CTK_D(ctkVTKColorTransferFunction);
00197   int index = -1;
00198   if (d->ColorTransferFunction.GetPointer() == 0)
00199     {
00200     return index;
00201     }
00202   QColor rgb = cp.value().value<QColor>();
00203   const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
00204   if (nonLinearCp)
00205     {
00206     // TODO retrieve midpoint & sharpness
00207     index = d->ColorTransferFunction->AddRGBPoint(
00208       cp.x(), rgb.redF(), rgb.greenF(), rgb.blueF());
00209     }
00210   else
00211     {
00212     index = d->ColorTransferFunction->AddRGBPoint(
00213       cp.x(), rgb.redF(), rgb.greenF(), rgb.blueF());
00214     }
00215   return index;
00216 }
00217 
00218 //-----------------------------------------------------------------------------
00219 void ctkVTKColorTransferFunction::setControlPointPos(int index, qreal pos)
00220 {
00221   CTK_D(ctkVTKColorTransferFunction);
00222   double values[6];
00223   d->ColorTransferFunction->GetNodeValue(index, values);
00224   values[0] = pos;
00225   // warning, a possible new range is not supported
00226   // SetNodeValue eventually fire the signal changed()
00227   d->ColorTransferFunction->SetNodeValue(index, values);
00228 }
00229 
00230 //-----------------------------------------------------------------------------
00231 void ctkVTKColorTransferFunction::setControlPointValue(int index, const QVariant& value)
00232 {
00233   CTK_D(ctkVTKColorTransferFunction);
00234   Q_ASSERT(value.value<QColor>().isValid());
00235   QColor rgb = value.value<QColor>();
00236   double values[6];
00237   d->ColorTransferFunction->GetNodeValue(index, values);
00238   values[1] = rgb.redF();
00239   values[2] = rgb.greenF();
00240   values[3] = rgb.blueF();
00241   // setNodeValue should eventually fired the signal changed()
00242   d->ColorTransferFunction->SetNodeValue(index, values);
00243 }
00244 
00245 //-----------------------------------------------------------------------------
00246 void ctkVTKColorTransferFunction::setColorTransferFunction(vtkColorTransferFunction* colorTransferFunction)
00247 {
00248   CTK_D(ctkVTKColorTransferFunction);
00249   d->ColorTransferFunction = colorTransferFunction;
00250   this->qvtkReconnect(d->ColorTransferFunction,vtkCommand::ModifiedEvent,
00251                       this, SIGNAL(changed()));
00252   emit changed();
00253 }
00254 
00255 //-----------------------------------------------------------------------------
00256 vtkColorTransferFunction* ctkVTKColorTransferFunction::colorTransferFunction()const
00257 {
00258   CTK_D(const ctkVTKColorTransferFunction);
00259   return d->ColorTransferFunction;
00260 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines