lamino.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <stdlib.h>
  2. #include <readline/readline.h>
  3. #include <readline/history.h>
  4. #include "reco.h"
  5. #include "io.h"
  6. typedef void (*CommandFunction)(Params *, gchar **argv);
  7. typedef struct {
  8. const gchar *command;
  9. CommandFunction func;
  10. } CommandMap;
  11. static void
  12. print (Params *params, gchar **argv)
  13. {
  14. for (gint i = 0; params->entries[i].long_name != NULL; i++) {
  15. GOptionEntry *entry;
  16. entry = &params->entries[i];
  17. if (argv[0] == NULL || g_strcmp0 (entry->long_name, argv[0]) == 0) {
  18. switch (entry->arg) {
  19. case G_OPTION_ARG_STRING:
  20. info ("%-15s%s\n", entry->long_name, *((gchar **) entry->arg_data));
  21. break;
  22. case G_OPTION_ARG_INT:
  23. info ("%-15s%i\n", entry->long_name, *((gint *) entry->arg_data));
  24. break;
  25. case G_OPTION_ARG_DOUBLE:
  26. info ("%-15s%f\n", entry->long_name, *((gdouble *) entry->arg_data));
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. static void
  35. set (Params *params, gchar **argv)
  36. {
  37. if (argv[0] == NULL) {
  38. warn ("No parameter given\n");
  39. return;
  40. }
  41. if (argv[1] == NULL) {
  42. warn ("No value given\n");
  43. return;
  44. }
  45. for (gint i = 0; params->entries[i].long_name != NULL; i++) {
  46. GOptionEntry *entry;
  47. entry = &params->entries[i];
  48. if (g_strcmp0 (entry->long_name, argv[0]) == 0) {
  49. switch (entry->arg) {
  50. case G_OPTION_ARG_STRING:
  51. {
  52. gchar *s;
  53. s = *((gchar **) entry->arg_data);
  54. g_free (s);
  55. *(gchar **) entry->arg_data = g_strjoinv (" ", &argv[1]);
  56. }
  57. break;
  58. case G_OPTION_ARG_INT:
  59. *(gint *) entry->arg_data = atoi (argv[1]);
  60. break;
  61. case G_OPTION_ARG_DOUBLE:
  62. *(gdouble *) entry->arg_data = atof (argv[1]);
  63. break;
  64. default:
  65. break;
  66. }
  67. print (params, argv);
  68. break;
  69. }
  70. }
  71. }
  72. static void
  73. quit (Params *params, gchar **argv)
  74. {
  75. exit (0);
  76. }
  77. static void
  78. start_shell (Params *params)
  79. {
  80. static CommandMap map[] = {
  81. { "run", run_simple_reconstruction },
  82. { "print", print },
  83. { "set", set },
  84. { "quit", quit },
  85. { NULL },
  86. };
  87. while (1) {
  88. gchar *line;
  89. gint i;
  90. gchar **split = NULL;
  91. line = readline ("> ");
  92. if (line && *line && (g_strchomp (line) == g_strchug (line))) {
  93. add_history (line);
  94. }
  95. else {
  96. goto cleanup_line;
  97. }
  98. split = g_strsplit (line, " ", 0);
  99. for (i = 0; map[i].command != NULL; i++) {
  100. /* Run command even if only partially written out */
  101. if (g_str_has_prefix (map[i].command, split[0])) {
  102. map[i].func (params, &split[1]);
  103. break;
  104. }
  105. }
  106. if (map[i].command == NULL)
  107. warn ("Unknown command `%s'\n", split[0]);
  108. cleanup_line:
  109. g_free (line);
  110. g_strfreev (split);
  111. }
  112. }
  113. static void
  114. parse_params (Params *params, int argc, char **argv)
  115. {
  116. GOptionContext *context;
  117. GError *error = NULL;
  118. GOptionEntry entries[] = {
  119. { "width", 0, 0, G_OPTION_ARG_INT, &params->width, "Width", "[int]" },
  120. { "height", 0, 0, G_OPTION_ARG_INT, &params->height, "Height", "[int]" },
  121. { "num-radios", 0, 0, G_OPTION_ARG_INT, &params->num_radios, "Number of radios", "[int]" },
  122. { "num-darks", 0, 0, G_OPTION_ARG_INT, &params->num_darks, "Number of darks", "[int]" },
  123. { "radios", 0, 0, G_OPTION_ARG_STRING, &params->radios, "Radios", "[path|glob]" },
  124. { "darks", 0, 0, G_OPTION_ARG_STRING, &params->darks, "Darks", "[path|glob]" },
  125. { "flats", 0, 0, G_OPTION_ARG_STRING, &params->flats, "Flats", "[path|glob]" },
  126. { "dark-scale", 0, 0, G_OPTION_ARG_DOUBLE, &params->dark_scale, "Dark scale", "[float]" },
  127. { "output", 0, 0, G_OPTION_ARG_STRING, &params->output, "Output", "[path]" },
  128. { "theta", 0, 0, G_OPTION_ARG_DOUBLE, &params->theta, "Tilt (theta)", "[float]" },
  129. { "tau", 0, 0, G_OPTION_ARG_DOUBLE, &params->tau, "Pixel size (theta)", "[float]" },
  130. { "psi", 0, 0, G_OPTION_ARG_DOUBLE, &params->psi, "Misalignment (psi)", "[float]" },
  131. { "px", 0, 0, G_OPTION_ARG_DOUBLE, &params->px, "X coordinate of axis", "[float]" },
  132. { "py", 0, 0, G_OPTION_ARG_DOUBLE, &params->py, "Y coordinate of axis", "[float]" },
  133. { "px-variation", 0, 0, G_OPTION_ARG_DOUBLE, &params->px_variation, "X axis variation", "[float]" },
  134. { "vx", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[0], "X coordinate of box origin", "[float]" },
  135. { "vy", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[1], "Y coordinate of box origin", "[float]" },
  136. { "vz", 0, 0, G_OPTION_ARG_DOUBLE, &params->v_origin[2], "Z coordinate of box origin", "[float]" },
  137. { "vw", 0, 0, G_OPTION_ARG_INT, &params->v_size[0], "Width of box", "[int]" },
  138. { "vh", 0, 0, G_OPTION_ARG_INT, &params->v_size[1], "Height of box", "[int]" },
  139. { "vd", 0, 0, G_OPTION_ARG_INT, &params->v_size[2], "Depth of box", "[int]" },
  140. { "interactive", 0, 0, G_OPTION_ARG_NONE, &params->interactive, "Start interactive mode", "" },
  141. { NULL }
  142. };
  143. context = g_option_context_new ("- laminographic reconstruction");
  144. g_option_context_add_main_entries (context, entries, NULL);
  145. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  146. g_print ("option parsing failed: %s\n", error->message);
  147. exit (1);
  148. }
  149. /* Copy entry information for later reference */
  150. params->entries = g_memdup (entries, sizeof (entries));
  151. if (params->interactive)
  152. goto parse_params_cleanup;
  153. if (!params_okay (params)) {
  154. err ("Parameters missing.\n\n");
  155. g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
  156. exit (1);
  157. }
  158. parse_params_cleanup:
  159. g_option_context_free (context);
  160. }
  161. int
  162. main (int argc, char **argv)
  163. {
  164. Params params = {
  165. .interactive = FALSE,
  166. .radios = NULL,
  167. .darks = NULL,
  168. .flats = NULL,
  169. .output = "volume-%i.tif",
  170. .width = 0,
  171. .height = 0,
  172. .num_radios = 0,
  173. .num_darks = 1,
  174. .theta = G_MAXDOUBLE,
  175. .dark_scale = 1.0,
  176. .tau = 1.0,
  177. .psi = 0.0,
  178. .px = G_MAXDOUBLE,
  179. .py = G_MAXDOUBLE,
  180. .px_variation = 0.0,
  181. .v_size = {256, 256, 256},
  182. .v_origin = {0.0, 0.0, 0.0},
  183. .cache = NULL,
  184. };
  185. #if !(GLIB_CHECK_VERSION (2, 36, 0))
  186. g_type_init ();
  187. #endif
  188. parse_params (&params, argc, argv);
  189. if (params.interactive)
  190. start_shell (&params);
  191. else
  192. run_simple_reconstruction (&params, NULL);
  193. return 0;
  194. }