-
Notifications
You must be signed in to change notification settings - Fork 1
/
svgtestc.pde
218 lines (190 loc) · 5.61 KB
/
svgtestc.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
import processing.svg.*;
import java.util.*;
MCorner[][] corners;
color [][] center;
int[][] domCount;
PImage test;
String imageToLoad = "t3.png";
void setup(){
size(1000, 1000);
noSmooth();
background(0);
File f = new File(dataPath(""));
for(var file:f.listFiles()){
if(file.getName().contains(".png")){
println(file.getName());
generateSVG(file.getAbsolutePath());
}
}
//todo:
// add contour cases for image edges within cells [x]
// generate contour from cells [x]
// - create contour polygons [x]
// - decimate useless vertexes.
// - create hierachy of polygon being 'enclosed' in other polygons for holes and polygons within polygons
// - some kind of color flood approach?
// - finally draw clean polygon with appropriate backward contours for holes
// export to svg [x]
}
void draw(){
}
void generateSVG(String imageloc){
test = loadImage(imageloc);
test.loadPixels();
corners = new MCorner[test.width][test.height];
center = new color[corners.length-1][corners[0].length-1];
domCount = new int[center.length][center[0].length];
BorderedImage bimg = new BorderedImage(test);
for (int i = 0; i<test.pixels.length; i++) {
int x = i % test.width;
int y = i / test.width;
corners[x][y] = new MCorner(bimg, x, y);
}
MCorner quad[] = new MCorner[4];
for (int x = 0; x<test.width-1; x++) {
for (int y = 0; y<test.height-1; y++) {
for (int z = 0; z<4; z++) {
int forw = (z+3)%4;
quad[z] = corners[x+corner[forw][0]][y+corner[forw][1]];
}
center[x][y] = getDominant(quad);
domCount[x][y] = countMostFrequentColor(quad);
}
}
float scl = 1;
//drawSquares(scl,corners,test,center,domCount);
fill(0,100);
rect(0,0,width,height);
var cells = generateCells(scl,corners,test,center,domCount);
var dvd = imageloc.split("\\\\");
PGraphics svg = createGraphics(test.width, test.height, SVG, "output/"+dvd[dvd.length-1].split("\\.")[0]+".svg");
svg.beginDraw();
svg.noStroke();
boolean[][] mapped = new boolean[test.width][test.height];
for (int x = 0; x<test.width-1; x++) {
for (int y = 0; y<test.height-1; y++) {
if(!mapped[x][y] && cells[x][y].hasRoute && alpha(test.pixels[x+y*test.width])>0){
var contour = getContour(cells,x,y,test.pixels[x+y*test.width],mapped);
if(contour!=null){
contour.draw(svg,scl);
contour.draw(g,scl);
}
}
}
}
svg.dispose();
svg.endDraw();
//image(svg,0,0);
translate(test.width,0);
}
Contour getContour(MSCell[][] cells, int x,int y, color c, boolean[][] mapped){
var origin = cells[x][y];
var current = origin;
int cdir = -1;
for(int i = 0;i<4;i++){
if(origin.route[i]!=-1 && origin.corners[(i+3)%4].c == c){
cdir = i;
}
}
if(cdir==-1){
return null;
}
int l = 0;
ArrayList<PVector> contour = new ArrayList();
while(true){
contour.add(current.getVert(cdir));
if(current == origin && l>1){
break;
}
if(current.routeStops[cdir]!=null){
contour.addAll(Arrays.asList(current.routeStops[cdir]));
}
for(int i = 0;i<4;i++){
if(current.corners[i].c == c){
mapped[current.corners[i].x][current.corners[i].y] = true;
}
}
cdir = current.route[cdir];
if(cdir==-1){
break;
}
//we went out of bound so traverse edge until we find a way back
if(!inBounds(current.x ,current.y ,cdir ,cells)){
int ax = current.x + dir[cdir][0];
int ay = current.y + dir[cdir][1];
contour.add(current.getAntiVert(cdir));
int dd = getBoundsDir(ax,ay,cells.length,cells[0].length);
while(true){
ax+= dir[dd][0];
ay+= dir[dd][1];
if(inBounds(ax ,ay ,(dd+1)%4 ,cells)){
var candidate = cells[ax + dir[(dd+1)%4][0]][ay + dir[(dd+1)%4][1]];
if(candidate.hasRoute && candidate.route[(dd+1)%4]!=-1){
current=candidate;
cdir=(dd+1)%4;
break;
}
}
int pd = dd;
dd = getBoundsDir(ax,ay,cells.length,cells[0].length);
if(dd!=pd){
contour.add(new PVector(constrain(ax,0,cells.length)+0.5,constrain(ay,0,cells.length)+0.5));
}
}
}else{
current = cells[current.x + dir[cdir][0]][current.y + dir[cdir][1]];
}
l++;
}
ArrayList<PVector> lcontour = new ArrayList();
for(int i = 0;i<contour.size();i++){
PVector prev = contour.get((i+contour.size()-1)%contour.size());
PVector curr = contour.get(i);
if(prev.dist(curr)<0.01 ){
continue;
}
lcontour.add(contour.get(i));
}
ArrayList<PVector> decimatedcontour = new ArrayList();
for(int i = 0;i<lcontour.size();i++){
PVector prev = lcontour.get((i+lcontour.size()-1)%lcontour.size());
PVector curr = lcontour.get(i);
PVector next = lcontour.get((i+1)%lcontour.size());
if(PVector.sub(curr,prev).normalize().dot(PVector.sub(next,curr).normalize()) < 0.9){
decimatedcontour.add(lcontour.get(i));
}
}
return new Contour(decimatedcontour,c);
}
int getBoundsDir(int x,int y, int w,int h){
if(y==-1 && x!= w){
return 0;
}
if(y!=h && x== w){
return 1;
}
if(y==h && x!= -1){
return 2;
}
if(y!=-1 && x== -1){
return 3;
}
return -1;
}
class Contour{
color c;
ArrayList<PVector> contour = new ArrayList();
Contour(ArrayList<PVector> contour, color c){
this.contour = contour;
this.c=c;
}
void draw(PGraphics pg, float scl){
pg.fill(c);
pg.noStroke();
pg.beginShape(POLYGON);
for(var v:contour){
pg.vertex(v.x*scl,v.y*scl);
}
pg.endShape(CLOSE);
}
}