Skip to content

Commit

Permalink
moved some utility functions from vrcad
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Oct 24, 2016
1 parent 8e80f2b commit 3eb734d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions math/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public static T Clamp<T>(T f, T low, T high) where T : IComparable



// fMinMaxValue may be signed
public static float RangeClamp(float fValue, float fMinMaxValue)
{
return Clamp(fValue, -Math.Abs(fMinMaxValue), Math.Abs(fMinMaxValue));
}



//! if yshift is 0, function approaches y=1 at xZero from y=0.
//! speed (> 0) controls how fast it gets there
//! yshift pushes the whole graph upwards (so that it actually crosses y=1 at some point)
Expand Down Expand Up @@ -56,5 +64,26 @@ public static float WyvillFalloff(float fD, float fInnerRad, float fOuterRad)
} else
return 1.0f;
}




// lerps from [0,1] for x in range [deadzone,R]
public static float LinearRampT(float R, float deadzoneR, float x)
{
float sign = Math.Sign(x);
x = Math.Abs(x);
if (x < deadzoneR)
return 0.0f;
else if (x > R)
return sign * 1.0f;
else {
x = Math.Min(x, R);
float d = (x - deadzoneR) / (R - deadzoneR);
return sign * d;
}
}


}
}
25 changes: 25 additions & 0 deletions queries/Distance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace g3
{
public class Distance
{
private Distance()
{
}

public static Vector3f ClosestPointOnLine(Vector3f p0, Vector3f dir, Vector3f pt)
{
float t = (pt - p0).Dot(dir);
return p0 + t * dir;
}
public static float ClosestPointOnLineT(Vector3f p0, Vector3f dir, Vector3f pt)
{
float t = (pt - p0).Dot(dir);
return t;
}
}
}
43 changes: 43 additions & 0 deletions queries/RayIntersection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace g3
{
public class RayIntersection
{
private RayIntersection()
{

}

// basic ray-sphere intersection
public static bool Sphere(Vector3f vOrigin, Vector3f vDirection, Vector3f vCenter, float fRadius, out float fRayT)
{
fRayT = 0.0f;
Vector3f m = vOrigin - vCenter;
float b = m.Dot(vDirection);
float c = m.Dot(m) - fRadius * fRadius;

// Exit if r’s origin outside s (c > 0) and r pointing away from s (b > 0)
if (c > 0.0f && b > 0.0f)
return false;
float discr = b * b - c;

// A negative discriminant corresponds to ray missing sphere
if (discr < 0.0f)
return false;

// Ray now found to intersect sphere, compute smallest t value of intersection
fRayT = -b - (float)Math.Sqrt(discr);

// If t is negative, ray started inside sphere so clamp t to zero
// [RMS] disabling this...want to know this info
//if (fRayT < 0.0f)
// fRayT = 0.0f;

return true;
}
}
}

0 comments on commit 3eb734d

Please sign in to comment.