-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHilberts_Curve.cs
354 lines (296 loc) · 11.6 KB
/
Hilberts_Curve.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
namespace HilbertsCurve_CS
{
public class HilbertsCurve
{
public int iterator; public int dimention;
private int min_height; private int max_height;
private int min_x; private int max_x;
public HilbertsCurve(int iterator, int dimention)
{
//Initialize a hilbert curve with:
//Args:
//iterator(int): iterations to use in constructing the hilbert curve.
// if float, must satisfy p % 1 = 0
//dimention(int): number of dimensions.
// if float must satisfy n % 1 = 0
//
if (iterator <= 0 | dimention <= 0)
{
throw new ArgumentException($"Error. Number of dimentions and number of iterations need to be positive. Ckeck please values: {iterator} and {dimention}.");
}
this.iterator = iterator;
this.dimention = dimention;
this.min_height = 0;
this.max_height = (int)System.Math.Pow(2, this.iterator * this.dimention) - 1;
this.min_x = 0;
this.max_x = (int)System.Math.Pow(2, this.iterator) - 1;
}
public HilbertsCurve(float iterator, float dimention)
{
//Initialize a hilbert curve with:
//Args:
//iterator(float): iterations to use in constructing the hilbert curve.
// if float, must satisfy p % 1 = 0
//dimention(float): number of dimensions.
// if float must satisfy n % 1 = 0
//
if (iterator <= 0 | dimention <= 0)
{
throw new ArgumentException($"Error. Number of dimentions and number of iterations need to be positive. Ckeck please values: {iterator} and {dimention}.");
}
this.iterator = (int)iterator;
this.dimention = (int)dimention;
this.min_height = 0;
this.max_height = (int)System.Math.Pow(2, this.iterator * this.dimention) - 1;
this.min_x = 0;
this.max_x = (int)System.Math.Pow(2, this.iterator) - 1;
}
public override string ToString()
{
return ($"Hilbert's curve:\nIterations - {this.iterator}, Dimention - {this.dimention}.");
}
private static string Slice(string _value, int start)
{
int len = _value.Length - start;
return _value.Substring(start, len);
}
private static string Slice(string _value, int start, int end)
{
int len = end - start;
return _value.Substring(start, len);
}
private static string Slice(string _value, int start, int end, int step)
{
int len = end - start;
_value = _value.Substring(start, len);
string str_step = "";
for (int i = 0; i < _value.Length; i = i + step)
{
str_step += _value[i];
}
return str_step;
}
private static string ToBinary(int number, int padding)
{
//Return a binary string representation of `number` zero padded to `padding`
string binary = Convert.ToString(number, 2).PadLeft(padding, '0');
return binary;
}
private static int FromBinary(string binary_number)
{
//Return int from a binary string `binary_number`
return Convert.ToInt32(binary_number, 2);
}
private List<List<int>> Hilbert_integer_to_transpose(int hilbert_number)
{
/* Store a hilbert integer (`hilbert_number`) as its transpose (`coods_list`).
Args:
hilbert_number(int): integer distance along hilbert curve
Returns:
coods_list(list): transpose of hilbert_number
*/
List<List<int>> coods_list = new List<List<int>>();
for (int i = 0; i < hilbert_number; i++)
{
List<int> coordinates = new List<int>();
string i_bin = HilbertsCurve.ToBinary(i, this.iterator * this.dimention);
for (int j = this.dimention; j < 2 * this.dimention; j++)
{
coordinates.Add(HilbertsCurve.FromBinary(new string(i_bin.Where((ch, index) => index % (this.dimention) == (j - this.dimention)).ToArray())));
}
coods_list.Add(coordinates);
}
return coods_list;
}
private List<int> Hilbert_integer_to_transpose_single(int hilbert_number)
{
/* Store a hilbert integer (`hilbert_number`) as its transpose (`coordinate`).
Args:
hilbert_number(int): integer distance along hilbert curve
Returns:
coordinate(list): transpose of hilbert_number
*/
List<int> coordinate = new List<int>();
string i_bin = HilbertsCurve.ToBinary(hilbert_number, this.iterator * this.dimention);
for (int i = 0; i < this.dimention; i++)
{
int j = 0;
coordinate.Add(HilbertsCurve.FromBinary(new string(i_bin.Where((ch, index) => index % (this.dimention) == (i)).ToArray())));
++j;
}
return coordinate;
}
private int Transpose_to_hilbert_integer(List<int> coods_list)
{
/*R estore a hilbert integer (`hilbert_number`) from its transpose (`coods_list`).
Args:
coods_list (list): transpose of hilbert_number
Returns:
hilbert_number (int): integer distance along hilbert curve
*/
List<string> binaries = new List<string>(); string temp_h_str = "";
for (int i = 0; i < this.dimention; i++)
{
binaries.Add(HilbertsCurve.ToBinary(coods_list[i], this.iterator));
}
foreach (string i in binaries)
{
for (int j = 0; j < this.iterator; j++)
{
temp_h_str += i[j];
}
}
return HilbertsCurve.FromBinary(temp_h_str);
}
public List<int> Point_from_distance(int distance)
{
/*Return a point in n-dimensional space given a distance along a hilbert curve.
Args:
distance (int): integer distance along hilbert curve
Returns:
point (iterable of ints): an n-dimensional vector of lengh n where
each component value is between 0 and 2**this.iterator-1.
*/
List<int> point = new List<int>();
int z; int t; int q = 2; int p;
point = this.Hilbert_integer_to_transpose_single(distance);
z = 2 << (this.iterator - 1);
t = point[this.dimention - 1] >> 1;
for (int i = this.dimention - 1; i > 0; i--)
{
point[i] ^= point[i - 1]; //logic
}
point[0] ^= t;
// Undo excess work
while (q != 2)
{
p = q - 1;
for (int i = this.dimention - 1; i > -1; i--)
{
if (Convert.ToBoolean(q & point[i]))
{
point[0] = point[0] ^ p;
}
else
{
t = (point[0] ^ point[i]) & p;
point[0] = point[0] ^ t;
point[i] = point[i] ^ t;
}
}
q <<= 1;
}
return point;
}
public List<List<int>> Points_from_distance(List<int> distances)
{
/*Return points in n-dimensional space given distances along a hilbert curve.
Args:
distances (iterable of int): iterable of integer distances along hilbert curve
Returns:
points (iterable of iterable of ints): an iterable of n-dimensional vectors
where each vector has lengh n and component values between 0 and 2**iterator - 1.
*/
List<List<int>> points = new List<List<int>>();
foreach (int i in distances)
{
if (i > this.max_height)
{
throw new ArgumentException($"All values in distances must be <= 2^(iterator * dimention)-1 ({Math.Pow(2, this.iterator * this.dimention) - 1})");
}
else if (i < this.min_height)
{
throw new ArgumentException($"All values in distances must be >= {this.min_height}");
}
}
foreach(int i in distances)
{
points.Add(this.Point_from_distance(i));
}
return points;
}
public int Distance_from_point(List<int> point)
{
/*Return distance along the hilbert curve for a given point.
Args:
point (iterable of ints): an n-dimensional vector where each component value
is between 0 and 2**iterator - 1.
Returns:
distance (int): integer distance along hilbert curve
*/
int m = 1 << (this.iterator - 1);
int q = m; int p; int t;
int distance;
while (q > 1)
{
p = q - 1;
for (int i = 0; i < this.dimention; i++)
{
if (Convert.ToBoolean(q & point[i]))
{
point[0] ^= p;
}
else
{
t = (point[0] ^ point[i]) & p;
point[0] ^= t;
point[i] ^= t;
}
}
q >>= 1;
}
for (int i = 1; i < this.dimention; i++)
{
point[i] ^= point[i - 1];
}
t = 0;
q = m;
while (q > 1)
{
if (Convert.ToBoolean(q & point[this.dimention - 1]))
{
t ^= q - 1;
}
q >>= 1;
}
for (int i = 0; i < this.dimention; i++)
{
point[i] ^= t;
}
distance = this.Transpose_to_hilbert_integer(point);
return distance;
}
public List<int> Distances_from_points(List<List<int>> points)
{
List<int> distances = new List<int>();
foreach(List<int> i in points)
{
if (i.Count != this.dimention)
{
throw new ArgumentException($"All vectors in points must have length {this.dimention} ({Math.Pow(2, this.iterator * this.dimention) - 1})");
}
foreach (int j in i)
{
if (j > this.max_x)
{
throw new ArgumentException($"All coordinate values in all vectors in points must be <= 2^iterator-1");
}
else if (j < this.min_x)
{
throw new ArgumentException($"All coordinate values in all vectors in points must be > {this.min_x}");
}
}
}
foreach (List<int> i in points)
{
distances.Add(this.Distance_from_point(i));
}
return distances;
}
}
}