vtk三维体模型渲染

2022/3/28 23:29:48

本文主要是介绍vtk三维体模型渲染,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

string Trim(string& str)
{
    //str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
    str.erase(0, str.find_first_not_of(" \t\r\n"));
    str.erase(str.find_last_not_of(" \t\r\n") + 1);
    return str;
}

int main()
{
    vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();

    vtkSmartPointer<vtkPoints> ps = vtkSmartPointer<vtkPoints>::New();
    double x, y, z;
    ifstream fin("D:\\QTData\\Vtk-new\\ceshi2.csv"); //打开文件流操作-绝对路径
    string line;
    while (getline(fin, line))   //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
    {
        //cout <<"原始字符串:"<< line << endl; //整行输出
        istringstream sin(line); //将整行字符串line读入到字符串流istringstream中
        vector<string> fields; //声明一个字符串向量
        string field;
        while (getline(sin, field, ',')) //将字符串流sin中的字符读入到field字符串中,以逗号为分隔符
        {
            fields.push_back(field); //将刚刚读取的字符串添加到向量fields中
        }

        string sx = Trim(fields[0]);
        string sy = Trim(fields[1]);
        string sz = Trim(fields[2]);

        //字符串转换成double型数字
        istringstream streamx, streamy, streamz;
        streamx.str(sx), streamy.str(sy), streamz.str(sz);
        streamx >> x, streamy >> y, streamz >> z;
        ps->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构 
    }
     fin.close();  //关闭文件

    vtkSmartPointer<vtkPolyData>polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(ps);

    vtkSmartPointer<vtkDelaunay3D> delaunay3D = vtkSmartPointer<vtkDelaunay3D>::New();
    delaunay3D->SetInputData(polydata);
    delaunay3D->Update();

    vtkSmartPointer<vtkDataSetMapper> delaunayMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    delaunayMapper->SetInputConnection(delaunay3D->GetOutputPort());

    vtkSmartPointer<vtkActor> delaunayActor = vtkSmartPointer<vtkActor>::New();
    delaunayActor->SetMapper(delaunayMapper);
    delaunayActor->GetProperty()->SetColor(color->GetColor3d("banana").GetData());
    delaunayActor->GetProperty()->EdgeVisibilityOn();


    vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
    glyphFilter->SetInputData(delaunay3D->GetOutput());

    vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    pointMapper->SetInputConnection(glyphFilter->GetOutputPort());

    vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
    pointActor->GetProperty()->SetColor(color->GetColor3d("Tomato").GetData());
    pointActor->GetProperty()->SetPointSize(5);
    pointActor->SetMapper(pointMapper);


    vtkSmartPointer<vtkRenderer> delaunayRenderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(900, 900);


    renderWindow->AddRenderer(delaunayRenderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);


    delaunayRenderer->AddActor(delaunayActor);
    delaunayRenderer->AddActor(pointActor);
    delaunayRenderer->SetBackground(color->GetColor3d("Mint").GetData());

    // Render and interact
    renderWindowInteractor->Start();
    return EXIT_SUCCESS;
}

 

 



这篇关于vtk三维体模型渲染的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程