OpenGM  2.3.x
Discrete Graphical Model Library
squared_difference.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SQUARED_DIFFERENCE_FUNCTION_HXX
3 #define OPENGM_SQUARED_DIFFERENCE_FUNCTION_HXX
4 
5 #include "opengm/opengm.hxx"
8 
9 namespace opengm {
10 
14 template<class T, class I = size_t, class L = size_t>
16 : public FunctionBase<SquaredDifferenceFunction<T, I, L>, T, I, L>
17 {
18 public:
19  typedef T ValueType;
20  typedef I IndexType;
21  typedef L LabelType;
22 
23  SquaredDifferenceFunction(const LabelType = 2, const LabelType = 2,
24  const ValueType = 1);
25  size_t shape(const IndexType) const;
26  size_t size() const;
27  size_t dimension() const;
28  T weight() const;
29  template<class ITERATOR> T operator()(ITERATOR) const;
30 
31 private:
32  size_t numberOfLabels1_;
33  size_t numberOfLabels2_;
34  T weight_;
35 };
36 
39 template <class T, class I, class L>
41  enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 4 };
42 };
43 
45 template <class T, class I, class L>
46 class FunctionSerialization<SquaredDifferenceFunction<T, I, L> > {
47 public:
48  typedef typename SquaredDifferenceFunction<T, I, L>::ValueType ValueType;
49 
50  static size_t indexSequenceSize(const SquaredDifferenceFunction<T, I, L>&);
51  static size_t valueSequenceSize(const SquaredDifferenceFunction<T, I, L>&);
52  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
53  static void serialize(const SquaredDifferenceFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
54  template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
55  static void deserialize(INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, SquaredDifferenceFunction<T, I, L>&);
56 };
58 
63 template <class T, class I, class L>
64 inline
66 (
67  const LabelType numberOfStates1,
68  const LabelType numberOfStates2,
69  const ValueType weight
70 )
71 : numberOfLabels1_(numberOfStates1),
72  numberOfLabels2_(numberOfStates2),
73  weight_(weight)
74 {}
75 
76 template <class T, class I, class L>
77 template <class ITERATOR>
78 inline T
80 (
81  ITERATOR begin
82 ) const {
83  T value = begin[0];
84  value -= begin[1];
85  return value*value*weight_;
86 }
87 
91 template <class T, class I, class L>
92 inline size_t
94  const IndexType i
95 ) const {
96  OPENGM_ASSERT(i < 2);
97  return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
98 }
99 
100 template <class T, class I, class L>
101 inline T
103  return weight_;
104 }
105 
106 // order (number of variables) of the function
107 template <class T, class I, class L>
108 inline size_t
110  return 2;
111 }
112 
114 template <class T, class I, class L>
115 inline size_t
117  return numberOfLabels1_ * numberOfLabels2_;
118 }
119 
120 template <class T, class I, class L>
121 inline size_t
123 (
125 ) {
126  return 2;
127 }
128 
129 template <class T, class I, class L>
130 inline size_t
131 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::valueSequenceSize
132 (
133  const SquaredDifferenceFunction<T, I, L>& src
134 ) {
135  return 1;
136 }
137 
138 template <class T, class I, class L>
139 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
140 inline void
141 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::serialize
142 (
143  const SquaredDifferenceFunction<T, I, L>& src,
144  INDEX_OUTPUT_ITERATOR indexOutIterator,
145  VALUE_OUTPUT_ITERATOR valueOutIterator
146 ) {
147  *indexOutIterator = src.shape(0);
148  ++indexOutIterator;
149  *indexOutIterator = src.shape(1);
150  *valueOutIterator =src.weight();
151 }
152 
153 template <class T, class I, class L>
154 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
155 inline void
156 FunctionSerialization<SquaredDifferenceFunction<T, I, L> >::deserialize
157 (
158  INDEX_INPUT_ITERATOR indexInIterator,
159  VALUE_INPUT_ITERATOR valueInIterator,
160  SquaredDifferenceFunction<T, I, L>& dst
161 ) {
162  const size_t shape0=*indexInIterator;
163  ++indexInIterator;
164  dst = SquaredDifferenceFunction<T, I, L>(shape0, *indexInIterator, *valueInIterator);
165 }
166 
167 } // namespace opengm
168 
169 #endif // OPENGM_SQUARED_DIFFERENCE_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
squared difference of the labels of two variables
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
size_t shape(const IndexType) const
extension a value table encoding this function would have
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
size_t size() const
number of entries a value table encoding this function would have (used for I/O)
SquaredDifferenceFunction(const LabelType=2, const LabelType=2, const ValueType=1)
constructor