JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
MinimalVec2.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
23
24#include <ostream>
25
26namespace JPL
27{
28 struct Vec2
29 {
30 float X;
31 float Y;
32
33 [[nodiscard]] static JPL_INLINE constexpr Vec2 Zero() noexcept { return Vec2{ 0.0f, 0.0f }; }
34
35 [[nodiscard]] JPL_INLINE constexpr float LengthSquared() const noexcept { return X * X + Y * Y; }
36 [[nodiscard]] JPL_INLINE constexpr float Length() const noexcept { return Math::Sqrt(LengthSquared()); }
38 {
39 const float invLength = Math::InvSqrt(LengthSquared());
40 X *= invLength;
41 Y *= invLength;
42 return *this;
43 }
44
45 [[nodiscard]] JPL_INLINE constexpr Vec2 operator-() const noexcept { return Vec2{ -X, -Y }; }
46 [[nodiscard]] JPL_INLINE constexpr Vec2 operator-(const Vec2& V) const noexcept { return Vec2{ X - V.X, Y - V.Y }; }
47 [[nodiscard]] JPL_INLINE constexpr Vec2 operator+(const Vec2& V) const noexcept { return Vec2{ X + V.X, Y + V.Y }; }
48
49 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
50 [[nodiscard]] JPL_INLINE constexpr Vec2 operator*(Arg Scale) const noexcept { return Vec2{ X * (float)Scale, Y * (float)Scale }; }
51 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
52 [[nodiscard]] JPL_INLINE constexpr Vec2 operator/(Arg Scale) const noexcept { const float invScale = 1.0f / (float)Scale; return Vec2{ X * invScale, Y * invScale }; }
53 template<typename Arg> requires(std::is_arithmetic_v<Arg>)
54 JPL_INLINE constexpr Vec2& operator/=(Arg Scale) noexcept { const float invScale = 1.0f / (float)Scale; X *= invScale; Y *= invScale; return *this; }
55
56 JPL_INLINE constexpr void operator+=(const Vec2& V) noexcept { X += V.X; Y += V.Y; }
57 };
58
59 [[nodiscard]] JPL_INLINE constexpr bool operator==(const Vec2& A, const Vec2& B) noexcept { return A.X == B.X && A.Y == B.Y; }
60
61 template<typename T> requires(std::is_arithmetic_v<T>)
62 [[nodiscard]] JPL_INLINE constexpr Vec2 operator*(T Scale, const JPL::Vec2& V) noexcept { return V.operator*(Scale); }
63 template<typename T> requires(std::is_arithmetic_v<T>)
64 [[nodiscard]] JPL_INLINE constexpr Vec2 operator/(T Scale, const JPL::Vec2& V) noexcept { return V.operator/(Scale); }
65
66 [[nodiscard]] JPL_INLINE constexpr Vec2 operator*(const JPL::Vec2& A, const JPL::Vec2& B) noexcept { return { A.X * B.X, A.Y * B.Y}; }
67 [[nodiscard]] JPL_INLINE constexpr Vec2 operator/(const JPL::Vec2& A, const JPL::Vec2& B) noexcept { return { A.X / B.X, A.Y / B.Y}; }
68
69 [[nodiscard]] static JPL_INLINE constexpr float CrossProduct(const Vec2& a, const Vec2& b) noexcept { return a.X * b.Y - a.Y * b.X; }
70 [[nodiscard]] static JPL_INLINE constexpr float DotProduct(const Vec2& a, const Vec2& b) noexcept { return a.X * b.X + a.Y * b.Y; }
71 [[nodiscard]] static JPL_INLINE constexpr Vec2 Normalized(const Vec2& V) noexcept { return Vec2(V).Normalize(); }
72
73 static JPL_INLINE constexpr Vec2& Normalize(Vec2& V) noexcept { return V.Normalize(); }
74
75 [[nodiscard]] static JPL_INLINE constexpr Vec2 Abs(const Vec2& V) noexcept { return { Math::Abs(V.X), Math::Abs(V.Y) }; }
76
77 inline std::ostream& operator<<(std::ostream& os, const Vec2& v) { os << "{ " << v.X << ", " << v.Y << " }"; return os; }
78
79} // namespace JPL
JPL_INLINE constexpr T InvSqrt(T x) noexcept
Definition Math.h:283
JPL_INLINE constexpr T Sqrt(T x) noexcept
Definition Math.h:269
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
Definition AcousticMaterial.h:36
JPL_INLINE constexpr Vec2 operator/(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:64
JPL_INLINE constexpr bool operator==(const Vec2 &A, const Vec2 &B) noexcept
Definition MinimalVec2.h:59
JPL_INLINE constexpr Vec2 operator*(T Scale, const JPL::Vec2 &V) noexcept
Definition MinimalVec2.h:62
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
std::ostream & operator<<(std::ostream &os, const AcousticMaterial &v)
Definition AcousticMaterial.h:104
Definition MinimalVec2.h:29
JPL_INLINE constexpr Vec2 operator-(const Vec2 &V) const noexcept
Definition MinimalVec2.h:46
JPL_INLINE constexpr Vec2 & Normalize() noexcept
Definition MinimalVec2.h:37
JPL_INLINE constexpr float Length() const noexcept
Definition MinimalVec2.h:36
static JPL_INLINE constexpr Vec2 Zero() noexcept
Definition MinimalVec2.h:33
float X
Definition MinimalVec2.h:30
JPL_INLINE constexpr Vec2 operator+(const Vec2 &V) const noexcept
Definition MinimalVec2.h:47
JPL_INLINE constexpr float LengthSquared() const noexcept
Definition MinimalVec2.h:35
JPL_INLINE constexpr Vec2 operator-() const noexcept
Definition MinimalVec2.h:45
JPL_INLINE constexpr void operator+=(const Vec2 &V) noexcept
Definition MinimalVec2.h:56
float Y
Definition MinimalVec2.h:31