-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cs
More file actions
193 lines (175 loc) · 4.15 KB
/
main.cs
File metadata and controls
193 lines (175 loc) · 4.15 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
using Godot;
using System;
using System.Collections.Generic;
using NumFlat;
public partial class Main : Node2D
{
private HashSet<Vector2I> inputPoints;
private float[,] gridDistance;
private Vector2I[,] closestPointIndex;
private Image level_image;
public override void _Ready()
{
inputPoints = new HashSet<Vector2I>();
level_image = Image.Create(1024,1024,false,0);
UpdateImage();
}
public override void _Process(double delta)
{
}
public void OnComputeButtonPressed()
{
}
public void ClearPoints()
{
inputPoints.Clear();
BlankImage();
UpdateImage();
}
public void ComputeClosest()
{
GD.Print("Working?");
int gridHeight = 1024;
int gridWidth = 1024;
//instantiate arrays
gridDistance = new float[gridHeight,gridWidth];
closestPointIndex = new Vector2I[gridHeight,gridWidth];
for (int i = 0; i < gridHeight; i++)
{
for (int j = 0; j < gridWidth; j++)
{
gridDistance[i,j] = 10_000_000;
closestPointIndex[i,j] = new Vector2I(-1,-1);
}
}
//fill in known values
foreach (Vector2I point in inputPoints)
{
gridDistance[point.X,point.Y] = 0;
closestPointIndex[point.X,point.Y] = point;
}
//fast sweeping method
FastSweeping(gridHeight,gridWidth);
GD.Print("Computed");
ImageFromSet(gridDistance);
UpdateImage();
}
private void FastSweeping(int gridHeight, int gridWidth) {
//+x
for (int iter = 0; iter < 2; iter++)
{
for (int i = 1; i < gridHeight; i++)
{
for (int j = 0; j < gridWidth; j++)
{
if (closestPointIndex[i-1,j].X != -1)
{
float distance = (closestPointIndex[i-1,j] - new Vector2I(i,j)).Length();
if (distance < gridDistance[i,j])
{
gridDistance[i,j] = distance;
closestPointIndex[i,j] = closestPointIndex[i-1,j];
}
}
}
}
//+y
for (int j = 1; j < gridHeight; j++)
{
for (int i = 0; i < gridWidth; i++)
{
if (closestPointIndex[i,j-1].X != -1)
{
float distance = (closestPointIndex[i,j-1] - new Vector2I(i,j)).Length();
if (distance < gridDistance[i,j])
{
gridDistance[i,j] = distance;
closestPointIndex[i,j] = closestPointIndex[i,j-1];
}
}
}
}
//-x
for (int i = gridHeight - 2; i >= 0; i--)
{
for (int j = 0; j < gridWidth; j++)
{
if (closestPointIndex[i+1,j].X != -1)
{
float distance = (closestPointIndex[i+1,j] - new Vector2I(i,j)).Length();
if (distance < gridDistance[i,j])
{
gridDistance[i,j] = distance;
closestPointIndex[i,j] = closestPointIndex[i+1,j];
}
}
}
}
//-y
for (int j = gridHeight - 2; j >= 0; j--)
{
for (int i = 0; i < gridWidth; i++)
{
if (closestPointIndex[i,j+1].X != -1)
{
float distance = (closestPointIndex[i,j+1] - new Vector2I(i,j)).Length();
if (distance < gridDistance[i,j])
{
gridDistance[i,j] = distance;
closestPointIndex[i,j] = closestPointIndex[i,j+1];
//GD.Print(distance);
}
}
}
}
}
}
private void CheckNeighbors(int x, int y)
{
if (closestPointIndex[x+1,y].X != -1)
{
float distance = (closestPointIndex[x+1,y] - new Vector2I(x,y)).Length();
if (distance < gridDistance[x,y])
{
gridDistance[x,y] = distance;
closestPointIndex[x,y] = closestPointIndex[x+1,y];
}
}
}
public void BlankImage()
{
level_image.Fill(Colors.White);
foreach (Vector2I point in inputPoints)
{
level_image.SetPixel(point.X,point.Y,Colors.Black);
}
}
public void ImageFromSet(float[,] dataset)
{
for (int i = 0; i < dataset.GetLength(0); i++)
{
for (int j = 0; j < dataset.GetLength(1); j++)
{
level_image.SetPixel(i, j, new Color(dataset[i,j] / 600f,0,0));
if (dataset[i,j] <= 0.001 && dataset[i,j] >= -0.001)
{
level_image.SetPixel(i,j,new Color(1,1,1));
}
}
}
}
public void UpdateImage()
{
GD.Print("Updating Image");
(GetNode("Sprite2D") as Sprite2D).Texture = ImageTexture.CreateFromImage(level_image);
}
public void DrawPixel(Vector2I position)
{
if (0 < position.X && position.X < 1024 && 0 < position.Y && position.Y < 1024)
{
inputPoints.Add(position);
BlankImage();
UpdateImage();
}
}
}