-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
ConstraintChecker.cs
112 lines (107 loc) · 4.63 KB
/
ConstraintChecker.cs
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
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace BepuPhysics.Constraints
{
public static class ConstraintChecker
{
/// <summary>
/// Checks if a value is a finite number- neither infinite nor NaN.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>True if the value is neither infinite nor NaN, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFiniteNumber(float value)
{
return !float.IsInfinity(value) && !float.IsNaN(value);
}
/// <summary>
/// Checks if a value is a finite value greater than zero and not NaN.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>True if the value is a finite number greater than zero and not NaN, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositiveNumber(float value)
{
return IsFiniteNumber(value) && value > 0;
}
/// <summary>
/// Checks if a value is a finite value greater than or equal to zero and not NaN.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>True if the value is a finite number greater than or equal to zero and not NaN, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNonnegativeNumber(float value)
{
return IsFiniteNumber(value) && value >= 0;
}
/// <summary>
/// Checks if a value is a finite value less than zero and not NaN.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>True if the value is a finite number less than zero and not NaN, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegativeNumber(float value)
{
return IsFiniteNumber(value) && value < 0;
}
/// <summary>
/// Checks if a value is a finite value less than or equal to zero and not NaN.
/// </summary>
/// <param name="value">Value to check.</param>
/// <returns>True if the value is a finite number less than or equal to zero and not NaN, false otherwise.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNonpositiveNumber(float value)
{
return IsFiniteNumber(value) && value <= 0;
}
[Conditional("DEBUG")]
public static void AssertUnitLength(Vector3 v, string typeName, string propertyName)
{
var lengthSquared = v.LengthSquared();
if (lengthSquared > 1 + 1e-5f || lengthSquared < 1 - 1e-5f || !IsFiniteNumber(lengthSquared))
{
Debug.Fail($"{typeName}.{propertyName} must be unit length.");
}
}
[Conditional("DEBUG")]
public static void AssertUnitLength(Quaternion q, string typeName, string propertyName)
{
var lengthSquared = q.LengthSquared();
if (lengthSquared > 1 + 1e-5f || lengthSquared < 1 - 1e-5f || !IsFiniteNumber(lengthSquared))
{
Debug.Fail($"{typeName}.{propertyName} must be unit length.");
}
}
[Conditional("DEBUG")]
public static void AssertValid(in SpringSettings settings, string typeName)
{
if (!SpringSettings.Validate(settings))
{
Debug.Fail($"{typeName}.SpringSettings must have positive frequency and nonnegative damping ratio.");
}
}
[Conditional("DEBUG")]
public static void AssertValid(in MotorSettings settings, string typeName)
{
if (!MotorSettings.Validate(settings))
{
Debug.Fail($"{typeName}.MotorSettings must have nonnegative maximum force and damping.");
}
}
[Conditional("DEBUG")]
public static void AssertValid(in ServoSettings settings, string typeName)
{
if (!ServoSettings.Validate(settings))
{
Debug.Fail($"{typeName}.ServoSettings must have nonnegative maximum speed, base speed, and maximum force.");
}
}
[Conditional("DEBUG")]
public static void AssertValid(in ServoSettings servoSettings, in SpringSettings springSettings, string typeName)
{
AssertValid(servoSettings, typeName);
AssertValid(springSettings, typeName);
}
}
}