ó
ÉÝ{Tc           @   sœ   d  Z  d d l Td Z d e Z d „  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 „  Z d S(   sB  
     pygl/util.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2005 University of York, CCLRC

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
iÿÿÿÿ(   t   *g-DTû!	@g     €f@c         C   s­  g  } | j  | d |  d ƒ | j  | d |  d ƒ | j  | d |  d ƒ d d d g } t | | ƒ } | d | d | d | d | d | d d k  rad d d g } t | | ƒ } | d | d | d | d | d | d d k  rad d d g } t | | ƒ } | d | d | d | d | d | d d k  r^d GHq^qan  t | ƒ } t | | ƒ } t | d | ƒ}	 t | d | ƒ}
 |	 |
 | f S(   sµ   
     Calculate position equation of
     line between two points and vertices
     of cuboid approximation to cylinder.
     No longer used by MG which uses a C
     version.
     i    i   i   g•Ö&è.>s3   Error cannot find suitable axis. Coincident points?t   radius(   t   appendt   vcrosst	   normalize(   t   v1t   v2t   sizet   linet   zaxist   at   yaxist   xaxist   bt   ct   d(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   calc_cylinder_vertices   s&    888c         C   sü   |  d  \ } } } | d  \ } } } | | } | | }	 | | }
 t  | | |	 |	 |
 |
 ƒ } | d k  r… | } | } | } n | | } |	 | } |
 | } | | | g }  d d d g } t |  | ƒ } t |  | ƒ } t | ƒ t } | | | f S(   s:   
     Work out how to align along a particular line.
     i   g•Ö&è.>g        g      ð?(   t   sqrtR   t   vdott   acost
   RAD_TO_DEG(   R   R   t   x1t   y1t   z1t   x2t   y2t   z2t   rxt   ryt   rzR   t   vxt   vyt   vzt   qt   st   omega(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt	   calc_line?   s&    


 	


c         C   s…   g  } | j  |  d | d | d |  d ƒ | j  |  d | d | d |  d ƒ | j  |  d | d | d |  d ƒ | S(   s1   
     Vector cross product. For 3d vectors.
     i   i   i    (   R   (   R   R   R!   (    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyR   Z   s
    )))c         C   si   t  |  ƒ } t  | ƒ } | | k r2 d GHd GHd Sd } x* t | ƒ D] } | |  | | | } qE W| S(   s   
     Vector dot product.
     s!   Error, trying to find dot products   of differet length vectors.g        (   t   lent   range(   R   R   t   order1t   order2R"   t   i(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyR   d   s    g      ð?c         C   sÈ   t  |  ƒ } d } x* t | ƒ D] } | |  | |  | } q Wt | ƒ } | d k  rŽ d G|  GHg  } x! t | ƒ D] } | j d ƒ qs W| Sg  } x- t | ƒ D] } | j |  | | | ƒ q¡ W| S(   s    
    Normalize an nD vector
    g        g»½×Ùß|Û=s   zero length vector in normalize(   R%   R&   R   R   (   t   vR   t   ordert   dsqR)   R   t   vnew(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyR   t   s    	c         C   sh   t  |  ƒ } d } x* t | ƒ D] } | |  | |  | } q Wt | ƒ } | d k  r` d GHd Sd | S(   Ng        g»½×Ùß|Û=s*   zero length vector in get_normalize_factori    g      ð?(   R%   R&   R   (   R*   R+   R,   R)   R   (    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   get_normalize_factor‹   s    c         C   sa   g  } xT t  t |  ƒ ƒ D]@ } | t | ƒ k r: d } n
 | | } | j |  | | ƒ q W| S(   Ng        (   R&   R%   R   (   R   R   R*   R)   t   add(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   vaddv–   s    	
c         C   sa   g  } xT t  t |  ƒ ƒ D]@ } | t | ƒ k r: d } n
 | | } | j |  | | ƒ q W| S(   Ng        (   R&   R%   R   (   R   R   R*   R)   R/   (    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   vsubv    s    	
c         C   sž   x) t  |  ƒ t  | ƒ k r+ | j d ƒ q Wd d d d g } xY t t  | ƒ ƒ D]E } x< t t  |  ƒ ƒ D]( } | | | | |  | | | | <qj WqQ W| S(   Ng        i    (   R%   R   R&   (   t   matrixt   vectort   resultR)   t   j(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   matrixXvectorª   s    *c         C   sk   d d  l  } | j | | ƒ } xE t | ƒ D]7 } x. t | ƒ D]  } |  | | | | | | <q? Wq, W| S(   Niÿÿÿÿ(   t   point_funcst   doubleaR&   (   t   Mt   nt   mR7   R2   R)   R5   (    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   buildmatrixµ   s    "c         C   s  |  d d  \ } } } |  d d  \ } } } |  d d  \ } } }	 | | |	 | |	 | | | | }
 | | | | | | |	 | | } | | | | | | | | | } | | |	 | | | | | | |	 | | | | | } |
 | | | f S(   sj   
     Given three points in 3D space
     it returns A,B,C,D in the plane
     equation Ax+By+Cz+D=0
     i    i   i   i   (    (   t   verticesR   R   R   R   R   R   t   x3t   y3t   z3t   At   Bt   Ct   D(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   define_plane¿   s    &&&?c         C   sN  | d } | d } |  d } |  d } |  d } |  d } | | d | | d | | d | } | | d | d | | d | d | | d | d }	 t  |	 ƒ d k  r¿ d GHd St | ƒ t |	 ƒ }
 g  } | j | d |
 | d | d ƒ | j | d |
 | d | d ƒ | j | d |
 | d | d ƒ | S(   sD   
     Calculate the point where plane
     and line intersect.
     i    i   i   i   g»½×Ùß|Û=s/   Error: Line parallel with plane in intersection(   i    i    i    (   t   abst   floatR   (   t   planeR   t   p0t   p1R
   R   R   R   t   numt   dent   ut   p(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   line_plane_intersectionÏ   s"    
 

 
 
 
*>%%%c         C   s  t  |  d |  d  g ƒ } d } d } d } t t t |  d  ƒ | ƒ ƒ } t t t |  d  ƒ | ƒ ƒ } t t t |  d  ƒ | ƒ ƒ } | t k r¥ d t | } n  | t k rÂ d t | } n  | t k rß d t | } n  | t d k rü t | } n  | t d k rt | } n  | t d k r6t | } n  | } | }	 | | k r_| } | }	 n  | | k r{| } | }	 n  t t |  d  ƒ |	 ƒ }
 g  } | j | d |
 d ƒ | j | d |
 d ƒ | j | d |
 d ƒ t t |  d  ƒ |
 ƒ }
 g  } | j | d |
 d ƒ | j | d |
 d ƒ | j | d |
 d ƒ t | | | g ƒ } t |  d  ƒ } t | d  ƒ } t	 |  d | d k ƒ rgt	 |  d | d k ƒ rt	 |  d | | d | |  d | | d | ƒ d	 k  rd
 GHqn  t	 |  d | d k ƒ rgt	 |  d | | d | |  d | | d | ƒ d	 k  rdd
 GHqdqgn  t	 |  d | d k ƒ rçt	 |  d | d k ƒ rçt	 |  d | | d | |  d | | d | ƒ d	 k  räd
 GHqäqçn  | | d k  r| | | f S| | | f Sd S(   s>   
     Find three (totally arbitrary!) points on a plane.
     i    i   g      ð?g        i   g      à?i   g»½×Ùß|Û=gÍÌÌÌÌÌì?s   Error finding plane pointsN(   i    i    i    (   g      ð?g        g        (   g        g      ð?g        (   g        g        g      ð?(
   RO   R   R   R   t   piR   R   RE   R.   RF   (   RH   RI   R   R   R	   t   axt   ayt   azt   maxat   axisR*   RJ   t   p2t	   new_planet   opfact   npfac(    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   find_points_on_planeã   sl      

>>>N(   t   __doc__t   matht   PIR   R   R$   R   R   R   R.   R0   R1   R6   R<   RE   RO   RZ   (    (    (    s6   /opt/xtal/ccp4-6.5-linux32/lib/py2/ccp4mg/pygl/util.pyt   <module>   s    

	!		
			
	
		
		