ctkVTKScalarsToColorsView.cpp 17 KB

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