ctkVTKCompositeFunction.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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* rangePWF = d->PiecewiseFunction->GetRange();
  137. double* rangeCTF = d->ColorTransferFunction->GetRange();
  138. d->PiecewiseFunction->GetNodeValue(index, valuesPWF);
  139. d->ColorTransferFunction->GetNodeValue(index, valuesCTF);
  140. QVariant rangeY[2];
  141. rangeY[0] = this->minValue();
  142. rangeY[1] = this->maxValue();
  143. // test piecewise
  144. Q_ASSERT(valuesPWF[0] >= rangePWF[0] && valuesPWF[0] <= rangePWF [1] && // X
  145. valuesPWF[1] >= rangeY[0].toDouble() && valuesPWF[1] <= rangeY[1].toDouble() && // Y
  146. valuesPWF[2] >= 0. && valuesPWF[2] <= 1. && // Midpoint
  147. valuesPWF[3] >= 0. && valuesPWF[3] <= 1. ); // Sharpness
  148. // test color transfer
  149. Q_ASSERT(valuesCTF[0] >= rangeCTF[0] &&
  150. valuesCTF[0] <= rangeCTF[1] &&
  151. valuesCTF[1] >= 0. && valuesCTF[1] <= 1. && // Red
  152. valuesCTF[2] >= 0. && valuesCTF[2] <= 1. && // Green
  153. valuesCTF[3] >= 0. && valuesCTF[3] <= 1. && // Blue
  154. valuesCTF[4] >= 0. && valuesCTF[4] <= 1. && // MidPoint
  155. valuesCTF[5] >= 0. && valuesCTF[5] <= 1.); // Sharpness
  156. // if only 2 points -> linear
  157. if (index + 1 >= this->count())
  158. {
  159. ctkControlPoint* cp = new ctkControlPoint();
  160. cp->P.X = valuesPWF[0];
  161. // update value of QVariant
  162. cp->P.Value = QColor::fromRgbF(
  163. valuesCTF[1], valuesCTF[2], valuesCTF[3], valuesPWF[1]);
  164. return cp;
  165. }
  166. //else
  167. ctkNonLinearControlPoint* cp = new ctkNonLinearControlPoint();
  168. cp->P.X = valuesPWF[0];
  169. // update value of QVariant
  170. cp->P.Value = QColor::fromRgbF(valuesCTF[1], valuesCTF[2], valuesCTF[3], valuesPWF[1]);
  171. double nextValuesPWF[4], nextValuesCTF[6];
  172. d->PiecewiseFunction->GetNodeValue(index + 1, nextValuesPWF);
  173. d->ColorTransferFunction->GetNodeValue(index + 1, nextValuesCTF);
  174. Q_ASSERT(nextValuesPWF[0] >= rangePWF[0] && nextValuesPWF[0] <= rangePWF[1] && // X
  175. nextValuesPWF[1] >= rangeY[0].toDouble() && nextValuesPWF[1] <= rangeY[1].toDouble() && // Y
  176. nextValuesPWF[2] >= 0. && nextValuesPWF[2] <= 1. && // Midpoint
  177. nextValuesPWF[3] >= 0. && nextValuesPWF[3] <= 1. ); // Sharpness
  178. Q_ASSERT(nextValuesCTF[0] >= rangeCTF[0] &&
  179. nextValuesCTF[0] <= rangeCTF[1] &&
  180. nextValuesCTF[1] >= 0. && nextValuesCTF[1] <= 1. && // Red
  181. nextValuesCTF[2] >= 0. && nextValuesCTF[2] <= 1. && // Green
  182. nextValuesCTF[3] >= 0. && nextValuesCTF[3] <= 1. && // Blue
  183. nextValuesCTF[4] >= 0. && nextValuesCTF[4] <= 1. && // MidPoint
  184. nextValuesCTF[5] >= 0. && nextValuesCTF[5] <= 1.); // Sharpness
  185. // Optimization: don't use subPoints if the ramp is linear (sharpness == 0)
  186. if (valuesPWF[3] == 0. && valuesCTF[5] == 0.)
  187. {
  188. cp->SubPoints << ctkPoint(valuesPWF[0], QColor::fromRgbF(
  189. valuesCTF[1], valuesCTF[2], valuesCTF[3], valuesPWF[1]));
  190. cp->SubPoints << ctkPoint(nextValuesPWF[0], QColor::fromRgbF(
  191. nextValuesCTF[1], nextValuesCTF[2], nextValuesCTF[3], nextValuesPWF[1]));
  192. return cp;
  193. }
  194. double subPointsCTF[300];
  195. double subPointsPWF[100];
  196. d->ColorTransferFunction->GetTable(cp->x(), nextValuesCTF[0], 100, subPointsCTF);
  197. d->PiecewiseFunction->GetTable(cp->x(), nextValuesCTF[0], 100, subPointsPWF);
  198. qreal interval = (nextValuesCTF[0] - cp->x()) / 99.;
  199. for(int i = 0; i < 100; ++i)
  200. {
  201. qreal red = subPointsCTF[3*i];
  202. qreal green = subPointsCTF[3*i+1];
  203. qreal blue = subPointsCTF[3*i+2];
  204. qreal alpha = subPointsPWF[i];
  205. QColor compositeValue = QColor::fromRgbF(red, green, blue, alpha );
  206. cp->SubPoints << ctkPoint(cp->x() + interval*i, compositeValue);
  207. }
  208. return cp;
  209. }
  210. //-----------------------------------------------------------------------------
  211. QVariant ctkVTKCompositeFunction::value(qreal pos)const
  212. {
  213. CTK_D(const ctkVTKCompositeFunction);
  214. Q_ASSERT(d->PiecewiseFunction.GetPointer());
  215. Q_ASSERT(d->ColorTransferFunction.GetPointer());
  216. // Get color
  217. double rgb[3];
  218. d->ColorTransferFunction->GetColor(pos, rgb);
  219. // Get Alpha
  220. qreal alpha;
  221. alpha = d->PiecewiseFunction->GetValue( pos );
  222. // returns RGBA
  223. QColor compositeValue(rgb[0], rgb[1], rgb[2], alpha);
  224. return compositeValue;
  225. }
  226. //-----------------------------------------------------------------------------
  227. int ctkVTKCompositeFunction::insertControlPoint(const ctkControlPoint& cp)
  228. {
  229. Q_UNUSED(cp);
  230. CTK_D(ctkVTKCompositeFunction);
  231. int index = -1;
  232. // check piecewise
  233. if (d->PiecewiseFunction.GetPointer() == 0)
  234. {
  235. return index;
  236. }
  237. // cp: x
  238. // value = rgba
  239. /*
  240. // check color tf
  241. qreal value = cp.value().value<qreal>();
  242. const ctkNonLinearControlPoint* nonLinearCp = dynamic_cast<const ctkNonLinearControlPoint*>(&cp);
  243. if (nonLinearCp)
  244. {
  245. // TODO retrieve midpoint & sharpness
  246. index = d->PiecewiseFunction->AddPoint(
  247. cp.x(), value);
  248. }
  249. else
  250. {
  251. index = d->PiecewiseFunction->AddPoint(
  252. cp.x(), value);
  253. }*/
  254. return index;
  255. }
  256. //-----------------------------------------------------------------------------
  257. int ctkVTKCompositeFunction::insertControlPoint(qreal pos)
  258. {
  259. CTK_D(ctkVTKCompositeFunction);
  260. int index = -1;
  261. // check piecewise
  262. if (d->PiecewiseFunction.GetPointer() == 0)
  263. {
  264. return index;
  265. }
  266. //check color tf
  267. if (d->ColorTransferFunction.GetPointer() == 0)
  268. {
  269. return index;
  270. }
  271. // Add color to CTF
  272. double color[3];
  273. d->ColorTransferFunction->GetColor( pos, color );
  274. int indexColor =
  275. d->ColorTransferFunction->AddRGBPoint( pos, color[0], color[1], color[2] );
  276. // Add point to piecewise
  277. int indexPiecewise =
  278. d->PiecewiseFunction->AddPoint( pos, 0);
  279. // check index
  280. Q_ASSERT(indexColor == indexPiecewise);
  281. index = indexColor;
  282. return index;
  283. }
  284. //-----------------------------------------------------------------------------
  285. void ctkVTKCompositeFunction::setControlPointPos(int index, qreal pos)
  286. {
  287. CTK_D(ctkVTKCompositeFunction);
  288. // update X pos in the CTF
  289. double valuesColor[6];
  290. d->ColorTransferFunction->GetNodeValue(index, valuesColor);
  291. valuesColor[0] = pos;
  292. // warning, a possible new range is not supported
  293. // SetNodeValue eventually fire the signal changed()
  294. d->ColorTransferFunction->SetNodeValue(index, valuesColor);
  295. // Update X pos in the PWF
  296. double valuesPiecewise[4];
  297. d->PiecewiseFunction->GetNodeValue(index, valuesPiecewise);
  298. // warning, a possible new range is not supported
  299. // SetNodeValue eventually fire the signal changed()
  300. valuesPiecewise[0] = pos;
  301. d->PiecewiseFunction->SetNodeValue(index, valuesPiecewise);
  302. }
  303. //-----------------------------------------------------------------------------
  304. void ctkVTKCompositeFunction::setControlPointValue(int index, const QVariant& value)
  305. {
  306. CTK_D(ctkVTKCompositeFunction);
  307. // QVariant = RGBA
  308. double values[4];
  309. d->PiecewiseFunction->GetNodeValue(index, values);
  310. values[1] = value.toDouble();
  311. // setNodeValue should eventually fired the signal changed()
  312. d->PiecewiseFunction->SetNodeValue(index, values);
  313. }
  314. //-----------------------------------------------------------------------------
  315. void ctkVTKCompositeFunction::setPiecewiseFunction(vtkPiecewiseFunction* piecewiseFunction)
  316. {
  317. CTK_D(ctkVTKCompositeFunction);
  318. d->PiecewiseFunction = piecewiseFunction;
  319. this->qvtkReconnect(d->PiecewiseFunction,vtkCommand::ModifiedEvent,
  320. this, SIGNAL(changed()));
  321. emit changed();
  322. }
  323. //-----------------------------------------------------------------------------
  324. void ctkVTKCompositeFunction::setColorTransferFunction(vtkColorTransferFunction* colorTransferFunction)
  325. {
  326. CTK_D(ctkVTKCompositeFunction);
  327. d->ColorTransferFunction = colorTransferFunction;
  328. this->qvtkReconnect(d->ColorTransferFunction,vtkCommand::ModifiedEvent,
  329. this, SIGNAL(changed()));
  330. emit changed();
  331. }
  332. //-----------------------------------------------------------------------------
  333. vtkPiecewiseFunction* ctkVTKCompositeFunction::piecewiseFunction()const
  334. {
  335. CTK_D(const ctkVTKCompositeFunction);
  336. return d->PiecewiseFunction;
  337. }
  338. //-----------------------------------------------------------------------------
  339. vtkColorTransferFunction* ctkVTKCompositeFunction::colorTransferFunction()const
  340. {
  341. CTK_D(const ctkVTKCompositeFunction);
  342. return d->ColorTransferFunction;
  343. }
  344. //-----------------------------------------------------------------------------
  345. void ctkVTKCompositeFunction::removeControlPoint( qreal pos )
  346. {
  347. Q_UNUSED(pos);
  348. // TO BE IMPLEMENTED
  349. }