以下是一个可能的欧拉角类实现:

template<typename T>
class EulerAngle {
public:
    EulerAngle() : pitch(0), yaw(0), roll(0), order("xyz") {}
    EulerAngle(T pitch, T yaw, T roll, const std::string& order = "xyz") :
        pitch(pitch), yaw(yaw), roll(roll), order(order) {}
    
    // Getters and setters
    T getPitch() const { return pitch; }
    T getYaw() const { return yaw; }
    T getRoll() const { return roll; }
    void setPitch(T pitch) { this->pitch = pitch; }
    void setYaw(T yaw) { this->yaw = yaw; }
    void setRoll(T roll) { this->roll = roll; }
    void setOrder(const std::string& order) { this->order = order; }
    
    // Left multiplication with 4x4 matrix
    Matrix4<T> operator*(const Matrix4<T>& m) const {
        Matrix4<T> result = m;
        const std::array<int, 3> axis = getAxisIndices(order);
        result = rotate(result, axis[0], pitch);
        result = rotate(result, axis[1], yaw);
        result = rotate(result, axis[2], roll);
        return result;
    }
    
private:
    T pitch, yaw, roll;
    std::string order;
    
    // Utility function to get the indices of the axes based on order
    std::array<int, 3> getAxisIndices(const std::string& order) const {
        std::array<int, 3> indices = {0, 1, 2};
        for (int i = 0; i < 3; i++) {
            if (order[i] == 'x') {
                indices[i] = 0;
            } else if (order[i] == 'y') {
                indices[i] = 1;
            } else if (order[i] == 'z') {
                indices[i] = 2;
            } else {
                throw std::invalid_argument("Invalid order string");
            }
        }
        return indices;
    }
    
    // Utility function to perform a rotation around an axis in the matrix
    Matrix4<T> rotate(const Matrix4<T>& m, int axisIndex, T angle) const {
        Matrix4<T> result = m;
        const T c = std::cos(angle);
        const T s = std::sin(angle);
        if (axisIndex == 0) {
            result *= Matrix4<T>::makeRotationX(c, s);
        } else if (axisIndex == 1) {
            result *= Matrix4<T>::makeRotationY(c, s);
        } else if (axisIndex == 2) {
            result *= Matrix4<T>::makeRotationZ(c, s);
        }
        return result;
    }
};

这个类包括了欧拉角的三个分量,以及旋转顺序(默认为“xyz”)。它还提供了一个重载的乘法运算符,可以对一个4x4矩阵进行左乘转换。转换的具体实现是通过按照给定的旋转顺序,依次对矩阵进行绕x、y、z轴的旋转得到的。其中,每次旋转使用了一个静态函数,可以生成对应的旋转矩阵。这个静态函数的实现可以参考以下代码:

template<typename T>
static Matrix4<T> makeRotationX(T c, T s) {
    Matrix4<T> m;
    m[1][1] = c;
    m[1][2] = s;
    m[2][1] = -s;
    m[2][2] = c;
    return m;
}

template<typename T>
static Matrix4<T> makeRotationY(T c, T s) {
    Matrix4<T> m;
    m[0][0] = c;
    m[0][2] = -s;
    m[2][0] = s;
    m[2][2] = c;
    return m;
}

template<typename T>
static Matrix4<T> makeRotationZ(T c, T s) {
    Matrix4<T> m;
    m[0][0] = c;
    m[0][1] = s;
    m[1][0] = -s;
    m[1][1] = c;
    return m;
}

这些静态函数可以生成对应轴的旋转矩阵,用于对矩阵进行旋转操作。

请写一个欧拉角类要求:C++模板类分量是xyz支持设置旋转顺序支持左乘4x4矩阵转换。

原文地址: https://www.cveoy.top/t/topic/7G3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录