Skip to content

Commit

Permalink
chore: cylinder generator
Browse files Browse the repository at this point in the history
  • Loading branch information
diogogmatos committed Mar 5, 2024
1 parent 96ab0d9 commit cbcf6b6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
11 changes: 11 additions & 0 deletions generator/include/shapes/cylinder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CYLINDER_HPP
#define CYLINDER_HPP

#include <string>

#include "cylinder.hpp"

bool generateCylinder(float radius, float height, int slices,
const char* filepath);

#endif // CYLINDER_HPP
1 change: 0 additions & 1 deletion generator/include/shapes/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define SOLAR_SYSTEM_PLANE_HPP

#include <string>
#include <vector>

#include "../utils.hpp"

Expand Down
21 changes: 15 additions & 6 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../include/shapes/cone.hpp"
#include "../include/shapes/cube.hpp"
#include "../include/shapes/cylinder.hpp"
#include "../include/shapes/plane.hpp"
#include "../include/shapes/sphere.hpp"
#include "../include/shapes/torus.hpp"
Expand All @@ -13,7 +14,7 @@ void generateFigure(int argc, char* argv[]) {
return;
}

char* figureType = argv[argc - 1];
char* fileName = argv[argc - 1];
std::string figureName = argv[1];

if (figureName == "sphere" && argc == 6) {
Expand All @@ -23,22 +24,22 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[3]);
int stacks = std::stoi(argv[4]);

generateSphere(radius, slices, stacks, figureType);
generateSphere(radius, slices, stacks, fileName);
} else if (figureName == "box" && argc == 5) {
// Generate Box
std::cout << "Generating Box\n";
float length = std::stof(argv[2]);
int divisions = std::stoi(argv[3]);

generateCube(length, divisions, figureType);
generateCube(length, divisions, fileName);
} 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,
figureType); // Assuming saveToFile is available
fileName); // Assuming saveToFile is available
} else if (figureName == "cone" && argc == 7) {
// Generate Cone
std::cout << "Generating Cone\n";
Expand All @@ -47,7 +48,7 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

generateCone(radius, height, slices, stacks, figureType);
generateCone(radius, height, slices, stacks, fileName);

} else if (figureName == "torus" && argc == 7) {
// Generate Torus
Expand All @@ -57,7 +58,15 @@ void generateFigure(int argc, char* argv[]) {
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

generateTorus(innerRadius, outerRadius, slices, stacks, figureType);
generateTorus(innerRadius, outerRadius, slices, stacks, fileName);
} else if (figureName == "cylinder" && argc == 6) {
// Generate Cylinder
std::cout << "Generating Cylinder\n";
float radius = std::stof(argv[2]);
float height = std::stof(argv[3]);
int slices = std::stoi(argv[4]);

generateCylinder(radius, height, slices, fileName);
} else {
std::cerr << "Invalid arguments\n";
}
Expand Down
70 changes: 70 additions & 0 deletions generator/src/shapes/cylinder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "../../include/shapes/cylinder.hpp"

#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>

#include "utils.hpp"

std::vector<Point> cylinderTriangles(const float radius, const float height,
const int slices) {
std::vector<Point> vertex;

float alpha = 2 * M_PI / slices;
float r = 0;

for (int i = 0; i < slices; i++) {
// base
float px = radius * sin(r);
float pz = radius * cos(r);
vertex.push_back(Point(px, 0, pz));
vertex.push_back(Point(0, 0, 0));
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, 0, pz));

// face
vertex.push_back(Point(px, 0, pz));
vertex.push_back(Point(px, height, pz));
r -= alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, height, pz));

vertex.push_back(Point(px, height, pz));
vertex.push_back(Point(px, 0, pz));
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, 0, pz));

// topo
r += alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, height, pz));
vertex.push_back(Point(0, height, 0));
r -= alpha;
px = radius * sin(r);
pz = radius * cos(r);
vertex.push_back(Point(px, height, pz));
}

return vertex;
}

bool generateCylinder(float radius, float height, int slices,
const char* filepath) {
std::vector<Point> triangles = cylinderTriangles(radius, height, slices);

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

saveToFile(triangles, filepath);

return true;
}

0 comments on commit cbcf6b6

Please sign in to comment.