snark
serializable_db.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_SENSORS_VELODYNE_IMPL_SERIALIZABLEDB_H_
20 #define SNARK_SENSORS_VELODYNE_IMPL_SERIALIZABLEDB_H_
21 
22 //#include <iostream>
23 #include <string>
24 #include <vector>
25 #include <boost/array.hpp>
26 #include <boost/serialization/nvp.hpp>
27 #include <boost/serialization/utility.hpp>
28 //#include <boost/serialization/list.hpp>
29 #include <boost/serialization/version.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/type_traits/is_class.hpp>
32 
33 namespace snark { namespace velodyne { namespace impl {
34 
38 template < bool AddDummyversion = false > // to avoid silly Windows warning
40 {
41  template< class Archive >
42  static void add_item_version( Archive& ) {}
43 };
44 
48 template <>
50 {
51  template< class Archive >
52  static void add_item_version( Archive& ar )
53  {
54  int itemversion( 0 );
55  ar & boost::serialization::make_nvp( "item_version", itemversion );
56  }
57 };
58 
62 template < typename T, std::size_t Size, bool HasUnexpectedItemversion = false >
64 {
65  public:
66  BOOST_STATIC_ASSERT( Size > 0 );
67  serializable_array() : m_count( Size ) {}
68  typedef boost::array< T, Size > Type;
69  const Type& operator()() const { return m_value; }
70  Type& operator()() { return m_value; }
71 
72  private:
73  std::size_t m_count;
74  Type m_value;
75 
76  friend class boost::serialization::access;
77  template < typename U, std::size_t S >
78  friend std::ostream& operator<<( std::ostream &os, const serializable_array< U, S >& gp );
79 
80  template< class Archive >
81  void serialize( Archive & ar, const unsigned int /* file_version */ )
82  {
83  ar & boost::serialization::make_nvp( "count", m_count );
84  serializable_array_item< boost::is_class< T >::value || HasUnexpectedItemversion >::add_item_version( ar ); // quick and dirty, hate it (see comment to this class above)
85  for( std::size_t i = 0; i < Size; ++i )
86  {
87  ar & boost::serialization::make_nvp( "item", m_value[i] );
88  }
89  }
90 };
91 
92 struct position_type
93 {
95 
96  template< class Archive >
97  void serialize( Archive & ar, const unsigned int /* file_version */ ) { ar & BOOST_SERIALIZATION_NVP( xyz ); }
98 };
99 
100 struct orientation_type
101 {
102  impl::serializable_array< float, 3 > rpy;
103 
104  template< class Archive >
105  void serialize( Archive & ar, const unsigned int /* file_version */ ) { ar & BOOST_SERIALIZATION_NVP( rpy ); }
106 };
107 
108 struct color_type
109 {
110  color_type() { rgb()[0] = rgb()[1] = rgb()[2] = 0; }
111  impl::serializable_array< float, 3 > rgb;
112 
113  template< class Archive >
114  void serialize( Archive & ar, const unsigned int /* file_version */ ) { ar & BOOST_SERIALIZATION_NVP( rgb ); }
115 };
116 
117 struct px_type
118 {
119  px_type()
120  : id_( 0 )
121  , rotCorrection_( 0 )
122  , vertCorrection_( 0 )
123  , distCorrection_( 0 )
124  , vertOffsetCorrection_( 0 )
125  , horizOffsetCorrection_( 0 )
126  {
127  }
128 
129  unsigned int id_;
130  double rotCorrection_;
131  double vertCorrection_;
132  float distCorrection_;
133  float vertOffsetCorrection_;
134  float horizOffsetCorrection_;
135 
136  template< class Archive >
137  void serialize( Archive & ar, const unsigned int /* file_version */ )
138  {
139  ar & BOOST_SERIALIZATION_NVP( id_ )
140  & BOOST_SERIALIZATION_NVP( rotCorrection_ )
141  & BOOST_SERIALIZATION_NVP( vertCorrection_ )
142  & BOOST_SERIALIZATION_NVP( distCorrection_ )
143  & BOOST_SERIALIZATION_NVP( vertOffsetCorrection_ )
144  & BOOST_SERIALIZATION_NVP( horizOffsetCorrection_ );
145  }
146 };
147 
148 struct point_type
149 {
150  px_type px;
151 
152  template< class Archive >
153  void serialize( Archive & ar, const unsigned int /* file_version */ ) { ar & BOOST_SERIALIZATION_NVP( px ); }
154 };
155 
156 struct serializable_db
157 {
158  float distLSB_;
159  impl::position_type position_;
160  impl::orientation_type orientation_;
161  impl::serializable_array< impl::color_type, 64 > colors_;
162  impl::serializable_array< unsigned int, 64 > enabled_; // quick and dirty, bool does not work
163  impl::serializable_array< unsigned char, 64 > intensity_;
164  impl::serializable_array< unsigned char, 64, true > minIntensity_;
165  impl::serializable_array< unsigned char, 64, true > maxIntensity_;
166  impl::serializable_array< impl::point_type, 64 > points_;
167 
168  friend class boost::serialization::access;
169  friend std::ostream & operator<<( std::ostream &os, const serializable_db &gp );
170 
171  template< class Archive >
172  void serialize( Archive & ar, const unsigned int /* file_version */ )
173  {
174  ar & BOOST_SERIALIZATION_NVP( distLSB_ )
175  & BOOST_SERIALIZATION_NVP( position_ )
176  & BOOST_SERIALIZATION_NVP( orientation_ )
177  & BOOST_SERIALIZATION_NVP( colors_ )
178  & BOOST_SERIALIZATION_NVP( enabled_ )
179  & BOOST_SERIALIZATION_NVP( intensity_ )
180  & BOOST_SERIALIZATION_NVP( minIntensity_ )
181  & BOOST_SERIALIZATION_NVP( maxIntensity_ )
182  & BOOST_SERIALIZATION_NVP( points_ );
183  }
184 };
185 
186 } } } // namespace snark { namespace velodyne { namespace impl {
187 
188 #endif // SNARK_SENSORS_VELODYNE_IMPL_SERIALIZABLEDB_H_