aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/WatchBall.h
blob: c72e2a511b6fc6e9889a9e039cb1d4bfe4c63083 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef   gra_WatchBall_H__
#define   gra_WatchBall_H__

#include "Camera.h"

#include "../Self.h"
#include "../geo/Vectors.h"
#include "../math/LinearTransformations.h"
#include "../oo/ObjBase.h"

#include <cmath>
#include <vector>

namespace meow {

/*!
 * @brief \b 多個camera, 一個offset, 一個rotation
 *
 * @author cat_leopard
 */
template<class Pixel>
class WatchBall: public ObjBase {
public:
  typedef std::vector<Camera<Pixel> > Cameras;
private:
  struct Myself {
    Cameras          cameras_;
    Vector3D<double>  offset_;

    Myself() {
    }
    ~Myself() {
    }
    Myself& copyFrom(Myself const& b) {
      cameras_ = b.cameras_;
      offset_  = b. offset_;
      return *this;
    }
  };

  Self<Myself> const self;
public:
  /*!
   * @brief constructor
   */
  WatchBall(): self(true) {
  }
  
  /*!
   * @brief copy constructor
   */
  WatchBall(WatchBall const& b): self(false) {
    copyFrom(b);
  }
  
  /*!
   * @brief destructor
   */
  ~WatchBall() {
  }
  
  /*!
   * @brief copy data
   */
  WatchBall& copyFrom(WatchBall const& b) {
    self().copyFrom(b.self);
    return *this;
  }
  
  /*!
   * @brief reference
   */
  WatchBall& referenceFrom(WatchBall const& b) {
    self().referenceFrom(b.self);
    return *this;
  }
  
  /*!
   * @brief 取得有幾個camera
   */
  size_t cameraSize() const {
    return self->cameras_.size();
  }
  
  /*!
   * @brief 取得 cameras
   */
  Cameras const& cameras() const {
    return self->cameras_;
  }
  
  /*!
   * @brief 取得 cameras (non-constant)
   */
  Cameras& camerasGet() {
    return self()->cameras_;
  }
  
  /*!
   * @brief 設定 camera
   */
  Cameras const& cameras(Cameras const& c) {
    self()->cameras_ = c;
    return cameras();
  }
  
  /*!
   * @brief 取得第i個camera
   */
  Camera<Pixel> const& camera(size_t i) const {
    return cameras()[i];
  }
  
  /*!
   * @brief 取得第i個camera (non-constant reference)
   */
  Camera<Pixel>& camera(size_t i) {
    return cameras()[i];
  }
  
  /*!
   * @brief 設定第i個camera
   */
  Camera<Pixel> const& camera(size_t i, Camera<Pixel> const& c) {
    cameras()[i] = c;
    return camera(i);
  }
  
  /*!
   * @brief 取得offset
   */
  Vector3D<double> const& offset() const {
    return self->offset_;
  }
  
  /*!
   * @brief 取得offset (non-constant reference)
   */
  Vector3D<double>& offset() {
    return self()->offset_;
  }
  
  /*!
   * @brief 設定offset
   */
  Vector3D<double> const& offset(Vector3D<double> const& ofs) {
    self()->offset_ = ofs;
    return offset();
  }
  
  /*!
   * @brief 取得底片color
   */
  Pixel color(Vector3D<double> p) const {
    Vector3D<double> p2(p - offset());
    Pixel sum(0);
    double ct = 0;
    for (size_t i = 0, I = cameraSize(); i < I; ++i) {
      if (camera(i).inside(p2)) {
        sum = sum + camera(i).color(p2);
        ++ct;
      }
    }
    return (ct > 0 ? sum / ct : sum);
  }
  
  /*!
   * @brief 輸出展開圖
   *
   * @param [in] radius 半徑
   */
  Bitmap<Pixel> expand(double radius) const {
    radius = std::max(radius, 0.5);
    size_t height = std::max<size_t>(1, 2.0 * radius);
    size_t width  = 2.0* PI * radius;
    Bitmap<Pixel> ret(height, width, Pixel(0));
    for (size_t i = 0; i < height; ++i) {
      for (size_t j = 0; j < width; ++j) {
        double theta = (1.0 * j /  width - 0.5) * 2 * PI;
        double phi   = asin(-(1.0 * i / height - 0.5) * 2.0);
        ret.pixel(i, j, color(Vector3D<double>(
              sin(-theta) * cos(phi),
              sin(phi),
              cos(-theta) * cos(phi)
        )));
      }
    }
    return ret;
  }
  
  /*!
   * @brief same as \c copyFrom(b)
   */
  WatchBall& operator=(WatchBall const& b) {
    return copyFrom(b);
  }
  
  /*! @brief 將資料寫入檔案
   *
   *  @note 未完成
   */
  bool write(FILE* f, bool bin, unsigned int fg) const {
    return false;
  }
  
  /*! @brief 將資料讀入
   *
   *  @note 未完成
   */
  bool read(FILE* f, bool bin, unsigned int fg) {
    return false;
  }

  /*! @brief new一個自己
   *
   *  @return 一個new出來的pointer
   */
  ObjBase* create() const {
    return new WatchBall();
  }
  
  /*!
   * @brief 複製資料
   *
   * 輸入型別是 \c ObjBase \c const*
   * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom
   *
   *  @param [in] b 資料來源
   *  @return this
   */
  ObjBase* copyFrom(ObjBase const* b) {
    return &(copyFrom(*(WatchBall*)b));
  }
  
  /*! @brief 回傳class的type
   *
   *  @return \c char \c const\c * 形式的typename
   */
  char const* ctype() const{
    static char const* ptr = typeid(*this).name();
    return ptr;
  }
  
  /*! @brief 回傳class的type
   *
   *  @return \c std::string 形式的typename
   */
  std::string type() const {
    return std::string(ctype());
  }
};

}

#endif // gra_WatchBall_H__