aboutsummaryrefslogtreecommitdiffstats
path: root/include/jau/math/vec3f.hpp
blob: e94ad40832bcba4c87a608841dbbd8d6be71f829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022-2024 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef JAU_VEC3F_HPP_
#define JAU_VEC3F_HPP_

#include <cmath>
#include <cstdarg>
#include <cstdint>
#include <limits>
#include <string>
#include <iostream>

#include <jau/float_math.hpp>
#include <jau/math/vec2f.hpp>

namespace jau::math {

    /** \addtogroup Math
     *
     *  @{
     */

    class Vec4f; // forward

    /**
     * 3D vector using three float components.
     */
    struct Vec3f {
        public:
            float x;
            float y;
            float z;

            constexpr Vec3f() noexcept
            : x(0), y(0), z(0) {}

            constexpr Vec3f(const float x_, const float y_, const float z_) noexcept
            : x(x_), y(y_), z(z_) {}

            constexpr Vec3f(const Vec3f& o) noexcept = default;
            constexpr Vec3f(Vec3f&& o) noexcept = default;
            constexpr Vec3f& operator=(const Vec3f&) noexcept = default;
            constexpr Vec3f& operator=(Vec3f&&) noexcept = default;

            /** Returns read-only component w/o boundary check */
            float operator[](size_t i) const noexcept {
                return reinterpret_cast<const float*>(this)[i];
            }

            /** Returns writeable reference to component w/o boundary check */
            float& operator[](size_t i) noexcept {
                return reinterpret_cast<float*>(this)[i];
            }

            /** xyz = this, returns xyz. */
            float* get(float xyz[/*3*/]) const noexcept {
                xyz[0] = x;
                xyz[1] = y;
                xyz[2] = z;
                return xyz;
            }

            constexpr bool equals(const Vec3f& o, const float epsilon=std::numeric_limits<float>::epsilon()) const noexcept {
                if( this == &o ) {
                    return true;
                } else {
                    return jau::equals(x, o.x, epsilon) &&
                           jau::equals(y, o.y, epsilon) &&
                           jau::equals(z, o.z, epsilon);
                }
            }
            constexpr bool operator==(const Vec3f& rhs) const noexcept {
                return equals(rhs);
            }
            /** TODO
            constexpr bool operator<=>(const vec3f_t& rhs ) const noexcept {
                return ...
            } */

            /** this = { o, z }, returns this. */
            constexpr Vec3f& set(const Vec2f& o, const float z_) noexcept {
                x = o.x;
                y = o.y;
                z = z_;
                return *this;
            }

            /** this = o while dropping w, returns this. */
            Vec3f& set(const Vec4f& o) noexcept;

            constexpr Vec3f& set(const float vx, const float vy, const float vz) noexcept
            { x=vx; y=vy; z=vz; return *this; }

            constexpr void add(const float dx, const float dy, const float dz) noexcept { x+=dx; y+=dy; z+=dz; }

            constexpr Vec3f& operator+=(const Vec3f& rhs ) noexcept {
                x+=rhs.x; y+=rhs.y; z+=rhs.z;
                return *this;
            }

            constexpr Vec3f& operator-=(const Vec3f& rhs ) noexcept {
                x-=rhs.x; y-=rhs.y; z-=rhs.z;
                return *this;
            }

            /**
             * Scale this vector with given scale factor
             * @param s scale factor
             * @return this instance
             */
            constexpr Vec3f& operator*=(const float s ) noexcept {
                x*=s; y*=s; z*=s;
                return *this;
            }

            /**
             * Divide this vector with given scale factor
             * @param s scale factor
             * @return this instance
             */
            constexpr Vec3f& operator/=(const float s ) noexcept {
                x/=s; y/=s; z/=s;
                return *this;
            }

            void rotate(const float radians, const Vec3f& ctr) noexcept {
                // FIXME z, consider using a matrix or quaternions
                const float cos = std::cos(radians);
                const float sin = std::sin(radians);
                const float x0 = x - ctr.x;
                const float y0 = y - ctr.y;
                const float tmp = x0 * cos - y0 * sin + ctr.x;
                              y = x0 * sin + y0 * cos + ctr.y;
                x = tmp;
            }
            constexpr void rotate(const float sin, const float cos, const Vec3f& ctr) noexcept {
                // FIXME z, consider using a matrix or quaternions
                const float x0 = x - ctr.x;
                const float y0 = y - ctr.y;
                const float tmp = x0 * cos - y0 * sin + ctr.x;
                              y = x0 * sin + y0 * cos + ctr.y;
                x = tmp;
            }

            std::string toString() const noexcept { return std::to_string(x)+"/"+std::to_string(y)+"/"+std::to_string(z); }

            constexpr bool is_zero() const noexcept {
                return jau::is_zero(x) && jau::is_zero(y) && jau::is_zero(z);
            }

            /**
             * Return the squared length of a vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i>
             */
            constexpr float length_sq() const noexcept {
                return x*x + y*y + z*z;
            }

            /**
             * Return the length of a vector, a.k.a the <i>norm</i> or <i>magnitude</i>
             */
            constexpr float length() const noexcept {
                return std::sqrt(length_sq());
            }

            /**
             * Normalize this vector in place
             */
            constexpr Vec3f& normalize() noexcept {
                const float lengthSq = length_sq();
                if ( jau::is_zero( lengthSq ) ) {
                    x = 0.0f;
                    y = 0.0f;
                    z = 0.0f;
                } else {
                    const float invSqr = 1.0f / std::sqrt(lengthSq);
                    x *= invSqr;
                    y *= invSqr;
                    z *= invSqr;
                }
                return *this;
            }

            /**
             * Return the squared distance between this vector and the given one.
             * <p>
             * When comparing the relative distance between two points it is usually sufficient to compare the squared
             * distances, thus avoiding an expensive square root operation.
             * </p>
             */
            constexpr float dist_sq(const Vec3f& o) const noexcept {
                const float dx = x - o.x;
                const float dy = y - o.y;
                const float dz = z - o.z;
                return dx*dx + dy*dy + dz*dz;
            }

            /**
             * Return the distance between this vector and the given one.
             */
            constexpr float dist(const Vec3f& o) const noexcept {
                return std::sqrt(dist_sq(o));
            }

            /**
             * Return the dot product of this vector and the given one
             * @return the dot product as float
             */
            constexpr float dot(const Vec3f& o) const noexcept {
                return x*o.x + y*o.y + z*o.z;
            }

            /**
             * cross product this x b
             * @return new resulting vector
             */
            constexpr Vec3f cross(const Vec3f& b) const noexcept {
                return Vec3f( y * b.z - z * b.y,
                              z * b.x - x * b.z,
                              x * b.y - y * b.y);
            }

            /**
             * cross product this = a x b, with a, b different from this
             * @return this for chaining
             */
            constexpr Vec3f& cross(const Vec3f& a, const Vec3f& b) noexcept {
                x = a.y * b.z - a.z * b.y;
                y = a.z * b.x - a.x * b.z;
                z = a.x * b.y - a.y * b.y;
                return *this;
            }

            /**
             * Return the cosines of the angle between to vectors
             * @param vec1 vector 1
             * @param vec2 vector 2
             */
            constexpr float cos_angle(const Vec3f& o) const noexcept {
                return dot(o) / ( length() * o.length() ) ;
            }

            /**
             * Return the angle between to vectors in radians
             * @param vec1 vector 1
             * @param vec2 vector 2
             */
            float angle(const Vec3f& o) const noexcept {
                return std::acos( cos_angle(o) );
            }

            bool intersects(const Vec3f& o) const noexcept {
                const float eps = std::numeric_limits<float>::epsilon();
                if( std::abs(x-o.x) >= eps || std::abs(y-o.y) >= eps || std::abs(z-o.z) >= eps ) {
                    return false;
                }
                return true;
            }
    };
    typedef Vec3f Point3f;

    constexpr Vec3f operator+(const Vec3f& lhs, const Vec3f& rhs ) noexcept {
        return Vec3f(lhs) += rhs;
    }

    constexpr Vec3f operator-(const Vec3f& lhs, const Vec3f& rhs ) noexcept {
        return Vec3f(lhs) -= rhs;
    }

    constexpr Vec3f operator*(const Vec3f& lhs, const float s ) noexcept {
        return Vec3f(lhs) *= s;
    }

    constexpr Vec3f operator*(const float s, const Vec3f& rhs) noexcept {
        return Vec3f(rhs) *= s;
    }

    constexpr Vec3f operator/(const Vec3f& lhs, const float s ) noexcept {
        return Vec3f(lhs) /= s;
    }

    std::ostream& operator<<(std::ostream& out, const Vec3f& v) noexcept {
        return out << v.toString();
    }

    /**
     * Simple compound denoting a ray.
     * <p>
     * A ray, also known as a half line, consists out of it's <i>origin</i>
     * and <i>direction</i>. Hence it is bound to only the <i>origin</i> side,
     * where the other end is +infinitive.
     * <pre>
     * R(t) = R0 + Rd * t with R0 origin, Rd direction and t > 0.0
     * </pre>
     * </p>
     */
    struct Ray3f {
        /** Origin of Ray. */
        Point3f orig;

        /** Normalized direction vector of ray. */
        Vec3f dir;

        std::string toString() const noexcept { return "Ray[orig "+orig.toString()+", dir "+dir.toString() +"]"; }
    };

    std::ostream& operator<<(std::ostream& out, const Ray3f& v) noexcept {
        return out << v.toString();
    }

    /**@}*/

} // namespace jau::math

#endif /*  JAU_VEC3F_HPP_ */