Skip to content

Commit

Permalink
Reimplement AdjacentPairs as a composition of RepeatFirst and Edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Majiir committed Sep 2, 2014
1 parent 4cc5d7b commit a4cd913
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 30 deletions.
34 changes: 34 additions & 0 deletions Plugin/EnumerableExtensions/Edges.cs
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;

namespace Kethane.EnumerableExtensions
{
public static class EdgesEx
{
public static IEnumerable<Pair<T>> Edges<T>(this IEnumerable<T> sequence)
{
if (sequence == null) { throw new ArgumentNullException("sequence"); }
return edgesImpl(sequence);
}

public static IEnumerable<Pair<T>> EdgesCircular<T>(this IEnumerable<T> sequence)
{
return sequence.RepeatFirstOrEmpty().Edges();
}

private static IEnumerable<Pair<T>> edgesImpl<T>(IEnumerable<T> sequence)
{
using (var enumerator = sequence.GetEnumerator())
{
if (!enumerator.MoveNext()) { yield break; }
T previous = enumerator.Current;
while (enumerator.MoveNext())
{
T current = enumerator.Current;
yield return new Pair<T>(previous, current);
previous = current;
}
}
}
}
}
25 changes: 0 additions & 25 deletions Plugin/EnumerableExtensions/EnumerableExtensions.cs

This file was deleted.

42 changes: 42 additions & 0 deletions Plugin/EnumerableExtensions/Repeat.cs
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;

namespace Kethane.EnumerableExtensions
{
public static class Repeat
{
public static IEnumerable<T> RepeatFirst<T>(this IEnumerable<T> sequence)
{
if (sequence == null) { throw new ArgumentNullException("sequence"); }
return repeatFirstImpl(sequence, true);
}

public static IEnumerable<T> RepeatFirstOrEmpty<T>(this IEnumerable<T> sequence)
{
if (sequence == null) { throw new ArgumentNullException("sequence"); }
return repeatFirstImpl(sequence, false);
}

private static IEnumerable<T> repeatFirstImpl<T>(IEnumerable<T> sequence, bool throwOnEmpty)
{
using (var enumerator = sequence.GetEnumerator())
{
if (!enumerator.MoveNext())
{
if (throwOnEmpty) { throw new InvalidOperationException("Sequence is empty"); }
yield break;
}

T first = enumerator.Current;
yield return first;

while (enumerator.MoveNext())
{
yield return enumerator.Current;
}

yield return first;
}
}
}
}
2 changes: 1 addition & 1 deletion Plugin/GeodesicGrid/BoundsMap.cs
Expand Up @@ -29,7 +29,7 @@ public BoundsMap(Func<Cell, float> heightAt, int level)
{
maxs[triangle.Index] = triangle.GetVertices(level).Max(heightAt);

var min = Mathf.Sqrt(triangle.GetVertices(level).AdjacentPairs().Min(e => (e.First.Position * heightAt(e.First) + e.Second.Position * heightAt(e.Second)).sqrMagnitude)) / 2;
var min = Mathf.Sqrt(triangle.GetVertices(level).EdgesCircular().Min(e => (e.First.Position * heightAt(e.First) + e.Second.Position * heightAt(e.Second)).sqrMagnitude)) / 2;
mins[triangle.Index] = Math.Min(min, triangle.GetVertices(level).Min(heightAt));
}

Expand Down
4 changes: 2 additions & 2 deletions Plugin/GeodesicGrid/Cell.cs
Expand Up @@ -140,14 +140,14 @@ public Vector3 Position
public IEnumerable<Vector3> GetVertices(int level)
{
var position = this.Position;
return this.GetNeighbors(level).Select(c => c.Position).AdjacentPairs().Select(p => (position + p.First + p.Second).normalized);
return this.GetNeighbors(level).Select(c => c.Position).EdgesCircular().Select(p => (position + p.First + p.Second).normalized);
}

public IEnumerable<Vector3> GetVertices(int level, Func<Cell, float> heightAt)
{
var position = this.Position;
var height = heightAt(this);
foreach (var pair in this.GetNeighbors(level).AdjacentPairs())
foreach (var pair in this.GetNeighbors(level).EdgesCircular())
{
yield return (position + pair.First.Position + pair.Second.Position).normalized * (height + heightAt(pair.First) + heightAt(pair.Second)) / 3;
}
Expand Down
2 changes: 1 addition & 1 deletion Plugin/GeodesicGrid/Triangle.cs
Expand Up @@ -180,7 +180,7 @@ private static bool intersectsCell(Ray ray, Triangle triangle, int level, float
if ((radius >= min) && triangleContains(triangle, level, ray.origin)) { return true; }
}

return triangle.GetVertices(level).Select(c => c.Position).AdjacentPairs().Any(p => intersectsFace(ray, p.Second, p.First, min, max));
return triangle.GetVertices(level).Select(c => c.Position).EdgesCircular().Any(p => intersectsFace(ray, p.Second, p.First, min, max));
}

private static bool triangleContains(Triangle triangle, int level, Vector3 line)
Expand Down
3 changes: 2 additions & 1 deletion Plugin/Kethane.csproj
Expand Up @@ -46,9 +46,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="EnumerableExtensions\Edges.cs" />
<Compile Include="EnumerableExtensions\AppendPrepend.cs" />
<Compile Include="EnumerableExtensions\MinMaxBy.cs" />
<Compile Include="EnumerableExtensions\Pair.cs" />
<Compile Include="EnumerableExtensions\Repeat.cs" />
<Compile Include="EnumerableExtensions\ReverseComparer.cs" />
<Compile Include="GeodesicGrid\BoundsMap.cs" />
<Compile Include="GeodesicGrid\Cell.cs" />
Expand All @@ -63,7 +65,6 @@
<Compile Include="InstallCleanup.cs" />
<Compile Include="IWindowToggle.cs" />
<Compile Include="LegacyResourceGenerator.cs" />
<Compile Include="EnumerableExtensions\EnumerableExtensions.cs" />
<Compile Include="HeatSinkAnimator.cs" />
<Compile Include="IResourceGenerator.cs" />
<Compile Include="IDetectorAnimator.cs" />
Expand Down

0 comments on commit a4cd913

Please sign in to comment.