前言:本博文主要介绍vtkUnstructuredGrid的特点、结构组成,vtkUnstructuredGrid的创建方法,及其vtkUnstructuredGrid相关的接口及示例。
特点
非结构化网格数据,是最常见的数据集类型,它的拓扑结构和几何结构都是非结构化的,所有单元类型都可以任意组合,所有单元的拓扑结构从零维延伸至三维。如上图f所示。在VTK中,任一类型的数据集都可用非结构化网格来表达,但其储存需要大量的空间,计算时需要消耗大量的资源,除非迫不得已,一般较少使用此种类型的数据集。主要用于有限元分析,计算几何和图形表示等领域。
创建方法
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellType.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnstructuredGrid.h>
int main( int, char *[] )
{
int i;
static double x[27][3]={{0,0,0}, {1,0,0}, {2,0,0}, {0,1,0}, {1,1,0}, {2,1,0},
{0,0,1}, {1,0,1}, {2,0,1}, {0,1,1}, {1,1,1}, {2,1,1},
{0,1,2}, {1,1,2}, {2,1,2}, {0,1,3}, {1,1,3}, {2,1,3},
{0,1,4}, {1,1,4}, {2,1,4}, {0,1,5}, {1,1,5}, {2,1,5},
{0,1,6}, {1,1,6}, {2,1,6}};
static vtkIdType pts[12][8]={{0, 1, 4, 3, 6, 7, 10, 9},
{1, 2, 5, 4, 7, 8, 11, 10},
{6, 10, 9, 12, 0, 0, 0, 0},
{8, 11, 10, 14, 0, 0, 0, 0},
{16, 17, 14, 13, 12, 15, 0, 0},
{18, 15, 19, 16, 20, 17, 0, 0},
{22, 23, 20, 19, 0, 0, 0, 0},
{21, 22, 18, 0, 0, 0, 0, 0},
{22, 19, 18, 0, 0, 0, 0, 0},
{23, 26, 0, 0, 0, 0, 0, 0},
{21, 24, 0, 0, 0, 0, 0, 0},
{25, 0, 0, 0, 0, 0, 0, 0}};
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
for (i=0; i<27; i++) points->InsertPoint(i,x[i]);
vtkSmartPointer<vtkUnstructuredGrid> ugrid =
vtkSmartPointer<vtkUnstructuredGrid>::New();
ugrid->Allocate(100);
ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[0]);
ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[1]);
ugrid->InsertNextCell(VTK_TETRA, 4, pts[2]);
ugrid->InsertNextCell(VTK_TETRA, 4, pts[3]);
ugrid->InsertNextCell(VTK_POLYGON, 6, pts[4]);
ugrid->InsertNextCell(VTK_TRIANGLE_STRIP, 6, pts[5]);
ugrid->InsertNextCell(VTK_QUAD, 4, pts[6]);
ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[7]);
ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[8]);
ugrid->InsertNextCell(VTK_LINE, 2, pts[9]);
ugrid->InsertNextCell(VTK_LINE, 2, pts[10]);
ugrid->InsertNextCell(VTK_VERTEX, 1, pts[11]);
ugrid->SetPoints(points);
vtkSmartPointer<vtkDataSetMapper> ugridMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
ugridMapper->SetInputData(ugrid);
vtkSmartPointer<vtkActor> ugridActor =
vtkSmartPointer<vtkActor>::New();
ugridActor->SetMapper(ugridMapper);
ugridActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
ugridActor->GetProperty()->EdgeVisibilityOn();
renderer->AddActor(ugridActor);
renderer->SetBackground(colors->GetColor3d("Beige").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Elevation(60.0);
renderer->GetActiveCamera()->Azimuth(30.0);
renderer->GetActiveCamera()->Dolly(1.2);
renWin->SetSize(640, 480);
// interact with data
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
vtkUnstructuredGrid->vtkPolyData
vtkPolyData->vtkUnstructuredGrid
vtkUnstructuredGrid相关示例
实例1:ClipUnstructuredGridWithPlane
该实例使用vtkTableBasedClipDataSet对vtkUnstructuredGrid数据集进行Clip。与其它Clip相关的Filter不同,vtkTableBasedClipDataSet在未Clip的部分保留原来的Cells。
输入数据:treemesh.vtk
#include <vtkSmartPointer.h>
#include <vtkTableBasedClipDataSet.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkPlane.h>
#include <vtkTransform.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellTypes.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNamedColors.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " filename.vtk" << std::endl;
return EXIT_FAILURE;
}
// Create the reader for the data.
std::string filename = argv[1];
std::cout << "Loading " << filename.c_str() << std::endl;
auto reader =
vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
double bounds[6];
reader->GetOutput()->GetBounds(bounds);
double center[3];
reader->GetOutput()->GetCenter(center);
auto colors =
vtkSmartPointer<vtkNamedColors>::New();
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
renderer->UseHiddenLineRemovalOn();
auto renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
auto interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
double xnorm[3] = {-1.0, -1.0, 1.0};
auto clipPlane = vtkSmartPointer<vtkPlane>::New();
clipPlane->SetOrigin(reader->GetOutput()->GetCenter());
clipPlane->SetNormal(xnorm);
auto clipper =
vtkSmartPointer<vtkTableBasedClipDataSet>::New();
clipper->SetClipFunction(clipPlane);
clipper->SetInputData(reader->GetOutput());
clipper->SetValue(0.0);
clipper->GenerateClippedOutputOn();
clipper->Update();
auto insideMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
insideMapper->SetInputData(clipper->GetOutput());
insideMapper->ScalarVisibilityOff();
auto insideActor =
vtkSmartPointer<vtkActor>::New();
insideActor->SetMapper(insideMapper);
insideActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("banana").GetData());
insideActor->GetProperty()->SetAmbient(.3);
insideActor->GetProperty()->EdgeVisibilityOn();
auto clippedMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
clippedMapper->SetInputData(clipper->GetClippedOutput());
clippedMapper->ScalarVisibilityOff();
auto clippedActor =
vtkSmartPointer<vtkActor>::New();
clippedActor->SetMapper(clippedMapper);
clippedActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("tomato").GetData());
insideActor->GetProperty()->SetAmbient(.3);
clippedActor->GetProperty()->EdgeVisibilityOn();
// Create transforms to make a better visualization
auto insideTransform = vtkSmartPointer<vtkTransform>::New();
insideTransform->Translate(-(bounds[1] - bounds[0]) * .75, 0, 0);
insideTransform->Translate(center[0], center[1], center[2]);
insideTransform->RotateY(-120.0);
insideTransform->Translate(-center[0], -center[1], -center[2]);
insideActor->SetUserTransform(insideTransform);
auto clippedTransform = vtkSmartPointer<vtkTransform>::New();
clippedTransform->Translate((bounds[1] - bounds[0]) * .75, 0, 0);
clippedTransform->Translate(center[0], center[1], center[2]);
clippedTransform->RotateY(60.0);
clippedTransform->Translate(-center[0], -center[1], -center[2]);
clippedActor->SetUserTransform(clippedTransform);
renderer->AddViewProp(clippedActor);
renderer->AddViewProp(insideActor);
renderer->ResetCamera();
renderer->GetActiveCamera()->Dolly(1.4);
renderer->ResetCameraClippingRange();
renderWindow->Render();
renderWindow->SetWindowName("ClipUnstructuredGridWithPlane");
renderWindow->Render();
interactor->Start();
// Generate a report
vtkIdType numberOfCells = clipper->GetOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The inside dataset contains a " << std::endl
<< clipper->GetOutput()->GetClassName()
<< " that has " << numberOfCells << " cells" << std::endl;
typedef std::map<int,int> CellContainer;
CellContainer cellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
cellMap[clipper->GetOutput()->GetCellType(i)]++;
}
for (auto c : cellMap)
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(c.first)
<< " occurs " << c.second << " times." << std::endl;
}
numberOfCells = clipper->GetClippedOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The clipped dataset contains a " << std::endl
<< clipper->GetClippedOutput()->GetClassName()
<< " that has " << numberOfCells << " cells" << std::endl;
typedef std::map<int,int> OutsideCellContainer;
CellContainer outsideCellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
outsideCellMap[clipper->GetClippedOutput()->GetCellType(i)]++;
}
for (auto c : outsideCellMap)
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(c.first)
<< " occurs " << c.second << " times." << std::endl;
}
return EXIT_SUCCESS;
}
实例2:ClipUnstructuredGridWithPlane2
该实例使用vtkClipDataSet对vtkUnstructuredGrid数据集进行Clip。vtkClipDataSet在未Clip的部位不会保留原有的Cells。与实例1比较,该实例在未Clip的区域将原有的vtkHexahedron转换成了vtkTetra。该实例的结果vtkUnstructuredGrid数据集的Cells数量比之前增加了四倍。
输入:treemesh.vtk
#include <vtkSmartPointer.h>
#include <vtkClipDataSet.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkPlane.h>
#include <vtkTransform.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellTypes.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkNamedColors.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " filename.vtk" << std::endl;
return EXIT_FAILURE;
}
// Create the reader for the data.
std::string filename = argv[1];
std::cout << "Loading " << filename.c_str() << std::endl;
auto reader =
vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();
double bounds[6];
reader->GetOutput()->GetBounds(bounds);
double center[3];
reader->GetOutput()->GetCenter(center);
auto colors =
vtkSmartPointer<vtkNamedColors>::New();
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
renderer->UseHiddenLineRemovalOn();
auto renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
auto interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
double xnorm[3] = {-1.0, -1.0, 1.0};
auto clipPlane = vtkSmartPointer<vtkPlane>::New();
clipPlane->SetOrigin(reader->GetOutput()->GetCenter());
clipPlane->SetNormal(xnorm);
auto clipper =
vtkSmartPointer<vtkClipDataSet>::New();
clipper->SetClipFunction(clipPlane);
clipper->SetInputData(reader->GetOutput());
clipper->SetValue(0.0);
clipper->GenerateClippedOutputOn();
clipper->Update();
auto insideMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
insideMapper->SetInputData(clipper->GetOutput());
insideMapper->ScalarVisibilityOff();
auto insideActor =
vtkSmartPointer<vtkActor>::New();
insideActor->SetMapper(insideMapper);
insideActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("banana").GetData());
insideActor->GetProperty()->SetAmbient(.3);
insideActor->GetProperty()->EdgeVisibilityOn();
auto clippedMapper =
vtkSmartPointer<vtkDataSetMapper>::New();
clippedMapper->SetInputData(clipper->GetClippedOutput());
clippedMapper->ScalarVisibilityOff();
auto clippedActor =
vtkSmartPointer<vtkActor>::New();
clippedActor->SetMapper(clippedMapper);
clippedActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("tomato").GetData());
insideActor->GetProperty()->SetAmbient(.3);
clippedActor->GetProperty()->EdgeVisibilityOn();
// Create transforms to make a better visualization
auto insideTransform = vtkSmartPointer<vtkTransform>::New();
insideTransform->Translate(-(bounds[1] - bounds[0]) * .75, 0, 0);
insideTransform->Translate(center[0], center[1], center[2]);
insideTransform->RotateY(-120.0);
insideTransform->Translate(-center[0], -center[1], -center[2]);
insideActor->SetUserTransform(insideTransform);
auto clippedTransform = vtkSmartPointer<vtkTransform>::New();
clippedTransform->Translate((bounds[1] - bounds[0]) * .75, 0, 0);
clippedTransform->Translate(center[0], center[1], center[2]);
clippedTransform->RotateY(60.0);
clippedTransform->Translate(-center[0], -center[1], -center[2]);
clippedActor->SetUserTransform(clippedTransform);
renderer->AddViewProp(clippedActor);
renderer->AddViewProp(insideActor);
renderer->ResetCamera();
renderer->GetActiveCamera()->Dolly(1.4);
renderer->ResetCameraClippingRange();
renderWindow->Render();
renderWindow->SetWindowName("ClipUnstructuredGridWithPlane2");
renderWindow->Render();
interactor->Start();
// Generate a report
vtkIdType numberOfCells = clipper->GetOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The inside dataset contains a " << std::endl
<< clipper->GetOutput()->GetClassName()
<< " that has " << numberOfCells << " cells" << std::endl;
typedef std::map<int,int> CellContainer;
CellContainer cellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
cellMap[clipper->GetOutput()->GetCellType(i)]++;
}
for (auto c : cellMap)
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(c.first)
<< " occurs " << c.second << " times." << std::endl;
}
numberOfCells = clipper->GetClippedOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The clipped dataset contains a " << std::endl
<< clipper->GetClippedOutput()->GetClassName()
<< " that has " << numberOfCells << " cells" << std::endl;
typedef std::map<int,int> OutsideCellContainer;
CellContainer outsideCellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
outsideCellMap[clipper->GetClippedOutput()->GetCellType(i)]++;
}
for (auto c : outsideCellMap)
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(c.first)
<< " occurs " << c.second << " times." << std::endl;
}
return EXIT_SUCCESS;
}