/* TUIO C# Library - part of the reacTIVision project Copyright (c) 2005-2014 Martin Kaltenbrunner This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. */ using System; using System.Collections.Generic; namespace TUIO { /** * The abstract TuioContainer class defines common attributes that apply * to both subclasses (TuioObject and TuioCursor). * * * * @author Martin Kaltenbrunner * @version 1.1.5 */ public abstract class TuioContainer : TuioPoint { /** * * The unique session ID number that is assigned to each TUIO object or cursor. */ protected long session_id; /** * * The X-axis velocity value. */ protected float x_speed; /** * * The Y-axis velocity value. */ protected float y_speed; /** * * The motion speed value. */ protected float motion_speed; /** * * The motion acceleration value. */ protected float motion_accel; /** * * A Vector of TuioPoints containing all the previous positions of the TUIO component. */ protected List path; #region State Enumeration Values /** * * Defines the ADDED state. */ public const int TUIO_ADDED = 0; /** * * Defines the ACCELERATING state. */ public const int TUIO_ACCELERATING = 1; /** * * Defines the DECELERATING state. */ public const int TUIO_DECELERATING = 2; /** * * Defines the STOPPED state. */ public const int TUIO_STOPPED = 3; /** * * Defines the REMOVED state. */ public const int TUIO_REMOVED = 4; #endregion /** * * Reflects the current state of the TuioComponent */ protected int state; #region Constructors /** * * This constructor takes a TuioTime argument and assigns it along with the provided * Session ID, X and Y coordinate to the newly created TuioContainer. * * the TuioTime to assign * the Session ID to assign * the X coordinate to assign * the Y coordinate to assign */ public TuioContainer(TuioTime ttime, long si, float xp, float yp) : base(ttime, xp, yp) { session_id = si; x_speed = 0.0f; y_speed = 0.0f; motion_speed = 0.0f; motion_accel = 0.0f; path = new List(); path.Add(new TuioPoint(currentTime, xpos, ypos)); state = TUIO_ADDED; } /** * * This constructor takes the provided Session ID, X and Y coordinate * and assigs these values to the newly created TuioContainer. * * the Session ID to assign * the X coordinate to assign * the Y coordinate to assign */ public TuioContainer(long si, float xp, float yp) : base(xp, yp) { session_id = si; x_speed = 0.0f; y_speed = 0.0f; motion_speed = 0.0f; motion_accel = 0.0f; path = new List(); path.Add(new TuioPoint(currentTime, xpos, ypos)); state = TUIO_ADDED; } /** * * This constructor takes the atttibutes of the provided TuioContainer * and assigs these values to the newly created TuioContainer. * * the TuioContainer to assign */ public TuioContainer(TuioContainer tcon) : base(tcon) { session_id = tcon.SessionID; x_speed = 0.0f; y_speed = 0.0f; motion_speed = 0.0f; motion_accel = 0.0f; path = new List(); path.Add(new TuioPoint(currentTime, xpos, ypos)); state = TUIO_ADDED; } #endregion #region Update Methods /** * * Takes a TuioTime argument and assigns it along with the provided * X and Y coordinate to the private TuioContainer attributes. * The speed and accleration values are calculated accordingly. * the TuioTime to assign * the X coordinate to assign * the Y coordinate to assign */ public new void update(TuioTime ttime, float xp, float yp) { TuioPoint lastPoint = path[path.Count - 1]; base.update(ttime, xp, yp); TuioTime diffTime = currentTime - lastPoint.TuioTime; float dt = diffTime.TotalMilliseconds / 1000.0f; float dx = this.xpos - lastPoint.X; float dy = this.ypos - lastPoint.Y; float dist = (float)Math.Sqrt(dx * dx + dy * dy); float last_motion_speed = this.motion_speed; this.x_speed = dx / dt; this.y_speed = dy / dt; this.motion_speed = dist / dt; this.motion_accel = (motion_speed - last_motion_speed) / dt; path.Add(new TuioPoint(currentTime, xpos, ypos)); if (motion_accel > 0) state = TUIO_ACCELERATING; else if (motion_accel < 0) state = TUIO_DECELERATING; else state = TUIO_STOPPED; } /** * * This method is used to calculate the speed and acceleration values of * TuioContainers with unchanged positions. */ public void stop(TuioTime ttime) { update(ttime, this.xpos, this.ypos); } /** * * Takes a TuioTime argument and assigns it along with the provided * X and Y coordinate, X and Y velocity and acceleration * to the private TuioContainer attributes. * * the TuioTime to assign * the X coordinate to assign * the Y coordinate to assign * the X velocity to assign * the Y velocity to assign * the acceleration to assign */ public void update(TuioTime ttime, float xp, float yp, float xs, float ys, float ma) { base.update(ttime, xp, yp); x_speed = xs; y_speed = ys; motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed); motion_accel = ma; path.Add(new TuioPoint(currentTime, xpos, ypos)); if (motion_accel > 0) state = TUIO_ACCELERATING; else if (motion_accel < 0) state = TUIO_DECELERATING; else state = TUIO_STOPPED; } /** * * Assigns the provided X and Y coordinate, X and Y velocity and acceleration * to the private TuioContainer attributes. The TuioTime time stamp remains unchanged. * * the X coordinate to assign * the Y coordinate to assign * the X velocity to assign * the Y velocity to assign * the acceleration to assign */ public void update(float xp, float yp, float xs, float ys, float ma) { base.update(xp, yp); x_speed = xs; y_speed = ys; motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed); motion_accel = ma; path.Add(new TuioPoint(currentTime, xpos, ypos)); if (motion_accel > 0) state = TUIO_ACCELERATING; else if (motion_accel < 0) state = TUIO_DECELERATING; else state = TUIO_STOPPED; } /** * * Takes the atttibutes of the provided TuioContainer * and assigs these values to this TuioContainer. * The TuioTime time stamp of this TuioContainer remains unchanged. * * the TuioContainer to assign */ public void update(TuioContainer tcon) { base.update(tcon.X, tcon.Y); x_speed = tcon.XSpeed; y_speed = tcon.YSpeed; motion_speed = (float)Math.Sqrt(x_speed * x_speed + y_speed * y_speed); motion_accel = tcon.MotionAccel; path.Add(new TuioPoint(currentTime, xpos, ypos)); if (motion_accel > 0) state = TUIO_ACCELERATING; else if (motion_accel < 0) state = TUIO_DECELERATING; else state = TUIO_STOPPED; } #endregion /** * * Assigns the REMOVE state to this TuioContainer and sets * its TuioTime time stamp to the provided TuioTime argument. * * the TuioTime to assign */ public void remove(TuioTime ttime) { currentTime = ttime; state = TUIO_REMOVED; } #region Properties & Getter/Setter Methods /** * * Returns the Session ID of this TuioContainer. * the Session ID of this TuioContainer */ public long SessionID { get { return session_id; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property SessionID instead is recommended.")] public long getSessionID() { return SessionID; } /** * * Returns the X velocity of this TuioContainer. * the X velocity of this TuioContainer */ public float XSpeed { get { return x_speed; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property XSpeed instead is recommended.")] public float getXSpeed() { return XSpeed; } /** * * Returns the Y velocity of this TuioContainer. * the Y velocity of this TuioContainer */ public float YSpeed { get { return y_speed; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property YSpeed instead is recommended.")] public float getYSpeed() { return YSpeed; } /** * * Returns the position of this TuioContainer. * the position of this TuioContainer */ public TuioPoint Position { get { return new TuioPoint(xpos, ypos); } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property Position instead is recommended.")] public TuioPoint getPosition() { return Position; } /** * * Returns the path of this TuioContainer. * the path of this TuioContainer */ public List Path { get { return path; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property Path instead is recommended.")] public List getPath() { return Path; } /** * * Returns the motion speed of this TuioContainer. * the motion speed of this TuioContainer */ public float MotionSpeed { get { return motion_speed; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property MotionSpeed instead is recommended.")] public float getMotionSpeed() { return MotionSpeed; } /** * * Returns the motion acceleration of this TuioContainer. * the motion acceleration of this TuioContainer */ public float MotionAccel { get { return motion_accel; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property MotionAccel instead is recommended.")] public float getMotionAccel() { return MotionAccel; } /** * * Returns the TUIO state of this TuioContainer. * the TUIO state of this TuioContainer */ public int TuioState { get { return state; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property TuioState instead is recommended.")] public int getTuioState() { return TuioState; } /** * * Returns true of this TuioContainer is moving. * true of this TuioContainer is moving */ public virtual bool isMoving { get { if ((state == TUIO_ACCELERATING) || (state == TUIO_DECELERATING)) return true; else return false; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property isMoving instead is recommended.")] public virtual bool getIsMoving() { return isMoving; } #endregion } }