ctkVTKPiecewiseFunction.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. bool ctkVTKPiecewiseFunction::isEditable()const
  57. {
  58. return true;
  59. }
  60. //-----------------------------------------------------------------------------
  61. void ctkVTKPiecewiseFunction::range(qreal& minRange, qreal& maxRange)const
  62. {
  63. CTK_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. CTK_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. CTK_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. CTK_D(const ctkVTKPiecewiseFunction);
  120. Q_ASSERT(index >= 0 && index < this->count());
  121. double values[4];
  122. double* range = d->PiecewiseFunction->GetRange();
  123. d->PiecewiseFunction->GetNodeValue(index, values);
  124. QVariant rangeY[2];
  125. rangeY[0] = this->minValue();
  126. rangeY[1] = this->maxValue();
  127. // rangeYDiff /= rangeY[1].toReal() - rangeY[0].toReal();
  128. Q_ASSERT(values[0] >= range[0] && values[0] <= range [1] && // X
  129. values[1] >= rangeY[0].toDouble() && values[1] <= rangeY[1].toDouble() && // Y
  130. values[2] >= 0. && values[2] <= 1. && // Midpoint
  131. values[3] >= 0. && values[3] <= 1. ); // Sharpness
  132. if (index + 1 >= this->count())
  133. {
  134. ctkControlPoint* cp = new ctkControlPoint();
  135. cp->P.X = values[0];
  136. cp->P.Value = values[1];
  137. return cp;
  138. }
  139. ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
  140. cp->P.X = values[0];
  141. cp->P.Value = values[1];
  142. // Optimization: don't use Subpoints if sharpness == 0
  143. if (values[3] == 0.)
  144. {
  145. cp->SubPoints << ctkPoint(values[0], values[1]);
  146. }
  147. double nextValues[4];
  148. d->PiecewiseFunction->GetNodeValue(index + 1, nextValues);
  149. Q_ASSERT(nextValues[0] >= range[0] && nextValues[0] <= range[1] && // X
  150. nextValues[1] >= rangeY[0].toDouble() && nextValues[1] <= rangeY[1].toDouble() && // Y
  151. nextValues[2] >= 0. && nextValues[2] <= 1. && // Midpoint
  152. nextValues[3] >= 0. && nextValues[3] <= 1. ); // Sharpness
  153. // Optimization: Don't use Subpoints if sharpness == 0
  154. if (values[3] == 0.)
  155. {
  156. cp->SubPoints << ctkPoint(nextValues[0], nextValues[1]);
  157. return cp;
  158. }
  159. double subPoints[100];
  160. d->PiecewiseFunction->GetTable(cp->x(), nextValues[0], 100, subPoints);
  161. qreal interval = (nextValues[0] - cp->x()) / 99.;
  162. // subPoints[i] since value varies (not like in color transfer function widget)
  163. for(int i = 0; i < 100; ++i)
  164. {
  165. cp->SubPoints << ctkPoint(cp->x() + interval*i, subPoints[i]);
  166. }
  167. return cp;
  168. }
  169. //-----------------------------------------------------------------------------
  170. QVariant ctkVTKPiecewiseFunction::value(qreal pos)const
  171. {
  172. CTK_D(const ctkVTKPiecewiseFunction);
  173. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  174. qreal value;
  175. // get value for given x
  176. value = d->PiecewiseFunction->GetValue( pos );
  177. return value;
  178. }
  179. //-----------------------------------------------------------------------------
  180. int ctkVTKPiecewiseFunction::insertControlPoint(const ctkControlPoint& cp)
  181. {
  182. CTK_D(ctkVTKPiecewiseFunction);
  183. int index = -1;
  184. if (d->PiecewiseFunction.GetPointer() == 0)
  185. {
  186. return index;
  187. }
  188. qreal value = cp.value().value<qreal>();
  189. const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
  190. if (nonLinearCp)
  191. {
  192. // TODO retrieve midpoint & sharpness
  193. index = d->PiecewiseFunction->AddPoint(
  194. cp.x(), value);
  195. }
  196. else
  197. {
  198. index = d->PiecewiseFunction->AddPoint(
  199. cp.x(), value);
  200. }
  201. return index;
  202. }
  203. //-----------------------------------------------------------------------------
  204. // insert point with value = 0
  205. int ctkVTKPiecewiseFunction::insertControlPoint(qreal pos)
  206. {
  207. CTK_D(ctkVTKPiecewiseFunction);
  208. int index = -1;
  209. if (d->PiecewiseFunction.GetPointer() == 0)
  210. {
  211. return index;
  212. }
  213. index = d->PiecewiseFunction->AddPoint( pos, 0);
  214. qDebug() << "index of new point: " << index;
  215. return index;
  216. }
  217. //-----------------------------------------------------------------------------
  218. void ctkVTKPiecewiseFunction::setControlPointPos(int index, qreal pos)
  219. {
  220. CTK_D(ctkVTKPiecewiseFunction);
  221. double values[4];
  222. d->PiecewiseFunction->GetNodeValue(index, values);
  223. // warning, a possible new range is not supported
  224. // SetNodeValue eventually fire the signal changed()
  225. values[0] = pos;
  226. d->PiecewiseFunction->SetNodeValue(index, values);
  227. }
  228. //-----------------------------------------------------------------------------
  229. void ctkVTKPiecewiseFunction::setControlPointValue(int index, const QVariant& value)
  230. {
  231. CTK_D(ctkVTKPiecewiseFunction);
  232. double values[4];
  233. d->PiecewiseFunction->GetNodeValue(index, values);
  234. qDebug() << "old value: " << values[1];
  235. values[1] = value.toDouble();
  236. qDebug() << "new value: " << values[1];
  237. // setNodeValue should eventually fired the signal changed()
  238. d->PiecewiseFunction->SetNodeValue(index, values);
  239. }
  240. //-----------------------------------------------------------------------------
  241. void ctkVTKPiecewiseFunction::setPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction)
  242. {
  243. CTK_D(ctkVTKPiecewiseFunction);
  244. d->PiecewiseFunction = piecewiseFunction;
  245. this->qvtkReconnect(d->PiecewiseFunction,vtkCommand::ModifiedEvent,
  246. this, SIGNAL(changed()));
  247. emit changed();
  248. }
  249. //-----------------------------------------------------------------------------
  250. vtkPiecewiseFunction* ctkVTKPiecewiseFunction::piecewiseFunction()const
  251. {
  252. CTK_D(const ctkVTKPiecewiseFunction);
  253. return d->PiecewiseFunction;
  254. }
  255. //-----------------------------------------------------------------------------
  256. void ctkVTKPiecewiseFunction::removeControlPoint( qreal pos )
  257. {
  258. CTK_D(ctkVTKPiecewiseFunction);
  259. if (d->PiecewiseFunction.GetPointer() == 0)
  260. {
  261. return;
  262. }
  263. d->PiecewiseFunction->RemovePoint( pos );
  264. }