-
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.
Fudging epsilons to make things more reliable
- Loading branch information
1 parent
17af724
commit d6d0f02
Showing
5 changed files
with
182 additions
and
45 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
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,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Sandbox.Polygons; | ||
|
||
partial class PolygonMeshBuilder | ||
{ | ||
[ThreadStatic] | ||
private static List<int> Validate_EdgeList; | ||
|
||
private string _svg; | ||
|
||
private void Validate() | ||
{ | ||
if ( _validated ) | ||
{ | ||
return; | ||
} | ||
|
||
_validated = true; | ||
|
||
return; | ||
|
||
_svg = ToSvg(); | ||
|
||
// Check active edge loops: | ||
// * Referenced edges must also be active | ||
// * Make sure references are correct in both directions | ||
// * Edges can't reference themselves | ||
|
||
foreach ( var edgeIndex in _activeEdges ) | ||
{ | ||
ref var edge = ref _allEdges[edgeIndex]; | ||
|
||
if ( !_activeEdges.Contains( edge.NextEdge ) ) | ||
{ | ||
throw InvalidPolygonException(); | ||
} | ||
|
||
if ( !_activeEdges.Contains( edge.PrevEdge ) ) | ||
{ | ||
throw InvalidPolygonException(); | ||
} | ||
|
||
if ( edge.NextEdge == edge.Index ) | ||
{ | ||
throw InvalidPolygonException(); | ||
} | ||
|
||
ref var next = ref _allEdges[edge.NextEdge]; | ||
|
||
if ( next.PrevEdge != edge.Index ) | ||
{ | ||
throw InvalidPolygonException(); | ||
} | ||
} | ||
|
||
// Check for intersecting edges | ||
// TODO: Bentley–Ottmann? | ||
|
||
Validate_EdgeList ??= new List<int>(); | ||
Validate_EdgeList.Clear(); | ||
Validate_EdgeList.AddRange( _activeEdges ); | ||
|
||
for ( var i = 0; i < Validate_EdgeList.Count; ++i ) | ||
{ | ||
ref var edgeA0 = ref _allEdges[Validate_EdgeList[i]]; | ||
ref var edgeA1 = ref _allEdges[edgeA0.NextEdge]; | ||
|
||
var a0 = edgeA0.Origin; | ||
var a1 = edgeA1.Origin; | ||
|
||
var minA = Vector2.Min( a0 ,a1 ); | ||
var maxA = Vector2.Max( a0, a1 ); | ||
|
||
for ( var j = i + 1; j < Validate_EdgeList.Count; ++j ) | ||
{ | ||
ref var edgeB0 = ref _allEdges[Validate_EdgeList[j]]; | ||
|
||
if ( edgeA0.NextEdge == edgeB0.Index || edgeA0.PrevEdge == edgeB0.Index ) | ||
{ | ||
continue; | ||
} | ||
|
||
ref var edgeB1 = ref _allEdges[edgeB0.NextEdge]; | ||
|
||
var b0 = edgeA0.Origin; | ||
var b1 = edgeA1.Origin; | ||
|
||
var minB = Vector2.Min( b0, b1 ); | ||
var maxB = Vector2.Max( b0, b1 ); | ||
|
||
if ( minA.x >= maxB.x || minA.y >= maxB.y || minB.x >= maxA.x || minB.y >= maxA.y ) | ||
{ | ||
continue; | ||
} | ||
|
||
if ( Helpers.LineSegmentsIntersect( a0, a1, b0, b1 ) ) | ||
{ | ||
throw InvalidPolygonException(); | ||
} | ||
} | ||
} | ||
|
||
_validated = true; | ||
} | ||
|
||
private static Exception InvalidPolygonException() | ||
{ | ||
return new Exception( "Invalid polygon" ); | ||
} | ||
} |
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 seems to help with Facepunch/sbox-sdf#17