summaryrefslogtreecommitdiffstats
path: root/include/astra/Globals.h
blob: 9407ef976118fbe9acffa2bc8292d61615da8274 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
-----------------------------------------------------------------------
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$
*/

#ifndef _INC_ASTRA_GLOBALS
#define _INC_ASTRA_GLOBALS

/*! \mainpage The ASTRA-toolbox 
 *
 * <img src="../images/logo_big.png"/>
 */


//----------------------------------------------------------------------------------------

#ifdef _MSC_VER

// disable warning: 'fopen' was declared deprecated
#pragma warning (disable : 4996)
// disable warning: C++ exception handler used, but unwind semantics are not enables
#pragma warning (disable : 4530)
// disable warning: no suitable definition provided for explicit template instantiation request
#pragma warning (disable : 4661)

#endif

//----------------------------------------------------------------------------------------
// standard includes
#include <cassert>
#include <iostream>
#include <fstream>
#include <math.h>
#include <boost/static_assert.hpp>
#include <boost/throw_exception.hpp>

//----------------------------------------------------------------------------------------
// macro's

#define ASTRA_TOOLBOXVERSION_MAJOR 1
#define ASTRA_TOOLBOXVERSION_MINOR 1
#define ASTRA_TOOLBOXVERSION ((ASTRA_TOOLBOXVERSION_MAJOR)*100 + (ASTRA_TOOLBOXVERSION_MINOR))
#define ASTRA_TOOLBOXVERSION_STRING "1.1"


#define ASTRA_ASSERT(a) assert(a)

#define ASTRA_CONFIG_CHECK(value, type, msg) if (!(value)) { cout << "Configuration Error in " << type << ": " << msg << endl; return false; }

#define ASTRA_CONFIG_WARNING(type, msg) { cout << "Warning in " << type << ": " << msg << endl; }


#define ASTRA_DELETE(a) if (a) { delete a; a = NULL; }
#define ASTRA_DELETE_ARRAY(a) if (a) { delete[] a; a = NULL; }

#ifdef _MSC_VER

#ifdef DLL_EXPORTS
#define _AstraExport __declspec(dllexport)
#define EXPIMP_TEMPLATE
#else
#define _AstraExport __declspec(dllimport)
#define EXPIMP_TEMPLATE extern
#endif

#else

#define _AstraExport

#endif


//----------------------------------------------------------------------------------------
// typedefs
namespace astra {
	typedef float float32;
	typedef double float64;
	typedef unsigned short int uint16;
	typedef signed short int sint16;
	typedef unsigned char uchar8;
	typedef signed char schar8;

	typedef int int32;
	typedef short int int16;
}

//----------------------------------------------------------------------------------------
// globals vars & functions
//namespace astra {
//#define ToolboxVersion 0.1f;

//float32 getVersion() { return ToolboxVersion; }

//_AstraExport bool cudaEnabled() { 
//#ifdef ASTRA_CUDA	
//	return true; 
//#else
//	return false;
//#endif
//}
//}

//----------------------------------------------------------------------------------------
// errors
namespace astra {

typedef enum {ASTRA_SUCCESS, 
			  ASTRA_ERROR_NOT_INITIALIZED,
			  ASTRA_ERROR_INVALID_FILE,
			  ASTRA_ERROR_OUT_OF_RANGE,
			  ASTRA_ERROR_DIMENSION_MISMATCH,
			  ASTRA_ERROR_EXTERNAL_LIBRARY,
			  ASTRA_ERROR_ALLOCATION,
			  ASTRA_ERROR_NOT_IMPLEMENTED} AstraError;
}


//----------------------------------------------------------------------------------------
// variables
namespace astra {
	const float32 PI = 3.14159265358979323846264338328f;
	const float32 PI32 = 3.14159265358979323846264338328f;
	const float32 PIdiv2 = PI / 2;
	const float32 PIdiv4 = PI / 4;
	const float32 eps = 1e-7f;
}

//----------------------------------------------------------------------------------------
// math
namespace astra {

	inline float32 cos_73s(float32 x) 
	{ 
		/*
		const float32 c1 =  0.999999953464f;
		const float32 c2 = -0.4999999053455f;
		const float32 c3 =  0.0416635846769f;
		const float32 c4 = -0.0013853704264f;
		const float32 c5 =  0.000023233f;
		*/
		const float c1= (float)0.99940307;
		const float c2= (float)-0.49558072;
		const float c3= (float)0.03679168;

		float32 x2;
		x2 = x * x;
		//return (c1 + x2*(c2 + x2*(c3 + x2*(c4 + c5*x2))));
		return (c1 + x2*(c2 + c3 * x2));
	}

	inline float32 fast_cos(float32 x) 
	{
		int quad; 

		//x = fmod(x, 2*PI);		// Get rid of values > 2* pi
		if (x < 0) x =- x;		// cos(-x) = cos(x)
		quad = int(x/PIdiv2);	// Get quadrant # (0 to 3) 
		switch (quad) {
			case 0: return  cos_73s(x);
			case 1: return -cos_73s(PI-x);
			case 2: return -cos_73s(x-PI);
			case 3: return  cos_73s(2*PI-x);
		}
		return 0.0f;
	}

	inline float32 fast_sin(float32 x){
		return fast_cos(PIdiv2-x);
	}

}

//----------------------------------------------------------------------------------------
// structs
namespace astra {
	/**
	 * Struct for storing pixel weigths
	 **/
	struct SPixelWeight
	{
		int m_iIndex;
		float32 m_fWeight;
	};

	/**
	 * Struct combining some properties of a detector in 1D detector row
	 **/
	struct SDetector2D
	{
		int m_iIndex;
		int m_iAngleIndex;
		int m_iDetectorIndex;
	};
	
	/**
	 * Struct combining some properties of a detector in 2D detector array
	 **/
	struct SDetector3D
	{
		int m_iIndex;
		int m_iAngleIndex;
		int m_iDetectorIndex;
		int m_iSliceIndex;
	};
}
//----------------------------------------------------------------------------------------
// some toys

// safe reinterpret cast
template <class To, class From>
To safe_reinterpret_cast(From from)
{
	BOOST_STATIC_ASSERT(sizeof(From) <= sizeof(To));
	return reinterpret_cast<To>(from);
}

//----------------------------------------------------------------------------------------
// functions for testing
template<typename T>
inline void writeArray(T*** arr, int dim1, int dim2, int dim3, const std::string& filename)
{
	std::ofstream out(filename.c_str());
	int i1, i2, i3;
	for (i1 = 0; i1 < dim1; ++i1) {
		for (i2 = 0; i2 < dim2; ++i2) {
			for (i3 = 0; i3 < dim3; ++i3) {
				out << arr[i1][i2][i3] << " ";
			}
			out << std::endl;
		}
		out << std::endl;
	}
	out.close();
}

template<typename T>
inline void writeArray(T** arr, int dim1, int dim2, const std::string& filename)
{
	std::ofstream out(filename.c_str());
	for (int i1 = 0; i1 < dim1; i1++) {
		for (int i2 = 0; i2 < dim2; i2++) {
			out << arr[i1][i2] << " ";
		}
		out << std::endl;
	}
	out.close();
}

template<typename T>
inline void writeArray(T* arr, int dim1, const std::string& filename)
{
	std::ofstream out(filename.c_str());
	for (int i1 = 0; i1 < dim1; i1++) {
		out << arr[i1] << " ";
	}
	out.close();
}
namespace astra {
_AstraExport inline int getVersion() { return ASTRA_TOOLBOXVERSION; }
_AstraExport inline const char* getVersionString() { return ASTRA_TOOLBOXVERSION_STRING; }
#ifdef ASTRA_CUDA
_AstraExport inline bool cudaEnabled() { return true; }
#else
_AstraExport inline bool cudaEnabled() { return false; }
#endif
}
//----------------------------------------------------------------------------------------
// portability between MSVC and Linux/gcc

#ifndef _MSC_VER
#include "swrap.h"
#define EXPIMP_TEMPLATE

#if !defined(FORCEINLINE) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
#define FORCEINLINE inline __attribute__((__always_inline__))
#else
#define FORCEINLINE inline
#endif

#else

#define FORCEINLINE __forceinline

#endif

#endif