Fawkes API  Fawkes Development Version
storage_adapter.h
1 
2 /***************************************************************************
3  * storage_adapter.h - Utility to store differently typed point clouds in
4  * common container.
5  *
6  * Created: Fri Nov 30 16:40:51 2012
7  * Copyright 2011-2012 Tim Niemueller [www.niemueller.de]
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef _LIBS_PCL_UTILS_STORAGE_ADAPTER_H_
24 #define _LIBS_PCL_UTILS_STORAGE_ADAPTER_H_
25 
26 #include <pcl/point_cloud.h>
27 #include <pcl_utils/transforms.h>
28 #include <pcl_utils/utils.h>
29 
30 namespace fawkes {
31 namespace pcl_utils {
32 
33 template <typename PointT>
34 class PointCloudStorageAdapter;
35 
37 {
38 public:
39  /** Virtual empty destructor. */
40  virtual ~StorageAdapter(){};
41 
42  template <typename PointT>
43  bool is_pointtype() const;
44 
45  template <typename PointT>
47 
48  virtual void transform(const std::string &target_frame, const tf::Transformer &transformer) = 0;
49 
50  virtual void transform(const std::string & target_frame,
51  const Time & target_time,
52  const std::string & fixed_frame,
53  const tf::Transformer &transformer) = 0;
54 
55  virtual const char * get_typename() = 0;
56  virtual StorageAdapter *clone() const = 0;
57  virtual size_t point_size() const = 0;
58  virtual unsigned int width() const = 0;
59  virtual unsigned int height() const = 0;
60  virtual size_t num_points() const = 0;
61  virtual void * data_ptr() const = 0;
62  virtual std::string frame_id() const = 0;
63  virtual void get_time(fawkes::Time &time) const = 0;
64 };
65 
66 template <typename PointT>
68 {
69 public:
70  /** Constructor.
71  * @param cloud cloud to encapsulate.
72  */
74  {
75  }
76 
77  /** Copy constructor.
78  * @param p storage adapter to copy
79  */
81  {
82  }
83 
84  /** The point cloud. */
86 
87  /** Get PCL shared pointer to cloud.
88  * @return PCL shared pointer to cloud
89  */
92  {
93  return pcl_utils::cloudptr_from_refptr(cloud);
94  }
95 
96  /** Get PCL const shared pointer to cloud.
97  * @return PCL const shared pointer to cloud
98  */
101  {
102  return pcl_utils::cloudptr_from_refptr(cloud);
103  }
104 
105  /** Clone this storage adapter.
106  * @return A pointer to a copy of this storage adapter.
107  */
108  virtual StorageAdapter *clone() const;
109 
110  /** Transform the point cloud.
111  * @param target_frame frame to transform to
112  * @param transformer transformer to get transform from
113  */
114  virtual void transform(const std::string &target_frame, const tf::Transformer &transformer);
115 
116  /** Transform point cloud.
117  * @param target_frame frame to transform to
118  * @param target_time time for which to transform
119  * @param fixed_frame frame fixed over time
120  * @param transformer transformer to get transform from
121  */
122  virtual void transform(const std::string & target_frame,
123  const Time & target_time,
124  const std::string & fixed_frame,
125  const tf::Transformer &transformer);
126 
127  /** Get typename of storage adapter.
128  * @return type name
129  */
130  virtual const char *
132  {
133  return typeid(this).name();
134  }
135 
136  /** Get size of a point.
137  * @return size in bytes of a single point
138  */
139  virtual size_t
140  point_size() const
141  {
142  return sizeof(PointT);
143  }
144 
145  /** Get width of point cloud.
146  * @return width of point cloud
147  */
148  virtual unsigned int
149  width() const
150  {
151  return cloud->width;
152  }
153 
154  /** Get height of point cloud.
155  * @return height of point cloud
156  */
157  virtual unsigned int
158  height() const
159  {
160  return cloud->height;
161  }
162 
163  /** Get numer of points in point cloud.
164  * @return number of points
165  */
166  virtual size_t
167  num_points() const
168  {
169  return cloud->points.size();
170  }
171 
172  /** Get pointer on data.
173  * @return pointer on data
174  */
175  virtual void *
176  data_ptr() const
177  {
178  return &cloud->points[0];
179  }
180 
181  /** Get frame ID of point cloud.
182  * @return Frame ID of point cloud.
183  */
184  virtual std::string
185  frame_id() const
186  {
187  return cloud->header.frame_id;
188  }
189 
190  /** Get last capture time.
191  * @param time upon return contains last capture time
192  */
193  virtual void get_time(fawkes::Time &time) const;
194 };
195 
196 template <typename PointT>
197 bool
199 {
201  dynamic_cast<const PointCloudStorageAdapter<PointT> *>(this);
202  return (!!pa);
203 }
204 
205 template <typename PointT>
208 {
210  if (!pa) {
211  throw Exception("PointCloud storage adapter is not of anticipated type");
212  }
213  return pa;
214 }
215 
216 template <typename PointT>
219 {
220  return new PointCloudStorageAdapter<PointT>(this);
221 }
222 
223 template <typename PointT>
224 void
226 {
227  pcl_utils::get_time(cloud, time);
228 }
229 
230 template <typename PointT>
231 void
232 PointCloudStorageAdapter<PointT>::transform(const std::string & target_frame,
233  const tf::Transformer &transformer)
234 {
236  pcl_utils::transform_pointcloud(target_frame, **cloud, tmp, transformer);
237  **cloud = tmp;
238 }
239 
240 template <typename PointT>
241 void
242 PointCloudStorageAdapter<PointT>::transform(const std::string & target_frame,
243  const Time & target_time,
244  const std::string & fixed_frame,
245  const tf::Transformer &transformer)
246 {
248  pcl_utils::transform_pointcloud(
249  target_frame, target_time, fixed_frame, **cloud, tmp, transformer);
250  **cloud = tmp;
251 }
252 
253 } // end namespace pcl_utils
254 } // end namespace fawkes
255 
256 #endif
Base class for exceptions in Fawkes.
Definition: exception.h:36
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
A class for handling time.
Definition: time.h:93
Adapter class for PCL point types.
virtual unsigned int height() const
Get height of point cloud.
virtual void get_time(fawkes::Time &time) const
Get last capture time.
virtual const char * get_typename()
Get typename of storage adapter.
PointCloudStorageAdapter(const PointCloudStorageAdapter< PointT > *p)
Copy constructor.
virtual std::string frame_id() const
Get frame ID of point cloud.
virtual size_t num_points() const
Get numer of points in point cloud.
virtual StorageAdapter * clone() const
Clone this storage adapter.
PointCloudStorageAdapter(RefPtr< pcl::PointCloud< PointT >> cloud)
Constructor.
virtual void * data_ptr() const
Get pointer on data.
pcl::PointCloud< PointT >::ConstPtr cloud_const_ptr()
Get PCL const shared pointer to cloud.
virtual size_t point_size() const
Get size of a point.
const RefPtr< pcl::PointCloud< PointT > > cloud
The point cloud.
pcl::PointCloud< PointT >::Ptr cloud_ptr()
Get PCL shared pointer to cloud.
virtual unsigned int width() const
Get width of point cloud.
virtual void transform(const std::string &target_frame, const tf::Transformer &transformer)
Transform the point cloud.
virtual void transform(const std::string &target_frame, const Time &target_time, const std::string &fixed_frame, const tf::Transformer &transformer)=0
Transform point cloud.
virtual size_t point_size() const =0
Get size of a point.
virtual unsigned int width() const =0
Get width of point cloud.
virtual void transform(const std::string &target_frame, const tf::Transformer &transformer)=0
Transform point cloud.
virtual unsigned int height() const =0
Get height of point cloud.
PointCloudStorageAdapter< PointT > * as_pointtype()
Transform to specific PointCloudStorageAdapter.
virtual size_t num_points() const =0
Get numer of points in point cloud.
virtual void get_time(fawkes::Time &time) const =0
Get last capture time.
virtual std::string frame_id() const =0
Get frame ID of point cloud.
virtual void * data_ptr() const =0
Get pointer on data.
virtual StorageAdapter * clone() const =0
Clone this storage adapter.
virtual const char * get_typename()=0
Get typename of storage adapter.
virtual ~StorageAdapter()
Virtual empty destructor.
bool is_pointtype() const
Check if storage adapter is for specified point type.
Coordinate transforms between any two frames in a system.
Definition: transformer.h:65
Fawkes library namespace.