From be2f61bce244223e6dd6df9bc29e8f0d96e7f55d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 18 Aug 2022 19:14:50 +0200 Subject: [PATCH] - optimized TVector3::XY() to return a writable reference. --- source/common/utility/vectors.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/common/utility/vectors.h b/source/common/utility/vectors.h index 355591020..7a8ef5321 100644 --- a/source/common/utility/vectors.h +++ b/source/common/utility/vectors.h @@ -308,6 +308,8 @@ template struct TVector3 { typedef TVector2 Vector2; + // this does not compile - should be true on all relevant hardware. + //static_assert(myoffsetof(TVector3, X) == myoffsetof(Vector2, X) && myoffsetof(TVector3, Y) == myoffsetof(Vector2, Y), "TVector2 and TVector3 are not aligned"); vec_t X, Y, Z; @@ -492,9 +494,14 @@ struct TVector3 } // returns the XY fields as a 2D-vector. - Vector2 XY() const + const Vector2& XY() const { - return{ X, Y }; + return *reinterpret_cast(this); + } + + Vector2& XY() + { + return *reinterpret_cast(this); } // Add a 3D vector and a 2D vector.