/* -*- mona-c++ -*-
* Copyright (c) 2004 Max-Planck-Institute for Human Cognitive and Brain Science
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libmona/mona.hh>
#include <libmona/3DVectorfield.hh>
#include <algorithm>
#include <memory>
using namespace std;
using namespace mona;
void error_report(vstream::Level level, const string& message)
{
cverb << level << message << endl;
}
bool compare_lossless(const C3DFVectorfield& orig, C3DVectorfieldPtr& loaded)
{
if (loaded.get_data_ptr()) {
const C3DFVectorfield& lvf = loaded.get_data();
if ( lvf.get_size() != orig.get_size()) {
cverr() << "fields differ in size loaded:"<<lvf.get_size() <<" vs. original: "<< orig.get_size() << endl;
return false;
}
bool result = inner_product(orig.begin(),
orig.end(),
lvf.begin(),
true,
logical_and<bool>(),
equal_to<C3DFVector>());
if (!result) {
C3DFVectorfield::const_iterator j = lvf.begin();
for (C3DFVectorfield::const_iterator i = orig.begin();
i != orig.end(); ++i, ++j) {
if (*i != *j) {
cverr() << *i << *j << endl;
}
}
}
return result;
}else {
cverr() << "Got the wrapper but not the contens ... very strange" << endl;
return false;
}
}
struct small_dist {
bool operator ()(const C3DFVector& a, const C3DFVector& b)
{
return (abs(a.x - b.x) < 0.13 &&
abs(a.y - b.y) < 0.13 &&
abs(a.z - b.z) < 0.13);
}
};
bool compare_lossy(const C3DFVectorfield& orig, C3DVectorfieldPtr& loaded)
{
if (loaded.get_data_ptr()) {
const C3DFVectorfield& lvf = loaded.get_data();
if ( lvf.get_size() != orig.get_size()) {
cverr() << "fields differ in size loaded:"<<lvf.get_size() <<" vs. original: "<< orig.get_size() << endl;
return false;
}
return inner_product(orig.begin(),
orig.end(),
lvf.begin(),
true,
logical_and<bool>(),
small_dist()
);
}else {
cverr() << "Got the wrapper but not the contens ... very strange" << endl;
return false;
}
}
bool check_save_load(const C3DVectorfieldPtr& vfptr, C3DVFieldIOPluginHandler& vfio, const string& format, bool lossless)
{
bool retval = false;
string tmp_name = string("/tmp/vftest_") + format;
error_report(vstream::ml_debug, tmp_name);
if (!vfio.save(format.c_str(), vfptr,tmp_name.c_str())) {
error_report(vstream::ml_error, format + string(" save failed"));
return false;
}
error_report(vstream::ml_debug, "saved");
auto_ptr<C3DVectorfieldPtr> loaded(vfio.load(tmp_name.c_str()));
if (loaded.get()) {
error_report(vstream::ml_debug, "loaded");
if (lossless)
retval = compare_lossless(vfptr.get_data(), *loaded);
else
retval = compare_lossy(vfptr.get_data(), *loaded);
}else{
cerr << "nothing loaded" << endl;
}
if (retval)
unlink(tmp_name.c_str());
return retval;
}
bool check_save_load_zipped(const C3DVectorfieldPtr& vfptr, C3DVFieldIOPluginHandler& vfio, const string& format, const string& suffix, bool lossless)
{
bool retval = false;
string tmp_name = string("/tmp/vftest_") + format + suffix;
cvdebug() << "Saving to " << tmp_name << "\n";
if (!vfio.save(format.c_str(), vfptr,tmp_name.c_str())) {
error_report(vstream::ml_error, format + string(" save failed"));
return false;
}
cvdebug() << "Loading from " << tmp_name << "\n";
auto_ptr<C3DVectorfieldPtr> loaded(vfio.load(tmp_name.c_str()));
if (loaded.get()) {
error_report(vstream::ml_debug, "loaded");
if (lossless)
retval = compare_lossless(vfptr.get_data(), *loaded);
else
retval = compare_lossy(vfptr.get_data(), *loaded);
}else{
cerr << "nothing loaded" << endl;
}
if (retval)
unlink(tmp_name.c_str());
return retval;
}
void vf_fill(C3DFVectorfield *vf)
{
C3DFVector v(0.0, 0.1, 0.2);
C3DFVector d(0.1, -0.1, 1.2);
C3DFVectorfield::iterator i = vf->begin();
C3DFVectorfield::iterator e = vf->end();
while (i != e) {
*i++ = v;
v += d;
}
}
int main (int argc, char **args)
{
cout << "Checking vectorfield io ... ";
list<std::string> path;
int result = 0;
cverb.set_verbosity(argc > 2 ? vstream::ml_debug : vstream::ml_error);
CPluginSearchPath::instance().clear();
CPluginSearchPath::instance().prepend("../plugins/io/.libs");
C3DVFieldIOPluginHandler vfio;
C3DVFieldIOPluginHandler::const_iterator plugin = vfio.begin();
auto_ptr<C3DVectorfieldPtr> field_ptr(new C3DVectorfieldPtr(new C3DFVectorfield(C3DBounds(5,4,6))));
vf_fill(field_ptr->get_data_ptr());
while (plugin != vfio.end()) {
cverb << vstream::ml_debug << plugin->first << endl;
bool lossless = !plugin->second->has_property(io_plugin_property_compress);
if (!check_save_load(*field_ptr, vfio, plugin->first, lossless)) {
cverb << vstream::ml_error << plugin->first << string(": FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": OK") << endl; ;
if (!check_save_load_zipped(*field_ptr, vfio, plugin->first, string(".gz"), lossless)) {
cverb << vstream::ml_error << plugin->first << string(": gz FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": gz OK") << endl; ;
if (!check_save_load_zipped(*field_ptr, vfio, plugin->first, string(".bz2"), lossless)) {
cverb << vstream::ml_error << plugin->first << string(": bz2 FAILED") << endl; ;
result++;
}else
cverb << vstream::ml_debug << plugin->first << string(": bz2 OK") << endl; ;
++plugin;
}
if (result == 0)
cout << " done." << endl;
return result;
}