HapticSpring.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 //
00027 //
00029 #ifndef __HAPTICSPRING_H__
00030 #define __HAPTICSPRING_H__
00031 
00032 #include "HapticForceEffect.h" 
00033 #include "Threads.h"
00034 
00035 namespace H3D {
00036 
00040   class H3DAPI_API HapticSpring: public HapticForceEffect {
00041   public:
00043     HapticSpring ( const H3D::ArithmeticTypes::Matrix4f & _transform,
00044                    bool _interpolate ):
00045       HapticForceEffect( _transform, _interpolate ),
00046       position( Vec3f( 0, 0, 0 ) ),
00047       spring_constant( 0 ) { }
00048     
00050     HapticSpring( const H3D::ArithmeticTypes::Matrix4f & _transform,
00051                   const Vec3f &_position,
00052                   H3DFloat _spring_constant,
00053                   bool _interpolate ):
00054       HapticForceEffect( _transform, _interpolate ),
00055       position( _position ),
00056       spring_constant( _spring_constant ) {}
00057     
00059     EffectOutput virtual calculateForces( const EffectInput &input ) {
00060       lock.lock();
00061       Vec3f local_pos = transform.inverse() * input.position;
00062       Vec3f local_force = ( position - local_pos ) * spring_constant;
00063       force = local_force;
00064       lock.unlock();
00065       return EffectOutput( transform.getRotationPart() * local_force );
00066     }
00067 
00068     // set position
00069     virtual void setPosition( const Vec3f &_position ) { 
00070       lock.lock();
00071       position = _position;
00072       lock.unlock();
00073     }
00074 
00075     // set velocity
00076     virtual void setVelocity( const Vec3f &_velocity ) {
00077       lock.lock();
00078       velocity = _velocity;
00079       lock.unlock();
00080     }
00081 
00082     // set velocity
00083     virtual void setSpringConstant( const H3DFloat &_sc ) { 
00084       lock.lock();
00085       spring_constant = _sc;
00086       lock.unlock();
00087     }
00088     
00089     // get and reset force
00090     virtual Vec3f getLatestForce() {
00091       lock.lock();
00092       Vec3f f = force;
00093       lock.unlock();
00094       return f;
00095     }
00096     
00097   protected:
00098     Vec3f position;
00099     Vec3f velocity;
00100     Vec3f force;
00101     H3DFloat spring_constant;
00102     MutexLock lock;
00103   };
00104 }
00105 
00106 #endif

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