OpenGM  2.3.x
Discrete Graphical Model Library
l_potts.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_L_POTTS_FUNCTION_HXX
3 #define OPENGM_L_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<LPottsFunction<T, I, L>, T, I, L>
21 {
22 public:
23  typedef T ValueType;
24  typedef L LabelType;
25  typedef I IndexType;
26 
28  const LabelType,
29  const LabelType,
30  const Parameters<ValueType,IndexType> & parameters,
31  const IndexType valueNotEqual
32  );
33  LabelType shape(const size_t) const;
34  size_t size() const;
35  size_t dimension() const;
36  template<class ITERATOR> ValueType operator()(ITERATOR) const;
37  bool operator==(const LPottsFunction& ) const;
38  // specializations
39  bool isPotts() const;
40  bool isGeneralizedPotts() const;
41  ValueType min() const;
42  ValueType max() const;
43  ValueType sum() const;
44  ValueType product() const;
45  MinMaxFunctor<ValueType> minMax() const;
46 
47  // parameters
48  size_t numberOfParameters()const{
49  return 1;
50  }
51  IndexType parameterIndex(const size_t paramNumber)const{
52  return piValueNotEqual_;
53  }
54 
55 
56 private:
57  LabelType numberOfLabels1_;
58  LabelType numberOfLabels2_;
59 
60  const Parameters<ValueType,IndexType> * params_;
61 
62  IndexType piValueNotEqual_;
63 
64 friend class FunctionSerialization<LPottsFunction<T, I, L> > ;
65 };
66 
67 
68 template<class T, class I, class L>
70  enum ID {
72  };
73 };
74 
75 
76 
77 
78 
79 template <class T, class I, class L>
80 inline
82 (
83  const L numberOfLabels1,
84  const L numberOfLabels2,
85  const Parameters<ValueType,IndexType> & parameters,
86  const IndexType valueNotEqual
87 )
88 : numberOfLabels1_(numberOfLabels1),
89  numberOfLabels2_(numberOfLabels2),
90  params_(&parameters),
91  piValueNotEqual_(valueNotEqual)
92 {}
93 
94 template <class T, class I, class L>
95 template <class ITERATOR>
96 inline T
98 (
99  ITERATOR begin
100 ) const {
101  return (begin[0]==begin[1] ?
102  static_cast<ValueType>(0.0) : params_->getParameter(piValueNotEqual_) );
103 }
104 
105 
106 
107 template <class T, class I, class L>
108 inline L
110 (
111  const size_t i
112 ) const {
113  OPENGM_ASSERT(i < 2);
114  return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
115 }
116 
117 template <class T, class I, class L>
118 inline size_t
120  return 2;
121 }
122 
123 template <class T, class I, class L>
124 inline size_t
126  return numberOfLabels1_*numberOfLabels2_;
127 }
128 
129 
130 template<class T, class I, class L>
131 inline bool
133 (
134  const LPottsFunction & fb
135  )const{
136  return numberOfLabels1_ == fb.numberOfLabels1_ &&
137  numberOfLabels2_ == fb.numberOfLabels2_ &&
138  piValueNotEqual_ == fb.piValueNotEqual_;
139 }
140 
141 
142 template<class T, class I, class L>
143 inline bool
145 {
146  return true;
147 }
148 
149 template<class T, class I, class L>
150 inline bool
152 {
153  return true;
154 }
155 
156 template<class T, class I, class L>
159 {
160  const T val = params_->getParameter(piValueNotEqual_);
161  return 0.0<val ? 0.0 :val;
162 }
163 
164 template<class T, class I, class L>
167 {
168  const T val = params_->getParameter(piValueNotEqual_);
169  return 0.0>val ? 0.0 :val;
170 }
171 
172 template<class T, class I, class L>
175 {
176  const T val = params_->getParameter(piValueNotEqual_);
177  const LabelType minLabels = std::min(numberOfLabels1_, numberOfLabels2_);
178  return val * static_cast<T>(numberOfLabels1_ * numberOfLabels2_ - minLabels);
179 }
180 
181 template<class T, class I, class L>
184 {
185  return static_cast<ValueType>(0);
186 }
187 
188 template<class T, class I, class L>
189 inline MinMaxFunctor<typename LPottsFunction<T, I, L>::ValueType>
191 {
192  if(static_cast<ValueType>(0) < piValueNotEqual_) {
193  return MinMaxFunctor<T>(static_cast<ValueType>(0), params_[piValueNotEqual_]);
194  }
195  else {
196  return MinMaxFunctor<T>(params_[piValueNotEqual_], static_cast<ValueType>(0));
197  }
198 }
199 
200 } // namespace opengm
201 
202 #endif // #ifndef OPENGM_L_POTTS_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
Potts function for two variables.
Definition: l_potts.hxx:19
LabelType shape(const size_t) const
Definition: l_potts.hxx:110
MinMaxFunctor< ValueType > minMax() const
Definition: l_potts.hxx:190
size_t dimension() const
Definition: l_potts.hxx:119
ValueType operator()(ITERATOR) const
bool operator==(const LPottsFunction &) const
Definition: l_potts.hxx:133
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
bool isGeneralizedPotts() const
Definition: l_potts.hxx:151
ValueType product() const
Definition: l_potts.hxx:183
ValueType min() const
Definition: l_potts.hxx:158
size_t size() const
Definition: l_potts.hxx:125
size_t numberOfParameters() const
Definition: l_potts.hxx:48
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
ValueType max() const
Definition: l_potts.hxx:166
IndexType parameterIndex(const size_t paramNumber) const
Definition: l_potts.hxx:51
LPottsFunction(const LabelType, const LabelType, const Parameters< ValueType, IndexType > &parameters, const IndexType valueNotEqual)
Definition: l_potts.hxx:82
ValueType sum() const
Definition: l_potts.hxx:174
bool isPotts() const
Definition: l_potts.hxx:144