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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 Vector2Divide(Vector2 v1, Vector2 v2)
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 @@ RMAPI Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max)
// 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 @@ RMAPI Vector3 Vector3Divide(Vector3 v1, Vector3 v2)
// 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 @@ RMAPI Vector3 Vector3Reject(Vector3 v1, Vector3 v2)
// 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 @@ RMAPI void Vector3OrthoNormalize(Vector3 *v1, Vector3 *v2)

// 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 @@ RMAPI Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle)
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;
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 @@ RMAPI Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max)
// 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 Vector4Divide(Vector4 v1, Vector4 v2)
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 @@ RMAPI Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
{
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 @@ RMAPI Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)

// 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 @@ RMAPI Quaternion QuaternionNormalize(Quaternion q)
{
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 @@ RMAPI Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount)

// 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 @@ RMAPI Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
// 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 @@ RMAPI Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle)

// 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;
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 @@ RMAPI void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle
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;
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