其他分享
首页 > 其他分享> > vtk三角网剖分

vtk三角网剖分

作者:互联网

主要流程:1. 获取三角网格数据

     2. 三角网剖分

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<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    double x, y, z;
    ifstream fin("D:\\QTData\\Vtk-new\\ceshi1.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;
        points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构  
    }
    fin.close();  //关闭文件

    vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points);

    vtkSmartPointer<vtkCellArray> aCellArray = vtkSmartPointer<vtkCellArray>::New();
    
    vtkSmartPointer<vtkPolyData> boundary = vtkSmartPointer<vtkPolyData>::New();
    boundary->SetPoints(polyData->GetPoints());
    boundary->SetPolys(aCellArray);

    vtkSmartPointer<vtkDelaunay2D> delaunay = vtkSmartPointer<vtkDelaunay2D>::New();
    #if VTK_MAJOR_VERSION <= 5  
    	delaunay->SetInput(polyData->GetOutput());
        delaunay->SetSource(boundary);
    #else  
    delaunay->SetInputData(polyData);
    delaunay->SetSourceData(boundary);
    #endif  
    delaunay->Update();

    vtkSmartPointer<vtkPolyDataMapper> meshMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    meshMapper->SetInputConnection(delaunay->GetOutputPort());

    vtkSmartPointer<vtkActor> meshActor = vtkSmartPointer<vtkActor>::New();
    meshActor->SetMapper(meshMapper);
    meshActor->GetProperty()->SetEdgeColor(0, 0, 1);
    meshActor->GetProperty()->SetInterpolationToFlat();
    meshActor->GetProperty()->SetRepresentationToWireframe();

    vtkSmartPointer<vtkPolyDataMapper> boundaryMapper = vtkSmartPointer<vtkPolyDataMapper>::New();

#if VTK_MAJOR_VERSION <= 5
	boundaryMapper->SetInputConnection(boundary->GetProducerPort());
#else
	boundaryMapper->SetInputData(boundary);
#endif
    boundaryMapper->Update();

    vtkSmartPointer<vtkActor> boundaryActor = vtkSmartPointer<vtkActor>::New();
    boundaryActor->SetMapper(boundaryMapper);
    boundaryActor->GetProperty()->SetColor(1, 0, 0);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderer->AddActor(meshActor);
    renderer->AddActor(boundaryActor);
    renderer->SetBackground(.3, .6, .3);

    renderWindow->Render();
    renderWindowInteractor->Start();

}

 

 

标签:string,剖分,三角网,vtk,vtkSmartPointer,str,fields,New,boundary
来源: https://www.cnblogs.com/wxd-/p/16062719.html