This file is indexed.

/usr/lib/python2.7/dist-packages/dipy/viz/window.py is in python-dipy 0.10.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
from __future__ import division, print_function, absolute_import

import numpy as np
from scipy import ndimage
from copy import copy

try:
    import Tkinter as tkinter
    has_tkinter = True
except ImportError:
    try:
        import tkinter
        has_tkinter = True
    except ImportError:
        has_tkinter = False

try:
    import tkFileDialog as filedialog
except ImportError:
    try:
        from tkinter import filedialog
    except ImportError:
        has_tkinter = False

# Conditional import machinery for vtk
from dipy.utils.optpkg import optional_package

from dipy import __version__ as dipy_version
from dipy.utils.six import string_types


# import vtk
# Allow import, but disable doctests if we don't have vtk
vtk, have_vtk, setup_module = optional_package('vtk')
colors, have_vtk_colors, _ = optional_package('vtk.util.colors')
numpy_support, have_ns, _ = optional_package('vtk.util.numpy_support')
_, have_imread, _ = optional_package('Image')

if have_vtk:
    version = vtk.vtkVersion.GetVTKSourceVersion().split(' ')[-1]
    major_version = vtk.vtkVersion.GetVTKMajorVersion()
    from vtk.util.numpy_support import vtk_to_numpy
    vtkRenderer = vtk.vtkRenderer
else:
    vtkRenderer = object

if have_imread:
    from scipy.misc import imread


class Renderer(vtkRenderer):
    """ Your scene class

    This is an important object that is responsible for preparing objects
    e.g. actors and volumes for rendering. This is a more pythonic version
    of ``vtkRenderer`` proving simple methods for adding and removing actors
    but also it provides access to all the functionality
    available in ``vtkRenderer`` if necessary.
    """

    def background(self, color):
        """ Set a background color
        """
        self.SetBackground(color)

    def add(self, actor):
        """ Add an actor to the renderer
        """
        if isinstance(actor, vtk.vtkVolume):
            self.AddVolume(actor)
        else:
            self.AddActor(actor)

    def rm(self, actor):
        """ Remove a specific actor
        """
        self.RemoveActor(actor)

    def clear(self):
        """ Remove all actors from the renderer
        """
        self.RemoveAllViewProps()

    def rm_all(self):
        """ Remove all actors from the renderer
        """
        self.RemoveAllViewProps()

    def projection(self, proj_type='perspective'):
        """ Deside between parallel or perspective projection

        Parameters
        ----------
        proj_type : str
            Can be 'parallel' or 'perspective' (default).

        """
        if proj_type == 'parallel':
            self.GetActiveCamera().ParallelProjectionOn()
        else:
            self.GetActiveCamera().ParallelProjectionOff()

    def reset_camera(self):
        """ Reset the camera to an automatic position given by the engine.
        """
        self.ResetCamera()

    def reset_clipping_range(self):
        self.ResetCameraClippingRange()

    def camera(self):
        return self.GetActiveCamera()

    def get_camera(self):
        cam = self.GetActiveCamera()
        return cam.GetPosition(), cam.GetFocalPoint(), cam.GetViewUp()

    def camera_info(self):
        cam = self.camera()
        print('# Active Camera')
        print('   Position (%.2f, %.2f, %.2f)' % cam.GetPosition())
        print('   Focal Point (%.2f, %.2f, %.2f)' % cam.GetFocalPoint())
        print('   View Up (%.2f, %.2f, %.2f)' % cam.GetViewUp())

    def set_camera(self, position=None, focal_point=None, view_up=None):
        if position is not None:
            self.GetActiveCamera().SetPosition(*position)
        if focal_point is not None:
            self.GetActiveCamera().SetFocalPoint(*focal_point)
        if view_up is not None:
            self.GetActiveCamera().SetViewUp(*view_up)
        self.ResetCameraClippingRange()

    def size(self):
        """ Renderer size"""
        return self.GetSize()

    def zoom(self, value):
        """ In perspective mode, decrease the view angle by the specified
        factor. In parallel mode, decrease the parallel scale by the specified
        factor. A value greater than 1 is a zoom-in, a value less than 1 is a
        zoom-out.
        """
        self.GetActiveCamera().Zoom(value)

    def azimuth(self, angle):
        """ Rotate the camera about the view up vector centered at the focal
        point. Note that the view up vector is whatever was set via SetViewUp,
        and is not necessarily perpendicular to the direction of projection.
        The result is a horizontal rotation of the camera.
        """
        self.GetActiveCamera().Azimuth(angle)

    def yaw(self, angle):
        """ Rotate the focal point about the view up vector, using the camera's
        position as the center of rotation. Note that the view up vector is
        whatever was set via SetViewUp, and is not necessarily perpendicular
        to the direction of projection. The result is a horizontal rotation of
        the scene.
        """
        self.GetActiveCamera().Yaw(angle)

    def elevation(self, angle):
        """ Rotate the camera about the cross product of the negative of the
        direction of projection and the view up vector, using the focal point
        as the center of rotation. The result is a vertical rotation of the
        scene.
        """
        self.GetActiveCamera().Elevation(angle)

    def pitch(self, angle):
        """ Rotate the focal point about the cross product of the view up
        vector and the direction of projection, using the camera's position as
        the center of rotation. The result is a vertical rotation of the
        camera.
        """
        self.GetActiveCamera().Pitch(angle)

    def roll(self, angle):
        """ Rotate the camera about the direction of projection. This will
        spin the camera about its axis.
        """
        self.GetActiveCamera().Roll(angle)

    def dolly(self, value):
        """ Divide the camera's distance from the focal point by the given
        dolly value. Use a value greater than one to dolly-in toward the focal
        point, and use a value less than one to dolly-out away from the focal
        point.
        """
        self.GetActiveCamera().Dolly(value)

    def camera_direction(self):
        """ Get the vector in the direction from the camera position to the
        focal point. This is usually the opposite of the ViewPlaneNormal, the
        vector perpendicular to the screen, unless the view is oblique.
        """
        return self.GetActiveCamera().GetDirectionOfProjection()


def renderer(background=None):
    """ Create a renderer.

    Parameters
    ----------
    background : tuple
        Initial background color of renderer

    Returns
    -------
    v : Renderer

    Examples
    --------
    >>> from dipy.viz import fvtk
    >>> import numpy as np
    >>> r=fvtk.ren()
    >>> lines=[np.random.rand(10,3)]
    >>> c=fvtk.line(lines, fvtk.colors.red)
    >>> fvtk.add(r,c)
    >>> #fvtk.show(r)
    """
    ren = Renderer()
    if background is not None:
        ren.SetBackground(background)

    return ren

if have_vtk:
    ren = renderer


def add(ren, a):
    """ Add a specific actor
    """
    ren.add(a)


def rm(ren, a):
    """ Remove a specific actor
    """
    ren.rm(a)


def clear(ren):
    """ Remove all actors from the renderer
    """
    ren.clear()


def rm_all(ren):
    """ Remove all actors from the renderer
    """
    ren.rm_all()


def open_file_dialog(file_types=[("All files", "*")]):
    """ Simple Tk file dialog for opening files

    Parameters
    ----------
    file_types : tuples of tuples
        Accepted file types.

    Returns
    -------
    file_paths : sequence of str
        Returns the full paths of all selected files
    """

    root = tkinter.Tk()
    root.withdraw()
    file_paths = filedialog.askopenfilenames(filetypes=file_types)
    return file_paths


def save_file_dialog(initial_file='dipy.png', default_ext='.png',
                     file_types=(("PNG file", "*.png"), ("All Files", "*.*"))):
    """ Simple Tk file dialog for saving a file

    Parameters
    ----------
    initial_file : str
        For example ``dipy.png``.
    default_ext : str
        Default extension to appear in the save dialog.
    file_types : tuples of tuples
        Accepted file types.

    Returns
    -------
    filepath : str
        Complete filename of saved file
    """

    root = tkinter.Tk()
    root.withdraw()
    file_path = filedialog.asksaveasfilename(initialfile=initial_file,
                                             defaultextension=default_ext,
                                             filetypes=file_types)
    return file_path


class ShowManager(object):
    """ This class is the interface between the renderer, the window and the
    interactor.
    """

    def __init__(self, ren, title='DIPY', size=(300, 300),
                 png_magnify=1, reset_camera=True, order_transparent=False,
                 interactor_style='trackball'):

        """ Manages the visualization pipeline

        Parameters
        ----------
        ren : Renderer() or vtkRenderer()
            The scene that holds all the actors.
        title : string
            A string for the window title bar.
        size : (int, int)
            ``(width, height)`` of the window. Default is (300, 300).
        png_magnify : int
            Number of times to magnify the screenshot. This can be used to save
            high resolution screenshots when pressing 's' inside the window.
        reset_camera : bool
            Default is True. You can change this option to False if you want to
            keep the camera as set before calling this function.
        order_transparent : bool
            True is useful when you want to order transparent
            actors according to their relative position to the camera. The
            default option which is False will order the actors according to
            the order of their addition to the Renderer().
        interactor_style : str or vtkInteractorStyle
            If str then if 'trackball' then vtkInteractorStyleTrackballCamera()
            is used or if 'image' then vtkInteractorStyleImage() is used (no
            rotation). Otherwise you can input your own interactor style.

        Attributes
        ----------
        ren : vtkRenderer()
        iren : vtkRenderWindowInteractor()
        style : vtkInteractorStyle()
        window : vtkRenderWindow()

        Methods
        -------
        initialize()
        render()
        start()
        add_window_callback()

        Notes
        -----
        Default interaction keys for

        * 3d navigation are with left, middle and right mouse dragging
        * resetting the camera press 'r'
        * saving a screenshot press 's'
        * for quiting press 'q'

        Examples
        --------
        >>> from dipy.viz import actor, window
        >>> renderer = window.Renderer()
        >>> renderer.add(actor.axes())
        >>> showm = window.ShowManager(renderer)
        >>> # showm.initialize()
        >>> # showm.render()
        >>> # showm.start()
        """

        self.ren = ren
        self.title = title
        self.size = size
        self.png_magnify = png_magnify
        self.reset_camera = reset_camera
        self.order_transparent = order_transparent
        self.interactor_style = interactor_style

        if self.reset_camera:
            self.ren.ResetCamera()

        self.window = vtk.vtkRenderWindow()
        self.window.AddRenderer(ren)

        if self.title == 'DIPY':
            self.window.SetWindowName(title + ' ' + dipy_version)
        else:
            self.window.SetWindowName(title)
        self.window.SetSize(size[0], size[1])

        if self.order_transparent:

            # Use a render window with alpha bits
            # as default is 0 (false))
            self.window.SetAlphaBitPlanes(True)

            # Force to not pick a framebuffer with a multisample buffer
            # (default is 8)
            self.window.SetMultiSamples(0)

            # Choose to use depth peeling (if supported)
            # (default is 0 (false)):
            self.ren.UseDepthPeelingOn()

            # Set depth peeling parameters
            # Set the maximum number of rendering passes (default is 4)
            ren.SetMaximumNumberOfPeels(4)

            # Set the occlusion ratio (initial value is 0.0, exact image):
            ren.SetOcclusionRatio(0.0)

        if self.interactor_style == 'image':
            self.style = vtk.vtkInteractorStyleImage()
        elif self.interactor_style == 'trackball':
            self.style = vtk.vtkInteractorStyleTrackballCamera()
        else:
            self.style = interactor_style

        self.iren = vtk.vtkRenderWindowInteractor()
        self.iren.SetRenderWindow(self.window)

        def key_press_standard(obj, event):

            key = obj.GetKeySym()
            if key == 's' or key == 'S':
                print('Saving image...')
                renderLarge = vtk.vtkRenderLargeImage()
                if major_version <= 5:
                    renderLarge.SetInput(ren)
                else:
                    renderLarge.SetInput(ren)
                renderLarge.SetMagnification(png_magnify)
                renderLarge.Update()

                file_types = (("PNG file", "*.png"), ("All Files", "*.*"))
                filepath = save_file_dialog(initial_file='dipy.png',
                                            default_ext='.png',
                                            file_types=file_types)
                if filepath == '':
                    print('No file was provided in the dialog')
                else:
                    writer = vtk.vtkPNGWriter()
                    writer.SetInputConnection(renderLarge.GetOutputPort())
                    writer.SetFileName(filepath)
                    writer.Write()
                    print('File ' + filepath + ' is saved.')

        self.iren.AddObserver('KeyPressEvent', key_press_standard)
        self.iren.SetInteractorStyle(self.style)

    def initialize(self):
        """ Initialize interaction
        """
        self.iren.Initialize()
        # picker.Pick(85, 126, 0, ren)

    def render(self):
        """ Renders only once
        """
        self.window.Render()

    def start(self):
        """ Starts interaction
        """
        try:
            self.iren.Start()
        except AttributeError:
            self.__init__(self.ren, self.title, size=self.size,
                          png_magnify=self.png_magnify,
                          reset_camera=self.reset_camera,
                          order_transparent=self.order_transparent,
                          interactor_style=self.interactor_style)
            self.initialize()
            self.render()
            self.iren.Start()

        # window.RemoveAllObservers()
        # ren.SetRenderWindow(None)

        self.window.RemoveRenderer(self.ren)
        self.ren.SetRenderWindow(None)
        del self.iren
        del self.window

    def add_window_callback(self, win_callback):
        """ Add window callbacks
        """
        self.window.AddObserver(vtk.vtkCommand.ModifiedEvent, win_callback)
        self.window.Render()


def show(ren, title='DIPY', size=(300, 300),
         png_magnify=1, reset_camera=True, order_transparent=False):
    """ Show window with current renderer

    Parameters
    ------------
    ren : Renderer() or vtkRenderer()
        The scene that holds all the actors.
    title : string
        A string for the window title bar. Default is DIPY and current version.
    size : (int, int)
        ``(width, height)`` of the window. Default is (300, 300).
    png_magnify : int
        Number of times to magnify the screenshot. Default is 1. This can be
        used to save high resolution screenshots when pressing 's' inside the
        window.
    reset_camera : bool
        Default is True. You can change this option to False if you want to
        keep the camera as set before calling this function.
    order_transparent : bool
        True is useful when you want to order transparent
        actors according to their relative position to the camera. The default
        option which is False will order the actors according to the order of
        their addition to the Renderer().

    Notes
    -----
    Default interaction keys for

    * 3d navigation are with left, middle and right mouse dragging
    * resetting the camera press 'r'
    * saving a screenshot press 's'
    * for quiting press 'q'

    Examples
    ----------
    >>> import numpy as np
    >>> from dipy.viz import fvtk
    >>> r=fvtk.ren()
    >>> lines=[np.random.rand(10,3),np.random.rand(20,3)]
    >>> colors=np.array([[0.2,0.2,0.2],[0.8,0.8,0.8]])
    >>> c=fvtk.line(lines,colors)
    >>> fvtk.add(r,c)
    >>> l=fvtk.label(r)
    >>> fvtk.add(r,l)
    >>> #fvtk.show(r)

    See also
    ---------
    dipy.viz.window.record
    dipy.viz.window.snapshot
    """

    show_manager = ShowManager(ren, title, size,
                               png_magnify, reset_camera, order_transparent)
    show_manager.initialize()
    show_manager.render()
    show_manager.start()


def record(ren=None, cam_pos=None, cam_focal=None, cam_view=None,
           out_path=None, path_numbering=False, n_frames=1, az_ang=10,
           magnification=1, size=(300, 300), reset_camera=True, verbose=False):
    """ This will record a video of your scene

    Records a video as a series of ``.png`` files of your scene by rotating the
    azimuth angle az_angle in every frame.

    Parameters
    -----------
    ren : vtkRenderer() object
        as returned from function ren()
    cam_pos : None or sequence (3,), optional
        Camera's position. If None then default camera's position is used.
    cam_focal : None or sequence (3,), optional
        Camera's focal point. If None then default camera's focal point is
        used.
    cam_view : None or sequence (3,), optional
        Camera's view up direction. If None then default camera's view up
        vector is used.
    out_path : str, optional
        Output path for the frames. If None a default dipy.png is created.
    path_numbering : bool
        When recording it changes out_path to out_path + str(frame number)
    n_frames : int, optional
        Number of frames to save, default 1
    az_ang : float, optional
        Azimuthal angle of camera rotation.
    magnification : int, optional
        How much to magnify the saved frame. Default is 1.
    size : (int, int)
        ``(width, height)`` of the window. Default is (300, 300).
    reset_camera : bool
        If True Call ``ren.reset_camera()``. Otherwise you need to set the
         camera before calling this function.
    verbose : bool
        print information about the camera. Default is False.


    Examples
    ---------
    >>> from dipy.viz import fvtk
    >>> r=fvtk.ren()
    >>> a=fvtk.axes()
    >>> fvtk.add(r,a)
    >>> #uncomment below to record
    >>> #fvtk.record(r)
    >>> #check for new images in current directory
    """

    if ren is None:
        ren = vtk.vtkRenderer()

    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    renWin.SetSize(size[0], size[1])
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    # ren.GetActiveCamera().Azimuth(180)

    if reset_camera:
        ren.ResetCamera()

    renderLarge = vtk.vtkRenderLargeImage()
    if major_version <= 5:
        renderLarge.SetInput(ren)
    else:
        renderLarge.SetInput(ren)
    renderLarge.SetMagnification(magnification)
    renderLarge.Update()

    writer = vtk.vtkPNGWriter()
    ang = 0

    if cam_pos is not None:
        cx, cy, cz = cam_pos
        ren.GetActiveCamera().SetPosition(cx, cy, cz)
    if cam_focal is not None:
        fx, fy, fz = cam_focal
        ren.GetActiveCamera().SetFocalPoint(fx, fy, fz)
    if cam_view is not None:
        ux, uy, uz = cam_view
        ren.GetActiveCamera().SetViewUp(ux, uy, uz)

    cam = ren.GetActiveCamera()
    if verbose:
        print('Camera Position (%.2f, %.2f, %.2f)' % cam.GetPosition())
        print('Camera Focal Point (%.2f, %.2f, %.2f)' % cam.GetFocalPoint())
        print('Camera View Up (%.2f, %.2f, %.2f)' % cam.GetViewUp())

    for i in range(n_frames):
        ren.GetActiveCamera().Azimuth(ang)
        renderLarge = vtk.vtkRenderLargeImage()
        renderLarge.SetInput(ren)
        renderLarge.SetMagnification(magnification)
        renderLarge.Update()
        writer.SetInputConnection(renderLarge.GetOutputPort())

        if path_numbering:
            if out_path is None:
                filename = str(i).zfill(6) + '.png'
            else:
                filename = out_path + str(i).zfill(6) + '.png'
        else:
            if out_path is None:
                filename = 'dipy.png'
            else:
                filename = out_path
        writer.SetFileName(filename)
        writer.Write()

        ang = +az_ang


def snapshot(ren, fname=None, size=(300, 300), offscreen=True,
             order_transparent=False):
    """ Saves a snapshot of the renderer in a file or in memory

    Parameters
    -----------
    ren : vtkRenderer
        as returned from function renderer()
    fname : str or None
        Save PNG file. If None return only an array without saving PNG.
    size : (int, int)
        ``(width, height)`` of the window. Default is (300, 300).
    offscreen : bool
        Default True. Go stealthmode no window should appear.
    order_transparent : bool
        Default False. Use depth peeling to sort transparent objects.

    Returns
    -------
    arr : ndarray
        Color array of size (width, height, 3) where the last dimension
        holds the RGB values.
    """

    width, height = size

    if offscreen:
        graphics_factory = vtk.vtkGraphicsFactory()
        graphics_factory.SetOffScreenOnlyMode(1)
        # TODO check if the line below helps in something
        # graphics_factory.SetUseMesaClasses(1)

    render_window = vtk.vtkRenderWindow()
    if offscreen:
        render_window.SetOffScreenRendering(1)
    render_window.AddRenderer(ren)
    render_window.SetSize(width, height)

    if order_transparent:

        # Use a render window with alpha bits
        # as default is 0 (false))
        render_window.SetAlphaBitPlanes(True)

        # Force to not pick a framebuffer with a multisample buffer
        # (default is 8)
        render_window.SetMultiSamples(0)

        # Choose to use depth peeling (if supported)
        # (default is 0 (false)):
        ren.UseDepthPeelingOn()

        # Set depth peeling parameters
        # Set the maximum number of rendering passes (default is 4)
        ren.SetMaximumNumberOfPeels(4)

        # Set the occlusion ratio (initial value is 0.0, exact image):
        ren.SetOcclusionRatio(0.0)

    render_window.Render()

    window_to_image_filter = vtk.vtkWindowToImageFilter()
    window_to_image_filter.SetInput(render_window)
    window_to_image_filter.Update()

    vtk_image = window_to_image_filter.GetOutput()
    h, w, _ = vtk_image.GetDimensions()
    vtk_array = vtk_image.GetPointData().GetScalars()
    components = vtk_array.GetNumberOfComponents()
    arr = vtk_to_numpy(vtk_array).reshape(h, w, components)

    if fname is None:
        return arr

    writer = vtk.vtkPNGWriter()
    writer.SetFileName(fname)
    writer.SetInputConnection(window_to_image_filter.GetOutputPort())
    writer.Write()
    return arr


def analyze_renderer(ren):

    class ReportRenderer(object):
        bg_color = None

    report = ReportRenderer()

    report.bg_color = ren.GetBackground()
    report.collection = ren.GetActors()
    report.actors = report.collection.GetNumberOfItems()

    report.collection.InitTraversal()
    report.actors_classnames = []
    for i in range(report.actors):
        class_name = report.collection.GetNextActor().GetClassName()
        report.actors_classnames.append(class_name)

    return report


def analyze_snapshot(im, bg_color=(0, 0, 0), colors=None,
                     find_objects=True,
                     strel=None):
    """ Analyze snapshot from memory or file

    Parameters
    ----------
    im: str or array
        If string then the image is read from a file otherwise the image is
        read from a numpy array. The array is expected to be of shape (X, Y, 3)
        where the last dimensions are the RGB values.
    colors: tuple (3,) or list of tuples (3,)
        List of colors to search in the image
    find_objects: bool
        If True it will calculate the number of objects that are different
        from the background and return their position in a new image.
    strel: 2d array
        Structure element to use for finding the objects.

    Returns
    -------
    report : ReportSnapshot
        This is an object with attibutes like ``colors_found`` that give
        information about what was found in the current snapshot array ``im``.

    """
    if isinstance(im, string_types):
        im = imread(im)

    class ReportSnapshot(object):
        objects = None
        labels = None
        colors_found = False

    report = ReportSnapshot()

    if colors is not None:
        if isinstance(colors, tuple):
            colors = [colors]
        flags = [False] * len(colors)
        for (i, col) in enumerate(colors):
            # find if the current color exist in the array
            flags[i] = np.any(np.all(im == col, axis=-1))

        report.colors_found = flags

    if find_objects is True:
        weights = [0.299, 0.587, 0.144]
        gray = np.dot(im[..., :3], weights)
        bg_color = im[0, 0]
        background = np.dot(bg_color, weights)

        if strel is None:
            strel = np.array([[0, 1, 0],
                              [1, 1, 1],
                              [0, 1, 0]])

        labels, objects = ndimage.label(gray != background, strel)
        report.labels = labels
        report.objects = objects

    return report