ufo-anka-backproject-task.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Copyright (C) 2011-2014 Karlsruhe Institute of Technology
  3. *
  4. * This file is part of Ufo.
  5. *
  6. * This library is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation, either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <glib.h>
  22. #include <glib/gprintf.h>
  23. #ifdef __APPLE__
  24. #include <OpenCL/cl.h>
  25. #else
  26. #include <CL/cl.h>
  27. #endif
  28. #include "ufo-anka-backproject-task.h"
  29. #include "lamino-roi.h"
  30. /* Copy only neccessary projection region */
  31. /* TODO: make this a parameter? */
  32. #define COPY_PROJECTION_REGION 0
  33. #define EXTRACT_FLOAT(region, index) g_value_get_float (g_value_array_get_nth ((region), (index)))
  34. #define REGION_SIZE(region) ((EXTRACT_INT ((region), 2)) == 0) ? 0 : \
  35. ((EXTRACT_INT ((region), 1) - EXTRACT_INT ((region), 0) - 1) /\
  36. EXTRACT_INT ((region), 2) + 1)
  37. #define PAD_TO_DIVIDE(dividend, divisor) ((dividend) + (divisor) - (dividend) % (divisor))
  38. /**
  39. * SECTION:ufo-anka-backproject-task
  40. * @Short_description: Backproject projection by projection
  41. * @Title: anka_backproject
  42. *
  43. */
  44. typedef enum {
  45. PARAM_Z,
  46. PARAM_CENTER,
  47. PARAM_LAMINO
  48. } Param;
  49. struct _UfoAnkaBackprojectTaskPrivate {
  50. /* private */
  51. gboolean generated;
  52. guint count;
  53. /* sine and cosine table size based on BURST */
  54. gsize table_size;
  55. /* OpenCL */
  56. cl_context context;
  57. cl_kernel vector_kernel;
  58. cl_kernel scalar_kernel;
  59. cl_sampler sampler;
  60. /* Buffered images for invoking backprojection on BURST projections at once.
  61. * We potentially don't need to copy the last image and can use the one from
  62. * framework directly but it seems to have no performance effects. */
  63. cl_mem images[BURST];
  64. /* properties */
  65. GValueArray *x_region;
  66. GValueArray *y_region;
  67. GValueArray *region;
  68. GValueArray *center;
  69. GValueArray *projection_offset;
  70. float sines[BURST], cosines[BURST];
  71. guint num_projections;
  72. gfloat overall_angle;
  73. gfloat tomo_angle;
  74. gfloat lamino_angle;
  75. gfloat z;
  76. gfloat roll_angle;
  77. Param parameter;
  78. };
  79. static void ufo_task_interface_init (UfoTaskIface *iface);
  80. G_DEFINE_TYPE_WITH_CODE (UfoAnkaBackprojectTask, ufo_anka_backproject_task, UFO_TYPE_TASK_NODE,
  81. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  82. ufo_task_interface_init))
  83. #define UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_ANKA_BACKPROJECT_TASK, UfoAnkaBackprojectTaskPrivate))
  84. enum {
  85. PROP_0,
  86. PROP_X_REGION,
  87. PROP_Y_REGION,
  88. PROP_Z,
  89. PROP_REGION,
  90. PROP_PROJECTION_OFFSET,
  91. PROP_CENTER,
  92. PROP_NUM_PROJECTIONS,
  93. PROP_OVERALL_ANGLE,
  94. PROP_TOMO_ANGLE,
  95. PROP_LAMINO_ANGLE,
  96. PROP_PARAMETER,
  97. PROP_ROLL_ANGLE,
  98. N_PROPERTIES
  99. };
  100. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  101. static void
  102. set_region (GValueArray *src, GValueArray **dst)
  103. {
  104. if (EXTRACT_INT (src, 0) > EXTRACT_INT (src, 1)) {
  105. g_log ("Ufo", G_LOG_LEVEL_CRITICAL,
  106. "Error <%s:%i>: Invalid region [\"from\", \"to\", \"step\"]: [%d, %d, %d], "\
  107. "\"from\" has to be less than or equal to \"to\"",
  108. __FILE__, __LINE__,
  109. EXTRACT_INT (src, 0), EXTRACT_INT (src, 1), EXTRACT_INT (src, 2));
  110. }
  111. else {
  112. g_value_array_free (*dst);
  113. *dst = g_value_array_copy (src);
  114. }
  115. }
  116. static void
  117. copy_to_image (UfoBuffer *input,
  118. cl_mem output_image,
  119. cl_command_queue cmd_queue,
  120. size_t origin[3],
  121. size_t region[3],
  122. gint in_width)
  123. {
  124. const UfoBufferLocation location = ufo_buffer_get_location (input);
  125. cl_mem input_data;
  126. cl_event event;
  127. gfloat *input_data_host;
  128. size_t src_offset;
  129. input_data = ufo_buffer_get_device_image (input, cmd_queue);
  130. UFO_RESOURCES_CHECK_CLERR (clEnqueueCopyImage (cmd_queue,
  131. input_data,
  132. output_image,
  133. origin,
  134. origin,
  135. region,
  136. 0,
  137. NULL,
  138. &event));
  139. UFO_RESOURCES_CHECK_CLERR (clWaitForEvents (1, &event));
  140. UFO_RESOURCES_CHECK_CLERR (clReleaseEvent (event));
  141. }
  142. UfoNode *
  143. ufo_anka_backproject_task_new (void)
  144. {
  145. return UFO_NODE (g_object_new (UFO_TYPE_ANKA_BACKPROJECT_TASK, NULL));
  146. }
  147. static void
  148. ufo_anka_backproject_task_setup (UfoTask *task,
  149. UfoResources *resources,
  150. GError **error)
  151. {
  152. UfoAnkaBackprojectTaskPrivate *priv;
  153. cl_int cl_error;
  154. gint i;
  155. gchar *vector_kernel_name, *kernel_filename;
  156. vector_kernel_name = g_strdup_printf ("backproject_burst_%d", BURST);
  157. if (!vector_kernel_name) {
  158. g_warning ("Error making burst kernel name");
  159. }
  160. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  161. priv->context = ufo_resources_get_context (resources);
  162. switch (priv->parameter) {
  163. case PARAM_Z:
  164. kernel_filename = g_strdup ("z_kernel.cl");
  165. break;
  166. case PARAM_CENTER:
  167. kernel_filename = g_strdup ("center_kernel.cl");
  168. break;
  169. case PARAM_LAMINO:
  170. kernel_filename = g_strdup ("lamino_kernel.cl");
  171. break;
  172. default:
  173. g_warning ("Unkown varying parameter");
  174. break;
  175. }
  176. priv->vector_kernel = ufo_resources_get_kernel (resources, kernel_filename,
  177. vector_kernel_name, error);
  178. priv->scalar_kernel = ufo_resources_get_kernel (resources, kernel_filename,
  179. "backproject_burst_1", error);
  180. priv->sampler = clCreateSampler (priv->context,
  181. (cl_bool) FALSE,
  182. CL_ADDRESS_CLAMP,
  183. CL_FILTER_LINEAR,
  184. &cl_error);
  185. UFO_RESOURCES_CHECK_CLERR (clRetainContext (priv->context));
  186. UFO_RESOURCES_CHECK_CLERR (cl_error);
  187. if (priv->vector_kernel) {
  188. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->vector_kernel));
  189. }
  190. if (priv->scalar_kernel) {
  191. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->scalar_kernel));
  192. }
  193. for (i = 0; i < BURST; i++) {
  194. priv->images[i] = NULL;
  195. }
  196. switch (BURST) {
  197. case 1: priv->table_size = sizeof (cl_float); break;
  198. case 2: priv->table_size = sizeof (cl_float2); break;
  199. case 4: priv->table_size = sizeof (cl_float4); break;
  200. case 8: priv->table_size = sizeof (cl_float8); break;
  201. case 16: priv->table_size = sizeof (cl_float16); break;
  202. default: g_warning ("Unsupported vector size"); break;
  203. }
  204. g_free (vector_kernel_name);
  205. g_free (kernel_filename);
  206. }
  207. static void
  208. ufo_anka_backproject_task_get_requisition (UfoTask *task,
  209. UfoBuffer **inputs,
  210. UfoRequisition *requisition)
  211. {
  212. UfoAnkaBackprojectTaskPrivate *priv;
  213. gfloat start, stop, step;
  214. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  215. start = EXTRACT_FLOAT (priv->region, 0);
  216. stop = EXTRACT_FLOAT (priv->region, 1);
  217. step = EXTRACT_FLOAT (priv->region, 2);
  218. if (!priv->num_projections) {
  219. g_warning ("Number of projections has not been set");
  220. }
  221. if (step == 0.0f) {
  222. g_warning ("Step in region is 0");
  223. }
  224. requisition->n_dims = 3;
  225. requisition->dims[0] = REGION_SIZE (priv->x_region);
  226. requisition->dims[1] = REGION_SIZE (priv->y_region);
  227. requisition->dims[2] = (gint) ceil ((stop - start) / step);
  228. }
  229. static guint
  230. ufo_anka_backproject_task_get_num_inputs (UfoTask *task)
  231. {
  232. return 1;
  233. }
  234. static guint
  235. ufo_anka_backproject_task_get_num_dimensions (UfoTask *task,
  236. guint input)
  237. {
  238. g_return_val_if_fail (input == 0, 0);
  239. return 3;
  240. }
  241. static gboolean
  242. ufo_anka_backproject_task_equal_real (UfoNode *n1,
  243. UfoNode *n2)
  244. {
  245. g_return_val_if_fail (UFO_IS_ANKA_BACKPROJECT_TASK (n1) && UFO_IS_ANKA_BACKPROJECT_TASK (n2), FALSE);
  246. return UFO_ANKA_BACKPROJECT_TASK (n1)->priv->vector_kernel == UFO_ANKA_BACKPROJECT_TASK (n2)->priv->vector_kernel;
  247. }
  248. static UfoTaskMode
  249. ufo_anka_backproject_task_get_mode (UfoTask *task)
  250. {
  251. return UFO_TASK_MODE_REDUCTOR | UFO_TASK_MODE_GPU;
  252. }
  253. static gboolean
  254. ufo_anka_backproject_task_process (UfoTask *task,
  255. UfoBuffer **inputs,
  256. UfoBuffer *output,
  257. UfoRequisition *requisition)
  258. {
  259. UfoAnkaBackprojectTaskPrivate *priv;
  260. UfoRequisition in_req;
  261. UfoGpuNode *node;
  262. UfoProfiler *profiler;
  263. gfloat tomo_angle, *sines, *cosines;
  264. gint i, index;
  265. gint cumulate;
  266. gsize table_size;
  267. gboolean scalar;
  268. /* regions stripped off the "to" value */
  269. gfloat x_region[2], y_region[2], z_region[2], x_center[2], z_ends[2], lamino_angles[2],
  270. y_center, sin_lamino, cos_lamino, norm_factor, sin_roll, cos_roll;
  271. gint x_copy_region[2], y_copy_region[2];
  272. cl_kernel kernel;
  273. cl_command_queue cmd_queue;
  274. cl_mem out_mem;
  275. cl_int cl_error;
  276. /* image creation and copying */
  277. cl_image_format image_fmt;
  278. size_t origin[3];
  279. size_t region[3];
  280. /* keep the warp size satisfied but make sure the local grid is localized
  281. * around a point in 3D for efficient caching */
  282. const gint real_size[4] = {requisition->dims[0], requisition->dims[1], requisition->dims[2], 0};
  283. const gsize local_work_size[] = {16, 8, 8};
  284. gsize global_work_size[3];
  285. global_work_size[0] = requisition->dims[0] % local_work_size[0] ?
  286. PAD_TO_DIVIDE (requisition->dims[0], local_work_size[0]) :
  287. requisition->dims[0];
  288. global_work_size[1] = requisition->dims[1] % local_work_size[1] ?
  289. PAD_TO_DIVIDE (requisition->dims[1], local_work_size[1]) :
  290. requisition->dims[1];
  291. global_work_size[2] = requisition->dims[2] % local_work_size[2] ?
  292. PAD_TO_DIVIDE (requisition->dims[2], local_work_size[2]) :
  293. requisition->dims[2];
  294. priv = UFO_ANKA_BACKPROJECT_TASK (task)->priv;
  295. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  296. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  297. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  298. ufo_buffer_get_requisition (inputs[0], &in_req);
  299. index = priv->count % BURST;
  300. tomo_angle = priv->tomo_angle > -G_MAXFLOAT ? priv->tomo_angle :
  301. priv->overall_angle * priv->count / priv->num_projections;
  302. norm_factor = priv->overall_angle / priv->num_projections;
  303. priv->sines[index] = sin (tomo_angle);
  304. priv->cosines[index] = cos (tomo_angle);
  305. x_region[0] = (gfloat) EXTRACT_INT (priv->x_region, 0);
  306. x_region[1] = (gfloat) EXTRACT_INT (priv->x_region, 2);
  307. y_region[0] = (gfloat) EXTRACT_INT (priv->y_region, 0);
  308. y_region[1] = (gfloat) EXTRACT_INT (priv->y_region, 2);
  309. if (priv->parameter == PARAM_Z) {
  310. z_ends[0] = z_region[0] = EXTRACT_FLOAT (priv->region, 0);
  311. z_region[1] = EXTRACT_FLOAT (priv->region, 2);
  312. z_ends[1] = EXTRACT_FLOAT (priv->region, 1);
  313. } else {
  314. z_ends[0] = z_region[0] = priv->z;
  315. z_ends[1] = priv->z + 1.0f;
  316. }
  317. if (priv->parameter == PARAM_CENTER) {
  318. x_center[0] = EXTRACT_FLOAT (priv->region, 0) - EXTRACT_INT (priv->projection_offset, 0);
  319. x_center[1] = EXTRACT_FLOAT (priv->region, 2);
  320. } else {
  321. x_center[0] = x_center[1] = EXTRACT_FLOAT (priv->center, 0) - EXTRACT_INT (priv->projection_offset, 0);
  322. }
  323. y_center = EXTRACT_FLOAT (priv->center, 1) - EXTRACT_INT (priv->projection_offset, 1);
  324. if (priv->parameter == PARAM_LAMINO) {
  325. lamino_angles[0] = EXTRACT_FLOAT (priv->region, 0);
  326. lamino_angles[1] = EXTRACT_FLOAT (priv->region, 2);
  327. } else {
  328. lamino_angles[0] = lamino_angles[1] = priv->lamino_angle;
  329. }
  330. sin_lamino = sinf (priv->lamino_angle);
  331. cos_lamino = cosf (priv->lamino_angle);
  332. /* Minus the value because we are rotating back */
  333. sin_roll = sinf (-priv->roll_angle);
  334. cos_roll = cosf (-priv->roll_angle);
  335. scalar = priv->count >= priv->num_projections / BURST * BURST ? 1 : 0;
  336. /* If COPY_PROJECTION_REGION is True we copy only the part necessary */
  337. /* for a given tomographic and laminographic angle */
  338. /* TODO: Extend the region determination to be able to handle PARAM_LAMINO */
  339. if (COPY_PROJECTION_REGION && priv->parameter != PARAM_LAMINO) {
  340. determine_x_region (x_copy_region, priv->x_region, priv->y_region, tomo_angle,
  341. EXTRACT_FLOAT (priv->center, 0), in_req.dims[0]);
  342. determine_y_region (y_copy_region, priv->x_region, priv->y_region, z_ends,
  343. tomo_angle, priv->lamino_angle, EXTRACT_FLOAT (priv->center, 1),
  344. in_req.dims[1]);
  345. origin[0] = x_copy_region[0];
  346. origin[1] = y_copy_region[0];
  347. origin[2] = 0;
  348. region[0] = x_copy_region[1] - x_copy_region[0];
  349. region[1] = y_copy_region[1] - y_copy_region[0];
  350. } else {
  351. origin[0] = origin[1] = origin[2] = 0;
  352. region[0] = in_req.dims[0];
  353. region[1] = in_req.dims[1];
  354. }
  355. region[2] = 1;
  356. if (priv->images[index] == NULL) {
  357. /* TODO: dangerous, don't rely on the ufo-buffer */
  358. image_fmt.image_channel_order = CL_INTENSITY;
  359. image_fmt.image_channel_data_type = CL_FLOAT;
  360. /* TODO: what with the "other" API? */
  361. priv->images[index] = clCreateImage2D (priv->context,
  362. CL_MEM_READ_ONLY,
  363. &image_fmt,
  364. in_req.dims[0],
  365. in_req.dims[1],
  366. 0,
  367. NULL,
  368. &cl_error);
  369. UFO_RESOURCES_CHECK_CLERR (cl_error);
  370. }
  371. copy_to_image (inputs[0], priv->images[index], cmd_queue, origin, region, in_req.dims[0]);
  372. if (scalar) {
  373. kernel = priv->scalar_kernel;
  374. cumulate = priv->count;
  375. table_size = sizeof (cl_float);
  376. sines = &priv->sines[index];
  377. cosines = &priv->cosines[index];
  378. i = 1;
  379. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, 0, sizeof (cl_mem), &priv->images[index]));
  380. } else {
  381. kernel = priv->vector_kernel;
  382. cumulate = priv->count + 1 == BURST ? 0 : 1;
  383. table_size = priv->table_size;
  384. sines = priv->sines;
  385. cosines = priv->cosines;
  386. i = BURST;
  387. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, index, sizeof (cl_mem), &priv->images[index]));
  388. }
  389. if (scalar || index == BURST - 1) {
  390. /* Execute the kernel after BURST images have arrived, i.e. we use more
  391. * projections at one invocation, so the number of read/writes to the
  392. * result is reduced by a factor of BURST. If there are not enough
  393. * projecttions left, execute the scalar kernel */
  394. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_mem), &out_mem));
  395. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_sampler), &priv->sampler));
  396. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_int3), real_size));
  397. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), x_center));
  398. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), (cl_float *) &y_center));
  399. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), x_region));
  400. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), y_region));
  401. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), z_region));
  402. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), lamino_angles));
  403. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &sin_lamino));
  404. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &cos_lamino));
  405. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, sines));
  406. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, cosines));
  407. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &norm_factor));
  408. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &sin_roll));
  409. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &cos_roll));
  410. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i, sizeof (cl_int), (cl_int *) &cumulate));
  411. profiler = ufo_task_node_get_profiler (UFO_TASK_NODE (task));
  412. ufo_profiler_call (profiler, cmd_queue, kernel, 3, global_work_size, local_work_size);
  413. }
  414. priv->count++;
  415. return TRUE;
  416. }
  417. static gboolean
  418. ufo_anka_backproject_task_generate (UfoTask *task,
  419. UfoBuffer *output,
  420. UfoRequisition *requisition)
  421. {
  422. UfoAnkaBackprojectTaskPrivate *priv;
  423. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  424. if (priv->generated) {
  425. return FALSE;
  426. }
  427. priv->generated = TRUE;
  428. return TRUE;
  429. }
  430. static void
  431. ufo_anka_backproject_task_finalize (GObject *object)
  432. {
  433. UfoAnkaBackprojectTaskPrivate *priv;
  434. gint i;
  435. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  436. g_value_array_free (priv->x_region);
  437. g_value_array_free (priv->y_region);
  438. g_value_array_free (priv->region);
  439. g_value_array_free (priv->projection_offset);
  440. g_value_array_free (priv->center);
  441. if (priv->vector_kernel) {
  442. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->vector_kernel));
  443. priv->vector_kernel = NULL;
  444. }
  445. if (priv->scalar_kernel) {
  446. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->scalar_kernel));
  447. priv->scalar_kernel = NULL;
  448. }
  449. if (priv->context) {
  450. UFO_RESOURCES_CHECK_CLERR (clReleaseContext (priv->context));
  451. priv->context = NULL;
  452. }
  453. if (priv->sampler) {
  454. UFO_RESOURCES_CHECK_CLERR (clReleaseSampler (priv->sampler));
  455. priv->sampler = NULL;
  456. }
  457. for (i = 0; i < BURST; i++) {
  458. if (priv->images[i] != NULL) {
  459. UFO_RESOURCES_CHECK_CLERR (clReleaseMemObject (priv->images[i]));
  460. priv->images[i] = NULL;
  461. }
  462. }
  463. G_OBJECT_CLASS (ufo_anka_backproject_task_parent_class)->finalize (object);
  464. }
  465. static void
  466. ufo_task_interface_init (UfoTaskIface *iface)
  467. {
  468. iface->setup = ufo_anka_backproject_task_setup;
  469. iface->get_requisition = ufo_anka_backproject_task_get_requisition;
  470. iface->get_num_inputs = ufo_anka_backproject_task_get_num_inputs;
  471. iface->get_num_dimensions = ufo_anka_backproject_task_get_num_dimensions;
  472. iface->get_mode = ufo_anka_backproject_task_get_mode;
  473. iface->process = ufo_anka_backproject_task_process;
  474. iface->generate = ufo_anka_backproject_task_generate;
  475. }
  476. static void
  477. ufo_anka_backproject_task_set_property (GObject *object,
  478. guint property_id,
  479. const GValue *value,
  480. GParamSpec *pspec)
  481. {
  482. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  483. GValueArray *array;
  484. switch (property_id) {
  485. case PROP_X_REGION:
  486. array = (GValueArray *) g_value_get_boxed (value);
  487. set_region (array, &priv->x_region);
  488. break;
  489. case PROP_Y_REGION:
  490. array = (GValueArray *) g_value_get_boxed (value);
  491. set_region (array, &priv->y_region);
  492. break;
  493. case PROP_Z:
  494. priv->z = g_value_get_float (value);
  495. break;
  496. case PROP_REGION:
  497. array = (GValueArray *) g_value_get_boxed (value);
  498. g_value_array_free (priv->region);
  499. priv->region = g_value_array_copy (array);
  500. break;
  501. case PROP_PROJECTION_OFFSET:
  502. array = (GValueArray *) g_value_get_boxed (value);
  503. g_value_array_free (priv->projection_offset);
  504. priv->projection_offset = g_value_array_copy (array);
  505. break;
  506. case PROP_CENTER:
  507. array = (GValueArray *) g_value_get_boxed (value);
  508. g_value_array_free (priv->center);
  509. priv->center = g_value_array_copy (array);
  510. break;
  511. case PROP_NUM_PROJECTIONS:
  512. priv->num_projections = g_value_get_uint (value);
  513. break;
  514. case PROP_OVERALL_ANGLE:
  515. priv->overall_angle = g_value_get_float (value);
  516. break;
  517. case PROP_TOMO_ANGLE:
  518. priv->tomo_angle = g_value_get_float (value);
  519. break;
  520. case PROP_LAMINO_ANGLE:
  521. priv->lamino_angle = g_value_get_float (value);
  522. break;
  523. case PROP_ROLL_ANGLE:
  524. priv->roll_angle = g_value_get_float (value);
  525. break;
  526. case PROP_PARAMETER:
  527. if (!g_strcmp0 (g_value_get_string (value), "z")) {
  528. priv->parameter = PARAM_Z;
  529. } else if (!g_strcmp0 (g_value_get_string (value), "x-center")) {
  530. priv->parameter = PARAM_CENTER;
  531. } else if (!g_strcmp0 (g_value_get_string (value), "lamino-angle")) {
  532. priv->parameter = PARAM_LAMINO;
  533. }
  534. break;
  535. default:
  536. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  537. break;
  538. }
  539. }
  540. static void
  541. ufo_anka_backproject_task_get_property (GObject *object,
  542. guint property_id,
  543. GValue *value,
  544. GParamSpec *pspec)
  545. {
  546. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  547. switch (property_id) {
  548. case PROP_X_REGION:
  549. g_value_set_boxed (value, priv->x_region);
  550. break;
  551. case PROP_Y_REGION:
  552. g_value_set_boxed (value, priv->y_region);
  553. break;
  554. case PROP_Z:
  555. g_value_set_float (value, priv->z);
  556. break;
  557. case PROP_REGION:
  558. g_value_set_boxed (value, priv->region);
  559. break;
  560. case PROP_PROJECTION_OFFSET:
  561. g_value_set_boxed (value, priv->projection_offset);
  562. break;
  563. case PROP_CENTER:
  564. g_value_set_boxed (value, priv->center);
  565. break;
  566. case PROP_NUM_PROJECTIONS:
  567. g_value_set_uint (value, priv->num_projections);
  568. break;
  569. case PROP_OVERALL_ANGLE:
  570. g_value_set_float (value, priv->overall_angle);
  571. break;
  572. case PROP_TOMO_ANGLE:
  573. g_value_set_float (value, priv->tomo_angle);
  574. break;
  575. case PROP_LAMINO_ANGLE:
  576. g_value_set_float (value, priv->lamino_angle);
  577. break;
  578. case PROP_ROLL_ANGLE:
  579. g_value_set_float (value, priv->roll_angle);
  580. break;
  581. case PROP_PARAMETER:
  582. switch (priv->parameter) {
  583. case PARAM_Z:
  584. g_value_set_string (value, "z");
  585. break;
  586. case PARAM_CENTER:
  587. g_value_set_string (value, "x-center");
  588. break;
  589. case PARAM_LAMINO:
  590. g_value_set_string (value, "lamino-angle");
  591. break;
  592. }
  593. break;
  594. default:
  595. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  596. break;
  597. }
  598. }
  599. static void
  600. ufo_anka_backproject_task_class_init (UfoAnkaBackprojectTaskClass *klass)
  601. {
  602. GObjectClass *oclass;
  603. UfoNodeClass *node_class;
  604. oclass = G_OBJECT_CLASS (klass);
  605. node_class = UFO_NODE_CLASS (klass);
  606. oclass->finalize = ufo_anka_backproject_task_finalize;
  607. oclass->set_property = ufo_anka_backproject_task_set_property;
  608. oclass->get_property = ufo_anka_backproject_task_get_property;
  609. GParamSpec *region_vals = g_param_spec_int ("region_values",
  610. "Region values",
  611. "Elements in regions",
  612. G_MININT,
  613. G_MAXINT,
  614. (gint) 0,
  615. G_PARAM_READWRITE);
  616. GParamSpec *float_region_vals = g_param_spec_float ("float_region_values",
  617. "Float Region values",
  618. "Elements in float regions",
  619. -G_MAXFLOAT,
  620. G_MAXFLOAT,
  621. 0.0f,
  622. G_PARAM_READWRITE);
  623. properties[PROP_X_REGION] =
  624. g_param_spec_value_array ("x-region",
  625. "X region for reconstruction as (from, to, step)",
  626. "X region for reconstruction as (from, to, step)",
  627. region_vals,
  628. G_PARAM_READWRITE);
  629. properties[PROP_Y_REGION] =
  630. g_param_spec_value_array ("y-region",
  631. "Y region for reconstruction as (from, to, step)",
  632. "Y region for reconstruction as (from, to, step)",
  633. region_vals,
  634. G_PARAM_READWRITE);
  635. properties[PROP_Z] =
  636. g_param_spec_float ("z",
  637. "Z coordinate of the reconstructed slice",
  638. "Z coordinate of the reconstructed slice",
  639. -G_MAXFLOAT,
  640. G_MAXFLOAT,
  641. 0.0f,
  642. G_PARAM_READWRITE);
  643. properties[PROP_REGION] =
  644. g_param_spec_value_array ("region",
  645. "Region for the parameter along z-axis as (from, to, step)",
  646. "Region for the parameter along z-axis as (from, to, step)",
  647. float_region_vals,
  648. G_PARAM_READWRITE);
  649. properties[PROP_PROJECTION_OFFSET] =
  650. g_param_spec_value_array ("projection-offset",
  651. "Offset to projection data as (x, y)",
  652. "Offset to projection data as (x, y) for the case input data \
  653. is cropped to the necessary range of interest",
  654. region_vals,
  655. G_PARAM_READWRITE);
  656. properties[PROP_CENTER] =
  657. g_param_spec_value_array ("center",
  658. "Center of the volume with respect to projections (x, y)",
  659. "Center of the volume with respect to projections (x, y), (rotation axes)",
  660. float_region_vals,
  661. G_PARAM_READWRITE);
  662. properties[PROP_OVERALL_ANGLE] =
  663. g_param_spec_float ("overall-angle",
  664. "Angle covered by all projections",
  665. "Angle covered by all projections (can be negative for negative steps "
  666. "in case only num-projections is specified",
  667. -G_MAXFLOAT,
  668. G_MAXFLOAT,
  669. G_PI,
  670. G_PARAM_READWRITE);
  671. properties[PROP_NUM_PROJECTIONS] =
  672. g_param_spec_uint ("num-projections",
  673. "Number of projections",
  674. "Number of projections",
  675. 0,
  676. 16384,
  677. 0,
  678. G_PARAM_READWRITE);
  679. properties[PROP_TOMO_ANGLE] =
  680. g_param_spec_float ("tomo-angle",
  681. "Tomographic rotation angle in radians",
  682. "Tomographic rotation angle in radians (used for acquiring projections)",
  683. -G_MAXFLOAT,
  684. G_MAXFLOAT,
  685. 0.0f,
  686. G_PARAM_READWRITE);
  687. properties[PROP_LAMINO_ANGLE] =
  688. g_param_spec_float ("lamino-angle",
  689. "Absolute laminogrpahic angle in radians",
  690. "Absolute laminogrpahic angle in radians determining the sample tilt",
  691. 0.0f,
  692. (float) G_PI / 2,
  693. 0.0f,
  694. G_PARAM_READWRITE);
  695. properties[PROP_ROLL_ANGLE] =
  696. g_param_spec_float ("roll-angle",
  697. "Sample angular misalignment to the side (roll) in radians",
  698. "Sample angular misalignment to the side (roll) in radians (CW is positive)",
  699. 0.0f,
  700. (float) G_PI / 2,
  701. 0.0f,
  702. G_PARAM_READWRITE);
  703. properties[PROP_PARAMETER] =
  704. g_param_spec_string ("parameter",
  705. "Which paramter will be varied along the z-axis",
  706. "Which paramter will be varied along the z-axis, from \"z\", \"x-center\", \"lamino-angle\"",
  707. "z",
  708. G_PARAM_READWRITE);
  709. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  710. g_object_class_install_property (oclass, i, properties[i]);
  711. node_class->equal = ufo_anka_backproject_task_equal_real;
  712. g_type_class_add_private (klass, sizeof(UfoAnkaBackprojectTaskPrivate));
  713. }
  714. static void
  715. ufo_anka_backproject_task_init(UfoAnkaBackprojectTask *self)
  716. {
  717. UfoAnkaBackprojectTaskPrivate *priv;
  718. self->priv = priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(self);
  719. guint i;
  720. GValue int_zero = G_VALUE_INIT;
  721. GValue float_zero = G_VALUE_INIT;
  722. g_value_init (&int_zero, G_TYPE_INT);
  723. g_value_init (&float_zero, G_TYPE_FLOAT);
  724. g_value_set_int (&int_zero, 0);
  725. g_value_set_float (&float_zero, 0.0f);
  726. self->priv->x_region = g_value_array_new (3);
  727. self->priv->y_region = g_value_array_new (3);
  728. self->priv->region = g_value_array_new (3);
  729. self->priv->z = 0.0f;
  730. self->priv->projection_offset = g_value_array_new (2);
  731. self->priv->center = g_value_array_new (2);
  732. for (i = 0; i < 3; i++) {
  733. g_value_array_insert (self->priv->x_region, i, &int_zero);
  734. g_value_array_insert (self->priv->y_region, i, &int_zero);
  735. g_value_array_insert (self->priv->region, i, &float_zero);
  736. if (i < 2) {
  737. g_value_array_insert (self->priv->projection_offset, i, &int_zero);
  738. g_value_array_insert (self->priv->center, i, &float_zero);
  739. }
  740. }
  741. self->priv->num_projections = 0;
  742. self->priv->overall_angle = G_PI;
  743. self->priv->tomo_angle = -G_MAXFLOAT;
  744. self->priv->lamino_angle = 0.0f;
  745. self->priv->roll_angle = 0.0f;
  746. self->priv->parameter = PARAM_Z;
  747. self->priv->count = 0;
  748. self->priv->generated = FALSE;
  749. }