Skip to content

Commit

Permalink
added Ray3, Segment2/3, AABox3, DistRay3Seg3, CurveUtils.FindClosestR…
Browse files Browse the repository at this point in the history
…ayIntersection
  • Loading branch information
rms80 committed Nov 24, 2016
1 parent 3b3ac56 commit d2cf2ba
Show file tree
Hide file tree
Showing 15 changed files with 745 additions and 28 deletions.
39 changes: 39 additions & 0 deletions curve/CurveUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,44 @@ public static int FindNearestIndex(ICurve c, Vector3d v)
}
return iNearest;
}



public static bool FindClosestRayIntersection(ICurve c, double segRadius, Ray3d ray, out double rayT)
{
if (c.Closed)
throw new InvalidOperationException("CurveUtils.FindClosestRayIntersection doesn't support closed curves yet");

DistRay3Segment3 dist = new DistRay3Segment3(ray, new Segment3d(Vector3d.Zero, Vector3d.Zero) );

rayT = double.MaxValue;
int nNearSegment = -1;
double fNearSegT = 0.0;

int N = c.VertexCount;
for (int i = 0; i < N-1; ++i) {
dist.Segment.SetEndpoints(c.GetVertex(i), c.GetVertex(i + 1));
dist.Reset();

// raycast to line bounding-sphere first (is this going ot be faster??)
double fSphereHitT;
bool bHitBoundSphere = RayIntersection.SphereSigned(ray.Origin, ray.Direction,
dist.Segment.Center, dist.Segment.Extent + segRadius, out fSphereHitT);
if (bHitBoundSphere == false)
continue;

// find ray/seg min-distance and use ray T
double dSqr = dist.GetSquared();
if ( dSqr < segRadius*segRadius) {
if (dist.RayParameter < rayT) {
rayT = dist.RayParameter;
fNearSegT = dist.SegmentParameter;
nNearSegment = i;
}
}
}
return (nNearSegment >= 0);
}

}
}
11 changes: 10 additions & 1 deletion curve/DCurve3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DCurve3 : ICurve
// [TODO] use dvector? or double-indirection indexing?
// question is how to insert efficiently...
public List<Vector3d> vertices;
public bool Closed;
public bool Closed { get; set; }
public int Timestamp;

public DCurve3()
Expand Down Expand Up @@ -47,5 +47,14 @@ public Vector3d End {
public IEnumerable<Vector3d> Vertices() {
return vertices;
}


public AxisAlignedBox3d GetBoundingBox()
{
AxisAlignedBox3d box = AxisAlignedBox3d.Empty;
foreach (Vector3d v in vertices)
box.Contain(v);
return box;
}
}
}
1 change: 1 addition & 0 deletions curve/ICurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace g3
public interface ICurve
{
int VertexCount { get; }
bool Closed { get; }

Vector3d GetVertex(int i);

Expand Down
6 changes: 3 additions & 3 deletions implicit/ImplicitField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IImplicitField2D

void Gradient( float fX, float fY, ref float fGX, ref float fGY );

Box2f Bounds { get; }
AxisAlignedBox2f Bounds { get; }
}

public interface IImplicitOperator2D : IImplicitField2D
Expand Down Expand Up @@ -52,9 +52,9 @@ public float Value( float fX, float fY ) {
return fDist2 * fDist2 * fDist2;
}

public Box2f Bounds {
public AxisAlignedBox2f Bounds {
get {
return new Box2f(LowX, LowY, HighX, HighY);
return new AxisAlignedBox2f(LowX, LowY, HighX, HighY);
}
}

Expand Down
4 changes: 2 additions & 2 deletions implicit/ImplicitOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ virtual public void Gradient(float fX, float fY, ref float fGX, ref float fGY)
fGY = (Value(fX, fY + fDelta) - fValue) / fDelta;
}

virtual public Box2f Bounds
virtual public AxisAlignedBox2f Bounds
{
get {
Box2f box = new Box2f();
AxisAlignedBox2f box = new AxisAlignedBox2f();
for (int i = 0; i < m_vChildren.Count; ++i)
box.Contain(m_vChildren[i].Bounds);
return box;
Expand Down
12 changes: 6 additions & 6 deletions implicit/MarchingQuads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MarchingQuads
{
PolyLine m_stroke;

Box2f m_bounds;
AxisAlignedBox2f m_bounds;
float m_fXShift;
float m_fYShift;
float m_fScale;
Expand Down Expand Up @@ -72,9 +72,9 @@ struct SeedPoint {
bool[] m_bEdgeSigns;


public MarchingQuads(int nSubdivisions, Box2f bounds, float fIsoValue) {
public MarchingQuads(int nSubdivisions, AxisAlignedBox2f bounds, float fIsoValue) {
m_stroke = new PolyLine();
m_bounds = new Box2f();
m_bounds = new AxisAlignedBox2f();

m_nCells = nSubdivisions;
SetBounds(bounds);
Expand All @@ -95,7 +95,7 @@ public int Subdivisions {
set { m_nCells = value; SetBounds( m_bounds ); InitializeCells(); }
}

public Box2f Bounds {
public AxisAlignedBox2f Bounds {
get { return m_bounds; }
set { SetBounds(value); }
}
Expand All @@ -105,7 +105,7 @@ public PolyLine Stroke {
}


public Box2f GetBounds() {
public AxisAlignedBox2f GetBounds() {
return m_bounds;
}

Expand Down Expand Up @@ -437,7 +437,7 @@ void InitializeCells() {
}
}

void SetBounds( Box2f bounds ) {
void SetBounds( AxisAlignedBox2f bounds ) {
m_bounds = bounds;

m_fXShift = (bounds.Min.x < 0) ? bounds.Min.x : -bounds.Min.x;
Expand Down
32 changes: 16 additions & 16 deletions math/Box2f.cs → math/AxisAlignedBox2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@

namespace g3
{
public struct Box2f
public struct AxisAlignedBox2f
{
public Vector2f Min;
public Vector2f Max;

public static Box2f Empty = new Box2f(false);
public static Box2f Infinite = new Box2f(Single.MinValue, Single.MinValue, Single.MaxValue, Single.MaxValue);
public static AxisAlignedBox2f Empty = new AxisAlignedBox2f(false);
public static AxisAlignedBox2f Infinite = new AxisAlignedBox2f(Single.MinValue, Single.MinValue, Single.MaxValue, Single.MaxValue);


public Box2f(bool bIgnore) {
public AxisAlignedBox2f(bool bIgnore) {
Min = new Vector2f(Single.MaxValue, Single.MaxValue);
Max = new Vector2f(Single.MinValue, Single.MinValue);
}

public Box2f(float xmin, float ymin, float xmax, float ymax) {
public AxisAlignedBox2f(float xmin, float ymin, float xmax, float ymax) {
Min = new Vector2f(xmin, ymin);
Max = new Vector2f(xmax, ymax);
}

public Box2f(float fSquareSize) {
public AxisAlignedBox2f(float fSquareSize) {
Min = new Vector2f(0, 0);
Max = new Vector2f(fSquareSize, fSquareSize);
}

public Box2f(float fWidth, float fHeight) {
public AxisAlignedBox2f(float fWidth, float fHeight) {
Min = new Vector2f(0, 0);
Max = new Vector2f(fWidth, fHeight);
}

public Box2f(Vector2f vMin, Vector2f vMax) {
public AxisAlignedBox2f(Vector2f vMin, Vector2f vMax) {
Min = new Vector2f(Math.Min(vMin.x, vMax.x), Math.Min(vMin.y, vMax.y));
Max = new Vector2f(Math.Max(vMin.x, vMax.x), Math.Max(vMin.y, vMax.y));
}

public Box2f(Vector2f vCenter, float fHalfWidth, float fHalfHeight) {
public AxisAlignedBox2f(Vector2f vCenter, float fHalfWidth, float fHalfHeight) {
Min = new Vector2f(vCenter.x - fHalfWidth, vCenter.y - fHalfHeight);
Max = new Vector2f(vCenter.x + fHalfWidth, vCenter.y + fHalfHeight);
}
public Box2f(Vector2f vCenter, float fHalfWidth) {
public AxisAlignedBox2f(Vector2f vCenter, float fHalfWidth) {
Min = new Vector2f(vCenter.x - fHalfWidth, vCenter.y - fHalfWidth);
Max = new Vector2f(vCenter.x + fHalfWidth, vCenter.y + fHalfWidth);
}

public Box2f(Box2f o) {
public AxisAlignedBox2f(AxisAlignedBox2f o) {
Min = new Vector2f(o.Min);
Max = new Vector2f(o.Max);
}
Expand Down Expand Up @@ -145,17 +145,17 @@ public void Contain(Vector2f v) {
Max.y = Math.Max(Max.y, v.y);
}

public void Contain(Box2f box) {
public void Contain(AxisAlignedBox2f box) {
Contain(box.Min);
Contain(box.Max);
}

public Box2f Intersect(Box2f box) {
Box2f intersect = new Box2f(
public AxisAlignedBox2f Intersect(AxisAlignedBox2f box) {
AxisAlignedBox2f intersect = new AxisAlignedBox2f(
Math.Max(Min.x, box.Min.x), Math.Max(Min.y, box.Min.y),
Math.Min(Max.x, box.Max.x), Math.Min(Max.y, box.Max.y));
if (intersect.Height <= 0 || intersect.Width <= 0)
return Box2f.Empty;
return AxisAlignedBox2f.Empty;
else
return intersect;
}
Expand All @@ -165,7 +165,7 @@ public Box2f Intersect(Box2f box) {
public bool Contains(Vector2f v) {
return (Min.x < v.x) && (Min.y < v.y) && (Max.x > v.x) && (Max.y > v.y);
}
public bool Intersects(Box2f box) {
public bool Intersects(AxisAlignedBox2f box) {
return !((box.Max.x < Min.x) || (box.Min.x > Max.x) || (box.Max.y < Min.y) || (box.Min.y > Max.y));
}

Expand Down
Loading

0 comments on commit d2cf2ba

Please sign in to comment.