Skip to content

Commit

Permalink
misc improvements, added Line/Ray and Ray/Ray distance
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Nov 30, 2016
1 parent 4d65386 commit 9b0db26
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 4 deletions.
17 changes: 16 additions & 1 deletion math/Frame3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using System.Text;
using g3;


#if G3_USING_UNITY
using UnityEngine;
using f3; // yuck? will go away though...
#endif

namespace g3
{
Expand Down Expand Up @@ -204,5 +207,17 @@ public virtual string ToString(string fmt) {
}



#if G3_USING_UNITY
public static implicit operator Frame3f(f3.Frame3 f)
{
return new Frame3f(f.Origin, f.Rotation);
}
public static implicit operator Frame3(Frame3f f)
{
return new Frame3(f.origin, f.rotation);
}
#endif

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

namespace g3
{
public class Line3d
{
public Vector3d Origin;
public Vector3d Direction;

public Line3d(Vector3d origin, Vector3d direction)
{
this.Origin = origin;
this.Direction = direction;
}

// parameter is distance along Line
public Vector3d PointAt(double d) {
return Origin + d * Direction;
}


// conversion operators
public static implicit operator Line3d(Line3f v)
{
return new Line3d(v.Origin, v.Direction);
}
public static explicit operator Line3f(Line3d v)
{
return new Line3f((Vector3f)v.Origin, (Vector3f)v.Direction);
}


}


public class Line3f
{
public Vector3f Origin;
public Vector3f Direction;

public Line3f(Vector3f origin, Vector3f direction)
{
this.Origin = origin;
this.Direction = direction;
}

// parameter is distance along Line
public Vector3f PointAt(float d)
{
return Origin + d * Direction;
}
}
}
16 changes: 13 additions & 3 deletions math/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace g3
public class MathUtil
{

public const double Deg2Rad = (180.0 / Math.PI);
public const double Rad2Deg = (Math.PI / 180.0);
public const double Deg2Rad = (Math.PI / 180.0);
public const double Rad2Deg = (180.0 / Math.PI);
public const double TwoPI = 2.0 * Math.PI;
public const double HalfPI = 0.5 * Math.PI;
public const double ZeroTolerance = 1e-08;
public const double Epsilon = 2.2204460492503131e-016;

public const float Rad2Degf = (float)(180.0 / Math.PI);
public const float Deg2Radf = (float)(Math.PI / 180.0);
public const float Rad2Degf = (float)(180.0 / Math.PI);
public const float PIf = (float)(Math.PI);
public const float TwoPIf = 2.0f * PIf;
public const float HalfPIf = 0.5f * PIf;
Expand Down Expand Up @@ -89,6 +89,16 @@ public static float PlaneAngleSignedD(Vector3f vFrom, Vector3f vTo, Vector3f pla



public static int MostParallelAxis(Frame3f f, Vector3f vDir) {
double dot0 = Math.Abs(f.X.Dot(vDir));
double dot1 = Math.Abs(f.Y.Dot(vDir));
double dot2 = Math.Abs(f.Z.Dot(vDir));
double m = Math.Max(dot0, Math.Max(dot1, dot2));
return (m == dot0) ? 0 : (m == dot1) ? 1 : 2;
}




//! if yshift is 0, function approaches y=1 at xZero from y=0.
//! speed (> 0) controls how fast it gets there
Expand Down
12 changes: 12 additions & 0 deletions math/Ray3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public Vector3d PointAt(double d) {
return Origin + d * Direction;
}


// conversion operators
public static implicit operator Ray3d(Ray3f v)
{
return new Ray3d(v.Origin, v.Direction);
}
public static explicit operator Ray3f(Ray3d v)
{
return new Ray3f((Vector3f)v.Origin, (Vector3f)v.Direction);
}


#if G3_USING_UNITY
public static implicit operator Ray3d(UnityEngine.Ray r)
{
Expand Down
114 changes: 114 additions & 0 deletions queries/DistLine3Ray3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace g3
{
public class DistLine3Ray3
{
Line3d line;
public Line3d Line
{
get { return line; }
set { line = value; DistanceSquared = -1.0; }
}

Ray3d ray;
public Ray3d Ray
{
get { return ray; }
set { ray = value; DistanceSquared = -1.0; }
}

public double DistanceSquared = -1.0;

public Vector3d LineClosest;
public double LineParameter;
public Vector3d RayClosest;
public double RayParameter;


public DistLine3Ray3(Ray3d ray, Line3d Line)
{
this.ray = ray; this.line = Line;
}

// have to do this if you are changing Ray/Line yourself and want to reuse this object
public void Reset()
{
DistanceSquared = -1.0f;
}


static public double MinDistance(Ray3d r, Line3d s)
{
return new DistLine3Ray3(r, s).Get();
}
static public double MinDistanceLineParam(Ray3d r, Line3d s)
{
return new DistLine3Ray3(r, s).Compute().LineParameter;
}


public DistLine3Ray3 Compute()
{
GetSquared();
return this;
}

public double Get()
{
return Math.Sqrt(GetSquared());
}


public double GetSquared ()
{
Vector3d kDiff = line.Origin - ray.Origin;
double a01 = -line.Direction.Dot(ray.Direction);
double b0 = kDiff.Dot(line.Direction);
double c = kDiff.LengthSquared;
double det = Math.Abs(1.0 - a01 * a01);
double b1, s0, s1, sqrDist;

if (det >= MathUtil.ZeroTolerance) {
b1 = -kDiff.Dot(ray.Direction);
s1 = a01 * b0 - b1;

if (s1 >= (double)0) {
// Two interior points are closest, one on line and one on ray.
double invDet = ((double)1) / det;
s0 = (a01 * b1 - b0) * invDet;
s1 *= invDet;
sqrDist = s0 * (s0 + a01 * s1 + ((double)2) * b0) +
s1 * (a01 * s0 + s1 + ((double)2) * b1) + c;
} else {
// Origin of ray and interior point of line are closest.
s0 = -b0;
s1 = (double)0;
sqrDist = b0 * s0 + c;
}
} else {
// Lines are parallel, closest pair with one point at ray origin.
s0 = -b0;
s1 = (double)0;
sqrDist = b0 * s0 + c;
}

LineClosest = line.Origin + s0 * line.Direction;
RayClosest = ray.Origin + s1 * ray.Direction;
LineParameter = s0;
RayParameter = s1;

// Account for numerical round-off errors.
if (sqrDist < (double)0) {
sqrDist = (double)0;
}
return sqrDist;
}



}
}
Loading

0 comments on commit 9b0db26

Please sign in to comment.