Fix code style
This commit is contained in:
@@ -70,23 +70,21 @@ Color Color::FromHex(const String& hexString, bool& isValid)
|
||||
return FromBytes(r, g, b, a);
|
||||
}
|
||||
|
||||
Color Color::FromHSV(const Vector3& hsv)
|
||||
Color Color::FromHSV(float hue, float saturation, float value, float alpha)
|
||||
{
|
||||
const float Hue = hsv.X;
|
||||
const float Saturation = hsv.Y;
|
||||
const float Value = hsv.Z;
|
||||
const float hDiv60 = hue / 60.0f;
|
||||
const float hDiv60Floor = Math::Floor(hDiv60);
|
||||
const float hDiv60Fraction = hDiv60 - hDiv60Floor;
|
||||
|
||||
const float HDiv60 = Hue / 60.0f;
|
||||
const float HDiv60_Floor = floorf(HDiv60);
|
||||
const float HDiv60_Fraction = HDiv60 - HDiv60_Floor;
|
||||
|
||||
const float RGBValues[4] = {
|
||||
Value,
|
||||
Value * (1.0f - Saturation),
|
||||
Value * (1.0f - HDiv60_Fraction * Saturation),
|
||||
Value * (1.0f - (1.0f - HDiv60_Fraction) * Saturation),
|
||||
const float rgbValues[4] =
|
||||
{
|
||||
value,
|
||||
value * (1.0f - saturation),
|
||||
value * (1.0f - hDiv60Fraction * saturation),
|
||||
value * (1.0f - (1.0f - hDiv60Fraction) * saturation),
|
||||
};
|
||||
const uint32 RGBSwizzle[6][3] = {
|
||||
const int32 rgbSwizzle[6][3]
|
||||
{
|
||||
{ 0, 3, 1 },
|
||||
{ 2, 0, 1 },
|
||||
{ 1, 0, 3 },
|
||||
@@ -94,12 +92,17 @@ Color Color::FromHSV(const Vector3& hsv)
|
||||
{ 3, 1, 0 },
|
||||
{ 0, 1, 2 },
|
||||
};
|
||||
const uint32 SwizzleIndex = (uint32)HDiv60_Floor % 6;
|
||||
const int32 swizzleIndex = (int32)hDiv60Floor % 6;
|
||||
|
||||
return Color(RGBValues[RGBSwizzle[SwizzleIndex][0]],
|
||||
RGBValues[RGBSwizzle[SwizzleIndex][1]],
|
||||
RGBValues[RGBSwizzle[SwizzleIndex][2]],
|
||||
1.0f);
|
||||
return Color(rgbValues[rgbSwizzle[swizzleIndex][0]],
|
||||
rgbValues[rgbSwizzle[swizzleIndex][1]],
|
||||
rgbValues[rgbSwizzle[swizzleIndex][2]],
|
||||
alpha);
|
||||
}
|
||||
|
||||
Color Color::FromHSV(const Vector3& hsv, float alpha)
|
||||
{
|
||||
return FromHSV(hsv.X, hsv.Y, hsv.Z, alpha);
|
||||
}
|
||||
|
||||
Color Color::Random()
|
||||
|
||||
@@ -516,7 +516,7 @@ namespace FlaxEngine
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1] paked to XYZ vector.
|
||||
/// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1] packed to XYZ vector.
|
||||
/// </summary>
|
||||
/// <param name="hsv">The HSV color.</param>
|
||||
/// <param name="alpha">The alpha value. Default is 1.</param>
|
||||
|
||||
@@ -152,12 +152,23 @@ public:
|
||||
|
||||
static Color FromHex(const String& hexString, bool& isValid);
|
||||
|
||||
/// <summary>
|
||||
/// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1].
|
||||
/// </summary>
|
||||
/// <param name="hue">The hue angle in degrees [0-360].</param>
|
||||
/// <param name="saturation">The saturation normalized [0-1].</param>
|
||||
/// <param name="value">The value normalized [0-1].</param>
|
||||
/// <param name="alpha">The alpha value. Default is 1.</param>
|
||||
/// <returns>The RGB color.</returns>
|
||||
static Color FromHSV(float hue, float saturation, float value, float alpha = 1.0f);
|
||||
|
||||
/// <summary>
|
||||
/// Creates RGB color from Hue[0-360], Saturation[0-1] and Value[0-1] packed to XYZ vector.
|
||||
/// </summary>
|
||||
/// <param name="hsv">The HSV color.</param>
|
||||
/// <param name="alpha">The alpha value. Default is 1.</param>
|
||||
/// <returns>The RGB color.</returns>
|
||||
static Color FromHSV(const Vector3& hsv);
|
||||
static Color FromHSV(const Vector3& hsv, float alpha = 1.0f);
|
||||
|
||||
/// <summary>
|
||||
/// Gets random color with opaque alpha.
|
||||
|
||||
@@ -3,6 +3,46 @@
|
||||
#include "Math.h"
|
||||
#include "Vector3.h"
|
||||
|
||||
void Math::SinCos(float angle, float& sine, float& cosine)
|
||||
{
|
||||
sine = sin(angle);
|
||||
cosine = cos(angle);
|
||||
}
|
||||
|
||||
uint32 Math::FloorLog2(uint32 value)
|
||||
{
|
||||
// References:
|
||||
// http://codinggorilla.domemtech.com/?p=81
|
||||
// http://en.wikipedia.org/wiki/Binary_logarithm
|
||||
|
||||
uint32 pos = 0;
|
||||
if (value >= 1 << 16)
|
||||
{
|
||||
value >>= 16;
|
||||
pos += 16;
|
||||
}
|
||||
if (value >= 1 << 8)
|
||||
{
|
||||
value >>= 8;
|
||||
pos += 8;
|
||||
}
|
||||
if (value >= 1 << 4)
|
||||
{
|
||||
value >>= 4;
|
||||
pos += 4;
|
||||
}
|
||||
if (value >= 1 << 2)
|
||||
{
|
||||
value >>= 2;
|
||||
pos += 2;
|
||||
}
|
||||
if (value >= 1 << 1)
|
||||
{
|
||||
pos += 1;
|
||||
}
|
||||
return value == 0 ? 0 : pos;
|
||||
}
|
||||
|
||||
Vector3 Math::RotateAboutAxis(const Vector3& normalizedRotationAxis, float angle, const Vector3& positionOnAxis, const Vector3& position)
|
||||
{
|
||||
// Project position onto the rotation axis and find the closest point on the axis to Position
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
#define ZeroTolerance 1e-6f
|
||||
|
||||
// Converts radians to degrees.
|
||||
#define RadiansToDegrees (180.f / PI)
|
||||
#define RadiansToDegrees (180.0f / PI)
|
||||
|
||||
// Converts degrees to radians.
|
||||
#define DegreesToRadians (PI / 180.f)
|
||||
#define DegreesToRadians (PI / 180.0f)
|
||||
|
||||
namespace Math
|
||||
{
|
||||
@@ -31,97 +31,14 @@ namespace Math
|
||||
/// <param name="angle">The input angle (in radians).</param>
|
||||
/// <param name="sine">The output sine value.</param>
|
||||
/// <param name="cosine">The output cosine value.</param>
|
||||
static void SinCos(float angle, float& sine, float& cosine)
|
||||
{
|
||||
#if 1
|
||||
// Map value to y in [-pi,pi], x = 2*pi*quotient + remainder
|
||||
float quotient = PI_INV * 0.5f * angle;
|
||||
if (angle >= 0.0f)
|
||||
{
|
||||
quotient = (float)(int)(quotient + 0.5f);
|
||||
}
|
||||
else
|
||||
{
|
||||
quotient = (float)(int)(quotient - 0.5f);
|
||||
}
|
||||
float y = angle - 2.0f * PI * quotient;
|
||||
|
||||
// Map y to [-pi/2,pi/2] with sin(y) = sin(value)
|
||||
float sign;
|
||||
if (y > PI_OVER_2)
|
||||
{
|
||||
y = PI - y;
|
||||
sign = -1.0f;
|
||||
}
|
||||
else if (y < -PI_OVER_2)
|
||||
{
|
||||
y = -PI - y;
|
||||
sign = -1.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
sign = +1.0f;
|
||||
}
|
||||
|
||||
const float y2 = y * y;
|
||||
|
||||
// 11-degree minimax approximation
|
||||
sine = (((((-2.3889859e-08f * y2 + 2.7525562e-06f) * y2 - 0.00019840874f) * y2 + 0.0083333310f) * y2 - 0.16666667f) * y2 + 1.0f) * y;
|
||||
|
||||
// 10-degree minimax approximation
|
||||
const float p = ((((-2.6051615e-07f * y2 + 2.4760495e-05f) * y2 - 0.0013888378f) * y2 + 0.041666638f) * y2 - 0.5f) * y2 + 1.0f;
|
||||
cosine = sign * p;
|
||||
#else
|
||||
sine = sin(angle);
|
||||
cosine = cos(angle);
|
||||
#endif
|
||||
}
|
||||
FLAXENGINE_API void SinCos(float angle, float& sine, float& cosine);
|
||||
|
||||
/// <summary>
|
||||
/// Computes the base 2 logarithm for an integer value that is greater than 0. The result is rounded down to the nearest integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to compute the log of.</param>
|
||||
/// <returns>The Log2 of value. 0 if value is 0.</returns>
|
||||
static uint32 FloorLog2(uint32 value)
|
||||
{
|
||||
// Reference implementation
|
||||
// uint32 bit = 32;
|
||||
// for (; bit > 0;)
|
||||
// {
|
||||
// bit--;
|
||||
// if (value & (1 << bit))
|
||||
// break;
|
||||
// }
|
||||
// return bit;
|
||||
|
||||
// Optimized version http://codinggorilla.domemtech.com/?p=81 or http://en.wikipedia.org/wiki/Binary_logarithm but modified to return 0 for a input value of 0
|
||||
uint32 pos = 0;
|
||||
if (value >= 1 << 16)
|
||||
{
|
||||
value >>= 16;
|
||||
pos += 16;
|
||||
}
|
||||
if (value >= 1 << 8)
|
||||
{
|
||||
value >>= 8;
|
||||
pos += 8;
|
||||
}
|
||||
if (value >= 1 << 4)
|
||||
{
|
||||
value >>= 4;
|
||||
pos += 4;
|
||||
}
|
||||
if (value >= 1 << 2)
|
||||
{
|
||||
value >>= 2;
|
||||
pos += 2;
|
||||
}
|
||||
if (value >= 1 << 1)
|
||||
{
|
||||
pos += 1;
|
||||
}
|
||||
return value == 0 ? 0 : pos;
|
||||
}
|
||||
FLAXENGINE_API uint32 FloorLog2(uint32 value);
|
||||
|
||||
static FORCE_INLINE float Trunc(float value)
|
||||
{
|
||||
@@ -712,68 +629,38 @@ namespace Math
|
||||
|
||||
static float ClampAxis(float angle)
|
||||
{
|
||||
// returns angle in the range (-360,360)
|
||||
angle = Mod(angle, 360.f);
|
||||
|
||||
if (angle < 0.f)
|
||||
{
|
||||
// shift to [0,360) range
|
||||
angle += 360.f;
|
||||
}
|
||||
|
||||
if (angle < 0.0f)
|
||||
angle += 360.0f;
|
||||
return angle;
|
||||
}
|
||||
|
||||
static float NormalizeAxis(float angle)
|
||||
{
|
||||
// returns angle in the range [0,360)
|
||||
angle = ClampAxis(angle);
|
||||
|
||||
if (angle > 180.f)
|
||||
{
|
||||
// shift to (-180,180]
|
||||
angle -= 360.f;
|
||||
}
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
||||
// Find the smallest angle between two headings (in radians).
|
||||
static float FindDeltaAngle(float a1, float a2)
|
||||
{
|
||||
// Find the difference
|
||||
float Delta = a2 - a1;
|
||||
|
||||
// If change is larger than PI
|
||||
if (Delta > PI)
|
||||
{
|
||||
// Flip to negative equivalent
|
||||
Delta = Delta - PI * 2.0f;
|
||||
}
|
||||
else if (Delta < -PI)
|
||||
{
|
||||
// Otherwise, if change is smaller than -PI
|
||||
// Flip to positive equivalent
|
||||
Delta = Delta + PI * 2.0f;
|
||||
}
|
||||
|
||||
// Return delta in [-PI,PI] range
|
||||
return Delta;
|
||||
float delta = a2 - a1;
|
||||
if (delta > PI)
|
||||
delta = delta - PI * 2.0f;
|
||||
else if (delta < -PI)
|
||||
delta = delta + PI * 2.0f;
|
||||
return delta;
|
||||
}
|
||||
|
||||
// Given a heading which may be outside the +/- PI range, 'unwind' it back into that range
|
||||
static float UnwindRadians(float a)
|
||||
{
|
||||
while (a > PI)
|
||||
{
|
||||
a -= (float)PI * 2.0f;
|
||||
}
|
||||
|
||||
while (a < -PI)
|
||||
{
|
||||
a += (float)PI * 2.0f;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
@@ -781,15 +668,9 @@ namespace Math
|
||||
static float UnwindDegrees(float a)
|
||||
{
|
||||
while (a > 180.f)
|
||||
{
|
||||
a -= 360.f;
|
||||
}
|
||||
|
||||
while (a < -180.f)
|
||||
{
|
||||
a += 360.f;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
@@ -818,13 +699,9 @@ namespace Math
|
||||
static float SmoothStep(float a, float b, float x)
|
||||
{
|
||||
if (x < a)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
if (x >= b)
|
||||
{
|
||||
return 1.0f;
|
||||
}
|
||||
const float fraction = (x - a) / (b - a);
|
||||
return fraction * fraction * (3.0f - 2.0f * fraction);
|
||||
}
|
||||
@@ -852,8 +729,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpEaseIn(const T& a, const T& b, float alpha, float exponent)
|
||||
{
|
||||
float const modifiedAlpha = Pow(alpha, exponent);
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = Pow(alpha, exponent);
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -862,8 +739,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpEaseOut(const T& a, const T& b, float alpha, float exponent)
|
||||
{
|
||||
float const modifiedAlpha = 1.f - Pow(1.f - alpha, exponent);
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = 1.f - Pow(1.f - alpha, exponent);
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -881,8 +758,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpSinIn(const T& a, const T& b, float alpha)
|
||||
{
|
||||
float const modifiedAlpha = -1.f * Cos(alpha * PI_HALF) + 1.f;
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = -1.f * Cos(alpha * PI_HALF) + 1.f;
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -891,8 +768,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpSinOut(const T& a, const T& b, float alpha)
|
||||
{
|
||||
float const modifiedAlpha = Sin(alpha * PI_HALF);
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = Sin(alpha * PI_HALF);
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -910,8 +787,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpExpoIn(const T& a, const T& b, float alpha)
|
||||
{
|
||||
float const modifiedAlpha = alpha == 0.f ? 0.f : Pow(2.f, 10.f * (alpha - 1.f));
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = alpha == 0.f ? 0.f : Pow(2.f, 10.f * (alpha - 1.f));
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -920,8 +797,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpExpoOut(const T& a, const T& b, float alpha)
|
||||
{
|
||||
float const modifiedAlpha = alpha == 1.f ? 1.f : -Pow(2.f, -10.f * alpha) + 1.f;
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = alpha == 1.f ? 1.f : -Pow(2.f, -10.f * alpha) + 1.f;
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -939,8 +816,8 @@ namespace Math
|
||||
template<class T>
|
||||
static FORCE_INLINE T InterpCircularIn(const T& a, const T& b, float alpha)
|
||||
{
|
||||
float const modifiedAlpha = -1.f * (Sqrt(1.f - alpha * alpha) - 1.f);
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = -1.f * (Sqrt(1.f - alpha * alpha) - 1.f);
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -950,8 +827,8 @@ namespace Math
|
||||
static FORCE_INLINE T InterpCircularOut(const T& a, const T& b, float alpha)
|
||||
{
|
||||
alpha -= 1.f;
|
||||
float const modifiedAlpha = Sqrt(1.f - alpha * alpha);
|
||||
return Lerp<T>(a, b, modifiedAlpha);
|
||||
const float blend = Sqrt(1.f - alpha * alpha);
|
||||
return Lerp<T>(a, b, blend);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -63,6 +63,8 @@ Vector3 Float1010102::ToVector3() const
|
||||
|
||||
FloatR11G11B10::FloatR11G11B10(float x, float y, float z)
|
||||
{
|
||||
// Reference: https://github.com/microsoft/DirectXMath
|
||||
|
||||
uint32 iValue[4];
|
||||
iValue[0] = *(uint32*)&x;
|
||||
iValue[1] = *(uint32*)&y;
|
||||
@@ -197,6 +199,8 @@ FloatR11G11B10::operator Vector3() const
|
||||
|
||||
Vector3 FloatR11G11B10::ToVector3() const
|
||||
{
|
||||
// Reference: https://github.com/microsoft/DirectXMath
|
||||
|
||||
uint32 result[4];
|
||||
uint32 mantissa;
|
||||
uint32 exponent;
|
||||
|
||||
@@ -360,8 +360,8 @@ void Quaternion::FindBetween(const Vector3& from, const Vector3& to, Quaternion&
|
||||
if (w < 1.e-6f * normFromNormTo)
|
||||
{
|
||||
result = Math::Abs(from.X) > Math::Abs(from.Z)
|
||||
? Quaternion(-from.Y, from.X, 0.f, 0.f)
|
||||
: Quaternion(0.f, -from.Z, from.Y, 0.f);
|
||||
? Quaternion(-from.Y, from.X, 0.0f, 0.0f)
|
||||
: Quaternion(0.0f, -from.Z, from.Y, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -311,39 +311,32 @@ void Vector3::Unproject(const Vector3& vector, float x, float y, float width, fl
|
||||
|
||||
void Vector3::CreateOrthonormalBasis(Vector3& xAxis, Vector3& yAxis, Vector3& zAxis)
|
||||
{
|
||||
// Project the X and Y axes onto the plane perpendicular to the Z axis.
|
||||
xAxis -= (xAxis | zAxis) / (zAxis | zAxis) * zAxis;
|
||||
yAxis -= (yAxis | zAxis) / (zAxis | zAxis) * zAxis;
|
||||
|
||||
// If the X axis was parallel to the Z axis, choose a vector which is orthogonal to the Y and Z axes.
|
||||
if (xAxis.LengthSquared() < ZeroTolerance)
|
||||
{
|
||||
xAxis = yAxis ^ zAxis;
|
||||
}
|
||||
|
||||
// If the Y axis was parallel to the Z axis, choose a vector which is orthogonal to the X and Z axes.
|
||||
if (yAxis.LengthSquared() < ZeroTolerance)
|
||||
{
|
||||
yAxis = xAxis ^ zAxis;
|
||||
}
|
||||
|
||||
// Normalize the basis vectors.
|
||||
xAxis.Normalize();
|
||||
yAxis.Normalize();
|
||||
zAxis.Normalize();
|
||||
}
|
||||
|
||||
void Vector3::FindBestAxisVectors(Vector3& Axis1, Vector3& Axis2) const
|
||||
void Vector3::FindBestAxisVectors(Vector3& firstAxis, Vector3& secondAxis) const
|
||||
{
|
||||
const float absX = Math::Abs(X);
|
||||
const float absY = Math::Abs(Y);
|
||||
const float absZ = Math::Abs(Z);
|
||||
|
||||
if (absZ > absX && absZ > absY)
|
||||
Axis1 = Vector3(1, 0, 0);
|
||||
firstAxis = Vector3(1, 0, 0);
|
||||
else
|
||||
Axis1 = Vector3(0, 0, 1);
|
||||
Axis1 = (Axis1 - *this * (Axis1 | *this)).GetNormalized();
|
||||
Axis2 = Axis1 ^ *this;
|
||||
firstAxis = Vector3(0, 0, 1);
|
||||
|
||||
firstAxis = (firstAxis - *this * (firstAxis | *this)).GetNormalized();
|
||||
secondAxis = firstAxis ^ *this;
|
||||
}
|
||||
|
||||
float Vector3::TriangleArea(const Vector3& v0, const Vector3& v1, const Vector3& v2)
|
||||
|
||||
@@ -866,9 +866,9 @@ public:
|
||||
/// <summary>
|
||||
/// Finds the best arbitrary axis vectors to represent U and V axes of a plane, by using this vector as the normal of the plane.
|
||||
/// </summary>
|
||||
/// <param name="Axis1">The reference to first axis.</param>
|
||||
/// <param name="Axis2">The reference to second axis.</param>
|
||||
void FindBestAxisVectors(Vector3& Axis1, Vector3& Axis2) const;
|
||||
/// <param name="firstAxis">The reference to first axis.</param>
|
||||
/// <param name="secondAxis">The reference to second axis.</param>
|
||||
void FindBestAxisVectors(Vector3& firstAxis, Vector3& secondAxis) const;
|
||||
|
||||
static Vector3 Round(const Vector3& v)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user