Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
Eye.h
Go to the documentation of this file.
1 #ifndef Eye_H__
2 #define Eye_H__
3 
4 #include "Camera.h"
5 
6 #include "../Self.h"
7 #include "../oo/ObjBase.h"
8 
9 namespace meow {
10 
16 template<class Pixel>
17 class Eye: public ObjBase {
18 private:
19  struct Myself {
20  Camera<Pixel> cam_;
21  Vector3D<double> ofs_;
22 
23  Myself(): cam_(), ofs_(0.0, 0.0, 0.0) {
24  }
25 
26  Myself(Camera<Pixel> const& c, Vector3D<double> const& o): cam_(c), ofs_(o){
27  }
28 
29  Myself(Myself const& b): cam_(b.cam_), ofs_(b.ofs_) {
30  }
31 
32  ~Myself() {
33  }
34  };
35 
36  Self<Myself> const self;
37 public:
38  Eye(): self() {
39  }
40 
41  Eye(Eye const& b): self(b.self(), Self<Myself>::COPY_FROM) {
42  }
43 
44  Eye(Camera<Pixel> const& c, Vector3D<double> const& o): self(Myself(c, o)) {
45  }
46 
47  ~Eye() {
48  }
49 
50  Eye& copyFrom(Eye const& e) {
51  self().copyFrom(e.self);
52  return *this;
53  }
54 
55  Eye& referenceFrom(Eye const& e) {
56  self().referenceFrom(e.self);
57  return *this;
58  }
59 
60  Camera<Pixel> const& camera() const {
61  return self->cam_;
62  }
63 
65  return self()->cam_;
66  }
67 
68  Camera<Pixel> const& camera(Camera<Pixel> const& c) {
69  self()->cam_.copyFrom(c);
70  return camera();
71  }
72 
73  Vector3D<double> const& offset() const {
74  return self->ofs_;
75  }
76 
78  return self()->ofs_;
79  }
80 
82  self()->ofs_ = ofs;
83  return offset();
84  }
85 
86  bool inside(Vector3D<double> const& v) const {
87  return camera().inside(v - offset());
88  }
89 
90  Eye& operator=(Eye const& e) {
91  return copyFrom(e);
92  }
93 
98  bool write(FILE* f, bool bin, unsigned int fg) const {
99  if (bin) {
100  double tmp;
101  for (size_t i = 0; i < 3; ++i) {
102  if (fwrite(&(tmp = offset()(i)), sizeof(tmp), 1, f) < 1)
103  return false;
104  }
105  }
106  else {
107  for (size_t i = 0; i < 3; ++i) {
108  if (fprintf(f, "%f ", offset()(i)) < 1) return false;
109  }
110  fprintf(f, "\n");
111  }
112  return camera().write(f, bin, fg);
113  }
114 
119  bool read(FILE* f, bool bin, unsigned int fg) {
120  if (bin) {
121  double tmp[3];
122  if (fread(tmp, sizeof(double), 3, f) < 3) return false;
123  offsetGet().xyz(tmp[0], tmp[1], tmp[2]);
124  }
125  else {
126  double a, b, c;
127  if (fscanf(f, "%lf %lf %lf", &a, &b, &c) < 3) return false;
128  offsetGet().x(a);
129  offsetGet().y(b);
130  offsetGet().z(c);
131  }
132  return cameraGet().read(f, bin, fg);
133  }
134 
139  ObjBase* create() const {
140  return new Eye();
141  }
142 
151  ObjBase* copyFrom(ObjBase const* b) {
152  return &(copyFrom(*(Eye*)b));
153  }
154 
159  char const* ctype() const{
160  return typeid(*this).name();
161  }
162 
167  std::string type() const {
168  return std::string(ctype());
169  }
170 };
171 
172 } // meow
173 
174 #endif // Eye_H__
Eye & operator=(Eye const &e)
Definition: Eye.h:90
Camera.
Definition: Camera.h:23
ObjBase * copyFrom(ObjBase const *b)
複製資料
Definition: Eye.h:151
Eye & copyFrom(Eye const &e)
Definition: Eye.h:50
Vector3D< double > & offsetGet()
Definition: Eye.h:77
Scalar const & z() const
access z
Definition: Vectors.h:305
bool read(FILE *f, bool bin, unsigned int fg)
將資料讀入
Definition: Eye.h:119
Vector3D & xyz(Scalar const &sx, Scalar const &sy, Scalar const &sz)
modify x and y
Definition: Vectors.h:343
bool write(FILE *f, bool bin, unsigned int fg) const
將資料寫入檔案
Definition: Eye.h:98
Camera< Pixel > const & camera(Camera< Pixel > const &c)
Definition: Eye.h:68
~Eye()
Definition: Eye.h:47
char const * ctype() const
回傳class的type
Definition: Eye.h:159
ObjBase * create() const
new一個自己
Definition: Eye.h:139
Scalar const & y() const
access y
Definition: Vectors.h:300
Eye(Camera< Pixel > const &c, Vector3D< double > const &o)
Definition: Eye.h:44
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
Eye()
Definition: Eye.h:38
Camera< Pixel > & cameraGet()
Definition: Eye.h:64
Camera< Pixel > const & camera() const
Definition: Eye.h:60
Scalar const & x() const
access x
Definition: Vectors.h:295
Eye & referenceFrom(Eye const &e)
Definition: Eye.h:55
Vector3D< double > const & offset() const
Definition: Eye.h:73
Eye(Eye const &b)
Definition: Eye.h:41
bool inside(Vector3D< double > const &v) const
Definition: Eye.h:86
一個 Camera 加上一個offset transformation
Definition: Eye.h:17
Vector3D< double > const & offset(Vector3D< double > const &ofs)
Definition: Eye.h:81
std::string type() const
回傳class的type
Definition: Eye.h:167