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 <core/exceptions/software.h>
25 #include <fvutils/statistical/histogram_block.h>
26 
27 #include <cstring>
28 
29 using namespace fawkes;
30 
31 namespace firevision {
32 
33 /** @class HistogramBlock <fvutils/statistical/histogram_block.h>
34  * This class defines a file block for histograms. Additionally, the very basic routines
35  * to acccess and manipulate data in the histograms are provided.
36  * @author Daniel Beck
37  */
38 
39 /** Constructor.
40  * @param type the type of the histogram block
41  * @param object_type the object type this histogram is meant for (e.g, ball)
42  * @param width the width of the histogram
43  * @param height the height of the histogram
44  * @param depth the depth of the histogram
45  */
46 HistogramBlock::HistogramBlock(histogram_block_type_t type,
47  hint_t object_type,
48  uint16_t width,
49  uint16_t height,
50  uint16_t depth)
52  (size_t)width * height * depth * sizeof(uint32_t),
54 {
55  _block_header = (histogram_block_header_t *)_spec_header;
56  _block_header->width = width;
57  _block_header->height = height;
58  _block_header->depth = depth;
59  _block_header->object_type = object_type;
60 
61  _histogram_data = (uint32_t *)_data;
62 }
63 
64 /** Copy constructor.
65  * @param block another block
66  */
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 
145  if (x >= _block_header->width) {
146  throw OutOfBoundsException("Given x value is too large (set_value, 2)",
147  float(x),
148  0.0f,
149  float(_block_header->width));
150  }
151 
152  if (y >= _block_header->height) {
153  throw OutOfBoundsException("Given y value is too large (set_value, 2)",
154  float(y),
155  0.0f,
156  float(_block_header->height));
157  }
158 
159  _histogram_data[y * _block_header->width + x] = val;
160 }
161 
162 /** Store a value in a certain cell of a 3-dimensional histogram.
163  * @param x the x-coordinate
164  * @param y the y-coordinate
165  * @param z the z-coordinate
166  * @param val the new value
167  */
168 void
169 HistogramBlock::set_value(uint16_t x, uint16_t y, uint16_t z, uint32_t val)
170 {
171  if (x >= _block_header->width) {
172  throw OutOfBoundsException("Given x value is too large (set_value, 3)",
173  float(x),
174  0.0f,
175  float(_block_header->width));
176  }
177 
178  if (y >= _block_header->height) {
179  throw OutOfBoundsException("Given y value is too large (set_value, 3)",
180  float(y),
181  0.0f,
182  float(_block_header->height));
183  }
184 
185  if (z >= _block_header->depth) {
186  throw OutOfBoundsException("Given z value is too large (set_value, 3)",
187  float(z),
188  0.0f,
189  float(_block_header->depth));
190  }
191 
192  _histogram_data[z * _block_header->width * _block_header->height + y * _block_header->width + x] =
193  val;
194 }
195 
196 /** Obtain a certain value from a 2-dimensional histogram.
197  * @param x the x-coordinate
198  * @param y the y-coordinate
199  * @return the histogram value
200  */
201 uint32_t
202 HistogramBlock::get_value(uint16_t x, uint16_t y)
203 {
204  if (_block_header->depth != 0) {
205  throw Exception("Trying to acces a 3-dim histogram with a 2-dim access method");
206  }
207 
208  if (x >= _block_header->width) {
209  throw OutOfBoundsException("Given x value is too large (get_value, 2)",
210  float(x),
211  0.0f,
212  float(_block_header->width));
213  }
214 
215  if (y >= _block_header->height) {
216  throw OutOfBoundsException("Given y value is too large (get_value, 2)",
217  float(y),
218  0.0f,
219  float(_block_header->height));
220  }
221 
222  return _histogram_data[y * _block_header->width + x];
223 }
224 
225 /** Obtain a certain value from a 3-dimensional histogram.
226  * @param x the x-coordinate
227  * @param y the y-coordinate
228  * @param z the z-coordinate
229  * @return the histogram value
230  */
231 uint32_t
232 HistogramBlock::get_value(uint16_t x, uint16_t y, uint16_t z)
233 {
234  if (x >= _block_header->width) {
235  throw OutOfBoundsException("Given x value is too large (get_value, 3)",
236  float(x),
237  0.0f,
238  _block_header->width - 1);
239  }
240 
241  if (y >= _block_header->height) {
242  throw OutOfBoundsException("Given y value is too large (get_value, 3)",
243  float(y),
244  0.0f,
245  _block_header->height - 1);
246  }
247 
248  if (z >= _block_header->depth) {
249  throw OutOfBoundsException("Given z value is too large (get_value, 3)",
250  float(z),
251  0.0f,
252  _block_header->depth - 1);
253  }
254 
255  return _histogram_data[z * _block_header->width * _block_header->height + y * _block_header->width
256  + x];
257 }
258 
259 /** Reset the histogram. */
260 void
262 {
263  memset(_histogram_data, 0, _data_size);
264 }
265 
266 } // end namespace firevision
firevision::HistogramBlock::width
uint16_t width() const
Returns the the width of the histogram.
Definition: histogram_block.cpp:82
firevision::HistogramBlock::set_object_type
void set_object_type(hint_t object_type)
Set the type of the object the histogram is associated with.
Definition: histogram_block.cpp:118
firevision::_histogram_block_header_t::depth
uint16_t depth
the depth of the histogram
Definition: histogram_block.h:39
firevision::HistogramBlock::reset
void reset()
Reset the histogram.
Definition: histogram_block.cpp:261
firevision::_histogram_block_header_t
Header for a histogram block.
Definition: histogram_block.h:36
firevision::FireVisionDataFileBlock::_data
void * _data
Pointer to the internal data segment.
Definition: fvfile_block.h:54
firevision::HistogramBlock::HistogramBlock
HistogramBlock(histogram_block_type_t type, hint_t object_type, uint16_t width, uint16_t height, uint16_t depth=0)
Constructor.
Definition: histogram_block.cpp:46
fawkes::OutOfBoundsException
Index out of bounds.
Definition: software.h:86
firevision::HistogramBlock::set_value
void set_value(uint16_t x, uint16_t y, uint32_t val)
Store a value in a certain cell of a 2-dimensional histogram.
Definition: histogram_block.cpp:139
firevision::_histogram_block_header_t::width
uint16_t width
the width of the histogram
Definition: histogram_block.h:37
firevision::HistogramBlock::object_type
hint_t object_type() const
Returns the type of the object the histogram is associated with.
Definition: histogram_block.cpp:109
fawkes
Fawkes library namespace.
firevision::FireVisionDataFileBlock::_spec_header
void * _spec_header
Pointer to the content specific block header.
Definition: fvfile_block.h:56
firevision::HistogramBlock::~HistogramBlock
virtual ~HistogramBlock()
Destructor.
Definition: histogram_block.cpp:74
firevision::FireVisionDataFileBlock::_data_size
size_t _data_size
Size of _data in bytes.
Definition: fvfile_block.h:55
firevision::HistogramBlock::height
uint16_t height() const
Returns the the height of the histogram.
Definition: histogram_block.cpp:91
firevision::HistogramBlock::get_value
uint32_t get_value(uint16_t x, uint16_t y)
Obtain a certain value from a 2-dimensional histogram.
Definition: histogram_block.cpp:202
firevision::HistogramBlock::set_data
void set_data(uint32_t *data)
Directly set the histogram data.
Definition: histogram_block.cpp:128
firevision::FireVisionDataFileBlock
FireVision File Format data block.
Definition: fvfile_block.h:34
firevision::HistogramBlock::depth
uint16_t depth() const
Returns the the depth of the histogram.
Definition: histogram_block.cpp:100
firevision::_histogram_block_header_t::object_type
uint8_t object_type
the type of object the histogram is associated with
Definition: histogram_block.h:40
firevision::_histogram_block_header_t::height
uint16_t height
the height of the histogram
Definition: histogram_block.h:38
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36