forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plan.cs
150 lines (133 loc) · 3.99 KB
/
Plan.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ProceduralToolkit.Skeleton
{
/// <summary>
/// Representation of the active plan during generation process
/// </summary>
public class Plan : IEnumerable<Plan.Vertex>
{
public int Count => vertices.Count;
public Vertex First => vertices[0];
private readonly List<Vertex> vertices = new List<Vertex>();
private Plan()
{
}
public Plan(IEnumerable<Vector2> polygon)
{
foreach (var vertex in polygon)
{
vertices.Add(new Vertex(vertex));
}
for (int i = 0; i < Count; i++)
{
var vertex = vertices[i];
vertex.previous = vertices.GetLooped(i - 1);
vertex.next = vertices.GetLooped(i + 1);
}
}
private void Add(Vertex vertex)
{
vertices.Add(vertex);
}
public void Insert(Vertex vertex, Vertex previous, Vertex next)
{
vertices.Add(vertex);
LinkVertices(previous, vertex, next);
}
public bool Remove(Vertex vertex)
{
return vertices.Remove(vertex);
}
public void Offset(float offset)
{
foreach (var vertex in vertices)
{
vertex.position -= vertex.bisector*Geometry.GetAngleOffset(offset, vertex.angle);
}
}
public List<Plan> Split()
{
var plans = new List<Plan>();
while (Count > 0)
{
int i = 0;
int max = Count;
var plan = new Plan();
var startVertex = First;
var currentVertex = startVertex;
do
{
if (i >= max)
{
Debug.LogError("Invalid connectivity");
break;
}
Remove(currentVertex);
plan.Add(currentVertex);
currentVertex = currentVertex.next;
i++;
} while (!currentVertex.Equals(startVertex));
plans.Add(plan);
}
return plans;
}
public IEnumerator<Vertex> GetEnumerator()
{
if (Count == 0)
{
yield break;
}
var startVertex = vertices[0];
var currentVertex = startVertex;
int i = 0;
int max = Count;
do
{
if (i >= max)
{
Debug.LogError("Invalid connectivity");
yield break;
}
yield return currentVertex;
currentVertex = currentVertex.next;
i++;
} while (!currentVertex.Equals(startVertex));
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private static void LinkVertices(Vertex a, Vertex b)
{
a.next = b;
b.previous = a;
}
private static void LinkVertices(Vertex a, Vertex b, Vertex c)
{
LinkVertices(a, b);
LinkVertices(b, c);
}
public class Vertex
{
public Vector2 position;
public float angle;
public Vector2 bisector;
public Vertex previous;
public Vertex next;
public bool inEvent;
public int previousPolygonIndex;
public int nextPolygonIndex;
public bool reflect => angle >= 180;
public Vertex(Vector2 position)
{
this.position = position;
}
public override string ToString()
{
return string.Format("{0} inEvent: {1}", position, inEvent);
}
}
}
}