ufo-anka-backproject-task.c 33 KB

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