U
    Vc{                     @   sp   d 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	 Zd
d ZdddZdddZdS )zFMesh summaries and TensorFlow operations to create them.

V2 versions
    N)tf2)summary_pb2)metadata)plugin_data_pb2)tensor_utilc           	   	   C   sX   t j|d}|j }dd |D }tj| d|||||d}t jjt| ||||dS )a  Creates a tensor summary with summary metadata.

    Args:
      name: A name for this summary. The summary tag used for TensorBoard will
        be this name prefixed by any active name scopes.
      description: Optional long-form description for this summary, as a
        constant `str`. Markdown is supported. Defaults to empty.
      tensor: Tensor to display in summary.
      content_type: Type of content inside the Tensor.
      components: Bitmask representing present parts (vertices, colors, etc.) that
        belong to the summary.
      json_config: A string, JSON-serialized dictionary of ThreeJS classes
        configuration.
      step: Explicit `int64`-castable monotonic step value for this summary. If
        omitted, this defaults to `tf.summary.experimental.get_step()`, which must
        not be None.

    Returns:
      A boolean indicating if summary was saved successfully or not.
    )valuec                 S   s   g | ]}|d k	r|ndqS N .0Zdimr
   r
   G/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/plugins/mesh/summary_v2.py
<listcomp>6   s     z"_write_summary.<locals>.<listcomp>Njson_config)tagtensorstepr   )	tfZconvert_to_tensorshapeZas_listr   create_summary_metadatasummarywriteget_instance_name)	namedescriptionr   content_type
componentsr   r   r   Ztensor_metadatar
   r
   r   _write_summary   s$    
	
r   c                 C   s   d}| dk	rt j| dd}|S )z6Parses and returns JSON string from python dictionary.z{}NT)	sort_keys)jsondumps)config_dictr   r
   r
   r   _get_json_configH   s    r#   c              
   C   s   t |}t|tjjtjt|tjjtj	t|tjj
tjg}dd |D }tdd |D }	ttjjddpztjj}
d}|
| d|d, |D ] }|ot| ||j|j|	||}qW 5 Q R X |S )	a  Writes a TensorFlow mesh summary.

    Args:
      name: A name for this summary. The summary tag used for TensorBoard will
        be this name prefixed by any active name scopes.
      vertices: Tensor of shape `[dim_1, ..., dim_n, 3]` representing the 3D
        coordinates of vertices.
      faces: Tensor of shape `[dim_1, ..., dim_n, 3]` containing indices of
        vertices within each triangle.
      colors: Tensor of shape `[dim_1, ..., dim_n, 3]` containing colors for each
        vertex.
      config_dict: Dictionary with ThreeJS classes names and configuration.
      step: Explicit `int64`-castable monotonic step value for this summary. If
        omitted, this defaults to `tf.summary.experimental.get_step()`, which must
        not be None.
      description: Optional long-form description for this summary, as a
        constant `str`. Markdown is supported. Defaults to empty.

    Returns:
      True if all components of the mesh were saved successfully and False
        otherwise.
    c                 S   s   g | ]}|j d k	r|qS Ndatar   r   r
   r
   r   r      s     
 zmesh.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r
   r   r'   r
   r
   r   r      s     summary_scopeNTZmesh_summary)values)r#   r   
MeshTensorr   MeshPluginDataVERTEXr   float32FACEint32COLORuint8get_components_bitmaskgetattrr   Zexperimentalr)   r   r&   r   )r   verticesfacescolorsr"   r   r   r   tensorsr   r)   Zall_successr   r
   r
   r   meshP   sJ          
r9   c              
   C   s   t |}g }t|tjjtjt|tjjtj	t|tjj
tjg}dd |D }tdd |D }	|D ]d}
|
jj}dd |D }tj|
j|
jd}tj| d|
j|	|||d}t| |
j}||||f qlt }|D ]\}}}|jj|||d q|S )	a  Create a mesh summary to save in pb format.

    Args:
      tag: String tag for the summary.
      vertices: numpy array of shape `[dim_1, ..., dim_n, 3]` representing the 3D
        coordinates of vertices.
      faces: numpy array of shape `[dim_1, ..., dim_n, 3]` containing indices of
        vertices within each triangle.
      colors: numpy array of shape `[dim_1, ..., dim_n, 3]` containing colors for
        each vertex.
      config_dict: Dictionary with ThreeJS classes names and configuration.
      description: Optional long-form description for this summary, as a
        constant `str`. Markdown is supported. Defaults to empty.

    Returns:
      Instance of tf.Summary class.
    c                 S   s   g | ]}|j d k	r|qS r$   r%   r'   r
   r
   r   r      s     
 zmesh_pb.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r
   r(   r'   r
   r
   r   r      s     c                 S   s   g | ]}|d k	r|ndqS r   r
   r   r
   r
   r   r      s     )ZdtypeNr   )r   r   r   )r#   r   r+   r   r,   r-   r   r.   r/   r0   r1   r2   r3   r&   r   r   Zmake_tensor_protoZ	data_typer   r   r   appendr   ZSummaryr   add)r   r5   r6   r7   r"   r   r   Z	summariesr8   r   r   r   Ztensor_protoZsummary_metadataZinstance_tagr   r
   r
   r   mesh_pb   s`           	  r<   )NNNNN)NNNN)__doc__r    Ztensorboard.compatr   r   Ztensorboard.compat.protor   Ztensorboard.plugins.meshr   r   Ztensorboard.utilr   r   r#   r9   r<   r
   r
   r
   r   <module>   s&   +     
J       