-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
bezier.py
196 lines (166 loc) · 5.23 KB
/
bezier.py
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
# -----------------------------------------------------------------------------
# Python & OpenGL for Scientific Visualization
# www.labri.fr/perso/nrougier/python+opengl
# Copyright (c) 2018, Nicolas P. Rougier
# Distributed under the 2-Clause BSD License.
# -----------------------------------------------------------------------------
import sys
import ctypes
import numpy as np
from glumpy import app, gloo, gl
from curves import curve4_bezier
point_vertex = """
uniform vec2 resolution;
attribute vec2 center;
attribute float radius;
varying vec2 v_center;
varying float v_radius;
void main() {
v_radius = radius;
v_center = center;
gl_PointSize = 2.0 + ceil(2.0*radius);
gl_Position = vec4(2.0*center/resolution-1.0, 0.0, 1.0);
} """
point_fragment = """
vec4 outline(float distance, float linewidth, float antialias,
vec4 fg_color, vec4 bg_color)
{
vec4 frag_color;
float t = linewidth/2.0 - antialias;
float signed_distance = distance;
float border_distance = abs(signed_distance) - t;
float alpha = border_distance/antialias;
alpha = exp(-alpha*alpha);
if( border_distance < 0.0 )
frag_color = fg_color;
else if( signed_distance < 0.0 )
frag_color = mix(bg_color, fg_color, sqrt(alpha));
else {
if( abs(signed_distance) < (linewidth/2.0 + antialias) ) {
frag_color = vec4(fg_color.rgb, fg_color.a * alpha);
} else {
discard;
}
}
return frag_color;
}
varying vec2 v_center;
varying float v_radius;
void main() {
vec2 p = gl_FragCoord.xy - v_center;
gl_FragColor = outline(length(p)-v_radius, 1.0, 1.0,
vec4(0,0,0,1), vec4(1,1,1,1));
/*
float a = 1.0;
float d = length(p) - v_radius + 1.0;
if(d > 0.0) a = exp(-d*d);
gl_FragColor = vec4(vec3(0.0), a);
*/
} """
line_vertex = """
uniform vec2 resolution;
uniform float antialias;
uniform float thickness;
uniform float linelength;
attribute vec4 prev, curr, next;
varying vec2 v_uv;
void main() {
float w = thickness/2.0 + antialias;
vec2 p;
if (prev.xy == curr.xy) {
vec2 t1 = normalize(next.xy - curr.xy);
vec2 n1 = vec2(-t1.y, t1.x);
v_uv = vec2(-w, curr.z*w);
p = curr.xy - w*t1 + curr.z*w*n1;
} else if (curr.xy == next.xy) {
vec2 t0 = normalize(curr.xy - prev.xy);
vec2 n0 = vec2(-t0.y, t0.x);
v_uv = vec2(linelength+w, curr.z*w);
p = curr.xy + w*t0 + curr.z*w*n0;
} else {
vec2 t0 = normalize(curr.xy - prev.xy);
vec2 t1 = normalize(next.xy - curr.xy);
vec2 n0 = vec2(-t0.y, t0.x);
vec2 n1 = vec2(-t1.y, t1.x);
vec2 miter = normalize(n0 + n1);
float dy = w / dot(miter, n1);
v_uv = vec2(curr.w, curr.z*w);
p = curr.xy + dy*curr.z*miter;
}
gl_Position = vec4(2.0*p/resolution-1.0, 0.0, 1.0);
} """
line_fragment = """
uniform vec3 color;
uniform float antialias, thickness, linelength;
varying vec2 v_uv;
void main() {
float d = 0;
float w = thickness/2.0 - antialias;
// Cap at start (square)
if (v_uv.x < 0)
d = max(abs(v_uv.x),abs(v_uv.y)) - w;
// Cap at end (square)
else if (v_uv.x >= linelength)
d = max(abs(v_uv.x-linelength),abs(v_uv.y)) - w;
// Body
else
d = abs(v_uv.y) - w;
if( d < 0) {
gl_FragColor = vec4(color, 1.0);
} else {
d /= antialias;
gl_FragColor = vec4(color, exp(-d*d));
}
} """
window = app.Window(512, 512, color=(1,1,1,1))
@window.event
def on_resize(width, height):
curve["resolution"] = width, height
points["resolution"] = width, height
@window.event
def on_draw(dt):
window.clear()
gl.glDepthMask(gl.GL_FALSE)
curve["color"] = 0.75, 0.75, 0.75
curve["thickness"] = 64
curve.draw(gl.GL_TRIANGLE_STRIP)
curve["color"] = 0.0, 0.0, 0.0
curve["thickness"] = 1.5
curve.draw(gl.GL_TRIANGLE_STRIP)
points.draw(gl.GL_POINTS)
gl.glDepthMask(gl.GL_TRUE)
def bake(P, closed=False):
epsilon = 1e-10
n = len(P)
if closed and ((P[0]-P[-1])**2).sum() > epsilon:
P = np.append(P, P[0])
P = P.reshape(n+1,2)
n = n+1
V = np.zeros(((1+n+1),2,4), dtype=np.float32)
V_prev, V_curr, V_next = V[:-2], V[1:-1], V[2:]
V_curr[...,0] = P[:,np.newaxis,0]
V_curr[...,1] = P[:,np.newaxis,1]
V_curr[...,2] = 1,-1
L = np.cumsum(np.sqrt(((P[1:]-P[:-1])**2).sum(axis=-1))).reshape(n-1,1)
V_curr[1:,:,3] = L
if closed:
V[0], V[-1] = V[-3], V[2]
else:
V[0], V[-1] = V[1], V[-2]
return V_prev, V_curr, V_next, L[-1]
d = 64
# P = curve4_bezier((d, 3*d), (512+d, 512+d), (512+d, -d), (d, 512-3*d))
# P = curve4_bezier((d, d), (0,512) , (512,0), (512-d,512-d))
P = curve4_bezier((d, d), (d,512) , (512-d,512), (512-d,d))
print(len(P))
V_prev, V_curr, V_next, length = bake(P)
curve = gloo.Program(line_vertex, line_fragment)
curve["prev"], curve["curr"], curve["next"] = V_prev, V_curr, V_next
curve["antialias"] = 1.5
curve["linelength"] = length
points = gloo.Program(point_vertex, point_fragment, count=len(P))
points["center"] = P
points["radius"] = 3.5
points["radius"][0] = 5
points["radius"][-1] = 5
app.run()