ctkVTKScalarsToColorsView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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.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 <QColorDialog>
  16. // CTK includes
  17. #include "ctkLogger.h"
  18. #include "ctkVTKScalarsToColorsView.h"
  19. // VTK includes
  20. #include <vtkAxis.h>
  21. #include <vtkChartXY.h>
  22. #include <vtkColorTransferControlPointsItem.h>
  23. #include <vtkColorTransferFunction.h>
  24. #include <vtkColorTransferFunctionItem.h>
  25. #include <vtkCompositeControlPointsItem.h>
  26. #include <vtkCompositeTransferFunctionItem.h>
  27. #include <vtkLookupTable.h>
  28. #include <vtkLookupTableItem.h>
  29. #include <vtkPiecewiseControlPointsItem.h>
  30. #include <vtkPiecewiseFunction.h>
  31. #include <vtkPiecewiseFunctionItem.h>
  32. //----------------------------------------------------------------------------
  33. static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKScalarsToColorsView");
  34. //----------------------------------------------------------------------------
  35. class ctkVTKScalarsToColorsViewPrivate
  36. {
  37. Q_DECLARE_PUBLIC(ctkVTKScalarsToColorsView);
  38. protected:
  39. ctkVTKScalarsToColorsView* const q_ptr;
  40. public:
  41. ctkVTKScalarsToColorsViewPrivate(ctkVTKScalarsToColorsView& object);
  42. void init();
  43. void updateChart();
  44. };
  45. // ----------------------------------------------------------------------------
  46. // ctkVTKScalarsToColorsViewPrivate methods
  47. // ----------------------------------------------------------------------------
  48. ctkVTKScalarsToColorsViewPrivate::ctkVTKScalarsToColorsViewPrivate(ctkVTKScalarsToColorsView& object)
  49. :q_ptr(&object)
  50. {
  51. }
  52. // ----------------------------------------------------------------------------
  53. void ctkVTKScalarsToColorsViewPrivate::init()
  54. {
  55. Q_Q(ctkVTKScalarsToColorsView);
  56. vtkChartXY* chart = q->chart();
  57. chart->SetAutoAxes(false);
  58. chart->SetHiddenAxisBorder(0);
  59. for (int i = 0; i < 4; ++i)
  60. {
  61. chart->GetAxis(i)->SetVisible(false);
  62. chart->GetAxis(i)->SetMinimumLimit(0.);
  63. chart->GetAxis(i)->SetMaximumLimit(1.);
  64. }
  65. }
  66. // ----------------------------------------------------------------------------
  67. void ctkVTKScalarsToColorsViewPrivate::updateChart()
  68. {
  69. }
  70. // ----------------------------------------------------------------------------
  71. // ctkVTKScalarsToColorsView methods
  72. // ----------------------------------------------------------------------------
  73. ctkVTKScalarsToColorsView::ctkVTKScalarsToColorsView(QWidget* parentWidget)
  74. :ctkVTKChartView(parentWidget)
  75. , d_ptr(new ctkVTKScalarsToColorsViewPrivate(*this))
  76. {
  77. Q_D(ctkVTKScalarsToColorsView);
  78. d->init();
  79. }
  80. // ----------------------------------------------------------------------------
  81. ctkVTKScalarsToColorsView::~ctkVTKScalarsToColorsView()
  82. {
  83. }
  84. // ----------------------------------------------------------------------------
  85. void ctkVTKScalarsToColorsView::addPlot(vtkPlot* plot)
  86. {
  87. if (vtkColorTransferControlPointsItem::SafeDownCast(plot))
  88. {
  89. this->qvtkConnect(plot, vtkControlPointsItem::CurrentPointEditEvent,
  90. this, SLOT(editPoint(vtkObject*, void*)));
  91. }
  92. this->Superclass::addPlot(plot);
  93. }
  94. // ----------------------------------------------------------------------------
  95. vtkPlot* ctkVTKScalarsToColorsView::addLookupTable(vtkLookupTable* lut)
  96. {
  97. Q_D(ctkVTKScalarsToColorsView);
  98. vtkSmartPointer<vtkLookupTableItem> item =
  99. vtkSmartPointer<vtkLookupTableItem>::New();
  100. item->SetLookupTable(lut);
  101. this->addPlot(item);
  102. d->updateChart();
  103. return item;
  104. }
  105. // ----------------------------------------------------------------------------
  106. vtkPlot* ctkVTKScalarsToColorsView
  107. ::addColorTransferFunction(vtkColorTransferFunction* colorTF,
  108. bool editable)
  109. {
  110. Q_D(ctkVTKScalarsToColorsView);
  111. vtkSmartPointer<vtkColorTransferFunctionItem> item =
  112. vtkSmartPointer<vtkColorTransferFunctionItem>::New();
  113. item->SetColorTransferFunction(colorTF);
  114. this->addPlot(item);
  115. if (editable)
  116. {
  117. this->addColorTransferFunctionControlPoints(colorTF);
  118. }
  119. d->updateChart();
  120. return item;
  121. }
  122. // ----------------------------------------------------------------------------
  123. vtkPlot* ctkVTKScalarsToColorsView
  124. ::addOpacityFunction(vtkPiecewiseFunction* opacityTF,
  125. bool editable)
  126. {
  127. return this->addPiecewiseFunction(opacityTF, editable);
  128. }
  129. // ----------------------------------------------------------------------------
  130. vtkPlot* ctkVTKScalarsToColorsView
  131. ::addPiecewiseFunction(vtkPiecewiseFunction* piecewiseTF,
  132. bool editable)
  133. {
  134. Q_D(ctkVTKScalarsToColorsView);
  135. vtkSmartPointer<vtkPiecewiseFunctionItem> item =
  136. vtkSmartPointer<vtkPiecewiseFunctionItem>::New();
  137. item->SetPiecewiseFunction(piecewiseTF);
  138. QColor defaultColor = this->palette().highlight().color();
  139. item->SetColor(defaultColor.redF(), defaultColor.greenF(), defaultColor.blueF());
  140. item->SetMaskAboveCurve(true);
  141. this->addPlot(item);
  142. if (editable)
  143. {
  144. this->addPiecewiseFunctionControlPoints(piecewiseTF);
  145. }
  146. d->updateChart();
  147. return item;
  148. }
  149. // ----------------------------------------------------------------------------
  150. vtkPlot* ctkVTKScalarsToColorsView
  151. ::addCompositeFunction(vtkColorTransferFunction* colorTF,
  152. vtkPiecewiseFunction* opacityTF,
  153. bool colorTFEditable, bool opacityTFEditable)
  154. {
  155. Q_D(ctkVTKScalarsToColorsView);
  156. vtkSmartPointer<vtkCompositeTransferFunctionItem> item =
  157. vtkSmartPointer<vtkCompositeTransferFunctionItem>::New();
  158. item->SetColorTransferFunction(colorTF);
  159. item->SetOpacityFunction(opacityTF);
  160. item->SetMaskAboveCurve(true);
  161. this->addPlot(item);
  162. if (colorTFEditable && opacityTFEditable)
  163. {
  164. this->addCompositeFunctionControlPoints(colorTF, opacityTF);
  165. }
  166. else if (colorTFEditable)
  167. {
  168. this->addColorTransferFunctionControlPoints(colorTF);
  169. }
  170. else if (opacityTFEditable)
  171. {
  172. this->addOpacityFunctionControlPoints(opacityTF);
  173. }
  174. d->updateChart();
  175. return item;
  176. }
  177. // ----------------------------------------------------------------------------
  178. vtkPlot* ctkVTKScalarsToColorsView
  179. ::addColorTransferFunctionControlPoints(vtkColorTransferFunction* colorTF)
  180. {
  181. vtkSmartPointer<vtkColorTransferControlPointsItem> controlPointsItem =
  182. vtkSmartPointer<vtkColorTransferControlPointsItem>::New();
  183. controlPointsItem->SetColorTransferFunction(colorTF);
  184. this->addPlot(controlPointsItem);
  185. return controlPointsItem;
  186. }
  187. // ----------------------------------------------------------------------------
  188. vtkPlot* ctkVTKScalarsToColorsView
  189. ::addOpacityFunctionControlPoints(vtkPiecewiseFunction* opacityTF)
  190. {
  191. return this->addPiecewiseFunctionControlPoints(opacityTF);
  192. }
  193. // ----------------------------------------------------------------------------
  194. vtkPlot* ctkVTKScalarsToColorsView
  195. ::addCompositeFunctionControlPoints(vtkColorTransferFunction* colorTF,
  196. vtkPiecewiseFunction* opacityTF)
  197. {
  198. vtkSmartPointer<vtkCompositeControlPointsItem> controlPointsItem =
  199. vtkSmartPointer<vtkCompositeControlPointsItem>::New();
  200. controlPointsItem->SetColorTransferFunction(colorTF);
  201. controlPointsItem->SetOpacityFunction(opacityTF);
  202. this->addPlot(controlPointsItem);
  203. return controlPointsItem;
  204. }
  205. // ----------------------------------------------------------------------------
  206. vtkPlot* ctkVTKScalarsToColorsView
  207. ::addPiecewiseFunctionControlPoints(vtkPiecewiseFunction* piecewiseTF)
  208. {
  209. vtkSmartPointer<vtkPiecewiseControlPointsItem> controlPointsItem =
  210. vtkSmartPointer<vtkPiecewiseControlPointsItem>::New();
  211. controlPointsItem->SetPiecewiseFunction(piecewiseTF);
  212. this->addPlot(controlPointsItem);
  213. return controlPointsItem;
  214. }
  215. // ----------------------------------------------------------------------------
  216. QList<vtkPlot*> ctkVTKScalarsToColorsView::plots()const
  217. {
  218. QList<vtkPlot*> res;
  219. const vtkIdType count = this->chart()->GetNumberOfPlots();
  220. for(vtkIdType i = 0; i < count; ++i)
  221. {
  222. res << this->chart()->GetPlot(i);
  223. }
  224. return res;
  225. }
  226. // ----------------------------------------------------------------------------
  227. QList<vtkPlot*> ctkVTKScalarsToColorsView::lookupTablePlots()const
  228. {
  229. QList<vtkPlot*> res;
  230. foreach(vtkPlot* plot, this->plots())
  231. {
  232. if (vtkLookupTableItem::SafeDownCast(plot))
  233. {
  234. res << plot;
  235. }
  236. }
  237. return res;
  238. }
  239. // ----------------------------------------------------------------------------
  240. QList<vtkPlot*> ctkVTKScalarsToColorsView::lookupTablePlots(vtkLookupTable* lut)const
  241. {
  242. QList<vtkPlot*> res;
  243. foreach(vtkPlot* plot, this->lookupTablePlots())
  244. {
  245. vtkLookupTableItem* item = vtkLookupTableItem::SafeDownCast(plot);
  246. if (item->GetLookupTable() == lut)
  247. {
  248. res << plot;
  249. }
  250. }
  251. return res;
  252. }
  253. // ----------------------------------------------------------------------------
  254. QList<vtkPlot*> ctkVTKScalarsToColorsView::colorTransferFunctionPlots()const
  255. {
  256. QList<vtkPlot*> res;
  257. foreach(vtkPlot* plot, this->plots())
  258. {
  259. if (vtkColorTransferFunctionItem::SafeDownCast(plot) ||
  260. vtkColorTransferControlPointsItem::SafeDownCast(plot))
  261. {
  262. res << plot;
  263. }
  264. }
  265. return res;
  266. }
  267. // ----------------------------------------------------------------------------
  268. QList<vtkPlot*> ctkVTKScalarsToColorsView
  269. ::colorTransferFunctionPlots(vtkColorTransferFunction* colorTF)const
  270. {
  271. QList<vtkPlot*> res;
  272. foreach(vtkPlot* plot, this->colorTransferFunctionPlots())
  273. {
  274. vtkColorTransferFunctionItem* item =
  275. vtkColorTransferFunctionItem::SafeDownCast(plot);
  276. if (item
  277. && item->GetColorTransferFunction() == colorTF)
  278. {
  279. res << plot;
  280. }
  281. vtkColorTransferControlPointsItem* controlPointsItem =
  282. vtkColorTransferControlPointsItem::SafeDownCast(plot);
  283. if (controlPointsItem
  284. && controlPointsItem->GetColorTransferFunction() == colorTF)
  285. {
  286. res << plot;
  287. }
  288. }
  289. return res;
  290. }
  291. // ----------------------------------------------------------------------------
  292. QList<vtkPlot*> ctkVTKScalarsToColorsView::opacityFunctionPlots()const
  293. {
  294. QList<vtkPlot*> res;
  295. foreach(vtkPlot* plot, this->plots())
  296. {
  297. if (vtkPiecewiseFunctionItem::SafeDownCast(plot) ||
  298. vtkPiecewiseControlPointsItem::SafeDownCast(plot))
  299. {
  300. res << plot;
  301. }
  302. }
  303. return res;
  304. }
  305. // ----------------------------------------------------------------------------
  306. QList<vtkPlot*> ctkVTKScalarsToColorsView
  307. ::opacityFunctionPlots(vtkPiecewiseFunction* opacityTF)const
  308. {
  309. QList<vtkPlot*> res;
  310. foreach(vtkPlot* plot, this->opacityFunctionPlots())
  311. {
  312. vtkPiecewiseFunctionItem* item =
  313. vtkPiecewiseFunctionItem::SafeDownCast(plot);
  314. if (item
  315. && item->GetPiecewiseFunction() == opacityTF)
  316. {
  317. res << plot;
  318. }
  319. vtkPiecewiseControlPointsItem* controlPointsItem =
  320. vtkPiecewiseControlPointsItem::SafeDownCast(plot);
  321. if (controlPointsItem
  322. && controlPointsItem->GetPiecewiseFunction() == opacityTF)
  323. {
  324. res << plot;
  325. }
  326. vtkCompositeTransferFunctionItem* compositeItem =
  327. vtkCompositeTransferFunctionItem::SafeDownCast(plot);
  328. if (compositeItem
  329. && compositeItem->GetOpacityFunction() == opacityTF)
  330. {
  331. res << plot;
  332. }
  333. vtkCompositeControlPointsItem* compositeControlPointsItem =
  334. vtkCompositeControlPointsItem::SafeDownCast(plot);
  335. if (compositeControlPointsItem
  336. && compositeControlPointsItem->GetOpacityFunction() == opacityTF)
  337. {
  338. res << plot;
  339. }
  340. }
  341. return res;
  342. }
  343. // ----------------------------------------------------------------------------
  344. void ctkVTKScalarsToColorsView::setLookuptTableToPlots(vtkLookupTable* lut)
  345. {
  346. foreach(vtkLookupTableItem* plot,
  347. this->plots<vtkLookupTableItem>())
  348. {
  349. plot->SetLookupTable(lut);
  350. }
  351. }
  352. // ----------------------------------------------------------------------------
  353. void ctkVTKScalarsToColorsView
  354. ::setColorTransferFunctionToPlots(vtkColorTransferFunction* colorTF)
  355. {
  356. foreach(vtkColorTransferFunctionItem* plot,
  357. this->plots<vtkColorTransferFunctionItem>())
  358. {
  359. plot->SetColorTransferFunction(colorTF);
  360. }
  361. foreach(vtkColorTransferControlPointsItem* plot,
  362. this->plots<vtkColorTransferControlPointsItem>())
  363. {
  364. plot->SetColorTransferFunction(colorTF);
  365. }
  366. }
  367. // ----------------------------------------------------------------------------
  368. void ctkVTKScalarsToColorsView
  369. ::setOpacityFunctionToPlots(vtkPiecewiseFunction* opacityTF)
  370. {
  371. this->setPiecewiseFunctionToPlots(opacityTF);
  372. foreach(vtkCompositeTransferFunctionItem* plot,
  373. this->plots<vtkCompositeTransferFunctionItem>())
  374. {
  375. plot->SetOpacityFunction(opacityTF);
  376. }
  377. foreach(vtkCompositeControlPointsItem* plot,
  378. this->plots<vtkCompositeControlPointsItem>())
  379. {
  380. plot->SetOpacityFunction(opacityTF);
  381. }
  382. }
  383. // ----------------------------------------------------------------------------
  384. void ctkVTKScalarsToColorsView
  385. ::setPiecewiseFunctionToPlots(vtkPiecewiseFunction* piecewiseTF)
  386. {
  387. foreach(vtkPiecewiseFunctionItem* plot,
  388. this->plots<vtkPiecewiseFunctionItem>())
  389. {
  390. plot->SetPiecewiseFunction(piecewiseTF);
  391. }
  392. foreach(vtkPiecewiseControlPointsItem* plot,
  393. this->plots<vtkPiecewiseControlPointsItem>())
  394. {
  395. plot->SetPiecewiseFunction(piecewiseTF);
  396. }
  397. }
  398. // ----------------------------------------------------------------------------
  399. void ctkVTKScalarsToColorsView::editPoint(vtkObject* caller, void* callData)
  400. {
  401. vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
  402. int pointToEdit = reinterpret_cast<unsigned long>(callData);
  403. if (!controlPoints || pointToEdit < 0)
  404. {
  405. return;
  406. }
  407. vtkColorTransferControlPointsItem* colorTransferFunctionItem =
  408. vtkColorTransferControlPointsItem::SafeDownCast(controlPoints);
  409. if (colorTransferFunctionItem)
  410. {
  411. double xrgbms[6];
  412. vtkColorTransferFunction* colorTF = colorTransferFunctionItem->GetColorTransferFunction();
  413. colorTF->GetNodeValue(pointToEdit, xrgbms);
  414. QColor oldColor = QColor::fromRgbF(xrgbms[0], xrgbms[1], xrgbms[2]);
  415. QColor newColor = QColorDialog::getColor(oldColor, this);
  416. if (newColor.isValid())
  417. {
  418. xrgbms[1] = newColor.redF();
  419. xrgbms[2] = newColor.greenF();
  420. xrgbms[3] = newColor.blueF();
  421. colorTF->SetNodeValue(pointToEdit, xrgbms);
  422. }
  423. }
  424. }