00001 #pragma once
00002 #ifndef OPENGM_TRUNCATED_SQUARED_DIFFERENCE_FUNCTION_HXX
00003 #define OPENGM_TRUNCATED_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 TruncatedSquaredDifferenceFunction
00016 : public FunctionBase<TruncatedSquaredDifferenceFunction<T, I, L>, T, I, L> {
00017 public:
00018 typedef T ValueType;
00019 typedef I IndexType;
00020 typedef L LabelType;
00021
00022 TruncatedSquaredDifferenceFunction(const LabelType = 2, const LabelType = 2,
00023 const ValueType = ValueType(), const ValueType = ValueType());
00024 size_t shape(const IndexType) const;
00025 size_t size() const;
00026 size_t dimension() const;
00027 template<class ITERATOR> T operator()(ITERATOR) const;
00028
00029 private:
00030 size_t numberOfLabels1_;
00031 size_t numberOfLabels2_;
00032 ValueType parameter1_;
00033 ValueType parameter2_;
00034
00035 friend class FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> > ;
00036 };
00037
00040 template <class T, class I, class L>
00041 struct FunctionRegistration< TruncatedSquaredDifferenceFunction<T, I, L> > {
00042 enum ID {
00043 Id = opengm::FUNCTION_TYPE_ID_OFFSET + 5
00044 };
00045 };
00046
00048 template <class T, class I, class L>
00049 class FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> > {
00050 public:
00051 typedef typename TruncatedSquaredDifferenceFunction<T, I, L>::ValueType ValueType;
00052 static size_t indexSequenceSize(const TruncatedSquaredDifferenceFunction<T, I, L>&);
00053 static size_t valueSequenceSize(const TruncatedSquaredDifferenceFunction<T, I, L>&);
00054 template<class INDEX_OUTPUT_ITERATOR,class VALUE_OUTPUT_ITERATOR >
00055 static void serialize(const TruncatedSquaredDifferenceFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR,VALUE_OUTPUT_ITERATOR);
00056 template<class INDEX_INPUT_ITERATOR ,class VALUE_INPUT_ITERATOR>
00057 static void deserialize( INDEX_INPUT_ITERATOR,VALUE_INPUT_ITERATOR,TruncatedSquaredDifferenceFunction<T, I, L>&);
00058 };
00060
00064 template <class T, class I, class L>
00065 inline
00066 TruncatedSquaredDifferenceFunction<T, I, L>::TruncatedSquaredDifferenceFunction
00067 (
00068 const LabelType numberOfLabels1,
00069 const LabelType numberOfLabels2,
00070 const ValueType truncation,
00071 const ValueType weight
00072 )
00073 : numberOfLabels1_(numberOfLabels1),
00074 numberOfLabels2_(numberOfLabels2),
00075 parameter1_(truncation),
00076 parameter2_(weight)
00077 {}
00078
00079 template <class T, class I, class L>
00080 template <class ITERATOR>
00081 inline typename TruncatedSquaredDifferenceFunction<T, I, L>::ValueType
00082 TruncatedSquaredDifferenceFunction<T, I, L>::operator()
00083 (
00084 ITERATOR begin
00085 ) const {
00086 ValueType value = begin[0];
00087 value -= begin[1];
00088 return value * value > parameter1_ ? parameter1_* parameter2_ : value * value * parameter2_;
00089 }
00090
00094 template <class T, class I, class L>
00095 inline size_t
00096 TruncatedSquaredDifferenceFunction<T, I, L>::shape(
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 TruncatedSquaredDifferenceFunction<T, I, L>::dimension() const {
00107 return 2;
00108 }
00109
00111 template <class T, class I, class L>
00112 inline size_t
00113 TruncatedSquaredDifferenceFunction<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<TruncatedSquaredDifferenceFunction<T, I, L> >::indexSequenceSize
00120 (
00121 const TruncatedSquaredDifferenceFunction<T, I, L>& src
00122 ) {
00123 return 2;
00124 }
00125
00126 template <class T, class I, class L>
00127 inline size_t
00128 FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> >::valueSequenceSize
00129 (
00130 const TruncatedSquaredDifferenceFunction<T, I, L>& src
00131 ) {
00132 return 2;
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<TruncatedSquaredDifferenceFunction<T, I, L> >::serialize
00139 (
00140 const TruncatedSquaredDifferenceFunction<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
00148 *valueOutIterator = src.parameter1_;
00149 ++valueOutIterator;
00150 *valueOutIterator = src.parameter2_;
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< TruncatedSquaredDifferenceFunction<T, I, L> >::deserialize
00157 (
00158 INDEX_INPUT_ITERATOR indexInIterator,
00159 VALUE_INPUT_ITERATOR valueInIterator,
00160 TruncatedSquaredDifferenceFunction<T, I, L>& dst
00161 ) {
00162 const size_t shape1=*indexInIterator;
00163 ++indexInIterator;
00164 const size_t shape2=*indexInIterator;
00165 const ValueType param1=*valueInIterator;
00166 ++valueInIterator;
00167 const ValueType param2=*valueInIterator;
00168 dst=TruncatedSquaredDifferenceFunction<T, I, L>(shape1,shape2,param1,param2);
00169 }
00170
00171 }
00172
00173 #endif // OPENGM_TRUNCATED_SQUARED_DIFFERENCE_FUNCTION_HXX