Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
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  Myself(): proj_(3) {
38  }
39  Myself(Myself const& b): bmp_(b.bmp_), c_(b.c_), proj_(b.proj_) {
40  }
41  ~Myself() {
42  }
43  };
44 
45  Self<Myself> const self;
46 
50  Vector2D<double> bitmapCoord(Vector2D<double> const& yx) const {
51  return Vector2D<double>(yx.x() + center().x(), -yx.y() + center().y());
52  }
53 public:
59  Photo(): self() {
60  self()->proj_.focal(1.0);
61  }
62 
70  Photo(Photo const& b): self(b.self, Self<Myself>::COPY_FROM) {
71  }
72 
80  Photo(Bitmap<Pixel> const& bmp): self() {
81  reset(bmp);
82  }
83 
92  Photo(Bitmap<Pixel> const& bmp, double f): self() {
93  reset(bmp, f);
94  }
95 
105  Photo(Bitmap<Pixel> const& bmp, double f, Vector2D<double> const& c): self() {
106  reset(bmp, f, c);
107  }
108 
112  ~Photo() {
113  }
114 
120  Photo& copyFrom(Photo const& b) {
121  self().copyFrom(b.self);
122  return *this;
123  }
124 
130  Photo& referneceFrom(Photo const& b) {
131  self().referenceFrom(b.self);
132  return *this;
133  }
134 
142  void reset(Bitmap<Pixel> const& bmp) {
143  bitmap(bmp);
144  focal(sqrt(squ(width()) + squ(height())));
145  center(Vector2D<double>(bmp.width() / 2, bmp.height() / 2));
146  }
147 
156  void reset(Bitmap<Pixel> const& bmp, double f) {
157  bitmap(bmp);
158  focal(f);
159  center(Vector2D<double>(bmp.width() / 2, bmp.height() / 2));
160  }
161 
169  void reset(Bitmap<Pixel> const& bmp, double f, Vector2D<double> const& c) {
170  bitmap(bmp);
171  focal(f);
172  center(c);
173  }
174 
178  Bitmap<Pixel> const& bitmap() const {
179  return self->bmp_;
180  }
181 
186  return self()->bmp_;
187  }
188 
195  Bitmap<Pixel> const& bitmap(Bitmap<Pixel> const& bmp) {
196  self()->bmp_.copyFrom(bmp);
197  return bitmap();
198  }
199 
203  double focal() const {
204  return self->proj_.focal();
205  }
206 
213  double focal(double f) {
214  self()->proj_.focal(f);
215  return focal();
216  }
217 
222  return self->proj_;
223  }
224 
229  if (p.dimension() == 3) {
230  self()->proj_ = p;
231  }
232  return projection();
233  }
234 
240  Vector2D<double> const& center() const {
241  return self->c_;
242  }
243 
250  return self()->c_;
251  }
252 
261  self()->c_ = c;
262  return center();
263  }
264 
268  size_t width() const {
269  return self->bmp_.width();
270  }
271 
275  size_t height() const {
276  return self->bmp_.height();
277  }
278 
282  Pixel pixel(size_t y, size_t x) const {
283  return self->bmp_.pixel(y, x);
284  }
285 
289  Pixel pixel(size_t y, size_t x, Pixel const& p) {
290  self()->bmp_.pixel(y, x, p);
291  return pixel(y, x);
292  }
293 
301  bool inside(Vector2D<double> const& yx) const {
302  Vector2D<double> c = bitmapCoord(yx);
303  ssize_t h_max = (ssize_t)height() - 1;
304  ssize_t w_max = (ssize_t)width () - 1;
305  return (0 <= c.y() && c.y() <= h_max && 0 <= c.x() && c.x() <= w_max);
306  }
307 
315  bool inside(Vector3D<double> const& p) const {
316  if (p.z() > 0) return false;
317  return inside(Vector2D<double>(self->proj_.transformate(p.matrix())));
318  }
319 
329  Pixel color(Vector2D<double> const& yx) const {
330  if (!inside(yx)) return Pixel(0);
331  Vector2D<double> c(bitmapCoord(yx));
332  int y0 = (int)c.y();
333  int x0 = (int)c.x();
334  double h[2] = {1 - (c.y() - y0), c.y() - y0};
335  double w[2] = {1 - (c.x() - x0), c.x() - x0};
336  Pixel sum(0);
337  for (int dy = 0; dy < 2; dy++)
338  for (int dx = 0; dx < 2; dx++) {
339  sum = sum + bitmap().pixel(
340  std::min(y0 + dy, (int)height() - 1),
341  std::min(x0 + dx, (int)width () - 1)) * (w[dy] * h[dx]);
342  }
343  return sum;
344  }
345 
354  Pixel color(Vector3D<double> const& p) const {
355  return color(Vector2D<double>(self->proj_.transformate(p.matrix())));
356  }
357 
361  Photo& operator=(Photo const& b) {
362  return copyFrom(b);
363  }
364 
369  bool write(FILE* f, bool bin, unsigned int fg) const {
370  if (bitmap().write(f, bin, fg) == false) return false;
371  if (bin) {
372  double tmp;
373  if (fwrite(&(tmp = center().x()), sizeof(tmp), 1, f) < 1) return false;
374  if (fwrite(&(tmp = center().y()), sizeof(tmp), 1, f) < 1) return false;
375  if (fwrite(&(tmp = focal()), sizeof(tmp), 1, f) < 1) return false;
376  }
377  else {
378  if (fprintf(f, "%f %f\n", center().x(), center().y()) < 2) return false;
379  if (fprintf(f, "%f\n", focal()) < 1) return false;
380  }
381  return true;
382  }
383 
388  bool read(FILE* f, bool bin, unsigned int fg) {
389  if (bitmapGet().read(f, bin, fg) == false) return false;
390  double tmp[3];
391  if (bin) {
392  if (fread(tmp, sizeof(double), 3, f) < 3) return false;
393  }
394  else {
395  if (fscanf(f, "%lf %lf %lf", tmp + 0, tmp + 1, tmp + 2) < 3) return false;
396  }
397  centerGet().x(tmp[0]);
398  centerGet().y(tmp[1]);
399  focal(tmp[2]);
400  return true;
401  }
402 
407  ObjBase* create() const {
408  return new Photo();
409  }
410 
420  ObjBase* copyFrom(ObjBase const* b) {
421  return &(copyFrom(*(Photo*)b));
422  }
423 
428  char const* ctype() const{
429  return typeid(*this).name();
430  }
431 
436  std::string type() const {
437  return std::string(ctype());
438  }
439 };
440 
441 } // meow
442 
443 #endif // gra_Photo_H__
PhotoProjection< double > projection(PhotoProjection< double > const &p)
設定 photo projection
Definition: Photo.h:228
double focal() const
回傳focal length
Definition: Photo.h:203
Bitmap< Pixel > const & bitmap() const
回傳bitmap
Definition: Photo.h:178
void reset(Bitmap< Pixel > const &bmp, double f)
重設bitmap, focal
Definition: Photo.h:156
Vector2D< double > & centerGet()
取得照片中心點底片座標 (non-constant reference)
Definition: Photo.h:249
PhotoProjection< double > projection() const
回傳相應的 photo projection
Definition: Photo.h:221
ObjBase * create() const
new一個自己
Definition: Photo.h:407
size_t height() const
回傳高度
Definition: Bitmap.h:144
Scalar const & x() const
access x
Definition: Vectors.h:56
char const * ctype() const
回傳class的type
Definition: Photo.h:428
Scalar const & z() const
access z
Definition: Vectors.h:305
Bitmap< Pixel > const & bitmap(Bitmap< Pixel > const &bmp)
設定bitmap
Definition: Photo.h:195
std::string type() const
回傳class的type
Definition: Photo.h:436
bool read(FILE *f, bool bin, unsigned int fg)
將資料讀入
Definition: Photo.h:388
double focal(double f)
設定 focal length
Definition: Photo.h:213
void reset(Bitmap< Pixel > const &bmp)
重設bitmap, focal 用猜的
Definition: Photo.h:142
二維點陣資料
Definition: Bitmap.h:23
Pixel pixel(size_t y, size_t x) const
回傳bitmap的某pixel
Definition: Photo.h:282
size_t dimension() const
Get the dimension of this projection.
size_t width() const
回傳bitmap寬
Definition: Photo.h:268
size_t width() const
回傳寬度
Definition: Bitmap.h:151
Photo(Bitmap< Pixel > const &bmp, double f)
constructor
Definition: Photo.h:92
Photo(Bitmap< Pixel > const &bmp)
constructor
Definition: Photo.h:80
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
Photo & referneceFrom(Photo const &b)
參照
Definition: Photo.h:130
Photo & copyFrom(Photo const &b)
複製資料
Definition: Photo.h:120
Matrix< Scalar > matrix() const
return a 3x1 matrix form of itself
Definition: Vectors.h:466
Bitmap< Pixel > & bitmapGet()
回傳bitmap 的參照(非constant)
Definition: Photo.h:185
Pixel color(Vector2D< double > const &yx) const
取得給照片座標中某點的色彩
Definition: Photo.h:329
Pixel color(Vector3D< double > const &p) const
取得給照片座標中某點的色彩
Definition: Photo.h:354
Photo(Bitmap< Pixel > const &bmp, double f, Vector2D< double > const &c)
constructor
Definition: Photo.h:105
Pixel pixel(size_t y, size_t x, Pixel const &p)
設定某pixel
Definition: Photo.h:289
Vector2D< double > const & center(Vector2D< double > const &c)
設定照片中心點底片座標
Definition: Photo.h:260
底片
Definition: Photo.h:31
void reset(Bitmap< Pixel > const &bmp, double f, Vector2D< double > const &c)
重設bitmap, focal, center
Definition: Photo.h:169
ObjBase * copyFrom(ObjBase const *b)
複製資料
Definition: Photo.h:420
Photo & operator=(Photo const &b)
same as .copyFrom(b)
Definition: Photo.h:361
Photo()
constructor
Definition: Photo.h:59
bool write(FILE *f, bool bin, unsigned int fg) const
將資料寫入檔案
Definition: Photo.h:369
Scalar const & y() const
access y
Definition: Vectors.h:71
Vector2D< double > const & center() const
取得照片中心點底片座標
Definition: Photo.h:240
bool inside(Vector2D< double > const &yx) const
檢查某點是否在底片範圍內
Definition: Photo.h:301
Photo(Photo const &b)
constructor
Definition: Photo.h:70
bool inside(Vector3D< double > const &p) const
檢查某點是否在底片範圍內
Definition: Photo.h:315
~Photo()
destructor
Definition: Photo.h:112
T squ(T const &x)
x*x
Definition: utility.h:67
size_t height() const
回傳bitmap高
Definition: Photo.h:275