From 3a46201c5a621c5779a5d4d2c54dda6b8c191de4 Mon Sep 17 00:00:00 2001 From: RH Date: Sun, 4 Aug 2024 21:50:14 +1000 Subject: [PATCH] Enhance Color4F and method naming change (#2072) * Move set methods from Vec4 to base Vec4Base to allow usage with all subclasses of Vec4Base Rename Vec4::set method to setDirection to reflect its actual purpose Remove trivial copy constructor method since it does not need to be explicitly implemented * Add copyright line --- core/math/Vec4.cpp | 43 +++--------------------- core/math/Vec4.h | 84 +++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 73 deletions(-) diff --git a/core/math/Vec4.cpp b/core/math/Vec4.cpp index 38d3f8983571..9cce1a49747e 100644 --- a/core/math/Vec4.cpp +++ b/core/math/Vec4.cpp @@ -1,6 +1,7 @@ /** Copyright 2013 BlackBerry Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md). Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -27,23 +28,15 @@ NS_AX_MATH_BEGIN -Vec4::Vec4() : Vec4Base() {} +Vec4::Vec4() {} Vec4::Vec4(float xx, float yy, float zz, float ww) : Vec4Base(xx, yy, zz, ww) {} -Vec4::Vec4(const float* src) -{ - set(src); -} +Vec4::Vec4(const float* src) : Vec4Base(src) {} Vec4::Vec4(const Vec4& p1, const Vec4& p2) { - set(p1, p2); -} - -Vec4::Vec4(const Vec4& copy) -{ - set(copy); + setDirection(p1, p2); } Vec4 Vec4::fromColor(unsigned int color) @@ -219,33 +212,7 @@ Vec4 Vec4::getNormalized() const return v; } -void Vec4::set(float xx, float yy, float zz, float ww) -{ - this->x = xx; - this->y = yy; - this->z = zz; - this->w = ww; -} - -void Vec4::set(const float* array) -{ - GP_ASSERT(array); - - x = array[0]; - y = array[1]; - z = array[2]; - w = array[3]; -} - -void Vec4::set(const Vec4& v) -{ - this->x = v.x; - this->y = v.y; - this->z = v.z; - this->w = v.w; -} - -void Vec4::set(const Vec4& p1, const Vec4& p2) +void Vec4::setDirection(const Vec4& p1, const Vec4& p2) { x = p2.x - p1.x; y = p2.y - p1.y; diff --git a/core/math/Vec4.h b/core/math/Vec4.h index e8394591d2ba..c17630339b86 100644 --- a/core/math/Vec4.h +++ b/core/math/Vec4.h @@ -2,6 +2,7 @@ Copyright 2013 BlackBerry Inc. Copyright (c) 2014-2017 Chukong Technologies Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md). Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -45,6 +46,8 @@ class Vec4Base Vec4Base() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {} Vec4Base(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {} + explicit Vec4Base(const float* src) { this->set(src); } + union { struct @@ -108,6 +111,50 @@ class Vec4Base return *static_cast(this); } + /** + * Sets the elements of this vector to the specified values. + * + * @param xx The new x coordinate. + * @param yy The new y coordinate. + * @param zz The new z coordinate. + * @param ww The new w coordinate. + */ + void set(float xx, float yy, float zz, float ww) + { + this->x = xx; + this->y = yy; + this->z = zz; + this->w = ww; + } + + /** + * Sets the elements of this vector from the values in the specified array. + * + * @param array An array containing the elements of the vector in the order x, y, z, w. + */ + void set(const float* array) + { + GP_ASSERT(array); + + this->x = array[0]; + this->y = array[1]; + this->z = array[2]; + this->w = array[3]; + } + + /** + * Sets the elements of this vector to those in the specified vector. + * + * @param v The vector to copy. + */ + void set(const impl_type& v) + { + this->x = v.x; + this->y = v.y; + this->z = v.z; + this->w = v.w; + } + inline impl_type& operator-() { return impl_type{*static_cast(this)}.negate(); } /** @@ -246,7 +293,7 @@ class AX_DLL Vec4 : public Vec4Base * * @param array An array containing the elements of the vector in the order x, y, z, w. */ - Vec4(const float* array); + explicit Vec4(const float* array); /** * Constructs a vector that describes the direction between the specified points. @@ -256,15 +303,6 @@ class AX_DLL Vec4 : public Vec4Base */ Vec4(const Vec4& p1, const Vec4& p2); - /** - * Constructor. - * - * Creates a new vector that is a copy of the specified vector. - * - * @param copy The vector to copy. - */ - Vec4(const Vec4& copy); - /** * Creates a new vector from an integer interpreted as an RGBA value. * E.g. 0xff0000ff represents opaque red or the vector (1, 0, 0, 1). @@ -413,37 +451,13 @@ class AX_DLL Vec4 : public Vec4Base */ Vec4 getNormalized() const; - /** - * Sets the elements of this vector to the specified values. - * - * @param xx The new x coordinate. - * @param yy The new y coordinate. - * @param zz The new z coordinate. - * @param ww The new w coordinate. - */ - void set(float xx, float yy, float zz, float ww); - - /** - * Sets the elements of this vector from the values in the specified array. - * - * @param array An array containing the elements of the vector in the order x, y, z, w. - */ - void set(const float* array); - - /** - * Sets the elements of this vector to those in the specified vector. - * - * @param v The vector to copy. - */ - void set(const Vec4& v); - /** * Sets this vector to the directional vector between the specified points. * * @param p1 The first point. * @param p2 The second point. */ - void set(const Vec4& p1, const Vec4& p2); + void setDirection(const Vec4& p1, const Vec4& p2); /** * Subtracts the specified vectors and stores the result in dst.