-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dfft.rs
335 lines (318 loc) · 10.8 KB
/
dfft.rs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Inspriation: Daniel Shiffman's Coding Challenge: Drawing with Fourier Transform and Epicycles
//draw something when the windows is open then let the fourier cycle draw it :-)
// note that, my aim was not the exact path from the user input, I just wanted to see the fourier cycle in action in some random way but of course, it still
// some how follows the path of the user input :-)
use nannou::prelude::*;
use nannou_egui::{self, egui, Egui};
use std::f32::consts::PI;
use std::cell::RefCell;
fn main() {
nannou::app(model)
.update(update)
.run();
}
struct Model {
egui: Egui,
drawing_state: DrawingState,
user_drawing: Vec<Point2>,
fourier_data: Vec<FourierComponent>,
path: RefCell<Vec<Point2>>,
draw_speed: f32,
is_interacting_with_gui: bool,
stroke_weight: f32,
drawing_method: DrawingMethod,
fourier_drawing_method: FourierDrawingMethod,
scale: f32,
show_ui: bool,
}
#[derive(PartialEq)]
enum DrawingState {
UserDrawing,
FourierDrawing,
}
#[derive(Copy, Clone)]
enum DrawingMethod {
Line,
Ellipse,
}
#[derive(Copy,Clone)]
enum FourierDrawingMethod {
Line,
Ellipse,
}
struct FourierComponent {
amp: f32,
freq: f32,
phase: f32,
}
impl Model {
fn new(window_id: WindowId, app: &App) -> Self {
let window = app.window(window_id).unwrap();
let egui = Egui::from_window(&window);
Model {
egui,
drawing_state: DrawingState::UserDrawing,
user_drawing: Vec::new(),
fourier_data: Vec::new(),
path: RefCell::new(Vec::new()),
draw_speed: 1.0,
is_interacting_with_gui: false,
stroke_weight: 2.0,
drawing_method: DrawingMethod::Line,
fourier_drawing_method: FourierDrawingMethod::Line,
scale:1.0,
show_ui: true,
}
}
}
fn model(app: &App) -> Model {
let window_id = app.new_window().view(view).raw_event(raw_window_event).build().unwrap();
Model::new(window_id, app)
}
fn update(app: &App, model: &mut Model, update: Update) {
let egui = &mut model.egui;
egui.set_elapsed_time(update.since_start);
if app.keys.down.contains(&Key::H) {
model.show_ui = !model.show_ui;
}
if model.drawing_state == DrawingState::UserDrawing && !model.is_interacting_with_gui && app.mouse.buttons.left().is_down() {
let mouse_pos = app.mouse.position();
if model.user_drawing.is_empty() || (model.user_drawing.last().unwrap().distance(mouse_pos) > 1.0) {
model.user_drawing.push(mouse_pos);
}
}
model.is_interacting_with_gui = model.egui.ctx().is_pointer_over_area();
let ctx = model.egui.begin_frame();
egui::Window::new("Control Panel").show(&ctx, |ui| {
if ui.button("Compute Fourier Transform").clicked() {
model.drawing_state = DrawingState::FourierDrawing;
model.fourier_data = compute_dft(&model.user_drawing);
model.path.borrow_mut().clear();
}
if ui.button("Reset Drawing").clicked() {
model.drawing_state = DrawingState::UserDrawing;
model.user_drawing.clear();
model.fourier_data.clear();
model.path.borrow_mut().clear();
}
ui.add(egui::Slider::new(&mut model.draw_speed, 0.0..=1.0).text("Speed"));
ui.add(egui::Slider::new(&mut model.stroke_weight, 0.1..=10.0).text("Thickness"));
ui.horizontal(|ui| {
ui.label("User Drawing Method: ");
if ui.button("Line").clicked() {
model.drawing_method = DrawingMethod::Line;
}
if ui.button("Ellipse").clicked() {
model.drawing_method = DrawingMethod::Ellipse;
}
});
ui.horizontal(|ui| {
ui.label("Fourier Drawing Method: ");
if ui.button("Line").clicked() {
model.fourier_drawing_method = FourierDrawingMethod::Line;
}
if ui.button("Ellipse").clicked() {
model.fourier_drawing_method = FourierDrawingMethod::Ellipse;
}
});
});
}
fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw().scale(model.scale);
draw.background().color(BLACK);
match model.drawing_state {
DrawingState::UserDrawing => draw_user_input(&draw, &model.user_drawing, model.drawing_method),
DrawingState::FourierDrawing => {
let mut path = model.path.borrow_mut();
let cycle_complete = app.time % (2.0 * PI) < 0.01;
if cycle_complete && !path.is_empty() {
path.clear();
}
if !cycle_complete {
draw_fourier_cycloids(&draw, &model.fourier_data, &mut path, app.time, model.draw_speed,model.stroke_weight, model.fourier_drawing_method);
}
},
}
draw.to_frame(app, &frame).unwrap();
if model.show_ui {
model.egui.draw_to_frame(&frame).unwrap();
}
if app.keys.down.contains(&Key::Space) {
let file_path = app
.project_path()
.expect("failed to locate project directory")
.join("frames")
.join(format!("{:0}.png", app.elapsed_frames()));
app.main_window().capture_frame(file_path);
}
}
fn draw_user_input(draw: &Draw, points: &[Point2], drawing_method: DrawingMethod) {
match drawing_method {
DrawingMethod::Line => {
if points.len() > 1 {
for window in points.windows(2) {
draw.line()
.start(window[0])
.end(window[1])
.color(WHITE);
}
}
},
DrawingMethod::Ellipse => {
for point in points {
draw.ellipse()
.x_y(point.x, point.y)
.radius(1.0)
.color(WHITE);
}
},
}
}
fn compute_dft(points: &[Point2]) -> Vec<FourierComponent> {
let n = points.len() as f32;
let mut fourier_components = Vec::new();
let n_half = (n as isize) / 2;
for k in -n_half..=n_half {
let mut sum = Complex::new(0.0, 0.0);
for (i, point) in points.iter().enumerate() {
let angle = (2.0 * PI * k as f32 * i as f32) / n;
let c = Complex::from_polar(1.0, -angle);
sum = sum + c * Complex::new(point.x, point.y);
}
sum = sum / n;
fourier_components.push(FourierComponent {
amp: sum.norm(),
freq: k as f32,
phase: sum.arg(),
});
}
fourier_components.sort_by(|a, b| b.amp.partial_cmp(&a.amp).unwrap());
fourier_components
}
fn draw_fourier_cycloids(draw: &Draw, fourier_data: &[FourierComponent], path: &mut Vec<Point2>, time: f32, speed: f32,stroke_weight: f32, fourier_drawing_method: FourierDrawingMethod) {
if fourier_data.is_empty() {
return;
}
let mut x = 0.0;
let mut y = 0.0;
for (index, comp) in fourier_data.iter().enumerate() {
let prev_x = x;
let prev_y = y;
x += comp.amp * (comp.freq * time * speed + comp.phase).cos();
y += comp.amp * (comp.freq * time * speed + comp.phase).sin();
let transparency = if index < 2 { 0.0 } else { 1.0 };
draw.line()
.start(pt2(prev_x, prev_y))
.end(pt2(x, y))
.color(rgba(1.0, 1.0, 1.0, transparency));
let hue = comp.freq / fourier_data.len() as f32;
draw.ellipse()
.x_y(prev_x, prev_y)
.radius(comp.amp)
.no_fill()
.stroke_color(hsla(hue, 0.8, 0.5, 0.8 * transparency))
.stroke_weight(1.0);
}
path.push(pt2(x, y));
if time < 0.01 || (path.is_empty() || path.last() != Some(&pt2(x, y))) {
path.push(pt2(x, y));
}
if path.len() > 1 {
path.windows(2).enumerate().for_each(|(i, points)| {
let color = hsla(
i as f32 / path.len() as f32,
1.0,
0.5,
1.0,
);
match fourier_drawing_method {
FourierDrawingMethod::Line => {
draw.line()
.start(points[0])
.end(points[1])
.color(color)
.stroke_weight(stroke_weight);
},
FourierDrawingMethod::Ellipse => {
draw.ellipse()
.x_y(points[0].x, points[0].y)
.radius(stroke_weight)
.color(color);
},
}
});
}
}
#[derive(Copy, Clone)]
struct Complex {
re: f32,
im: f32,
}
impl Complex {
fn new(re: f32, im: f32) -> Self {
Complex { re, im }
}
fn norm(&self) -> f32 {
(self.re.powi(2) + self.im.powi(2)).sqrt()
}
fn arg(&self) -> f32 {
self.im.atan2(self.re)
}
fn from_polar(r: f32, theta: f32) -> Self {
Complex {
re: r * theta.cos(),
im: r * theta.sin(),
}
}
}
impl std::ops::Add for Complex {
type Output = Complex;
fn add(self, other: Complex) -> Complex {
Complex {
re: self.re + other.re,
im: self.im + other.im,
}
}
}
impl std::ops::Mul for Complex {
type Output = Complex;
fn mul(self, other: Complex) -> Complex {
Complex {
re: self.re * other.re - self.im * other.im,
im: self.re * other.im + self.im * other.re,
}
}
}
impl std::ops::Div<f32> for Complex {
type Output = Complex;
fn div(self, rhs: f32) -> Complex {
Complex {
re: self.re / rhs,
im: self.im / rhs,
}
}
}
fn raw_window_event(_app: &App, model: &mut Model, event: &nannou::winit::event::WindowEvent) {
model.egui.handle_raw_event(event);
if let nannou::winit::event::WindowEvent::MouseWheel { delta, .. } = event {
let cursor_over_egui = model.egui.ctx().wants_pointer_input();
if !cursor_over_egui {
match delta {
nannou::winit::event::MouseScrollDelta::LineDelta(_, y) => {
model.scale *= 1.0 + *y * 0.05;
model.scale = model.scale.max(0.1).min(10.0);
}
_ => (),
}
}
}
if let nannou::winit::event::WindowEvent::KeyboardInput { input, .. } = event {
if let (Some(nannou::winit::event::VirtualKeyCode::F), true) =
(input.virtual_keycode, input.state == nannou::winit::event::ElementState::Pressed)
{
let window = _app.main_window();
let fullscreen = window.fullscreen().is_some();
window.set_fullscreen(!fullscreen);
}
}
}