Skip to content

Commit

Permalink
Fix #1769 and introduce an optional argument to specify a different r…
Browse files Browse the repository at this point in the history
…ounding strategy if needed.
  • Loading branch information
Kryptos-FR committed Oct 18, 2023
1 parent 51f60ac commit db8aeec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
51 changes: 29 additions & 22 deletions sources/core/Stride.Core.Mathematics/Int3.cs
Expand Up @@ -143,16 +143,6 @@ public Int3(int[] values)
Z = values[2];
}

/// <summary>
/// Creates a new instance of the <see cref="Int3"/> struct by rounding the X, Y, and Z components of a <see cref="Vector3"/> struct to the nearest integer.
/// </summary>
public Int3 RoundToInt3(Vector3 value)
{
X = (int)Math.Round(value.X);
Y = (int)Math.Round(value.Y);
Z = (int)Math.Round(value.Z);
}

/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
Expand Down Expand Up @@ -200,12 +190,12 @@ public int Length()
}

/// <summary>
/// Calculates the unrounded length of the vector.
/// Calculates the untruncated length of the vector.
/// </summary>
/// <returns>The length of the vector unrounded.</returns>
/// <returns>The length of the vector untruncated.</returns>
public float LengthUntruncated()
{
return Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
return (float)Math.Sqrt(Length());
}

/// <summary>
Expand Down Expand Up @@ -551,7 +541,32 @@ public static Int3 Min(Int3 left, Int3 right)
Min(ref left, ref right, out result);
return result;
}


/// <summary>
/// Returns a vector containing the rounded values of the specified vector.
/// </summary>
/// <param name="value">The source vector.</param>
/// <param name="result">When the method completes, contains an new vector composed of the rounded values of the specified vector.</param>
/// <param name="rounding">The rounding strategy to use.</param>
public static void Round(ref Vector3 value, out Int3 result, MidpointRounding rounding = default)
{
result.X = (int)Math.Round(value.X, rounding);
result.Y = (int)Math.Round(value.Y, rounding);
result.Z = (int)Math.Round(value.Z, rounding);
}

/// <summary>
/// Returns a vector containing the rounded values of the specified vector.
/// </summary>
/// <param name="value">The source vector.</param>
/// <param name="rounding">The rounding strategy to use.</param>
/// <returns>A vector containing the rounded values of the source vector.</returns>
public static Int3 Round(Vector3 value, MidpointRounding rounding = default)
{
Round(ref value, out var result, rounding);
return result;
}

/// <summary>
/// Adds two vectors.
/// </summary>
Expand Down Expand Up @@ -790,14 +805,6 @@ public void Deconstruct(out int x, out int y, out int z)
y = Y;
z = Z;
}

/// <summary>
/// Preforms an implicit conversion from <see cref="Stride.Core.Mathematics.Int3"/> to <see cref="Stride.Core.Mathematics.Vector3"/>.
/// </summary>
public static implicit operator Vector3(Int3 value)
{
return new Vector3(value);
}

#if WPFInterop
/// <summary>
Expand Down
10 changes: 0 additions & 10 deletions sources/core/Stride.Core.Mathematics/Vector3.cs
Expand Up @@ -143,16 +143,6 @@ public Vector3(float[] values)
Z = values[2];
}

/// <summary>
/// Initializes a new instance of the <see cref="Stride.Core.Mathematics.Vector3"/> struct given a <see cref="Stride.Core.Mathematics.Int3"/>.
/// </summary>
public Vector3(Int3 value)
{
X = value.X;
Y = value.Y;
Z = value.Z;
}

/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>
Expand Down

0 comments on commit db8aeec

Please sign in to comment.