First commit
This commit is contained in:
commit
b75663feab
7 changed files with 348 additions and 0 deletions
132
vec3.hpp
Normal file
132
vec3.hpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#ifndef VEC3_H
|
||||
#define VEC3_H
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct vec3 {
|
||||
/* Members */
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
|
||||
// Constructor proper. Values default to 0
|
||||
vec3(double x = 0, double y = 0, double z = 0)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
/* Overriden operators */
|
||||
|
||||
// - operator. Not to be confused with substraction
|
||||
vec3 operator-()
|
||||
{
|
||||
return vec3(-x, -y, -z);
|
||||
}
|
||||
|
||||
// Straightforward vector sum
|
||||
vec3& operator+=(const vec3 &v)
|
||||
{
|
||||
this->x += v.x;
|
||||
this->y += v.y;
|
||||
this->z += v.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Scalar multiplication
|
||||
vec3& operator*=(const double t)
|
||||
{
|
||||
x *= t;
|
||||
y *= t;
|
||||
z *= t;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Division by a scalar t
|
||||
vec3& operator/=(const double t)
|
||||
{
|
||||
x /= t;
|
||||
y /= t;
|
||||
z /= t;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* Methods */
|
||||
|
||||
double length() const
|
||||
{
|
||||
return sqrt(x*x + y*y + z*z);
|
||||
}
|
||||
};
|
||||
|
||||
/* Type aliases */
|
||||
typedef vec3 point3;
|
||||
typedef vec3 color;
|
||||
|
||||
|
||||
/* More overloads */
|
||||
|
||||
// Straightforward vector sum
|
||||
inline vec3 operator+(const vec3 &u, const vec3 &v)
|
||||
{
|
||||
return vec3(u.x + v.x,
|
||||
u.y + v.y,
|
||||
u.z + v.z);
|
||||
}
|
||||
|
||||
// Straightforward vector difference
|
||||
inline vec3 operator-(const vec3 &u, const vec3 &v)
|
||||
{
|
||||
return vec3(u.x - v.x,
|
||||
u.y - v.y,
|
||||
u.z - v.z);
|
||||
}
|
||||
|
||||
// Hadamard product
|
||||
inline vec3 operator*(const vec3 &u, const vec3 &v)
|
||||
{
|
||||
return vec3(u.x * v.x,
|
||||
u.y * v.y,
|
||||
u.z * v.z);
|
||||
}
|
||||
|
||||
// Scalar product
|
||||
inline vec3 operator*(double t,const vec3 &v)
|
||||
{
|
||||
return vec3(t*v.x, t*v.y, t*v.z);
|
||||
}
|
||||
|
||||
inline vec3 operator*(const vec3 &v, double t)
|
||||
{
|
||||
return t * v;
|
||||
}
|
||||
|
||||
// Vector division by scalar. Note that we redefine it as multiplying by 1/t to avoid division by 0
|
||||
inline vec3 operator/(vec3 v, double t)
|
||||
{
|
||||
return 1/t * v;
|
||||
}
|
||||
|
||||
// Straightforward dot product
|
||||
inline double dot(const vec3 &u, const vec3 &v)
|
||||
{
|
||||
return u.x*v.x + u.y*v.y + u.z*v.z;
|
||||
}
|
||||
|
||||
// Cross product between two vectors
|
||||
inline vec3 cross(const vec3 &u, const vec3 &v)
|
||||
{
|
||||
return vec3(u.y * v.z - u.z * v.y,
|
||||
u.z * v.x - u.x * v.z,
|
||||
u.x * v.y - u.y * v.x);
|
||||
}
|
||||
|
||||
// Normalize vector so its length = 1
|
||||
inline vec3 normalize(const vec3 v)
|
||||
{
|
||||
return v / v.length();
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue