~ | removing useless trycatch

This commit is contained in:
RedShip
2025-01-13 18:50:55 +01:00
parent 449f059a07
commit 819c07f90d
8 changed files with 136 additions and 191 deletions

View File

@ -20,26 +20,23 @@ class Cube : public Object
public: public:
Cube(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Cube(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float width, height, depth;
float width, height, depth; int mat_index;
int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing position values"); throw std::runtime_error("Missing position values");
if (!(line >> width >> height >> depth)) if (!(line >> width >> height >> depth))
throw std::runtime_error("Missing width, height or depth values"); throw std::runtime_error("Missing width, height or depth values");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_size = glm::vec3(width, height, depth); _size = glm::vec3(width, height, depth);
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception& e) { throw; }
} }
Cube(const glm::vec3& position, const glm::vec3 &size, const int mat_index) Cube(const glm::vec3& position, const glm::vec3 &size, const int mat_index)
: Object(position, mat_index), _size(size) {} : Object(position, mat_index), _size(size) {}

View File

@ -20,35 +20,32 @@ class Cylinder : public Object
public: public:
Cylinder(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Cylinder(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float radius, height;
float radius, height; float pitch, yaw, roll;
float pitch, yaw, roll;
int mat_index; int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing position"); throw std::runtime_error("Missing position");
if (!(line >> radius >> height)) if (!(line >> radius >> height))
throw std::runtime_error("Missing radius or height values"); throw std::runtime_error("Missing radius or height values");
if (!(line >> pitch >> yaw >> roll)) if (!(line >> pitch >> yaw >> roll))
throw std::runtime_error("Missing rotation values"); throw std::runtime_error("Missing rotation values");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_radius = radius; _radius = radius;
_height = height; _height = height;
_rotation = glm::mat3(glm::eulerAngleXYZ(pitch, yaw, roll)); _rotation = glm::mat3(glm::eulerAngleXYZ(pitch, yaw, roll));
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception& e) { throw; }
} }
Cylinder(const glm::vec3& position, float radius, const int mat_index) Cylinder(const glm::vec3& position, float radius, const int mat_index)
: Object(position, mat_index), _radius(radius) {} : Object(position, mat_index), _radius(radius) {}

View File

@ -20,26 +20,23 @@ class Plane : public Object
public: public:
Plane(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Plane(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float nx, ny, nz;
float nx, ny, nz; int mat_index;
int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing position"); throw std::runtime_error("Missing position");
if (!(line >> nx >> ny >> nz)) if (!(line >> nx >> ny >> nz))
throw std::runtime_error("Missing plane's normal"); throw std::runtime_error("Missing plane's normal");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_normal = glm::vec3(nx, ny, nz); _normal = glm::vec3(nx, ny, nz);
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception &e) { throw; }
} }
Plane(const glm::vec3 &position, const glm::vec3 &normal, const int mat_index) Plane(const glm::vec3 &position, const glm::vec3 &normal, const int mat_index)
: Object(position, mat_index), _normal(normal) {} : Object(position, mat_index), _normal(normal) {}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ /* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
/* Updated: 2024/12/23 19:47:09 by ycontre ### ########.fr */ /* Updated: 2025/01/13 18:44:40 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,48 +20,45 @@ class Portal : public Object
public: public:
Portal(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Portal(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float x1, y1, z1;
float x1, y1, z1; float x2, y2, z2;
float x2, y2, z2;
bool invert_normal;
bool invert_normal;
int mat_index; int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing position"); throw std::runtime_error("Missing position");
if (!(line >> x1 >> y1 >> z1)) if (!(line >> x1 >> y1 >> z1))
throw std::runtime_error("Missing Portal's first edge "); throw std::runtime_error("Missing Portal's first edge ");
if (!(line >> x2 >> y2 >> z2)) if (!(line >> x2 >> y2 >> z2))
throw std::runtime_error("Missing Portal's second edge"); throw std::runtime_error("Missing Portal's second edge");
if (!(line >> invert_normal)) if (!(line >> invert_normal))
throw std::runtime_error("Missing invert_normal"); throw std::runtime_error("Missing invert_normal");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_up = glm::vec3(x1, y1, z1); _up = glm::vec3(x1, y1, z1);
_right = glm::vec3(x2, y2, z2); _right = glm::vec3(x2, y2, z2);
glm::vec3 up = glm::normalize(_up); glm::vec3 up = glm::normalize(_up);
glm::vec3 right = glm::normalize(_right); glm::vec3 right = glm::normalize(_right);
glm::vec3 forward = glm::normalize(glm::cross(right, up)); glm::vec3 forward = glm::normalize(glm::cross(right, up));
up = normalize(glm::cross(forward, right)); up = normalize(glm::cross(forward, right));
_rotation = glm::mat3(right, up, forward); _rotation = glm::mat3(right, up, forward);
_normal = forward * (invert_normal ? -1.0f : 1.0f); _normal = forward * (invert_normal ? -1.0f : 1.0f);
_linked_portal = -1; _linked_portal = -1;
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception &e) { throw; }
} }
Portal(const glm::vec3 &position, const glm::vec3 &edge1, const glm::vec3 &edge2, const int linked_portal, const int mat_index) Portal(const glm::vec3 &position, const glm::vec3 &edge1, const glm::vec3 &edge2, const int linked_portal, const int mat_index)
: Object(position, mat_index), _up(edge1), _right(edge2), _linked_portal(linked_portal) {} : Object(position, mat_index), _up(edge1), _right(edge2), _linked_portal(linked_portal) {}

View File

@ -20,31 +20,28 @@ class Quad : public Object
public: public:
Quad(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Quad(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float x1, y1, z1;
float x1, y1, z1; float x2, y2, z2;
float x2, y2, z2; int mat_index;
int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing position"); throw std::runtime_error("Missing position");
if (!(line >> x1 >> y1 >> z1)) if (!(line >> x1 >> y1 >> z1))
throw std::runtime_error("Missing quad's first edge "); throw std::runtime_error("Missing quad's first edge ");
if (!(line >> x2 >> y2 >> z2)) if (!(line >> x2 >> y2 >> z2))
throw std::runtime_error("Missing quad's second edge"); throw std::runtime_error("Missing quad's second edge");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_up = glm::vec3(x1, y1, z1); _up = glm::vec3(x1, y1, z1);
_right = glm::vec3(x2, y2, z2); _right = glm::vec3(x2, y2, z2);
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception &e) { throw; }
} }
Quad(const glm::vec3 &position, const glm::vec3 &edge1, const glm::vec3 &edge2, const int mat_index) Quad(const glm::vec3 &position, const glm::vec3 &edge1, const glm::vec3 &edge2, const int mat_index)
: Object(position, mat_index), _up(edge1), _right(edge2) {} : Object(position, mat_index), _up(edge1), _right(edge2) {}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ /* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
/* Updated: 2025/01/08 20:20:34 by ycontre ### ########.fr */ /* Updated: 2025/01/13 18:46:58 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,25 +20,22 @@ class Sphere : public Object
public: public:
Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z, radius;
float x, y, z, radius; int mat_index;
int mat_index;
if (!(line >> x >> y >> z >> radius)) if (!(line >> x >> y >> z >> radius))
throw std::runtime_error("Missing position or radius values"); throw std::runtime_error("Missing position or radius values");
if (radius <= 0.0f) if (radius <= 0.0f)
throw std::runtime_error("Radius must be positive"); throw std::runtime_error("Radius must be positive");
if (!(line >> mat_index)) if (!(line >> mat_index))
throw std::runtime_error("Missing material properties"); throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z); _position = glm::vec3(x, y, z);
_radius = radius / 2.0; _radius = radius / 2.0;
_mat_index = mat_index; _mat_index = mat_index;
}
catch (const std::exception& e) { throw; }
} }
Sphere(const glm::vec3& position, float radius, const int mat_index) Sphere(const glm::vec3& position, float radius, const int mat_index)
: Object(position, mat_index), _radius(radius) {} : Object(position, mat_index), _radius(radius) {}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ /* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
/* Updated: 2025/01/13 17:59:21 by tomoron ### ########.fr */ /* Updated: 2025/01/13 18:43:18 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,37 +20,34 @@ class Triangle : public Object
public: public:
Triangle(std::stringstream &line) : Object(glm::vec3(0.0f), -1) Triangle(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
{ {
try { float x, y, z;
float x, y, z; float x2, y2, z2;
float x2, y2, z2; float x3, y3, z3;
float x3, y3, z3;
int mat_index; int mat_index;
if (!(line >> x >> y >> z)) if (!(line >> x >> y >> z))
throw std::runtime_error("Missing first vertex position"); throw std::runtime_error("Missing first vertex position");
if (!(line >> x2 >> y2 >> z2)) if (!(line >> x2 >> y2 >> z2))
throw std::runtime_error("Missing second vertex position"); throw std::runtime_error("Missing second vertex position");
if (!(line >> x3 >> y3 >> z3))
throw std::runtime_error("Missing third vertex position");
if (!(line >> mat_index))
throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z);
_vertex2 = glm::vec3(x2, y2, z2);
_vertex3 = glm::vec3(x3, y3, z3);
_vertex2 -= _position; //optimization
_vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
_mat_index = mat_index; if (!(line >> x3 >> y3 >> z3))
} throw std::runtime_error("Missing third vertex position");
catch (const std::exception &e) { throw; }
if (!(line >> mat_index))
throw std::runtime_error("Missing material properties");
_position = glm::vec3(x, y, z);
_vertex2 = glm::vec3(x2, y2, z2);
_vertex3 = glm::vec3(x3, y3, z3);
_vertex2 -= _position; //optimization
_vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
_mat_index = mat_index;
} }
Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index) Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index)
: Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) { : Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) {

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* SceneParser.cpp :+: :+: :+: */ /* SceneParser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */ /* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */
/* Updated: 2025/01/13 17:59:30 by tomoron ### ########.fr */ /* Updated: 2025/01/13 18:49:10 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,47 +14,13 @@
SceneParser::SceneParser(Scene *scene) : _scene(scene) SceneParser::SceneParser(Scene *scene) : _scene(scene)
{ {
object_parsers["sp"] = [](std::stringstream &ss) -> Object * object_parsers["sp"] = [](std::stringstream &ss) -> Object * { return (new Sphere(ss)); };
{ object_parsers["pl"] = [](std::stringstream &ss) -> Object * { return (new Plane(ss)); };
try { return (new Sphere(ss)); } object_parsers["qu"] = [](std::stringstream &ss) -> Object * { return (new Quad(ss)); };
catch (const std::exception &e) { throw; } object_parsers["tr"] = [](std::stringstream &ss) -> Object * { return (new Triangle(ss)); };
}; object_parsers["cu"] = [](std::stringstream &ss) -> Object * { return (new Cube(ss)); };
object_parsers["po"] = [](std::stringstream &ss) -> Object * { return (new Portal(ss)); };
object_parsers["pl"] = [](std::stringstream &ss) -> Object * object_parsers["cy"] = [](std::stringstream &ss) -> Object * { return (new Cylinder(ss)); };
{
try { return (new Plane(ss)); }
catch (const std::exception &e) { throw; }
};
object_parsers["qu"] = [](std::stringstream &ss) -> Object *
{
try { return (new Quad(ss)); }
catch (const std::exception &e) { throw; }
};
object_parsers["tr"] = [](std::stringstream &ss) -> Object *
{
try { return (new Triangle(ss)); }
catch (const std::exception &e) { throw; }
};
object_parsers["cu"] = [](std::stringstream &ss) -> Object *
{
try { return (new Cube(ss)); }
catch (const std::exception &e) { throw; }
};
object_parsers["po"] = [](std::stringstream &ss) -> Object *
{
try { return (new Portal(ss)); }
catch (const std::exception &e) { throw; }
};
object_parsers["cy"] = [](std::stringstream &ss) -> Object *
{
try { return (new Cylinder(ss)); }
catch (const std::exception &e) { throw; }
};
} }
void SceneParser::parseMaterial(std::stringstream &line) void SceneParser::parseMaterial(std::stringstream &line)