diff --git a/core/FileSystemUtils.cs b/core/FileSystemUtils.cs new file mode 100644 index 00000000..88e8a6ea --- /dev/null +++ b/core/FileSystemUtils.cs @@ -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; + } + + } +} diff --git a/math/Vector3f.cs b/math/Vector3f.cs index 3209ac6a..a0dedd1d 100644 --- a/math/Vector3f.cs +++ b/math/Vector3f.cs @@ -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) diff --git a/queries/RayIntersection.cs b/queries/RayIntersection.cs index fdfb265c..e24a371e 100644 --- a/queries/RayIntersection.cs +++ b/queries/RayIntersection.cs @@ -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; + } + } }