open_beamlet_batch.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* kernel_readin.c */
  2. #include "mex.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. // #define batchfilename "beamletbatch0.bin"
  7. #define BATCHNAME (prhs[0])
  8. #define BEAMLETS (plhs[0]) // output structure
  9. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  10. {
  11. FILE *fid;
  12. mxArray *field, *struc;
  13. int j,k;
  14. int celldims[2]; // dimensions in the output cell array for the beamlets
  15. int dims[3]; // dimensions of sparse grids
  16. int dims2[2]; // dimensions of arrays that will hold indices and data
  17. int Nind; // number of non-zero indices
  18. int Nbeamlets; // number of beamlets in file
  19. int num; // beamlet number
  20. int *intPtr;
  21. float *floatPtr;
  22. char errstr[200];
  23. const int one = 1;
  24. char *batchfilename; // beamlet batch file name
  25. // information for sparse output data:
  26. int nfields = 6;
  27. const char *field_names[6] = {"num","x_count","y_count","z_count",
  28. "non_zero_indices","non_zero_values"};
  29. batchfilename = mxArrayToString(BATCHNAME);
  30. // open the beamlet batch file
  31. if ((fid = fopen(batchfilename,"rb")) == NULL)
  32. {
  33. sprintf(errstr,"Failed to open beamlet batch file %s\n",batchfilename);
  34. mexErrMsgTxt(errstr);
  35. }
  36. // read in the number of beamlets that are in the batch file
  37. if (fread(&Nbeamlets,sizeof(int),1,fid) != 1)
  38. {
  39. sprintf(errstr,"Error reading the number of beamlets from %s\n",batchfilename);
  40. mexErrMsgTxt(errstr);
  41. }
  42. // declare the BEAMLETS structure
  43. celldims[0] = 1;
  44. celldims[1] = Nbeamlets;
  45. if ((BEAMLETS = mxCreateCellArray(2,celldims)) == NULL)
  46. mexErrMsgTxt("Error in the declaration of the beamlets cell array.\n");
  47. // read in each of the beamlets from the file
  48. for (k=0;k<Nbeamlets;k++)
  49. {
  50. // create the structure that will hold the sparse beamlet
  51. if ((struc = mxCreateStructArray(1,&one,nfields,field_names)) == NULL)
  52. {
  53. sprintf(errstr,"Failed to allocate memory for the structure that will hold sparse beamlet %d\n",k);
  54. mexErrMsgTxt(errstr);
  55. }
  56. // read in the beamlet number
  57. if (fread(&num,sizeof(int),1,fid) != 1)
  58. {
  59. sprintf(errstr,"Failed to read in the beamlet number from beamlet %d.\n",k);
  60. mexErrMsgTxt(errstr);
  61. }
  62. // read in the beamlet dimensions
  63. if (fread(dims,sizeof(int),3,fid) != 3)
  64. {
  65. sprintf(errstr,"Failed to read in the dimensions of beamlet %d.\n",k);
  66. mexErrMsgTxt(errstr);
  67. }
  68. // assign fields to the structure for the beamlet number and beamlet dimensions
  69. dims2[0] = 1;
  70. dims2[1] = 1;
  71. if ((field = mxCreateNumericArray(2,dims2,mxINT32_CLASS,mxREAL)) == NULL)
  72. {
  73. sprintf(errstr,"Failed to allocate memory for the dummy field for beamlet %d\n",k);
  74. mexErrMsgTxt(errstr);
  75. }
  76. intPtr = (int *)mxGetData(field);
  77. intPtr[0] = num;
  78. mxSetField(struc,0,"num",mxDuplicateArray(field));
  79. intPtr[0] = dims[0];
  80. mxSetField(struc,0,"x_count",mxDuplicateArray(field));
  81. intPtr[0] = dims[1];
  82. mxSetField(struc,0,"y_count",mxDuplicateArray(field));
  83. intPtr[0] = dims[2];
  84. mxSetField(struc,0,"z_count",mxDuplicateArray(field));
  85. // read in the number of non-zero indices
  86. if (fread(&Nind,sizeof(int),1,fid) != 1)
  87. {
  88. sprintf(errstr,"Failed to read in the number of non-zero indices for beamlet k.\n",k);
  89. mexErrMsgTxt(errstr);
  90. }
  91. // assign fields to the structure for the beamlet dimensions
  92. dims2[0] = 1;
  93. dims2[1] = Nind;
  94. // allocate memory for non-zero indices and read-in the data
  95. if ((field = mxCreateNumericArray(2,dims2,mxINT32_CLASS,mxREAL)) == NULL)
  96. {
  97. sprintf(errstr,"Failed to allocate memory for the index array of beamlet %d\n",k);
  98. mexErrMsgTxt(errstr);
  99. }
  100. intPtr = (int *)mxGetData(field);
  101. if (fread(intPtr,sizeof(int),Nind,fid) != Nind)
  102. {
  103. sprintf(errstr,"Unable to read-in non-zero indices for beamlet %d.",k);
  104. mexErrMsgTxt(errstr);
  105. }
  106. // add unity to all of the indices to convert them to Matlab indexing notation
  107. for (j=0;j<Nind;j++) intPtr[j] += 1;
  108. mxSetField(struc,0,"non_zero_indices",mxDuplicateArray(field));
  109. // read in the non-zero values
  110. if ((field = mxCreateNumericArray(2,dims2,mxSINGLE_CLASS,mxREAL)) == NULL)
  111. {
  112. sprintf(errstr,"Failed to allocate memory for the data array of beamlet %d\n",k);
  113. mexErrMsgTxt(errstr);
  114. }
  115. floatPtr = (float *)mxGetData(field);
  116. if (fread(floatPtr,sizeof(float),Nind,fid) != Nind)
  117. {
  118. sprintf(errstr,"Unable to read-in non-zero data for beamlet %d.",k);
  119. mexErrMsgTxt(errstr);
  120. }
  121. mxSetField(struc,0,"non_zero_values",mxDuplicateArray(field));
  122. // copy the sparse beamlet structure to the beamlet batch cell array
  123. mxSetCell(BEAMLETS,k,mxDuplicateArray(struc));
  124. }
  125. fclose(fid);
  126. // I have not managed to free the memory allocated for the field and struc arrays
  127. // due to segmentation errors. I'm not sure how to get around this problem either.
  128. }