vtk-两种方法读取点坐标

可直接用:vtkSimplePointsReader读取文件,最方便
比较麻烦的方式如下:

方法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<stdio.h>
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 <iostream>
#include <vector>
#include <sstream>
//
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
#include"vtkAutoInit.h"
#include <vtkVertexGlyphFilter.h>
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 <iostream>
#include <vector>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkSmartPointer.h>
#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;
}

使用C++ ifstream来读取文件时,发现在读到文件结尾时会多读一行。
解决方法:

        while (std::getline(filestream, line))  //整行读取文件(会多读取一行)
        {
       
            if (line.empty())
            {
                break;
            }
            //do    sth....
    }        

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值