
    j                         d Z ddlmZ ddlmZ ddlmZ  ej                  d      Zde	de	de
fd	Zdefd
Zde	de
fdZddedede	fdZddede	ddfdZy)z
units.py
--------------
Deal with physical unit systems (i.e. inches, mm)

Very basic conversions, and no requirement for
sympy.physics.units or pint.
   )	resources)log)Geometryzunits_to_inches.jsoncurrentdesiredreturnc                     t        | j                         j                               t        |j                         j                               z  S )af  
    Calculate the conversion from one set of units to another.

    Parameters
    ---------
    current : str
        Unit system values are in now (eg 'millimeters')
    desired : str
        Unit system we'd like values in (eg 'inches')

    Returns
    ---------
    conversion : float
        Number to multiply by to put values into desired units
    )to_inchstriplower)r   r   s     :/DATA/.local/lib/python3.12/site-packages/trimesh/units.pyunit_conversionr      s7    " 7==?((*+ggmmo6K6K6M.NNN    c                  <    t        t        j                               S )z
    Return a set containing all currently valid units.

    Returns
    --------
    keys
      All units with conversions i.e. {'in', 'm', ...}
    )set_lookupkeys r   r   r   r   &   s     w||~r   unitc                 0   t         j                  | j                         j                         d      }||S 	 | j	                  d      \  }}t         |j                            t        |      z  S # t        $ r}d|  d| d}Y d}~t        |      d}~ww xY w)ax  
    Calculate the conversion to an arbitrary common unit.

    Parameters
    ------------
    unit
      Either a key in `units_to_inches.json` or in the simple
      `{float} * {str}` form, i.e. "1.2 * meters". We don't
      support arbitrary `eval` of any math string

    Returns
    ----------
    conversion
      Factor to multiply by to get to an `inch` system.
    N*z:arbitrary units must be in the form "1.21 * meters", not "z" ())r   getr   r   splitfloatBaseException
ValueError)r   lookupvaluekeyEmessages         r   r
   r
   2   s    " [[++-t4F
ZZ_
ssyy{#eEl22 
 IcRSQTTUV 	 W

s   6A/ /	B8	BBobjguessc                 N   | j                   j                  dd      g}| j                  %|j                  | j                  j                         |D ]  }||j                         j                  dd      j                  dd      }dD ]  }|j                  |d      } dj                  d |D              }|j                         j                         D ]  }|t        v s|c c S   |st        d	      t        j                  d
       t        | j                        dkD  ryy)a9  
    Try to extract hints from metadata and if that fails
    guess based on the object scale.


    Parameters
    ------------
    obj
      A geometry object.
    guess
      If metadata doesn't have units make a "best guess"

    Returns
    ------------
    units
     A guess of what the units might be
    nameNunits r   z_-. c              3   *   K   | ]  }|d vs|  yw)
0123456789Nr   ).0cs     r   	<genexpr>z&units_from_metadata.<locals>.<genexpr>s   s     @$Q!<*?q$s   	z"No units and not allowed to guess!zno units: guessing from scaleg      Y@millimetersinches)metadatar   sourceappend	file_namer   replacejoinr   r   r   r   r   debugr   scale)r#   r$   hintshintdelimhs         r   units_from_metadatar=   T   s    & \\fd+,E
zzSZZ))* <zz|##GS199&#FE<<s+D ww@$@@##%AG| &  =>>
 II-.SYY%r   Nc                     | j                   t        | |      | _         t        j                  d| j                   |       t	        | j                   |      }| j                  |       || _         y)a{  
    Given an object with scale and units try to scale
    to different units via the object's `apply_scale`.

    Parameters
    ---------
    obj :  object
        With apply_scale method (i.e. Trimesh, Path2D, etc)
    desired : str
        Units desired (eg 'inches')
    guess:   bool
        Whether we are allowed to guess the units
        if they are not specified.
    N)r$   zconverting units from %s to %s)r'   r=   r   r7   r   apply_scale)r#   r   r$   
conversions       r   _convert_unitsrA      sX     yy (59	II.		7C G4J OOJCIr   )T)F)__doc__r)   r   	constantsr   parentr   get_jsonr   strr   r   r   r   r
   boolr=   rA   r   r   r   <module>rH      s       )

3
4OS O3 O5 O(	c 	# % D0X 0d 0c 0f 3  r   