From 293d47abcbd4201d4383d143d06a6f21597785e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Pinto?= Date: Tue, 27 Feb 2024 18:46:40 +0000 Subject: [PATCH] feat: cube --- generator/include/shapes/cube.hpp | 11 +++ generator/include/shapes/plane.hpp | 6 -- generator/include/utils.hpp | 6 ++ generator/src/main.cpp | 13 ++- generator/src/shapes/cube.cpp | 136 +++++++++++++++++++++++++++++ generator/src/shapes/plane.cpp | 20 +---- generator/src/utils.cpp | 19 +++- 7 files changed, 179 insertions(+), 32 deletions(-) create mode 100644 generator/include/shapes/cube.hpp create mode 100644 generator/src/shapes/cube.cpp diff --git a/generator/include/shapes/cube.hpp b/generator/include/shapes/cube.hpp new file mode 100644 index 0000000..206a375 --- /dev/null +++ b/generator/include/shapes/cube.hpp @@ -0,0 +1,11 @@ +#ifndef SOLAR_SYSTEM_CUBE_HPP +#define SOLAR_SYSTEM_CUBE_HPP + + +#include +#include +#include "../utils.hpp" + +bool generateCube(float length, int divisions, const char* filepath); + +#endif //SOLAR_SYSTEM_PLANE_HPP \ No newline at end of file diff --git a/generator/include/shapes/plane.hpp b/generator/include/shapes/plane.hpp index eb0a303..4f70f9b 100644 --- a/generator/include/shapes/plane.hpp +++ b/generator/include/shapes/plane.hpp @@ -6,12 +6,6 @@ #include #include "../utils.hpp" -std::vector calculateTriangles(float length, int divisions); - -void saveToFile(const std::vector& points, const std::string& filepath); - bool generatePlane(float length, int divisions, const char* filepath); - - #endif //SOLAR_SYSTEM_PLANE_HPP diff --git a/generator/include/utils.hpp b/generator/include/utils.hpp index 5a951d0..86274f8 100644 --- a/generator/include/utils.hpp +++ b/generator/include/utils.hpp @@ -1,8 +1,12 @@ #ifndef SOLAR_SYSTEM_UTILS_HPP #define SOLAR_SYSTEM_UTILS_HPP +#include "utils.hpp" #include #include +#include +#include +#include // Define Point struct typedef struct Point { @@ -18,4 +22,6 @@ typedef struct Point { // Function to convert Point to string std::string pointToString(const Point& point); +void saveToFile(const std::vector& points, const char* filepath); + #endif //SOLAR_SYSTEM_UTILS_HPP diff --git a/generator/src/main.cpp b/generator/src/main.cpp index 8b854db..4cf4388 100644 --- a/generator/src/main.cpp +++ b/generator/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include "../include/shapes/plane.hpp" +#include "../include/shapes/cube.hpp" void generateFigure(int argc, char* argv[]) { if (argc < 5) { @@ -11,20 +12,24 @@ void generateFigure(int argc, char* argv[]) { std::string figureType = argv[argc - 1]; std::string figureName = argv[1]; - if (figureName == "sphere" && figureType == "sphere.3d" && argc == 6) { + if (figureName == "sphere" && argc == 6) { // Generate Sphere std::cout << "Generating Sphere\n"; - } else if (figureName == "box" && figureType == "box.3d" && argc == 5) { + } else if (figureName == "box" && argc == 5) { // Generate Box std::cout << "Generating Box\n"; - } else if (figureName == "plane" && figureType == "plane.3d" && argc == 5) { + float length = std::stof(argv[2]); + int divisions = std::stoi(argv[3]); + + generateCube(length, divisions, argv[4]); + } else if (figureName == "plane"&& argc == 5) { // Generate Plane std::cout << "Generating Plane\n"; // Added newline here float length = std::stof(argv[2]); int divisions = std::stoi(argv[3]); generatePlane(length, divisions, argv[4]); // Assuming saveToFile is available - } else if (figureName == "cone" && figureType == "cone.3d" && argc == 7) { + } else if (figureName == "cone" && argc == 7) { // Generate Cone std::cout << "Generating Cone\n"; } else { diff --git a/generator/src/shapes/cube.cpp b/generator/src/shapes/cube.cpp new file mode 100644 index 0000000..784ed2b --- /dev/null +++ b/generator/src/shapes/cube.cpp @@ -0,0 +1,136 @@ +#include "utils.hpp" + +#include +#include +#include + +std::vector cubeTriangles(float length, int divisions) { + + float halfSize = length / 2.0f; + float step = length / divisions; + + std::vector points; + + // Front face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float x1 = -halfSize + i * step; + float y1 = -halfSize + j * step; + float x2 = x1 + step; + float y2 = y1 + step; + + points.push_back(Point(x1, y1, halfSize)); + points.push_back(Point(x2, y1, halfSize)); + points.push_back(Point(x1, y2, halfSize)); + + points.push_back(Point(x2, y1, halfSize)); + points.push_back(Point(x2, y2, halfSize)); + points.push_back(Point(x1, y2, halfSize)); + } + } + + // Back face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float x1 = -halfSize + i * step; + float y1 = -halfSize + j * step; + float x2 = x1 + step; + float y2 = y1 + step; + + points.push_back(Point(x1, y1, -halfSize)); + points.push_back(Point(x1, y2, -halfSize)); + points.push_back(Point(x2, y1, -halfSize)); + + points.push_back(Point(x2, y1, -halfSize)); + points.push_back(Point(x1, y2, -halfSize)); + points.push_back(Point(x2, y2, -halfSize)); + } + } + + // Left face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float y1 = -halfSize + i * step; + float z1 = -halfSize + j * step; + float y2 = y1 + step; + float z2 = z1 + step; + + points.push_back(Point(-halfSize, y1, z1)); + points.push_back(Point(-halfSize, y2, z1)); + points.push_back(Point(-halfSize, y1, z2)); + + points.push_back(Point(-halfSize, y2, z1)); + points.push_back(Point(-halfSize, y2, z2)); + points.push_back(Point(-halfSize, y1, z2)); + } + } + + // Right face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float y1 = -halfSize + i * step; + float z1 = -halfSize + j * step; + float y2 = y1 + step; + float z2 = z1 + step; + + points.push_back(Point(halfSize, y1, z1)); + points.push_back(Point(halfSize, y1, z2)); + points.push_back(Point(halfSize, y2, z1)); + + points.push_back(Point(halfSize, y1, z2)); + points.push_back(Point(halfSize, y2, z2)); + points.push_back(Point(halfSize, y2, z1)); + } + } + + // Top face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float x1 = -halfSize + i * step; + float z1 = -halfSize + j * step; + float x2 = x1 + step; + float z2 = z1 + step; + + points.push_back(Point(x1, halfSize, z1)); + points.push_back(Point(x2, halfSize, z1)); + points.push_back(Point(x1, halfSize, z2)); + + points.push_back(Point(x2, halfSize, z1)); + points.push_back(Point(x2, halfSize, z2)); + points.push_back(Point(x1, halfSize, z2)); + } + } + + // Bottom face + for(int i = 0; i < divisions; ++i) { + for(int j = 0; j < divisions; ++j) { + float x1 = -halfSize + i * step; + float z1 = -halfSize + j * step; + float x2 = x1 + step; + float z2 = z1 + step; + + points.push_back(Point(x1, -halfSize, z1)); + points.push_back(Point(x1, -halfSize, z2)); + points.push_back(Point(x2, -halfSize, z1)); + + points.push_back(Point(x2, -halfSize, z1)); + points.push_back(Point(x1, -halfSize, z2)); + points.push_back(Point(x2, -halfSize, z2)); + } + } + + return points; +} + +bool generateCube(float length, int divisions, const char* filepath) { // Changed parameter type to const char* + std::vector triangles = cubeTriangles(length, divisions); + + if (triangles.empty()) { + std::cerr << "Error: Empty vector of triangles.\n"; + return false; + } + + saveToFile(triangles, filepath); + + return true; +} \ No newline at end of file diff --git a/generator/src/shapes/plane.cpp b/generator/src/shapes/plane.cpp index 87428c9..e923edc 100644 --- a/generator/src/shapes/plane.cpp +++ b/generator/src/shapes/plane.cpp @@ -4,11 +4,7 @@ #include #include -float planeWidth = 5.0f; -float planeLength = 5.0f; -int divisions = 3; - -std::vector calculateTriangles(float length, int divisions) { +std::vector planeTriangles(float length, int divisions) { float half = length / 2.0f; float steps = length / divisions; @@ -42,22 +38,10 @@ std::vector calculateTriangles(float length, int divisions) { return points; } -void saveToFile(const std::vector& points, const char* filepath) { // Changed parameter type to const char* - std::ofstream file(filepath); - if (file.is_open()) { - for (const auto& point : points) { - file << point.x << " " << point.y << " " << point.z << "\n"; - } - file.close(); - std::cout << "File saved successfully.\n"; - } else { - std::cerr << "Unable to open file: " << filepath << std::endl; - } -} bool generatePlane(float length, int divisions, const char* filepath) { // Changed parameter type to const char* - std::vector triangles = calculateTriangles(length, divisions); + std::vector triangles = planeTriangles(length, divisions); if (triangles.empty()) { std::cerr << "Error: Empty vector of triangles.\n"; diff --git a/generator/src/utils.cpp b/generator/src/utils.cpp index 0f1b3e8..d09f9e3 100644 --- a/generator/src/utils.cpp +++ b/generator/src/utils.cpp @@ -1,9 +1,20 @@ #include "utils.hpp" #include #include +#include +#include +#include -std::string pointToString(Point point) { - std::ostringstream oss; - oss << point.x << " " << point.y << " " << point.z << "\n"; - return oss.str(); +void saveToFile(const std::vector& points, const char* filepath) { // Changed parameter type to const char* + std::ofstream file(filepath); + + if (file.is_open()) { + for (const auto& point : points) { + file << point.x << " " << point.y << " " << point.z << "\n"; + } + file.close(); + std::cout << "File saved successfully.\n"; + } else { + std::cerr << "Unable to open file: " << filepath << std::endl; + } }