#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 Eye: public ObjBase { private: struct Myself { Camera cam_; Vector3D ofs_; Myself(): cam_(), ofs_(0.0, 0.0, 0.0) { } Myself(Camera const& c, Vector3D const& o): cam_(c), ofs_(o){ } Myself(Myself const& b): cam_(b.cam_), ofs_(b.ofs_) { } ~Myself() { } }; Self const self; public: Eye(): self() { } Eye(Eye const& b): self(b.self(), Self::COPY_FROM) { } Eye(Camera const& c, Vector3D 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 const& camera() const { return self->cam_; } Camera& cameraGet() { return self()->cam_; } Camera const& camera(Camera const& c) { self()->cam_.copyFrom(c); return camera(); } Vector3D const& offset() const { return self->ofs_; } Vector3D& offsetGet() { return self()->ofs_; } Vector3D const& offset(Vector3D const& ofs) { self()->ofs_ = ofs; return offset(); } bool inside(Vector3D 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__