ó
v™xTc           @   se  d  Z  d d l m Z y d d l Z Wn e k
 r? d Z n Xe d k	 r„ d d l Z d d l m Z d d l m	 Z	 d „  Z
 n d d l Z d „  Z	 d „  Z
 d d l Z d	 e f d
 „  ƒ  YZ e ƒ  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d „  Z d „  Z d d d „ Z d d d e d „ Z d S(   s®  Python version of a subset of Roy Featherstone's spatial_v1 matlab code:

  http://axiom.anu.edu.au/~roy/spatial/

  Version 1: January 2008 (latest bug fix: 7 October 2008)

The subset of converted files covers all dependencies of:
  ID.m
  FDab.m

The original matlab comments are preserved as Python docstrings.

See also:
  Rigid Body Dynamics Algorithms.
  Roy Featherstone,
  Springer, New York, 2007.
  ISBN-10: 0387743146
iÿÿÿÿ(   t   divisionN(   t   matrix(   t   ifloorc         C   s=   t  j t j j j d |  j ƒ  d d d d ƒ j ƒ  j ƒ  ƒ S(   Nt   mt   relative_epsilongê-�™—q=t   absolute_epsilon(	   R   t   sqrt   scitbxt   linalgt   eigensystemt   real_symmetrict   as_flex_double_matrixt   generalized_inverse_as_packed_ut   matrix_packed_u_as_symmetric(   R   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   generalized_inverse    s
    c         C   s   d „  } | t  j |  ƒ ƒ S(   Nc         S   s(   |  d k  r t  |  d ƒ St  |  d ƒ S(   Ni    g      à?(   t   int(   t   x(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   iround.   s     (   t   matht   floor(   R   R   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyR   -   s    	c         C   s
   |  j  ƒ  S(   N(   t   inverse(   R   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyR   3   s    t   InfTypec           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyR   8   s    c      %   C   s™   t  j |  ƒ } t  j |  ƒ } t j d d d d d d d | | d d d d | | d d d d d d d d d d d d d | | d d d d | | f$ ƒ S(   s  
% Xrotx  spatial coordinate transform (X-axis rotation).
% Xrotx(theta) calculates the coordinate transform matrix from A to B
% coordinates for spatial motion vectors, where coordinate frame B is
% rotated by an angle theta (radians) relative to frame A about their
% common X axis.
  i   i    (   R   t   cost   sinR   R   (   t   thetat   ct   s(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   Xrotx;   s    c      %   C   s™   t  j |  ƒ } t  j |  ƒ } t j | d | d d d d d d d d d | d | d d d d d d | d | d d d d d d d d d | d | f$ ƒ S(   s  
% Xroty  spatial coordinate transform (Y-axis rotation).
% Xroty(theta) calculates the coordinate transform matrix from A to B
% coordinates for spatial motion vectors, where coordinate frame B is
% rotated by an angle theta (radians) relative to frame A about their
% common Y axis.
  i    i   (   R   R   R   R   R   (   R   R   R   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   XrotyM   s    c      %   C   s™   t  j |  ƒ } t  j |  ƒ } t j | | d d d d | | d d d d d d d d d d d d d | | d d d d | | d d d d d d d f$ ƒ S(   s  
% Xrotz  spatial coordinate transform (Z-axis rotation).
% Xrotz(theta) calculates the coordinate transform matrix from A to B
% coordinates for spatial motion vectors, where coordinate frame B is
% rotated by an angle theta (radians) relative to frame A about their
% common Z axis.
  i    i   (   R   R   R   R   R   (   R   R   R   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   Xrotz_   s    c   
   %   C   sš   |  \	 } } } } } } } } }	 t  j | | | d d d | | | d d d | | |	 d d d d d d | | | d d d | | | d d d | | |	 f$ ƒ S(   s=   
  Featherstone (2007) Tab. 2.2
  Added in Python version.
  i    (   R   R   (
   t   Et   at   bR   t   dt   et   ft   gt   ht   i(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   Xrotq   s    !c      %   C   s‹   |  \ } } } t  j d d d d d d d d d d d d d d d d d d d | | d d d | d | d d d | | d d d d f$ ƒ S(   s  
% Xtrans  spatial coordinate transform (translation of origin).
% Xtrans(r) calculates the coordinate transform matrix from A to B
% coordinates for spatial motion vectors, in which frame B is translated by
% an amount r (3D vector) relative to frame A.
  i   i    (   R   R   (   t   rt   r1t   r2t   r3(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   Xtrans   s    c      %   C   sš   |  \ } } } } } } t  j d | | d d d | d | d d d | | d d d d d | | d | | | d | | d | | | d | | d f$ ƒ S(   s¶   
% crm  spatial cross-product operator (motion).
% crm(v) calculates the 6x6 matrix such that the expression crm(v)*m is the
% cross product of the spatial motion vectors v and m.
  i    (   R   R   (   t   vt   v1t   v2t   v3t   v4t   v5t   v6(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   crm�   s    c         C   s   t  |  ƒ j ƒ  S(   sÐ   
% crf  spatial cross-product operator (force).
% crf(v) calculates the 6x6 matrix such that the expression crf(v)*f is the
% cross product of the spatial motion vector v with the spatial force
% vector f.
  (   R6   t	   transpose(   R/   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   crfž   s    c      
   C   sˆ   | \ } } } t  j d | | | d | | | d f	 ƒ } t  j | |  | | j ƒ  |  | |  | j ƒ  |  t  j d ƒ f ƒ j ƒ  S(   sû   
% mcI  spatial rigid-body inertia from mass, CoM and rotational inertia.
% mcI(m,c,I) calculates the spatial inertia matrix of a rigid body from its
% mass, centre of mass (3D vector) and rotational inertia (3x3 matrix)
% about its centre of mass.
  i    i   (   R   R   R7   t   identityt   resolve_partitions(   R   R   t   It   c1t   c2t   c3t   C(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   mcI§   s    

t   autoTreec           B   s   e  Z d  d d  d „ Z RS(   i   i    c   
      C   st  | |  _  d g | |  _ d
 g | |  _ g  |  _ g  |  _ g  } x,t | ƒ D]} t | d t j	 | ƒ | ƒ d |  j | <|  j | d k rµ |  j j
 t d d d g ƒ ƒ n4 |  j j
 t | ƒ t | |  j | d d g ƒ ƒ | j
 | | ƒ | d | } | | t j d d d g ƒ } | | | d t j d d d g ƒ }	 |  j j
 t | | |	 ƒ ƒ qN Wd
 S(   s€  
% autoTree  Create System Models of Kinematic Trees
% autoTree(nb,bf,skew,taper) creates system models of kinematic trees
% having revolute joints.  nb and bf specify the number of bodies in the
% tree, and the branching factor, respectively.  The latter is the average
% number of children of a nonterminal node, and must be >=1.  bf=1 produces
% an unbranched tree; bf=2 produces a binary tree; and non-integer values
% produce trees in which the number of children alternates between
% floor(bf) and ceil(bf) in such a way that the average is bf.  Trees are
% constructed (and numbered) breadth-first.  Link i is a thin-walled
% cylindrical tube of length l(i), radius l(i)/20, and mass m(i), lying
% between 0 and l(i) on the x axis of its local coordinate system.  The
% values of l(i) and m(i) are determined by the tapering coefficient:
% l(i)=taper^(i-1) and m(i)=taper^(3*(i-1)).  Thus, if taper=1 then
% m(i)=l(i)=1 for all i.  The inboard joint axis of link i lies on the
% local z axis, and its outboard axis passes through the point (l(i),0,0)
% and is rotated about the x axis by an angle of skew radians relative to
% the inboard axis.  If the link has more than one outboard joint then they
% all have the same axis.  If skew=0 then the mechanism is planar.  The
% final one, two or three arguments can be omitted, in which case they
% assume default values of taper=1, skew=0 and bf=1.
    i    i   iÿÿÿÿi   g      à?i   g{®Gázd?g=
×£p=ð?i   Ng§tÚ@§µ?g§tÚ@§µ?(   t   NBt   pitcht   Nonet   parentt   XtreeR;   t   xrangeR   R   t   ceilt   appendR.   R   R   t   colt   diagR@   (
   t   selft   nbt   bft   skewt   tapert   len_R(   t   masst   CoMt   Icm(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   __init__¹   s     			,"4 ((   R   R   RU   (    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyRA   ·   s   c         C   s÷   t  |  t t t f ƒ s( |  j d | ƒ S|  d k rd t | ƒ } t j d d d d d d g ƒ } n‰ |  t k r© t	 d d | g ƒ } t j d d d d d d g ƒ } nD t | ƒ t	 d d | |  g ƒ } t j d d d d d |  g ƒ } | | f S(   s_  
% jcalc  Calculate joint transform and motion subspace.
% [Xj,S]=jcalc(pitch,q) calculates the joint transform and motion subspace
% matrices for a revolute (pitch==0), prismatic (pitch==inf) or helical
% (pitch==any other value) joint.  For revolute and helical joints, q is
% the joint angle.  For prismatic joints, q is the linear displacement.
  t   qi    i   (
   t
   isinstanceR   t   floatR   t   Xj_SR   R   RJ   t   InfR.   (   RC   RV   t   Xjt   S(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   jcalcâ   s    $$#!c         C   si   |  d  k r+ t j d d d d d d g ƒ St |  ƒ }  t |  ƒ d k sO t ‚ t j d d d g |  ƒ S(   Ni    g…ëQ¸ž#Ài   (   RD   R   RJ   t   listt   lent   AssertionError(   t	   grav_accn(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   grav_accn_as_a_grav÷   s
    c         C   s±  t  | ƒ } d g |  j } d g |  j } d g |  j }	 d g |  j }
 d g |  j } x•t |  j ƒ D]„} t |  j | | | ƒ \ } | | <| | d k r½ | | } | | } n$ | | | | } | | | | } | |  j | | | <|  j | d k r-| |	 | <| | | | |
 | <nT | | |	 |  j | | |	 | <| | |
 |  j | | t |	 | ƒ | |
 | <|  j	 | |
 | t
 |	 | ƒ |  j	 | |	 | | | <| d k	 rl | | d k	 rl | | | | | | <ql ql Wd g |  j } x¦ t |  j d d d ƒ D]‹ } | | d k rE| | | | <n | | j ƒ  | | | | <|  j | d k r| |  j | | | j ƒ  | | | |  j | <qqW| S(   sL  
% ID  Inverse Dynamics via Recursive Newton-Euler Algorithm
% ID(model,q,qd,qdd,f_ext,grav_accn) calculates the inverse dynamics of a
% kinematic tree via the recursive Newton-Euler algorithm.  q, qd and qdd
% are vectors of joint position, velocity and acceleration variables; and
% the return value is a vector of joint force variables.  f_ext is a cell
% array specifying external forces acting on the bodies.  If f_ext == {}
% then there are no external forces; otherwise, f_ext{i} is a spatial force
% vector giving the force acting on body i, expressed in body i
% coordinates.  Empty cells in f_ext are interpreted as zero forces.
% grav_accn is a 3D vector expressing the linear acceleration due to
% gravity.  The arguments f_ext and grav_accn are optional, and default to
% the values {} and [0,0,-9.81], respectively, if omitted.
  iÿÿÿÿi   N(   Rb   RD   RB   RG   R]   RC   RF   RE   R6   R;   R8   R7   (   t   modelRV   t   qdt   qddt   f_extRa   t   a_gravR\   t   XupR/   R!   R%   R(   t   XJt   vJt   aJt   tau(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   IDþ   s<    $

!3: 9c         C   s  t  | ƒ } d g |  j } d g |  j }	 d g |  j }
 d g |  j } d g |  j } d g |  j } d g |  j } xÈt |  j ƒ D]·} t |  j | | | ƒ \ } | | <| | d k rÓ | | } n | | | | } | |  j | |	 | <|  j | d k rM|	 | |
 | <| | | <t j	 d d d d d d g ƒ | | <nV |	 | |
 |  j | |
 | <|	 | | |  j | | | | <t
 | | ƒ | | | <|  j | | | <t | | ƒ |  j | | | | | <| d k	 rŒ | | d k	 rŒ | s| | | | | | <qC| | |
 | j ƒ  j ƒ  | | | | <qŒ qŒ Wd g |  j } d g |  j } d g |  j } xþt |  j d d d ƒ D]ã} | | d k r| | | | <| | } | d k sÛ| | d k rí| | | | <q”| | | | | | <nŽ | | | | | | <| | j ƒ  | | } | d k sP| | d k rp| | j ƒ  | | | | <n$ | | | | j ƒ  | | | | <t | ƒ | | <|  j | d k r‘| | | | | | | | j ƒ  } | | | | | | | | | | | } | |  j | |	 | j ƒ  | |	 | | |  j | <| |  j | |	 | j ƒ  | | |  j | <q‘q‘Wd g |  j } d g |  j } xà t |  j ƒ D]Ï } |  j | d k rß|	 | | | | | | <n% |	 | | |  j | | | | | <| | | | | | j ƒ  | | | | <| | d k rY| | | | | | <q¨| | | | | | | | <q¨W| S(   sD  
% FDab  Forward Dynamics via Articulated-Body Algorithm
% FDab(model,q,qd,tau,f_ext,grav_accn) calculates the forward dynamics of a
% kinematic tree via the articulated-body algorithm.  q, qd and tau are
% vectors of joint position, velocity and force variables; and the return
% value is a vector of joint acceleration variables.  f_ext is a cell array
% specifying external forces acting on the bodies.  If f_ext == {} then
% there are no external forces; otherwise, f_ext{i} is a spatial force
% vector giving the force acting on body i, expressed in body i
% coordinates.  Empty cells in f_ext are interpreted as zero forces.
% grav_accn is a 3D vector expressing the linear acceleration due to
% gravity.  The arguments f_ext and grav_accn are optional, and default to
% the values {} and [0,0,-9.81], respectively, if omitted.
  iÿÿÿÿi    i   N(   Rb   RD   RB   RG   R]   RC   RF   RE   R   RJ   R6   R;   R8   R   R7   R   (   Rc   RV   Rd   Rl   Rf   Ra   t   f_ext_in_ffRg   R\   Rh   t   X0R/   R   t   IAt   pAR(   Ri   Rj   t   Ut   d_invt   uR#   t   Iat   paR!   Re   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   FDab3  sz    $
(!'1 
 $(.('%,"(   t   __doc__t
   __future__R    R   t   ImportErrorRD   t   scitbx.mathR   t   libtbx.math_utilsR   R   t   scitbx_matrixR   t   objectR   RZ   R   R   R   R)   R.   R6   R8   R@   RA   R]   Rb   Rm   t   FalseRw   (    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/rigid_body/proto/featherstone.pyt   <module>   s:   
												+		5