-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hilbert.pde
211 lines (181 loc) · 6.45 KB
/
Hilbert.pde
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
/**
* Class that sorts the pixels in the order of the hilbert curve.
* The functionality and the comments of the sortFunction and revSortFunction are based on the algorithm of princeton university: https://introcs.cs.princeton.edu/java/32class/Hilbert.java.html
* @author Finn Bayer 23-11-2017
**/
public class Hilbert {
/**
* The order of the hilbert curve. Is set by the final HILBERTORDER variable in the SeeWithEars class.
**/
private int hilbertOrder;
/**
* The state is used to determine which pixels is added next.
**/
private int state = 0;
/**
* The number of different states (left, right, up, down)
**/
private final int NUM_STATES = 4;
/**
* XValue.
**/
private int x;
/**
* YValue.
**/
private int y;
/**
* Used to copy the pixel to the right spot in the array
**/
private int counter;
/**
* The widht of the given image.
**/
private int picWidth;
/**
* Standard constructor.
* Sets the Hilbert Order.
* @param hilbertOrder The designated hilbert order
**/
public Hilbert(int hilbertOrder){
this.hilbertOrder = hilbertOrder;
}
/**
* Overhead of the recursive functions that are sorting the pixels according to the hilbert curve.
* @param pix The pixels of the image
* @param sortedPixels The array that stores the sorted Pixels of the image (has to be the same size like pix)
* @param x The x variable of the starting point of the hilbert curve
* @param y The y variable of the starting point of the hilbert curve
* @param picWidth The width of the given picture
* @return float[] The array of the sorted Pixels.
**/
public float[] hilbertSort(int[] pix, float[] sortedPixels, int x, int y, int picWidth) {
this.x = x;
this.y = y;
this.counter = 0;
this.picWidth = picWidth;
sortedPixels[0] = brightness(pix[y*picWidth+x]);
sortedPixels = sortPixels(pix,sortedPixels, hilbertOrder);
//switch the quadrants of the hilbert curve to match the criteria that higher spatial position is associated with higher pitch.
float[] backupArray = sortedPixels.clone();
int lowerRight = 3* sortedPixels.length/4;
int upperRight = 2*sortedPixels.length/4;
for (int upperLeft = sortedPixels.length/4; upperLeft <= sortedPixels.length/2 - 1; upperLeft++){
sortedPixels[upperRight] = backupArray[upperLeft];
sortedPixels[upperLeft] = backupArray[lowerRight];
sortedPixels[lowerRight] = backupArray[upperRight];
lowerRight++;
upperRight++;
}
return sortedPixels;
}
/**
* Recursive function that sorts the pixels.
* @param pix The pixels of the image
* @param sortedPixels The array that stores the sorted Pixels of the image (has to be the same size like pix)
* @param recDeep The depth of the recursion. Break condition.
* @return float[] The sorted pixel array
**/
private float[] sortPixels(int[] pix, float[] sortedPixels, int recDeep) {
// break condition
if (recDeep == 0) return sortedPixels;
//turnleft(+)
this.state = (this.state+1) % NUM_STATES;
//call revSortPixels
sortedPixels = revSortPixels(pix, sortedPixels, recDeep-1);
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//turnleft(-)
this.state = (this.state-1) % NUM_STATES;
if (this.state<0) this.state+=NUM_STATES;
//call sortPixels
sortedPixels = sortPixels(pix, sortedPixels, recDeep-1);
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//call sortPixels
sortedPixels = sortPixels(pix, sortedPixels, recDeep-1);
//turnleft(-)
this.state = (this.state-1) % NUM_STATES;
if (this.state<0) this.state+=NUM_STATES;
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//call revSortPixels
sortedPixels = revSortPixels(pix, sortedPixels, recDeep-1);
//turnleft(+)
this.state = (this.state+1) % NUM_STATES;
return sortedPixels;
}
/**
* The reverse sorting pixels algorithm.
* @param pix The pixels of the image
* @param sortedPixels The array that stores the sorted Pixels of the image (has to be the same size like pix)
* @param recDeep The depth of the recursion. Break condition.
* @return float[] The sorted pixel array
**/
private float[] revSortPixels(int[]pix, float[] sortedPixels, int recDeep) {
if (recDeep == 0) return sortedPixels;
//turnleft(-)
this.state = (this.state-1) % NUM_STATES;
if (this.state<0) this.state+=NUM_STATES;
//call sortPixels
sortedPixels = sortPixels(pix, sortedPixels, recDeep-1);
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//turnleft(+)
this.state = (this.state+1) % NUM_STATES;
//call revSortPixels
sortedPixels = revSortPixels(pix, sortedPixels, recDeep-1);
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//call revSortPixels
sortedPixels = revSortPixels(pix, sortedPixels, recDeep-1);
//turnleft(+)
this.state = (this.state+1) % NUM_STATES;
//goForward (add Element)
this.refreshXY();
this.counter += 1;
sortedPixels[this.counter] = brightness(pix[this.y*this.picWidth+this.x]);
//call sortPixels
sortedPixels = sortPixels(pix, sortedPixels, recDeep-1);
//turnleft(-)
this.state = (this.state-1) % NUM_STATES;
if (this.state<0) this.state+=NUM_STATES;
return sortedPixels;
}
/**
* Update the x and y coordinate depending of the state set by the sorting function.
**/
private void refreshXY() {
switch (this.state) {
case 0:
this.x += 1;
break;
case 1:
this.y -= 1;
break;
case 2:
this.x -= 1;
break;
case 3:
this.y += 1;
break;
}
}
/**
* Return the Hilbert Order that is used.
* @return int The hilbert order that is used.
**/
public int getHilbertOrder() {
return this.hilbertOrder;
}
}