/// <summary> /// Adds two vectors. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the sum.</returns> public static Vector3FT add(Vector3FT vector1, Vector3FT vector2) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X + vector2.X; sol.Y = vector1.Y + vector2.Y; sol.Z = vector1.Z + vector2.Z; return sol; }
/// <summary> /// Adds a scalar to all the coordinates of the source vector. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="value">The scalar to add.</param> /// <returns>Returs the sum.</returns> public static Vector3FT add(Vector3FT vector1, float value) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X + value; sol.Y = vector1.Y + value; sol.Z = vector1.Z + value; return sol; }
/// <summary> /// Copy and create a new instance of Vector3FT. /// </summary> /// <param name="other">The Vector3FT to copy.</param> public Vector3FT(Vector3FT other) { this.X = other.X; this.Y = other.Y; this.Z = other.Z; }
/// <summary> /// Calculates the cross product of two vectors. /// </summary> /// <param name="vector2">Source vector.</param> /// <returns>Returns the cross product of the source vectors.</returns> public static Vector3FT cross(Vector3FT vector1, Vector3FT vector2) { Vector3FT sol = new Vector3FT(); sol.X = vector1.Y * vector2.Z - vector1.Z * vector2.Y; sol.Y = vector1.Z * vector2.X - vector1.X * vector2.Z; sol.Z = vector1.X * vector2.Y - vector1.Y * vector2.X; return sol; }
/// <summary> /// Calculates the smaller angle between two vectors. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the angle in radians.</returns> public static float angle(Vector3FT vector1, Vector3FT vector2) { return (float)Math.Acos((vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z) / (length(vector1) * length(vector2))); }
/// <summary> /// Calculates the dot product of two vectors. Returns a floating point value between -1 and 1. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the dot product of the source vectors.</returns> public static float dot(Vector3FT vector1, Vector3FT vector2) { return (vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z) / (length(vector1) * length(vector2)); }
/// <summary> /// Calculates the distance between two vectors. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the distance between the vectors squared.</returns> public static float distanceSquared(Vector3FT vector1, Vector3FT vector2) { return (vector1.X - vector2.X) * (vector1.X - vector2.X) + (vector1.Y - vector2.Y) * (vector1.Y - vector2.Y) + (vector1.Z - vector2.Z) * (vector1.Z - vector2.Z); }
// Normalize in the interval [-1, 1] the given 3D point. // The Z value is in the inverval [-1, 0], being 0 the closest distance. private Vector3FT transformTo3DCoord(PointFT p, int width, int height, int distance) { Vector3FT v = new Vector3FT(); v.X = p.Y / (width * 1.0f) * 2 - 1; v.Y = (1 - p.X / (height * 1.0f)) * 2 - 1; v.Z = (distance - 400) / -7600.0f; return v; }
/// <summary> /// Creates a unit vector from the specified vector. The result is a vector one unit in length pointing in the same direction as the original vector. /// </summary> /// <param name="vector1">Source vector.</param> /// <returns>The created unit vector.</returns> public static Vector3FT normalize(Vector3FT vector1) { float l = length(vector1); return divide(vector1, l); }
/// <summary> /// Returns the length of the source vector. /// </summary> /// <returns>The length of the vector.</returns> public static float length(Vector3FT vector1) { return (float)Math.Sqrt(vector1.X * vector1.X + vector1.Y * vector1.Y + vector1.Z * vector1.Z); }
/// <summary> /// Divides the source vector by an scalar. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="value">The divisor.</param> /// <returns>Returns the division.</returns> public static Vector3FT divide(Vector3FT vector1, float value) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X / value; sol.Y = vector1.Y / value; sol.Z = vector1.Z / value; return sol; }
/// <summary> /// Divides two vectors. /// </summary> /// <param name="vector1">The dividend.</param> /// <param name="vector2">The divisor.</param> /// <returns>Returns the division.</returns> public static Vector3FT divide(Vector3FT vector1, Vector3FT vector2) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X / vector2.X; sol.Y = vector1.Y / vector2.Y; sol.Z = vector1.Z / vector2.Z; return sol; }
/// <summary> /// Multiplies the source vector by a scalar. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="value">The factor.</param> /// <returns>Returns the product.</returns> public static Vector3FT multiply(Vector3FT vector1, float value) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X * value; sol.Y = vector1.Y * value; sol.Z = vector1.Z * value; return sol; }
/// <summary> /// Multiplies two vectors. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the product.</returns> public static Vector3FT multiply(Vector3FT vector1, Vector3FT vector2) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X * vector2.X; sol.Y = vector1.Y * vector2.Y; sol.Z = vector1.Z * vector2.Z; return sol; }
/// <summary> /// Subtracts a scalar to all the coordinates of the source vector. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="value">The scalar to substract.</param> /// <returns>Returs the difference.</returns> public static Vector3FT subtract(Vector3FT vector1, float value) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X - value; sol.Y = vector1.Y - value; sol.Z = vector1.Z - value; return sol; }
/// <summary> /// Calculates the distance between two vectors. /// </summary> /// <param name="vector1">Source vector.</param> /// <param name="vector2">Source vector.</param> /// <returns>Returns the distance between the vectors.</returns> public static float distance(Vector3FT vector1, Vector3FT vector2) { return (float)Math.Sqrt((vector1.X - vector2.X) * (vector1.X - vector2.X) + (vector1.Y - vector2.Y) * (vector1.Y - vector2.Y) + (vector1.Z - vector2.Z) * (vector1.Z - vector2.Z)); }
/// <summary> /// Subtracts two vectors. /// </summary> /// <param name="vector1">Minuend vector.</param> /// <param name="vector2">Subtrahend vector.</param> /// <returns>Returns the difference .</returns> public static Vector3FT subtract(Vector3FT vector1, Vector3FT vector2) { Vector3FT sol = new Vector3FT(); sol.X = vector1.X - vector2.X; sol.Y = vector1.Y - vector2.Y; sol.Z = vector1.Z - vector2.Z; return sol; }
// Obtain the 3D normalized point and add it to the list of fingertips public void calculate3DPoints(int width, int height, int[] distance) { if (palm.X != -1 && palm.Y != -1) palm3D = transformTo3DCoord(palm, width, height, distance[palm.X * width + palm.Y]); fingertips3D.Clear(); for (int i = 0; i < fingertips.Count; ++i) { PointFT f = fingertips[i]; fingertips3D.Add(transformTo3DCoord(f, width, height, distance[f.X * width + f.Y])); } }