From f84e739a7ff3525f9d0481c67d23c899f8c2dd22 Mon Sep 17 00:00:00 2001 From: AfonsoCMSousa Date: Fri, 13 Jun 2025 16:35:10 +0100 Subject: [PATCH] File opperations to convert text into OBJECTS --- include/io/io_file_op.cpp | 9 ++++ include/io/io_file_op.hpp | 1 + include/vector/obj_utils.cpp | 55 +++++++++++++++++++++ include/vector/obj_utils.hpp | 92 ++++++++++++++++++++++++++++++++++++ include/vector/vec_utils.hpp | 13 ++++- source/main.cpp | 13 ++--- 6 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 include/vector/obj_utils.cpp create mode 100644 include/vector/obj_utils.hpp diff --git a/include/io/io_file_op.cpp b/include/io/io_file_op.cpp index f7e1edc..7bc3cc5 100644 --- a/include/io/io_file_op.cpp +++ b/include/io/io_file_op.cpp @@ -83,9 +83,18 @@ file_content w_read_file(FILE *file) line_start = i + 1; } } + __result.lines = lines; return __result; } +void w_close_file(FILE *file) +{ + if (file != NULL) + { + fclose(file); + } +} + file_content init_file_content() { file_content r; diff --git a/include/io/io_file_op.hpp b/include/io/io_file_op.hpp index fc240a1..b81e32e 100644 --- a/include/io/io_file_op.hpp +++ b/include/io/io_file_op.hpp @@ -13,6 +13,7 @@ typedef struct FILE *w_open_file(const char *filepath); file_content w_read_file(FILE *file); +void w_close_file(FILE *file); file_content init_file_content(); diff --git a/include/vector/obj_utils.cpp b/include/vector/obj_utils.cpp new file mode 100644 index 0000000..2c9ead8 --- /dev/null +++ b/include/vector/obj_utils.cpp @@ -0,0 +1,55 @@ +#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 +} \ No newline at end of file diff --git a/include/vector/obj_utils.hpp b/include/vector/obj_utils.hpp new file mode 100644 index 0000000..8ddc26d --- /dev/null +++ b/include/vector/obj_utils.hpp @@ -0,0 +1,92 @@ +#ifndef OBJ_UTILS_HPP +#define OBJ_UTILS_HPP + +#include "vector/vec_utils.hpp" + +class vertex3 +{ +private: + float x, y, z; + float w; + +public: + vertex3() : x(0), y(0), z(0), w(1) {} + vertex3(float x, float y, float z, float w = 1.0f) : x(x), y(y), z(z), w(w) {} + + float getX() const { return x; } + float getY() const { return y; } + float getZ() const { return z; } + float getW() const { return w; } + + void setX(float val) { this->x = val; } + void setY(float val) { this->y = val; } + void setZ(float val) { this->z = val; } + void setW(float val) { this->w = val; } + + vertex3 operator+(const vertex3 &other) const; + vertex3 operator-(const vertex3 &other) const; + vertex3 operator*(float scalar) const; + vertex3 operator/(float scalar) const; + + float dot(const vertex3 &other) const; + vertex3 cross(const vertex3 &other) const; + vertex3 normalize() const; +}; + +class cube +{ +private: + vertex3 *vertices; + vertex3 center; + float size; + +public: + cube() + { + vertices = (vertex3 *)malloc(8 * sizeof(vertex3)); + if (!vertices) + { + fprintf(stderr, "Error: Memory allocation failed for cube vertices.\n"); + return; + } + } + + ~cube() + { + if (vertices) + { + free(vertices); + vertices = nullptr; + } + } + + void setVertex(int index, const vertex3 &v) + { + if (index < 0 || index >= 8) + { + fprintf(stderr, "Error: Index out of bounds in cube::setVertex.\n"); + return; + } + vertices[index] = v; + } + + vertex3 getVertex(int index) const + { + if (index < 0 || index >= 8) + { + fprintf(stderr, "Error: Index out of bounds in cube::getVertex.\n"); + return vertex3(); + } + return vertices[index]; + } + vertex3 getCenter() const + { + return center; + } + float getSize() const + { + return size; + } +}; + +#endif // !OBJ_UTILS_HPP diff --git a/include/vector/vec_utils.hpp b/include/vector/vec_utils.hpp index b493b09..e0c2df6 100644 --- a/include/vector/vec_utils.hpp +++ b/include/vector/vec_utils.hpp @@ -11,18 +11,27 @@ class vec3 { private: float x, y, z; + float orientation; + float scale; + float rotation; public: - vec3() : x(0), y(0), z(0) {} - vec3(float x, float y, float z) : x(x), y(y), z(z) {} + vec3() : x(0), y(0), z(0), orientation(0), scale(1), rotation(0) {} + vec3(float x, float y, float z) : x(x), y(y), z(z), orientation(0), scale(1), rotation(0) {} float getX() const { return x; } float getY() const { return y; } float getZ() const { return z; } + float getOrientation() const { return orientation; } + float getScale() const { return scale; } + float getRotation() const { return rotation; } void setX(float val) { this->x = val; } void setY(float val) { this->y = val; } void setZ(float val) { this->z = val; } + void setOrientation(float val) { this->orientation = val; } + void setScale(float val) { this->scale = val; } + void setRotation(float val) { this->rotation = val; } vec3 operator+(const vec3 &other) const; vec3 operator-(const vec3 &other) const; diff --git a/source/main.cpp b/source/main.cpp index f77af87..74350e7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,22 +2,19 @@ // Include the vector utilities header file #include "vector/vec_utils.hpp" +#include "vector/obj_utils.hpp" #include "io/io_file_op.hpp" int main() { file_content file_info = w_read_file(w_open_file("./CUBE.obj")); - - for (size_t i = 0; i < (u_int32_t)file_info.num_lines; i++) + if (file_info.lines == NULL || file_info.num_lines <= 0) { - printf("Line %04zu: \t", i); - for (size_t j = 0; file_info.lines[i][j] != '\0'; j++) - { - printf("%c", file_info.lines[i][j]); - } - printf("\n"); + fprintf(stderr, "Error: Could not read file or file is empty.\n"); + return 1; } + w_close_file(w_open_file("./CUBE.obj")); free(file_info.lines); return 0; } \ No newline at end of file