OpenGM  2.3.x
Discrete Graphical Model Library
modelviewfunction.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_MODELVIEWFUNCTION_HXX
3 #define OPENGM_MODELVIEWFUNCTION_HXX
4 
6 
7 namespace opengm {
8 
15 template<class GM, class MARRAY>
17 : public FunctionBase<ModelViewFunction<GM,MARRAY>,
18  typename GM::ValueType,
19  typename GM::IndexType,
20  typename GM::LabelType>
21 {
22 public:
23  typedef GM GraphicalModelType;
24  typedef MARRAY OffsetType;
25  typedef typename GM::ValueType ValueType;
26  typedef typename GM::ValueType value_type;
27  typedef typename GM::IndexType IndexType;
28  typedef typename GM::LabelType LabelType;
29 
30  ModelViewFunction(const GraphicalModelType& gm, const IndexType factorIndex, const ValueType scale, OffsetType const* offset);
31  ModelViewFunction(const GraphicalModelType& gm, const IndexType factorIndex, const ValueType scale);
32  ModelViewFunction(OffsetType const* offset);
33 
34  template<class Iterator> ValueType operator()(Iterator begin) const;
35  size_t size() const;
36  LabelType shape(const size_t i) const;
37  size_t dimension() const;
38 
39 private:
41  enum ViewType {
43  VIEW,
45  VIEWOFFSET,
47  OFFSET
48  };
49 
50  GraphicalModelType const* gm_;
51  IndexType factorIndex_;
52  ValueType scale_;
53  OffsetType const* offset_;
54  ViewType viewType_;
55 };
56 
62 template<class GM, class MARRAY>
64 (
65  const GM& gm ,
66  const typename ModelViewFunction<GM, MARRAY>::IndexType factorIndex,
67  const typename ModelViewFunction<GM, MARRAY>::ValueType scale,
68  MARRAY const* offset
69 )
70 : gm_(&gm),
71  factorIndex_(factorIndex),
72  scale_(scale),
73  offset_(offset),
74  viewType_(VIEWOFFSET)
75 {
76  //viewType_ = VIEWOFFSET;
77  OPENGM_ASSERT((*offset_).size() == gm_->operator[](factorIndex_).size());
78  OPENGM_ASSERT((*offset_).dimension() == gm_->operator[](factorIndex_).numberOfVariables());
79  for(size_t i=0; i<(*offset_).dimension();++i)
80  OPENGM_ASSERT((*offset_).shape(i) == gm_->operator[](factorIndex_).numberOfLabels(i));
81 }
82 
87 template<class GM, class MARRAY>
89 (
90  const GM& gm,
91  const typename ModelViewFunction<GM, MARRAY>::IndexType factorIndex,
92  const ValueType scale
93 )
94 : gm_(&gm),
95  factorIndex_(factorIndex),
96  scale_(scale),
97  viewType_(VIEW)
98 {
99 }
100 
103 template<class GM, class MARRAY>
105 (
106  MARRAY const* offset
107 )
108 : gm_(NULL),
109  factorIndex_(0),
110  scale_(0),
111  offset_(offset),
112  viewType_(OFFSET)
113 {
114 }
115 
116 template<class GM, class MARRAY>
117 template<class Iterator>
120 (
121  Iterator begin
122 ) const
123 {
124  switch(viewType_) {
125  case VIEWOFFSET:
126  return scale_*gm_->operator[](factorIndex_)(begin) + (*offset_)(begin);
127  case VIEW:
128  return scale_*gm_->operator[](factorIndex_)(begin);
129  case OFFSET:
130  return (*offset_)(begin);
131  default:
132  break;
133  }
134  return 0;
135 }
136 
137 template<class GM, class MARRAY>
140 {
141  switch(viewType_) {
142  case VIEWOFFSET:
143  OPENGM_ASSERT(gm_->operator[](factorIndex_).shape(i)==(*offset_).shape(i));
144  return (*offset_).shape(i);
145  case VIEW:
146  return gm_->operator[](factorIndex_).shape(i);
147  case OFFSET:
148  return (*offset_).shape(i);
149  //default:
150  }
151  // To avoid compiler error "warning : control reached end
152  return 0;
153 }
154 
155 template<class GM, class MARRAY>
157 {
158  switch(viewType_) {
159  case VIEWOFFSET:
160  return (*offset_).size();
161  case VIEW:
162  return gm_->operator[](factorIndex_).size();
163  case OFFSET:
164  return (*offset_).size();
165  //default:
166  }
167  return 0;
168 }
169 
170 template<class GM, class MARRAY>
172 {
173  switch(viewType_) {
174  case VIEWOFFSET:
175  OPENGM_ASSERT(gm_->operator[](factorIndex_).numberOfVariables()==(*offset_).dimension());
176  return (*offset_).dimension();
177  case VIEW:
178  return gm_->operator[](factorIndex_).numberOfVariables();
179  case OFFSET:
180  return (*offset_).dimension();
181  default:
182  ;
183  }
184  // To avoid compiler error "warning : control reached end
185  return 0;
186 }
187 
188 } // namespace opengm
189 
190 #endif // #ifndef OPENGM_MODELVIEWFUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
ValueType operator()(Iterator begin) const
Function that refers to a factor of another GraphicalModel.
LabelType shape(const size_t i) const
ModelViewFunction(const GraphicalModelType &gm, const IndexType factorIndex, const ValueType scale, OffsetType const *offset)