Add support for tuples/point/vector and corresponding tests.

This commit is contained in:
Godzil
2020-02-14 14:22:13 +00:00
parent 7b07106816
commit 332e73a3d9
3 changed files with 277 additions and 0 deletions

59
source/include/tuples.h Normal file
View File

@@ -0,0 +1,59 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Tuples header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_TUPLES_H
#define DORAYME_TUPLES_H
#include <math_helper.h>
class Tuple
{
public:
double x, y, z, w;
public:
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); };
bool isVector() { return (this->w == 0.0); };
bool operator==(const Tuple &b) const { return double_equal(this->x, b.x) &&
double_equal(this->y, b.y) &&
double_equal(this->z, b.z) &&
double_equal(this->w, b.w); };
Tuple operator+(const Tuple &b) const { return Tuple(this->x + b.x, this->y + b.y,
this->z + b.z, this->w + b.w); };
Tuple operator-(const Tuple &b) const { return Tuple(this->x - b.x, this->y - b.y,
this->z - b.z, this->w - b.w); };
Tuple operator-() const { return Tuple(-this->x, -this->y, -this->z, -this->w); };
Tuple operator*(const double &b) const { return Tuple(this->x * b, this->y * b,
this->z * b, this->w * b); };
Tuple operator/(const double &b) const { return Tuple(this->x / b, this->y / b,
this->z / b, this->w / b); };
double magnitude();
Tuple normalise();
double dot(const Tuple &b);
};
class Point: public Tuple
{
public:
Point(double x, double y, double z) : Tuple(x, y, z, 1.0) {};
};
class Vector: public Tuple
{
public:
Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
Vector cross(const Vector &b) const;
};
#endif /* DORAYME_TUPLES_H */

35
source/tuples.cpp Normal file
View File

@@ -0,0 +1,35 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Tuples implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuples.h>
#include <math.h>
double Tuple::magnitude()
{
return sqrt(pow(this->x, 2) + pow(this->y, 2) + pow(this->z, 2) + pow(this->w, 2));
}
Tuple Tuple::normalise()
{
double mag = this->magnitude();
return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag);
}
double Tuple::dot(const Tuple &b)
{
return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
}
Vector Vector::cross(const Vector &b) const
{
return Vector(this->y * b.z - this->z * b.y,
this->z * b.x - this->x * b.z,
this->x * b.y - this->y * b.x);
}