绘制线:
//绘制一条线
void InitLineActor(vtkPoints* points,vtkActor* lactor,double color[3]){
vtkSmartPointer<vtkPolyLine> polyLine =
vtkSmartPointer<vtkPolyLine>::New();
polyLine->GetPointIds()->SetNumberOfIds(points->GetNumberOfPoints());
for(unsigned int i = 0; i < points->GetNumberOfPoints(); i++)
{
polyLine->GetPointIds()->SetId(i,i);
}
vtkSmartPointer<vtkCellArray> cells =
vtkSmartPointer<vtkCellArray>::New();
cells->InsertNextCell(polyLine);
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(cells);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
lactor->SetMapper(mapper);
lactor->GetProperty()->SetLineWidth(1);
lactor->GetProperty()->SetColor(color);
}
//绘制多条线段
void CreateMultiLine(){
vtkPoints* points = vtkPoints::New();
points->InsertNextPoint(0,0,0);
points->InsertNextPoint(1,0,0);
points->InsertNextPoint(0,0,1);
points->InsertNextPoint(1,1,1);
vtkSmartPointer<vtkPolyLine> polyLine1 =
vtkSmartPointer<vtkPolyLine>::New();
polyLine1->GetPointIds()->SetNumberOfIds(2);
polyLine1->GetPointIds()->SetId(0,0);
polyLine1->GetPointIds()->SetId(1,1);
vtkSmartPointer<vtkPolyLine> polyLine2 =
vtkSmartPointer<vtkPolyLine>::New();
polyLine2->GetPointIds()->SetNumberOfIds(2);
polyLine2->GetPointIds()->SetId(0,2);
polyLine2->GetPointIds()->SetId(1,3);
vtkSmartPointer<vtkCellArray> cells =
vtkSmartPointer<vtkCellArray>::New();
cells->InsertNextCell(polyLine1);
cells->InsertNextCell(polyLine2);
// cells->InsertNextCell(2);
// cells->InsertCellPoint(0);
// cells->InsertCellPoint(1);
// cells->InsertNextCell(2);
// cells->InsertCellPoint(2);
// cells->InsertCellPoint(3);
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);
polyData->SetLines(cells);
//设置线段的颜色
float red[3] = { 255.0, 0, 0 };
float green[3] = { 0, 255.0, 0 };
vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->InsertNextTuple(red);
colors->InsertNextTuple(green);
polyData->GetCellData()->SetScalars(colors);
}
绘制点集:
void DrawPoints(){
vtkSmartPointer<vtkVertex> vertex =
vtkSmartPointer<vtkVertex>::New();
vertex->GetPointIds()->SetNumberOfIds(clickPoints2->GetNumberOfPoints());
for(int i=0; i<clickPoints2->GetNumberOfPoints(); i++){
vertex->GetPointIds()->SetId(i, i);
}
vtkSmartPointer<vtkCellArray> vertices =
vtkSmartPointer<vtkCellArray>::New();
vertices->InsertNextCell(vertex);
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(clickPoints2);
// polydata->SetVerts(vertices);
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();
// Setup actor and mapper
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(glyphFilter->GetOutput());
projectActor->SetMapper(mapper);
projectActor->GetProperty()->SetPointSize(2.5);
projectActor->GetProperty()->SetColor(0,0,1);
projectActor->GetProperty()->SetOpacity(0.1);
this->DefaultRenderer->AddActor(projectActor);
this->DefaultRenderer->GetRenderWindow()->Render();
}
创建球:
vtkActor* CreateSphereActor(double center[], double color[], double radius){
vtkSmartPointer<vtkSphereSource> sphere =
vtkSmartPointer<vtkSphereSource>::New();
sphere->SetCenter(center);
sphere->SetRadius(radius);
//sphere->SetPhiResolution(20);//越大球越光滑
//sphere->SetThetaResolution(20);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphere->GetOutputPort());
vtkActor* actor = vtkActor::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(color);
return actor;
}