lamino.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <stdlib.h>
  2. #include <ufo-0/ufo/ufo.h>
  3. typedef struct {
  4. gchar *radios;
  5. gchar *darks;
  6. gchar *flats;
  7. gchar *output;
  8. gint width;
  9. gint height;
  10. guint num_radios;
  11. guint num_darks;
  12. gdouble theta;
  13. gdouble tau;
  14. gdouble psi;
  15. gdouble px;
  16. gdouble py;
  17. gdouble v_origin[3];
  18. guint v_size[3];
  19. } Params;
  20. static void
  21. check (GError *error)
  22. {
  23. if (error != NULL) {
  24. if (error->message != NULL)
  25. g_printerr ("%s", error->message);
  26. exit (1);
  27. }
  28. }
  29. static UfoTaskNode *
  30. make_task (UfoPluginManager *pm, const gchar *name)
  31. {
  32. GError *error = NULL;
  33. UfoTaskNode *task;
  34. task = ufo_plugin_manager_get_task (pm, name, &error);
  35. check (error);
  36. return task;
  37. }
  38. static guint
  39. next_power_of_two (guint32 x)
  40. {
  41. --x;
  42. x |= x >> 1;
  43. x |= x >> 2;
  44. x |= x >> 4;
  45. x |= x >> 8;
  46. x |= x >> 16;
  47. return x + 1;
  48. }
  49. static void
  50. run_reconstruction (Params *params)
  51. {
  52. UfoPluginManager *pm;
  53. UfoTaskGraph *graph;
  54. UfoBaseScheduler *sched;
  55. UfoTaskNode *radio_reader;
  56. UfoTaskNode *dark_reader;
  57. UfoTaskNode *flat_reader;
  58. UfoTaskNode *ffc;
  59. UfoTaskNode *pad;
  60. UfoTaskNode *ramp;
  61. UfoTaskNode *fft1;
  62. UfoTaskNode *fft2;
  63. UfoTaskNode *ifft;
  64. UfoTaskNode *conv;
  65. UfoTaskNode *reco;
  66. UfoTaskNode *writer;
  67. guint padded_width;
  68. guint padded_height;
  69. guint xl, xr, yt, yb;
  70. GError *error = NULL;
  71. pm = ufo_plugin_manager_new (NULL);
  72. graph = UFO_TASK_GRAPH (ufo_task_graph_new ());
  73. radio_reader = make_task (pm, "reader");
  74. flat_reader = make_task (pm, "reader");
  75. dark_reader = make_task (pm, "reader");
  76. ffc = make_task (pm, "flat-field-correction");
  77. pad = make_task (pm, "padding-2d");
  78. ramp = make_task (pm, "lamino-ramp");
  79. fft1 = make_task (pm, "fft");
  80. fft2 = make_task (pm, "fft");
  81. ifft = make_task (pm, "ifft");
  82. conv = make_task (pm, "lamino-conv");
  83. reco = make_task (pm, "lamino-bp");
  84. writer = make_task (pm, "writer");
  85. g_object_set (radio_reader,
  86. "path", params->radios,
  87. "end", params->num_radios - 1,
  88. NULL);
  89. g_object_set (flat_reader, "path", params->flats, NULL);
  90. g_object_set (dark_reader, "path", params->darks, NULL);
  91. g_object_set (ffc, "num-darks", params->num_darks, NULL);
  92. g_object_set (fft1, "dimensions", 2, NULL);
  93. g_object_set (fft2, "dimensions", 2, NULL);
  94. g_object_set (ifft, "dimensions", 2, NULL);
  95. padded_width = next_power_of_two ((guint32) params->width);
  96. padded_height = next_power_of_two ((guint32) params->height);
  97. xl = (padded_width - params->width) / 2;
  98. xr = xl;
  99. yt = (padded_height - params->height) / 2;
  100. yb = yt;
  101. g_object_set (pad,
  102. "xl", xl,
  103. "xr", xr,
  104. "yt", yt,
  105. "yb", yb,
  106. "mode", "brep",
  107. NULL);
  108. g_object_set (ramp,
  109. "width", padded_width,
  110. "height", padded_height,
  111. "theta", G_PI * 2 / params->theta,
  112. "tau", params->tau,
  113. NULL);
  114. g_object_set (reco,
  115. "angle-step", (G_PI * 2.0) / params->num_radios,
  116. "theta", G_PI * 2 / params->theta,
  117. "psi", params->psi,
  118. "proj-ox", params->px + xl,
  119. "proj-oy", params->py,
  120. "vol-ox", params->v_origin[0],
  121. "vol-oy", params->v_origin[1],
  122. "vol-oz", params->v_origin[2],
  123. "vol-sx", params->v_size[0],
  124. "vol-sy", params->v_size[1],
  125. "vol-sz", params->v_size[2],
  126. NULL);
  127. g_object_set (writer,
  128. "filename", "/data/visitor/ma2183/id19/volume-%i.tif",
  129. NULL);
  130. ufo_task_graph_connect_nodes_full (graph, radio_reader, ffc, 0);
  131. ufo_task_graph_connect_nodes_full (graph, dark_reader, ffc, 1);
  132. ufo_task_graph_connect_nodes_full (graph, flat_reader, ffc, 2);
  133. ufo_task_graph_connect_nodes (graph, ffc, pad);
  134. ufo_task_graph_connect_nodes (graph, pad, fft1);
  135. ufo_task_graph_connect_nodes (graph, ramp, fft2);
  136. ufo_task_graph_connect_nodes_full (graph, fft1, conv, 0);
  137. ufo_task_graph_connect_nodes_full (graph, fft2, conv, 1);
  138. ufo_task_graph_connect_nodes (graph, conv, ifft);
  139. ufo_task_graph_connect_nodes (graph, ifft, reco);
  140. ufo_task_graph_connect_nodes (graph, reco, writer);
  141. sched = UFO_BASE_SCHEDULER (ufo_scheduler_new (NULL, NULL));
  142. ufo_base_scheduler_run (sched, graph, &error);
  143. check (error);
  144. g_object_unref (pm);
  145. g_object_unref (graph);
  146. g_object_unref (sched);
  147. g_object_unref (radio_reader);
  148. g_object_unref (flat_reader);
  149. g_object_unref (dark_reader);
  150. g_object_unref (ffc);
  151. g_object_unref (pad);
  152. g_object_unref (fft1);
  153. g_object_unref (fft2);
  154. g_object_unref (conv);
  155. g_object_unref (reco);
  156. g_object_unref (writer);
  157. }
  158. static void
  159. parse_params (Params *params, int argc, char **argv)
  160. {
  161. GOptionContext *context;
  162. GError *error = NULL;
  163. GOptionEntry entries[] = {
  164. { "width", 0, 0, G_OPTION_ARG_INT, &params->width, "Width", "" },
  165. { "height", 0, 0, G_OPTION_ARG_INT, &params->height, "Height", "" },
  166. { "num-radios", 0, 0, G_OPTION_ARG_INT, &params->num_radios, "Number of radios", "" },
  167. { "num-darks", 0, 0, G_OPTION_ARG_INT, &params->num_darks, "Number of darks", "" },
  168. { "radios", 0, 0, G_OPTION_ARG_STRING, &params->radios, "Radios", "" },
  169. { "darks", 0, 0, G_OPTION_ARG_STRING, &params->darks, "Darks", "" },
  170. { "flats", 0, 0, G_OPTION_ARG_STRING, &params->flats, "Flats", "" },
  171. { "output", 0, 0, G_OPTION_ARG_STRING, &params->output, "Output", "" },
  172. { "theta", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Tilt (theta)", "" },
  173. { "tau", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Pixel size (theta)", "" },
  174. { "psi", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Misalignment (psi)", "" },
  175. { "px", 0, 0, G_OPTION_ARG_DOUBLE, &params->px, "X coordinate of axis", "" },
  176. { "py", 0, 0, G_OPTION_ARG_DOUBLE, &params->py, "Y coordinate of axis", "" },
  177. { "vx", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[0], "X coordinate of box origin", "" },
  178. { "vy", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[1], "Y coordinate of box origin", "" },
  179. { "vz", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[2], "Z coordinate of box origin", "" },
  180. { "vw", 0, 0, G_OPTION_ARG_INT, &params->v_size[0], "Width of box", "" },
  181. { "vh", 0, 0, G_OPTION_ARG_INT, &params->v_size[1], "Height of box", "" },
  182. { "vd", 0, 0, G_OPTION_ARG_INT, &params->v_size[2], "Depth of box", "" },
  183. { NULL }
  184. };
  185. context = g_option_context_new ("- test tree model performance");
  186. g_option_context_add_main_entries (context, entries, NULL);
  187. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  188. g_print ("option parsing failed: %s\n", error->message);
  189. exit (1);
  190. }
  191. if (params->width == 0 || params->height == 0 || params->num_radios == 0 ||
  192. !params->radios || !params->darks || !params->flats ||
  193. params->theta == G_MAXDOUBLE ||
  194. params->px == G_MAXDOUBLE || params->py == G_MAXDOUBLE) {
  195. g_printerr ("Error: Parameters missing.\n\n%s",
  196. g_option_context_get_help (context, TRUE, NULL));
  197. exit (1);
  198. }
  199. g_option_context_free (context);
  200. }
  201. static void
  202. check_params (Params *params)
  203. {
  204. }
  205. int
  206. main (int argc, char **argv)
  207. {
  208. Params params = {
  209. .radios = NULL,
  210. .darks = NULL,
  211. .flats = NULL,
  212. .output = "volume-%i.tif",
  213. .width = 0,
  214. .height = 0,
  215. .num_radios = 0,
  216. .num_darks = 1,
  217. .theta = G_MAXDOUBLE,
  218. .tau = 1.0,
  219. .psi = 0.0,
  220. .px = G_MAXDOUBLE,
  221. .py = G_MAXDOUBLE,
  222. .v_size = {256, 256, 256},
  223. .v_origin = {0.0, 0.0, 0.0},
  224. };
  225. #if !(GLIB_CHECK_VERSION (2, 36, 0))
  226. g_type_init ();
  227. #endif
  228. parse_params (&params, argc, argv);
  229. check_params (&params);
  230. run_reconstruction (&params);
  231. return 0;
  232. }