summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: dd281241dcc86026fba5686413de0023029fb3a6 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
mod poly;

use poly::Poly;

use std::ffi::{CStr, CString};

use gl::types::*;

pub struct Bezier {
    pub degree: usize,
    pub vx: Vec<f32>,
    pub vy: Vec<f32>,
    pub px: Poly,
    pub py: Poly,
    pub dpx: Poly,
    pub dpy: Poly,
}

impl Bezier {
    pub fn new() -> Bezier {
        Bezier {
            vx: Vec::<f32>::new(),
            vy: Vec::<f32>::new(),
            px: Poly::new(vec![]),
            py: Poly::new(vec![]),
            dpy: Poly::new(vec![]),
            dpx: Poly::new(vec![]),
            degree: 0,
        }
    }

    pub fn push(&mut self, x: i32, y: i32) {
        self.vx.push(x as f32);
        self.vy.push(y as f32);
        self.px = to_poly(&self.vx);
        self.py = to_poly(&self.vy);
        self.dpx = self.px.deriv();
        self.dpy = self.py.deriv();
        self.degree += 1;
    }

    pub fn remove(&mut self, index: usize) {
        self.vx.remove(index);
        self.vy.remove(index);
        self.px = to_poly(&self.vx);
        self.py = to_poly(&self.vy);
        self.dpx = self.px.deriv();
        self.dpy = self.py.deriv();
        self.degree -= 1;
    }

    fn closer(&self, x: f32, y: f32) -> usize {
        let mut best = 10000.0;
        let mut best_index = 0;
        for i in 0..self.degree {
            let candidate = ((x - self.vx[i]).powi(2) + (y - self.vy[i]).powi(2)).sqrt();
            if candidate < best {
                best = candidate;
                best_index = i;
            }
        }
        best_index
    }

    pub fn grab_closer(&mut self, x: f32, y: f32) {
        let closer = self.closer(x, y);
        self.vx[closer] = x;
        self.vy[closer] = y;
        self.px = to_poly(&self.vx);
        self.py = to_poly(&self.vy);
        self.dpx = self.px.deriv();
        self.dpy = self.py.deriv();
    }

    fn show_x(&self) -> String {
        let mut s = String::new();
        for i in 0..self.degree {
            s.push_str(&self.vx[i].to_string());
            if i < self.degree - 1 {
                s.push_str(", ");
            }
        }
        s
    }

    fn show_y(&self) -> String {
        let mut s = String::new();
        for i in 0..self.degree {
            s.push_str(&self.vy[i].to_string());
            if i < self.degree - 1 {
                s.push_str(", ");
            }
        }
        s
    }

    pub fn draw(&self) {
        let vertex_shader_source = b"
        #version 410 core
        layout (location = 0) in vec3 aPos;
    
        void main() {
            gl_Position = vec4(aPos.xyz, 1.0);
        }
        "
        .to_vec();
    
        let fragment_shader_source = format!(
            "
        #version 410 core
        out vec4 FragColor;
    
        float vx[{s}] = float[{s}]({vx});
        float vy[{s}] = float[{s}]({vy});
    
        float px[{sx}] = float[{sx}]({px});
        float py[{sy}] = float[{sy}]({py});
    
        float dpx[{dsx}] = float[{dsx}]({dpx});
        float dpy[{dsy}] = float[{dsy}]({dpy});
    
        float thr = 16;
        float step_size = 5;
        float inf = 1000000;
    
        float eval_px(float t) {{
            float result = 0.0;
            for (int i = 0; i < {sx}; i++) {{
              result += px[i] * pow(t, i);
            }}
            return result;
        }}
    
        float eval_py(float t) {{
            float result = 0.0;
            for (int i = 0; i < {sy}; i++) {{
              result += py[i] * pow(t, i);
            }}
            return result;
        }}
    
        float eval_dpx(float t) {{
            float result = 0.0;
            for (int i = 0; i < {dsx}; i++) {{
              result += dpx[i] * pow(t, i);
            }}
            return result;
        }}
    
        float eval_dpy(float t) {{
            float result = 0.0;
            for (int i = 0; i < {dsy}; i++) {{
              result += dpy[i] * pow(t, i);
            }}
            return result;
        }}
    
        float control(vec2 pos) {{
          float closest = inf;
          for (int i = 0; i < {s}; i++) {{
            float d = distance(pos, vec2(vx[i], vy[i]));
            if (d < closest) {{
              closest = d;
            }}
          }}
          return 1 - closest / thr;
        }}
    
        float inside(vec2 pos) {{
          float closest = inf;
          float t = 0.0;
          while (t < 1.0) {{
            float dstep = step_size / length(vec2(eval_dpx(t), eval_dpy(t)));
            float step = min(t + dstep, 1.0);
            vec2 a = vec2(eval_px(t), eval_py(t));
            vec2 b = vec2(eval_px(step), eval_py(step));
            float c = pow(length(a - b), 2);
            float d = distance(pos, a + clamp(dot(pos - a, b - a) / c, 0, 1) * (b - a));
            if (d < closest) {{
              closest = d;
            }}
            t = step;
          }}
          return 1 - closest / thr;
        }}
    
        void main() {{
          float r = inside(gl_FragCoord.xy);
          float c = control(gl_FragCoord.xy);
          FragColor = vec4(r, c, 0.0, 1.0);
        }}
        ",
            s = self.degree,
            vx = self.show_x(),
            vy = self.show_y(),
            px = &self.px,
            py = &self.py,
            sx = self.px.degree() + 1,
            sy = self.py.degree() + 1,
            dpx = &self.dpx,
            dpy = &self.dpy,
            dsx = self.dpx.degree() + 1,
            dsy = self.dpy.degree() + 1,
        )
        .into_bytes();
    
        let shader_program: GLuint;
        unsafe {
            let vertex_shader = shader_from_source(
                &CString::from_vec_unchecked(vertex_shader_source),
                gl::VERTEX_SHADER,
            );
            let fragment_shader = shader_from_source(
                &CString::from_vec_unchecked(fragment_shader_source),
                gl::FRAGMENT_SHADER,
            );
            shader_program = gl::CreateProgram();
            gl::AttachShader(shader_program, vertex_shader);
            gl::AttachShader(shader_program, fragment_shader);
            gl::LinkProgram(shader_program);
            gl::DeleteShader(vertex_shader);
            gl::DeleteShader(fragment_shader);
            gl::UseProgram(shader_program);
        }
    }
}

fn to_poly(v: &Vec<f32>) -> Poly {
    match v.len() {
        0 => Poly::new(vec![0.0]),
        1 => Poly::new(vec![v[0]]),
        2 => Poly::new(vec![v[0], v[1] - v[0]]),
        _ => {
            let mut pv = Vec::new();
            for i in 0..v.len() - 1 {
                pv.push(Poly::new(vec![v[i], v[i + 1] - v[i]]));
            }
            while pv.len() > 1 {
                let mut npv = Vec::new();
                for i in 0..pv.len() - 1 {
                    let c = &pv[i + 1] - &pv[i];
                    npv.push(&pv[i] + &c.shift());
                }
                pv = npv;
            }
            pv[0].clone()
        }
    }
}

fn shader_from_source(source: &CStr, shader_type: GLenum) -> GLuint {
    unsafe {
        let shader: GLuint = gl::CreateShader(shader_type);
        gl::ShaderSource(shader, 1, &source.as_ptr(), std::ptr::null());
        gl::CompileShader(shader);
        let mut success: gl::types::GLint = 1;
        gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success);
        if success == 0 {
            let mut length: GLint = 0;
            gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut length);
            let mut buffer: Vec<u8> = Vec::with_capacity(length as usize + 1);
            buffer.extend([b' '].iter().cycle().take(length as usize));
            let error: CString = CString::from_vec_unchecked(buffer);
            gl::GetShaderInfoLog(shader, length, std::ptr::null_mut(), error.as_ptr() as *mut GLchar);
            println!("Compilation error: {}", error.to_str().unwrap());
        }
        shader
    }
}