ctkVTKScalarsToColorsView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. vtkCompositeTransferFunctionItem::SafeDownCast(plot) ||
  294. vtkCompositeControlPointsItem::SafeDownCast(plot))
  295. {
  296. res << plot;
  297. }
  298. }
  299. return res;
  300. }
  301. // ----------------------------------------------------------------------------
  302. QList<vtkPlot*> ctkVTKScalarsToColorsView
  303. ::opacityFunctionPlots(vtkPiecewiseFunction* opacityTF)const
  304. {
  305. QList<vtkPlot*> res;
  306. foreach(vtkPlot* plot, this->opacityFunctionPlots())
  307. {
  308. vtkPiecewiseFunctionItem* item =
  309. vtkPiecewiseFunctionItem::SafeDownCast(plot);
  310. if (item
  311. && item->GetPiecewiseFunction() == opacityTF)
  312. {
  313. res << plot;
  314. }
  315. vtkPiecewiseControlPointsItem* controlPointsItem =
  316. vtkPiecewiseControlPointsItem::SafeDownCast(plot);
  317. if (controlPointsItem
  318. && controlPointsItem->GetPiecewiseFunction() == opacityTF)
  319. {
  320. res << plot;
  321. }
  322. vtkCompositeTransferFunctionItem* compositeItem =
  323. vtkCompositeTransferFunctionItem::SafeDownCast(plot);
  324. if (compositeItem
  325. && compositeItem->GetOpacityFunction() == opacityTF)
  326. {
  327. res << plot;
  328. }
  329. vtkCompositeControlPointsItem* compositeControlPointsItem =
  330. vtkCompositeControlPointsItem::SafeDownCast(plot);
  331. if (compositeControlPointsItem
  332. && compositeControlPointsItem->GetOpacityFunction() == opacityTF)
  333. {
  334. res << plot;
  335. }
  336. }
  337. return res;
  338. }
  339. // ----------------------------------------------------------------------------
  340. void ctkVTKScalarsToColorsView::setLookuptTableToPlots(vtkLookupTable* lut)
  341. {
  342. foreach(vtkLookupTableItem* plot,
  343. this->plots<vtkLookupTableItem>())
  344. {
  345. plot->SetLookupTable(lut);
  346. }
  347. this->onChartUpdated();
  348. }
  349. // ----------------------------------------------------------------------------
  350. void ctkVTKScalarsToColorsView
  351. ::setColorTransferFunctionToPlots(vtkColorTransferFunction* colorTF)
  352. {
  353. foreach(vtkColorTransferFunctionItem* plot,
  354. this->plots<vtkColorTransferFunctionItem>())
  355. {
  356. plot->SetColorTransferFunction(colorTF);
  357. }
  358. foreach(vtkColorTransferControlPointsItem* plot,
  359. this->plots<vtkColorTransferControlPointsItem>())
  360. {
  361. plot->SetColorTransferFunction(colorTF);
  362. }
  363. this->onChartUpdated();
  364. }
  365. // ----------------------------------------------------------------------------
  366. void ctkVTKScalarsToColorsView
  367. ::setOpacityFunctionToPlots(vtkPiecewiseFunction* opacityTF)
  368. {
  369. this->setPiecewiseFunctionToPlots(opacityTF);
  370. foreach(vtkCompositeTransferFunctionItem* plot,
  371. this->plots<vtkCompositeTransferFunctionItem>())
  372. {
  373. plot->SetOpacityFunction(opacityTF);
  374. }
  375. foreach(vtkCompositeControlPointsItem* plot,
  376. this->plots<vtkCompositeControlPointsItem>())
  377. {
  378. plot->SetOpacityFunction(opacityTF);
  379. }
  380. this->onChartUpdated();
  381. }
  382. // ----------------------------------------------------------------------------
  383. void ctkVTKScalarsToColorsView
  384. ::setPiecewiseFunctionToPlots(vtkPiecewiseFunction* piecewiseTF)
  385. {
  386. foreach(vtkPiecewiseFunctionItem* plot,
  387. this->plots<vtkPiecewiseFunctionItem>())
  388. {
  389. plot->SetPiecewiseFunction(piecewiseTF);
  390. }
  391. foreach(vtkPiecewiseControlPointsItem* plot,
  392. this->plots<vtkPiecewiseControlPointsItem>())
  393. {
  394. plot->SetPiecewiseFunction(piecewiseTF);
  395. }
  396. this->onChartUpdated();
  397. }
  398. // ----------------------------------------------------------------------------
  399. void ctkVTKScalarsToColorsView
  400. ::setUserBoundsToPlots(double* bounds)
  401. {
  402. foreach(vtkScalarsToColorsItem* plot,
  403. this->plots<vtkScalarsToColorsItem>())
  404. {
  405. plot->SetUserBounds(bounds);
  406. }
  407. foreach(vtkControlPointsItem* plot,
  408. this->plots<vtkControlPointsItem>())
  409. {
  410. plot->SetUserBounds(bounds);
  411. }
  412. }
  413. // ----------------------------------------------------------------------------
  414. void ctkVTKScalarsToColorsView::editPoint(vtkObject* caller, void* callData)
  415. {
  416. vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
  417. int pointToEdit = reinterpret_cast<unsigned long>(callData);
  418. if (!controlPoints || pointToEdit < 0)
  419. {
  420. return;
  421. }
  422. vtkColorTransferControlPointsItem* colorTransferFunctionItem =
  423. vtkColorTransferControlPointsItem::SafeDownCast(controlPoints);
  424. vtkCompositeControlPointsItem* compositeControlPoints =
  425. vtkCompositeControlPointsItem::SafeDownCast(controlPoints);
  426. if (colorTransferFunctionItem &&
  427. (!compositeControlPoints ||
  428. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorPointsFunction ||
  429. compositeControlPoints->GetPointsFunction() == vtkCompositeControlPointsItem::ColorAndOpacityPointsFunction))
  430. {
  431. double xrgbms[6];
  432. vtkColorTransferFunction* colorTF = colorTransferFunctionItem->GetColorTransferFunction();
  433. colorTF->GetNodeValue(pointToEdit, xrgbms);
  434. QColor oldColor = QColor::fromRgbF(xrgbms[1], xrgbms[2], xrgbms[3]);
  435. QColor newColor = QColorDialog::getColor(oldColor, this);
  436. if (newColor.isValid())
  437. {
  438. xrgbms[1] = newColor.redF();
  439. xrgbms[2] = newColor.greenF();
  440. xrgbms[3] = newColor.blueF();
  441. colorTF->SetNodeValue(pointToEdit, xrgbms);
  442. }
  443. }
  444. }
  445. // ----------------------------------------------------------------------------
  446. void ctkVTKScalarsToColorsView::setAxesToChartBounds()
  447. {
  448. vtkChartXY* chart = this->chart();
  449. double userBounds[8];
  450. this->chartUserBounds(userBounds);
  451. if (userBounds[0] < userBounds[1])
  452. {
  453. this->setUserBoundsToPlots(userBounds);
  454. }
  455. }