From cd72626d91974bb76fe819c6f4d1d911abb0d314 Mon Sep 17 00:00:00 2001
From: Mansard <nmansard@laas.fr>
Date: Fri, 11 Feb 2011 10:58:07 +0100
Subject: [PATCH] IVIGIT.

---
 src/CMakeLists.txt                    |   2 +
 src/dynamic_graph/matlab.py           | 107 ++++++++++++++++++++++++++
 src/dynamic_graph/script_shortcuts.py |  85 ++++++++++++++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 src/dynamic_graph/matlab.py
 create mode 100644 src/dynamic_graph/script_shortcuts.py

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a1ef211..bbec789 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -104,6 +104,8 @@ SET (PYTHON_SOURCES
     dynamic_graph/__init__.py
     dynamic_graph/entity.py
     dynamic_graph/signal_base.py
+    dynamic_graph/matlab.py
+    dynamic_graph/script_shortcuts.py
 )
 
 SET (PYTHON_SOURCES_FULLPATH "")
diff --git a/src/dynamic_graph/matlab.py b/src/dynamic_graph/matlab.py
new file mode 100644
index 0000000..78c23aa
--- /dev/null
+++ b/src/dynamic_graph/matlab.py
@@ -0,0 +1,107 @@
+# The matlab class is define to produce a nice rendering of vector and matrix
+# signals. The class builds at construction a string from the display
+# of the matrix/vector, and stores the string internally. The string can be recovered
+# using the str() member.
+# The statics prec, space and fullPrec can be used to modify the display.
+
+
+def pseudozero(prec):
+  """
+  Return a string with '0...' and enough space to fill prec cars.
+  """
+  res='0...'
+  for i in range(2,prec):
+    res+=' '
+  return res
+
+class matlab:
+  prec=12
+  space=2
+  fullPrec=1e-5
+
+  def __init__( self,obj ):
+    try:
+      return self.matlabFromMatrix(obj)
+    except:
+      pass
+    try:
+      return self.matlabFromVector(obj)
+    except:
+      pass
+    self.resstr = str(obj)
+
+  def __str__(self):
+    return self.resstr
+
+  def matlabFromMatrix(self,A):
+    nr=len(A)
+    nc=len(A[0]);
+    fm='%.'+str(self.prec)+'f'
+    maxstr=0
+    mstr=(())
+    for v in A:
+      lnstr=()
+      for x in v:
+        if (abs(x)<self.fullPrec*self.fullPrec):
+          curr='0'
+        else:
+            if (abs(x)<self.fullPrec):
+                curr=pseudozero(self.prec)
+            else:
+                curr= ' '+(fm % x)
+        if( maxstr<len(curr)):
+          maxstr=len(curr)
+        lnstr+=(curr,)
+      mstr+=(lnstr,)
+
+    maxstr+=self.space
+    resstr='[...\n'
+    first=True
+    for v in mstr:
+      if first:
+        first=False;
+      else:
+        resstr+=' ;\n'
+      firstC=True
+      for x in v:
+        if firstC:
+          firstC=False
+        else:
+          resstr+=','
+        for i in range(1,maxstr-len(x)):
+          resstr+=' '
+        resstr+=x
+    resstr+='   ];'
+    self.resstr = resstr
+
+
+  def matlabFromVector(self,v):
+    nr=len(v)
+    fm='%.'+str(self.prec)+'f'
+    maxstr=0
+    vstr=(())
+    for x in v:
+        if (abs(x)<self.fullPrec*self.fullPrec):
+            curr='0'
+        else:
+            if (abs(x)<self.fullPrec):
+                curr=pseudozero(self.prec)
+            else:
+                curr= ' '+(fm % x)
+        if( maxstr<len(curr)):
+            maxstr=len(curr)
+        vstr+=(curr,)
+
+    maxstr+=self.space
+    resstr='[  '
+    first=True
+    for x in vstr:
+      if first:
+        first=False;
+      else:
+        resstr+=','
+      for i in range(1,maxstr-len(x)):
+        resstr+=' '
+      resstr+=x
+    resstr+='   ]\';'
+    self.resstr = resstr
diff --git a/src/dynamic_graph/script_shortcuts.py b/src/dynamic_graph/script_shortcuts.py
new file mode 100644
index 0000000..cd6648a
--- /dev/null
+++ b/src/dynamic_graph/script_shortcuts.py
@@ -0,0 +1,85 @@
+# Defines the following shortcuts:
+# signal.name -> return the shortname
+# signal      -> display nicely the content of the signal
+# signal(3)   -> recompute the signal at time 3, and display nicely
+# signal +1   -> increment the signal time of 1, recompute, and display.
+# signal.deps -> display the graph dependancy up to the default value (3)
+# signal.deps(6) -> same, but with depth = 6.
+# entity      -> same as print(entity)
+# change the prompt to be '%'
+
+
+
+from dynamic_graph.signal_base import *
+from dynamic_graph.entity import *
+from matlab import matlab
+
+# Enables shortcut "name"
+def sig_short_name(self):
+    return self.getName().split(':')[-1]
+
+setattr(SignalBase,'name',property(sig_short_name))
+
+# Enables shortcuts "m"
+# This code implements a pseudo function 'm' in the class signal_base,
+# with no args, or optional args. Three calls can be made:
+#    - sig.m : print the current value.
+#    - sig.m(time): recompute at given <time>, and display the current value
+#    - sig.m +time: recompute at <time> after current time, and display.
+class PrettySignalPrint:
+    sig = None
+    def __init__(self,sig):
+        self.sig = sig
+    def __repr__(self):
+        return str(matlab(self.sig.value))
+    def __call__(self,iter):
+        self.sig.recompute(iter)
+        return self
+    def __add__(self,iter):
+        self.sig.recompute( self.sig.time+iter )
+        return self
+
+def sigMatPrint(sig):
+    return PrettySignalPrint(sig)
+
+setattr(SignalBase,'m',property(PrettySignalPrint))
+print('Pretty matlab print set')
+
+# Enable the same as 'm', but directly on the signal object.
+def sigRepr( self ):
+    return self.name+' = '+str(matlab(self.value))
+
+def sigCall( sig,iter ):
+    sig.recompute(iter)
+    print sigRepr(sig)
+
+def sigTimeIncr( sig,iter ):
+    sig.recompute(sig.time+iter)
+    print sigRepr(sig)
+
+setattr(SignalBase,'__repr__',sigRepr)
+setattr(SignalBase,'__call__',sigCall)
+setattr(SignalBase,'__add__',sigTimeIncr)
+
+# Enables shortcut "deps"
+# Implements the peudo function 'deps', that can be called without arg,
+# or specifying a specific depth to be printed.
+class SignalDepPrint:
+    defaultDepth = 2
+    sig = None
+    def __init__(self,sig):
+        self.sig=sig
+    def __repr__(self):
+        return self.sig.displayDependencies(self.defaultDepth)
+    def __call__(self,depth):
+        self.defaultDepth = depth
+        return self
+
+setattr(SignalBase,'deps',property(SignalDepPrint))
+
+setattr(Entity,'__repr__',Entity.__str__)
+
+# Changing prompt
+import sys
+sys.ps1 = '% '
+
-- 
GitLab