lamino.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. const int COLOR_RED = 31;
  21. const int COLOR_GREEN = 32;
  22. const int COLOR_YELLOW = 33;
  23. static void
  24. check (GError *error)
  25. {
  26. if (error != NULL) {
  27. if (error->message != NULL)
  28. g_printerr ("%s", error->message);
  29. exit (1);
  30. }
  31. }
  32. static UfoTaskNode *
  33. make_task (UfoPluginManager *pm, const gchar *name)
  34. {
  35. GError *error = NULL;
  36. UfoTaskNode *task;
  37. task = ufo_plugin_manager_get_task (pm, name, &error);
  38. check (error);
  39. return task;
  40. }
  41. static guint
  42. next_power_of_two (guint32 x)
  43. {
  44. --x;
  45. x |= x >> 1;
  46. x |= x >> 2;
  47. x |= x >> 4;
  48. x |= x >> 8;
  49. x |= x >> 16;
  50. return x + 1;
  51. }
  52. static void
  53. colored_print (gint color, const gchar *fmt, va_list args)
  54. {
  55. gchar *s;
  56. s = g_strdup_vprintf (fmt, args);
  57. g_print ("%c[%d;%dm%s%c[%dm", 0x1B, 1, color, s, 0x1b, 0);
  58. g_free (s);
  59. }
  60. static void
  61. warn (const gchar *fmt, ...)
  62. {
  63. va_list args;
  64. va_start (args, fmt);
  65. colored_print (COLOR_YELLOW, fmt, args);
  66. va_end (args);
  67. }
  68. static void
  69. err (const gchar *fmt, ...)
  70. {
  71. va_list args;
  72. va_start (args, fmt);
  73. colored_print (COLOR_RED, fmt, args);
  74. va_end (args);
  75. }
  76. static void
  77. info (const gchar *fmt, ...)
  78. {
  79. va_list args;
  80. gchar *s;
  81. va_start (args, fmt);
  82. g_print ("%c[%d;%dm>%c[%dm ", 0x1B, 1, COLOR_GREEN, 0x1b, 0);
  83. s = g_strdup_vprintf (fmt, args);
  84. g_print ("%s", s);
  85. va_end (args);
  86. g_free (s);
  87. }
  88. static void
  89. print_dots (gpointer user)
  90. {
  91. static int n = 0;
  92. if ((n++ % 10) == 0)
  93. g_print (".");
  94. }
  95. static void
  96. run_reconstruction (Params *params)
  97. {
  98. guint xl, xr, yt, yb;
  99. guint padded_width;
  100. guint padded_height;
  101. gdouble angle_step;
  102. gdouble time = 0.0;
  103. UfoPluginManager *pm = NULL;
  104. UfoTaskGraph *graph = NULL;
  105. UfoBaseScheduler *sched = NULL;
  106. UfoTaskNode *radio_reader = NULL;
  107. UfoTaskNode *dark_reader = NULL;
  108. UfoTaskNode *flat_reader = NULL;
  109. UfoTaskNode *ffc = NULL;
  110. UfoTaskNode *pad = NULL;
  111. UfoTaskNode *ramp = NULL;
  112. UfoTaskNode *fft1 = NULL;
  113. UfoTaskNode *fft2 = NULL;
  114. UfoTaskNode *ifft = NULL;
  115. UfoTaskNode *conv = NULL;
  116. UfoTaskNode *reco = NULL;
  117. UfoTaskNode *writer = NULL;
  118. GError *error = NULL;
  119. pm = ufo_plugin_manager_new (NULL);
  120. graph = UFO_TASK_GRAPH (ufo_task_graph_new ());
  121. radio_reader = make_task (pm, "reader");
  122. pad = make_task (pm, "padding-2d");
  123. ramp = make_task (pm, "lamino-ramp");
  124. fft1 = make_task (pm, "fft");
  125. fft2 = make_task (pm, "fft");
  126. ifft = make_task (pm, "ifft");
  127. conv = make_task (pm, "lamino-conv");
  128. reco = make_task (pm, "lamino-bp");
  129. writer = make_task (pm, "writer");
  130. g_object_set (radio_reader,
  131. "path", params->radios,
  132. "end", params->num_radios - 1,
  133. NULL);
  134. g_object_set (fft1, "dimensions", 2, NULL);
  135. g_object_set (fft2, "dimensions", 2, NULL);
  136. g_object_set (ifft, "dimensions", 2, NULL);
  137. padded_width = next_power_of_two ((guint32) params->width);
  138. padded_height = next_power_of_two ((guint32) params->height);
  139. xl = (padded_width - params->width) / 2;
  140. xr = xl;
  141. yt = (padded_height - params->height) / 2;
  142. yb = yt;
  143. angle_step = (G_PI * 2.0) / params->num_radios;
  144. info ("Padding: size=[%d %d] (xl=%d xr=%d yt=%d yb=%d)\n",
  145. padded_width, padded_height, xl, xr, yt, yb);
  146. info ("Parameters: theta=%.3f tau=%.3f step=%.5f\n",
  147. params->theta, params->tau, angle_step);
  148. info ("Volume: origin=[%.1f %.1f %.1f] size=[%d %d %d]\n",
  149. params->v_origin[0], params->v_origin[1], params->v_origin[2],
  150. params->v_size[0], params->v_size[1], params->v_size[2]);
  151. g_object_set (pad,
  152. "xl", xl, "xr", xr,
  153. "yt", yt, "yb", yb,
  154. "mode", "brep",
  155. NULL);
  156. g_object_set (ramp,
  157. "width", padded_width,
  158. "height", padded_height,
  159. "theta", G_PI * 2 / params->theta,
  160. "tau", params->tau,
  161. NULL);
  162. g_object_set (reco,
  163. "angle-step", angle_step,
  164. "theta", G_PI * 2 / params->theta,
  165. "psi", params->psi,
  166. "proj-ox", params->px + xl,
  167. "proj-oy", params->py,
  168. "vol-ox", params->v_origin[0],
  169. "vol-oy", params->v_origin[1],
  170. "vol-oz", params->v_origin[2],
  171. "vol-sx", params->v_size[0],
  172. "vol-sy", params->v_size[1],
  173. "vol-sz", params->v_size[2],
  174. NULL);
  175. g_object_set (writer,
  176. "filename", params->output,
  177. NULL);
  178. if (params->darks != NULL && params->flats != NULL) {
  179. flat_reader = make_task (pm, "reader");
  180. dark_reader = make_task (pm, "reader");
  181. ffc = make_task (pm, "flat-field-correction");
  182. g_object_set (flat_reader, "path", params->flats, NULL);
  183. g_object_set (dark_reader, "path", params->darks, NULL);
  184. ufo_task_graph_connect_nodes_full (graph, radio_reader, ffc, 0);
  185. ufo_task_graph_connect_nodes_full (graph, dark_reader, ffc, 1);
  186. ufo_task_graph_connect_nodes_full (graph, flat_reader, ffc, 2);
  187. ufo_task_graph_connect_nodes (graph, ffc, pad);
  188. }
  189. else {
  190. warn ("> No flat/dark field correction\n");
  191. ufo_task_graph_connect_nodes (graph, radio_reader, pad);
  192. }
  193. ufo_task_graph_connect_nodes (graph, pad, fft1);
  194. ufo_task_graph_connect_nodes (graph, ramp, fft2);
  195. ufo_task_graph_connect_nodes_full (graph, fft1, conv, 0);
  196. ufo_task_graph_connect_nodes_full (graph, fft2, conv, 1);
  197. ufo_task_graph_connect_nodes (graph, conv, ifft);
  198. ufo_task_graph_connect_nodes (graph, ifft, reco);
  199. ufo_task_graph_connect_nodes (graph, reco, writer);
  200. g_signal_connect (reco, "processed", G_CALLBACK (print_dots), NULL);
  201. sched = UFO_BASE_SCHEDULER (ufo_scheduler_new (NULL, NULL));
  202. ufo_base_scheduler_run (sched, graph, &error);
  203. check (error);
  204. g_object_get (sched, "time", &time, NULL);
  205. g_print ("\n");
  206. info("Finished in %3.3fs\n", time);
  207. g_object_unref (pm);
  208. g_object_unref (graph);
  209. g_object_unref (sched);
  210. g_object_unref (radio_reader);
  211. g_object_unref (pad);
  212. g_object_unref (fft1);
  213. g_object_unref (fft2);
  214. /* g_object_unref (conv); */
  215. g_object_unref (reco);
  216. g_object_unref (writer);
  217. if (params->darks != NULL && params->flats != NULL) {
  218. g_object_unref (ffc);
  219. g_object_unref (flat_reader);
  220. g_object_unref (dark_reader);
  221. }
  222. }
  223. static void
  224. parse_params (Params *params, int argc, char **argv)
  225. {
  226. GOptionContext *context;
  227. GError *error = NULL;
  228. GOptionEntry entries[] = {
  229. { "width", 0, 0, G_OPTION_ARG_INT, &params->width, "Width", "" },
  230. { "height", 0, 0, G_OPTION_ARG_INT, &params->height, "Height", "" },
  231. { "num-radios", 0, 0, G_OPTION_ARG_INT, &params->num_radios, "Number of radios", "" },
  232. { "num-darks", 0, 0, G_OPTION_ARG_INT, &params->num_darks, "Number of darks", "" },
  233. { "radios", 0, 0, G_OPTION_ARG_STRING, &params->radios, "Radios", "" },
  234. { "darks", 0, 0, G_OPTION_ARG_STRING, &params->darks, "Darks", "" },
  235. { "flats", 0, 0, G_OPTION_ARG_STRING, &params->flats, "Flats", "" },
  236. { "output", 0, 0, G_OPTION_ARG_STRING, &params->output, "Output", "" },
  237. { "theta", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Tilt (theta)", "" },
  238. { "tau", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Pixel size (theta)", "" },
  239. { "psi", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Misalignment (psi)", "" },
  240. { "px", 0, 0, G_OPTION_ARG_DOUBLE, &params->px, "X coordinate of axis", "" },
  241. { "py", 0, 0, G_OPTION_ARG_DOUBLE, &params->py, "Y coordinate of axis", "" },
  242. { "vx", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[0], "X coordinate of box origin", "" },
  243. { "vy", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[1], "Y coordinate of box origin", "" },
  244. { "vz", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[2], "Z coordinate of box origin", "" },
  245. { "vw", 0, 0, G_OPTION_ARG_INT, &params->v_size[0], "Width of box", "" },
  246. { "vh", 0, 0, G_OPTION_ARG_INT, &params->v_size[1], "Height of box", "" },
  247. { "vd", 0, 0, G_OPTION_ARG_INT, &params->v_size[2], "Depth of box", "" },
  248. { NULL }
  249. };
  250. context = g_option_context_new ("- test tree model performance");
  251. g_option_context_add_main_entries (context, entries, NULL);
  252. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  253. g_print ("option parsing failed: %s\n", error->message);
  254. exit (1);
  255. }
  256. if (params->width == 0 || params->height == 0 || params->num_radios == 0 ||
  257. params->radios == NULL ||
  258. params->theta == G_MAXDOUBLE ||
  259. params->px == G_MAXDOUBLE || params->py == G_MAXDOUBLE) {
  260. err ("Parameters missing.\n\n");
  261. g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
  262. exit (1);
  263. }
  264. g_option_context_free (context);
  265. }
  266. int
  267. main (int argc, char **argv)
  268. {
  269. Params params = {
  270. .radios = NULL,
  271. .darks = NULL,
  272. .flats = NULL,
  273. .output = "volume-%i.tif",
  274. .width = 0,
  275. .height = 0,
  276. .num_radios = 0,
  277. .num_darks = 1,
  278. .theta = G_MAXDOUBLE,
  279. .tau = 1.0,
  280. .psi = 0.0,
  281. .px = G_MAXDOUBLE,
  282. .py = G_MAXDOUBLE,
  283. .v_size = {256, 256, 256},
  284. .v_origin = {0.0, 0.0, 0.0},
  285. };
  286. #if !(GLIB_CHECK_VERSION (2, 36, 0))
  287. g_type_init ();
  288. #endif
  289. parse_params (&params, argc, argv);
  290. run_reconstruction (&params);
  291. return 0;
  292. }