The replacement of that inline function with a macro removes the type-safety
offered by C++ and makes the code more accident prone. I did the
static-casts with the multiple parameters because the parameters to the
function aren't necessarily always the same type. Looking into it further, a
single parameter, which is manually specified when in doubt, seems to be the
smartest solution, since it makes it explicit at the point of the call what
precision is being used when it is ambiguous. I've attached files for an
updated function which combines the best of all solutions so far: template
function with a single parameter, body uses osg::absolute to simplify
expression, and one explicit specification of the template parameter in
calling code. Simpler and more explicit all around, I think - hopefully this
will be acceptable. It build cleanly here: windows, vc9, maya 2012 x64
Ryan
On Tue, May 24, 2011 at 5:20 PM, Javier Taibo <[email protected]>wrote:
> Thanks for the fixes. Changes are now committed to svn trunk.
>
> About the second issue, it seems a much complicated piece of code
> for what it's doing. I suggest this lighter and cleaner alternative
> instead of a template with three different typename parameters and the
> huge expression with four static_cast.
>
> #define inTolerance(base, match, tolerance) (
> osg::absolute((base)-(match)) < tolerance )
>
> However, this part of the code was written by Peter Particle, so
> I'll redirect to him both proposals.
>
> Meanwhile, can you test if it is working for you right now?
>
>
> Regards,
>
>
> On Tue, May 24, 2011 at 11:08 PM, Ryan Pavlik <[email protected]> wrote:
> > The changes to osgpreview.cpp are for the first issue, while the changes
> to
> > utility.h are for the second way of solving the second issue.
> > Ryan
> >
> > On Tue, May 24, 2011 at 12:59 PM, Javier Taibo <[email protected]>
> > wrote:
> >>
> >> Hi Ryan,
> >>
> >> Thank you for the patches. Can you send the whole files instead of
> >> patches, please? You can zip all files together to make it easy.
> >>
> >> Thank you!
> >>
> >>
> >> Best Regards,
> >>
> >>
> >> On Tue, May 24, 2011 at 7:28 PM, Ryan Pavlik <[email protected]>
> wrote:
> >> > Hello all,
> >> > I've attached small patches to two issues I've found with Maya2OSG.
> >> > One allows it to build successfully with the current 2.8 branch -
> there
> >> > are
> >> > some manipulators not available in this branch so we just disable them
> >> > by
> >> > checking the osg version.
> >> > The other issue was ambiguity in calling inTolerance, preventing me
> from
> >> > building successfully on VC9 x64. I solved this two different ways:
> >> > take
> >> > your pick of which way you prefer.
> >> >
> >> > I've made inTolerance a template function with a single parameter type
> >> > and
> >> > explicitly specified the template parameter in the one case.
> >> > (fix-vc9-build)
> >> > inTolerance is a template function with three parameter types, and
> >> > static_casts all arguments to the type of the tolerance parameter
> before
> >> > doing math. This one doesn't require explicit template param
> >> > specification,
> >> > and seems "more correct" to me, or at least more explicit in how it
> >> > works. I
> >> > personally prefer this one. (alternate-fix-build)
> >> >
> >> > These patches were made against the latest svn trunk of maya2osg using
> >> > tortoisesvn.
> >> > Ryan
> >> > --
> >> > Ryan Pavlik
> >> > HCI Graduate Student
> >> > Virtual Reality Applications Center
> >> > Iowa State University
> >> >
> >> > [email protected]
> >> > http://academic.cleardefinition.com
> >> >
> >> > _______________________________________________
> >> > osg-users mailing list
> >> > [email protected]
> >> >
> >> >
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >> >
> >> >
> >>
> >>
> >>
> >> --
> >> Javier Taibo
> >> _______________________________________________
> >> osg-users mailing list
> >> [email protected]
> >>
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >
> >
> >
> > --
> > Ryan Pavlik
> > HCI Graduate Student
> > Virtual Reality Applications Center
> > Iowa State University
> >
> > [email protected]
> > http://academic.cleardefinition.com
> >
> > _______________________________________________
> > osg-users mailing list
> > [email protected]
> >
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
> >
> >
>
>
>
> --
> Javier Taibo
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
--
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University
[email protected]
http://academic.cleardefinition.com
#ifndef _ANIMATION_H_
#define _ANIMATION_H_
#include "utility.h"
// ----- Maya -----------------------------------------------------------------------
#include <maya/MTime.h>
//#include <maya/MDagNode.h>
//#include <maya/MDagPath.h>
#include <maya/MIntArray.h>
#include <maya/MPlugArray.h>
#include <maya/MAnimControl.h>
#include <maya/MDoubleArray.h>
#include <maya/MObjectArray.h>
#include <maya/MStringArray.h>
// ----- OSG ------------------------------------------------------------------------
//#include <osg/Group>
//#include <osg/Object>
#include <osg/Material>
#include <osg/MatrixTransform>
#include <osgAnimation/Keyframe>
#include <osgAnimation/Channel>
#include <osgAnimation/BasicAnimationManager>
//#include <sys/types.h>
//#include <string>
//#include <vector>
#include <fstream>
#include <sstream>
class Animation
{
public :
//Animation( std::ofstream & logFile ) ;
//Animation( std::ofstream & logFile , osg::ref_ptr< osgAnimation::BasicAnimationManager > animationManager ) ;
static void mapInputConnections( const MObject & mObject , osg::ref_ptr< osg::MatrixTransform > & osgXForm ) ;
static void mapInputConnections( const MObject & mObject , osg::ref_ptr< osg::Material > & osgMaterial ) ;
static bool exporta() ; // formally generateKeyframesy
static osg::ref_ptr< osgAnimation::BasicAnimationManager > getManager() { return _manager ; }
static void init() ;
private :
static void addAnimationClip( const std::string clipName ) ;
static void sampleAttributeAnimation( osgAnimation::Animation * animation ) ;
static bool _isInited ;
static double _slopeTolerance ; // key if slope between keys changes more then this value
static MStatus _mStatus ;
static MObjectArray _mKeyframeNode ; // holds all DAG Paths for key framing
static MIntArray _mKeyframeAttr ; // holds the corresponding Pointer into keyAttributes ( Types, e.g. "translate" )
static MStringArray _keyAttributes ; // Array of Attributes for keying
static MDoubleArray _keyDefaults ; // Holds default Values for keyAttributes
static MSpace::Space _mQuatSpace ;
static std::vector< std::string > _osgAttributes ; // Holds defining osg channel names
static osg::ref_ptr< osgAnimation::BasicAnimationManager > _manager ;
//inline bool inTolerance( double base , double test ) const ;
// ToDo :
// - First Channel in Animation must have length of entire animation, otherwise playback is corrupted
// VEC : Type of Vector, either osg::Quat or osg::Vec3
template < typename VEC >
static void doKeyframe( osgAnimation::TemplateKeyframeContainer< VEC > * keys ,
VEC osgVec ,
const unsigned int numComps ,
const double time )
{
bool sameSlope ;
osgAnimation::TemplateKeyframe< VEC > * key1 , * key2 ;
if ( keys -> size() > 1 ) { // at least 2 key frames need to be present already to check the slope
sameSlope = true ; // assume that the slope is the same for all components of two key frames
key1 = & keys -> at( keys -> size() - 1 ) ; // handle to last key frame
key2 = & keys -> at( keys -> size() - 2 ) ; // handle to last but one key frame
for( unsigned int vs = 0 ; vs < numComps ; ++vs ) { // loop through each Component of the Vec3
sameSlope = sameSlope && inTolerance<double>( // check if slope of last key frames is in tolerance with slope of current and last
( key1 -> getValue()[ vs ] - key2 -> getValue()[ vs ] ) / ( key1 -> getTime() - key2 -> getTime() ) ,
( osgVec[ vs ] - key1 -> getValue()[ vs ] ) / ( time - key1 -> getTime() ) ,
_slopeTolerance ) ;
}
// if the slope is the same, delete the last key frame
if ( sameSlope )
keys -> pop_back() ;
}
// now key at the current time with the current values
keys -> push_back( osgAnimation::TemplateKeyframe< VEC >( time , osgVec ) ) ;
// Unfortunately Animation plays wrong, if the first channel has not length of entire Animation
// Use this Feature in a later version, where playback might work right
// Also remove the first key, if not necessary
// get rid of last key frame if slope is horizontal
/* if ( frame == MAnimControl::maxTime().as( MTime::uiUnit() ) && keys -> size() > 1 ) {
sameSlope = true ; // use Variable for checking each component though
key1 = & keys -> at( keys -> size() - 1 ) ; // handle to last key frame
key2 = & keys -> at( keys -> size() - 2 ) ; // handle to last but one key frame
for( unsigned int vs = 0 ; vs < 3 ; ++vs ) { // loop through each Component of the Vec3, to check if values are inTolerance
sameSlope = sameSlope && inTolerance( key1 -> getValue()[ vs ] , key2 -> getValue()[ vs ] , _slopeTolerance ) ;
}
// if last and last but one keyframes are the same, delete the last one
if( sameSlope )
keys -> pop_back() ;
}*/
}
} ;
#endif // _ANIMATION_H_#ifndef _UTILITY_H_
#define _UTILITY_H_
// ----- Maya -----------------------------------------------------------------------
#include <maya/MColor.h>
#include <maya/MPoint.h>
#include <maya/MMatrix.h>
#include <maya/MVector.h>
#include <maya/MFloatPoint.h>
// ----- OSG ------------------------------------------------------------------------
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrixd>
#include <osg/Matrixf>
// CHECK if distance from BASE is in TOLERANCE
template<typename T>
inline bool inTolerance( T base , T match , T tolerance ) {
return (osg::absolute(base - match) <= tolerance);
}
template < typename SCALAR , typename VEC >
static VEC roundTolerance( SCALAR base , VEC match , SCALAR tolerance ) {
for( uint i = 0 ; i < VEC::num_components ; ++i )
if ( inTolerance( base , match[ i ] , tolerance ) )
match[ i ] = base ;
return match ;
}
//------------------------------------------------------------------------------
static inline void assign( MFloatPoint & mFloatPoint , const osg::Vec3 & osgVec3 ) {
mFloatPoint[ 0 ] = osgVec3[ 0 ] ;
mFloatPoint[ 1 ] = osgVec3[ 1 ] ;
mFloatPoint[ 2 ] = osgVec3[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Vec3 & osgVec3 , const MPoint & mPoint ) {
osgVec3[ 0 ] = mPoint[ 0 ] ;
osgVec3[ 1 ] = mPoint[ 1 ] ;
osgVec3[ 2 ] = mPoint[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( MFloatVector & mFloatVector , const osg::Vec3 & osgVec3 ) {
mFloatVector[ 0 ] = osgVec3[ 0 ] ;
mFloatVector[ 1 ] = osgVec3[ 1 ] ;
mFloatVector[ 2 ] = osgVec3[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Vec3 & osgVec3 , const MFloatVector & mFloatVector ) {
osgVec3[ 0 ] = mFloatVector[ 0 ] ;
osgVec3[ 1 ] = mFloatVector[ 1 ] ;
osgVec3[ 2 ] = mFloatVector[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( MVector & mVector , const osg::Vec3 & osgVec3 ) {
mVector[ 0 ] = osgVec3[ 0 ] ;
mVector[ 1 ] = osgVec3[ 1 ] ;
mVector[ 2 ] = osgVec3[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Vec3 & osgVec3 , const MVector & mVector ) {
osgVec3[ 0 ] = mVector[ 0 ] ;
osgVec3[ 1 ] = mVector[ 1 ] ;
osgVec3[ 2 ] = mVector[ 2 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( MVector & mColor , const osg::Vec4 & osgVec4 ) {
mColor[ 0 ] = osgVec4[ 0 ] ;
mColor[ 1 ] = osgVec4[ 1 ] ;
mColor[ 2 ] = osgVec4[ 2 ] ;
mColor[ 3 ] = osgVec4[ 3 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Vec4 & osgVec4 , const MColor & mColor ) {
osgVec4[ 0 ] = mColor[ 0 ] ;
osgVec4[ 1 ] = mColor[ 1 ] ;
osgVec4[ 2 ] = mColor[ 2 ] ;
osgVec4[ 3 ] = mColor[ 3 ] ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Matrixf & osgMatrix , const MMatrix & mMatrix ) {
osgMatrix = osg::Matrixf(
mMatrix( 0 , 0 ) , mMatrix( 0 , 1 ) , mMatrix( 0 , 2 ) , mMatrix( 0 , 3 ) ,
mMatrix( 1 , 0 ) , mMatrix( 1 , 1 ) , mMatrix( 1 , 2 ) , mMatrix( 1 , 3 ) ,
mMatrix( 2 , 0 ) , mMatrix( 2 , 1 ) , mMatrix( 2 , 2 ) , mMatrix( 2 , 3 ) ,
mMatrix( 3 , 0 ) , mMatrix( 3 , 1 ) , mMatrix( 3 , 2 ) , mMatrix( 3 , 3 ) ) ;
}
//------------------------------------------------------------------------------
static inline void assign( osg::Matrixd & osgMatrix , const MMatrix & mMatrix ) {
osgMatrix = osg::Matrixd(
mMatrix( 0 , 0 ) , mMatrix( 0 , 1 ) , mMatrix( 0 , 2 ) , mMatrix( 0 , 3 ) ,
mMatrix( 1 , 0 ) , mMatrix( 1 , 1 ) , mMatrix( 1 , 2 ) , mMatrix( 1 , 3 ) ,
mMatrix( 2 , 0 ) , mMatrix( 2 , 1 ) , mMatrix( 2 , 2 ) , mMatrix( 2 , 3 ) ,
mMatrix( 3 , 0 ) , mMatrix( 3 , 1 ) , mMatrix( 3 , 2 ) , mMatrix( 3 , 3 ) ) ;
}
#endif // _UTILITY_H________________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org