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 
36 class StorageAdapter
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  */
80  PointCloudStorageAdapter(const PointCloudStorageAdapter<PointT> *p) : cloud(p->cloud)
81  {
82  }
83 
84  /** The point cloud. */
86 
87  /** Get PCL shared pointer to cloud.
88  * @return PCL shared pointer to cloud
89  */
91  cloud_ptr()
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  virtual StorageAdapter *clone() const;
106 
107  virtual void transform(const std::string &target_frame, const tf::Transformer &transformer);
108 
109  virtual void transform(const std::string & target_frame,
110  const Time & target_time,
111  const std::string & fixed_frame,
112  const tf::Transformer &transformer);
113 
114  virtual const char *
115  get_typename()
116  {
117  return typeid(this).name();
118  }
119  virtual size_t
120  point_size() const
121  {
122  return sizeof(PointT);
123  }
124  virtual unsigned int
125  width() const
126  {
127  return cloud->width;
128  }
129  virtual unsigned int
130  height() const
131  {
132  return cloud->height;
133  }
134  virtual size_t
135  num_points() const
136  {
137  return cloud->points.size();
138  }
139  virtual void *
140  data_ptr() const
141  {
142  return &cloud->points[0];
143  }
144  virtual std::string
145  frame_id() const
146  {
147  return cloud->header.frame_id;
148  }
149  virtual void get_time(fawkes::Time &time) const;
150 };
151 
152 template <typename PointT>
153 bool
155 {
156  const PointCloudStorageAdapter<PointT> *pa =
157  dynamic_cast<const PointCloudStorageAdapter<PointT> *>(this);
158  return (!!pa);
159 }
160 
161 template <typename PointT>
162 PointCloudStorageAdapter<PointT> *
164 {
165  PointCloudStorageAdapter<PointT> *pa = dynamic_cast<PointCloudStorageAdapter<PointT> *>(this);
166  if (!pa) {
167  throw Exception("PointCloud storage adapter is not of anticipated type");
168  }
169  return pa;
170 }
171 
172 template <typename PointT>
173 StorageAdapter *
174 PointCloudStorageAdapter<PointT>::clone() const
175 {
176  return new PointCloudStorageAdapter<PointT>(this);
177 }
178 
179 template <typename PointT>
180 void
182 {
183  pcl_utils::get_time(cloud, time);
184 }
185 
186 template <typename PointT>
187 void
188 PointCloudStorageAdapter<PointT>::transform(const std::string & target_frame,
189  const tf::Transformer &transformer)
190 {
192  pcl_utils::transform_pointcloud(target_frame, **cloud, tmp, transformer);
193  **cloud = tmp;
194 }
195 
196 template <typename PointT>
197 void
198 PointCloudStorageAdapter<PointT>::transform(const std::string & target_frame,
199  const Time & target_time,
200  const std::string & fixed_frame,
201  const tf::Transformer &transformer)
202 {
204  pcl_utils::transform_pointcloud(
205  target_frame, target_time, fixed_frame, **cloud, tmp, transformer);
206  **cloud = tmp;
207 }
208 
209 } // end namespace pcl_utils
210 } // end namespace fawkes
211 
212 #endif
fawkes::pcl_utils::PointCloudStorageAdapter
Definition: storage_adapter.h:46
fawkes::pcl_utils::StorageAdapter::point_size
virtual size_t point_size() const =0
fawkes::pcl_utils::StorageAdapter::height
virtual unsigned int height() const =0
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:57
fawkes::pcl_utils::StorageAdapter::width
virtual unsigned int width() const =0
fawkes::tf::Transformer
Definition: transformer.h:74
fawkes::pcl_utils::StorageAdapter::num_points
virtual size_t num_points() const =0
fawkes::pcl_utils::StorageAdapter::get_time
virtual void get_time(fawkes::Time &time) const =0
fawkes::pcl_utils::PointCloudStorageAdapter::cloud
const RefPtr< pcl::PointCloud< PointT > > cloud
The point cloud.
Definition: storage_adapter.h:97
fawkes::pcl_utils::PointCloudStorageAdapter::PointCloudStorageAdapter
PointCloudStorageAdapter(RefPtr< pcl::PointCloud< PointT >> cloud)
Constructor.
Definition: storage_adapter.h:85
fawkes::pcl_utils::StorageAdapter::transform
virtual void transform(const std::string &target_frame, const tf::Transformer &transformer)=0
fawkes
fawkes::pcl_utils::StorageAdapter
Definition: storage_adapter.h:48
pcl::PointCloud
Definition: pointcloud.h:38
fawkes::pcl_utils::StorageAdapter::data_ptr
virtual void * data_ptr() const =0
fawkes::pcl_utils::PointCloudStorageAdapter::cloud_const_ptr
pcl::PointCloud< PointT >::ConstPtr cloud_const_ptr()
Get PCL const shared pointer to cloud.
Definition: storage_adapter.h:112
fawkes::pcl_utils::StorageAdapter::frame_id
virtual std::string frame_id() const =0
fawkes::pcl_utils::StorageAdapter::clone
virtual StorageAdapter * clone() const =0
fawkes::Time
Definition: time.h:98
fawkes::pcl_utils::StorageAdapter::get_typename
virtual const char * get_typename()=0
fawkes::pcl_utils::StorageAdapter::is_pointtype
bool is_pointtype() const
Definition: storage_adapter.h:166
fawkes::pcl_utils::PointCloudStorageAdapter::cloud_ptr
pcl::PointCloud< PointT >::Ptr cloud_ptr()
Get PCL shared pointer to cloud.
Definition: storage_adapter.h:103
fawkes::pcl_utils::StorageAdapter::as_pointtype
PointCloudStorageAdapter< PointT > * as_pointtype()
Definition: storage_adapter.h:175
fawkes::pcl_utils::StorageAdapter::~StorageAdapter
virtual ~StorageAdapter()
Virtual empty destructor.
Definition: storage_adapter.h:52
fawkes::Exception
Definition: exception.h:41