Skip to content

Commit

Permalink
Merge pull request stride3d#1582 from skohanowski/Deconstructors
Browse files Browse the repository at this point in the history
Added deconstructors to most of the structs in Stride.Mathematics
  • Loading branch information
manio143 authored Jan 9, 2023
2 parents 2c5053c + e3e2e6b commit 8df2f5d
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 22 deletions.
13 changes: 13 additions & 0 deletions sources/core/Stride.Core.Mathematics/Color3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,19 @@ public override bool Equals(object value)

return Equals((Color3)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="r">The R component</param>
/// <param name="g">The G component</param>
/// <param name="b">The B component</param>
public void Deconstruct(out float r, out float g, out float b)
{
r = R;
g = G;
b = B;
}

#if SlimDX1xInterop
/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions sources/core/Stride.Core.Mathematics/Color4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,22 @@ public override bool Equals(object value)
return false;

return Equals((Color4)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="r">The R component</param>
/// <param name="g">The G component</param>
/// <param name="b">The B component</param>
/// <param name="a">The A component</param>
public void Deconstruct(out float r, out float g, out float b, out float a)
{
r = R;
g = G;
b = B;
a = A;
}

}
}
16 changes: 16 additions & 0 deletions sources/core/Stride.Core.Mathematics/ColorBGRA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,22 @@ private static byte ToByte(float component)
{
var value = (int)(component * 255.0f);
return (byte)(value < 0 ? 0 : value > 255 ? 255 : value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="r">The R component</param>
/// <param name="g">The G component</param>
/// <param name="b">The B component</param>
/// <param name="a">The A component</param>
public void Deconstruct(out byte r, out byte g, out byte b, out byte a)
{
r = R;
g = G;
b = B;
a = A;
}

}
}
16 changes: 16 additions & 0 deletions sources/core/Stride.Core.Mathematics/ColorHSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ public string ToString(string format, IFormatProvider formatProvider)
S.ToString(format, formatProvider),
V.ToString(format, formatProvider),
A.ToString(format, formatProvider));
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="h">The H component</param>
/// <param name="s">The S component</param>
/// <param name="v">The V component</param>
/// <param name="a">The A component</param>
public void Deconstruct(out float h, out float s, out float v, out float a)
{
h = H;
s = S;
v = V;
a = A;
}

}
}
11 changes: 11 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,17 @@ public override bool Equals(object value)

return Equals((Double2)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
public void Deconstruct(out double x, out double y)
{
x = X;
y = Y;
}

#if WPFInterop
/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,19 @@ public override bool Equals(object value)

return Equals((Double3)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
public void Deconstruct(out double x, out double y, out double z)
{
x = X;
y = Y;
z = Z;
}


#if WPFInterop
Expand Down
15 changes: 15 additions & 0 deletions sources/core/Stride.Core.Mathematics/Double4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,21 @@ public override bool Equals(object value)

return Equals((Double4)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
/// <param name="w">The W component</param>
public void Deconstruct(out double x, out double y, out double z, out double w)
{
x = X;
y = Y;
z = Z;
w = W;
}

#if WPFInterop
/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions sources/core/Stride.Core.Mathematics/Half2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,16 @@ public static explicit operator Vector2(Half2 value)
{
return new Vector2(value.X, value.Y);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
public void Deconstruct(out Half x, out Half y)
{
x = X;
y = Y;
}
}
}
13 changes: 13 additions & 0 deletions sources/core/Stride.Core.Mathematics/Half3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,18 @@ public override bool Equals(object obj)
}
return this.Equals((Half3)obj);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
public void Deconstruct(out Half x, out Half y, out Half z)
{
x = X;
y = Y;
z = Z;
}
}
}
15 changes: 15 additions & 0 deletions sources/core/Stride.Core.Mathematics/Half4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ public override bool Equals(object obj)
return false;
}
return this.Equals((Half4)obj);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
/// <param name="w">The W component</param>
public void Deconstruct(out Half x, out Half y, out Half z, out Half w)
{
x = X;
y = Y;
z = Z;
w = W;
}
}
}
26 changes: 19 additions & 7 deletions sources/core/Stride.Core.Mathematics/Int2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ public struct Int2 : IEquatable<Int2>, IFormattable
/// <summary>
/// A <see cref="Stride.Core.Mathematics.Int2"/> with all of its components set to zero.
/// </summary>
public static readonly Int2 Zero = new Int2();
public static readonly Int2 Zero = new();

/// <summary>
/// The X unit <see cref="Stride.Core.Mathematics.Int2"/> (1, 0, 0).
/// </summary>
public static readonly Int2 UnitX = new Int2(1, 0);
public static readonly Int2 UnitX = new(1, 0);

/// <summary>
/// The Y unit <see cref="Stride.Core.Mathematics.Int2"/> (0, 1, 0).
/// </summary>
public static readonly Int2 UnitY = new Int2(0, 1);
public static readonly Int2 UnitY = new(0, 1);

/// <summary>
/// A <see cref="Stride.Core.Mathematics.Int2"/> with all of its components set to one.
/// </summary>
public static readonly Int2 One = new Int2(1, 1);
public static readonly Int2 One = new(1, 1);

/// <summary>
/// The X component of the vector.
Expand Down Expand Up @@ -435,7 +435,7 @@ public static Int2 Lerp(Int2 start, Int2 end, float amount)
public static void SmoothStep(ref Int2 start, ref Int2 end, float amount, out Int2 result)
{
amount = (amount > 1) ? 1 : ((amount < 0) ? 0 : amount);
amount = (amount * amount) * (3 - (2 * amount));
amount = amount * amount * (3 - (2 * amount));

result.X = (int)(start.X + ((end.X - start.X) * amount));
result.Y = (int)(start.Y + ((end.Y - start.Y) * amount));
Expand Down Expand Up @@ -696,8 +696,8 @@ public override int GetHashCode()
/// </returns>
public bool Equals(Int2 other)
{
return (MathF.Abs(other.X - X) < MathUtil.ZeroTolerance &&
MathF.Abs(other.Y - Y) < MathUtil.ZeroTolerance);
return MathF.Abs(other.X - X) < MathUtil.ZeroTolerance &&
MathF.Abs(other.Y - Y) < MathUtil.ZeroTolerance;
}

/// <summary>
Expand All @@ -717,6 +717,18 @@ public override bool Equals(object value)

return Equals((Int2)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
public void Deconstruct(out int x, out int y)
{
x = X;
y = Y;
}

#if WPFInterop
/// <summary>
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Int2"/> to <see cref="System.Windows.Media.Media3D.Int3D"/>.
Expand Down
14 changes: 14 additions & 0 deletions sources/core/Stride.Core.Mathematics/Int3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,20 @@ public override bool Equals(object value)

return Equals((Int3)value);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
public void Deconstruct(out int x, out int y, out int z)
{
x = X;
y = Y;
z = Z;
}

#if WPFInterop
/// <summary>
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Int3"/> to <see cref="System.Windows.Media.Media3D.Int3D"/>.
Expand Down
37 changes: 22 additions & 15 deletions sources/core/Stride.Core.Mathematics/Int4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,14 @@ public Int4(int[] values)
/// <exception cref = "System.ArgumentOutOfRangeException">Thrown when the <paramref name = "index" /> is out of the range [0, 3].</exception>
public int this[int index]
{
get
get => index switch
{
switch (index)
{
case 0:
return X;
case 1:
return Y;
case 2:
return Z;
case 3:
return W;
}

throw new ArgumentOutOfRangeException("index", "Indices for Int4 run from 0 to 3, inclusive.");
}
0 => X,
1 => Y,
2 => Z,
3 => W,
_ => throw new ArgumentOutOfRangeException("index", "Indices for Int4 run from 0 to 3, inclusive."),
};

set
{
Expand Down Expand Up @@ -696,5 +688,20 @@ public static implicit operator int[](Int4 input)
{
return input.ToArray();
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
/// <param name="z">The Z component</param>
/// <param name="w">The W component</param>
public void Deconstruct(out int x, out int y, out int z, out int w)
{
x = X;
y = Y;
z = Z;
w = W;
}
}
}
12 changes: 12 additions & 0 deletions sources/core/Stride.Core.Mathematics/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,17 @@ public static implicit operator Vector2(Point value)
{
return new Vector2(value.X, value.Y);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="x">The X component</param>
/// <param name="y">The Y component</param>
public void Deconstruct(out int x, out int y)
{
x = X;
y = Y;
}

}
}
11 changes: 11 additions & 0 deletions sources/core/Stride.Core.Mathematics/Size2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,16 @@ public override string ToString()
{
return string.Format("({0},{1})", Width, Height);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="width">The Width component</param>
/// <param name="height">The Height component</param>
public void Deconstruct(out int width, out int height)
{
width = Width;
height = Height;
}
}
}
11 changes: 11 additions & 0 deletions sources/core/Stride.Core.Mathematics/Size2F.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public override int GetHashCode()
public override string ToString()
{
return string.Format("({0},{1})", Width, Height);
}

/// <summary>
/// Deconstructs the vector's components into named variables.
/// </summary>
/// <param name="width">The Width component</param>
/// <param name="height">The Height component</param>
public void Deconstruct(out float width, out float height)
{
width = Width;
height = Height;
}
}
}
Loading

0 comments on commit 8df2f5d

Please sign in to comment.