ctkVTKHistogram.cpp 10 KB

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