a
    «ˆc<·  ã                   @   s¦  d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZ d	Zd
d„ Zdadd„Zdd„ Zdd„ Zdd„ Zdg d¢d	fdd„Zdbdd„Zdcdd„ZG dd„ deƒZ G dd„ de ƒZ!G d d!„ d!e!ƒZ"G d"d#„ d#e!ƒZ#G d$d%„ d%e!ƒZ$G d&d'„ d'e!ƒZ%G d(d)„ d)e!ƒZ&G d*d+„ d+e!ƒZ'G d,d-„ d-e ƒZ(G d.d/„ d/e ƒZ)G d0d1„ d1e ƒZ*G d2d3„ d3e ƒZ+G d4d5„ d5e+ƒZ,G d6d7„ d7e+ƒZ-G d8d9„ d9e+ƒZ.G d:d;„ d;e+ƒZ/G d<d=„ d=e.ƒZ0G d>d?„ d?e/ƒZ1G d@dA„ dAe+ƒZ2G dBdC„ dCe+ƒZ3G dDdE„ dEe+ƒZ4G dFdG„ dGeƒZ5dddHdI„Z6dJdK„ Z7dLdM„ Z8dNdO„ Z9dPdQ„ Z:dedRdS„Z;dTdU„ Z<dVdW„ Z=dXdY„ Z>dZd[„ Z?d\d]„ Z@d^d_„ ZAeBd`k�r¢eAƒ  dS )fa�  A module that provides Matlab like 3d visualization functionality.

The general idea is shamelessly stolen from the `high-level API`_
provided by Octaviz_. Some of the test cases and demos are also
translated from there!

.. _Octaviz: http://octaviz.sourceforge.net/
.. _high-level API: http://octaviz.sourceforge.net/index.php?page=manpagesq

The implementation provided here is object oriented and each
visualization capability is implemented as a class that has traits.
So each of these may be configured.  Each visualization class derives
(ultimately) from MLabBase which is responsible for adding/removing
its actors into the render window.  The classes all require that the
RenderWindow be a `pyface.tvtk.scene.Scene` instance (this constraint
can be relaxed if necessary later on).

This module offers the following broad class of functionality:

`Figure`
  This basically manages all of the objects rendered.  Just like
  figure in any Matlab like environment.  A convenience function
  called `figure` may be used to create a nice Figure instance.

`Glyphs`
  This and its subclasses let one place glyphs at points specified as
  inputs.  The subclasses are: `Arrows`, `Cones`, `Cubes`,
  `Cylinders`, `Spheres`, and `Points`.

`Line3`
  Draws lines between the points specified at initialization time.

`Outline`
  Draws an outline for the contained objects.

`Title`
  Draws a title for the entire figure.

`LUTBase`
  Manages a lookup table and a scalar bar (legend) for it.  This is
  subclassed by all classes that need a LUT.

`SurfRegular`
  MayaVi1's imv.surf like functionality that plots surfaces given x
  (1D), y(1D) and z (or a callable) arrays.

`SurfRegularC`
  Also plots contour lines.

`TriMesh`
  Given triangle connectivity and points, plots a mesh of them.

`FancyTriMesh`
  Plots the mesh using tubes and spheres so its fancier.

`Mesh`
  Given x, y generated from numpy.mgrid, and a z to go with it.  Along
  with optional scalars.  This class builds the triangle connectivity
  (assuming that x, y are from numpy.mgrid) and builds a mesh and
  shows it.

`FancyMesh`
  Like mesh but shows the mesh using tubes and spheres.

`Surf`
  This generates a surface mesh just like Mesh but renders the mesh as
  a surface.

`Contour3`
  Shows contour for a mesh.

`ImShow`
  Allows one to view large numeric arrays as image data using an image
  actor.  This is just like MayaVi1's `mayavi.tools.imv.viewi`.

To see nice examples of all of these look at the `test_*` functions at
the end of this file.  Here is a quick example that uses these test
functions::

 >>> from tvtk.tools import mlab
 >>> f = mlab.figure()
 >>> mlab.test_surf(f) # Create a spherical harmonic.
 >>> f.pop() # Remove it.
 >>> mlab.test_molecule(f) # Show a caffeine molecule.
 >>> f.renwin.reset_zoom() # Scale the view.
 >>> f.pop() # Remove this.
 >>> mlab.test_lines(f) # Show pretty lines.
 >>> f.clear() # Remove all the stuff on screen.

é    N)	Ú	HasTraitsÚListÚInstanceÚAnyÚFloatÚBoolÚStrÚTraitÚInt)ÚGUI)Útvtk)ÚTVTKBaseÚvtk_color_trait)Úconfigure_input_data)ÚivtkTc                  K   s@   t r,tjdd�}d|j_|jf i | ¤Ž |S tjf i | ¤ŽS dS )zyReturn a TVTK actor.  If `mlab.USE_LOD_ACTOR` is `True` it
    returns an LODActor if not it returns a normal actor.
    iÜ  )Znumber_of_cloud_pointsg       @N)ÚUSE_LOD_ACTORr   ZLODActorÚpropertyZ
point_sizeÚ	trait_setZActor)ÚkwargsÚr© r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/tools/mlab.pyÚ_make_actorq   s    r   c                 C   sº   t | ƒ}t |ƒ}|dur@t |¡}|| |ks@J d|||f ƒ‚| d |d  }}| d | d  |d |d   }}	tj||df||df||	dfd�}
|dur¶t |¡|
j_d|
jj_|
S )a0  Creates a StructuredPoints object given input data in the form
    of numpy arrays.

    Input Arguments:
       x -- Array of x-coordinates.  These should be regularly spaced.

       y -- Array of y-coordinates.  These should be regularly spaced.

       z -- Array of z values for the x, y values given.  The values
       should be computed such that the z values are computed as x
       varies fastest and y next.  If z is None then no scalars are
       associated with the structured points.  Only the structured
       points data set is created.
    Nz6len(x)*len(y) != len(z)You passed nx=%d, ny=%d,  nz=%dr   é   )Z
dimensionsÚoriginÚspacingÚscalars)	ÚlenÚnumpyÚsizer   ZStructuredPointsÚravelÚ
point_datar   Úname)ÚxÚyÚzÚnxÚnyZnzZxminZyminÚdxÚdyÚspr   r   r   Ú _create_structured_points_direct~   s"    
ÿ"þ
r+   c                 O   sT   || dd…df t  |¡ t  |dd…df t  | ¡ ¡g|¢R i |¤Ž}t  |¡S )a  Samples a function at an array of ordered points (with equal
    spacing) and returns an array of scalars as per VTK's requirements
    for a structured points data set, i.e. x varying fastest and y
    varying next.

    Input Arguments:
        xa -- Array of x points.

        ya -- Array if y points.

        func -- function of x, and y to sample.

        args -- additional positional arguments for func()
        (default is empty)

        kwargs -- a dict of additional keyword arguments for func()
        (default is empty)
    N)r   Z
zeros_likeZ	transpose)ÚxaÚyaÚfuncÚargsr   Úretr   r   r   Úsampler¡   s    ÿþþr1   c                 C   s´   dt |jƒ }t|jƒdks$J |ƒ‚t|jƒdkr°dt| ƒt|ƒ|jf }|jd |jd  t| ƒt|ƒ ksvJ |ƒ‚dt|ƒt| ƒt |jƒf }|jt|ƒt| ƒfks°J |ƒ‚dS )zAChecks the given arrays to see if they are suitable for
    surf.zAOnly ravelled or 2D arrays can be viewed! This array has shape %sé   zElen(x)*len(y) != len(z.flat).  You passed nx=%d, ny=%d, shape of z=%sr   r   zUlength of y(%d) and x(%d) must match shape of z %s. (Maybe you need to swap x and y?)N)ÚstrÚshaper   )r#   r$   r%   Úmsgr   r   r   Ú_check_sanity»   s    ÿÿ,ÿÿr6   c              
   C   s:   t  | ¡} t  | j¡}t  | tt  t  |d¡|¡ƒ¡}|S )z3Returns a with any ones from the shape of a removedr   )r   Úasarrayr4   ÚreshapeÚtupleÚcompressÚ	not_equal)ÚaÚbÚvalr   r   r   ÚsqueezeÍ   s    
ÿr?   r   ©ç      ð?rA   rA   c                 O   sJ  t |ƒr<t t| ||g|¢R i |¤Ž¡}t| ƒt|ƒ } }n<t| ƒt|ƒ } }t| ||ƒ t |¡}t|ƒdksxJ dƒ‚| |d  }	||d  }
t|	|
|ƒ}|s¤|S |�rt 	¡ }t
||ƒ tj|d d�}t
||jƒ tjdd�}t
||jƒ tjt|ƒt|ƒfd�}t
||jƒ n"tjt|ƒt|ƒfd�}t
||ƒ t|d	�}||fS )
aµ  Creates a surface given regularly spaced values of x, y and the
    corresponding z as arrays.  Also works if z is a function.
    Currently works only for regular data - can be enhanced later.

    Parameters
    ----------

        x -- Array of x points (regularly spaced)

        y -- Array if y points (regularly spaced)

        z -- A 2D array for the x and y points with x varying fastest
        and y next.  Also will work if z is a callable which supports
        x and y arrays as the arguments.

        warp -- If true, warp the data to show a 3D surface
        (default = 1).

        scale -- Scale the x, y and z axis as per passed values.
        Defaults to [1.0, 1.0, 1.0].

        make_actor -- also create actors suitably (default True)

        args -- additional positional arguments for func()
        (default is empty)

        kwargs -- a dict of additional keyword arguments for func()
        (default is empty)
    r   zz is empty - nothing to plot!r   r2   )Zscale_factoré-   )Zfeature_angle)Úscalar_range©Úmapper)Úcallabler   r    r1   r?   r6   r   r+   r   ZImageDataGeometryFilterr   Z
WarpScalarÚoutputZPolyDataNormalsÚPolyDataMapperÚminÚmaxr   )r#   r$   r%   ÚwarpÚscaleZ
make_actorr/   r   ZzvalÚxsZysÚdataZgeom_fZwarperZnormalsrE   Úactorr   r   r   Úmake_surf_actorÖ   s2      



rP   c                 C   sv   t  | d¡}|jd dks"J dƒ‚|d urDt|ƒtt  |¡ƒksDJ ‚tj||d�}|d urrt  |¡|j_d|jj_	|S )NÚlr   é   z!The list of polygons must be Nx3.©ÚpointsÚpolysr   )
r   r7   r4   r   r    r   ÚPolyDatar!   r   r"   )Ú	trianglesrT   r   ÚtÚpdr   r   r   Úmake_triangle_polydata  s    
rZ   c                 C   sø  t | jƒdksJ dƒ‚t |jƒdks,J dƒ‚t |jƒdksBJ dƒ‚| j|jksVJ dƒ‚|j|jksjJ dƒ‚| j\}}tjd|d …d|d …f \}}t |¡t |¡ }}|| | |d | | |d | |d  f}|d | |d  || |d  || | f}	t |d ƒ}
t |
d d	fd
¡}|\|d|
…df< |d|
…df< |d|
…df< |	\||
d…df< ||
d…df< ||
d…df< t ||d	fd¡}| ||  |dd…dd…df< |dd…dd…df< |dd…dd…df< t ||| d	f¡}||fS )a  Given x, y, and z co-ordinates made using numpy.mgrid and
    optional scalars.  This function returns triangles and points
    corresponding to a mesh formed by them.

    Parameters
    ----------

    - x : array
        A list of x coordinate values formed using numpy.mgrid.
    - y : array
        A list of y coordinate values formed using numpy.mgrid.
    - z : array
        A list of z coordinate values formed using numpy.mgrid.
    - scalars : array (optional)
        Scalars to associate with the points.
    r2   zArray x must be 2 dimensional.zArray y must be 2 dimensional.zArray z must be 2 dimensional.z$Arrays x and y must have same shape.z$Arrays y and z must have same shape.r   r   rR   rQ   NÚd)r   r4   r   Úmgridr    Úzerosr8   )r#   r$   r%   r   r&   r'   ÚiÚjÚt1Út2ÚntrW   rT   r   r   r   Úmake_triangles_points$  s$    
"....Frc   c                   @   sH   e Zd ZeeƒZeZdd„ Zdd„ Z	dd„ Z
dd„ Zd	d
„ Zdd„ ZdS )ÚMLabBasec                 C   s   | j  ¡  d S ©N©ÚrenwinÚrender©Úselfr   r   r   ÚupdateV  s    zMLabBase.updatec                 C   s   | j r| j  ¡  d S re   rf   ri   r   r   r   rh   Y  s    zMLabBase.renderc                 C   s4   |r|  | j¡ | ¡  |r0| | j¡ | ¡  d S re   )Úremove_actorsÚactorsrh   Ú
add_actors©rj   ÚoldÚnewr   r   r   Ú_renwin_changed]  s    zMLabBase._renwin_changedc                 C   s   |   ||¡ d S re   )Ú_handle_actorsro   r   r   r   Ú_actors_changede  s    zMLabBase._actors_changedc                 C   s   |   |j|j¡ d S re   )rs   ÚremovedÚadded©rj   Z
list_eventr   r   r   Ú_actors_items_changedh  s    zMLabBase._actors_items_changedc                 C   s*   | j }|r&| |¡ | |¡ | ¡  d S re   )rg   rl   rn   rh   )rj   ru   rv   Úrwr   r   r   rs   k  s
    

zMLabBase._handle_actorsN)Ú__name__Ú
__module__Ú__qualname__r   r   rm   r   rg   rk   rh   rr   rt   rx   rs   r   r   r   r   rd   P  s   rd   c                       sV   e Zd ZeZeejddddœƒZe	dƒZ
d‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z‡  ZS )ÚGlyphsr   Z
use_vectorZdata_scaling_off)Zvector_modeZ
scale_moder@   Nc           	         s&  t t| ƒjf i |¤Ž |d ur2t|ƒt|ƒks2J ‚|d urNt|ƒt|ƒksNJ ‚|| _|| _|| _t dt|ƒdd¡}t 	|t|ƒdf¡}t
j||d�}| jd ur²||j_d|jj_| jd urÎ||j_d|jj_|| _t| j|ƒ | jrò| jj| j_t
j| jjd�}t|d�}| j|j_| j |¡ d S )	Nr   r   rQ   rS   Úvectorsr   ©ÚinputrD   )Úsuperr}   Ú__init__r   rT   r~   r   r   Úaranger8   r   rV   r!   r"   Ú	poly_datar   ÚglyphÚglyph_sourcerG   ÚsourcerH   r   Úcolorr   rm   Úappend)	rj   rT   r~   r   ÚtraitsrU   rY   rE   rO   ©Ú	__class__r   r   r‚   ƒ  s2    





zGlyphs.__init__c                 C   s   | j  ¡  | j ¡  d S re   )r„   rk   rg   rh   ri   r   r   r   rk   ¤  s    
zGlyphs.updatec                 C   s    | j r|| j d j_|  ¡  d S ©Nr   ©rm   r   rˆ   rh   ©rj   r>   r   r   r   Ú_color_changed¨  s    zGlyphs._color_changedc                 C   s   |j | j_|  ¡  d S re   )rG   r…   r‡   rh   r�   r   r   r   Ú_glyph_source_changed­  s    
zGlyphs._glyph_source_changed)NN)rz   r{   r|   r   r†   r   r   ÚGlyph3Dr…   r   rˆ   r‚   rk   r�   r‘   Ú__classcell__r   r   r‹   r   r}   w  s   
ÿ!r}   c                   @   s   e Zd ZeejdƒZdS )ÚArrowsr   N)rz   r{   r|   r   r   ZArrowSourcer†   r   r   r   r   r”   µ  s   r”   c                       s>   e Zd ZeejdƒZeddd�Zd
‡ fdd„	Z	dd	„ Z
‡  ZS )ÚConesr   çš™™™™™©?zradius of the cone©ÚdescNc                    s,   t t| ƒj|||fi |¤Ž |  | j¡ d S re   )r�   r•   r‚   Ú_radius_changedÚradius©rj   rT   r~   r   rŠ   r‹   r   r   r‚   Ã  s    zCones.__init__c                 C   s   || j _|  ¡  d S re   ©r†   rš   rh   r�   r   r   r   r™   Ç  s    zCones._radius_changed)NN)rz   r{   r|   r   r   Z
ConeSourcer†   r   rš   r‚   r™   r“   r   r   r‹   r   r•   ¼  s   r•   c                       s>   e Zd ZeejdƒZeddd�Zd
‡ fdd„	Z	dd	„ Z
‡  ZS )ÚCubesr   r–   zside length of the cuber—   Nc                    s,   t t| ƒj|||fi |¤Ž |  | j¡ d S re   )r�   r�   r‚   r™   rš   r›   r‹   r   r   r‚   Õ  s    zCubes.__init__c                 C   s$   || j _|| j _|| j _|  ¡  d S re   )r†   Zx_lengthZy_lengthZz_lengthrh   r�   r   r   r   Ú_length_changedÙ  s    zCubes._length_changed)NN)rz   r{   r|   r   r   Z
CubeSourcer†   r   Úlengthr‚   rž   r“   r   r   r‹   r   r�   Ï  s   r�   c                   @   s   e Zd ZeejdƒZdS )Ú	Cylindersr   N)rz   r{   r|   r   r   ZCylinderSourcer†   r   r   r   r   r    ã  s   r    c                       sF   e Zd ZeejddddœƒZeddd�Zd‡ fd	d
„	Z	dd„ Z
‡  ZS )ÚSpheresr   é   é   )Úphi_resolutionÚtheta_resolutionr–   zradius of the spherer—   Nc                    s,   t t| ƒj|||fi |¤Ž |  | j¡ d S re   )r�   r¡   r‚   r™   rš   r›   r‹   r   r   r‚   ó  s    zSpheres.__init__c                 C   s   || j _|  ¡  d S re   rœ   r�   r   r   r   r™   ÷  s    zSpheres._radius_changed)NN)rz   r{   r|   r   r   ÚSphereSourcer†   r   rš   r‚   r™   r“   r   r   r‹   r   r¡   ë  s   ÿÿr¡   c                   @   s    e Zd ZeejddddœƒZdS )ÚPointsr   r   r   )rš   Znumber_of_pointsN)rz   r{   r|   r   r   ZPointSourcer†   r   r   r   r   r§   ÿ  s   ÿr§   c                       sf   e Zd Zeddd�Zeddd�Zeej	dddiƒZ
ed	ƒZ‡ fd
d„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚLine3ç{®Gáz„?úradius of the tubesr—   Tz+specifies if the tube filter should be usedr   Únumber_of_sidesé   r@   c           	         s   t t| ƒjf i |¤Ž t|d ƒdks.J dƒ‚|| _t|ƒd }t |dfd¡}t d|d dd¡|d d …df< t d|d dd¡|d d …df< tj	||d�}|| _
t ¡ }|| _| j}| j|_| jrÜt||ƒ t||jƒ t|d	�}| j|j_| j |¡ d S )
Nr   rR   zThe points must be 3Dr   r2   rQ   ç      à?)rT   ÚlinesrD   )r�   rd   r‚   r   rT   r   r]   rƒ   r   rV   r„   rH   rE   Útube_filterrš   Ú	use_tubesr   rG   r   rˆ   r   rm   r‰   )	rj   rT   rŠ   Únpr®   rY   rE   Útfr<   r‹   r   r   r‚     s&      


zLine3.__init__c                 C   s   || j _|  ¡  d S re   )r¯   rš   rh   r�   r   r   r   r™   /  s    zLine3._radius_changedc                 C   s@   |r&| j }t|| jƒ t| j|jƒ nt| j| jƒ |  ¡  d S re   )r¯   r   r„   rE   rG   rh   )rj   r>   r²   r   r   r   Ú_use_tubes_changed3  s    zLine3._use_tubes_changedc                 C   s    | j r|| j d j_|  ¡  d S r�   rŽ   r�   r   r   r   r�   <  s    zLine3._color_changed)rz   r{   r|   r   rš   r   r°   r   r   Ú
TubeFilterr¯   r   rˆ   r‚   r™   r³   r�   r“   r   r   r‹   r   r¨     s   ÿ	r¨   c                
       sX   e Zd ZeejddddddddœƒZeejdƒZ‡ fd	d
„Z	dd„ Z
‡ fdd„Z‡  ZS )ÚOutliner   z%4.2gZouter_edgesg      ô?é   ç        r   )Zlabel_formatZfly_modeZfont_factorZnumber_of_labelsZcorner_offsetÚscalingc                    sb   t t| ƒjf i |¤Ž tj| jjd�}t|d�}| j}t	|dƒrH||_
n||_| j ||g¡ d S )Nr   rD   Ú	view_prop)r�   rµ   r‚   r   rH   ÚoutlinerG   r   ÚaxisÚhasattrr¹   Úproprm   Úextend)rj   rŠ   Z
out_mapperZ	out_actorr»   r‹   r   r   r‚   N  s    

zOutline.__init__c                 C   sv   | j rr| j }dd„ | jD ƒ\}}d| jd _d| jd _| ¡  |j ¡ }|| j_| ¡  || jd _|| jd _d S )Nc                 S   s   g | ]
}|j ‘qS r   )Ú
visibility)Ú.0r#   r   r   r   Ú
<listcomp>^  ó    z"Outline.update.<locals>.<listcomp>r   r   )rg   rm   r¿   rh   ÚrendererZcompute_visible_prop_boundsrº   Úbounds)rj   ry   Zv1Zv2rÄ   r   r   r   rk   [  s    
zOutline.updatec                    sj   t t| ƒ ||¡ |r:|j| jddd� |j| jddd� |rf|jj| j_| | jd¡ | | jd¡ d S )NZactor_addedT)ÚremoveZactor_removed)	r�   rµ   rr   Zon_trait_changerk   rÃ   Zactive_camerar»   Zcameraro   r‹   r   r   rr   h  s    zOutline._renwin_changed)rz   r{   r|   r   r   ZCubeAxesActor2Dr»   ZOutlineSourcerº   r‚   rk   rr   r“   r   r   r‹   r   rµ   E  s   þÿrµ   c                       s<   e Zd Zed dd�ZeejdƒZ‡ fdd„Z	dd„ Z
‡  ZS )ÚTitleztext of the titler—   r   c                    sR   t t| ƒjf i |¤Ž | j}|jdd| jd� |j}d|_d|_| j	 
| j¡ d S )Nr½   r–   )Ztext_scale_modeÚheightr€   Únormalized_viewport)g      Ð?gš™™™™™í?r·   )r�   rÆ   r‚   Ú
text_actorr   ÚtextÚposition_coordinateÚcoordinate_systemÚvaluerm   r‰   )rj   rŠ   ÚtaÚpcr‹   r   r   r‚   }  s    zTitle.__init__c                 C   s   || j _|  ¡  d S re   )rÉ   r€   rh   r�   r   r   r   Ú_text_changed‡  s    zTitle._text_changed)rz   r{   r|   r   rÊ   r   r   Z	TextActorrÉ   r‚   rÐ   r“   r   r   r‹   r   rÆ   v  s   
rÆ   c                       sš   e Zd Zeddddddd�ZeejdƒZeej	ddd	d
dœƒZ
eejdƒZeddd�Zeddd�Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡ fdd„Z‡  ZS )ÚLUTBaseúred-blueúblue-redúblack-whiteúwhite-blackzthe type of the lookup tabler—   r   Z
horizontalçš™™™™™é?gÃõ(\�ÂÅ?)ZorientationÚwidthrÇ   ZScalarzthe title of the legendFz'specifies if scalar bar is shown or notc                    sh   t t| ƒjf i |¤Ž d| j_|  | j¡ | jj| j| j	d� | jj
}d|_d|_| jj| jdd� d S )Né   )Úlookup_tableÚtitlerÈ   )çš™™™™™¹?r©   r·   F)Zscalar_bar_actorZkey_press_activation)r�   rÑ   r‚   ÚlutZnumber_of_colorsÚ_lut_type_changedÚlut_typeÚ
scalar_barr   Úlegend_textrË   rÌ   rÍ   Úscalar_bar_widget)rj   rŠ   rÏ   r‹   r   r   r‚   ¦  s    
ÿ
ÿzLUTBase.__init__c                 C   s„   |dkrd}d}d}n@|dkr,d}d}d}n*|dkrBd}d}d}n|d	krVd}d}d
}| j }|j|||ddd� | ¡  |  ¡  d S )NrÒ   )r·   çKÈ=›Uå?)rA   rA   rÓ   )râ   r·   rÔ   )r·   r·   )r·   rA   rÕ   )rA   r·   rØ   Úsqrt)Ú	hue_rangeÚsaturation_rangeÚvalue_rangeZnumber_of_table_valuesZramp)rÜ   r   Zforce_buildrh   )rj   r>   rä   rå   ræ   rÜ   r   r   r   rÝ   ²  s.    þzLUTBase._lut_type_changedc                 C   s   || j _| j  ¡  |  ¡  d S re   )rß   rÚ   Úmodifiedrh   r�   r   r   r   Ú_legend_text_changedË  s    
zLUTBase._legend_text_changedc                 C   s   | j r|| j_| j  ¡  d S re   )rg   rá   Úenabledrh   r�   r   r   r   Ú_show_scalar_bar_changedÐ  s    z LUTBase._show_scalar_bar_changedc                    sJ   | j }|rd |_| ¡  |r4|j|_| j|_| ¡  tt| ƒ ||¡ d S re   )rá   Z
interactorrh   Úshow_scalar_barré   r�   rÑ   rr   )rj   rp   rq   Zsbwr‹   r   r   rr   Õ  s    zLUTBase._renwin_changed)rz   r{   r|   r	   rÞ   r   r   ZLookupTablerÜ   ZScalarBarActorrß   ZScalarBarWidgetrá   r   rà   r   rë   r‚   rÝ   rè   rê   rr   r“   r   r   r‹   r   rÑ   Ž  s(   þÿÿÿrÑ   c                       s*   e Zd Zdg d¢ddf‡ fdd„	Z‡  ZS )ÚSurfRegularr   r@   r   Nc                    sr   t t| ƒjf i |¤Ž |d u r"i }t|||||g|¢R i |¤Ž\}	}
|	| _|
j}| j|_|j| j_	| j
 |
¡ d S re   )r�   rì   r‚   rP   rN   rE   rÜ   rÙ   rC   Útable_rangerm   r‰   )rj   r#   r$   r%   rK   rL   Úf_argsÚf_kwargsrŠ   rN   rO   rE   r‹   r   r   r‚   ç  s    ÿ

zSurfRegular.__init__©rz   r{   r|   r‚   r“   r   r   r‹   r   rì   å  s   
ÿrì   c                       sJ   e Zd Zeddd�ZeejdƒZdg d¢ddf‡ fdd	„	Z	d
d„ Z
‡  ZS )ÚSurfRegularCé
   únumber of contours valuesr—   r   r   r@   Nc                    sÄ   t t| ƒjf i |¤Ž |d u r"i }t|||||g|¢R i |¤Ž\}	}
|
j}| j|_|j| j_|	| _	|	j
jj}| j}t||	ƒ | | j|d |d ¡ tj|j| jd�}t|d�}| j |
|g¡ d S )Nr   r   )r€   rÙ   rD   )r�   rñ   r‚   rP   rE   rÜ   rÙ   rC   rí   rN   r!   r   ÚrangeÚcontour_filterr   Úgenerate_valuesÚnumber_of_contoursr   rH   rG   r   rm   r¾   )rj   r#   r$   r%   rK   rL   rî   rï   rŠ   rN   rO   rE   ÚdrÚcfÚ
cont_actorr‹   r   r   r‚     s"    ÿ




zSurfRegularC.__init__c                 C   s0   | j jjj}| j ||d |d ¡ |  ¡  d S ©Nr   r   )rN   r!   r   rô   rõ   rö   rh   ©rj   r>   rø   r   r   r   Ú_number_of_contours_changed  s    z(SurfRegularC._number_of_contours_changed©rz   r{   r|   r
   r÷   r   r   ZContourFilterrõ   r‚   rý   r“   r   r   r‹   r   rñ   û  s   
ÿrñ   c                       sV   e Zd Zeddd�Zeddd�ZedƒZd‡ fdd„	Zd	d
„ Z	dd„ Z
dd„ Z‡  ZS )ÚTriMeshFúshow scalar visibilityr—   zshow as surface or wireframe©r­   rA   r­   Nc           
         sÈ   t t| ƒjf i |¤Ž t|||ƒ| _tj| j| j| jd�}|durjt	 
|¡}t|ƒt|ƒf}||_|| j_t|d�}d}	| jr‚d}	|	dkr¢|jjdd| j|	d� n|jjdd| j|	d� | j |¡ dS )	áJ  
        Parameters
        ----------

        - triangles : array
          This contains a list of vertex indices forming the triangles.
        - points : array
          Contains the list of points referred to in the triangle list.
        - scalars : array (optional)
          Scalars to associate with the points.
        ©r€   rÙ   Úscalar_visibilityNrD   ÚwÚsr·   rA   )ÚdiffuseÚambientrˆ   Úrepresentation)r�   rÿ   r‚   rZ   rY   r   rH   rÜ   r  r   r    rI   rJ   rC   rí   r   Úsurfacer   r   rˆ   rm   r‰   )
rj   rW   rT   r   rŠ   rE   Úrsrø   rO   r	  r‹   r   r   r‚   ,  s,    ÿ

ÿÿzTriMesh.__init__c                 C   s$   | j r| j d j}||_|  ¡  d S r�   ©rm   rE   r  rh   ©rj   r>   rE   r   r   r   Ú_scalar_visibility_changedQ  s    z"TriMesh._scalar_visibility_changedc                 C   sV   | j rJd}|rd}| j d }|dkr8|jjdd|d� n|jjdd|d� |  ¡  d S )Nr  r  r   r·   rA   )r  r  r	  )rm   r   r   rh   )rj   r>   r	  rO   r   r   r   Ú_surface_changedW  s    

ÿ
ÿzTriMesh._surface_changedc                 C   s    | j r|| j d j_|  ¡  d S r�   rŽ   r�   r   r   r   r�   f  s    zTriMesh._color_changed)N)rz   r{   r|   r   r  r
  r   rˆ   r‚   r  r  r�   r“   r   r   r‹   r   rÿ   "  s   %rÿ   c                       s–   e Zd ZdZeddd�ZedƒZeddd�Z	eddd�Z
eejd	d
ddœƒZeejd	dddœƒZd‡ fdd„	Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚFancyTriMeshzOShows a mesh of triangles and draws the edges as tubes and
    points as balls.Fr   r—   r  r·   rª   zradius of the spheresr   Zvary_radius_offr¬   )Zvary_radiusr«   é   )r¥   r¤   Nc                    s0  t t| ƒjf i |¤Ž || _t|||ƒ| _|  | j¡ |  | j	¡ | j
}tj| jd�| _| j}| jj|j| jd� tj| jj| j|d�}t|d�}| j|j_| j	| j_tjd| jj|jd�}	tj|	j| j|d�}
t|
d�}| j|j_|du�rt |¡}t|ƒt|ƒf}|| j_||_||
_| j  ||g¡ dS )r  r   )r€   rš   r  rD   r   )r¸   r‡   r€   N)!r�   r  r‚   rT   rZ   rY   Ú_tube_radius_changedÚtube_radiusÚ_sphere_radius_changedÚsphere_radiusr  r   ZExtractEdgesZextract_filterr¯   r   rG   rH   rÜ   r   rˆ   r   Úsphere_sourcerš   r’   r   r    rI   rJ   rí   rC   rm   r¾   )rj   rW   rT   r   rŠ   Z
scalar_visZ	extract_fZedge_mapperZ
edge_actorZspheresZsphere_mapperZsphere_actorr  rø   r‹   r   r   r‚   ‡  sD    
ÿ
þ


ÿþ



zFancyTriMesh.__init__c                 C   s&   | j r| j D ]}||j_q|  ¡  d S re   r  )rj   r>   r^   r   r   r   r  ¼  s    

z'FancyTriMesh._scalar_visibility_changedc                 C   sH   | j }|dk r.tt |¡ƒtt |¡ƒ d }|| _|| j_|  ¡  d S )Nç•Ö&è.>ç     @o@)	rT   rJ   r   r    rI   r  r¯   rš   rh   ©rj   r>   rT   r   r   r   r  Â  s    ÿÿz!FancyTriMesh._tube_radius_changedc                 C   sH   | j }|dk r.tt |¡ƒtt |¡ƒ d }|| _|| j_|  ¡  d S )Nr  g      Y@)	rT   rJ   r   r    rI   r  r  rš   rh   r  r   r   r   r  Ë  s    ÿÿz#FancyTriMesh._sphere_radius_changedc                 C   s    | j r|| j d j_|  ¡  d S r�   rŽ   r�   r   r   r   r�   Ô  s    zFancyTriMesh._color_changed)N)rz   r{   r|   Ú__doc__r   r  r   rˆ   r   r  r  r   r   r´   r¯   r¦   r  r‚   r  r  r  r�   r“   r   r   r‹   r   r  o  s(   ÿÿÿÿ5		r  c                       s   e Zd Zd‡ fdd„	Z‡  ZS )ÚMeshNc                    s2   t ||||ƒ\}}tt| ƒj|||fi |¤Ž dS ©á‡  
        Parameters
        ----------

        - x : array
          A list of x coordinate values formed using numpy.mgrid.
        - y : array
          A list of y coordinate values formed using numpy.mgrid.
        - z : array
          A list of z coordinate values formed using numpy.mgrid.
        - scalars : array (optional)
          Scalars to associate with the points.
        N)rc   r�   r  r‚   ©rj   r#   r$   r%   r   rŠ   rW   rT   r‹   r   r   r‚   Ý  s    zMesh.__init__)Nrð   r   r   r‹   r   r  Ü  s   r  c                       s   e Zd Zd‡ fdd„	Z‡  ZS )Ú	FancyMeshNc                    s2   t ||||ƒ\}}tt| ƒj|||fi |¤Ž dS r  )rc   r�   r  r‚   r  r‹   r   r   r‚   ó  s    zFancyMesh.__init__)Nrð   r   r   r‹   r   r  ò  s   r  c                       sJ   e Zd Zeddd�ZedƒZd‡ fdd„	Zdd	„ Zd
d„ Z	dd„ Z
‡  ZS )ÚSurfTr   r—   r  Nc                    s¦   t t| ƒjf i |¤Ž t||||ƒ\}}t|||ƒ| _tj| j| j| j	d�}|dur|t
 |¡}	t|	ƒt|	ƒf}
|
|_|
| j_t|d�}|jj| jd� | j |¡ dS )r  r  NrD   ©rˆ   )r�   r   r‚   rc   rZ   rY   r   rH   rÜ   r  r   r    rI   rJ   rC   rí   r   r   r   rˆ   rm   r‰   )rj   r#   r$   r%   r   rŠ   rW   rT   rE   r  rø   rO   r‹   r   r   r‚     s    ÿ

zSurf.__init__c                 C   s$   | j r| j d j}||_|  ¡  d S r�   r  r  r   r   r   r  -  s    zSurf._scalar_visibility_changedc                 C   s,   | j r d}|rd}|| j d j_|  ¡  d S )Nr  r  r   )rm   r   r	  rh   )rj   r>   r	  r   r   r   r  3  s    zSurf._surface_changedc                 C   s    | j r|| j d j_|  ¡  d S r�   rŽ   r�   r   r   r   r�   ;  s    zSurf._color_changed)N)rz   r{   r|   r   r  r   rˆ   r‚   r  r  r�   r“   r   r   r‹   r   r     s   r   c                       s<   e Zd Zeddd�ZeejdƒZ‡ fdd„Z	dd„ Z
‡  ZS )	ÚContour3rò   ró   r—   r   c                    s¢   t t| ƒjf i |¤Ž t||||ƒ\}}t|||ƒ| _| jjjj}|| j	_
| j}	t|	| jƒ |	 | j|d |d ¡ tj|	j| j	|d�}
t|
d�}| j |¡ dS )a|  
        Parameters
        ----------

        - x : array
          A list of x coordinate values formed using numpy.mgrid.
        - y : array
          A list of y coordinate values formed using numpy.mgrid.
        - z : array
          A list of z coordinate values formed using numpy.mgrid.
        - scalars : array
          Scalars to associate with the points.
        r   r   )r€   rÙ   rC   rD   N)r�   r"  r‚   rc   rZ   rY   r!   r   rô   rÜ   rí   rõ   r   rö   r÷   r   rH   rG   r   rm   r‰   )rj   r#   r$   r%   r   rŠ   rW   rT   rø   rù   rE   rú   r‹   r   r   r‚   K  s    ÿ
zContour3.__init__c                 C   s0   | j jjj}| j ||d |d ¡ |  ¡  d S rû   )rY   r!   r   rô   rõ   rö   rh   rü   r   r   r   rý   i  s    z$Contour3._number_of_contours_changedrþ   r   r   r‹   r   r"  D  s   r"  c                       s<   e Zd ZdZeddd�Zg d¢f‡ fdd„	Zdd	„ Z‡  ZS )
ÚImShowzyAllows one to view a 2D numpy array as an image.  This works
    best for very large arrays (like 1024x1024 arrays).
    Fz)specifies if image should be interpolatedr—   r@   c                    s
  t t| ƒjf i |¤Ž t|jƒdks,J dƒ‚|j\}}t |¡d \}}}t d||d  d|  |d¡}	t d||d  d|  |d¡}
t |¡}t	|ƒ}t
|ƒ}t|	|
ƒ}| j}||f|_| |dd¡}||j_d|jj_d	|_d
|_tj|| jd�}| j |¡ dS )z½
        Parameters
        ----------
        - arr : Array to be viewed.

        - scale : Scale the x, y and z axis as per passed values.
          Defaults to [1.0, 1.0, 1.0].
        r2   zOnly 2D arrays can be viewed!rA   r   rÛ   Úfr   r   Zunsigned_charé   )r€   ÚinterpolateN)r�   r#  r‚   r   r4   r   Úarrayrƒ   r    rI   rJ   r+   rÜ   rí   Zmap_scalarsr!   r   r"   Zscalar_typeZnumber_of_scalar_componentsr   Z
ImageActorr&  rm   r‰   )rj   ZarrrL   rŠ   r'   r&   r(   r)   Zjunkr,   r-   Zarr_flatZmin_valZmax_valr*   rÜ   r<   Úiar‹   r   r   r‚   z  s&    	
  



zImShow.__init__c                 C   s"   | j r| j d }||_|  ¡  d S r�   )rm   r&  rh   )rj   r>   r(  r   r   r   Ú_interpolate_changed�  s    
zImShow._interpolate_changed)	rz   r{   r|   r  r   r&  r‚   r)  r“   r   r   r‹   r   r#  r  s   #r#  c                       sX   e Zd ZdZeeƒZ‡ fdd„Zdd„ Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Z‡  ZS )ÚFigurezpA Figure manages varuous MLabBase objects.  Each of these
    objects contains an actor and does something neat.c                    s    t t| ƒjf i |¤Ž || _d S re   )r�   r*  r‚   rg   )rj   rg   rŠ   r‹   r   r   r‚   ®  s    zFigure.__init__c                 C   s   | j  |¡ dS )z]Add an object to the figure.  This adds the actors of the
        object to the renderwindow.N)Úobjectsr‰   )rj   Úobjr   r   r   Úadd²  s    z
Figure.addc                 C   s
   | j  ¡ S )zPops out the last object.)r+  Úpopri   r   r   r   r.  ·  s    z
Figure.popc                 C   s
   g | _ dS )z"Removes all objects in the figure.N)r+  ri   r   r   r   Úclear»  s    zFigure.clearc                 C   s   |   ||¡ d S re   )Ú_handle_objects)rj   rq   rp   r   r   r   Ú_objects_changed¿  s    zFigure._objects_changedc                 C   s   |   |j|j¡ d S re   )r0  ru   rv   rw   r   r   r   Ú_objects_items_changedÂ  s    zFigure._objects_items_changedc                 C   s:   |D ]
}d |_ q| j }|D ]
}||_ q| ¡  | ¡  d S re   )rg   Ú
reset_zoomrh   )rj   ru   rv   r,  ry   r   r   r   r0  Å  s    zFigure._handle_objects)rz   r{   r|   r  r   rd   r+  r‚   r-  r.  r/  r1  r2  r0  r“   r   r   r‹   r   r*  §  s   r*  c                 C   s6   t  |¡}t|jƒ}| r(tƒ }| |¡ |j ¡  |S )ar  Simple helper function that returns a usable figure.

    Parameters
    ----------

    - outline : `bool` (default: True)

      If True, create an outline bounding box along with an axes
      marker for the scene.

    - browser : `bool` (default, True)

      If True, creates an IVTK scene with an embedded PipelineBrowser.
      If False, does not create it.
    )r   Zviewerr*  Úscenerµ   r-  r3  )rº   ZbrowserÚvr$  Úor   r   r   ÚfigureÏ  s    



r7  c                 C   s4   t g d¢g d¢gg d¢g d¢gdd�}|  |¡ d S )N)éÿÿÿÿr8  r8  ©r   r   r   ©r   r   r   ©r   r   r   r!  )r”   r-  )Úfigr<   r   r   r   Útest_arrowsë  s    &r=  c                 C   sü   d\}}t j}|d }t  dd| d|  |d¡}|| }t  |¡dt  || | ¡d   }t  |¡dt  || | ¡d   }t  || | ¡d }	t  t|ƒdfd¡}
|||	  |
d	d	…d
f< |
d	d	…df< |
d	d	…df< t|
ddd�}|  |¡ d	S )z Generates a pretty set of lines.)r¬   é   g     @�@r·   r2   r­   r[   r   rR   Nr   r–   )r·   r·   rÖ   ©rš   rˆ   )	r   Úpirƒ   ÚcosÚsinr]   r   r¨   r-  )r<  Zn_merZn_longr@  ÚdphiÚphiÚmur#   r$   r%   ÚptsrQ   r   r   r   Ú
test_linesð  s    $$4rG  c           
      C   sø   g d¢g d¢g}g d¢g d¢g d¢g d¢g}g d¢g d¢g d	¢g d
¢g d¢g d¢g d¢g d¢g}g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g d¢g
}t |ddd�}t |ddd�}t |ddd�}t |dd d�}||||fD ]}	|  |	¡ qäd!S )"z(Generates and shows a Caffeine molecule.)r£   é>   é   )é   é   rò   )é   rK  r>  )é   é*   é   )é7   é.   é   )é8   é   é   )r¶   é1   r¢   )r£   é2   é   )rN  rN  r¢   )é+   é   rU  )rM  é   r  )é    r¬   rJ  )é?   é$   r¢   )é;   é<   é   )é   r¶   é   )r\  r   rX  )é%   r¶   r   )éI   r^  rX  )éE   r`  ra  )é6   rH  r[  )é9   éB   r  )r¬   r_  rX  )r   é,   é   )r   rV  r¬   rJ  r9  r?  rò   )r   r   r   r;  r¶   r:  N)r¡   r-  )
r<  r6  ÚnÚcÚhZoxygenZnitrogenZcarbonZhydrogenr^   r   r   r   Útest_molecule  s    ÿÿro  c                 C   sl   t  g d¢g d¢g d¢gd¡}g d¢g}t||ƒ}|  |¡ | ¡ }d|dd…df< t||ƒ}|  |¡ dS )	zTest for simple triangle mesh.)r·   r   r   )rA   r·   r·   )r   r   r   r[   )r   r   r2   rA   Nr2   )r   r'  rÿ   r-  Úcopyr  )r<  rF  rW   r`   Zpts1ra   r   r   r   Útest_trimesh  s    



rq  c                 C   sP   dd„ }t  ddd¡}t  ddd¡}|r6t|||ƒ}nt|||ƒ}|  |¡ d	S )
z7Test Surf on regularly spaced co-ordinates like MayaVi.c                 S   s   t  | | ¡| |  S re   )r   rB  )r#   r$   r   r   r   r$     s    ztest_surf_regular.<locals>.fg      Àg333333@rÛ   g      Àg333333@r–   N)r   rƒ   rñ   rì   r-  )r<  Zcontourr$  r#   r$   r  r   r   r   Útest_surf_regular  s    rr  c                 C   sF   t jddd…ddd…f \}}|}t|||t  |d¡ƒ}|  |¡ dS )z-Test Surf with a simple collection of points.r   rR   r   r[   N)r   r\   r   r7   r-  )r<  r#   r$   r%   r  r   r   r   Útest_simple_surf,  s    rs  c                 C   s  t j}t j}t j}|d |d  }}t jd||d  |…dd| |d  |…f \}}d}d}	d}
d}d}d}d}d}||| ƒ|	 ||
| ƒ|  ||| ƒ|  ||| ƒ|  }|||ƒ ||ƒ }|||ƒ }|||ƒ ||ƒ }t||||ƒ}|  |¡ dS )	zUA very pretty picture of spherical harmonics translated from
    the octaviz example.r  r   ç      ø?r2   r%  rR   r¬   N)r   r@  rA  rB  r\   r   r-  )r<  r@  rA  rB  rC  ZdthetarD  ÚthetaZm0Úm1Úm2Zm3Zm4Zm5Zm6Zm7r   r#   r$   r%   r  r   r   r   Ú	test_surf3  s    2 @rx  c                 C   s®   t j}t j}t j}|d |d  }}t jd||d  |…dd| |d  |…f \}}d}|||ƒ ||ƒ }	|||ƒ ||ƒ }
|||ƒ }t|	|
||dd�}|  |¡ d	S )
z)Create a simple sphere and test the mesh.ç      4@r©   rt  r   r2   rA   T©r  N©r   r@  rA  rB  r\   r  r-  )r<  r@  rA  rB  ÚduÚdvrD  ru  r   r#   r$   r%   r  r   r   r   Útest_mesh_sphereD  s    2r~  c                 C   sú   t j}t j}t j}|d |d  }}t jd||d  |…dd| |d  |…f \}}d||ƒ ||d| d  ƒ ||d| d  ƒ d	 }d||ƒ ||d| d  ƒ ||d| d  ƒ d	 }	||d| d  ƒ}
t||	|
|
d
d�}|  |¡ dS )z9Create a fancy looking mesh (example taken from octaviz).ry  r©   rt  r   r2   r   rR   g      @r­   Trz  Nr{  )r<  r@  rA  rB  r|  r}  Úur5  r#   r$   r%   Úmr   r   r   Ú	test_meshR  s    288r�  c                 C   s"   t j d¡}t|ƒ}|  |¡ dS )zShow a large random array.)i   i   N)r   Úrandomr#  r-  )r<  Zz_larger^   r   r   r   Útest_imshow`  s    rƒ  c                  C   sR   t ƒ } tjdd�}| ¡  t|jƒ}tƒ }| |¡ t|ƒ |j 	¡  |  
¡  d S )N)i   iX  )r   )r   r   ZIVTKWithCrustAndBrowserÚopenr*  r4  rµ   r-  rx  r3  Zstart_event_loop)ÚguiZwindowr$  r6  r   r   r   Úmaing  s    


r†  Ú__main__)N)N)N)TT)r   )Cr  r   Z
traits.apir   r   r   r   r   r   r   r	   r
   Z
pyface.apir   Ztvtk.apir   Ztvtk.tvtk_baser   r   Ztvtk.commonr   Z
tvtk.toolsr   r   r   r+   r1   r6   r?   rP   rZ   rc   rd   r}   r”   r•   r�   r    r¡   r§   r¨   rµ   rÆ   rÑ   rì   rñ   rÿ   r  r  r  r   r"  r#  r*  r7  r=  rG  ro  rq  rr  rs  rx  r~  r�  rƒ  r†  rz   r   r   r   r   Ú<module>   sl   ^,
#	ÿ
@

,'>	=1W'Mm<.5(


