Changeset 595


Ignore:
Timestamp:
11/30/11 18:36:52 (6 months ago)
Author:
ddangelo
Message:

hpr to matrix and matrix to hpr added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/avango-osg/python/avango/osg/OSGMatrix.cpp

    r391 r595  
    7474    return ss.str(); 
    7575  } 
     76 
     77  //taken from delta3D 
     78  osg::Matrix HprToMatrix(const osg::Vec3& hpr ) 
     79  { 
     80     //implementation converted from plib's sg.cxx 
     81     //PLIB - A Suite of Portable Game Libraries 
     82     //Copyright (C) 1998,2002  Steve Baker 
     83     //For further information visit http://plib.sourceforge.net 
     84 
     85      osg::Matrix rotation; 
     86 
     87     double ch, sh, cp, sp, cr, sr, srsp, crsp, srcp ; 
     88 
     89     // this can't be smart for both 32 and 64 bit types. 
     90     ///\todo find a preprocessor way to assign this constant different for the different precision types. 
     91     const osg::Vec3::value_type magic_epsilon = 0.00001; 
     92 
     93     if ( osg::equivalent(hpr[0],(osg::Vec3::value_type)0.0,magic_epsilon) ) 
     94     { 
     95        ch = 1.0 ; 
     96        sh = 0.0 ; 
     97     } 
     98     else 
     99     { 
     100        sh = sinf(hpr[0]); 
     101        ch = cosf(hpr[0]); 
     102     } 
     103 
     104     if ( osg::equivalent(hpr[1],(osg::Vec3::value_type)0.0,magic_epsilon) ) 
     105     { 
     106        cp = 1.0 ; 
     107        sp = 0.0 ; 
     108     } 
     109     else 
     110     { 
     111        sp = sinf(hpr[1]); 
     112        cp = cosf(hpr[1]); 
     113     } 
     114 
     115     if ( osg::equivalent(hpr[2],(osg::Vec3::value_type)0.0,magic_epsilon) ) 
     116     { 
     117        cr   = 1.0 ; 
     118        sr   = 0.0 ; 
     119        srsp = 0.0 ; 
     120        srcp = 0.0 ; 
     121        crsp = sp ; 
     122     } 
     123     else 
     124     { 
     125        sr   = sinf(hpr[2]); 
     126        cr   = cosf(hpr[2]); 
     127        srsp = sr * sp ; 
     128        crsp = cr * sp ; 
     129        srcp = sr * cp ; 
     130     } 
     131 
     132     rotation(0, 0) = (  ch * cr - sh * srsp ) ; 
     133     rotation(1, 0) = ( -sh * cp ) ; 
     134     rotation(2, 0) = (  sr * ch + sh * crsp ) ; 
     135 
     136     rotation(0, 1) = ( cr * sh + srsp * ch ) ; 
     137     rotation(1, 1) = ( ch * cp ) ; 
     138     rotation(2, 1) = ( sr * sh - crsp * ch ) ; 
     139 
     140     rotation(0, 2) = ( -srcp ) ; 
     141     rotation(1, 2) = (  sp ) ; 
     142     rotation(2, 2) = (  cr * cp ) ; 
     143 
     144     rotation(3, 0) =  0.0;  // x trans 
     145     rotation(3, 1) =  0.0;  // y trans 
     146     rotation(3, 2) =  0.0;  // z trans 
     147 
     148     rotation(0, 3) =  0.0; 
     149     rotation(1, 3) =  0.0; 
     150     rotation(2, 3) =  0.0; 
     151     rotation(3, 3) =  1.0; 
     152 
     153     return rotation; 
     154  } 
     155 
     156  float ClampUnity( float x ) 
     157  { 
     158     if ( x >  1.0f ) return  1.0f; 
     159     if ( x < -1.0f ) return -1.0f; 
     160     return x ; 
     161  } 
     162 
     163  //taken from delta3D 
     164  osg::Matrix MatrixToHpr( osg::Vec3& hpr ) 
     165  { 
     166     //implementation converted from plib's sg.cxx 
     167     //PLIB - A Suite of Portable Game Libraries 
     168     //Copyright (C) 1998,2002  Steve Baker 
     169     //For further information visit http://plib.sourceforge.net 
     170 
     171     osg::Matrix rotation; 
     172 
     173     osg::Matrix mat; 
     174 
     175     osg::Vec3 col1(rotation(0, 0), rotation(0, 1), rotation(0, 2)); 
     176     double s = col1.length(); 
     177 
     178     const double magic_epsilon = 0.00001; 
     179     if ( s <= magic_epsilon ) 
     180     { 
     181        hpr.set(0.0f, 0.0f, 0.0f); 
     182        return osg::Matrix(); 
     183     } 
     184 
     185 
     186     double oneOverS = 1.0f / s; 
     187     for( int i = 0; i < 3; i++ ) 
     188        for( int j = 0; j < 3; j++ ) 
     189           mat(i, j) = rotation(i, j) * oneOverS; 
     190 
     191 
     192     double sin_pitch = ClampUnity(mat(1, 2)); 
     193     double pitch = asin(sin_pitch); 
     194     hpr[1] = pitch; 
     195 
     196     double cp = cos(pitch); 
     197 
     198     if ( cp > -magic_epsilon && cp < magic_epsilon ) 
     199     { 
     200        double cr = ClampUnity(-mat(2,1)); 
     201        double sr = ClampUnity(mat(0,1)); 
     202 
     203        hpr[0] = 0.0f; 
     204        hpr[2] = atan2(sr,cr); 
     205     } 
     206     else 
     207     { 
     208        double one_over_cp = 1.0 / cp ; 
     209        double sr = ClampUnity(-mat(0,2) * one_over_cp); 
     210        double cr = ClampUnity(mat(2,2) * one_over_cp); 
     211        double sh = ClampUnity(-mat(1,0) * one_over_cp); 
     212        double ch = ClampUnity(mat(1,1) * one_over_cp); 
     213 
     214        if ( ( osg::equivalent(sh,0.0,magic_epsilon) && osg::equivalent(ch,0.0,magic_epsilon) ) || 
     215             ( osg::equivalent(sr,0.0,magic_epsilon) && osg::equivalent(cr,0.0,magic_epsilon) ) ) 
     216        { 
     217           cr = ClampUnity(-mat(2,1)); 
     218           sr = ClampUnity(mat(0,1));; 
     219 
     220           hpr[0] = 0.0f; 
     221        } 
     222        else 
     223        { 
     224          hpr[0] = atan2(sh, ch); 
     225        } 
     226 
     227        hpr[2] = atan2(sr, cr); 
     228     } 
     229     return rotation; 
     230  } 
     231 
    76232} 
    77233 
     
    191347  def("make_frustum_mat", ::osg::Matrix::frustum); 
    192348  def("make_ortho_mat", ::osg::Matrix::ortho); 
     349  def("hpr_to_matrix", HprToMatrix); 
     350  def("matrix_to_hpr", MatrixToHpr); 
     351 
     352 
     353 
    193354 
    194355 
Note: See TracChangeset for help on using the changeset viewer.