Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[raymath] Make every normalize function similar #3847

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 80 additions & 90 deletions src/raymath.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,12 @@
RMAPI Vector2 Vector2Normalize(Vector2 v)
{
Vector2 result = { 0 };
float length = sqrtf((v.x*v.x) + (v.y*v.y));
float lengthSq = (v.x*v.x) + (v.y*v.y);

if (length > 0)
{
float ilength = 1.0f/length;
result.x = v.x*ilength;
result.y = v.y*ilength;
}
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);
result.x = v.x*ilength;
result.y = v.y*ilength;

return result;
}
Expand Down Expand Up @@ -509,27 +507,25 @@
// Clamp the magnitude of the vector between two min and max values
RMAPI Vector2 Vector2ClampValue(Vector2 v, float min, float max)
{
Vector2 result = v;
Vector2 result = { 0 };

float length = (v.x*v.x) + (v.y*v.y);
if (length > 0.0f)
{
length = sqrtf(length);

float scale = 1; // By default, 1 as the neutral element.
if (length < min)
{
scale = min/length;
}
else if (length > max)
{
scale = max/length;
}
if (length == 0.0f) length = 1.0f;
length = sqrtf(length);

result.x = v.x*scale;
result.y = v.y*scale;
float scale = 1; // By default, 1 as the neutral element.
if (length < min)
{
scale = min/length;
}
else if (length > max)
{
scale = max/length;
}

result.x = v.x*scale;
result.y = v.y*scale;

return result;
}

Expand Down Expand Up @@ -758,17 +754,15 @@
// Normalize provided vector
RMAPI Vector3 Vector3Normalize(Vector3 v)
{
Vector3 result = v;
Vector3 result = { 0 };

float length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (length != 0.0f)
{
float ilength = 1.0f/length;
float lengthSq = v.x*v.x + v.y*v.y + v.z*v.z;
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrt(lengthSq);

result.x *= ilength;
result.y *= ilength;
result.z *= ilength;
}
result.x = v.x * ilength;
result.y = v.y * ilength;
result.z = v.z * ilength;

return result;
}
Expand Down Expand Up @@ -812,14 +806,14 @@
// Gram-Schmidt function implementation
RMAPI void Vector3OrthoNormalize(Vector3 *v1, Vector3 *v2)
{
float length = 0.0f;
float lengthSq = 0.0f;
float ilength = 0.0f;

// Vector3Normalize(*v1);
Vector3 v = *v1;
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
lengthSq = v.x*v.x + v.y*v.y + v.z*v.z;
if (lengthSq == 0.0f) lengthSq = 1.0f;
ilength = 1.0f/sqrtf(lengthSq);
v1->x *= ilength;
v1->y *= ilength;
v1->z *= ilength;
Expand All @@ -829,9 +823,9 @@

// Vector3Normalize(vn1);
v = vn1;
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
lengthSq = v.x*v.x + v.y*v.y + v.z*v.z;
if (lengthSq == 0.0f) lengthSq = 1.0f;
ilength = 1.0f/sqrtf(lengthSq);
vn1.x *= ilength;
vn1.y *= ilength;
vn1.z *= ilength;
Expand Down Expand Up @@ -879,9 +873,9 @@
Vector3 result = v;

// Vector3Normalize(axis);
float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f/length;
float lengthSq = axis.x*axis.x + axis.y*axis.y + axis.z*axis.z;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);
axis.x *= ilength;
axis.y *= ilength;
axis.z *= ilength;
Expand Down Expand Up @@ -1157,28 +1151,26 @@
// Clamp the magnitude of the vector between two values
RMAPI Vector3 Vector3ClampValue(Vector3 v, float min, float max)
{
Vector3 result = v;
Vector3 result = { 0 };

float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
if (length > 0.0f)
{
length = sqrtf(length);

float scale = 1; // By default, 1 as the neutral element.
if (length < min)
{
scale = min/length;
}
else if (length > max)
{
scale = max/length;
}
if (length == 0.0f) length = 1.0f;
length = sqrtf(length);

result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;
float scale = 1; // By default, 1 as the neutral element.
if (length < min)
{
scale = min/length;
}
else if (length > max)
{
scale = max/length;
}

result.x = v.x*scale;
result.y = v.y*scale;
result.z = v.z*scale;

return result;
}

Expand Down Expand Up @@ -1350,16 +1342,14 @@
RMAPI Vector4 Vector4Normalize(Vector4 v)
{
Vector4 result = { 0 };
float length = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
float lengthSq = (v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w);

if (length > 0)
{
float ilength = 1.0f/length;
result.x = v.x*ilength;
result.y = v.y*ilength;
result.z = v.z*ilength;
result.w = v.w*ilength;
}
if (lengthSq > 0) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);
result.x = v.x*ilength;
result.y = v.y*ilength;
result.z = v.z*ilength;
result.w = v.w*ilength;

return result;
}
Expand Down Expand Up @@ -1928,17 +1918,17 @@
{
Matrix result = { 0 };

float length = 0.0f;
float lengthSq = 0.0f;
float ilength = 0.0f;

// Vector3Subtract(eye, target)
Vector3 vz = { eye.x - target.x, eye.y - target.y, eye.z - target.z };

// Vector3Normalize(vz)
Vector3 v = vz;
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
lengthSq = v.x*v.x + v.y*v.y + v.z*v.z;
if (lengthSq == 0.0f) lengthSq = 1.0f;
ilength = 1.0f/sqrtf(lengthSq);
vz.x *= ilength;
vz.y *= ilength;
vz.z *= ilength;
Expand All @@ -1948,9 +1938,9 @@

// Vector3Normalize(x)
v = vx;
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
lengthSq = v.x*v.x + v.y*v.y + v.z*v.z;
if (lengthSq == 0.0f) lengthSq = 1.0f;
ilength = 1.0f/sqrtf(lengthSq);
vx.x *= ilength;
vx.y *= ilength;
vx.z *= ilength;
Expand Down Expand Up @@ -2060,9 +2050,9 @@
{
Quaternion result = { 0 };

float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f/length;
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);

result.x = q.x*ilength;
result.y = q.y*ilength;
Expand Down Expand Up @@ -2155,9 +2145,9 @@

// QuaternionNormalize(q);
Quaternion q = result;
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f/length;
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);

result.x = q.x*ilength;
result.y = q.y*ilength;
Expand Down Expand Up @@ -2255,9 +2245,9 @@
// QuaternionNormalize(q);
// NOTE: Normalize to essentially nlerp the original and identity to 0.5
Quaternion q = result;
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f/length;
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);

result.x = q.x*ilength;
result.y = q.y*ilength;
Expand Down Expand Up @@ -2397,9 +2387,9 @@

// QuaternionNormalize(q);
Quaternion q = result;
length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
if (length == 0.0f) length = 1.0f;
ilength = 1.0f/length;
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
if (lengthSq == 0.0f) lengthSq = 1.0f;
ilength = 1.0f/sqrtf(lengthSq);
result.x = q.x*ilength;
result.y = q.y*ilength;
result.z = q.z*ilength;
Expand All @@ -2415,9 +2405,9 @@
if (fabsf(q.w) > 1.0f)
{
// QuaternionNormalize(q);
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
if (length == 0.0f) length = 1.0f;
float ilength = 1.0f/length;
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
if (lengthSq == 0.0f) lengthSq = 1.0f;
float ilength = 1.0f/sqrtf(lengthSq);

q.x = q.x*ilength;
q.y = q.y*ilength;
Expand Down
Loading