lamino.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 theta_rad;
  102. gdouble angle_step;
  103. guint fwidth;
  104. gdouble time = 0.0;
  105. UfoPluginManager *pm = NULL;
  106. UfoTaskGraph *graph = NULL;
  107. UfoBaseScheduler *sched = NULL;
  108. UfoTaskNode *radio_reader = NULL;
  109. UfoTaskNode *dark_reader = NULL;
  110. UfoTaskNode *flat_reader = NULL;
  111. UfoTaskNode *ffc = NULL;
  112. UfoTaskNode *pad = NULL;
  113. UfoTaskNode *ramp = NULL;
  114. UfoTaskNode *fft1 = NULL;
  115. UfoTaskNode *fft2 = NULL;
  116. UfoTaskNode *ifft = NULL;
  117. UfoTaskNode *conv = NULL;
  118. UfoTaskNode *reco = NULL;
  119. UfoTaskNode *writer = NULL;
  120. GError *error = NULL;
  121. pm = ufo_plugin_manager_new (NULL);
  122. graph = UFO_TASK_GRAPH (ufo_task_graph_new ());
  123. radio_reader = make_task (pm, "reader");
  124. pad = make_task (pm, "padding-2d");
  125. ramp = make_task (pm, "lamino-ramp");
  126. fft1 = make_task (pm, "fft");
  127. fft2 = make_task (pm, "fft");
  128. ifft = make_task (pm, "ifft");
  129. conv = make_task (pm, "lamino-conv");
  130. reco = make_task (pm, "lamino-bp");
  131. writer = make_task (pm, "writer");
  132. g_object_set (radio_reader,
  133. "path", params->radios,
  134. "end", params->num_radios - 1,
  135. NULL);
  136. g_object_set (fft1, "dimensions", 2, NULL);
  137. g_object_set (fft2, "dimensions", 2, NULL);
  138. g_object_set (ifft, "dimensions", 2, NULL);
  139. fwidth = ((guint) params->px) * 2;
  140. padded_width = next_power_of_two (fwidth) * 2;
  141. padded_height = next_power_of_two ((guint32) params->height + 1);
  142. xl = params->px - params->width / 2;
  143. xr = padded_width - params->width - xl;
  144. yt = params->py - params->height / 2;
  145. yb = padded_height - params->height - yt;
  146. angle_step = (G_PI * 2.0) / params->num_radios;
  147. theta_rad = params->theta / 360. * G_PI * 2;
  148. info ("Axis: x=%.1f y=%.1f\n",
  149. params->px, params->py);
  150. info ("Lamino: theta=%.3f tau=%.3f step=%.5f fwidth=%d\n",
  151. theta_rad, params->tau, angle_step, fwidth);
  152. info ("Padding: size=[%d %d] (xl=%d xr=%d yt=%d yb=%d)\n",
  153. padded_width, padded_height, xl, xr, yt, yb);
  154. info ("Volume: origin=[%.1f %.1f %.1f] size=[%d %d %d]\n",
  155. params->v_origin[0], params->v_origin[1], params->v_origin[2],
  156. params->v_size[0], params->v_size[1], params->v_size[2]);
  157. g_object_set (pad,
  158. "xl", xl, "xr", xr,
  159. "yt", yt, "yb", yb,
  160. "mode", "brep",
  161. NULL);
  162. g_object_set (ramp,
  163. "width", padded_width,
  164. "height", padded_height,
  165. "theta", theta_rad,
  166. "tau", params->tau,
  167. "fwidth", fwidth,
  168. NULL);
  169. g_object_set (reco,
  170. "angle-step", angle_step,
  171. "theta", theta_rad,
  172. "psi", params->psi,
  173. "proj-ox", params->px,
  174. "proj-oy", params->py,
  175. "vol-ox", params->v_size[0] / 2 + params->v_origin[0],
  176. "vol-oy", params->v_size[1] / 2 + params->v_origin[1],
  177. "vol-oz", params->v_size[2] / 2 + params->v_origin[2],
  178. "vol-sx", params->v_size[0],
  179. "vol-sy", params->v_size[1],
  180. "vol-sz", params->v_size[2],
  181. NULL);
  182. g_object_set (writer,
  183. "filename", params->output,
  184. NULL);
  185. if (params->darks != NULL && params->flats != NULL) {
  186. flat_reader = make_task (pm, "reader");
  187. dark_reader = make_task (pm, "reader");
  188. ffc = make_task (pm, "flat-field-correction");
  189. g_object_set (flat_reader, "path", params->flats, NULL);
  190. g_object_set (dark_reader, "path", params->darks, NULL);
  191. ufo_task_graph_connect_nodes_full (graph, radio_reader, ffc, 0);
  192. ufo_task_graph_connect_nodes_full (graph, dark_reader, ffc, 1);
  193. ufo_task_graph_connect_nodes_full (graph, flat_reader, ffc, 2);
  194. ufo_task_graph_connect_nodes (graph, ffc, pad);
  195. }
  196. else {
  197. warn ("> No flat/dark field correction\n");
  198. ufo_task_graph_connect_nodes (graph, radio_reader, pad);
  199. }
  200. ufo_task_graph_connect_nodes (graph, pad, fft1);
  201. ufo_task_graph_connect_nodes (graph, ramp, fft2);
  202. ufo_task_graph_connect_nodes_full (graph, fft1, conv, 0);
  203. ufo_task_graph_connect_nodes_full (graph, fft2, conv, 1);
  204. ufo_task_graph_connect_nodes (graph, conv, ifft);
  205. ufo_task_graph_connect_nodes (graph, ifft, reco);
  206. ufo_task_graph_connect_nodes (graph, reco, writer);
  207. g_signal_connect (reco, "processed", G_CALLBACK (print_dots), NULL);
  208. sched = UFO_BASE_SCHEDULER (ufo_scheduler_new (NULL, NULL));
  209. ufo_base_scheduler_run (sched, graph, &error);
  210. check (error);
  211. g_object_get (sched, "time", &time, NULL);
  212. g_print ("\n");
  213. info("Finished in %3.3fs\n", time);
  214. g_object_unref (pm);
  215. g_object_unref (graph);
  216. g_object_unref (sched);
  217. g_object_unref (radio_reader);
  218. g_object_unref (pad);
  219. g_object_unref (fft1);
  220. g_object_unref (fft2);
  221. /* g_object_unref (conv); */
  222. g_object_unref (reco);
  223. g_object_unref (writer);
  224. if (params->darks != NULL && params->flats != NULL) {
  225. g_object_unref (ffc);
  226. g_object_unref (flat_reader);
  227. g_object_unref (dark_reader);
  228. }
  229. }
  230. static void
  231. parse_params (Params *params, int argc, char **argv)
  232. {
  233. GOptionContext *context;
  234. GError *error = NULL;
  235. GOptionEntry entries[] = {
  236. { "width", 0, 0, G_OPTION_ARG_INT, &params->width, "Width", "" },
  237. { "height", 0, 0, G_OPTION_ARG_INT, &params->height, "Height", "" },
  238. { "num-radios", 0, 0, G_OPTION_ARG_INT, &params->num_radios, "Number of radios", "" },
  239. { "num-darks", 0, 0, G_OPTION_ARG_INT, &params->num_darks, "Number of darks", "" },
  240. { "radios", 0, 0, G_OPTION_ARG_STRING, &params->radios, "Radios", "" },
  241. { "darks", 0, 0, G_OPTION_ARG_STRING, &params->darks, "Darks", "" },
  242. { "flats", 0, 0, G_OPTION_ARG_STRING, &params->flats, "Flats", "" },
  243. { "output", 0, 0, G_OPTION_ARG_STRING, &params->output, "Output", "" },
  244. { "theta", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Tilt (theta)", "" },
  245. { "tau", 0, 0, G_OPTION_ARG_DOUBLE, &params->tau, "Pixel size (theta)", "" },
  246. { "psi", 0, 0, G_OPTION_ARG_DOUBLE, &params->psi, "Misalignment (psi)", "" },
  247. { "px", 0, 0, G_OPTION_ARG_DOUBLE, &params->px, "X coordinate of axis", "" },
  248. { "py", 0, 0, G_OPTION_ARG_DOUBLE, &params->py, "Y coordinate of axis", "" },
  249. { "vx", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[0], "X coordinate of box origin", "" },
  250. { "vy", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[1], "Y coordinate of box origin", "" },
  251. { "vz", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[2], "Z coordinate of box origin", "" },
  252. { "vw", 0, 0, G_OPTION_ARG_INT, &params->v_size[0], "Width of box", "" },
  253. { "vh", 0, 0, G_OPTION_ARG_INT, &params->v_size[1], "Height of box", "" },
  254. { "vd", 0, 0, G_OPTION_ARG_INT, &params->v_size[2], "Depth of box", "" },
  255. { NULL }
  256. };
  257. context = g_option_context_new ("- test tree model performance");
  258. g_option_context_add_main_entries (context, entries, NULL);
  259. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  260. g_print ("option parsing failed: %s\n", error->message);
  261. exit (1);
  262. }
  263. if (params->width == 0 || params->height == 0 || params->num_radios == 0 ||
  264. params->radios == NULL ||
  265. params->theta == G_MAXDOUBLE ||
  266. params->px == G_MAXDOUBLE || params->py == G_MAXDOUBLE) {
  267. err ("Parameters missing.\n\n");
  268. g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
  269. exit (1);
  270. }
  271. g_option_context_free (context);
  272. }
  273. int
  274. main (int argc, char **argv)
  275. {
  276. Params params = {
  277. .radios = NULL,
  278. .darks = NULL,
  279. .flats = NULL,
  280. .output = "volume-%i.tif",
  281. .width = 0,
  282. .height = 0,
  283. .num_radios = 0,
  284. .num_darks = 1,
  285. .theta = G_MAXDOUBLE,
  286. .tau = 1.0,
  287. .psi = 0.0,
  288. .px = G_MAXDOUBLE,
  289. .py = G_MAXDOUBLE,
  290. .v_size = {256, 256, 256},
  291. .v_origin = {0.0, 0.0, 0.0},
  292. };
  293. #if !(GLIB_CHECK_VERSION (2, 36, 0))
  294. g_type_init ();
  295. #endif
  296. parse_params (&params, argc, argv);
  297. run_reconstruction (&params);
  298. return 0;
  299. }