vtk-两种方法读取点坐标

方法1:

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    FILE *fp = NULL;
    fp = fopen("../data/test.txt", "r");
    if(!fp) {
        printf("打开文件失败!!\n");
        exit(1);
    }
    double x = 0, y = 0, z = 0;
    int i = 0;
    while(!feof(fp)) {
        fscanf(fp, "%lf %lf %lf", &x, &y, &z);
        points->InsertPoint(i, x, y, z);
        vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();  //_存放顶点,用于渲染(显示点云所必须的)
        vertices->InsertNextCell(1);     //_加入顶点信息----用于渲染点集
        vertices->InsertCellPoint(i++);
    }
    fclose(fp);
    

关于feof()导致文件最后一行重复读写的现象
https://blog.csdn.net/qq_29006825/article/details/103806345

#include
int main()
{
    FILE *fp;
    double x,y;
    fp=fopen("C:\\Users\\Administrator\\Desktop\\test.txt","r");
    while(!feof(fp)){
        fscanf(fp,"%lf %lf",&x,&y);
        if(feof(fp))break;
        printf("%.3lf\t%.3lf\n",x,y);
    }
    fclose(fp);
    return 0;
}

fscanf(fp , “%s %d %lf” , a , &b , &c);
格式字符说明编辑
常用基本参数对照:
%d:读入一个十进制整数.
%i :读入十进制,八进制,十六进制整数,与%d类似,但是在编译时通过数据前置或后置来区分进制,如加入“0x”则是十六进制,加入“0”则为八进制。例如串“031”使用%d时会被算作31,但是使用%i时会算作25.
%u:读入一个无符号十进制整数.
%f %F %g %G : 用来输入实数,可以用小数形式或指数形式输入.
%x %X: 读入十六进制整数.
%o’: 读入八进制整数.
%s : 读入一个字符串,遇空字符‘\0’结束。
%c : 读入一个字符。无法读入空值。空格可以被读入。
附加格式说明字符表修饰符说明
L/l 长度修饰符 输入"长"数据
h 长度修饰符 输入"短"数据

方法2:

std::string filename = "../data/test.txt";
       std::ifstream filestream(filename.c_str()); //文件流
       std::string line;
   
       vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
       while(std::getline(filestream, line)) { //整行读取文件
           double x, y, z;
           std::stringstream linestream;
           linestream << line;
           linestream >> x >> y >> z;
           points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构
       }
       filestream.close();  //关闭文件流操作

第二个方法我比较喜欢,下面是完整的程序,读取TXT点数据并渲染

#include 
#include 
#include 
//
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include"vtkAutoInit.h"
#include 
using namespace std;

int main(int argc, char *argv[]) {
    std::string filename = "../data/quanshidiandian.txt";//quanshidiandian//daosi
    std::ifstream filestream(filename.c_str()); //文件流
    std::string line;
    vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
    while(std::getline(filestream, line)) { //整行读取文件
        double x, y, z;
        int i = 0;
        std::stringstream linestream;
        linestream << line;
        cout << line << endl;
        linestream >> x >> y >> z;
        m_Points->InsertNextPoint(x, y, z);  //新读取的数据赋予点的几何结构
    }
    filestream.close();  //关闭文件流操作
//
    vtkSmartPointer<vtkPolyData> VesselPointsPolyData = vtkSmartPointer<vtkPolyData>::New();
    VesselPointsPolyData->SetPoints(m_Points);
    vtkIdType PointsNumber = VesselPointsPolyData->GetNumberOfPoints();
    std::cout << "几何数据(点数):" << PointsNumber << std::endl;

    vtkSmartPointer<vtkVertexGlyphFilter> vertexGlyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
    vertexGlyphFilter->AddInputData(VesselPointsPolyData);
    vertexGlyphFilter->Update();

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

//--------------------------------------渲染管线可视化-----------------------------------------------
    vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer< vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

    pointActor->SetMapper(pointMapper);
    pointActor->GetProperty()->SetColor(0.0, 0.1, 1.0);
    pointActor->GetProperty()->SetAmbient(0.5);
    pointActor->GetProperty()->SetPointSize(2);
//
    ren1->AddActor(pointActor);
    ren1->SetBackground(0, 0, 0);
//
    renWin->AddRenderer(ren1);
    renWin->SetSize(800, 800);
//
    iren->SetInteractorStyle(istyle);
    iren->SetRenderWindow(renWin);  //交互
    renWin->Render();
    iren->Start();
    return EXIT_SUCCESS;
}
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include"vtkAutoInit.h"
/*
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
*/
using namespace std;

int main(int argc, char *argv[]) {
    vtkSmartPointer<vtkPoints> m_Points = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();   //_存放细胞顶点,用于渲染(显示点云所必须的)
    vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer< vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> istyle = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

    //_读进点云数据信息
    FILE *fp = NULL;
    fp = fopen("../data/quanshidiandian.txt", "r"); //daosi.txt//quanshidiandian//getFINALpoints.txt
    if(!fp) {
        printf("打开文件失败!!\n");
        exit(1);
    }
    double x = 0, y = 0, z = 0;
    int i = 0;
    while(!feof(fp)) {
        fscanf(fp, "%lf %lf %lf", &x, &y, &z);
        m_Points->InsertPoint(i, x, y, z);      //_加入点信息
        vertices->InsertNextCell(1);     //_加入细胞顶点信息----用于渲染点集
        vertices->InsertCellPoint(i++);
    }
    fclose(fp);
//
    vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(m_Points);       //_设置点集
    polyData->SetVerts(vertices);        //_设置渲染顶点
//下面都是为了可视化
    vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    pointMapper->SetInputData(polyData);
//
    pointActor->SetMapper(pointMapper);
    pointActor->GetProperty()->SetColor(0.0, 0.1, 1.0);
    pointActor->GetProperty()->SetAmbient(0.5);
    pointActor->GetProperty()->SetPointSize(2);
//
    ren1->AddActor(pointActor);
    ren1->SetBackground(0, 0, 0);
//
    renWin->AddRenderer(ren1);
    renWin->SetSize(800, 800);
//
    iren->SetInteractorStyle(istyle);
    iren->SetRenderWindow(renWin);  //交互
    renWin->Render();
    iren->Start();
    return EXIT_SUCCESS;
}

你可能感兴趣的:(VTK)