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

2D plane model of an ellipse. More...

#include "libtpcrec.h"

Go to the source code of this file.

Functions

void ellipseInit (ELLIPSE *ell)
void ellipseEmpty (ELLIPSE *ell)
void ellipseInfo (ELLIPSE *ell)
int ellipseAllocate (ELLIPSE *ell, int imgDim)
int ellipseSetFromParams (ELLIPSE *ell, int imgDim, float *semis, float *cent, float incli, float val)
int ellipseReadEllipse (FILE *fp, ELLIPSE *ell)
int ellipseSaveEllipse (ELLIPSE *ell, FILE *fp)
float ellipseGetMajor (ELLIPSE *ell)
float ellipseGetMinor (ELLIPSE *ell)
float ellipseGetCenterX (ELLIPSE *ell)
float ellipseGetCenterY (ELLIPSE *ell)
float ellipseGetInclination (ELLIPSE *ell)
int ellipseGetImgSize (ELLIPSE *ell)
int ellipseGetValue (ELLIPSE *ell)
int ** ellipseGetArray (ELLIPSE *ell)
int ellipseIsInside (ELLIPSE *ell, int row, int col)

Variables

int ELLIPSE_TEST
 Drive in test mode if not 0.
int ELLIPSE_VERBOSE
 Drive in verbose mode if not 0.

Detailed Description

2D plane model of an ellipse.

Author
Jarkko Johansson
Date
2004-04-20

Definition in file ellipse.c.

Function Documentation

◆ ellipseAllocate()

int ellipseAllocate ( ELLIPSE * ell,
int imgDim )

Allocates memory for ELLIPSE data. Normally used only in SET-functions.

Precondition
ell is initialized && imgDim is positive
Postcondition
memory is allocated for ELLIPSE structure.
Parameters
ellpointer to ellipse for which the allocation is done.
imgDimsize of the image plane on which the ellipse is to be done.
Returns
0 if ok.

Definition at line 94 of file ellipse.c.

95{
96 int i;
97 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseAllocate(*ell,%i) \n",imgDim);
98
99 /*check the arguments*/
100 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(1);
101 if(imgDim<1 || imgDim>4096) return(2);
102
103 //allocate memory
104 ell->ellipseptr=(int**)calloc(imgDim,sizeof(int*));
105 for(i=0;i<imgDim;i++){
106 ell->ellipseptr[i]=(int*)calloc(imgDim,sizeof(int));
107 }
108 return(0);
109}
int ELLIPSE_VERBOSE
Drive in verbose mode if not 0.
Definition ellipse.c:9
char status
ellipse status.
Definition libtpcrec.h:39
int ** ellipseptr
Definition libtpcrec.h:55

Referenced by ellipseSetFromParams().

◆ ellipseEmpty()

void ellipseEmpty ( ELLIPSE * ell)

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

Postcondition
ellipse is emptied.
Parameters
ellpointer to ellipse to be emptied.

Definition at line 47 of file ellipse.c.

48{
49 if(ELLIPSE_VERBOSE) printf("ELLIPSE: ellipseEmpty(). \n");
50
51 //if ell is already empty
52 if(ell->status<ELLIPSE_STATUS_OCCUPIED) return;
53
54 //free the memory occupied by ellipse array
55 free(ell->ellipseptr);
56
57 //Init the information again
58 ell->semiaxis[0]=ell->semiaxis[1]=0.;
59 ell->center[0]=ell->center[1]=0.;
60 ell->inclination=0.;
61 ell->imageDim=0;
62 ell->value=0;
63 /*Init the ellipse array*/
64 ell->ellipseptr=(int**)NULL;
65
66 ell->status=ELLIPSE_STATUS_INITIALIZED;
67}
float value
Value inside the ellipse.
Definition libtpcrec.h:57
float inclination
Inclination (degrees).
Definition libtpcrec.h:48
float semiaxis[2]
Definition libtpcrec.h:43
int imageDim
Size of the image plane on which the ellipse is defined.
Definition libtpcrec.h:50
float center[2]
Definition libtpcrec.h:46

Referenced by ellipseReadEllipse().

◆ ellipseGetArray()

int ** ellipseGetArray ( ELLIPSE * ell)

Returns the ellipse array of the given ellipse.

Ellipse array contains n x n items, single item is one if it is inside the ellipse and zero otherwise. Coordinates on a two dimensional plane are numbered from upper left corner.

Parameters
ellpointer to ellipse.
Returns
the ellipse array of the given ellipse.

Definition at line 339 of file ellipse.c.

340{
341 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetArray(). \n");
342 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return((int**)-1);
343 return(ell->ellipseptr);
344}

◆ ellipseGetCenterX()

float ellipseGetCenterX ( ELLIPSE * ell)

Returns the center x-coordinate of the ellipse.

Parameters
ellpointer to ellipse.
Returns
the center x-coordinate of the given ellipse, some negative value if ERROR.

Definition at line 277 of file ellipse.c.

278{
279 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetCenterX(). \n");
280 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
281 return(ell->center[0]);
282}

◆ ellipseGetCenterY()

float ellipseGetCenterY ( ELLIPSE * ell)

Returns the center y-coordinate of the ellipse.

Parameters
ellpointer to ellipse.
Returns
the center y-coordinate of the given ellipse, some negative value if ERROR.

Definition at line 289 of file ellipse.c.

290{
291 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetCenterY(). \n");
292 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
293 return(ell->center[1]);
294}

◆ ellipseGetImgSize()

int ellipseGetImgSize ( ELLIPSE * ell)

Returns the size of the image plane on which the ellipse is drawn.

Parameters
ellpointer to ellipse.
Returns
the size of the image plane of the given ellipse, some negative value if ERROR.

Definition at line 312 of file ellipse.c.

313{
314 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetImgSize(). \n");
315 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
316 return(ell->imageDim);
317}

◆ ellipseGetInclination()

float ellipseGetInclination ( ELLIPSE * ell)

Returns the inclination of the ellipse.

Parameters
ellpointer to ellipse.
Returns
the inclination of the given ellipse, some negative value if ERROR.

Definition at line 300 of file ellipse.c.

301{
302 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetInclination(). \n");
303 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
304 return(ell->inclination);
305}

◆ ellipseGetMajor()

float ellipseGetMajor ( ELLIPSE * ell)

Returns the major semiaxe of the ellipse.

Parameters
ellpointer to ellipse.
Returns
the major semiaxe of the given ellipse, some negative value if ERROR.

Definition at line 254 of file ellipse.c.

255{
256 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetMajor(). \n");
257 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
258 return(ell->semiaxis[0]);
259}

Referenced by radonSetBases(), and radonSetBasesEA().

◆ ellipseGetMinor()

float ellipseGetMinor ( ELLIPSE * ell)

Returns the minor semiaxe of the ellipse.

Parameters
ellpointer to ellipse.
Returns
the minor semiaxe of the given ellipse, some negative value if ERROR.

Definition at line 265 of file ellipse.c.

266{
267 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetMinor(). \n");
268 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
269 return(ell->semiaxis[1]);
270}

Referenced by radonSetBases(), and radonSetBasesEA().

◆ ellipseGetValue()

int ellipseGetValue ( ELLIPSE * ell)

Returns the value of the pixels inside the ellipse.

Parameters
ellpointer to ellipse.
Returns
the value of the pixels inside the ellipse.

Definition at line 323 of file ellipse.c.

324{
325 if(ELLIPSE_VERBOSE) printf("ELLIPSE:ellipseGetValue(). \n");
326 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(-1);
327 return(ell->value);
328}

◆ ellipseInfo()

void ellipseInfo ( ELLIPSE * ell)

Prints the information of the ellipse to the screen.

Parameters
ellpointer to ellipse to be printed.

Definition at line 73 of file ellipse.c.

74{
75 printf("ellipseInfo()\n");
76 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED){
77 printf("Ellipse data not initialized.\n"); return;
78 }
79 printf("Semiaxis: (%.5f,%.5f)\n",ell->semiaxis[0], ell->semiaxis[1]);
80 printf("Center: (%.2f,%.2f)\n",ell->center[0],ell->center[1]);
81 printf("Inclination (degrees): %f\n",ell->inclination);
82 printf("Image dimension: %i\n",ell->imageDim);
83 printf("Value: %f\n",ell->value);
84}

◆ ellipseInit()

void ellipseInit ( ELLIPSE * ell)

Initializes ELLIPSE datatype for use. To be used before any use of ELLIPSE type variables.

Postcondition
ellipse is initialized.
Parameters
ellpointer to ellipse to be initialized.

Definition at line 23 of file ellipse.c.

24{
25 if(ELLIPSE_VERBOSE) printf("ELLIPSE: ellipseInit(). \n");
26
27 /*Init the information on ellipse.*/
28 //buffer ell to contain zero
29 memset(ell, 0, sizeof(ELLIPSE));
30 ell->status=ELLIPSE_STATUS_INITIALIZED;
31 ell->semiaxis[0]=ell->semiaxis[1]=0.;
32 ell->center[0]=ell->center[1]=0.;
33 ell->inclination=0.;
34 ell->imageDim=0;
35 ell->value=0;
36
37 /*Init the ellipse array*/
38 ell->ellipseptr=(int**)NULL;
39}
Ellipse on two dimensional plane.
Definition libtpcrec.h:37

◆ ellipseIsInside()

int ellipseIsInside ( ELLIPSE * ell,
int row,
int col )

Tests whether the given pixel is inside the given ellipse or not.

Returns
one if the given pixel is inside the given ellipse zero otherwise, some negative value if ERROR.
Parameters
ellellipse on which the testing is to be done.
Precondition
ell is initialized && 0<=row<=imgDim-1 && 0<=col<=imgDim-1.
Parameters
rowrow coordinate of a pixel.
colcolumn coordinate of a pixel.

Definition at line 352 of file ellipse.c.

360 {
361 //no checking on parameters to accelerate use inside a loop
362 return(ell->ellipseptr[row][col]);
363}

Referenced by radonSetBases(), radonSetBasesEA(), and radonSetLUT().

◆ ellipseReadEllipse()

int ellipseReadEllipse ( FILE * fp,
ELLIPSE * ell )

Reads one ellipse from the given file to the given ELLIPSE structure.

A coordinate file contains the parameters of the ellipses in one line in the following order: Coordinate 1: v the additive intensity value of the ellipse Coordinate 2: a the length of the horizontal semi-axis of the ellipse Coordinate 3: b the length of the vertical semi-axis of the ellipse Coordinate 4: x the x-coordinate of the center of the ellipse Coordinate 5: y the y-coordinate of the center of the ellipse Coordinate 6: p the angle (in degrees) between the horizontal semi-axis of the ellipse and the x-axis of the image
Coordinate 7: d the image dimension

Postcondition
An ellipse is read from the file fname.
Returns
0 if ok 1 if there were no more ellipses.
Parameters
fpA pointer to open file containing ellipse(s) in correct format.
ellpointer to ELLIPSE structure where the read ellipse is to be set.
Precondition
ell is initialized.

Definition at line 198 of file ellipse.c.

204 {
205 float v, incli, semiaxis[2], center[2], imgDim;
206 int ret=0;
207
208 if(ELLIPSE_VERBOSE) printf("ellipseReadEllipse() \n");
209
210 if(fread(&v,sizeof(float),1,fp)==0) {fclose(fp); return(2);}
211 if(fread(&semiaxis[0],sizeof(float),1,fp)==0) {fclose(fp); return(2);}
212 if(fread(&semiaxis[1],sizeof(float),1,fp)==0) {fclose(fp); return(2);}
213 if(fread(&center[0],sizeof(float),1,fp)==0) {fclose(fp); return(2);}
214 if(fread(&center[1],sizeof(float),1,fp)==0) {fclose(fp); return(2);}
215 if(fread(&incli,sizeof(float),1,fp)==0) {fclose(fp); return(2);}
216 if(fread(&imgDim,sizeof(float),1,fp)==0) {fclose(fp); return(2);}
217
218 if(feof(fp)) return 1;
219
220 //create the ellipse
221 ret=ellipseSetFromParams(ell, imgDim, semiaxis, center, incli,v);
222 if(ret){ ellipseEmpty(ell); return ret;}
223 return 0;
224}
int ellipseSetFromParams(ELLIPSE *ell, int imgDim, float *semis, float *cent, float incli, float val)
Definition ellipse.c:119
void ellipseEmpty(ELLIPSE *ell)
Definition ellipse.c:47

◆ ellipseSaveEllipse()

int ellipseSaveEllipse ( ELLIPSE * ell,
FILE * fp )

Adds the given ellipse to the file.

Postcondition
ellipse is saved in the file called fname.
Returns
0 if OK.
Parameters
ellPointer to the ellipse to be saved.
fpOpen file for saving.

Definition at line 230 of file ellipse.c.

235 {
236 if(ELLIPSE_VERBOSE) printf("ellipseSaveEllipse()\n");
237
238 // Put ellipse in the file.
239 fwrite(&(ell->value),sizeof(float),1,fp);
240 fwrite(&(ell->semiaxis[0]),sizeof(float),1,fp);
241 fwrite(&(ell->semiaxis[1]),sizeof(float),1,fp);
242 fwrite(&(ell->center[0]),sizeof(float),1,fp);
243 fwrite(&(ell->center[1]),sizeof(float),1,fp);
244 fwrite(&(ell->inclination),sizeof(float),1,fp);
245 fwrite(&(ell->imageDim),sizeof(float),1,fp);
246
247 return(0);
248}

◆ ellipseSetFromParams()

int ellipseSetFromParams ( ELLIPSE * ell,
int imgDim,
float * semis,
float * cent,
float incli,
float val )

Sets the ellipse according to given coordinates and image dimension.

Note
The origin is in the middle.
Postcondition
ellipse is set.
Returns
0 if ok.
Parameters
ellpointer to ellipse for which the setting is to be done.
Precondition
ell is initialized.
Parameters
imgDimsize of the image plane.
Precondition
imgDim is positive.
Parameters
semismajor and minor semiaxis of the ellipse.
Precondition
semis[i] are positive.
Parameters
centcenter of the ellipse.
Precondition
-imgDim/2<=cent[i]<=imgDim/2.
Parameters
incliinclination of the ellipse.
valvalue inside the ellipse.

Definition at line 119 of file ellipse.c.

136 {
137 float d, x, y, inclination;
138 int row, col;
139
140 /*check the arguments*/
141 if(ell->status==ELLIPSE_STATUS_UNINITIALIZED) return(1);
142 if(imgDim<1 || imgDim>4096) return(2);
143 if(0>semis[0] || 0>semis[1]) return(3);
144 if(-(float)imgDim/2.>cent[0] || (float)imgDim/2.<cent[0]) return(4);
145 if(-(float)imgDim/2.>cent[1] || (float)imgDim/2.<cent[1]) return(5);
146
147 //allocate memory for ellipse
148 ellipseAllocate(ell,imgDim);
149
150 //set the information on ellipse
151 ell->semiaxis[0]=semis[0];
152 ell->semiaxis[1]=semis[1];
153 ell->center[0]=cent[0];
154 ell->center[1]=cent[1];
155 ell->inclination=incli;
156 ell->imageDim=imgDim;
157 ell->value=val;
158
159 //set inclination ratio for setting inclination approximately
160 inclination= -(float)(incli/90.);
161
162 //set ellipse array according to given arguments
163 for(row=0; row<imgDim; row++) {
164 for(col=0; col<imgDim; col++) {
165 // the y-coordinate of row in coordinates where origin is set in the middle
166 y=(float)imgDim*0.5 - (float)row - cent[1];
167 /* the x-coordinate of column in coordinates where origin is set in
168 the middle and inclination is added */
169 x=(float)col - (float)imgDim*0.5 - (cent[0] + inclination*y);
170 d=(x*x)/(semis[0]*semis[0]) + (y*y)/(semis[1]*semis[1]);
171 //if this pixel is inside the ellipse
172 if(d<1.0) {
173 //put 1 in place (row,col)
174 ell->ellipseptr[row][col]=1;
175 }
176 }//END OF COL-LOOP
177 }//END OF ROW-LOOP
178 return(0);
179}
int ellipseAllocate(ELLIPSE *ell, int imgDim)
Definition ellipse.c:94

Referenced by ellipseReadEllipse().

Variable Documentation

◆ ELLIPSE_TEST

int ELLIPSE_TEST

Drive in test mode if not 0.

Definition at line 8 of file ellipse.c.

◆ ELLIPSE_VERBOSE