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.
misc improvements, added Line/Ray and Ray/Ray distance
- Loading branch information
Showing
7 changed files
with
389 additions
and
4 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,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; | ||
} | ||
} | ||
} |
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
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,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; | ||
} | ||
|
||
|
||
|
||
} | ||
} |
Oops, something went wrong.