aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/Camera.h
blob: 8682ad1aa952a509bfa4a6f3321960a295a6afa4 (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
256
257
258
259
260
261
262
#ifndef   gra_Camera_H__
#define   gra_Camera_H__

#include "Photo.h"
#include "IdentityPoints.h"
#include "../Self.h"
#include "../math/utility.h"
#include "../math/LinearTransformations.h"
#include "../math/methods.h"
#include "../oo/ObjBase.h"

namespace meow {

/*!
 * @brief Camera
 *
 * 實際上就是一個 \c Photo 加上一個 \c Rotation3D.
 * 另外附有 fixedPoint, 可以用來定位時參考
 *
 * @author cat_leopard
 */
template<class Pixel>
class Camera: public ObjBase {
public:
  typedef IdentityPoints<int, double> FixedPoints2D;
private:
  struct Myself {
    Photo<Pixel>         photo_;
    Rotation3D<double>     rot_;
    FixedPoints2D      fixed2D_;

    Myself() {
      fixed2D_.dimension(2);
    }
    
    Myself(Myself const& v):
    photo_(v.photo_), rot_(v.rot_), fixed2D_(v.fixed2D_) {
    }
    
    ~Myself() {
    }
  };

  Self<Myself> const self;
public:
  /*!
   * @brief constructor
   */
  Camera(): self() {
  }

  /*!
   * @brief copy constructor
   */
  Camera(Camera const& b): self(b.self, Self<Myself>::COPY_FROM) {
  }

  /*!
   * @brief destructor
   */
  ~Camera() {
  }

  /*!
   * @brief 複製資料
   */
  Camera& copyFrom(Camera const& b) {
    self().copyFrom(b.self);
    return *this;
  }

  /*!
   * @brief 參照
   */
  Camera& referenceFrom(Camera const& b) {
    self().referenceFrom(b.self);
    return *this;
  }

  /*!
   * @brief 取得 photo
   */
  Photo<Pixel> photo() const {
    return self->photo_;
  }

  /*!
   * @brief 取得 photo (non-constant)
   */
  Photo<Pixel>& photoGet() {
    return self()->photo_;
  }

  /*!
   * @brief 設定 photo
   */
  Photo<Pixel> photo(Photo<Pixel> const& pho) {
    self()->photo_.copyFrom(pho);
    return photo();
  }

  /*!
   * @brief 取得rotation
   */
  Rotation3D<double> rotation() const {
    return self->rot_;
  }

  /*!
   * @brief 取得rotation (non-constant)
   */
  Rotation3D<double>& rotationGet() {
    return self()->rot_;
  }

  /*!
   * @brief 設定rotation
   */
  Rotation3D<double> rotation(Rotation3D<double> const& rot) {
    self()->rot_.copyFrom(rot);
    return rotation();
  }

  /*!
   * @brief 取得所有FixedPoint
   */
  FixedPoints2D fixedPoints2D() const {
    return self->fixed2D_;
  }

  /*!
   * @brief 取得所有FixedPoint(non-constant reference)
   */
  FixedPoints2D& fixedPoints2DGet() const {
    return self()->fixed2D_;
  }

  /*!
   * @brief 設定FixedPoint
   */
  FixedPoints2D fixedPoints2D(FixedPoints2D const& fps2d) const {
    if (fps2d.dimension() == 2) {
      self()->fixed2D_.copyFrom(fps2d);
    }
    return fixedPoints2D();
  }

  /*!
   * @brief 取得編號為i的fixed points 2d
   */
  Vector<double> fixedPoint2D(int i) {
    return self->fixed2D_.identityPoint(i);
  }

  /*!
   * @brief 詢問某點是否在底片範圍內
   */
  bool inside(Vector3D<double> p) const {
    return self->photo_.inside(
      Vector3D<double>(rotation().transformate(p.matrix())));
  }

  /*!
   * @brief 取得底片color
   */
  Pixel color(Vector3D<double> p) const {
    return self->photo_.color(
      Vector3D<double>(rotation().transformate(p.matrix())));
  }

  /*!
   * @brief same as \c copyFrom(b)
   */
  Camera& operator=(Camera const& b) {
    return copyFrom(b);
  }

  /*! @brief 將資料寫入檔案
   *
   *  @note 未完成
   */
  bool write(FILE* f, bool bin, unsigned int fg) const {
    if (bin) {
      double tmp;
      for (size_t i = 0; i < 3; ++i) {
        if (fwrite(&(tmp = rotation().theta(i)), sizeof(tmp), 1, f) < 1)
          return false;
      }
    }
    else {
      for (size_t i = 0; i < 3; ++i) {
        if (fprintf(f, "%f ", rotation().theta(i)) < 1) return false;
      }
      fprintf(f, "\n");
    }
    return (fixedPoints2D().write(f, bin, fg) && photo().write(f, bin, fg));
  }

  /*! @brief 將資料讀入
   *
   *  @note 未完成
   */
  bool read(FILE* f, bool bin, unsigned int fg) {
    if (bin) {
      double tmp;
      for (size_t i = 0; i < 3; ++i) {
        if (fread(&tmp, sizeof(tmp), 1, f) < 1) {
          return false;
        }
        rotationGet().theta(i, tmp);
      }
    }
    else {
      double a;
      for (size_t i = 0; i < 3; ++i) {
        if (fscanf(f, "%lf", &a) < 1) return false;
        rotationGet().theta(i, a);
      }
    }
    return (fixedPoints2DGet().read(f, bin, fg) && photoGet().read(f, bin, fg));
  }

  /*! @brief new一個自己
   *
   *  @return 一個new出來的pointer
   */
  ObjBase* create() const {
    return new Camera();
  }

  /*! @brief 複製資料
   *
   * 輸入型別是 \c ObjBase \c const*
   * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom
   *
   *  @param [in] b 資料來源
   *  @return this
   */
  ObjBase* copyFrom(ObjBase const* b) {
    return &(copyFrom(*(Camera*)b));
  }

  /*! @brief 回傳class的type
   *
   *  @return \c char \c const\c * 形式的typename
   */
  char const* ctype() const{
    return typeid(*this).name();
  }

  /*! @brief 回傳class的type
   *
   *  @return \c std::string 形式的typename
   */
  std::string type() const {
    return std::string(ctype());
  }
};

} // meow

#endif // gra_Camera_H__