You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.9 KiB
113 lines
3.9 KiB
2 years ago
|
//=================================================================================================
|
||
|
// Written in 2016 by Peter Shirley <ptrshrl@gmail.com>
|
||
|
//
|
||
|
// To the extent possible under law, the author(s) have dedicated all copyright and related and
|
||
|
// neighboring rights to this software to the public domain worldwide. This software is distributed
|
||
|
// without any warranty.
|
||
|
//
|
||
|
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along
|
||
|
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
||
|
//==================================================================================================
|
||
|
|
||
|
#ifndef VEC3H
|
||
|
#define VEC3H
|
||
|
|
||
|
#include <cmath>
|
||
|
#include <cstdlib>
|
||
|
#include <iostream>
|
||
|
|
||
|
class vec3 {
|
||
|
public:
|
||
|
vec3() {}
|
||
|
vec3(float e0, float e1, float e2) {
|
||
|
e[0] = e0;
|
||
|
e[1] = e1;
|
||
|
e[2] = e2;
|
||
|
}
|
||
|
float x() const { return e[0]; }
|
||
|
float y() const { return e[1]; }
|
||
|
float z() const { return e[2]; }
|
||
|
float r() const { return e[0]; }
|
||
|
float g() const { return e[1]; }
|
||
|
float b() const { return e[2]; }
|
||
|
|
||
|
inline vec3& operator+=(const vec3& v2) const {
|
||
|
vec3* temp = new vec3(this->x() + v2.x(), this->y() + v2.y(), this->z() + v2.z());
|
||
|
return *temp;
|
||
|
};
|
||
|
inline vec3& operator-=(const vec3& v2) const {
|
||
|
vec3* temp = new vec3(this->x() - v2.x(), this->y() - v2.y(), this->z() - v2.z());
|
||
|
return *temp;
|
||
|
};
|
||
|
inline vec3& operator*=(const vec3& v2) {
|
||
|
vec3* temp = new vec3(this->x() * v2.x(), this->y() * v2.y(), this->z() * v2.z());
|
||
|
return *temp;
|
||
|
};
|
||
|
inline vec3& operator/=(const vec3& v2) {
|
||
|
vec3* temp = new vec3(this->x() / v2.x(), this->y() / v2.y(), this->z() / v2.z());
|
||
|
return *temp;
|
||
|
};
|
||
|
|
||
|
inline vec3 operator*=(const float t) const {
|
||
|
return vec3(this->x() * t, this->y() * t, this->z() * t);
|
||
|
};
|
||
|
inline vec3& operator/=(const float t) {
|
||
|
vec3* temp = new vec3(this->x() / t, this->y() / t, this->z() / t);
|
||
|
return *temp;
|
||
|
};
|
||
|
|
||
|
inline float length() const { return sqrt(e[0] * e[0] + e[1] * e[1] + e[2] * e[2]); }
|
||
|
inline float squared_length() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; }
|
||
|
inline void make_unit_vector();
|
||
|
|
||
|
float e[3];
|
||
|
};
|
||
|
|
||
|
inline vec3 operator+(vec3 v1, vec3 v2) {
|
||
|
return vec3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
|
||
|
}
|
||
|
inline vec3 operator-(vec3 v1, vec3 v2) {
|
||
|
return vec3(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
|
||
|
}
|
||
|
inline vec3 operator*(vec3 v1, vec3 v2) {
|
||
|
return vec3(v1.x() * v2.x(), v1.y() * v2.y(), v1.z() * v2.z());
|
||
|
}
|
||
|
inline vec3 operator/(vec3 v1, vec3 v2) {
|
||
|
return vec3(v1.x() / v2.x(), v1.y() / v2.y(), v1.z() / v2.z());
|
||
|
}
|
||
|
inline vec3 operator*(vec3 v1, float t) {
|
||
|
return vec3(v1.x() * t, v1.y() * t, v1.z() * t);
|
||
|
}
|
||
|
inline vec3 operator*(float t, vec3 v1) {
|
||
|
return vec3(v1.x() * t, v1.y() * t, v1.z() * t);
|
||
|
}
|
||
|
inline vec3 operator/(vec3 v1, float t) {
|
||
|
return vec3(v1.x() / t, v1.y() / t, v1.z() / t);
|
||
|
}
|
||
|
|
||
|
inline std::ostream& operator<<(std::ostream& os, const vec3& t) {
|
||
|
os << "(" << t.x() << ", " << t.y() << ", " << t.z() << ")";
|
||
|
return os;
|
||
|
}
|
||
|
|
||
|
inline void vec3::make_unit_vector() {
|
||
|
float length = sqrt(this->x() * this->x() + this->y() * this->y() + this->z() * this->z());
|
||
|
this->e[0] = this->x() / length;
|
||
|
this->e[1] = this->y() / length;
|
||
|
this->e[2] = this->z() / length;
|
||
|
}
|
||
|
|
||
|
inline float dot(const vec3& v1, const vec3& v2) {
|
||
|
return (v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z());
|
||
|
}
|
||
|
|
||
|
inline vec3 cross(const vec3& v1, const vec3& v2) {
|
||
|
return vec3((v1.x() * v2.y() - v1.y() * v2.x()), (v1.y() * v2.z() - v1.z() * v2.y()), (v1.z() * v2.x() - v1.x() * v2.z()));
|
||
|
}
|
||
|
|
||
|
inline vec3 unit_vector(vec3 v) {
|
||
|
float length = v.length();
|
||
|
return vec3(v.x() / length, v.y() / length, v.z() / length);
|
||
|
}
|
||
|
|
||
|
#endif
|