/* * mp_AUResults.cpp * * Author: Andrew Salamon * Date: Wed Jan 17, 2007 * * Copyright (c) 2007 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include "mp_AUResults.h" #include "mp_AUResults_ser.h" //#include //#include //#include //#include #include #include #include #include mp_AUResults::mp_AUResults() : name("") { initVectorSizes( 0 ); } mp_AUResults::mp_AUResults( const std::string &_name, unsigned int auCount ) : name(_name) { initVectorSizes( auCount ); } mp_AUResults::mp_AUResults( const std::string &_name, std::vector &_auResults, std::vector< std::vector > &_frequencies ) : name(_name), auResults(_auResults), frequencies(_frequencies) { // Assert( auResults.size() == frequencies.size() ); } mp_AUResults::mp_AUResults( const mp_AUResults &other ) { name = other.name; auResults = other.auResults; frequencies = other.frequencies; } mp_AUResults::~mp_AUResults() { } void mp_AUResults::addFrequency( unsigned int ni, unsigned int mu, std::vector &step ) { if ( ! frequencies.size() ) // NRL { // NRL initVectorSizes( step.size() ); // NRL } // NRL transform( auResults.begin(), auResults.end(), step.begin(), auResults.begin(), std::plus() ); // Now copy the values into the correct place in the frequencies table unsigned int freqIndex = indexForNIMU( ni, mu ); for( unsigned int auIndex = 0; auIndex < frequencies.size(); ++ auIndex ) { frequencies[auIndex][freqIndex] = step[auIndex]; } } void mp_AUResults::clear() { unsigned int count = frequencies.size(); auResults.clear(); frequencies.clear(); initVectorSizes( count ); } std::vector mp_AUResults::getAUResults() const { return auResults; } std::vector mp_AUResults::frequenciesForAUIndex( unsigned int index ) const { return frequencies[index]; } unsigned int mp_AUResults::indexForNIMU( unsigned int ni, unsigned int mu ) { const unsigned int maxMU = 8; return (ni-1)*maxMU + (mu-1); } void mp_AUResults::initVectorSizes( unsigned int auCount ) { auResults.resize( auCount ); frequencies.resize( auCount ); for( unsigned int index = 0; index < auCount; ++index ) { frequencies[index].resize(72); } } mp_AUResults & mp_AUResults::operator=( const mp_AUResults &other ) { name = other.name; auResults = other.auResults; frequencies = other.frequencies; return *this; } mp_AUResults mp_AUResults::operator+( const mp_AUResults &other ) { mp_AUResults temp( *this ); temp += other; return temp; } mp_AUResults & mp_AUResults::operator+=( const mp_AUResults &other ) { if ( ! frequencies.size() ) // NRL { // NRL initVectorSizes( other.auResults.size() ); // NRL } // NRL transform( auResults.begin(), auResults.end(), other.auResults.begin(), auResults.begin(), std::plus() ); for( unsigned int auIndex = 0; auIndex < frequencies.size(); ++auIndex ) { transform( frequencies[auIndex].begin(), frequencies[auIndex].end(), other.frequencies[auIndex].begin(), frequencies[auIndex].begin(), std::plus() ); } return *this; } std::string mp_AUResults::extensionForFiletype( filetype ftype ) { switch( ftype ) { case text: return "aus"; break; case binary: return "ausbin"; break; case xml: return "ausxml"; break; default: return ""; break; } return ""; } mp_AUResults::filetype mp_AUResults::filetypeFromName( const std::string &filename ) { // std::string extension; // filetype ftype; // std::string::size_type loc; // int lastType = end; for( int ftype = begin; ftype < end; ++ftype ) { std::string extension = extensionForFiletype( static_cast(ftype) ); std::string::size_type loc = filename.find( extension ); if( loc == (filename.size() - extension.size()) ) return static_cast(ftype); } // ftype = xml; // extension = extensionForFiletype( ftype ); // loc = filename.find( extension ); // if( loc == (filename.size() - extension.size()) ) // return ftype; // // ftype = binary; // extension = extensionForFiletype( ftype ); // loc = filename.find( extension ); // if( loc == (filename.size() - extension.size()) ) // return ftype; return unknown; } void mp_AUResults::save( std::ostream &os, filetype type ) { if( xml == type ) { boost::archive::xml_oarchive oa(os); oa << boost::serialization::make_nvp( "AUResults", *this ); } // else if( text == type ) // { // boost::archive::text_oarchive oa(os); // oa << boost::serialization::make_nvp( "AUResults", *this ); // } // else if( binary == type ) // { // boost::archive::binary_oarchive oa(os); // oa << boost::serialization::make_nvp( "AUResults", *this ); // } } void mp_AUResults::restore( std::istream &is, filetype type ) { if( xml == type ) { boost::archive::xml_iarchive ia(is); ia >> boost::serialization::make_nvp( "AUResults", *this ); } // else if( text == type ) // { // boost::archive::text_iarchive ia(is); // ia >> boost::serialization::make_nvp( "AUResults", *this ); // } // else if( binary == type ) // { // boost::archive::binary_iarchive ia(is); // ia >> boost::serialization::make_nvp( "AUResults", *this ); // } } std::ostream &operator<<( std::ostream &os, const mp_AUResults &res ) { std::cout << res.name << ": " << std::endl; for( unsigned int freq = 0; freq < 72; ++freq ) { for( unsigned int auIndex = 0; auIndex < res.frequencies.size(); ++auIndex ) { std::cout << res.frequencies[auIndex][freq] << "\t"; } std::cout << std::endl; } return os; } bool operator==( const mp_AUResults& lhs, const mp_AUResults& rhs ) { if( lhs.name != rhs.name ) return false; if( lhs.auResults != rhs.auResults ) return false; if( lhs.frequencies != rhs.frequencies ) return false; return true; }