CoordinateInterpolator.h

Go to the documentation of this file.
00001 
00002 //    Copyright 2004, SenseGraphics AB
00003 //
00004 //    This file is part of H3D API.
00005 //
00006 //    H3D API is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    H3D API is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with H3D API; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 //    A commercial license is also available. Please contact us at 
00021 //    www.sensegraphics.com for more information.
00022 //
00023 //
00028 //
00030 #ifndef __COORDINATEINTERPOLATOR_H__
00031 #define __COORDINATEINTERPOLATOR_H__
00032 
00033 #include "X3DInterpolatorNode.h"
00034 #include "MFVec3f.h"
00035 
00036 namespace H3D {
00037 
00051   class H3DAPI_API CoordinateInterpolator : public X3DInterpolatorNode {
00052   public:
00053 
00060     struct MFValue : TypedField< MFVec3f, Types< SFFloat, MFFloat, MFVec3f > > {
00061       virtual void update() {
00062         CoordinateInterpolator *interpolator = 
00063             static_cast<CoordinateInterpolator*>( getOwner() );
00064         H3DFloat fraction = static_cast<SFFloat*>(routes_in[0])->getValue(interpolator->id);
00065         int key_size = static_cast<MFFloat*>(routes_in[1])->size();
00066         H3DFloat weight;
00067         int key_index = static_cast<CoordinateInterpolator*>(owner)->lookupKey( fraction, weight );
00068         vector< Vec3f > key_values = static_cast<MFVec3f*>(routes_in[2])->getValue();
00069         int value_size = key_values.size() / key_size;
00070         value.resize( value_size );
00071 
00072                 if ( key_size > 0 && key_index >= 0 ) {
00073           if (weight<=0) 
00074             for (int x = 0; x < value_size; x++ )
00075               value[x] = key_values[ x ];
00076           else if (weight>=1)
00077             for (int x = 0; x < value_size; x++ )
00078               value[x] = key_values[ (key_size-1)*value_size + x];
00079           else { // else, interpolate linearly
00080             for (int x = 0; x < value_size; x++ ) {
00081               Vec3f a = key_values[ key_index*value_size + x ];
00082               Vec3f b = key_values[ (key_index+1)*value_size + x  ];
00083               value[ x ] = (1-weight)*a + (weight)*b;
00084             }
00085           }
00086         }
00087       }
00088     };
00089     
00091     CoordinateInterpolator( Inst< SFNode  >  _metadata      = 0,
00092                             Inst< SFFloat >  _set_fraction  = 0,
00093                             Inst< MFFloat >  _key           = 0,
00094                             Inst< MFVec3f >  _keyValue      = 0,
00095                             Inst< MFValue >  _value_changed = 0 );
00096 
00098     auto_ptr< MFVec3f >  keyValue;
00099 
00101     auto_ptr< MFValue >  value_changed;
00102 
00104     static H3DNodeDatabase database;
00105   };
00106 }
00107 
00108 #endif

Generated on Thu Aug 24 12:38:32 2006 for H3D API by  doxygen 1.4.5