Skip to content

Commit

Permalink
feat: cube
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed Feb 27, 2024
1 parent 71c9556 commit 293d47a
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 32 deletions.
11 changes: 11 additions & 0 deletions generator/include/shapes/cube.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef SOLAR_SYSTEM_CUBE_HPP
#define SOLAR_SYSTEM_CUBE_HPP


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

bool generateCube(float length, int divisions, const char* filepath);

#endif //SOLAR_SYSTEM_PLANE_HPP
6 changes: 0 additions & 6 deletions generator/include/shapes/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
#include <string>
#include "../utils.hpp"

std::vector<Point> calculateTriangles(float length, int divisions);

void saveToFile(const std::vector<Point>& points, const std::string& filepath);

bool generatePlane(float length, int divisions, const char* filepath);



#endif //SOLAR_SYSTEM_PLANE_HPP
6 changes: 6 additions & 0 deletions generator/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef SOLAR_SYSTEM_UTILS_HPP
#define SOLAR_SYSTEM_UTILS_HPP

#include "utils.hpp"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>

// Define Point struct
typedef struct Point {
Expand All @@ -18,4 +22,6 @@ typedef struct Point {
// Function to convert Point to string
std::string pointToString(const Point& point);

void saveToFile(const std::vector<Point>& points, const char* filepath);

#endif //SOLAR_SYSTEM_UTILS_HPP
13 changes: 9 additions & 4 deletions generator/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <string>
#include "../include/shapes/plane.hpp"
#include "../include/shapes/cube.hpp"

void generateFigure(int argc, char* argv[]) {
if (argc < 5) {
Expand All @@ -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 {
Expand Down
136 changes: 136 additions & 0 deletions generator/src/shapes/cube.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "utils.hpp"

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

std::vector<Point> cubeTriangles(float length, int divisions) {

float halfSize = length / 2.0f;
float step = length / divisions;

std::vector<Point> 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<Point> triangles = cubeTriangles(length, divisions);

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

saveToFile(triangles, filepath);

return true;
}
20 changes: 2 additions & 18 deletions generator/src/shapes/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
#include <fstream>
#include <vector>

float planeWidth = 5.0f;
float planeLength = 5.0f;
int divisions = 3;

std::vector<Point> calculateTriangles(float length, int divisions) {
std::vector<Point> planeTriangles(float length, int divisions) {
float half = length / 2.0f;
float steps = length / divisions;

Expand Down Expand Up @@ -42,22 +38,10 @@ std::vector<Point> calculateTriangles(float length, int divisions) {
return points;
}

void saveToFile(const std::vector<Point>& 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<Point> triangles = calculateTriangles(length, divisions);
std::vector<Point> triangles = planeTriangles(length, divisions);

if (triangles.empty()) {
std::cerr << "Error: Empty vector of triangles.\n";
Expand Down
19 changes: 15 additions & 4 deletions generator/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#include "utils.hpp"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>

std::string pointToString(Point point) {
std::ostringstream oss;
oss << point.x << " " << point.y << " " << point.z << "\n";
return oss.str();
void saveToFile(const std::vector<Point>& 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;
}
}

0 comments on commit 293d47a

Please sign in to comment.