ctkVTKDataSetModelTest1.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <QApplication>
  16. #include <QComboBox>
  17. #include <QTimer>
  18. #include <QDebug>
  19. // CTK includes
  20. #include "ctkVTKDataSetModel.h"
  21. // VTK includes
  22. #include <vtkCellData.h>
  23. #include <vtkFloatArray.h>
  24. #include <vtkIntArray.h>
  25. #include <vtkNew.h>
  26. #include <vtkPointData.h>
  27. #include <vtkPoints.h>
  28. #include <vtkPolyData.h>
  29. // STD includes
  30. #include <iostream>
  31. namespace
  32. {
  33. //-----------------------------------------------------------------------------
  34. bool checkItems(int line, const QList<vtkDataArray*>& expectedAttributeArrays, ctkVTKDataSetModel* dataSetModel)
  35. {
  36. foreach(vtkDataArray* expectedDataArray, expectedAttributeArrays)
  37. {
  38. QList<QStandardItem*> items = dataSetModel->findItems(expectedDataArray->GetName());
  39. if(items.count() != 1)
  40. {
  41. std::cerr << "Line " << line << " - Problem with number of item matching name:" << expectedDataArray->GetName() << "\n"
  42. "\tExpected count: 1\n"
  43. "\tCurrent count: " << items.count() << "\n";
  44. return false;
  45. }
  46. vtkDataArray * currentDataArray = dataSetModel->arrayFromItem(items.at(0));
  47. if (currentDataArray != expectedDataArray)
  48. {
  49. std::cerr << "Line " << line << " - Problem with model - Incorrect array associated with item name:" << expectedDataArray->GetName() << "\n"
  50. "\tExpected: " << expectedDataArray << "\n"
  51. "\tCurrent: " << currentDataArray << "\n";
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. int ctkVTKDataSetModelTest1(int argc, char * argv [] )
  60. {
  61. QApplication app(argc, argv);
  62. vtkNew<vtkPolyData> dataSet;
  63. vtkNew<vtkIntArray> ints;
  64. {
  65. ints->SetName("Ints");
  66. int added = dataSet->GetPointData()->AddArray(ints.GetPointer());
  67. if (added == -1)
  68. {
  69. std::cerr << "Line " << __LINE__ << " - Failed to add Ints array";
  70. return EXIT_FAILURE;
  71. }
  72. }
  73. vtkNew<vtkFloatArray> floats;
  74. {
  75. floats->SetName("Floats");
  76. int added = dataSet->GetCellData()->AddArray(floats.GetPointer());
  77. if (added == -1)
  78. {
  79. std::cerr << "Line " << __LINE__ << " - Failed to add Floats array";
  80. return EXIT_FAILURE;
  81. }
  82. }
  83. vtkNew<vtkFloatArray> scalars;
  84. {
  85. scalars->SetName("Scalars");
  86. scalars->SetNumberOfComponents(0);
  87. int added = dataSet->GetPointData()->SetScalars(scalars.GetPointer());
  88. if (added == -1)
  89. {
  90. std::cerr << "Line " << __LINE__ << " - Failed to add Scalars array";
  91. return EXIT_FAILURE;
  92. }
  93. }
  94. vtkNew<vtkFloatArray> vectors;
  95. {
  96. vectors->SetNumberOfComponents(3);
  97. vectors->SetName("Vectors");
  98. int added = dataSet->GetPointData()->SetVectors(vectors.GetPointer());
  99. if (added == -1)
  100. {
  101. std::cerr << "Line " << __LINE__ << " - Failed to add Vectors array";
  102. return EXIT_FAILURE;
  103. }
  104. }
  105. vtkNew<vtkFloatArray> normals;
  106. {
  107. normals->SetNumberOfComponents(3);
  108. normals->SetName("Normals");
  109. int added = dataSet->GetPointData()->SetNormals(normals.GetPointer());
  110. if (added == -1)
  111. {
  112. std::cerr << "Line " << __LINE__ << " - Failed to add Normals array";
  113. return EXIT_FAILURE;
  114. }
  115. }
  116. vtkNew<vtkFloatArray> tcoords;
  117. {
  118. tcoords->SetNumberOfComponents(3);
  119. tcoords->SetName("Tcoords");
  120. int added = dataSet->GetPointData()->SetTCoords(tcoords.GetPointer());
  121. if (added == -1)
  122. {
  123. std::cerr << "Line " << __LINE__ << " - Failed to add Tcoords array";
  124. return EXIT_FAILURE;
  125. }
  126. }
  127. vtkNew<vtkFloatArray> tensors;
  128. {
  129. tensors->SetNumberOfComponents(9);
  130. tensors->SetName("Tensors");
  131. int added = dataSet->GetPointData()->SetTensors(tensors.GetPointer());
  132. if (added == -1)
  133. {
  134. std::cerr << "Line " << __LINE__ << " - Failed to add Tensors array";
  135. return EXIT_FAILURE;
  136. }
  137. }
  138. vtkNew<vtkFloatArray> globalids;
  139. {
  140. globalids->SetNumberOfComponents(1);
  141. globalids->SetName("GlobalIDs");
  142. int added = dataSet->GetPointData()->SetGlobalIds(globalids.GetPointer());
  143. if (added == -1)
  144. {
  145. std::cerr << "Line " << __LINE__ << " - Failed to add GlobalIDs array";
  146. return EXIT_FAILURE;
  147. }
  148. }
  149. vtkNew<vtkFloatArray> pedigreeids;
  150. {
  151. pedigreeids->SetNumberOfComponents(1);
  152. pedigreeids->SetName("PedigreeIDs");
  153. int added = dataSet->GetPointData()->SetPedigreeIds(pedigreeids.GetPointer());
  154. if (added == -1)
  155. {
  156. std::cerr << "Line " << __LINE__ << " - Failed to add PedigreeIDs array";
  157. return EXIT_FAILURE;
  158. }
  159. }
  160. ctkVTKDataSetModel dataSetModel;
  161. dataSetModel.setDataSet(dataSet.GetPointer());
  162. QList<vtkDataArray*> notAttributeArrays;
  163. notAttributeArrays << ints.GetPointer() << floats.GetPointer();
  164. if (!checkItems(__LINE__, notAttributeArrays, &dataSetModel))
  165. {
  166. return EXIT_FAILURE;
  167. }
  168. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::ScalarsAttribute);
  169. if (!checkItems(__LINE__, QList<vtkDataArray*>() << scalars.GetPointer(), &dataSetModel))
  170. {
  171. return EXIT_FAILURE;
  172. }
  173. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::ScalarsAttribute);
  174. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << scalars.GetPointer(), &dataSetModel))
  175. {
  176. return EXIT_FAILURE;
  177. }
  178. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::VectorsAttribute);
  179. if (!checkItems(__LINE__, QList<vtkDataArray*>() << vectors.GetPointer(), &dataSetModel))
  180. {
  181. return EXIT_FAILURE;
  182. }
  183. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::VectorsAttribute);
  184. if (!checkItems(__LINE__, QList<vtkDataArray*>() << vectors.GetPointer(), &dataSetModel))
  185. {
  186. return EXIT_FAILURE;
  187. }
  188. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::VectorsAttribute);
  189. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << vectors.GetPointer(), &dataSetModel))
  190. {
  191. return EXIT_FAILURE;
  192. }
  193. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NormalsAttribute);
  194. if (!checkItems(__LINE__, QList<vtkDataArray*>() << normals.GetPointer(), &dataSetModel))
  195. {
  196. return EXIT_FAILURE;
  197. }
  198. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::NormalsAttribute);
  199. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << normals.GetPointer(), &dataSetModel))
  200. {
  201. return EXIT_FAILURE;
  202. }
  203. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::TCoordsAttribute);
  204. if (!checkItems(__LINE__, QList<vtkDataArray*>() << tcoords.GetPointer(), &dataSetModel))
  205. {
  206. return EXIT_FAILURE;
  207. }
  208. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::TCoordsAttribute);
  209. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << tcoords.GetPointer(), &dataSetModel))
  210. {
  211. return EXIT_FAILURE;
  212. }
  213. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::TensorsAttribute);
  214. if (!checkItems(__LINE__, QList<vtkDataArray*>() << tensors.GetPointer(), &dataSetModel))
  215. {
  216. return EXIT_FAILURE;
  217. }
  218. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::TensorsAttribute);
  219. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << tensors.GetPointer(), &dataSetModel))
  220. {
  221. return EXIT_FAILURE;
  222. }
  223. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::GlobalIDsAttribute);
  224. if (!checkItems(__LINE__, QList<vtkDataArray*>() << globalids.GetPointer(), &dataSetModel))
  225. {
  226. return EXIT_FAILURE;
  227. }
  228. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::GlobalIDsAttribute);
  229. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << globalids.GetPointer(), &dataSetModel))
  230. {
  231. return EXIT_FAILURE;
  232. }
  233. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::PedigreeIDsAttribute);
  234. if (!checkItems(__LINE__, QList<vtkDataArray*>() << pedigreeids.GetPointer(), &dataSetModel))
  235. {
  236. return EXIT_FAILURE;
  237. }
  238. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::NoAttribute | ctkVTKDataSetModel::PedigreeIDsAttribute);
  239. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays << pedigreeids.GetPointer(), &dataSetModel))
  240. {
  241. return EXIT_FAILURE;
  242. }
  243. dataSetModel.setAttributeTypes(ctkVTKDataSetModel::AllAttribute);
  244. if (!checkItems(__LINE__, QList<vtkDataArray*>() << notAttributeArrays
  245. << scalars.GetPointer() << vectors.GetPointer()
  246. << normals.GetPointer() << tcoords.GetPointer()
  247. << tensors.GetPointer() << globalids.GetPointer()
  248. << pedigreeids.GetPointer(), &dataSetModel))
  249. {
  250. return EXIT_FAILURE;
  251. }
  252. QComboBox comboBox;
  253. comboBox.setModel(&dataSetModel);
  254. comboBox.show();
  255. if (argc < 2 || QString(argv[1]) != "-I")
  256. {
  257. QTimer::singleShot(1000, &app, SLOT(quit()));
  258. }
  259. return app.exec();
  260. }