From bc2e4018054f494fcba01e6a27a63e151bf1e9a4 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 16 Feb 2016 15:15:13 +0100 Subject: Refactor AstraObjectManager to add an AstraIndexManager The new AstraIndexManager can be used to obtain information about objects without knowing their type. --- include/astra/Algorithm.h | 4 +- include/astra/AstraObjectManager.h | 104 ++++++++++++++++++++++++++++--------- include/astra/Projector2D.h | 4 +- include/astra/Projector3D.h | 4 +- src/AstraObjectManager.cpp | 16 +++--- 5 files changed, 94 insertions(+), 38 deletions(-) diff --git a/include/astra/Algorithm.h b/include/astra/Algorithm.h index 049417c..18c465f 100644 --- a/include/astra/Algorithm.h +++ b/include/astra/Algorithm.h @@ -81,7 +81,7 @@ public: * * @return initialized */ - bool isInitialized(); + bool isInitialized() const; /** get a description of the class * @@ -133,7 +133,7 @@ private: // inline functions inline std::string CAlgorithm::description() const { return "Algorithm"; }; -inline bool CAlgorithm::isInitialized() { return m_bIsInitialized; } +inline bool CAlgorithm::isInitialized() const { return m_bIsInitialized; } } // end namespace diff --git a/include/astra/AstraObjectManager.h b/include/astra/AstraObjectManager.h index 895f955..eab3f03 100644 --- a/include/astra/AstraObjectManager.h +++ b/include/astra/AstraObjectManager.h @@ -52,17 +52,41 @@ namespace astra { * among all ObjectManagers. */ +class CAstraObjectManagerBase { + virtual std::string getInfo(int index) const =0; + virtual void remove(int index) =0; + virtual std::string getType() const =0; +}; -class CAstraIndexManager { -protected: - /** The index of the previously stored data object. + +class CAstraIndexManager : public Singleton { +public: + CAstraIndexManager() : m_iLastIndex(0) { } + + int store(CAstraObjectManagerBase* m) { + m_table[++m_iLastIndex] = m; + return m_iLastIndex; + } + + CAstraObjectManagerBase* get(int index) const { + std::map::const_iterator i; + i = m_table.find(index); + if (i != m_table.end()) + return i->second; + else + return 0; + } + +private: + /** The index last handed out */ - static int m_iPreviousIndex; + int m_iLastIndex; + std::map m_table; }; template -class CAstraObjectManager : public Singleton >, CAstraIndexManager { +class CAstraObjectManager : public CAstraObjectManagerBase { public: @@ -117,7 +141,11 @@ public: */ void clear(); - /** Get info. + /** Get info of object. + */ + std::string getInfo(int index) const; + + /** Get list with info of all managed objects. */ std::string info(); @@ -149,9 +177,9 @@ CAstraObjectManager::~CAstraObjectManager() template int CAstraObjectManager::store(T* _pDataObject) { - m_iPreviousIndex++; - m_mIndexToObject[m_iPreviousIndex] = _pDataObject; - return m_iPreviousIndex; + int iIndex = CAstraIndexManager::getSingleton().store(this); + m_mIndexToObject[iIndex] = _pDataObject; + return iIndex; } //---------------------------------------------------------------------------------------- @@ -219,20 +247,30 @@ void CAstraObjectManager::clear() //---------------------------------------------------------------------------------------- // Print info to string +template +std::string CAstraObjectManager::getInfo(int index) const { + typename map::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 std::string CAstraObjectManager::info() { std::stringstream res; res << "id init description" << std::endl; res << "-----------------------------------------" << std::endl; - for (typename map::iterator it = m_mIndexToObject.begin(); it != m_mIndexToObject.end(); it++) { - res << (*it).first << " \t"; - T* pObject = m_mIndexToObject[(*it).first]; - if (pObject->isInitialized()) { - res << "v "; - } else { - res << "x "; - } - res << pObject->description() << endl; + for (typename map::const_iterator it = m_mIndexToObject.begin(); it != m_mIndexToObject.end(); it++) { + res << getInfo(it->first) << endl; } res << "-----------------------------------------" << std::endl; return res.str(); @@ -247,42 +285,60 @@ std::string CAstraObjectManager::info() { * assigned to each data object by which it can be accessed in the future. * Indices are always >= 1. */ -class _AstraExport CProjector2DManager : public CAstraObjectManager{}; +class _AstraExport CProjector2DManager : public Singleton, public CAstraObjectManager +{ + 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 CAstraObjectManager{}; +class _AstraExport CProjector3DManager : public Singleton, public CAstraObjectManager +{ + 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 CAstraObjectManager{}; +class _AstraExport CData2DManager : public Singleton, public CAstraObjectManager +{ + 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 CAstraObjectManager{}; +class _AstraExport CData3DManager : public Singleton, public CAstraObjectManager +{ + 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 CAstraObjectManager{}; +class _AstraExport CAlgorithmManager : public Singleton, public CAstraObjectManager +{ + 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 CAstraObjectManager{}; +class _AstraExport CMatrixManager : public Singleton, public CAstraObjectManager +{ + virtual std::string getType() const { return "matrix"; } +}; } // end namespace diff --git a/include/astra/Projector2D.h b/include/astra/Projector2D.h index a1ea0ce..c7a899d 100644 --- a/include/astra/Projector2D.h +++ b/include/astra/Projector2D.h @@ -174,7 +174,7 @@ public: * * @return initialized successfully */ - bool isInitialized(); + bool isInitialized() const; /** get a description of the class * @@ -191,7 +191,7 @@ private: }; // inline functions -inline bool CProjector2D::isInitialized() { return m_bIsInitialized; } +inline bool CProjector2D::isInitialized() const { return m_bIsInitialized; } inline CProjectionGeometry2D* CProjector2D::getProjectionGeometry() { return m_pProjectionGeometry; } inline CVolumeGeometry2D* CProjector2D::getVolumeGeometry() { return m_pVolumeGeometry; } diff --git a/include/astra/Projector3D.h b/include/astra/Projector3D.h index 1ef1ba7..88ca2be 100644 --- a/include/astra/Projector3D.h +++ b/include/astra/Projector3D.h @@ -153,7 +153,7 @@ public: * * @return initialized successfully */ - bool isInitialized(); + bool isInitialized() const; /** get a description of the class * @@ -174,7 +174,7 @@ private: }; // inline functions -inline bool CProjector3D::isInitialized() { return m_bIsInitialized; } +inline bool CProjector3D::isInitialized() const { return m_bIsInitialized; } inline CProjectionGeometry3D* CProjector3D::getProjectionGeometry() { return m_pProjectionGeometry; } inline CVolumeGeometry3D* CProjector3D::getVolumeGeometry() { return m_pVolumeGeometry; } diff --git a/src/AstraObjectManager.cpp b/src/AstraObjectManager.cpp index c49f273..46eae4b 100644 --- a/src/AstraObjectManager.cpp +++ b/src/AstraObjectManager.cpp @@ -31,13 +31,13 @@ $Id$ namespace astra { -int CAstraIndexManager::m_iPreviousIndex = 0; - -DEFINE_SINGLETON(CAstraObjectManager); -DEFINE_SINGLETON(CAstraObjectManager); -DEFINE_SINGLETON(CAstraObjectManager); -DEFINE_SINGLETON(CAstraObjectManager); -DEFINE_SINGLETON(CAstraObjectManager); -DEFINE_SINGLETON(CAstraObjectManager); +DEFINE_SINGLETON(CProjector2DManager); +DEFINE_SINGLETON(CProjector3DManager); +DEFINE_SINGLETON(CData2DManager); +DEFINE_SINGLETON(CData3DManager); +DEFINE_SINGLETON(CAlgorithmManager); +DEFINE_SINGLETON(CMatrixManager); + +DEFINE_SINGLETON(CAstraIndexManager); } // end namespace -- cgit v1.2.3 From 9847d2c4a8287f220f169236b4fa1962d50d1187 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 16 Feb 2016 15:22:59 +0100 Subject: Also remove objects from index manager --- include/astra/AstraObjectManager.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/astra/AstraObjectManager.h b/include/astra/AstraObjectManager.h index eab3f03..8b9da30 100644 --- a/include/astra/AstraObjectManager.h +++ b/include/astra/AstraObjectManager.h @@ -77,6 +77,13 @@ public: return 0; } + void remove(int index) { + std::map::iterator i; + i = m_table.find(index); + if (i != m_table.end()) + m_table.erase(i); + } + private: /** The index last handed out */ @@ -216,7 +223,9 @@ void CAstraObjectManager::remove(int _iIndex) // delete data delete (*it).second; // delete from map - m_mIndexToObject.erase(it); + m_mIndexToObject.erase(it); + + CAstraIndexManager::getSingleton().remove(_iIndex); } //---------------------------------------------------------------------------------------- -- cgit v1.2.3 From 8cfe13a410a051e5a6b9835e3a4cd6c808cf5d29 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 16 Feb 2016 15:34:08 +0100 Subject: Add astra_mex delete/info based on index manager --- include/astra/AstraObjectManager.h | 1 + matlab/mex/astra_mex_c.cpp | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/astra/AstraObjectManager.h b/include/astra/AstraObjectManager.h index 8b9da30..35b4534 100644 --- a/include/astra/AstraObjectManager.h +++ b/include/astra/AstraObjectManager.h @@ -53,6 +53,7 @@ namespace astra { */ class CAstraObjectManagerBase { +public: virtual std::string getInfo(int index) const =0; virtual void remove(int index) =0; virtual std::string getType() const =0; diff --git a/matlab/mex/astra_mex_c.cpp b/matlab/mex/astra_mex_c.cpp index fdf4f33..a8623be 100644 --- a/matlab/mex/astra_mex_c.cpp +++ b/matlab/mex/astra_mex_c.cpp @@ -36,10 +36,14 @@ $Id$ #include "mexInitFunctions.h" #include "astra/Globals.h" +#include "astra/AstraObjectManager.h" + #ifdef ASTRA_CUDA #include "../cuda/2d/darthelper.h" #include "astra/CompositeGeometryManager.h" #endif + + using namespace std; using namespace astra; @@ -142,6 +146,47 @@ void astra_mex_version(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[ } } +//----------------------------------------------------------------------------------------- + +void astra_mex_info(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) +{ + if (nrhs < 2) { + mexErrMsgTxt("Usage: astra_mex('info', index/indices);\n"); + return; + } + + for (int i = 1; i < nrhs; i++) { + int iDataID = (int)(mxGetScalar(prhs[i])); + CAstraObjectManagerBase *ptr; + ptr = CAstraIndexManager::getSingleton().get(iDataID); + if (ptr) { + mexPrintf("%s\t%s\n", ptr->getType().c_str(), ptr->getInfo(iDataID).c_str()); + } + } + +} + +//----------------------------------------------------------------------------------------- + +void astra_mex_delete(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) +{ + if (nrhs < 2) { + mexErrMsgTxt("Usage: astra_mex('delete', index/indices);\n"); + return; + } + + for (int i = 1; i < nrhs; i++) { + int iDataID = (int)(mxGetScalar(prhs[i])); + CAstraObjectManagerBase *ptr; + ptr = CAstraIndexManager::getSingleton().get(iDataID); + if (ptr) + ptr->remove(iDataID); + } + +} + + + //----------------------------------------------------------------------------------------- static void printHelp() @@ -178,6 +223,10 @@ void mexFunction(int nlhs, mxArray* plhs[], astra_mex_credits(nlhs, plhs, nrhs, prhs); } else if (sMode == std::string("set_gpu_index")) { astra_mex_set_gpu_index(nlhs, plhs, nrhs, prhs); + } else if (sMode == std::string("info")) { + astra_mex_info(nlhs, plhs, nrhs, prhs); + } else if (sMode == std::string("delete")) { + astra_mex_delete(nlhs, plhs, nrhs, prhs); } else { printHelp(); } -- cgit v1.2.3 From 7b6d74bd403f5bb0e4c8ba451eefa13193e7ed34 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 16 Feb 2016 16:19:26 +0100 Subject: Slightly simplify CAstraObjectManager::remove --- include/astra/AstraObjectManager.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/astra/AstraObjectManager.h b/include/astra/AstraObjectManager.h index 35b4534..ad89c2a 100644 --- a/include/astra/AstraObjectManager.h +++ b/include/astra/AstraObjectManager.h @@ -216,11 +216,10 @@ T* CAstraObjectManager::get(int _iIndex) const template void CAstraObjectManager::remove(int _iIndex) { - if (!hasIndex(_iIndex)) { - return; - } // find data typename map::iterator it = m_mIndexToObject.find(_iIndex); + if (it == m_mIndexToObject.end()) + return; // delete data delete (*it).second; // delete from map -- cgit v1.2.3 From 0884a3928395dd368aea61eeef768677018f9095 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Wed, 17 Feb 2016 16:18:23 +0100 Subject: Add python astra.astra delete/info based on index manager --- python/astra/PyIndexManager.pxd | 40 ++++++++++++++++++++++++++++++++++++++++ python/astra/astra.py | 20 ++++++++++++++++++++ python/astra/astra_c.pyx | 30 ++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 python/astra/PyIndexManager.pxd diff --git a/python/astra/PyIndexManager.pxd b/python/astra/PyIndexManager.pxd new file mode 100644 index 0000000..c1ad502 --- /dev/null +++ b/python/astra/PyIndexManager.pxd @@ -0,0 +1,40 @@ +# ----------------------------------------------------------------------- +# Copyright: 2010-2016, iMinds-Vision Lab, University of Antwerp +# 2013-2016, CWI, Amsterdam +# +# Contact: astra@uantwerpen.be +# Website: http://sf.net/projects/astra-toolbox +# +# 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 . +# +# ----------------------------------------------------------------------- + +from libcpp.string cimport string + +from .PyIncludes cimport * + +cdef extern from "astra/AstraObjectManager.h" namespace "astra": + cdef cppclass CAstraObjectManagerBase: + string getInfo(int) + void remove(int) + string getType() + cdef cppclass CAstraIndexManager: + CAstraObjectManagerBase* get(int) + +cdef extern from "astra/AstraObjectManager.h" namespace "astra::CAstraIndexManager": + cdef CAstraIndexManager* getSingletonPtr() + diff --git a/python/astra/astra.py b/python/astra/astra.py index 9328b6b..61c26ee 100644 --- a/python/astra/astra.py +++ b/python/astra/astra.py @@ -56,3 +56,23 @@ def set_gpu_index(idx, memory=0): :type idx: :class:`int` """ a.set_gpu_index(idx, memory) + +def delete(ids): + """Delete an astra object. + + :param ids: ID or list of ID's to delete. + :type ids: :class:`int` or :class:`list` + + """ + return a.delete(ids) + +def info(ids): + """Print info about an astra object. + + :param ids: ID or list of ID's to show. + :type ids: :class:`int` or :class:`list` + + """ + return a.info(ids) + + diff --git a/python/astra/astra_c.pyx b/python/astra/astra_c.pyx index 2a9c816..c70bb8e 100644 --- a/python/astra/astra_c.pyx +++ b/python/astra/astra_c.pyx @@ -33,6 +33,9 @@ from .utils import wrap_from_bytes from libcpp.string cimport string from libcpp.vector cimport vector from libcpp cimport bool +cimport PyIndexManager +from .PyIndexManager cimport CAstraObjectManagerBase + cdef extern from "astra/Globals.h" namespace "astra": int getVersion() string getVersionString() @@ -51,6 +54,7 @@ cdef extern from "astra/CompositeGeometryManager.h" namespace "astra": cdef extern from "astra/CompositeGeometryManager.h" namespace "astra::CCompositeGeometryManager": void setGlobalGPUParams(SGPUParams&) + def credits(): six.print_("""The ASTRA Toolbox has been developed at the University of Antwerp and CWI, Amsterdam by * Prof. dr. Joost Batenburg @@ -78,11 +82,10 @@ def version(printToScreen=False): return getVersion() def set_gpu_index(idx, memory=0): - import types import collections cdef SGPUParams params if use_cuda()==True: - if not isinstance(idx, collections.Iterable) or isinstance(idx, types.StringTypes): + if not isinstance(idx, collections.Iterable) or isinstance(idx, six.string_types + (six.text_type,six.binary_type)): idx = (idx,) params.memory = memory params.GPUIndices = idx @@ -90,3 +93,26 @@ def set_gpu_index(idx, memory=0): ret = setGPUIndex(params.GPUIndices[0]) if not ret: six.print_("Failed to set GPU " + str(params.GPUIndices[0])) + +def delete(ids): + import collections + cdef CAstraObjectManagerBase* ptr + if not isinstance(ids, collections.Iterable) or isinstance(ids, six.string_types + (six.text_type,six.binary_type)): + ids = (ids,) + for i in ids: + ptr = PyIndexManager.getSingletonPtr().get(i) + if ptr: + ptr.remove(i) + +def info(ids): + import collections + cdef CAstraObjectManagerBase* ptr + if not isinstance(ids, collections.Iterable) or isinstance(ids, six.string_types + (six.text_type,six.binary_type)): + ids = (ids,) + for i in ids: + ptr = PyIndexManager.getSingletonPtr().get(i) + if ptr: + s = ptr.getType() + six.b("\t") + ptr.getInfo(i) + six.print_(wrap_from_bytes(s)) + + -- cgit v1.2.3 From da11f9e7c7559eb01cc422ac55c9f7a1cd4bb803 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Feb 2016 12:17:53 +0100 Subject: Add missing help --- matlab/mex/astra_mex_c.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlab/mex/astra_mex_c.cpp b/matlab/mex/astra_mex_c.cpp index a8623be..f499528 100644 --- a/matlab/mex/astra_mex_c.cpp +++ b/matlab/mex/astra_mex_c.cpp @@ -192,7 +192,7 @@ void astra_mex_delete(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[] static void printHelp() { mexPrintf("Please specify a mode of operation.\n"); - mexPrintf(" Valid modes: version, use_cuda, credits\n"); + mexPrintf(" Valid modes: version, use_cuda, credits, set_gpu_index, info, delete\n"); } //----------------------------------------------------------------------------------------- -- cgit v1.2.3 From 7b8a508f0bc7a8a02766b15fa094dfd18c1b0525 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 1 Mar 2016 15:05:53 +0100 Subject: Fix build --- src/CompositeGeometryManager.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CompositeGeometryManager.cpp b/src/CompositeGeometryManager.cpp index c9cbaaa..084ba8c 100644 --- a/src/CompositeGeometryManager.cpp +++ b/src/CompositeGeometryManager.cpp @@ -45,7 +45,7 @@ along with the ASTRA Toolbox. If not, see . #include #include -#include +#include #ifndef USE_PTHREADS #include @@ -116,17 +116,17 @@ bool CCompositeGeometryManager::splitJobs(TJobSet &jobs, size_t maxSize, int div // c. create jobs for new (input,output) subparts TPartList splitOutput; - pOutput->splitZ(splitOutput, maxSize/3, SIZE_MAX, div); + pOutput->splitZ(splitOutput, maxSize/3, UINT_MAX, div); #if 0 TPartList splitOutput2; for (TPartList::iterator i_out = splitOutput.begin(); i_out != splitOutput.end(); ++i_out) { boost::shared_ptr outputPart = *i_out; - outputPart.get()->splitX(splitOutput2, SIZE_MAX, SIZE_MAX, 1); + outputPart.get()->splitX(splitOutput2, UINT_MAX, UINT_MAX, 1); } splitOutput.clear(); for (TPartList::iterator i_out = splitOutput2.begin(); i_out != splitOutput2.end(); ++i_out) { boost::shared_ptr outputPart = *i_out; - outputPart.get()->splitY(splitOutput, SIZE_MAX, SIZE_MAX, 1); + outputPart.get()->splitY(splitOutput, UINT_MAX, UINT_MAX, 1); } splitOutput2.clear(); #endif @@ -164,12 +164,12 @@ bool CCompositeGeometryManager::splitJobs(TJobSet &jobs, size_t maxSize, int div TPartList splitInput2; for (TPartList::iterator i_in = splitInput.begin(); i_in != splitInput.end(); ++i_in) { boost::shared_ptr inputPart = *i_in; - inputPart.get()->splitX(splitInput2, SIZE_MAX, maxBlockDim, 1); + inputPart.get()->splitX(splitInput2, UINT_MAX, maxBlockDim, 1); } splitInput.clear(); for (TPartList::iterator i_in = splitInput2.begin(); i_in != splitInput2.end(); ++i_in) { boost::shared_ptr inputPart = *i_in; - inputPart.get()->splitY(splitInput, SIZE_MAX, maxBlockDim, 1); + inputPart.get()->splitY(splitInput, UINT_MAX, maxBlockDim, 1); } splitInput2.clear(); -- cgit v1.2.3