Templates -- Meow  1.2.11
A C++ template contains kinds of interesting classes and functions
FeaturePoint.h
Go to the documentation of this file.
1 #ifndef gra_FeaturePoint_H__
2 #define gra_FeaturePoint_H__
3 
4 #include "../oo/ObjBase.h"
5 
6 #include "../math/Vector.h"
7 
8 #include <string>
9 #include <typeinfo>
10 #include <cstdlib>
11 #include <cstdio>
12 
13 namespace meow {
14 
20 template <class Scalar, class Description,
21  class Position = Vector<Scalar >,
22  class Feature = Vector<Description> >
23 
24 class FeaturePoint: public ObjBase {
25 private:
26  Position pos_;
27  Feature des_;
28 public:
33  }
34 
38  FeaturePoint(size_t pDim, size_t dDim):
39  pos_(pDim, Scalar(0)), des_(dDim, Description(0)) {
40  }
41 
45  FeaturePoint(Position const& v, Feature const& d):
46  pos_(v), des_(d) {
47  }
48 
53  pos_(fp.pos_), des_(fp.des_) {
54  }
55 
60  }
61 
66  pos_.copyFrom(fp.pos_);
67  des_.copyFrom(fp.des_);
68  return *this;
69  }
70 
75  pos_.referenceFrom(fp.pos_);
76  des_.referenceFrom(fp.des_);
77  return *this;
78  }
79 
83  Position position() const {
84  return pos_;
85  }
86 
90  Position& positionGet() {
91  return pos_;
92  }
93 
97  Feature description() const {
98  return des_;
99  }
100 
104  Feature& descriptionGet() {
105  return des_;
106  }
107 
111  Position position(Position const& p) {
112  pos_.copyFrom(p);
113  return position();
114  }
115 
119  Feature description(Feature const& d) {
120  des_.copyFrom(d);
121  return description();
122  }
123 
127  Scalar position(size_t index) const {
128  return position()(index);
129  }
130 
134  Description description(size_t index) const {
135  return des_(index);
136  }
137 
141  Scalar position(size_t i, Scalar const& s) {
142  pos_.scalar(i, s);
143  return position()(i);
144  }
145 
149  Description description(size_t i, Description const& d) {
150  des_.scalar(i, d);
151  return description()(i);
152  }
153 
158  return copyFrom(fp);
159  }
160 
164  Scalar const& operator()(size_t i) const {
165  return position(i);
166  }
167 
171  Description operator[](size_t i) const {
172  return description(i);
173  }
174 
175  bool write(FILE* f, bool bin, unsigned int fg) const {
176  if (bin) {
177  double tmp;
178  int a, b;
179  a = position().dimension();
180  b = description().dimension();
181  if (fwrite(&a, sizeof(a), 1, f) < 1) return false;
182  if (fwrite(&b, sizeof(b), 1, f) < 1) return false;
183  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
184  if (fwrite(&(tmp = position(i)), sizeof(tmp), 1, f) < 1) return false;
185  }
186  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
187  if (fwrite(&(tmp = description(i)), sizeof(tmp), 1, f) < 1)
188  return false;
189  }
190  }
191  else {
192  int a, b;
193  a = position().dimension();
194  b = description().dimension();
195  if (fprintf(f, "%d %d\n", a, b) < 2) return false;
196  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
197  if (fprintf(f, "%f ", (double)position(i)) < 1) return false;
198  }
199  fprintf(f, "\n");
200  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
201  if (fprintf(f, "%f ", (double)description(i)) < 1) return false;
202  }
203  fprintf(f, "\n");
204  }
205  return true;
206  }
207 
208  bool read(FILE* f, bool bin, unsigned int fg) {
209  if (bin) {
210  double tmp;
211  int a, b;
212  if (fread(&a, sizeof(a), 1, f) < 1) return false;
213  if (fread(&b, sizeof(b), 1, f) < 1) return false;
214  position(Position((size_t)a, Scalar(0)));
215  description(Feature((size_t)b, Description(0)));
216  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
217  if (fread(&tmp, sizeof(tmp), 1, f) < 1) return false;
218  position(i, tmp);
219  }
220  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
221  if (fread(&tmp, sizeof(tmp), 1, f) < 1) return false;
222  description(i, tmp);
223  }
224  }
225  else {
226  double tmp;
227  int a, b;
228  if (fscanf(f, "%d %d", &a, &b) < 2) return false;
229  position(Position((size_t)a, Scalar(0)));
230  description(Feature((size_t)b, Description(0)));
231  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
232  if (fscanf(f, "%lf", &tmp) < 1) return false;
233  position(i, tmp);
234  }
235  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
236  if (fscanf(f, "%lf", &tmp) < 1) return false;
237  description(i, tmp);
238  }
239  }
240  return true;
241  }
242 
243  ObjBase* create() const {
244  return new FeaturePoint();
245  }
246 
247  ObjBase* copyFrom(ObjBase const& b) {
248  return &(copyFrom(*(FeaturePoint const*)b));
249  }
250 
251  char const* ctype() const {
252  return typeid(*this).name();
253  }
254 
255  std::string type() const {
256  return std::string(ctype());
257  }
258 };
259 
260 } // meow
261 
262 #endif // gra_FeaturePoint_H__
FeaturePoint(FeaturePoint const &fp)
constructor
Definition: FeaturePoint.h:52
Scalar position(size_t index) const
回傳position的第i個scalar
Definition: FeaturePoint.h:127
Description description(size_t index) const
回傳description的第i個Description
Definition: FeaturePoint.h:134
Description description(size_t i, Description const &d)
修改description的第i個Description
Definition: FeaturePoint.h:149
char const * ctype() const
用C-style string回傳這個class的type name
Definition: FeaturePoint.h:251
Feature description() const
回傳description
Definition: FeaturePoint.h:97
FeaturePoint(Position const &v, Feature const &d)
constructor
Definition: FeaturePoint.h:45
Position position(Position const &p)
修改position
Definition: FeaturePoint.h:111
FeaturePoint & referenceFrom(FeaturePoint const &fp)
參照
Definition: FeaturePoint.h:74
Scalar position(size_t i, Scalar const &s)
修改position的第i個scalar
Definition: FeaturePoint.h:141
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
Description operator[](size_t i) const
same as description(i)
Definition: FeaturePoint.h:171
ObjBase * copyFrom(ObjBase const &b)
Definition: FeaturePoint.h:247
bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
Definition: FeaturePoint.h:175
~FeaturePoint()
destructor
Definition: FeaturePoint.h:59
std::string type() const
用std::string回傳這個class的type name
Definition: FeaturePoint.h:255
Feature description(Feature const &d)
修改description
Definition: FeaturePoint.h:119
Position position() const
回傳position
Definition: FeaturePoint.h:83
FeaturePoint & copyFrom(FeaturePoint const &fp)
複製
Definition: FeaturePoint.h:65
Feature & descriptionGet()
回傳description (non-const reference)
Definition: FeaturePoint.h:104
FeaturePoint(size_t pDim, size_t dDim)
constructor
Definition: FeaturePoint.h:38
FeaturePoint & operator=(FeaturePoint const &fp)
same as copyFrom(fp)
Definition: FeaturePoint.h:157
bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false
Definition: FeaturePoint.h:208
Position & positionGet()
回傳position (non-const reference)
Definition: FeaturePoint.h:90
ObjBase * create() const
回傳一個new出來的物件, 預設implement為直接回傳 NULL
Definition: FeaturePoint.h:243
Scalar const & operator()(size_t i) const
same as position(i)
Definition: FeaturePoint.h:164
FeaturePoint()
constructor
Definition: FeaturePoint.h:32