-
Notifications
You must be signed in to change notification settings - Fork 46
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
aaburamadan
wants to merge
3
commits into
Jacobs-University:master
Choose a base branch
from
aaburamadan:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; ///< | ||
}; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explanations in problem 2 ?