Skip to content

Commit

Permalink
added ray-inf-cylinder intersection, not really tested, possibly buggy.
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Nov 8, 2016
1 parent a97732e commit 39ab19e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/FileSystemUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Linq;
using System.IO;

namespace g3
{
public static class FileSystemUtils
{
static public bool CanAccessFolder(string sPath)
{
try {
Directory.GetDirectories(sPath);
} catch (Exception) {
return false;
}
return true;
}


static public bool IsValidFilenameCharacter(char c)
{
return Path.GetInvalidFileNameChars().Contains(c) == false;
}
static public bool IsValidFilenameString(string s)
{
for (int i = 0; i < s.Length; ++i)
if (Path.GetInvalidFileNameChars().Contains(s[i]))
return false;
return true;
}

}
}
13 changes: 13 additions & 0 deletions math/Vector3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ public float Dot(Vector3f v2)
return v[0] * v2[0] + v[1] * v2[1] + v[2] * v2[2];
}

public Vector3f Cross(Vector3f v2)
{
return new Vector3f(
v[1] * v2.v[2] - v[2] * v2.v[1],
v[2] * v2.v[0] - v[0] * v2.v[2],
v[0] * v2.v[1] - v[1] * v2.v[0]);
}

public float SqrDistance(Vector3f v2)
{
float a = (v[0] - v2[0]), b = (v[1] - v2[1]), c = (v[2] - v2[2]);
return a * a + b * b + c * c;
}


public void Set(Vector3f o)
Expand Down
39 changes: 39 additions & 0 deletions queries/RayIntersection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,44 @@ public static bool SphereSigned(Vector3f vOrigin, Vector3f vDirection, Vector3f

return true;
}




public static bool InfiniteCylinder(Vector3f vOrigin, Vector3f vDirection, Vector3f vCylOrigin, Vector3f vCylAxis, float fRadius, out float fRayT)
{
bool bHit = InfiniteCylinderSigned(vOrigin, vDirection, vCylOrigin, vCylAxis, fRadius, out fRayT);
fRayT = Math.Abs(fRayT);
return bHit;
}
public static bool InfiniteCylinderSigned(Vector3f vOrigin, Vector3f vDirection, Vector3f vCylOrigin, Vector3f vCylAxis, float fRadius, out float fRayT)
{
// [RMS] ugh this is shit...not even sure it works in general, but works for a ray inside cylinder

fRayT = 0.0f;


Vector3f AB = vCylAxis;
Vector3f AO = (vOrigin - vCylOrigin);
if (AO.SqrDistance(AO.Dot(AB) * AB) > fRadius * fRadius)
return false;

Vector3f AOxAB = AO.Cross(AB);
Vector3f VxAB = vDirection.Cross(AB);
float ab2 = AB.Dot(AB);
float a = VxAB.Dot(VxAB);
float b = 2 * VxAB.Dot(AOxAB);
float c = AOxAB.Dot(AOxAB) - (fRadius * fRadius * ab2);

double discrim = b * b - 4 * a * c;
if (discrim <= 0)
return false;
discrim = Math.Sqrt(discrim);
fRayT = (-b - (float)discrim) / (2 * a);
//float t1 = (-b + (float)discrim) / (2 * a);

return true;
}

}
}

0 comments on commit 39ab19e

Please sign in to comment.