00001 #pragma once
00002 #ifndef OPENGM_MODELVIEWFUNCTION_HXX
00003 #define OPENGM_MODELVIEWFUNCTION_HXX
00004
00005 #include "opengm/functions/function_properties_base.hxx"
00006
00007 namespace opengm {
00008
00015 template<class GM, class MARRAY>
00016 class ModelViewFunction
00017 : public FunctionBase<ModelViewFunction<GM,MARRAY>,
00018 typename GM::ValueType,
00019 typename GM::IndexType,
00020 typename GM::LabelType>
00021 {
00022 public:
00023 typedef GM GraphicalModelType;
00024 typedef MARRAY OffsetType;
00025 typedef typename GM::ValueType ValueType;
00026 typedef typename GM::ValueType value_type;
00027 typedef typename GM::IndexType IndexType;
00028 typedef typename GM::LabelType LabelType;
00029
00030 ModelViewFunction(const GraphicalModelType& gm, const IndexType factorIndex, const ValueType scale, OffsetType const* offset);
00031 ModelViewFunction(const GraphicalModelType& gm, const IndexType factorIndex, const ValueType scale);
00032 ModelViewFunction(OffsetType const* offset);
00033
00034 template<class Iterator> ValueType operator()(Iterator begin) const;
00035 size_t size() const;
00036 LabelType shape(const size_t i) const;
00037 size_t dimension() const;
00038
00039 private:
00041 enum ViewType {
00043 VIEW,
00045 VIEWOFFSET,
00047 OFFSET
00048 };
00049
00050 GraphicalModelType const* gm_;
00051 IndexType factorIndex_;
00052 ValueType scale_;
00053 OffsetType const* offset_;
00054 ViewType viewType_;
00055 };
00056
00062 template<class GM, class MARRAY>
00063 inline ModelViewFunction<GM, MARRAY>::ModelViewFunction
00064 (
00065 const GM& gm ,
00066 const typename ModelViewFunction<GM, MARRAY>::IndexType factorIndex,
00067 const typename ModelViewFunction<GM, MARRAY>::ValueType scale,
00068 MARRAY const* offset
00069 )
00070 : gm_(&gm),
00071 factorIndex_(factorIndex),
00072 scale_(scale),
00073 offset_(offset),
00074 viewType_(VIEWOFFSET)
00075 {
00076
00077 OPENGM_ASSERT((*offset_).size() == gm_->operator[](factorIndex_).size());
00078 OPENGM_ASSERT((*offset_).dimension() == gm_->operator[](factorIndex_).numberOfVariables());
00079 for(size_t i=0; i<(*offset_).dimension();++i)
00080 OPENGM_ASSERT((*offset_).shape(i) == gm_->operator[](factorIndex_).numberOfLabels(i));
00081 }
00082
00087 template<class GM, class MARRAY>
00088 inline ModelViewFunction<GM, MARRAY>::ModelViewFunction
00089 (
00090 const GM& gm,
00091 const typename ModelViewFunction<GM, MARRAY>::IndexType factorIndex,
00092 const ValueType scale
00093 )
00094 : gm_(&gm),
00095 factorIndex_(factorIndex),
00096 scale_(scale),
00097 viewType_(VIEW)
00098 {
00099 }
00100
00103 template<class GM, class MARRAY>
00104 inline ModelViewFunction<GM, MARRAY>::ModelViewFunction
00105 (
00106 MARRAY const* offset
00107 )
00108 : gm_(NULL),
00109 factorIndex_(0),
00110 scale_(0),
00111 offset_(offset),
00112 viewType_(OFFSET)
00113 {
00114 }
00115
00116 template<class GM, class MARRAY>
00117 template<class Iterator>
00118 inline typename opengm::ModelViewFunction<GM, MARRAY>::ValueType
00119 ModelViewFunction<GM, MARRAY>::operator()
00120 (
00121 Iterator begin
00122 ) const
00123 {
00124 switch(viewType_) {
00125 case VIEWOFFSET:
00126 return scale_*gm_->operator[](factorIndex_)(begin) + (*offset_)(begin);
00127 case VIEW:
00128 return scale_*gm_->operator[](factorIndex_)(begin);
00129 case OFFSET:
00130 return (*offset_)(begin);
00131 default:
00132 break;
00133 }
00134 return 0;
00135 }
00136
00137 template<class GM, class MARRAY>
00138 inline typename ModelViewFunction<GM, MARRAY>::LabelType
00139 ModelViewFunction<GM, MARRAY>::shape(const size_t i) const
00140 {
00141 switch(viewType_) {
00142 case VIEWOFFSET:
00143 OPENGM_ASSERT(gm_->operator[](factorIndex_).shape(i)==(*offset_).shape(i));
00144 return (*offset_).shape(i);
00145 case VIEW:
00146 return gm_->operator[](factorIndex_).shape(i);
00147 case OFFSET:
00148 return (*offset_).shape(i);
00149
00150 }
00151
00152 return 0;
00153 }
00154
00155 template<class GM, class MARRAY>
00156 inline size_t ModelViewFunction<GM, MARRAY>::size() const
00157 {
00158 switch(viewType_) {
00159 case VIEWOFFSET:
00160 return (*offset_).size();
00161 case VIEW:
00162 return gm_->operator[](factorIndex_).size();
00163 case OFFSET:
00164 return (*offset_).size();
00165
00166 }
00167 return 0;
00168 }
00169
00170 template<class GM, class MARRAY>
00171 inline size_t ModelViewFunction<GM, MARRAY>::dimension() const
00172 {
00173 switch(viewType_) {
00174 case VIEWOFFSET:
00175 OPENGM_ASSERT(gm_->operator[](factorIndex_).numberOfVariables()==(*offset_).dimension());
00176 return (*offset_).dimension();
00177 case VIEW:
00178 return gm_->operator[](factorIndex_).numberOfVariables();
00179 case OFFSET:
00180 return (*offset_).dimension();
00181 default:
00182 ;
00183 }
00184
00185 return 0;
00186 }
00187
00188 }
00189
00190 #endif // #ifndef OPENGM_MODELVIEWFUNCTION_HXX