ufo-anka-backproject-task.c 31 KB

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