Skip to content

Commit

Permalink
Implementing translation and rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 26, 2024
1 parent c976952 commit a006885
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ class Aabb {
}

};

inline Aabb operator+(const Aabb& bbox, const Vector& offset) {
return Aabb(bbox.x() + offset.x(), bbox.y() + offset.y(), bbox.z() + offset.z());
}

inline Aabb operator+(const Vector& offset, const Aabb& bbox) {
return bbox + offset;
}
73 changes: 73 additions & 0 deletions src/intersectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,76 @@ class Intersectable {
virtual Aabb boundingBox() const = 0;

};

class Translate : public Intersectable {

private:

Vector _offset;
std::shared_ptr<Intersectable> _intersectable;
Aabb _bbox;

public:

Translate(const std::shared_ptr<Intersectable>& intersectable, const Vector& offset):
_intersectable(intersectable),_offset(offset) {
_bbox = _intersectable->boundingBox() + _offset;
}

bool intersects(const Ray &ray, const Interval &interval, Hit &hit) const override {
Ray offset_r(ray.orig() - _offset, ray.dir(), ray.time());
if (! _intersectable->intersects(offset_r, interval, hit)) return false;
hit.setPoint(hit.point() + _offset);
return true;
}

Aabb boundingBox() const override {
return _bbox;
}

};

class RotateY : public Intersectable {

private:

std::shared_ptr<Intersectable> _intersectable;
double _sinTheta;
double _cosTheta;
Aabb _bbox;

public:

bool intersects(const Ray &ray, const Interval &interval, Hit &hit) const override {
// Change the ray from world space to object space
auto origin = ray.orig();
auto direction = ray.dir();

origin[0] = _cosTheta*ray.orig()[0] - _sinTheta*ray.orig()[2];
origin[2] = _sinTheta*ray.orig()[0] + _cosTheta*ray.orig()[2];

direction[0] = _cosTheta*ray.dir()[0] - _sinTheta*ray.dir()[2];
direction[2] = _sinTheta*ray.dir()[0] + _cosTheta*ray.dir()[2];

Ray rotated_r(origin, direction, ray.time());

// Determine where (if any) an intersection occurs in object space
if (!_intersectable->intersects(rotated_r, interval, hit)) return false;

// Change the intersection point from object space to world space
auto p = hit.point();
p[0] = _cosTheta*hit.point()[0] + _sinTheta*hit.point()[2];
p[2] = -_sinTheta*hit.point()[0] + _cosTheta*hit.point()[2];

// Change the normal from object space to world space
auto normal = hit.normal();
normal[0] = _cosTheta*hit.normal()[0] + _sinTheta*hit.normal()[2];
normal[2] = -_sinTheta*hit.normal()[0] + _cosTheta*hit.normal()[2];

hit.setPoint(p);
hit.setNormal(normal);

return true;
}

};
8 changes: 8 additions & 0 deletions src/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ class Interval {
double size() const { return _max - _min; }

};

inline Interval operator+(const Interval& ival, double displacement) {
return Interval(ival.min() + displacement, ival.max() + displacement);
}

inline Interval operator+(double displacement, const Interval& ival) {
return ival + displacement;
}
1 change: 1 addition & 0 deletions src/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Vector {
inline double g() const { return _c[1]; }
inline double b() const { return _c[2]; }

inline double& operator[](int index) { return _c[index]; }
inline double operator[](int index) const { return _c[index]; }

Vector operator-() const { return Vector(-x(), -y(), -z()); }
Expand Down

0 comments on commit a006885

Please sign in to comment.