From 3b2bffb3daa11363e84427496e56624ab5e909e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?arvid=20schr=C3=B6der?= Date: Mon, 21 Nov 2022 17:20:15 +0100 Subject: [PATCH] Added bounds checking --- primitive/objmodel.cpp | 55 ++++++++++++++++++++++++++++++++++++------ primitive/objmodel.h | 6 ++++- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/primitive/objmodel.cpp b/primitive/objmodel.cpp index cd805b6..8c82d0d 100644 --- a/primitive/objmodel.cpp +++ b/primitive/objmodel.cpp @@ -6,8 +6,9 @@ #include #include #include "objmodel.h" +#include "shader/flatshader.h" -void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d translation) { +void ObjModel::loadObj(const std::string &fileName, Vector3d scale, Vector3d translation) { std::vector vertices; std::vector vertex_normals; @@ -27,7 +28,7 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra vertex_normals.emplace_back(x, y, z); } else if (key == "f") { int x, y, z, - xn, yn, zn = 0; + xn, yn, zn = 0; { std::string token; std::getline(file, token, '/'); @@ -53,7 +54,8 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra file.ignore(1); file >> zn; - this->faces.emplace_back(vertices[x - 1] * scale + translation, vertices[y - 1] * scale + translation, vertices[z - 1] * scale + translation, + this->faces.emplace_back(vertices[x - 1] * scale + translation, vertices[y - 1] * scale + translation, + vertices[z - 1] * scale + translation, vertex_normals[xn - 1], vertex_normals[yn - 1], vertex_normals[zn - 1], this->shader()); } else if (key == "#") { @@ -63,23 +65,60 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra } } file.close(); + + Vector3d minBounds; + Vector3d maxBounds; + + auto minCalc = [this](int dimension) { + return std::min_element(this->faces.begin(), + this->faces.end(), + [&dimension](auto &face1, auto &face2) { + return face1.minimumBounds(dimension) < face2.minimumBounds(dimension); + })->minimumBounds(dimension); + }; + minBounds.x = minCalc(0); + minBounds.y = minCalc(1); + minBounds.z = minCalc(2); + + auto maxCalc = [this](int dimension) { + return std::max_element(this->faces.begin(), + this->faces.end(), + [&dimension](auto &face1, auto &face2) { + return face1.maximumBounds(dimension) < face2.maximumBounds(dimension); + })->maximumBounds(dimension); + }; + maxBounds.x = maxCalc(0); + maxBounds.y = maxCalc(1); + maxBounds.z = maxCalc(2); + + boundingBox.setCenter(minBounds + (maxBounds - minBounds) / 2); + boundingBox.setSize(maxBounds - minBounds); } } bool ObjModel::intersect(Ray &ray) const { + auto testRay = Ray(ray.origin, ray.direction); + if (!boundingBox.intersect(testRay)) { + return false; + } return std::count_if(this->faces.begin(), this->faces.end(), - [&ray](auto& face){ - return face.intersect(ray); - }) > 0; + [&ray](auto &face) { + return face.intersect(ray); + }) > 0; } float ObjModel::minimumBounds(int dimension) const { - return 0; + return this->boundingBox.minimumBounds(dimension); } float ObjModel::maximumBounds(int dimension) const { - return 0; + return this->boundingBox.maximumBounds(dimension); +} + +ObjModel::ObjModel(const std::shared_ptr &shader) : + Primitive(shader), + boundingBox(Box(std::make_shared(Color(0, 0, 0)))) { } diff --git a/primitive/objmodel.h b/primitive/objmodel.h index c2629cb..cbce7b9 100644 --- a/primitive/objmodel.h +++ b/primitive/objmodel.h @@ -3,12 +3,13 @@ #include "primitive.h" #include "triangle.h" +#include "box.h" #include #include class ObjModel : public Primitive { public: - explicit ObjModel(const std::shared_ptr &shader) : Primitive(shader) {} + explicit ObjModel(const std::shared_ptr &shader); void loadObj(const std::string& fileName, Vector3d translation, Vector3d upVector); bool intersect(Ray &ray) const override; @@ -18,6 +19,9 @@ public: protected: std::vector faces; + Box boundingBox; + + }; #endif //CG1_TRACER_OBJMODEL_H