forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved some utility functions from vrcad
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |