Templates -- Meow  204.13.18
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 FeaturePoint: public ObjBase {
22 private:
23  Vector<Scalar> pos_;
25 public:
30  }
31 
35  FeaturePoint(size_t pDim, size_t dDim):
36  pos_(pDim, Scalar(0)), des_(dDim, Description(0)) {
37  }
38 
43  pos_(fp.pos_), des_(fp.des_) {
44  }
45 
50  }
51 
56  pos_.copyFrom(fp.pos_);
57  des_.copyFrom(fp.des_);
58  return *this;
59  }
60 
65  pos_.referenceFrom(fp.pos_);
66  des_.referenceFrom(fp.des_);
67  return *this;
68  }
69 
73  Vector<Scalar> const& position() const {
74  return pos_;
75  }
76 
81  return pos_;
82  }
83 
88  return des_;
89  }
90 
95  return des_;
96  }
97 
101  Vector<Scalar> const& position(Vector<Scalar> const& p) const {
102  pos_.copyFrom(p);
103  return position();
104  }
105 
110  des_.copyFrom(d);
111  return description();
112  }
113 
117  Scalar position(size_t index) const {
118  return pos_(index);
119  }
120 
124  Description description(size_t i) const {
125  return des_(i);
126  }
127 
131  Scalar position(size_t i, Scalar const& s) {
132  pos_.entry(i, s);
133  return position(i);
134  }
135 
139  Description description(size_t i, Description const& d) {
140  des_.entry(i, d);
141  return description(i);
142  }
143 
148  return copyFrom(fp);
149  }
150 
154  Scalar const& operator()(size_t i) const {
155  return position(i);
156  }
157 
161  Description operator[](size_t i) const {
162  return description(i);
163  }
164 
165  bool write(FILE* f, bool bin, unsigned int fg) const {
166  if (bin) {
167  double tmp;
168  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
169  if (fwrite(&(tmp = position(i)), sizeof(tmp), 1, f) < 1) return false;
170  }
171  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
172  if (fwrite(&(tmp = description(i)), sizeof(tmp), 1, f) < 1)
173  return false;
174  }
175  }
176  else {
177  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
178  if (fprintf(f, "%f ", (double)position(i)) < 1) return false;
179  }
180  fprintf(f, "\n");
181  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
182  if (fprintf(f, "%f ", (double)description(i)) < 1) return false;
183  }
184  fprintf(f, "\n");
185  }
186  return true;
187  }
188 
189  bool read (FILE* f, bool bin, unsigned int fg) {
190  if (bin) {
191  double tmp;
192  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
193  if (fread(&tmp, sizeof(tmp), 1, f) < 1) return false;
194  position(i, tmp);
195  }
196  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
197  if (fread(&tmp, sizeof(tmp), 1, f) < 1) return false;
198  description(i, tmp);
199  }
200  }
201  else {
202  double tmp;
203  for (size_t i = 0, I = position().dimension(); i < I; ++i) {
204  if (fscanf(f, "%lf", &tmp) < 1) return false;
205  position(i, tmp);
206  }
207  for (size_t i = 0, I = description().dimension(); i < I; ++i) {
208  if (fscanf(f, "%lf", &tmp) < 1) return false;
209  description(i, tmp);
210  }
211  }
212  return true;
213  }
214 
215  ObjBase* create() const {
216  return new FeaturePoint();
217  }
218 
219  ObjBase* copyFrom(ObjBase const& b) {
220  return &(copyFrom(*(FeaturePoint*)b));
221  }
222 
223  char const* ctype() const {
224  return typeid(*this).name();
225  }
226 
227  std::string type() const {
228  return std::string(ctype());
229  }
230 };
231 
232 } // meow
233 
234 #endif // gra_FeaturePoint_H__
Scalar position(size_t i, Scalar const &s)
修改position的第i個scalar
Definition: FeaturePoint.h:131
FeaturePoint()
constructor
Definition: FeaturePoint.h:29
FeaturePoint & operator=(FeaturePoint const &fp)
same as copyFrom(fp)
Definition: FeaturePoint.h:147
~FeaturePoint()
destructor
Definition: FeaturePoint.h:49
Vector< Description > const & description() const
回傳description
Definition: FeaturePoint.h:87
FeaturePoint(FeaturePoint const &fp)
constructor
Definition: FeaturePoint.h:42
bool write(FILE *f, bool bin, unsigned int fg) const
將物件寫入檔案, 預設implement為直接回傳 false
Definition: FeaturePoint.h:165
Vector< Description > & descriptionGet()
回傳description (non-const reference)
Definition: FeaturePoint.h:94
Scalar const & operator()(size_t i) const
same as position(i)
Definition: FeaturePoint.h:154
Vector< Scalar > const & position(Vector< Scalar > const &p) const
修改position
Definition: FeaturePoint.h:101
FeaturePoint(size_t pDim, size_t dDim)
constructor
Definition: FeaturePoint.h:35
Vector & copyFrom(Vector const &v)
copy from ...
Definition: Vector.h:83
Scalar position(size_t index) const
回傳position的第i個scalar
Definition: FeaturePoint.h:117
ObjBase * create() const
回傳一個new出來的物件, 預設implement為直接回傳 NULL
Definition: FeaturePoint.h:215
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
vector
Definition: Vector.h:19
ObjBase * copyFrom(ObjBase const &b)
Definition: FeaturePoint.h:219
Vector< Scalar > & positionGet()
回傳position (non-const reference)
Definition: FeaturePoint.h:80
Description description(size_t i, Description const &d)
修改description的第i個Description
Definition: FeaturePoint.h:139
Description operator[](size_t i) const
same as description(i)
Definition: FeaturePoint.h:161
std::string type() const
用std::string回傳這個class的type name
Definition: FeaturePoint.h:227
char const * ctype() const
用C-style string回傳這個class的type name
Definition: FeaturePoint.h:223
FeaturePoint & copyFrom(FeaturePoint const &fp)
複製
Definition: FeaturePoint.h:55
Vector< Scalar > const & position() const
回傳position
Definition: FeaturePoint.h:73
Description description(size_t i) const
回傳description的第i個Description
Definition: FeaturePoint.h:124
Vector< Description > const & description(Vector< Description > const &d)
修改description
Definition: FeaturePoint.h:109
Vector & referenceFrom(Vector const &v)
reference from ...
Definition: Vector.h:89
FeaturePoint & referenceFrom(FeaturePoint const &fp)
參照
Definition: FeaturePoint.h:64
bool read(FILE *f, bool bin, unsigned int fg)
將物件從檔案讀出, 預設implement為直接回傳 false
Definition: FeaturePoint.h:189