Skip to content

Commit 8097fe8

Browse files
committed
RMF method
1 parent da7bc4d commit 8097fe8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Runtime/Mathfs.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,25 @@ public static Vector3 ClampMagnitude( Vector3 v, float min, float max ) {
11031103
/// <inheritdoc cref="DistanceSquared(Vector2,Vector2)"/>
11041104
[MethodImpl( INLINE )] public static float DistanceSquared( Vector4 a, Vector4 b ) => ( a.x - b.x ).Square() + ( a.y - b.y ).Square() + ( a.z - b.z ).Square() + ( a.w - b.w ).Square();
11051105

1106+
/// <summary>Calculates a rotation minimizing normal direction, given start and end conditions. This is usually used when evaluating rotation minimizing frames on curves.</summary>
1107+
/// <param name="posA">The start position</param>
1108+
/// <param name="tangentA">The start tangent direction</param>
1109+
/// <param name="normalA">The start normal direction</param>
1110+
/// <param name="posB">The end position</param>
1111+
/// <param name="tangentB">The end tangent direction</param>
1112+
public static Vector3 GetRotationMinimizingNormal( Vector3 posA, Vector3 tangentA, Vector3 normalA, Vector3 posB, Vector3 tangentB ) {
1113+
// source: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/12/Computation-of-rotation-minimizing-frames.pdf
1114+
Vector3 v1 = posB - posA;
1115+
float v1_dot_v1_half = Vector3.Dot( v1, v1 ) / 2;
1116+
float r1 = Vector3.Dot( v1, normalA ) / v1_dot_v1_half;
1117+
float r2 = Vector3.Dot( v1, tangentA ) / v1_dot_v1_half;
1118+
Vector3 nL = normalA - r1 * v1;
1119+
Vector3 tL = tangentA - r2 * v1;
1120+
Vector3 v2 = tangentB - tL;
1121+
float r3 = Vector3.Dot( v2, nL ) / Vector3.Dot( v2, v2 );
1122+
return ( nL - 2 * r3 * v2 ).normalized;
1123+
}
1124+
11061125
#endregion
11071126

11081127
#region Angles & Rotation

0 commit comments

Comments
 (0)