Point Cloud Library (PCL)  1.7.0
quantized_map.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_FEATURES_QUANTIZED_MAP
39 #define PCL_FEATURES_QUANTIZED_MAP
40 
41 #include <vector>
42 #include <pcl/pcl_macros.h>
43 
44 namespace pcl
45 {
46  class PCL_EXPORTS QuantizedMap
47  {
48  public:
49 
50  QuantizedMap ();
51  QuantizedMap (size_t width, size_t height);
52  QuantizedMap (const QuantizedMap & copy_me);
53 
54  virtual ~QuantizedMap ();
55 
56  inline size_t
57  getWidth () const { return (width_); }
58 
59  inline size_t
60  getHeight () const { return (height_); }
61 
62  inline unsigned char*
63  getData () { return (&data_[0]); }
64 
65  inline const unsigned char*
66  getData () const { return (&data_[0]); }
67 
68  inline QuantizedMap
69  getSubMap (size_t x,
70  size_t y,
71  size_t width,
72  size_t height)
73  {
74  QuantizedMap subMap(width, height);
75 
76  for (size_t row_index = 0; row_index < height; ++row_index)
77  {
78  for (size_t col_index = 0; col_index < width; ++col_index)
79  {
80  //const size_t index = (row_index+y)*width_ + (col_index+x);
81  //const unsigned char value = data_[index];
82  //subMap.data_[row_index*width + col_index] = value;//data_[(row_index+y)*width_ + (col_index+x)];
83  subMap (col_index, row_index) = (*this) (col_index + x, row_index + y);
84  }
85  }
86 
87  return subMap;
88  }
89 
90  void
91  resize (size_t width, size_t height);
92 
93  inline unsigned char &
94  operator() (const size_t x, const size_t y)
95  {
96  return (data_[y*width_+x]);
97  }
98 
99  inline const unsigned char &
100  operator() (const size_t x, const size_t y) const
101  {
102  return (data_[y*width_+x]);
103  }
104 
105  static void
106  spreadQuantizedMap (const QuantizedMap & input_map, QuantizedMap & output_map, size_t spreading_size);
107 
108  void
109  serialize (std::ostream & stream) const
110  {
111  const int width = static_cast<int> (width_);
112  const int height = static_cast<int> (height_);
113 
114  stream.write (reinterpret_cast<const char*> (&width), sizeof (width));
115  stream.write (reinterpret_cast<const char*> (&height), sizeof (height));
116 
117  const int num_of_elements = static_cast<int> (data_.size ());
118  stream.write (reinterpret_cast<const char*> (&num_of_elements), sizeof (num_of_elements));
119  for (int element_index = 0; element_index < num_of_elements; ++element_index)
120  {
121  stream.write (reinterpret_cast<const char*> (&(data_[element_index])), sizeof (data_[element_index]));
122  }
123  }
124 
125  void
126  deserialize (std::istream & stream)
127  {
128  int width;
129  int height;
130 
131  stream.read (reinterpret_cast<char*> (&width), sizeof (width));
132  stream.read (reinterpret_cast<char*> (&height), sizeof (height));
133 
134  width_ = static_cast<size_t> (width);
135  height_ = static_cast<size_t> (height);
136 
137  int num_of_elements;
138  stream.read (reinterpret_cast<char*> (&num_of_elements), sizeof (num_of_elements));
139  data_.resize (num_of_elements);
140  for (int element_index = 0; element_index < num_of_elements; ++element_index)
141  {
142  stream.read (reinterpret_cast<char*> (&(data_[element_index])), sizeof (data_[element_index]));
143  }
144  }
145 
146 
147  //private:
148  std::vector<unsigned char> data_;
149  size_t width_;
150  size_t height_;
151 
152  };
153 }
154 
155 #endif