PCDObservationIO.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <fstream>
8 #include <cassert>
9 #include <algorithm>
10 #include <cmath>
11 #include <boost/tuple/tuple.hpp>
12 
14 #include <nuklei/PCDObservation.h>
15 #include <nuklei/Common.h>
16 #include <nuklei/Match.h>
17 #include <nuklei/Indenter.h>
18 
19 #ifdef NUKLEI_USE_PCL
20 #include <pcl/io/pcd_io.h>
21 #include <pcl/point_types.h>
22 #include <nuklei/PCLBridge.h>
23 #endif
24 
25 namespace nuklei {
26 
27 
28 
29  PCDReader::PCDReader(const std::string &observationFileName) :
30  KernelReader(observationFileName)
31  {
32  }
33 
34  PCDReader::~PCDReader()
35  {
36  }
37 
38  void PCDReader::init_()
39  {
40  NUKLEI_TRACE_BEGIN();
41 #ifdef NUKLEI_USE_PCL
42  kc_.clear();
43 
44  {
45  std::ifstream ifs(observationFileName_.c_str());
46  if (!ifs.is_open())
47  throw ObservationIOError(std::string("Could not open file ") +
48  observationFileName_ + " for reading.");
49 
50  std::string line;
51  if (!std::getline(ifs, line) ||
52  line.size() < 1 ||
53  line.at(0) != '#' ||
54  line.find("PCD") == std::string::npos)
55  throw ObservationIOError("File " +
56  observationFileName_ +
57  " does not appear to be PCD.");
58 
59  if (!std::getline(ifs, line) || !std::getline(ifs, line) ||
60  line.size() < 1)
61  throw ObservationIOError("File " +
62  observationFileName_ +
63  " does not appear to be PCD.");
64 
65  if (line.find("normal_x") != std::string::npos)
66  {
67  if (line.find("rgb") != std::string::npos ||
68  line.find("r g b") != std::string::npos)
69  {
70  // Normals & RGB
71  pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud
72  (new pcl::PointCloud<pcl::PointXYZRGBNormal>);
73 
74  if (pcl::io::loadPCDFile<pcl::PointXYZRGBNormal>
75  (observationFileName_, *cloud) == -1)
76  throw ObservationIOError("PCL cannot read file `" + observationFileName_ + "'.");
77  else
78  kc_ = nukleiFromPcl(*cloud, true);
79  }
80  else
81  {
82  // Normals
83  pcl::PointCloud<pcl::PointNormal>::Ptr cloud
84  (new pcl::PointCloud<pcl::PointNormal>);
85 
86  if (pcl::io::loadPCDFile<pcl::PointNormal>
87  (observationFileName_, *cloud) == -1)
88  throw ObservationIOError("PCL cannot read file `" + observationFileName_ + "'.");
89  else
90  kc_ = nukleiFromPcl(*cloud, true);
91  }
92  }
93  else
94  {
95  if (line.find("rgb") != std::string::npos ||
96  line.find("r g b") != std::string::npos)
97  {
98  // RGB
99  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud
100  (new pcl::PointCloud<pcl::PointXYZRGB>);
101 
102  if (pcl::io::loadPCDFile<pcl::PointXYZRGB>
103  (observationFileName_, *cloud) == -1)
104  throw ObservationIOError("PCL cannot read file `" + observationFileName_ + "'.");
105  else
106  kc_ = nukleiFromPcl(*cloud, true);
107  }
108  else
109  {
110  // only xyz
111  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud
112  (new pcl::PointCloud<pcl::PointXYZ>);
113 
114  if (pcl::io::loadPCDFile<pcl::PointXYZ>
115  (observationFileName_, *cloud) == -1)
116  throw ObservationIOError("PCL cannot read file `" + observationFileName_ + "'.");
117  else
118  kc_ = nukleiFromPcl(*cloud, true);
119  }
120  }
121  }
122 
123  idx_ = 0;
124 #else
125  throw ObservationIOError("This method requires PCL.");
126 #endif
127  NUKLEI_TRACE_END();
128  }
129 
130  NUKLEI_UNIQUE_PTR<Observation> PCDReader::readObservation_()
131  {
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_++)));
136  NUKLEI_TRACE_END();
137  }
138 
139  PCDWriter::PCDWriter(const std::string &observationFileName) :
140  KernelWriter(observationFileName)
141  {
142  NUKLEI_TRACE_BEGIN();
143  NUKLEI_TRACE_END();
144  }
145 
146  PCDWriter::~PCDWriter()
147  {
148  }
149 
150 
151  void PCDWriter::writeBuffer()
152  {
153  NUKLEI_TRACE_BEGIN();
154 
155 #ifdef NUKLEI_USE_PCL
156  NUKLEI_ASSERT(kc_.size() > 0);
157 
158  if (kc_.kernelType() == kernel::base::R3)
159  {
160  if (kc_.front().hasDescriptor())
161  {
162  pcl::PointCloud<pcl::PointXYZRGB> cloud = pclFromNuklei<pcl::PointXYZRGB>(kc_);
163  pcl::io::savePCDFileASCII(observationFileName_, cloud);
164  }
165  else
166  {
167  pcl::PointCloud<pcl::PointXYZ> cloud = pclFromNuklei<pcl::PointXYZ>(kc_);
168  pcl::io::savePCDFileASCII(observationFileName_, cloud);
169  }
170  }
171  else if (kc_.kernelType() == kernel::base::R3XS2P)
172  {
173  if (kc_.front().hasDescriptor())
174  {
175  pcl::PointCloud<pcl::PointXYZRGBNormal> cloud = pclFromNuklei<pcl::PointXYZRGBNormal>(kc_);
176  pcl::io::savePCDFileASCII(observationFileName_, cloud);
177  }
178  else
179  {
180  pcl::PointCloud<pcl::PointNormal> cloud = pclFromNuklei<pcl::PointNormal>(kc_);
181  pcl::io::savePCDFileASCII(observationFileName_, cloud);
182  }
183  }
184  else NUKLEI_THROW("Kernel type unsupported by PCD.");
185 #else
186  NUKLEI_THROW("This method requires PCL.");
187 #endif
188 
189  NUKLEI_TRACE_END();
190  }
191 
192 }
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.