Skip to content

Scalar2 #200

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

Merged
merged 3 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ insert_final_newline=false
indent_style=space
indent_size=4

[*.cs]
# Microsoft .NET properties
csharp_new_line_before_members_in_object_initializers=false
csharp_preferred_modifier_order=public, private, protected, internal, new, abstract, virtual, sealed, override, static, readonly, extern, unsafe, volatile, async:suggestion
csharp_style_var_elsewhere=true:hint
csharp_style_var_for_built_in_types=true:hint
csharp_style_var_when_type_is_apparent=true:hint
csharp_style_expression_bodied_methods = when_on_single_line:hint
csharp_style_expression_bodied_constructors = when_on_single_line:hint
csharp_style_expression_bodied_operators = when_on_single_line:hint
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = when_on_single_line:hint
dotnet_style_predefined_type_for_locals_parameters_members=true:hint
dotnet_style_predefined_type_for_member_access=true:hint
dotnet_style_qualification_for_event=false:warning
Expand Down
2 changes: 1 addition & 1 deletion src/Maths/Silk.NET.Maths/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using System;

[assembly: CLSCompliant(true)]
[assembly: CLSCompliant(true)]
99 changes: 48 additions & 51 deletions src/Maths/Silk.NET.Maths/Box2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Globalization;
using System.Text;
using static Silk.NET.Maths.Scalar;

namespace Silk.NET.Maths
{
Expand All @@ -20,8 +21,9 @@ public Box2(Vector2<T> min, Vector2<T> max)
Max = max;
}

public Box2(T minX, T minY, T maxX, T maxY) : this (new Vector2<T>(minX, minY), new Vector2<T>(maxX, maxY))
{ }
public Box2(T minX, T minY, T maxX, T maxY) : this(new Vector2<T>(minX, minY), new Vector2<T>(maxX, maxY))
{
}

public Vector2<T> Size => Max - Min;

Expand All @@ -30,56 +32,51 @@ public Box2(Vector2<T> min, Vector2<T> max)
public Vector2<T> Center => Min + Max / Scalar<T>.Two;

public Box2<T> WithMin(Vector2<T> min) => new Box2<T>(min, Max);

public Box2<T> WithMax(Vector2<T> max) => new Box2<T>(Min, max);

public bool Contains(Vector2<T> point, bool boundaryInclusive = false)
=> boundaryInclusive
? Scalar<T>.LargerEquals(point.X, Min.X)
&& Scalar<T>.LargerEquals(point.Y, Min.Y)
&& Scalar<T>.SmallerEquals(point.X, Max.X)
&& Scalar<T>.SmallerEquals(point.Y, Min.Y)
:
Scalar<T>.Larger(point.X, Min.X)
&& Scalar<T>.Larger(point.Y, Min.Y)
&& Scalar<T>.Smaller(point.X, Max.X)
&& Scalar<T>.Smaller(point.Y, Min.Y);

public bool Contains(Box2<T> other, bool boundaryInclusive = false)
=> boundaryInclusive
? Scalar<T>.LargerEquals(other.Min.X, Min.X)
&& Scalar<T>.LargerEquals(other.Min.Y, Min.Y)
&& Scalar<T>.SmallerEquals(other.Max.X, Max.X)
&& Scalar<T>.SmallerEquals(other.Max.Y, Min.Y)
:
Scalar<T>.Larger(other.Min.X, Min.X)
&& Scalar<T>.Larger(other.Min.Y, Min.Y)
&& Scalar<T>.Smaller(other.Max.X, Max.X)
&& Scalar<T>.Smaller(other.Max.Y, Min.Y);

public T DistanceToNearestEdge(Vector2<T> point)
=> Vector2<T>.ComponentMax(Vector2<T>.Zero, Vector2<T>.ComponentMax(Min - point, point - Max)).Length;

public Box2<T> Translate(Vector2<T> distance)
=> new Box2<T>(Min + distance, Max + distance);

public Box2<T> Scale(Vector2<T> scale, Vector2<T> anchor)
=> new Box2<T>(anchor + (Min - anchor) * scale, anchor + (Max - anchor) * scale);

public Box2<T> Inflate(Vector2<T> point)
=> new Box2<T>(Vector2<T>.ComponentMin(point, Min), Vector2<T>.ComponentMin(point, Max));

public static bool operator ==(Box2<T> left, Box2<T> right)
=> left.Min == right.Min && left.Max == right.Max;

public static bool operator !=(Box2<T> left, Box2<T> right)
=> !(left == right);

public bool Equals(Box2<T> other)
=> this == other;

public override bool Equals(object? obj)
=> obj is Box2<T> box && Equals(box);
public bool Contains(Vector2<T> point, bool boundaryInclusive = false) =>
boundaryInclusive
? LargerEquals(point.X, Min.X)
&& LargerEquals(point.Y, Min.Y)
&& SmallerEquals(point.X, Max.X)
&& SmallerEquals(point.Y, Min.Y)
: Larger(point.X, Min.X)
&& Larger(point.Y, Min.Y)
&& Smaller(point.X, Max.X)
&& Smaller(point.Y, Min.Y);

public bool Contains(Box2<T> other, bool boundaryInclusive = false) =>
boundaryInclusive
? LargerEquals(other.Min.X, Min.X)
&& LargerEquals(other.Min.Y, Min.Y)
&& SmallerEquals(other.Max.X, Max.X)
&& SmallerEquals(other.Max.Y, Min.Y)
: Larger(other.Min.X, Min.X)
&& Larger(other.Min.Y, Min.Y)
&& Smaller(other.Max.X, Max.X)
&& Smaller(other.Max.Y, Min.Y);

public T DistanceToNearestEdge(Vector2<T> point) => Vector2<T>.ComponentMax
(Vector2<T>.Zero, Vector2<T>.ComponentMax(Min - point, point - Max))
.Length;

public Box2<T> Translate(Vector2<T> distance) => new Box2<T>(Min + distance, Max + distance);

public Box2<T> Scale
(Vector2<T> scale, Vector2<T> anchor) => new Box2<T>
(anchor + (Min - anchor) * scale, anchor + (Max - anchor) * scale);

public Box2<T> Inflate
(Vector2<T> point) => new Box2<T>(Vector2<T>.ComponentMin(point, Min), Vector2<T>.ComponentMin(point, Max));

public static bool operator ==(Box2<T> left, Box2<T> right) => left.Min == right.Min && left.Max == right.Max;

public static bool operator !=(Box2<T> left, Box2<T> right) => !(left == right);

public bool Equals(Box2<T> other) => this == other;

public override bool Equals(object? obj) => obj is Box2<T> box && Equals(box);

public override int GetHashCode()
{
Expand All @@ -93,7 +90,7 @@ public override int GetHashCode()
public override string ToString() => ToString("G");

public string ToString(string? format) => ToString(format, CultureInfo.CurrentCulture);

public string ToString(string? format, IFormatProvider? formatProvider)
{
var sb = new StringBuilder();
Expand Down
115 changes: 56 additions & 59 deletions src/Maths/Silk.NET.Maths/Box3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,83 @@
// of the MIT license. See the LICENSE file for details.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using static Silk.NET.Maths.Scalar;

namespace Silk.NET.Maths
{
public readonly struct Box3<T> : IEquatable<Box3<T>>, IFormattable where T : unmanaged, IFormattable
{
public readonly Vector3<T> Min;
public readonly Vector3<T> Max;

public Box3(Vector3<T> min, Vector3<T> max)
{
Min = min;
Max = max;
}

public Box3(T minX, T minY, T minZ, T maxX, T maxY, T maxZ) : this(new Vector3<T>(minX, minY, minZ), new Vector3<T>(maxX, maxY, maxZ))
{ }
public Box3(T minX, T minY, T minZ, T maxX, T maxY, T maxZ) : this
(new Vector3<T>(minX, minY, minZ), new Vector3<T>(maxX, maxY, maxZ))
{
}

public Vector3<T> Size => Max - Min;

public Vector3<T> HalfSize => (Max - Min) / Scalar<T>.Two;

public Vector3<T> Center => Min + Max / Scalar<T>.Two;

public bool Contains(Vector3<T> point, bool boundaryInclusive = false)
=> boundaryInclusive
? Scalar<T>.LargerEquals(point.X, Min.X)
&& Scalar<T>.LargerEquals(point.Y, Min.Y)
&& Scalar<T>.LargerEquals(point.Z, Min.Z)
&& Scalar<T>.SmallerEquals(point.X, Max.X)
&& Scalar<T>.SmallerEquals(point.Y, Min.Y)
&& Scalar<T>.SmallerEquals(point.Z, Min.Z)
:
Scalar<T>.Larger(point.X, Min.X)
&& Scalar<T>.Larger(point.Y, Min.Y)
&& Scalar<T>.Larger(point.Z, Min.Z)
&& Scalar<T>.Smaller(point.X, Max.X)
&& Scalar<T>.Smaller(point.Y, Min.Y)
&& Scalar<T>.Smaller(point.Z, Min.Z);

public bool Contains(Box3<T> other, bool boundaryInclusive = false)
=> boundaryInclusive
? Scalar<T>.LargerEquals(other.Min.X, Min.X)
&& Scalar<T>.LargerEquals(other.Min.Y, Min.Y)
&& Scalar<T>.LargerEquals(other.Min.Z, Min.Z)
&& Scalar<T>.SmallerEquals(other.Max.X, Max.X)
&& Scalar<T>.SmallerEquals(other.Max.Y, Min.Y)
&& Scalar<T>.SmallerEquals(other.Max.Z, Min.Z)
:
Scalar<T>.Larger(other.Min.X, Min.X)
&& Scalar<T>.Larger(other.Min.Y, Min.Y)
&& Scalar<T>.Smaller(other.Max.X, Max.X)
&& Scalar<T>.Smaller(other.Max.Y, Min.Y)
&& Scalar<T>.Smaller(other.Max.Z, Min.Z);

public T DistanceToNearestEdge(Vector3<T> point)
=> Vector3<T>.ComponentMax(Vector3<T>.Zero, Vector3<T>.ComponentMax(Min - point, point - Max)).Length;

public Box3<T> Translate(Vector3<T> distance)
=> new Box3<T>(Min + distance, Max + distance);

public Box3<T> Scale(Vector3<T> scale, Vector3<T> anchor)
=> new Box3<T>(anchor + (Min - anchor) * scale, anchor + (Max - anchor) * scale);

public Box3<T> Inflate(Vector3<T> point)
=> new Box3<T>(Vector3<T>.ComponentMin(point, Min), Vector3<T>.ComponentMin(point, Max));

public static bool operator ==(Box3<T> left, Box3<T> right)
=> left.Min == right.Min && left.Max == right.Max;

public static bool operator !=(Box3<T> left, Box3<T> right)
=> !(left == right);

public bool Equals(Box3<T> other)
=> this == other;

public override bool Equals(object? obj)
=> obj is Box3<T> box && Equals(box);
public bool Contains(Vector3<T> point, bool boundaryInclusive = false) =>
boundaryInclusive
? LargerEquals(point.X, Min.X)
&& LargerEquals(point.Y, Min.Y)
&& LargerEquals(point.Z, Min.Z)
&& SmallerEquals(point.X, Max.X)
&& SmallerEquals(point.Y, Min.Y)
&& SmallerEquals(point.Z, Min.Z)
: Larger(point.X, Min.X)
&& Larger(point.Y, Min.Y)
&& Larger(point.Z, Min.Z)
&& Smaller(point.X, Max.X)
&& Smaller(point.Y, Min.Y)
&& Smaller(point.Z, Min.Z);

public bool Contains(Box3<T> other, bool boundaryInclusive = false) =>
boundaryInclusive
? LargerEquals(other.Min.X, Min.X)
&& LargerEquals(other.Min.Y, Min.Y)
&& LargerEquals(other.Min.Z, Min.Z)
&& SmallerEquals(other.Max.X, Max.X)
&& SmallerEquals(other.Max.Y, Min.Y)
&& SmallerEquals(other.Max.Z, Min.Z)
: Larger(other.Min.X, Min.X)
&& Larger(other.Min.Y, Min.Y)
&& Smaller(other.Max.X, Max.X)
&& Smaller(other.Max.Y, Min.Y)
&& Smaller(other.Max.Z, Min.Z);

public T DistanceToNearestEdge(Vector3<T> point) => Vector3<T>.ComponentMax
(Vector3<T>.Zero, Vector3<T>.ComponentMax(Min - point, point - Max))
.Length;

public Box3<T> Translate(Vector3<T> distance) => new Box3<T>(Min + distance, Max + distance);

public Box3<T> Scale
(Vector3<T> scale, Vector3<T> anchor) => new Box3<T>
(anchor + (Min - anchor) * scale, anchor + (Max - anchor) * scale);

public Box3<T> Inflate
(Vector3<T> point) => new Box3<T>(Vector3<T>.ComponentMin(point, Min), Vector3<T>.ComponentMin(point, Max));

public static bool operator ==(Box3<T> left, Box3<T> right) => left.Min == right.Min && left.Max == right.Max;

public static bool operator !=(Box3<T> left, Box3<T> right) => !(left == right);

public bool Equals(Box3<T> other) => this == other;

public override bool Equals(object? obj) => obj is Box3<T> box && Equals(box);

public override int GetHashCode()
{
Expand All @@ -97,7 +94,7 @@ public override int GetHashCode()
public override string ToString() => ToString("G");

public string ToString(string? format) => ToString(format, CultureInfo.CurrentCulture);

public string ToString(string? format, IFormatProvider? formatProvider)
{
var sb = new StringBuilder();
Expand Down
Loading