ctkVTKPiecewiseFunction.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 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.apache.org/licenses/LICENSE-2.0.txt
  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
  23. {
  24. public:
  25. vtkSmartPointer<vtkPiecewiseFunction> PiecewiseFunction;
  26. };
  27. //-----------------------------------------------------------------------------
  28. ctkVTKPiecewiseFunction::ctkVTKPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction,
  29. QObject* parentObject)
  30. :ctkTransferFunction(parentObject)
  31. , d_ptr(new ctkVTKPiecewiseFunctionPrivate)
  32. {
  33. this->setPiecewiseFunction(piecewiseFunction);
  34. }
  35. //-----------------------------------------------------------------------------
  36. ctkVTKPiecewiseFunction::~ctkVTKPiecewiseFunction()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. int ctkVTKPiecewiseFunction::count()const
  41. {
  42. Q_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. bool ctkVTKPiecewiseFunction::isEditable()const
  57. {
  58. return true;
  59. }
  60. //-----------------------------------------------------------------------------
  61. void ctkVTKPiecewiseFunction::range(qreal& minRange, qreal& maxRange)const
  62. {
  63. Q_D(const ctkVTKPiecewiseFunction);
  64. if (d->PiecewiseFunction.GetPointer() == 0)
  65. {
  66. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  67. minRange = 1.;
  68. maxRange = 0.;
  69. return;
  70. }
  71. double rangeValues[2];
  72. d->PiecewiseFunction->GetRange(rangeValues);
  73. minRange = rangeValues[0];
  74. maxRange = rangeValues[1];
  75. }
  76. //-----------------------------------------------------------------------------
  77. QVariant ctkVTKPiecewiseFunction::minValue()const
  78. {
  79. Q_D(const ctkVTKPiecewiseFunction);
  80. if (d->PiecewiseFunction.GetPointer() == 0)
  81. {
  82. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  83. return -1;
  84. }
  85. //Initialize to max value
  86. /// TODO initialize with max value
  87. double minValue = VTK_DOUBLE_MAX;
  88. for (int i = 0; i < this->count(); ++i)
  89. {
  90. double value[4];
  91. d->PiecewiseFunction->GetNodeValue(i, value);
  92. minValue = qMin(value[1], minValue);
  93. }
  94. return minValue;
  95. }
  96. //-----------------------------------------------------------------------------
  97. QVariant ctkVTKPiecewiseFunction::maxValue()const
  98. {
  99. Q_D(const ctkVTKPiecewiseFunction);
  100. if (d->PiecewiseFunction.GetPointer() == 0)
  101. {
  102. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  103. return -1;
  104. }
  105. //Initialize to max value
  106. /// TODO initialize with max value
  107. qreal maxValue = VTK_DOUBLE_MIN;
  108. for (int i = 0; i < this->count(); ++i)
  109. {
  110. double value[4];
  111. d->PiecewiseFunction->GetNodeValue(i, value);
  112. maxValue = qMax(maxValue, value[1]);
  113. }
  114. return maxValue;
  115. }
  116. //-----------------------------------------------------------------------------
  117. ctkControlPoint* ctkVTKPiecewiseFunction::controlPoint(int index)const
  118. {
  119. Q_D(const ctkVTKPiecewiseFunction);
  120. Q_ASSERT(index >= 0 && index < this->count());
  121. double values[4];
  122. #ifndef QT_NO_DEBUG
  123. double* range = d->PiecewiseFunction->GetRange();
  124. #endif
  125. d->PiecewiseFunction->GetNodeValue(index, values);
  126. QVariant rangeY[2];
  127. rangeY[0] = this->minValue();
  128. rangeY[1] = this->maxValue();
  129. // rangeYDiff /= rangeY[1].toReal() - rangeY[0].toReal();
  130. Q_ASSERT(values[0] >= range[0] && values[0] <= range [1] && // X
  131. values[1] >= rangeY[0].toDouble() && values[1] <= rangeY[1].toDouble() && // Y
  132. values[2] >= 0. && values[2] <= 1. && // Midpoint
  133. values[3] >= 0. && values[3] <= 1. ); // Sharpness
  134. if (index + 1 >= this->count())
  135. {
  136. ctkControlPoint* cp = new ctkControlPoint();
  137. cp->P.X = values[0];
  138. cp->P.Value = values[1];
  139. return cp;
  140. }
  141. ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
  142. cp->P.X = values[0];
  143. cp->P.Value = values[1];
  144. // Optimization: don't use Subpoints if sharpness == 0
  145. if (values[3] == 0.)
  146. {
  147. cp->SubPoints << ctkPoint(values[0], values[1]);
  148. }
  149. double nextValues[4];
  150. d->PiecewiseFunction->GetNodeValue(index + 1, nextValues);
  151. Q_ASSERT(nextValues[0] >= range[0] && nextValues[0] <= range[1] && // X
  152. nextValues[1] >= rangeY[0].toDouble() && nextValues[1] <= rangeY[1].toDouble() && // Y
  153. nextValues[2] >= 0. && nextValues[2] <= 1. && // Midpoint
  154. nextValues[3] >= 0. && nextValues[3] <= 1. ); // Sharpness
  155. // Optimization: Don't use Subpoints if sharpness == 0
  156. if (values[3] == 0.)
  157. {
  158. cp->SubPoints << ctkPoint(nextValues[0], nextValues[1]);
  159. return cp;
  160. }
  161. double subPoints[100];
  162. d->PiecewiseFunction->GetTable(cp->x(), nextValues[0], 100, subPoints);
  163. qreal interval = (nextValues[0] - cp->x()) / 99.;
  164. // subPoints[i] since value varies (not like in color transfer function widget)
  165. for(int i = 0; i < 100; ++i)
  166. {
  167. cp->SubPoints << ctkPoint(cp->x() + interval*i, subPoints[i]);
  168. }
  169. return cp;
  170. }
  171. //-----------------------------------------------------------------------------
  172. QVariant ctkVTKPiecewiseFunction::value(qreal pos)const
  173. {
  174. Q_D(const ctkVTKPiecewiseFunction);
  175. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  176. qreal value;
  177. // get value for given x
  178. value = d->PiecewiseFunction->GetValue( pos );
  179. return value;
  180. }
  181. //-----------------------------------------------------------------------------
  182. int ctkVTKPiecewiseFunction::insertControlPoint(const ctkControlPoint& cp)
  183. {
  184. Q_D(ctkVTKPiecewiseFunction);
  185. int index = -1;
  186. if (d->PiecewiseFunction.GetPointer() == 0)
  187. {
  188. return index;
  189. }
  190. qreal value = cp.value().value<qreal>();
  191. const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
  192. if (nonLinearCp)
  193. {
  194. // TODO retrieve midpoint & sharpness
  195. index = d->PiecewiseFunction->AddPoint(
  196. cp.x(), value);
  197. }
  198. else
  199. {
  200. index = d->PiecewiseFunction->AddPoint(
  201. cp.x(), value);
  202. }
  203. return index;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // insert point with value = 0
  207. int ctkVTKPiecewiseFunction::insertControlPoint(qreal pos)
  208. {
  209. Q_D(ctkVTKPiecewiseFunction);
  210. int index = -1;
  211. if (d->PiecewiseFunction.GetPointer() == 0)
  212. {
  213. return index;
  214. }
  215. index = d->PiecewiseFunction->AddPoint( pos, 0);
  216. qDebug() << "index of new point: " << index;
  217. return index;
  218. }
  219. //-----------------------------------------------------------------------------
  220. void ctkVTKPiecewiseFunction::setControlPointPos(int index, qreal pos)
  221. {
  222. Q_D(ctkVTKPiecewiseFunction);
  223. double values[4];
  224. d->PiecewiseFunction->GetNodeValue(index, values);
  225. // warning, a possible new range is not supported
  226. // SetNodeValue eventually fire the signal changed()
  227. values[0] = pos;
  228. d->PiecewiseFunction->SetNodeValue(index, values);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void ctkVTKPiecewiseFunction::setControlPointValue(int index, const QVariant& value)
  232. {
  233. Q_D(ctkVTKPiecewiseFunction);
  234. double values[4];
  235. d->PiecewiseFunction->GetNodeValue(index, values);
  236. qDebug() << "old value: " << values[1];
  237. values[1] = value.toDouble();
  238. qDebug() << "new value: " << values[1];
  239. // setNodeValue should eventually fired the signal changed()
  240. d->PiecewiseFunction->SetNodeValue(index, values);
  241. }
  242. //-----------------------------------------------------------------------------
  243. void ctkVTKPiecewiseFunction::setPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction)
  244. {
  245. Q_D(ctkVTKPiecewiseFunction);
  246. d->PiecewiseFunction = piecewiseFunction;
  247. this->qvtkReconnect(d->PiecewiseFunction,vtkCommand::ModifiedEvent,
  248. this, SIGNAL(changed()));
  249. emit changed();
  250. }
  251. //-----------------------------------------------------------------------------
  252. vtkPiecewiseFunction* ctkVTKPiecewiseFunction::piecewiseFunction()const
  253. {
  254. Q_D(const ctkVTKPiecewiseFunction);
  255. return d->PiecewiseFunction;
  256. }
  257. //-----------------------------------------------------------------------------
  258. void ctkVTKPiecewiseFunction::removeControlPoint( qreal pos )
  259. {
  260. Q_D(ctkVTKPiecewiseFunction);
  261. if (d->PiecewiseFunction.GetPointer() == 0)
  262. {
  263. return;
  264. }
  265. d->PiecewiseFunction->RemovePoint( pos );
  266. }