iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DenseData.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_DENSEDATA_H
37 #define INCLUDE_NRT_POINTCLOUD2_DETAILS_DENSEDATA_H
38 
41 #include <string>
42 #include <vector>
43 
44 #include <nrt/External/cereal/types/vector.hpp>
45 #include <nrt/External/cereal/types/string.hpp>
46 
47 namespace nrt
48 {
49  namespace exception
50  {
51  //! Exception thrown for dense data out of bounds errors
53  {
54  public:
55  DenseDataBoundsException( size_t const & index, size_t const & size, std::string const & name ) throw();
56 
57  virtual ~DenseDataBoundsException() throw();
58 
59  size_t const & index() const;
60 
61  size_t const & size() const;
62 
63  std::string const & name() const;
64 
65  private:
66  size_t itsIndex;
67  size_t itsSize;
68  std::string itsName;
69  std::string whatstring;
70  };
71  }
72 
73  //! A class for representing dense (one entry per geometry) data
74  /*! Dense data is stored in a contiguous dynamic data structure
75  that can be considered opaque by the developer. When
76  creating new dense data, the size in bytes of the
77  data to be represented must be known, along with the name
78  associated with the data.
79 
80  Access to the data is only done through typed accessors */
81  class DenseData
82  {
83  public:
84  //! Creates an empty (invalid) DenseData
85  DenseData() = default;
86 
87  //! Create a new densedata
88  /*! @param[in] elemSize Size in bytes of an element
89  @param[in] numElems Number of elements to include
90  @param[in] name Name of the type associated with the data */
91  DenseData( size_t elemSize, size_t numElems, std::string const & name );
92 
93  //! Copy construction
94  DenseData( DenseData const & other ) = default;
95 
96  //! Move construction
97  DenseData( DenseData && other ) = default;
98 
99  //! Assignment
100  DenseData & operator=( DenseData const & other ) = default;
101 
102  //! Move assignment
103  DenseData & operator=( DenseData && other ) = default;
104 
105  //! Resizes the data
106  /*! New data created through resizing will be zero filled
107  and thus potentially invalid when cast to its true type */
108  void resize( size_t size );
109 
110  //! Clears all data
111  void clear();
112 
113  //! Returns the number of elements in the container
114  inline size_t const size() const;
115 
116  //! Returns the name of the type associated with this data
117  inline std::string const & name() const;
118 
119  //! Gets the data located at an index
120  /*! @tparam T How to interpret the data */
121  template <class T>
122  T & get( size_t index );
123 
124  //! Constant version of get
125  /*! @tparam T How to interpret the data */
126  template <class T>
127  T const & get( size_t index ) const;
128 
129  //! Returns a pointer to the specified index
130  /*! @tparam T How to interpret the data */
131  template <class T>
132  T * const ptr( size_t index );
133 
134  //! Returns a constant pointer to the specified index
135  /*! @tparam T How to interpret the data */
136  template <class T>
137  T const * const ptr( size_t index ) const;
138 
139  //! Returns a pointer to the beginning of the data
140  /*! @tparam T How to interpret the data */
141  template <class T>
142  T * begin();
143 
144  //! Returns a pointer to the beginning of the data
145  /*! @tparam T How to interpret the data */
146  template <class T>
147  T const * begin() const;
148 
149  //! Returns a pointer to the end of the data (one past the last element)
150  /*! @tparam T How to interpret the data */
151  template <class T>
152  T * end();
153 
154  //! Returns a pointer to the end of the data (one past the last element)
155  /*! @tparam T How to interpret the data */
156  template <class T>
157  T const * end() const;
158 
159  //! Inserts one junk element to the end of the vector
160  /*! This is used when we do an insertion into a point cloud with
161  a partially specified set of dense fields and need to add data
162  to the other dense fields that were not specified. Since we
163  don't know the type and it wouldn't necessarily be valid for us
164  to store what a default value would look like, we'll just insert
165  the appropriate amount of zero valued data into our array and
166  leave it up to the user to figure out how to use it.
167 
168  @return The external index of the newly inserted item */
169  size_t insert();
170 
171  //! Inserts a new entry to the end of the vector
172  /*! @return The external index of the newly inserted item */
173  /*! @tparam T How to interpret the data */
174  template <class T>
175  size_t insert( T const & data = T() );
176 
177  //! Appends all data in another DenseData into this one
178  void append( DenseData const & other );
179 
180  //! Removes the element at the specified index
181  /*! This process causes all data elements to be moved, taking O(n) time, but may
182  be faster in actuallity due to caching and other reasons */
183  void remove( size_t index );
184 
185  //! Removes the elemnt at the specified index
186  /*! This process takes O(1) time but does not guarantee local ordering */
187  void quickRemove( size_t index );
188 
189  protected:
190  //! Converts an external index to an internal index
191  inline size_t const convertIndex( size_t const index ) const;
192 
193  private:
194  //! The data
195  std::vector<unsigned char, nrt::AlignedAllocator<unsigned char, 16>> itsData;
196  //std::vector<unsigned char> itsData;
197 
198  //! Number of items currently in the data
199  size_t itsNumElems;
200 
201  //! Size, in bytes, of an entry in the data
202  size_t itsElemSize;
203 
204  //! The name of this data
205  std::string itsName;
206 
207  friend class cereal::access;
208  template <class Archive>
209  void serialize( Archive & ar )
210  {
211  ar( itsData,
212  itsNumElems,
213  itsElemSize,
214  itsName );
215  }
216  };
217 }
218 
219 // Implementation details
221 
222 #endif // INCLUDE_NRT_POINTCLOUD2_DETAILS_DENSEDATA_H