VTM数据结构1

VTM数据结构学习1



前言

VTM的基本数据结构 Size,Position,Area
这些数据信息对二维信息进行了基本的描述,提供了宽,高,以及位置坐标信息。这些信息在CommonLib/Common.h中定义

提示:以下是本篇文章正文内容,下面案例可供参考

一、Position

简介:Position表示了位置坐标
VTM对Position的定义:

typedef int PosType;
typedef uint32_t SizeType; //typedef unsigned int uint32_t
struct Position
{
  PosType x;//位置坐标
  PosType y;

  Position()                                   : x(0),  y(0)  { } //默认构造函数
  Position(const PosType _x, const PosType _y) : x(_x), y(_y) { }  //有参构造函数,初始化

  bool operator!=(const Position &other)  const { return x != other.x || y != other.y; }  //重载!=
  bool operator==(const Position &other)  const { return x == other.x && y == other.y; }  //重载==

  Position offset(const Position pos)                 const { return Position(x + pos.x, y + pos.y); } //增加偏移量,传入的是Position实例化的对象
  Position offset(const PosType _x, const PosType _y) const { return Position(x + _x   , y + _y   ); }   //增加偏移量,传入的是两个int数据
  void     repositionTo(const Position newPos)              { x  = newPos.x; y  = newPos.y; }  //重新赋值坐标信息(相当于更新)
  void     relativeTo  (const Position origin)              { x -= origin.x; y -= origin.y; }   //减去一个位置信息

  Position operator-( const Position &other )         const { return{ x - other.x, y - other.y }; }  //重载-
};

二、Size

简介:Size提供一个矩形块的宽高信息
VTM对Size的定义:


//typedef unsigned int uint32_t
//typedef uint32_t SizeType
//相当于SizeType是unsigned int型数据
struct Size 
{
  SizeType width; //宽
  SizeType height; //高

  Size()                                              : width(0),      height(0)       { }   //默认构造函数
  Size(const SizeType _width, const SizeType _height) : width(_width), height(_height) { }    //有参构造函数

  bool operator!=(const Size &other)      const { return (width != other.width) || (height != other.height); }   //重载!=
  bool operator==(const Size &other)      const { return (width == other.width) && (height == other.height); }    //重载==
  uint32_t area()                             const { return (uint32_t) width * (uint32_t) height; }   //计算次区域的面积并返回
#if REUSE_CU_RESULTS_WITH_MULTIPLE_TUS
  void resizeTo(const Size newSize)             { width = newSize.width; height = newSize.height; }
#endif
};

三、Area

简介:Area继承于Position和Size
VTM对Area的定义:


struct Area : public Position, public Size
{
  Area()                                                                         : Position(),       Size()       { } //默认构造
  Area(const Position &_pos, const Size &_size)                                  : Position(_pos),   Size(_size)  { } //有参构造,用对象初始化
  Area(const PosType _x, const PosType _y, const SizeType _w, const SizeType _h) : Position(_x, _y), Size(_w, _h) { }  //有参构造,用数据进行初始化
//返回对象的信息,用到了类型转换,将子类对象转换为弗列对象
        Position& pos()                           { return *this; }
  const Position& pos()                     const { return *this; }
        Size&     size()                          { return *this; }
  const Size&     size()                    const { return *this; }
//返回不同位置的坐标信息
  const Position& topLeft()                 const { return *this; } //左上角
        Position  topRight()                const { return { (PosType) (x + width - 1), y                          }; }  //右上角
        Position  bottomLeft()              const { return { x                        , (PosType) (y + height - 1) }; }  //左下角
        Position  bottomRight()             const { return { (PosType) (x + width - 1), (PosType) (y + height - 1) }; }   //右下角
        Position  center()                  const { return { (PosType) (x + width / 2), (PosType) (y + height / 2) }; }   //中心位置

  bool contains(const Position &_pos)       const { return (_pos.x >= x) && (_pos.x < (x + width)) && (_pos.y >= y) && (_pos.y < (y + height)); }  //确定包含的关系
  bool contains(const Area &_area)          const { return contains(_area.pos()) && contains(_area.bottomRight()); }  

  bool operator!=(const Area &other)        const { return (Size::operator!=(other)) || (Position::operator!=(other)); }  //重载!=
  bool operator==(const Area &other)        const { return (Size::operator==(other)) && (Position::operator==(other)); }   //重载==
};

总结

Position ,Size 和Area是最基本的二维数据结构,它们会作为父类,后续的CompArea以及UnitArea会继承它们,来表示分量的二维信息

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值