Fawkes API  Fawkes Development Version
histogram_block.cpp
1 
2 /***************************************************************************
3  * histogram_block.cpp - Histogram block
4  *
5  * Created: Sat Mar 29 21:01:35 2008
6  * Copyright 2008 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvutils/statistical/histogram_block.h>
25 #include <core/exceptions/software.h>
26 #include <cstring>
27 
28 using namespace fawkes;
29 
30 namespace firevision {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class HistogramBlock <fvutils/statistical/histogram_block.h>
36  * This class defines a file block for histograms. Additionally, the very basic routines
37  * to acccess and manipulate data in the histograms are provided.
38  * @author Daniel Beck
39  */
40 
41 
42 /** Constructor.
43  * @param type the type of the histogram block
44  * @param object_type the object type this histogram is meant for (e.g, ball)
45  * @param width the width of the histogram
46  * @param height the height of the histogram
47  * @param depth the depth of the histogram
48  */
49 HistogramBlock::HistogramBlock(histogram_block_type_t type, hint_t object_type,
50  uint16_t width, uint16_t height, uint16_t depth)
51  : FireVisionDataFileBlock(type, width * height * depth * sizeof(uint32_t),
53 {
54  _block_header = (histogram_block_header_t*) _spec_header;
55  _block_header->width = width;
56  _block_header->height = height;
57  _block_header->depth = depth;
58  _block_header->object_type = object_type;
59 
60  _histogram_data = (uint32_t*) _data;
61 }
62 
63 /** Copy constructor.
64  * @param block another block
65  */
68 {
69  _block_header = (histogram_block_header_t*) _spec_header;
70  _histogram_data = (uint32_t*) _data;
71 }
72 
73 /** Destructor. */
75 {
76 }
77 
78 /** Returns the the width of the histogram.
79  * @return the width of the histogram
80  */
81 uint16_t
83 {
84  return _block_header->width;
85 }
86 
87 /** Returns the the height of the histogram.
88  * @return the height of the histogram
89  */
90 uint16_t
92 {
93  return _block_header->height;
94 }
95 
96 /** Returns the the depth of the histogram.
97  * @return the depth of the histogram
98  */
99 uint16_t
101 {
102  return _block_header->depth;
103 }
104 
105 /** Returns the type of the object the histogram is associated with.
106  * @return the object type of the histogram
107  */
108 hint_t
110 {
111  return (hint_t) _block_header->object_type;
112 }
113 
114 /** Set the type of the object the histogram is associated with.
115  * @param object_type the new type of the object
116  */
117 void
119 {
120  _block_header->object_type = object_type;
121 }
122 
123 /** Directly set the histogram data.
124  * Note: You are reponsible that the data has the right size and format!
125  * @param data pointer to the histogram data
126  */
127 void
129 {
130  memcpy(_data, data, _data_size);
131 }
132 
133 /** Store a value in a certain cell of a 2-dimensional histogram.
134  * @param x the x-coordinate
135  * @param y the y-coordinate
136  * @param val the new value
137  */
138 void
139 HistogramBlock::set_value(uint16_t x, uint16_t y, uint32_t val)
140 {
141  if (_block_header->depth != 0)
142  { throw Exception("Trying to acces a 3-dim histogram with a 2-dim access method"); }
143 
144  if (x >= _block_header->width)
145  {
146  throw OutOfBoundsException("Given x value is too large (set_value, 2)",
147  float(x), 0.0f, float(_block_header->width));
148  }
149 
150  if (y >= _block_header->height)
151  {
152  throw OutOfBoundsException("Given y value is too large (set_value, 2)",
153  float(y), 0.0f, float(_block_header->height));
154  }
155 
156  _histogram_data[y * _block_header->width + x] = val;
157 }
158 
159 /** Store a value in a certain cell of a 3-dimensional histogram.
160  * @param x the x-coordinate
161  * @param y the y-coordinate
162  * @param z the z-coordinate
163  * @param val the new value
164  */
165 void
166 HistogramBlock::set_value(uint16_t x, uint16_t y, uint16_t z, uint32_t val)
167 {
168  if ( x >= _block_header->width)
169  {
170  throw OutOfBoundsException("Given x value is too large (set_value, 3)",
171  float(x), 0.0f, float(_block_header->width));
172  }
173 
174  if ( y >= _block_header->height)
175  {
176  throw OutOfBoundsException("Given y value is too large (set_value, 3)",
177  float(y), 0.0f, float(_block_header->height));
178  }
179 
180  if ( z >= _block_header->depth)
181  {
182  throw OutOfBoundsException("Given z value is too large (set_value, 3)",
183  float(z), 0.0f, float(_block_header->depth));
184  }
185 
186  _histogram_data[z * _block_header->width * _block_header->height + y * _block_header->width + x] = val;
187 }
188 
189 /** Obtain a certain value from a 2-dimensional histogram.
190  * @param x the x-coordinate
191  * @param y the y-coordinate
192  * @return the histogram value
193  */
194 uint32_t
195 HistogramBlock::get_value(uint16_t x, uint16_t y)
196 {
197  if (_block_header->depth != 0)
198  { throw Exception("Trying to acces a 3-dim histogram with a 2-dim access method"); }
199 
200  if ( x >= _block_header->width)
201  {
202  throw OutOfBoundsException("Given x value is too large (get_value, 2)",
203  float(x), 0.0f, float(_block_header->width));
204  }
205 
206  if ( y >= _block_header->height)
207  {
208  throw OutOfBoundsException("Given y value is too large (get_value, 2)",
209  float(y), 0.0f, float(_block_header->height));
210  }
211 
212  return _histogram_data[y * _block_header->width + x];
213 }
214 
215 /** Obtain a certain value from a 3-dimensional histogram.
216  * @param x the x-coordinate
217  * @param y the y-coordinate
218  * @param z the z-coordinate
219  * @return the histogram value
220  */
221 uint32_t
222 HistogramBlock::get_value(uint16_t x, uint16_t y, uint16_t z)
223 {
224  if ( x >= _block_header->width)
225  {
226  throw OutOfBoundsException("Given x value is too large (get_value, 3)",
227  float(x), 0.0f, _block_header->width - 1);
228  }
229 
230  if ( y >= _block_header->height)
231  {
232  throw OutOfBoundsException("Given y value is too large (get_value, 3)",
233  float(y), 0.0f, _block_header->height - 1);
234  }
235 
236  if ( z >= _block_header->depth)
237  {
238  throw OutOfBoundsException("Given z value is too large (get_value, 3)",
239  float(z), 0.0f, _block_header->depth - 1);
240  }
241 
242  return _histogram_data[z * _block_header->width * _block_header->height + y * _block_header->width + x];
243 }
244 
245 /** Reset the histogram. */
246 void
248 {
249  memset(_histogram_data, 0, _data_size);
250 }
251 
252 } // end namespace firevision
void set_object_type(hint_t object_type)
Set the type of the object the histogram is associated with.
uint8_t object_type
the type of object the histogram is associated with
uint16_t depth() const
Returns the the depth of the histogram.
Fawkes library namespace.
void * _data
Pointer to the internal data segment.
Definition: fvfile_block.h:55
void set_value(uint16_t x, uint16_t y, uint32_t val)
Store a value in a certain cell of a 2-dimensional histogram.
hint_t object_type() const
Returns the type of the object the histogram is associated with.
uint16_t width() const
Returns the the width of the histogram.
FireVision File Format data block.
Definition: fvfile_block.h:35
uint16_t width
the width of the histogram
virtual ~HistogramBlock()
Destructor.
Header for a histogram block.
void * _spec_header
Pointer to the content specific block header.
Definition: fvfile_block.h:57
Base class for exceptions in Fawkes.
Definition: exception.h:36
void set_data(uint32_t *data)
Directly set the histogram data.
void reset()
Reset the histogram.
uint16_t depth
the depth of the histogram
HistogramBlock(histogram_block_type_t type, hint_t object_type, uint16_t width, uint16_t height, uint16_t depth=0)
Constructor.
uint16_t height() const
Returns the the height of the histogram.
Index out of bounds.
Definition: software.h:88
uint32_t get_value(uint16_t x, uint16_t y)
Obtain a certain value from a 2-dimensional histogram.
size_t _data_size
Size of _data in bytes.
Definition: fvfile_block.h:56
uint16_t height
the height of the histogram