[vtkusers] from point cloud to PLY file ??

Michele Natrella michele.cetma at yahoo.it
Mon Dec 21 12:42:08 EST 2009


Hi everybody,
My name is (Mr) Michele, I am a beginner with VTK.

I am dealing with 3D point clouds (in ascii files). My purpose is to obtain a PLY file. Could anyone give me any hints on the best way to achieve this result?

So far I have been able to obtain a PLY file by using vtkPoints, vtkPolyData, vtkDelaunay2D and vtkPLYWriter. Is this a good procedure?
If so, I would want to refine my risult by performing an oversampling of my 3D point clouds. Is this feasible with VTK?

Should anyone be interested, as follows the code I wrote (for the moment I have to use the 4.2 version of VTK):



#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkDelaunay2D.h"
#include "vtkPolyData.h"
#include "vtkOBJExporter.h"
#include "vtkIVExporter.h"
#include "vtkPLYWriter.h"

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <cstdlib>

using namespace std;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);



int main()
{
    vtkPolyData* profile = vtkPolyData::New();
    vtkPoints* points = vtkPoints::New();

// -1- Apertura file in lettura
    ifstream fileStream("C:/Documents and Settings/michele.natrella/Desktop/file.txt");

    // -2- Controllo sulla correttezza dell'apertura del file
    if( !fileStream )
    {
        cout << "Impossibile aprire il file " << endl;
        return 1;
    }

// -3- Recupero linea per linea e assegnamento valori 
    string line;
    vector<string> data;
    float x, y, z; //Punti da inserire  
    while(getline(fileStream, line)) //Prende una nuova linea
    {
        if(!line.empty()) // se la linea non è vuota
        {
        data=split(line, ' '); //il carattere di separazione  lo spazio
        
        //Conversione da std::string a float (se si vuole convertire in double sostituire atof con atod)
        x=(float)atof(data[0].c_str());
        y=(float)atof(data[1].c_str());
        z=(float)atof(data[2].c_str());
        
        points->InsertNextPoint(x, y, z);
        }
    }

    profile->SetPoints(points);

    // Perform a 2D Delaunay triangulation on them.
    vtkDelaunay2D* delny = vtkDelaunay2D::New();
    delny->SetInput(profile);
    delny->SetTolerance(0.0001);
    vtkPolyDataMapper* mapMesh = vtkPolyDataMapper::New();
    mapMesh->SetInput(delny->GetOutput());
    vtkActor* meshActor = vtkActor::New();
    meshActor->SetMapper(mapMesh);
    meshActor->GetProperty()->SetColor(0.8, 0.8, 0.8);

    // Create the rendering window, renderer, and interactive renderer
    vtkRenderer* ren = vtkRenderer::New();
    vtkRenderWindow* renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren);
    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    // Add the actors to the renderer, set the background and size
    ren->AddActor(meshActor);
    ren->SetBackground(0, 0, 0);
    renWin->SetSize(500, 500);

    //write the scene into Ply format
    vtkPLYWriter* ply = vtkPLYWriter::New();
    ply->SetInput(delny->GetOutput());
    ply->SetFileName ("C:/Documents and Settings/michele.natrella/Desktop/file.ply");
    ply->SetDataByteOrderToBigEndian();
    ply->Write();

    ren->ResetCamera();
    ren->GetActiveCamera()->Zoom(1.5);

    // Interact with the data.
    iren->Initialize();
    renWin->Render();
    iren->Start();

    return 0;
}


std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20091221/a939d598/attachment.htm>


More information about the vtkusers mailing list