ctkVTKCompositeFunction.cpp 13 KB

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