Skip to content

Commit

Permalink
add: models
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed Feb 29, 2024
1 parent fdc2a0c commit c0033e4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions generator/include/shapes/cone.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CONE_HPP
#define CONE_HPP

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

#include "../utils.hpp"

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

#endif //CONE_HPP
9 changes: 9 additions & 0 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "../include/shapes/cube.hpp"
#include "../include/shapes/plane.hpp"
#include "../include/shapes/cone.hpp"


void generateFigure(int argc, char* argv[]) {
if (argc < 5) {
Expand Down Expand Up @@ -34,6 +36,13 @@ void generateFigure(int argc, char* argv[]) {
} else if (figureName == "cone" && argc == 7) {
// Generate Cone
std::cout << "Generating Cone\n";
float radius = std::stof(argv[2]);
float height = std::stof(argv[3]);
int slices = std::stoi(argv[4]);
int stacks = std::stoi(argv[5]);

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

} else {
std::cerr << "Invalid arguments\n";
}
Expand Down
57 changes: 57 additions & 0 deletions generator/src/shapes/cone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "utils.hpp"

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

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

// Generate points for each stack
for (int i = 0; i < stacks; ++i) {
float y1 = height * i / stacks;
float y2 = height * (i + 1) / stacks;

for (int j = 0; j < slices; ++j) {
float theta1 = 2.0f * M_PI * j / slices;
float theta2 = 2.0f * M_PI * (j + 1) / slices;

float x1 = radius * std::cos(theta1);
float z1 = radius * std::sin(theta1);

float x2 = radius * std::cos(theta2);
float z2 = radius * std::sin(theta2);

// Side triangles
points.emplace_back(x1, y1, z1);
points.emplace_back(x2, y1, z2);
points.emplace_back(0.0f, y2, 0.0f);

// Bottom triangles
if (i == 0) {
points.emplace_back(0.0f, 0.0f, 0.0f);
points.emplace_back(x2, y1, z2);
points.emplace_back(x1, y1, z1);
}
}
}

// Cone tip
points.emplace_back(0.0f, height, 0.0f);

return points;
}

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

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

saveToFile(triangles, filepath);

return true;
}

0 comments on commit c0033e4

Please sign in to comment.