snark
CameraReader.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 
20 
21 #ifndef SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_CAMERAREADER_H_
22 #define SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_CAMERAREADER_H_
23 
24 #include <deque>
25 #include <iostream>
26 #include <cmath>
27 #include <sstream>
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/thread.hpp>
30 #include <comma/csv/stream.h>
31 #include <comma/io/stream.h>
32 #include <snark/visiting/eigen.h>
33 
34 namespace snark { namespace graphics { namespace View {
35 
36 struct point_with_orientation // quick and dirty
37 {
38  Eigen::Vector3d point;
39  Eigen::Vector3d orientation;
40 };
41 
42 
43 class CameraReader
44 {
45  public:
46  const comma::csv::options options;
47 
48  CameraReader( comma::csv::options& options );
49 
50  void start();
51  bool readOnce();
52  bool ready() const { return m_stream->ready(); }
53  void render();
54 
55  bool isShutdown() const;
56  void shutdown();
57  Eigen::Vector3d position() const;
58  Eigen::Vector3d orientation() const;
59  void read();
60 
61  private:
62  bool m_shutdown;
63  comma::io::istream m_istream;
64  Eigen::Vector3d m_position;
65  Eigen::Vector3d m_orientation;
66  boost::scoped_ptr< comma::csv::input_stream< point_with_orientation > > m_stream;
67  mutable boost::recursive_mutex m_mutex; // todo: make lock-free
68  boost::scoped_ptr< boost::thread > m_thread;
69 
70 };
71 
72 } } } // namespace snark { namespace graphics { namespace View {
73 
74 namespace comma { namespace visiting {
75 
76 template <> struct traits< snark::graphics::View::point_with_orientation >
77 {
78  template < typename Key, class Visitor >
79  static void visit( Key, snark::graphics::View::point_with_orientation& p, Visitor& v )
80  {
81  v.apply( "point", p.point );
82  v.apply( "roll", p.orientation.x() );
83  v.apply( "pitch", p.orientation.y() );
84  v.apply( "yaw", p.orientation.z() );
85  }
86 
87  template < typename Key, class Visitor >
88  static void visit( Key, const snark::graphics::View::point_with_orientation& p, Visitor& v )
89  {
90  v.apply( "point", p.point );
91  v.apply( "roll", p.orientation.x() );
92  v.apply( "pitch", p.orientation.y() );
93  v.apply( "yaw", p.orientation.z() );
94  }
95 };
96 
97 } } // namespace comma { namespace visiting {
98 
99 #endif /*SNARK_GRAPHICS_APPLICATIONS_VIEWPOINTS_CAMERAREADER_H_*/