TPCCLIB
Loading...
Searching...
No Matches
prmat.c File Reference

Data handling for projection matrix. More...

#include "libtpcrec.h"

Go to the source code of this file.

Functions

void prmatInit (PRMAT *mat)
void prmatEmpty (PRMAT *mat)
int prmatAllocate (PRMAT *mat, int set, unsigned int rows, unsigned int *coords)
unsigned int prmatGetNV (PRMAT *mat)
unsigned int prmatGetNB (PRMAT *mat)
unsigned int prmatGetID (PRMAT *mat)
unsigned int prmatGetPIX (PRMAT *mat)
unsigned int prmatGetPixCoord (PRMAT *mat, int row)
unsigned int prmatGetRays (PRMAT *mat, int row)
unsigned int prmatGetBinView (PRMAT *mat, int row, int ind)
unsigned int prmatGetRows (PRMAT *mat)
unsigned int prmatGetPixels (PRMAT *mat, int row)
unsigned int prmatGetCoord (PRMAT *mat, int row, int pix)
unsigned int prmatGetXCoord (PRMAT *mat, int row, int pix)
unsigned int prmatGetYCoord (PRMAT *mat, int row, int pix)
float prmatGetFactor (PRMAT *mat, int row, int pix)
float prmatGetMajor (PRMAT *mat)
float prmatGetMinor (PRMAT *mat)
float prmatGetMin (PRMAT *mat)
float prmatGetMax (PRMAT *mat)
float prmatGetFactorSum (PRMAT *mat)
float prmatGetFactorSqrSum (PRMAT *mat, int row)
int prmatSaveMatrix (PRMAT *mat)
int prmatReadMatrix (char *fname, PRMAT *mat)

Variables

int PRMAT_TEST
 If not 0 drive in test mode.
int PRMAT_VERBOSE
 If not 0 drive in verbose mode.

Detailed Description

Data handling for projection matrix.

Author
Jarkko Johansson
Date
2004-04-20

Definition in file prmat.c.

Function Documentation

◆ prmatAllocate()

int prmatAllocate ( PRMAT * mat,
int set,
unsigned int rows,
unsigned int * coords )

Allocates memory for PRMAT data. Normally used only in SET-functions.
Allocates memory for look-up table if set = 0 and for projection matrix otherwise.

Precondition
*mat is initialized && rows is positive && *coords list contains as many positive integers as there are rows.
Postcondition
memory is allocated for PRMAT structure mat.
Parameters
matpointer to projection matrix for which the allocation is done.
settells for which part of the structure allocation is to be done.
rowsnumber of rows in a matrix.
coordslist of number of entries in each row.
Returns
0 if ok.

Definition at line 105 of file prmat.c.

106{
107 unsigned int *iptr, ir, ip;
108 if(PRMAT_VERBOSE) printf("PRMAT:prmatAllocate(*mat,%d,*coords) \n",rows);
109
110 /*check the arguments*/
111 if(mat->status==PRMAT_STATUS_UNINITIALIZED) return(1);
112 if(rows<1) return(2);
113
114 //If set != 0 allocate for projection matrix.
115 if(set){
116 /*allocate memory*/
117 mat->factor_sqr_sum=calloc(rows,sizeof(float));
118 mat->dimr=rows; //for dimr
119 mat->dime=calloc(rows,sizeof(int));
120 mat->_factdata=
121 (unsigned short int***)calloc(rows,sizeof(unsigned short int**));
122 //if not enough memory
123 if(mat->_factdata==NULL || mat->dime==NULL) return(4);
124 //iterate through the rows
125 for(ir=0,iptr=coords; ir<rows; ir++){
126 //copy the coords vector in the same loop
127 mat->dime[ir]=*iptr;
128 //*iptr is the number of (non-zero) factors in this row
129 mat->_factdata[ir]=
130 (unsigned short int**)calloc(*iptr,sizeof(unsigned short int*));
131 //if not enough memory
132 if(mat->_factdata[ir]==NULL) return(4);
133 //iterate through the factors in this row
134 for(ip=0; ip<(*iptr); ip++){
135 //NOTE that the leaves are coordinates and factor in this order
136 mat->_factdata[ir][ip]=
137 (unsigned short int*)calloc(3,sizeof(unsigned short int));
138 if(mat->_factdata[ir][ip]==NULL) return(4);
139 }
140 iptr++;
141 }
142 /*set data pointers; these pointers are used to avoid data loss*/
143 mat->fact=mat->_factdata;
144 } else {
145 //If set was 0 allocate for look-up table.
146 //allocate memory for look up table
147 mat->nrp=rows;
148 mat->_linesdata=(unsigned int**)calloc(rows,sizeof(unsigned int*));
149 //if not enough memory
150 if(mat->_linesdata==NULL) return(4);
151 //iterate through the rows
152 for(ir=0,iptr=coords; ir<rows; ir++){
153 //iptr is the number of lors belonging to this pixel
154 mat->_linesdata[ir]=(unsigned int*)calloc(*iptr + 2,sizeof(unsigned int));
155 //if not enough memory
156 if(mat->_linesdata[ir]==NULL) return(4);
157 iptr++;
158 }
159 //set data pointers; these pointers are used to avoid data loss
160 mat->lines=mat->_linesdata;
161 }
162 return(0);
163}
int PRMAT_VERBOSE
If not 0 drive in verbose mode.
Definition prmat.c:9
unsigned int ** lines
Definition libtpcrec.h:194
char status
Prmat status.
Definition libtpcrec.h:164
float * factor_sqr_sum
Square sums of factors in each row in the projection matrix.
Definition libtpcrec.h:202
unsigned short int *** fact
Definition libtpcrec.h:208
unsigned int * dime
Number of pixels hit by a line for every line.
Definition libtpcrec.h:204
unsigned int dimr
Dimension of rows (lines of response) in the projection matrix.
Definition libtpcrec.h:200
unsigned int nrp
Number of pixels inside the fov. i.e. number of rows in the look-up table.
Definition libtpcrec.h:189
unsigned int ** _linesdata
Hidden pointer for the actual data.
Definition libtpcrec.h:196
unsigned short int *** _factdata
Hidden pointer for the actual data.
Definition libtpcrec.h:210

Referenced by prmatReadMatrix(), radonSetLORS(), and radonSetLUT().

◆ prmatEmpty()

void prmatEmpty ( PRMAT * mat)

Frees the memory allocated for mat. All data is cleared.

Parameters
matpointer to projection matrix to be emptied.

Definition at line 56 of file prmat.c.

57{
58 unsigned int ir, ip;
59 if(PRMAT_VERBOSE) printf("PRMAT:prmatEmpty() \n");
60
61 //if mat is allready empty
62 if(mat->status==PRMAT_STATUS_INITIALIZED) return;
63
64 //if look-up table is occupied
65 if(mat->status==PRMAT_STATUS_LU_OCCUPIED) {
66 /*free the occupied memory*/
67 for(ir=0; ir<mat->nrp; ir++) { //every row
68 free((unsigned int*)mat->_linesdata[ir]);
69 }
70 if(mat->nrp > 0) free(mat->_linesdata);
71 }
72
73 //if projection matrix is occupied
74 if(mat->status==PRMAT_STATUS_PR_OCCUPIED ||
75 mat->status==PRMAT_STATUS_BS_OCCUPIED )
76 {
77 free((float*)mat->factor_sqr_sum);
78 for(ir=0; ir<mat->dimr; ir++){ //every row
79 for(ip=0; ip<mat->dime[ir]; ip++){ //every factor
80 free((unsigned short int*)mat->_factdata[ir][ip]);
81 }
82 }
83
84 free((int*)mat->dime);
85
86 if(mat->dimr>0) free(mat->_factdata);
87 mat->dimr=0;
88 }
89 return;
90}

Referenced by prmatReadMatrix(), and radonSetLORS().

◆ prmatGetBinView()

unsigned int prmatGetBinView ( PRMAT * mat,
int row,
int ind )

Returns coordinates of a line of response in place (row,ind) in the look-up table.

Precondition
0<=row<=prmatGetPIX(*mat) && 0<=ind<=prmatGetRays(*mat,row) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowthe row index.
indthe (non-zero) lor index.
Returns
int coordinates (view*binNr + bin) of the referred line of response, some negative value if ERROR.

Definition at line 253 of file prmat.c.

254{
255 return mat->lines[row][ind+2];
256}

Referenced by prmatSaveMatrix().

◆ prmatGetCoord()

unsigned int prmatGetCoord ( PRMAT * mat,
int row,
int pix )

Returns coordinate of a pixel in place (row,pix) in the given projection matrix.

Precondition
0<=row<=prmatGetLORS(*mat) && 0<=pix<=prmatGetPixels(row,*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowthe row index.
pixthe (non-zero) pixel index.
Returns
coordinate of the referred pixel, some negative value if ERROR.

Definition at line 297 of file prmat.c.

298{
299 return (int)(mat->fact[row][pix][1])*mat->imgDim + (int)(mat->fact[row][pix][0]);
300}
unsigned int imgDim
Scanner geometrics, field imgDim.
Definition libtpcrec.h:172

◆ prmatGetFactor()

float prmatGetFactor ( PRMAT * mat,
int row,
int pix )

Returns factor (weighting coefficient) for a pixel in place (row,pix) in given projection matrix.

Precondition
0<=row<=prmatGetLORS(*mat) && 0<=pix<=prmatGetPixels(row,*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowthe row index.
pixthe (non-zero) pixel index.
Returns
factor for the referred pixel, some negative value if ERROR.

Definition at line 342 of file prmat.c.

343{
344 return((float)(mat->scaling_factor)*(mat->fact[row][pix][2]));
345}
float scaling_factor
Scaling factor for factors (notice that factors are stored in integers).
Definition libtpcrec.h:185

Referenced by radonBackTransformPRM(), and radonFwdTransformPRM().

◆ prmatGetFactorSqrSum()

float prmatGetFactorSqrSum ( PRMAT * mat,
int row )

Returns square sum of all factors in given projection matrix in given row.

Precondition
mat is initialized && 0<=row<=prmatGetLORS(*mat).
Parameters
matpointer to a projection matrix.
rowrow index for accessing factors.
Returns
square sum of all factors in given projection matrix in given row.

Definition at line 409 of file prmat.c.

410{
411 return(mat->factor_sqr_sum[row]);
412}

Referenced by prmatSaveMatrix(), and radonSetLORS().

◆ prmatGetFactorSum()

float prmatGetFactorSum ( PRMAT * mat)

Returns sum of all factors in given projection matrix. For initializing image matrix.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
sum of all factors in given projection matrix.

Definition at line 397 of file prmat.c.

398{
399 return(mat->factor_sum);
400}
float factor_sum
The sum of all factors in the projection matrix.
Definition libtpcrec.h:183

◆ prmatGetID()

unsigned int prmatGetID ( PRMAT * mat)

Returns the image dimension in the geometrics.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
the image dimension in the geometrics.

Definition at line 197 of file prmat.c.

198{
199 return mat->imgDim;
200}

Referenced by radonBackTransformPRM(), and radonFwdTransformPRM().

◆ prmatGetMajor()

float prmatGetMajor ( PRMAT * mat)

Returns major semiaxe of the FOV in given projection matrix.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
major semiaxe of the FOV in given projection matrix

Definition at line 353 of file prmat.c.

354{
355 return(mat->prmatfov[0]);
356}
int * prmatfov
Definition libtpcrec.h:177

◆ prmatGetMax()

float prmatGetMax ( PRMAT * mat)

Returns greatest value in given projection matrix.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
maximal value in given projection matrix.

Definition at line 386 of file prmat.c.

386 {
387 return(mat->max);
388}
float max
Maximal factor value in the projection matrix.
Definition libtpcrec.h:181

◆ prmatGetMin()

float prmatGetMin ( PRMAT * mat)

Returns minimal value in given projection matrix.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
minimal value in given projection matrix.

Definition at line 375 of file prmat.c.

376{
377 return(mat->min);
378}
float min
Minimal factor value in the projection matrix.
Definition libtpcrec.h:179

◆ prmatGetMinor()

float prmatGetMinor ( PRMAT * mat)

Returns minor semiaxe of the FOV in given projection matrix.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
minor semiaxe of the FOV in given projection matrix

Definition at line 364 of file prmat.c.

365{
366 return(mat->prmatfov[1]);
367}

◆ prmatGetNB()

unsigned int prmatGetNB ( PRMAT * mat)

Returns the number of bins (distances) in the geometrics.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
the number of bins in the geometrics.

Definition at line 186 of file prmat.c.

187{
188 return mat->binNr;
189}
unsigned int binNr
Scanner geometrics, field binNr.
Definition libtpcrec.h:170

Referenced by radonBackTransformPRM(), and radonFwdTransformPRM().

◆ prmatGetNV()

unsigned int prmatGetNV ( PRMAT * mat)

Returns the number of views (angles) in the geometrics.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
the number of views in the geometrics.

Definition at line 175 of file prmat.c.

176{
177 return mat->viewNr;
178}
unsigned int viewNr
Scanner geometrics, field viewNr.
Definition libtpcrec.h:168

Referenced by radonBackTransformPRM(), and radonFwdTransformPRM().

◆ prmatGetPIX()

unsigned int prmatGetPIX ( PRMAT * mat)

Returns number of rows (pixels) in the look-up table.

Precondition
mat is initialized.
Parameters
matpointer to a projection matrix.
Returns
number of pixels in look-up table, some negative value if ERROR.

Definition at line 210 of file prmat.c.

211{
212 return mat->nrp;
213}

Referenced by prmatReadMatrix(), and prmatSaveMatrix().

◆ prmatGetPixCoord()

unsigned int prmatGetPixCoord ( PRMAT * mat,
int row )

Returns coordinates of a pixel in place 'row' in the look-up table.

Precondition
0<=row<=prmatGetPIX(*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowindex of the pixel whose coordinates are to be returned.
Returns
coordinates of a pixel in the look-up table, some negative value if ERROR.

Definition at line 223 of file prmat.c.

224{
225 return mat->lines[row][0];
226}

Referenced by prmatSaveMatrix().

◆ prmatGetPixels()

unsigned int prmatGetPixels ( PRMAT * mat,
int row )

Returns number of pixels intersected by the given line of response.

Precondition
0<=row<=prmatGetLORS(*mat) && mat is initialized.
Parameters
matpointer to the projection matrix.
rowindex of the line of response for which the number of pixels is returned.
Returns
number of pixels intersected by given line of response, some negative value if ERROR.

Definition at line 282 of file prmat.c.

283{
284 return(mat->dime[row]);
285}

Referenced by prmatReadMatrix(), prmatSaveMatrix(), radonBackTransformPRM(), radonFwdTransformPRM(), radonSetLORS(), and radonSetLUT().

◆ prmatGetRays()

unsigned int prmatGetRays ( PRMAT * mat,
int row )

Returns the number of lines of response (rays) intersecting the pixel in place 'row'.

Precondition
0<=row<=prmatGetPIX(*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowindex of the pixel for which the number of lors is returned.
Returns
number of lines of response intersecting the pixel, some negative value if ERROR.

Definition at line 237 of file prmat.c.

238{
239 return mat->lines[row][1];
240}

Referenced by prmatReadMatrix(), and prmatSaveMatrix().

◆ prmatGetRows()

unsigned int prmatGetRows ( PRMAT * mat)

Returns number of rows (lines of response) in the given projection matrix.

Precondition
mat is initialized.
Parameters
matpointer to the projection matrix.
Returns
number of rows in the given projection matrix, some negative value if ERROR.

Definition at line 267 of file prmat.c.

268{
269 //NOTE no checking on parameters to accelerate iteration
270 return(mat->dimr);
271}

Referenced by prmatSaveMatrix(), and radonSetLORS().

◆ prmatGetXCoord()

unsigned int prmatGetXCoord ( PRMAT * mat,
int row,
int pix )

Returns the x-coordinate of a pixel in place (row,pix) in the given projection matrix.

Precondition
0<=row<=prmatGetLORS(*mat) && 0<=pix<=prmatGetPixels(row,*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowthe row index.
pixthe (non-zero) pixel index.
Returns
coordinate of the referred pixel, some negative value if ERROR.

Definition at line 312 of file prmat.c.

313{
314 return (int)(mat->fact[row][pix][0]);
315}

Referenced by prmatSaveMatrix(), radonBackTransformPRM(), radonFwdTransformPRM(), radonSetLORS(), and radonSetLUT().

◆ prmatGetYCoord()

unsigned int prmatGetYCoord ( PRMAT * mat,
int row,
int pix )

Returns the y-coordinate of a pixel in place (row,pix) in the given projection matrix.

Precondition
0<=row<=prmatGetLORS(*mat) && 0<=pix<=prmatGetPixels(row,*mat) && mat is initialized.
Parameters
matpointer to a projection matrix.
rowthe row index.
pixthe (non-zero) pixel index.
Returns
coordinate of the referred pixel, some negative value if ERROR.

Definition at line 327 of file prmat.c.

328{
329 return (int)(mat->fact[row][pix][1]);
330}

Referenced by prmatSaveMatrix(), radonBackTransformPRM(), radonFwdTransformPRM(), radonSetLORS(), and radonSetLUT().

◆ prmatInit()

void prmatInit ( PRMAT * mat)

Initializes PRMAT datatype for use. To be utilised before any use of PRMAT type variables.

Parameters
matpointer to projection matrix to be initialized.

Definition at line 22 of file prmat.c.

23{
24 if(PRMAT_VERBOSE) printf("PRMAT:prmatInit() \n");
25
26 /*init the information of projection matrix*/
27 //buffer mat to contain zero
28 memset(mat, 0, sizeof(PRMAT));
29 mat->type=PRMAT_TYPE_NA;
30 mat->viewNr=0;
31 mat->binNr=0;
32 mat->mode=PRMAT_DMODE_01;
33 mat->prmatfov= (int*)NULL;
34 mat->max=mat->min=0.0;
35 mat->scaling_factor=1.0;
36 mat->factor_sum=0.0;
37 mat->factor_sqr_sum=(float*)NULL;
38
39 /*init look-up table*/
40 mat->nrp=0;
41 mat->lines=mat->_linesdata=(unsigned int**)NULL; //data array
42
43 /*init projection data*/
44 mat->dimr=0;
45 mat->dime=(unsigned int*)NULL;
46 mat->fact=mat->_factdata=(unsigned short int***)NULL; //data array
47
48 mat->status=PRMAT_STATUS_INITIALIZED;
49}
char type
Scanner information on the prmat. 0=ECAT931 1=GE Advance.
Definition libtpcrec.h:166
int mode
Discretisation model utilised.
Definition libtpcrec.h:174

Referenced by radonSetLORS().

◆ prmatReadMatrix()

int prmatReadMatrix ( char * fname,
PRMAT * mat )

Reads one projection matrix from the given file to the given PRMAT structure mat.

Precondition
mat is initialized.
Postcondition
a projection matrix is read from the file fname.
Parameters
fnamefile name to read.
matpointer to the projection matrix datatype.
Returns
0 if OK.

Definition at line 550 of file prmat.c.

551{
552 unsigned int fov[2], *dimentry=NULL, *nrlor, *iptr, dimtmp, count=0;
553 unsigned int row, lor=0, ind, fac;
554 unsigned short int *nrlor_c, *nrlor_l, *usiptr, *usiptr_c;
555 unsigned short int x_coordinate=0, y_coordinate=0, value=0;
556 float *sqrsum=NULL, *fptr, sqrtmp;
557 FILE *fp;
558
559 if(PRMAT_VERBOSE) printf("\n PRMAT:prmatReadMatrix(%s,mat) \n",fname);
560 if(mat->status==PRMAT_STATUS_UNINITIALIZED) return(1);
561 prmatEmpty(mat);
562
563 fp=fopen(fname,"rb");
564 if(fp==NULL) return(1);
565
566 //read first the statical parts of the structure
567 if(fread(&(mat->status),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
568 if(PRMAT_VERBOSE) printf("PRMAT: status: %i \n",mat->status);
569 if(fread(&(mat->type),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
570 if(PRMAT_VERBOSE) printf("PRMAT: scanner type: %i \n",mat->type);
571 if(fread(&(mat->viewNr),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
572 if(fread(&(mat->binNr),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
573 if(fread(&(mat->imgDim),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
574 if(PRMAT_VERBOSE)
575 printf("PRMAT: scanner geometrics: (%i,%i,%i) \n",
576 mat->viewNr,mat->binNr,mat->imgDim);
577 if(fread(&(mat->mode),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
578 if(PRMAT_VERBOSE) printf("PRMAT: discretisation mode: %i \n",mat->mode);
579 if(fread(fov,sizeof(int),1,fp)==0) {fclose(fp); return(2);}
580 if(fread((fov+1),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
581 mat->prmatfov=calloc(2,sizeof(int));
582 mat->prmatfov[0]=fov[0];
583 mat->prmatfov[1]=fov[1];
584 if(PRMAT_VERBOSE)
585 printf("PRMAT: FOV: (%i,%i) \n", mat->prmatfov[0],mat->prmatfov[1]);
586 // Read min, max, sum and scaling factor.
587 if(fread(&(mat->min),sizeof(float),1,fp)==0) {fclose(fp); return(2);}
588 if(PRMAT_VERBOSE) printf("PRMAT: min: %f \n",mat->min);
589 if(fread(&(mat->max),sizeof(float),1,fp)==0) {fclose(fp); return(2);}
590 if(PRMAT_VERBOSE) printf("PRMAT: max: %f \n",mat->max);
591 if(fread(&(mat->factor_sum),sizeof(float),1,fp)==0) {fclose(fp); return(2);}
592 if(PRMAT_VERBOSE) printf("PRMAT: sum of all factors: %f \n",mat->factor_sum);
593 if(fread(&(mat->scaling_factor),sizeof(float),1,fp)==0) {fclose(fp); return(2);}
594 if(PRMAT_VERBOSE) printf("PRMAT: scaling factor: %f \n",mat->scaling_factor);
595
596 // If projection matrix has been occupied.
597 if(mat->status >= PRMAT_STATUS_BS_OCCUPIED){
598 if(PRMAT_VERBOSE) printf("PRMAT: reading projection matrix \n");
599 if(fread(&(mat->dimr),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
600 if(PRMAT_VERBOSE)
601 printf("PRMAT: number of rows in the projection matrix: %i \n",mat->dimr);
602 //read square sums
603 sqrsum=calloc(mat->dimr,sizeof(float));
604 for(row=0, fptr=sqrsum;row<(mat->dimr); row++, fptr++) {
605 if(fread(&sqrtmp,sizeof(float),1,fp)==0) {fclose(fp); return(2);}
606 *fptr=sqrtmp;
607 }
608 //read dime
609 dimentry=(unsigned int*)calloc(mat->dimr,sizeof(int));
610 count = 0;
611 for(row=0,iptr=dimentry;row<(mat->dimr);row++,iptr++){
612 if(fread(&dimtmp,sizeof(int),1,fp)==0) {fclose(fp); return(2);}
613 *iptr=dimtmp;
614 count += dimtmp;
615 }
616
617 //ready to allocate; NOTE that dime is also set in function prmatAllocate()
618 prmatAllocate(mat,1,mat->dimr,dimentry);
619 if(PRMAT_VERBOSE)
620 printf("PRMAT:prmatAllocate(mat) done in prmatReadMatrix(%s,mat)\n",fname);
621
622 //now we can set square sums
623 for(row=0, fptr=sqrsum;row<(mat->dimr);row++, fptr++){
624 mat->factor_sqr_sum[row]=*fptr;
625 }
626 //read the actual data _factdata
627 for(row=0;row<(mat->dimr);row++){
628 //read coordinates and values in every row
629 for(fac=0;fac<prmatGetPixels(mat,row);fac++){
630 if(fread(&x_coordinate,sizeof(unsigned short int),1,fp)==0) {
631 fclose(fp); return(2);}
632 mat->fact[row][fac][0]=x_coordinate;
633 if(fread(&y_coordinate,sizeof(unsigned short int),1,fp)==0) {
634 fclose(fp); return(2);}
635 mat->fact[row][fac][1]=y_coordinate;
636 if(fread(&value,sizeof(unsigned short int),1,fp)==0) {
637 fclose(fp); return(2);}
638 mat->fact[row][fac][2]=value;
639 }
640 }
641 }// END OF READING PROJECTIONS
642
643 // If look-up table has been occupied.
644 if(mat->status == PRMAT_STATUS_LU_OCCUPIED){
645 if(PRMAT_VERBOSE) printf("PRMAT: reading look-up table \n");
646 if(fread(&(mat->nrp),sizeof(int),1,fp)==0) {fclose(fp); return(2);}
647 if(PRMAT_VERBOSE)
648 printf("PRMAT: number of pixels inside the FOV: %i \n",mat->nrp);
649 //read nrlor
650 nrlor_c=(unsigned short int*)calloc(mat->nrp,sizeof(unsigned short int));
651 nrlor_l=(unsigned short int*)calloc(mat->nrp,sizeof(unsigned short int));
652
653 //first coordinates of pixels.
654 usiptr = nrlor_c;
655 for(row=0; row<mat->nrp; row++, usiptr++){
656 // Read next coordinate from file.
657 if(fread(usiptr,sizeof(unsigned short int),1,fp)==0) {fclose(fp); return(2);}
658 }
659 //then number of lors hitting a pixel
660 usiptr = nrlor_l;
661 for(row=0; row<mat->nrp; row++, usiptr++){
662 // Read next lornr from file.
663 if(fread(usiptr,sizeof(unsigned short int),1,fp)==0) {fclose(fp); return(2);}
664 }
665
666 if(PRMAT_VERBOSE){
667 // Get number of lors in lut.
668 usiptr = nrlor_l;
669 count = 0;
670 for(row=0; row<mat->nrp; row++, usiptr++){
671 count += *usiptr;
672 }
673 printf("PRMAT: nr of lors in lut: %i \n",count);
674 //printf(" nr of lors in ave: %i \n",count/mat->nrp);
675 }
676
677 // Copy list usi format to integer list.
678 nrlor = (unsigned int*)calloc(mat->nrp,sizeof(int));
679 usiptr = nrlor_l;
680 iptr = nrlor;
681 for(row=0; row<mat->nrp; row++, iptr++){
682 *iptr = (int)*usiptr++;
683 }
684
685 // Now we can allocate memory for look-up table.
686 prmatAllocate(mat, 0, mat->nrp, nrlor);
687 if(PRMAT_VERBOSE)
688 printf("PRMAT:prmatAllocate(mat) done in prmatReadMatrix(%s,mat)\n",fname);
689 // And fill mat->lines.
690 usiptr_c = nrlor_c;
691 usiptr = nrlor_l;
692 for(row=0;row<mat->nrp;row++ , usiptr++, usiptr_c++){
693 mat->lines[row][0] = *usiptr_c;
694 mat->lines[row][1] = *usiptr;
695 }
696
697 // Read the data _linesdata.
698 for(row=0;row<prmatGetPIX(mat);row++){
699 for(ind=0; ind<prmatGetRays(mat,row); ind++){
700 if(fread(&lor,sizeof(unsigned int),1,fp)==0) {fclose(fp); return(2);}
701 mat->lines[row][ind+2] = lor;
702 }
703 }
704
705 free((unsigned short int*)nrlor_l);
706 free((unsigned short int*)nrlor_c);
707 }// END OF READING LOOK-UP TABLE
708
709 if(PRMAT_VERBOSE) printf("PRMAT: prmatReadMatrix() done. \n");
710 free((float*)sqrsum); free((int*)dimentry);
711
712 fclose(fp);
713
714 return(0);
715}
unsigned int prmatGetPIX(PRMAT *mat)
Definition prmat.c:210
unsigned int prmatGetRays(PRMAT *mat, int row)
Definition prmat.c:237
void prmatEmpty(PRMAT *mat)
Definition prmat.c:56
int prmatAllocate(PRMAT *mat, int set, unsigned int rows, unsigned int *coords)
Definition prmat.c:105
unsigned int prmatGetPixels(PRMAT *mat, int row)
Definition prmat.c:282

◆ prmatSaveMatrix()

int prmatSaveMatrix ( PRMAT * mat)

Adds the given projection matrix to the file called prTDXXYY.prm. Where T is the type of the scanner (E=ECAT931 and G=GE Advance), D is the type of discretisation (0='0/1', 1='length of intersection' and 2='exact area'), XX is the major semiaxe of the FOV, and YY the minor semiaxe. If file already exists it is NOT created or rewritten.

Precondition
mat is initialized (contains a projection matrix).
Postcondition
mat is saved in the file prTDXXYY.prm.
Parameters
matpointer to the projection matrix to be saved.
Returns
0 if OK.

Definition at line 428 of file prmat.c.

429{
430 char matrixfile[FILENAME_MAX], semi_x_c[2], semi_y_c[2], mod[2];
431 int semi_x=mat->prmatfov[0], semi_y=mat->prmatfov[1], mode=mat->mode, itmp;
432 unsigned short int x_coordinate, y_coordinate, value, p_coord, lors;
433 unsigned int lor, row, fac, ind;
434 float ftmp;
435 FILE *fp;
436
437 //Compose the file name to write
438 sprintf(semi_x_c,"%i",semi_x);
439 sprintf(semi_y_c,"%i",semi_y);
440 //decide the type
441 if(mat->type==PRMAT_TYPE_ECAT931) strcpy(matrixfile,"prE");
442 else
443 if(mat->type==PRMAT_TYPE_GE) strcpy(matrixfile,"prG");
444 else strcpy(matrixfile,"prN");
445 //decide the mode
446 sprintf(mod,"%i",mode);
447 strcat(matrixfile,mod);
448 strcat(matrixfile,semi_x_c);
449 strcat(matrixfile,semi_y_c);
450 strcat(matrixfile,".prm");
451
452 if(access(matrixfile, 0) != -1) {
453 if(PRMAT_VERBOSE) printf("PRMAT:File %s already exists.\n", matrixfile);
454 return(0);
455 }
456
457 fp=fopen(matrixfile,"wb+");
458 if(fp==NULL) return(1);
459
460 // Save status.
461 fwrite(&(mat->status),sizeof(int),1,fp);
462
463 // Save type of the scanner.
464 fwrite(&(mat->type),sizeof(int),1,fp);
465
466 // Save scanner geometrics.
467 fwrite(&(mat->viewNr),sizeof(int),1,fp);
468 fwrite(&(mat->binNr),sizeof(int),1,fp);
469 fwrite(&(mat->imgDim),sizeof(int),1,fp);
470
471 // Save mode.
472 fwrite(&(mat->mode),sizeof(int),1,fp);
473
474 // Save FOV.
475 fwrite(&(mat->prmatfov[0]),sizeof(int),1,fp);
476 fwrite(&(mat->prmatfov[1]),sizeof(int),1,fp);
477
478 // Save min, max, sum and scaling factor.
479 fwrite(&(mat->min),sizeof(float),1,fp);
480 fwrite(&(mat->max),sizeof(float),1,fp);
481 fwrite(&(mat->factor_sum),sizeof(float),1,fp);
482 fwrite(&(mat->scaling_factor),sizeof(float),1,fp);
483
484 // If projection matrix is set.
485 if(mat->status >= PRMAT_STATUS_BS_OCCUPIED){
486
487 fwrite(&(mat->dimr),sizeof(int),1,fp);
488 //square sums
489 for(row=0;row<prmatGetRows(mat);row++){
490 ftmp=prmatGetFactorSqrSum(mat,row);
491 fwrite(&ftmp,sizeof(float),1,fp);
492 }
493 //dime
494 for(row=0;row<prmatGetRows(mat);row++){
495 itmp=prmatGetPixels(mat,row);
496 fwrite(&itmp,sizeof(int),1,fp);
497 }
498 //the actual data _factdata
499 for(row=0;row<prmatGetRows(mat);row++){
500 //coordinates and values in every row
501 for(fac=0;fac<prmatGetPixels(mat,row);fac++){
502 x_coordinate=prmatGetXCoord(mat,row,fac);
503 fwrite(&x_coordinate,sizeof(unsigned short int),1,fp);
504 y_coordinate=prmatGetYCoord(mat,row,fac);
505 fwrite(&y_coordinate,sizeof(unsigned short int),1,fp);
506 //NOTE that we wan't to save the values in unsigned short ints
507 value=mat->fact[row][fac][2];
508 fwrite(&value,sizeof(unsigned short int),1,fp);
509 }
510 }
511 }// END OF SAVING PROJECTION MATRIX.
512
513 // If look-up table is set.
514 if(mat->status == PRMAT_STATUS_LU_OCCUPIED){
515 //save first the statical parts of the structure
516 fwrite(&(mat->nrp),sizeof(int),1,fp);
517 //save lines: first coordinates of pixels inside the fov,
518 for(row=0;row<prmatGetPIX(mat);row++){
519 p_coord=prmatGetPixCoord(mat,row);
520 fwrite(&p_coord,sizeof(unsigned short int),1,fp);
521 }
522 // then number of lors hitting a pixel.
523 for(row=0; row<prmatGetPIX(mat); row++){
524 lors=prmatGetRays(mat,row);
525 fwrite(&lors,sizeof(unsigned short int),1,fp);
526 }
527
528 //save lors
529 for(row=0;row<prmatGetPIX(mat);row++){
530 for(ind=0; ind<prmatGetRays(mat,row); ind++){
531 lor = prmatGetBinView(mat,row,ind);
532 fwrite(&lor,sizeof(unsigned int),1,fp);
533 }
534 }
535 }// END OF SAVING LOOK-UP TABLE.
536
537 fclose(fp);
538 return(0);
539}
unsigned int prmatGetBinView(PRMAT *mat, int row, int ind)
Definition prmat.c:253
unsigned int prmatGetRows(PRMAT *mat)
Definition prmat.c:267
float prmatGetFactorSqrSum(PRMAT *mat, int row)
Definition prmat.c:409
unsigned int prmatGetYCoord(PRMAT *mat, int row, int pix)
Definition prmat.c:327
unsigned int prmatGetXCoord(PRMAT *mat, int row, int pix)
Definition prmat.c:312
unsigned int prmatGetPixCoord(PRMAT *mat, int row)
Definition prmat.c:223

Variable Documentation

◆ PRMAT_TEST

int PRMAT_TEST

If not 0 drive in test mode.

Definition at line 8 of file prmat.c.

◆ PRMAT_VERBOSE

int PRMAT_VERBOSE

If not 0 drive in verbose mode.

Definition at line 9 of file prmat.c.

Referenced by prmatAllocate(), prmatEmpty(), prmatInit(), prmatReadMatrix(), and prmatSaveMatrix().