aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/gra/Eye.h
blob: 2621a16efbb033c6f8348b1f5cf695ce2099d197 (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
#ifndef   Eye_H__
#define   Eye_H__

#include "Camera.h"

#include "../Self.h"
#include "../oo/ObjBase.h"

namespace meow {

/*!
 * @brief 一個 \c Camera 加上一個offset transformation
 * 
 * @author cat_leopard
 */
template<class Pixel>
class Eye: public ObjBase {
private:
  struct Myself {
    Camera<Pixel>    cam_;
    Vector3D<double> ofs_;
    
    Myself(): cam_(), ofs_(0.0, 0.0, 0.0) {
    }
    
    Myself(Camera<Pixel> const& c, Vector3D<double> const& o): cam_(c), ofs_(o){
    }
    
    Myself(Myself const& b): cam_(b.cam_), ofs_(b.ofs_) {
    }
    
    ~Myself() {
    }
  };
  
  Self<Myself> const self;
public:
  Eye(): self() {
  }
  
  Eye(Eye const& b): self(b.self(), Self<Myself>::COPY_FROM) {
  }
  
  Eye(Camera<Pixel> const& c, Vector3D<double> const& o): self(Myself(c, o)) {
  }
  
  ~Eye() {
  }
  
  Eye& copyFrom(Eye const& e) {
    self().copyFrom(e.self);
    return *this;
  }
  
  Eye& referenceFrom(Eye const& e) {
    self().referenceFrom(e.self);
    return *this;
  }
  
  Camera<Pixel> const& camera() const {
    return self->cam_;
  }
  
  Camera<Pixel>& cameraGet() {
    return self()->cam_;
  }
  
  Camera<Pixel> const& camera(Camera<Pixel> const& c) {
    self()->cam_.copyFrom(c);
    return camera();
  }
  
  Vector3D<double> const& offset() const {
    return self->ofs_;
  }
  
  Vector3D<double>& offsetGet() {
    return self()->ofs_;
  }
  
  Vector3D<double> const& offset(Vector3D<double> const& ofs) {
    self()->ofs_ = ofs;
    return offset();
  }
  
  bool inside(Vector3D<double> const& v) const {
    return camera().inside(v - offset());
  }
  
  Eye& operator=(Eye const& e) {
    return copyFrom(e);
  }
  
  /*! @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 = offset()(i)), sizeof(tmp), 1, f) < 1)
          return false;
      }
    }
    else {
      for (size_t i = 0; i < 3; ++i) {
        if (fprintf(f, "%f ", offset()(i)) < 1) return false;
      }
      fprintf(f, "\n");
    }
    return camera().write(f, bin, fg);
  }

  /*! @brief 將資料讀入
   *
   *  @note 未完成
   */
  bool read(FILE* f, bool bin, unsigned int fg) {
    if (bin) {
      double tmp[3];
      if (fread(tmp, sizeof(double), 3, f) < 3) return false;
      offsetGet().xyz(tmp[0], tmp[1], tmp[2]);
    }
    else {
      double a, b, c;
      if (fscanf(f, "%lf %lf %lf", &a, &b, &c) < 3) return false;
      offsetGet().x(a);
      offsetGet().y(b);
      offsetGet().z(c);
    }
    return cameraGet().read(f, bin, fg);
  }

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

  /*! @brief 複製資料
   *
   * 輸入型別是 \c ObjBase \c const*
   * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom
   *
   *  @param [in] b 資料來源
   *  @return this
   */
  ObjBase* copyFrom(ObjBase const* b) {
    return &(copyFrom(*(Eye*)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 // Eye_H__