[TinyRenderer] Chapter1 p2 vec

在上一小节中,我们完成了对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";
    }
};
posted @ 2024-06-12 22:59  qiyuewuyi2333  阅读(19)  评论(0编辑  收藏  举报