00001 #pragma once
00002 #ifndef OPENGM_SQUARED_DIFFERENCE_FUNCTION_HXX
00003 #define OPENGM_SQUARED_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 SquaredDifferenceFunction
00016 : public FunctionBase<SquaredDifferenceFunction<T, I, L>, T, I, L>
00017 {
00018 public:
00019 typedef T ValueType;
00020 typedef I IndexType;
00021 typedef L LabelType;
00022
00023 SquaredDifferenceFunction(const LabelType = 2, const LabelType = 2,
00024 const ValueType = 1);
00025 size_t shape(const IndexType) const;
00026 size_t size() const;
00027 size_t dimension() const;
00028 T weight() const;
00029 template<class ITERATOR> T operator()(ITERATOR) const;
00030
00031 private:
00032 size_t numberOfLabels1_;
00033 size_t numberOfLabels2_;
00034 T weight_;
00035 };
00036
00039 template <class T, class I, class L>
00040 struct FunctionRegistration<SquaredDifferenceFunction<T, I, L> > {
00041 enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 4 };
00042 };
00043
00045 template <class T, class I, class L>
00046 class FunctionSerialization<SquaredDifferenceFunction<T, I, L> > {
00047 public:
00048 typedef typename SquaredDifferenceFunction<T, I, L>::ValueType ValueType;
00049
00050 static size_t indexSequenceSize(const SquaredDifferenceFunction<T, I, L>&);
00051 static size_t valueSequenceSize(const SquaredDifferenceFunction<T, I, L>&);
00052 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00053 static void serialize(const SquaredDifferenceFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
00054 template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
00055 static void deserialize(INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, SquaredDifferenceFunction<T, I, L>&);
00056 };
00058
00063 template <class T, class I, class L>
00064 inline
00065 SquaredDifferenceFunction<T, I, L>::SquaredDifferenceFunction
00066 (
00067 const LabelType numberOfStates1,
00068 const LabelType numberOfStates2,
00069 const ValueType weight
00070 )
00071 : numberOfLabels1_(numberOfStates1),
00072 numberOfLabels2_(numberOfStates2),
00073 weight_(weight)
00074 {}
00075
00076 template <class T, class I, class L>
00077 template <class ITERATOR>
00078 inline T
00079 SquaredDifferenceFunction<T, I, L>::operator()
00080 (
00081 ITERATOR begin
00082 ) const {
00083 T value = begin[0];
00084 value -= begin[1];
00085 return value*value*weight_;
00086 }
00087
00091 template <class T, class I, class L>
00092 inline size_t
00093 SquaredDifferenceFunction<T, I, L>::shape(
00094 const IndexType i
00095 ) const {
00096 OPENGM_ASSERT(i < 2);
00097 return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
00098 }
00099
00100 template <class T, class I, class L>
00101 inline T
00102 SquaredDifferenceFunction<T, I, L>::weight() const {
00103 return weight_;
00104 }
00105
00106
00107 template <class T, class I, class L>
00108 inline size_t
00109 SquaredDifferenceFunction<T, I, L>::dimension() const {
00110 return 2;
00111 }
00112
00114 template <class T, class I, class L>
00115 inline size_t
00116 SquaredDifferenceFunction<T, I, L>::size() const {
00117 return numberOfLabels1_ * numberOfLabels2_;
00118 }
00119
00120 template <class T, class I, class L>
00121 inline size_t
00122 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::indexSequenceSize
00123 (
00124 const SquaredDifferenceFunction<T, I, L>& src
00125 ) {
00126 return 2;
00127 }
00128
00129 template <class T, class I, class L>
00130 inline size_t
00131 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::valueSequenceSize
00132 (
00133 const SquaredDifferenceFunction<T, I, L>& src
00134 ) {
00135 return 1;
00136 }
00137
00138 template <class T, class I, class L>
00139 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00140 inline void
00141 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::serialize
00142 (
00143 const SquaredDifferenceFunction<T, I, L>& src,
00144 INDEX_OUTPUT_ITERATOR indexOutIterator,
00145 VALUE_OUTPUT_ITERATOR valueOutIterator
00146 ) {
00147 *indexOutIterator = src.shape(0);
00148 ++indexOutIterator;
00149 *indexOutIterator = src.shape(1);
00150 *valueOutIterator =src.weight();
00151 }
00152
00153 template <class T, class I, class L>
00154 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
00155 inline void
00156 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::deserialize
00157 (
00158 INDEX_INPUT_ITERATOR indexInIterator,
00159 VALUE_INPUT_ITERATOR valueInIterator,
00160 SquaredDifferenceFunction<T, I, L>& dst
00161 ) {
00162 const size_t shape0=*indexInIterator;
00163 ++indexInIterator;
00164 dst = SquaredDifferenceFunction<T, I, L>(shape0, *indexInIterator, *valueInIterator);
00165 }
00166
00167 }
00168
00169 #endif // OPENGM_SQUARED_DIFFERENCE_FUNCTION_HXX