From 819c07f90d0edc8e1bf395896005626db39ecdad Mon Sep 17 00:00:00 2001 From: RedShip Date: Mon, 13 Jan 2025 18:50:55 +0100 Subject: [PATCH] ~ | removing useless trycatch --- includes/RT/objects/Cube.hpp | 29 +++++++-------- includes/RT/objects/Cylinder.hpp | 39 ++++++++++---------- includes/RT/objects/Plane.hpp | 29 +++++++-------- includes/RT/objects/Portal.hpp | 61 +++++++++++++++----------------- includes/RT/objects/Quad.hpp | 35 +++++++++--------- includes/RT/objects/Sphere.hpp | 29 +++++++-------- includes/RT/objects/Triangle.hpp | 53 +++++++++++++-------------- srcs/class/SceneParser.cpp | 52 +++++---------------------- 8 files changed, 136 insertions(+), 191 deletions(-) diff --git a/includes/RT/objects/Cube.hpp b/includes/RT/objects/Cube.hpp index 8e4c0b6..e2899c1 100644 --- a/includes/RT/objects/Cube.hpp +++ b/includes/RT/objects/Cube.hpp @@ -20,26 +20,23 @@ class Cube : public Object public: Cube(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float width, height, depth; - int mat_index; + float x, y, z; + float width, height, depth; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing position values"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position values"); - if (!(line >> width >> height >> depth)) - throw std::runtime_error("Missing width, height or depth values"); + if (!(line >> width >> height >> depth)) + throw std::runtime_error("Missing width, height or depth values"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); - _size = glm::vec3(width, height, depth); - - _mat_index = mat_index; - } - catch (const std::exception& e) { throw; } + _position = glm::vec3(x, y, z); + _size = glm::vec3(width, height, depth); + + _mat_index = mat_index; } Cube(const glm::vec3& position, const glm::vec3 &size, const int mat_index) : Object(position, mat_index), _size(size) {} diff --git a/includes/RT/objects/Cylinder.hpp b/includes/RT/objects/Cylinder.hpp index 25e59a4..b317fd8 100644 --- a/includes/RT/objects/Cylinder.hpp +++ b/includes/RT/objects/Cylinder.hpp @@ -20,35 +20,32 @@ class Cylinder : public Object public: Cylinder(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float radius, height; - float pitch, yaw, roll; + float x, y, z; + float radius, height; + float pitch, yaw, roll; - int mat_index; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing position"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position"); - if (!(line >> radius >> height)) - throw std::runtime_error("Missing radius or height values"); + if (!(line >> radius >> height)) + throw std::runtime_error("Missing radius or height values"); - if (!(line >> pitch >> yaw >> roll)) - throw std::runtime_error("Missing rotation values"); + if (!(line >> pitch >> yaw >> roll)) + throw std::runtime_error("Missing rotation values"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); + _position = glm::vec3(x, y, z); - _radius = radius; - _height = height; - - _rotation = glm::mat3(glm::eulerAngleXYZ(pitch, yaw, roll)); + _radius = radius; + _height = height; + + _rotation = glm::mat3(glm::eulerAngleXYZ(pitch, yaw, roll)); - _mat_index = mat_index; - } - catch (const std::exception& e) { throw; } + _mat_index = mat_index; } Cylinder(const glm::vec3& position, float radius, const int mat_index) : Object(position, mat_index), _radius(radius) {} diff --git a/includes/RT/objects/Plane.hpp b/includes/RT/objects/Plane.hpp index 8d8947f..5715174 100644 --- a/includes/RT/objects/Plane.hpp +++ b/includes/RT/objects/Plane.hpp @@ -20,26 +20,23 @@ class Plane : public Object public: Plane(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float nx, ny, nz; - int mat_index; + float x, y, z; + float nx, ny, nz; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing position"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position"); - if (!(line >> nx >> ny >> nz)) - throw std::runtime_error("Missing plane's normal"); + if (!(line >> nx >> ny >> nz)) + throw std::runtime_error("Missing plane's normal"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); - _normal = glm::vec3(nx, ny, nz); - - _mat_index = mat_index; - } - catch (const std::exception &e) { throw; } + _position = glm::vec3(x, y, z); + _normal = glm::vec3(nx, ny, nz); + + _mat_index = mat_index; } Plane(const glm::vec3 &position, const glm::vec3 &normal, const int mat_index) : Object(position, mat_index), _normal(normal) {} diff --git a/includes/RT/objects/Portal.hpp b/includes/RT/objects/Portal.hpp index de274a9..dd5561b 100644 --- a/includes/RT/objects/Portal.hpp +++ b/includes/RT/objects/Portal.hpp @@ -6,7 +6,7 @@ /* 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: Portal(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float x1, y1, z1; - float x2, y2, z2; - - bool invert_normal; + float x, y, z; + float x1, y1, z1; + float x2, y2, z2; + + bool invert_normal; - int mat_index; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing position"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position"); - if (!(line >> x1 >> y1 >> z1)) - throw std::runtime_error("Missing Portal's first edge "); + if (!(line >> x1 >> y1 >> z1)) + throw std::runtime_error("Missing Portal's first edge "); - if (!(line >> x2 >> y2 >> z2)) - throw std::runtime_error("Missing Portal's second edge"); - - if (!(line >> invert_normal)) - throw std::runtime_error("Missing invert_normal"); + if (!(line >> x2 >> y2 >> z2)) + throw std::runtime_error("Missing Portal's second edge"); + + if (!(line >> invert_normal)) + throw std::runtime_error("Missing invert_normal"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); - _up = glm::vec3(x1, y1, z1); - _right = glm::vec3(x2, y2, z2); + _position = glm::vec3(x, y, z); + _up = glm::vec3(x1, y1, z1); + _right = glm::vec3(x2, y2, z2); - glm::vec3 up = glm::normalize(_up); - glm::vec3 right = glm::normalize(_right); - glm::vec3 forward = glm::normalize(glm::cross(right, up)); + glm::vec3 up = glm::normalize(_up); + glm::vec3 right = glm::normalize(_right); + 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); - _normal = forward * (invert_normal ? -1.0f : 1.0f); + _rotation = glm::mat3(right, up, forward); + _normal = forward * (invert_normal ? -1.0f : 1.0f); - _linked_portal = -1; + _linked_portal = -1; - _mat_index = mat_index; - } - catch (const std::exception &e) { throw; } + _mat_index = 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) {} diff --git a/includes/RT/objects/Quad.hpp b/includes/RT/objects/Quad.hpp index 851f2a0..3df3ff1 100644 --- a/includes/RT/objects/Quad.hpp +++ b/includes/RT/objects/Quad.hpp @@ -20,31 +20,28 @@ class Quad : public Object public: Quad(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float x1, y1, z1; - float x2, y2, z2; - int mat_index; + float x, y, z; + float x1, y1, z1; + float x2, y2, z2; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing position"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position"); - if (!(line >> x1 >> y1 >> z1)) - throw std::runtime_error("Missing quad's first edge "); + if (!(line >> x1 >> y1 >> z1)) + throw std::runtime_error("Missing quad's first edge "); - if (!(line >> x2 >> y2 >> z2)) - throw std::runtime_error("Missing quad's second edge"); + if (!(line >> x2 >> y2 >> z2)) + throw std::runtime_error("Missing quad's second edge"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); - _up = glm::vec3(x1, y1, z1); - _right = glm::vec3(x2, y2, z2); + _position = glm::vec3(x, y, z); + _up = glm::vec3(x1, y1, z1); + _right = glm::vec3(x2, y2, z2); - _mat_index = mat_index; - } - catch (const std::exception &e) { throw; } + _mat_index = 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) {} diff --git a/includes/RT/objects/Sphere.hpp b/includes/RT/objects/Sphere.hpp index babb4dc..edea856 100644 --- a/includes/RT/objects/Sphere.hpp +++ b/includes/RT/objects/Sphere.hpp @@ -6,7 +6,7 @@ /* 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: Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z, radius; - int mat_index; + float x, y, z, radius; + int mat_index; - if (!(line >> x >> y >> z >> radius)) - throw std::runtime_error("Missing position or radius values"); + if (!(line >> x >> y >> z >> radius)) + throw std::runtime_error("Missing position or radius values"); - if (radius <= 0.0f) - throw std::runtime_error("Radius must be positive"); + if (radius <= 0.0f) + throw std::runtime_error("Radius must be positive"); - if (!(line >> mat_index)) - throw std::runtime_error("Missing material properties"); + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); - _position = glm::vec3(x, y, z); - _radius = radius / 2.0; - - _mat_index = mat_index; - } - catch (const std::exception& e) { throw; } + _position = glm::vec3(x, y, z); + _radius = radius / 2.0; + + _mat_index = mat_index; } Sphere(const glm::vec3& position, float radius, const int mat_index) : Object(position, mat_index), _radius(radius) {} diff --git a/includes/RT/objects/Triangle.hpp b/includes/RT/objects/Triangle.hpp index 16bf763..d47e0ee 100644 --- a/includes/RT/objects/Triangle.hpp +++ b/includes/RT/objects/Triangle.hpp @@ -6,7 +6,7 @@ /* 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: Triangle(std::stringstream &line) : Object(glm::vec3(0.0f), -1) { - try { - float x, y, z; - float x2, y2, z2; - float x3, y3, z3; + float x, y, z; + float x2, y2, z2; + float x3, y3, z3; - int mat_index; + int mat_index; - if (!(line >> x >> y >> z)) - throw std::runtime_error("Missing first vertex position"); + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing first vertex position"); - if (!(line >> x2 >> y2 >> z2)) - 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 + if (!(line >> x2 >> y2 >> z2)) + throw std::runtime_error("Missing second vertex position"); - _mat_index = mat_index; - } - catch (const std::exception &e) { throw; } + 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; } 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) { diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index cb7499a..786343d 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* SceneParser.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { - object_parsers["sp"] = [](std::stringstream &ss) -> Object * - { - try { return (new Sphere(ss)); } - catch (const std::exception &e) { throw; } - }; - - object_parsers["pl"] = [](std::stringstream &ss) -> Object * - { - 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; } - }; + object_parsers["sp"] = [](std::stringstream &ss) -> Object * { return (new Sphere(ss)); }; + object_parsers["pl"] = [](std::stringstream &ss) -> Object * { return (new Plane(ss)); }; + object_parsers["qu"] = [](std::stringstream &ss) -> Object * { return (new Quad(ss)); }; + 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["cy"] = [](std::stringstream &ss) -> Object * { return (new Cylinder(ss)); }; } void SceneParser::parseMaterial(std::stringstream &line)