Exemplo n.º 1
0
 def OnDown(self, event):
     """ Called when the mouse is pressed down in the axes.
     """
     
     # Only process left mouse button
     if event.button != 1:
         return False
     
     # Store location
     self._active = Point(event.x2d, event.y2d)
     
     # Clear temp line object
     if self._line1:
         self._line1.Destroy()
     
     # Create line objects
     tmp = Pointset(2)
     tmp.append(self._active)
     tmp.append(self._active)
     self._line1 = vv.plot(tmp, lc='r', lw='1', axes=self._a, axesAdjust=0)
     
     # Draw
     self._a.Draw()
     
     # Prevent dragging by indicating the event needs no further handling
     return True
Exemplo n.º 2
0
    def test_gui(self):
        pp = Pointset(3)
        pp.append(0, 0, 0)
        pp.append(0, 1, 0)
        pp.append(1, 2, 0)
        pp.append(0, 2, 1)

        # Create all solids
        vv.solidBox((0, 0, 0))
        sphere = vv.solidSphere((3, 0, 0))
        cone = vv.solidCone((6, 0, 0))
        # a cone with 4 faces is a pyramid
        pyramid = vv.solidCone((9, 0, 0), N=4)
        vv.solidCylinder((0, 3, 0), (1, 1, 2))
        ring = vv.solidRing((3, 3, 0))
        vv.solidTeapot((6, 3, 0))
        vv.solidLine(pp+Point(9, 3, 0), radius=0.2)

        # Make the ring green
        ring.faceColor = 'g'

        # Make the sphere dull
        sphere.specular = 0
        sphere.diffuse = 0.4

        # Show lines in yellow pyramid
        pyramid.faceColor = 'r'
        pyramid.edgeShading = 'plain'

        # Colormap example
        N = cone._vertices.shape[0]
        cone.SetValues(np.linspace(0, 1, N))
        cone.colormap = vv.CM_JET
Exemplo n.º 3
0
def compareGraphsVisually(graph1, graph2, fig=None):
    """ compareGraphsVisually(graph1, graph2, fig=None)
    Show the two graphs together in a figure. Matched nodes are
    indicated by lines between them.
    """
    
    # Get figure
    if isinstance(fig,int):
        fig = vv.figure(fig)
    elif fig is None:
        fig = vv.figure()
    
    # Prepare figure and axes
    fig.Clear()
    a = vv.gca()
    a.cameraType = '3d'; a.daspectAuto = False
    
    # Draw both graphs
    graph1.Draw(lc='b', mc='b')
    graph2.Draw(lc='r', mc='r')
    
    # Set the limits
    a.SetLimits()
    
    # Make a line from the edges
    pp = Pointset(3)
    for node in graph1:
        if hasattr(node, 'match') and node.match is not None:
            pp.append(node); pp.append(node.match)
    
    # Plot edges
    vv.plot(pp, lc='g', ls='+')
Exemplo n.º 4
0
 def on_down(self, event):
     
     if event.button != 1:
         return False        
     if not vv.KEY_SHIFT in event.modifiers:
         return False
     
     # Store location
     self._active = Point(event.x2d, event.y2d)
     
     # Clear any line object
     for l in self._lines:
         l.Destroy()
     
     # Create line objects
     tmp = Pointset(2)
     tmp.append(self._active)
     tmp.append(self._active)
     l1 = vv.plot(tmp, lc='g', lw='3', axes=self._a2, axesAdjust=0)
     l2 = vv.plot(tmp[:1], ls='', ms='.', mc='g', axes=self._a2, axesAdjust=0)
     self._lines = [l1, l2]
     
     # Draw
     self._a2.Draw()
     
     # Prevent dragging by indicating the event needs no further handling
     return True
Exemplo n.º 5
0
def create_mesh(graph, radius=1.0, fullPaths=True):
    """ Create a polygonal model from the stent and return it as
    a visvis.BaseMesh object. To draw the mesh, instantiate
    a normal mesh using vv.Mesh(vv.gca(), thisMesh).
    """
    from visvis.processing import lineToMesh, combineMeshes
    from visvis import Pointset  # lineToMesh does not like the new PointSet class

    # Init list of meshes
    meshes = []

    for n1, n2 in graph.edges():
        # Obtain path of edge and make mesh
        if fullPaths:
            path = graph.edge[n1][n2]['path']
            path = Pointset(path)  # Make a visvis pointset
        else:
            path = Pointset(3)
            path.append(n1)
            path.append(n2)
        meshes.append(lineToMesh(path, radius, 8))

    # Combine meshes and return
    if meshes:
        return combineMeshes(meshes)
    else:
        return None
Exemplo n.º 6
0
def compareGraphsVisually(graph1, graph2, fig=None):
    """ compareGraphsVisually(graph1, graph2, fig=None)
    Show the two graphs together in a figure. Matched nodes are
    indicated by lines between them.
    """
    
    # Get figure
    if isinstance(fig,int):
        fig = vv.figure(fig)
    elif fig is None:
        fig = vv.figure()
    
    # Prepare figure and axes
    fig.Clear()
    a = vv.gca()
    a.cameraType = '3d'; a.daspectAuto = False
    
    # Draw both graphs
    graph1.Draw(lc='b', mc='b')
    graph2.Draw(lc='r', mc='r')
    
    # Set the limits
    a.SetLimits()
    
    # Make a line from the edges
    pp = Pointset(3)
    for node in graph1:
        if hasattr(node, 'match') and node.match is not None:
            pp.append(node); pp.append(node.match)
    
    # Plot edges
    vv.plot(pp, lc='g', ls='+')
Exemplo n.º 7
0
    def OnDown(self, event):
        """ Called when the mouse is pressed down in the axes.
        """

        # Only process left mouse button
        if event.button != 1:
            return False

        # Store location
        self._active = Point(event.x2d, event.y2d)

        # Clear temp line object
        if self._line1:
            self._line1.Destroy()

        # Create line objects
        tmp = Pointset(2)
        tmp.append(self._active)
        tmp.append(self._active)
        self._line1 = vv.plot(tmp, lc="r", lw="1", axes=self._a, axesAdjust=0)

        # Draw
        self._a.Draw()

        # Prevent dragging by indicating the event needs no further handling
        return True
Exemplo n.º 8
0
 def on_motion(self, event):
     if self._active and self._lines:
         # Update line
         tmp = Pointset(2)
         tmp.append(self._active)
         tmp.append(event.x2d, event.y2d)
         l1 = self._lines[0]
         l1.SetPoints(tmp)
         # Draw
         self._a2.Draw()
Exemplo n.º 9
0
 def Pack(self):
     """ Pack()
     Pack the contents in an ssdf struct, such that it can be stored.
     """
     
     # If nodelist is empty, ndim defaults to two
     ndim = 2
     
     # Get a list of all edges
     cc = self.GetEdges()
     
     # Check whether the nodes are homogenous, otherwise we cannot store
     if len(self):
         ndim = self[0].ndim
         for node in self:
             if not node.ndim == ndim:
                 raise ValueError('All nodes should have the same dimension.')
     
     # Init struct
     struct = ssdf.new()
     
     # Create array of nodes        
     pp = Pointset(ndim)
     for node in self:
         pp.append(node)
     struct.nodes = pp.data
     
     # Create the edges
     tmp = np.zeros((len(cc), 2), dtype=np.uint32)
     for i in range(len(cc)):
         c = cc[i]            
         tmp[i,0] = c._i1
         tmp[i,1] = c._i2
     struct.edges = tmp
     
     
     # Store the properties of the edges. The propRefs array
     # Does contain redundant data, but it can be stored efficiently
     # because the compression handles that...
     allProps = ''
     propRefs = np.zeros((len(cc), 2), dtype=np.uint32)
     for i in range(len(cc)):
         tmp = serializeProps(cc[i].props)
         propRefs[i, 0] = len(allProps)
         propRefs[i, 1] = len(allProps) + len(tmp)
         allProps += tmp
     struct.edgePropRefs = propRefs
     if allProps:
         struct.edgeProps = np.frombuffer( allProps, dtype=np.uint8)
     else:
         struct.edgeProps = np.zeros( (0,), dtype=np.uint8)
     
     # done
     return struct
Exemplo n.º 10
0
 def Pack(self):
     """ Pack()
     Pack the contents in an ssdf struct, such that it can be stored.
     """
     
     # If nodelist is empty, ndim defaults to two
     ndim = 2
     
     # Get a list of all edges
     cc = self.GetEdges()
     
     # Check whether the nodes are homogenous, otherwise we cannot store
     if len(self):
         ndim = self[0].ndim
         for node in self:
             if not node.ndim == ndim:
                 raise ValueError('All nodes should have the same dimension.')
     
     # Init struct
     struct = ssdf.new()
     
     # Create array of nodes        
     pp = Pointset(ndim)
     for node in self:
         pp.append(node)
     struct.nodes = pp.data
     
     # Create the edges
     tmp = np.zeros((len(cc), 2), dtype=np.uint32)
     for i in range(len(cc)):
         c = cc[i]            
         tmp[i,0] = c._i1
         tmp[i,1] = c._i2
     struct.edges = tmp
     
     
     # Store the properties of the edges. The propRefs array
     # Does contain redundant data, but it can be stored efficiently
     # because the compression handles that...
     allProps = b''
     propRefs = np.zeros((len(cc), 2), dtype=np.uint32)
     for i in range(len(cc)):
         tmp = serializeProps(cc[i].props)
         propRefs[i, 0] = len(allProps)
         propRefs[i, 1] = len(allProps) + len(tmp)
         allProps += tmp
     struct.edgePropRefs = propRefs
     if allProps:
         struct.edgeProps = np.frombuffer( allProps, dtype=np.uint8)
     else:
         struct.edgeProps = np.zeros( (0,), dtype=np.uint8)
     
     # done
     return struct
Exemplo n.º 11
0
    def OnMotion(self, event):
        """ Called when the mouse is moved in the axes.
        """

        if self._active and self._line1:
            # Update line
            tmp = Pointset(2)
            tmp.append(self._active)
            tmp.append(event.x2d, event.y2d)
            self._line1.SetPoints(tmp)
            # Draw
            self._a.Draw()
Exemplo n.º 12
0
 def OnMotion(self, event):
     """ Called when the mouse is moved in the axes.
     """
     
     if self._active and self._line1:
         # Update line
         tmp = Pointset(2)
         tmp.append(self._active)
         tmp.append(event.x2d, event.y2d)
         self._line1.SetPoints(tmp)
         # Draw
         self._a.Draw()
Exemplo n.º 13
0
    def apply(self, event=None):

        # Get axes
        a1 = self._a1

        # Get sampling
        grid_sampling = self._sampling, self._sampling * 2**self._levels

        # Create grid
        tmp = self._pp[:, 0]
        tmp.shape = (tmp.size, 1)
        pp = Pointset(tmp)
        grid1 = SplineGrid.from_points_multiscale(
            (self._fieldSize, ), grid_sampling, pp.data, self._pp[:, 1])

        # Get copy
        grid2 = grid1.copy()

        # Freeze edges
        self.freeze_edges(grid1)

        #         # Create second grid
        #         grid2 = SplineGrid.from_field(grid.get_field(), grid.grid_sampling)
        #         grid3 = SplineGrid.from_field_multiscale(grid.get_field(), grid.grid_sampling)

        # Get grid points
        ppg1 = Pointset(2)
        ppg2 = Pointset(2)
        for gx in range(grid1.grid_shape[0]):
            ppg1.append((gx - 1) * grid1.grid_sampling, grid1.knots[gx])
            ppg2.append((gx - 1) * grid2.grid_sampling, grid2.knots[gx])

        # Get field
        field = grid1.get_field()
        #field2 = grid2.get_field()
        #field3 = grid3.get_field()

        # Delete objects in scene
        for ob in a1.wobjects:
            if ob is not self._line1 and not isinstance(
                    ob, vv.axises.BaseAxis):
                ob.Destroy()

        # Draw
        vv.plot(ppg1, ls='', ms='x', mc='b', mw=9, axes=a1, axesAdjust=False)
        vv.plot(ppg2, ls='', ms='x', mc='c', mw=9, axes=a1, axesAdjust=False)
        vv.plot(np.arange(0, field.size),
                field,
                ls='-',
                lc='g',
                lw=3,
                axes=a1,
                axesAdjust=False)
Exemplo n.º 14
0
 def Draw(self, mc='g', lc='y', mw=7, lw=0.6, alpha=0.5, axes=None):
     """ Draw(mc='g', lc='y', mw=7, lw=0.6, alpha=0.5, axes=None)
     Draw nodes and edges. 
     """ 
     
     # We can only draw if we have any nodes
     if not len(self):
         return
     
     # Make sure there are elements in the lines list
     while len(self._lines)<2:
         self._lines.append(None)
     
     # Build node list
     if mc and mw:
         pp = Pointset(self[0].ndim)
         for p in self:
             pp.append(p)
         # Draw nodes, reuse if possible!
         l_node = self._lines[0]            
         if l_node and len(l_node._points) == len(pp):
             l_node.SetPoints(pp)
         elif l_node:
             l_node.Destroy()
             l_node = None
         if l_node is None:                
             l_node = vv.plot(pp, ls='', ms='o', mc=mc, mw=mw, 
                 axesAdjust=0, axes=axes, alpha=alpha)
             self._lines[0] = l_node
     
     # For simplicity, always redraw edges
     if self._lines[1] is not None: 
         self._lines[1].Destroy()
     
     # Build edge list
     if lc and lw:
         cc = self.GetEdges()
         # Draw edges
         pp = Pointset(self[0].ndim)
         for c in cc:            
             pp.append(c.end1); pp.append(c.end2)
         tmp = vv.plot(pp, ms='', ls='+', lc=lc, lw=lw, 
             axesAdjust=0, axes=axes, alpha=alpha)
         self._lines[1] = tmp
Exemplo n.º 15
0
 def Draw(self, mc='g', lc='y', mw=7, lw=0.6, alpha=0.5, axes=None):
     """ Draw(mc='g', lc='y', mw=7, lw=0.6, alpha=0.5, axes=None)
     Draw nodes and edges. 
     """ 
     
     # We can only draw if we have any nodes
     if not len(self):
         return
     
     # Make sure there are elements in the lines list
     while len(self._lines)<2:
         self._lines.append(None)
     
     # Build node list
     if mc and mw:
         pp = Pointset(self[0].ndim)
         for p in self:
             pp.append(p)
         # Draw nodes, reuse if possible!
         l_node = self._lines[0]            
         if l_node and len(l_node._points) == len(pp):
             l_node.SetPoints(pp)
         elif l_node:
             l_node.Destroy()
             l_node = None
         if l_node is None:                
             l_node = vv.plot(pp, ls='', ms='o', mc=mc, mw=mw, 
                 axesAdjust=0, axes=axes, alpha=alpha)
             self._lines[0] = l_node
     
     # For simplicity, always redraw edges
     if self._lines[1] is not None: 
         self._lines[1].Destroy()
     
     # Build edge list
     if lc and lw:
         cc = self.GetEdges()
         # Draw edges
         pp = Pointset(self[0].ndim)
         for c in cc:            
             pp.append(c.end1); pp.append(c.end2)
         tmp = vv.plot(pp, ms='', ls='+', lc=lc, lw=lw, 
             axesAdjust=0, axes=axes, alpha=alpha)
         self._lines[1] = tmp
Exemplo n.º 16
0
plain color, colormaps and texture.

On the website, this example also demonstrates the fly camera to fly through
the mesh objects.

"""

import numpy as np
import visvis as vv
from visvis import Point, Pointset
vv.figure()
a = vv.gca()

# Define points for the line
pp = Pointset(3)
pp.append(0,0,0); pp.append(0,1,0); pp.append(1,2,0); pp.append(0,2,1)

# Create all solids
box = vv.solidBox((0,0,0))
sphere = vv.solidSphere((3,0,0))
cone = vv.solidCone((6,0,0))
pyramid = vv.solidCone((9,0,0), N=4) # a cone with 4 faces is a pyramid
cylinder = vv.solidCylinder((0,3,0),(1,1,2))
ring = vv.solidRing((3,3,0))
teapot = vv.solidTeapot((6,3,0))
line = vv.solidLine(pp+Point(9,3,0), radius = 0.2)

# Let's put a face on that cylinder
# This works because 2D texture coordinates are automatically generated for
# the sphere, cone, cylinder and ring.
im = vv.imread('astronaut.png')
Exemplo n.º 17
0
 def apply(self):
     
     # Get sampling
     grid_sampling = self._sampling, self._sampling*2**self._levels
     
     # Init field and deform
     if not self._pp1:
         # Unit deform
         deform = self.DeformationField(FieldDescription(self._im))
     elif self._multiscale:
         deform = self.DeformationField.from_points_multiscale(self._im, grid_sampling, 
                     self._pp1.data, self._pp2.data,
                     injective=self._injective, frozenedge=self._frozenedge)
     else:
         DeformationGrid = DeformationGridForward
         if not self._forward:
             DeformationGrid = DeformationGridBackward
         grid = DeformationGrid.from_points(self._im, self._sampling, 
                     self._pp1.data, self._pp2.data, 
                     injective=self._injective, frozenedge=self._frozenedge)
         deform = grid.as_deformation_field()
     
     # Store grid
     field0 = self.DeformationField(FieldDescription(self._im))
     self._field2 = deform
     field3 = self._field1.compose(self._field2)
     
     # Deform
     im2 = self._field1.apply_deformation(self._im)
     im3 = field3.apply_deformation(self._im)
     
     # Update imagesf
     self._t2.SetData(im2)
     self._t3.SetData(im3)
     
     # Update grids
     for a, field in zip(    [self._a4, self._a5, self._a6],
                             [field0, self._field1, field3] ):
         a.Clear()
         field.show(a, False)
         a.axis.visible = False
     
     # Update lines
     tmp = Pointset(2)
     for i in range(len(self._pp1)):            
         tmp.append(self._pp1[i])
         tmp.append(self._pp2[i])
     self._line1.SetPoints(self._pp1)
     self._line2.SetPoints(tmp)
     
     # Draw
     self._a2.Draw()
     
     # Show text
     text1 = 'B-spline (S) with sampling %i (U/D) and %i levels (L/R).' % (
         self._sampling, self._levels)
     
     text2 = 'Multiscale %i (M), injective %1.1f (I), frozen edge %i (E), forward %i (F).' % (
         self._multiscale, self._injective, self._frozenedge, self._forward)
     
     # Display and apply
     self._text1.text = text1
     self._text2.text = text2
Exemplo n.º 18
0
class Drawer:
    def __init__(self):

        # Create figure and axes
        vv.figure()
        self._a = a = vv.gca()
        vv.title("Hold mouse to draw lines. Use 'rgbcmyk' and '1-9' keys.")

        # Set axes
        a.SetLimits((0, 1), (0, 1))
        a.cameraType = "2d"
        a.daspectAuto = False
        a.axis.showGrid = True

        # Init variables needed during drawing
        self._active = None
        self._pp = Pointset(2)

        # Create null and empty line objects
        self._line1 = None
        self._line2 = vv.plot(vv.Pointset(2), ls="+", lc="c", lw="2", axes=a)

        # Bind to events
        a.eventMouseDown.Bind(self.OnDown)
        a.eventMouseUp.Bind(self.OnUp)
        a.eventMotion.Bind(self.OnMotion)
        a.eventKeyDown.Bind(self.OnKey)

    def OnKey(self, event):
        """ Called when a key is pressed down in the axes.
        """

        if event.text and event.text.lower() in "rgbcmywk":
            self._line2.lc = event.text.lower()
        else:
            try:
                tickness = int(event.text)
            except Exception:
                tickness = 0
            if tickness > 0:
                self._line2.lw = tickness

    def OnDown(self, event):
        """ Called when the mouse is pressed down in the axes.
        """

        # Only process left mouse button
        if event.button != 1:
            return False

        # Store location
        self._active = Point(event.x2d, event.y2d)

        # Clear temp line object
        if self._line1:
            self._line1.Destroy()

        # Create line objects
        tmp = Pointset(2)
        tmp.append(self._active)
        tmp.append(self._active)
        self._line1 = vv.plot(tmp, lc="r", lw="1", axes=self._a, axesAdjust=0)

        # Draw
        self._a.Draw()

        # Prevent dragging by indicating the event needs no further handling
        return True

    def OnMotion(self, event):
        """ Called when the mouse is moved in the axes.
        """

        if self._active and self._line1:
            # Update line
            tmp = Pointset(2)
            tmp.append(self._active)
            tmp.append(event.x2d, event.y2d)
            self._line1.SetPoints(tmp)
            # Draw
            self._a.Draw()

    def OnUp(self, event):
        """ Called when the mouse is released (when first pressed down 
        in the axes).
        """

        # Only if a point is active
        if self._active is None:
            return False

        # Get points
        p1 = self._active
        p2 = Point(event.x2d, event.y2d)

        # Add!
        self._pp.append(p1)
        self._pp.append(p2)

        # We're done with this one
        self._active = None

        # Clear temp line object
        if self._line1:
            self._line1.Destroy()
            self._line1 = None

        # Update lines
        self._line2.SetPoints(self._pp)

        # Draw
        self._a.Draw()
Exemplo n.º 19
0
class SplineByHand:
    """ SplineByHand()
    
    Demo application to influence a 1D spline grid using control points.
    """
    def __init__(self):

        # Setup visualization
        self._fig = fig = vv.figure()
        self._a1 = a1 = vv.subplot(111)

        # Init pointset with control points
        self._pp = Pointset(2)

        # Init lines to show control points
        self._line1 = vv.plot(self._pp, ls='', ms='.', mc='r', mw=11, axes=a1)
        self._line1.hitTest = True

        # Init grid properties
        self._fieldSize = 100
        self._sampling = 10
        self._levels = 5

        # Member to indicate the point being dragged (as an index)
        self._active = None

        # Bind to events
        a1.eventDoubleClick.Bind(self.on_doubleclick)
        self._line1.eventDoubleClick.Bind(self.on_doubleclick_line)
        self._line1.eventMouseDown.Bind(self.on_down_line)
        self._line1.eventMouseUp.Bind(self.on_up_line)
        a1.eventMotion.Bind(self.on_motion)
        fig.eventKeyDown.Bind(self.on_key_down)

        print(
            'Use left/right to control #levels and up/down to control grid sampling.'
        )

        # Init
        self.apply()
        a1.daspectAuto = False
        a1.SetLimits()
        a1.axis.showGrid = True

    def on_key_down(self, event):

        # Update level
        if event.key == vv.KEY_UP:
            self._sampling += 1
        elif event.key == vv.KEY_DOWN:
            self._sampling -= 1
        elif event.key == vv.KEY_RIGHT:
            self._levels += 1
        elif event.key == vv.KEY_LEFT:
            self._levels -= 1
        else:
            return

        # Correct
        if self._sampling < 1:
            self._sampling = 1

        # Apply and print
        print('Using B-spline grid with %i sampling and %i levels.' %
              (self._sampling, self._levels))
        self.apply()

    def on_doubleclick(self, event):  # On axes

        # Get new point
        p = Point(event.x2d, event.y2d)

        # Add to pointset
        self._pp.append(p)
        self._line1.SetPoints(self._pp)

        # Apply
        self.apply()

    def on_doubleclick_line(self, event):  # On axes

        # Get closest point
        dists = Point(event.x2d, event.y2d).distance(self._pp)
        I, = np.where(dists == dists.min())
        if not len(I):
            return False

        # Remove from pointset
        self._pp.Pop(I[0])
        self._line1.SetPoints(self._pp)

        # Apply
        self._a1.Draw()
        self.apply()

    def on_down_line(self, event):  # On line instance

        if event.button != 1:
            return False

        # Get closest point
        dists = Point(event.x2d, event.y2d).distance(self._pp)
        I, = np.where(dists == dists.min())
        if not len(I):
            return False

        # Store
        self._active = I[0]

        # Prevent dragging by indicating the event needs no further handling
        return True

    def on_motion(self, event):

        if self._active is None:
            return False

        # Update line
        self._pp[self._active] = Point(event.x2d, event.y2d)
        self._line1.SetPoints(self._pp)
        # Draw
        self._a1.Draw()

    def on_up_line(self, event):

        if self._active is None:
            return False

        # Update point
        self.on_motion(event)

        # Deactivate
        self._active = None

        # Apply
        self.apply()

    def apply(self, event=None):

        # Get axes
        a1 = self._a1

        # Get sampling
        grid_sampling = self._sampling, self._sampling * 2**self._levels

        # Create grid
        tmp = self._pp[:, 0]
        tmp.shape = (tmp.size, 1)
        pp = Pointset(tmp)
        grid1 = SplineGrid.from_points_multiscale(
            (self._fieldSize, ), grid_sampling, pp.data, self._pp[:, 1])

        # Get copy
        grid2 = grid1.copy()

        # Freeze edges
        self.freeze_edges(grid1)

        #         # Create second grid
        #         grid2 = SplineGrid.from_field(grid.get_field(), grid.grid_sampling)
        #         grid3 = SplineGrid.from_field_multiscale(grid.get_field(), grid.grid_sampling)

        # Get grid points
        ppg1 = Pointset(2)
        ppg2 = Pointset(2)
        for gx in range(grid1.grid_shape[0]):
            ppg1.append((gx - 1) * grid1.grid_sampling, grid1.knots[gx])
            ppg2.append((gx - 1) * grid2.grid_sampling, grid2.knots[gx])

        # Get field
        field = grid1.get_field()
        #field2 = grid2.get_field()
        #field3 = grid3.get_field()

        # Delete objects in scene
        for ob in a1.wobjects:
            if ob is not self._line1 and not isinstance(
                    ob, vv.axises.BaseAxis):
                ob.Destroy()

        # Draw
        vv.plot(ppg1, ls='', ms='x', mc='b', mw=9, axes=a1, axesAdjust=False)
        vv.plot(ppg2, ls='', ms='x', mc='c', mw=9, axes=a1, axesAdjust=False)
        vv.plot(np.arange(0, field.size),
                field,
                ls='-',
                lc='g',
                lw=3,
                axes=a1,
                axesAdjust=False)
        #vv.plot(field2, ls=':', lc=(0,0.5,0), lw=6, axes=a1, axesAdjust=False)
        #vv.plot(field3, ls=':', lc=(0,0.75,0), lw=6, axes=a1, axesAdjust=False)

    def freeze_edges(self, grid):

        # Store grid for debugging

        self._grid = grid

        # Freeze left edge
        grid.knots[1] = 0
        grid.knots[0] = -grid.knots[2]

        #         c0, c1, c2, c3 = pirt.get_cubic_spline_coefs(0, 'B')
        #         k1, k2, k3 = grid.knots[0], grid.knots[1], grid.knots[2]
        #         grid.knots[0] = 0
        #         grid.knots[1]=0
        #         grid.knots[2]= )

        # Calculate t factor
        field_edge = (grid.field_shape[0] - 1) * grid.field_sampling[0]
        grid_edge = (grid.grid_shape[0] - 4) * grid.grid_sampling
        t = (field_edge - grid_edge) / grid.grid_sampling

        # Get coefficients
        c0, c1, c2, c3 = interp.get_cubic_spline_coefs(t, 'B')

        # Freeze right edge
        #         grid.knots[-3] = t*grid.knots[-3] # + (1-t)*0
        #         k0, k1 = grid.knots[-4], grid.knots[-3]
        #         grid.knots[-2] = t**2 * (k1-k0) + t*(2*k0-k1) - k0
        #         k0, k1, k2, k3 = grid.knots[-4], grid.knots[-3], grid.knots[-2], grid.knots[-1]
        #         grid.knots[-1] = -(k0*c0 + k1*c1 + k2*c2)/c3

        grid.knots[-3] = t * grid.knots[-3]  # + (1-t)*0
        grid.knots[-1] = 0
        k0, k1 = grid.knots[-4], grid.knots[-3]
        grid.knots[-2] = -(k0 * c0 + k1 * c1) / c2
Exemplo n.º 20
0
plain color, colormaps and texture.

On the website, this example also demonstrates the fly camera to fly through 
the mesh objects.

"""

import numpy as np
import visvis as vv
from visvis import Point, Pointset
vv.figure()
a = vv.gca()

# Define points for the line
pp = Pointset(3)
pp.append(0,0,0); pp.append(0,1,0); pp.append(1,2,0); pp.append(0,2,1)

# Create all solids
box = vv.solidBox((0,0,0))
sphere = vv.solidSphere((3,0,0))
cone = vv.solidCone((6,0,0))
pyramid = vv.solidCone((9,0,0), N=4) # a cone with 4 faces is a pyramid
cylinder = vv.solidCylinder((0,3,0),(1,1,2))
ring = vv.solidRing((3,3,0))
teapot = vv.solidTeapot((6,3,0))
line = vv.solidLine(pp+Point(9,3,0), radius = 0.2)

# Let's put a face on that cylinder
# This works because 2D texture coordinates are automatically generated for
# the sphere, cone, cylinder and ring. 
im = vv.imread('lena.png')
Exemplo n.º 21
0
class DeformByHand:
    """ DeformByHand(im, grid_sampling=40)
    
    Demo application to deform a 2D image by hand using a spline grid.
    
    Use the grid property to obtain the deformation grid.
    Use the run() method to wait for the user to close the figure.
    
    """
    
    def __init__(self, im, grid_sampling=40):
        
        # Store image
        self._im = im
        
        # Setup visualization
        self._fig = fig = vv.figure()
        self._a1 = a1 = vv.subplot(231);
        self._a2 = a2 = vv.subplot(232);
        self._a3 = a3 = vv.subplot(233);
        self._a4 = a4 = vv.subplot(234);
        self._a5 = a5 = vv.subplot(235);
        self._a6 = a6 = vv.subplot(236);
        
        # Text objects
        self._text1 = vv.Label(fig)
        self._text1.position = 5, 2
        self._text2 = vv.Label(fig)
        self._text2.position = 5, 20
        
        # Move axes 
        a1.parent.position = 0.0, 0.1, 0.33, 0.45
        a2.parent.position = 0.33, 0.1, 0.33, 0.45
        a3.parent.position = 0.66, 0.1, 0.33, 0.45
        a4.parent.position = 0.0, 0.55, 0.33, 0.45
        a5.parent.position = 0.33, 0.55, 0.33, 0.45
        a6.parent.position = 0.66, 0.55, 0.33, 0.45
        
        # Correct axes, share camera
        cam = vv.cameras.TwoDCamera()
        for a in [a1, a2, a3, a4, a5, a6]:
            a.axis.visible = False
            a.camera = cam
        
        # Show images
        im0 = im*0
        self._t1 = vv.imshow(im, axes=a1)
        self._t2 = vv.imshow(im, axes=a2)
        self._t3 = vv.imshow(im, axes=a3)
        self._t4 = vv.imshow(im0, axes=a4)
        self._t5 = vv.imshow(im0, axes=a5)
        self._t6 = vv.imshow(im0, axes=a6)
        
        # Init pointsets
        self._pp1 = Pointset(2)
        self._pp2 = Pointset(2)
        self._active = None
        self._lines = []
        
        # Init lines to show all deformations
        tmp = vv.Pointset(2)        
        self._line1 = vv.plot(tmp, ls='', ms='.', mc='c', axes=a2)
        self._line2 = vv.plot(tmp, ls='+', lc='c', lw='2', axes=a2)
        
        # Init grid properties
        self._sampling = grid_sampling
        self._levels = 5
        self._multiscale = True
        self._injective = 0.5
        self._frozenedge = 1
        self._forward = True
        
        # Init grid
        self.DeformationField = DeformationFieldForward
        self._field1 = self.DeformationField(FieldDescription(self._im))
        self._field2 = self.DeformationField(FieldDescription(self._im))
        
        # Bind to events
        a2.eventMouseDown.Bind(self.on_down)
        a2.eventMouseUp.Bind(self.on_up)
        a2.eventMotion.Bind(self.on_motion)
        fig.eventKeyDown.Bind(self.on_key_down)
        #a1.eventDoubleClick.Bind(self.OnDone)
        
        # Apply
        self.apply()
    
    
    def on_key_down(self, event):
        
        # Update level
        if event.key == vv.KEY_UP:
            self._sampling += 2
        elif event.key == vv.KEY_DOWN:
            self._sampling -= 2
        elif event.key == vv.KEY_RIGHT:
            self._levels += 1
        elif event.key == vv.KEY_LEFT:
            self._levels -= 1
        #
        elif event.text.upper() == 'M':
            self._multiscale = not self._multiscale
        elif event.text.upper() == 'I':
            self._injective += 0.4
            if self._injective > 0.8:
                self._injective = -0.8
        elif event.text.upper() == 'E':
            self._frozenedge = not self._frozenedge
        elif event.text.upper() == 'F':
            self._forward = not self._forward
            # reset global field
            if self._forward:
                self.DeformationField = DeformationFieldForward
            else:
                self.DeformationField = DeformationFieldBackward
            self._field1 = self.DeformationField(FieldDescription(self._im))
        #
        elif event.key == vv.KEY_ESCAPE:
            self._pp1.clear()
            self._pp2.clear()
            self.apply()
        elif event.text == ' ':
            self.apply_deform()
        #
        else:
            return
        
        # Correct
        if self._sampling < 1:
            self._sampling = 1
        
        self.apply()
    
    
    def on_down(self, event):
        
        if event.button != 1:
            return False        
        if not vv.KEY_SHIFT in event.modifiers:
            return False
        
        # Store location
        self._active = Point(event.x2d, event.y2d)
        
        # Clear any line object
        for l in self._lines:
            l.Destroy()
        
        # Create line objects
        tmp = Pointset(2)
        tmp.append(self._active)
        tmp.append(self._active)
        l1 = vv.plot(tmp, lc='g', lw='3', axes=self._a2, axesAdjust=0)
        l2 = vv.plot(tmp[:1], ls='', ms='.', mc='g', axes=self._a2, axesAdjust=0)
        self._lines = [l1, l2]
        
        # Draw
        self._a2.Draw()
        
        # Prevent dragging by indicating the event needs no further handling
        return True
    
    
    def on_motion(self, event):
        if self._active and self._lines:
            # Update line
            tmp = Pointset(2)
            tmp.append(self._active)
            tmp.append(event.x2d, event.y2d)
            l1 = self._lines[0]
            l1.SetPoints(tmp)
            # Draw
            self._a2.Draw()
    
    
    def on_up(self, event):
        
        if self._active is None:
            return False
        
        # Get points
        p1 = self._active
        p2 = Point(event.x2d, event.y2d)
        
        # Add!
        self._pp1.append(p1)
        self._pp2.append(p2)
        
        # We're done with this one
        self._active = None
        
        # Clear any line object
        for l in self._lines:
            l.Destroy()
        
        # Apply
        self.apply()
    
    
    def apply_deform(self):
        # Apply current point-wise deformation
        
        # Compose deformations
        self._field1 = self._field1.compose(self._field2)
        
        # Clear points
        self._pp1.clear()
        self._pp2.clear()
        
        # Update
        self.apply()
    
    
    def apply(self):
        
        # Get sampling
        grid_sampling = self._sampling, self._sampling*2**self._levels
        
        # Init field and deform
        if not self._pp1:
            # Unit deform
            deform = self.DeformationField(FieldDescription(self._im))
        elif self._multiscale:
            deform = self.DeformationField.from_points_multiscale(self._im, grid_sampling, 
                        self._pp1.data, self._pp2.data,
                        injective=self._injective, frozenedge=self._frozenedge)
        else:
            DeformationGrid = DeformationGridForward
            if not self._forward:
                DeformationGrid = DeformationGridBackward
            grid = DeformationGrid.from_points(self._im, self._sampling, 
                        self._pp1.data, self._pp2.data, 
                        injective=self._injective, frozenedge=self._frozenedge)
            deform = grid.as_deformation_field()
        
        # Store grid
        field0 = self.DeformationField(FieldDescription(self._im))
        self._field2 = deform
        field3 = self._field1.compose(self._field2)
        
        # Deform
        im2 = self._field1.apply_deformation(self._im)
        im3 = field3.apply_deformation(self._im)
        
        # Update imagesf
        self._t2.SetData(im2)
        self._t3.SetData(im3)
        
        # Update grids
        for a, field in zip(    [self._a4, self._a5, self._a6],
                                [field0, self._field1, field3] ):
            a.Clear()
            field.show(a, False)
            a.axis.visible = False
        
        # Update lines
        tmp = Pointset(2)
        for i in range(len(self._pp1)):            
            tmp.append(self._pp1[i])
            tmp.append(self._pp2[i])
        self._line1.SetPoints(self._pp1)
        self._line2.SetPoints(tmp)
        
        # Draw
        self._a2.Draw()
        
        # Show text
        text1 = 'B-spline (S) with sampling %i (U/D) and %i levels (L/R).' % (
            self._sampling, self._levels)
        
        text2 = 'Multiscale %i (M), injective %1.1f (I), frozen edge %i (E), forward %i (F).' % (
            self._multiscale, self._injective, self._frozenedge, self._forward)
        
        # Display and apply
        self._text1.text = text1
        self._text2.text = text2
    
    
    @property
    def field(self):
        return self._field1
    
    
    def run(self):
        
        # Setup detecting closing of figure
        self._closed = False
        def callback(event):
            self._closed = True
        self._fig.eventClose.Bind(callback)
        
        while not self._closed:
            time.sleep(0.02)
            vv.processEvents()
        
        self.apply_deform()
Exemplo n.º 22
0
On the website, this example also demonstrates the fly camera to fly through 
the mesh objects.

"""

import numpy as np
import visvis as vv
from visvis import Point, Pointset

vv.figure()
a = vv.gca()

# Define points for the line
pp = Pointset(3)
pp.append(0, 0, 0)
pp.append(0, 1, 0)
pp.append(1, 2, 0)
pp.append(0, 2, 1)

# Create all solids
box = vv.solidBox((0, 0, 0))
sphere = vv.solidSphere((3, 0, 0))
cone = vv.solidCone((6, 0, 0))
pyramid = vv.solidCone((9, 0, 0), N=4)  # a cone with 4 faces is a pyramid
cylinder = vv.solidCylinder((0, 3, 0), (1, 1, 2))
ring = vv.solidRing((3, 3, 0))
teapot = vv.solidTeapot((6, 3, 0))
line = vv.solidLine(pp + Point(9, 3, 0), radius=0.2)

# Let's put a face on that cylinder
Exemplo n.º 23
0
def compareGraphs(graph1, graph2, maxDist):
    """ compareGraphs(graph1, graph2, maxDist)
    Compare two graphs to produce a matching score. Returns a MatchingScore
    instance.
    
    Matching and not-matching edges are counted to obtain a matching
    score. nodes should be closer than maxDist to be considered 'at the 
    same location'.
    """
    
    # Check graphs
    if not graph1 or not graph2:
        print('Warning: one of the graphs to compare is empty.')
        return MatchingScore(0,1,0)
        #raise ValueError('One of the graphs to compare is empty.')
    
    # Create pointsets of the nodes
    pp1 = Pointset(3)
    for node in graph1:
        pp1.append(node)    
    pp2 = Pointset(3)
    for node in graph2:
        pp2.append(node)    
    
    # Match the nodes of graph1 to graph2
    for node in graph1:
        dists = node.distance(pp2)
        i, = np.where(dists==dists.min())
        node.match = None
        if len(i):
            i = int(i[0])
            dist = float(dists[i])
            if dist < maxDist:
                node.match = graph2[i]
#         if not hasattr(node, 'dontCare'):
#             node.dontCare = False
    
    # Match the nodes of graph2 to graph1
    for node in graph2:
        dists = node.distance(pp1)
        i, = np.where(dists==dists.min())
        node.match = None
        if len(i):
            i = int(i[0])
            dist = float(dists[i])
            if dist < maxDist:
                node.match = graph1[i]
#         if not hasattr(node, 'dontCare'):
#             node.dontCare = False
    
    # Init amounts
    nmatch1, nmatch2, nmiss, nwrong = 0,0,0,0
    
    # Count matches and wrongs    
    for c in graph1.GetEdges():        
        end1 = c.end1.match
        end2 = c.end2.match
        if end1 and end2:
            if end1 in end2.GetNeighbours():
                nmatch1 += 1
                continue
#         elif c.end1.dontCare or c.end2.dontCare:
#             continue
        nwrong += 1
    
    # todo: use dontCare 'beleid' or not?
    
    # Count matches and misses
    for c in graph2.GetEdges():        
        end1 = c.end1.match
        end2 = c.end2.match
        if end1 and end2:
            if end1 in end2.GetNeighbours():
                nmatch2 += 1
                continue
#         elif c.end1.dontCare or c.end2.dontCare:
#             continue
        nmiss += 1
    
    # Compose score
    return MatchingScore(nmatch1, nmiss, nwrong)
Exemplo n.º 24
0
class Drawer:
    
    def __init__(self):
        
        # Create figure and axes
        vv.figure()
        self._a = a = vv.gca()
        vv.title("Hold mouse to draw lines. Use 'rgbcmyk' and '1-9' keys.")
        
        # Set axes
        a.SetLimits((0,1), (0,1))
        a.cameraType = '2d'
        a.daspectAuto = False
        a.axis.showGrid = True
        
        # Init variables needed during drawing
        self._active = None
        self._pp = Pointset(2)
        
        # Create null and empty line objects
        self._line1 = None
        self._line2 = vv.plot(vv.Pointset(2), ls='+', lc='c', lw='2', axes=a)
        
        # Bind to events
        a.eventMouseDown.Bind(self.OnDown)
        a.eventMouseUp.Bind(self.OnUp)
        a.eventMotion.Bind(self.OnMotion)
        a.eventKeyDown.Bind(self.OnKey)
    
    
    def OnKey(self, event):
        """ Called when a key is pressed down in the axes.
        """
        
        if event.text and event.text.lower() in 'rgbcmywk':
            self._line2.lc = event.text.lower()
        else:
            try:
                tickness = int(event.text)
            except Exception:
                tickness = 0
            if tickness > 0:
                self._line2.lw = tickness
    
    
    def OnDown(self, event):
        """ Called when the mouse is pressed down in the axes.
        """
        
        # Only process left mouse button
        if event.button != 1:
            return False
        
        # Store location
        self._active = Point(event.x2d, event.y2d)
        
        # Clear temp line object
        if self._line1:
            self._line1.Destroy()
        
        # Create line objects
        tmp = Pointset(2)
        tmp.append(self._active)
        tmp.append(self._active)
        self._line1 = vv.plot(tmp, lc='r', lw='1', axes=self._a, axesAdjust=0)
        
        # Draw
        self._a.Draw()
        
        # Prevent dragging by indicating the event needs no further handling
        return True

    
    def OnMotion(self, event):
        """ Called when the mouse is moved in the axes.
        """
        
        if self._active and self._line1:
            # Update line
            tmp = Pointset(2)
            tmp.append(self._active)
            tmp.append(event.x2d, event.y2d)
            self._line1.SetPoints(tmp)
            # Draw
            self._a.Draw()
    
    
    def OnUp(self, event):
        """ Called when the mouse is released (when first pressed down 
        in the axes).
        """
        
        # Only if a point is active
        if self._active is None:
            return False
        
        # Get points
        p1 = self._active
        p2 = Point(event.x2d, event.y2d)
        
        # Add!
        self._pp.append(p1)
        self._pp.append(p2)
        
        # We're done with this one
        self._active = None
        
        # Clear temp line object
        if self._line1:
            self._line1.Destroy()
            self._line1 = None
        
        # Update lines
        self._line2.SetPoints(self._pp)
        
        # Draw
        self._a.Draw()
Exemplo n.º 25
0
def compareGraphs(graph1, graph2, maxDist):
    """ compareGraphs(graph1, graph2, maxDist)
    Compare two graphs to produce a matching score. Returns a MatchingScore
    instance.
    
    Matching and not-matching edges are counted to obtain a matching
    score. nodes should be closer than maxDist to be considered 'at the 
    same location'.
    """
    
    # Check graphs
    if not graph1 or not graph2:
        print('Warning: one of the graphs to compare is empty.')
        return MatchingScore(0,1,0)
        #raise ValueError('One of the graphs to compare is empty.')
    
    # Create pointsets of the nodes
    pp1 = Pointset(3)
    for node in graph1:
        pp1.append(node)    
    pp2 = Pointset(3)
    for node in graph2:
        pp2.append(node)    
    
    # Match the nodes of graph1 to graph2
    for node in graph1:
        dists = node.distance(pp2)
        i, = np.where(dists==dists.min())
        node.match = None
        if len(i):
            i = int(i[0])
            dist = float(dists[i])
            if dist < maxDist:
                node.match = graph2[i]
#         if not hasattr(node, 'dontCare'):
#             node.dontCare = False
    
    # Match the nodes of graph2 to graph1
    for node in graph2:
        dists = node.distance(pp1)
        i, = np.where(dists==dists.min())
        node.match = None
        if len(i):
            i = int(i[0])
            dist = float(dists[i])
            if dist < maxDist:
                node.match = graph1[i]
#         if not hasattr(node, 'dontCare'):
#             node.dontCare = False
    
    # Init amounts
    nmatch1, nmatch2, nmiss, nwrong = 0,0,0,0
    
    # Count matches and wrongs    
    for c in graph1.GetEdges():        
        end1 = c.end1.match
        end2 = c.end2.match
        if end1 and end2:
            if end1 in end2.GetNeighbours():
                nmatch1 += 1
                continue
#         elif c.end1.dontCare or c.end2.dontCare:
#             continue
        nwrong += 1
    
    # todo: use dontCare 'beleid' or not?
    
    # Count matches and misses
    for c in graph2.GetEdges():        
        end1 = c.end1.match
        end2 = c.end2.match
        if end1 and end2:
            if end1 in end2.GetNeighbours():
                nmatch2 += 1
                continue
#         elif c.end1.dontCare or c.end2.dontCare:
#             continue
        nmiss += 1
    
    # Compose score
    return MatchingScore(nmatch1, nmiss, nwrong)