55 lines
1.7 KiB
C++
55 lines
1.7 KiB
C++
#include "vec_utils.hpp"
|
|
#include "obj_utils.hpp"
|
|
|
|
vertex3 vertex3::operator+(const vertex3 &other) const
|
|
{
|
|
return vertex3(x + other.x, y + other.y, z + other.z, w + other.w);
|
|
}
|
|
vertex3 vertex3::operator-(const vertex3 &other) const
|
|
{
|
|
return vertex3(x - other.x, y - other.y, z - other.z, w - other.w);
|
|
}
|
|
vertex3 vertex3::operator*(float scalar) const
|
|
{
|
|
return vertex3(x * scalar, y * scalar, z * scalar, w * scalar);
|
|
}
|
|
vertex3 vertex3::operator/(float scalar) const
|
|
{
|
|
if (scalar == 0)
|
|
{
|
|
fprintf(stderr, "Error: Division by zero in vertex3::operator/.\n");
|
|
return vertex3(0, 0, 0, 1); // Return a default vertex if division fails
|
|
}
|
|
return vertex3(x / scalar, y / scalar, z / scalar, w / scalar);
|
|
}
|
|
float vertex3::dot(const vertex3 &other) const
|
|
{
|
|
return x * other.x + y * other.y + z * other.z + w * other.w;
|
|
}
|
|
vertex3 vertex3::cross(const vertex3 &other) const
|
|
{
|
|
return vertex3(
|
|
y * other.z - z * other.y,
|
|
z * other.x - x * other.z,
|
|
x * other.y - y * other.x);
|
|
}
|
|
vertex3 vertex3::normalize() const
|
|
{
|
|
float length = sqrt(x * x + y * y + z * z + w * w);
|
|
if (length == 0)
|
|
{
|
|
fprintf(stderr, "Error: Normalization of zero vector in vertex3::normalize.\n");
|
|
return vertex3(0, 0, 0, 1); // Return a default vertex if normalization fails
|
|
}
|
|
return vertex3(x / length, y / length, z / length, w / length);
|
|
}
|
|
// Function to convert vertex3 to vec3
|
|
vec3 vertex3_to_vec3(const vertex3 &v)
|
|
{
|
|
return vec3(v.getX(), v.getY(), v.getZ());
|
|
}
|
|
// Function to convert vec3 to vertex3
|
|
vertex3 vec3_to_vertex3(const vec3 &v)
|
|
{
|
|
return vertex3(v.getX(), v.getY(), v.getZ(), 1.0f); // w is set to 1 for vertex3
|
|
} |