-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphGenerator.cs
More file actions
259 lines (210 loc) · 8.33 KB
/
GraphGenerator.cs
File metadata and controls
259 lines (210 loc) · 8.33 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GraphGenerator: MonoBehaviour {
public float spacing;
public int Length;
public int Width;
public int Height;
public GameObject Node;
public GameObject Edge;
public Material Selected;
public Material Path;
[Range(1, 50)]
[SerializeField] float radius;
[Range(0.5 f, 5)]
[SerializeField] float MaxEdgeDistance;
[Range(2, 500)]
[SerializeField] int regionSize;
[Range(1, 30)]
[SerializeField] int rejectionSamples;
public StaticValues.GraphStyle style;
public StaticValues.Algorithm algorithm;
private Graph graph;
void Start() {
Screen.fullScreen = true;
spacing = StaticValues.spacing;
Length = StaticValues.Length;
Width = StaticValues.Width;
Height = StaticValues.Height;
Length = (Length < 1) ? 1 : Length;
Width = (Width < 1) ? 1 : Width;
Height = (Height < 1) ? 1 : Height;
radius = StaticValues.radius;
MaxEdgeDistance = StaticValues.MaxEdgeDistance;
regionSize = StaticValues.regionSize;
rejectionSamples = StaticValues.rejectionSamples;
style = StaticValues.graphStyle;
algorithm = StaticValues.algorithm;
}
bool first = true;
private void Update() {
if (first) {
GenerateGraph();
startSearch();
first = false;
}
}
void startSearch() {
switch (algorithm) {
case StaticValues.Algorithm.AStar:
graph.AStar();
break;
case StaticValues.Algorithm.Dijkstra:
graph.dijkstra();
break;
case StaticValues.Algorithm.BFS:
graph.BFS();
break;
case StaticValues.Algorithm.DFS:
graph.DFS();
break;
}
}
private void GenerateGraph() {
switch (style) {
case StaticValues.GraphStyle.Line:
graph = new Graph(Length);
Line(Length);
break;
case StaticValues.GraphStyle.Grid:
graph = new Graph(Width * Length);
Grid(Width, Length);
break;
case StaticValues.GraphStyle.Lattice:
graph = new Graph(Width * Length * Height);
Lattice(Width, Length, Height);
break;
case StaticValues.GraphStyle.Poisson3D:
PoissonGenerator();
break;
case StaticValues.GraphStyle.Poisson2D:
PoissonGenerator2D();
break;
}
}
public void DeleteAll() {
foreach(GameObject o in FindObjectsOfType < GameObject > ()) {
if (o != gameObject)
Destroy(o);
}
}
private Edge NewEdge(Node n1, Node n2) {
Vector3 difference = n1.transform.position - n2.transform.position;
Edge edge = NewEdge((n1.transform.position.x + n2.transform.position.x) / 2, (n1.transform.position.y + n2.transform.position.y) / 2, (n1.transform.position.z + n2.transform.position.z) / 2, (float) Math.Sqrt(Math.Pow(n1.transform.position.x - n2.transform.position.x, 2) + Math.Pow(n1.transform.position.y - n2.transform.position.y, 2) + Math.Pow(n1.transform.position.z - n2.transform.position.z, 2)) / 2);
if (edge != null) {
edge.transform.up = difference.normalized;
} else {
return null;
}
return edge;
}
public Node NewNode(float xPosition = 0, float yPosition = 0, float zPosition = 0) {
GameObject currentNodeGameObject = Instantiate(Node, new Vector3(xPosition, yPosition, zPosition), Quaternion.identity);
Node currentNode = currentNodeGameObject.GetComponent < Node > ();
graph.AddNode(currentNode);
return currentNode;
}
public Edge NewEdge(float xPosition = 0, float yPosition = 0, float zPosition = 0, float length = 1) {
if (length <= 0) {
return null;
}
GameObject edgeGameObject = Instantiate(Edge, new Vector3(xPosition, yPosition, zPosition), Quaternion.identity);
edgeGameObject.transform.localScale = new Vector3(edgeGameObject.transform.localScale.x, length, edgeGameObject.transform.localScale.z);
Edge edge = edgeGameObject.GetComponent < Edge > ();
return edge;
}
public Node[] Line(int length, float yPosition = 0, float zPosition = 0) {
Node[] line = new Node[length];
Node previousNode = null;
int index = 0;
for (float f = -length / 2.0 f; f < length / 2.0 f; f++) {
Node currentNode = NewNode(f * spacing + 1, yPosition, zPosition);
if (previousNode != null) {
ConnectNodesFully(previousNode, currentNode);
}
previousNode = currentNode;
line[index] = currentNode;
index++;
}
return line;
}
public Node[, ] Grid(int width, int length, float yPosition = 0) {
Node[, ] grid = new Node[width, length];
Node[] previousRow = new Node[length];
int index = 0;
for (float i = -width / 2.0 f; i < width / 2.0 f; i++) {
Node[] row = Line(length, yPosition, zPosition: i * spacing + 1);
for (int rowIndex = 0; rowIndex < length; rowIndex++) {
if (previousRow[rowIndex] != null) {
ConnectNodesFully(previousRow[rowIndex], row[rowIndex]);
}
grid[index, rowIndex] = row[rowIndex];
}
previousRow = row;
index++;
}
return grid;
}
public void Lattice(int width, int length, int height, float yPosition = 0) {
Node[, ] previousGrid = new Node[width, length];
for (float i = -height / 2.0 f; i < height / 2.0 f; i++) {
Node[, ] grid = Grid(width, length, i * spacing + 1);
for (int columnIndex = 0; columnIndex < width; columnIndex++) {
for (int rowIndex = 0; rowIndex < length; rowIndex++) {
if (previousGrid[columnIndex, rowIndex] != null) {
ConnectNodesFully(previousGrid[columnIndex, rowIndex], grid[columnIndex, rowIndex]);
}
}
}
previousGrid = grid;
}
}
public int PoissonGenerator() {
List < Vector3 > points;
List < Node > nodes = new List < Node > ();
points = PoissonSampler.GeneratePoisson(regionSize, radius, rejectionSamples);
graph = new Graph(points.Count);
if (points != null) {
for (int i = 0; i < points.Count; i++) {
Node currentNode = NewNode(points[i].x, points[i].y, points[i].z);
nodes.Add(currentNode);
}
foreach(var node in nodes) {
Collider[] NearNodes = Physics.OverlapSphere(node.transform.position, radius * MaxEdgeDistance);
foreach(var NearNode in NearNodes) {
if (NearNode.gameObject.GetComponent < Node > () != null) {
ConnectNodesFully(node, NearNode.gameObject.GetComponent < Node > ());
}
}
}
}
return points.Count;
}
public int PoissonGenerator2D() {
List < Vector3 > points;
List < Node > nodes = new List < Node > ();
points = PoissonSampler2D.GeneratePoisson(regionSize, radius, rejectionSamples);
graph = new Graph(points.Count);
if (points != null) {
for (int i = 0; i < points.Count; i++) {
Node currentNode = NewNode(points[i].x, points[i].y, points[i].z);
nodes.Add(currentNode);
}
foreach(var node in nodes) {
Collider[] NearNodes = Physics.OverlapSphere(node.transform.position, radius * MaxEdgeDistance);
foreach(var NearNode in NearNodes) {
if (NearNode.gameObject.GetComponent < Node > () != null) {
ConnectNodesFully(node, NearNode.gameObject.GetComponent < Node > ());
}
}
}
}
return points.Count;
}
public void ConnectNodesFully(Node currentNode, Node previousNode) {
Edge edge = NewEdge(currentNode, previousNode);
graph.ConnectNodesFully(currentNode, previousNode, edge);
}
}