vtk中绘制线和绘制点集

绘制线:

//绘制一条线
    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;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力减肥的小胖子5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值