hdf5和las文件的读写

来源:互联网 发布:蒋欣刘涛关系破裂知乎 编辑:程序博客网 时间:2024/06/05 09:15

下面的的代码值关于Hdf5和las的读写和相互转化的函数,代码没有整理,可以参照改成自己所需要的形式。

#include <iostream>#include <string>#include <vector>#include "pointxyz.h"#include <lasreader.hpp>#include <laswriter.hpp>#include <H5Cpp.h>using namespace H5 ;const H5std_string DATASET_NAME( "/Points" );int readHDF5data(std::string &filename , std::vector<PointXYZ> &cloud) ;int writeHDF5data(std::vector<PointXYZ> &cloud , std::string &filename) ;int readLasData(std::string &filename , std::vector<PointXYZ> &cloud) ;int writeLasData(std::string &filename , std::vector<PointXYZ> &cloud) ;int main (int argc , char *argv[]){    std::vector<PointXYZ> cloud ;    std::string filename = "SDS.h5" ;    for (int i = 0 ; i < 10 ; ++i)    {        cloud.push_back(PointXYZ(1.0f * rand()/10 , 1.0f * rand()/10 , 1.0f * rand()/10)) ;    }    //  writeHDF5data(cloud , filename) ;    //  readHDF5data(filename , cloud) ;    std::string savelas = "savelas.las" ;    writeLasData(savelas , cloud) ;    std::vector<PointXYZ> cloud_out ;    readLasData(savelas , cloud_out) ;    return 0;  // successfully terminated}int readHDF5data(std::string &filename , std::vector<PointXYZ> &cloud){    H5File file(filename.c_str() , H5F_ACC_RDONLY) ;    DataSet dataset = file.openDataSet(DATASET_NAME) ;    H5T_class_t type_class = dataset.getTypeClass() ;    DataSpace dataspace = dataset.getSpace() ; //获取数据集的数据空间    int rank = dataspace.getSimpleExtentNdims() ;//获取数据空间的维度    hsize_t dims_out[2] ;    int ndims = dataspace.getSimpleExtentDims(dims_out , NULL) ;//检索数据空间的维度和最大尺寸#ifdef _DEBUG    std::cout<<"rank"<<rank<<", dimensions "<<dims_out[0]<<" x "<<dims_out[1]<<std::endl ;#endif // _DEBUG    if (type_class == H5T_INTEGER)    {        //      std::cout<<"Data set has Integer type"<<std::endl ;        //      IntType intype = dataset.getIntType() ;        //         //      H5std_string order_string ;        //      H5T_order_t order = intype.getOrder(order_string) ;        //      std::cout<<order_string<<std::endl ;        //      size_t size = intype.getSign() ;        //      std::cout<<"Data size is"<<size<<std::endl ;        int *data = new int[dims_out[0] * dims_out[1]] ;        dataset.read(data , PredType::NATIVE_INT) ;#ifdef _DEBUG        for (int i = 0 ; i < dims_out[0] ; ++i)        {            for (int j = 0 ; j < dims_out[1] ; ++j)            {                std::cout<<data[dims_out[1] * i + j]<<" " ;            }            std::cout<<std::endl ;        }#endif // _DEBUG    }    else if (type_class = H5T_FLOAT)    {        float *data = new float[dims_out[0] * dims_out[1]] ;        dataset.read(data , PredType::NATIVE_FLOAT) ;#ifdef _DEBUG        for (int i = 0 ; i < dims_out[0] ; ++i)        {            for (int j = 0 ; j < dims_out[1] ; ++j)            {                std::cout<<data[dims_out[1] * i + j]<<" " ;            }            std::cout<<std::endl ;        }#endif // _DEBUG    }    return 1 ;}int writeHDF5data(std::vector<PointXYZ> &cloud , std::string &filename){    if (cloud.empty())    {#ifdef _DEBUG        std::cout<<"欲保存点云数据为空!\n" ;#endif // _DEBUG        return 0 ;    }    int num_of_cloud = cloud.size() ;    float *data = new float[num_of_cloud * 3] ;    for (int i = 0 ; i < num_of_cloud ; ++i)    {        data[3 * i + 0] = cloud[i].get_x() ;        data[3 * i + 1] = cloud[i].get_y() ;        data[3 * i + 2] = cloud[i].get_z() ;    }    H5File file(filename.c_str() , H5F_ACC_TRUNC) ;    hsize_t dimsf[2] ;    dimsf[0] = num_of_cloud ;    dimsf[1] = 3 ;    DataSpace dataspace(2 , dimsf) ;    IntType datatype(PredType::NATIVE_FLOAT) ;    datatype.setOrder(H5T_ORDER_LE) ;    DataSet dataset = file.createDataSet(DATASET_NAME , datatype , dataspace) ;    dataset.write(data , PredType::NATIVE_FLOAT) ;    return 1 ;}int readLasData(std::string &filename , std::vector<PointXYZ> &cloud){    LASreadOpener lasreadopener ;    lasreadopener.set_file_name(filename.c_str()) ;    if (!lasreadopener.active())    {#ifdef _DEBUG        std::cout<<"opener not actie!\n" ;#endif // _DEBUG        return 0  ;    }    LASreader *lasreader = lasreadopener.open() ;    if (!lasreader)    {#ifdef _DEBUG        std::cout<<"can not open file!\n" ;#endif // _DEBUG        return 0 ;    }    PointXYZ p3d ;    while (lasreader->read_point())    {        p3d.set_x(lasreader->point.get_x()) ;        p3d.set_y(lasreader->point.get_y()) ;        p3d.set_z(lasreader->point.get_z()) ;        cloud.push_back(p3d) ;    }    return 1 ;}int writeLasData(std::string &filename , std::vector<PointXYZ> &cloud){    if (cloud.empty())    {#ifdef _DEBUG        std::cout<<"欲保存点云数据为空!\n" ;#endif // _DEBUG        return 0 ;    }    LASwriteOpener laswriteropener ;    laswriteropener.set_file_name(filename.c_str()) ;    LASheader lasheader ;    lasheader.x_scale_factor = 0.1;    lasheader.y_scale_factor = 0.01;    lasheader.z_scale_factor = 0.001;    lasheader.x_offset = 1000.0;    lasheader.y_offset = 2000.0;    lasheader.z_offset = 0.0;    lasheader.point_data_format = 2;    lasheader.point_data_record_length = 28;    LASpoint pt ;    pt.init(&lasheader , lasheader.point_data_format , lasheader.point_data_record_length , 0) ;    LASwriter *laswriter = laswriteropener.open(&lasheader) ;    if (laswriter == 0)    {#ifdef _DEBUG        std::cout<<"failture!\n" ;#endif // _DEBUG        return 0 ;    }    int num_of_cloud = cloud.size() ;    for (int i = 0 ; i < num_of_cloud ; i++)    {        pt.set_X(cloud[i].get_x()) ;        pt.set_Y(cloud[i].get_y()) ;        pt.set_Z(cloud[i].get_z()) ;        laswriter->write_point(&pt) ;        laswriter->update_inventory(&pt) ;    }    laswriter->update_header(&lasheader , TRUE) ;    laswriter->close() ;    delete laswriter ;    return 1 ;}
0 0
原创粉丝点击