PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshCellInterfaceTest.cxx

https://github.com/paniwani/ITK
C++ | 459 lines | 346 code | 70 blank | 43 comment | 56 complexity | d3f696a1750c93dd0060d28ad8e373f5 MD5 | raw file
  1. /*=========================================================================
  2. *
  3. * Copyright Insight Software Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0.txt
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. *=========================================================================*/
  18. #include <iostream>
  19. #include "itkQuadEdgeMesh.h"
  20. #include "itkHexahedronCell.h"
  21. #include "itkTetrahedronCell.h"
  22. #include "itkQuadraticTriangleCell.h"
  23. #include "itkPolygonCell.h"
  24. #include "itkCellInterfaceVisitor.h"
  25. /**
  26. * Define a mesh type that stores a PixelType of "int". Use the defaults
  27. * for the other template parameters.
  28. */
  29. typedef int PixelType;
  30. typedef itk::QuadEdgeMesh<PixelType, 3> MeshType;
  31. typedef MeshType::CellTraits CellTraits;
  32. typedef CellTraits::QuadEdgeType QEType;
  33. typedef itk::CellInterface< int, CellTraits > CellInterfaceType;
  34. typedef itk::QuadEdgeMeshLineCell<CellInterfaceType> QELineCellType;
  35. typedef itk::QuadEdgeMeshPolygonCell<CellInterfaceType> QEPolygonCellType;
  36. /**
  37. * Typedef the generic cell type for the mesh. It is an abstract class,
  38. * so we can only use information from it, like get its pointer type.
  39. */
  40. typedef MeshType::CellType CellType;
  41. typedef CellType::CellAutoPointer CellAutoPointer;
  42. class CustomQELineVisitor
  43. {
  44. public:
  45. void Visit(unsigned long cellId, QELineCellType * t )
  46. {
  47. (void)cellId;
  48. (void)t;
  49. }
  50. };
  51. class CustomQEPolyVisitor
  52. {
  53. public:
  54. void Visit(unsigned long cellId, QEPolygonCellType * t )
  55. {
  56. (void)cellId;
  57. (void)t;
  58. }
  59. };
  60. // Test the cell interface
  61. template<class TCell> int TestCellInterface(std::string name, TCell *aCell)
  62. {
  63. CellAutoPointer cell(aCell,true);
  64. const TCell * cell2 = aCell;
  65. std::cout << "-------- " << name << "("
  66. << aCell->GetNameOfClass() << ")" << std::endl;
  67. std::cout << " Type: " << cell->GetType() << std::endl;
  68. std::cout << " Dimension: " << cell->GetDimension() << std::endl;
  69. std::cout << " NumberOfPoints: " << cell->GetNumberOfPoints() << std::endl;
  70. std::cout << " NumberOfBoundaryFeatures:" << std::endl;
  71. // Note the <= is here to test the default case
  72. for (unsigned int i = 0; i <= cell->GetDimension(); i++)
  73. {
  74. std::cout << " " << i << ": " << cell->GetNumberOfBoundaryFeatures(i)
  75. << std::endl;
  76. for (unsigned int j = 0; j < cell->GetNumberOfBoundaryFeatures(i); j++)
  77. {
  78. CellAutoPointer feature;
  79. cell->GetBoundaryFeature(i, j, feature);
  80. }
  81. }
  82. std::cout << " Iterator test: PointIds for empty cell: ";
  83. typename TCell::PointIdIterator pointId = cell->PointIdsBegin();
  84. typename TCell::PointIdIterator endId = cell->PointIdsEnd();
  85. while (pointId != endId)
  86. {
  87. std::cout << *pointId << ", ";
  88. pointId++;
  89. }
  90. std::cout << std::endl;
  91. std::cout << " ConstIterator test: PointIds for empty cell: ";
  92. typename TCell::PointIdConstIterator cpointId = cell2->PointIdsBegin();
  93. typename TCell::PointIdConstIterator cendId = cell2->PointIdsEnd();
  94. while (cpointId != cendId)
  95. {
  96. std::cout << *cpointId << ", ";
  97. cpointId++;
  98. }
  99. std::cout << std::endl;
  100. // Add point ids
  101. std::cout << " SetPointIds" << std::endl;
  102. typedef MeshType::PointIdentifier PointIdentifier;
  103. PointIdentifier *pointIds = new PointIdentifier[cell->GetNumberOfPoints() * 2];
  104. for (unsigned int i = 0; i < cell->GetNumberOfPoints() * 2; i++)
  105. {
  106. pointIds[i] = i;
  107. }
  108. cell->SetPointIds(pointIds);
  109. // exercing the const GetPointIds() method
  110. // null for QE Cells
  111. if(cell2->GetPointIds())
  112. {
  113. cell->SetPointIds(cell2->GetPointIds());
  114. }
  115. if (cell->GetNumberOfPoints() > 0)
  116. {
  117. cell->SetPointId(0, 100);
  118. }
  119. std::cout << " ConstIterator test: PointIds for populated cell: ";
  120. typename TCell::PointIdConstIterator ppointId = cell2->PointIdsBegin();
  121. typename TCell::PointIdConstIterator pendId = cell2->PointIdsEnd();
  122. while (ppointId != pendId)
  123. {
  124. std::cout << *ppointId << ", ";
  125. ppointId++;
  126. }
  127. std::cout << std::endl;
  128. cell->SetPointIds(&pointIds[cell->GetNumberOfPoints()],
  129. &pointIds[cell->GetNumberOfPoints() * 2]);
  130. std::cout << " Iterator test: PointIds for populated cell: ";
  131. typename TCell::PointIdIterator pxpointId = cell->PointIdsBegin();
  132. typename TCell::PointIdIterator pxendId = cell->PointIdsEnd();
  133. while (pxpointId != pxendId)
  134. {
  135. std::cout << *pxpointId << ", ";
  136. pxpointId++;
  137. }
  138. std::cout << std::endl;
  139. // Make a copy
  140. CellAutoPointer copyOfCell;
  141. cell->MakeCopy(copyOfCell);
  142. std::cout << " PointIds for copied cell: ";
  143. typename TCell::PointIdConstIterator xpointId = copyOfCell->PointIdsBegin();
  144. typename TCell::PointIdConstIterator xendId = copyOfCell->PointIdsEnd();
  145. while (xpointId != xendId)
  146. {
  147. std::cout << *xpointId << ", ";
  148. xpointId++;
  149. }
  150. std::cout << std::endl;
  151. delete []pointIds;
  152. return EXIT_SUCCESS;
  153. }
  154. // Test the QEcell interface
  155. template<class TCell> int TestQECellInterface(std::string name, TCell *aCell)
  156. {
  157. std::cout << "-------- " << name << "("
  158. << aCell->GetNameOfClass() << ")" << std::endl;
  159. TCell * cell = aCell;
  160. const TCell * cell2 = aCell;
  161. std::cout << " QE Iterator test: PointIds for empty cell: ";
  162. typename TCell::PointIdInternalIterator pointId
  163. = cell->InternalPointIdsBegin();
  164. typename TCell::PointIdInternalIterator endId
  165. = cell->InternalPointIdsEnd();
  166. while (pointId != endId)
  167. {
  168. std::cout << *pointId << ", ";
  169. pointId++;
  170. }
  171. std::cout << std::endl;
  172. std::cout << " ConstIterator test: PointIds for empty cell: ";
  173. typename TCell::PointIdInternalConstIterator cpointId
  174. = cell2->InternalPointIdsBegin();
  175. typename TCell::PointIdInternalConstIterator cendId
  176. = cell2->InternalPointIdsEnd();
  177. while (cpointId != cendId)
  178. {
  179. std::cout << *cpointId << ", ";
  180. cpointId++;
  181. }
  182. std::cout << std::endl;
  183. // Add point ids
  184. std::cout << " SetPointIds" << std::endl;
  185. typedef typename TCell::PointIdentifier PointIdentifier;
  186. PointIdentifier *pointIds = new PointIdentifier[cell->GetNumberOfPoints() * 2];
  187. for (unsigned int i = 0; i < cell->GetNumberOfPoints() * 2; i++)
  188. {
  189. pointIds[i] = i;
  190. }
  191. // actually populate
  192. cell->SetPointIds(pointIds);
  193. // exercing the non const internal equivalent.
  194. cell->InternalSetPointIds( cell->InternalGetPointIds( ) );
  195. // exercing the const internal equivalent
  196. cell->InternalSetPointIds( cell2->InternalGetPointIds( ));
  197. std::cout << " ConstIterator test: PointIds for populated cell: ";
  198. typename TCell::PointIdInternalConstIterator ppointId
  199. = cell2->InternalPointIdsBegin();
  200. typename TCell::PointIdInternalConstIterator pendId
  201. = cell2->InternalPointIdsEnd();
  202. while (ppointId != pendId)
  203. {
  204. std::cout << *ppointId << ", ";
  205. ppointId++;
  206. }
  207. std::cout << std::endl;
  208. cell->InternalSetPointIds( cell2->InternalPointIdsBegin(),
  209. cell2->InternalPointIdsEnd());
  210. std::cout << " Iterator test: PointIds for populated cell: ";
  211. typename TCell::PointIdInternalIterator pxpointId
  212. = cell->InternalPointIdsBegin();
  213. typename TCell::PointIdInternalIterator pxendId
  214. = cell->InternalPointIdsEnd();
  215. while (pxpointId != pxendId)
  216. {
  217. std::cout << *pxpointId << ", ";
  218. pxpointId++;
  219. }
  220. std::cout << std::endl;
  221. delete []pointIds;
  222. return EXIT_SUCCESS;
  223. }
  224. int itkQuadEdgeMeshCellInterfaceTest(int, char* [] )
  225. {
  226. int status;
  227. /*
  228. * ITK NATIVE CELLS
  229. */
  230. typedef itk::VertexCell<CellInterfaceType> VertexCellType;
  231. status = TestCellInterface("Vertex", new VertexCellType);
  232. if (status != 0)
  233. {
  234. return EXIT_FAILURE;
  235. }
  236. typedef itk::LineCell<CellInterfaceType> LineCellType;
  237. status = TestCellInterface("Line", new LineCellType);
  238. if (status != 0)
  239. {
  240. return EXIT_FAILURE;
  241. }
  242. typedef itk::TriangleCell<CellInterfaceType> TriangleCellType;
  243. status = TestCellInterface("Triangle", new TriangleCellType);
  244. if (status != 0)
  245. {
  246. return EXIT_FAILURE;
  247. }
  248. typedef itk::HexahedronCell<CellInterfaceType> HexahedronCellType;
  249. status = TestCellInterface("HexahedronCell", new HexahedronCellType);
  250. if (status != 0)
  251. {
  252. return EXIT_FAILURE;
  253. }
  254. typedef itk::TetrahedronCell<CellInterfaceType> TetrahedronCellType;
  255. status = TestCellInterface("TetrahedronCell", new TetrahedronCellType);
  256. if (status != 0)
  257. {
  258. return EXIT_FAILURE;
  259. }
  260. typedef itk::QuadraticEdgeCell<CellInterfaceType> QuadraticEdgeCellType;
  261. status = TestCellInterface("QuadraticEdgeCell", new QuadraticEdgeCellType);
  262. if (status != 0)
  263. {
  264. return EXIT_FAILURE;
  265. }
  266. typedef itk::QuadraticTriangleCell<CellInterfaceType>
  267. QuadraticTriangleCellType;
  268. status = TestCellInterface("QuadraticTriangleCell",
  269. new QuadraticTriangleCellType);
  270. if (status != 0)
  271. {
  272. return EXIT_FAILURE;
  273. }
  274. typedef itk::QuadrilateralCell<CellInterfaceType> QuadrilateralCellType;
  275. status = TestCellInterface("QuadrilateralCell",
  276. new QuadrilateralCellType);
  277. if (status != 0)
  278. {
  279. return EXIT_FAILURE;
  280. }
  281. typedef itk::PolygonCell<CellInterfaceType> PolygonCellType;
  282. status = TestCellInterface("PolygonCell with 0 vertices",
  283. new PolygonCellType());
  284. if (status != 0)
  285. {
  286. return EXIT_FAILURE;
  287. }
  288. typedef itk::PolygonCell<CellInterfaceType> PolygonCellType;
  289. status = TestCellInterface("PolygonCell with 5 vertices",
  290. new PolygonCellType(5));
  291. if (status != 0)
  292. {
  293. return EXIT_FAILURE;
  294. }
  295. // ITK QuadEdgeMesh CELLS - Standard cell API test
  296. QEPolygonCellType* tempQEPolygon = new QEPolygonCellType;
  297. status = TestCellInterface("QuadEdgePolygonCell with 0 vertices",
  298. tempQEPolygon);
  299. if (status != 0)
  300. {
  301. return EXIT_FAILURE;
  302. }
  303. tempQEPolygon = new QEPolygonCellType(5);
  304. status = TestCellInterface("QuadEdgePolygonCell with 5 vertices",
  305. tempQEPolygon);
  306. if (status != 0)
  307. {
  308. return EXIT_FAILURE;
  309. }
  310. status = TestCellInterface("QuadEdgeLineCell", new QELineCellType());
  311. if (status != 0)
  312. {
  313. return EXIT_FAILURE;
  314. }
  315. // ITK QuadEdgeMesh CELLS - Specific QEcell API test
  316. tempQEPolygon = new QEPolygonCellType();
  317. status = TestQECellInterface("QuadEdgePolygonCell with 0 vertices",
  318. tempQEPolygon);
  319. delete tempQEPolygon;
  320. if (status != 0)
  321. {
  322. return EXIT_FAILURE;
  323. }
  324. tempQEPolygon = new QEPolygonCellType(5);
  325. status = TestQECellInterface("QuadEdgePolygonCell with 5 vertices",
  326. tempQEPolygon);
  327. delete tempQEPolygon;
  328. if (status != 0)
  329. {
  330. return EXIT_FAILURE;
  331. }
  332. QELineCellType* tempQELine = new QELineCellType;
  333. status = TestQECellInterface("QuadEdgeLineCell", tempQELine);
  334. delete tempQELine;
  335. if (status != 0)
  336. {
  337. return EXIT_FAILURE;
  338. }
  339. // test the visitor API
  340. typedef itk::CellInterfaceVisitorImplementation<
  341. PixelType, MeshType::CellTraits,
  342. QELineCellType, CustomQELineVisitor
  343. > QELineVisitorInterfaceType;
  344. QELineVisitorInterfaceType::Pointer QELineVisitor =
  345. QELineVisitorInterfaceType::New();
  346. typedef itk::CellInterfaceVisitorImplementation<
  347. PixelType, MeshType::CellTraits,
  348. QEPolygonCellType, CustomQEPolyVisitor
  349. > QEPolyVisitorInterfaceType;
  350. QEPolyVisitorInterfaceType::Pointer QEPolyVisitor =
  351. QEPolyVisitorInterfaceType::New();
  352. typedef CellType::MultiVisitor CellMultiVisitorType;
  353. CellMultiVisitorType::Pointer multiVisitor = CellMultiVisitorType::New();
  354. multiVisitor->AddVisitor( QELineVisitor );
  355. multiVisitor->AddVisitor( QEPolyVisitor );
  356. MeshType::Pointer mesh = MeshType::New( );
  357. MeshType::PointType pts[3];
  358. pts[0][0] = 0; pts[0][1] = 0; pts[0][2] = 0;
  359. pts[1][0] = 0; pts[1][1] = 0; pts[1][2] = 1;
  360. pts[2][0] = 0; pts[2][1] = 1; pts[2][2] = 0;
  361. mesh->SetPoint( 0, pts[0] );
  362. mesh->SetPoint( 1, pts[1] );
  363. mesh->SetPoint( 2, pts[2] );
  364. mesh->AddFaceTriangle( 0, 1, 2 );
  365. mesh->Accept( multiVisitor );
  366. // test 4 very specific QELineCell destructor cases
  367. QELineCellType* test = new QELineCellType();
  368. QEType* m_QuadEdgeGeom = test->GetQEGeom( );
  369. delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  370. m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( NULL );
  371. delete test;
  372. test = new QELineCellType();
  373. m_QuadEdgeGeom = test->GetQEGeom( );
  374. delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  375. m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( NULL );
  376. delete m_QuadEdgeGeom->GetRot( )->GetRot( );
  377. m_QuadEdgeGeom->GetRot( )->SetRot( NULL );
  378. delete test;
  379. test = new QELineCellType();
  380. m_QuadEdgeGeom = test->GetQEGeom( );
  381. delete m_QuadEdgeGeom->GetRot( )->GetRot( )->GetRot( );
  382. m_QuadEdgeGeom->GetRot( )->GetRot( )->SetRot( NULL );
  383. delete m_QuadEdgeGeom->GetRot( )->GetRot( );
  384. m_QuadEdgeGeom->GetRot( )->SetRot( NULL );
  385. delete m_QuadEdgeGeom->GetRot( );
  386. m_QuadEdgeGeom->SetRot( NULL );
  387. delete test;
  388. return status;
  389. }