ó
Ñ%�Tc           @   s°   d  Z  d d l m Z m Z m Z m Z m Z m Z m Z d „  Z	 d „  Z
 d „  Z d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d S(   sL   functions for working with 3D "vectors" (really just arrays of length three)iÿÿÿÿ(   t   sqrtt   acost   degreest   atan2t   radianst   cost   sinc         C   s1   |  d | d |  d | d |  d | d g S(   s   Add two 3D vectorsi    i   i   (    (   t   at   b(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   plus   s    c         C   s1   |  d | d |  d | d |  d | d g S(   s   Subtract two 3D vectorsi    i   i   (    (   R   R   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   minus   s    c         C   s*   t  |  d d |  d d |  d d ƒ S(   s&   Calculate the magnitude of a 3D vectori    i   i   (   R    (   R   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt	   magnitude   s    i   c         C   s   t  | t |  ƒ |  ƒ S(   sù   Set a vector to length 1 (or a custom length)
    
    ARGUMENTS:
        a - the vector to normalize
    OPTIONAL ARGUMENTS:
        length - the length to set the vector to (default 1)
    RETURNS:
        vector a normalized to length length
    (   t
   scalarProdR   (   R   t   length(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt	   normalize#   s    c         C   s   t  t |  | ƒ ƒ S(   s-   calculate the distance between two 3D vectors(   R   R
   (   R   R   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   dist0   s    c         C   sS   t  |  t ƒ s t  |  t ƒ r. | |  }  } n  |  | d |  | d |  | d g S(   sœ   Calculate the scalar product between a scalar and a 3D vector
    
    NOTE:
        this function can be called with the arguments reversed if desired
    i    i   i   (   t
   isinstancet   listt   tuple(   t   scalart   vector(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyR   5   s    c         C   s0   |  d | d |  d | d |  d | d S(   s+   Calculate the dot product of two 3D vectorsi    i   i   (    (   R   R   (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   dotProdB   s    c         C   sO   |  \ } } } | \ } } } | | | | | | | | | | | | g S(   s*   Calculate the cross product of two vectors(    (   R   R   t   axt   ayt   azt   bxt   byt   bz(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt	   crossProdG   s
    c         C   s™   t  |  | ƒ } t  | | ƒ } t d t | ƒ | ƒ } t d t | ƒ | ƒ } t | | ƒ } | d k  rt d } n | d k r‰ d } n  t t | ƒ ƒ S(   s,   Calculate the angle between three 3D vectorsi   iÿÿÿÿ(   R
   R   R   R   R   R   (   R   R   t   ct   vec1t   vec2t   dotprod(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   angleR   s    		c         C   s¼   t  | |  ƒ } t  | | ƒ } t  | | ƒ } t t | ƒ | ƒ } t | | ƒ } t | | ƒ }	 t | | ƒ }
 | } t |
 | ƒ } t |	 | ƒ } t | ƒ } | d k  r¸ | d 7} n  | S(   s'   Calculate to torsion between four atomsi    ih  (   R
   R   R   R   R   R   R   (   t   atom1t   atom2t   atom3t   atom4t   vector1t   vector2t   vector3t   y1t   y2t   yt   x1t   x2t   xt   torsion(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyR/   g   s    c         C   sA   t  | | ƒ } t  | |  ƒ } t | | ƒ } t | ƒ t | ƒ S(   s  Calculate the distance from a point to a line, where the line is defined by two points on the line
    
    ARGUMENTS:
        pointP - the point not on the line
        pointC - point 1 that defines the line
        pointN - point 2 that defines the line
    RETURNS:
        the distance from pointP to a line through pointC and pointN
    NOTE:
        the distance from point P to line CN is calculated as:
                        |(C-N) x (N-P)|
            distance = -----------------
                             |C-N|
    (   R
   R   R   (   t   pointPt   pointCt   pointNt   cMinusNt   nMinusPt   crossProduct(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt
   distToLine€   s    c         C   sk  t  | d t | ƒ ƒ } | \ } } } t | ƒ } t | ƒ } t | ƒ } t ƒ  }	 xØ |  D]Ð }
 |  |
 \ } } } | | | | | | } | | | | | | | | | | | } | | | | | | | | | | | } | | | | | | | | | | | } | | | g |	 |
 <q\ W| d k	 rgx( |	 D] }
 t | |	 |
 ƒ |	 |
 <qCWn  |	 S(   sÀ  Rotate a group of atoms about a specified axis
    
    ARGUMENTS:
        atoms        - the list of atoms to rotate
        axis         - the vector to rotate the atoms about
        angle        - the angle to rotate the atoms by
    OPTIONAL ARGUMENTS:
        translate    - if given, all atoms are tranlated by this vector *after* the rotation is performed
    RETURNS:
        rotatedAtoms - the rotated (and possibly translated) atoms
    g      ð?N(   R   R   R   R   R   t   dictt   NoneR	   (   t   atomst   axisR!   t	   translatet   ut   vt   wt   cosThetat   sinThetat   rotatedAtomst   curAtomR.   R+   t   zR   t   newXt   newYt   newZ(    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   rotateAtoms˜   s"    	...N(   t   __doc__t   mathR    R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R!   R/   R6   R8   RG   (    (    (    sW   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/python2.7/site-packages/coot/rcrane/strucCalc.pyt   <module>   s   4										