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::Cone Class Reference

#include <Cone.hpp>

Inheritance diagram for RayTracer::Cone:
RayTracer::IPrimitive

Public Member Functions

 Cone (const Math::Point3D &apex, double height, double radius, std::shared_ptr< IMaterial > material)
 
 ~Cone () 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 _apex
 
double _height
 
double _radius
 
Math::Vector3D _axis
 
std::shared_ptr< IMaterial_material
 

Detailed Description

Definition at line 15 of file Cone.hpp.

Constructor & Destructor Documentation

◆ Cone()

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

Definition at line 10 of file Cone.cpp.

11 : _apex(apex), _height(height), _radius(radius), _material(std::move(material))
12{}
Math::Point3D _apex
Definition Cone.hpp:26
double _radius
Definition Cone.hpp:28
std::shared_ptr< IMaterial > _material
Definition Cone.hpp:30
double _height
Definition Cone.hpp:27

References _apex, _height, _material, and _radius.

◆ ~Cone()

RayTracer::Cone::~Cone ( )
overridedefault

Member Function Documentation

◆ getMaterial()

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

Implements RayTracer::IPrimitive.

Definition at line 24 of file Cone.cpp.

25{
26 return _material;
27}

References _material.

◆ intersect()

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

Implements RayTracer::IPrimitive.

Definition at line 29 of file Cone.cpp.

30{
31 Math::Vector3D oc = ray.origin - _apex;
32 double k = _radius / _height;
33 k = k * k;
34
35 double dx = ray.direction.x;
36 double dy = ray.direction.y;
37 double dz = ray.direction.z;
38 double ox = oc.x;
39 double oy = oc.y;
40 double oz = oc.z;
41
42 double a = dx * dx + dz * dz - k * dy * dy;
43 double b = 2 * (ox * dx + oz * dz - k * oy * dy);
44 double c = ox * ox + oz * oz - k * oy * oy;
45
46 double discriminant = b * b - 4 * a * c;
47 if (discriminant < 0)
48 return false;
49
50 double sqrtDisc = std::sqrt(discriminant);
51 double t0 = (-b - sqrtDisc) / (2 * a);
52 double t1 = (-b + sqrtDisc) / (2 * a);
53
54 if (t0 > t1)
55 std::swap(t0, t1);
56
57 double y = oy + t0 * dy;
58 if (y < 0 || y > _height) {
59 y = oy + t1 * dy;
60 if (y < 0 || y > _height)
61 return false;
62 t = t1;
63 } else {
64 t = t0;
65 }
66
67 hitPoint = ray.origin + ray.direction * t;
68
69 Math::Vector3D tmp = hitPoint - _apex;
70 tmp.y = -(k * std::sqrt(tmp.x * tmp.x + tmp.z * tmp.z));
71 double length = std::sqrt(tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z);
72 if (length != 0)
73 normal = tmp / length;
74 else
75 normal = tmp;
76
77 return true;
78}

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

◆ rotate()

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

Implements RayTracer::IPrimitive.

Definition at line 19 of file Cone.cpp.

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

References _axis, and Math::rotateVector().

◆ translate()

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

Implements RayTracer::IPrimitive.

Definition at line 14 of file Cone.cpp.

15{
16 _apex = _apex + v;
17}

References _apex.

Member Data Documentation

◆ _apex

Math::Point3D RayTracer::Cone::_apex
private

Definition at line 26 of file Cone.hpp.

◆ _axis

Math::Vector3D RayTracer::Cone::_axis
private

Definition at line 29 of file Cone.hpp.

◆ _height

double RayTracer::Cone::_height
private

Definition at line 27 of file Cone.hpp.

◆ _material

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

Definition at line 30 of file Cone.hpp.

◆ _radius

double RayTracer::Cone::_radius
private

Definition at line 28 of file Cone.hpp.


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