Skip to content
Snippets Groups Projects
Commit 36b8e450 authored by Gerardo Tauriello's avatar Gerardo Tauriello
Browse files

Simplify template-handling in image_state

parent d8c2643c
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ PARENT_SCOPE
)
set(OST_IMG_IMAGE_STATE_HEADERS
binop.hh
dispatch.hh
image_state.hh
image_state_fw.hh
......
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2011 by the OpenStructure authors
// Copyright (C) 2003-2010 by the IPLT authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
/*
Author: Ansgar Philippsen
*/
#ifndef IMG_IMAGE_STATE_INST_H
#error "binop.cc must be included from image_state_inst.hh"
#endif
#include "binop.hh"
#include <ost/img/extent_iterator.hh>
namespace ost { namespace img { namespace image_state { namespace binop {
template<typename T1, class D1, typename T2, class D2>
void fnc_add_ip<T1,D1,T2,D2>::operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)+=Val2Val<T2,T1>(rhs->Value(it));
}
}
template<typename T1, class D1, typename T2, class D2>
void fnc_sub_ip<T1,D1,T2,D2>::operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)-=Val2Val<T2,T1>(rhs->Value(it));
}
}
template<typename T1, class D1, typename T2, class D2>
void fnc_mul_ip<T1,D1,T2,D2>::operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)*=Val2Val<T2,T1>(rhs->Value(it));
}
}
template<typename T1, class D1, typename T2, class D2>
void fnc_div_ip<T1,D1,T2,D2>::operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)/=Val2Val<T2,T1>(rhs->Value(it));
}
}
// partial specialization for half_frequency rhs
template<typename T1, class D1>
struct fnc_paste_ip<T1,D1,Complex,HalfFrequencyDomain> {
void operator()(ImageStateImpl<T1,D1>* lhs,
const ImageStateImpl<Complex,HalfFrequencyDomain>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetLogicalExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)=Val2Val<Complex,T1>(rhs->GetComplex(it));
}
}
};
// default for rest
template<typename T1, class D1, typename T2, class D2>
void fnc_paste_ip<T1,D1,T2,D2>::operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)=Val2Val<T2,T1>(rhs->Value(it));
}
}
// required explicit template instantiation for optimization to work
template struct fnc_paste_ip<Real,SpatialDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Complex,SpatialDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Word,SpatialDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Real,FrequencyDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Complex,FrequencyDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Complex,HalfFrequencyDomain,Complex,HalfFrequencyDomain>;
template struct fnc_paste_ip<Real,FrequencyDomain,Complex,SpatialDomain>;
template struct fnc_paste_ip<Real,FrequencyDomain,unsigned short,SpatialDomain>;
template struct fnc_paste_ip<Complex,FrequencyDomain,unsigned short,SpatialDomain>;
template struct fnc_paste_ip<Complex,HalfFrequencyDomain,unsigned short,SpatialDomain>;
template struct fnc_paste_ip<Complex,HalfFrequencyDomain,Real,SpatialDomain>;
template struct fnc_paste_ip<Complex,HalfFrequencyDomain,Complex,SpatialDomain>;
template struct fnc_paste_ip<Complex,FrequencyDomain,Complex,SpatialDomain>;
template struct fnc_paste_ip<Complex,FrequencyDomain,Real,SpatialDomain>;
template struct fnc_paste_ip<Real,FrequencyDomain,Real,SpatialDomain>;
template struct fnc_add_ip<Real,SpatialDomain,Real,SpatialDomain>;
template struct fnc_sub_ip<Real,SpatialDomain,Real,SpatialDomain>;
template struct fnc_mul_ip<Real,SpatialDomain,Real,SpatialDomain>;
template struct fnc_div_ip<Real,SpatialDomain,Real,SpatialDomain>;
}
template struct dispatch::binary_dispatch_ip<binop::fnc_add_ip>;
template struct dispatch::binary_dispatch_ip<binop::fnc_sub_ip>;
template struct dispatch::binary_dispatch_ip<binop::fnc_mul_ip>;
template struct dispatch::binary_dispatch_ip<binop::fnc_div_ip>;
template struct dispatch::binary_dispatch_ip<binop::fnc_paste_ip>;
}}} // ns
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2011 by the OpenStructure authors
// Copyright (C) 2003-2010 by the IPLT authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
/*
Author: Ansgar Philippsen
*/
#ifndef IMG_IMAGE_STATE_BINOP_HH
#define IMG_IMAGE_STATE_BINOP_HH
#include "dispatch.hh"
namespace ost { namespace img { namespace image_state { namespace binop {
// add
template<typename T1, class D1, typename T2, class D2>
struct fnc_add_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs);
};
typedef dispatch::binary_dispatch_ip<fnc_add_ip> add_ip;
// sub
template<typename T1, class D1, typename T2, class D2>
struct fnc_sub_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs);
};
typedef dispatch::binary_dispatch_ip<fnc_sub_ip> sub_ip;
// mul
template<typename T1, class D1, typename T2, class D2>
struct fnc_mul_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs);
};
typedef dispatch::binary_dispatch_ip<fnc_mul_ip> mul_ip;
// div
template<typename T1, class D1, typename T2, class D2>
struct fnc_div_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs);
};
typedef dispatch::binary_dispatch_ip<fnc_div_ip> div_ip;
// paste
template<typename T1, class D1, typename T2, class D2>
struct fnc_paste_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs);
};
typedef dispatch::binary_dispatch_ip<fnc_paste_ip> paste_ip;
}}}} // ns
#endif
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2011 by the OpenStructure authors
// Copyright (C) 2003-2010 by the IPLT authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
#ifndef IMG_IMAGE_STATE_DISPATCH_FW_HH
#define IMG_IMAGE_STATE_DISPATCH_FW_HH
namespace ost { namespace img { namespace image_state { namespace dispatch {
template <template <typename T, class D> class FNC>
struct unary_dispatch_ip;
template <template <typename T, class D> class FNC>
struct unary_dispatch_op;
template <template <typename T1, class D1, typename T2, class D2> class FNC>
struct binary_dispatch_ip;
template <template <typename T1, class D1, typename T2, class D2> class FNC>
struct binary_dispatch_op;
}}}} // ns
#endif
......@@ -23,34 +23,84 @@
*/
#include "image_state_base.hh"
#include "binop.hh"
#include "dispatch.hh"
namespace ost { namespace img { namespace image_state {
// define functors to be dispatched
namespace {
template<typename T1, class D1, typename T2, class D2>
struct fnc_add_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)+=Val2Val<T2,T1>(rhs->Value(it));
}
}
};
// sub
template<typename T1, class D1, typename T2, class D2>
struct fnc_sub_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)-=Val2Val<T2,T1>(rhs->Value(it));
}
}
};
// mul
template<typename T1, class D1, typename T2, class D2>
struct fnc_mul_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)*=Val2Val<T2,T1>(rhs->Value(it));
}
}
};
// div
template<typename T1, class D1, typename T2, class D2>
struct fnc_div_ip {
void operator()(ImageStateImpl<T1,D1>* lhs, const ImageStateImpl<T2,D2>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)/=Val2Val<T2,T1>(rhs->Value(it));
}
}
};
} // anon ns
ImageStateBase& ImageStateBase::operator+=(const ImageStateBase& b)
{
static image_state::binop::add_ip f_add_ip;
static dispatch::binary_dispatch_ip<fnc_add_ip> f_add_ip;
f_add_ip(this,&b);
return *this;
}
ImageStateBase& ImageStateBase::operator-=(const ImageStateBase& b)
{
static image_state::binop::sub_ip f_sub_ip;
static dispatch::binary_dispatch_ip<fnc_sub_ip> f_sub_ip;
f_sub_ip(this,&b);
return *this;
}
ImageStateBase& ImageStateBase::operator*=(const ImageStateBase& b)
{
static image_state::binop::mul_ip f_mul_ip;
static dispatch::binary_dispatch_ip<fnc_mul_ip> f_mul_ip;
f_mul_ip(this,&b);
return *this;
}
ImageStateBase& ImageStateBase::operator/=(const ImageStateBase& b)
{
static image_state::binop::div_ip f_div_ip;
static dispatch::binary_dispatch_ip<fnc_div_ip> f_div_ip;
f_div_ip(this,&b);
return *this;
}
......
......@@ -34,7 +34,6 @@
#include "image_state_impl.cc"
#include "image_state_algorithm.cc"
#include "value_holder.cc"
#include "binop.cc"
#include "image_state_impl.hh"
#include "image_state_spatial_domain.hh"
......
......@@ -24,11 +24,40 @@
#include "paste_impl.hh"
#include "image_state/binop.hh"
#include "image_state/dispatch.hh"
#include "image.hh"
namespace ost { namespace img { namespace detail {
// define functors to be dispatched
namespace {
// paste
template<typename T1, class D1, typename T2, class D2>
struct fnc_paste_ip {
void operator()(image_state::ImageStateImpl<T1,D1>* lhs,
const image_state::ImageStateImpl<T2,D2>* rhs) {
Extent ov=Overlap(lhs->GetExtent(),rhs->GetExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)=Val2Val<T2,T1>(rhs->Value(it));
}
}
};
// partial specialization for half_frequency rhs
template<typename T1, class D1>
struct fnc_paste_ip<T1,D1,Complex,image_state::HalfFrequencyDomain> {
void operator()(image_state::ImageStateImpl<T1,D1>* lhs,
const image_state::ImageStateImpl<Complex,
image_state::HalfFrequencyDomain>* rhs)
{
Extent ov=Overlap(lhs->GetExtent(),rhs->GetLogicalExtent());
for(ExtentIterator it(ov);!it.AtEnd();++it) {
lhs->Value(it)=Val2Val<Complex,T1>(rhs->GetComplex(it));
}
}
};
} // anon ns
PasteFnc::PasteFnc():
target_()
{}
......@@ -40,7 +69,7 @@ PasteFnc::PasteFnc(const ImageHandle& h):
template<typename V, class D>
void PasteFnc::VisitState(const image_state::ImageStateImpl<V,D>& isi)
{
static image_state::binop::paste_ip f_paste_ip;
static image_state::dispatch::binary_dispatch_ip<fnc_paste_ip> f_paste_ip;
f_paste_ip(target_.ImageStatePtr().get(),&isi);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment