added some nodiscards

This commit is contained in:
2024-05-18 13:40:29 +03:00
parent 4e01f3ee09
commit 7ea5cecff0
7 changed files with 53 additions and 18 deletions

1
.idea/vcs.xml generated
View File

@@ -2,5 +2,6 @@
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
</component>
</project>

View File

@@ -4,7 +4,6 @@
#pragma once
#include <optional>
#include <vector>
#include <uml/Vector3.h>
@@ -35,7 +34,6 @@ namespace uml::prediction
const Projectile& projectile) const;
private:
const float m_gravity;
const float m_maxTravelTime;
const float m_timeStepSize;
@@ -49,8 +47,9 @@ namespace uml::prediction
const Vector3& targetPosition) const;
[[nodiscard]]
std::optional<float> ProjectileTravelTime(const Vector3& end, const Projectile& projectile,
const float angle) const;
std::optional<float> ProjectileTravelTime(const Vector3& end,
const Projectile& projectile,
float angle) const;
};
};

View File

@@ -12,10 +12,13 @@ namespace uml
public:
float w = 0.f;
Vector4(float x = 0.f, float y = 0.f, float z = 0.f, float w = 0.f) : Vector3(x, y, z), w(w) {}
Vector4(const float x = 0.f, const float y = 0.f, const float z = 0.f, const float w = 0.f) : Vector3(x, y, z), w(w) {}
Vector4() = default;
[[nodiscard]]
bool operator==(const Vector4& src) const;
[[nodiscard]]
bool operator!=(const Vector4& src) const;
Vector4& operator+=(const Vector4& v);
@@ -28,15 +31,32 @@ namespace uml
[[nodiscard]] float Length() const;
[[nodiscard]] float LengthSqr() const;
[[nodiscard]] float Dot(const Vector4& vOther) const;
Vector4& Abs();
Vector4& Clamp(float min, float max);
[[nodiscard]]
Vector4 operator-() const;
[[nodiscard]]
Vector4 operator+(const Vector4& v) const;
[[nodiscard]]
Vector4 operator-(const Vector4& v) const;
[[nodiscard]]
Vector4 operator*(float scalar) const;
[[nodiscard]]
Vector4 operator*(const Vector4& v) const;
[[nodiscard]]
Vector4 operator/(float scalar) const;
[[nodiscard]]
Vector4 operator/(const Vector4& v) const;
[[nodiscard]] float Sum() const;
[[nodiscard]]
float Sum() const;
};
}

View File

@@ -19,7 +19,7 @@ namespace uml::color
public:
Color(float r, float g, float b, float a);
static Color FromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
explicit Color(const Vector4& vec);
explicit Color(Vector4 vec);
[[nodiscard]] Color Blend(const Color& other, float ratio) const;
[[nodiscard]] static Color Red() {return {1.f, 0.f, 0.f, 1.f};}

View File

@@ -3,6 +3,8 @@
//
#include "uml/Vector4.h"
#include <algorithm>
#include <cmath>
namespace uml
@@ -81,39 +83,48 @@ namespace uml
return *this;
}
Vector4& Vector4::Clamp(const float min, const float max)
{
x = std::clamp(x, min, max);
y = std::clamp(y, min, max);
z = std::clamp(z, min, max);
return *this;
}
Vector4 Vector4::operator-() const
{
return Vector4(-x, -y, -z, -w);
return {-x, -y, -z, -w};
}
Vector4 Vector4::operator+(const Vector4& v) const
{
return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
return {x + v.x, y + v.y, z + v.z, w + v.w};
}
Vector4 Vector4::operator-(const Vector4& v) const
{
return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);
return {x - v.x, y - v.y, z - v.z, w - v.w};
}
Vector4 Vector4::operator*(float scalar) const
{
return Vector4(x * scalar, y * scalar, z * scalar, w * scalar);
return {x * scalar, y * scalar, z * scalar, w * scalar};
}
Vector4 Vector4::operator*(const Vector4& v) const
{
return Vector4(x * v.x, y * v.y, z * v.z, w * v.w);
return {x * v.x, y * v.y, z * v.z, w * v.w};
}
Vector4 Vector4::operator/(float scalar) const
{
return Vector4(x / scalar, y / scalar, z / scalar, w / scalar);
return {x / scalar, y / scalar, z / scalar, w / scalar};
}
Vector4 Vector4::operator/(const Vector4& v) const
{
return Vector4(x / v.x, y / v.y, z / v.z, w / v.w);
return {x / v.x, y / v.y, z / v.z, w / v.w};
}
float Vector4::Sum() const

View File

@@ -17,20 +17,22 @@ namespace uml::color
return Color( (*this * (1.f - ratio)) + (other * ratio) );
}
Color::Color(float r, float g, float b, float a) : Vector4(r,g,b,a)
Color::Color(const float r, const float g, const float b, const float a)
: Vector4(std::clamp(r, 0.f, 1.f),
std::clamp(g, 0.f, 1.f),
std::clamp(b, 0.f, 1.f),
std::clamp(a, 0.f, 1.f))
{
}
Color::Color(const Vector4 &vec) : Vector4(vec)
Color::Color(Vector4 vec) : Vector4(vec.Clamp(0.f, 1.f))
{
}
Color Color::FromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
{
return Color{Vector4(r, g, b, a) / 255.f};
}
}

View File

@@ -4,6 +4,8 @@
#include <gtest/gtest.h>
#include <uml/matrix.h>
#include <print>
TEST(UnitTestMatrix, ToString)
{
uml::matrix matrix(2, 2);