iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseData.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant <wgrant@usc.edu>
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_POINTCLOUD2_DETAILS_SPARSEDATA_H
37 #define INCLUDE_NRT_POINTCLOUD2_DETAILS_SPARSEDATA_H
38 
41 #include <nrt/External/cereal/types/vector.hpp>
42 #include <nrt/External/cereal/types/string.hpp>
43 #include <nrt/External/cereal/types/map.hpp>
44 #include <vector>
45 #include <string>
46 #include <map>
47 
48 namespace nrt
49 {
50  //! Base class for marking a field as sparse
51  struct SparseFieldCore {};
52 
53  //! Used to wrap a field and mark it as sparse
54  /*! This wraps a class in an identifier
55  that can be used to determine whether it is
56  a sparse field. Wrapping a class to mark it as
57  sparse is only necessary during insertion */
58  template <class T>
59  struct SparseField : public SparseFieldCore
60  {
61  static_assert( !std::is_base_of<SparseFieldCore, T>(),
62  "SparseField cannot wrap another SparseField!" );
63 
64  typedef T Actual;
65  };
66 
67  template <bool IsSparse, class T>
68  struct StripSparseCore;
69 
70  template <class T>
71  struct StripSparseCore<false, T>
72  {
73  typedef T type;
74  };
75 
76  template <class T>
77  struct StripSparseCore<true, T>
78  {
79  typedef typename T::Actual type;
80  };
81 
82  //! Strips sparse attribute, if present, from a type
83  /*! True type can be accessed through the type typedef */
84  template <class T>
85  struct StripSparse : StripSparseCore<std::is_base_of<SparseFieldCore, T>::value, T> { };
86 
87  namespace exception
88  {
89  //! Exception thrown for Sparse data not found
91  {
92  public:
93  SparseDataNotFoundException( size_t const & index, std::string const & name ) throw();
94 
95  virtual ~SparseDataNotFoundException() throw();
96 
97  size_t const & index() const;
98 
99  std::string const & name() const;
100 
101  private:
102  size_t itsIndex;
103  std::string itsName;
104  std::string whatstring;
105  };
106  }
107 
108  //! A class for representing Sparse (one or less entries per geometry)
109  /*! Sparse data is stored in an opaque map structure. When
110  creating new Sparse data, the size in bytes of the data
111  to be represented must be known, along with the name of
112  the associatd data.
113 
114  Access to the data is only done through typed accessors */
116  {
117  typedef std::map<size_t, std::vector<unsigned char, nrt::AlignedAllocator<unsigned char, 16>>> MapType;
118  //typedef std::map<size_t, std::vector<unsigned char>> MapType;
119  typedef MapType::const_iterator const_iterator;
120 
121  public:
122  //! Creates an empty (invalid) SparseData
123  SparseData() = default;
124 
125  //! Create a new Sparsedata
126  /*! @param[in] elemSize Size in bytes of an element
127  @param[in] name Name of the type associated with the data */
128  SparseData( size_t elemSize, std::string const & name );
129 
130  //! Copy construction
131  SparseData( SparseData const & other ) = default;
132 
133  //! Move construction
134  SparseData( SparseData && other ) = default;
135 
136  //! Assignment
137  SparseData & operator=( SparseData const & other ) = default;
138 
139  //! Move assignment
140  SparseData & operator=( SparseData && other ) = default;
141 
142  //! Removes all values that have keys equal to or greater than than size
143  void clearFrom( size_t const size );
144 
145  //! Clears all data
146  void clear();
147 
148  //! Returns the number of elements in the container
149  inline size_t const size() const;
150 
151  //! Returns the name of the type associated with this data
152  inline std::string const & name() const;
153 
154  //! Gets the data located at an index
155  /*! @tparam T How to interpret the data */
156  template <class T>
157  T & get( size_t index );
158 
159  //! Gets a constant reference to the data at an index
160  /*! @tparam T How to interpret the data */
161  template <class T>
162  T const & get( size_t index ) const;
163 
164  //! Checks for data at a specific index
165  bool hasData( size_t const index ) const;
166 
167  //! Returns a pointer to the specified index
168  /*! @tparam T How to interpret the data */
169  template <class T>
170  T * ptr( size_t index );
171 
172  //! Returns a constant pointer to the specified index
173  /*! @tparam T How to interpret the data */
174  template <class T>
175  T const * ptr( size_t index ) const;
176 
177  //! Get an iterator to the beginning of the data
178  /*! Note that this is a map iterator and will not
179  provide safe access to the data - you will need
180  to correctly cast data accessed through this.
181 
182  Also note that because this iterator is not type
183  safe with what we really hold, only read-only
184  versions are available */
185  const_iterator begin() const;
186 
187  //! Get an iterator to the end of the data
188  /*! See notes for begin() */
189  const_iterator end() const;
190 
191  //! Inserts a new entry to the end of the vector
192  /*! @tparam T How to interpret the data */
193  template <class T>
194  void insert( size_t index, T const & data );
195 
196  //! Inserts one junk element to the specified index
197  /*! This will only be utilized immediately before an assignment in special circumstances */
198  void insert( size_t const index );
199 
200  //! Appends all data in another SparseData to this one
201  /*! @param other The other sparse data
202  @param size The size of the point cloud that we belong to */
203  void append( SparseData const & other, size_t const size );
204 
205  //! Removes the element at the specified index
206  void remove( size_t index );
207 
208  private:
209  //! The data
210  MapType itsData;
211 
212  //! Size, in bytes, of an entry in the data
213  size_t itsElemSize;
214 
215  //! The name of this data
216  std::string itsName;
217 
218  friend class cereal::access;
219  template <class Archive>
220  void serialize( Archive & ar )
221  {
222  ar( itsData,
223  itsElemSize,
224  itsName );
225  }
226  };
227 } // namespace nrt
228 
229 // Implementation
231 #endif // INCLUDE_NRT_POINTCLOUD2_DETAILS_SPARSEDATA_H