From 787f5490a9fb571a142303a407df2a6f29c860bd Mon Sep 17 00:00:00 2001 From: gramosomi Date: Thu, 29 Feb 2024 21:04:28 +0000 Subject: [PATCH] sphere --- generator/include/shapes/sphere.hpp | 11 ++++++ generator/src/main.cpp | 6 +++ generator/src/shapes/sphere.cpp | 61 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 generator/include/shapes/sphere.hpp create mode 100644 generator/src/shapes/sphere.cpp diff --git a/generator/include/shapes/sphere.hpp b/generator/include/shapes/sphere.hpp new file mode 100644 index 0000000..5b0fb5b --- /dev/null +++ b/generator/include/shapes/sphere.hpp @@ -0,0 +1,11 @@ +#ifndef SOLAR_SYSTEM_SPHERE_HPP +#define SOLAR_SYSTEM_SPHERE_HPP + + +#include +#include +#include "../utils.hpp" + +bool generateSphere(float radius, int slices, int stacks, const char* filepath); + +#endif //SOLAR_SYSTEM_SPHERE_HPP \ No newline at end of file diff --git a/generator/src/main.cpp b/generator/src/main.cpp index d5084a6..e3eea97 100644 --- a/generator/src/main.cpp +++ b/generator/src/main.cpp @@ -4,6 +4,7 @@ #include "../include/shapes/cone.hpp" #include "../include/shapes/cube.hpp" #include "../include/shapes/plane.hpp" +#include "../include/shapes/sphere.hpp" void generateFigure(int argc, char* argv[]) { if (argc < 5) { @@ -17,6 +18,11 @@ void generateFigure(int argc, char* argv[]) { if (figureName == "sphere" && argc == 6) { // Generate Sphere std::cout << "Generating Sphere\n"; + float radius = std::stof(argv[2]); + int slices = std::stoi(argv[3]); + int stacks = std::stoi(argv[4]); + + generateSphere(radius, slices, stacks, figureType); } else if (figureName == "box" && argc == 5) { // Generate Box std::cout << "Generating Box\n"; diff --git a/generator/src/shapes/sphere.cpp b/generator/src/shapes/sphere.cpp new file mode 100644 index 0000000..2ab9258 --- /dev/null +++ b/generator/src/shapes/sphere.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +#include "utils.hpp" + +std::vector sphereTriangles(float radius, int slices, int stacks) { + std::vector points; + + for (int i = 0; i < slices; ++i) { + float theta1 = static_cast(i) * static_cast(M_PI) / static_cast(slices); + float theta2 = static_cast(i + 1) * static_cast(M_PI) / static_cast(slices); + + for (int j = 0; j < stacks; ++j) { + float phi1 = static_cast(j) * 2.0f * static_cast(M_PI) / static_cast(stacks); + float phi2 = static_cast(j + 1) * 2.0f * static_cast(M_PI) / static_cast(stacks); + + // Vertices + float x1 = radius * std::sin(theta1) * std::cos(phi1); + float y1 = radius * std::sin(theta1) * std::sin(phi1); + float z1 = radius * std::cos(theta1); + + float x2 = radius * std::sin(theta1) * std::cos(phi2); + float y2 = radius * std::sin(theta1) * std::sin(phi2); + float z2 = radius * std::cos(theta1); + + float x3 = radius * std::sin(theta2) * std::cos(phi1); + float y3 = radius * std::sin(theta2) * std::sin(phi1); + float z3 = radius * std::cos(theta2); + + float x4 = radius * std::sin(theta2) * std::cos(phi2); + float y4 = radius * std::sin(theta2) * std::sin(phi2); + float z4 = radius * std::cos(theta2); + + // Push vertices in counter-clockwise order + points.push_back(Point(x1, y1, z1)); + points.push_back(Point(x2, y2, z2)); + points.push_back(Point(x4, y4, z4)); + + points.push_back(Point(x1, y1, z1)); + points.push_back(Point(x4, y4, z4)); + points.push_back(Point(x3, y3, z3)); + } + } + + return points; +} + +bool generateSphere(float radius, int slices, int stacks, const char* filepath) { + std::vector triangles = sphereTriangles(radius, slices, stacks); + + if (triangles.empty()) { + std::cerr << "Error: Empty vector of triangles.\n"; + return false; + } + + saveToFile(triangles, filepath); + + return true; +} \ No newline at end of file