Skip to content

Commit

Permalink
catch garbage input to 2D line/line and seg/seg intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Dec 19, 2016
1 parent 574ffb2 commit 2452ad9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
3 changes: 2 additions & 1 deletion queries/Intersection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum IntersectionResult
{
NotComputed,
Intersects,
NoIntersection
NoIntersection,
InvalidQuery
}

public enum IntersectionType
Expand Down
10 changes: 9 additions & 1 deletion queries/IntrLine2Line2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ public IntrLine2Line2 Compute()
public bool Find()
{
if (Result != IntersectionResult.NotComputed)
return (Result != g3.IntersectionResult.NoIntersection);
return (Result == IntersectionResult.Intersects);

// [RMS] if either line direction is not a normalized vector,
// results are garbage, so fail query
if ( line1.Direction.IsNormalized == false || line2.Direction.IsNormalized == false ) {
Type = IntersectionType.Empty;
Result = IntersectionResult.InvalidQuery;
return false;
}

Vector2d s = Vector2d.Zero;
Type = Classify(line1.Origin, line1.Direction,
Expand Down
42 changes: 39 additions & 3 deletions queries/IntrSegment2Segment2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;

namespace g3
{
Expand Down Expand Up @@ -74,8 +75,12 @@ public bool IsSimpleIntersection {

public Vector2d Point0;
public Vector2d Point1; // only set if Quantity == 2, ie segment overlap
public double Segment1Parameter;
public double Segment2Parameter;


public IntrSegment2Segment2(Segment2d seg1, Segment2d seg2)
{
segment1 = seg1; segment2 = seg2;
}

public IntrSegment2Segment2 Compute()
{
Expand All @@ -87,7 +92,15 @@ public IntrSegment2Segment2 Compute()
public bool Find()
{
if (Result != IntersectionResult.NotComputed)
return (Result != g3.IntersectionResult.NoIntersection);
return (Result == IntersectionResult.Intersects);

// [RMS] if either segment direction is not a normalized vector,
// results are garbage, so fail query
if ( segment1.Direction.IsNormalized == false || segment2.Direction.IsNormalized == false ) {
Type = IntersectionType.Empty;
Result = IntersectionResult.InvalidQuery;
return false;
}


Vector2d s = Vector2d.Zero;
Expand Down Expand Up @@ -140,7 +153,30 @@ public bool Find()

Result = (Type != IntersectionType.Empty) ?
IntersectionResult.Intersects : IntersectionResult.NoIntersection;

// [RMS] for debugging...
//sanity_check();

return (Result == IntersectionResult.Intersects);
}



void sanity_check() {
if ( Quantity == 0 ) {
Util.gDevAssert(Type == IntersectionType.Empty);
Util.gDevAssert(Result == IntersectionResult.NoIntersection);
} else if (Quantity == 1) {
Util.gDevAssert(Type == IntersectionType.Point);
Util.gDevAssert( segment1.DistanceSquared(Point0) < MathUtil.ZeroTolerance );
Util.gDevAssert( segment2.DistanceSquared(Point0) < MathUtil.ZeroTolerance);
} else if ( Quantity == 2 ) {
Util.gDevAssert(Type == IntersectionType.Segment);
Util.gDevAssert( segment1.DistanceSquared(Point0) < MathUtil.ZeroTolerance );
Util.gDevAssert( segment1.DistanceSquared(Point1) < MathUtil.ZeroTolerance );
Util.gDevAssert( segment2.DistanceSquared(Point0) < MathUtil.ZeroTolerance);
Util.gDevAssert( segment2.DistanceSquared(Point1) < MathUtil.ZeroTolerance);
}
}
}
}

0 comments on commit 2452ad9

Please sign in to comment.