-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCM.java
188 lines (177 loc) · 4.48 KB
/
SCM.java
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
public class SCM
implements Runnable
{
public static void main(final String[] args)
{
try
{
double epsilon=Double.parseDouble(args[0]);
double radius=Double.parseDouble(args[1]);
int density=Integer.parseInt(args[2]);
int threads=Integer.parseInt(args[3]);
new SCM(epsilon, radius, density, threads).run();
}
catch(final Exception ex)
{
System.out.println("Usage:\n\tjava SCM [parameters]\n"
+"Parameters:\n"
+"\tepsilon\n"
+"\trho\n"
+"\tdensity\n"
+"\tthreads\n"
);
}
}
public SCM(final double epsilon, final double radius, final int density, final int threads)
{
this.epsilon=epsilon;
this.radius=radius;
this.density=density;
this.threads=threads;
this.fn=String.format(java.util.Locale.US, "SCM-%1.3f.png", epsilon);
this.treshold=(int)(15.D*Math.log(256.D)/radius);
}
public void run()
{
startTime=System.currentTimeMillis();
final var stpe=threads>0?
java.util.concurrent.Executors.newWorkStealingPool(threads):
java.util.concurrent.Executors.newWorkStealingPool();
final var bi=new java.awt.image.BufferedImage(density, density, java.awt.image.BufferedImage.TYPE_BYTE_GRAY);
final var raster=bi.getRaster();
for(int i=0; i<density; i++)
{
final int i_=i;
stpe.submit(()->
{
for(int j=0;
2*j<density
|| 2*j==density && 2*i_<=density;
j++)
{
final int j_=j;
final double[] z=new double[]
{
2.D*Math.PI*i_/density-Math.PI,
2.D*Math.PI*(density-j_)/density-Math.PI,
};
final double value=Math.scalb(iterate(z), 8);
final double[] pixel=new double[] {value};
raster.setPixel(i_, j_, pixel);
if(density-i_>=0 && density-i_<density && density-j_>=0 && density-j_<density)
raster.setPixel(density-i_, density-j_, pixel);
}
progress();
});
}
stpe.submit(()->
{
for(int j=density/2+1; j<density; j++)
{
final int j_=j;
final double[] z=new double[]
{
-Math.PI,
2.D*Math.PI*(density-j_)/density-Math.PI,
};
final double value=Math.scalb(iterate(z), 8);
final double[] pixel=new double[] {value};
raster.setPixel(0, j_, pixel);
}
progress();
});
stpe.shutdown();
try
{
stpe.awaitTermination(24, java.util.concurrent.TimeUnit.HOURS);
}
catch(final InterruptedException e)
{
System.err.println(e);
}
try
{
javax.imageio.ImageIO.write(bi, "png", new java.io.File(fn));
final var duration=java.time.Duration.ofMillis(System.currentTimeMillis()-startTime);
System.out.printf("\u001B[2K\rFile written: “%s”: %d bytes, %02d:%02d:%02d spent\n",
fn,
java.nio.file.Files.size(java.nio.file.Path.of(fn)),
duration.toHoursPart(),
duration.toMinutesPart(),
duration.toSecondsPart()
);
}
catch(final java.io.IOException e)
{
System.err.println(e);
}
}
private static double modulo(double x, final double mod)
{
while(x<0.D)
x+=mod;
return x % mod;
}
private static double normalize(final double x)
{
return modulo(x+Math.PI, 2.D*Math.PI)-Math.PI;
}
private double[] map(final double[] z)
{
double x=z[0], y=z[1];
y+=(epsilon*Math.sin(x));
y=normalize(y);
x+=y;
x=normalize(x);
return new double[] {x, y};
}
private double dist(final double[] a, final double[] b)
{
double dx=Math.abs(a[0]-b[0]);
double dy=Math.abs(a[1]-b[1]);
return Math.hypot(Math.min(dx, 2.D*Math.PI-dx), Math.min(dy, 2.D*Math.PI-dy));
}
private double iterate(final double[] z)
{
double[] t=java.util.Arrays.copyOf(z, 2);
double[] h=java.util.Arrays.copyOf(z, 2);
int n=0;
while(true)
{
t=map(t);
h=map(map(h));
if(dist(t, h)<radius)
return Math.exp(-n*radius/15.D);
if(n>treshold)
return 0.D;
n++;
}
}
private void progress()
{
progress++;
final long currentTime=System.currentTimeMillis();
final long timeElapsed=currentTime-startTime;
final long timeRemained=progress==0?
Long.MAX_VALUE:
(long)(timeElapsed*((double)(density)/(double)(progress)-1.D));
final var eta=java.time.Duration.ofMillis(timeRemained);
System.out.printf("\u001B[2K\r%s: %.2f%%\tETA: %02d:%02d:%02d %s",
fn,
100.D*progress/density,
eta.toHoursPart(),
eta.toMinutesPart(),
eta.toSecondsPart(),
"|/-\\".charAt(progress%4)
);
System.out.flush();
}
private int density=1200;
private double radius=0.01D;
private double epsilon=1.D;
private int progress=0;
private final String fn;
private long startTime;
private int treshold;
private int threads;
}