-
Notifications
You must be signed in to change notification settings - Fork 1
/
statsAndOffsets.h
142 lines (123 loc) · 3.15 KB
/
statsAndOffsets.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once
#ifndef STATS_AND_OFFSETS_LOOK_UP_TABLES_V_C_MOHAN
#define STATS_AND_OFFSETS_LOOK_UP_TABLES_V_C_MOHAN
//------------------------------------------------------------
//Look up tables for rectangular and circular grids
//Variance, average of grid
//-----------------------------------------------------------------
int makeLinearLUT(int* offsets, int pitch, int xcoord, int ycoord );
int makeRectGridLUT(int* offsets, int pitch, int xgrid, int ygrid = 0, int coffset = 0);
int makeCircularLUT(int* offsets, int pitch, int rad, int coffset = 0);
template <typename finc>
float getMeanValue(const finc* sp, int* offsets, int noff);
template <typename finc>
float getVariance(finc* sp, int* Offsets, int noff, float avg);
//---------------------------------------------------------------------
int makeLinearLUT(int* offsets, int pitch, int xcoord, int ycoord)
{
int count = 0, npoints = 0;
int absx = abs(xcoord), absy = abs(ycoord);
if (absx < absy)
{
npoints = 2 * absy + 1;
if (xcoord == 0)
{
// xcoord = 0. So avoid divide by zero
for (int i = 0; i < npoints; i++)
{
offsets[i] = (i - npoints / 2) * pitch;
count++;
}
}
else
{
for (int i = 0; i < npoints; i++)
{
offsets[i] = (absy / ycoord) * (i - npoints / 2) * pitch
+ absx / xcoord * (((i - npoints / 2) * absx
+ (absy / 2)) / absy);
count++;
}
}
}
else
{
npoints = 2 * absx + 1;
if (ycoord == 0) // avoid div by zero
{
for (int i = 0; i < npoints; i++)
{
offsets[i] = (i - npoints / 2);
count++;
}
}
else
{
for (int i = 0; i < npoints; i++)
{
offsets[i] = (absx / xcoord) * (i - npoints / 2)
+ (absy / ycoord) * pitch * (((i - npoints / 2) * absy
+ (absx / 2)) / absx);
count++;
}
}
}
return count;
}
//definitions
// makes offsets table from left top corner of grid and adds coffset to bring them to any point required
int makeRectGridLUT(int* offsets, int pitch, int xgrid, int ygrid, int coffset)
{
if (ygrid == 0)
ygrid = xgrid;
int i = 0;
for (int h = 0; h < ygrid; h++)
{
for (int w = 0; w < xgrid; w++)
{
offsets[i] = h * pitch + w + coffset;
i++;
}
}
return i;
}
// makes offset table from center of circle and adds required offset to move ref to any point
int makeCircularLUT(int* cOff, int pitch, int rad, int coffset)
{
int count = 0;
int rsq = rad * rad;
for (int h = -rad; h <= rad; h++)
{
int hsq = h * h;
for (int w = -rad; w <= rad; w++)
{
if (hsq + (w * w) <= rsq)
{
cOff[count] = h * pitch + w + coffset;
count++;
}
}
}
return count;
}
// get mean value of a grid/ disc given offsets LUT pointer and number offsets
template <typename finc>
float getMeanValue(const finc* sp, int* offsets, int noff)
{
float sum = 0;
for (int i = 0; i < noff; i++)
sum += (float)sp[offsets[i]];
return (sum / noff);
}
// gives variance of given grid/ area with offsetsLUT, n offsets and average of grid / area
template <typename finc>
float getVariance(finc* sp, int* offsets, int noff, float avg)
{
float sum = 0.0f;
for (int i = 0; i < noff; i++)
{
sum += (avg - sp[offsets[i]]) * (avg - sp[offsets[i]]);
}
return sum / noff;
}
#endif