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

submission hw3 #11

Open
wants to merge 5 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
46 changes: 42 additions & 4 deletions src/BSPNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,50 @@ class CBSPNode
*/
bool intersect(Ray& ray, double t0, double t1) const
{
CBSPNode Node1;
CBSPNode Node2;
if (isLeaf()) {
// --- PUT YOUR CODE HERE ---
return false;
bool hit = false;
for (auto& pPrim : m_vpPrims)
hit |= pPrim->intersect(ray);
return hit;
} else {
// --- PUT YOUR CODE HERE ---
return false;

double dis = (m_splitVal - ray.org[m_splitDim]) / ray.dir[m_splitDim];



if (ray.dir[m_splitDim] < 0) {
Node1 = Right();
}
else {
Node1 = Left();

}


if (ray.dir[m_splitDim] < 0) {
Node2 = Left();
}
else {
Node2 = Right();
}

if (dis <= t0) {
return Node2->intersect(ray, t0, t1);
}
else if (dis >= t1) {
return Node1->intersect(ray, t0, t1);
}
else if (Node1->intersect(ray, t0, dis)) {
return true;
}
else
{

return Node2->intersect(ray, dis, t1);
}

}
}

Expand Down
12 changes: 10 additions & 2 deletions src/BSPTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ class CBSPTree
*/
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return false;
double dist = 0;
double dist2 = ray.t;

m_treeBoundingBox.clip(ray, dist, dist2);
if (dist < dist2) {
return false;
}
else {
return m_root->intersect(ray, dist, dist2);
}
}


Expand Down
97 changes: 93 additions & 4 deletions src/BoundingBox.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "BoundingBox.h"
#include "BoundingBox.h"
#include "ray.h"

namespace {
Expand All @@ -16,28 +16,117 @@ namespace {
void CBoundingBox::extend(const Vec3f& p)
{
// --- PUT YOUR CODE HERE ---
this->m_maxPoint[0] = max(p[0], this->m_maxPoint[0]);
this->m_minPoint[0] = min(p[0], this->m_minPoint[0]);

this->m_maxPoint[1] = max(p[1], this->m_maxPoint[1]);
this->m_minPoint[1] = min(p[1], this->m_minPoint[1]);

this->m_maxPoint[2] = max(p[2], this->m_maxPoint[2]);
this->m_minPoint[2] = min(p[2], this->m_minPoint[2]);

}

void CBoundingBox::extend(const CBoundingBox& box)
{
this->extend(box.m_minPoint);
this->extend(box.m_maxPoint);
// --- PUT YOUR CODE HERE ---
}

std::pair<CBoundingBox, CBoundingBox> CBoundingBox::split(int dim, float val) const
{
// --- PUT YOUR CODE HERE ---
auto res = std::make_pair(*this, *this);

CBoundingBox box1;
CBoundingBox box2;

box1.m_maxPoint = this->m_maxPoint;
box1.m_maxPoint[dim] = this->m_maxPoint[dim] - val;
box1.m_minPoint = this->m_minPoint;

box2.m_minPoint = this->m_minPoint;
box2.m_minPoint[dim] = this->m_minPoint[dim] + val;
box2.m_maxPoint = box2.m_maxPoint;

auto res = std::make_pair(box1, box2);

return res;
}

bool CBoundingBox::overlaps(const CBoundingBox& box) const
{
// --- PUT YOUR CODE HERE ---
return false;
if (this->m_minPoint[0] < box.m_maxPoint[0] && box.m_minPoint[0] < this->m_maxPoint[0] &&
this->m_minPoint[1] < box.m_maxPoint[1] && box.m_minPoint[1] < this->m_maxPoint[1] &&
this->m_minPoint[2] < box.m_maxPoint[2] && box.m_minPoint[2] < this->m_maxPoint[2])
{

return true;
}
else
{
return false;
}
}

void CBoundingBox::clip(const Ray& ray, double& t0, double& t1) const
{
// --- PUT YOUR CODE HERE ---

Vec3f vec0, vec1;

float f_dim = std::numeric_limits<float>::infinity() * (-1);
float s_dim = std::numeric_limits<float>::infinity();

for (int i = 0; i < 3; i++) {
if (ray.dir[i] == 0) {


vec0[i] = (this->m_minPoint[i] - ray.org[i]) / ray.dir[i];
vec1[i] = (this->m_maxPoint[i] - ray.org[i]) / ray.dir[i];

if (s_dim<0 || f_dim > s_dim) {
t0 = std::numeric_limits<float>::infinity();
t1 = std::numeric_limits<float>::infinity();
return;
}
if (vec0[i] > f_dim) {
f_dim = vec0[i];
}
if (vec1[i] < s_dim) {
s_dim = vec1[i];
}
if (vec0[i] > vec1[i]) {
auto tmp = vec1;
vec1 = vec0;
vec0 = tmp;
}

}
else {

vec0[i] = (this->m_minPoint[i] - ray.org[i]) / ray.dir[i];
vec1[i] = (this->m_maxPoint[i] - ray.org[i]) / ray.dir[i];
if (s_dim<0 || f_dim >s_dim) {
t0 = std::numeric_limits<float>::infinity();
t1 = std::numeric_limits<float>::infinity();
return;
}
if (vec0[i] > f_dim) {
f_dim = vec0[i];
}
if (vec1[i] < s_dim) {
s_dim = vec1[i];
}
if (vec0[i] > vec1[i]) {
auto tmp = vec1;
vec1 = vec0;
vec0 = tmp;
}

}
}
t0 = f_dim;
t1 = s_dim;
}

1 change: 0 additions & 1 deletion src/BoundingBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class CBoundingBox
*/
Vec3f getMaxPoint(void) const { return m_maxPoint; }

private:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you delete this line ?

Copy link
Author

Choose a reason for hiding this comment

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

because I was using one of the private variables in another class and it was not accessible. I know I could have used static casting or get method, but this was giving me an error at submission

Vec3f m_minPoint; ///< The minimal point defying the size of the bounding box
Vec3f m_maxPoint; ///< The maximal point defying the size of the bounding box
};
6 changes: 5 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ class CPrimPlane : public IPrim

virtual CBoundingBox getBoundingBox(void) const override
{
CBoundingBox bounds;
CBoundingBox bounds = CBoundingBox(m_origin,m_origin);
// --- PUT YOUR CODE HERE ---

Copy link
Contributor

Choose a reason for hiding this comment

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

The case when the plane is orthogonal to one of the axises?

bounds.m_minPoint = std::numeric_limits<Vec3f>::infinity() * (-1);
bounds.m_maxPoint = std::numeric_limits<Vec3f>::infinity();

return bounds;
}

Expand Down
3 changes: 3 additions & 0 deletions src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class CPrimSphere : public IPrim
{
CBoundingBox res;
// --- PUT YOUR CODE HERE ---
res.extend(m_origin + Vec3f(m_radius, m_radius, m_radius));
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to add the -radius as well

res.m_minPoint = std::numeric_limits<Vec3f>::infinity();
res.m_maxPoint = std::numeric_limits<Vec3f>::infinity() * (-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add these two lines ?

return res;
}

Expand Down
8 changes: 8 additions & 0 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ class CPrimTriangle : public IPrim

virtual CBoundingBox getBoundingBox(void) const override
{



CBoundingBox res;
// --- PUT YOUR CODE HERE ---
res.extend(m_a);
res.extend(m_b);
res.extend(m_c);
res.m_minPoint = std::numeric_limits<Vec3f>::infinity();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add these two lines ?

res.m_maxPoint = std::numeric_limits<Vec3f>::infinity() * (-1);
return res;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ class CScene
*/
void add(const CSolid& solid)
{
// --- PUT YOUR CODE HERE ---
for (const auto& pPrim : solid.getPrims())
m_vpPrims.push_back(pPrim);
}


/**
* @brief (Re-) Build the BSP tree for the current geometry present in scene
* @details This function takes into accound all the primitives in scene and builds the BSP tree with the root node in \b m_pBSPTree variable.
Expand Down
22 changes: 12 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Mat RenderFrame(void)
{
// Camera resolution
const Size resolution(800, 600);

// Define a scene
CScene scene;

// Add camera to scene
scene.add(std::make_shared<CCameraPerspective>(resolution, Vec3f(0, 3.5f, -13), Vec3f(0, 0, 1), Vec3f(0, 1, 0), 60));

Expand All @@ -39,25 +39,27 @@ Mat RenderFrame(void)

// Build BSPTree
scene.buildAccelStructure(20, 3);

Vec3f pointLightIntensity(3, 3, 3);
Vec3f lightPosition2(-3, 5, 4);
Vec3f lightPosition3(0, 1, 4);

scene.add(std::make_shared<CLightOmni>(pointLightIntensity, lightPosition2));
scene.add(std::make_shared<CLightOmni>(pointLightIntensity, lightPosition3));

Mat img(resolution, CV_32FC3); // image array
Ray ray; // primary ray
Copy link
Contributor

Choose a reason for hiding this comment

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

Ray should lie inside the parallel part of the code


for (int y = 0; y < img.rows; y++)
for (int x = 0; x < img.cols; x++) {
scene.getActiveCamera()->InitRay(ray, x, y); // initialize ray
img.at<Vec3f>(y, x) = scene.RayTrace(ray);
}

parallel_for_(cv::Range(0, img.rows), [&](const Range& r) {
for (int y = r.start; y < r.end; y++)
for (int x = 0; x < img.cols; x++) {
scene.getActiveCamera()->InitRay(ray, x, y); // initialize ray
img.at<Vec3f>(y, x) = scene.RayTrace(ray);
}
});
img.convertTo(img, CV_8UC3, 255);
return img;

}

int main(int argc, char* argv[])
Expand Down