From 36b8e450820f5d971e98ec6a7f91f7019dff197e Mon Sep 17 00:00:00 2001 From: Gerardo Tauriello <gerardo.tauriello@unibas.ch> Date: Wed, 25 Sep 2019 16:08:36 +0200 Subject: [PATCH] Simplify template-handling in image_state --- .../img/base/src/image_state/CMakeLists.txt | 1 - modules/img/base/src/image_state/binop.cc | 129 ------------------ modules/img/base/src/image_state/binop.hh | 74 ---------- .../img/base/src/image_state/dispatch_fw.hh | 43 ------ .../base/src/image_state/image_state_base.cc | 60 +++++++- .../base/src/image_state/image_state_inst.hh | 1 - modules/img/base/src/paste_impl.cc | 33 ++++- 7 files changed, 86 insertions(+), 255 deletions(-) delete mode 100644 modules/img/base/src/image_state/binop.cc delete mode 100644 modules/img/base/src/image_state/binop.hh delete mode 100644 modules/img/base/src/image_state/dispatch_fw.hh diff --git a/modules/img/base/src/image_state/CMakeLists.txt b/modules/img/base/src/image_state/CMakeLists.txt index f02420efc..71a1daf9a 100644 --- a/modules/img/base/src/image_state/CMakeLists.txt +++ b/modules/img/base/src/image_state/CMakeLists.txt @@ -6,7 +6,6 @@ PARENT_SCOPE ) set(OST_IMG_IMAGE_STATE_HEADERS -binop.hh dispatch.hh image_state.hh image_state_fw.hh diff --git a/modules/img/base/src/image_state/binop.cc b/modules/img/base/src/image_state/binop.cc deleted file mode 100644 index dc3339aa2..000000000 --- a/modules/img/base/src/image_state/binop.cc +++ /dev/null @@ -1,129 +0,0 @@ -//------------------------------------------------------------------------------ -// 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 - diff --git a/modules/img/base/src/image_state/binop.hh b/modules/img/base/src/image_state/binop.hh deleted file mode 100644 index 68fdc3bef..000000000 --- a/modules/img/base/src/image_state/binop.hh +++ /dev/null @@ -1,74 +0,0 @@ -//------------------------------------------------------------------------------ -// 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 diff --git a/modules/img/base/src/image_state/dispatch_fw.hh b/modules/img/base/src/image_state/dispatch_fw.hh deleted file mode 100644 index 2d9ca733c..000000000 --- a/modules/img/base/src/image_state/dispatch_fw.hh +++ /dev/null @@ -1,43 +0,0 @@ -//------------------------------------------------------------------------------ -// 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 diff --git a/modules/img/base/src/image_state/image_state_base.cc b/modules/img/base/src/image_state/image_state_base.cc index 15108e258..2c96b2f86 100644 --- a/modules/img/base/src/image_state/image_state_base.cc +++ b/modules/img/base/src/image_state/image_state_base.cc @@ -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; } diff --git a/modules/img/base/src/image_state/image_state_inst.hh b/modules/img/base/src/image_state/image_state_inst.hh index 93c874348..3d0a8472b 100644 --- a/modules/img/base/src/image_state/image_state_inst.hh +++ b/modules/img/base/src/image_state/image_state_inst.hh @@ -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" diff --git a/modules/img/base/src/paste_impl.cc b/modules/img/base/src/paste_impl.cc index 77ea06c96..c3f5840b7 100644 --- a/modules/img/base/src/paste_impl.cc +++ b/modules/img/base/src/paste_impl.cc @@ -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); } -- GitLab