Skip to content

Commit

Permalink
Enhance Color4F and method naming change (#2072)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rh101 authored Aug 4, 2024
1 parent 8fd2a55 commit 3a46201
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 73 deletions.
43 changes: 5 additions & 38 deletions core/math/Vec4.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
84 changes: 49 additions & 35 deletions core/math/Vec4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -108,6 +111,50 @@ class Vec4Base
return *static_cast<impl_type*>(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<impl_type*>(this)}.negate(); }

/**
Expand Down Expand Up @@ -246,7 +293,7 @@ class AX_DLL Vec4 : public Vec4Base<Vec4>
*
* @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.
Expand All @@ -256,15 +303,6 @@ class AX_DLL Vec4 : public Vec4Base<Vec4>
*/
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).
Expand Down Expand Up @@ -413,37 +451,13 @@ class AX_DLL Vec4 : public Vec4Base<Vec4>
*/
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.
Expand Down

0 comments on commit 3a46201

Please sign in to comment.