单元格以指针的方式存储在itk::Mesh中,指针指向通用单元格itk::CellInterface。
- 这意味着只能调用在这个基单元格类上定义的虚方法。
- 为了使用特定于每个单元格类型的方法,必须向下转换为指向单元格的实际类型指针。这可以通过利用允许识别单元格的实际类型的GetType()方法安全地完成。
下例,假设建立了一个由一个四面体及其所有边界面定义的网格。也就是四个三角形,六个边和四个顶点。
可以使用CellsContainer迭代器访问单元格。迭代器Value()对应于CellType基类的原始指针。
typedef MeshType::CellsContainer::ConstIterator CellIterator;
CellIterator cellIterator = mesh->GetCells()->Begin();
CellIterator cellEnd = mesh->GetCells()->End();
while( cellIterator != cellEnd )
{
CellType * cell = cellIterator.Value();
std::cout << cell->GetNumberOfPoints() << std::endl;
++cellIterator;
}
为了以安全的方式执行向下转换,可以首先使用GetType()方法查询单元格类型。单元格类型的代码已经在itkCellInterface.h头文件中用枚举类型定义,有:
• VERTEX CELL
• LINE CELL
• TRIANGLE CELL
• QUADRILATERAL CELL
• POLYGON CELL
• TETRAHEDRON CELL
• HEXAHEDRON CELL
• QUADRATIC EDGE CELL
• QUADRATIC TRIANGLE CELL
方法GetType()返回这些代码之一。然后可以在将单元格的指针向下转换为实际类型之前,测试单元格的类型。
- 例如,下面的代码访问网格中的所有单元格,并测试哪些单元格实际上是LINE CELL类型。只有那些单元格向下映射为LineType单元格,并调用LineType特定的方法。
cellIterator = mesh->GetCells()->Begin();
cellEnd = mesh->GetCells()->End();
while( cellIterator != cellEnd )
{
CellType * cell = cellIterator.Value();
if( cell->GetType() == CellType::LINE_CELL )
{
LineType * line = static_cast<LineType *>( cell );
std::cout << "dimension = " <<
line->GetDimension(); std::cout << " # points = "
<< line->GetNumberOfPoints();
std::cout << std::endl;
}
++cellIterator;
}
为了对不同的单元格类型执行不同的操作,switch语句可以用于每个单元格类型的case。下面的代码演示了对单元格的迭代以及对每种单元格类型的不同方法的调用。
cellIterator = mesh->GetCells()->Begin();
cellEnd = mesh->GetCells()->End();
while( cellIterator != cellEnd )
{
CellType * cell = cellIterator.Value();
switch( cell->GetType() )
{
case CellType::VERTEX_CELL:
{
std::cout << "VertexCell : " << std::endl;
VertexType * line = dynamic_cast<VertexType *>( cell );
std::cout << "dimension = " << line->GetDimension() << std::endl;
std::cout << "# points = " << line->GetNumberOfPoints() << std::endl;
break;
}
case CellType::LINE_CELL:
{
std::cout << "LineCell : " << std::endl;
LineType * line = dynamic_cast<LineType *>( cell );
std::cout << "dimension = " << line->GetDimension() << std::endl;
std::cout << "# points = " << line->GetNumberOfPoints() << std::endl;
break;
}
case CellType::TRIANGLE_CELL:
{
std::cout << "TriangleCell : " << std::endl;
TriangleType * line = dynamic_cast<TriangleType *>( cell );
std::cout << "dimension = " << line->GetDimension() << std::endl;
std::cout << "# points = " << line->GetNumberOfPoints() << std::endl;
break;
}
default:
{
std::cout << "Cell with more than three points" << std::endl;
std::cout << "dimension = " << cell->GetDimension() << std::endl;
std::cout << "# points = " << cell->GetNumberOfPoints() << std::endl;
break;
}
}
++cellIterator;
}