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
[API Proposal]: Add Indexer to Vectors #59924
Comments
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsBackground and motivationRunning the same operation in-terms of different vector components is a fairly common operation. In order to make this easier, having indexers into the Example: API Proposalnamespace System.Collections.Generic
{
public struct Vector3 : System.IEquatable<System.Numerics.Vector3>, System.IFormattable
{
/// 0 is X, 1 is Y, 2 is Z
public float this[int i] { get; }
}
} API UsageVector3 v3 = Vector3.UnitY;
v3[0] == 0;
v3[1] == 1;
v3[2] == 0; RisksNone I don't think
|
Here's an example of when this would be useful public struct AABB
{
public AABB(Vector3 maximum, Vector3 minimum)
{
Maximum = maximum;
Minimum = minimum;
}
public bool IsHit(Ray ray, float maxClip, float minClip)
{
// Currently have to copy all my Vector3s to float[]s
float[] min = { Minimum.X, Minimum.Y, Minimum.Z };
float[] max = { Maximum.X, Maximum.Y, Maximum.Z };
float[] origin = { ray.Origin.X, ray.Origin.Y, ray.Origin.Z };
float[] dir = { ray.Direction.X, ray.Direction.Y, ray.Direction.Z };
for (int a = 0; a < 3; a++)
{
float invD = 1f / dir[a];
float t0 = (min[a] - origin[a]) * invD;
float t1 = (max[a] - origin[a]) * invD;
if (invD < 0.0f)
{
float swap = t0;
t0 = t1;
t1 = swap;
}
minClip = t0 > minClip ? t0 : minClip;
maxClip = t1 < maxClip ? t1 : maxClip;
if (maxClip <= minClip) return false;
}
return true;
}
public Vector3 Maximum { get; }
public Vector3 Minimum { get; }
}
``` |
@Avid29 You can use something like this today: public static class Extensions
{
public static ref float ElementAt(this in Vector3 v, nint idx){
return ref Unsafe.Add(ref Unsafe.As<Vector3, float>(ref Unsafe.AsRef(v)), idx);
}
} |
@hypeartist I thought about it a little more, and now I do use that. However, I still believe this is a reasonable addition to the API. It's a sensible interpretation of the indexer on a Vector and Unity's Vectors already have this behavior. |
@Avid29, I'm fine with this suggestion. It can also be optimized for constant inputs and brings it more inline with the other vector types. However, could you please update the proposal to follow the form of: public partial struct Vector3
{
public float this[int i] { get; set; }
} and to also list the other Once that's done, I can mark this ready for review (and if you get to this before tomorrow, it can likely goto API review then, assuming we have nothing else on the docket). |
That all sounds good, I'll add that Matrix indexers are a duplicate to #21705 |
If possible, I'd recommend covering them here. It's likely better to simply have |
I've updated the original post covering the types I listed. |
Looks good as proposed. We went ahead and added read-only indexers on the other Vector types namespace System.Numerics
{
public partial struct Vector2
{
public float this[int index] { get; set; }
}
public partial struct Vector3
{
public float this[int index] { get; set; }
}
public partial struct Vector4
{
public float this[int index] { get; set; }
}
public partial struct Quaternion
{
public float this[int index] { get; set; }
}
public partial struct Matrix3x2
{
public float this[int row, int column] { get; set; }
}
public partial struct Matrix4x4
{
public float this[int row, int column] { get; set; }
}
}
namespace System.Runtime.Intrinsics
{
public partial struct Vector64<T>
{
public T this[int index] { get; }
}
public partial struct Vector128<T>
{
public T this[int index] { get; }
}
public partial struct Vector256<T>
{
public T this[int index] { get; }
}
} |
Background and motivation
Running the same operation in-terms of different vector components is a fairly common operation.
In order to make this easier, having indexers into the
System.Numerics
Vectors that returns the value at a position in the vector would make a ton of sense.Example:
v.X == v[0]
,v.Y == v[1]
, etc wouldAPI Proposal
API Usage
Risks
None I don't think
The text was updated successfully, but these errors were encountered: