Creating pointer to read from .las binary file throws bad_alloc error

273 views Asked by At

I am currently writing code to read in a .las (LIDAR point data) file. After reading in the file, I created a pointer to iterate through all the points. However, it gives me a bad_alloc error.

The only thing I can think of is that header is not the same size as the las file header specifies it should be. I double checked my header and I don't think I see anything wrong.

All the header variables in Header and PointRecord3 come from this documentation, pages 3 and 10 respectively.

UPDATE: the number of points reading in is 402,673,112. However, when I open it in other LIDAR software to double check and the number of points is only 5,104,152. I believe the reason for this is because I filtered the cloud in other software before opening it in C++, therefore reducing the number of points.

Here is my code:

#ifndef POINTCLOUD_H
#define POINTCLOUD_H
#pragma once
#include <string>


class PointCloud
{
    public:
        PointCloud(const std::string &path);

    private:
        struct __attribute__((packed)) Header {

            char magic[4];
            uint16_t fileourceID;
            uint16_t globalEncoding;
            uint32_t guiData1;
            uint16_t guiData2;
            uint16_t guiData3;
            uint8_t guiData[8];
            uint8_t versionMaj, versionMin;
            char systemIdentifer[32];
            char genSoftware[32];
            uint16_t creationDay, creationYear;
            uint16_t headerSize;
            uint32_t pointDataOffset;
            uint32_t numVarLenRecords;
            uint8_t pointDataRecordFormat;
            uint16_t pointDataRecordLen;
            uint32_t numberOfPoints;
            uint32_t numPointsByReturn[5];
            double scaleX, scaleY, scaleZ;
            double offX, offY, offZ;
            double minX, minY, minZ;
            double maxX, maxY, maxZ;
        };

        struct __attribute__((packed)) PointRecord3 {
            uint32_t X, Y, Z;
            uint16_t intensity;
            uint8_t flags;
            uint8_t classification;
            uint8_t scanAngleRank;
            uint8_t userData;
            uint16_t pointSourceID;
            double gpsTime;
            uint16_t red;
            uint16_t green;
            uint16_t blue;
        }; 



#include "PointCloud.h"
#include <fstream>
#include <iostream>
#include <string>
#include <assert.h>

using namespace std;

PointCloud::PointCloud(const string &path)
{
    read(path);
}

void PointCloud::read(const string &path) {
    ifstream in(path.c_str(), ios::binary );
    if(in.is_open()){
        Header header;
        in.read((char *)&header, sizeof(header));

        assert(header.versionMaj == 1 && header.versionMin == 2);
        cout << "headerSize " << header.headerSize << " == " << sizeof(header) << " size of header struct" <<endl;

        cout << "FILE TYPE: LAS " << (int)header.versionMaj << "." << (int)header.versionMin << endl;
        cout << "POINT DATA OFFSET: " << (int)header.pointDataOffset <<endl;
        cout << "POINT DATA RECORD FORMAT: " << (int)header.pointDataRecordFormat <<endl;
        cout << "NUMBER OF POINTS: " << (int)header.numberOfPoints << endl;



        PointRecord3 *points = new PointRecord3[header.numberOfPoints];

       in.seekg(header.pointDataOffset);
        for(uint32_t i = 0; i < header.numberOfPoints; i++) {
            in.read((char *) (points + i), sizeof(*points));
       }
       if(in.good()) {

          throw runtime_error("error");
        }

      delete points;


    } else {
        cout << "file not opened" << endl;

    }

}

Any help would be appreciated thanks!

Also here is the output of the LAS file header:

file signature:             'LASF'
  file source ID:             0
  global_encoding:            0
  project ID GUID data 1-4:   00000000-0000-0000-0000-000000000000
  version major.minor:        1.2
  system identifier:          'EXPORT'
  generating software:        'RiSCAN_PRO'
  file creation day/year:     317/2018
  header size:                227
  offset to point data:       313
  number var. length records: 1
  point data format:          3
  point data record length:   34
  number of point records:    5104152
  number of points by return: 5104152 0 0 0 0
  scale factor x y z:         0.00025 0.00025 0.00025
  offset x y z:               267805 3197054 13
  min x y z:                  267804.25250 3196596.64500 -1.89000
  max x y z:                  268760.27000 3197586.23000 23.33000
variable length header record 1 of 1:
  reserved             43707
  user ID              'LASF_Projection'
  record ID            34735
  length after header  32
  description          'GeoKeyDirectoryTag (mandatory)'
    GeoKeyDirectoryTag version 1.1.0 number of keys 3
      key 1025 tiff_tag_location 0 count 1 value_offset 2 - GTRasterTypeGeoKey: RasterPixelIsPoint
      key 1024 tiff_tag_location 0 count 1 value_offset 3 - GTModelTypeGeoKey: ModelTypeGeocentric
      key 2048 tiff_tag_location 0 count 1 value_offset 4326 - GeographicTypeGeoKey: GCS_WGS_84
reporting minimum and maximum for all LAS point record entries ...
  X               -2989    3821075
  Y            -1829412    2128911
  Z              -59559      41318
  intensity       32767      32768
  return_number       1          1
  number_of_returns   1          1
  edge_of_flight_line 0          0
  scan_direction_flag 1          1
  classification      0          0
  scan_angle_rank     0          0
  user_data           0          0
  point_source_ID     0          0
  gps_time 0.000000 0.000000
  Color R 0 0
        G 0 0
        B 0 0
number of first returns:        5104152
number of intermediate returns: 0
number of last returns:         5104152
number of single returns:       5104152
overview over number of returns of given pulse: 5104152 0 0 0 0 0 0
histogram of classification of points:
         5104152  never classified (0)
1

There are 1 answers

0
p0ps1c1e On BEST ANSWER

I figured it out. So what was happening was that C++ was adding a few extra bytes which resulted in my header struct being 5 bytes larger than the actual file header. I fixed this by adding #pragma pack (push, 1).

#ifndef POINTCLOUD_H
#define POINTCLOUD_H
#pragma pack (push, 1)
#include <string>


struct float3
{
    float x, y, z;

};

class PointCloud
{
    public:
        PointCloud(const std::string &path);

    private:

        struct __attribute__((packed)) Header
        {

            char fileSig[4];
            uint16_t fileourceID;
            uint16_t globalEncoding;
            uint32_t guiData1;
            uint16_t guiData2;
            uint16_t guiData3;
            uint8_t guiData[8];
            uint8_t versionMaj, versionMin;
            char systemIdentifer[32];
            char genSoftware[32];
            uint16_t creationDay, creationYear;
            uint16_t headerSize;
            uint32_t pointDataOffset;
            uint32_t numVarLenRecords;
            uint8_t pointDataRecordFormat;
            uint16_t pointDataRecordLen;
            uint32_t numberOfPoints;
            uint32_t numPointsByReturn[5];
            double scaleX, scaleY, scaleZ;
            double offX, offY, offZ;
            double minX, minY, minZ;
            double maxX, maxY, maxZ;
        };

        struct __attribute__((packed)) PointRecord3
        {
            uint32_t X, Y, Z;
            uint16_t intensity;
            uint8_t flags;
            uint8_t classification;
            uint8_t scanAngleRank;
            uint8_t userData;
            uint16_t pointSourceID;
            double gpsTime;
            uint16_t red;
            uint16_t green;
            uint16_t blue;
        };


        void read(const std::string &path);
};



#endif // POINTCLOUD_H