snark
frame.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_POINTS_FRAME_HEADER
20 #define SNARK_POINTS_FRAME_HEADER
21 
22 #include <fstream>
23 #include <iostream>
24 #include <sstream>
25 #include <vector>
26 #include <boost/optional.hpp>
27 
28 #include <Eigen/Geometry>
29 #include <comma/base/exception.h>
30 #include <comma/csv/options.h>
31 #include <comma/csv/names.h>
32 #include <comma/csv/stream.h>
33 #include <comma/io/stream.h>
34 #include <snark/math/rotation_matrix.h>
35 #include <snark/visiting/eigen.h>
36 #include "./timestamped.h"
37 
38 namespace snark{ namespace applications {
39 
40 struct position
41 {
42  position() : coordinates( Eigen::Vector3d::Zero() ), orientation( Eigen::Vector3d::Zero() ) {}
43  position( const Eigen::Vector3d& p ) : coordinates( p ), orientation( Eigen::Vector3d::Zero() ) {}
44  position( const Eigen::Vector3d& p, const Eigen::Vector3d& o ) : coordinates( p ), orientation( o ) {}
45  Eigen::Vector3d coordinates;
46  Eigen::Vector3d orientation;
47 };
48 
49 } }
50 
51 namespace comma { namespace visiting {
52 
53 template <> struct traits< snark::applications::position >
54 {
55  template < typename Key, class Visitor >
56  static void visit( const Key&, snark::applications::position& p, Visitor& v )
57  {
58  v.apply( "x", p.coordinates.x() );
59  v.apply( "y", p.coordinates.y() );
60  v.apply( "z", p.coordinates.z() );
61  v.apply( "roll", p.orientation.x() );
62  v.apply( "pitch", p.orientation.y() );
63  v.apply( "yaw", p.orientation.z() );
64  }
65 
66  template < typename Key, class Visitor >
67  static void visit( const Key&, const snark::applications::position& p, Visitor& v )
68  {
69  v.apply( "x", p.coordinates.x() );
70  v.apply( "y", p.coordinates.y() );
71  v.apply( "z", p.coordinates.z() );
72  v.apply( "roll", p.orientation.x() );
73  v.apply( "pitch", p.orientation.y() );
74  v.apply( "yaw", p.orientation.z() );
75  }
76 };
77 
78 } }
79 
80 namespace snark{ namespace applications {
81 
82 class frame
83 {
84  public:
85  typedef timestamped< position, boost::posix_time::ptime > position_type;
86 
87  typedef position_type point_type;
88 
89  frame( const position& p, bool to = false, bool interpolate = true, bool rotation_present = false );
90 
91  frame( const comma::csv::options& options, bool discardOutOfOrder, boost::optional< boost::posix_time::time_duration > maxGap, bool outputframe, bool to = false, bool interpolate = true, bool rotation_present = false );
92 
93  ~frame();
94 
95  const point_type* converted( const point_type& rhs );
96 
97  bool discarded() const { return m_discarded; }
98 
99  const position& last() const { return m_position; }
100 
101  const bool outputframe;
102 
103  private:
104  void set_position( const position& p );
105 
106  const point_type& convert( const point_type& rhs );
107 
108  bool m_to;
109  bool m_interpolate;
110  position m_position;
111  ::Eigen::Vector3d m_orientation;
112  ::Eigen::Matrix3d m_rotation;
113  ::Eigen::Affine3d m_transform;
114  ::Eigen::Translation3d m_translation;
115 
116  boost::scoped_ptr< comma::io::istream > m_istream;
117  boost::scoped_ptr< comma::csv::input_stream< position_type > > m_is;
118  std::pair< position_type, position_type > m_pair;
119  point_type m_converted;
120  bool m_discardOutOfOrder;
121  bool m_discarded;
122  boost::optional< boost::posix_time::time_duration > m_maxGap;
123  bool rotation_present_;
124 };
125 
126 } } // namespace snark{ namespace applications {
127 
128 #endif // SNARK_POINTS_FRAME_HEADER