Skip to content

Commit 21aba0b

Browse files
author
Daniel Tobon
committed
Merge branch 'feature-parser-txt'
2 parents e907aa1 + cd0c85b commit 21aba0b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

parser/include/modern/concrete_parses.hpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <pcl/io/ply_io.h>
1313
#include <pcl/io/vtk_lib_io.h>
1414

15+
#include <fstream>
16+
1517
#include "inteface_parser.hpp"
1618

1719
namespace CloudParserLibrary {
@@ -29,13 +31,6 @@ class ParserPCD : public InterfaceParser {
2931
class ParserPLY : public InterfaceParser {
3032
public:
3133
std::string parser_name = "ParserPLY";
32-
bool cloud_is_good(pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
33-
if ((cloud->points.size() > 0) or
34-
(cloud->points[0].x > 0 && cloud->points[0].y > 0 && cloud->points[0].z > 0)) {
35-
return true;
36-
}
37-
return false;
38-
}
3934
void load_cloudfile(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
4035
pcl::io::loadPLYFile(filename, *cloud);
4136
if (cloud_is_good(cloud)) {
@@ -62,5 +57,44 @@ class ParserPLY : public InterfaceParser {
6257
std::exit(-1);
6358
}
6459
};
60+
61+
class ParserTXT : public InterfaceParser {
62+
public:
63+
std::string parser_name = "ParserTXT";
64+
void load_cloudfile(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
65+
std::ifstream file(filename, std::ifstream::in);
66+
if (!file.is_open()) {
67+
pcl::console::print_error("\nError: Could not find %s\n", filename);
68+
std::exit(-1);
69+
}
70+
71+
double x_, y_, z_, r, g, b;
72+
73+
while (file >> x_ >> y_ >> z_ >> r >> g >> b) {
74+
pcl::PointXYZRGB pt;
75+
pt.x = x_;
76+
pt.y = y_;
77+
pt.z = z_;
78+
79+
uint8_t r_, g_, b_;
80+
r_ = uint8_t(r);
81+
g_ = uint8_t(g);
82+
b_ = uint8_t(b);
83+
84+
uint32_t rgb_ = ((uint32_t)r_ << 16 | (uint32_t)g_ << 8 | (uint32_t)b_);
85+
pt.rgb = *reinterpret_cast<float*>(&rgb_);
86+
87+
cloud->points.push_back(pt);
88+
}
89+
90+
file.close();
91+
if (cloud_is_good(cloud)) {
92+
return;
93+
}
94+
95+
pcl::console::print_error("\nError empty cloud.\n");
96+
std::exit(-1);
97+
}
98+
};
6599
} // namespace CloudParserLibrary
66100
#endif

parser/include/modern/inteface_parser.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ namespace CloudParserLibrary {
2626

2727
class InterfaceParser {
2828
public:
29+
bool cloud_is_good(pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) {
30+
if ((cloud->points.size() > 0) or
31+
(cloud->points[0].x > 0 && cloud->points[0].y > 0 && cloud->points[0].z > 0)) {
32+
return true;
33+
}
34+
return false;
35+
}
2936
virtual void load_cloudfile(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud) = 0;
3037
};
3138

parser/include/modern/parser.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ParserCloudFile {
3535
ParserCloudFile() {
3636
parser_factory.register_format("PCD", new ParserPCD());
3737
parser_factory.register_format("PLY", new ParserPLY());
38+
parser_factory.register_format("TXT", new ParserTXT());
3839
}
3940
void load_cloudfile(std::string filename, pcl::PointCloud<pcl::PointXYZRGB>::Ptr &cloud) {
4041
int position = filename.find_last_of(".");

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int main(int argc, char **argv) {
6565
viewer->addText(str, xpos, ypos, fontSize, r, g, b, "text1");
6666

6767
// set cloud color if not defined
68-
if (cloud->points[0].r <= 0 and cloud->points[0].g <= 0 and cloud->points[0].b <= 0) {
68+
if (cloud->points[0].r <= 0 && cloud->points[0].g <= 0 && cloud->points[0].b <= 0) {
6969
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> color_handler(cloud, 255, 255, 0);
7070
viewer->removeAllPointClouds(0);
7171
viewer->addPointCloud(cloud, color_handler, "POINTCLOUD");

0 commit comments

Comments
 (0)