parse_func.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // parse_func.c
  2. /* This file contains the data readin routines for the simann.c program. */
  3. #include "lsq.h"
  4. extern char errstr[200]; // error string that all routines have access to
  5. // file lines that will be searched for during the simulated annealing parameters read-in process
  6. #define number_of_iterations_line "Niterations"
  7. #define number_of_iterations_per_batch_line "Nperbatch"
  8. #define prescription_filename_line "prescFile"
  9. #define beamlet_header_line "beamletHeaderFileName"
  10. #define initial_beam_weights_filename_line "initBeamWeightFile"
  11. #define dose_batch_base_name_line "doseBatchBaseName"
  12. #define dose_batch_extension_line "doseBatchExtension"
  13. #define weight_batch_base_name_line "weightBatchBaseName"
  14. #define weight_batch_extension_line "weightBatchExtension"
  15. #define obj_func_name_line "objFuncFileName"
  16. #define mod_factor_line "modFactor"
  17. int read_in_opt_parameters(OPT_PARAM *op, char *opt_header)
  18. // Loads the optimization parameters that define how the optimization will take place.
  19. {
  20. int num_iterations_flag = 0;
  21. int num_per_batch_flag = 0;
  22. int prescription_filename_flag = 0;
  23. int beamlet_header_flag = 0;
  24. int init_beam_weights_flag = 0;
  25. int dose_batch_base_name_flag = 0;
  26. int dose_batch_extension_flag = 0;
  27. int weight_batch_base_name_flag = 0;
  28. int weight_batch_extension_flag = 0;
  29. int obj_func_name_flag = 0;
  30. int mod_factor_flag = 0;
  31. char str[200];
  32. FILE *fid;
  33. // open the optimization header
  34. if ( (fid = fopen(opt_header,"r")) == NULL)
  35. {
  36. printf("%s\n",opt_header);
  37. sprintf(errstr,"Could not open the optimization header file.\n");
  38. return(FAILURE);
  39. }
  40. // read in the optimization parameters
  41. while (fscanf(fid,"%s\n",str) != EOF) // read until the end of the file
  42. {
  43. if (!strcmp(str,number_of_iterations_line))
  44. if (fscanf(fid,"%d\n",&op->Niterations) != 1)
  45. {
  46. sprintf(errstr,"Failed to read-in the number of iterations from %s file.",opt_header);
  47. return(FAILURE);
  48. }
  49. else
  50. num_iterations_flag = 1;
  51. else if (!strcmp(str,number_of_iterations_per_batch_line))
  52. if (fscanf(fid,"%d\n",&op->Nperbatch) != 1)
  53. {
  54. sprintf(errstr,"Failed to read-in the number of iterations per batch from %s file.",opt_header);
  55. return(FAILURE);
  56. }
  57. else
  58. num_per_batch_flag = 1;
  59. else if (!strcmp(str,prescription_filename_line))
  60. if (fscanf(fid,"%s\n",op->prescription_filename) != 1)
  61. {
  62. sprintf(errstr,"Failed to read-in the prescription filename line from %s file.",opt_header);
  63. return(FAILURE);
  64. }
  65. else
  66. prescription_filename_flag = 1;
  67. else if (!strcmp(str,beamlet_header_line))
  68. if (fscanf(fid,"%s\n",op->beamlet_header_file) != 1)
  69. {
  70. sprintf(errstr,"Failed to read-in the beamlet header file line from %s file.",opt_header);
  71. return(FAILURE);
  72. }
  73. else
  74. beamlet_header_flag = 1;
  75. else if (!strcmp(str,initial_beam_weights_filename_line))
  76. if (fscanf(fid,"%s\n",op->initial_beam_weights_filename) != 1)
  77. {
  78. sprintf(errstr,"Failed to read-in the initial beam weights file from %s file.",opt_header);
  79. return(FAILURE);
  80. }
  81. else
  82. init_beam_weights_flag = 1;
  83. else if (!strcmp(str,dose_batch_base_name_line))
  84. if (fscanf(fid,"%s\n",op->dose_batch_base_name) != 1)
  85. {
  86. sprintf(errstr,"Failed to read-in the dose batch base name from %s file.",opt_header);
  87. return(FAILURE);
  88. }
  89. else
  90. dose_batch_base_name_flag = 1;
  91. else if (!strcmp(str,dose_batch_extension_line))
  92. if (fscanf(fid,"%s\n",op->dose_batch_extension) != 1)
  93. {
  94. sprintf(errstr,"Failed to read-in the dose batch base extension from %s file.",opt_header);
  95. return(FAILURE);
  96. }
  97. else
  98. dose_batch_extension_flag = 1;
  99. else if (!strcmp(str,weight_batch_base_name_line))
  100. if (fscanf(fid,"%s\n",op->weight_batch_base_name) != 1)
  101. {
  102. sprintf(errstr,"Failed to read-in the weight batch base name from %s file.",opt_header);
  103. return(FAILURE);
  104. }
  105. else
  106. weight_batch_base_name_flag = 1;
  107. else if (!strcmp(str,weight_batch_extension_line))
  108. if (fscanf(fid,"%s\n",op->weight_batch_extension) != 1)
  109. {
  110. sprintf(errstr,"Failed to read-in the weight batch base extension from %s file.",opt_header);
  111. return(FAILURE);
  112. }
  113. else
  114. weight_batch_extension_flag = 1;
  115. else if (!strcmp(str,obj_func_name_line))
  116. if (fscanf(fid,"%s\n",op->obj_func_name) != 1)
  117. {
  118. sprintf(errstr,"Failed to read-in the objective function filename from %s file.",opt_header);
  119. return(FAILURE);
  120. }
  121. else
  122. obj_func_name_flag = 1;
  123. else if (!strcmp(str,mod_factor_line))
  124. if (fscanf(fid,"%f\n",&op->modFactor) != 1)
  125. {
  126. sprintf(errstr,"Failed to read-in the modulation factor from %s file.",opt_header);
  127. return(FAILURE);
  128. }
  129. else
  130. mod_factor_flag = 1;
  131. else
  132. {
  133. sprintf(errstr,"Unrecognized string in the %s file:\n%s\n",opt_header,str);
  134. return(FAILURE);
  135. }
  136. }
  137. fclose(fid);
  138. // confirm that all of the required lines have been found
  139. if (num_iterations_flag == 0)
  140. {
  141. sprintf(errstr,"Unable to find the number of iterations line in the file %s.\n",opt_header);
  142. return(FAILURE);
  143. }
  144. if (num_per_batch_flag == 0)
  145. {
  146. sprintf(errstr,"Unable to find the number of iterations per batch line in the file %s.\n",opt_header);
  147. return(FAILURE);
  148. }
  149. else if (prescription_filename_flag == 0)
  150. {
  151. sprintf(errstr,"Unable to find the prescription filename file in the file %s.\n",opt_header);
  152. return(FAILURE);
  153. }
  154. else if (beamlet_header_flag == 0)
  155. {
  156. sprintf(errstr,"Unable to find the beamlet header line in the file %s.\n",opt_header);
  157. return(FAILURE);
  158. }
  159. else if (init_beam_weights_flag == 0)
  160. {
  161. sprintf(errstr,"Unable to find the initial beam weights filename in the file %s.\n",opt_header);
  162. return(FAILURE);
  163. }
  164. else if (dose_batch_base_name_flag == 0)
  165. {
  166. sprintf(errstr,"Unable to find the dose batch base name in the file %s.\n",opt_header);
  167. return(FAILURE);
  168. }
  169. else if (dose_batch_extension_flag == 0)
  170. {
  171. sprintf(errstr,"Unable to find the dose batch extension in the file %s.\n",opt_header);
  172. return(FAILURE);
  173. }
  174. else if (weight_batch_base_name_flag == 0)
  175. {
  176. sprintf(errstr,"Unable to find the weight batch base name in the file %s.\n",opt_header);
  177. return(FAILURE);
  178. }
  179. else if (weight_batch_extension_flag == 0)
  180. {
  181. sprintf(errstr,"Unable to find the weight batch extension in the file %s.\n",opt_header);
  182. return(FAILURE);
  183. }
  184. else if (obj_func_name_flag == 0)
  185. {
  186. sprintf(errstr,"Unable to find the objective function file name in the file %s.\n",opt_header);
  187. return(FAILURE);
  188. }
  189. else if (mod_factor_flag == 0)
  190. {
  191. sprintf(errstr,"Unable to find the modulation factor in the file %s.\n",opt_header);
  192. return(FAILURE);
  193. }
  194. return(SUCCESS);
  195. }
  196. // file lines that will be searched-for during the geometry read-in process
  197. #define Ntissue_line "Ntissue"
  198. #define tissueNum_line "tissueNum"
  199. #define name_line "name"
  200. #define alpha_line "alpha"
  201. #define betaVPlus_line "betaVPlus"
  202. #define dVPlus_line "dVPlus"
  203. #define vPlus_line "vPlus"
  204. #define betaVMinus_line "betaVMinus"
  205. #define dVMinus_line "dMinus"
  206. #define vMinus_line "vMinus"
  207. #define betaPlus_line "betaPlus"
  208. #define dosePlusFilename_line "dosePlusFilename"
  209. #define betaMinus_line "betaMinus"
  210. #define doseMinusFilename_line "doseMinusFilename"
  211. int read_in_prescription(PRESC *presc, char *presc_filename)
  212. // Prescription structure is read in
  213. {
  214. char str[200];
  215. int i,j,dims[3],Nind;
  216. FILE *fid, *doseMinusFile, *dosePlusFile;
  217. // read in the prescription file
  218. if ( (fid = fopen(presc_filename,"r")) == NULL) // open the prescription file
  219. {
  220. sprintf(errstr,"Could not open the prescription file, %s",presc_filename);
  221. return(FAILURE);
  222. }
  223. // read in the number of tissue types
  224. if (fscanf(fid,"%s\n",str) != 1)
  225. {
  226. sprintf(errstr,"Failed to read in first line of %s",presc_filename);
  227. return(FAILURE);
  228. }
  229. else if (strcmp(str,Ntissue_line))
  230. {
  231. sprintf(errstr,"First line of %s must be %s",presc_filename,Ntissue_line);
  232. return(FAILURE);
  233. }
  234. else if (fscanf(fid,"%d\n",&presc->Ntissue) != 1)
  235. // found Ntissue_line, so read-in next line
  236. {
  237. sprintf(errstr,"Failed to read in the number of tissues from %s",presc_filename);
  238. return(FAILURE);
  239. }
  240. // allocate memory for the tissue type structure
  241. if ((presc->array = (TISSUE *)malloc(sizeof(TISSUE)*presc->Ntissue)) == NULL)
  242. {
  243. sprintf(errstr,"Failed to allocate space for prescription tissues",presc_filename);
  244. return(FAILURE);
  245. }
  246. printf("allocated space for %d tissues\n",presc->Ntissue);
  247. // read-in the prescription parameters for each tissue type
  248. for (i=0;i<presc->Ntissue;i++)
  249. {
  250. // pop off the tissue_num line
  251. if (fgets(str,100,fid) == NULL)
  252. {
  253. sprintf(errstr,"Could not read from from header file.");
  254. return(FAILURE);
  255. }
  256. else if (!strcmp(str,tissueNum_line))
  257. {
  258. sprintf(errstr,"Missing tissueNum line from tissue %d in file %s\n",i,presc_filename);
  259. return(FAILURE);
  260. }
  261. // get the tissue number
  262. if (fscanf(fid,"%d\n",&j) != 1)
  263. {
  264. sprintf(errstr,"Could not read in tissue number for tissue %d in file %s.\n",i,presc_filename);
  265. return(FAILURE);
  266. }
  267. else if (i != j)
  268. {
  269. sprintf(errstr,"Tissue indexed by %d in file %s has an inconsistent tissue number of %d.\n",i,presc_filename,j);
  270. return(FAILURE);
  271. }
  272. printf("tissue number is %d\n",j);
  273. // pop off the name line
  274. if (fgets(str,100,fid) == NULL)
  275. {
  276. sprintf(errstr,"Could not read from from header file.");
  277. return(FAILURE);
  278. }
  279. else if (!strcmp(str,name_line))
  280. {
  281. sprintf(errstr,"Missing name line from tissue %d in file %s\n",i,presc_filename);
  282. return(FAILURE);
  283. }
  284. // get the tissue name
  285. if (fgets(presc->array[i].name,100,fid) == NULL)
  286. {
  287. sprintf(errstr,"Could not read in tissue name for tissue %d in file %s.\n",i,presc_filename);
  288. return(FAILURE);
  289. }
  290. printf("Tissue name is %s\n",presc->array[i].name);
  291. // pop off the alpha line
  292. if (fgets(str,100,fid) == NULL)
  293. {
  294. sprintf(errstr,"Could not read from from header file.");
  295. return(FAILURE);
  296. }
  297. else if (!strcmp(str,alpha_line))
  298. {
  299. sprintf(errstr,"Missing alpha line from tissue %d in file %s\n",i,presc_filename);
  300. return(FAILURE);
  301. }
  302. // get the tissue alpha (importance)
  303. if (fscanf(fid,"%f\n",&presc->array[i].alpha) != 1)
  304. {
  305. sprintf(errstr,"Could not read in tissue alpha for tissue %d in file %s.\n",i,presc_filename);
  306. return(FAILURE);
  307. }
  308. printf("Tissue alpha is %f\n",presc->array[i].alpha);
  309. // pop off the betaVPlus line
  310. if (fgets(str,100,fid) == NULL)
  311. {
  312. sprintf(errstr,"Could not read from from header file.");
  313. return(FAILURE);
  314. }
  315. else if (!strcmp(str,betaVPlus_line))
  316. {
  317. sprintf(errstr,"Missing betaVPlus line from tissue %d in file %s\n",i,presc_filename);
  318. return(FAILURE);
  319. }
  320. // get the tissue betaVPlus
  321. if (fscanf(fid,"%f\n",&presc->array[i].betaVPlus) != 1)
  322. {
  323. sprintf(errstr,"Could not read in tissue betaVPlus for tissue %d in file %s.\n",i,presc_filename);
  324. return(FAILURE);
  325. }
  326. printf("Tissue betaVPlus is %f\n",presc->array[i].betaVPlus);
  327. // pop off the dVPlus line
  328. if (fgets(str,100,fid) == NULL)
  329. {
  330. sprintf(errstr,"Could not read from from header file.");
  331. return(FAILURE);
  332. }
  333. else if (!strcmp(str,dVPlus_line))
  334. {
  335. sprintf(errstr,"Missing dVPlus line from tissue %d in file %s\n",i,presc_filename);
  336. return(FAILURE);
  337. }
  338. // get the tissue dVPlus
  339. if (fscanf(fid,"%f\n",&presc->array[i].dVPlus) != 1)
  340. {
  341. sprintf(errstr,"Could not read in tissue dVPlus for tissue %d in file %s.\n",i,presc_filename);
  342. return(FAILURE);
  343. }
  344. printf("Tissue dVPlus is %f\n",presc->array[i].dVPlus);
  345. // pop off the vPlus line
  346. if (fgets(str,100,fid) == NULL)
  347. {
  348. sprintf(errstr,"Could not read from from header file.");
  349. return(FAILURE);
  350. }
  351. else if (!strcmp(str,vPlus_line))
  352. {
  353. sprintf(errstr,"Missing vPlus line from tissue %d in file %s\n",i,presc_filename);
  354. return(FAILURE);
  355. }
  356. // get the tissue vPlus
  357. if (fscanf(fid,"%f\n",&presc->array[i].vPlus) != 1)
  358. {
  359. sprintf(errstr,"Could not read in tissue vPlus for tissue %d in file %s.\n",i,presc_filename);
  360. return(FAILURE);
  361. }
  362. printf("Tissue vPlus is %f\n",presc->array[i].vPlus);
  363. // pop off the betaVMinus line
  364. if (fgets(str,100,fid) == NULL)
  365. {
  366. sprintf(errstr,"Could not read from from header file.");
  367. return(FAILURE);
  368. }
  369. else if (!strcmp(str,betaVMinus_line))
  370. {
  371. sprintf(errstr,"Missing betaVMinus line from tissue %d in file %s\n",i,presc_filename);
  372. return(FAILURE);
  373. }
  374. // get the tissue beta_minus
  375. if (fscanf(fid,"%f\n",&presc->array[i].betaVMinus) != 1)
  376. {
  377. sprintf(errstr,"Could not read in tissue betaVMinus for tissue %d in file %s.\n",i,presc_filename);
  378. return(FAILURE);
  379. }
  380. printf("Tissue betaVMinus is %f\n",presc->array[i].betaVMinus);
  381. // pop off the dVMinus line
  382. if (fgets(str,100,fid) == NULL)
  383. {
  384. sprintf(errstr,"Could not read from from header file.");
  385. return(FAILURE);
  386. }
  387. else if (!strcmp(str,dVMinus_line))
  388. {
  389. sprintf(errstr,"Missing dVMinus line from tissue %d in file %s\n",i,presc_filename);
  390. return(FAILURE);
  391. }
  392. // get the tissue dVMinus
  393. if (fscanf(fid,"%f\n",&presc->array[i].dVMinus) != 1)
  394. {
  395. sprintf(errstr,"Could not read in tissue dVMinus for tissue %d in file %s.\n",i,presc_filename);
  396. return(FAILURE);
  397. }
  398. printf("Tissue dVMinus is %f\n",presc->array[i].dVMinus);
  399. // pop off the vMinus line
  400. if (fgets(str,100,fid) == NULL)
  401. {
  402. sprintf(errstr,"Could not read from from header file.");
  403. return(FAILURE);
  404. }
  405. else if (!strcmp(str,vMinus_line))
  406. {
  407. sprintf(errstr,"Missing vMinus line from tissue %d in file %s\n",i,presc_filename);
  408. return(FAILURE);
  409. }
  410. // get the tissue vMinus
  411. if (fscanf(fid,"%f\n",&presc->array[i].vMinus) != 1)
  412. {
  413. sprintf(errstr,"Could not read in tissue vMinus for tissue %d in file %s.\n",i,presc_filename);
  414. return(FAILURE);
  415. }
  416. printf("Tissue vMinus is %f\n",presc->array[i].vMinus);
  417. // pop off the betaPlus line
  418. if (fgets(str,100,fid) == NULL)
  419. {
  420. sprintf(errstr,"Could not read from from header file.");
  421. return(FAILURE);
  422. }
  423. else if (!strcmp(str,betaPlus_line))
  424. {
  425. sprintf(errstr,"Missing betaPlus line from tissue %d in file %s\n",i,presc_filename);
  426. return(FAILURE);
  427. }
  428. // get the tissue betaPlus
  429. if (fscanf(fid,"%f\n",&presc->array[i].betaPlus) != 1)
  430. {
  431. sprintf(errstr,"Could not read in tissue betaPlus for tissue %d in file %s.\n",i,presc_filename);
  432. return(FAILURE);
  433. }
  434. printf("Tissue betaPlus is %f\n",presc->array[i].betaPlus);
  435. // pop off the dosePlusFileName
  436. if (fgets(str,100,fid) == NULL)
  437. {
  438. sprintf(errstr,"Could not read from from header file.");
  439. return(FAILURE);
  440. }
  441. else if (!strcmp(str,dosePlusFilename_line))
  442. {
  443. sprintf(errstr,"Missing dosePlusfilename line from tissue %d in file %s\n",i,presc_filename);
  444. return(FAILURE);
  445. }
  446. // get the dose plus prescription filename
  447. if (fscanf(fid,"%s\n",presc->array[i].dosePlusFilename) != 1)
  448. {
  449. sprintf(errstr,"Could not read in dosePlusFilename for tissue %d in file %s.\n",i,presc_filename);
  450. return(FAILURE);
  451. }
  452. // read in the dose prescription file, which is stored in the same format as an individual beamlet:
  453. // Xcount Ycount Zcount Nind non_zero_indices non_zero_values
  454. // int int int int int array single array
  455. // open the overdose penalty file
  456. if ((dosePlusFile = fopen(presc->array[i].dosePlusFilename,"rb")) == NULL)
  457. {
  458. sprintf(errstr,"Could not read in the file %s.\n",presc->array[i].dosePlusFilename);
  459. return(FAILURE);
  460. }
  461. // read-in the first three indices, which are the int values (Xcount, Ycount, Zcount)
  462. if (fread(dims,sizeof(int),3,dosePlusFile) != 3)
  463. {
  464. sprintf(errstr,"Could not read in Xcount, Ycount, Zcount data from %s\n",presc->array[i].dosePlusFilename);
  465. return(FAILURE);
  466. }
  467. if (i == 0)
  468. {
  469. // record tissue mask dimensions
  470. presc->x_count = dims[0];
  471. presc->y_count = dims[1];
  472. presc->z_count = dims[2];
  473. presc->Nvox = presc->x_count*presc->y_count*presc->z_count;
  474. } // ensure that all tissue masks have the same dimensions
  475. else
  476. if ((int)dims[0] != presc->x_count || dims[1] != presc->y_count || dims[2] != presc->z_count)
  477. {
  478. sprintf(errstr,"Dose prescription file %d of %d has dimensions of (%d,%d,%d), when they should be (%d,%d,%d).\n",
  479. i+1,presc->Ntissue,dims[0],dims[1],dims[2],
  480. presc->x_count,presc->y_count,presc->z_count);
  481. return(FAILURE);
  482. }
  483. // read in the number of non-zero indices
  484. if (fread(&Nind,sizeof(int),1,dosePlusFile) != 1)
  485. {
  486. sprintf(errstr,"Could not read in Nind data from %s\n",presc->array[i].dosePlusFilename);
  487. return(FAILURE);
  488. }
  489. else
  490. presc->array[i].Nind = Nind;
  491. // allocate space for the indices
  492. if ((presc->array[i].ind = (int *)malloc(sizeof(int)*Nind)) == NULL)
  493. {
  494. sprintf(errstr,"Failed to allocate space for mask indices from %s\n",presc->array[i].dosePlusFilename);
  495. return(FAILURE);
  496. }
  497. // allocate space for the dose prescription data
  498. if ((presc->array[i].dPlus = (float *)malloc(sizeof(int)*Nind)) == NULL)
  499. {
  500. sprintf(errstr,"Failed to allocate space for data indices from %s\n",presc->array[i].dosePlusFilename);
  501. return(FAILURE);
  502. }
  503. // read in the non-zero indices from the mask file
  504. if (fread(presc->array[i].ind,sizeof(int),Nind,dosePlusFile) != Nind)
  505. {
  506. sprintf(errstr,"Failed to read in all mask indices from %s\n",presc->array[i].dosePlusFilename);
  507. return(FAILURE);
  508. }
  509. // read in the non-zero data from the mask file
  510. if (fread(presc->array[i].dPlus,sizeof(float),Nind,dosePlusFile) != Nind)
  511. {
  512. sprintf(errstr,"Failed to read in all non-zero data from %s\n",presc->array[i].dosePlusFilename);
  513. return(FAILURE);
  514. }
  515. fclose(dosePlusFile);
  516. // pop off the betaMinus line
  517. if (fgets(str,100,fid) == NULL)
  518. {
  519. sprintf(errstr,"Could not read from from header file.");
  520. return(FAILURE);
  521. }
  522. else if (!strcmp(str,betaMinus_line))
  523. {
  524. sprintf(errstr,"Missing betaMinus line from tissue %d in file %s\n",i,presc_filename);
  525. return(FAILURE);
  526. }
  527. // get the tissue betaMinus
  528. if (fscanf(fid,"%f\n",&presc->array[i].betaMinus) != 1)
  529. {
  530. sprintf(errstr,"Could not read in tissue betaMinus for tissue %d in file %s.\n",i,presc_filename);
  531. return(FAILURE);
  532. }
  533. printf("Tissue betaMinus is %f\n",presc->array[i].betaMinus);
  534. // pop off the doseMinusFileName
  535. if (fgets(str,100,fid) == NULL)
  536. {
  537. sprintf(errstr,"Could not read from from header file.");
  538. return(FAILURE);
  539. }
  540. else if (!strcmp(str,doseMinusFilename_line))
  541. {
  542. sprintf(errstr,"Missing doseMinusfilename line from tissue %d in file %s\n",i,presc_filename);
  543. return(FAILURE);
  544. }
  545. // get the dose plus prescription filename
  546. if (fscanf(fid,"%s\n",presc->array[i].doseMinusFilename) != 1)
  547. {
  548. sprintf(errstr,"Could not read in doseMinusFilename for tissue %d in file %s.\n",i,presc_filename);
  549. return(FAILURE);
  550. }
  551. // read in the dose prescription file, which is stored in the same format as an individual beamlet:
  552. // Xcount Ycount Zcount Nind non_zero_indices non_zero_values
  553. // int int int int int array single array
  554. // open the underdose penalty file
  555. if ((doseMinusFile = fopen(presc->array[i].doseMinusFilename,"rb")) == NULL)
  556. {
  557. sprintf(errstr,"Could not read in the file %s.\n",presc->array[i].doseMinusFilename);
  558. return(FAILURE);
  559. }
  560. // read-in the first three indices, which are the int values (Xcount, Ycount, Zcount)
  561. if (fread(dims,sizeof(int),3,doseMinusFile) != 3)
  562. {
  563. sprintf(errstr,"Could not read in Xcount, Ycount, Zcount data from %s\n",presc->array[i].doseMinusFilename);
  564. return(FAILURE);
  565. }
  566. if (i == 0)
  567. {
  568. // record tissue mask dimensions
  569. presc->x_count = dims[0];
  570. presc->y_count = dims[1];
  571. presc->z_count = dims[2];
  572. } // ensure that all tissue masks have the same dimensions
  573. else
  574. if ((int)dims[0] != presc->x_count || dims[1] != presc->y_count || dims[2] != presc->z_count)
  575. {
  576. sprintf(errstr,"Dose prescription file %d of %d has dimensions of (%d,%d,%d), when they should be (%d,%d,%d).\n",
  577. i+1,presc->Ntissue,dims[0],dims[1],dims[2],
  578. presc->x_count,presc->y_count,presc->z_count);
  579. return(FAILURE);
  580. }
  581. // read in the number of non-zero indices
  582. if (fread(&Nind,sizeof(int),1,doseMinusFile) != 1)
  583. {
  584. sprintf(errstr,"Could not read in Nind data from %s\n",presc->array[i].doseMinusFilename);
  585. return(FAILURE);
  586. }
  587. else
  588. presc->array[i].Nind = Nind;
  589. // allocate space for the indices
  590. if ((presc->array[i].ind = (int *)malloc(sizeof(int)*Nind)) == NULL)
  591. {
  592. sprintf(errstr,"Failed to allocate space for mask indices from %s\n",presc->array[i].doseMinusFilename);
  593. return(FAILURE);
  594. }
  595. // allocate space for the dose prescription data
  596. if ((presc->array[i].dMinus = (float *)malloc(sizeof(int)*Nind)) == NULL)
  597. {
  598. sprintf(errstr,"Failed to allocate space for data indices from %s\n",presc->array[i].doseMinusFilename);
  599. return(FAILURE);
  600. }
  601. // read in the non-zero indices from the mask file
  602. if (fread(presc->array[i].ind,sizeof(int),Nind,doseMinusFile) != Nind)
  603. {
  604. sprintf(errstr,"Failed to read in all mask indices from %s\n",presc->array[i].doseMinusFilename);
  605. return(FAILURE);
  606. }
  607. // read in the non-zero data from the mask file
  608. if (fread(presc->array[i].dMinus,sizeof(float),Nind,doseMinusFile) != Nind)
  609. {
  610. sprintf(errstr,"Failed to read in all non-zero data from %s\n",presc->array[i].doseMinusFilename);
  611. return(FAILURE);
  612. }
  613. fclose(doseMinusFile);
  614. // diagnostic lines for ensuring that indices were properly read in
  615. /* printf("successfully read-in indices\n");
  616. sprintf(str2,"tissue%d.bin",i);
  617. printf("str2 = %s\n",str2);
  618. test_file = fopen(str2,"wb");
  619. fwrite(presc->array[i].ind,sizeof(int),presc->array[i].Nind,test_file);
  620. fclose(test_file); */
  621. }
  622. fclose(fid);
  623. return SUCCESS;
  624. }
  625. int read_in_beamlets(GRID_ARRAY *beamlets, char *beamlet_header, char *init_beam_weights_file,
  626. int *usedVoxels, OPT_PARAM *op, GRID *doseInit)
  627. // This function is lazier than read_in_geometry since it does not check to ensure that
  628. // all of the required lines in the beamlet_header file are present. This is something
  629. // that can be added later, as it is not necessary for the function of the program.
  630. {
  631. int k,j,m,n,Nbeamlets,num,Nind,beamletNum;
  632. int dims[3];
  633. int *beamlet_tally; // tally to ensure that all beamlets are read-in
  634. int Nuv;
  635. int *ind;
  636. float *data;
  637. char bixbatchfile[200];
  638. char str[200];
  639. char bix_dir[200]; // directory containing beamlet batches
  640. char bix_batch_base_filename[200];
  641. char bix_batch_extension[200];
  642. FILE *fid;
  643. // read in beamlet header file
  644. if( (fid = fopen(beamlet_header,"rb")) == NULL)
  645. {
  646. sprintf(errstr,"Could not open beamlet header file.");
  647. return(FAILURE);
  648. }
  649. else
  650. {
  651. // pop off the next line of the header file
  652. if (fgets(str,100,fid) == NULL)
  653. {
  654. sprintf(errstr,"Could not read from from header file.");
  655. return(FAILURE);
  656. }
  657. // read in the dimensions of the beamlets
  658. if (fscanf(fid,"%d %d %d\n",&beamlets->x_count,&beamlets->y_count,&beamlets->z_count) != 3)
  659. {
  660. sprintf(errstr,"Could not read-in the beamlet dimensions.\n");
  661. return(FAILURE);
  662. }
  663. else
  664. beamlets->Nvox = beamlets->x_count*beamlets->y_count*beamlets->z_count;
  665. // pop off the next line of the header file
  666. if (fgets(str,100,fid) == NULL)
  667. {
  668. sprintf(errstr,"Could not read from from header file.");
  669. return(FAILURE);
  670. }
  671. // read in the number of beamlets
  672. if (fscanf(fid,"%d\n",&beamlets->num_beamlets) != 1)
  673. {
  674. sprintf(errstr,"Could not read-in the number of beamlets.\n");
  675. return(FAILURE);
  676. }
  677. // pop off the next line of the header file
  678. if (fgets(str,100,fid) == NULL)
  679. {
  680. sprintf(errstr,"Could not read from from header file.");
  681. return(FAILURE);
  682. }
  683. // read in the number of beamlet batches
  684. if (fscanf(fid,"%d\n",&beamlets->num_beamlet_batches) != 1)
  685. {
  686. sprintf(errstr,"Could not read-in the number of beamlet batches.\n");
  687. return(FAILURE);
  688. }
  689. // pop off the next line of the header file
  690. if (fgets(str,100,fid) == NULL)
  691. {
  692. sprintf(errstr,"Could not read from from header file.");
  693. return(FAILURE);
  694. }
  695. // read in the name of the beamlet directory
  696. if (fscanf(fid,"%s\n",bix_dir) != 1)
  697. {
  698. sprintf(errstr,"Could not read-in the name of the beamlet directory.\n");
  699. return(FAILURE);
  700. }
  701. strcpy(op->bix_dir,bix_dir); // save bixel directory
  702. // pop off the next line of the header file
  703. if (fgets(str,100,fid) == NULL)
  704. {
  705. sprintf(errstr,"Could not read from from header file.");
  706. return(FAILURE);
  707. }
  708. // read in the name of the beamlet directory
  709. if (fscanf(fid,"%s\n",bix_batch_base_filename) != 1)
  710. {
  711. sprintf(errstr,"Could not read-in the base filename of the beamlet batches.\n");
  712. return(FAILURE);
  713. }
  714. strcpy(op->bix_batch_base_filename,bix_batch_base_filename); // save bixel base filename
  715. // pop off the next line of the header file
  716. if (fgets(str,100,fid) == NULL)
  717. {
  718. sprintf(errstr,"Could not read from from header file.");
  719. return(FAILURE);
  720. }
  721. // read in the beamlet batch extension name
  722. if (fscanf(fid,"%s\n",bix_batch_extension) != 1)
  723. {
  724. sprintf(errstr,"Could not read-in the beamlet batch extension name.\n");
  725. return(FAILURE);
  726. }
  727. strcpy(op->bix_batch_extension,bix_batch_extension); // save bixel extension
  728. }
  729. // read-in beamlet data
  730. if((beamlet_tally = (int *)malloc(beamlets->num_beamlets*sizeof(int))) == NULL)
  731. {
  732. sprintf(errstr,"Unable to allocate memory for beamlet_tally.");
  733. return(FAILURE);
  734. }
  735. // allocate space for sparse beamlets
  736. if ((beamlets->array = (SPARSE_ARRAY *)malloc(beamlets->num_beamlets*sizeof(SPARSE_ARRAY))) == NULL)
  737. {
  738. sprintf(errstr,"Unable to allocate memory for sparse beamlet matrix.\n");
  739. return(FAILURE);
  740. }
  741. // allocate space for the other arrays in beamlets
  742. if ((beamlets->beam_weight = (float *)malloc(beamlets->num_beamlets*sizeof(float))) == NULL)
  743. {
  744. sprintf(errstr,"Unable to allocate memory for beam_weight array in beamlet structure.\n");
  745. return(FAILURE);
  746. }
  747. if ((beamlets->initial_beam_weight = (float *)malloc(beamlets->num_beamlets*sizeof(float))) == NULL)
  748. {
  749. sprintf(errstr,"Unable to allocate memory for initial_beam_weight array in beamlet structure.\n");
  750. return(FAILURE);
  751. }
  752. // load the initial beamlet weights
  753. if((fid = fopen(init_beam_weights_file,"rb")) == NULL)
  754. {
  755. sprintf(errstr,"Failed to open initial beam intensity file.\n");
  756. return(FAILURE);
  757. }
  758. if((int)fread(beamlets->initial_beam_weight,sizeof(float),beamlets->num_beamlets,fid) != beamlets->num_beamlets)
  759. {
  760. sprintf(errstr,"Failed to read in initial beamlet intensities.\n");
  761. return(FAILURE);
  762. }
  763. else
  764. printf("Successfully read in initial beamlet weights.\n");
  765. // copy initial beam weights to the variable beam weights
  766. for (k=0;k<beamlets->num_beamlets;k++)
  767. beamlets->beam_weight[k] = beamlets->initial_beam_weight[k];
  768. // truncate beamlet weights that are less than zero
  769. for (k=0;k<beamlets->num_beamlets;k++)
  770. if (beamlets->beam_weight[k] < 0)
  771. beamlets->beam_weight[k] = 0.0;
  772. // truncate the beamlet weights that violate the modulation requirements
  773. truncateBeamWeights(beamlets->beam_weight,beamlets->num_beamlets, op->modFactor);
  774. // start tally at all zeros
  775. for (k=0;k<beamlets->num_beamlets;k++) beamlet_tally[k] = 0;
  776. // start initial dose grid with all zeros
  777. for (k=0;k<beamlets->Nvox;k++)
  778. doseInit->matrix[k] = 0.0;
  779. beamletNum = 0;
  780. for (k=0;k<beamlets->num_beamlet_batches;k++)
  781. {
  782. // open the beamlet batch file
  783. sprintf(bixbatchfile,"%s/%s%d.%s",bix_dir,bix_batch_base_filename,k,bix_batch_extension);
  784. if ((fid = fopen(bixbatchfile,"rb")) == NULL)
  785. {
  786. sprintf(errstr,"Unable to open file for beamlet batch %d.\n",k);
  787. return(FAILURE);
  788. }
  789. // read-in the number of beamlets that are in this file
  790. if((int)fread(&Nbeamlets,sizeof(int),1,fid) != 1)
  791. {
  792. sprintf(errstr,"Unable to read number of beamlets in file:\n%s",bixbatchfile);
  793. return(FAILURE);
  794. }
  795. // read-in all of the beamlets in the current batch
  796. for (j=0;j<Nbeamlets;j++)
  797. {
  798. if((int)fread(&num,sizeof(int),1,fid) != 1)
  799. {
  800. sprintf(errstr,"Unable to read beamlet number for beamlet %d in file:\n%s",j,bixbatchfile);
  801. return(FAILURE);
  802. }
  803. // read the beamlet dimensions
  804. if((int)fread(dims,sizeof(int),3,fid) != 3)
  805. {
  806. sprintf(errstr,"Unable to read beamlet dimensions for beamlet %d in file:\n%s",j,bixbatchfile);
  807. return(FAILURE);
  808. }
  809. // ensure that the beamlet has the same grid size as the prescription
  810. if((dims[0] != beamlets->x_count) || (dims[1] != beamlets->y_count) || (dims[2] != beamlets->z_count))
  811. {
  812. sprintf(errstr,"Mismatch in grid dimensions for beamlet %d in file:\n%s",j,bixbatchfile);
  813. return(FAILURE);
  814. }
  815. // read the number of non-zero values in the current beamlet
  816. if((int)fread(&Nind,sizeof(int),1,fid) != 1)
  817. {
  818. sprintf(errstr,"Unable to read number of non-zero values for beamlet %d in file:\n%s",j,bixbatchfile);
  819. return(FAILURE);
  820. }
  821. // allocate memory and read-in indices for the current beamlet
  822. if((ind = (int *)malloc(sizeof(int)*Nind)) == NULL)
  823. {
  824. sprintf(errstr,"Unable to allocate memory for indices of beamlet %d\n",num);
  825. return(FAILURE);
  826. }
  827. if((int)fread(ind,sizeof(int),Nind,fid) != Nind)
  828. {
  829. sprintf(errstr,"Unable to read-in indices for beamlet %d\n",num);
  830. return(FAILURE);
  831. }
  832. // allocate memory and read-in indices for the current beamlet
  833. if((data = (float *)malloc(sizeof(float)*Nind)) == NULL)
  834. {
  835. sprintf(errstr,"Unable to allocate memory for data of beamlet %d\n",num);
  836. return(FAILURE);
  837. }
  838. if((int)fread(data,sizeof(float),Nind,fid) != Nind)
  839. {
  840. sprintf(errstr,"Unable to read-in dose data for beamlet %d\n",num);
  841. return(FAILURE);
  842. }
  843. if (beamletNum < 0 || beamletNum >= beamlets->num_beamlets)
  844. {
  845. sprintf(errstr,"%d is an invalid beamlet number for beamlet %d in batch %d.",num,j,k);
  846. return(FAILURE);
  847. }
  848. else
  849. beamlet_tally[beamletNum] = 1; // tally that beamlet "beamletNum" was just read in
  850. // add the beamlet to the initial dose distribution
  851. for (m=0;m<Nind;m++)
  852. doseInit->matrix[ind[m]] += beamlets->beam_weight[beamletNum]*data[m];
  853. // Count number of voxels in the current beamlet that will actually be used
  854. // in the optimization.
  855. Nuv = 0;
  856. for (m=0;m<Nind;m++)
  857. if (usedVoxels[ind[m]] == 1)
  858. Nuv++;
  859. beamlets->array[beamletNum].size = Nuv;
  860. // allocate space for the cleaned-beamlets
  861. if((beamlets->array[beamletNum].indices = (int *)malloc(sizeof(int)*Nuv)) == NULL)
  862. {
  863. sprintf(errstr,"Unable to allocate memory for indices of beamlet %d\n",num);
  864. return(FAILURE);
  865. }
  866. // allocate memory and read-in indices for the current beamlet
  867. if((beamlets->array[beamletNum].data = (float *)malloc(sizeof(float)*Nuv)) == NULL)
  868. {
  869. sprintf(errstr,"Unable to allocate memory for data of beamlet %d\n",num);
  870. return(FAILURE);
  871. }
  872. // pull out only the voxels that are actually used
  873. n = 0;
  874. for (m=0;m<Nind;m++)
  875. if (usedVoxels[ind[m]] == 1)
  876. {
  877. beamlets->array[beamletNum].indices[n] = ind[m];
  878. beamlets->array[beamletNum].data[n] = data[m];
  879. n++;
  880. }
  881. // free indices and data for the full beamlet
  882. free(ind);
  883. free(data);
  884. beamletNum++; // increment the beamlet number
  885. }
  886. printf("Successfully loaded beamlet batch %d of %d.\n",k+1,beamlets->num_beamlet_batches);
  887. fclose(fid);
  888. }
  889. // check beamlet tally to ensure that all beamlets were read in
  890. for (j=0;j<beamlets->num_beamlet_batches;j++)
  891. if (beamlet_tally[j] == 0)
  892. {
  893. sprintf(errstr,"Missing beamlet %d.",j);
  894. return(FAILURE);
  895. }
  896. return(SUCCESS);
  897. }