Skip to content

Commit 6d6cf42

Browse files
committed
Use buffered file stream for PLY import
1 parent d0d62ed commit 6d6cf42

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

libraries/YarpCloudUtils/YarpCloudUtils-tinyply.cpp

+58-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <ostream>
1212
#include <stdexcept>
13+
#include <streambuf>
1314
#include <vector>
1415

1516
#include <yarp/os/LogStream.h>
@@ -18,6 +19,39 @@
1819

1920
namespace
2021
{
22+
// https://github.com/ddiakopoulos/tinyply/blob/master/source/example-utils.hpp
23+
24+
struct memory_buffer : public std::streambuf
25+
{
26+
char * p_start {nullptr};
27+
char * p_end {nullptr};
28+
std::size_t size;
29+
30+
memory_buffer(char const * first_elem, std::size_t size)
31+
: p_start(const_cast<char *>(first_elem)), p_end(p_start + size), size(size)
32+
{
33+
setg(p_start, p_start, p_end);
34+
}
35+
36+
pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override
37+
{
38+
if (dir == std::ios_base::cur) gbump(static_cast<int>(off));
39+
else setg(p_start, (dir == std::ios_base::beg ? p_start : p_end) + off, p_end);
40+
return gptr() - p_start;
41+
}
42+
43+
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override
44+
{
45+
return seekoff(pos, std::ios_base::beg, which);
46+
}
47+
};
48+
49+
struct memory_stream : virtual memory_buffer, public std::istream
50+
{
51+
memory_stream(char const * first_elem, size_t size)
52+
: memory_buffer(first_elem, size), std::istream(static_cast<std::streambuf *>(this)) {}
53+
};
54+
2155
void write(std::ostream & os, const yarp::sig::PointCloudXY & cloud, const yarp::sig::VectorOf<int> & vertices, bool isBinary)
2256
{
2357
tinyply::PlyFile ply;
@@ -373,7 +407,7 @@ namespace
373407
return it->size;
374408
}
375409

376-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXY & cloud, yarp::sig::VectorOf<int> & vertices)
410+
bool read(std::istream & ifs, yarp::sig::PointCloudXY & cloud, yarp::sig::VectorOf<int> & vertices)
377411
{
378412
tinyply::PlyFile file;
379413

@@ -411,7 +445,7 @@ namespace
411445
return false;
412446
}
413447

414-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXYZ & cloud, yarp::sig::VectorOf<int> & vertices)
448+
bool read(std::istream & ifs, yarp::sig::PointCloudXYZ & cloud, yarp::sig::VectorOf<int> & vertices)
415449
{
416450
tinyply::PlyFile file;
417451

@@ -452,7 +486,7 @@ namespace
452486
return false;
453487
}
454488

455-
bool read(std::ifstream & ifs, yarp::sig::PointCloudNormal & cloud, yarp::sig::VectorOf<int> & vertices)
489+
bool read(std::istream & ifs, yarp::sig::PointCloudNormal & cloud, yarp::sig::VectorOf<int> & vertices)
456490
{
457491
tinyply::PlyFile file;
458492

@@ -507,7 +541,7 @@ namespace
507541
return false;
508542
}
509543

510-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXYZRGBA & cloud, yarp::sig::VectorOf<int> & vertices)
544+
bool read(std::istream & ifs, yarp::sig::PointCloudXYZRGBA & cloud, yarp::sig::VectorOf<int> & vertices)
511545
{
512546
tinyply::PlyFile file;
513547

@@ -574,7 +608,7 @@ namespace
574608
return false;
575609
}
576610

577-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXYZI & cloud, yarp::sig::VectorOf<int> & vertices)
611+
bool read(std::istream & ifs, yarp::sig::PointCloudXYZI & cloud, yarp::sig::VectorOf<int> & vertices)
578612
{
579613
tinyply::PlyFile file;
580614

@@ -629,7 +663,7 @@ namespace
629663
return false;
630664
}
631665

632-
bool read(std::ifstream & ifs, yarp::sig::PointCloudInterestPointXYZ & cloud, yarp::sig::VectorOf<int> & vertices)
666+
bool read(std::istream & ifs, yarp::sig::PointCloudInterestPointXYZ & cloud, yarp::sig::VectorOf<int> & vertices)
633667
{
634668
tinyply::PlyFile file;
635669

@@ -684,7 +718,7 @@ namespace
684718
return false;
685719
}
686720

687-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXYZNormal & cloud, yarp::sig::VectorOf<int> & vertices)
721+
bool read(std::istream & ifs, yarp::sig::PointCloudXYZNormal & cloud, yarp::sig::VectorOf<int> & vertices)
688722
{
689723
tinyply::PlyFile file;
690724

@@ -751,7 +785,7 @@ namespace
751785
return false;
752786
}
753787

754-
bool read(std::ifstream & ifs, yarp::sig::PointCloudXYZNormalRGBA & cloud, yarp::sig::VectorOf<int> & vertices)
788+
bool read(std::istream & ifs, yarp::sig::PointCloudXYZNormalRGBA & cloud, yarp::sig::VectorOf<int> & vertices)
755789
{
756790
tinyply::PlyFile file;
757791

@@ -899,17 +933,31 @@ bool loadPLY(const std::string & filename, yarp::sig::PointCloud<T> & cloud)
899933
template <typename T>
900934
bool loadPLY(const std::string & filename, yarp::sig::PointCloud<T> & cloud, yarp::sig::VectorOf<int> & vertices)
901935
{
902-
std::ifstream ifs(filename);
936+
std::ifstream ifs(filename, std::ios::binary);
903937

904938
if (ifs.fail())
905939
{
906940
yError() << "unable to open" << filename << "for read";
907941
return false;
908942
}
909943

944+
std::vector<std::uint8_t> fileBufferBytes;
945+
ifs.seekg(0, std::ios::end);
946+
std::size_t sizeBytes = ifs.tellg();
947+
ifs.seekg(0, std::ios::beg);
948+
fileBufferBytes.resize(sizeBytes);
949+
950+
if (!ifs.read((char *)fileBufferBytes.data(), sizeBytes))
951+
{
952+
yError() << "unable to read from" << filename;
953+
return false;
954+
}
955+
956+
memory_stream ms((char *)fileBufferBytes.data(), fileBufferBytes.size());
957+
910958
try
911959
{
912-
return read(ifs, cloud, vertices);
960+
return read(ms, cloud, vertices);
913961
}
914962
catch (const std::exception & e)
915963
{

0 commit comments

Comments
 (0)