Skip to content

Commit 6cd8421

Browse files
committed
working diff
1 parent 9d2512a commit 6cd8421

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

chalk/backend/cairo.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def render_segment(
8585
end = seg.angle + seg.dangle
8686

8787
for i in range(q.shape[0]):
88-
if tx.np.abs(dangle[i]) < 1:
88+
if tx.np.abs(dangle[i]) < 0.1:
8989
ctx.line_to(q[i, 0, 0], q[i, 1, 0])
9090
else:
9191
ctx.save()
@@ -221,5 +221,6 @@ def render(
221221
e = s.get_envelope()
222222
assert e is not None
223223
s = s.translate(e(-tx.unit_x), e(-tx.unit_y))
224+
ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD)
224225
render_cairo_prims(s, ctx, Style.root(max(width, height)))
225226
surface.write_to_png(path)

chalk/trail.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def circle(radius: Floating = 1.0, clockwise: bool = True) -> Trail:
190190
arc.arc_seg_angle(0, dangle).rotate_by(rotate_by * i / sides)
191191
for i in range(sides)
192192
]
193-
).close()
193+
).close().scale(radius)
194194

195195
@staticmethod
196196
def regular_polygon(sides: int, side_length: Floating) -> Trail:

examples/render.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from chalk.backend.cairo import ToList
1111
from chalk.trace import Trace
1212
import jax
13-
jax.config.update("jax_debug_nans", True)
13+
#jax.config.update("jax_debug_nans", True)
1414
# blue = Color("#005FDB")
1515
# d = rectangle(10, 10).rotate(30) #| rectangle(10, 10).align_t() | circle(50).align_t()
1616

@@ -22,12 +22,12 @@
2222
def make_scene(splits, mask, scene):
2323
split_int = np.floor(splits).astype(int)
2424
loc = np.arange(splits.shape[0]) % 2
25-
scene = scene.at[split_int * mask].set(2 * (1 - loc) - 1)
25+
scene = scene.at[split_int * mask].add(2 * (1 - loc) - 1)
2626
scene = scene.at[0].set(0)
2727
scene = np.cumsum(scene)
2828
scene = scene.at[split_int * mask].set(1.0 * (1-loc) + (2 * loc - 1) * ((splits) - split_int))
2929
scene = scene.at[0].set(0)
30-
out = jax.vmap(lambda s: scene[s + samples] @ gaussian_kernel)(np.arange(scene.shape[0]))
30+
out = jax.vmap(lambda s: scene[s + samples] @ kernel(0))(np.arange(scene.shape[0]))
3131
return scene, out
3232

3333
def f_fwd(x, mask, scene):
@@ -38,18 +38,21 @@ def f_bwd(res, g):
3838
_, g = g
3939
splits, mask, scene = res
4040
split_int = np.floor(splits).astype(int)
41-
def grad_p(s):
41+
def grad_p(s, s_off):
42+
off = s_off - s
4243
v = g[s + samples]
43-
return (v @ gaussian_kernel) * (scene[s-1] - scene[s+1])
44-
r = jax.vmap(grad_p)(split_int) * mask
44+
return (v @ kernel(off)) * (scene[s-1] - scene[s+1])
45+
r = jax.vmap(grad_p, in_axes=(0, 0))(split_int, splits) * mask
4546
return r, None, None
4647

4748
make_scene.defvjp(f_fwd, f_bwd)
4849

4950
kern = 11
5051
samples = np.arange(kern) - (kern//2)
51-
gaussian_kernel = np.exp(-((samples/2) **2))
52-
gaussian_kernel = gaussian_kernel / gaussian_kernel.sum()
52+
def kernel(offset):
53+
off_samples = samples - offset
54+
gaussian_kernel = kern - np.abs(off_samples)
55+
return np.maximum(0, gaussian_kernel / (kern - np.abs(samples)).sum())
5356

5457
def render_out(d, x=200, y=200):
5558
t = 4 * x
@@ -59,7 +62,7 @@ def color_row(i, counter):
5962
idx = i // (t // y)
6063
return make_scene(ps[i], m[i], counter)
6164
i = np.arange(1, ps.shape[0], 4)
62-
scene, out1 = jax.vmap(color_row, in_axes=(0, 0))(i, counter[i])
65+
scene, out1 = jax.vmap(color_row, in_axes=(0, 0))(i, counter[i // 4])
6366
# out1 = jax.vmap(lambda c:
6467
# jax.vmap(lambda s: c[s + samples] @ gaussian_kernel)(np.arange(x)))(counter)
6568
#out = out1[..., None] * np.array(color)

0 commit comments

Comments
 (0)