diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/common.hpp | 6 | ||||
-rw-r--r-- | src/core/ray.cpp | 17 | ||||
-rw-r--r-- | src/core/ray.hpp | 15 | ||||
-rw-r--r-- | src/core/vector.cpp | 106 | ||||
-rw-r--r-- | src/core/vector.hpp | 33 |
5 files changed, 177 insertions, 0 deletions
diff --git a/src/core/common.hpp b/src/core/common.hpp new file mode 100644 index 0000000..fd219bf --- /dev/null +++ b/src/core/common.hpp @@ -0,0 +1,6 @@ +#ifndef COMMON_H +#define COMMON_H + +#define ZERO_APPROX 1e-6 + +#endif diff --git a/src/core/ray.cpp b/src/core/ray.cpp new file mode 100644 index 0000000..7bc6201 --- /dev/null +++ b/src/core/ray.cpp @@ -0,0 +1,17 @@ +#include "ray.hpp" + +Ray::Ray(Vec3d start, Vec3d direction, bool normalize) { + m_start = start; + m_direction = direction; + + if (normalize) { + m_direction.normalize(); + } +} + +Ray::Ray(Vec3d a, Vec3d b) { + m_start = a; + m_direction = b - a; + + m_direction.normalize(); +} diff --git a/src/core/ray.hpp b/src/core/ray.hpp new file mode 100644 index 0000000..6341d44 --- /dev/null +++ b/src/core/ray.hpp @@ -0,0 +1,15 @@ +#ifndef RAY_H +#define RAY_H + +#include "vector.hpp" + +class Ray { + public: + Ray(Vec3d start, Vec3d direction, bool normalize); + Ray(Vec3d a, Vec3d b); + + Vec3d m_start; + Vec3d m_direction; +}; + +#endif diff --git a/src/core/vector.cpp b/src/core/vector.cpp new file mode 100644 index 0000000..51d8e2e --- /dev/null +++ b/src/core/vector.cpp @@ -0,0 +1,106 @@ +#include "vector.hpp" + +#include <math.h> +#include <stdexcept> + +Vec3d::Vec3d() { + set(0, 0, 0); +} + +Vec3d::Vec3d(double x, double y, double z) { + set(x, y, z); +} + +void Vec3d::set(double x, double y, double z) { + m_x = x; + m_y = y; + m_z = z; +} + +void Vec3d::normalize() { + auto len = length(); + if (len == 0) { + throw std::runtime_error("Normalizing zero vector"); + } + + m_x /= len; + m_y /= len; + m_z /= len; +} + +double Vec3d::length() const { + return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); +} + +double Vec3d::dot(const Vec3d &vec) const { + return m_x * vec.m_x + m_y * vec.m_y + m_z * vec.m_z; +} + +Vec3d Vec3d::cross(const Vec3d &vec) const { + return Vec3d( + m_y * vec.m_z - m_z * vec.m_y, + m_z * vec.m_x - m_x * vec.m_z, + m_x * vec.m_y - m_y * vec.m_x + ); +} + +Vec3d Vec3d::operator+(const Vec3d &vec) const { + return Vec3d( + m_x + vec.m_x, + m_y + vec.m_y, + m_z + vec.m_z + ); +} + +Vec3d& Vec3d::operator+=(const Vec3d &vec) { + m_x += vec.m_x; + m_y += vec.m_y; + m_z += vec.m_z; + return *this; +} + +Vec3d Vec3d::operator-(const Vec3d &vec) const { + return Vec3d( + m_x - vec.m_x, + m_y - vec.m_y, + m_z - vec.m_z + ); +} + +Vec3d Vec3d::operator-() const { + return Vec3d( + -m_x, + -m_y, + -m_z + ); +} + + +Vec3d Vec3d::operator*(double op) const { + return Vec3d( + m_x * op, + m_y * op, + m_z * op + ); +} + +Vec3d Vec3d::operator*(const Vec3d &vec) const { + return Vec3d( + m_x * vec.m_x, + m_y * vec.m_y, + m_z * vec.m_z + ); +} + +Vec3d Vec3d::operator/(double op) const { + return Vec3d( + m_x / op, + m_y / op, + m_z / op + ); +} + +std::ostream& operator<<(std::ostream &out, const Vec3d &v){ + out << "[ " << v.m_x << ", " << v.m_y << ", " << v.m_z << " ]"; + return out; +} diff --git a/src/core/vector.hpp b/src/core/vector.hpp new file mode 100644 index 0000000..20e8210 --- /dev/null +++ b/src/core/vector.hpp @@ -0,0 +1,33 @@ +#ifndef VECTOR_H +#define VECTOR_H + +#include <iostream> + +class Vec3d { + public: + Vec3d(); + Vec3d(double x, double y, double z); + + void set(double x, double y, double z); + void normalize(); + + double length() const; + double dot(const Vec3d &vec) const; + + Vec3d cross(const Vec3d &vec) const; + + // Operators + Vec3d operator+(const Vec3d &vec) const; + Vec3d& operator+=(const Vec3d &vec); + Vec3d operator-(const Vec3d &vec) const; + Vec3d operator-() const; + Vec3d operator*(double) const; + Vec3d operator*(const Vec3d &vec) const; + Vec3d operator/(double) const; + + friend std::ostream& operator<<(std::ostream& os, const Vec3d &v); + + double m_x, m_y, m_z; +}; + +#endif |