00001 #pragma once
00002 #ifndef OPENGM_ABSOLUTE_DIFFERENCE_FUNCTION_HXX
00003 #define OPENGM_ABSOLUTE_DIFFERENCE_FUNCTION_HXX
00004
00005 #include "opengm/opengm.hxx"
00006 #include "opengm/functions/function_registration.hxx"
00007 #include "opengm/functions/function_properties_base.hxx"
00008
00009 namespace opengm {
00010
00014 template<class T, class I = size_t, class L = size_t>
00015 class AbsoluteDifferenceFunction
00016 : public FunctionBase<AbsoluteDifferenceFunction<T, I, L>, T, I, L>
00017 {
00018 public:
00019 typedef T ValueType;
00020 typedef I IndexType;
00021 typedef L LabelType;
00022
00023 AbsoluteDifferenceFunction(const LabelType = 2, const LabelType = 2, const ValueType = 1);
00024 size_t shape(const IndexType) const;
00025 size_t size() const;
00026 size_t dimension() const;
00027 template<class ITERATOR> ValueType operator()(ITERATOR) const;
00028
00029 private:
00030 LabelType numberOfLabels1_;
00031 LabelType numberOfLabels2_;
00032 ValueType scale_;
00033 };
00034
00037 template <class T, class I, class L>
00038 struct FunctionRegistration< AbsoluteDifferenceFunction<T, I, L> >{
00040 enum ID {
00041 Id = opengm::FUNCTION_TYPE_ID_OFFSET + 2
00042 };
00043 };
00044
00046 template<class T, class I, class L>
00047 class FunctionSerialization<AbsoluteDifferenceFunction<T, I, L> > {
00048 public:
00049 typedef typename AbsoluteDifferenceFunction<T, I, L>::ValueType ValueType;
00050
00051 static size_t indexSequenceSize(const AbsoluteDifferenceFunction<T, I, L>&);
00052 static size_t valueSequenceSize(const AbsoluteDifferenceFunction<T, I, L>&);
00053 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00054 static void serialize(const AbsoluteDifferenceFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
00055 template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
00056 static void deserialize(INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, AbsoluteDifferenceFunction<T, I, L>&);
00057 };
00059
00065 template <class T, class I, class L>
00066 inline
00067 AbsoluteDifferenceFunction<T, I, L>::AbsoluteDifferenceFunction
00068 (
00069 const LabelType numberOfLabels1,
00070 const LabelType numberOfLabels2,
00071 const ValueType scale
00072 )
00073 : numberOfLabels1_(numberOfLabels1),
00074 numberOfLabels2_(numberOfLabels2),
00075 scale_(scale)
00076 {}
00077
00078 template <class T, class I, class L>
00079 template <class ITERATOR>
00080 inline typename AbsoluteDifferenceFunction<T, I, L>::ValueType
00081 AbsoluteDifferenceFunction<T, I, L>::operator()
00082 (
00083 ITERATOR begin
00084 ) const {
00085 ValueType value = begin[0];
00086 value -= begin[1];
00087 return scale_*abs(value);
00088 }
00089
00093 template <class T, class I, class L>
00094 inline size_t
00095 AbsoluteDifferenceFunction<T, I, L>::shape
00096 (
00097 const IndexType i
00098 ) const {
00099 OPENGM_ASSERT(i < 2);
00100 return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
00101 }
00102
00103
00104 template <class T, class I, class L>
00105 inline size_t
00106 AbsoluteDifferenceFunction<T, I, L>::dimension() const {
00107 return 2;
00108 }
00109
00111 template <class T, class I, class L>
00112 inline size_t
00113 AbsoluteDifferenceFunction<T, I, L>::size() const {
00114 return numberOfLabels1_ * numberOfLabels2_;
00115 }
00116
00117 template<class T, class I, class L>
00118 inline size_t
00119 FunctionSerialization<AbsoluteDifferenceFunction<T, I, L> >::indexSequenceSize
00120 (
00121 const AbsoluteDifferenceFunction<T, I, L>& src
00122 ) {
00123 return 2;
00124 }
00125
00126 template<class T, class I, class L>
00127 inline size_t
00128 FunctionSerialization<AbsoluteDifferenceFunction<T, I, L> >::valueSequenceSize
00129 (
00130 const AbsoluteDifferenceFunction<T, I, L>& src
00131 ) {
00132 return 1;
00133 }
00134
00135 template<class T, class I, class L>
00136 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00137 inline void
00138 FunctionSerialization<AbsoluteDifferenceFunction<T, I, L> >::serialize
00139 (
00140 const AbsoluteDifferenceFunction<T, I, L>& src,
00141 INDEX_OUTPUT_ITERATOR indexOutIterator,
00142 VALUE_OUTPUT_ITERATOR valueOutIterator
00143 ) {
00144 *indexOutIterator = src.shape(0);
00145 ++indexOutIterator;
00146 *indexOutIterator = src.shape(1);
00147 L l[]={0,1};
00148 *valueOutIterator = src(l);
00149 }
00150
00151 template<class T, class I, class L>
00152 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
00153 inline void
00154 FunctionSerialization< AbsoluteDifferenceFunction<T, I, L> >::deserialize
00155 (
00156 INDEX_INPUT_ITERATOR indexInIterator,
00157 VALUE_INPUT_ITERATOR valueInIterator,
00158 AbsoluteDifferenceFunction<T, I, L>& dst
00159 ) {
00160 const size_t shape0=*indexInIterator;
00161 ++ indexInIterator;
00162 dst=AbsoluteDifferenceFunction<T, I, L>(shape0, *indexInIterator,*valueInIterator);
00163 }
00164
00165 }
00166
00167 #endif // OPENGM_ABSOLUTE_DIFFERENCE_FUNCTION_HXX