TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcrec
ellipse.c
Go to the documentation of this file.
1
7
/*****************************************************************************/
8
int
ELLIPSE_TEST
;
9
int
ELLIPSE_VERBOSE
;
10
/*****************************************************************************/
11
#include "
libtpcrec.h
"
12
/*****************************************************************************/
13
14
/*****************************************************************************/
15
/* Initialization and memory handling for ellipse data.*/
16
/*****************************************************************************/
23
void
ellipseInit
(
ELLIPSE
*ell)
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
}
40
/*****************************************************************************/
47
void
ellipseEmpty
(
ELLIPSE
*ell)
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
}
68
/*****************************************************************************/
73
void
ellipseInfo
(
ELLIPSE
*ell)
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
}
85
/*****************************************************************************/
94
int
ellipseAllocate
(
ELLIPSE
*ell,
int
imgDim)
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
}
110
/*****************************************************************************/
111
/* GET and SET procedures for ELLIPSE datatype.
112
* USE ONLY THESE FOR SETTING AND GETTING. */
113
/*****************************************************************************/
119
int
ellipseSetFromParams
(
122
ELLIPSE
*ell,
125
int
imgDim,
128
float
*semis,
131
float
*cent,
133
float
incli,
135
float
val
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
}
180
/*****************************************************************************/
198
int
ellipseReadEllipse
(
200
FILE *fp,
203
ELLIPSE
*ell
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(¢er[0],
sizeof
(
float
),1,fp)==0) {fclose(fp);
return
(2);}
214
if
(fread(¢er[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
}
225
/*****************************************************************************/
230
int
ellipseSaveEllipse
(
232
ELLIPSE
*ell,
234
FILE *fp
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
}
249
/*****************************************************************************/
254
float
ellipseGetMajor
(
ELLIPSE
*ell)
255
{
256
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetMajor(). \n"
);
257
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
258
return
(ell->
semiaxis
[0]);
259
}
260
/*****************************************************************************/
265
float
ellipseGetMinor
(
ELLIPSE
*ell)
266
{
267
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetMinor(). \n"
);
268
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
269
return
(ell->
semiaxis
[1]);
270
}
271
/*****************************************************************************/
277
float
ellipseGetCenterX
(
ELLIPSE
*ell)
278
{
279
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetCenterX(). \n"
);
280
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
281
return
(ell->
center
[0]);
282
}
283
/*****************************************************************************/
289
float
ellipseGetCenterY
(
ELLIPSE
*ell)
290
{
291
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetCenterY(). \n"
);
292
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
293
return
(ell->
center
[1]);
294
}
295
/*****************************************************************************/
300
float
ellipseGetInclination
(
ELLIPSE
*ell)
301
{
302
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetInclination(). \n"
);
303
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
304
return
(ell->
inclination
);
305
}
306
/*****************************************************************************/
312
int
ellipseGetImgSize
(
ELLIPSE
*ell)
313
{
314
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetImgSize(). \n"
);
315
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
316
return
(ell->
imageDim
);
317
}
318
/*****************************************************************************/
323
int
ellipseGetValue
(
ELLIPSE
*ell)
324
{
325
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetValue(). \n"
);
326
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
(-1);
327
return
(ell->
value
);
328
}
329
/*****************************************************************************/
339
int
**
ellipseGetArray
(
ELLIPSE
*ell)
340
{
341
if
(
ELLIPSE_VERBOSE
) printf(
"ELLIPSE:ellipseGetArray(). \n"
);
342
if
(ell->
status
==ELLIPSE_STATUS_UNINITIALIZED)
return
((
int
**)-1);
343
return
(ell->
ellipseptr
);
344
}
345
/*****************************************************************************/
346
/* Testing procedures for ellipse. */
347
/*****************************************************************************/
352
int
ellipseIsInside
(
355
ELLIPSE
*ell,
357
int
row,
359
int
col
360
) {
361
//no checking on parameters to accelerate use inside a loop
362
return
(ell->
ellipseptr
[row][col]);
363
}
364
/*****************************************************************************/
365
366
/*****************************************************************************/
ellipseAllocate
int ellipseAllocate(ELLIPSE *ell, int imgDim)
Definition
ellipse.c:94
ellipseInfo
void ellipseInfo(ELLIPSE *ell)
Definition
ellipse.c:73
ellipseReadEllipse
int ellipseReadEllipse(FILE *fp, ELLIPSE *ell)
Definition
ellipse.c:198
ellipseGetInclination
float ellipseGetInclination(ELLIPSE *ell)
Definition
ellipse.c:300
ellipseGetImgSize
int ellipseGetImgSize(ELLIPSE *ell)
Definition
ellipse.c:312
ellipseSetFromParams
int ellipseSetFromParams(ELLIPSE *ell, int imgDim, float *semis, float *cent, float incli, float val)
Definition
ellipse.c:119
ellipseGetMinor
float ellipseGetMinor(ELLIPSE *ell)
Definition
ellipse.c:265
ELLIPSE_VERBOSE
int ELLIPSE_VERBOSE
Drive in verbose mode if not 0.
Definition
ellipse.c:9
ellipseGetValue
int ellipseGetValue(ELLIPSE *ell)
Definition
ellipse.c:323
ellipseGetCenterY
float ellipseGetCenterY(ELLIPSE *ell)
Definition
ellipse.c:289
ellipseInit
void ellipseInit(ELLIPSE *ell)
Definition
ellipse.c:23
ellipseEmpty
void ellipseEmpty(ELLIPSE *ell)
Definition
ellipse.c:47
ellipseGetArray
int ** ellipseGetArray(ELLIPSE *ell)
Definition
ellipse.c:339
ellipseGetMajor
float ellipseGetMajor(ELLIPSE *ell)
Definition
ellipse.c:254
ellipseSaveEllipse
int ellipseSaveEllipse(ELLIPSE *ell, FILE *fp)
Definition
ellipse.c:230
ellipseGetCenterX
float ellipseGetCenterX(ELLIPSE *ell)
Definition
ellipse.c:277
ellipseIsInside
int ellipseIsInside(ELLIPSE *ell, int row, int col)
Definition
ellipse.c:352
ELLIPSE_TEST
int ELLIPSE_TEST
Drive in test mode if not 0.
Definition
ellipse.c:8
libtpcrec.h
Header file for libtpcrec.
ELLIPSE
Ellipse on two dimensional plane.
Definition
libtpcrec.h:37
ELLIPSE::value
float value
Value inside the ellipse.
Definition
libtpcrec.h:57
ELLIPSE::inclination
float inclination
Inclination (degrees).
Definition
libtpcrec.h:48
ELLIPSE::status
char status
ellipse status.
Definition
libtpcrec.h:39
ELLIPSE::semiaxis
float semiaxis[2]
Definition
libtpcrec.h:43
ELLIPSE::imageDim
int imageDim
Size of the image plane on which the ellipse is defined.
Definition
libtpcrec.h:50
ELLIPSE::ellipseptr
int ** ellipseptr
Definition
libtpcrec.h:55
ELLIPSE::center
float center[2]
Definition
libtpcrec.h:46
Generated on
for TPCCLIB by
1.17.0