vec4.h 4.95 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2
3
4

#pragma once

#include <algorithm>
TheNumbat's avatar
TheNumbat committed
5
#include <cmath>
TheNumbat's avatar
TheNumbat committed
6
7
8
9
10
11
12
#include <ostream>

#include "log.h"
#include "vec3.h"

struct Vec4 {

TheNumbat's avatar
TheNumbat committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    Vec4() {
        x = 0.0f;
        y = 0.0f;
        z = 0.0f;
        w = 0.0f;
    }
    explicit Vec4(float _x, float _y, float _z, float _w) {
        x = _x;
        y = _y;
        z = _z;
        w = _w;
    }
    explicit Vec4(float f) { x = y = z = w = f; }
    explicit Vec4(int _x, int _y, int _z, int _w) {
        x = (float)_x;
        y = (float)_y;
        z = (float)_z;
        w = (float)_w;
    }
    explicit Vec4(Vec3 xyz, float _w) {
        x = xyz.x;
        y = xyz.y;
        z = xyz.z;
        w = _w;
    }

    Vec4(const Vec4 &) = default;
    Vec4 &operator=(const Vec4 &) = default;
    ~Vec4() = default;

    float &operator[](int idx) {
        assert(idx >= 0 && idx <= 3);
        return data[idx];
    }
    float operator[](int idx) const {
        assert(idx >= 0 && idx <= 3);
        return data[idx];
    }

    Vec4 operator+=(Vec4 v) {
        x += v.x;
        y += v.y;
        z += v.z;
        w += v.w;
        return *this;
    }
    Vec4 operator-=(Vec4 v) {
        x -= v.x;
        y -= v.y;
        z -= v.z;
        w -= v.w;
        return *this;
    }
    Vec4 operator*=(Vec4 v) {
        x *= v.x;
        y *= v.y;
        z *= v.z;
        w *= v.w;
        return *this;
    }
    Vec4 operator/=(Vec4 v) {
        x /= v.x;
        y /= v.y;
        z /= v.z;
        w /= v.w;
        return *this;
    }

    Vec4 operator+=(float s) {
        x += s;
        y += s;
        z += s;
        w += s;
        return *this;
    }
    Vec4 operator-=(float s) {
        x -= s;
        y -= s;
        z -= s;
        w -= s;
        return *this;
    }
    Vec4 operator*=(float s) {
        x *= s;
        y *= s;
        z *= s;
        w *= s;
        return *this;
    }
    Vec4 operator/=(float s) {
        x /= s;
        y /= s;
        z /= s;
        w /= s;
        return *this;
    }

    Vec4 operator+(Vec4 v) const { return Vec4(x + v.x, y + v.y, z + v.z, w + v.w); }
    Vec4 operator-(Vec4 v) const { return Vec4(x - v.x, y - v.y, z - v.z, w - v.w); }
    Vec4 operator*(Vec4 v) const { return Vec4(x * v.x, y * v.y, z * v.z, w * v.w); }
    Vec4 operator/(Vec4 v) const { return Vec4(x / v.x, y / v.y, z / v.z, w / v.w); }

    Vec4 operator+(float s) const { return Vec4(x + s, y + s, z + s, w + s); }
    Vec4 operator-(float s) const { return Vec4(x - s, y - s, z - s, w - s); }
    Vec4 operator*(float s) const { return Vec4(x * s, y * s, z * s, w * s); }
    Vec4 operator/(float s) const { return Vec4(x / s, y / s, z / s, w / s); }

    bool operator==(Vec4 v) const { return x == v.x && y == v.y && z == v.z && w == v.w; }
    bool operator!=(Vec4 v) const { return x != v.x || y != v.y || z != v.z || w != v.w; }

    /// Absolute value
    Vec4 abs() const { return Vec4(std::abs(x), std::abs(y), std::abs(z), std::abs(w)); }
    /// Negation
    Vec4 operator-() const { return Vec4(-x, -y, -z, -w); }
    /// Are all members real numbers?
    bool valid() const {
        return !(std::isinf(x) || std::isinf(y) || std::isinf(z) || std::isinf(w) ||
                 std::isnan(x) || std::isnan(y) || std::isnan(z) || std::isnan(w));
    }

    /// Modify vec to have unit length
    Vec4 normalize() {
        float n = norm();
        x /= n;
        y /= n;
        z /= n;
        w /= n;
        return *this;
    }
    /// Return unit length vec in the same direction
    Vec4 unit() const {
        float n = norm();
        return Vec4(x / n, y / n, z / n, w / n);
    }

    float norm_squared() const { return x * x + y * y + z * z + w * w; }
    float norm() const { return std::sqrt(norm_squared()); }

    /// Returns first three components
    Vec3 xyz() const { return Vec3(x, y, z); }
    /// Performs perspective division (xyz/w)
    Vec3 project() const { return Vec3(x / w, y / w, z / w); }

    union {
        struct {
            float x;
            float y;
            float z;
            float w;
        };
        float data[4] = {};
    };
TheNumbat's avatar
TheNumbat committed
165
166
};

TheNumbat's avatar
TheNumbat committed
167
168
169
170
inline Vec4 operator+(float s, Vec4 v) { return Vec4(v.x + s, v.y + s, v.z + s, v.w + s); }
inline Vec4 operator-(float s, Vec4 v) { return Vec4(v.x - s, v.y - s, v.z - s, v.w - s); }
inline Vec4 operator*(float s, Vec4 v) { return Vec4(v.x * s, v.y * s, v.z * s, v.w * s); }
inline Vec4 operator/(float s, Vec4 v) { return Vec4(s / v.x, s / v.y, s / v.z, s / v.w); }
TheNumbat's avatar
TheNumbat committed
171
172
173

/// Take minimum of each component
inline Vec4 hmin(Vec4 l, Vec4 r) {
TheNumbat's avatar
TheNumbat committed
174
    return Vec4(std::min(l.x, r.x), std::min(l.y, r.y), std::min(l.z, r.z), std::min(l.w, r.w));
TheNumbat's avatar
TheNumbat committed
175
176
177
}
/// Take maximum of each component
inline Vec4 hmax(Vec4 l, Vec4 r) {
TheNumbat's avatar
TheNumbat committed
178
    return Vec4(std::max(l.x, r.x), std::max(l.y, r.y), std::max(l.z, r.z), std::max(l.w, r.w));
TheNumbat's avatar
TheNumbat committed
179
180
181
}

/// 4D dot product
TheNumbat's avatar
TheNumbat committed
182
inline float dot(Vec4 l, Vec4 r) { return l.x * r.x + l.y * r.y + l.z * r.z + l.w * r.w; }
TheNumbat's avatar
TheNumbat committed
183

TheNumbat's avatar
TheNumbat committed
184
185
186
inline std::ostream &operator<<(std::ostream &out, Vec4 v) {
    out << "{" << v.x << "," << v.y << "," << v.z << "," << v.w << "}";
    return out;
TheNumbat's avatar
TheNumbat committed
187
}