aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/FeaturePointsMatch_K_Match.h
blob: ff5956262b2baa6b6648b1db39f643488daf6913 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef   gra_FeaturePointsMatch_K_Match_H__
#define   gra_FeaturePointsMatch_K_Match_H__

#include "FeaturePointsMatch.h"

#include "../Self.h"
#include "../dsa/VP_Tree.h"
#include "../oo/ObjBase.h"

#include <cstdlib>

namespace meow {

template<class Scalar, class Description>
class FeaturePointsMatch_K_Match:
public FeaturePointsMatch<Scalar, Description> {
# define FPMKM FeaturePointsMatch_K_Match
public:
  typedef std::vector<FeaturePoint<Scalar, Description> > FeaturePoints ;
  typedef std::vector<FeaturePoints                     > FeaturePointss;
private:
  struct Node {
    size_t                   id_;
    size_t                index_;
    FeaturePointss const*   ptr_;

    Node() {
    }
    Node(Node const& nd) {
      id_    = nd.   id_;
      index_ = nd.index_;
      ptr_   = nd.  ptr_;
    }
    Node(size_t id, size_t index, FeaturePointss const* ptr) {
      id_    = id;
      index_ = index;
      ptr_   = ptr;
    }
    ~Node() {
    }
    bool operator<(Node const& nd) const {
      return (id_ < nd.id_);
    }
    Description operator[](size_t id) const {
      return (*ptr_)[id_][index_][id];
    }
  };

  struct Myself {
    size_t k_;
    Myself() {
      k_ = 1;
    }
    Myself(size_t k): k_(k) {
    }
    Myself(Myself const& m): k_(m.k_) {
    }
    ~Myself() {
    }
  };

  Self<Myself> const self;
public:
  FPMKM(): self() {
  }

  FPMKM(FPMKM const& m): self(m.self, Self<Myself>::COPY_FROM) {
    self().copyFrom(m.self);
  }

  FPMKM(size_t k): self(Myself(k)) {
  }

  ~FPMKM() {
  }

  FPMKM& copyFrom(FPMKM const& m) {
    self().copyFrom(m.self);
    return *this;
  }

  FPMKM& referenceFrom(FPMKM const& m) {
    self().referenceFrom(m.self);
    return *this;
  }

  size_t paramK() const {
    return self->k_;
  }

  size_t paramK(size_t k) {
    self()->k_ = std::max(k, (size_t)1);
    return paramK();
  }


  FeaturePointIndexPairs match(size_t dimension,
                               FeaturePoints const& from,
                               FeaturePoints const& to) const {
    return match(dimension, FeaturePointss(1, from), FeaturePointss(1, to));
  }


  FeaturePointIndexPairs match(size_t dimension,
                               FeaturePoints const& from,
                               FeaturePointss const& to) const {
    return match(dimension, FeaturePointss(1, from), to);
  }

  FeaturePointIndexPairs match(size_t dimension,
                               FeaturePointss const& from,
                               FeaturePointss const& to) const {
    VP_Tree<Node, Description> tree(dimension);
    for (size_t i = 0, I = to.size(); i < I; i++) {
      for (size_t j = 0, J = to[i].size(); j < J; j++) {
        tree.insert(Node(i, j, &to));
      }
    }
    FeaturePointIndexPairs ret(from.size());
    for (size_t i = 0, I = from.size(); i < I; i++) {
      for (size_t j = 0, J = from[i].size(); j < J; j++) {
        Node now(i, j, &from);
        std::vector<Node> tree_ret = tree.query(now, self->k_, true);
        for (size_t k = 0, K = tree_ret.size(); k < K; k++) {
          ret.push_back(FeaturePointIndexPair(i, j,
                                              tree_ret[k].id_,
                                              tree_ret[k].index_));
        }
      }
    }
    return ret;
  }

  FeaturePointIndexPairs match(size_t dimension,
                               FeaturePointss const& fpss) const {
    FeaturePointIndexPairs ret(fpss.size()), add;
    FeaturePointss to(fpss);
    for (size_t i = 0, I = fpss.size(); i < I; i++) {
      FeaturePoints tmp(to[i]);
      to[i].clear();
      add = match(dimension, fpss[i], to);
      for (size_t j = 0, J = add.size(); j < J; j++) {
        ret.push_back(FeaturePointIndexPair(i              , add[j].from.second,
                                            add[j].to.first, add[j].to.second));
      }
      to[i] = tmp;
    }
    return ret;
  }

  FPMKM& operator=(FPMKM const& b) {
    return copyFrom(b);
  }


  bool write(FILE* f, bool bin, unsigned int fg) const {
    // TODO
    return false;
  }

  bool read (FILE* f, bool bin, unsigned int fg) {
    // TODO
    return false;
  }

  ObjBase* create() const {
    return new FPMKM();
  }

  ObjBase* copyFrom(ObjBase const* ptr) {
    return &(copyFrom(*(FPMKM*)ptr));
  }

  char const* ctype() const {
    return typeid(*this).name();
  }

  std::string type() const {
    return std::string(ctype());
  }
# undef FPMKM
};

} // meow

#endif // gra_FeaturePointsMatch_K_Match_H__