Sirikata
libcore/include/sirikata/core/util/MotionVector.hpp
Go to the documentation of this file.
00001 /*  Sirikata
00002  *  MotionVector.hpp
00003  *
00004  *  Copyright (c) 2009, Ewen Cheslack-Postava
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are
00009  *  met:
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in
00014  *    the documentation and/or other materials provided with the
00015  *    distribution.
00016  *  * Neither the name of Sirikata nor the names of its contributors may
00017  *    be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00021  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00024  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef _SIRIKATA_MOTION_VECTOR_HPP_
00034 #define _SIRIKATA_MOTION_VECTOR_HPP_
00035 
00036 #include <sirikata/core/util/Platform.hpp>
00037 #include <sirikata/core/util/Time.hpp>
00038 #include <sirikata/core/util/TemporalValue.hpp>
00039 
00040 namespace Sirikata {
00041 
00042 template<typename CoordType>
00043 class MotionVector {
00044 public:
00045     typedef CoordType PositionType;
00046     typedef CoordType VelocityType;
00047 
00048     MotionVector()
00049      : mStart(0,0,0), mDirection(0,0,0)
00050     {
00051     }
00052 
00053     MotionVector(const CoordType& pos, const CoordType& vel)
00054      : mStart(pos), mDirection(vel)
00055     {
00056     }
00057 
00058     const CoordType& position() const {
00059         return mStart;
00060     }
00061 
00062     const CoordType& velocity() const {
00063         return mDirection;
00064     }
00065 
00066     MotionVector extrapolate(const Duration& dt) const {
00067         return MotionVector(mStart + mDirection * dt.toSeconds(), mDirection);
00068     }
00069 
00070 private:
00071     CoordType mStart;
00072     CoordType mDirection;
00073 }; // class MotionVector
00074 
00075 typedef MotionVector<Vector3f> MotionVector3f;
00076 typedef MotionVector<Vector3d> MotionVector3d;
00077 
00078 template<typename MotionVectorType>
00079 class TimedMotionVector : public TemporalValue<MotionVectorType> {
00080 public:
00081     typedef TemporalValue<MotionVectorType> Base;
00082     typedef typename MotionVectorType::PositionType PositionType;
00083     typedef typename MotionVectorType::VelocityType VelocityType;
00084 
00085     TimedMotionVector()
00086      : TemporalValue<MotionVectorType>()
00087     {}
00088     TimedMotionVector(const Time& when, const MotionVectorType& l)
00089      : TemporalValue<MotionVectorType>(when, l)
00090     {}
00091 
00092     Time updateTime() const {
00093         return Base::time();
00094     }
00095 
00096     const PositionType& position() const {
00097         return Base::value().position();
00098     }
00099 
00100     PositionType position(const Duration& dt) const {
00101         return Base::value().position() + Base::value().velocity() * dt.toSeconds();
00102     }
00103 
00104     PositionType position(const Time& t) const {
00105         return position(t - Base::time());
00106     }
00107 
00108     const VelocityType& velocity() const {
00109         return Base::value().velocity();
00110     }
00111     TimedMotionVector& operator+=(const PositionType &offset) {
00112         Base::value().position()+=offset;
00113         return *this;
00114     }
00115     void update(const Time& t, const PositionType& pos, const VelocityType& vel) {
00116         assert(t > Base::time());
00117         Base::updateValue(t, MotionVectorType(pos, vel));
00118     }
00119 };
00120 
00121 typedef TimedMotionVector<MotionVector3f> TimedMotionVector3f;
00122 typedef TimedMotionVector<MotionVector3d> TimedMotionVector3d;
00123 
00124 } // namespace Sirikata
00125 
00126 #endif //_SIRIKATA_MOTION_VECTOR_HPP_