Structural deformable models
MStruct.h
Go to the documentation of this file.
1 #ifndef _MSTRUCT_H_
2 #define _MSTRUCT_H_
3 
4 #include <iostream>
5 #include <string>
6 #include <map>
7 #include <list>
8 #include <vector>
9 #include "common.h"
10 #include "ParseFile.h"
11 #include "Model.h"
12 #include "ExpMap.h"
13 #include "Searcher.h"
14 #include "FFind.h"
15 
16 //----------------------------------------------------------------------------
17 class StructTable;
18 class MStructure;
19 
20 //----------------------------------------------------------------------------
22 {
23 public:
26 
27  SubStructure(const std::string& subStructName="",
28  const std::string& supStructName="",
29  const PropTF& transform=PropTF(0.f),
30  int pivot=-1);
31  void clear();
32  void analyseTF(const DMatrixf& tfmat);
33  EMDistribution* generateEMDist(const PropVec& wprop, float ppmm,
34  bool inverse = false,
35  dword dtype = EMD_RECT) const;
36  void getLBUB(PropVec& lb, PropVec& ub,
37  const PropVec& wprop, float ppmm, float radius = 3) const;
38  bool read(ParseFile& is);
39  friend std::ostream& operator<<(std::ostream& os, const SubStructure& ss);
40 
43  int m_Pivot;
44  bool m_Polar;
49 };
50 
51 //----------------------------------------------------------------------------
53 {
54  friend class StructTable;
55 public:
56  MStructure(const std::string& name="", StructTable* psTable=NULL);
57  MStructure(const MStructure& rhs);
58  ~MStructure();
59  void clear();
60  MStructure& operator=(const MStructure& rhs);
61 
62  void setName(const std::string& name);
63  const std::string& getName() const { return m_Name; }
64  void setSize(float size) { m_Size = size; }
65  float getSize() const { return m_Size; }
66  void setModel(const Model& model) { m_Model = model; }
67  const Model& getModel() const { return m_Model; }
68  Model& getModel() { return m_Model; }
69  void setWeight(float weight) { m_Weight = weight; }
70  float getWeight() const { return m_Weight; }
71  void attachStructTable(StructTable* psTable) { m_PStructTable = psTable;}
72  StructTable* getStructTable() { return m_PStructTable; }
73  const Searcher& getSearcher() const { return m_Searcher; }
74  const Winner* getWinner(dword wid) const;
75  bool isFrame() const { return !m_Model; }
76  operator bool() const { return !m_Name.empty(); }
77 
78  void addSubStruct(const SubStructure& substruct);
79  const SubStructure& getSubStruct(const std::string sname) const
80  { return m_SubStructures.find(sname)->second; }
81  bool hasSubStruct(const std::string sname) const
82  { return m_SubStructures.find(sname) != m_SubStructures.end(); }
83  const SubStructure& getSupStruct(const std::string sname) const
84  { return *m_SupStructures.find(sname)->second; }
85  bool hasSupStruct(const std::string sname) const
86  { return m_SupStructures.find(sname) != m_SupStructures.end(); }
87 
88  bool read(ParseFile &is);
89  void write(std::ostream &os) const;
91  { st.read(is); return is; }
92  friend std::ostream& operator<<(std::ostream& os, const MStructure& st)
93  { st.write(os); return os; }
94  void showStats(const SubStructure& subs, std::ostream& os=std::cout) const;
95 
96  std::string getInfoFilename(const std::string& suffix) const;
97  bool getRefModel(dword id, Model& model) const;
98  void setRefModel(dword id, const Model& model);
99  bool getRefProp(dword id, PropVec& prop) const;
100  void setRefProp(dword id, const PropVec& prop);
101  template<class Iter> dword getRefModelIDs(Iter iter) const;
102  dword loadRefProp();
103  void saveRefProp() const;
104 
105  void buildAllStats();
106  bool buildMasterModel(float scscale=1.f);
107  void rebuildExpMap();
108  void verifyWinnerRating();
109  const ExpectationMap& getExpMap() const
110  { return m_Searcher.getExpectationMap(); }
111  bool stepSearch(float dt);
112 
113 protected:
114  void refSubSuper(bool doclear=false);
115  bool addExpectation(const SubStructure& subs,
116  ExpectationMap& expmap, bool inverse=false) const;
117 protected:
119  std::string m_Name;
121  float m_Size;
122  std::map<std::string, SubStructure> m_SubStructures;
123  std::map<std::string, SubStructure*> m_SupStructures;
124  std::map<dword,PropVec> m_RefProp;
126  float m_Weight;
128 };
129 
130 //------------------- INLINES ---------------------------------------------
131 template<class Iter> dword MStructure::getRefModelIDs(Iter iter) const {
132  dword count = 0;
133  if(!m_Model) {
134  for(std::map<dword,PropVec>::const_iterator rp=m_RefProp.begin();
135  rp!=m_RefProp.end(); rp++, count++)
136  *iter++ = rp->first;
137  return count;
138  }
139  std::string filemask = getInfoFilename("*");
140  std::list<std::string> files;
141  if(FFind::findFiles(filemask, back_inserter(files)))
142  {
143  for(std::list<std::string>::iterator cf=files.begin();
144  cf != files.end(); cf++) {
145  std::string suff = cf->substr(cf->rfind('_')+1);
146  suff = suff.substr(0, suff.find('.'));
147  dword id=0;
148  fromString(suff, id);
149  if(id) {
150  *iter++ = id;
151  count++;
152  }
153  }
154  }
155  return count;
156 }
157 
158 #endif
#define NULL
Definition: simpletypes.h:9
Model m_Model
Definition: MStruct.h:120
void analyseTF(const DMatrixf &tfmat)
Definition: MStruct.cpp:646
std::string m_SubStructName
Definition: MStruct.h:41
DMatrix< T > inverse(const DMatrix< T > &dmat)
Definition: DMatrixLinAlg.h:67
bool hasSubStruct(const std::string sname) const
Definition: MStruct.h:81
T & fromString(const std::string &str, T &v)
Definition: utils.h:67
dword m_Mode
Definition: MStruct.h:48
std::map< std::string, SubStructure > m_SubStructures
Definition: MStruct.h:122
std::map< std::string, SubStructure * > m_SupStructures
Definition: MStruct.h:123
StructTable * m_PStructTable
Definition: MStruct.h:118
void setWeight(float weight)
Definition: MStruct.h:69
bool read(ParseFile &is)
Definition: MStruct.cpp:834
int findFiles(const std::string &fmask, T iter)
Definition: ffind.cpp:33
DMatrixf m_PC
Definition: MStruct.h:46
float m_Size
stdradius in mm
Definition: MStruct.h:121
std::map< dword, PropVec > m_RefProp
Definition: MStruct.h:124
friend std::ostream & operator<<(std::ostream &os, const SubStructure &ss)
Definition: MStruct.cpp:864
float m_RateWeight
Definition: MStruct.h:47
void setSize(float size)
Definition: MStruct.h:64
dword getRefModelIDs(Iter iter) const
Definition: MStruct.h:131
bool stepSearch(float dt)
float m_Weight
Definition: MStruct.h:126
std::string m_SupStructName
Definition: MStruct.h:41
const Searcher & getSearcher() const
Definition: MStruct.h:73
bool isFrame() const
Definition: MStruct.h:75
const Model & getModel() const
Definition: MStruct.h:67
friend ParseFile & operator>>(ParseFile &is, MStructure &st)
Definition: MStruct.h:90
void clear()
Definition: MStruct.cpp:822
float m_RateTH
Definition: MStruct.h:47
StructTable * getStructTable()
Definition: MStruct.h:72
SubStructure(const std::string &subStructName="", const std::string &supStructName="", const PropTF &transform=PropTF(0.f), int pivot=-1)
Definition: MStruct.cpp:636
float getWeight() const
Definition: MStruct.h:70
Model & getModel()
Definition: MStruct.h:68
PropVec PropTF
Definition: PropVec.h:9
void write(std::ostream &os) const
Definition: MStruct.cpp:132
friend std::ostream & operator<<(std::ostream &os, const MStructure &st)
Definition: MStruct.h:92
DMatrixf m_Sigma
Definition: MStruct.h:46
Definition: ExpMap.h:10
Definition: Model.h:33
std::string m_Name
Definition: MStruct.h:119
bool m_Polar
Definition: MStruct.h:44
void setModel(const Model &model)
Definition: MStruct.h:66
unsigned long dword
Definition: simpletypes.h:6
const std::string & getName() const
Definition: MStruct.h:63
Searcher m_Searcher
Definition: MStruct.h:125
PropTF m_Transform
Definition: MStruct.h:42
void write(std::ostream &os) const
const ExpectationMap & getExpMap() const
Definition: MStruct.h:109
float getSize() const
Definition: MStruct.h:65
EMDistribution * generateEMDist(const PropVec &wprop, float ppmm, bool inverse=false, dword dtype=EMD_RECT) const
Definition: MStruct.cpp:711
Winner m_FrameWinner
Definition: MStruct.h:127
const SubStructure & getSubStruct(const std::string sname) const
Definition: MStruct.h:79
bool hasSupStruct(const std::string sname) const
Definition: MStruct.h:85
PropVec m_Stdev
Definition: MStruct.h:45
void getLBUB(PropVec &lb, PropVec &ub, const PropVec &wprop, float ppmm, float radius=3) const
Definition: MStruct.cpp:803
void attachStructTable(StructTable *psTable)
Definition: MStruct.h:71
const SubStructure & getSupStruct(const std::string sname) const
Definition: MStruct.h:83
DMatrixf m_IPC
Definition: MStruct.h:46
int m_Pivot
Definition: MStruct.h:43
bool read(ParseFile &is)
Definition: MStruct.cpp:94
PropVec m_Mean
Definition: MStruct.h:45