-
Notifications
You must be signed in to change notification settings - Fork 6
/
BoundsMap.cs
70 lines (58 loc) · 2.17 KB
/
BoundsMap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using GeodesicGrid.EnumerableExtensions;
using System;
using System.Linq;
using Mathf = UnityEngine.Mathf;
namespace GeodesicGrid
{
public class BoundsMap
{
private readonly float[][] minVals;
private readonly float[][] maxVals;
public BoundsMap(Func<Cell, float> heightAt, int level)
{
minVals = new float[level + 1][];
maxVals = new float[level + 1][];
for (var i = 0; i <= level; i++)
{
var count = Triangle.CountAtLevel(i);
minVals[i] = new float[count];
maxVals[i] = new float[count];
}
var mins = minVals[level];
var maxs = maxVals[level];
foreach (var triangle in Triangle.AtLevel(level))
{
maxs[triangle.Index] = triangle.GetVertices(level).Max(heightAt);
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));
}
for (int i = level - 1; i >= 0; i--)
{
var childMins = mins;
var childMaxs = maxs;
mins = minVals[i];
maxs = maxVals[i];
foreach (var triangle in Triangle.AtLevel(i))
{
var min = float.PositiveInfinity;
var max = 0f;
foreach (var child in triangle.GetChildren(i + 1))
{
min = Math.Min(min, childMins[child.Index]);
max = Math.Max(max, childMaxs[child.Index]);
}
mins[triangle.Index] = min;
maxs[triangle.Index] = max;
}
}
}
public float GetMin(Triangle triangle, int level)
{
return minVals[level][triangle.Index];
}
public float GetMax(Triangle triangle, int level)
{
return maxVals[level][triangle.Index];
}
}
}