11 #include <boost/tuple/tuple.hpp>
20 #include <pcl/io/pcd_io.h>
21 #include <pcl/point_types.h>
29 PCDReader::PCDReader(
const std::string &observationFileName) :
30 KernelReader(observationFileName)
34 PCDReader::~PCDReader()
38 void PCDReader::init_()
45 std::ifstream ifs(observationFileName_.c_str());
47 throw ObservationIOError(std::string(
"Could not open file ") +
48 observationFileName_ +
" for reading.");
51 if (!std::getline(ifs, line) ||
54 line.find(
"PCD") == std::string::npos)
55 throw ObservationIOError(
"File " +
56 observationFileName_ +
57 " does not appear to be PCD.");
59 if (!std::getline(ifs, line) || !std::getline(ifs, line) ||
61 throw ObservationIOError(
"File " +
62 observationFileName_ +
63 " does not appear to be PCD.");
65 if (line.find(
"normal_x") != std::string::npos)
67 if (line.find(
"rgb") != std::string::npos ||
68 line.find(
"r g b") != std::string::npos)
71 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud
72 (
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
74 if (pcl::io::loadPCDFile<pcl::PointXYZRGBNormal>
75 (observationFileName_, *cloud) == -1)
76 throw ObservationIOError(
"PCL cannot read file `" + observationFileName_ +
"'.");
78 kc_ = nukleiFromPcl(*cloud,
true);
83 pcl::PointCloud<pcl::PointNormal>::Ptr cloud
84 (
new pcl::PointCloud<pcl::PointNormal>);
86 if (pcl::io::loadPCDFile<pcl::PointNormal>
87 (observationFileName_, *cloud) == -1)
88 throw ObservationIOError(
"PCL cannot read file `" + observationFileName_ +
"'.");
90 kc_ = nukleiFromPcl(*cloud,
true);
95 if (line.find(
"rgb") != std::string::npos ||
96 line.find(
"r g b") != std::string::npos)
99 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud
100 (
new pcl::PointCloud<pcl::PointXYZRGB>);
102 if (pcl::io::loadPCDFile<pcl::PointXYZRGB>
103 (observationFileName_, *cloud) == -1)
104 throw ObservationIOError(
"PCL cannot read file `" + observationFileName_ +
"'.");
106 kc_ = nukleiFromPcl(*cloud,
true);
111 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud
112 (
new pcl::PointCloud<pcl::PointXYZ>);
114 if (pcl::io::loadPCDFile<pcl::PointXYZ>
115 (observationFileName_, *cloud) == -1)
116 throw ObservationIOError(
"PCL cannot read file `" + observationFileName_ +
"'.");
118 kc_ = nukleiFromPcl(*cloud,
true);
125 throw ObservationIOError(
"This method requires PCL.");
130 NUKLEI_UNIQUE_PTR<Observation> PCDReader::readObservation_()
132 NUKLEI_TRACE_BEGIN();
133 if (idx_ < 0)
NUKLEI_THROW(
"Reader does not seem inited.");
134 if (idx_ >=
int(kc_.size()))
return NUKLEI_UNIQUE_PTR<Observation>();
135 else return NUKLEI_UNIQUE_PTR<Observation>(
new PCDObservation(kc_.at(idx_++)));
139 PCDWriter::PCDWriter(
const std::string &observationFileName) :
140 KernelWriter(observationFileName)
142 NUKLEI_TRACE_BEGIN();
146 PCDWriter::~PCDWriter()
151 void PCDWriter::writeBuffer()
153 NUKLEI_TRACE_BEGIN();
155 #ifdef NUKLEI_USE_PCL
158 if (kc_.kernelType() == kernel::base::R3)
160 if (kc_.front().hasDescriptor())
162 pcl::PointCloud<pcl::PointXYZRGB> cloud = pclFromNuklei<pcl::PointXYZRGB>(kc_);
163 pcl::io::savePCDFileASCII(observationFileName_, cloud);
167 pcl::PointCloud<pcl::PointXYZ> cloud = pclFromNuklei<pcl::PointXYZ>(kc_);
168 pcl::io::savePCDFileASCII(observationFileName_, cloud);
171 else if (kc_.kernelType() == kernel::base::R3XS2P)
173 if (kc_.front().hasDescriptor())
175 pcl::PointCloud<pcl::PointXYZRGBNormal> cloud = pclFromNuklei<pcl::PointXYZRGBNormal>(kc_);
176 pcl::io::savePCDFileASCII(observationFileName_, cloud);
180 pcl::PointCloud<pcl::PointNormal> cloud = pclFromNuklei<pcl::PointNormal>(kc_);
181 pcl::io::savePCDFileASCII(observationFileName_, cloud);