Skip to content

Commit

Permalink
stable but cbed disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Chu-Ping committed Nov 7, 2023
1 parent 79456fa commit 404c945
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 179 deletions.
112 changes: 54 additions & 58 deletions src/Ricom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ Ricom::Ricom() : stem_max(-FLT_MAX), stem_min(FLT_MAX),
b_recompute_kernel(false), detector(),
kernel(),
offset{128, 128}, com_public{0.0, 0.0},
comx_data(), comy_data(),
ricom_data(),
stem_data(),
comx_image(), comy_image(),
ricom_image(),
stem_image(),
nx(1024), ny(1024), nxy(0),
rep(1), fr_total(0),
skip_row(1), skip_img(0),
Expand Down Expand Up @@ -294,16 +294,16 @@ void Ricom::draw_ricom_image(int y0, int ye)
}

// set pixel in ricom image srf_ricom at location idx, idy to value corresponding
// value in ricom_data array
// value in ricom_image array
void Ricom::set_ricom_pixel(int idx, int idy)
{
// determine location index of value in memory

int idr = idy * nx + idx;
if (ricom_data[idr] < ricom_min) {ricom_min = ricom_data[idr];}
if (ricom_data[idr] > ricom_max) {ricom_max = ricom_data[idr];}
if (ricom_image[idr] < ricom_min) {ricom_min = ricom_image[idr];}
if (ricom_image[idr] > ricom_max) {ricom_max = ricom_image[idr];}

float val = (ricom_data[idr] - ricom_min) / ((ricom_max - ricom_min));
float val = (ricom_image[idr] - ricom_min) / ((ricom_max - ricom_min));

// Update pixel at location
SDL_Utils::draw_pixel(srf_ricom, idx, idy, val, ricom_cmap);
Expand All @@ -326,8 +326,8 @@ void Ricom::draw_stem_image()
void Ricom::draw_stem_image(int y0, int ye)
{
if (y0==0) {
stem_min = stem_data[y0*nx];
stem_max = stem_data[y0*nx];
stem_min = stem_image[y0*nx];
stem_max = stem_image[y0*nx];
}

std::lock_guard<std::mutex> lock(stem_mutex);
Expand Down Expand Up @@ -367,23 +367,23 @@ void Ricom::draw_e_field_image()
}

// set pixel in stem image srf_stem at location idx, idy to value corresponding
// value in stem_data array
// value in stem_image array
void Ricom::set_stem_pixel(size_t idx, size_t idy)
{
// determine location index of value in memory
int idr = idy * nx + idx;

if (stem_data[idr] < stem_min) {stem_min = stem_data[idr];}
if (stem_data[idr] > stem_max) {stem_max = stem_data[idr];}
if (stem_image[idr] < stem_min) {stem_min = stem_image[idr];}
if (stem_image[idr] > stem_max) {stem_max = stem_image[idr];}

float val = (stem_data[idr] - stem_min) / (stem_max - stem_min);
float val = (stem_image[idr] - stem_min) / (stem_max - stem_min);

// Update pixel at location
SDL_Utils::draw_pixel(srf_stem, idx, idy, val, stem_cmap);
}

// set pixel in stem image srf_stem at location idx, idy to value corresponding
// value in stem_data array
// value in stem_image array
void Ricom::set_e_field_pixel(size_t idx, size_t idy)
{
// determine location index of value in memory
Expand Down Expand Up @@ -516,6 +516,8 @@ void Ricom::line_processor(
if ((int)(prog_mon->fr_count / nx) < preprocessor_line)
{
processor_line = (int)(prog_mon->fr_count) / nx;
if (processor_line%ny==0)
id_image = processor_line / ny % 2;
idxx = (int)(prog_mon->fr_count) % nxy;
*prog_mon += nx;

Expand All @@ -526,28 +528,25 @@ void Ricom::line_processor(
int idxx_p_i = idxx + i;
if ((idxx_p_i >= 0) | (nx > 1))
{
if (dose_data[idxx_p_i] == 0)
if (dose_data[id_image][idxx_p_i] == 0)
{
comx_data[idxx_p_i] = offset[0];
comy_data[idxx_p_i] = offset[1];
comx_image[idxx_p_i] = offset[0];
comy_image[idxx_p_i] = offset[1];
}
else
{
comx_data[idxx_p_i] = sumx_data[idxx_p_i] / dose_data[idxx_p_i];
comy_data[idxx_p_i] = sumy_data[idxx_p_i] / dose_data[idxx_p_i];
}
com_xy_sum[0] += comx_data[idxx_p_i];
com_xy_sum[1] += comy_data[idxx_p_i];
if (!b_cumulative) {
sumx_data[idxx_p_i] = 0;
sumy_data[idxx_p_i] = 0;
dose_data[idxx_p_i] = 0;
comx_image[idxx_p_i] = sumx_data[id_image][idxx_p_i] / dose_data[id_image][idxx_p_i];
comy_image[idxx_p_i] = sumy_data[id_image][idxx_p_i] / dose_data[id_image][idxx_p_i];
}
com_xy_sum[0] += comx_image[idxx_p_i];
com_xy_sum[1] += comy_image[idxx_p_i];
}
if (b_vSTEM)
stem_image[idxx_p_i] = (float)stem_data[id_image][idxx_p_i];
if (b_e_mag)
{
com_xy[0] = comx_data[idxx_p_i];
com_xy[1] = comy_data[idxx_p_i];
com_xy[0] = comx_image[idxx_p_i];
com_xy[1] = comy_image[idxx_p_i];
compute_electric_field(com_xy, idxx_p_i);
}
}
Expand All @@ -566,8 +565,6 @@ void Ricom::line_processor(
int update_line = idxx / nx - kernel.kernel_size*2;
if ((prog_mon->report_set) && (update_line)>0)
{
update_surfaces(update_line, frame);

fr_freq = prog_mon->fr_freq;
rescales_recomputes();
for (int i = 0; i < 2; i++)
Expand Down Expand Up @@ -628,7 +625,7 @@ void Ricom::icom_group_classical(int idxx)
for (int i_line = 0, idr_delay = idxx - kernel.kernel_size * nx;
i_line < nx;
i_line++, idr_delay++)
{ ricom_data[idr_delay]=0; }
{ ricom_image[idr_delay]=0; }


for (int iy = -kernel.kernel_size; iy <= kernel.kernel_size; iy++)
Expand All @@ -644,11 +641,11 @@ void Ricom::icom_group_classical(int idxx)
{
if (((idr_x + ix) >= 0) & ((idr_x + ix) < nx))
{
// ricom_data[idr] += ((comx_data[idc + ix] - offset[0]) * -kernel.kernel_x[idk] +
// (comy_data[idc + ix] - offset[1]) * -kernel.kernel_y[idk]);
// ricom_image[idr] += ((comx_image[idc + ix] - offset[0]) * -kernel.kernel_x[idk] +
// (comy_image[idc + ix] - offset[1]) * -kernel.kernel_y[idk]);

ricom_data[idr_delay] += ((comx_data[idc + ix] - offset[0]) * -kernel.kernel_x[idk] +
(comy_data[idc + ix] - offset[1]) * -kernel.kernel_y[idk]);
ricom_image[idr_delay] += ((comx_image[idc + ix] - offset[0]) * -kernel.kernel_x[idk] +
(comy_image[idc + ix] - offset[1]) * -kernel.kernel_y[idk]);
}
++idk;
}
Expand Down Expand Up @@ -683,17 +680,14 @@ void Ricom::run(int mode)
b_ricom,
b_e_mag,
b_airpi,
&b_cumulative,
&detector.radius2,
&offset,
&stem_data,
&ricom_data,
&comx_data,
&comy_data,
&dose_data,
&sumx_data,
&sumy_data,
&stem_data,
&frame,
&airpi_data,
&processor_line,
&preprocessor_line,
mode,
Expand All @@ -717,17 +711,14 @@ void Ricom::run(int mode)
b_ricom,
b_e_mag,
b_airpi,
&b_cumulative,
&detector.radius2,
&offset,
&stem_data,
&ricom_data,
&comx_data,
&comy_data,
&dose_data,
&sumx_data,
&sumy_data,
&stem_data,
&frame,
&airpi_data,
&processor_line,
&preprocessor_line,
mode,
Expand Down Expand Up @@ -765,10 +756,10 @@ void Ricom::reset_limits()

void Ricom::reinit_vectors_limits()
{
std::fill(ricom_data.begin(), ricom_data.end(), 0);
std::fill(stem_data.begin(), stem_data.end(), 0);
std::fill(comx_data.begin(), comx_data.end(), 0);
std::fill(comy_data.begin(), comy_data.end(), 0);
std::fill(ricom_image.begin(), ricom_image.end(), 0);
std::fill(stem_image.begin(), stem_image.end(), 0);
std::fill(comx_image.begin(), comx_image.end(), 0);
std::fill(comy_image.begin(), comy_image.end(), 0);
last_y = 0;
reset_limits();
}
Expand All @@ -781,10 +772,11 @@ void Ricom::reset()

// Initializations
nxy = nx * ny;
id_image = 0;
fr_total = nxy * rep;
fr_count = 0;
init_surface();

// Imaging tools
switch (camera)
{
Expand All @@ -805,15 +797,19 @@ void Ricom::reset()
detector.compute_detector(n_cam, n_cam, offset);

// Allocate memory for image arrays
stem_data.assign(nxy, 0);
ricom_data.assign(nxy, 0);
comx_data.assign(nxy, 0);
comy_data.assign(nxy, 0);
dose_data.assign(nxy, 0);
sumx_data.assign(nxy, 0);
sumy_data.assign(nxy, 0);
stem_image.assign(nxy, 0);
ricom_image.assign(nxy, 0);
comx_image.assign(nxy, 0);
comy_image.assign(nxy, 0);
for (int i=0; i<2; i++)
{
dose_data[i].assign(nxy, 0);
sumx_data[i].assign(nxy, 0);
sumy_data[i].assign(nxy, 0);
stem_data[i].assign(nxy, 0);
}
frame.assign(n_cam * n_cam, 0);
airpi_data.assign(nxy, 0); // not correct, airpi_data size will be larger due to super resolution
airpi_image.assign(nxy, 0); // not correct, airpi_image size will be larger due to super resolution

// Data Processing Progress
processor_line = 0;
Expand Down
18 changes: 10 additions & 8 deletions src/Ricom.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,15 @@ class Ricom
Ricom_kernel kernel;
std::array<float, 2> offset;
std::array<float, 2> com_public;
std::vector<float> comx_data;
std::vector<float> comy_data;
std::vector<float> ricom_data;
std::vector<float> stem_data;
std::vector<float> airpi_data;
std::vector<size_t> dose_data;
std::vector<size_t> sumx_data;
std::vector<size_t> sumy_data;
std::vector<float> comx_image;
std::vector<float> comy_image;
std::vector<float> ricom_image;
std::vector<float> stem_image;
std::vector<float> airpi_image;
std::vector<size_t> dose_data[2];
std::vector<size_t> sumx_data[2];
std::vector<size_t> sumy_data[2];
std::vector<size_t> stem_data[2];
std::vector<size_t> frame;
std::vector<std::complex<float>> e_field_data;

Expand All @@ -269,6 +270,7 @@ class Ricom
int skip_img;
int processor_line;
int preprocessor_line;
int id_image;

// Variables for progress and performance
int n_threads;
Expand Down
2 changes: 1 addition & 1 deletion src/RunCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ int run_cli(int argc, char *argv[], Ricom *ricom)
}
if (save_dat != "")
{
save_numpy(&save_dat, ricom->nx, ricom->ny, &ricom->ricom_data);
save_numpy(&save_dat, ricom->nx, ricom->ny, &ricom->ricom_image);
std::cout << "riCOM reconstruction data saved as " + save_dat << std::endl;
}
if (save_img != "")
Expand Down
28 changes: 14 additions & 14 deletions src/RunGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int run_gui(Ricom *ricom)
{
if (&ricom->b_ricom)
{
GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_data);
GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_image);
}
else
{
Expand All @@ -313,7 +313,7 @@ int run_gui(Ricom *ricom)
{
if (show_com_x)
{
GENERIC_WINDOW("COM-X").set_data(ricom->nx, ricom->ny, &ricom->comx_data);
GENERIC_WINDOW("COM-X").set_data(ricom->nx, ricom->ny, &ricom->comx_image);
}
else
{
Expand All @@ -324,7 +324,7 @@ int run_gui(Ricom *ricom)
{
if (show_com_y)
{
GENERIC_WINDOW("COM-Y").set_data(ricom->nx, ricom->ny, &ricom->comy_data);
GENERIC_WINDOW("COM-Y").set_data(ricom->nx, ricom->ny, &ricom->comy_image);
}
else
{
Expand All @@ -346,7 +346,7 @@ int run_gui(Ricom *ricom)
{
if (ricom->b_vSTEM)
{
GENERIC_WINDOW("vSTEM").set_data(ricom->nx, ricom->ny, &ricom->stem_data);
GENERIC_WINDOW("vSTEM").set_data(ricom->nx, ricom->ny, &ricom->stem_image);
}
else
{
Expand Down Expand Up @@ -481,7 +481,7 @@ int run_gui(Ricom *ricom)
std::this_thread::sleep_for(std::chrono::milliseconds(500));
cheetah_comm.start();
// RICOM::run(ricom, RICOM::TCP);
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_data);
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_image);
}

if (ImGui::Button("Acquire", ImVec2(-1.0f, 0.0f)))
Expand All @@ -496,7 +496,7 @@ int run_gui(Ricom *ricom)
run_thread.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
cheetah_comm.start();
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_data);
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_image);
}

if (ImGui::Button("Stop", ImVec2(-1.0f, 0.0f)))
Expand Down Expand Up @@ -547,7 +547,7 @@ int run_gui(Ricom *ricom)
b_started = true;
b_restarted = true;
run_thread.detach();
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_data);
// GENERIC_WINDOW("RICOM").set_data(ricom->nx, ricom->ny, &ricom->ricom_image);
}
}
}
Expand Down Expand Up @@ -603,13 +603,13 @@ int run_gui(Ricom *ricom)
com_rel_y = (std::min)(p.y + tex_wh, com_rel_y);

float cross_width = tex_wh / 15.0f;
if (b_redraw)
{
if (ricom->srf_cbed != NULL)
{
bind_tex(ricom->srf_cbed, uiTextureIDs[0]);
}
}
// if (b_redraw)
// {
// if (ricom->srf_cbed != NULL)
// {
// bind_tex(ricom->srf_cbed, uiTextureIDs[0]);
// }
// }
ImGui::Image((ImTextureID)uiTextureIDs[0], ImVec2(tex_wh, tex_wh), uv_min, uv_max, tint_col, border_col);
ImGui::GetWindowDrawList()->AddCircle(ImVec2(centre_x, centre_y), tex_wh * 0.03, IM_COL32(255, 255, 255, 255), 256);
ImGui::GetWindowDrawList()->AddLine(ImVec2(com_rel_x - cross_width, com_rel_y), ImVec2(com_rel_x + cross_width, com_rel_y), IM_COL32(255, 0, 0, 255), 1.5f);
Expand Down
Loading

0 comments on commit 404c945

Please sign in to comment.