Skip to content

Commit

Permalink
fix a bunch of sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
petiaccja committed Aug 12, 2024
1 parent 57c1044 commit edb7aff
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 88 deletions.
4 changes: 2 additions & 2 deletions include/Mathter/Geometry/BezierCurve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class BezierCurve {
}

/// <summary> Construct from array of control points. </summary>
BezierCurve(const std::array<Vec, Order + 1>& controlPoints) : controlPoints(controlPoints) {}
explicit BezierCurve(const std::array<Vec, Order + 1>& controlPoints) : controlPoints(controlPoints) {}

/// <summary> Construct from control points. </summary>
template <class... Vectors, class = std::enable_if_t<sizeof...(Vectors) == Order + 1>>
BezierCurve(const Vectors&... controlPoints) : controlPoints{ controlPoints... } {}
explicit BezierCurve(const Vectors&... controlPoints) : controlPoints{ controlPoints... } {}

/// <summary> Interpolates the Bezier curve. </summary>
Vec operator()(const T& t) const {
Expand Down
5 changes: 2 additions & 3 deletions include/Mathter/Geometry/Hyperplane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class Hyperplane {

/// <summary> Converts from a hyperplane with different scalar type. </summary>
template <class TOther>
Hyperplane(const Hyperplane<TOther, Dim>& other) : normal(other.normal), scalar(other.scalar) {}
Hyperplane(const Hyperplane<TOther, Dim>& other) : normal(other.normal), scalar(static_cast<T>(other.scalar)) {}

/// <summary> Construct a plane through a point and a vector normal to the plane. </summary>
Hyperplane(const Vec& base, const Vec& normal) : normal(normal) {
Hyperplane(const Vec& base, const Vec& normal) : normal(normal), scalar(Dot(normal, base)) {
assert(std::abs(T(1) - Length(normal)) < 0.0001f);
scalar = Dot(normal, base);
}

/// <summary> Construct a plane by its algebraic equation. </summary>
Expand Down
17 changes: 9 additions & 8 deletions include/Mathter/Geometry/Intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ namespace impl {
template <class T1, class T2, int Dim>
auto Intersect(const Hyperplane<T1, Dim>& plane, const Line<T2, Dim>& line)
-> std::optional<Vector<common_arithmetic_type_t<T1, T2>, Dim, false>> {
using T = common_arithmetic_type_t<T1, T2>;
const auto t = impl::IntersectHyperplaneLine(plane, line);
if (t) {
return line.PointAt(*t);
return Line<T, Dim>(line).PointAt(*t);
}
else {
return std::nullopt;
Expand All @@ -107,9 +108,10 @@ auto Intersect(const Line<T1, Dim>& line, const Hyperplane<T2, Dim>& plane)
template <class T1, class T2, int Dim>
auto Intersect(const Hyperplane<T1, Dim>& plane, const LineSegment<T2, Dim>& lineSegment)
-> std::optional<Vector<common_arithmetic_type_t<T1, T2>, Dim, false>> {
using T = common_arithmetic_type_t<T1, T2>;
const auto t = impl::IntersectHyperplaneLine(plane, lineSegment.Line());
if (0 <= t && t < Distance(lineSegment.point1, lineSegment.point2)) {
return lineSegment.Line().PointAt(*t);
return Line<T, Dim>(lineSegment.Line()).PointAt(*t);
}
return std::nullopt;
}
Expand All @@ -133,9 +135,10 @@ auto Intersect(const LineSegment<T1, Dim>& lineSegment, const Hyperplane<T2, Dim
template <class T1, class T2, int Dim>
auto Intersect(const Hyperplane<T1, Dim>& plane, const Ray<T2, Dim>& ray)
-> std::optional<Vector<common_arithmetic_type_t<T1, T2>, Dim, false>> {
using T = common_arithmetic_type_t<T1, T2>;
const auto t = impl::IntersectHyperplaneLine(plane, ray.Line());
if (0 <= t) {
return ray.Line().PointAt(*t);
return Line<T, Dim>(ray.Line()).PointAt(*t);
}
return std::nullopt;
}
Expand Down Expand Up @@ -173,10 +176,8 @@ template <class T1, class T2>
auto Intersect(const LineSegment<T1, 2>& lhs, const LineSegment<T2, 2>& rhs)
-> std::optional<Vector<common_arithmetic_type_t<T1, T2>, 2, false>> {
const auto intersection = Intersect(lhs.Line(), rhs.Line());
if (intersection) {
if (impl::IsWithin(lhs, *intersection) && impl::IsWithin(rhs, *intersection)) {
return intersection;
}
if (intersection && impl::IsWithin(lhs, *intersection) && impl::IsWithin(rhs, *intersection)) {
return intersection;
}
return std::nullopt;
}
Expand Down Expand Up @@ -246,7 +247,7 @@ auto Intersect(const Ray<T1, 3>& ray, const Triangle<T2, 3>& triangle)
if (t < epsilon) {
return std::nullopt;
}
const auto point = ray.PointAt(t);
const auto point = Ray<T, 3>(ray).PointAt(t);
return point;
}

Expand Down
3 changes: 2 additions & 1 deletion include/Mathter/Geometry/Line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class Line {
}

public:
Vec direction, base;
Vec direction;
Vec base;
};

} // namespace mathter
3 changes: 2 additions & 1 deletion include/Mathter/Geometry/LineSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class LineSegment {
}

public:
Vec point1, point2;
Vec point1;
Vec point2;
};

} // namespace mathter
2 changes: 1 addition & 1 deletion include/Mathter/Geometry/Triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Triangle {
: corners{ a, b, c } {}

/// <summary> Construct a triangle from its corners. </summary>
Triangle(const std::array<Vec, 3>& corners)
explicit Triangle(const std::array<Vec, 3>& corners)
: corners{ corners } {}

/// <summary> Convert from a triangle with different scalar type. </summary>
Expand Down
12 changes: 7 additions & 5 deletions include/Mathter/Matrix/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ class Matrix : public MatrixStorage<T, Rows, Columns, Order, Layout, Packed> {
Matrix(const Matrix<T2, Columns, Rows, opposite_order_v<Order>, Layout2, Packed2>& rhs);

/// <summary> Creates a matrix from its elements. </summary>
template <class... Scalars, class = std::enable_if_t<(... && std::is_convertible_v<std::decay_t<Scalars>, T>) && sizeof...(Scalars) == size_t(Rows* Columns), int>>
template <class... Scalars, class = std::enable_if_t<(... && std::is_convertible_v<std::decay_t<Scalars>, T>)&&sizeof...(Scalars) == size_t(Rows* Columns), int>>
Matrix(Scalars&&... elements);

/// <summary> Constructs a row or column matrix from a vector. </summary>
/// <remarks> This can only be used for row or column matrices. </remarks>
template <class TOther, bool PackedOther, class = std::enable_if_t<isVector && sizeof(TOther) != 0>>
Matrix(const Vector<TOther, vectorDim, PackedOther>& v);
explicit Matrix(const Vector<TOther, vectorDim, PackedOther>& v);

/// <summary> Used by internal methods. </summary>
template <class... Stripes>
Expand Down Expand Up @@ -145,14 +145,16 @@ class Matrix : public MatrixStorage<T, Rows, Columns, Order, Layout, Packed> {

/// <summary> Convert a row or column matrix to a vector. </summary>
template <class TOther, bool PackedOther, class = std::enable_if_t<isVector && std::is_convertible_v<T, TOther>>>
operator Vector<TOther, vectorDim, PackedOther>() const;
explicit operator Vector<TOther, vectorDim, PackedOther>() const;

protected:
template <int i, int j, class Head, class... Args>
void Assign(Head head, Args... args);

template <int, int>
void Assign() {}
void Assign() {
// Overload to terminate recursion when assign is called with zero arguments.
}
};


Expand Down Expand Up @@ -351,7 +353,7 @@ Matrix<T, Rows, Columns, Order, Layout, Packed>::operator Vector<TOther, Matrix:
template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
template <int i, int j, class Head, class... Args>
void Matrix<T, Rows, Columns, Order, Layout, Packed>::Assign(Head head, Args... args) {
(*this)(i, j) = (T)head;
(*this)(i, j) = static_cast<T>(head);
Assign<((j != Columns - 1) ? i : (i + 1)), ((j + 1) % Columns)>(args...);
}

Expand Down
6 changes: 2 additions & 4 deletions include/Mathter/Quaternion/Quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ class Quaternion {
//-----------------------------------------------

/// <summary> Returns the scalar part (w) of (w + xi + yj + zk). </summary>
/// <remarks> Deprecated: use q.scalar instead. </remarks>
[[deprecated]] T ScalarPart() const;
[[deprecated("use .scalar")]] T ScalarPart() const;

/// <summary> Returns the vector part (x, y, z) of (w + xi + yj + zk). </summary>
/// <remarks> Deprecated: use q.vector instead. </remarks>
[[deprecated]] Vector<T, 3, Packed> VectorPart() const;
[[deprecated("use .vector")]] Vector<T, 3, Packed> VectorPart() const;

/// <summary> Returns the angle of the rotation represented by quaternion. </summary>
/// <remarks> Only valid for unit quaternions. </remarks>
Expand Down
6 changes: 4 additions & 2 deletions include/Mathter/Transforms/OrthographicBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ namespace impl {
}
}

Vector<T, Dim, Packed> minBounds, maxBounds;
T projNearPlane, projFarPlane;
Vector<T, Dim, Packed> minBounds;
Vector<T, Dim, Packed> maxBounds;
T projNearPlane;
T projFarPlane;
};

} // namespace impl
Expand Down
10 changes: 5 additions & 5 deletions include/Mathter/Transforms/Rotation3DBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,27 @@ namespace impl {

template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, 4, 4, Order, Layout, MPacked>() const {
return RotationMatrixAxisAngle<U, 4, 4, Order, Layout, MPacked>(axis, angle);
return RotationMatrixAxisAngle<U, 4, 4, Order, Layout, MPacked>(axis, U(angle));
}

template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, 3, 3, Order, Layout, MPacked>() const {
return RotationMatrixAxisAngle<U, 3, 3, Order, Layout, MPacked>(axis, angle);
return RotationMatrixAxisAngle<U, 3, 3, Order, Layout, MPacked>(axis, U(angle));
}

template <class U, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, 3, 4, eMatrixOrder::PRECEDE_VECTOR, Layout, MPacked>() const {
return RotationMatrixAxisAngle<U, 3, 4, eMatrixOrder::PRECEDE_VECTOR, Layout, MPacked>(axis, angle);
return RotationMatrixAxisAngle<U, 3, 4, eMatrixOrder::PRECEDE_VECTOR, Layout, MPacked>(axis, U(angle));
}

template <class U, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, 4, 3, eMatrixOrder::FOLLOW_VECTOR, Layout, MPacked>() const {
return RotationMatrixAxisAngle<U, 4, 3, eMatrixOrder::FOLLOW_VECTOR, Layout, MPacked>(axis, angle);
return RotationMatrixAxisAngle<U, 4, 3, eMatrixOrder::FOLLOW_VECTOR, Layout, MPacked>(axis, U(angle));
}

template <class U, eQuaternionLayout Layout, bool QPacked>
operator Quaternion<U, Layout, QPacked>() const {
return RotationQuaternionAxisAngle<U, Layout, QPacked>(axis, angle);
return RotationQuaternionAxisAngle<U, Layout, QPacked>(axis, U(angle));
}

private:
Expand Down
6 changes: 3 additions & 3 deletions include/Mathter/Transforms/ScaleBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace impl {
template <class T, int Dim>
class ScaleBuilder {
public:
ScaleBuilder(const std::array<T, Dim>& scale) : scale(scale) {}
explicit ScaleBuilder(const std::array<T, Dim>& scale) : scale(scale) {}

template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, Dim + 1, Dim + 1, Order, Layout, MPacked>() const {
Expand Down Expand Up @@ -56,10 +56,10 @@ namespace impl {
m = Zero();
size_t i;
for (i = 0; i < scale.size(); ++i) {
m(i, i) = std::move(scale[i]);
m(i, i) = static_cast<U>(scale[i]);
}
for (; i < std::min(Rows, Columns); ++i) {
m(i, i) = T(1);
m(i, i) = static_cast<U>(1);
}
}

Expand Down
10 changes: 5 additions & 5 deletions include/Mathter/Transforms/TranslationBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace impl {
template <class T, int Dim>
class TranslationBuilder {
public:
TranslationBuilder(const std::array<T, Dim>& translation) : translation(translation) {}
explicit TranslationBuilder(const std::array<T, Dim>& translation) : translation(translation) {}

template <class U, eMatrixOrder Order, eMatrixLayout Layout, bool MPacked>
operator Matrix<U, Dim + 1, Dim + 1, Order, Layout, MPacked>() const {
Expand Down Expand Up @@ -48,13 +48,13 @@ namespace impl {
void Set(Matrix<U, Rows, Columns, Order, Layout, MPacked>& m) const {
m = Identity();
if constexpr (Order == eMatrixOrder::FOLLOW_VECTOR) {
for (int i = 0; i < translation.size(); ++i) {
m(Rows - 1, i) = U(translation[i]);
for (size_t i = 0; i < translation.size(); ++i) {
m(Rows - 1, i) = static_cast<U>(translation[i]);
}
}
else {
for (int i = 0; i < translation.size(); ++i) {
m(i, Columns - 1) = U(translation[i]);
for (size_t i = 0; i < translation.size(); ++i) {
m(i, Columns - 1) = static_cast<U>(translation[i]);
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions include/Mathter/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "Common/TypeTraits.hpp"
#include "Common/Types.hpp"
#include "Vector/Math.hpp"
#include "Vector/Vector.hpp"

#include <algorithm>
#include <type_traits>
Expand Down Expand Up @@ -39,15 +41,15 @@ class Constants {
template <class Scalar>
auto Rad2Deg(Scalar rad) {
using Real = remove_complex_t<Scalar>;
using ComputeT = std::conditional_t<std::is_floating_point<Real>::value, Real, long double>;
using ComputeT = std::conditional_t<std::is_floating_point_v<Real>, Real, long double>;
return rad / Constants<ComputeT>::Pi * ComputeT(180);
}

/// <summary> Converts degrees to radians. </summary>
template <class Scalar>
auto Deg2Rad(Scalar deg) {
using Real = remove_complex_t<Scalar>;
using ComputeT = std::conditional_t<std::is_floating_point<Real>::value, Real, long double>;
using ComputeT = std::conditional_t<std::is_floating_point_v<Real>, Real, long double>;
return deg / ComputeT(180) * Constants<ComputeT>::Pi;
}

Expand Down Expand Up @@ -75,15 +77,6 @@ template <class T, int Dim, bool Packed>
Vector<T, Dim, Packed> Saturate(const Vector<T, Dim, Packed>& arg);


} // namespace mathter


#include "Vector/Math.hpp"
#include "Vector/Vector.hpp"

// Implementations of vector clamp functions.
namespace mathter {

template <class T, int Dim, bool Packed>
Vector<T, Dim, Packed> Clamp(const Vector<T, Dim, Packed>& arg, T lower, T upper) {
using Vec = Vector<T, Dim, Packed>;
Expand Down
2 changes: 1 addition & 1 deletion include/Mathter/Vector/Arithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace mathter {


template <class Vec, class Fun>
auto AvoidDivByZero(Vec&& vec, const Fun& fun) {
auto AvoidDivByZero(const Vec& vec, const Fun&) {
using VecDecay = std::decay_t<Vec>;
if constexpr (VecDecay::isBatched && std::is_same_v<std::decay_t<Fun>, std::divides<void>>) {
return VecDecay(FillMasked<dimension_v<VecDecay>>(vec.elements.Load(), static_cast<scalar_type_t<VecDecay>>(1)));
Expand Down
10 changes: 5 additions & 5 deletions include/Mathter/Vector/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ namespace impl {
int sign = 2 * (Dim % 2) - 1;
for (size_t idx = 0; idx < result.Dimension(); ++idx, sign *= -1) {
// Fill up sub-matrix the determinant of which yields the coefficient of base-vector.
for (int j = 0; j < idx; ++j) {
for (int i = 0; i < detCalc.RowCount(); ++i) {
for (size_t j = 0; j < idx; ++j) {
for (size_t i = 0; i < detCalc.RowCount(); ++i) {
detCalc(i, j) = (*vectors[i]).get()[j];
}
}
for (int j = idx + 1; j < result.Dimension(); ++j) {
for (size_t j = idx + 1; j < result.Dimension(); ++j) {
for (int i = 0; i < detCalc.RowCount(); ++i) {
detCalc(i, j - 1) = (*vectors[i]).get()[j];
}
Expand All @@ -326,13 +326,13 @@ template <class IterFirst, class IterLast, class Vec>
auto Cross(IterFirst first, IterLast last) -> std::enable_if_t<is_vector_v<Vec>, Vec> {
constexpr auto Dim = dimension_v<Vec>;

if constexpr (dimension_v<Vec> == 2) {
if constexpr (Dim == 2) {
if (first == last) {
throw std::invalid_argument("not enough arguments for cross product");
}
return Vec(-first->y, first->x);
}
if constexpr (dimension_v<Vec> == 3) {
if constexpr (Dim == 3) {
if (first == last) {
throw std::invalid_argument("not enough arguments for cross product");
}
Expand Down
2 changes: 1 addition & 1 deletion include/Mathter/Vector/OperationUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template <int NumNonMasked, class Batch, class Element>
Batch FillMasked(Batch batch, Element value) {
#ifdef MATHTER_ENABLE_SIMD
struct MaskGenerator {
static constexpr bool get(unsigned idx, unsigned size) noexcept {
static constexpr bool get(unsigned idx, [[maybe_unused]] unsigned size) noexcept {
return idx < NumNonMasked;
}
};
Expand Down
22 changes: 16 additions & 6 deletions include/Mathter/Vector/SIMDUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
namespace mathter {

constexpr int GetBatchSize(int Dim, bool Packed) {
return Packed ? Dim :
Dim == 3 ? 4 :
Dim == 5 ? 8 :
Dim == 6 ? 8 :
Dim == 7 ? 8 :
Dim;
if (!Packed) {
switch (Dim) {
case 1: return 1;
case 2: return 2;
case 3: return 4;
case 4: return 4;
case 5: return 8;
case 6: return 8;
case 7: return 8;
case 8: return 8;
default: return Dim;
}
}
else {
return Dim;
}
}


Expand Down
Loading

0 comments on commit edb7aff

Please sign in to comment.