truncated_absolute_difference.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_TRUNCATED_ABSOLUTE_DIFFERENCE_FUNCTION_HXX
00003 #define OPENGM_TRUNCATED_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 TruncatedAbsoluteDifferenceFunction
00016 : public FunctionBase<TruncatedAbsoluteDifferenceFunction<T, I, L>, T, I, L> {
00017 public:
00018    typedef T ValueType;
00019    typedef I IndexType;
00020    typedef L LabelType;
00021 
00022    TruncatedAbsoluteDifferenceFunction(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    T parameter1_;
00033    T parameter2_;
00034 
00035 friend class FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> > ;
00036 };
00037 
00040 template <class T, class I, class L>
00041 struct FunctionRegistration<TruncatedAbsoluteDifferenceFunction<T, I, L> >{
00042    enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 3 };
00043 };
00044 
00046 template <class T, class I, class L>
00047 class FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> > {
00048 public:
00049    typedef typename TruncatedAbsoluteDifferenceFunction<T, I, L>::ValueType ValueType;
00050 
00051    static size_t indexSequenceSize(const TruncatedAbsoluteDifferenceFunction<T, I, L>&);
00052    static size_t valueSequenceSize(const TruncatedAbsoluteDifferenceFunction<T, I, L>&);
00053    template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00054       static void serialize(const TruncatedAbsoluteDifferenceFunction<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, TruncatedAbsoluteDifferenceFunction<T, I, L>&);
00057 };
00059 
00060 template <class T, class I, class L>
00061 inline
00062 TruncatedAbsoluteDifferenceFunction<T, I, L>::TruncatedAbsoluteDifferenceFunction
00063 (
00064    const LabelType numberOfLabels1,
00065    const LabelType numberOfLabels2,
00066    const ValueType parameter1,
00067    const ValueType parameter2
00068 )
00069 :  numberOfLabels1_(numberOfLabels1),
00070    numberOfLabels2_(numberOfLabels2),
00071    parameter1_(parameter1),
00072    parameter2_(parameter2)
00073 {}
00074 
00075 template <class T, class I, class L>
00076 template <class ITERATOR>
00077 inline typename TruncatedAbsoluteDifferenceFunction<T, I, L>::ValueType
00078 TruncatedAbsoluteDifferenceFunction<T, I, L>::operator()
00079 (
00080    ITERATOR begin
00081 ) const {
00082    T value = begin[0];
00083    value -= begin[1];
00084    return abs(value) > parameter1_ ? parameter1_ * parameter2_ : abs(value) * parameter2_;
00085 }
00086 
00090 template <class T, class I, class L>
00091 inline size_t
00092 TruncatedAbsoluteDifferenceFunction<T, I, L>::shape(
00093    const IndexType i
00094 ) const {
00095    OPENGM_ASSERT(i < 2);
00096    return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
00097 }
00098 
00099 // order (number of variables) of the function
00100 template <class T, class I, class L>
00101 inline size_t
00102 TruncatedAbsoluteDifferenceFunction<T, I, L>::dimension() const {
00103    return 2;
00104 }
00105 
00107 template <class T, class I, class L>
00108 inline size_t
00109 TruncatedAbsoluteDifferenceFunction<T, I, L>::size() const {
00110    return numberOfLabels1_ * numberOfLabels2_;
00111 }
00112 
00113 template <class T, class I, class L>
00114 inline size_t
00115 FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> >::indexSequenceSize
00116 (
00117    const TruncatedAbsoluteDifferenceFunction<T, I, L>& src
00118 ) {
00119    return 2;
00120 }
00121 
00122 template <class T, class I, class L>
00123 inline size_t
00124 FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> >::valueSequenceSize
00125 (
00126    const TruncatedAbsoluteDifferenceFunction<T, I, L>& src
00127 ) {
00128    return 2;
00129 }
00130 
00131 template <class T, class I, class L>
00132 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00133 inline void
00134 FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> >::serialize
00135 (
00136    const TruncatedAbsoluteDifferenceFunction<T, I, L>& src,
00137    INDEX_OUTPUT_ITERATOR indexOutIterator,
00138    VALUE_OUTPUT_ITERATOR valueOutIterator
00139 ) {
00140    *indexOutIterator = src.shape(0);
00141    ++indexOutIterator;
00142    *indexOutIterator = src.shape(1);
00143 
00144    *valueOutIterator = src.parameter1_;
00145    ++valueOutIterator;
00146    *valueOutIterator = src.parameter2_;
00147 }
00148 
00149 template <class T, class I, class L>
00150 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
00151 inline void
00152 FunctionSerialization<TruncatedAbsoluteDifferenceFunction<T, I, L> >::deserialize
00153 (
00154    INDEX_INPUT_ITERATOR indexInIterator,
00155    VALUE_INPUT_ITERATOR valueInIterator,
00156    TruncatedAbsoluteDifferenceFunction<T, I, L>& dst
00157 ) {
00158    const size_t shape1=*indexInIterator;
00159    ++ indexInIterator;
00160    const size_t shape2=*indexInIterator;
00161    const ValueType param1=*valueInIterator;
00162    ++valueInIterator;
00163    const ValueType param2=*valueInIterator;
00164    dst = TruncatedAbsoluteDifferenceFunction<T, I, L>(shape1,shape2,param1,param2);
00165 }
00166 
00167 } // namespace opengm
00168 
00169 #endif // OPENGM_TRUNCATED_ABSOLUTE_DIFFERENCE_FUNCTION_HXX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:06 2013 for OpenGM by  doxygen 1.6.3