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.
added a bunch of distance functions, ray/triangle intersection
- Loading branch information
Showing
14 changed files
with
1,029 additions
and
8 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,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace g3 | ||
{ | ||
public struct Triangle3d | ||
{ | ||
public Vector3d V0, V1, V2; | ||
|
||
public Triangle3d(Vector3d v0, Vector3d v1, Vector3d v2) | ||
{ | ||
V0 = v0; V1 = v1; V2 = v2; | ||
} | ||
|
||
public Vector3d this[int key] | ||
{ | ||
get { return (key == 0) ? V0 : (key == 1) ? V1 : V2; } | ||
set { if (key == 0) V0 = value; else if (key == 1) V1 = value; else V2 = value; } | ||
} | ||
|
||
public Vector3d PointAt(double bary0, double bary1, double bary2) | ||
{ | ||
return bary0 * V0 + bary1 * V1 + bary2 * V2; | ||
} | ||
public Vector3d PointAt(Vector3d bary) | ||
{ | ||
return bary.x* V0 + bary.y* V1 + bary.z* V2; | ||
} | ||
|
||
// conversion operators | ||
public static implicit operator Triangle3d(Triangle3f v) | ||
{ | ||
return new Triangle3d(v.V0, v.V1, v.V2); | ||
} | ||
public static explicit operator Triangle3f(Triangle3d v) | ||
{ | ||
return new Triangle3f((Vector3f)v.V0, (Vector3f)v.V1, (Vector3f)v.V2); | ||
} | ||
} | ||
|
||
|
||
|
||
public struct Triangle3f | ||
{ | ||
public Vector3f V0, V1, V2; | ||
|
||
public Triangle3f(Vector3f v0, Vector3f v1, Vector3f v2) | ||
{ | ||
V0 = v0; V1 = v1; V2 = v2; | ||
} | ||
|
||
public Vector3f this[int key] | ||
{ | ||
get { return (key == 0) ? V0 : (key == 1) ? V1 : V2; } | ||
set { if (key == 0) V0 = value; else if (key == 1) V1 = value; else V2 = value; } | ||
} | ||
|
||
|
||
public Vector3f PointAt(float bary0, float bary1, float bary2) | ||
{ | ||
return bary0 * V0 + bary1 * V1 + bary2 * V2; | ||
} | ||
public Vector3f PointAt(Vector3f bary) | ||
{ | ||
return bary.x * V0 + bary.y * V1 + bary.z * V2; | ||
} | ||
} | ||
|
||
} |
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,126 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace g3 | ||
{ | ||
// ported from WildMagic 5 | ||
// https://www.geometrictools.com/Downloads/Downloads.html | ||
|
||
public class DistLine3Segment3 | ||
{ | ||
Line3d line; | ||
public Line3d Line | ||
{ | ||
get { return line; } | ||
set { line = value; DistanceSquared = -1.0; } | ||
} | ||
|
||
Segment3d segment; | ||
public Segment3d Segment | ||
{ | ||
get { return segment; } | ||
set { segment = value; DistanceSquared = -1.0; } | ||
} | ||
|
||
public double DistanceSquared = -1.0; | ||
|
||
public Vector3d LineClosest; | ||
public double LineParameter; | ||
public Vector3d SegmentClosest; | ||
public double SegmentParameter; | ||
|
||
|
||
public DistLine3Segment3( Line3d LineIn, Segment3d SegmentIn) | ||
{ | ||
this.segment = SegmentIn; this.line = LineIn; | ||
} | ||
|
||
static public double MinDistance(Line3d line, Segment3d segment) | ||
{ | ||
return new DistLine3Segment3( line, segment ).Get(); | ||
} | ||
static public double MinDistanceLineParam( Line3d line, Segment3d segment ) | ||
{ | ||
return new DistLine3Segment3( line, segment ).Compute().LineParameter; | ||
} | ||
|
||
|
||
public DistLine3Segment3 Compute() | ||
{ | ||
GetSquared(); | ||
return this; | ||
} | ||
|
||
public double Get() | ||
{ | ||
return Math.Sqrt( GetSquared() ); | ||
} | ||
|
||
|
||
public double GetSquared() | ||
{ | ||
if (DistanceSquared >= 0) | ||
return DistanceSquared; | ||
|
||
Vector3d diff = line.Origin - segment.Center; | ||
double a01 = -line.Direction.Dot(segment.Direction); | ||
double b0 = diff.Dot(line.Direction); | ||
double c = diff.LengthSquared; | ||
double det = Math.Abs(1 - a01 * a01); | ||
double b1, s0, s1, sqrDist, extDet; | ||
|
||
if (det >= MathUtil.ZeroTolerance) { | ||
// The line and segment are not parallel. | ||
b1 = -diff.Dot(segment.Direction); | ||
s1 = a01 * b0 - b1; | ||
extDet = segment.Extent * det; | ||
|
||
if (s1 >= -extDet) { | ||
if (s1 <= extDet) { | ||
// Two interior points are closest, one on the line and one | ||
// on the segment. | ||
double invDet = (1) / det; | ||
s0 = (a01 * b1 - b0) * invDet; | ||
s1 *= invDet; | ||
sqrDist = s0 * (s0 + a01 * s1 + (2) * b0) + | ||
s1 * (a01 * s0 + s1 + (2) * b1) + c; | ||
} else { | ||
// The endpoint e1 of the segment and an interior point of | ||
// the line are closest. | ||
s1 = segment.Extent; | ||
s0 = -(a01 * s1 + b0); | ||
sqrDist = -s0 * s0 + s1 * (s1 + (2) * b1) + c; | ||
} | ||
} else { | ||
// The end point e0 of the segment and an interior point of the | ||
// line are closest. | ||
s1 = -segment.Extent; | ||
s0 = -(a01 * s1 + b0); | ||
sqrDist = -s0 * s0 + s1 * (s1 + (2) * b1) + c; | ||
} | ||
} else { | ||
// The line and segment are parallel. Choose the closest pair so that | ||
// one point is at segment center. | ||
s1 = 0; | ||
s0 = -b0; | ||
sqrDist = b0 * s0 + c; | ||
} | ||
|
||
LineClosest = line.Origin + s0 * line.Direction; | ||
SegmentClosest = segment.Center + s1 * segment.Direction; | ||
LineParameter = s0; | ||
SegmentParameter = s1; | ||
|
||
// Account for numerical round-off errors. | ||
if (sqrDist < 0) { | ||
sqrDist = 0; | ||
} | ||
|
||
DistanceSquared = sqrDist; | ||
return sqrDist; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.