RayTracer 1.0
Ray tracing is a technique used to generate realistic digital images by simulating the inverse path of light. Our goal is to create a program able to generate an image from a file describing the scene.
 
Loading...
Searching...
No Matches
RayTracer::Cylinder Class Reference

#include <Cylinder.hpp>

Inheritance diagram for RayTracer::Cylinder:
RayTracer::IPrimitive

Public Member Functions

 Cylinder (const Math::Point3D &baseCenter, double radius, double height, std::shared_ptr< IMaterial > material)
 
 ~Cylinder () override=default
 
bool intersect (const Ray &ray, double &t, Math::Point3D &hitPoint, Math::Vector3D &normal) const override
 
void translate (const Math::Vector3D &v) override
 
void rotate (const Math::Vector3D &axis, double angleDegrees) override
 
std::shared_ptr< IMaterialgetMaterial () const override
 
- Public Member Functions inherited from RayTracer::IPrimitive
virtual ~IPrimitive ()=default
 
Color getBaseColor () const
 

Private Attributes

Math::Point3D _baseCenter
 
double _radius
 
double _height
 
Math::Vector3D _axis
 
std::shared_ptr< IMaterial_material
 
Math::Point3D _center
 

Detailed Description

Definition at line 15 of file Cylinder.hpp.

Constructor & Destructor Documentation

◆ Cylinder()

RayTracer::Cylinder::Cylinder ( const Math::Point3D & baseCenter,
double radius,
double height,
std::shared_ptr< IMaterial > material )

Definition at line 10 of file Cylinder.cpp.

11 : _baseCenter(baseCenter), _radius(radius), _height(height), _material(std::move(material))
12{}
std::shared_ptr< IMaterial > _material
Definition Cylinder.hpp:30
Math::Point3D _baseCenter
Definition Cylinder.hpp:26

References _baseCenter, _height, _material, and _radius.

◆ ~Cylinder()

RayTracer::Cylinder::~Cylinder ( )
overridedefault

Member Function Documentation

◆ getMaterial()

std::shared_ptr< RayTracer::IMaterial > RayTracer::Cylinder::getMaterial ( ) const
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 24 of file Cylinder.cpp.

25{
26 return _material;
27}

References _material.

◆ intersect()

bool RayTracer::Cylinder::intersect ( const Ray & ray,
double & t,
Math::Point3D & hitPoint,
Math::Vector3D & normal ) const
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 29 of file Cylinder.cpp.

30{
31 Math::Vector3D oc = ray.origin - _center;
32
33 double a = ray.direction.x * ray.direction.x + ray.direction.z * ray.direction.z;
34 double b = 2.0 * (oc.x * ray.direction.x + oc.z * ray.direction.z);
35 double c = oc.x * oc.x + oc.z * oc.z - _radius * _radius;
36
37 double discriminant = b * b - 4 * a * c;
38 if (discriminant < 0)
39 return false;
40
41 double sqrtDisc = std::sqrt(discriminant);
42 double t0 = (-b - sqrtDisc) / (2 * a);
43 double t1 = (-b + sqrtDisc) / (2 * a);
44
45 if (t0 > t1)
46 std::swap(t0, t1);
47
48 double y0 = oc.y + t0 * ray.direction.y;
49 double y1 = oc.y + t1 * ray.direction.y;
50
51 if (y0 < 0 || y0 > _height) {
52 if (y1 < 0 || y1 > _height)
53 return false;
54 t = t1;
55 } else {
56 t = t0;
57 }
58
59 hitPoint = ray.origin + ray.direction * t;
60
61 Math::Vector3D temp(hitPoint.x - _center.x, 0.0, hitPoint.z - _center.z);
62 double length = std::sqrt(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z);
63 if (length != 0)
64 normal = temp / length;
65 else
66 normal = temp;
67
68 return true;
69}
Math::Point3D _center
Definition Cylinder.hpp:31

References _center, _height, _radius, RayTracer::Ray::direction, RayTracer::Ray::origin, Math::Point3D::x, Math::Vector3D::x, Math::Vector3D::y, Math::Point3D::z, and Math::Vector3D::z.

◆ rotate()

void RayTracer::Cylinder::rotate ( const Math::Vector3D & axis,
double angleDegrees )
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 19 of file Cylinder.cpp.

20{
21 _axis = Math::rotateVector(_axis, axis, angleDegrees);
22}
Math::Vector3D _axis
Definition Cylinder.hpp:29
Vector3D rotateVector(const Vector3D &v, const Vector3D &axis, double angleDegrees)
Definition Math3D.cpp:90

References _axis, and Math::rotateVector().

◆ translate()

void RayTracer::Cylinder::translate ( const Math::Vector3D & v)
overridevirtual

Implements RayTracer::IPrimitive.

Definition at line 14 of file Cylinder.cpp.

15{
17}

References _baseCenter.

Member Data Documentation

◆ _axis

Math::Vector3D RayTracer::Cylinder::_axis
private

Definition at line 29 of file Cylinder.hpp.

◆ _baseCenter

Math::Point3D RayTracer::Cylinder::_baseCenter
private

Definition at line 26 of file Cylinder.hpp.

◆ _center

Math::Point3D RayTracer::Cylinder::_center
private

Definition at line 31 of file Cylinder.hpp.

◆ _height

double RayTracer::Cylinder::_height
private

Definition at line 28 of file Cylinder.hpp.

◆ _material

std::shared_ptr<IMaterial> RayTracer::Cylinder::_material
private

Definition at line 30 of file Cylinder.hpp.

◆ _radius

double RayTracer::Cylinder::_radius
private

Definition at line 27 of file Cylinder.hpp.


The documentation for this class was generated from the following files: