Итак, у меня есть этот код здесь для моей функции Makeuler < /p>
Matrix3 Matrix3::MakeEuler(float pitch, float yaw, float roll) // Create a matrix from Euler angles (pitch = X, yaw = Y, roll = Z)
{
float cx = cosf(pitch);
float sx = sinf(pitch);
float cy = cosf(yaw);
float sy = sinf(yaw);
float cz = cosf(roll);
float sz = sinf(roll);
Matrix3 result;
result.m1 = cy * cz;
result.m2 = cz * sx * sy - cx * sz;
result.m3 = sx * sz + cx * cz * sy;
result.m4 = cy * sz;
result.m5 = cx * cz + sx * sy * sz;
result.m6 = cx * sy * sz - cz * sx;
result.m7 = -sy;
result.m8 = cy * sx;
result.m9 = cx * cy;
// Sync to array if needed
result.data[0] = result.m1;
result.data[1] = result.m2;
result.data[2] = result.m3;
result.data[3] = result.m4;
result.data[4] = result.m5;
result.data[5] = result.m6;
result.data[6] = result.m7;
result.data[7] = result.m8;
result.data[8] = result.m9;
return result;
}
< /code>
Но он не проходит модульный тест, всегда сбой из -за чего -то ... < /p>
Мне действительно нужна помощь, чтобы решить это быстро, чтобы я мог перейти к исправлению других модульных тестов.#pragma once
#include "Vector3.h" // including this for Vector operation
#include
namespace MathClasses {
class Matrix3 {
public:
float data[9];
float mm[3][3];
float m1 = 0;
float m2 = 0;
float m3 = 0;
float m4 = 0;
float m5 = 0;
float m6 = 0;
float m7 = 0;
float m8 = 0;
float m9 = 0;
Matrix3();
Matrix3(const float* values);
Matrix3(float m1_Input, float m2_Input, float m3_Input,
float m4_Input, float m5_Input, float m6_Input,
float m7_Input, float m8_Input, float m9_Input);
//operators
Matrix3 operator*(const Matrix3& other) const;
Vector3 operator*(const Vector3& other) const;
Matrix3 Transposed() const;
void setXRotation(float radian);
void setYRotation(float radian);
void setZRotation(float radian);
void setTranslate(float x, float y);
float& operator[](int index);
const float& operator[](int index) const;
operator float* ();
operator const float* () const;
static Matrix3 MakeTranslation(float x, float y, float z);
static Matrix3 MakeTranslation(const Vector3& v);
static Matrix3 MakeRotateX(float radian);
static Matrix3 MakeRotateY(float radian);
static Matrix3 MakeRotateZ(float radian);
static Matrix3 MakeIdentity();
static Matrix3 MakeEuler(float pitch, float yaw, float roll);
static Matrix3 MakeEuler(const Vector3& eulerAngles);
static Matrix3 MakeScale(float sx, float sy);
static Matrix3 MakeScale(float sx, float sy, float sz);
static Matrix3 MakeScale(const Vector3& scale);
std::string ToString() const;
Matrix3 transpose() const;
bool operator==(const Matrix3& rhs) const;
bool operator!=(const Matrix3& rhs) const;
};
}
< /code>
matrix3.cpp
#include "Matrix3.h"
#include
#include
#include
namespace MathClasses {
Matrix3::Matrix3() { // sets every part of the matrix to 0
for (int i = 0; i < 9; i++) {
data = 0;
}
m1 = data[0];
m2 = data[1];
m3 = data[2];
m4 = data[3];
m5 = data[4];
m6 = data[5];
m7 = data[6];
m8 = data[7];
m9 = data[8];
}
Matrix3::Matrix3(const float* values) { // sets matrix from float array
for (int i = 0; i < 9; i++) {
data = values;
}
m1 = data[0];
m2 = data[1];
m3 = data[2];
m4 = data[3];
m5 = data[4];
m6 = data[5];
m7 = data[6];
m8 = data[7];
m9 = data[8];
}
Matrix3::Matrix3(float m1_Input, float m2_Input, float m3_Input, float m4_Input, float m5_Input, float m6_Input, float m7_Input, float m8_Input, float m9_Input) { // sets each part of the matrix manually
data[0] = m1_Input;
data[1] = m2_Input;
data[2] = m3_Input;
data[3] = m4_Input;
data[4] = m5_Input;
data[5] = m6_Input;
data[6] = m7_Input;
data[7] = m8_Input;
data[8] = m9_Input;
m1 = m1_Input;
m2 = m2_Input;
m3 = m3_Input;
m4 = m4_Input;
m5 = m5_Input;
m6 = m6_Input;
m7 = m7_Input;
m8 = m8_Input;
m9 = m9_Input;
}
Matrix3 Matrix3::operator*(const Matrix3& other) const // Matrix multiplication (Matrix3 * Matrix3)
{
Matrix3 result;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
float sum = 0.0f;
for (int k = 0; k < 3; ++k) {
// Column-major layout access
sum += data[k * 3 + row] * other.data[col * 3 + k];
}
result.data[col * 3 + row] = sum; // Store result in column-major
}
}
// Sync to m1 to m9
result.m1 = result.data[0];
result.m2 = result.data[1];
result.m3 = result.data[2];
result.m4 = result.data[3];
result.m5 = result.data[4];
result.m6 = result.data[5];
result.m7 = result.data[6];
result.m8 = result.data[7];
result.m9 = result.data[8];
return result;
}
Vector3 Matrix3::operator*(const Vector3& other) const // Matrix-vector multiplication (Matrix3 * Vector3)
{
float x = data[0] * other.x + data[3] * other.y + data[6] * other.z;
float y = data[1] * other.x + data[4] * other.y + data[7] * other.z;
float z = data[2] * other.x + data[5] * other.y + data[8] * other.z;
return Vector3(x, y, z);
}
Matrix3 Matrix3::Transposed() const // Transpose the matrix (swap rows and columns)
{
return Matrix3{
m1, m4, m7,
m2, m5, m8,
m3, m6, m9
};
}
void Matrix3::setXRotation(float radian) // sets the x rotation
{
float c = cosf(radian);
float s = sinf(radian);
*this = Matrix3(1, 0, 0,
0, c, -s,
0, s, c);
}
void Matrix3::setYRotation(float radian) // sets the y rotation
{
float c = cosf(radian);
float s = sinf(radian);
*this = Matrix3(c, 0, s,
0, 1, 0,
-s, 0, c);
}
void Matrix3::setZRotation(float radian) // sets the z axis rotation
{
float c = cosf(radian);
float s = sinf(radian);
*this = Matrix3(c, -s, 0,
s, c, 0,
0, 0, 1);
}
void Matrix3::setTranslate(float x, float y) // sets the matrix to a 2D translation matrix
{
*this = MakeIdentity();
data[6] = x;
data[7] = y;
}
float& Matrix3::operator[](int index) // gets the value of the data in x position in the matrix array
{
return data[index];
}
const float& Matrix3::operator[](int index) const // gets the value of the data in x position in the matrix array
{
return data[index];
}
Matrix3::operator float* () { // casts to a float pointer
return data;
}
Matrix3::operator const float* () const { // casts to a float pointer
return data;
}
Matrix3 Matrix3::MakeTranslation(float x, float y, float z)
{
Matrix3 result = MakeIdentity();
result.data[6] = x;
result.data[7] = y;
result.data[8] = z;
return result;
}
Matrix3 Matrix3::MakeTranslation(const Vector3& v) // creates a translation matrix
{
return MakeTranslation(v.x, v.y, v.z);
}
Matrix3 Matrix3::MakeRotateX(float radian)
{
Matrix3 result;
result.setXRotation(radian);
return result;
}
Matrix3 Matrix3::MakeRotateY(float radian)
{
Matrix3 result;
result.setYRotation(radian);
return result;
}
Matrix3 Matrix3::MakeRotateZ(float radian)
{
Matrix3 result;
result.setZRotation(radian);
return result;
}
Matrix3 Matrix3::MakeIdentity() { // Create an identity matrix
return Matrix3(
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f);
}
Matrix3 Matrix3::MakeEuler(float pitch, float yaw, float roll) // Create a matrix from Euler angles (pitch = X, yaw = Y, roll = Z)
{
float cx = cosf(pitch);
float sx = sinf(pitch);
float cy = cosf(yaw);
float sy = sinf(yaw);
float cz = cosf(roll);
float sz = sinf(roll);
Matrix3 result;
result.m1 = cy * cz;
result.m2 = cz * sx * sy - cx * sz;
result.m3 = sx * sz + cx * cz * sy;
result.m4 = cy * sz;
result.m5 = cx * cz + sx * sy * sz;
result.m6 = cx * sy * sz - cz * sx;
result.m7 = -sy;
result.m8 = cy * sx;
result.m9 = cx * cy;
// Sync to array if needed
result.data[0] = result.m1;
result.data[1] = result.m2;
result.data[2] = result.m3;
result.data[3] = result.m4;
result.data[4] = result.m5;
result.data[5] = result.m6;
result.data[6] = result.m7;
result.data[7] = result.m8;
result.data[8] = result.m9;
return result;
}
Matrix3 Matrix3::MakeEuler(const Vector3& eulerAngles) // Create a matrix from a vector of Euler angles
{
return MakeEuler(eulerAngles.x, eulerAngles.y, eulerAngles.z);
}
Matrix3 Matrix3::MakeScale(float sx, float sy) // Create a 2D scale matrix
{
Matrix3 result = MakeIdentity();
result.data[0] = sx;
result.data[4] = sy;
return result;
}
Matrix3 Matrix3::MakeScale(float sx, float sy, float sz) // Creates a 3D scale matrix
{
Matrix3 result = MakeIdentity();
result.data[0] = sx;
result.data[4] = sy;
result.data[8] = sz;
return result;
}
std::string MathClasses::Matrix3::ToString() const { // Convert the matrix to a string
std::ostringstream oss;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
oss
Подробнее здесь: https://stackoverflow.com/questions/796 ... it-wont-pa
(C ++) Попытка написать функцию MakeUuler для пользовательского класса Matrix, но он не пройдет модульный тест после 2 ч ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Функция Makeuuler для пользовательского класса матрицы не пройдет модульный тест [закрыто]
Anonymous » » в форуме C++ - 0 Ответы
- 2 Просмотры
-
Последнее сообщение Anonymous
-