From 6481f8cce909bc801f7544d67f2bf5f96cd01181 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 3 Apr 2025 07:51:03 +0200 Subject: [PATCH] rewrote XY and XYZ accessors for vectors to be read-only and not use type punning. --- src/common/utility/vectors.h | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/common/utility/vectors.h b/src/common/utility/vectors.h index 47eb597938..299a08985a 100644 --- a/src/common/utility/vectors.h +++ b/src/common/utility/vectors.h @@ -539,15 +539,9 @@ struct TVector3 return *this; } - // returns the XY fields as a 2D-vector. - constexpr const Vector2& XY() const + constexpr Vector2 XY() const { - return *reinterpret_cast(this); - } - - constexpr Vector2& XY() - { - return *reinterpret_cast(this); + return Vector2(X, Y); } // Add a 3D vector and a 2D vector. @@ -785,28 +779,16 @@ struct TVector4 } // returns the XY fields as a 2D-vector. - constexpr const Vector2& XY() const + constexpr Vector2 XY() const { - return *reinterpret_cast(this); + return Vector2(X, Y); } - constexpr Vector2& XY() + constexpr Vector3 XYZ() const { - return *reinterpret_cast(this); + return Vector3(X, Y, Z); } - // returns the XY fields as a 2D-vector. - constexpr const Vector3& XYZ() const - { - return *reinterpret_cast(this); - } - - constexpr Vector3& XYZ() - { - return *reinterpret_cast(this); - } - - // Test for approximate equality bool ApproximatelyEquals(const TVector4 &other) const { @@ -1789,7 +1771,9 @@ struct TRotator template inline TVector3::TVector3 (const TRotator &rot) { - XY() = rot.Pitch.Cos() * rot.Yaw.ToVector(); + auto XY = rot.Pitch.Cos() * rot.Yaw.ToVector(); + X = XY.X; + Y = XY.Y; Z = rot.Pitch.Sin(); }