11 #ifndef VECTOR_H_INCLUDED
12 #define VECTOR_H_INCLUDED
18 #include "../headers/types.hpp"
27 Vec& zero() { x = y = z = 0;
return (*
this); }
28 Vec& set(Flt r) { x = y = z = r;
return (*
this); }
29 Vec& set(Flt x, Flt y, Flt z) { this->x = x, this->y = y, this->z = z;
return (*
this); }
30 Flt length() {
return sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); }
31 std::string asText() {
32 std::stringstream result(std::stringstream::in | std::stringstream::out);
33 result <<
"X: " << x <<
" Y: " << y <<
" Z: " << z;
37 Vec& operator+=(Flt r) { x += r; y += r; z += r;
return (*
this); }
38 Vec& operator-=(Flt r) { x -= r; y -= r; z -= r;
return (*
this); }
39 Vec& operator*=(Flt r) { x *= r; y *= r; z *= r;
return (*
this); }
40 Vec& operator/=(Flt r) { x /= r; y /= r; z /= r;
return (*
this); }
41 Vec& operator+=(
const Vec &v) { x += v.x; y += v.y; z += v.z;
return (*
this); }
42 Vec& operator-=(
const Vec &v) { x -= v.x; y -= v.y; z -= v.z;
return (*
this); }
43 Vec& operator*=(
const Vec &v) { x *= v.x; y *= v.y; z *= v.z;
return (*
this); }
44 Vec& operator/=(
const Vec &v) { x /= v.x; y /= v.y; z /= v.z;
return (*
this); }
45 Bool operator==(
const Vec &v)
const {
46 if (x == v.x && y == v.y && z == v.z)
return true;
49 Bool operator!=(
const Vec &v)
const {
50 if (x != v.x || y != v.y || z != v.z)
return true;
54 friend Vec operator+ (
const Vec &v, Flt r) {
return Vec(v.x + r, v.y + r, v.z + r); }
55 friend Vec operator- (
const Vec &v, Flt r) {
return Vec(v.x - r, v.y - r, v.z - r); }
56 friend Vec operator* (
const Vec &v, Flt r) {
return Vec(v.x*r, v.y*r, v.z*r); }
57 friend Vec operator/ (
const Vec &v, Flt r) {
return Vec(v.x / r, v.y / r, v.z / r); }
58 friend Vec operator+ (Flt r,
const Vec &v) {
return Vec(r + v.x, r + v.y, r + v.z); }
59 friend Vec operator- (Flt r,
const Vec &v) {
return Vec(r - v.x, r - v.y, r - v.z); }
60 friend Vec operator* (Flt r,
const Vec &v) {
return Vec(r*v.x, r*v.y, r*v.z); }
61 friend Vec operator/ (Flt r,
const Vec &v) {
return Vec(r / v.x, r / v.y, r / v.z); }
62 friend Vec operator+ (
const Vec &a,
const Vec &b) {
return Vec(a.x + b.x, a.y + b.y, a.z + b.z); }
63 friend Vec operator- (
const Vec &a,
const Vec &b) {
return Vec(a.x - b.x, a.y - b.y, a.z - b.z); }
64 friend Vec operator* (
const Vec &a,
const Vec &b) {
return Vec(a.x*b.x, a.y*b.y, a.z*b.z); }
65 friend Vec operator/ (
const Vec &a,
const Vec &b) {
return Vec(a.x / b.x, a.y / b.y, a.z / b.z); }
69 Vec(Flt r) { set(r); }
70 Vec(Flt x, Flt y, Flt z) { set(x, y, z); }
76 inline Int Min(Int x, Int y) {
return (x<y) ? x : y; }
77 inline Int Min(UInt x, Int y) {
return (static_cast<Int>(x)<y) ?
static_cast<Int
>(x) : y; }
78 inline Int Min(Int x, UInt y) {
return (x<static_cast<Int>(y)) ? x :
static_cast<Int
>(y); }
79 inline UInt Min(UInt x, UInt y) {
return (x<y) ? x : y; }
80 inline I64 Min(Int x, I64 y) {
return (static_cast<I64>(x)<y) ?
static_cast<I64
>(x) : y; }
81 inline Flt Min(Flt x, Flt y) {
return (x<y) ? x : y; }
82 inline Flt Min(Int x, Flt y) {
return (static_cast<Flt>(x)<y) ?
static_cast<Flt
>(x) : y; }
83 inline Flt Min(Flt x, Int y) {
return (x<static_cast<Flt>(y)) ? x :
static_cast<Flt
>(y); }
84 inline Dbl Min(Dbl x, Dbl y) {
return (x<y) ? x : y; }
86 inline Int Max(Int x, Int y) {
return (x>y) ? x : y; }
87 inline UInt Max(UInt x, Int y) {
return (x>static_cast<UInt>(y)) ? x :
static_cast<UInt
>(y); }
88 inline UInt Max(Int x, UInt y) {
return (static_cast<UInt>(x)>y) ?
static_cast<UInt
>(x) : y; }
89 inline UInt Max(UInt x, UInt y) {
return (x>y) ? x : y; }
90 inline I64 Max(Int x, I64 y) {
return (static_cast<I64>(x)>y) ?
static_cast<I64
>(x) : y; }
91 inline Flt Max(Flt x, Flt y) {
return (x>y) ? x : y; }
92 inline Flt Max(Int x, Flt y) {
return (static_cast<Flt>(x)>y) ?
static_cast<Flt
>(x) : y; }
93 inline Flt Max(Flt x, Int y) {
return (x>static_cast<Flt>(y)) ? x :
static_cast<Flt
>(y); }
94 inline Dbl Max(Dbl x, Dbl y) {
return (x>y) ? x : y; }
96 inline Vec Min(
const Vec &a,
const Vec &b) {
return Vec(Min(a.x, b.x), Min(a.y, b.y), Min(a.z, b.z)); }
97 inline Vec Avg(
const Vec &a,
const Vec &b) {
return (a + b)*0.5f; }
98 inline Flt Dist(
const Vec &a,
const Vec &b) {
99 return sqrt(std::pow((b.x - a.x), 2) + std::pow((b.y - a.y), 2) + std::pow((b.z - a.z), 2));
101 inline Flt Dot(
const Vec &a,
const Vec &b) {
return a.x*b.x + a.y*b.y + a.z*b.z; }
102 Vec Cross(
const Vec &a,
const Vec &b);
111 aVec() { x.store(0.f), y.store(0.f), z.store(0.f); }
112 aVec(Flt x, Flt y, Flt z) {
117 aVec(
const Vec & v) { x.store(v.x); y.store(v.y); z.store(v.z); }
118 aVec & operator=(
const Vec & v) { x.store(v.x); y.store(v.y); z.store(v.z);
return *
this; }
125 #endif // VECTOR_H_INCLUDED