在上一小节中,我们完成了对BMPImage类的构建,成功实现了我们这个小小引擎的图像输出功能。

你已经完成了图像输出了,接着就开始路径追踪吧。。。

开个玩笑XD

对于曾经学习过一些图形学经典教材的人来说,下一步应当开始着手于画线算法了,但对于本文来说,肯定是要走一些不走寻常路的

所谓万事开头难,我决定先打好地基。编写一个鲁棒性,扩展性还不错的向量类。

由于我们不打算借助外部库,所以,适当的设计是必要的。在这里,我打算借鉴一下pbrt-v4的设计。

也就是利用模板基类来定制我们的数据结构。

这么做的好处是,我们可以按照元素数量划分父类

本章目标

构建基本的数据结构,包括点和向量。

需求

  • 类型可扩展,鲁棒性还可以
  • 易于使用

实现

class Tuple2
/**
* \brief every element's parent class who has two parameter
* \tparam Child a template class contains one template parameter
* \tparam T defined by the child's type of template parameter
*/
template <template <typename> class Child, typename T> class Tuple2
{
public:
static constexpr int nDimensions = 2; Tuple2() = default;
Tuple2(T x, T y) : x(x), y(y)
{
}
Tuple2(Child<T> c)
{
x = c.x;
y = c.y;
}
Child<T>& operator=(Child<T> c)
{
x = c.x;
y = c.y;
return static_cast<Child<T>&>(*this);
}
template <typename U> auto operator+(Child<U> c) const -> Child<decltype(T{} + U{})>
{
return {x + c.x, y + c.y};
}
template <typename U> Child<T>& operator+=(Child<U> c)
{
x += c.x;
y += c.y;
return static_cast<Child<T>&>(*this);
} template <typename U> auto operator-(Child<U> c) const -> Child<decltype(T{} - U{})>
{
return {x - c.x, y - c.y};
}
template <typename U> Child<T>& operator-=(Child<U> c)
{
x -= c.x;
y -= c.y;
return static_cast<Child<T>&>(*this);
} bool operator==(Child<T> c) const
{
return x == c.x && y == c.y;
} bool operator!=(Child<T> c) const
{
return x != c.x || y != c.y;
} template <typename U> auto operator*(U s) const -> Child<decltype(T{} * U{})>
{
return {s * x, s * y};
}
template <typename U> Child<T>& operator*=(U s)
{
x *= s;
y *= s;
return static_cast<Child<T>&>(*this);
}
template <typename U> Child<decltype(T{} / U{})> operator/(U d) const
{
VEC_CHECK(d != 0);
return {x / d, y / d};
} template <typename U> [[nodiscard]] Child<T>& operator/=(U d)
{
VEC_CHECK(d != 0);
x /= d;
y /= d;
return static_cast<Child<T>&>(*this);
} [[nodiscard]] Child<T> operator-() const
{
return {-x, -y};
} [[nodiscard]] T& operator[](const int i) const
{
VEC_CHECK(i >= 0 && i <= 1);
return (i == 0) ? x : y;
}
[[nodiscard]] T& operator[](const int i)
{
VEC_CHECK(i >= 0 && i <= 1);
return (i == 0) ? x : y;
}
[[nodiscard]] std::string toString() const
{
return std::to_string(x) + std::to_string(x);
}
T x{};
T y{};
};

在代码中,用到了一些C++的高级技巧,我将在下面一一解释给大家:

  1. VEC_CHECK()宏定义

    为了达成鲁棒性的需求,我在方法的定义中加入了仅在debug模式下生效的assert,同时封装进入宏变量中,提高了代码健壮性的同时,也不会影响性能。

  2. [[nodiscard]]声明

    该声明于C++17版本中加入,声明在使用该方法时,不应当遗弃返回值,否则会发出警告,在调试时,略有作用

  3. template <template <typename> class Child, typename T>

    作为Tuple2的模板参数声明,使用Child作为当基类进行继承时的模板特化

    这是一种二级的模板特化结构,子类对父类的继承仅指定了Child这一模板参数的值,如此实现,基类的模板方法会非常好写。

    而T只需要在编译时进行确定,就可以生成对应的类了。

    这是一种在C++中实现动态绑定的方式。

那么在Tuple2的基础上,再去实现Vec2和Point2就十分容易了。

// 派生类 Vec2,继承自 Tuple2
template <typename T> class Vec2 : public Tuple2<Vec2, T>
{
public:
using Tuple2<Vec2, T>::x;
using Tuple2<Vec2, T>::y; Vec2() : Tuple2<Vec2, T>()
{
}
Vec2(T x, T y) : Tuple2<Vec2, T>(x, y)
{
} void print() const
{
std::cout << "Vec2: (" << this->x << ", " << this->y << ")\n";
}
}; template <typename T> class Point2 : public Tuple2<Point2, T>
{
public:
using Tuple2<Point2, T>::x;
using Tuple2<Point2, T>::y; Point2() : Tuple2<Point2, T>()
{
}
Point2(T x, T y) : Tuple2<Point2, T>(x, y)
{
} void print() const
{
std::cout << "Point2: (" << this->x << ", " << this->y << ")\n";
}
};

同理,只需要把元素数量改成3个,我们就可以得到Tuple3以及其子类

// 基类模板 Tuple3
template <template <typename> class Child, typename T> class Tuple3
{
public:
static constexpr int nDimensions = 3; Tuple3() = default;
Tuple3(T x, T y, T z) : x(x), y(y), z(z)
{
}
Tuple3(Child<T> c)
{
x = c.x;
y = c.y;
z = c.z;
}
Child<T>& operator=(Child<T> c)
{
x = c.x;
y = c.y;
z = c.z;
return static_cast<Child<T>&>(*this);
} template <typename U> auto operator+(Child<U> c) const -> Child<decltype(T{} + U{})>
{
return {x + c.x, y + c.y, z + c.z};
} template <typename U> Child<T>& operator+=(Child<U> c)
{
x += c.x;
y += c.y;
z += c.z;
return static_cast<Child<T>&>(*this);
} template <typename U> auto operator-(Child<U> c) const -> Child<decltype(T{} - U{})>
{
return {x - c.x, y - c.y, z - c.z};
} template <typename U> Child<T>& operator-=(Child<U> c)
{
x -= c.x;
y -= c.y;
z -= c.z;
return static_cast<Child<T>&>(*this);
} bool operator==(Child<T> c) const
{
return x == c.x && y == c.y && z == c.z;
} bool operator!=(Child<T> c) const
{
return x != c.x || y != c.y || z != c.z;
} template <typename U> auto operator*(U s) const -> Child<decltype(T{} * U{})>
{
return {s * x, s * y, s * z};
} template <typename U> Child<T>& operator*=(U s)
{
x *= s;
y *= s;
z *= s;
return static_cast<Child<T>&>(*this);
} template <typename U> Child<decltype(T{} / U{})> operator/(U d) const
{
VEC_CHECK(d != 0);
return {x / d, y / d, z / d};
} template <typename U> [[nodiscard]] Child<T>& operator/=(U d)
{
VEC_CHECK(d != 0);
x /= d;
y /= d;
z /= d;
return static_cast<Child<T>&>(*this);
} [[nodiscard]] Child<T> operator-() const
{
return {-x, -y, -z};
} [[nodiscard]] T& operator[](const int i) const
{
VEC_CHECK(i >= 0 && i <= 2);
return (i == 0) ? x : (i == 1) ? y : z;
} [[nodiscard]] T& operator[](const int i)
{
VEC_CHECK(i >= 0 && i <= 2);
return (i == 0) ? x : (i == 1) ? y : z;
} [[nodiscard]] std::string toString() const
{
return std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z);
} T x{};
T y{};
T z{};
};
// 派生类 Vec3,继承自 Tuple3
template <typename T> class Vec3 : public Tuple3<Vec3, T>
{
public:
// 显式引入模板基类的属性
using Tuple3<Vec3, T>::x;
using Tuple3<Vec3, T>::y;
using Tuple3<Vec3, T>::z; Vec3() : Tuple3<Vec3, T>()
{
}
Vec3(T x, T y, T z) : Tuple3<Vec3, T>(x, y, z)
{
} void print() const
{
std::cout << "Vec3: (" << this->x << ", " << this->y << ", " << this->z << ")\n";
}
};
template <typename T> class Point3 : public Tuple3<Point3, T>
{
public:
using Tuple3<Point3, T>::x;
using Tuple3<Point3, T>::y;
using Tuple3<Point3, T>::z; Point3() : Tuple3<Point3, T>()
{
}
Point3(T x, T y, T z) : Tuple3<Point3, T>(x, y, z)
{
} void print() const
{
std::cout << "Point3: (" << this->x << ", " << this->y << ", " << this->z << ")\n";
}
};

Chapter1 p2 vec的更多相关文章

  1. OpenCASCADE Root-Finding Algorithm

    OpenCASCADE Root-Finding Algorithm eryar@163.com Abstract. A root-finding algorithm is a numerical m ...

  2. POJ3525 Most Distant Point from the Sea(半平面交)

    给你一个凸多边形,问在里面距离凸边形最远的点. 方法就是二分这个距离,然后将对应的半平面沿着法向平移这个距离,然后判断是否交集为空,为空说明这个距离太大了,否则太小了,二分即可. #pragma wa ...

  3. USACO3.41Closed Fences(几何)

    这题水的真不易..300多行 累死了 对着数据查错啊 枚举每个边上的点到源点 是否中间隔着别的边  每条边划分500份就够了  注意一下与源点在一条直线上的边不算 几何 啊,,好繁琐 参考各种模版.. ...

  4. bzoj 1185

    题目大意: 给你n个点求最小矩形覆盖. 思路:枚举凸包上的边然后,旋转卡壳找三个相应的为止把矩形的四个点求出来. #include<bits/stdc++.h> #define LL lo ...

  5. [Processing]点到线段的最小距离

    PVector p1,p2,n; float d = 0; void setup() { size(600,600); p1 = new PVector(150,30);//线段第一个端点 p2 = ...

  6. STL vector 内存释放

    最近在论坛看到一个提问帖子,问题是vector中存储了对象的指针,调用clear后这些指针如何删除? class Test { public: Test() {} ~Test() { cout < ...

  7. [C++] Copy Control (part 1)

    Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...

  8. [LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC

    [LOJ#6437][BZOJ5373]「PKUSC2018」PKUSC 试题描述 九条可怜是一个爱玩游戏的女孩子. 最近她在玩一个无双割草类的游戏,平面上有 \(n\) 个敌人,每一个敌人的坐标为 ...

  9. [BZOJ4920][Lydsy六月月赛]薄饼切割

    [BZOJ4920][Lydsy六月月赛]薄饼切割 试题描述 有一天,tangjz 送给了 quailty 一张薄饼,tangjz 将它放在了水平桌面上,从上面看下去,薄饼形成了一个 \(H \tim ...

  10. [AH2017/HNOI2017]影魔(主席树+单调栈)

    设\(l[i]\)为i左边第一个比i大的数的下标.\(r[i]\)为i右边第一个比i大的数的下标. 我们把\(p1,p2\)分开考虑. 当产生贡献为\(p1\)时\(i\)和\(j\)一定满足,分别为 ...

随机推荐

  1. 如何基于MaxCompute快速打通数据仓库和数据湖的湖仓一体实践

    简介: MaxCompute 是面向分析的企业级 SaaS 模式云数据仓库,以 Serverless 架构提供快速.全托管的在线数据仓库服务,消除了传统数据平台在资源扩展性和弹性方面的限制,最小化用户 ...

  2. WPF 已知问题 Popup 吃掉 PreviewMouseDown 事件

    在 WPF 中,使用 Popup 也许会看到 PreviewMouseDown 事件被吃掉 因为 PreviewMouseDown 是 RoutingStrategy.Direct 路由事件,不能在多 ...

  3. WPF 漂亮的现代化控件 新 ModernWPF 界面库

    这是一个在 GitHub 上完全开源的库,有十分漂亮的界面,整个都是 Win10 风,界面部分和默认 UWP 相近 这个库支持了 .NET Framework 4.5 和以上的版本,以及 .NET C ...

  4. 6.prometheus监控--监控redis/rabbitmq/mongodb

    1.监控redis 1.1 redis_exporter安装方式 1.1.1 二进制源码安装方式 参考nginx二进制安装方法 redis_exporter下载地址:https://github.co ...

  5. HAL库移植RT-Thread Nano

    一.移植RT-Thread Nano准备 keil软件 CubeMx软件 STM32 CubeMx使用教程:https://www.cnblogs.com/jzcn/p/16313803.html S ...

  6. 实验1 在MAX10 FPGA上实现组合逻辑

    实验1 在MAX10 FPGA上实现组合逻辑 实验前的准备工作:参照讲义步骤安装Quartus,Modelsim和System Builder.阅读材料:1)推荐的文件组织形式:2)Verilog 1 ...

  7. 都2024年了,你还不知道git worktree么?

    三年前 python 大佬吉多·范罗苏姆(为 Python 程序设计语言的最初设计者及主要架构师)才知道 git worktree ,我现在才知道,我觉得没啥丢人的. 应用场景 如果你正在 featu ...

  8. 开源相机管理库Aravis例程学习(六)——camera-features

    目录 简介 例程代码 函数说明 arv_camera_get_integer arv_camera_get_string 简介 本文针对官方例程中的:04-camera-features做简单的讲解. ...

  9. sqli-labs-master 第十一关

    本关为POST请求: 输入:admin'# 密码:随意 爆出当前数据库: 用户名:payload1:admin' and extractvalue(1,concat(0x7e,database(),0 ...

  10. ssh秘钥对免密码登陆

    准备两台linux服务器 a和b , 在a上使用ssh命令登陆b服务器 , 并且不用 输入密码 1.在a服务器上,比如是root用户 ,进去/root/.ssh目录 ,没有就创建, 就是进入家目录的. ...