ctkVTKScalarsToColorsView.cpp 15 KB

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