示例程序的代码如下:
#include "stdafx.h" #include <vtkPoints.h> #include <vtkSmartPointer.h> #include <vtkCellArray.h> #include <vtkPolyData.h> #include <vtkPolyDataMapper.h> #include <vtkRotationalExtrusionFilter.h> #include <vtkActor.h> #include <vtkProperty.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> int _tmain(int argc, _TCHAR* argv[]) { //定义苹果轮廓线的点坐标 vtkSmartPointer<vtkPoints>pPoints=vtkPoints::New(); pPoints->InsertPoint(0,0.0,0.0,1.5); pPoints->InsertPoint(1,1.0,0.0,1); pPoints->InsertPoint(2,2.0,0.0,1.5); pPoints->InsertPoint(3,3.0,0.0,2.5); pPoints->InsertPoint(4,3.5,0.0,3.0); pPoints->InsertPoint(5,4.0,0.0,5.0); pPoints->InsertPoint(6,3.5,0.0,6.0); pPoints->InsertPoint(7,2.0,0.0,7.0); pPoints->InsertPoint(8,1.0,0.0,6.0); pPoints->InsertPoint(9,0.0,0.0,5.0); //构建线单元,绘制轮廓线 vtkSmartPointer<vtkCellArray>pLineCell=vtkCellArray::New(); //构建一个单元,由10个点构成 pLineCell->InsertNextCell(10); //构成单元点集的索引号 pLineCell->InsertCellPoint(0); pLineCell->InsertCellPoint(1); pLineCell->InsertCellPoint(2); pLineCell->InsertCellPoint(3); pLineCell->InsertCellPoint(4); pLineCell->InsertCellPoint(5); pLineCell->InsertCellPoint(6); pLineCell->InsertCellPoint(7); pLineCell->InsertCellPoint(8); pLineCell->InsertCellPoint(9); //定义多边形数据集 vtkSmartPointer<vtkPolyData>pPolyData=vtkPolyData::New(); //设置构成多边形的几何数据 pPolyData->SetPoints(pPoints); //设置单元类型,线单元 pPolyData->SetLines(pLineCell); //压制轮廓线,形成苹果实体 vtkSmartPointer<vtkRotationalExtrusionFilter>pExtruFilter=vtkRotationalExtrusionFilter::New(); pExtruFilter->SetInput(pPolyData); //设置生成苹果表面的光滑度参数,苹果的表面由多少个面构成 pExtruFilter->SetResolution(10); //将数据映射到图形系统进行绘制 vtkSmartPointer<vtkPolyDataMapper>pMap=vtkPolyDataMapper::New(); pMap->SetInput(pExtruFilter->GetOutput()); vtkSmartPointer<vtkActor>pActor=vtkActor::New(); pActor->SetMapper(pMap); pActor->GetProperty()->SetColor(0.0,1.0,0.0); //绘制 vtkSmartPointer<vtkRenderer>pRen=vtkRenderer::New(); pRen->AddActor(pActor); //创建绘制窗体 vtkSmartPointer<vtkRenderWindow>pRenWin=vtkRenderWindow::New(); pRenWin->AddRenderer(pRen); //创建交互器 vtkSmartPointer<vtkRenderWindowInteractor>pIren=vtkRenderWindowInteractor::New(); pIren->SetRenderWindow(pRenWin); //设置背景颜色 pRen->SetBackground(1,1,1); //设置窗口尺寸 pRenWin->SetSize(300,300); pRenWin->Render(); pRen->ResetCamera(); //开始绘制 pIren->Initialize(); pIren->Start(); return 0; }
原文地址:http://blog.csdn.net/zhurui_idea/article/details/39346453