ctkVTKPiecewiseFunction.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "ctkVTKPiecewiseFunction.h"
  19. /// VTK includes
  20. #include <vtkPiecewiseFunction.h>
  21. #include <vtkSmartPointer.h>
  22. class ctkVTKPiecewiseFunctionPrivate: public ctkPrivate<ctkVTKPiecewiseFunction>
  23. {
  24. public:
  25. vtkSmartPointer<vtkPiecewiseFunction> PiecewiseFunction;
  26. };
  27. //-----------------------------------------------------------------------------
  28. ctkVTKPiecewiseFunction::ctkVTKPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction,
  29. QObject* parentObject)
  30. :ctkTransferFunction(parentObject)
  31. {
  32. CTK_INIT_PRIVATE(ctkVTKPiecewiseFunction);
  33. this->setPiecewiseFunction(piecewiseFunction);
  34. }
  35. //-----------------------------------------------------------------------------
  36. ctkVTKPiecewiseFunction::~ctkVTKPiecewiseFunction()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. int ctkVTKPiecewiseFunction::count()const
  41. {
  42. CTK_D(const ctkVTKPiecewiseFunction);
  43. if (d->PiecewiseFunction.GetPointer() == 0)
  44. {
  45. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  46. return -1;
  47. }
  48. return d->PiecewiseFunction->GetSize();
  49. }
  50. //-----------------------------------------------------------------------------
  51. bool ctkVTKPiecewiseFunction::isDiscrete()const
  52. {
  53. return false;
  54. }
  55. //-----------------------------------------------------------------------------
  56. void ctkVTKPiecewiseFunction::range(qreal& minRange, qreal& maxRange)const
  57. {
  58. CTK_D(const ctkVTKPiecewiseFunction);
  59. if (d->PiecewiseFunction.GetPointer() == 0)
  60. {
  61. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  62. minRange = 1.;
  63. maxRange = 0.;
  64. return;
  65. }
  66. double rangeValues[2];
  67. d->PiecewiseFunction->GetRange(rangeValues);
  68. minRange = rangeValues[0];
  69. maxRange = rangeValues[1];
  70. }
  71. //-----------------------------------------------------------------------------
  72. QVariant ctkVTKPiecewiseFunction::minValue()const
  73. {
  74. CTK_D(const ctkVTKPiecewiseFunction);
  75. if (d->PiecewiseFunction.GetPointer() == 0)
  76. {
  77. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  78. return -1;
  79. }
  80. //Initialize to max value
  81. /// TODO initialize with max value
  82. double minValue = 9999.99;
  83. for (int i = 0; i < this->count(); ++i)
  84. {
  85. double value[4];
  86. d->PiecewiseFunction->GetNodeValue(i, value);
  87. if ( value[1] < minValue)
  88. {
  89. minValue = value[1];
  90. }
  91. }
  92. return minValue;
  93. }
  94. //-----------------------------------------------------------------------------
  95. QVariant ctkVTKPiecewiseFunction::maxValue()const
  96. {
  97. CTK_D(const ctkVTKPiecewiseFunction);
  98. if (d->PiecewiseFunction.GetPointer() == 0)
  99. {
  100. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  101. return -1;
  102. }
  103. //Initialize to max value
  104. /// TODO initialize with max value
  105. qreal maxValue = 0.;
  106. for (int i = 0; i < this->count(); ++i)
  107. {
  108. double value[4];
  109. d->PiecewiseFunction->GetNodeValue(i, value);
  110. if ( maxValue < value[1])
  111. {
  112. maxValue = value[1];
  113. }
  114. }
  115. return maxValue;
  116. }
  117. //-----------------------------------------------------------------------------
  118. ctkControlPoint* ctkVTKPiecewiseFunction::controlPoint(int index)const
  119. {
  120. CTK_D(const ctkVTKPiecewiseFunction);
  121. Q_ASSERT(index >= 0 && index < this->count());
  122. double values[4];
  123. double* range = d->PiecewiseFunction->GetRange();
  124. d->PiecewiseFunction->GetNodeValue(index, values);
  125. QVariant rangeY[2];
  126. rangeY[0] = this->minValue();
  127. rangeY[1] = this->maxValue();
  128. // rangeYDiff /= rangeY[1].toReal() - rangeY[0].toReal();
  129. Q_ASSERT(values[0] >= range[0] && values[0] <= range [1] && // X
  130. values[1] >= rangeY[0].toDouble() && values[1] <= rangeY[1].toDouble() && // Y
  131. values[2] >= 0. && values[2] <= 1. && // Midpoint
  132. values[3] >= 0. && values[3] <= 1. ); // Sharpness
  133. if (index + 1 >= this->count())
  134. {
  135. ctkControlPoint* cp = new ctkControlPoint();
  136. cp->P.X = values[0];
  137. cp->P.Value = values[1];
  138. return cp;
  139. }
  140. ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
  141. cp->P.X = values[0];
  142. cp->P.Value = values[1];
  143. d->PiecewiseFunction->GetNodeValue(index + 1, values);
  144. Q_ASSERT(values[0] >= range[0] && values[0] <= range[1] && // X
  145. values[1] >= rangeY[0].toDouble() && values[1] <= rangeY[1].toDouble() && // Y
  146. values[2] >= 0. && values[2] <= 1. && // Midpoint
  147. values[3] >= 0. && values[3] <= 1. ); // Sharpness
  148. double subPoints[30];
  149. d->PiecewiseFunction->GetTable(cp->x(), values[0], 10, subPoints);
  150. qreal interval = (values[0] - cp->x()) / 9.;
  151. // subPoints[i] since value varies (not like in color transfer function widget)
  152. for(int i = 0; i < 10; ++i)
  153. {
  154. cp->SubPoints << ctkPoint(cp->x() + interval*i, subPoints[i]);
  155. }
  156. return cp;
  157. }
  158. //-----------------------------------------------------------------------------
  159. QVariant ctkVTKPiecewiseFunction::value(qreal pos)const
  160. {
  161. CTK_D(const ctkVTKPiecewiseFunction);
  162. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  163. qreal value;
  164. // get value for given x
  165. value = d->PiecewiseFunction->GetValue( pos );
  166. return value;
  167. }
  168. //-----------------------------------------------------------------------------
  169. int ctkVTKPiecewiseFunction::insertControlPoint(const ctkControlPoint& cp)
  170. {
  171. CTK_D(ctkVTKPiecewiseFunction);
  172. int index = -1;
  173. if (d->PiecewiseFunction.GetPointer() == 0)
  174. {
  175. return index;
  176. }
  177. qreal value = cp.value().value<qreal>();
  178. const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
  179. if (nonLinearCp)
  180. {
  181. // TODO retrieve midpoint & sharpness
  182. index = d->PiecewiseFunction->AddPoint(
  183. cp.x(), value);
  184. }
  185. else
  186. {
  187. index = d->PiecewiseFunction->AddPoint(
  188. cp.x(), value);
  189. }
  190. return index;
  191. }
  192. //-----------------------------------------------------------------------------
  193. void ctkVTKPiecewiseFunction::setControlPointPos(int index, qreal pos)
  194. {
  195. CTK_D(ctkVTKPiecewiseFunction);
  196. double values[4];
  197. d->PiecewiseFunction->GetNodeValue(index, values);
  198. // warning, a possible new range is not supported
  199. // SetNodeValue eventually fire the signal changed()
  200. values[0] = pos;
  201. d->PiecewiseFunction->SetNodeValue(index, values);
  202. }
  203. //-----------------------------------------------------------------------------
  204. void ctkVTKPiecewiseFunction::setControlPointValue(int index, const QVariant& value)
  205. {
  206. CTK_D(ctkVTKPiecewiseFunction);
  207. Q_ASSERT(value.value<QColor>().isValid());
  208. QColor rgb = value.value<QColor>();
  209. double values[4];
  210. d->PiecewiseFunction->GetNodeValue(index, values);
  211. // setNodeValue should eventually fired the signal changed()
  212. d->PiecewiseFunction->SetNodeValue(index, values);
  213. }
  214. //-----------------------------------------------------------------------------
  215. void ctkVTKPiecewiseFunction::setPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction)
  216. {
  217. CTK_D(ctkVTKPiecewiseFunction);
  218. d->PiecewiseFunction = piecewiseFunction;
  219. this->qvtkReconnect(d->PiecewiseFunction,vtkCommand::ModifiedEvent,
  220. this, SIGNAL(changed()));
  221. emit changed();
  222. }
  223. //-----------------------------------------------------------------------------
  224. vtkPiecewiseFunction* ctkVTKPiecewiseFunction::piecewiseFunction()const
  225. {
  226. CTK_D(const ctkVTKPiecewiseFunction);
  227. return d->PiecewiseFunction;
  228. }