ctkVTKCompositeFunction.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "ctkVTKCompositeFunction.h"
  19. /// VTK includes
  20. #include <vtkPiecewiseFunction.h>
  21. #include <vtkColorTransferFunction.h>
  22. #include <vtkSmartPointer.h>
  23. class ctkVTKCompositeFunctionPrivate: public ctkPrivate<ctkVTKCompositeFunction>
  24. {
  25. public:
  26. vtkSmartPointer<vtkPiecewiseFunction> PiecewiseFunction;
  27. vtkSmartPointer<vtkColorTransferFunction> ColorTransferFunction;
  28. };
  29. //-----------------------------------------------------------------------------
  30. ctkVTKCompositeFunction::ctkVTKCompositeFunction(vtkPiecewiseFunction* piecewiseFunction,
  31. vtkColorTransferFunction* colorTransferFunction,
  32. QObject* parentObject)
  33. :ctkTransferFunction(parentObject)
  34. {
  35. CTK_INIT_PRIVATE(ctkVTKCompositeFunction);
  36. this->setPiecewiseFunction(piecewiseFunction);
  37. this->setColorTransferFunction(colorTransferFunction);
  38. }
  39. //-----------------------------------------------------------------------------
  40. ctkVTKCompositeFunction::~ctkVTKCompositeFunction()
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. int ctkVTKCompositeFunction::count()const
  45. {
  46. CTK_D(const ctkVTKCompositeFunction);
  47. // count points from piecewise
  48. // could be from color transfer function
  49. if (d->PiecewiseFunction.GetPointer() == 0)
  50. {
  51. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  52. return -1;
  53. }
  54. if (d->ColorTransferFunction.GetPointer() == 0)
  55. {
  56. Q_ASSERT(d->ColorTransferFunction.GetPointer());
  57. return -1;
  58. }
  59. //qDebug() << "Piecewise: " << d->PiecewiseFunction->GetSize();
  60. //qDebug() << "Color Transfer: " << d->ColorTransferFunction->GetSize();
  61. // check if 2 tranfer function have same size
  62. Q_ASSERT( d->PiecewiseFunction->GetSize() == d->ColorTransferFunction->GetSize());
  63. return d->PiecewiseFunction->GetSize();
  64. }
  65. //-----------------------------------------------------------------------------
  66. bool ctkVTKCompositeFunction::isDiscrete()const
  67. {
  68. return false;
  69. }
  70. //-----------------------------------------------------------------------------
  71. bool ctkVTKCompositeFunction::isEditable()const
  72. {
  73. return true;
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkVTKCompositeFunction::range(qreal& minRange, qreal& maxRange)const
  77. {
  78. CTK_D(const ctkVTKCompositeFunction);
  79. if (d->PiecewiseFunction.GetPointer() == 0)
  80. {
  81. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  82. minRange = 1.;
  83. maxRange = 0.;
  84. return;
  85. }
  86. double rangeValues[2];
  87. d->PiecewiseFunction->GetRange(rangeValues);
  88. minRange = rangeValues[0];
  89. maxRange = rangeValues[1];
  90. }
  91. //-----------------------------------------------------------------------------
  92. QVariant ctkVTKCompositeFunction::minValue()const
  93. {
  94. CTK_D(const ctkVTKCompositeFunction);
  95. if (d->PiecewiseFunction.GetPointer() == 0)
  96. {
  97. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  98. return -1;
  99. }
  100. //Initialize to max value
  101. double minValue = VTK_DOUBLE_MAX;
  102. for (int i = 0; i < this->count(); ++i)
  103. {
  104. double value[4];
  105. d->PiecewiseFunction->GetNodeValue(i, value);
  106. minValue = qMin(value[1], minValue);
  107. }
  108. return minValue;
  109. }
  110. //-----------------------------------------------------------------------------
  111. QVariant ctkVTKCompositeFunction::maxValue()const
  112. {
  113. CTK_D(const ctkVTKCompositeFunction);
  114. if (d->PiecewiseFunction.GetPointer() == 0)
  115. {
  116. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  117. return -1;
  118. }
  119. //Initialize to min value
  120. double maxValue = VTK_DOUBLE_MIN;
  121. for (int i = 0; i < this->count(); ++i)
  122. {
  123. double value[4];
  124. d->PiecewiseFunction->GetNodeValue(i, value);
  125. maxValue = qMax(maxValue, value[1]);
  126. }
  127. return maxValue;
  128. }
  129. //-----------------------------------------------------------------------------
  130. ctkControlPoint* ctkVTKCompositeFunction::controlPoint(int index)const
  131. {
  132. CTK_D(const ctkVTKCompositeFunction);
  133. Q_ASSERT(index >= 0 && index < this->count());
  134. double valuesPWF[4];
  135. double valuesCTF[6];
  136. double* range = d->PiecewiseFunction->GetRange();
  137. d->PiecewiseFunction->GetNodeValue(index, valuesPWF);
  138. d->ColorTransferFunction->GetNodeValue(index, valuesCTF);
  139. QVariant rangeY[2];
  140. rangeY[0] = this->minValue();
  141. rangeY[1] = this->maxValue();
  142. // test piecewise
  143. Q_ASSERT(valuesPWF[0] >= range[0] && valuesPWF[0] <= range [1] && // X
  144. valuesPWF[1] >= rangeY[0].toDouble() && valuesPWF[1] <= rangeY[1].toDouble() && // Y
  145. valuesPWF[2] >= 0. && valuesPWF[2] <= 1. && // Midpoint
  146. valuesPWF[3] >= 0. && valuesPWF[3] <= 1. ); // Sharpness
  147. // test color transfer
  148. Q_ASSERT(valuesCTF[0] >= d->ColorTransferFunction->GetRange()[0] &&
  149. valuesCTF[0] <= d->ColorTransferFunction->GetRange()[1] &&
  150. valuesCTF[1] >= 0. && valuesCTF[1] <= 1. && // Red
  151. valuesCTF[2] >= 0. && valuesCTF[2] <= 1. && // Green
  152. valuesCTF[3] >= 0. && valuesCTF[3] <= 1. && // Blue
  153. valuesCTF[4] >= 0. && valuesCTF[4] <= 1. && // MidPoint
  154. valuesCTF[5] >= 0. && valuesCTF[5] <= 1.); // Sharpness
  155. // if only 2 points -> linear
  156. if (index + 1 >= this->count())
  157. {
  158. ctkControlPoint* cp = new ctkControlPoint();
  159. cp->P.X = valuesPWF[0];
  160. // update value of QVariant
  161. QColor compositeValue(valuesCTF[1], valuesCTF[2], valuesCTF[3], valuesPWF[1]);
  162. cp->P.Value = compositeValue;
  163. return cp;
  164. }
  165. //else
  166. ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
  167. cp->P.X = valuesPWF[0];
  168. // update value of QVariant
  169. QColor compositeValue(valuesCTF[1], valuesCTF[2], valuesCTF[3], valuesPWF[1]);
  170. cp->P.Value = compositeValue;
  171. d->PiecewiseFunction->GetNodeValue(index + 1, valuesPWF);
  172. d->ColorTransferFunction->GetNodeValue(index +1, valuesCTF);
  173. Q_ASSERT(valuesPWF[0] >= range[0] && valuesPWF[0] <= range[1] && // X
  174. valuesPWF[1] >= rangeY[0].toDouble() && valuesPWF[1] <= rangeY[1].toDouble() && // Y
  175. valuesPWF[2] >= 0. && valuesPWF[2] <= 1. && // Midpoint
  176. valuesPWF[3] >= 0. && valuesPWF[3] <= 1. ); // Sharpness
  177. Q_ASSERT(valuesCTF[0] >= d->ColorTransferFunction->GetRange()[0] &&
  178. valuesCTF[0] <= d->ColorTransferFunction->GetRange()[1] &&
  179. valuesCTF[1] >= 0. && valuesCTF[1] <= 1. && // Red
  180. valuesCTF[2] >= 0. && valuesCTF[2] <= 1. && // Green
  181. valuesCTF[3] >= 0. && valuesCTF[3] <= 1. && // Blue
  182. valuesCTF[4] >= 0. && valuesCTF[4] <= 1. && // MidPoint
  183. valuesCTF[5] >= 0. && valuesCTF[5] <= 1.); // Sharpness
  184. double subPointsCTF[30];
  185. double subPointsPWF[10];
  186. d->ColorTransferFunction->GetTable(cp->x(), valuesCTF[0], 10, subPointsCTF);
  187. d->PiecewiseFunction->GetTable(cp->x(), valuesCTF[0], 10, subPointsPWF);
  188. qreal interval = (valuesCTF[0] - cp->x()) / 9.;
  189. for(int i = 0; i < 10; ++i)
  190. {
  191. qreal red = subPointsCTF[3*i];
  192. qreal green = subPointsCTF[3*i+1];
  193. qreal blue = subPointsCTF[3*i+2];
  194. qreal alpha = subPointsPWF[i];
  195. QColor compositeValue = QColor::fromRgbF(red, green, blue, alpha );
  196. cp->SubPoints << ctkPoint(cp->x() + interval*i, compositeValue);
  197. }
  198. return cp;
  199. }
  200. //-----------------------------------------------------------------------------
  201. QVariant ctkVTKCompositeFunction::value(qreal pos)const
  202. {
  203. CTK_D(const ctkVTKCompositeFunction);
  204. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  205. Q_ASSERT(d->ColorTransferFunction.GetPointer());
  206. // Get color
  207. double rgb[3];
  208. d->ColorTransferFunction->GetColor(pos, rgb);
  209. // Get Alpha
  210. qreal alpha;
  211. alpha = d->PiecewiseFunction->GetValue( pos );
  212. // returns RGBA
  213. QColor compositeValue(rgb[0], rgb[1], rgb[2], alpha);
  214. return compositeValue;
  215. }
  216. //-----------------------------------------------------------------------------
  217. int ctkVTKCompositeFunction::insertControlPoint(const ctkControlPoint& cp)
  218. {
  219. CTK_D(ctkVTKCompositeFunction);
  220. int index = -1;
  221. // check piecewise
  222. if (d->PiecewiseFunction.GetPointer() == 0)
  223. {
  224. return index;
  225. }
  226. // cp: x
  227. // value = rgba
  228. /*
  229. // check color tf
  230. qreal value = cp.value().value<qreal>();
  231. const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
  232. if (nonLinearCp)
  233. {
  234. // TODO retrieve midpoint & sharpness
  235. index = d->PiecewiseFunction->AddPoint(
  236. cp.x(), value);
  237. }
  238. else
  239. {
  240. index = d->PiecewiseFunction->AddPoint(
  241. cp.x(), value);
  242. }*/
  243. return index;
  244. }
  245. //-----------------------------------------------------------------------------
  246. int ctkVTKCompositeFunction::insertControlPoint(qreal pos)
  247. {
  248. CTK_D(ctkVTKCompositeFunction);
  249. int index = -1;
  250. // check piecewise
  251. if (d->PiecewiseFunction.GetPointer() == 0)
  252. {
  253. return index;
  254. }
  255. //check color tf
  256. if (d->ColorTransferFunction.GetPointer() == 0)
  257. {
  258. return index;
  259. }
  260. // Add color to CTF
  261. double color[3];
  262. d->ColorTransferFunction->GetColor( pos, color );
  263. int indexColor =
  264. d->ColorTransferFunction->AddRGBPoint( pos, color[0], color[1], color[2] );
  265. // Add point to piecewise
  266. int indexPiecewise =
  267. d->PiecewiseFunction->AddPoint( pos, 0);
  268. // check index
  269. Q_ASSERT(indexColor == indexPiecewise);
  270. index = indexColor;
  271. return index;
  272. }
  273. //-----------------------------------------------------------------------------
  274. void ctkVTKCompositeFunction::setControlPointPos(int index, qreal pos)
  275. {
  276. CTK_D(ctkVTKCompositeFunction);
  277. // update X pos in the CTF
  278. double valuesColor[6];
  279. d->ColorTransferFunction->GetNodeValue(index, valuesColor);
  280. valuesColor[0] = pos;
  281. // warning, a possible new range is not supported
  282. // SetNodeValue eventually fire the signal changed()
  283. d->ColorTransferFunction->SetNodeValue(index, valuesColor);
  284. // Update X pos in the PWF
  285. double valuesPiecewise[4];
  286. d->PiecewiseFunction->GetNodeValue(index, valuesPiecewise);
  287. // warning, a possible new range is not supported
  288. // SetNodeValue eventually fire the signal changed()
  289. valuesPiecewise[0] = pos;
  290. d->PiecewiseFunction->SetNodeValue(index, valuesPiecewise);
  291. }
  292. //-----------------------------------------------------------------------------
  293. void ctkVTKCompositeFunction::setControlPointValue(int index, const QVariant& value)
  294. {
  295. CTK_D(ctkVTKCompositeFunction);
  296. // QVariant = RGBA
  297. double values[4];
  298. d->PiecewiseFunction->GetNodeValue(index, values);
  299. values[1] = value.toDouble();
  300. // setNodeValue should eventually fired the signal changed()
  301. d->PiecewiseFunction->SetNodeValue(index, values);
  302. }
  303. //-----------------------------------------------------------------------------
  304. void ctkVTKCompositeFunction::setPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction)
  305. {
  306. CTK_D(ctkVTKCompositeFunction);
  307. d->PiecewiseFunction = piecewiseFunction;
  308. this->qvtkReconnect(d->PiecewiseFunction,vtkCommand::ModifiedEvent,
  309. this, SIGNAL(changed()));
  310. emit changed();
  311. }
  312. //-----------------------------------------------------------------------------
  313. void ctkVTKCompositeFunction::setColorTransferFunction(vtkColorTransferFunction* colorTransferFunction)
  314. {
  315. CTK_D(ctkVTKCompositeFunction);
  316. d->ColorTransferFunction = colorTransferFunction;
  317. this->qvtkReconnect(d->ColorTransferFunction,vtkCommand::ModifiedEvent,
  318. this, SIGNAL(changed()));
  319. emit changed();
  320. }
  321. //-----------------------------------------------------------------------------
  322. vtkPiecewiseFunction* ctkVTKCompositeFunction::piecewiseFunction()const
  323. {
  324. CTK_D(const ctkVTKCompositeFunction);
  325. return d->PiecewiseFunction;
  326. }
  327. //-----------------------------------------------------------------------------
  328. vtkColorTransferFunction* ctkVTKCompositeFunction::colorTransferFunction()const
  329. {
  330. CTK_D(const ctkVTKCompositeFunction);
  331. return d->ColorTransferFunction;
  332. }