3
ÌœŸWŽ&  ã               @   st   d Z ddlmZ ddlmZ dd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
G dd„ dejƒZG dd„ dejƒZdS )z'Color transfer function related code.

é    )ÚList)Útvtkc             C   s   d|t | ƒf S )NzK%s %s does not have either a "nodes" attribute or a "get_node_value" method)Ústr)ÚobjZcls_name© r   úP/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/tvtk/util/ctf.pyÚ_err_msg   s    r   c             C   sx  | }|j }|jƒ }|j\}}|j}g }t|dƒrpx’t|ƒD ]0}|j| }	|j|	ƒ\}
}}|j|	|
||gƒ q:W nTt|dƒr¶dgd }x>t|ƒD ]"}|j||ƒ |j|dd… ƒ qŽW nt	t
|dƒƒ‚|j}g }t|dƒ�rxŒt|ƒD ]&}|j| }	|j|	ƒ}|j|	|gƒ qäW nXt|dƒ�rZdgd }x@t|ƒD ]$}|j||ƒ |j|dd… ƒ �q0W nt	t
|d	ƒƒ‚||f||d
œS )zÔGiven a `tvtk.VolumeProperty` it saves the state of the RGB and
    opacity CTF to a dictionary and returns that.

    The 'rgb' key stores a list of (x, r, g, b) and the 'alpha' a list
    of (x, a) values.
    ÚnodesÚget_node_valuer   é   Né   ÚColorTransferFunctioné   ÚPiecewiseFunction)ÚrangeÚrgbÚalpha)Úrgb_transfer_functionÚget_scalar_opacityr   ÚsizeÚhasattrr	   Ú	get_colorÚappendr
   Ú	TypeErrorr   Ú	get_value)Úvolume_propertyÚvpÚctfÚotfÚs1Ús2Úncr   ÚiÚxÚrÚgÚbÚvalÚnaÚar   r   r   Ú	save_ctfs   s>    







r*   c             C   s   | d }| d }d}|j }t|tƒr2d}|jƒ  ntƒ }t|ƒ}x4t|ƒD ](}|j|| d f|| dd… žŽ  qJW |r„|j|ƒ y| d |_W n tk
r¦   Y nX t|ƒ}d}	|j	ƒ }
t|
t
ƒrÔd}	|
jƒ  nt
ƒ }
x,t|ƒD ] }|
j|| d || d ƒ qäW |	�r|j|
ƒ ||
fS )	zÁ Given the saved data produced via `save_ctfs`, this sets the
    state of the passed volume_property appropriately.

    It returns the new color transfer function and piecewise function.
    r   r   TFr   é   Nr   )r   Ú
isinstancer   Úremove_all_pointsÚlenr   Úadd_rgb_pointÚ	set_colorÚ	Exceptionr   r   Ú	add_pointÚset_scalar_opacity)Z
saved_datar   r   r)   Znew_ctfr   r!   r"   r(   Znew_otfr   r   r   r   Ú	load_ctfsA   s:    

(


 
r4   c             C   sØ   | j }| jƒ }|j}dd„ }|d |d krÐ|d |d krÐt| ƒ}|\}}||krb||f|d< n||f|d< |d }	x |	D ]}
||
d ||ƒ|
d< q|W |d }x |D ]}
||
d ||ƒ|
d< q¦W t|| ƒ\}}||fS )zÍ Given the volume_property with a new_range for the data while
    using the same transfer functions, this function rescales the
    CTF's so that everything works OK.

    It returns the CTF and OTF.
    c             S   s8   | |d  |d |d   }|d ||d |d    S )Nr   r+   r   )r#   ÚoldÚnewÚnxr   r   r   Ú_rescale_valuey   s    z$rescale_ctfs.<locals>._rescale_valuer   r+   r   r   r   )r   r   r   r*   r4   )r   Z	new_ranger   r   Z	old_ranger8   Zs_dr   r    r   Úvr   r   r   r   Úrescale_ctfso   s$     

r:   c             C   sˆ   |}|j }|jƒ }|j\}}| j}t|| ƒ|d  }xLt|ƒD ]@}	|j||	|  ƒ\}
}}|j||	|  ƒ}| j|	|
|||ƒ q@W dS )záGiven a `tvtk.LookupTable` and a `tvtk.VolumeProperty` it saves
    the state of the RGB and opacity CTF from the volume property to
    the LUT.  The number of colors to use is obtained from the LUT and
    not the CTF.
    r+   N)r   r   r   Únumber_of_colorsÚfloatr   r   Zset_table_value)Úlutr   r   r   r   r   r    r!   Údsr"   r$   r%   r&   r)   r   r   r   Úset_lut�   s    
r?   c             C   sž   |}|j }|j\}}| j}t|| ƒ|d  }tƒ }tƒ }xJt|ƒD ]>}	||	|  }
| j|	ƒ\}}}}|j|
|||ƒ |j|
|ƒ qDW |j	|ƒ |j
|ƒ dS )a  Given a `tvtk.LookupTable` and a `tvtk.VolumeProperty` it loads
    the state of the RGB and opacity CTF from the lookup table to the
    CTF.  The CTF range is obtained from the volume property and the
    number of colors to use is obtained from the LUT.
    r+   N)r   r   r;   r<   r   r   Zget_table_valuer/   r2   r0   r3   )r=   r   r   r   r   r    r!   r>   r   r"   r9   r$   r%   r&   r)   r   r   r   Úset_ctf_from_lut¡   s    

r@   c                   s<   e Zd ZdZeZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  Z	S )r   z®Overrides a few important methods that allow us to glean node
    information.  This is useful in cases where the super class does
    not have methods to get the nodes.
    c                s.   t t| ƒj|Ž }| jj|d ƒ | jjƒ  |S )ac  V.add_rgb_point(float, float, float, float) -> int
        V.add_rgb_point(float, float, float, float, float, float) -> int

        Add/Remove a point to/from the function defined in RGB or HSV
        Return the index of the point (0 based), or -1 on error.

        Wrapper around parent class functionality to store node
        information.

        r   )Úsuperr   r/   r	   r   Úsort)ÚselfÚargsÚret)Ú	__class__r   r   r/   Å   s    
z#ColorTransferFunction.add_rgb_pointc                s.   t t| ƒj|Ž }| jj|d ƒ | jjƒ  |S )ac  V.add_hsv_point(float, float, float, float) -> int
        V.add_hsv_point(float, float, float, float, float, float) -> int

        Add/Remove a point to/from the function defined in RGB or HSV
        Return the index of the point (0 based), or -1 on error.

        Wrapper around parent class functionality to store node
        information.

        r   )rA   r   Úadd_hsv_pointr	   r   rB   )rC   rD   rE   )rF   r   r   rG   Õ   s    
z#ColorTransferFunction.add_hsv_pointc                s   t t| ƒjƒ  g | _dS )zRemove all the points.
        N)rA   r   r-   r	   )rC   )rF   r   r   r-   å   s    z'ColorTransferFunction.remove_all_points)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r/   rG   r-   Ú__classcell__r   r   )rF   r   r   º   s
   r   c                   sH   e Zd ZdZeZ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd	„Z	‡  Z
S )
r   z®Overrides a few important methods that allow us to glean node
    information.  This is useful in cases where the super class does
    not have methods to get the nodes.
    c                s   t t| ƒjƒ  g | _dS )zéV.initialize()

        Clears out the current function. A newly created
        PiecewiseFunction is alreay initialized, so there is no need
        to call this method which in turn simply calls
        remove_all_points()
        N)rA   r   Ú
initializer	   )rC   )rF   r   r   rM   ú   s    zPiecewiseFunction.initializec                s,   t t| ƒj||ƒ}| jj|ƒ | jjƒ  |S )a-  V.add_point(float, float) -> int
        V.add_point(float, float, float, float) -> int

        Add/Remove points to/from the function. If a duplicate point
        is added then the function value is changed at that location.
        Return the index of the point (0 based), or -1 on error.
        )rA   r   r2   r	   r   rB   )rC   r#   r'   rE   )rF   r   r   r2     s    
zPiecewiseFunction.add_pointc                s*   t t| ƒj|ƒ}| jj|ƒ | jjƒ  |S )zòV.remove_point(float) -> int

        Add/Remove points to/from the function. If a duplicate point
        is added then the function value is changed at that location.
        Return the index of the point (0 based), or -1 on error.
        )rA   r   Úremove_pointr	   ÚremoverB   )rC   r#   rE   )rF   r   r   rN     s    
zPiecewiseFunction.remove_pointc                s   t t| ƒjƒ  g | _dS )zRemove all the points.
        N)rA   r   r-   r	   )rC   )rF   r   r   r-     s    z#PiecewiseFunction.remove_all_points)rH   rI   rJ   rK   r   r	   rM   r2   rN   r-   rL   r   r   )rF   r   r   ï   s   r   N)rK   Ú
traits.apir   Útvtk.apir   r   r*   r4   r:   r?   r@   r   r   r   r   r   r   Ú<module>   s   -.!5