3
5Ad]§3  ã               @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ d	d
lmZmZmZ d	dlmZ dd„ Zd,dd„Zdd„ Zdd„ Zd-dd„Zdd„ Zdd„ Zdd„ Zd.dd„Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Z d/d(d)„Z!d0d*d+„Z"dS )1zO
The general purpose tools to manipulate the pipeline with the mlab interface.
é    N)Útvtk)ÚVTKDataSource)ÚVTKObjectSource)ÚModuleManager)ÚSource)ÚFilteré   )Ú
get_engineÚengine_managerÚget_null_engine)Úgcfc             C   sV   d }d| krt ƒ  tƒ }n8| d dkr.tƒ }n$| d d k	rR| d }tj|ƒ}||_|S )NÚfigureF)r   r	   r   r
   Zfind_figure_engineZcurrent_scene)ÚkwargsÚenginer   © r   úy/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/mayavi-4.7.1-py3.6-linux-x86_64.egg/mayavi/tools/tools.pyÚ_get_engine_from_kwarg    s    
r   Ú c             K   sÐ   t | tjtjfƒr&tƒ }tj| ƒ|_nvt | tjtj	fƒr^t
ƒ }tjƒ }|jtj| ƒƒ ||_n>t | tjtjfƒr„t
ƒ }tj| ƒ|_nt | tƒr”| }ntdƒ‚t|ƒdkr®||_t|ƒ}|dkrÂ|S |j|ƒ |S )aª  Add a dataset object to the Mayavi pipeline.

    **Parameters**

    :dataset: a tvtk/vtk dataset/tvtk/VTK Algorithm, or a Mayavi source. The
              dataset added to the Mayavi pipeline

    :figure: a figure identifier number or string, None or False, optionnal.

            If no `figure` keyword argument is given, the data
            is added to the current figure (a new figure if created if
            necessary).

            If a `figure` keyword argument is given, it should either the name
            the number of the figure the dataset should be added to, or None,
            in which case the data is not added to the pipeline.

            If figure is False, a null engine is created. This null
            engine does not create figures, and is mainly usefull for
            tensting, or using the VTK algorithms without visualization.

    **Returns**

    The corresponding Mayavi source is returned.

    z@first argument should be either a TVTK object or a mayavi sourcer   N)Ú
isinstancer   ZDataSetÚvtkÚ
vtkDataSetr   Zto_tvtkÚdataZ
DataObjectÚvtkDataObjectr   ZTrivialProducerZ
set_outputÚobjectÚObjectÚ	vtkObjectr   Ú	TypeErrorÚlenÚnamer   Z
add_source)Zdatasetr   r   ÚdÚtpr   r   r   r   Úadd_dataset1   s,    

r!   c             C   s   t ƒ jtƒ | ƒS )zZ Add a module-manager, to control colors and legend bars to the
        given object.
    )r	   Z
add_moduler   )r   r   r   r   Úadd_module_managerh   s    r"   c             c   sL   y,x&| j D ]}xt|ƒD ]
}|V  qW q
W W n tk
r@   Y nX | V  dS )a&   Generator to traverse a tree accessing the nodes' children
        attribute.

        **Example**

        Here is a simple example printing the names of all the objects in
        the pipeline::

            for obj in mlab.pipeline.traverse(mlab.gcf()):
                print obj.name

    N)ÚchildrenÚ	_traverseÚAttributeError)ÚnodeÚleafZleafletr   r   r   r$   o   s    r$   Tc                s”   t | tjƒr.| jdƒr| gS t| dƒr.| jgS t| dƒp@t | tƒsNtd|  ƒ‚x@t | tƒr†|sjt | tƒ r†dd„ ‰ ‡ fdd„| j	D ƒS | j
} qPW d	S )
aÀ   Goes up the Mayavi pipeline to find the data sources of a given
        object.

        **Parameters**

        :object: any Mayavi visualization object

        :stop_at_filter: optional boolean flag: if True, the first object
                         exposing data found going up the pipeline is
                         returned. If False, only the source itself
                         is returned.

        **Returns**

        :sources: List of vtk data sources (vtk data sources, and not
                  Mayavi source objects).

        **Notes**

        This function traverses the Mayavi pipeline. Thus the input
        object 'mayavi_object' should already be added to the pipeline.
    r   ÚoutputÚparentz+Cannot find data source for given object %sc             S   s   | j dƒr| S | jS )Nr   )Úis_ar(   )Úxr   r   r   Ú<lambda>ª   s    zget_vtk_src.<locals>.<lambda>c                s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0r+   )Ú
get_outputr   r   ú
<listcomp>«   s    zget_vtk_src.<locals>.<listcomp>N)r   r   r   r*   Úhasattrr(   r   r   r   Úoutputsr)   )Zmayavi_objectZstop_at_filterr   )r.   r   Úget_vtk_src…   s    




r2   c             C   s:   t | ƒ}x,|D ]$}|jjdk	r"dS |jjdk	rdS qW dS )z(Tests if an object has scalar data.
    NTF)r2   Ú
point_dataÚscalarsÚ	cell_data)r   Údata_sourcesÚsourcer   r   r   Ú_has_scalar_data¯   s    
r8   c             C   s:   t | ƒ}x,|D ]$}|jjdk	r"dS |jjdk	rdS qW dS )z(Tests if an object has vector data.
    NTF)r2   r3   Zvectorsr5   )r   r6   r7   r   r   r   Ú_has_vector_data»   s    
r9   c             C   s:   t | ƒ}x,|D ]$}|jjdk	r"dS |jjdk	rdS qW dS )z(Tests if an object has tensor data.
    NTF)r2   r3   Ztensorsr5   )r   r6   r7   r   r   r   Ú_has_tensor_dataÇ   s    
r:   c             C   sî   | dkrŒxàt tƒ ƒD ]r} t| tƒr|dkrZt| ƒs4qy| jjjsBwW n tk
rX   Y nX |dkrnt	| ƒ rnq|dkr‚t
| ƒ r‚q| S qW n^t| dƒrâ|dkr¦t| ƒsÎ|dkr¶t	| ƒsÎ|dkrÆt
| ƒsÎ|dkrÔ| jS td| ƒ ntdƒ dS )zvIf an object is specified, returns its module_manager, elsewhere finds
    the first module_manager in the scene.
    NÚscalarÚvectorÚtensorÚmodule_managerzThis object has no %s datazThis object has no color map)r$   r   r   r   r8   ÚactorZmapperZscalar_visibilityr%   r9   r:   r0   r>   Úprint)r   Z	data_typer   r   r   Ú_find_module_managerÓ   s2    



rA   c             C   sb   | j \}}}}}}tj|| d || d  || d  d| jd   ƒ}|dkrVdS d| S dS )z¶ Returns a typical distance in a cloud of points.
        This is done by taking the size of the bounding box, and dividing it
        by the cubic root of the number of points.
    é   é   g…ëQ¸Õ?r   r   gš™™™™™Ù?N)ÚboundsÚnumpyÚsqrtZnumber_of_points)Zdata_objZx_minZx_maxZy_minZy_maxZz_minZz_maxZdistancer   r   r   Ú_typical_distanceõ   s    rG   c             C   s^   t j| jdƒ| jdƒ d |jd	ƒ|jdƒ d  |jdƒ|jdƒ d  ƒ}||dk jƒ S )z¬ Return the minimum interparticle distance in a cloud of points.
        This is done by brute force calculation of all the distances
        between particle couples.
    r   rB   r   éÿÿÿÿ)rH   rH   )r   rH   rH   )rH   rH   )r   rH   rH   )rH   rH   )r   rH   )rE   rF   ÚreshapeÚmin)r+   ÚyÚzÚ	distancesr   r   r   Ú_min_distance  s    JrN   c             C   s6   dd„ }t || ƒ||ƒ||ƒƒ}|tjkr.dS |S dS )zÝ Return the minimum interparticle distance in a cloud of points
        along one of the axis.
        This is done by brute force calculation of all the distances with
        norm infinity between particle couples.
    c             S   s>   t j| jdƒ| jdƒ ƒ} | | dk } | jdkr6t jS | jƒ S )Nr   r   rH   )rH   rH   )rH   r   )rE   ÚabsrI   ÚsizeÚinfrJ   )Úar   r   r   Úaxis_min  s
    
z$_min_axis_distance.<locals>.axis_minr   N)rJ   rE   rQ   )r+   rK   rL   rS   rM   r   r   r   Ú_min_axis_distance  s
    
rT   c             C   s°  t j|dkƒrdS t| dƒs,td|  ƒ dS |\}}}}}}d||  }d||  }	d||  }
d||  }d||  }d||  }t| jd| jƒ}|j\}}}}}}d||  }d||  }d||  }|j\}}}t j||ƒsð|| | }nd}t j||ƒ�s|| | }nd}t j||ƒ�s0|| | }nd}|||f|_|j\}}}}}}d||  }d||  }d||  }d|_	|j
\}}}|| | ||	 | ||
 | f|_
dS )a£   Attempts to set the physical extents of the given module.

        The extents are given as (xmin, xmax, ymin, ymax, zmin, zmax).
        This does not work on an image plane widget, as this module does
        not have an actor.

        Once you use this function on a module, be aware that other
        modules applied on the same data source will not share the same
        scale. Thus for instance an outline module will not respect the
        outline of the actors whose extent you modified. You should pass
        in the same "extents" parameter for this to work.You can have a
        look at the wigner.py example for a heavy use of this
        functionnality.

        **Note**

        This function does not work on some specific modules, such as
        Outline, Axes, or ImagePlaneWidget. For Outline and Axes, use the
        extent keyword argument of mlab.pipeline.outline and
        mlab.pipeline.axes.
    ç        Nr?   zCannot set extents for %sg      à?r   )rU   rU   rU   )rE   Úallr0   r@   Úgetattrr?   rD   ÚscaleÚallcloseÚoriginÚposition)ÚmoduleZextentsÚxminÚxmaxZyminZymaxZzminZzmaxZxoÚyoZzoZextentxZextentyZextentzr?   ZboundsxZboundsyZboundszÚxsZysÚzsZscalexZscaleyZscalezZxcenterZycenterZzcenterZxposZyposZzposr   r   r   Ú
set_extent#  sJ    


rb   c             C   s<   ddl m} tƒ }d| }|jdks,t|ƒ‚||| d�}|S )zêStart automatic script recording.  If the `ui` parameter is
    `True`, it creates a recorder with a user interface, if not it
    creates a vanilla recorder without a UI.

    **Returns**
        The `Recorder` instance created.
    r   )Ústart_recordingz.Current engine, %s, is already being recorded.N)Úui)Úapptools.scripting.apirc   r	   ÚrecorderÚAssertionError)rd   ÚstartÚeÚmsgÚrr   r   r   rc   m  s    rc   c             C   sx   ddl m} tƒ }|j}|dk	rt||dd� t| ƒtkrVt| dƒ}|j|ƒ |jƒ  nt	| dƒrtt	| dƒrt|j| ƒ dS )	z£Stop the automatic script recording.

    **Parameters**

     :file: An open file or a filename or `None`.  If this is `None`,
            nothing is saved.

    r   )Ústop_recordingNF)ÚsaveÚwÚwriteÚflush)
re   rl   r	   rf   ÚtypeÚstrÚopenrm   Úcloser0   )ÚfileÚstopri   rk   Úfr   r   r   rl   }  s    	


rl   )r   )T)NN)T)N)#Ú__doc__rE   r   Ztvtk.apir   Zmayavi.sources.vtk_data_sourcer   Z mayavi.sources.vtk_object_sourcer   Zmayavi.core.module_managerr   Zmayavi.core.sourcer   Zmayavi.core.filterr   r
   r	   r   r   r   r   r!   r"   r$   r2   r8   r9   r:   rA   rG   rN   rT   rb   rc   rl   r   r   r   r   Ú<module>   s2   
7
*
"J
