ctkVTKScalarsToColorsView.cpp 16 KB

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