Skip to content

Commit 41aa58d

Browse files
committed
Generate vector specific methods in templates.
1 parent 25f6994 commit 41aa58d

10 files changed

Lines changed: 142 additions & 73 deletions

File tree

sources/Maths/Maths/Vector2F.cs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,6 @@ namespace Silk.NET.Maths
1616
/// <summary>A structure representing a 2D floating-point vector.</summary>
1717
internal partial struct Vector2F<T>
1818
{
19-
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
20-
public T LengthSquared => (X * X) + (Y * Y);
21-
22-
/// <summary>Gets the length of the vector.</summary>
23-
public T Length => T.Sqrt(LengthSquared);
24-
25-
/// <summary> Computes the dot product of this vector with another vector. </summary>
26-
public T Dot(Vector2F<T> other) => (X * other.X) + (Y * other.Y);
27-
28-
/// <summary> Computes the dot product of two vectors. </summary>
29-
public static T Dot(Vector2F<T> left, Vector2F<T> right) => (left.X * right.X) + (left.Y * right.Y);
30-
31-
/// <summary> Computes the cross product of this vector with another vector. </summary>
32-
public T Cross(Vector2F<T> other) => (X * other.Y) - (Y * other.X);
33-
34-
/// <summary> Computes the cross product of two vectors. </summary>
35-
public static T Cross(Vector2F<T> left, Vector2F<T> right) => (left.X * right.Y) - (left.Y * right.X);
36-
37-
/// <summary>Normalizes this vector.</summary>
38-
public Vector2F<T> Normalize()
39-
{
40-
T length = Length;
41-
return length != T.Zero ? this / length : Zero;
42-
}
43-
44-
/// <summary>Normalizes a vector.</summary>
45-
public static Vector2F<T> Normalize(Vector2F<T> vector)
46-
{
47-
T length = vector.Length;
48-
return length != T.Zero ? vector / length : Zero;
49-
}
50-
5119
/// <summary>Returns a vector with the component-wise maximum of this and another vector.</summary>
5220
public Vector2F<T> Max(Vector2F<T> other) =>
5321
new(T.Max(X, other.X), T.Max(Y, other.Y));
@@ -122,20 +90,6 @@ public static Vector2F<T> LerpClamped(Vector2F<T> a, Vector2F<T> b, Vector2F<T>
12290
a.Y + (b.Y - a.Y) * T.Clamp(t.Y, T.Zero, T.One)
12391
);
12492

125-
/// <summary>Reflects a vector over a normal vector.</summary>
126-
public Vector2F<T> Reflect(Vector2F<T> normal)
127-
{
128-
T dot = Dot(normal);
129-
return this - (normal * (dot + dot));
130-
}
131-
132-
/// <summary>Reflects a vector over a normal vector.</summary>
133-
public static Vector2F<T> Reflect(Vector2F<T> vector, Vector2F<T> normal)
134-
{
135-
T dot = Dot(vector, normal);
136-
return vector - (normal * (dot + dot));
137-
}
138-
13993
/// <summary>Returns a vector where each component is the sign of the original vector's component.</summary>
14094
public Vector2F<T> Sign() => new(T.CreateChecked(T.Sign(X)), T.CreateChecked(T.Sign(Y)));
14195

@@ -358,4 +312,11 @@ public static Vector2F<T> ScaleB(Vector2F<T> x, Vector2I<int> n) =>
358312
public static Vector2F<T> ScaleB(Vector2F<T> x, int n) =>
359313
new(T.ScaleB(x.X, n), T.ScaleB(x.Y, n));
360314
}
315+
316+
static partial class Vector2F
317+
{
318+
/// <summary> Computes the cross product of two vectors.</summary>
319+
public static T Cross<T>(this Vector2F<T> left, Vector2F<T> right)
320+
where T : IFloatingPointIeee754<T> => (left.X * right.Y) - (left.Y * right.X);
321+
}
361322
}

sources/Maths/Maths/Vector2F.gen.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public Vector2F(ReadOnlySpan<T> values)
5151
/// <summary>Gets the vector (0, 1).</summary>
5252
public static Vector2F<T> UnitY => new(Scalar<T>.Zero, Scalar<T>.One);
5353

54+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
55+
public T LengthSquared => Vector2F.Dot(this, this);
56+
5457
/// <inheritdoc/>
5558
T IReadOnlyList<T>.this[int index] => this[index];
5659

@@ -342,6 +345,32 @@ static bool IParsable<Vector2F<T>>.TryParse([NotNullWhen(true)] string? s, IForm
342345

343346
static partial class Vector2F
344347
{
348+
/// <summary>Computes the dot product of two vectors.</summary>
349+
public static T Dot<T>(this Vector2F<T> left, Vector2F<T> right)
350+
where T : IFloatingPointIeee754<T> =>
351+
left.X * right.X + left.Y * right.Y;
352+
353+
/// <summary>Reflects a vector over a normal vector.</summary>
354+
public static Vector2F<T> Reflect<T>(Vector2F<T> vector, Vector2F<T> normal)
355+
where T : IFloatingPointIeee754<T>
356+
{
357+
T dot = vector.Dot(normal);
358+
return vector - (normal * (dot + dot));
359+
}
360+
361+
/// <summary>Computes the length of the vector.</summary>
362+
public static T GetLength<T>(this Vector2F<T> vector)
363+
where T : IFloatingPointIeee754<T> =>
364+
T.Sqrt(vector.LengthSquared);
365+
366+
/// <summary>Normalizes a vector.</summary>
367+
public static Vector2F<T> Normalize<T>(this Vector2F<T> vector)
368+
where T : IFloatingPointIeee754<T>
369+
{
370+
T length = vector.GetLength();
371+
return length != T.Zero ? vector / length : Vector2F<T>.Zero;
372+
}
373+
345374
public static Vector2F<TSelf> Log<TSelf>(this Vector2F<TSelf> x)
346375
where TSelf : IFloatingPointIeee754<TSelf>, ILogarithmicFunctions<TSelf> =>
347376
new(TSelf.Log(x.X), TSelf.Log(x.Y));

sources/Maths/Maths/Vector2I.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ namespace Silk.NET.Maths
1414
/// <summary>A structure representing a 2D integer vector.</summary>
1515
internal partial struct Vector2I<T>
1616
{
17-
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
18-
public T LengthSquared => (X * X) + (Y * Y);
19-
20-
/// <summary> Computes the dot product of this vector with another vector. </summary>
21-
public T Dot(Vector2I<T> other) => (X * other.X) + (Y * other.Y);
22-
23-
/// <summary> Computes the dot product of two vectors. </summary>
24-
public static T Dot(Vector2I<T> left, Vector2I<T> right) => (left.X * right.X) + (left.Y * right.Y);
25-
2617
/// <summary> Computes the cross product of this vector with another vector. </summary>
2718
public T Cross(Vector2I<T> other) => (X * other.Y) - (Y * other.X);
2819

sources/Maths/Maths/Vector2I.gen.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public Vector2I(ReadOnlySpan<T> values)
5454
/// <summary>Gets a vector with all bits set for each component.</summary>
5555
public static Vector2I<T> AllBitsSet => new Vector2I<T>(T.AllBitsSet, T.AllBitsSet);
5656

57+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
58+
public T LengthSquared => Vector2I.Dot(this, this);
59+
5760
/// <inheritdoc/>
5861
T IReadOnlyList<T>.this[int index] => this[index];
5962

@@ -374,6 +377,19 @@ static bool IParsable<Vector2I<T>>.TryParse([NotNullWhen(true)] string? s, IForm
374377

375378
static partial class Vector2I
376379
{
380+
/// <summary>Computes the dot product of two vectors.</summary>
381+
public static T Dot<T>(this Vector2I<T> left, Vector2I<T> right)
382+
where T : IBinaryInteger<T> =>
383+
left.X * right.X + left.Y * right.Y;
384+
385+
/// <summary>Reflects a vector over a normal vector.</summary>
386+
public static Vector2I<T> Reflect<T>(Vector2I<T> vector, Vector2I<T> normal)
387+
where T : IBinaryInteger<T>
388+
{
389+
T dot = vector.Dot(normal);
390+
return vector - (normal * (dot + dot));
391+
}
392+
377393
public static Vector2I<TSelf> Log<TSelf>(this Vector2I<TSelf> x)
378394
where TSelf : IBinaryInteger<TSelf>, ILogarithmicFunctions<TSelf> =>
379395
new(TSelf.Log(x.X), TSelf.Log(x.Y));

sources/Maths/Maths/Vector3F.gen.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public Vector3F(ReadOnlySpan<T> values)
6161
/// <summary>Gets the vector (0, 0, 1).</summary>
6262
public static Vector3F<T> UnitZ => new(Scalar<T>.Zero, Scalar<T>.Zero, Scalar<T>.One);
6363

64+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
65+
public T LengthSquared => Vector3F.Dot(this, this);
66+
6467
/// <inheritdoc/>
6568
T IReadOnlyList<T>.this[int index] => this[index];
6669

@@ -379,6 +382,32 @@ static bool IParsable<Vector3F<T>>.TryParse([NotNullWhen(true)] string? s, IForm
379382

380383
static partial class Vector3F
381384
{
385+
/// <summary>Computes the dot product of two vectors.</summary>
386+
public static T Dot<T>(this Vector3F<T> left, Vector3F<T> right)
387+
where T : IFloatingPointIeee754<T> =>
388+
left.X * right.X + left.Y * right.Y + left.Z * right.Z;
389+
390+
/// <summary>Reflects a vector over a normal vector.</summary>
391+
public static Vector3F<T> Reflect<T>(Vector3F<T> vector, Vector3F<T> normal)
392+
where T : IFloatingPointIeee754<T>
393+
{
394+
T dot = vector.Dot(normal);
395+
return vector - (normal * (dot + dot));
396+
}
397+
398+
/// <summary>Computes the length of the vector.</summary>
399+
public static T GetLength<T>(this Vector3F<T> vector)
400+
where T : IFloatingPointIeee754<T> =>
401+
T.Sqrt(vector.LengthSquared);
402+
403+
/// <summary>Normalizes a vector.</summary>
404+
public static Vector3F<T> Normalize<T>(this Vector3F<T> vector)
405+
where T : IFloatingPointIeee754<T>
406+
{
407+
T length = vector.GetLength();
408+
return length != T.Zero ? vector / length : Vector3F<T>.Zero;
409+
}
410+
382411
public static Vector3F<TSelf> Log<TSelf>(this Vector3F<TSelf> x)
383412
where TSelf : IFloatingPointIeee754<TSelf>, ILogarithmicFunctions<TSelf> =>
384413
new(TSelf.Log(x.X), TSelf.Log(x.Y), TSelf.Log(x.Z));

sources/Maths/Maths/Vector3I.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ namespace Silk.NET.Maths
1414
/// <summary>A structure representing a 3D integer vector.</summary>
1515
internal partial struct Vector3I<T>
1616
{
17-
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
18-
public T LengthSquared => (X * X) + (Y * Y) + (Z * Z);
19-
20-
/// <summary> Computes the dot product of this vector with another vector. </summary>
21-
public T Dot(Vector3I<T> other) => (X * other.X) + (Y * other.Y) + (Z * other.Z);
22-
23-
/// <summary> Computes the dot product of two vectors. </summary>
24-
public static T Dot(Vector3I<T> left, Vector3I<T> right) => (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z);
25-
2617
/// <summary> Computes the cross product of this vector with another vector. </summary>
2718
public Vector3I<T> Cross(Vector3I<T> other) =>
2819
new Vector3I<T>(

sources/Maths/Maths/Vector3I.gen.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public Vector3I(ReadOnlySpan<T> values)
6464
/// <summary>Gets a vector with all bits set for each component.</summary>
6565
public static Vector3I<T> AllBitsSet => new Vector3I<T>(T.AllBitsSet, T.AllBitsSet, T.AllBitsSet);
6666

67+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
68+
public T LengthSquared => Vector3I.Dot(this, this);
69+
6770
/// <inheritdoc/>
6871
T IReadOnlyList<T>.this[int index] => this[index];
6972

@@ -411,6 +414,19 @@ static bool IParsable<Vector3I<T>>.TryParse([NotNullWhen(true)] string? s, IForm
411414

412415
static partial class Vector3I
413416
{
417+
/// <summary>Computes the dot product of two vectors.</summary>
418+
public static T Dot<T>(this Vector3I<T> left, Vector3I<T> right)
419+
where T : IBinaryInteger<T> =>
420+
left.X * right.X + left.Y * right.Y + left.Z * right.Z;
421+
422+
/// <summary>Reflects a vector over a normal vector.</summary>
423+
public static Vector3I<T> Reflect<T>(Vector3I<T> vector, Vector3I<T> normal)
424+
where T : IBinaryInteger<T>
425+
{
426+
T dot = vector.Dot(normal);
427+
return vector - (normal * (dot + dot));
428+
}
429+
414430
public static Vector3I<TSelf> Log<TSelf>(this Vector3I<TSelf> x)
415431
where TSelf : IBinaryInteger<TSelf>, ILogarithmicFunctions<TSelf> =>
416432
new(TSelf.Log(x.X), TSelf.Log(x.Y), TSelf.Log(x.Z));

sources/Maths/Maths/Vector4F.gen.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public Vector4F(ReadOnlySpan<T> values)
7171
/// <summary>Gets the vector (0, 0, 0, 1).</summary>
7272
public static Vector4F<T> UnitW => new(Scalar<T>.Zero, Scalar<T>.Zero, Scalar<T>.Zero, Scalar<T>.One);
7373

74+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
75+
public T LengthSquared => Vector4F.Dot(this, this);
76+
7477
/// <inheritdoc/>
7578
T IReadOnlyList<T>.this[int index] => this[index];
7679

@@ -416,6 +419,32 @@ static bool IParsable<Vector4F<T>>.TryParse([NotNullWhen(true)] string? s, IForm
416419

417420
static partial class Vector4F
418421
{
422+
/// <summary>Computes the dot product of two vectors.</summary>
423+
public static T Dot<T>(this Vector4F<T> left, Vector4F<T> right)
424+
where T : IFloatingPointIeee754<T> =>
425+
left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
426+
427+
/// <summary>Reflects a vector over a normal vector.</summary>
428+
public static Vector4F<T> Reflect<T>(Vector4F<T> vector, Vector4F<T> normal)
429+
where T : IFloatingPointIeee754<T>
430+
{
431+
T dot = vector.Dot(normal);
432+
return vector - (normal * (dot + dot));
433+
}
434+
435+
/// <summary>Computes the length of the vector.</summary>
436+
public static T GetLength<T>(this Vector4F<T> vector)
437+
where T : IFloatingPointIeee754<T> =>
438+
T.Sqrt(vector.LengthSquared);
439+
440+
/// <summary>Normalizes a vector.</summary>
441+
public static Vector4F<T> Normalize<T>(this Vector4F<T> vector)
442+
where T : IFloatingPointIeee754<T>
443+
{
444+
T length = vector.GetLength();
445+
return length != T.Zero ? vector / length : Vector4F<T>.Zero;
446+
}
447+
419448
public static Vector4F<TSelf> Log<TSelf>(this Vector4F<TSelf> x)
420449
where TSelf : IFloatingPointIeee754<TSelf>, ILogarithmicFunctions<TSelf> =>
421450
new(TSelf.Log(x.X), TSelf.Log(x.Y), TSelf.Log(x.Z), TSelf.Log(x.W));

sources/Maths/Maths/Vector4I.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ namespace Silk.NET.Maths
1313
/// <summary>A structure representing a 4D integer vector.</summary>
1414
internal partial struct Vector4I<T>
1515
{
16-
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
17-
public T LengthSquared => (X * X) + (Y * Y) + (Z * Z) + (W * W);
18-
19-
/// <summary> Computes the dot product of this vector with another vector. </summary>
20-
public T Dot(Vector4I<T> other) => (X * other.X) + (Y * other.Y) + (Z * other.Z) + (W * other.W);
21-
22-
/// <summary> Computes the dot product of two vectors. </summary>
23-
public static T Dot(Vector4I<T> left, Vector4I<T> right) => (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W);
24-
2516
/// <summary>Returns a vector with the component-wise maximum of this and another vector.</summary>
2617
public Vector4I<T> Max(Vector4I<T> other) =>
2718
new Vector4I<T>(T.Max(X, other.X), T.Max(Y, other.Y), T.Max(Z, other.Z), T.Max(W, other.W));

sources/Maths/Maths/Vector4I.gen.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public Vector4I(ReadOnlySpan<T> values)
7474
/// <summary>Gets a vector with all bits set for each component.</summary>
7575
public static Vector4I<T> AllBitsSet => new Vector4I<T>(T.AllBitsSet, T.AllBitsSet, T.AllBitsSet, T.AllBitsSet);
7676

77+
/// <summary>Gets the squared length of the vector (dot product with itself).</summary>
78+
public T LengthSquared => Vector4I.Dot(this, this);
79+
7780
/// <inheritdoc/>
7881
T IReadOnlyList<T>.this[int index] => this[index];
7982

@@ -448,6 +451,19 @@ static bool IParsable<Vector4I<T>>.TryParse([NotNullWhen(true)] string? s, IForm
448451

449452
static partial class Vector4I
450453
{
454+
/// <summary>Computes the dot product of two vectors.</summary>
455+
public static T Dot<T>(this Vector4I<T> left, Vector4I<T> right)
456+
where T : IBinaryInteger<T> =>
457+
left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
458+
459+
/// <summary>Reflects a vector over a normal vector.</summary>
460+
public static Vector4I<T> Reflect<T>(Vector4I<T> vector, Vector4I<T> normal)
461+
where T : IBinaryInteger<T>
462+
{
463+
T dot = vector.Dot(normal);
464+
return vector - (normal * (dot + dot));
465+
}
466+
451467
public static Vector4I<TSelf> Log<TSelf>(this Vector4I<TSelf> x)
452468
where TSelf : IBinaryInteger<TSelf>, ILogarithmicFunctions<TSelf> =>
453469
new(TSelf.Log(x.X), TSelf.Log(x.Y), TSelf.Log(x.Z), TSelf.Log(x.W));

0 commit comments

Comments
 (0)