Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
WatchBall.h
Go to the documentation of this file.
1 #ifndef gra_WatchBall_H__
2 #define gra_WatchBall_H__
3 
4 #include "Camera.h"
5 
6 #include "../Self.h"
7 #include "../geo/Vectors.h"
8 #include "../math/LinearTransformations.h"
9 #include "../oo/ObjBase.h"
10 
11 #include <cmath>
12 #include <vector>
13 
14 namespace meow {
15 
21 template<class Pixel>
22 class WatchBall: public ObjBase {
23 public:
24  typedef std::vector<Camera<Pixel> > Cameras;
25 private:
26  struct Myself {
27  Cameras cameras_;
28  Vector3D<double> offset_;
29 
30  Myself() {
31  }
32  ~Myself() {
33  }
34  Myself& copyFrom(Myself const& b) {
35  cameras_ = b.cameras_;
36  offset_ = b. offset_;
37  return *this;
38  }
39  };
40 
41  Self<Myself> const self;
42 public:
46  WatchBall(): self() {
47  }
48 
52  WatchBall(WatchBall const& b): self(b.self, Self<Myself>::COPY_FROM) {
53  }
54 
59  }
60 
65  self().copyFrom(b.self);
66  return *this;
67  }
68 
73  self().referenceFrom(b.self);
74  return *this;
75  }
76 
80  size_t cameraSize() const {
81  return self->cameras_.size();
82  }
83 
87  Cameras const& cameras() const {
88  return self->cameras_;
89  }
90 
95  return self()->cameras_;
96  }
97 
101  Cameras const& cameras(Cameras const& c) {
102  self()->cameras_ = c;
103  return cameras();
104  }
105 
109  Camera<Pixel> const& camera(size_t i) const {
110  return cameras()[i];
111  }
112 
116  Camera<Pixel>& camera(size_t i) {
117  return cameras()[i];
118  }
119 
123  Camera<Pixel> const& camera(size_t i, Camera<Pixel> const& c) {
124  cameras()[i] = c;
125  return camera(i);
126  }
127 
131  Vector3D<double> const& offset() const {
132  return self->offset_;
133  }
134 
139  return self()->offset_;
140  }
141 
146  self()->offset_ = ofs;
147  return offset();
148  }
149 
153  Pixel color(Vector3D<double> p) const {
154  Vector3D<double> p2(p - offset());
155  Pixel sum(0);
156  double ct = 0;
157  for (size_t i = 0, I = cameraSize(); i < I; ++i) {
158  if (camera(i).inside(p2)) {
159  sum = sum + camera(i).color(p2);
160  ++ct;
161  }
162  }
163  return (ct > 0 ? sum / ct : sum);
164  }
165 
171  Bitmap<Pixel> expand(double radius) const {
172  radius = std::max(radius, 0.5);
173  size_t height = std::max<size_t>(1, 2.0 * radius);
174  size_t width = 2.0* PI * radius;
175  Bitmap<Pixel> ret(height, width, Pixel(0));
176  for (size_t i = 0; i < height; ++i) {
177  for (size_t j = 0; j < width; ++j) {
178  double theta = (1.0 * j / width - 0.5) * 2 * PI;
179  double phi = asin(-(1.0 * i / height - 0.5) * 2.0);
180  ret.pixel(i, j, color(Vector3D<double>(
181  sin(theta) * cos(phi),
182  sin(phi),
183  -cos(theta) * cos(phi)
184  )));
185  }
186  }
187  return ret;
188  }
189 
194  return copyFrom(b);
195  }
196 
201  bool write(FILE* f, bool bin, unsigned int fg) const {
202  return false;
203  }
204 
209  bool read(FILE* f, bool bin, unsigned int fg) {
210  return false;
211  }
212 
217  ObjBase* create() const {
218  return new WatchBall();
219  }
220 
230  ObjBase* copyFrom(ObjBase const* b) {
231  return &(copyFrom(*(WatchBall*)b));
232  }
233 
238  char const* ctype() const{
239  static char const* ptr = typeid(*this).name();
240  return ptr;
241  }
242 
247  std::string type() const {
248  return std::string(ctype());
249  }
250 };
251 
252 }
253 
254 #endif // gra_WatchBall_H__
WatchBall & referenceFrom(WatchBall const &b)
reference
Definition: WatchBall.h:72
Vector3D< double > & offset()
取得offset (non-constant reference)
Definition: WatchBall.h:138
WatchBall()
constructor
Definition: WatchBall.h:46
~WatchBall()
destructor
Definition: WatchBall.h:58
bool write(FILE *f, bool bin, unsigned int fg) const
將資料寫入檔案
Definition: WatchBall.h:201
Vector3D< double > const & offset(Vector3D< double > const &ofs)
設定offset
Definition: WatchBall.h:145
Camera.
Definition: Camera.h:23
char const * ctype() const
回傳class的type
Definition: WatchBall.h:238
二維點陣資料
Definition: Bitmap.h:23
Cameras const & cameras() const
取得 cameras
Definition: WatchBall.h:87
ObjBase * create() const
new一個自己
Definition: WatchBall.h:217
Pixel pixel(size_t y, size_t x) const
取得 (y, x) 的pixel
Definition: Bitmap.h:203
Camera< Pixel > const & camera(size_t i, Camera< Pixel > const &c)
設定第i個camera
Definition: WatchBall.h:123
一切物件的Base, 並要求每個物件都要有read, write, create, ... 等功能
Definition: ObjBase.h:15
WatchBall & copyFrom(WatchBall const &b)
copy data
Definition: WatchBall.h:64
多個camera, 一個offset, 一個rotation
Definition: WatchBall.h:22
ObjBase * copyFrom(ObjBase const *b)
複製資料
Definition: WatchBall.h:230
bool read(FILE *f, bool bin, unsigned int fg)
將資料讀入
Definition: WatchBall.h:209
Pixel color(Vector3D< double > p) const
取得底片color
Definition: WatchBall.h:153
Bitmap< Pixel > expand(double radius) const
輸出展開圖
Definition: WatchBall.h:171
Camera< Pixel > const & camera(size_t i) const
取得第i個camera
Definition: WatchBall.h:109
static const double PI
圓周率...
Definition: utility.h:12
Camera< Pixel > & camera(size_t i)
取得第i個camera (non-constant reference)
Definition: WatchBall.h:116
size_t cameraSize() const
取得有幾個camera
Definition: WatchBall.h:80
WatchBall(WatchBall const &b)
copy constructor
Definition: WatchBall.h:52
std::string type() const
回傳class的type
Definition: WatchBall.h:247
Vector3D< double > const & offset() const
取得offset
Definition: WatchBall.h:131
WatchBall & operator=(WatchBall const &b)
same as copyFrom(b)
Definition: WatchBall.h:193
Cameras const & cameras(Cameras const &c)
設定 camera
Definition: WatchBall.h:101
Cameras & camerasGet()
取得 cameras (non-constant)
Definition: WatchBall.h:94
std::vector< Camera< Pixel > > Cameras
Definition: WatchBall.h:24