ctkVTKLookupTable.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. /// Qt includes
  15. #include <QColor>
  16. #include <QDebug>
  17. /// CTK includes
  18. #include "ctkVTKLookupTable.h"
  19. /// VTK includes
  20. #include <vtkLookupTable.h>
  21. #include <vtkSmartPointer.h>
  22. //-----------------------------------------------------------------------------
  23. class ctkVTKLookupTablePrivate: public ctkPrivate<ctkVTKLookupTable>
  24. {
  25. public:
  26. vtkSmartPointer<vtkLookupTable> LookupTable;
  27. };
  28. //-----------------------------------------------------------------------------
  29. ctkVTKLookupTable::ctkVTKLookupTable(QObject* parentObject)
  30. :ctkTransferFunction(parentObject)
  31. {
  32. CTK_INIT_PRIVATE(ctkVTKLookupTable);
  33. }
  34. //-----------------------------------------------------------------------------
  35. ctkVTKLookupTable::ctkVTKLookupTable(vtkLookupTable* lookupTable,
  36. QObject* parentObject)
  37. :ctkTransferFunction(parentObject)
  38. {
  39. CTK_INIT_PRIVATE(ctkVTKLookupTable);
  40. this->setLookupTable(lookupTable);
  41. }
  42. //-----------------------------------------------------------------------------
  43. ctkVTKLookupTable::~ctkVTKLookupTable()
  44. {
  45. }
  46. //-----------------------------------------------------------------------------
  47. int ctkVTKLookupTable::count()const
  48. {
  49. CTK_D(const ctkVTKLookupTable);
  50. if (d->LookupTable.GetPointer() == 0)
  51. {
  52. Q_ASSERT(d->LookupTable.GetPointer());
  53. return -1;
  54. }
  55. return d->LookupTable->GetNumberOfColors();
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool ctkVTKLookupTable::isDiscrete()const
  59. {
  60. return true;
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkVTKLookupTable::range(qreal& minRange, qreal& maxRange)const
  64. {
  65. CTK_D(const ctkVTKLookupTable);
  66. if (d->LookupTable.GetPointer() == 0)
  67. {
  68. Q_ASSERT(d->LookupTable.GetPointer());
  69. minRange = 1.; // set incorrect values
  70. maxRange = 0.;
  71. return;
  72. }
  73. double rangeValues[2];
  74. d->LookupTable->GetTableRange(rangeValues);
  75. minRange = rangeValues[0];
  76. maxRange = rangeValues[1];
  77. }
  78. //-----------------------------------------------------------------------------
  79. QVariant ctkVTKLookupTable::minValue()const
  80. {
  81. CTK_D(const ctkVTKLookupTable);
  82. if (d->LookupTable.GetPointer() == 0)
  83. {
  84. Q_ASSERT(d->LookupTable.GetPointer());
  85. return QColor();
  86. }
  87. QColor minValue = QColor::fromHsvF(
  88. d->LookupTable->GetHueRange()[0],
  89. d->LookupTable->GetSaturationRange()[0],
  90. d->LookupTable->GetValueRange()[0],
  91. d->LookupTable->GetAlphaRange()[0]);
  92. return minValue;
  93. }
  94. //-----------------------------------------------------------------------------
  95. QVariant ctkVTKLookupTable::maxValue()const
  96. {
  97. CTK_D(const ctkVTKLookupTable);
  98. if (d->LookupTable.GetPointer() == 0)
  99. {
  100. Q_ASSERT(d->LookupTable.GetPointer());
  101. return QColor();
  102. }
  103. QColor maxValue = QColor::fromHsvF(
  104. d->LookupTable->GetHueRange()[1],
  105. d->LookupTable->GetSaturationRange()[1],
  106. d->LookupTable->GetValueRange()[1],
  107. d->LookupTable->GetAlphaRange()[1]);
  108. return maxValue;
  109. }
  110. //-----------------------------------------------------------------------------
  111. ctkControlPoint* ctkVTKLookupTable::controlPoint(int index)const
  112. {
  113. CTK_D(const ctkVTKLookupTable);
  114. double* range = d->LookupTable->GetRange();
  115. ctkControlPoint* cp = new ctkControlPoint();
  116. cp->P.X = range[0] + index * (range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1);
  117. cp->P.Value = this->value(cp->P.X);
  118. return cp;
  119. }
  120. //-----------------------------------------------------------------------------
  121. QVariant ctkVTKLookupTable::value(qreal pos)const
  122. {
  123. CTK_D(const ctkVTKLookupTable);
  124. Q_ASSERT(d->LookupTable.GetPointer());
  125. double rgb[3];
  126. d->LookupTable->GetColor(pos, rgb);
  127. double alpha = d->LookupTable->GetOpacity(pos);
  128. return QColor::fromRgbF(rgb[0], rgb[1], rgb[2], alpha);;
  129. }
  130. //-----------------------------------------------------------------------------
  131. int ctkVTKLookupTable::insertControlPoint(const ctkControlPoint& cp)
  132. {
  133. CTK_D(ctkVTKLookupTable);
  134. qDebug() << "ctkVTKLookupTable doesn't support insertControlPoint";
  135. return -1;
  136. }
  137. //-----------------------------------------------------------------------------
  138. void ctkVTKLookupTable::setControlPointPos(int index, qreal pos)
  139. {
  140. CTK_D(ctkVTKLookupTable);
  141. // TODO, inform that nothing is done here.
  142. qDebug() << "ctkVTKLookupTable doesn't support setControlPointPos";
  143. return;
  144. }
  145. //-----------------------------------------------------------------------------
  146. void ctkVTKLookupTable::setControlPointValue(int index, const QVariant& value)
  147. {
  148. CTK_D(ctkVTKLookupTable);
  149. Q_ASSERT(value.value<QColor>().isValid());
  150. QColor rgba = value.value<QColor>();
  151. d->LookupTable->SetTableValue(index, rgba.redF(), rgba.greenF(), rgba.blueF(), rgba.alphaF());
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ctkVTKLookupTable::setLookupTable(vtkLookupTable* lookupTable)
  155. {
  156. CTK_D(ctkVTKLookupTable);
  157. d->LookupTable = lookupTable;
  158. this->qvtkReconnect(d->LookupTable,vtkCommand::ModifiedEvent,
  159. this, SIGNAL(changed()));
  160. emit changed();
  161. }
  162. //-----------------------------------------------------------------------------
  163. vtkLookupTable* ctkVTKLookupTable::lookupTable()const
  164. {
  165. CTK_D(const ctkVTKLookupTable);
  166. return d->LookupTable;
  167. }