Assignments Assignments

keyFrames.inl
Go to the documentation of this file.
1 namespace Ray
2{
4 // KeyFrameEvaluator //
6 template< typename DataType >
7 KeyFrameEvaluator< DataType >::~KeyFrameEvaluator( void ){}
8
10 // KeyFrameData //
12 template< typename DataType >
13 KeyFrameData< DataType >::KeyFrameData( void ) : _keyFrameEvaluator(NULL) {}
14
15 template< typename DataType >
16 KeyFrameData< DataType >::~KeyFrameData( void ) { if( _keyFrameEvaluator ) delete _keyFrameEvaluator; }
17
18 template< typename DataType >
19 float KeyFrameData< DataType >::duration( void ) const { return _duration; }
20
21 template< typename DataType >
22 int KeyFrameData< DataType >::keyframes( void ) const { return (int)_data[0].size(); }
23
24 template< typename DataType >
25 int KeyFrameData< DataType >::dofs( void ) const { return (int)_data.size(); }
26
27 template< typename DataType >
28 const DataType &KeyFrameData< DataType >::current( const std::string &dofName ) const
29 {
30 for( int i=0 ; i<_dofNames.size() ; i++ ) if( _dofNames[i]==dofName ) return _currentValues[i];
31 THROW( "could not find dof name: " , dofName );
32 return _currentValues[0];
33 }
34
35 template< typename DataType >
36 template< typename ParameterType >
37 void KeyFrameData< DataType >::setEvaluator( void )
38 {
39 if( _keyFrameEvaluator ) delete _keyFrameEvaluator;
40 _keyFrameEvaluator = new KeyFrameParameters< DataType , ParameterType >( *this );
41 }
42
43 template< typename DataType >
44 void KeyFrameData< DataType >::setCurrentValues( double t , int curveType )
45 {
46 if( !_keyFrameEvaluator ) THROW( "_keyFrameEvaluator has not been initialized" );
47 for( int dof=0 ; dof<_currentValues.size() ; dof++ ) _currentValues[ dof ] = _keyFrameEvaluator->evaluate( dof , t , curveType );
48 }
49
50 template< typename DataType >
51 std::ostream &operator << ( std::ostream &stream , const KeyFrameData< DataType > &keyFrameData )
52 {
53 stream << "#DOFS " << keyFrameData._dofNames.size() << std::endl;
54 for( int i=0 ; i<keyFrameData._dofNames.size() ; i++ ) stream << " " << keyFrameData._dofNames[i] << std::endl;
55 stream << "#DURATION " << keyFrameData._duration << std::endl;
56 stream << "#FRAMES " << keyFrameData._data[0].size() << std::endl;
57 for( int i=0 ; i<keyFrameData._data[0].size() ; i++ )
58 {
59 for( int j=0 ; j<keyFrameData._data.size() ; j++ ) stream << " " << keyFrameData._data[j][i];
60 stream << std::endl;
61 }
62 return stream;
63 }
64
65 template< typename DataType >
66 std::istream &operator >> ( std::istream &stream , KeyFrameData< DataType > &keyFrameData )
67 {
68 std::string str;
69 int dofs;
70 stream >> str >> dofs;
71 if( !stream || str!="#DOFS" || dofs<=0 ) THROW( "Failed to parse DOFS" );
72
73 keyFrameData._currentValues.resize( dofs );
74 keyFrameData._dofNames.resize( dofs );
75 keyFrameData._data.resize( dofs );
76
77 for( int i=0 ; i<dofs ; i++ ) if( !( stream >> keyFrameData._dofNames[i] ) ) THROW( "Failed to read DOF names" );
78
79 stream >> str >> keyFrameData._duration;
80 if( !stream || str!="#DURATION" || keyFrameData._duration<=0 ) THROW( "Failed to read DURATION" );
81
82 int frames;
83 stream >> str >> frames;
84 if( !stream || str!="#FRAMES" || frames<=0 ) THROW( "Failed to read FRAMES" );
85
86 for( int i=0 ; i<keyFrameData._data.size() ; i++ ) keyFrameData._data[i].resize( frames );
87
88 for( int j=0 ; j<frames ; j++ ) for( int i=0 ; i<dofs ; i++ ) if( !( stream >> keyFrameData._data[i][j] ) ) THROW( "Failed to read parameter" );
89 return stream;
90 }
91
93 // KeyFrameParameters //
95 template< typename DataType , typename ParameterType >
96 KeyFrameParameters< DataType , ParameterType >::KeyFrameParameters( const KeyFrameData< DataType > &keyFrameData )
97 {
98 // Allocate for the parameters
99 _parameters.resize( keyFrameData._data.size() );
100 for( int i=0 ; i<_parameters.size() ; i++ ) _parameters[i].resize( keyFrameData._data[i].size() );
101
102 // Transform data -> parameters
103 for( int d=0 ; d<_parameters.size() ; d++ )
104 {
105 // Set the first transformation naively
106 _parameters[d][0] = ParameterType( keyFrameData._data[d][0] );
107 // Set the rest using the current and previous
108 for( int f=1 ; f<_parameters[d].size() ; f++ ) _parameters[d][f] = ParameterType( keyFrameData._data[d][f] , _parameters[d][f-1] );
109 }
110 }
111
112 template< typename DataType , typename ParameterType >
113 DataType KeyFrameParameters< DataType , ParameterType >::evaluate( unsigned int dof , double t , int curveType )
114 {
115 ParameterType param = Util::Interpolation::Sample( _parameters[dof] , t , curveType );
116 return param();
117 }
118}
Ray::KeyFrameData
Definition keyFrames.h:26
Ray::KeyFrameData::_dofNames
std::vector< std::string > _dofNames
Definition keyFrames.h:35
Ray::KeyFrameData::_duration
float _duration
Definition keyFrames.h:32
Ray::KeyFrameData::current
const DataType & current(const std::string &dofName) const
Definition keyFrames.inl:28
Ray::KeyFrameData::_currentValues
std::vector< DataType > _currentValues
Definition keyFrames.h:41
Ray::KeyFrameData::~KeyFrameData
~KeyFrameData(void)
Definition keyFrames.inl:16
Ray::KeyFrameData::setCurrentValues
void setCurrentValues(double t, int curveType)
Definition keyFrames.inl:44
Ray::KeyFrameData::duration
float duration(void) const
Definition keyFrames.inl:19
Ray::KeyFrameData::_data
std::vector< std::vector< DataType > > _data
Definition keyFrames.h:38
Ray::KeyFrameData::KeyFrameData
KeyFrameData(void)
Definition keyFrames.inl:13
Ray::KeyFrameData::dofs
int dofs(void) const
Definition keyFrames.inl:25
Ray::KeyFrameData::keyframes
int keyframes(void) const
Definition keyFrames.inl:22
Ray::KeyFrameData::setEvaluator
void setEvaluator(void)
Definition keyFrames.inl:37
Ray::KeyFrameEvaluator::~KeyFrameEvaluator
virtual ~KeyFrameEvaluator(void)
Definition keyFrames.inl:7
Ray::KeyFrameParameters
Definition keyFrames.h:91
Ray::KeyFrameParameters::evaluate
DataType evaluate(unsigned int dof, double t, int curveType)
Definition keyFrames.inl:113
Ray::KeyFrameParameters::KeyFrameParameters
KeyFrameParameters(const KeyFrameData< DataType > &data)
Definition keyFrames.inl:96
Util::Interpolation::Sample
static SampleType Sample(const std::vector< SampleType > &samples, double t, int interpolationType)
Definition interpolation.todo.inl:38
THROW
#define THROW(...)
Definition exceptions.h:151
ParameterType
CmdLineParameter< int > ParameterType("parameter", RotationParameters::TRIVIAL+1)
Ray
Definition box.h:7
Ray::operator>>
std::istream & operator>>(std::istream &stream, Camera &camera)
Definition camera.cpp:15
Ray::operator<<
std::ostream & operator<<(std::ostream &stream, const Camera &camera)
Definition camera.cpp:25