Fawkes API  Fawkes Development Version
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 namespace firevision {
00031 #if 0 /* just to make Emacs auto-indent happy */
00032 }
00033 #endif
00034 
00035 /** @class HistogramFile <fvutils/statistical/histogram_file.h>
00036  * A fileformat for histograms. Such a file might contain multiple histograms, each for a
00037  * a different type of object.
00038  * @author Daniel Beck
00039  */
00040 
00041 /** Constructor. */
00042 HistogramFile::HistogramFile()
00043   : FireVisionDataFile(FIREVISION_HISTOGRAM_MAGIC, FIREVISION_HISTOGRAM_CURVER)
00044 {
00045   attached_histograms.clear();
00046 }
00047 
00048 
00049 /** Destructor. */
00050 HistogramFile::~HistogramFile()
00051 {
00052   attached_histograms.clear();
00053 }
00054 
00055 
00056 /** Adds a new histogram block to the file.
00057  * @param block the histogram block
00058  */
00059 void
00060 HistogramFile::add_histogram_block(HistogramBlock* block)
00061 {
00062   if ( attached_histograms.find( block->object_type() ) != attached_histograms.end() )
00063     { throw Exception("Cannot add another histogram of type %d to the file", block->object_type()); }
00064 
00065   attached_histograms[ block->object_type() ] = block;
00066   add_block(block);
00067 }
00068 
00069 
00070 /** Generates a list of histogram blocks attached to the file.
00071  * @return a list of all attached histogram blocks
00072  */
00073 HistogramFile::HistogramBlockList
00074 HistogramFile::histogram_blocks()
00075 {
00076   FireVisionDataFile::BlockList bl = blocks();
00077   FireVisionDataFile::BlockList::iterator blit;
00078 
00079   HistogramBlockList hbl;
00080   
00081   for (blit = bl.begin(); blit != bl.end(); ++blit)
00082     {
00083       if ((*blit)->type() == FIREVISION_HISTOGRAM_TYPE_16 ||
00084           (*blit)->type() == FIREVISION_HISTOGRAM_TYPE_32 )
00085         {
00086           HistogramBlock* hb = new HistogramBlock(*blit);
00087           hbl.push_back(hb);
00088         }
00089     }
00090 
00091   return hbl;
00092 }
00093 
00094 
00095 /** Get a value from a certain histogram.
00096  * @param object_type the requested value is obtained from the histogram for this type of
00097  *                    object
00098  * @param x the x-coordinate
00099  * @param y the y-coordinate
00100  * @param z the z-coordinate
00101  * @return value
00102  */
00103 uint32_t
00104 HistogramFile::get_value(hint_t object_type,
00105                          uint16_t x, uint16_t y, uint16_t z)
00106 {
00107   if ( attached_histograms.find(object_type) == attached_histograms.end() )
00108     { throw Exception("File contains no histogram for type %d", object_type); }
00109 
00110   return attached_histograms[object_type]->get_value(x, y, z);
00111 }
00112 
00113 
00114 /** Set a value in a certain histogram.
00115  * @param object_type this specifies the type for which the respective histogram is changed
00116  * @param x the x-coordinate
00117  * @param y the y-coordinate
00118  * @param z the z-coordinate
00119  * @param val the new value for the specified cell
00120  */
00121 void
00122 HistogramFile::set_value(hint_t object_type,
00123                          uint16_t x, uint16_t y, uint16_t z,
00124                          uint32_t val)
00125 {
00126   if ( attached_histograms.find(object_type) == attached_histograms.end() )
00127     { throw Exception("File contains no histogram for type %d", object_type); }
00128 
00129   attached_histograms[object_type]->set_value(x, y, z, val);
00130 }
00131 
00132 } // end namespace firevision