OpenGM  2.3.x
Discrete Graphical Model Library
potts.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_POTTS_FUNCTION_HXX
3 #define OPENGM_POTTS_FUNCTION_HXX
4 
5 #include <algorithm>
6 #include <vector>
7 #include <cmath>
8 
9 #include "opengm/opengm.hxx"
12 
13 namespace opengm {
14 
18 template<class T, class I = size_t, class L = size_t>
20 : public FunctionBase<PottsFunction<T, I, L>, T, size_t, size_t>
21 {
22 public:
23  typedef T ValueType;
24  typedef L LabelType;
25  typedef I IndexType;
26 
27  PottsFunction(const LabelType = 2, const LabelType = 2,
28  const ValueType = ValueType(), const ValueType = ValueType());
29  LabelType shape(const size_t) const;
30  size_t size() const;
31  size_t dimension() const;
32  template<class ITERATOR> ValueType operator()(ITERATOR) const;
33  bool operator==(const PottsFunction& ) const;
34  ValueType valueEqual() const;
35  ValueType valueNotEqual() const;
36  IndexType numberOfParameters() const;
37  ValueType parameter(const size_t index) const;
38  ValueType& parameter(const size_t index);
39 
40  // specializations
41  bool isPotts() const;
42  bool isGeneralizedPotts() const;
43  ValueType min() const;
44  ValueType max() const;
45  ValueType sum() const;
46  ValueType product() const;
47  MinMaxFunctor<ValueType> minMax() const;
48 
49 private:
50  LabelType numberOfLabels1_;
51  LabelType numberOfLabels2_;
52  ValueType valueEqual_;
53  ValueType valueNotEqual_;
54 
55 friend class FunctionSerialization<PottsFunction<T, I, L> > ;
56 };
57 
60 template<class T, class I, class L>
61 struct FunctionRegistration<PottsFunction<T, I, L> > {
62  enum ID {
64  };
65 };
66 
68 template<class T, class I, class L>
69 class FunctionSerialization<PottsFunction<T, I, L> > {
70 public:
71  typedef typename PottsFunction<T, I, L>::ValueType ValueType;
72 
73  static size_t indexSequenceSize(const PottsFunction<T, I, L>&);
74  static size_t valueSequenceSize(const PottsFunction<T, I, L>&);
75  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR>
76  static void serialize(const PottsFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
77  template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR>
78  static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, PottsFunction<T, I, L>&);
79 };
81 
87 template <class T, class I, class L>
88 inline
90 (
91  const L numberOfLabels1,
92  const L numberOfLabels2,
93  const T valueEqual,
94  const T valueNotEqual
95 )
96 : numberOfLabels1_(numberOfLabels1),
97  numberOfLabels2_(numberOfLabels2),
98  valueEqual_(valueEqual),
99  valueNotEqual_(valueNotEqual)
100 {}
101 
102 template <class T, class I, class L>
103 template <class ITERATOR>
104 inline T
106 (
107  ITERATOR begin
108 ) const {
109  return (begin[0]==begin[1] ? valueEqual_ : valueNotEqual_);
110 }
111 
112 template <class T, class I, class L>
113 inline T
115  return valueEqual_;
116 }
117 
118 template <class T, class I, class L>
119 inline T
121  return valueEqual_;
122 }
123 
124 template <class T, class I, class L>
125 inline L
127 (
128  const size_t i
129 ) const {
130  OPENGM_ASSERT(i < 2);
131  return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
132 }
133 
134 template <class T, class I, class L>
135 inline size_t
137  return 2;
138 }
139 
140 template <class T, class I, class L>
141 inline size_t
143  return numberOfLabels1_*numberOfLabels2_;
144 }
145 
146 template<class T, class I, class L>
147 inline size_t
149 (
150  const PottsFunction<T, I, L> & src
151 ) {
152  return 2;
153 }
154 
155 template<class T, class I, class L>
156 inline size_t
157 FunctionSerialization<PottsFunction<T, I, L> >::valueSequenceSize
158 (
159  const PottsFunction<T, I, L> & src
160 ) {
161  return 2;
162 }
163 
164 template<class T, class I, class L>
165 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
166 inline void
167 FunctionSerialization<PottsFunction<T, I, L> >::serialize
168 (
169  const PottsFunction<T, I, L> & src,
170  INDEX_OUTPUT_ITERATOR indexOutIterator,
171  VALUE_OUTPUT_ITERATOR valueOutIterator
172 ) {
173  *indexOutIterator = src.shape(0);
174  ++indexOutIterator;
175  *indexOutIterator = src.shape(1);
176 
177  *valueOutIterator = src.valueEqual_;
178  ++valueOutIterator;
179  *valueOutIterator = src.valueNotEqual_;
180 }
181 
182 template<class T, class I, class L>
183 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
184 inline void
185 FunctionSerialization<PottsFunction<T, I, L> >::deserialize
186 (
187  INDEX_INPUT_ITERATOR indexInIterator,
188  VALUE_INPUT_ITERATOR valueInIterator,
189  PottsFunction<T, I, L> & dst
190 ) {
191  const size_t shape1=*indexInIterator;
192  ++ indexInIterator;
193  const size_t shape2=*indexInIterator;
194  const ValueType param1=*valueInIterator;
195  ++valueInIterator;
196  const ValueType param2=*valueInIterator;
197  dst=PottsFunction<T, I, L>(shape1, shape2, param1, param2);
198 }
199 
200 template<class T, class I, class L>
201 inline bool
202 PottsFunction<T, I, L>::operator==
203 (
204  const PottsFunction & fb
205  )const{
206  return numberOfLabels1_ == fb.numberOfLabels1_ &&
207  numberOfLabels2_ == fb.numberOfLabels2_ &&
208  valueEqual_ == fb.valueEqual_ &&
209  valueNotEqual_ == fb.valueNotEqual_;
210 }
211 
212 template<class T, class I, class L>
213 inline typename PottsFunction<T, I, L>::IndexType
215 {
216  return 2;
217 }
218 
219 template<class T, class I, class L>
220 inline typename PottsFunction<T, I, L>::ValueType
222  const size_t index
223 ) const
224 {
225  OPENGM_ASSERT(index < 2);
226  return index == 0 ? valueEqual_ : valueNotEqual_;
227 }
228 
229 template<class T, class I, class L>
230 inline typename PottsFunction<T, I, L>::ValueType&
232  const size_t index
233 )
234 {
235  OPENGM_ASSERT(index < 2);
236  return index==0 ? valueEqual_:valueNotEqual_;
237 }
238 
239 template<class T, class I, class L>
240 inline bool
242 {
243  return true;
244 }
245 
246 template<class T, class I, class L>
247 inline bool
249 {
250  return true;
251 }
252 
253 template<class T, class I, class L>
254 inline typename PottsFunction<T, I, L>::ValueType
256 {
257  return valueEqual_<valueNotEqual_ ? valueEqual_ :valueNotEqual_;
258 }
259 
260 template<class T, class I, class L>
261 inline typename PottsFunction<T, I, L>::ValueType
263 {
264  return valueNotEqual_>valueEqual_ ? valueNotEqual_ :valueEqual_;
265 }
266 
267 template<class T, class I, class L>
268 inline typename PottsFunction<T, I, L>::ValueType
270 {
271  const LabelType minLabels = std::min(numberOfLabels1_, numberOfLabels2_);
272  return valueNotEqual_ * static_cast<T>(numberOfLabels1_ * numberOfLabels2_ - minLabels)
273  + valueEqual_*static_cast<T>(minLabels);
274 }
275 
276 template<class T, class I, class L>
277 inline typename PottsFunction<T, I, L>::ValueType
279 {
280  const LabelType minLabels = std::min(numberOfLabels1_, numberOfLabels2_);
281  // TODO: improve this: do not use std::pow, instead write a proper pow functor class for OpenGM
282  // the call of std::pow is ambiguous for many common combinations of types. this is just a
283  // work-around with possible loss of precision, e.g. if valuesNotEqual_ is a long double
284  const double x1 = static_cast<double>(valueNotEqual_);
285  const int n1 = static_cast<int>(numberOfLabels1_ * numberOfLabels2_ - minLabels);
286  const double x2 = static_cast<double>(valueEqual_);
287  const int n2 = static_cast<int>(minLabels);
288  return static_cast<T>(std::pow(x1, n1) * std::pow(x2, n2));
289 }
290 
291 template<class T, class I, class L>
292 inline MinMaxFunctor<typename PottsFunction<T, I, L>::ValueType>
294 {
295  if(valueEqual_<valueNotEqual_) {
296  return MinMaxFunctor<T>(valueEqual_, valueNotEqual_);
297  }
298  else {
299  return MinMaxFunctor<T>(valueNotEqual_, valueEqual_);
300  }
301 }
302 
303 } // namespace opengm
304 
305 #endif // #ifndef OPENGM_POTTS_FUNCTION_HXX
ValueType min() const
Definition: potts.hxx:255
The OpenGM namespace.
Definition: config.hxx:43
bool isGeneralizedPotts() const
Definition: potts.hxx:248
Fallback implementation of member functions of OpenGM functions.
ValueType product() const
Definition: potts.hxx:278
LabelType shape(const size_t) const
Definition: potts.hxx:127
ValueType valueEqual() const
Definition: potts.hxx:114
ValueType operator()(ITERATOR) const
ValueType max() const
Definition: potts.hxx:262
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
size_t dimension() const
Definition: potts.hxx:136
MinMaxFunctor< ValueType > minMax() const
Definition: potts.hxx:293
IndexType numberOfParameters() const
Definition: potts.hxx:214
PottsFunction(const LabelType=2, const LabelType=2, const ValueType=ValueType(), const ValueType=ValueType())
constructor
Definition: potts.hxx:90
bool isPotts() const
Definition: potts.hxx:241
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
ValueType parameter(const size_t index) const
Definition: potts.hxx:221
Potts function for two variables.
Definition: potts.hxx:19
ValueType valueNotEqual() const
Definition: potts.hxx:120
bool operator==(const PottsFunction &) const
Definition: potts.hxx:203
size_t size() const
Definition: potts.hxx:142
ValueType sum() const
Definition: potts.hxx:269