printers.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 # This file is part of Eigen, a lightweight C++ template library
00003 # for linear algebra.
00004 #
00005 # Copyright (C) 2009 Benjamin Schindler <bschindler@inf.ethz.ch>
00006 #
00007 # Eigen is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 3 of the License, or (at your option) any later version.
00011 #
00012 # Alternatively, you can redistribute it and/or
00013 # modify it under the terms of the GNU General Public License as
00014 # published by the Free Software Foundation; either version 2 of
00015 # the License, or (at your option) any later version.
00016 #
00017 # Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 # GNU General Public License for more details.
00021 #
00022 # You should have received a copy of the GNU Lesser General Public
00023 # License and a copy of the GNU General Public License along with
00024 # Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 # Pretty printers for Eigen::Matrix
00027 # This is still pretty basic as the python extension to gdb is still pretty basic. 
00028 # It cannot handle complex eigen types and it doesn't support any of the other eigen types
00029 # Such as quaternion or some other type. 
00030 # This code supports fixed size as well as dynamic size matrices
00031 
00032 # To use it:
00033 #
00034 # * create a directory and put the file as well as an empty __init__.py in that directory
00035 # * Create a ~/.gdbinit file, that contains the following:
00036 
00037 
00038 import gdb
00039 import re
00040 import itertools
00041 
00042 
00043 class EigenMatrixPrinter:
00044         "Print Eigen Matrix of some kind"
00045 
00046         def __init__(self, val):
00047                 "Extract all the necessary information"
00048                 # The gdb extension does not support value template arguments - need to extract them by hand
00049                 type = val.type
00050                 if type.code == gdb.TYPE_CODE_REF:
00051                         type = type.target()
00052                 self.type = type.unqualified().strip_typedefs()
00053                 tag = self.type.tag
00054                 regex = re.compile('<.*>')
00055                 m = regex.findall(tag)[0][1:-1]
00056                 template_params = m.split(',')
00057                 template_params = map(lambda x:x.replace(" ", ""), template_params)
00058 
00059                 if template_params[1] == '-0x00000000000000001' or template_params[1] == '-0x000000001':
00060                         self.rows = val['m_storage']['m_rows']
00061                 else:
00062                         self.rows = int(template_params[1])
00063                 
00064                 if template_params[2] == '-0x00000000000000001' or template_params[2] == '-0x000000001':
00065                         self.cols = val['m_storage']['m_cols']
00066                 else:
00067                         self.cols = int(template_params[2])
00068                 
00069                 self.options = 0 # default value
00070                 if len(template_params) > 3:
00071                         self.options = template_params[3];
00072                 
00073                 self.rowMajor = (int(self.options) & 0x1)
00074 
00075                 self.innerType = self.type.template_argument(0)
00076 
00077                 self.val = val
00078                 
00079                 # Fixed size matrices have a struct as their storage, so we need to walk through this
00080                 self.data = self.val['m_storage']['m_data']
00081                 if self.data.type.code == gdb.TYPE_CODE_STRUCT:
00082                         self.data = self.data['array']
00083                         self.data = self.data.cast(self.innerType.pointer())
00084                         
00085         class _iterator:
00086                 def __init__ (self, rows, cols, dataPtr, rowMajor):
00087                         self.rows = rows
00088                         self.cols = cols
00089                         self.dataPtr = dataPtr
00090                         self.currentRow = 0
00091                         self.currentCol = 0
00092                         self.rowMajor = rowMajor
00093 
00094                 def __iter__ (self):
00095                         return self
00096 
00097                 def next(self):
00098                 
00099                         row = self.currentRow
00100                         col = self.currentCol
00101                         if self.rowMajor == 0:
00102                                 if self.currentCol >= self.cols:
00103                                         raise StopIteration
00104                                         
00105                                 self.currentRow = self.currentRow + 1
00106                                 if self.currentRow >= self.rows:
00107                                         self.currentRow = 0
00108                                         self.currentCol = self.currentCol + 1
00109                         else:
00110                                 if self.currentRow >= self.rows:
00111                                         raise StopIteration
00112                                         
00113                                 self.currentCol = self.currentCol + 1
00114                                 if self.currentCol >= self.cols:
00115                                         self.currentCol = 0
00116                                         self.currentRow = self.currentRow + 1
00117                                 
00118 
00119                         item = self.dataPtr.dereference()
00120                         self.dataPtr = self.dataPtr + 1
00121                         if (self.cols == 1): #if it's a column vector
00122                                 return ('[%d]' % (row,), item)
00123                         elif (self.rows == 1): #if it's a row vector
00124                                 return ('[%d]' % (col,), item)
00125                         return ('[%d,%d]' % (row, col), item)
00126 
00127         def children(self):
00128                 
00129                 return self._iterator(self.rows, self.cols, self.data, self.rowMajor)
00130 
00131         def to_string(self):
00132                 return "Eigen::Matrix<%s,%d,%d,%s> (data ptr: %s)" % (self.innerType, self.rows, self.cols, "RowMajor" if self.rowMajor else  "ColMajor", self.data)
00133 
00134 class EigenQuaternionPrinter:
00135         "Print an Eigen Quaternion"
00136 
00137         def __init__(self, val):
00138                 "Extract all the necessary information"
00139                 # The gdb extension does not support value template arguments - need to extract them by hand
00140                 type = val.type
00141                 if type.code == gdb.TYPE_CODE_REF:
00142                         type = type.target()
00143                 self.type = type.unqualified().strip_typedefs()
00144                 self.innerType = self.type.template_argument(0)
00145                 self.val = val
00146                 
00147                 # Quaternions have a struct as their storage, so we need to walk through this
00148                 self.data = self.val['m_coeffs']['m_storage']['m_data']['array']
00149                 self.data = self.data.cast(self.innerType.pointer())
00150                         
00151         class _iterator:
00152                 def __init__ (self, dataPtr):
00153                         self.dataPtr = dataPtr
00154                         self.currentElement = 0
00155                         self.elementNames = ['x', 'y', 'z', 'w']
00156 
00157                 def __iter__ (self):
00158                         return self
00159 
00160                 def next(self):
00161                         element = self.currentElement
00162 
00163                         if self.currentElement >= 4: #there are 4 elements in a quanternion
00164                                 raise StopIteration
00165                         
00166                         self.currentElement = self.currentElement + 1
00167 
00168                         item = self.dataPtr.dereference()
00169                         self.dataPtr = self.dataPtr + 1
00170                         return ('[%s]' % (self.elementNames[element],), item)
00171                         
00172         def children(self):
00173                 
00174                 return self._iterator(self.data)
00175 
00176         def to_string(self):
00177                 return "Eigen::Quaternion<%s> (data ptr: %s)" % (self.innerType, self.data)
00178 
00179 def build_eigen_dictionary ():
00180         pretty_printers_dict[re.compile('^Eigen::Quaternion<.*>$')] = lambda val: EigenQuaternionPrinter(val)
00181         pretty_printers_dict[re.compile('^Eigen::Matrix<.*>$')] = lambda val: EigenMatrixPrinter(val)
00182 
00183 def register_eigen_printers(obj):
00184         "Register eigen pretty-printers with objfile Obj"
00185 
00186         if obj == None:
00187                 obj = gdb
00188         obj.pretty_printers.append(lookup_function)
00189 
00190 def lookup_function(val):
00191         "Look-up and return a pretty-printer that can print va."
00192 
00193         type = val.type
00194 
00195         if type.code == gdb.TYPE_CODE_REF:
00196                 type = type.target()
00197         
00198         type = type.unqualified().strip_typedefs()
00199 
00200         typename = type.tag
00201         if typename == None:
00202                 return None
00203 
00204         for function in pretty_printers_dict:
00205                 if function.search(typename):
00206                         return pretty_printers_dict[function](val)
00207 
00208         return None
00209 
00210 pretty_printers_dict = {}
00211 
00212 build_eigen_dictionary ()


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:11