summaryrefslogtreecommitdiffstats
path: root/include/astra/ParallelBeamBlobKernelProjector2D.inl
blob: 70764b162bd180e93a8152398140ddd909acbff7 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
-----------------------------------------------------------------------
Copyright 2012 iMinds-Vision Lab, University of Antwerp

Contact: astra@ua.ac.be
Website: http://astra.ua.ac.be


This file is part of the
All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox").

The ASTRA Toolbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

The ASTRA Toolbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------
$Id$
*/



//----------------------------------------------------------------------------------------
// PROJECT ALL 
template <typename Policy>
void CParallelBeamBlobKernelProjector2D::project(Policy& p)
{
	for (int iAngle = 0; iAngle < m_pProjectionGeometry->getProjectionAngleCount(); ++iAngle) {
		for (int iDetector = 0; iDetector < m_pProjectionGeometry->getDetectorCount(); ++iDetector) {
			projectSingleRay(iAngle, iDetector, p);
		}
	}
}


//----------------------------------------------------------------------------------------
// PROJECT SINGLE PROJECTION
template <typename Policy>
void CParallelBeamBlobKernelProjector2D::projectSingleProjection(int _iProjection, Policy& p)
{
	for (int iDetector = 0; iDetector < m_pProjectionGeometry->getDetectorCount(); ++iDetector) {
		projectSingleRay(_iProjection, iDetector, p);
	}
}



//----------------------------------------------------------------------------------------
// PROJECT SINGLE RAY
template <typename Policy>
void CParallelBeamBlobKernelProjector2D::projectSingleRay(int _iProjection, int _iDetector, Policy& p)
{
	ASTRA_ASSERT(m_bIsInitialized);

	int iRayIndex = _iProjection * m_pProjectionGeometry->getDetectorCount() + _iDetector;

	// POLICY: RAY PRIOR
	if (!p.rayPrior(iRayIndex)) return;

	// get values
	float32 t = m_pProjectionGeometry->indexToDetectorOffset(_iDetector);
	float32 theta = m_pProjectionGeometry->getProjectionAngle(_iProjection);
	if (theta >= 7*PIdiv4) theta -= 2*PI;

	bool flip = false;

	if (theta >= 3*PIdiv4) {
		theta -= PI;
		t = -t;
		flip = true;
	}


	if (theta <= PIdiv4) { // -pi/4 <= theta <= pi/4

		// precalculate sin, cos, 1/cos
		float32 sin_theta = sin(theta);
		float32 cos_theta = cos(theta);
		float32 inv_cos_theta = 1.0f / cos_theta; 

		// precalculate other stuff
		float32 lengthPerRow = m_pVolumeGeometry->getPixelLengthY() * inv_cos_theta;
		float32 updatePerRow = sin_theta * lengthPerRow;
		float32 inv_pixelLengthX = 1.0f / m_pVolumeGeometry->getPixelLengthX();
		float32 pixelLengthX_over_blobSize = m_pVolumeGeometry->getPixelLengthX() / m_fBlobSize;
		
		// some variables
		int row, col, xmin, xmax;
		float32 P, x, d;

		// calculate P and x for row 0
		P = (t - sin_theta * m_pVolumeGeometry->pixelRowToCenterY(0)) * inv_cos_theta;
		x = (P - m_pVolumeGeometry->getWindowMinX()) * inv_pixelLengthX - 0.5f;

		// for each row
		for (row = 0; row < m_pVolumeGeometry->getGridRowCount(); ++row) {
			
			// calculate extent
			xmin = (int)ceil((P - m_fBlobSize - m_pVolumeGeometry->getWindowMinX()) * inv_pixelLengthX - 0.5f);
			xmax = (int)floor((P + m_fBlobSize - m_pVolumeGeometry->getWindowMinX()) * inv_pixelLengthX - 0.5f);
	
			// add pixels
			for (col = xmin; col <= xmax; col++) {
				if (col >= 0 && col < m_pVolumeGeometry->getGridColCount()) {
					//d = abs(x - col) * pixelLengthX_over_blobSize;
					//index = (int)(d*m_iBlobSampleCount+0.5f);
					//float32 fWeight = m_pfBlobValues[min(index,m_iBlobSampleCount-1)] * lengthPerRow;

					float32 fWeight;
					int index;
					if ((x >= col) ^ flip) {
						d = abs(x - col) * pixelLengthX_over_blobSize * cos_theta;
						index = (int)(d*m_iBlobSampleCount+0.5f);
						fWeight = m_pfBlobValues[min(index,m_iBlobSampleCount-1)];
					} else {
						d = abs(x - col) * pixelLengthX_over_blobSize * cos_theta;
						index = (int)(d*m_iBlobSampleCount+0.5f);
						fWeight = m_pfBlobValuesNeg[min(index,m_iBlobSampleCount-1)];
					}

					int iVolumeIndex = m_pVolumeGeometry->pixelRowColToIndex(row, col);
					// POLICY: PIXEL PRIOR + ADD + POSTERIOR
					if (p.pixelPrior(iVolumeIndex)) {
						p.addWeight(iRayIndex, iVolumeIndex, fWeight);
						p.pixelPosterior(iVolumeIndex);
					}
				}
			}

			// update P and x
			P += updatePerRow;
			x += updatePerRow * inv_pixelLengthX;
		}

	} else { // pi/4 < theta < 3pi/4

		// precalculate sin cos
		float32 sin_90_theta = sin(PIdiv2-theta);
		float32 cos_90_theta = cos(PIdiv2-theta);
		float32 inv_cos_90_theta = 1.0f / cos_90_theta; 

		// precalculate other stuff
		float32 lengthPerCol = m_pVolumeGeometry->getPixelLengthX() * inv_cos_90_theta;
		float32 updatePerCol = sin_90_theta * lengthPerCol;
		float32 inv_pixelLengthY = 1.0f / m_pVolumeGeometry->getPixelLengthY();
		float32 pixelLengthY_over_blobSize = m_pVolumeGeometry->getPixelLengthY() / m_fBlobSize;

		// some variables
		int row, col, xmin, xmax;
		float32 P,x, d;

		// calculate P and x for col 0
		P = (sin_90_theta * m_pVolumeGeometry->pixelColToCenterX(0) - t) * inv_cos_90_theta;
		x = (P - m_pVolumeGeometry->getWindowMinY()) * inv_pixelLengthY - 0.5f;

		// for each col
		for (col = 0; col < m_pVolumeGeometry->getGridColCount(); ++col) {

			// calculate extent
			xmin = (int)ceil((P - m_fBlobSize - m_pVolumeGeometry->getWindowMinY()) * inv_pixelLengthY - 0.5f);
			xmax = (int)floor((P + m_fBlobSize - m_pVolumeGeometry->getWindowMinY()) * inv_pixelLengthY - 0.5f);

			// add pixels
			for (row = xmin; row <= xmax; row++) {
				if (row >= 0 && row < m_pVolumeGeometry->getGridRowCount()) {
					//d = abs(x - row) * pixelLengthY_over_blobSize;
					//int index = (int)(d*m_iBlobSampleCount+0.5f);
					//float32 fWeight = m_pfBlobValues[min(index,m_iBlobSampleCount-1)] * lengthPerCol;

					float32 fWeight;
					int index;
					if ((x <= row) ^ flip) {
						d = abs(x - row) * pixelLengthY_over_blobSize * cos_90_theta;
						index = (int)(d*m_iBlobSampleCount+0.5f);
						fWeight = m_pfBlobValues[min(index,m_iBlobSampleCount-1)];
					} else {
						d = abs(x - row) * pixelLengthY_over_blobSize * cos_90_theta;
						index = (int)(d*m_iBlobSampleCount+0.5f);
						fWeight = m_pfBlobValuesNeg[min(index,m_iBlobSampleCount-1)];
					}


					int iVolumeIndex = m_pVolumeGeometry->pixelRowColToIndex(row, col);
					// POLICY: PIXEL PRIOR + ADD + POSTERIOR
					if (p.pixelPrior(iVolumeIndex)) {
						p.addWeight(iRayIndex, iVolumeIndex, fWeight);
						p.pixelPosterior(iVolumeIndex);
					}
				}
			}

			// update P and x
			P += updatePerCol;
			x += updatePerCol * inv_pixelLengthY;
		}

	}

	// POLICY: RAY POSTERIOR
	p.rayPosterior(iRayIndex);



}