forked from dotnet/Silk.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cs
More file actions
176 lines (157 loc) · 7.02 KB
/
Sphere.cs
File metadata and controls
176 lines (157 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Runtime.Serialization;
namespace Silk.NET.Maths
{
/// <summary>
/// A structure representing a sphere using a <see cref="Center"/> and a <see cref="Radius"/>.
/// </summary>
[Serializable]
[DataContract]
public struct Sphere<T>
: IEquatable<Sphere<T>> where T : unmanaged, IFormattable, IEquatable<T>, IComparable<T>
{
/// <summary>
/// The center.
/// </summary>
[DataMember]
public Vector3D<T> Center;
/// <summary>
/// The radius.
/// </summary>
[DataMember]
public T Radius;
/// <summary>
/// Constructs a sphere from a <paramref name="center"/> and a <paramref name="radius"/>
/// </summary>
/// <param name="center">The center.</param>
/// <param name="radius">The radius.</param>
public Sphere(Vector3D<T> center, T radius)
{
Center = center;
Radius = radius;
}
/// <summary>
/// Constructs a sphere from components of a center and a <paramref name="radius"/>
/// </summary>
/// <param name="centerX">The X component of the center.</param>
/// <param name="centerY">The Y component of the center.</param>
/// <param name="centerZ">The Z component of the center.</param>
/// <param name="radius">The radius.</param>
public Sphere(T centerX, T centerY, T centerZ, T radius)
: this(new Vector3D<T>(centerX, centerY, centerZ), radius)
{
}
/// <summary>
/// The diameter.
/// </summary>
[IgnoreDataMember]
public T Diameter => Scalar.Multiply(Radius, Scalar<T>.Two);
/// <summary>
/// The radius squared.
/// </summary>
[IgnoreDataMember]
public T SquaredRadius => Scalar.Multiply(Radius, Radius);
/// <summary>
/// Calculates whether this sphere contains a point.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>True if this sphere contains the point; False otherwise.</returns>
/// <remarks>This does consider a point on the edge contained.</remarks>
public bool Contains(Vector3D<T> point)
{
return Scalar.LessThanOrEqual(Vector3D.DistanceSquared(point, Center), SquaredRadius);
}
/// <summary>
/// Calculates whether this sphere contains another sphere
/// </summary>
/// <param name="other">The sphere.</param>
/// <returns>True if this sphere contains the given sphere; False otherwise.</returns>
/// <remarks>This does consider a sphere that touches the edge contained.</remarks>
public bool Contains(Sphere<T> other)
{
var distanceSquared = Vector3D.DistanceSquared(Center, other.Center);
var radiusDiff = Scalar.Subtract(Radius, other.Radius);
return Scalar.LessThanOrEqual(distanceSquared, Scalar.Multiply(radiusDiff, radiusDiff));
}
/// <summary>
/// Calculates the squared distance to the nearest edge from the point.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>The distance squared.</returns>
public T GetDistanceToNearestEdgeSquared(Vector3D<T> point)
{
return Scalar.Subtract(Vector3D.DistanceSquared(Center, point), SquaredRadius);
}
/// <summary>
/// Calculates the distance to the nearest edge from the point.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>The distance.</returns>
public T GetDistanceToNearestEdge(Vector3D<T> point) => Scalar.Sqrt(GetDistanceToNearestEdgeSquared(point));
/// <summary>
/// Calculates a new sphere translated by a given distance.
/// </summary>
/// <param name="distance">The distance.</param>
/// <returns>The calculated sphere.</returns>
public Sphere<T> GetTranslated(Vector3D<T> distance)
{
return new(Center + distance, Radius);
}
/// <summary>
/// Calculates a sphere inflated to contain the given point.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>The sphere.</returns>
public Sphere<T> GetInflated(Vector3D<T> point)
{
return new(Center, Scalar.Max(Radius, Vector3D.Distance(Center, point)));
}
/// <summary>Returns a boolean indicating whether the given Sphere is equal to this Sphere instance.</summary>
/// <param name="other">The Sphere to compare this instance to.</param>
/// <returns>True if the other Sphere is equal to this instance; False otherwise.</returns>
public bool Equals(Sphere<T> other)
{
return Center.Equals(other.Center) && Radius.Equals(other.Radius);
}
/// <summary>Returns a boolean indicating whether the given Object is equal to this Sphere instance.</summary>
/// <param name="obj">The Object to compare against.</param>
/// <returns>True if the Object is equal to this Sphere; False otherwise.</returns>
public override bool Equals(object? obj)
{
return obj is Sphere<T> other && Equals(other);
}
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
return HashCode.Combine(Center, Radius);
}
/// <summary>Returns a boolean indicating whether the two given Spheres are equal.</summary>
/// <param name="value1">The first Spheres to compare.</param>
/// <param name="value2">The second Spheres to compare.</param>
/// <returns>True if the Spheres are equal; False otherwise.</returns>
public static bool operator ==(Sphere<T> value1, Sphere<T> value2)
{
return value1.Equals(value2);
}
/// <summary>Returns a boolean indicating whether the two given Spheres are not equal.</summary>
/// <param name="value1">The first Sphere to compare.</param>
/// <param name="value2">The second Sphere to compare.</param>
/// <returns>True if the Spheres are not equal; False if they are equal.</returns>
public static bool operator !=(Sphere<T> value1, Sphere<T> value2)
{
return !value1.Equals(value2);
}
/// <summary>
/// Returns this sphere casted to <typeparamref name="TOther"></typeparamref>
/// </summary>
/// <typeparam name="TOther">The type to cast to</typeparam>
/// <returns>The casted sphere</returns>
public Sphere<TOther> As<TOther>() where TOther : unmanaged, IFormattable, IEquatable<TOther>, IComparable<TOther>
{
return new(Center.As<TOther>(), Scalar.As<T, TOther>(Radius));
}
}
}