Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment 4 submission #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Dealine**: 12.11.2020

Please put your name here:
**Name:** .......
**Name:** Adnan Abu Ramadan
## Problem 1
### Sphere Solid (Points 25)
In this assignment we will continue working with _compound objects_: solids.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanations in problem 2 ?

Expand Down
Binary file added renders/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/problem2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/problem3&4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/problem5bonus.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/render 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
228 changes: 114 additions & 114 deletions src/BSPTree.h
Original file line number Diff line number Diff line change
@@ -1,114 +1,114 @@
#pragma once

#include "BSPNode.h"
#include "BoundingBox.h"
#include "IPrim.h"
#include "ray.h"

namespace {
// Calculates and return the bounding box, containing the whole scene
CBoundingBox calcBoundingBox(const std::vector<ptr_prim_t>& vpPrims)
{
CBoundingBox res;
for (auto pPrim : vpPrims)
res.extend(pPrim->getBoundingBox());
return res;
}

// Returns the best dimension index for next split
int MaxDim(const Vec3f& v)
{
return (v.val[0] > v.val[1]) ? ((v.val[0] > v.val[2]) ? 0 : 2) : ((v.val[1] > v.val[2]) ? 1 : 2);
}
}

// ================================ BSP Tree Class ================================
/**
* @brief Binary Space Partitioning (BSP) tree class
*/
class CBSPTree
{
public:
CBSPTree(void) = default;
CBSPTree(const CBSPTree&) = delete;
~CBSPTree(void) = default;
const CBSPTree& operator=(const CBSPTree&) = delete;

/**
* @brief Builds the BSP tree for the primitives provided via \b vpPrims
* @param vpPrims The vector of pointers to the primitives in the scene
* @param maxDepth The maximum allowed depth of the tree.
* Increasing the depth of the tree may speed-up rendering, but increse the memory consumption.
* @param minPrimitives The minimum number of primitives in a leaf-node.
* This parameters should be alway above 1.
*/
void build(const std::vector<ptr_prim_t>& vpPrims, size_t maxDepth = 20, size_t minPrimitives = 3) {
m_treeBoundingBox = calcBoundingBox(vpPrims);
m_maxDepth = maxDepth;
m_minPrimitives = minPrimitives;
std::cout << "Scene bounds are : " << m_treeBoundingBox << std::endl;
m_root = build(m_treeBoundingBox, vpPrims, 0);
}
/**
* @brief Checks whether the ray \b ray intersects a primitive.
* @details If ray \b ray intersects a primitive, the \b ray.t value will be updated
* @param[in,out] ray The ray
*/
bool intersect(Ray& ray) const
{
double t0 = 0;
double t1 = ray.t;
m_treeBoundingBox.clip(ray, t0, t1);
if (t1 < t0) return false; // no intersection with the bounding box

return m_root->intersect(ray, t0, t1);
}


private:
/**
* @brief Builds the BSP tree
* @details This function builds the BSP tree recursively
* @param box The bounding box containing all the scene primitives
* @param vpPrims The vector of pointers to the primitives included in the bounding box \b box
* @param depth The distance from the root node of the tree
*/
ptr_bspnode_t build(const CBoundingBox& box, const std::vector<ptr_prim_t>& vpPrims, size_t depth)
{
// Check for stoppong criteria
if (depth >= m_maxDepth || vpPrims.size() <= m_minPrimitives)
return std::make_shared<CBSPNode>(vpPrims); // => Create a leaf node and break recursion

// else -> prepare for creating a branch node
// First split the bounding volume into two halfes
int splitDim = MaxDim(box.getMaxPoint() - box.getMinPoint()); // Calculate split dimension as the dimension where the aabb is the widest
float splitVal = (box.getMinPoint()[splitDim] + box.getMaxPoint()[splitDim]) / 2; // Split the aabb exactly in two halfes
auto splitBoxes = box.split(splitDim, splitVal);
CBoundingBox& lBox = splitBoxes.first;
CBoundingBox& rBox = splitBoxes.second;

// Second order the primitives into new nounding boxes
std::vector<ptr_prim_t> lPrim;
std::vector<ptr_prim_t> rPrim;
for (auto pPrim : vpPrims) {
if (pPrim->getBoundingBox().overlaps(lBox))
lPrim.push_back(pPrim);
if (pPrim->getBoundingBox().overlaps(rBox))
rPrim.push_back(pPrim);
}

// Next build recursively 2 subtrees for both halfes
auto pLeft = build(lBox, lPrim, depth + 1);
auto pRight = build(rBox, rPrim, depth + 1);

return std::make_shared<CBSPNode>(splitDim, splitVal, pLeft, pRight);
}


private:
CBoundingBox m_treeBoundingBox; ///<
size_t m_maxDepth; ///< The maximum allowed depth of the tree
size_t m_minPrimitives; ///< The minimum number of primitives in a leaf-node
ptr_bspnode_t m_root = nullptr; ///<
};

#pragma once
#include "BSPNode.h"
#include "BoundingBox.h"
#include "IPrim.h"
#include "ray.h"
namespace {
// Calculates and return the bounding box, containing the whole scene
CBoundingBox calcBoundingBox(const std::vector<ptr_prim_t>& vpPrims)
{
CBoundingBox res;
for (auto pPrim : vpPrims)
res.extend(pPrim->getBoundingBox());
return res;
}
// Returns the best dimension index for next split
int MaxDim(const Vec3f& v)
{
return (v.val[0] > v.val[1]) ? ((v.val[0] > v.val[2]) ? 0 : 2) : ((v.val[1] > v.val[2]) ? 1 : 2);
}
}
// ================================ BSP Tree Class ================================
/**
* @brief Binary Space Partitioning (BSP) tree class
*/
class CBSPTree
{
public:
CBSPTree(void) = default;
CBSPTree(const CBSPTree&) = delete;
~CBSPTree(void) = default;
const CBSPTree& operator=(const CBSPTree&) = delete;
/**
* @brief Builds the BSP tree for the primitives provided via \b vpPrims
* @param vpPrims The vector of pointers to the primitives in the scene
* @param maxDepth The maximum allowed depth of the tree.
* Increasing the depth of the tree may speed-up rendering, but increse the memory consumption.
* @param minPrimitives The minimum number of primitives in a leaf-node.
* This parameters should be alway above 1.
*/
void build(const std::vector<ptr_prim_t>& vpPrims, size_t maxDepth = 20, size_t minPrimitives = 3) {
m_treeBoundingBox = calcBoundingBox(vpPrims);
m_maxDepth = maxDepth;
m_minPrimitives = minPrimitives;
std::cout << "Scene bounds are : " << m_treeBoundingBox << std::endl;
m_root = build(m_treeBoundingBox, vpPrims, 0);
}
/**
* @brief Checks whether the ray \b ray intersects a primitive.
* @details If ray \b ray intersects a primitive, the \b ray.t value will be updated
* @param[in,out] ray The ray
*/
bool intersect(Ray& ray) const
{
double t0 = 0;
double t1 = ray.t;
m_treeBoundingBox.clip(ray, t0, t1);
if (t1 < t0) return false; // no intersection with the bounding box
return m_root->intersect(ray, t0, t1);
}
private:
/**
* @brief Builds the BSP tree
* @details This function builds the BSP tree recursively
* @param box The bounding box containing all the scene primitives
* @param vpPrims The vector of pointers to the primitives included in the bounding box \b box
* @param depth The distance from the root node of the tree
*/
ptr_bspnode_t build(const CBoundingBox& box, const std::vector<ptr_prim_t>& vpPrims, size_t depth)
{
// Check for stoppong criteria
if (depth >= m_maxDepth || vpPrims.size() <= m_minPrimitives)
return std::make_shared<CBSPNode>(vpPrims); // => Create a leaf node and break recursion
// else -> prepare for creating a branch node
// First split the bounding volume into two halfes
int splitDim = MaxDim(box.getMaxPoint() - box.getMinPoint()); // Calculate split dimension as the dimension where the aabb is the widest
float splitVal = (box.getMinPoint()[splitDim] + box.getMaxPoint()[splitDim]) / 2; // Split the aabb exactly in two halfes
auto splitBoxes = box.split(splitDim, splitVal);
CBoundingBox& lBox = splitBoxes.first;
CBoundingBox& rBox = splitBoxes.second;
// Second order the primitives into new nounding boxes
std::vector<ptr_prim_t> lPrim;
std::vector<ptr_prim_t> rPrim;
for (auto pPrim : vpPrims) {
if (pPrim->getBoundingBox().overlaps(lBox))
lPrim.push_back(pPrim);
if (pPrim->getBoundingBox().overlaps(rBox))
rPrim.push_back(pPrim);
}
// Next build recursively 2 subtrees for both halfes
auto pLeft = build(lBox, lPrim, depth + 1);
auto pRight = build(rBox, rPrim, depth + 1);
return std::make_shared<CBSPNode>(splitDim, splitVal, pLeft, pRight);
}
private:
CBoundingBox m_treeBoundingBox; ///<
size_t m_maxDepth; ///< The maximum allowed depth of the tree
size_t m_minPrimitives; ///< The minimum number of primitives in a leaf-node
ptr_bspnode_t m_root = nullptr; ///<
};
Loading