Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sphere
Browse files Browse the repository at this point in the history
gramosomi committed Feb 29, 2024
1 parent e44dfc0 commit 787f549
Showing 3 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions generator/include/shapes/sphere.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef SOLAR_SYSTEM_SPHERE_HPP
#define SOLAR_SYSTEM_SPHERE_HPP


#include <vector>
#include <string>
#include "../utils.hpp"

bool generateSphere(float radius, int slices, int stacks, const char* filepath);

#endif //SOLAR_SYSTEM_SPHERE_HPP
6 changes: 6 additions & 0 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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";
61 changes: 61 additions & 0 deletions generator/src/shapes/sphere.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>

#include "utils.hpp"

std::vector<Point> sphereTriangles(float radius, int slices, int stacks) {
std::vector<Point> points;

for (int i = 0; i < slices; ++i) {
float theta1 = static_cast<float>(i) * static_cast<float>(M_PI) / static_cast<float>(slices);
float theta2 = static_cast<float>(i + 1) * static_cast<float>(M_PI) / static_cast<float>(slices);

for (int j = 0; j < stacks; ++j) {
float phi1 = static_cast<float>(j) * 2.0f * static_cast<float>(M_PI) / static_cast<float>(stacks);
float phi2 = static_cast<float>(j + 1) * 2.0f * static_cast<float>(M_PI) / static_cast<float>(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<Point> triangles = sphereTriangles(radius, slices, stacks);

if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
return false;
}

saveToFile(triangles, filepath);

return true;
}

0 comments on commit 787f549

Please sign in to comment.