1. 程式人生 > >VTK利用點雲高程渲染點雲(+讀寫檔案)

VTK利用點雲高程渲染點雲(+讀寫檔案)

原始資料格式是個TXT檔案,,包含x,y,z座標,現在想利用點雲的高程進行渲染,但是使用vtkSimplePointReader類讀取的 x-y-z座標只能給點雲統一指定顏色,而類vtkParticleReader讀取的資料形式為x-y-z-a,後來自己從新寫了下檔案,將x-y-z後面再寫個z值作為a,然後實現利用a(也就是原資料的z渲染點雲),程式碼如下:

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkParticleReader.h>
#include <vtkProperty.h>
 
int main()
{
    /*vtkSmartPointer<vtkSphereSource> sphereSoutce = vtkSmartPointer<vtkSphereSource>::New();
    sphereSoutce->Update();*/
    vtkSmartPointer<vtkParticleReader> sphereSource = vtkSmartPointer<vtkParticleReader>::New();
    sphereSource->SetFileName("d:\\01_1.txt");
    sphereSource->SetDataByteOrderToBigEndian();
    sphereSource->Update();
 
    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    sphereMapper->SetScalarRange(-2,20);//用來設定標量的最大最小值,修改這個可以看出渲染效果的不同
 
    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetPointSize(0.2);
 
    vtkSmartPointer<vtkRenderer> sphereRenderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> sphereRenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    sphereRenderWindow->AddRenderer(sphereRenderer);
 
    vtkSmartPointer<vtkRenderWindowInteractor> sphereIRen = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    sphereIRen->SetRenderWindow(sphereRenderWindow);
 
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    sphereIRen->SetInteractorStyle(style);
 
    sphereRenderer->AddActor(sphereActor);
    sphereRenderWindow->Render();
 
    sphereIRen->Start();
 
    return EXIT_SUCCESS;
}

重新寫檔案還是比較耗時的,繼續找找不需要重寫檔案的方法。

 下面是重新寫檔案的程式碼:

#include <stdio.h>
#include <math.h>
#include <iostream>
 
void ReadAPoint(double * pt , FILE *f);
 
using namespace std;
 
void main() 
{
    FILE *fRead,*fWrite;
    fRead=fopen(/*"2.txt" */     "d:\\01.txt" , "r");
    fWrite = fopen(/*"22.txt"*/   "d:\\01_1.txt" , "w");
    double pt[3];
    while(!feof(fRead))
    {
        ReadAPoint(pt,fRead);
        fprintf(fWrite,"%lf %lf %lf %lf",pt[0],pt[1],pt[2],pt[2]);
        fprintf(fWrite,"\n");
    }
    
 
    fclose(fRead);
    fclose(fWrite);
    cout<<"Finished!"<<endl;
}
 
 
 
void ReadAPoint(double * pt , FILE *f)
{
    int x,y,z;
    x=fscanf(f,"%lf",pt);
    y=fscanf(f,"%lf",pt+1);
    z=fscanf(f,"%lf",pt+2);
}

轉:https://blog.csdn.net/u013094443/article/details/48846447