From 0884a3928395dd368aea61eeef768677018f9095 Mon Sep 17 00:00:00 2001
From: Willem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>
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

(limited to 'python/astra')

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 <http://www.gnu.org/licenses/>.
+#
+# -----------------------------------------------------------------------
+
+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