Structural deformable models
ExpMap.h
Go to the documentation of this file.
1 #ifndef _EXPMAP_H_
2 #define _EXPMAP_H_
3 #include <list>
4 #include <map>
5 #include "Model.h"
6 #include "VVector.h"
7 #include "DMatrix.h"
8 #include "common.h"
9 
10 class Winner {
11 public:
12  enum ReservedID { WID_EMPTY = 0, WID_FIRST, WID_LAST = 0xfffffdff,
14  typedef std::map<std::string,dword> BestRatings;
15  typedef const std::map<std::string,dword> CBestRatings;
16  typedef std::map<std::string, std::map<dword,float> > Ratings;
17  typedef const std::map<std::string, std::map<dword,float> > CRatings;
18 
20  { setModel(m); }
21  void setModel(Model* m);
22  void setStructName(const std::string& sname) { m_StructName = sname; }
23  void clearRatings();
24  float rateBy(const std::string& creatorname,dword creatorid, float rating);
25  float updateBestRating();
26  const BestRatings::mapped_type&
27  getBestRatingID(const std::string& sname) const
28  { return m_BestRatings.find(sname)->second; }
29  const Ratings::mapped_type& getRatingList(const std::string& sname) const
30  { return m_Ratings.find(sname)->second; }
31  const std::pair<dword,float> getBest(const std::string& sname) const;
32  bool hasConnection(const std::string& sname) const
33  { return m_Ratings.find(sname) != m_Ratings.end(); }
34  float getConnection(const std::string& sname, dword wid) const;
35 public:
38  std::string m_StructName;
39  float m_BestRating;
40  Ratings m_Ratings;
41  BestRatings m_BestRatings;
42 };
43 
45 public:
46  enum EDFlags { ED_NONE=0, ED_OLD=1 };
47 EMDistribution() : m_Integral(1.f), m_ShootCount(0), m_Flags(ED_NONE) {};
48  virtual ~EMDistribution() {}
49  virtual PropVec getPropVec() const
50  { return PropVec(0.); }
51  virtual void setIntegral(float integral)
52  { m_Integral = integral; }
53  virtual EMDistribution* copy() const {
54  return new EMDistribution(*this);
55  }
56  virtual float ratePropVec(const PropVec& prop, Winner* winner=NULL) const
57  { return 0; }
58  void setCreator(const Winner& creator) {
59  m_Creator = creator;
60  }
61  void setShootCount(dword scount) { m_ShootCount = scount; }
62  dword getShootCount() const { return m_ShootCount; }
63  bool hasFlags(dword flags) const { return m_Flags&flags; }
64  void setFlags(dword flags) { m_Flags |= flags; }
65  void unsetFlags(dword flags=0xffffffff) { m_Flags &= ~flags; }
66 
67  float m_Integral;
71 };
72 
73 class EMDRect : public EMDistribution {
74 public:
75 EMDRect(const PropVec &lb=PropVec(0.f),
76  const PropVec &ub=PropVec(1.f))
77  : m_LB(lb), m_UB(ub)
78  {}
79  virtual ~EMDRect() {}
80  virtual EMDistribution* copy() const
81  { return (EMDistribution*) (new EMDRect(*this)); }
82  PropVec getPropVec() const;
83  float ratePropVec(const PropVec& prop, Winner* winner=NULL) const;
84  PropVec& clamp(PropVec &v) { return v.clamp(m_LB, m_UB); }
85  const PropVec& getUB() const { return m_UB; }
86  const PropVec& getLB() const { return m_LB; }
87  void setUB(const PropVec &ub) { m_UB = ub; }
88  void setLB(const PropVec &lb) { m_LB = lb; }
89 
90  PropVec m_LB, m_UB;
91 };
92 
93 class EMDGauss : public EMDistribution {
94 public:
96  const PropVec &stdev=PropVec(1.f))
97  : m_Avg(avg) { setStdev(stdev); }
98  virtual ~EMDGauss() {};
99  virtual EMDistribution* copy() const
100  { return (EMDistribution*) (new EMDGauss(*this)); }
101  PropVec getPropVec() const;
102  float ratePropVec(const PropVec& prop, Winner* winner=NULL) const;
103  void setAvg(const PropVec& avg) { m_Avg = avg; }
104  void setStdev(const PropVec& stdev);
105 protected:
106  PropVec m_Avg, m_Stdev, m_StdevRate;
107 };
108 
109 class EMDXformer : public EMDistribution {
110 public:
112  const PropVec& origin=getIdentityPropTF(),
113  const DMatrixf& tfmat=DMatrixf())
114  : m_InputEMD(input), m_Origin(origin), m_Mean(0.f), m_Stdev(1.f) {
115  assert(input != NULL);
116  setTFMat(tfmat);
117  }
118 /* EMDXformer(EMDistribution* input, const DMatrixf& tfmat, */
119 /* const PropVec& origin) */
120 /* : m_InputEMD(input), m_Origin(origin) { */
121 /* setTFMat(tfmat); */
122 /* } */
123  virtual ~EMDXformer() {
124  delete m_InputEMD; m_InputEMD = NULL;
125  }
126  virtual EMDistribution* copy() const {
127  EMDXformer* cpy = new EMDXformer(*this);
128  cpy->m_InputEMD = m_InputEMD->copy();
129  return (EMDistribution*) cpy;
130  }
131  PropVec getPropVec() const;
132  float ratePropVec(const PropVec& prop, Winner* winner=NULL) const;
133  void setTFMat(const DMatrixf& mat);
134  void setMean(const PropVec& mean) { m_Mean = mean; }
135  void setStdev(const PropVec& stdev) { m_Stdev = stdev; }
136 protected:
138  DMatrixf m_TFMat, m_ITFMat;
139  PropVec m_Origin, m_Mean, m_Stdev;
140 };
141 
142 class ExpectationMap : public EMDRect {
143 public:
144  typedef std::map<dword,EMDistribution*> EDistributions;
145 
147  ExpectationMap(const Model &model)
148  { setRepresentative(model); }
150  operator=(rhs); }
151  virtual ~ExpectationMap() { clear(); }
152  ExpectationMap& operator=(const ExpectationMap &rhs);
153  virtual EMDistribution* copy() const;
154  float ratePropVec(const PropVec& prop, Winner* winner=NULL) const;
155  void setRepresentative(const Model &model);
156  Model& getRepresentative() { return m_Representative; }
157  void add(EMDistribution *ed);
158  void add(EMDistribution *ed, dword wid);
159  void add(const EDistributions &distlist);
160  bool erase(dword wid);
161  bool hasDist(dword wid) const;
162  EMDistribution* getEDist(dword wid);
163  const EMDistribution* getEDist(dword wid) const;
164  EDistributions& getDistList() { return m_Distributions; }
165  virtual PropVec getPropVec() const;
166  Model* generateInstance() const;
167  float updateIntegral();
168  void clear(bool oldonly=false);
169  void markAllOld();
170  dword getShootCount(dword wid) const;
171  operator bool() const { return !m_Distributions.empty(); }
172  static void correctLBUB(PropVec& lb, PropVec& ub);
173 
174 protected:
176  EDistributions m_Distributions;
177  std::map<float, EMDistribution*> m_SortDist;
178 };
179 
180 //-----------------------------------------------------------------------------
181 //affine or linear or no property vector transformation (homogeneous coord.)
182 inline PropVec& hgPropTF(const DMatrixf& tfmat, PropVec& prop)
183 {
184  if(!tfmat.empty() && tfmat.sizeX() == tfmat.sizeY()) {
185  DMatrixf propDM(1,PropVec::size(), prop.begin());
186  if(tfmat.sizeX() == propDM.sizeY()) { // linear
187  propDM = propDM.mulLeft(tfmat);
188  prop = &*propDM.getData().begin();
189  } else if(tfmat.sizeX() == propDM.sizeY()+1) {//affine (homogeneous)
190  DMatrixf propDMH(1,propDM.sizeY()+1, 1.f);
191  propDMH.setRange(0,0,propDM);
192  propDMH = propDMH.mulLeft(tfmat);
193  prop = &*propDMH.getData().begin();
194  }
195  }
196  return prop;
197 }
198 
199 #endif
const std::map< std::string, dword > CBestRatings
Definition: ExpMap.h:15
PropVec m_StdevRate
Definition: ExpMap.h:106
BestRatings m_BestRatings
Definition: ExpMap.h:41
virtual PropVec getPropVec() const
Definition: ExpMap.h:49
float m_Integral
Definition: ExpMap.h:67
std::string m_StructName
Definition: ExpMap.h:38
virtual ~EMDistribution()
Definition: ExpMap.h:48
#define NULL
Definition: simpletypes.h:9
const Ratings::mapped_type & getRatingList(const std::string &sname) const
Definition: ExpMap.h:29
dword m_ShootCount
Definition: ExpMap.h:69
void clearRatings()
Definition: ExpMap.cpp:288
Model m_Representative
Definition: ExpMap.h:175
virtual EMDistribution * copy() const
Definition: ExpMap.h:126
void setAvg(const PropVec &avg)
Definition: ExpMap.h:103
EDistributions m_Distributions
Definition: ExpMap.h:176
virtual EMDistribution * copy() const
Definition: ExpMap.h:80
std::map< std::string, std::map< dword, float > > Ratings
Definition: ExpMap.h:16
bool empty() const
Definition: DMatrix.h:50
std::map< std::string, dword > BestRatings
Definition: ExpMap.h:14
const std::pair< dword, float > getBest(const std::string &sname) const
Definition: ExpMap.cpp:306
dword m_Flags
Definition: ExpMap.h:70
VVector< float, 4 > PropVec
Definition: PropVec.h:8
PropVec & clamp(PropVec &v)
Definition: ExpMap.h:84
dword getShootCount() const
Definition: ExpMap.h:62
PropTF getIdentityPropTF()
Definition: PropVec.h:81
VT & clamp(const VT &lb, const VT &ub)
Definition: VVector.h:253
DMatrix< float > DMatrixf
Definition: DMatrix.h:311
virtual ~EMDGauss()
Definition: ExpMap.h:98
PropVec m_UB
Definition: ExpMap.h:90
void setShootCount(dword scount)
Definition: ExpMap.h:61
void setCreator(const Winner &creator)
Definition: ExpMap.h:58
PropVec & hgPropTF(const DMatrixf &tfmat, PropVec &prop)
Definition: ExpMap.h:182
EMDistribution * m_InputEMD
Definition: ExpMap.h:137
Model * m_Model
Definition: ExpMap.h:36
void setModel(Model *m)
Definition: ExpMap.cpp:281
std::map< float, EMDistribution * > m_SortDist
Definition: ExpMap.h:177
void setFlags(dword flags)
Definition: ExpMap.h:64
void setStructName(const std::string &sname)
Definition: ExpMap.h:22
void setUB(const PropVec &ub)
Definition: ExpMap.h:87
Winner(Model *m=NULL)
Definition: ExpMap.h:19
void unsetFlags(dword flags=0xffffffff)
Definition: ExpMap.h:65
float m_BestRating
Definition: ExpMap.h:39
void setMean(const PropVec &mean)
Definition: ExpMap.h:134
ReservedID
Definition: ExpMap.h:12
Definition: ExpMap.h:73
float getConnection(const std::string &sname, dword wid) const
Definition: ExpMap.cpp:313
bool hasConnection(const std::string &sname) const
Definition: ExpMap.h:32
dword sizeX() const
Definition: DMatrix.h:42
Definition: ExpMap.h:10
Definition: Model.h:33
virtual float ratePropVec(const PropVec &prop, Winner *winner=NULL) const
Definition: ExpMap.h:56
ExpectationMap()
Definition: ExpMap.h:146
const PropVec & getLB() const
Definition: ExpMap.h:86
TPtr begin()
Definition: VVector.h:37
const std::map< std::string, std::map< dword, float > > CRatings
Definition: ExpMap.h:17
ExpectationMap(const ExpectationMap &rhs)
Definition: ExpMap.h:149
float updateBestRating()
Definition: ExpMap.cpp:322
unsigned long dword
Definition: simpletypes.h:6
MT mulLeft(const MT &rhs) const
Definition: DMatrix.h:159
EMDRect(const PropVec &lb=PropVec(0.f), const PropVec &ub=PropVec(1.f))
Definition: ExpMap.h:75
float rateBy(const std::string &creatorname, dword creatorid, float rating)
Definition: ExpMap.cpp:295
virtual ~EMDRect()
Definition: ExpMap.h:79
Model & getRepresentative()
Definition: ExpMap.h:156
EMDistribution()
Definition: ExpMap.h:47
virtual void setIntegral(float integral)
Definition: ExpMap.h:51
dword m_WinnerID
Definition: ExpMap.h:37
bool hasFlags(dword flags) const
Definition: ExpMap.h:63
const PropVec & getUB() const
Definition: ExpMap.h:85
ExpectationMap(const Model &model)
Definition: ExpMap.h:147
DMatrix< T > avg(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:90
virtual EMDistribution * copy() const
Definition: ExpMap.h:99
MT & setRange(dword ox, dword oy, const MT &mat)
Definition: DMatrix.h:185
virtual ~EMDXformer()
Definition: ExpMap.h:123
EMDXformer(EMDistribution *input, const PropVec &origin=getIdentityPropTF(), const DMatrixf &tfmat=DMatrixf())
Definition: ExpMap.h:111
PropVec m_Stdev
Definition: ExpMap.h:139
DMatrix< T > stdev(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:106
dword sizeY() const
Definition: DMatrix.h:43
DMatrixf m_TFMat
Definition: ExpMap.h:138
std::map< dword, EMDistribution * > EDistributions
Definition: ExpMap.h:144
Ratings m_Ratings
Definition: ExpMap.h:40
EDistributions & getDistList()
Definition: ExpMap.h:164
Winner m_Creator
Definition: ExpMap.h:68
const BestRatings::mapped_type & getBestRatingID(const std::string &sname) const
Definition: ExpMap.h:27
EMDGauss(const PropVec &avg=PropVec(0.f), const PropVec &stdev=PropVec(1.f))
Definition: ExpMap.h:95
static unsigned int size()
Definition: VVector.h:41
virtual ~ExpectationMap()
Definition: ExpMap.h:151
void setLB(const PropVec &lb)
Definition: ExpMap.h:88
virtual EMDistribution * copy() const
Definition: ExpMap.h:53
void setStdev(const PropVec &stdev)
Definition: ExpMap.h:135