Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
FeaturePointsMatch_K_Match.h
Go to the documentation of this file.
1 #ifndef gra_FeaturePointsMatch_K_Match_H__
2 #define gra_FeaturePointsMatch_K_Match_H__
3 
4 #include "FeaturePointsMatch.h"
5 
6 #include "../Self.h"
7 #include "../dsa/VP_Tree.h"
8 #include "../oo/ObjBase.h"
9 
10 #include <cstdlib>
11 
12 namespace meow {
13 
14 template<class Scalar, class Description>
16 public FeaturePointsMatch<Scalar, Description> {
17 # define FPMKM FeaturePointsMatch_K_Match
18 public:
19  typedef std::vector<FeaturePoint<Scalar, Description> > FeaturePoints ;
20  typedef std::vector<FeaturePoints > FeaturePointss;
21 private:
22  struct Node {
23  size_t id_;
24  size_t index_;
25  FeaturePointss const* ptr_;
26 
27  Node() {
28  }
29  Node(Node const& nd) {
30  id_ = nd. id_;
31  index_ = nd.index_;
32  ptr_ = nd. ptr_;
33  }
34  Node(size_t id, size_t index, FeaturePointss const* ptr) {
35  id_ = id;
36  index_ = index;
37  ptr_ = ptr;
38  }
39  ~Node() {
40  }
41  bool operator<(Node const& nd) const {
42  return (id_ < nd.id_);
43  }
44  Description operator[](size_t id) const {
45  return (*ptr_)[id_][index_][id];
46  }
47  };
48 
49  struct Myself {
50  size_t k_;
51  Myself() {
52  k_ = 1;
53  }
54  Myself(size_t k): k_(k) {
55  }
56  Myself(Myself const& m): k_(m.k_) {
57  }
58  ~Myself() {
59  }
60  };
61 
62  Self<Myself> const self;
63 public:
64  FPMKM(): self() {
65  }
66 
67  FPMKM(FPMKM const& m): self(m.self, Self<Myself>::COPY_FROM) {
68  self().copyFrom(m.self);
69  }
70 
71  FPMKM(size_t k): self(Myself(k)) {
72  }
73 
74  ~FPMKM() {
75  }
76 
77  FPMKM& copyFrom(FPMKM const& m) {
78  self().copyFrom(m.self);
79  return *this;
80  }
81 
82  FPMKM& referenceFrom(FPMKM const& m) {
83  self().referenceFrom(m.self);
84  return *this;
85  }
86 
87  size_t paramK() const {
88  return self->k_;
89  }
90 
91  size_t paramK(size_t k) {
92  self()->k_ = std::max(k, (size_t)1);
93  return paramK();
94  }
95 
96 
97  FeaturePointIndexPairs match(size_t dimension,
98  FeaturePoints const& from,
99  FeaturePoints const& to) const {
100  return match(dimension, FeaturePointss(1, from), FeaturePointss(1, to));
101  }
102 
103 
104  FeaturePointIndexPairs match(size_t dimension,
105  FeaturePoints const& from,
106  FeaturePointss const& to) const {
107  return match(dimension, FeaturePointss(1, from), to);
108  }
109 
110  FeaturePointIndexPairs match(size_t dimension,
111  FeaturePointss const& from,
112  FeaturePointss const& to) const {
113  VP_Tree<Node, Description> tree(dimension);
114  for (size_t i = 0, I = to.size(); i < I; i++) {
115  for (size_t j = 0, J = to[i].size(); j < J; j++) {
116  tree.insert(Node(i, j, &to));
117  }
118  }
119  FeaturePointIndexPairs ret(from.size());
120  for (size_t i = 0, I = from.size(); i < I; i++) {
121  for (size_t j = 0, J = from[i].size(); j < J; j++) {
122  Node now(i, j, &from);
123  std::vector<Node> tree_ret = tree.query(now, self->k_, true);
124  for (size_t k = 0, K = tree_ret.size(); k < K; k++) {
125  ret.push_back(FeaturePointIndexPair(i, j,
126  tree_ret[k].id_,
127  tree_ret[k].index_));
128  }
129  }
130  }
131  return ret;
132  }
133 
134  FeaturePointIndexPairs match(size_t dimension,
135  FeaturePointss const& fpss) const {
136  FeaturePointIndexPairs ret(fpss.size()), add;
137  FeaturePointss to(fpss);
138  for (size_t i = 0, I = fpss.size(); i < I; i++) {
139  FeaturePoints tmp(to[i]);
140  to[i].clear();
141  add = match(dimension, fpss[i], to);
142  for (size_t j = 0, J = add.size(); j < J; j++) {
143  ret.push_back(FeaturePointIndexPair(i , add[j].from.second,
144  add[j].to.first, add[j].to.second));
145  }
146  to[i] = tmp;
147  }
148  return ret;
149  }
150 
151  FPMKM& operator=(FPMKM const& b) {
152  return copyFrom(b);
153  }
154 
155 
156  bool write(FILE* f, bool bin, unsigned int fg) const {
157  // TODO
158  return false;
159  }
160 
161  bool read (FILE* f, bool bin, unsigned int fg) {
162  // TODO
163  return false;
164  }
165 
166  ObjBase* create() const {
167  return new FPMKM();
168  }
169 
170  ObjBase* copyFrom(ObjBase const* ptr) {
171  return &(copyFrom(*(FPMKM*)ptr));
172  }
173 
174  char const* ctype() const {
175  return typeid(*this).name();
176  }
177 
178  std::string type() const {
179  return std::string(ctype());
180  }
181 # undef FPMKM
182 };
183 
184 } // meow
185 
186 #endif // gra_FeaturePointsMatch_K_Match_H__
ObjBase * create() const
回傳一個new出來的物件, 預設implement為直接回傳 NULL
std::string type() const
用std::string回傳這個class的type name
FeaturePointIndexPairs match(size_t dimension, FeaturePoints const &from, FeaturePointss const &to) const
std::vector< FeaturePoint< Scalar, Description > > FeaturePoints
FeaturePointIndexPairs match(size_t dimension, FeaturePoints const &from, FeaturePoints const &to) const
std::vector< FeaturePoint< Scalar, Description > > FeaturePoints
void insert(Vector const &vector)
將給定的Vector加到set中
Definition: VP_Tree.h:252
PairToPair< size_t, size_t, size_t, size_t > FeaturePointIndexPair
std::vector< FeaturePoints > FeaturePointss
FeaturePointIndexPairs match(size_t dimension, FeaturePointss const &from, FeaturePointss const &to) const
char const * ctype() const
用C-style string回傳這個class的type name
std::vector< FeaturePointIndexPair > FeaturePointIndexPairs
bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
ObjBase * copyFrom(ObjBase const *ptr)
複製, 預設使用operator=
跟KD_Tree很像歐
Definition: VP_Tree.h:51
std::vector< FeaturePoints > FeaturePointss
bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
FeaturePointIndexPairs match(size_t dimension, FeaturePointss const &fpss) const
A little class use for packing the data part of another class. With this technique, it can achieve Copy-On-Write(COR) mechanism at background and have a reference mechanism which much more flexible then the one C++ has.
Definition: Self.h:104