OpenGM  2.3.x
Discrete Graphical Model Library
truncated_squared_difference.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_TRUNCATED_SQUARED_DIFFERENCE_FUNCTION_HXX
3 #define OPENGM_TRUNCATED_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<TruncatedSquaredDifferenceFunction<T, I, L>, T, I, L> {
17 public:
18  typedef T ValueType;
19  typedef I IndexType;
20  typedef L LabelType;
21 
22  TruncatedSquaredDifferenceFunction(const LabelType = 2, const LabelType = 2,
23  const ValueType = ValueType(), const ValueType = ValueType());
24  size_t shape(const IndexType) const;
25  size_t size() const;
26  size_t dimension() const;
27  template<class ITERATOR> T operator()(ITERATOR) const;
28 
29 private:
30  size_t numberOfLabels1_;
31  size_t numberOfLabels2_;
32  ValueType parameter1_;
33  ValueType parameter2_;
34 
36 };
37 
40 template <class T, class I, class L>
42  enum ID {
44  };
45 };
46 
48 template <class T, class I, class L>
49 class FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> > {
50 public:
51  typedef typename TruncatedSquaredDifferenceFunction<T, I, L>::ValueType ValueType;
52  static size_t indexSequenceSize(const TruncatedSquaredDifferenceFunction<T, I, L>&);
53  static size_t valueSequenceSize(const TruncatedSquaredDifferenceFunction<T, I, L>&);
54  template<class INDEX_OUTPUT_ITERATOR,class VALUE_OUTPUT_ITERATOR >
55  static void serialize(const TruncatedSquaredDifferenceFunction<T, I, L>&, INDEX_OUTPUT_ITERATOR,VALUE_OUTPUT_ITERATOR);
56  template<class INDEX_INPUT_ITERATOR ,class VALUE_INPUT_ITERATOR>
57  static void deserialize( INDEX_INPUT_ITERATOR,VALUE_INPUT_ITERATOR,TruncatedSquaredDifferenceFunction<T, I, L>&);
58 };
60 
64 template <class T, class I, class L>
65 inline
67 (
68  const LabelType numberOfLabels1,
69  const LabelType numberOfLabels2,
70  const ValueType truncation,
71  const ValueType weight
72 )
73 : numberOfLabels1_(numberOfLabels1),
74  numberOfLabels2_(numberOfLabels2),
75  parameter1_(truncation),
76  parameter2_(weight)
77 {}
78 
79 template <class T, class I, class L>
80 template <class ITERATOR>
83 (
84  ITERATOR begin
85 ) const {
86  ValueType value = begin[0];
87  value -= begin[1];
88  return value * value > parameter1_ ? parameter1_* parameter2_ : value * value * parameter2_;
89 }
90 
94 template <class T, class I, class L>
95 inline size_t
97  const IndexType i
98 ) const {
99  OPENGM_ASSERT(i < 2);
100  return i==0 ? numberOfLabels1_ : numberOfLabels2_;
101 }
102 
103 // order (number of variables) of the function
104 template <class T, class I, class L>
105 inline size_t
107  return 2;
108 }
109 
111 template <class T, class I, class L>
112 inline size_t
114  return numberOfLabels1_ * numberOfLabels2_;
115 }
116 
117 template <class T, class I, class L>
118 inline size_t
120 (
122 ) {
123  return 2;
124 }
125 
126 template <class T, class I, class L>
127 inline size_t
128 FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> >::valueSequenceSize
129 (
130  const TruncatedSquaredDifferenceFunction<T, I, L>& src
131 ) {
132  return 2;
133 }
134 
135 template <class T, class I, class L>
136 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
137 inline void
138 FunctionSerialization<TruncatedSquaredDifferenceFunction<T, I, L> >::serialize
139 (
140  const TruncatedSquaredDifferenceFunction<T, I, L>& src,
141  INDEX_OUTPUT_ITERATOR indexOutIterator,
142  VALUE_OUTPUT_ITERATOR valueOutIterator
143 ) {
144  *indexOutIterator = src.shape(0);
145  ++indexOutIterator;
146  *indexOutIterator = src.shape(1);
147 
148  *valueOutIterator = src.parameter1_;
149  ++valueOutIterator;
150  *valueOutIterator = src.parameter2_;
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< TruncatedSquaredDifferenceFunction<T, I, L> >::deserialize
157 (
158  INDEX_INPUT_ITERATOR indexInIterator,
159  VALUE_INPUT_ITERATOR valueInIterator,
160  TruncatedSquaredDifferenceFunction<T, I, L>& dst
161 ) {
162  const size_t shape1=*indexInIterator;
163  ++indexInIterator;
164  const size_t shape2=*indexInIterator;
165  const ValueType param1=*valueInIterator;
166  ++valueInIterator;
167  const ValueType param2=*valueInIterator;
168  dst=TruncatedSquaredDifferenceFunction<T, I, L>(shape1,shape2,param1,param2);
169 }
170 
171 } // namespace opengm
172 
173 #endif // OPENGM_TRUNCATED_SQUARED_DIFFERENCE_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
size_t shape(const IndexType) const
extension a value table encoding this function would have
size_t size() const
number of entries a value table encoding this function would have (used for I/O)
TruncatedSquaredDifferenceFunction(const LabelType=2, const LabelType=2, const ValueType=ValueType(), const ValueType=ValueType())
Constructor.
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
truncated squared difference of the labels of two variables