/* -*- mia-c++ -*-
*
* Copyright (c) 2007 Gert Wollny <gert dot wollny at acm dot org>
*
* 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
*
*/
#include <mia/2d/transform.hh>
#include <mia/2d/deformer.hh>
#include <mia/2d/2dvfio.hh>
#include <boost/lambda/lambda.hpp>
NS_MIA_BEGIN
using namespace boost::lambda;
bool C2DTransformation::save(const std::string& filename, const std::string& type) const
{
return do_save(filename, type);
}
P2DTransformation C2DTransformation::upscale(const C2DBounds& size) const
{
return do_upscale(size);
}
void C2DTransformation::add(const C2DTransformation& a)
{
do_add(a);
}
P2DImage C2DAffineTransformation::apply(const C2DImage& input) const
{
assert(0);
return P2DImage();
}
C2DFVector C2DAffineTransformation::operator() (const C2DFVector& x) const
{
return C2DFVector(
_M_t[0] * x.x + _M_t[1] * x.y + _M_t[2],
_M_t[3] * x.x + _M_t[4] * x.y + _M_t[5]
);
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size):
_M_t(9,0.0f),
_M_ops(op_all),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size, std::vector<float> transform, C2DAffineTransformation::EOps ops):
_M_t(transform),
_M_ops(ops),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
C2DAffineTransformation::C2DAffineTransformation(const C2DBounds& size, C2DAffineTransformation::EOps ops):
_M_t(9,0.0f),
_M_ops(ops),
_M_size(size)
{
_M_t[0] = _M_t[4] = _M_t[8] = 1.0f;
}
bool C2DAffineTransformation::do_save(const std::string& filename, const std::string& type) const
{
return false;
}
void C2DAffineTransformation::scale(float x, float y)
{
_M_t[0] *= x; _M_t[1] *= x; _M_t[2] *= x;
_M_t[3] *= y; _M_t[4] *= y; _M_t[5] *= y;
}
const C2DBounds& C2DAffineTransformation::get_size() const
{
return _M_size;
}
P2DTransformation C2DAffineTransformation::do_upscale(const C2DBounds& size) const
{
float x_mult = float(size.x) / (float)get_size().x;
float y_mult = float(size.y) / (float)get_size().y;
C2DAffineTransformation *result = new C2DAffineTransformation(size, _M_t, _M_ops);
result->scale(x_mult, y_mult);
return P2DTransformation(result);
}
void C2DAffineTransformation::do_add(const C2DTransformation& other)
{
const C2DAffineTransformation& a = dynamic_cast<const C2DAffineTransformation&>(other);
// matrix multiply
vector<float> tmp(_M_t.begin(), _M_t.end());
_M_t[0] = tmp[0] * a._M_t[0] + tmp[1] * a._M_t[3] + tmp[2] * a._M_t[6];
_M_t[1] = tmp[0] * a._M_t[1] + tmp[1] * a._M_t[4] + tmp[2] * a._M_t[7];
_M_t[2] = tmp[0] * a._M_t[2] + tmp[1] * a._M_t[5] + tmp[2] * a._M_t[8];
_M_t[3] = tmp[3] * a._M_t[0] + tmp[4] * a._M_t[3] + tmp[5] * a._M_t[6];
_M_t[4] = tmp[3] * a._M_t[1] + tmp[4] * a._M_t[4] + tmp[5] * a._M_t[7];
_M_t[5] = tmp[3] * a._M_t[2] + tmp[4] * a._M_t[5] + tmp[5] * a._M_t[8];
_M_t[6] = tmp[6] * a._M_t[0] + tmp[7] * a._M_t[3] + tmp[8] * a._M_t[6];
_M_t[7] = tmp[6] * a._M_t[1] + tmp[7] * a._M_t[4] + tmp[8] * a._M_t[7];
_M_t[8] = tmp[6] * a._M_t[2] + tmp[7] * a._M_t[5] + tmp[8] * a._M_t[8];
}
/*
C2DGridTransformation& C2DGridTransformation::operator += (C2DGridTransformation& other)
{
TRACE("C2DGridTransformation::operator +=");
_M_field += other.field();
return *this;
}
C2DGridTransformation C2DGridTransformation::operator + (const C2DGridTransformation& other)
{
TRACE("C2DGridTransformation::operator +");
assert(_M_field.get_size() == other._M_field.get_size());
C2DGridTransformation tmp(_M_field.get_size(), _M_ipf);
C2DFVectorfield::iterator itmp = tmp._M_field.begin();
C2DFVectorfield::const_iterator ib = other._M_field.begin();
for (size_t y = 0; y < _M_field.get_size().y; ++y) {
for (size_t x = 0; x < _M_field.get_size().x; ++x, ++itmp, ++ib) {
C2DFVector xi = C2DFVector(x, y) - *ib;
*itmp = _M_field.get_interpol_val_at(xi) + *ib;
}
}
return tmp;
}
*/
C2DGridTransformation::C2DGridTransformation(const C2DBounds& size, P2DInterpolatorFactory ipf):
_M_field(size),
_M_ipf(ipf)
{
}
C2DFVectorfield& C2DGridTransformation::field()
{
return _M_field;
}
const C2DFVectorfield& C2DGridTransformation::field() const
{
return _M_field;
}
P2DInterpolatorFactory C2DGridTransformation::get_ipf()
{
return _M_ipf;
}
P2DImage C2DGridTransformation::apply(const C2DImage& input) const
{
TRACE("C2DGridTransformation::apply");
FDeformer2D deformer(_M_field, *_M_ipf);
return mia::filter(deformer, input);
}
P2DTransformation C2DGridTransformation::do_upscale(const C2DBounds& size) const
{
TRACE("C2DGridTransformation::upscale");
C2DGridTransformation *result = new C2DGridTransformation(size, _M_ipf);
// initial upscale
if (_M_field.get_size().x != 0 && _M_field.get_size().y != 0) {
float x_mult = float(size.x) / (float)_M_field.get_size().x;
float y_mult = float(size.y) / (float)_M_field.get_size().y;
float ix_mult = 1.0f / x_mult;
float iy_mult = 1.0f / y_mult;
C2DFVectorfield::iterator i = result->_M_field.begin();
for (unsigned int y = 0; y < size.y; y++){
for (unsigned int x = 0; x < size.x; x++,++i){
C2DFVector help(ix_mult * x, iy_mult * y);
C2DFVector val = _M_field.get_interpol_val_at(help);
*i = C2DFVector(val.x * x_mult,val.y * y_mult);
}
}
}
return P2DTransformation(result);
}
const C2DBounds& C2DGridTransformation::get_size() const
{
return _M_field.get_size();
}
bool C2DGridTransformation::do_save(const std::string& filename, const std::string& type) const
{
C2DIOVectorfield outfield(_M_field);
return C2DVFIOPluginHandler::instance().save(type, filename, outfield);
}
void C2DGridTransformation::do_add(const C2DTransformation& a)
{
const C2DGridTransformation& other = dynamic_cast<const C2DGridTransformation&>(a);
_M_field += other.field();
}
NS_MIA_END