ctkVTKHistogram.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <QColor>
  16. #include <QDebug>
  17. /// CTK includes
  18. #include "ctkVTKHistogram.h"
  19. /// VTK includes
  20. #include <vtkDataArray.h>
  21. #include <vtkIntArray.h>
  22. #include <vtkMath.h>
  23. #include <vtkSmartPointer.h>
  24. /// STL include
  25. #include <limits>
  26. //-----------------------------------------------------------------------------
  27. class ctkVTKHistogramPrivate
  28. {
  29. public:
  30. ctkVTKHistogramPrivate();
  31. vtkSmartPointer<vtkDataArray> DataArray;
  32. vtkSmartPointer<vtkIntArray> Bins;
  33. int UserNumberOfBins;
  34. int Component;
  35. mutable double Range[2];
  36. int MinBin;
  37. int MaxBin;
  38. int computeNumberOfBins()const;
  39. };
  40. //-----------------------------------------------------------------------------
  41. ctkVTKHistogramPrivate::ctkVTKHistogramPrivate()
  42. {
  43. this->Bins = vtkSmartPointer<vtkIntArray>::New();
  44. this->UserNumberOfBins = -1;
  45. this->Component = 0;
  46. this->Range[0] = this->Range[1] = 0.;
  47. this->MinBin = 0;
  48. this->MaxBin = 0;
  49. }
  50. //-----------------------------------------------------------------------------
  51. int ctkVTKHistogramPrivate::computeNumberOfBins()const
  52. {
  53. if (this->DataArray.GetPointer() == 0)
  54. {
  55. return -1;
  56. }
  57. if (this->DataArray->GetDataType() == VTK_CHAR ||
  58. this->DataArray->GetDataType() == VTK_SIGNED_CHAR ||
  59. this->DataArray->GetDataType() == VTK_UNSIGNED_CHAR)
  60. {
  61. this->Range[0] = this->DataArray->GetDataTypeMin();
  62. this->Range[1] = this->DataArray->GetDataTypeMax();
  63. }
  64. else
  65. {
  66. this->DataArray->GetRange(this->Range, this->Component);
  67. if (this->DataArray->GetDataType() == VTK_FLOAT ||
  68. this->DataArray->GetDataType() == VTK_DOUBLE)
  69. {
  70. this->Range[1] += 0.01;
  71. }
  72. else
  73. {
  74. this->Range[1] += 1;
  75. }
  76. }
  77. if (this->UserNumberOfBins > 0)
  78. {
  79. return this->UserNumberOfBins;
  80. }
  81. return static_cast<int>(this->Range[1] - this->Range[0]);
  82. }
  83. //-----------------------------------------------------------------------------
  84. ctkVTKHistogram::ctkVTKHistogram(QObject* parentObject)
  85. :ctkHistogram(parentObject)
  86. , d_ptr(new ctkVTKHistogramPrivate)
  87. {
  88. }
  89. //-----------------------------------------------------------------------------
  90. ctkVTKHistogram::ctkVTKHistogram(vtkDataArray* dataArray,
  91. QObject* parentObject)
  92. :ctkHistogram(parentObject)
  93. , d_ptr(new ctkVTKHistogramPrivate)
  94. {
  95. this->setDataArray(dataArray);
  96. }
  97. //-----------------------------------------------------------------------------
  98. ctkVTKHistogram::~ctkVTKHistogram()
  99. {
  100. }
  101. //-----------------------------------------------------------------------------
  102. int ctkVTKHistogram::count()const
  103. {
  104. Q_D(const ctkVTKHistogram);
  105. return d->Bins->GetNumberOfTuples();
  106. }
  107. //-----------------------------------------------------------------------------
  108. void ctkVTKHistogram::range(qreal& minRange, qreal& maxRange)const
  109. {
  110. Q_D(const ctkVTKHistogram);
  111. if (d->DataArray.GetPointer() == 0)
  112. {
  113. Q_ASSERT(d->DataArray.GetPointer());
  114. minRange = 1.; // set incorrect values
  115. maxRange = 0.;
  116. return;
  117. }
  118. if (d->Range[0] == d->Range[1])
  119. {
  120. minRange = d->DataArray->GetDataTypeMin();
  121. maxRange = d->DataArray->GetDataTypeMax();
  122. return;
  123. }
  124. minRange = d->Range[0];
  125. maxRange = d->Range[1];
  126. }
  127. //-----------------------------------------------------------------------------
  128. QVariant ctkVTKHistogram::minValue()const
  129. {
  130. //Q_D(const ctkVTKHistogram);
  131. return 0;//d->MinBin;
  132. }
  133. //-----------------------------------------------------------------------------
  134. QVariant ctkVTKHistogram::maxValue()const
  135. {
  136. Q_D(const ctkVTKHistogram);
  137. return d->MaxBin;
  138. }
  139. //-----------------------------------------------------------------------------
  140. ctkControlPoint* ctkVTKHistogram::controlPoint(int index)const
  141. {
  142. Q_D(const ctkVTKHistogram);
  143. ctkHistogramBar* cp = new ctkHistogramBar();
  144. cp->P.X = this->indexToPos(index);
  145. cp->P.Value = d->Bins->GetValue(index);
  146. return cp;
  147. }
  148. //-----------------------------------------------------------------------------
  149. QVariant ctkVTKHistogram::value(qreal pos)const
  150. {
  151. QSharedPointer<ctkControlPoint> point(this->controlPoint(this->posToIndex(pos)));
  152. return point->value();
  153. }
  154. //-----------------------------------------------------------------------------
  155. qreal ctkVTKHistogram::indexToPos(int index)const
  156. {
  157. qreal posRange[2];
  158. this->range(posRange[0], posRange[1]);
  159. return posRange[0] + index * ((posRange[1] - posRange[0]) / (this->count() - 1));
  160. }
  161. //-----------------------------------------------------------------------------
  162. int ctkVTKHistogram::posToIndex(qreal pos)const
  163. {
  164. qreal posRange[2];
  165. this->range(posRange[0], posRange[1]);
  166. return (pos - posRange[0]) / ((posRange[1] - posRange[0]) / (this->count() - 1));
  167. }
  168. //-----------------------------------------------------------------------------
  169. void ctkVTKHistogram::setDataArray(vtkDataArray* newDataArray)
  170. {
  171. Q_D(ctkVTKHistogram);
  172. d->DataArray = newDataArray;
  173. this->qvtkReconnect(d->DataArray,vtkCommand::ModifiedEvent,
  174. this, SIGNAL(changed()));
  175. emit changed();
  176. }
  177. //-----------------------------------------------------------------------------
  178. vtkDataArray* ctkVTKHistogram::dataArray()const
  179. {
  180. Q_D(const ctkVTKHistogram);
  181. return d->DataArray;
  182. }
  183. //-----------------------------------------------------------------------------
  184. void ctkVTKHistogram::setComponent(int component)
  185. {
  186. Q_D(ctkVTKHistogram);
  187. d->Component = component;
  188. // need rebuild
  189. }
  190. //-----------------------------------------------------------------------------
  191. int ctkVTKHistogram::component()const
  192. {
  193. Q_D(const ctkVTKHistogram);
  194. return d->Component;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void ctkVTKHistogram::setNumberOfBins(int number)
  198. {
  199. Q_D(ctkVTKHistogram);
  200. d->UserNumberOfBins = number;
  201. }
  202. //-----------------------------------------------------------------------------
  203. template <class T>
  204. void populateBins(vtkIntArray* bins, const ctkVTKHistogram* histogram)
  205. {
  206. vtkDataArray* scalars = histogram->dataArray();
  207. int* binsPtr = bins->WritePointer(0, bins->GetNumberOfTuples());
  208. // reset bins to 0
  209. memset(binsPtr, 0, bins->GetNumberOfComponents()*bins->GetNumberOfTuples()*sizeof(int));
  210. const vtkIdType componentNumber = scalars->GetNumberOfComponents();
  211. const vtkIdType tupleNumber = scalars->GetNumberOfTuples();
  212. int component = histogram->component();
  213. double range[2];
  214. histogram->range(range[0], range[1]);
  215. T offset = static_cast<T>(range[0]);
  216. T* ptr = static_cast<T*>(scalars->WriteVoidPointer(0, tupleNumber));
  217. T* endPtr = ptr + tupleNumber * componentNumber;
  218. ptr += component;
  219. for (; ptr < endPtr; ptr += componentNumber)
  220. {
  221. Q_ASSERT( (static_cast<long long>(*ptr) - offset) ==
  222. (static_cast<int>(*ptr) - offset));
  223. binsPtr[static_cast<int>(*ptr - offset)]++;
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. template <class T>
  228. void populateIrregularBins(vtkIntArray* bins, const ctkVTKHistogram* histogram)
  229. {
  230. vtkDataArray* scalars = histogram->dataArray();
  231. int* binsPtr = bins->WritePointer(0, bins->GetNumberOfComponents()*bins->GetNumberOfTuples());
  232. // reset bins to 0
  233. memset(binsPtr, 0, bins->GetNumberOfTuples() * sizeof(int));
  234. const vtkIdType componentNumber = scalars->GetNumberOfComponents();
  235. const vtkIdType tupleNumber = scalars->GetNumberOfTuples();
  236. int component = histogram->component();
  237. double range[2];
  238. histogram->range(range[0], range[1]);
  239. double offset = range[0];
  240. double binWidth = 1.;
  241. if (range[1] != range[0])
  242. {
  243. binWidth = static_cast<double>(bins->GetNumberOfTuples()) / (range[1] - range[0]);
  244. }
  245. T* ptr = static_cast<T*>(scalars->WriteVoidPointer(0, tupleNumber));
  246. T* endPtr = ptr + tupleNumber * componentNumber;
  247. ptr += component;
  248. for (; ptr < endPtr; ptr += componentNumber)
  249. {
  250. if (std::numeric_limits<T>::has_quiet_NaN &&
  251. std::numeric_limits<T>::quiet_NaN() == *ptr)
  252. {
  253. continue;
  254. }
  255. binsPtr[vtkMath::Floor((static_cast<double>(*ptr) - offset) * binWidth)]++;
  256. }
  257. }
  258. //-----------------------------------------------------------------------------
  259. void ctkVTKHistogram::build()
  260. {
  261. Q_D(ctkVTKHistogram);
  262. if (d->DataArray.GetPointer() == 0)
  263. {
  264. d->MinBin = 0;
  265. d->MaxBin = 0;
  266. d->Bins->SetNumberOfTuples(0);
  267. return;
  268. }
  269. const int binCount = d->computeNumberOfBins();
  270. d->Bins->SetNumberOfComponents(1);
  271. d->Bins->SetNumberOfTuples(binCount);
  272. if (static_cast<double>(binCount) != (d->Range[1] - d->Range[2]))
  273. {
  274. switch(d->DataArray->GetDataType())
  275. {
  276. vtkTemplateMacro(populateIrregularBins<VTK_TT>(d->Bins, this));
  277. }
  278. }
  279. else
  280. {
  281. switch(d->DataArray->GetDataType())
  282. {
  283. vtkTemplateMacro(populateBins<VTK_TT>(d->Bins, this));
  284. }
  285. }
  286. // update Min/Max values
  287. int* binPtr = d->Bins->GetPointer(0);
  288. int* endPtr = d->Bins->GetPointer(binCount-1);
  289. d->MinBin = *endPtr;
  290. d->MaxBin = *endPtr;
  291. for (;binPtr < endPtr; ++binPtr)
  292. {
  293. d->MinBin = qMin(*binPtr, d->MinBin);
  294. d->MaxBin = qMax(*binPtr, d->MaxBin);
  295. }
  296. emit changed();
  297. }
  298. //-----------------------------------------------------------------------------
  299. void ctkVTKHistogram::removeControlPoint( qreal pos )
  300. {
  301. Q_UNUSED(pos);
  302. // TO BE IMPLEMENTED
  303. }