ctkVTKLookupTable.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. bool ctkVTKLookupTable::isEditable()const
  64. {
  65. return true;
  66. }
  67. //-----------------------------------------------------------------------------
  68. void ctkVTKLookupTable::range(qreal& minRange, qreal& maxRange)const
  69. {
  70. CTK_D(const ctkVTKLookupTable);
  71. if (d->LookupTable.GetPointer() == 0)
  72. {
  73. Q_ASSERT(d->LookupTable.GetPointer());
  74. minRange = 1.; // set incorrect values
  75. maxRange = 0.;
  76. return;
  77. }
  78. double rangeValues[2];
  79. d->LookupTable->GetTableRange(rangeValues);
  80. minRange = rangeValues[0];
  81. maxRange = rangeValues[1];
  82. }
  83. //-----------------------------------------------------------------------------
  84. QVariant ctkVTKLookupTable::minValue()const
  85. {
  86. CTK_D(const ctkVTKLookupTable);
  87. if (d->LookupTable.GetPointer() == 0)
  88. {
  89. Q_ASSERT(d->LookupTable.GetPointer());
  90. return QColor();
  91. }
  92. QColor minValue = QColor::fromHsvF(
  93. d->LookupTable->GetHueRange()[0],
  94. d->LookupTable->GetSaturationRange()[0],
  95. d->LookupTable->GetValueRange()[0],
  96. d->LookupTable->GetAlphaRange()[0]);
  97. return minValue;
  98. }
  99. //-----------------------------------------------------------------------------
  100. QVariant ctkVTKLookupTable::maxValue()const
  101. {
  102. CTK_D(const ctkVTKLookupTable);
  103. if (d->LookupTable.GetPointer() == 0)
  104. {
  105. Q_ASSERT(d->LookupTable.GetPointer());
  106. return QColor();
  107. }
  108. QColor maxValue = QColor::fromHsvF(
  109. d->LookupTable->GetHueRange()[1],
  110. d->LookupTable->GetSaturationRange()[1],
  111. d->LookupTable->GetValueRange()[1],
  112. d->LookupTable->GetAlphaRange()[1]);
  113. return maxValue;
  114. }
  115. //-----------------------------------------------------------------------------
  116. qreal ctkVTKLookupTable::indexToPos(int index)const
  117. {
  118. CTK_D(const ctkVTKLookupTable);
  119. double* range = d->LookupTable->GetRange();
  120. return range[0] + index * ((range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1));
  121. }
  122. //-----------------------------------------------------------------------------
  123. int ctkVTKLookupTable::posToIndex(qreal pos)const
  124. {
  125. CTK_D(const ctkVTKLookupTable);
  126. double* range = d->LookupTable->GetRange();
  127. return (pos - range[0]) / ((range[1] - range[0]) / (d->LookupTable->GetNumberOfColors() - 1));
  128. }
  129. //-----------------------------------------------------------------------------
  130. ctkControlPoint* ctkVTKLookupTable::controlPoint(int index)const
  131. {
  132. CTK_D(const ctkVTKLookupTable);
  133. ctkControlPoint* cp = new ctkControlPoint();
  134. cp->P.X = this->indexToPos(index);
  135. cp->P.Value = this->value(cp->P.X);
  136. return cp;
  137. }
  138. //-----------------------------------------------------------------------------
  139. QVariant ctkVTKLookupTable::value(qreal pos)const
  140. {
  141. CTK_D(const ctkVTKLookupTable);
  142. Q_ASSERT(d->LookupTable.GetPointer());
  143. double rgb[3];
  144. d->LookupTable->GetColor(pos, rgb);
  145. double alpha = d->LookupTable->GetOpacity(pos);
  146. return QColor::fromRgbF(rgb[0], rgb[1], rgb[2], alpha);
  147. }
  148. //-----------------------------------------------------------------------------
  149. int ctkVTKLookupTable::insertControlPoint(const ctkControlPoint& cp)
  150. {
  151. CTK_D(ctkVTKLookupTable);
  152. qDebug() << "ctkVTKLookupTable doesn't support insertControlPoint";
  153. return -1;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // insert point with value = 0
  157. int ctkVTKLookupTable::insertControlPoint(qreal pos)
  158. {
  159. // nothing
  160. int index = 0;
  161. return index;
  162. }
  163. //-----------------------------------------------------------------------------
  164. void ctkVTKLookupTable::setControlPointPos(int index, qreal pos)
  165. {
  166. CTK_D(ctkVTKLookupTable);
  167. // TODO, inform that nothing is done here.
  168. qDebug() << "ctkVTKLookupTable doesn't support setControlPointPos";
  169. return;
  170. }
  171. //-----------------------------------------------------------------------------
  172. void ctkVTKLookupTable::setControlPointValue(int index, const QVariant& value)
  173. {
  174. CTK_D(ctkVTKLookupTable);
  175. Q_ASSERT(value.value<QColor>().isValid());
  176. QColor rgba = value.value<QColor>();
  177. d->LookupTable->SetTableValue(index, rgba.redF(), rgba.greenF(), rgba.blueF(), rgba.alphaF());
  178. }
  179. //-----------------------------------------------------------------------------
  180. void ctkVTKLookupTable::setLookupTable(vtkLookupTable* lookupTable)
  181. {
  182. CTK_D(ctkVTKLookupTable);
  183. d->LookupTable = lookupTable;
  184. this->qvtkReconnect(d->LookupTable,vtkCommand::ModifiedEvent,
  185. this, SIGNAL(changed()));
  186. emit changed();
  187. }
  188. //-----------------------------------------------------------------------------
  189. vtkLookupTable* ctkVTKLookupTable::lookupTable()const
  190. {
  191. CTK_D(const ctkVTKLookupTable);
  192. return d->LookupTable;
  193. }