# split pd-maths into pd-core and pd-image (0.2.3)
This commit is contained in:
34
include/pd/core/bit_util.hpp
Normal file
34
include/pd/core/bit_util.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace BitUtil {
|
||||
bool IsSingleBit(u32 v);
|
||||
u32 GetPow2(u32 v);
|
||||
} // namespace BitUtil
|
||||
} // namespace PD
|
102
include/pd/core/color.hpp
Normal file
102
include/pd/core/color.hpp
Normal file
@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
/// @brief Color class (Supports hex, rgb(a)8, u32 input)
|
||||
/// @note no safeteychecks used here for performance
|
||||
class Color {
|
||||
public:
|
||||
Color() : m_r(0), m_g(0), m_b(0), m_a(0) {}
|
||||
Color(u32 color) {
|
||||
m_a = (color >> 24) & 0xff;
|
||||
m_b = (color >> 16) & 0xff;
|
||||
m_g = (color >> 8) & 0xff;
|
||||
m_r = color & 0xff;
|
||||
}
|
||||
Color(u8 r, u8 g, u8 b, u8 a = 255) {
|
||||
m_r = r;
|
||||
m_g = g;
|
||||
m_b = b;
|
||||
m_a = a;
|
||||
}
|
||||
Color(float r, float g, float b, float a = 1.f) {
|
||||
m_r = static_cast<u8>(255.f * r);
|
||||
m_g = static_cast<u8>(255.f * g);
|
||||
m_b = static_cast<u8>(255.f * b);
|
||||
m_a = static_cast<u8>(255.f * a);
|
||||
}
|
||||
Color(const std::string& hex) { Hex(hex); }
|
||||
~Color() {}
|
||||
|
||||
Color& Hex(const std::string& hex);
|
||||
std::string Hex(bool rgba = false) const;
|
||||
|
||||
Color& r(u8 v) {
|
||||
m_r = v;
|
||||
return *this;
|
||||
}
|
||||
u8 r() const { return m_r; }
|
||||
Color& g(u8 v) {
|
||||
m_g = v;
|
||||
return *this;
|
||||
}
|
||||
u8 g() const { return m_r; }
|
||||
Color& b(u8 v) {
|
||||
m_b = v;
|
||||
return *this;
|
||||
}
|
||||
u8 b() const { return m_r; }
|
||||
Color& a(u8 v) {
|
||||
m_a = v;
|
||||
return *this;
|
||||
}
|
||||
u8 a() const { return m_r; }
|
||||
|
||||
Color& Fade(const Color& color, float p) {
|
||||
m_a = static_cast<u8>((color.a() - m_a) * ((p + 1.f) / 2));
|
||||
m_b = static_cast<u8>((color.b() - m_b) * ((p + 1.f) / 2));
|
||||
m_g = static_cast<u8>((color.g() - m_g) * ((p + 1.f) / 2));
|
||||
m_r = static_cast<u8>((color.r() - m_r) * ((p + 1.f) / 2));
|
||||
return *this;
|
||||
}
|
||||
u32 Get() const { return (m_a << 24) | (m_b << 16) | (m_g << 8) | m_r; }
|
||||
float Luminance() const {
|
||||
// For Reference https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
|
||||
return (0.3 * (m_r / 255.f) + 0.59 * (m_g / 255.f) + 0.11 * (m_b / 255.f));
|
||||
}
|
||||
bool IsLight() const { return (Luminance() >= 0.5); }
|
||||
|
||||
operator u32() const { return Get(); }
|
||||
|
||||
private:
|
||||
u8 m_r;
|
||||
u8 m_g;
|
||||
u8 m_b;
|
||||
u8 m_a;
|
||||
};
|
||||
} // namespace PD
|
161
include/pd/core/tween.hpp
Normal file
161
include/pd/core/tween.hpp
Normal file
@ -0,0 +1,161 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <pd/core/vec.hpp>
|
||||
|
||||
namespace PD {
|
||||
template <typename T>
|
||||
class Tween {
|
||||
public:
|
||||
enum Effect {
|
||||
Linear,
|
||||
EaseInQuad,
|
||||
EaseOutQuad,
|
||||
EaseInOutQuad,
|
||||
EaseInCubic,
|
||||
EaseOutCubic,
|
||||
EaseInOutCubic,
|
||||
EaseInSine,
|
||||
EaseOutSine,
|
||||
EaseInOutSine,
|
||||
};
|
||||
Tween() {}
|
||||
~Tween() {}
|
||||
|
||||
void Update(float delta) {
|
||||
time += delta / 1000.f;
|
||||
if (time > tend) {
|
||||
finished = true;
|
||||
time = tend;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsFinished() const { return finished; }
|
||||
|
||||
Tween& Finish() {
|
||||
time = tend;
|
||||
finished = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Tween& From(const T& start) {
|
||||
Reset();
|
||||
this->start = start;
|
||||
return *this;
|
||||
}
|
||||
Tween& To(const T& end) {
|
||||
Reset();
|
||||
this->end = end;
|
||||
return *this;
|
||||
}
|
||||
Tween& In(float seconds) {
|
||||
Reset();
|
||||
tend = seconds;
|
||||
return *this;
|
||||
}
|
||||
Tween& As(const Effect& e) {
|
||||
effect = e;
|
||||
return *this;
|
||||
}
|
||||
Tween& Reset() {
|
||||
finished = false;
|
||||
time = 0.f;
|
||||
return *this;
|
||||
}
|
||||
/// @brief Probably usefull for fading colors by animation
|
||||
/// Used to create this caus dont wanted to create a
|
||||
/// fade effect fpr keyboard without having to Tween functions
|
||||
/// @return
|
||||
float Progress() const { return time / tend; }
|
||||
|
||||
Tween& Swap() {
|
||||
T temp = start;
|
||||
start = end;
|
||||
end = temp;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator T() {
|
||||
float t = 0.f;
|
||||
switch (effect) {
|
||||
case EaseInQuad:
|
||||
t = time / tend;
|
||||
return (end - start) * t * t + start;
|
||||
break;
|
||||
case EaseOutQuad:
|
||||
t = time / tend;
|
||||
return -(end - start) * t * (t - 2) + start;
|
||||
break;
|
||||
case EaseInOutQuad:
|
||||
t = time / (tend / 2);
|
||||
if (t < 1) return (end - start) / 2 * t * t + start;
|
||||
t--;
|
||||
return -(end - start) / 2 * (t * (t - 2) - 1) + start;
|
||||
break;
|
||||
case EaseInCubic:
|
||||
t = time / tend;
|
||||
return (end - start) * t * t * t + start;
|
||||
break;
|
||||
case EaseOutCubic:
|
||||
t = time / tend;
|
||||
t--;
|
||||
return (end - start) * (t * t * t + 1) + start;
|
||||
break;
|
||||
// case EaseInOutCubic:
|
||||
// t = time / (tend / 2);
|
||||
// if (t < 1) return (end - start) / 2 * t * t * t + start;
|
||||
// t--;
|
||||
// return (end - start) / 2 * (t * t * t * 2) + start;
|
||||
// break;
|
||||
case EaseInSine:
|
||||
return -(end - start) * cos(time / tend * (M_PI / 2)) + (end - start) +
|
||||
start;
|
||||
break;
|
||||
case EaseOutSine:
|
||||
return (end - start) * sin(time / tend * (M_PI / 2)) + start;
|
||||
break;
|
||||
case EaseInOutSine:
|
||||
return -(end - start) / 2 * (cos(M_PI * time / tend) - 1) + start;
|
||||
break;
|
||||
|
||||
default: // Linear
|
||||
return (end - start) * (time / tend) + start;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Effect effect;
|
||||
float time = 0.f;
|
||||
// Defaulting to one to prevent div zero
|
||||
// without a safetey check
|
||||
// not implementing one cause if the user is
|
||||
// Writing a In(0.f) its their fault
|
||||
float tend = 1.f;
|
||||
T start;
|
||||
T end;
|
||||
bool finished = false;
|
||||
};
|
||||
} // namespace PD
|
426
include/pd/core/vec.hpp
Normal file
426
include/pd/core/vec.hpp
Normal file
@ -0,0 +1,426 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Why Creating this:
|
||||
* Cause using makes coding much better structured
|
||||
* and easy to use like in glsl or glm
|
||||
*/
|
||||
|
||||
#include <pd/core/common.hpp>
|
||||
|
||||
namespace PD {
|
||||
struct vec2 {
|
||||
// Init Funcs
|
||||
vec2() {
|
||||
v[0] = 0;
|
||||
v[1] = 0;
|
||||
}
|
||||
vec2(float x, float y) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
}
|
||||
vec2(const vec2 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
}
|
||||
vec2(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
}
|
||||
|
||||
// Operations
|
||||
// Add
|
||||
vec2 &operator+=(const vec2 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator+=(const float &i) {
|
||||
v[0] += i;
|
||||
v[1] += i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator+(const vec2 &i) const { return vec2(v[0] + i[0], v[1] + i[1]); }
|
||||
vec2 operator+(const float &i) const { return vec2(v[0] + i, v[1] + i); }
|
||||
|
||||
// Sub
|
||||
vec2 &operator-=(const vec2 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator-=(const float &i) {
|
||||
v[0] -= i;
|
||||
v[1] -= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator-(const vec2 &i) const { return vec2(v[0] - i[0], v[1] - i[1]); }
|
||||
vec2 operator-(const float &i) const { return vec2(v[0] - i, v[1] - i); }
|
||||
|
||||
// Mul
|
||||
vec2 &operator*=(const vec2 &i) {
|
||||
v[0] *= i[0];
|
||||
v[1] *= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator*=(const float &i) {
|
||||
v[0] *= i;
|
||||
v[1] *= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator*(const vec2 &i) const { return vec2(v[0] * i[0], v[1] * i[1]); }
|
||||
vec2 operator*(const float &i) const { return vec2(v[0] * i, v[1] * i); }
|
||||
|
||||
// Div
|
||||
vec2 &operator/=(const vec2 &i) {
|
||||
v[0] /= i[0];
|
||||
v[1] /= i[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 &operator/=(const float &i) {
|
||||
v[0] /= i;
|
||||
v[1] /= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec2 operator/(const vec2 &i) const { return vec2(v[0] / i[0], v[1] / i[1]); }
|
||||
vec2 operator/(const float &i) const { return vec2(v[0] / i, v[1] / i); }
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec2 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1];
|
||||
}
|
||||
|
||||
bool operator!=(const vec2 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
vec2 operator-() const { return vec2(-v[0], -v[1]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const { return v[0] * v[0] + v[1] * v[1]; }
|
||||
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
// Internal Values
|
||||
float v[2];
|
||||
};
|
||||
|
||||
struct vec3 {
|
||||
// Init Funcs
|
||||
vec3() {
|
||||
v[0] = 0.f;
|
||||
v[1] = 0.f;
|
||||
v[2] = 0.f;
|
||||
}
|
||||
vec3(float x, float y, float z) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
v[2] = z;
|
||||
}
|
||||
vec3(const vec3 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
v[2] = i[2];
|
||||
}
|
||||
vec3(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
v[2] = i;
|
||||
}
|
||||
//// PD REWRITE ADDITIONAL CONTENT ////
|
||||
vec3(const vec2 &xy, float z) {
|
||||
v[0] = xy[0];
|
||||
v[1] = xy[1];
|
||||
v[2] = z;
|
||||
}
|
||||
vec3(float x, const vec2 &yz) {
|
||||
v[0] = x;
|
||||
v[1] = yz[0];
|
||||
v[2] = yz[1];
|
||||
}
|
||||
|
||||
// Operations
|
||||
// Add
|
||||
vec3 &operator+=(const vec3 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
v[2] += i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator+=(const float &i) {
|
||||
v[0] += i;
|
||||
v[1] += i;
|
||||
v[2] += i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator+(const vec3 &i) const {
|
||||
return vec3(v[0] + i[0], v[1] + i[1], v[2] + i[2]);
|
||||
}
|
||||
vec3 operator+(const float &i) const {
|
||||
return vec3(v[0] + i, v[1] + i, v[2] + i);
|
||||
}
|
||||
|
||||
// Sub
|
||||
vec3 &operator-=(const vec3 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
v[2] -= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator-=(const float &i) {
|
||||
v[0] -= i;
|
||||
v[1] -= i;
|
||||
v[2] -= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator-(const vec3 &i) const {
|
||||
return vec3(v[0] - i[0], v[1] - i[1], v[2] - i[2]);
|
||||
}
|
||||
vec3 operator-(const float &i) const {
|
||||
return vec3(v[0] - i, v[1] - i, v[2] - i);
|
||||
}
|
||||
|
||||
// Mul
|
||||
vec3 &operator*=(const vec3 &i) {
|
||||
v[0] *= i[0];
|
||||
v[1] *= i[1];
|
||||
v[2] *= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator*=(const float &i) {
|
||||
v[0] *= i;
|
||||
v[1] *= i;
|
||||
v[2] *= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator*(const vec3 &i) const {
|
||||
return vec3(v[0] * i[0], v[1] * i[1], v[2] * i[2]);
|
||||
}
|
||||
vec3 operator*(const float &i) const {
|
||||
return vec3(v[0] * i, v[1] * i, v[2] * i);
|
||||
}
|
||||
|
||||
// Div
|
||||
vec3 &operator/=(const vec3 &i) {
|
||||
v[0] /= i[0];
|
||||
v[1] /= i[1];
|
||||
v[2] /= i[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 &operator/=(const float &i) {
|
||||
v[0] /= i;
|
||||
v[1] /= i;
|
||||
v[2] /= i;
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec3 operator/(const vec3 &i) const {
|
||||
return vec3(v[0] / i[0], v[1] / i[1], v[2] / i[2]);
|
||||
}
|
||||
vec3 operator/(const float &i) const {
|
||||
return vec3(v[0] / i, v[1] / i, v[2] / i);
|
||||
}
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec3 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1] && v[2] == v[2];
|
||||
}
|
||||
|
||||
bool operator!=(const vec3 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
// Base
|
||||
vec3 operator-() const { return vec3(-v[0], -v[1], -v[2]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const { return v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; }
|
||||
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
float z() const { return v[2]; }
|
||||
float &z() { return v[2]; }
|
||||
|
||||
//// PALLADIUM REWRITE ADDITIONAL CONTENT ////
|
||||
vec2 xy() const { return vec2(v[0], v[1]); }
|
||||
vec2 yz() const { return vec2(v[1], v[2]); }
|
||||
|
||||
float v[3];
|
||||
};
|
||||
|
||||
struct vec4 {
|
||||
// Init Funcs
|
||||
vec4() {
|
||||
v[0] = 0.f;
|
||||
v[1] = 0.f;
|
||||
v[2] = 0.f;
|
||||
v[3] = 0.f;
|
||||
}
|
||||
vec4(float x, float y, float z, float w) {
|
||||
v[0] = x;
|
||||
v[1] = y;
|
||||
v[2] = z;
|
||||
v[3] = w;
|
||||
}
|
||||
vec4(const vec4 &i) {
|
||||
v[0] = i[0];
|
||||
v[1] = i[1];
|
||||
v[2] = i[2];
|
||||
v[3] = i[3];
|
||||
}
|
||||
|
||||
vec4(const vec2 &i0, const vec2 &i1) {
|
||||
v[0] = i0[0];
|
||||
v[1] = i0[1];
|
||||
v[2] = i1[0];
|
||||
v[3] = i1[1];
|
||||
}
|
||||
vec4(float i) {
|
||||
v[0] = i;
|
||||
v[1] = i;
|
||||
v[2] = i;
|
||||
v[3] = i;
|
||||
}
|
||||
|
||||
vec4(const vec3 &xyz, float w) {
|
||||
v[0] = xyz[0];
|
||||
v[1] = xyz[1];
|
||||
v[2] = xyz[2];
|
||||
v[3] = w;
|
||||
}
|
||||
|
||||
vec4(float x, const vec3 &yzw) {
|
||||
v[0] = x;
|
||||
v[1] = yzw[1];
|
||||
v[2] = yzw[2];
|
||||
v[3] = yzw[3];
|
||||
}
|
||||
|
||||
// Operators
|
||||
// Add
|
||||
vec4 &operator+=(const vec4 &i) {
|
||||
v[0] += i[0];
|
||||
v[1] += i[1];
|
||||
v[2] += i[2];
|
||||
v[3] += i[3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec4 operator+(const vec4 &i) const {
|
||||
return vec4(v[0] + i[0], v[1] + i[1], v[2] + i[2], v[3] + i[3]);
|
||||
}
|
||||
|
||||
// Sub
|
||||
vec4 &operator-=(const vec4 &i) {
|
||||
v[0] -= i[0];
|
||||
v[1] -= i[1];
|
||||
v[2] -= i[2];
|
||||
v[3] -= i[3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
vec4 operator-(const vec4 &i) const {
|
||||
return vec4(v[0] - i[0], v[1] - i[1], v[2] - i[2], v[3] - i[3]);
|
||||
}
|
||||
|
||||
// Compare
|
||||
bool operator==(const vec4 &in) const {
|
||||
return v[0] == in[0] && v[1] == in[1] && v[2] == in[2] && v[3] == in[3];
|
||||
}
|
||||
|
||||
bool operator!=(const vec4 &in) const {
|
||||
// use the first comparefuncs result
|
||||
// and swap it lol
|
||||
return !(*this == in);
|
||||
}
|
||||
|
||||
// Base
|
||||
vec4 operator-() const { return vec4(-v[0], -v[1], -v[2], -v[3]); }
|
||||
float operator[](int i) const { return v[i]; }
|
||||
float &operator[](int i) { return v[i]; }
|
||||
|
||||
float len() const { return sqrt(sqlen()); }
|
||||
float sqlen() const {
|
||||
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
|
||||
}
|
||||
|
||||
// Vec Acess
|
||||
float x() const { return v[0]; }
|
||||
float &x() { return v[0]; }
|
||||
float y() const { return v[1]; }
|
||||
float &y() { return v[1]; }
|
||||
float z() const { return v[2]; }
|
||||
float &z() { return v[2]; }
|
||||
float w() const { return v[3]; }
|
||||
float &w() { return v[3]; }
|
||||
vec2 xy() const { return vec2(v[0], v[1]); }
|
||||
vec2 zw() const { return vec2(v[2], v[3]); }
|
||||
vec3 xyz() const { return vec3(v[0], v[1], v[2]); }
|
||||
vec3 yzw() const { return vec3(v[1], v[2], v[3]); }
|
||||
// Quaternion Acess
|
||||
float r() const { return v[0]; }
|
||||
float &r() { return v[0]; }
|
||||
float k() const { return v[1]; }
|
||||
float &k() { return v[1]; }
|
||||
float j() const { return v[2]; }
|
||||
float &j() { return v[2]; }
|
||||
float i() const { return v[3]; }
|
||||
float &i() { return v[3]; }
|
||||
vec2 rk() const { return vec2(v[0], v[1]); }
|
||||
vec2 ji() const { return vec2(v[2], v[3]); }
|
||||
vec3 rkj() const { return vec3(v[0], v[1], v[2]); }
|
||||
vec3 kji() const { return vec3(v[1], v[2], v[3]); }
|
||||
// Internal Values
|
||||
float v[4];
|
||||
};
|
||||
} // namespace PD
|
Reference in New Issue
Block a user