#include "vec3.h" Vec3::Vec3() : x(0.0), y(0.0), z(0.0) { } Vec3::Vec3(const float& x, const float& y, const float& z) { this->x = x; this->y = y; this->z = z; } const float* Vec3::GetVec3() const { float* ret = new float[3]; ret[0] = this->x; ret[1] = this->y; ret[2] = this->z; return ret; } float* Vec3::GetVec3() { float* ret = new float[3]; ret[0] = this->x; ret[1] = this->y; ret[2] = this->z; return ret; } Vec3& Vec3::operator=(const Vec3& vec) { this->x = vec.x; this->y = vec.y; this->z = vec.z; return *this; } float& Vec3::operator[](unsigned int i) { return (i == 0 ? x : i == 1 ? y : z); } const float& Vec3::operator[](unsigned int i) const { return (i == 0 ? this->x : i == 1 ? this->y : this->z); } Vec3 Vec3::operator+() const { return *this; } Vec3 Vec3::operator-() const { return Vec3(-this->x, -this->y, -this->z); } Vec3& Vec3::operator+=(const Vec3& b) { this->x += b.x; this->y += b.y; this->z += b.z; return *this; } Vec3& Vec3::operator-=(const Vec3& b) { this->x -= b.x; this->y -= b.y; this->z -= b.z; return *this; } Vec3& Vec3::operator*=(const float& k) { this->x *= k; this->y *= k; this->z *= k; return *this; } Vec3& Vec3::operator/=(const float& k) { this->x /= k; this->y /= k; this->z /= k; return *this; } Vec3 Vec3::operator+(const Vec3& b) { return Vec3(this->x + b.x, this->y + b.y, this->z + b.z); } Vec3 Vec3::operator-(const Vec3& b) { return Vec3(this->x - b.x, this->y - b.y, this->z - b.z); } Vec3 Vec3::operator*(const float& k) { return Vec3(this->x * k, this->y * k, this->z * k); } Vec3 Vec3::operator/(const float& k) { return Vec3(this->x / k, this->y / k, this->z / k); } bool Vec3::operator==(const Vec3& b) const { //double epsilon = std::numeric_limits::epsilon(); //return (fabs(this->x - b.x) <= epsilon) && (fabs(this->y - b.y) <= epsilon) //&& (fabs(this->z - b.z) <= epsilon); return this->x == b.x && this->y == b.y && this->z == b.z; } bool Vec3::operator!=(const Vec3& b) const { return (!(*this == b)); } bool Vec3::operator<(const Vec3& b) const { if(this->x < b.x) return true; else if(this->x > b.x) return false; else { if(this->y < b.y) return true; else if(this->y > b.y) return false; else { if(this->z < b.z) return true; return false; } } } bool Vec3::operator>(const Vec3& b) { if(this->x > b.x) return true; else if(this->x < b.x) return false; else { if(this->y > b.y) return true; else if(this->y < b.y) return false; else { if(this->z > b.z) return true; return false; } } } float Vec3::Norm() const { return sqrt(this->x * this->x + this->y * this->y + this->z * this->z); } void Vec3::Normalize() { float norm = sqrt(this->x * this->x + this->y * this->y + this->z * this->z); this->x = this->x / norm; this->y = this->y / norm; this->z = this->z / norm; } void Vec3::set(const float& x, const float& y, const float& z) { this->x = x; this->y = y; this->z = z; } float Vec3::Dot(const Vec3& b) const { return (this->x * b.x + this->y * b.y + this->z * b.z); } Vec3 Vec3::Orthogonal(const Vec3& b) const { Vec3 u = Vec3(fabs(b.x), fabs(b.y), fabs(b.z)); int i = 0; int j = 1; if ((u.x > u.y) && (u.z > u.y)) j = 2; else { i = 1; j = 2; if (u.x > u.z) j = 0; } u = Vec3(); u[i] = b[j]; u[j] = -b[i]; return u; } Vec3 Vec3::Cross(const Vec3& b) const { return Vec3(this->y * b.z - this->z * b.y, this->z * b.x - this->x * b.z, this->x * b.y - this->y * b.x); } float Vec3::EculideanDistance(const Vec3& b) const { return sqrt( pow(this->x - b.x, 2.0) + pow(this->y - b.y, 2.0) + pow(this->z - b.z, 2.0)); } std::ostream& operator<<(std::ostream& os, const Vec3& coord) { os << coord[0] << " " << coord[1] << " " << coord[2]; return os; }