Efficiency improvements

This commit is contained in:
Pascal Serrarens 2024-01-23 10:57:39 +01:00
parent dc9d4ee42e
commit fd252d4b45
2 changed files with 40 additions and 23 deletions

View File

@ -38,6 +38,13 @@ const Vector3 Vector3::down = Vector3(0, -1, 0);
const Vector3 Vector3::forward = Vector3(0, 0, 1);
const Vector3 Vector3::back = Vector3(0, 0, -1);
// inline float Vector3::Forward() { return z; }
// inline float Vector3::Up() { return y; }
// inline float Vector3::Right() { return x; }
Vector3 Vector3::FromHorizontal(const Vector2 &v) {
return Vector3(v.x, 0, v.y);
}
float Vector3::Magnitude(const Vector3 &a) {
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
}
@ -48,7 +55,7 @@ float Vector3::SqrMagnitude(const Vector3 &a) {
}
float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); }
Vector3 Vector3::Normalize(Vector3 v) {
Vector3 Vector3::Normalize(const Vector3 &v) {
float num = Vector3::Magnitude(v);
Vector3 result = Vector3::zero;
if (num > epsilon) {
@ -83,7 +90,7 @@ Vector3 Vector3::operator*(float f) const {
return Vector3(this->x * f, this->y * f, this->z * f);
}
Vector3 Vector3::operator/(const float &d) {
Vector3 Vector3::operator/(float d) const {
return Vector3(this->x / d, this->y / d, this->z / d);
}
@ -104,7 +111,7 @@ Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
v1.x * v2.y - v1.y * v2.x);
}
Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
Vector3 Vector3::Project(const Vector3 &vector, const Vector3 &onNormal) {
float sqrMagnitude = Dot(onNormal, onNormal);
if (sqrMagnitude < epsilon)
return Vector3::zero;
@ -115,12 +122,13 @@ Vector3 Vector3::Project(Vector3 vector, Vector3 onNormal) {
}
}
Vector3 Vector3::ProjectOnPlane(Vector3 vector, Vector3 planeNormal) {
Vector3 Vector3::ProjectOnPlane(const Vector3 &vector,
const Vector3 &planeNormal) {
Vector3 r = vector - Project(vector, planeNormal);
return r;
}
Vector2 Vector3::ProjectHorizontalPlane(Vector3 vector) {
Vector2 Vector3::ProjectHorizontalPlane(const Vector3 &vector) {
Vector2 r = Vector2(vector.x, vector.z);
return r;
}
@ -131,7 +139,7 @@ float clamp(float x, float lower, float upper) {
return upperClamp;
}
float Vector3::Angle(Vector3 from, Vector3 to) {
float Vector3::Angle(const Vector3 &from, const Vector3 &to) {
float denominator = sqrtf(from.sqrMagnitude() * to.sqrMagnitude());
if (denominator < epsilon)
return 0;
@ -146,7 +154,8 @@ float Vector3::Angle(Vector3 from, Vector3 to) {
return r;
}
float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
float Vector3::SignedAngle(const Vector3 &from, const Vector3 &to,
const Vector3 &axis) {
// angle in [0,180]
float angle = Vector3::Angle(from, to);
@ -160,7 +169,7 @@ float Vector3::SignedAngle(Vector3 from, Vector3 to, Vector3 axis) {
return signed_angle;
}
Vector3 Vector3::Lerp(Vector3 from, Vector3 to, float f) {
Vector3 Vector3::Lerp(const Vector3 &from, const Vector3 &to, float f) {
Vector3 v = from + (to - from) * f;
return v;
}

View File

@ -63,30 +63,36 @@ public:
/// </summary>
const static Vector3 one;
/// <summary>
/// A vector with values (1, 0, 0)
/// A normalized vector pointing in the right direction
/// </summary>
const static Vector3 right;
/// <summary>
/// A vector3 with values (-1, 0, 0)
/// A normalized vector pointing in the left direction
/// </summary>
const static Vector3 left;
/// <summary>
/// A vector with values (0, 1, 0)
/// A normalized vector pointing in the upward direction
/// </summary>
const static Vector3 up;
/// <summary>
/// A vector with values (0, -1, 0)
/// A normalized vector pointing in the downward direcion
/// </summary>
const static Vector3 down;
/// <summary>
/// A vector with values (0, 0, 1)
/// A normalized vector pointing in the forward direction
/// </summary>
const static Vector3 forward;
/// <summary>
/// A vector with values (0, 0, -1)
/// A normalized vector pointing in the backward direction
/// </summary>
const static Vector3 back;
// Experimental Access functions which are intended to replace the use of XYZ
inline float Forward() { return z; };
inline float Up() { return y; };
inline float Right() { return x; };
static Vector3 FromHorizontal(const Vector2 &vector);
/// <summary>
/// The length of a vector
/// </summary>
@ -119,7 +125,7 @@ public:
/// </summary>
/// <param name="vector">The vector to convert</param>
/// <returns>The vector with length 1</returns>
static Vector3 Normalize(Vector3 vector);
static Vector3 Normalize(const Vector3 &vector);
/// <summary>
/// Convert the vector to a length of a
/// </summary>
@ -161,14 +167,14 @@ public:
/// <param name="factor">The scaling factor</param>
/// <returns>The scaled vector</returns>
/// Each component of the vector will be multipled with the same factor.
Vector3 operator*(float factor) const;
Vector3 operator*(const float factor) const;
/// <summary>
/// Scale a vector uniformy down
/// </summary>
/// <param name="factor">The scaling factor</param>
/// <returns>The scaled vector</returns>
/// Each componet of the vector will be divided by the same factor.
Vector3 operator/(const float &factor);
Vector3 operator/(const float factor) const;
/// <summary>
/// The dot product of two vectors
@ -209,7 +215,7 @@ public:
/// <param name="vector">The vector to project</param>
/// <param name="onNormal">The normal vector to project on</param>
/// <returns>The projected vector</returns>
static Vector3 Project(Vector3 vector, Vector3 onNormal);
static Vector3 Project(const Vector3 &vector, const Vector3 &onNormal);
/// <summary>
/// Projects a vector onto a plane defined by a normal orthogonal to the
/// plane.
@ -217,7 +223,8 @@ public:
/// <param name="vector">The vector to project</param>
/// <param name="planeNormal">The normal of the plane to project on</param>
/// <returns></returns>
static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
static Vector3 ProjectOnPlane(const Vector3 &vector,
const Vector3 &planeNormal);
/// <summary>
/// Projects a vector onto the horizontal plane.
@ -225,7 +232,7 @@ public:
/// <param name="vector">The vector to project</param>
/// <returns>A 2D carthesian vector with the coordinates in the horizontal
/// plane.</returns>
static Vector2 ProjectHorizontalPlane(Vector3 vector);
static Vector2 ProjectHorizontalPlane(const Vector3 &vector);
/// <summary>
/// Calculate the angle between two vectors
@ -236,7 +243,7 @@ public:
/// This reterns an unsigned angle which is the shortest distance
/// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed.
static float Angle(Vector3 vector1, Vector3 vector2);
static float Angle(const Vector3 &vector1, const Vector3 &vector2);
/// <summary>
/// Calculate the angle between two vectors rotation around an axis.
@ -245,7 +252,8 @@ public:
/// <param name="to">The ending vector</param>
/// <param name="axis">The axis to rotate around</param>
/// <returns>The signed angle</returns>
static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
static float SignedAngle(const Vector3 &from, const Vector3 &to,
const Vector3 &axis);
/// <summary>
/// Lerp between two vectors
@ -257,7 +265,7 @@ public:
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1
/// matches the *to* vector Value -1 is *from* vector minus the difference
/// between *from* and *to* etc.
static Vector3 Lerp(Vector3 from, Vector3 to, float f);
static Vector3 Lerp(const Vector3 &from, const Vector3 &to, float f);
};
#endif