Templates -- Meow  204.13.18
A C++ template contains kinds of interesting classes and functions
Photo.h
Go to the documentation of this file.
1 #ifndef gra_Photo_H__
2 #define gra_Photo_H__
3 
4 #include "Bitmap.h"
5 
6 #include "../Self.h"
7 
8 #include "../geo/Vectors.h"
9 #include "../math/utility.h"
10 #include "../math/Matrix.h"
11 #include "../math/Transformations.h"
12 
13 #include "../oo/ObjBase.h"
14 
15 #include <vector>
16 #include <cmath>
17 #include <string>
18 #include <typeinfo>
19 #include <cstdlib>
20 
21 namespace meow {
22 
30 template<class Pixel>
31 class Photo: public ObjBase {
32 private:
33  struct Myself {
34  Bitmap<Pixel> bmp_;
37 
38  Myself(): proj_(3) {
39  }
40 
41  Myself(Myself const& b): bmp_(b.bmp_), c_(b.c_), proj_(b.proj_) {
42  }
43 
44  ~Myself() {
45  }
46  };
47 
48  Self<Myself> const self;
49 
53  Vector2D<double> bitmapCoord(Vector2D<double> const& yx) const {
54  return Vector2D<double>(yx.x() + center().x(), -yx.y() + center().y());
55  }
56 public:
62  Photo(): self() {
63  self()->proj_.focal(1.0);
64  }
65 
73  Photo(Photo const& b): self(b.self, Self<Myself>::COPY_FROM) {
74  }
75 
83  Photo(Bitmap<Pixel> const& bmp): self() {
84  reset(bmp);
85  }
86 
95  Photo(Bitmap<Pixel> const& bmp, double f): self() {
96  reset(bmp, f);
97  }
98 
108  Photo(Bitmap<Pixel> const& bmp, double f, Vector2D<double> const& c): self() {
109  reset(bmp, f, c);
110  }
111 
115  ~Photo() {
116  }
117 
123  Photo& copyFrom(Photo const& b) {
124  self().copyFrom(b.self);
125  return *this;
126  }
127 
133  Photo& referneceFrom(Photo const& b) {
134  self().referenceFrom(b.self);
135  return *this;
136  }
137 
145  void reset(Bitmap<Pixel> const& bmp) {
146  bitmap(bmp);
147  focal(sqrt(squ(width()) + squ(height())));
148  center(Vector2D<double>(bmp.width() / 2, bmp.height() / 2));
149  }
150 
159  void reset(Bitmap<Pixel> const& bmp, double f) {
160  bitmap(bmp);
161  focal(f);
162  center(Vector2D<double>(bmp.width() / 2, bmp.height() / 2));
163  }
164 
172  void reset(Bitmap<Pixel> const& bmp, double f, Vector2D<double> const& c) {
173  bitmap(bmp);
174  focal(f);
175  center(c);
176  }
177 
181  Bitmap<Pixel> const& bitmap() const {
182  return self->bmp_;
183  }
184 
189  return self()->bmp_;
190  }
191 
198  Bitmap<Pixel> const& bitmap(Bitmap<Pixel> const& bmp) {
199  self()->bmp_.copyFrom(bmp);
200  return bitmap();
201  }
202 
206  double focal() const {
207  return self->proj_.focal();
208  }
209 
216  double focal(double f) {
217  self()->proj_.focal(f);
218  return focal();
219  }
220 
225  return self->proj_;
226  }
227 
232  if (p.dimension() == 3) {
233  self()->proj_ = p;
234  }
235  return projection();
236  }
237 
243  Vector2D<double> const& center() const {
244  return self->c_;
245  }
246 
253  return self()->c_;
254  }
255 
264  self()->c_ = c;
265  return center();
266  }
267 
271  size_t width() const {
272  return self->bmp_.width();
273  }
274 
278  size_t height() const {
279  return self->bmp_.height();
280  }
281 
285  Pixel pixel(size_t y, size_t x) const {
286  return self->bmp_.pixel(y, x);
287  }
288 
292  Pixel pixel(size_t y, size_t x, Pixel const& p) {
293  self()->bmp_.pixel(y, x, p);
294  return pixel(y, x);
295  }
296 
304  bool inside(Vector2D<double> const& yx) const {
305  Vector2D<double> c = bitmapCoord(yx);
306  ssize_t h_max = (ssize_t)height() - 1;
307  ssize_t w_max = (ssize_t)width () - 1;
308  return (0 <= c.y() && c.y() <= h_max && 0 <= c.x() && c.x() <= w_max);
309  }
310 
318  bool inside(Vector3D<double> const& p) const {
319  if (p.z() > 0) return false;
320  return inside(Vector2D<double>(self->proj_.transformate(p.matrix())));
321  }
322 
332  Pixel color(Vector2D<double> const& yx) const {
333  if (!inside(yx)) return Pixel(0);
334  Vector2D<double> c(bitmapCoord(yx));
335  int y0 = (int)c.y();
336  int x0 = (int)c.x();
337  double h[2] = {1 - (c.y() - y0), c.y() - y0};
338  double w[2] = {1 - (c.x() - x0), c.x() - x0};
339  Pixel sum(0);
340  for (int dy = 0; dy < 2; dy++)
341  for (int dx = 0; dx < 2; dx++) {
342  sum = sum + bitmap().pixel(
343  std::min(y0 + dy, (int)height() - 1),
344  std::min(x0 + dx, (int)width () - 1)) * (w[dy] * h[dx]);
345  }
346  return sum;
347  }
348 
357  Pixel color(Vector3D<double> const& p) const {
358  return color(Vector2D<double>(self->proj_.transformate(p.matrix())));
359  }
360 
364  Photo& operator=(Photo const& b) {
365  return copyFrom(b);
366  }
367 
372  bool write(FILE* f, bool bin, unsigned int fg) const {
373  if (bitmap().write(f, bin, fg) == false) return false;
374  if (bin) {
375  double tmp;
376  if (fwrite(&(tmp = center().x()), sizeof(tmp), 1, f) < 1) return false;
377  if (fwrite(&(tmp = center().y()), sizeof(tmp), 1, f) < 1) return false;
378  if (fwrite(&(tmp = focal()), sizeof(tmp), 1, f) < 1) return false;
379  }
380  else {
381  if (fprintf(f, "%f %f\n", center().x(), center().y()) < 2) return false;
382  if (fprintf(f, "%f\n", focal()) < 1) return false;
383  }
384  return true;
385  }
386 
391  bool read(FILE* f, bool bin, unsigned int fg) {
392  if (bitmapGet().read(f, bin, fg) == false) return false;
393  double tmp[3];
394  if (bin) {
395  if (fread(tmp, sizeof(double), 3, f) < 3) return false;
396  }
397  else {
398  if (fscanf(f, "%lf %lf %lf", tmp + 0, tmp + 1, tmp + 2) < 3) return false;
399  }
400  centerGet().x(tmp[0]);
401  centerGet().y(tmp[1]);
402  focal(tmp[2]);
403  return true;
404  }
405 
410  ObjBase* create() const {
411  return new Photo();
412  }
413 
423  ObjBase* copyFrom(ObjBase const* b) {
424  return &(copyFrom(*(Photo*)b));
425  }
426 
431  char const* ctype() const{
432  return typeid(*this).name();
433  }
434 
439  std::string type() const {
440  return std::string(ctype());
441  }
442 };
443 
444 } // meow
445 
446 #endif // gra_Photo_H__
PhotoProjection< double > projection(PhotoProjection< double > const &p)
設定 photo projection
Definition: Photo.h:231
double focal() const
回傳focal length
Definition: Photo.h:206
Bitmap< Pixel > const & bitmap() const
回傳bitmap
Definition: Photo.h:181
void reset(Bitmap< Pixel > const &bmp, double f)
重設bitmap, focal
Definition: Photo.h:159
Vector2D< double > & centerGet()
取得照片中心點底片座標 (non-constant reference)
Definition: Photo.h:252
PhotoProjection< double > projection() const
回傳相應的 photo projection
Definition: Photo.h:224
ObjBase * create() const
new一個自己
Definition: Photo.h:410
size_t height() const
回傳高度
Definition: Bitmap.h:146
Scalar const & x() const
access x
Definition: Vectors.h:56
char const * ctype() const
回傳class的type
Definition: Photo.h:431
Scalar const & z() const
access z
Definition: Vectors.h:315
Bitmap< Pixel > const & bitmap(Bitmap< Pixel > const &bmp)
設定bitmap
Definition: Photo.h:198
std::string type() const
回傳class的type
Definition: Photo.h:439
bool read(FILE *f, bool bin, unsigned int fg)
將資料讀入
Definition: Photo.h:391
double focal(double f)
設定 focal length
Definition: Photo.h:216
void reset(Bitmap< Pixel > const &bmp)
重設bitmap, focal 用猜的
Definition: Photo.h:145
二維點陣資料
Definition: Bitmap.h:25
Pixel pixel(size_t y, size_t x) const
回傳bitmap的某pixel
Definition: Photo.h:285
size_t dimension() const
Get the dimension of this projection.
size_t width() const
回傳bitmap寬
Definition: Photo.h:271
size_t width() const
回傳寬度
Definition: Bitmap.h:153
Photo(Bitmap< Pixel > const &bmp, double f)
constructor
Definition: Photo.h:95
Photo(Bitmap< Pixel > const &bmp)
constructor
Definition: Photo.h:83
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
Photo & referneceFrom(Photo const &b)
參照
Definition: Photo.h:133
Photo & copyFrom(Photo const &b)
複製資料
Definition: Photo.h:123
Matrix< Scalar > matrix() const
return a 3x1 matrix form of itself
Definition: Vectors.h:486
Bitmap< Pixel > & bitmapGet()
回傳bitmap 的參照(非constant)
Definition: Photo.h:188
Pixel color(Vector2D< double > const &yx) const
取得給照片座標中某點的色彩
Definition: Photo.h:332
Pixel color(Vector3D< double > const &p) const
取得給照片座標中某點的色彩
Definition: Photo.h:357
Photo(Bitmap< Pixel > const &bmp, double f, Vector2D< double > const &c)
constructor
Definition: Photo.h:108
Pixel pixel(size_t y, size_t x, Pixel const &p)
設定某pixel
Definition: Photo.h:292
Vector2D< double > const & center(Vector2D< double > const &c)
設定照片中心點底片座標
Definition: Photo.h:263
底片
Definition: Photo.h:31
void reset(Bitmap< Pixel > const &bmp, double f, Vector2D< double > const &c)
重設bitmap, focal, center
Definition: Photo.h:172
ObjBase * copyFrom(ObjBase const *b)
複製資料
Definition: Photo.h:423
Photo & operator=(Photo const &b)
same as .copyFrom(b)
Definition: Photo.h:364
Photo()
constructor
Definition: Photo.h:62
bool write(FILE *f, bool bin, unsigned int fg) const
將資料寫入檔案
Definition: Photo.h:372
Scalar const & y() const
access y
Definition: Vectors.h:71
Vector2D< double > const & center() const
取得照片中心點底片座標
Definition: Photo.h:243
bool inside(Vector2D< double > const &yx) const
檢查某點是否在底片範圍內
Definition: Photo.h:304
Photo(Photo const &b)
constructor
Definition: Photo.h:73
bool inside(Vector3D< double > const &p) const
檢查某點是否在底片範圍內
Definition: Photo.h:318
~Photo()
destructor
Definition: Photo.h:115
T squ(T const &x)
x*x
Definition: utility.h:67
size_t height() const
回傳bitmap高
Definition: Photo.h:278