-
Notifications
You must be signed in to change notification settings - Fork 1
/
paper.jl
223 lines (180 loc) · 4.95 KB
/
paper.jl
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
# 0 is black, 1 is white
# the paper extends from (0,0) to (1,1)
# the underlying image is a function from position and incident light to
# brightness. the need for the light is something of a hack - it allows
# us to calculate the effect of folds on brightness even when we're
# still unsure whether we're evaluating the brightness of the paper (or
# are outside, in the background).
const border_shadow = 0.005
const border = 0.05 # less than shadow as entire image shrinks from folds
const plain_shade = 0.5
function to_bottom_corner(x)
if 0 < x < 1
return 0
elseif x > 1
return 1 - x
else
return x
end
end
function undistorted(xy, light)
x, y = viewport(xy)
if 0 < x < 1 && 0 < y < 1
return min(1, max(0, light))
elseif x < 0 || y < 0
return 1
else
x = to_bottom_corner(x)
y = to_bottom_corner(y)
r = sqrt(x^2 + y^2)
return min(1, r / border_shadow)
end
end
# this transform shifts the paper (originally also in the unit square)
# and border (originally outside) into the unit square (it just makes
# things neater if we can use rand() at the top level).
function viewpoint(x)
return x * (1 + 2 * border) - border
end
function viewport(xy)
x, y = xy
return (viewpoint(x), viewpoint(y))
end
# now the meat. given a fold (position and angle), transform the x, y
# coordinates and the incident light. these are just nice-looking
# approximations - there's no real physics here.
function distance(xy, fold)
x, y = xy
(p, q), theta = fold
return (x - p) * sin(theta) - (y - q) * cos(theta)
end
const n_folds = 200
const fold_width = 0.04
const fold_shift = 0.15 / n_folds
const fold_grey = 0.15
function normalize(d)
d = d / fold_width
if d > 1
return 1
elseif d < -1
return -1
else
return sign(d) * abs(d) ^ 1.5
end
end
function shift(xy, theta, d)
x, y = xy
return (x + d * fold_shift * sin(theta), y - d * fold_shift * cos(theta))
end
function shade(g, theta, d)
return g * (1 + (1 - abs(d)) * sign(d) * cos(theta + pi/3) * fold_grey)
end
# a stream of random folds.
function random_folds()
while true
produce(((rand(), rand()), pi * rand()))
end
end
# take 'n' folds from the stream, and an underlying function (the paper)
# and create a new function that accumulates the effect of all the folds.
# an alternative would be to make an explicit iteration over the folds
# (i have no idea which is faster, but this seems neater).
function combine_folds(n, folds, base)
if n == 0
return base
else
fold = consume(folds)
pq, theta = fold
function wrapped(xy, light)
d = normalize(distance(xy, fold))
xy = shift(xy, theta, d)
return base(xy, shade(light, theta, d))
end
return combine_folds(n-1, folds, wrapped)
end
end
# to render the image we evaluate it and random points.
typealias Point (Float64, Float64)
typealias Result (Point, Float64)
function random_point()
return (rand(), rand())
end
function random_points()
while true
produce(random_point())
end
end
function take(n, seq)
function inner()
for i = 1:n
produce(consume(seq))
end
end
return Task(inner)
end
const block_size = 10000
function block_eval(seed, image)
a = Array(Result, block_size)
srand(seed)
for i = 1:block_size
point = random_point()
a[i] = (point, image(point, plain_shade))
end
return a
end
function driver(n, image, output)
blocks = div(n, block_size)
for block in map(seed -> block_eval(seed, image), 1:blocks)
for result in block
output(result...)
end
end
end
function pdriver(n, image, output)
blocks = div(n, block_size)
for block in pmap(seed -> block_eval(seed, image), 1:blocks)
for result in block
output(result...)
end
end
end
# testing
function print_output(xy, g)
x, y = xy
println("$x $y $g")
end
#driver(20, undistorted, print_output)
# output to cairo pdf
using Cairo
const line_width = 0.5
function set_line_cap(ctx::CairoContext, style)
ccall((:cairo_set_line_cap, "libcairo"), Void, (Ptr{Void},Int), ctx.ptr, style)
end
function cairo_pdf(file, size)
s = CairoPDFSurface(file, size, size)
c = CairoContext(s)
# set_line_width(c, line_width)
set_line_cap(c, 1)
set_source_rgb(c, 0, 0, 0)
function output(xy, g)
if true || g < rand()
# set_source_rgb(c, g, g, g)
set_line_width(c, line_width * (1 - g))
x, y = xy
move_to(c, x * size, y * size)
close_path(c)
# rel_line_to(c, line_width/sqrt(2), line_width/sqrt(2))
stroke(c)
end
end
for x = 0:1
for y = 0:1
output((x,y), 0)
end
end
function close()
stroke(c)
finish(s)
end
return (output, close)
end