-
Notifications
You must be signed in to change notification settings - Fork 0
/
smvi.pde
277 lines (228 loc) · 6.73 KB
/
smvi.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
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
import java.util.Random;
final int MARGIN = 6;
final int SPACE_COLOR = 0;
final int MAX_COLOR_VAL = 255;
final String IMAGE_PATH = "image.jpg";
final int FUZZING_STEP = 3000;
final float TOLERANCE = 0.6;
final float THRESHOLD = 0.75;
Grid grid;
Manipulator manipulator;
void setup() {
size(512, 512);
grid = new Grid(MARGIN, new Color(SPACE_COLOR));
grid.loadImage_(IMAGE_PATH);
manipulator = new Manipulator(grid);
}
void draw() {
manipulator.update();
grid.draw();
}
void mouseClicked() {
manipulator.changeState();
}
class Color {
int red;
int green;
int blue;
Color(int initialVal) {
red = green = blue = initialVal;
}
Color(int initialRed, int initialGreen, int initialBlue) {
red = initialRed;
green = initialGreen;
blue = initialBlue;
}
Color copy() {
return new Color(red, green, blue);
}
}
enum CellType { AGENT, SPACE };
class Cell {
Color color_;
CellType type;
Cell(final Color initialColor) {
color_ = initialColor.copy();
if (color_.red >= (SPACE_COLOR - TOLERANCE*MAX_COLOR_VAL) && color_.red <= (SPACE_COLOR + TOLERANCE*MAX_COLOR_VAL) &&
color_.green >= (SPACE_COLOR - TOLERANCE*MAX_COLOR_VAL) && color_.green <= (SPACE_COLOR + TOLERANCE*MAX_COLOR_VAL) &&
color_.blue >= (SPACE_COLOR - TOLERANCE*MAX_COLOR_VAL) && color_.blue <= (SPACE_COLOR + TOLERANCE*MAX_COLOR_VAL)) {
type = CellType.SPACE;
} else {
type = CellType.AGENT;
}
}
}
class Grid {
int margin;
Color spaceColor;
Cell[] cells;
Grid(int initialMargin, final Color initialSpaceColor) {
cells = new Cell[width*height];
margin = initialMargin;
spaceColor = initialSpaceColor.copy();
for (int i = 0; i < cells.length; ++i) {
cells[i] = new Cell(initialSpaceColor);
}
}
void loadImage_(final String path) {
PImage img = loadImage(path);
for (int row = 0; row < height - 2*margin; ++row) {
for (int col = 0; col < width - 2*margin; ++col) {
int cell_idx = width*(row + margin) + (col + margin);
color c = img.get(col, row);
cells[cell_idx] = new Cell(new Color(int(red(c)), int(green(c)), int(blue(c))));
}
}
}
void draw() {
loadPixels();
for (int i = 0; i < cells.length; ++i) {
pixels[i] = color(cells[i].color_.red, cells[i].color_.green, cells[i].color_.blue);
}
updatePixels();
}
void swapCells(int index1, int index2) {
Cell tmpCell = cells[index1];
cells[index1] = cells[index2];
cells[index2] = tmpCell;
}
int[] getCellNeighbourIndex(int index) {
int left = getCellLeftNeighbourIndex(index);
int top = getCellTopNeighbourIndex(index);
int right = getCellRightNeighbourIndex(index);
int bottom = getCellBottomNeighbourIndex(index);
int topLeft = getCellLeftNeighbourIndex(top);
int topRight = getCellRightNeighbourIndex(top);
int bottomLeft = getCellLeftNeighbourIndex(bottom);
int bottomRight = getCellRightNeighbourIndex(bottom);
return new int[] { left, topLeft, top, topRight, right, bottomRight, bottom, bottomLeft };
}
int getCellLeftNeighbourIndex(int index) {
if (index % width == 0) {
return index + width - 1;
} else {
return index - 1;
}
}
int getCellRightNeighbourIndex(int index) {
if ((index + 1) % width == 0) {
return index - width + 1;
} else {
return index + 1;
}
}
int getCellTopNeighbourIndex(int index) {
if (index < width) {
return width*height - (width - index);
} else {
return index - width;
}
}
int getCellBottomNeighbourIndex(int index) {
if (index >= width*(height - 1)) {
return index - width*(height - 1);
} else {
return index + width;
}
}
}
class State {
Grid grid;
int step;
int pos = 0;
State(Grid initialGrid, int initialStep) {
grid = initialGrid;
step = initialStep;
}
void update() {
}
}
class Fuzzing extends State {
Fuzzing(Grid initialGrid, int initialStep) {
super(initialGrid, initialStep);
}
void update() {
Random rand = new Random();
for (int i = 0; i < step; ++i) {
int index1 = rand.nextInt(grid.cells.length);
int index2 = rand.nextInt(grid.cells.length);
grid.swapCells(index1, index2);
}
}
}
class Segregation extends State {
float threshold;
float tolerance;
IntList spaceCellsIndexes;
Segregation(Grid initialGrid, int initialStep, float initialThreshold, float initialTolerance) {
super(initialGrid, initialStep);
threshold = initialThreshold;
tolerance = initialTolerance;
spaceCellsIndexes = new IntList();
for (int i = 0; i < grid.cells.length; ++i) {
if (grid.cells[i].type == CellType.SPACE) {
spaceCellsIndexes.append(i);
}
}
}
boolean areColorsSimilar(final Color color1, final Color color2) {
int[] diffs = {
abs(color1.red - color2.red),
abs(color1.green - color2.green),
abs(color1.blue - color2.blue)
};
for (int i = 0; i < diffs.length; ++i) {
if (diffs[i] >= TOLERANCE*MAX_COLOR_VAL) {
return false;
}
}
return true;
}
boolean isCellSatisfied(int index) {
if (grid.cells[index].type == CellType.SPACE) {
return true;
}
int numOfNeighboursHavingSimilarColor = 0;
int[] neighbourIndexes = grid.getCellNeighbourIndex(index);
for (int i = 0; i < neighbourIndexes.length; ++i) {
if (areColorsSimilar(grid.cells[index].color_, grid.cells[neighbourIndexes[i]].color_)) {
++numOfNeighboursHavingSimilarColor;
}
}
return (float(numOfNeighboursHavingSimilarColor) / float(neighbourIndexes.length)) >= threshold;
}
void update() {
IntList unsatisfiedCellIndexes = new IntList();
for (int i = 0; i < grid.cells.length; ++i) {
if (isCellSatisfied(i) == false) {
unsatisfiedCellIndexes.append(i);
}
}
unsatisfiedCellIndexes.shuffle();
Random rand = new Random();
for (int i = 0; i < unsatisfiedCellIndexes.size(); ++i) {
int unsatisfiedIndex = unsatisfiedCellIndexes.get(i);
int spaceIndex = rand.nextInt(spaceCellsIndexes.size());
grid.swapCells(spaceCellsIndexes.get(spaceIndex), unsatisfiedIndex);
spaceCellsIndexes.set(spaceIndex, unsatisfiedIndex);
}
}
}
class Manipulator {
Grid grid;
State state;
Manipulator(Grid initialGrid) {
grid = initialGrid;
state = new State(grid, 0);
}
void update() {
state.update();
}
void changeState() {
if (state.getClass() == State.class || state.getClass() == Segregation.class) {
state = new Fuzzing(grid, FUZZING_STEP);
} else if (state.getClass() == Fuzzing.class) {
state = new Segregation(grid, 0, THRESHOLD, TOLERANCE);
}
}
}