histogram_file.cpp

00001
00002 /***************************************************************************
00003  *  histogram_file.cpp - Histogram file
00004  *
00005  *  Created: Sat Mar 29 21:37:33 2008
00006  *  Copyright  2008  Daniel Beck
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <fvutils/statistical/histogram_file.h>
00025 #include <fvutils/statistical/histogram_block.h>
00026 #include <core/exception.h>
00027
00028 using namespace fawkes;
00029
00030 
00031 /** @class HistogramFile fvutils/statistical/histogram_file.h
00032  * A fileformat for histograms. Such a file might contain multiple histograms, each for a
00033  * a different type of object.
00034  * @author Daniel Beck
00035  */
00036 
00037 /** Constructor. */
00038 HistogramFile::HistogramFile()
00039   : FireVisionDataFile(FIREVISION_HISTOGRAM_MAGIC, FIREVISION_HISTOGRAM_CURVER)
00040 {
00041   attached_histograms.clear();
00042 }
00043
00044 
00045 /** Destructor. */
00046 HistogramFile::~HistogramFile()
00047 {
00048   attached_histograms.clear();
00049 }
00050
00051 
00052 /** Adds a new histogram block to the file.
00053  * @param block the histogram block
00054  */
00055 void
00056 HistogramFile::add_histogram_block(HistogramBlock* block)
00057 {
00058   if ( attached_histograms.find( block->object_type() ) != attached_histograms.end() )
00059     { throw Exception("Cannot add another histogram of type %d to the file", block->object_type()); }
00060
00061   attached_histograms[ block->object_type() ] = block;
00062   add_block(block);
00063 }
00064
00065 
00066 /** Generates a list of histogram blocks attached to the file.
00067  * @return a list of all attached histogram blocks
00068  */
00069 HistogramFile::HistogramBlockList
00070 HistogramFile::histogram_blocks()
00071 {
00072   FireVisionDataFile::BlockList bl = blocks();
00073   FireVisionDataFile::BlockList::iterator blit;
00074
00075   HistogramBlockList hbl;
00076
00077   for (blit = bl.begin(); blit != bl.end(); ++blit)
00078     {
00079       if ((*blit)->type() == FIREVISION_HISTOGRAM_TYPE_16 ||
00080           (*blit)->type() == FIREVISION_HISTOGRAM_TYPE_32 )
00081         {
00082           HistogramBlock* hb = new HistogramBlock(*blit);
00083           hbl.push_back(hb);
00084         }
00085     }
00086
00087   return hbl;
00088 }
00089
00090 
00091 /** Get a value from a certain histogram.
00092  * @param object_type the requested value is obtained from the histogram for this type of
00093  *                    object
00094  * @param x the x-coordinate
00095  * @param y the y-coordinate
00096  * @param z the z-coordinate
00097  */
00098 uint32_t
00099 HistogramFile::get_value(hint_t object_type,
00100                          uint16_t x, uint16_t y, uint16_t z)
00101 {
00102   if ( attached_histograms.find(object_type) == attached_histograms.end() )
00103     { throw Exception("File contains no histogram for type %d", object_type); }
00104
00105   return attached_histograms[object_type]->get_value(x, y, z);
00106 }
00107
00108 
00109 /** Set a value in a certain histogram.
00110  * @param object_type this specifies the type for which the respective histogram is changed
00111  * @param x the x-coordinate
00112  * @param y the y-coordinate
00113  * @param z the z-coordinate
00114  * @param val the new value for the specified cell
00115  */
00116 void
00117 HistogramFile::set_value(hint_t object_type,
00118                          uint16_t x, uint16_t y, uint16_t z,
00119                          uint32_t val)
00120 {
00121   if ( attached_histograms.find(object_type) == attached_histograms.end() )
00122     { throw Exception("File contains no histogram for type %d", object_type); }
00123
00124   attached_histograms[object_type]->set_value(x, y, z, val);
00125 }