Fawkes API  Fawkes Development Version
histogram_file.cpp
1 
2 /***************************************************************************
3  * histogram_file.cpp - Histogram file
4  *
5  * Created: Sat Mar 29 21:37:33 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/exception.h>
25 #include <fvutils/statistical/histogram_block.h>
26 #include <fvutils/statistical/histogram_file.h>
27 
28 using namespace fawkes;
29 
30 namespace firevision {
31 
32 /** @class HistogramFile <fvutils/statistical/histogram_file.h>
33  * A fileformat for histograms. Such a file might contain multiple histograms, each for a
34  * a different type of object.
35  * @author Daniel Beck
36  */
37 
38 /** Constructor. */
39 HistogramFile::HistogramFile()
40 : FireVisionDataFile(FIREVISION_HISTOGRAM_MAGIC, FIREVISION_HISTOGRAM_CURVER)
41 {
42  attached_histograms.clear();
43 }
44 
45 /** Destructor. */
47 {
48  attached_histograms.clear();
49 }
50 
51 /** Adds a new histogram block to the file.
52  * @param block the histogram block
53  */
54 void
56 {
57  if (attached_histograms.find(block->object_type()) != attached_histograms.end()) {
58  throw Exception("Cannot add another histogram of type %d to the file", block->object_type());
59  }
60 
61  attached_histograms[block->object_type()] = block;
62  add_block(block);
63 }
64 
65 /** Generates a list of histogram blocks attached to the file.
66  * @return a list of all attached histogram blocks
67  */
70 {
72  FireVisionDataFile::BlockList::iterator blit;
73 
75 
76  for (blit = bl.begin(); blit != bl.end(); ++blit) {
77  if ((*blit)->type() == FIREVISION_HISTOGRAM_TYPE_16
78  || (*blit)->type() == FIREVISION_HISTOGRAM_TYPE_32) {
79  HistogramBlock *hb = new HistogramBlock(*blit);
80  hbl.push_back(hb);
81  }
82  }
83 
84  return hbl;
85 }
86 
87 /** Get a value from a certain histogram.
88  * @param object_type the requested value is obtained from the histogram for this type of
89  * object
90  * @param x the x-coordinate
91  * @param y the y-coordinate
92  * @param z the z-coordinate
93  * @return value
94  */
95 uint32_t
96 HistogramFile::get_value(hint_t object_type, uint16_t x, uint16_t y, uint16_t z)
97 {
98  if (attached_histograms.find(object_type) == attached_histograms.end()) {
99  throw Exception("File contains no histogram for type %d", object_type);
100  }
101 
102  return attached_histograms[object_type]->get_value(x, y, z);
103 }
104 
105 /** Set a value in a certain histogram.
106  * @param object_type this specifies the type for which the respective histogram is changed
107  * @param x the x-coordinate
108  * @param y the y-coordinate
109  * @param z the z-coordinate
110  * @param val the new value for the specified cell
111  */
112 void
113 HistogramFile::set_value(hint_t object_type, uint16_t x, uint16_t y, uint16_t z, uint32_t val)
114 {
115  if (attached_histograms.find(object_type) == attached_histograms.end()) {
116  throw Exception("File contains no histogram for type %d", object_type);
117  }
118 
119  attached_histograms[object_type]->set_value(x, y, z, val);
120 }
121 
122 } // end namespace firevision
firevision::HistogramFile::HistogramBlockList
std::list< HistogramBlock * > HistogramBlockList
Convenience typdef for a STL list of pointers to histogram blocks.
Definition: histogram_file.h:48
firevision::HistogramFile::set_value
void set_value(hint_t object_type, uint16_t x, uint16_t y, uint16_t z, uint32_t val)
Set a value in a certain histogram.
Definition: histogram_file.cpp:112
firevision::FireVisionDataFile
Definition: fvfile.h:39
firevision::HistogramBlock
Definition: histogram_block.h:53
firevision::HistogramFile::get_value
uint32_t get_value(hint_t object_type, uint16_t x, uint16_t y, uint16_t z)
Get a value from a certain histogram.
Definition: histogram_file.cpp:95
firevision::FireVisionDataFile::BlockList
std::list< FireVisionDataFileBlock * > BlockList
List of FireVision data file blocks.
Definition: fvfile.h:71
firevision::FireVisionDataFile::add_block
virtual void add_block(FireVisionDataFileBlock *block)
Add a block.
Definition: fvfile.cpp:224
firevision::HistogramBlock::object_type
hint_t object_type() const
Returns the type of the object the histogram is associated with.
Definition: histogram_block.cpp:108
fawkes
firevision::HistogramFile::histogram_blocks
HistogramBlockList histogram_blocks()
Generates a list of histogram blocks attached to the file.
Definition: histogram_file.cpp:68
firevision::HistogramFile::~HistogramFile
~HistogramFile()
Destructor.
Definition: histogram_file.cpp:45
firevision::FireVisionDataFile::blocks
BlockList & blocks()
Get blocks.
Definition: fvfile.cpp:233
firevision::HistogramFile::add_histogram_block
void add_histogram_block(HistogramBlock *block)
Adds a new histogram block to the file.
Definition: histogram_file.cpp:54
fawkes::Exception
Definition: exception.h:39