ctkVTKScalarsToColorsView.cpp 17 KB

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