Skip to content

Commit

Permalink
Started working on boundingboxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Godzil committed Feb 24, 2020
1 parent d1965ca commit 3011544
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 3 deletions.
21 changes: 21 additions & 0 deletions source/include/boundingbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Bounding box header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_BOUNDINGBOX_H
#define DORAYME_BOUNDINGBOX_H

struct BoundingBox
{
Tuple min;
Tuple max;

BoundingBox() : min(-0, -0, -0), max(0, 0, 0) { };
BoundingBox(Tuple min, Tuple max) : min(min), max(max) { };
};

#endif //DORAYME_BOUNDINGBOX_H
1 change: 1 addition & 0 deletions source/include/cone.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Cone : public Shape {
double maxCap;

Cone() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CONE) {};
BoundingBox getBounds();
};

#endif /* DORAYME_CONE_H */
2 changes: 2 additions & 0 deletions source/include/cylinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Cylinder : public Shape {
double maxCap;

Cylinder() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CYLINDER) {};

BoundingBox getBounds();
};

#endif //DORAYME_CYLINDER_H
2 changes: 2 additions & 0 deletions source/include/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Group : public Shape

Intersect intersect(Ray r);

BoundingBox getBounds();

Group();
};

Expand Down
1 change: 1 addition & 0 deletions source/include/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Plane : public Shape

public:
Plane() : Shape(SHAPE_PLANE) { };
BoundingBox getBounds();
};

#endif //DORAYME_PLANE_H
7 changes: 5 additions & 2 deletions source/include/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Shape;
#include <matrix.h>
#include <intersect.h>
#include <material.h>
#include <boundingbox.h>

enum ShapeType
{
Expand Down Expand Up @@ -51,13 +52,15 @@ class Shape
public:
Shape(ShapeType = SHAPE_NONE);

Intersect intersect(Ray r);
virtual Intersect intersect(Ray r);
Tuple normalAt(Tuple point);

//virtual Bounds getBounds();
/* Bouding box points are always world value */
virtual BoundingBox getBounds();

void updateTransform();
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
Tuple normalToWorld(Tuple normalVector);

void setTransform(Matrix transform);
Expand Down
4 changes: 4 additions & 0 deletions source/include/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Tuple
double x, y, z, w;

public:
Tuple() : x(0), y(0), z(0), w(0.0) {};
Tuple(double x, double y, double z) : x(x), y(y), z(z), w(0.0) {};
Tuple(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {};
bool isPoint() { return (this->w == 1.0); };
Expand All @@ -39,6 +40,7 @@ class Tuple
Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
this->z / b, this->w / b); };

void set(double nX, double nY, double nZ) { this->x = nX; this->y = nY; this->z = nZ; };
double magnitude();
Tuple normalise();
double dot(const Tuple &b);
Expand All @@ -49,12 +51,14 @@ class Tuple
class Point: public Tuple
{
public:
Point() : Tuple(0, 0, 0, 1.0) {};
Point(double x, double y, double z) : Tuple(x, y, z, 1.0) {};
};

class Vector: public Tuple
{
public:
Vector() : Tuple(0, 0, 0, 0.0) {};
Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
};

Expand Down
10 changes: 10 additions & 0 deletions source/shapes/cone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ Tuple Cone::localNormalAt(Tuple point)
}
return Vector(point.x, y, point.z);
}

BoundingBox Cone::getBounds()
{
BoundingBox ret;

ret.min = this->objectToWorld(Point(-1, this->minCap, -1));
ret.max = this->objectToWorld(Point(1, this->maxCap, 1));

return ret;
}
10 changes: 10 additions & 0 deletions source/shapes/cylinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ Tuple Cylinder::localNormalAt(Tuple point)

return Vector(point.x, 0, point.z);
}

BoundingBox Cylinder::getBounds()
{
BoundingBox ret;

ret.min = this->objectToWorld(Point(-1, this->minCap, -1));
ret.max = this->objectToWorld(Point(1, this->maxCap, 1));

return ret;
}
27 changes: 27 additions & 0 deletions source/shapes/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,31 @@ void Group::addObject(Shape *s)
bool Group::isEmpty()
{
return (this->objectCount == 0);
}

BoundingBox Group::getBounds()
{
BoundingBox ret;

if (this->objectCount > 0)
{
ret.min = Point(INFINITY, INFINITY, INFINITY);
ret.max = Point(-INFINITY, -INFINITY, -INFINITY);

int i;
for(i = 0; i < this->objectCount; i++)
{
BoundingBox obj = this->objectList[i]->getBounds();

if (ret.min.x > obj.min.x) { ret.min.x = obj.min.x; }
if (ret.min.y > obj.min.y) { ret.min.y = obj.min.y; }
if (ret.min.z > obj.min.z) { ret.min.z = obj.min.z; }

if (ret.max.x < obj.max.x) { ret.max.x = obj.max.x; }
if (ret.max.y < obj.max.y) { ret.max.y = obj.max.y; }
if (ret.max.z < obj.max.z) { ret.max.z = obj.max.z; }
}
}

return ret;
}
10 changes: 10 additions & 0 deletions source/shapes/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ Intersect Plane::localIntersect(Ray r)
Tuple Plane::localNormalAt(Tuple point)
{
return Vector(0, 1, 0);
}

BoundingBox Plane::getBounds()
{
BoundingBox ret;

ret.min = this->objectToWorld(Point(-INFINITY, 0-getEpsilon(), -INFINITY));
ret.max = this->objectToWorld(Point(INFINITY, 0+getEpsilon(), INFINITY));

return ret;
}
9 changes: 9 additions & 0 deletions source/shapes/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ void Shape::setTransform(Matrix transform)
this->localTransformMatrix = transform;
this->updateTransform();
}

BoundingBox Shape::getBounds()
{
BoundingBox ret;

ret.min = this->objectToWorld(Point(-1, -1, -1));
ret.max = this->objectToWorld(Point(1, 1, 1));
return ret;
}
13 changes: 12 additions & 1 deletion tests/shape_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,15 @@ TEST(TestShape, Finding_the_normal_on_a_child_object)
ASSERT_EQ(p, Vector(0.2857, 0.4286, -0.8571));

set_equal_precision(FLT_EPSILON);
}
}

TEST(TestShape, Test_the_bouding_box_of_the_test_shape)
{
TestShape t = TestShape();
BoundingBox b = BoundingBox(Point(-1, -1, -1), Point(1, 1, 1));

BoundingBox res = t.getBounds();

ASSERT_EQ(res.min, b.min);
ASSERT_EQ(res.max, b.max);
}

0 comments on commit 3011544

Please sign in to comment.