From 6ad7555ad73e37b94ff1ef72256a9ecf721af68f Mon Sep 17 00:00:00 2001 From: iutbgdin Date: Fri, 10 Oct 2025 10:21:36 +0200 Subject: [PATCH] tofix --- CMakeLists.txt | 3 ++- src/object/3dObject.h | 1 + src/object/map.cpp | 18 ++++++++++++------ src/object/map.h | 1 + src/object/object.cpp | 19 +++++++++++++++++-- src/object/object.h | 4 +++- src/object/plane.cpp | 15 +++++++++------ src/object/plane.h | 2 ++ src/object/point.cpp | 23 +++++++++++++++-------- src/object/point.h | 1 + src/util/math.cpp | 10 ++++++++++ src/util/math.h | 8 ++++++++ 12 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 src/util/math.cpp create mode 100644 src/util/math.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 319634b..c7ee768 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,8 @@ add_executable(3d-renderer src/main.cpp src/object/map.cpp src/object/map.h src/object/object.cpp src/object/object.h src/object/plane.cpp src/object/plane.h - src/object/point.cpp src/object/point.h) + src/object/point.cpp src/object/point.h + src/util/math.h src/util/math.cpp) include(GNUInstallDirs) install(TARGETS 3d-renderer diff --git a/src/object/3dObject.h b/src/object/3dObject.h index 480a914..0e8024c 100644 --- a/src/object/3dObject.h +++ b/src/object/3dObject.h @@ -1,3 +1,4 @@ +#pragma once #include class object3D { diff --git a/src/object/map.cpp b/src/object/map.cpp index 3af3601..d412ab2 100644 --- a/src/object/map.cpp +++ b/src/object/map.cpp @@ -26,12 +26,19 @@ void Map::load(std::string file, std::string txtpos) { } else { pos = line.substr(0, line.find(":")); line.erase(0, line.find(":") + 1); - if (line.substr(line.length() - 3, line.length()) == "obj") { + std::string fname = line.substr(line.length() - 3, line.length()); + + if (fname == "obj") { objs.push_back(new Object()); - } else if (line.substr(line.length() - 3, line.length()) == "map") { + } else if (fname == "map") { + if (fname == file) { + std::cerr << "Recursive map import detected! Stopping import for " << file << "." << std::endl; + return; + } objs.push_back(new Map()); } else { - std::cerr << "Object " << line << " have unknown extension (" << line.substr(line.length() - 2, line.length()) << ")." << std::endl; + std::cerr << "Object " << line << " have unknown extension (" << fname << "). Import stopped for " << file << "." << std::endl; + return; } (*objs.rbegin())->load(line, pos); } @@ -41,9 +48,8 @@ void Map::load(std::string file, std::string txtpos) { std::cerr << "Could not open file " << file << std::endl; return; } - if (txtpos != "") { - this->pts = new Point(txtpos); - } + + this->pts = new Point(txtpos); } std::ostream & operator<<(std::ostream& os, Map& m){ diff --git a/src/object/map.h b/src/object/map.h index c931e81..71be666 100644 --- a/src/object/map.h +++ b/src/object/map.h @@ -1,3 +1,4 @@ +#pragma once #include "object.h" class Map: public object3D { diff --git a/src/object/object.cpp b/src/object/object.cpp index d31b875..3c0fc66 100644 --- a/src/object/object.cpp +++ b/src/object/object.cpp @@ -2,6 +2,7 @@ #include #include "object.h" +#include "../util/math.h" void Object::load(std::string file, std::string txtpos) { @@ -33,19 +34,33 @@ void Object::load(std::string file, std::string txtpos) { } this->pts = new Point(txtpos); + this->diameter = new float(this->diameterCalc()); } std::vector Object::getSides() { return this->plns; } -bool Object::isContained() { return true; }; +bool Object::isContained() { return true; } std::ostream & operator<<(std::ostream& os, Object& o){ return o.print(os); } std::ostream& Object::print(std::ostream& os) { - os << " - Object " << this << std::endl; + os << " - Object " << this << "\n - Position:\n" << (*this->pts) << "\n - Diameter:\n - " << (*this->diameter) << std::endl; for (Plane* i: this->getSides()) { os << (*i); } return os; } + +float Object::diameterCalc() { + float max_dist = 0; + for (Plane* p: this->plns) { + for (Point* i: p->getPoints()) { + float tmp = distance((*this->pts), (*i)); + if ( tmp > max_dist) { + max_dist = tmp; + } + } + } + return max_dist; +} diff --git a/src/object/object.h b/src/object/object.h index 32d1a56..8f9b4ed 100644 --- a/src/object/object.h +++ b/src/object/object.h @@ -1,3 +1,4 @@ +#pragma once #include "plane.h" #include "3dObject.h" @@ -10,7 +11,8 @@ class Object: public object3D { std::ostream& print(std::ostream& os); friend std::ostream & operator<<(std::ostream& os, Object& o); private: + float diameterCalc(); std::vector plns; Point* pts; - float* radius; + float* diameter; }; diff --git a/src/object/plane.cpp b/src/object/plane.cpp index 2210d3a..454654e 100644 --- a/src/object/plane.cpp +++ b/src/object/plane.cpp @@ -31,21 +31,24 @@ Plane::Plane(std::string& txt) { this->color = new uint64_t(st64(txt.substr(txt.find(":") + 1, txt.length()))); txt.erase(txt.find(":") + 1, txt.length()); + std::string tmp = ""; while ((pos = txt.find(";")) != std::string::npos) { std::string tmp = txt.substr(0, pos); this->pts.push_back(new Point(tmp)); txt.erase(0, pos + 1); it++; } - this->pts.push_back(new Point(txt)); + tmp = txt.substr(0, txt.length - 1); + this->pts.push_back(new Point(tmp)); } std::vector Plane::getPoints() { return pts; } -uint8_t Plane::getR() { return (*this->color) >> 32; } -uint8_t Plane::getG() { return ((*this->color) << 32) >> 56; } -uint8_t Plane::getB() { return ((*this->color) << 40) >> 56; } -uint8_t Plane::getA() { return ((*this->color) << 48) >> 56; } -uint8_t Plane::getL() { return ((*this->color) << 56) >> 56; } +uint8_t Plane::getR() { return (*this->color) >> 40; } +uint8_t Plane::getG() { return ((*this->color) << 8) >> 40; } +uint8_t Plane::getB() { return ((*this->color) << 16) >> 40; } +uint8_t Plane::getA() { return ((*this->color) << 24) >> 40; } +uint8_t Plane::getL() { return ((*this->color) << 32) >> 40; } +uint8_t Plane::getZ() { return ((*this->color) << 40) >> 40; } std::ostream & operator<<(std::ostream& os, Plane& p){ os << " - Plan " << &p << "\n - R: " << (int)p.getR() << "\n - G: "<< (int)p.getG() << "\n - B: "<< (int)p.getB() << "\n - A: "<< (int)p.getA() << "\n - L: "<< (int)p.getL() << std::endl; diff --git a/src/object/plane.h b/src/object/plane.h index 99eaa68..0d5ab47 100644 --- a/src/object/plane.h +++ b/src/object/plane.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include @@ -13,6 +14,7 @@ class Plane { uint8_t getB(); uint8_t getA(); uint8_t getL(); + uint8_t getZ(); friend std::ostream & operator<<(std::ostream& os, Plane& p); private: std::vector pts; diff --git a/src/object/point.cpp b/src/object/point.cpp index 1d11ea8..b4a62c8 100644 --- a/src/object/point.cpp +++ b/src/object/point.cpp @@ -1,17 +1,24 @@ #include "point.h" #include -Point::Point(std::string &txt) { - size_t pos = 0; +Point::Point(std::string& txt) { + if (txt != "") { + size_t pos = 0; - while ((pos = txt.find(",")) != std::string::npos) { - this->xyz.push_back(new float(std::stof(txt.substr(0, pos)))); - txt.erase(0, pos + 1); - } - this->xyz.push_back(new float(std::stof(txt))); + while ((pos = txt.find(",")) != std::string::npos) { + this->xyz.push_back(new float(std::stof(txt.substr(0, pos)))); + txt.erase(0, pos + 1); + } + std::cout << txt << std::endl; + this->xyz.push_back(new float(std::stof(txt))); + } + + if (this->xyz.size() != 3) { + this->xyz = {new float(0), new float(0), new float(0)}; + } } -uint16_t Point::translate3D() { return 0; }; +uint16_t Point::translate3D() { return 0; } float Point::getX() { return (*this->xyz.at(0)); } float Point::getY() { return (*this->xyz.at(1)); } diff --git a/src/object/point.h b/src/object/point.h index fe5be9a..355e71b 100644 --- a/src/object/point.h +++ b/src/object/point.h @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/src/util/math.cpp b/src/util/math.cpp new file mode 100644 index 0000000..6b1c37d --- /dev/null +++ b/src/util/math.cpp @@ -0,0 +1,10 @@ +#include + +#include "math.h" + +float distance(Point& a, Point& b) { + return sqrt( pow(a.getX() - b.getX(), 2) + + pow(a.getY() - b.getY(), 2) + + pow(a.getZ() - b.getZ(), 2) + ); +} diff --git a/src/util/math.h b/src/util/math.h new file mode 100644 index 0000000..c611aa8 --- /dev/null +++ b/src/util/math.h @@ -0,0 +1,8 @@ +#ifndef MATH_H +#define MATH_H + +#include "../object/point.h" + +float distance(Point& a, Point& b); + +#endif // MATH_H