diff options
author | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-01 13:56:57 +0800 |
---|---|---|
committer | cathook <b01902109@csie.ntu.edu.tw> | 2014-06-01 13:56:57 +0800 |
commit | d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0 (patch) | |
tree | 16f7920c5079e0aefcf9509d2dbab59c464d42bd /meowpp/gra/IdentityPoints.h | |
parent | bd58f63900410ec4764031f2e6de2d75e91434b3 (diff) | |
download | meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.gz meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.bz2 meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.lz meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.xz meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.tar.zst meow-d5052f1c296dddf51b3e83d59bf3e3c1952cb2d0.zip |
big chnage
Diffstat (limited to 'meowpp/gra/IdentityPoints.h')
-rw-r--r-- | meowpp/gra/IdentityPoints.h | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/meowpp/gra/IdentityPoints.h b/meowpp/gra/IdentityPoints.h new file mode 100644 index 0000000..c33eb55 --- /dev/null +++ b/meowpp/gra/IdentityPoints.h @@ -0,0 +1,279 @@ +#ifndef gra_IdentityPoints_H__ +#define gra_IdentityPoints_H__ + +#include "../Self.h" + +#include "../math/Vector.h" + +#include "../oo/ObjBase.h" + +#include <map> + +#include <cstdlib> + +namespace meow { + +/*! + * @brief 把一個 \c std::map<ID,Vector<Scalar> > 包起來 + * + * @author cat_leopard + */ +template<class ID, class Scalar> +class IdentityPoints: public ObjBase { +public: + typedef typename std::map<ID, Vector<Scalar> > IdentityPointsMap; + typedef typename IdentityPointsMap:: iterator IdentityPointsMapIter; + typedef typename IdentityPointsMap::const_iterator IdentityPointsMapIterK; + +private: + struct Myself { + IdentityPointsMap points_; + size_t dimension_; + + Myself() { + dimension_ = 1; + } + ~Myself() { + } + Myself& copyFrom(Myself const& b) { + points_ = b.points_; + dimension_ = b.dimension_; + return *this; + } + }; + + Self<Myself> const self; +public: + /*! + * @brief constructor + */ + IdentityPoints(): self(true) { + } + + /*! + * @brief constructor, 並且複製資料 + */ + IdentityPoints(IdentityPoints const& b): self(false) { + copyFrom(b); + } + + /*! + * @brief destructor + */ + ~IdentityPoints() { + } + + /*! + * @brief 複製資料 + */ + IdentityPoints& copyFrom(IdentityPoints const& b) { + self().copyFrom(b.self); + return *this; + } + + /*! + * @brief 參照 + */ + IdentityPoints& referenceFrom(IdentityPoints const& b) { + self().referenceFrom(b.self); + return *this; + } + + /*! + * @brief 清除一切identity points + */ + void clear() { + self()->points_.clear(); + } + + /*! + * @brief 回傳有幾個identity points + */ + size_t size() const { + return self->points_.size(); + } + + /*! + * @brief 回傳是否沒有identity points + */ + bool empty() const { + return (size() == 0u); + } + + /*! + * @brief 檢查某id是否有使用 + */ + bool exist(ID const& id) const { + return (self->points_.find(id) != self->points_.end()); + } + + /*! + * @brief 回傳dimension + */ + size_t dimension() const { + return self->dimension_; + } + + /*! + * @brief 設定dimension, 並且清空資料 + */ + size_t dimension(size_t dim) { + self()->dimension_ = dim; + clear(); + return dimension(); + } + + /*! + * @brief 設定dimension, 並且針對每個identity point指定重設dimension + */ + size_t dimension(size_t dim, Scalar const& init_value) { + self()->dimension_ = dim; + for (IdentityPointsMapIter + it = self()->points_.begin(); it != self()->points_.end(); ++it) { + it.second.dimension(dim, init_value); + } + return dimension(); + } + + /*! + * @brief 取得所有identity points + */ + IdentityPointsMap const& identityPoints() const { + return self->points_; + } + + /*! + * @brief 設定所有identity points + */ + IdentityPointsMap const& identityPoints(IdentityPointsMap const& points) { + clear(); + return identityPointsAdd(points); + } + + /*! + * @brief 加入identity Points + */ + IdentityPointsMap const& identityPointsAdd(IdentityPointsMap const& points) { + for (IdentityPointsMapIterK it = points.begin(); it != points.end(); ++it) { + identityPointAdd(it.first, it.second); + } + return identityPoints(); + } + + /*! + * @brief 移除identity Points + */ + IdentityPointsMap const& identityPointsDel(std::set<ID> const& ids) { + for (typename std::set<ID>::const_iterator + it = ids.begin(); it != ids.end(); ++it) { + identityPointDel(*it); + } + return identityPoints(); + } + + /*! + * @brief 取得一個identity point + */ + Vector<Scalar> identityPoint(ID const& id) const { + return (exist(id) ? self->points_.find(id)->second : Vector<Scalar>()); + } + + /*! + * @brief 修改一個identity point + */ + Vector<Scalar> identityPoint(ID const& id, Vector<Scalar> const& b) { + if (b.dimension() == self->dimension_ && exist(id)) { + self()->points_[id].copyFrom(b); + } + return identityPoint(id); + } + + /*! + * @brief 新增一個identity point + */ + Vector<Scalar> identityPointAdd(ID const& id, Vector<Scalar> const& b) { + if (b.dimension() == self->dimension_ && !exist(id)) { + self()->points_[id].copyFrom(b); + } + return identityPoint(id); + } + + /*! + * @brief 刪除一個identity point + */ + void identityPointDel(ID const& id) { + self()->points_.erase(id); + } + + /*! + * @brief 取得一個identity point, non-constant reference + */ + Vector<Scalar>& identityPointGet(ID const& id) { + return self()->points_[id]; + } + + /*! + * @brief same as \c copyFrom(b) + */ + IdentityPoints& operator=(IdentityPoints 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出來的Bitmap<Pixel> + */ + ObjBase* create() const { + return new IdentityPoints(); + } + + /*! @brief 複製資料 + * + * 輸入型別是 \c ObjBase \c const* + * 這裡假設實體其實是 \c Bitmap. + * 事實上這個method就只是幫忙轉型然後呼叫原本的\c copyFrom + * + * @param [in] b 資料來源 + * @return this + */ + ObjBase* copyFrom(ObjBase const* b) { + return &(copyFrom(*(IdentityPoints*)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_IdentityPoints_H__ |