00001 /* 00002 Copyright (c) 2010, The Cinder Project: http://libcinder.org 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include "cinder/Cinder.h" 00026 #include "cinder/app/Event.h" 00027 #include "cinder/Vector.h" 00028 #include "cinder/Quaternion.h" 00029 00030 namespace cinder { namespace app { 00031 00033 class AccelEvent : public Event { 00034 public: 00035 AccelEvent( const Vec3f &data, const Vec3f &rawData, const Vec3f &prevData, const Vec3f &prevRawData ) 00036 : Event(), mData( data ), mRawData( rawData ), mPrevData( prevData ), mPrevRawData( prevRawData ) 00037 { 00038 } 00039 00041 Vec3f getData() const { return mData; } 00043 Vec3f getRawData() const { return mRawData; } 00045 Vec3f getPrevData() const { return mPrevData; } 00047 Vec3f getPrevRawData() const { return mPrevRawData; } 00048 00050 bool isShake( float shakeDelta = 2.2f ) const 00051 { return (mRawData - mPrevRawData).lengthSquared() > shakeDelta * shakeDelta; } 00052 00053 Vec2f getPolarPlaneVector() const // (r, theta), theta is in degrees 00054 { 00055 float r = math<float>::sin( M_PI*(mData.z+1) / 2 ); 00056 float theta = toDegrees( math<float>::asin( mData.y / math<float>::sqrt( mData.x*mData.x + mData.y*mData.y )) ); 00057 00058 if( mData.x < 0 ) { 00059 theta = -theta + 180.0f; 00060 } 00061 else { 00062 if( mData.y < 0 ) { 00063 theta = theta + 360.0f; 00064 } 00065 } 00066 00067 return Vec2f( r, theta ); 00068 } 00069 00070 Vec2f getPlaneVector() const 00071 { 00072 Vec2f v = getPolarPlaneVector(); 00073 return Vec2f( -v.x * math<float>::cos( toRadians( v.y ) ), v.x * math<float>::sin( toRadians( v.y ) ) ); 00074 } 00075 00077 Matrix44f getMatrix() const 00078 { 00079 return Quatf( Vec3f( 0, -1, 0 ), mData.normalized() ).toMatrix44(); 00080 } 00081 00082 private: 00083 Vec3f mData, mPrevData; 00084 Vec3f mRawData, mPrevRawData; 00085 }; 00086 00087 // For convenience only 00088 inline std::ostream& operator<<( std::ostream &out, const AccelEvent &event ) 00089 { 00090 out << event.getData() << " raw: " << event.getRawData(); 00091 return out; 00092 } 00093 00094 00095 } } // namespace cinder::app