From f8420e0a46e1069220dcd642bca30c3fcb8c1b91 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 2 Apr 2016 13:00:29 +0200 Subject: Don't update xsections if unnecessary. --- src/physics.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- src/physics.h | 1 + src/renderer.c | 13 ++++++++++++- src/renderer.h | 6 +++++- src/ui.c | 11 +++-------- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/physics.c b/src/physics.c index 3981258..04c655c 100644 --- a/src/physics.c +++ b/src/physics.c @@ -34,6 +34,8 @@ void phy_sim_reset(phy_sim *sim) sim->time = 0; sim->running = false; + + sim->frame_index = get_time(); } int phy_sim_create(phy_sim *sim) @@ -45,8 +47,8 @@ int phy_sim_create(phy_sim *sim) fi = &sim->field_info; - fi->width = 100; - fi->height = 100; + fi->width = 50; + fi->height = 50; fi->depth = 50; fi->spacing = 1.5e10f / max3(fi->width, fi->height, fi->depth); sim->time_delta = 0.1; @@ -171,45 +173,58 @@ void phy_sim_step_H(phy_field_info *fi, float *mul_const, float dt, } } -void phy_sim_add_sources(phy_sim *sim) +void phy_sim_add_sources_E(phy_sim *sim) { phy_field_info *fi = &sim->field_info; float *E; size_t x, y, z; - for (z = 1; z < fi->depth - 1; z++) - for (y = 15; y < fi->height - 15; y++) - for (x = 1; x < fi->width - 1; x++) { - float s; + x = fi->width / 2; + y = fi->height / 2; + z = fi->depth / 2; - E = sim->fields[2].E + z * fi->zstr + y * fi->ystr + x * fi->xstr; + E = sim->fields[2].E + z * fi->zstr + y * fi->ystr + x * fi->xstr; + E[0] += exp(-0.1f * pow(sim->time - 5.0f, 2)) * 5; +} - s = exp(-600.0f * pow((float)x / fi->width - 0.15f, 2)); - s *= sin(sim->time); - //s *= exp(-2.0f * pow(sim->time - 2.0f, 2)) * 1000; - E[0] += s; - } +void phy_sim_add_sources_H(phy_sim *sim) +{ + phy_field_info *fi = &sim->field_info; + float *H; + size_t x, y, z; + + x = fi->width / 2; + y = fi->height / 2; + z = fi->depth / 2; + + H = sim->fields[2].H + z * fi->zstr + y * fi->ystr + x * fi->xstr; + H[1] -= exp(-0.1f * pow(sim->time - 4.5f, 2)) * 5; } void phy_sim_step(phy_sim *sim) { phy_field_info *fi = &sim->field_info; phy_field_em *fields = sim->fields, tmp; - // note: only pointers are rotated, not the entire fields + + phy_sim_step_E(fi, sim->field_mu, sim->time_delta, + fields[2].E, fields[0].E, fields[1].H); + phy_sim_add_sources_E(sim); + phy_sim_step_H(fi, sim->field_eps, sim->time_delta, + fields[2].H, fields[0].H, fields[1].E); + phy_sim_add_sources_H(sim); + SDL_mutexP(sim->rotate_lock); + + // note: only pointers are rotated, not the entire fields memcpy(&tmp, fields, sizeof(phy_field_em)); memcpy(fields, fields + 1, sizeof(phy_field_em)); memcpy(fields + 1, fields + 2, sizeof(phy_field_em)); memcpy(fields + 2, &tmp, sizeof(phy_field_em)); - SDL_mutexV(sim->rotate_lock); + sim->time += sim->time_delta; - phy_sim_step_H(fi, sim->field_eps, sim->time_delta, - fields[2].H, fields[0].H, fields[1].E); - phy_sim_step_E(fi, sim->field_mu, sim->time_delta, - fields[2].E, fields[0].E, fields[1].H); - phy_sim_add_sources(sim); + sim->frame_index = get_time(); - sim->time += sim->time_delta; + SDL_mutexV(sim->rotate_lock); } void phy_run_command(phy_sim *sim, int number, void *data) diff --git a/src/physics.h b/src/physics.h index 92ec6fd..1ce2a9c 100644 --- a/src/physics.h +++ b/src/physics.h @@ -35,6 +35,7 @@ typedef struct { bool running; itc_chan ctl; SDL_mutex *rotate_lock; + int64_t frame_index; // to avoid re-drawing the same fields } phy_sim; void phy_sim_destroy(phy_sim *sim); diff --git a/src/renderer.c b/src/renderer.c index 3c403a0..fb1647b 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -360,13 +360,21 @@ void r_xsection_destroy(r_xsection *xsection) } int r_xsection_update(r_window *rw, r_xsection *xsection, - phy_field_info *fi, float *field, + phy_field_info *fi, float *field, int64_t frame_index, r_xsection_type type, float frac) { size_t width, height, x, y, z; uint8_t *pixels; int pitch; + if (xsection->frame_index && + xsection->frame_index == frame_index && + xsection->last_frac == frac && + xsection->last_type == type) + return 0; // nothing's changed + + printf("r_xsection_update: tick %ld\n", frame_index); + switch (type) { case XSECTION_XY: width = fi->width; @@ -462,6 +470,9 @@ int r_xsection_update(r_window *rw, r_xsection *xsection, xsection->aspect_ratio = (float)width / height; + xsection->last_frac = frac; + xsection->last_type = type; + xsection->frame_index = frame_index; return 0; } diff --git a/src/renderer.h b/src/renderer.h index 0c38e6d..ce373df 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -49,12 +49,16 @@ typedef enum { typedef struct { SDL_Texture *texture; float aspect_ratio; + + r_xsection_type last_type; + float last_frac; + int64_t frame_index; } r_xsection; void r_xsection_create(r_xsection *xsection); void r_xsection_destroy(r_xsection *xsection); int r_xsection_update(r_window *rw, r_xsection *xsection, - phy_field_info *fi, float *field, + phy_field_info *fi, float *field, int64_t frame_index, r_xsection_type type, float frac); void r_xsection_draw(r_window *rw, r_xsection *xsection, float x, float y, float w, float h); diff --git a/src/ui.c b/src/ui.c index 72a7c7b..700950b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -141,11 +141,6 @@ void ui_infof(ui_window *uiw, const char *fmt, ...) uiw->simview.info_time = get_time(); } -static void ui_simview_set_select(ui_simview *sv, vec2_t sel_2d) -{ - -} - void ui_event_window(SDL_Event *event, ui_window *uiw) { ui_simview *sv = &uiw->simview; @@ -300,8 +295,9 @@ void ui_draw_simview_xsection(ui_window *uiw, float *field) vec2_t origin_s, scale_s; SDL_mutexP(sim->rotate_lock); - r_xsection_update(uiw->rw, &sv->xsection, &sim->field_info, - field, sv->xsection_type, sv->xsection_frac); + r_xsection_update(uiw->rw, &sv->xsection, &sim->field_info, field, + sim->frame_index, sv->xsection_type, + sv->xsection_frac); SDL_mutexV(sim->rotate_lock); aspect_ratio = sv->xsection.aspect_ratio; @@ -326,7 +322,6 @@ void ui_draw_simview_xsection(ui_window *uiw, float *field) void ui_draw_simview_selection(ui_window *uiw) { ui_simview *sv = &uiw->simview; - phy_sim *sim = sv->sim; vec2_t select_s; if (sv->selecting) -- cgit