snark
Dataset.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_GRAPHICS_APPLICATIONS_LABELPOINTS_DATASET_H_
20 #define SNARK_GRAPHICS_APPLICATIONS_LABELPOINTS_DATASET_H_
21 
22 #include <deque>
23 #include <comma/base/types.h>
24 #include <comma/csv/options.h>
25 #include <snark/math/interval.h>
26 #include <snark/graphics/qt3d/vertex_buffer.h>
27 #include "./PointMap.h"
28 #include "./PointWithId.h"
29 #include <Qt3D/qglpainter.h>
30 
31 namespace snark { namespace graphics { namespace View {
32 
33 class BasicDataset
34 {
35  public:
36  struct Data
37  {
38  comma::uint32 id;
39  std::size_t index;
40  Data() {}
41  Data( const Data& rhs ) { operator=( rhs ); }
42  Data( comma::uint32 id, std::size_t index ) : id( id ), index( index ) {}
43  };
44  typedef PointMap< Eigen::Vector3d, Data > Points;
45  typedef std::map< comma::uint32, Points > Partitions;
46  BasicDataset();
47  BasicDataset( const Eigen::Vector3d& offset );
48  const Points& points() const;
49  const Partitions& partitions() const;
50  const Eigen::Vector3d& offset() const;
51  const math::closed_interval< double, 3 >& extents() const;
52  void init();
53  void draw( QGLPainter* painter ) const;
54  void visible( bool visible );
55  bool visible() const;
56  void clear();
57  void insert( const Points& m );
58  void erase( const Points& m );
59 
60  protected:
61  bool m_visible;
62  Points m_points;
63  Partitions m_partitions;
64  boost::scoped_ptr< qt3d::vertex_buffer > m_vertices;
65  boost::optional< Eigen::Vector3d > m_offset;
66  boost::optional< math::closed_interval< double, 3 > > m_extents;
67  void insert( const Eigen::Vector3d& p, const Data& data );
68 };
69 
70 class Dataset : public BasicDataset
71 {
72  public:
73  Dataset( const std::string& filename, const comma::csv::options& options, bool relabelDuplicated );
74  Dataset( const std::string& filename, const comma::csv::options& options, const Eigen::Vector3d& offset, bool relabelDuplicated );
75  void save();
76  void saveAs( const std::string& f );
77  void backup();
78  void label( const Eigen::Vector3d& p, comma::uint32 id );
79  void label( const Points& p, comma::uint32 id );
80  void writable( bool enabled );
81  bool writable() const;
82  bool modified() const;
83  void commit();
84  BasicDataset& selection();
85  const BasicDataset& selection() const;
86  const std::string& filename() const;
87  const comma::csv::options& options() const;
88  bool valid() const;
89  static void repair( const comma::csv::options& options );
90 
91  private:
92  void load();
93  void insert( const Points& m );
94  void erase( const Points& m );
95  void clear();
96  std::size_t labelimpl( const Eigen::Vector3d& p, comma::uint32 id );
97  void labelDuplicated();
98  //typedef std::deque< std::pair< PointWithId, std::vector< std::string > > > Deque;
99  typedef std::deque< std::pair< PointWithId, std::string > > Deque;
100  Deque m_deque;
101  std::string m_filename;
102  const comma::csv::options m_options;
103  boost::scoped_ptr< BasicDataset > m_selection;
104  bool m_writable;
105  bool m_modified;
106  bool m_valid;
107 };
108 
109 } } } // namespace snark { namespace graphics { namespace View {
110 
111 #endif // SNARK_GRAPHICS_APPLICATIONS_LABELPOINTS_DATASET_H_