summaryrefslogtreecommitdiffstats
path: root/include/astra/AstraObjectManager.h
blob: 7a9cb91d09462f3a0ed0c659ef0fcb7f7746edf2 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
-----------------------------------------------------------------------
Copyright: 2010-2021, imec Vision Lab, University of Antwerp
           2014-2021, CWI, Amsterdam

Contact: astra@astra-toolbox.com
Website: http://www.astra-toolbox.com/

This file is part of the 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/>.

-----------------------------------------------------------------------
*/

#ifndef _INC_ASTRA_ASTRAOBJECTMANAGER
#define _INC_ASTRA_ASTRAOBJECTMANAGER

#include <map>
#include <sstream>

#include "Globals.h"
#include "Singleton.h"
#include "Projector2D.h"
#include "Projector3D.h"
#include "Float32Data2D.h"
#include "Float32Data3D.h"
#include "SparseMatrix.h"
#include "Algorithm.h"

namespace astra {

/**
 * This class contains functionality to store objects.  A unique index handle
 * will be assigned to each data object by which it can be accessed in the
 * future.  Indices are always >= 1.
 *
 * We store them in a special common base class to make indices unique
 * among all ObjectManagers.
 */

class CAstraObjectManagerBase {
public:
	virtual std::string getInfo(int index) const =0;
	virtual void remove(int index) =0;
	virtual std::string getType() const =0;
};


class _AstraExport CAstraIndexManager : public Singleton<CAstraIndexManager> {
public:
	CAstraIndexManager() : m_iLastIndex(0) { }

	int store(CAstraObjectManagerBase* m) {
		m_table[++m_iLastIndex] = m;
		return m_iLastIndex;
	}

	CAstraObjectManagerBase* get(int index) const {
		std::map<int, CAstraObjectManagerBase*>::const_iterator i;
		i = m_table.find(index);
		if (i != m_table.end())
			return i->second;
		else
			return 0;
	}

	void remove(int index) {
		std::map<int, CAstraObjectManagerBase*>::iterator i;
		i = m_table.find(index);
		if (i != m_table.end())
			m_table.erase(i);
	}

private:
	/** The index last handed out
	 */
	int m_iLastIndex;
	std::map<int, CAstraObjectManagerBase*> m_table;
};


template <typename T>
class CAstraObjectManager : public CAstraObjectManagerBase {

public:

	/** Default constructor.
	 */
	CAstraObjectManager();

	/** Destructor.  
	 */
	~CAstraObjectManager();

	/** Store the object in the manager and assign a unique index handle to it.
	 *
	 * @param _pObject A pointer to the object that should be stored.
	 * @return The index of the stored data object.  If the index in negative, an error occurred 
	 * and the object was NOT stored.
	 */
	int store(T* _pObject);

	/** Does the manager contain an object with the index _iIndex?
	 *
	 * @param _iIndex Index handle to the data object in question.
	 * @return True if the manager contains an object with the index handle _iIndex.
	 */
	bool hasIndex(int _iIndex) const;

	/** Fetch the object to which _iIndex refers to.
	 *
	 * @param _iIndex Index handle to the data object in question.
	 * @return Pointer to the stored data object.  A null pointer is returned if no object with index _iIndex is found.
	 */
	T* get(int _iIndex) const;

	/** Delete an object that was previously stored.  This actually DELETES the objecy.  Therefore, after this 
	* function call, the object in question will have passed on. It will be no more. It will have ceased 
	* to be.  It will be expired and will go to meet its maker.  Bereft of life, it will rest in peace.  
	* It will be an EX-OBJECT.
	*
	* @param _iIndex Index handle to the object in question.
	* @return Error code. 0 for success.
	*/
	void remove(int _iIndex);

	/** Get the index of the object, zero if it doesn't exist.
	 *
	 * @param _pObject The data object.
	 * @return Index of the stored object, 0 if not found.
	 */
	int getIndex(const T* _pObject) const;

	/** Clear all data.  This will also delete all the content of each object.
	 */
	void clear();

	/** Get info of object.
	 */
	std::string getInfo(int index) const;

	/** Get list with info of all managed objects.
	 */
	std::string info();

protected:

	/** Map each data object to a unique index.
	 */
	std::map<int, T*> m_mIndexToObject;

};

//----------------------------------------------------------------------------------------
// Constructor
template <typename T>
CAstraObjectManager<T>::CAstraObjectManager()
{
}

//----------------------------------------------------------------------------------------
// Destructor
template <typename T>
CAstraObjectManager<T>::~CAstraObjectManager()
{

}

//----------------------------------------------------------------------------------------
// store data
template <typename T>
int CAstraObjectManager<T>::store(T* _pDataObject) 
{
	int iIndex = CAstraIndexManager::getSingleton().store(this);
	m_mIndexToObject[iIndex] = _pDataObject;
	return iIndex;
}

//----------------------------------------------------------------------------------------
// has data?
template <typename T>
bool CAstraObjectManager<T>::hasIndex(int _iIndex) const
{
	typename std::map<int,T*>::const_iterator it = m_mIndexToObject.find(_iIndex);
	return it != m_mIndexToObject.end();
}

//----------------------------------------------------------------------------------------
// get data
template <typename T>
T* CAstraObjectManager<T>::get(int _iIndex) const
{
	typename std::map<int,T*>::const_iterator it = m_mIndexToObject.find(_iIndex);
	if (it != m_mIndexToObject.end())
		return it->second;
	else
		return 0;
}

//----------------------------------------------------------------------------------------
// delete data
template <typename T>
void CAstraObjectManager<T>::remove(int _iIndex)
{
	// find data
	typename std::map<int,T*>::iterator it = m_mIndexToObject.find(_iIndex);
	if (it == m_mIndexToObject.end())
		return;
	// delete data
	delete (*it).second;
	// delete from map
	m_mIndexToObject.erase(it);

	CAstraIndexManager::getSingleton().remove(_iIndex);
}

//----------------------------------------------------------------------------------------
// Get Index
template <typename T>
int CAstraObjectManager<T>::getIndex(const T* _pObject) const
{
	for (typename std::map<int,T*>::const_iterator it = m_mIndexToObject.begin(); it != m_mIndexToObject.end(); it++) {
		if ((*it).second == _pObject) return (*it).first;
	}
	return 0;
}


//----------------------------------------------------------------------------------------
// clear
template <typename T>
void CAstraObjectManager<T>::clear()
{
	for (typename std::map<int,T*>::iterator it = m_mIndexToObject.begin(); it != m_mIndexToObject.end(); it++) {
		// delete data
		delete (*it).second;
		(*it).second = 0;
	}

	m_mIndexToObject.clear();
}

//----------------------------------------------------------------------------------------
// Print info to string
template <typename T>
std::string CAstraObjectManager<T>::getInfo(int index) const {
	typename std::map<int,T*>::const_iterator it = m_mIndexToObject.find(index);
	if (it == m_mIndexToObject.end())
		return "";
	const T* pObject = it->second;
	std::stringstream res;
	res << index << " \t";
	if (pObject->isInitialized()) {
		res << "v     ";
	} else {
		res << "x     ";
	}
	res << pObject->description();
	return res.str();
}

template <typename T>
std::string CAstraObjectManager<T>::info() {
	std::stringstream res;
	res << "id  init  description" << std::endl;
	res << "-----------------------------------------" << std::endl;
	for (typename std::map<int,T*>::const_iterator it = m_mIndexToObject.begin(); it != m_mIndexToObject.end(); it++) {
		res << getInfo(it->first) << std::endl;
	}
	res << "-----------------------------------------" << std::endl;
	return res.str();
}



//----------------------------------------------------------------------------------------
// Create the necessary Object Managers
/**
 * This class contains functionality to store 2D projector objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CProjector2DManager : public Singleton<CProjector2DManager>, public CAstraObjectManager<CProjector2D>
{
	virtual std::string getType() const { return "projector2d"; }
};

/**
 * This class contains functionality to store 3D projector objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CProjector3DManager : public Singleton<CProjector3DManager>, public CAstraObjectManager<CProjector3D>
{
	virtual std::string getType() const { return "projector3d"; }
};

/**
 * This class contains functionality to store 2D data objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CData2DManager : public Singleton<CData2DManager>, public CAstraObjectManager<CFloat32Data2D>
{
	virtual std::string getType() const { return "data2d"; }
};

/**
 * This class contains functionality to store 3D data objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CData3DManager : public Singleton<CData3DManager>, public CAstraObjectManager<CFloat32Data3D>
{
	virtual std::string getType() const { return "data3d"; }
};

/**
 * This class contains functionality to store algorithm objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CAlgorithmManager : public Singleton<CAlgorithmManager>, public CAstraObjectManager<CAlgorithm>
{
	virtual std::string getType() const { return "algorithm"; }
};

/**
 * This class contains functionality to store matrix objects.  A unique index handle will be 
 * assigned to each data object by which it can be accessed in the future.
 * Indices are always >= 1.
 */
class _AstraExport CMatrixManager : public Singleton<CMatrixManager>, public CAstraObjectManager<CSparseMatrix>
{
	virtual std::string getType() const { return "matrix"; }
};


} // end namespace

#endif