convolution_mex_orig.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* kernel_readin.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <math.h>
  6. #include <string.h>
  7. #include "mex.h"
  8. #include "defs.h"
  9. #define MATLAB_KERNELS (prhs[0])
  10. #define GEOMETRY (prhs[1])
  11. #define DOSE (plhs[0])
  12. /* #define TERMA (plhs[1])
  13. #define KERMAC (plhs[2])
  14. #define DEFF (plhs[3])
  15. #define TERMA_MASK (plhs[4])
  16. #define DOSE_MASK (plhs[5]) */
  17. //function prototypes
  18. void check_MATLAB_KERNELS(const mxArray *);
  19. void check_GEOMETRY(const mxArray *);
  20. void load_kernels(const mxArray *, MONO_KERNELS *);
  21. void load_Geometry(const mxArray *, DOUBLE_GRID *, BEAM *);
  22. int calc_deff(DOUBLE_GRID *,DOUBLE_GRID *, DOUBLE_GRID *, BEAM *);
  23. int terma_kerma(DOUBLE_GRID *,DOUBLE_GRID *,DOUBLE_GRID *,MONO_KERNELS *,DOUBLE_GRID *);
  24. int make_poly_kernel(MONO_KERNELS *, KERNEL *);
  25. int calc_dose(DOUBLE_GRID *,DOUBLE_GRID *,DOUBLE_GRID *,KERNEL *,BEAM *, DOUBLE_GRID *);
  26. int terma_dose_masks(DOUBLE_GRID *, DOUBLE_GRID *, BEAM *);
  27. char errstr[200]; // error string that all routines have access to
  28. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  29. {
  30. int dims[3]; // dimensions of input arrays
  31. // matlab arrays for easy output if need be (just comment these lines out and uncomment
  32. // the #define lines above and the mxDestroy lines below).
  33. mxArray *TERMA, *KERMAC, *DEFF, *TERMA_MASK, *DOSE_MASK;
  34. char tmpstr[200];
  35. DOUBLE_GRID density;
  36. DOUBLE_GRID terma_mask;
  37. DOUBLE_GRID dose_mask;
  38. DOUBLE_GRID deff;
  39. DOUBLE_GRID terma;
  40. DOUBLE_GRID kermac;
  41. DOUBLE_GRID dose;
  42. MONO_KERNELS mono_kernels;
  43. KERNEL poly_kernel;
  44. BEAM beam;
  45. // check and load-in input data
  46. check_MATLAB_KERNELS(MATLAB_KERNELS); // see function comments for proper input format
  47. check_GEOMETRY(GEOMETRY); // see function comments for proper input format
  48. load_kernels(MATLAB_KERNELS, &mono_kernels);
  49. load_Geometry(GEOMETRY,&density,&beam);
  50. // copy density grid geometry to all other grids on interest
  51. copy_grid_geometry(&density,&terma_mask);
  52. copy_grid_geometry(&density,&dose_mask);
  53. copy_grid_geometry(&density,&deff);
  54. copy_grid_geometry(&density,&terma);
  55. copy_grid_geometry(&density,&kermac);
  56. copy_grid_geometry(&density,&dose);
  57. // Allocate memory for all of the grids
  58. // dimensions of all the grids
  59. dims[0] = density.x_count;
  60. dims[1] = density.y_count;
  61. dims[2] = density.z_count;
  62. // Allocate memory for all of the data grids using Matlab mxArray functions.
  63. // The idea behind this is for the outputs to send the outputs directly to Matlab.
  64. if ((TERMA_MASK = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  65. mexErrMsgTxt("Could not allocate memory for terma_mask grid");
  66. terma_mask.matrix = mxGetPr(TERMA_MASK);
  67. if ((DOSE_MASK = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  68. mexErrMsgTxt("Could not allocate memory for dose_mask grid");
  69. dose_mask.matrix = mxGetPr(DOSE_MASK);
  70. if ((DEFF = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  71. mexErrMsgTxt("Could not allocate memory for deff grid");
  72. deff.matrix = mxGetPr(DEFF);
  73. if ((TERMA = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  74. mexErrMsgTxt("Could not allocate memory for terma grid");
  75. terma.matrix = mxGetPr(TERMA);
  76. if ((KERMAC = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  77. mexErrMsgTxt("Could not allocate memory for kermac grid");
  78. kermac.matrix = mxGetPr(KERMAC);
  79. if ((DOSE = mxCreateNumericArray(3,dims,mxDOUBLE_CLASS,mxREAL))==NULL)
  80. mexErrMsgTxt("Could not allocate memory for dose matrix");
  81. // Point dose matrix pointer at DOSE Matlab matrix data
  82. dose.matrix = mxGetPr(DOSE);
  83. /* start calculations */
  84. // create terma and dose masks
  85. if (SUCCESS != terma_dose_masks(&terma_mask,&dose_mask,&beam))
  86. {
  87. sprintf(tmpstr,"Failed in terma_dose_masks!\n");
  88. strcat(tmpstr,errstr);
  89. strcpy(errstr,tmpstr);
  90. mexErrMsgTxt(errstr);
  91. }
  92. //create polyenergetic kernel from mono kernels and fluence,mu data
  93. if (SUCCESS != make_poly_kernel(&mono_kernels,&poly_kernel) )
  94. {
  95. sprintf(tmpstr,"Failed making polyenergetic kernel!\n");
  96. strcat(tmpstr,errstr);
  97. strcpy(errstr,tmpstr);
  98. mexErrMsgTxt(errstr);
  99. }
  100. //create effective depth array from density array
  101. if (SUCCESS != calc_deff(&density,&deff,&terma_mask,&beam))
  102. {
  103. sprintf(tmpstr,"Failed in calc_deff!\n");
  104. strcat(tmpstr,errstr);
  105. strcpy(errstr,tmpstr);
  106. mexErrMsgTxt(errstr);
  107. }
  108. //create kerma and terma arrays
  109. //note kerma is collision kerma and is used for a kernel hardening correction
  110. if (SUCCESS != terma_kerma(&deff,&terma,&kermac,&mono_kernels,&terma_mask))
  111. {
  112. sprintf(tmpstr,"Failed in terma_kerma calculation!\n");
  113. strcat(tmpstr,errstr);
  114. strcpy(errstr,tmpstr);
  115. mexErrMsgTxt(errstr);
  116. }
  117. //use all this stuff to calculate dose
  118. if ( (SUCCESS != calc_dose(&density,&terma,&dose,&poly_kernel,&beam,&dose_mask)) )
  119. {
  120. sprintf(tmpstr,"Failed calculating dose!\n");
  121. strcat(tmpstr,errstr);
  122. strcpy(errstr,tmpstr);
  123. mexErrMsgTxt(errstr);
  124. }
  125. // Do not free any of the matrices because they are all MATLAB mxArrays that
  126. // are used in the outputs.
  127. // destroy the arrays created with mxCreate
  128. mxDestroyArray(DEFF);
  129. mxDestroyArray(TERMA_MASK);
  130. mxDestroyArray(DOSE_MASK);
  131. mxDestroyArray(TERMA);
  132. mxDestroyArray(KERMAC);
  133. }