Exemple #1
0
        /// <summary>
        /// Multiplies two objects. Scalar scalar, scalar vector, scalar matrix, vector vector (component),
        /// vector matrix, matrix vector matrix matrix multiplications are supported.
        /// </summary>
        /// <param name="obj1">The first object.</param>
        /// <param name="obj2">The second object.</param>
        /// <returns>Result cast into object.</returns>
        public static object Mul(object obj1, object obj2)
        {
            if (obj1.GetType() != obj2.GetType())
            {
                throw new ArgumentException("Object are not of the same type.");
            }

            if (obj1 is BigNum)
            {
                return((BigNum)obj1 * (BigNum)obj2);
            }

            // Float version:
            if (obj1 is float)
            {
                return((float)obj1 * (float)obj2);
            }
            if (obj1 is Vector2f)
            {
                return(Vector2f.ComponentMultiply((Vector2f)obj1, (Vector2f)obj2));
            }
            if (obj1 is Vector3f)
            {
                return(Vector3f.ComponentMultiply((Vector3f)obj1, (Vector3f)obj2));
            }
            if (obj1 is Vector4f)
            {
                return(Vector4f.ComponentMultiply((Vector4f)obj1, (Vector4f)obj2));
            }
            if (obj1 is Matrix.Matrix2x2f)
            {
                return((Matrix.Matrix2x2f)obj1 * (Matrix.Matrix2x2f)obj2);
            }
            if (obj1 is Matrix.Matrix3x3f)
            {
                return((Matrix.Matrix3x3f)obj1 * (Matrix.Matrix3x3f)obj2);
            }
            if (obj1 is Matrix.Matrix4x4f)
            {
                return((Matrix.Matrix4x4f)obj1 * (Matrix.Matrix4x4f)obj2);
            }
            if (obj1 is Complexf)
            {
                return((Complexf)obj1 * (Complexf)obj2);
            }
            if (obj1 is Quaternionf)
            {
                return((Quaternionf)obj1 * (Quaternionf)obj2);
            }

            // Double version:
            if (obj1 is double)
            {
                return((double)obj1 * (double)obj2);
            }
            if (obj1 is Vector2d)
            {
                return(Vector2d.ComponentMultiply((Vector2d)obj1, (Vector2d)obj2));
            }
            if (obj1 is Vector3d)
            {
                return(Vector3d.ComponentMultiply((Vector3d)obj1, (Vector3d)obj2));
            }
            if (obj1 is Vector4d)
            {
                return(Vector4d.ComponentMultiply((Vector4d)obj1, (Vector4d)obj2));
            }
            if (obj1 is Matrix.Matrix2x2d)
            {
                return((Matrix.Matrix2x2d)obj1 * (Matrix.Matrix2x2d)obj2);
            }
            if (obj1 is Matrix.Matrix3x3d)
            {
                return((Matrix.Matrix3x3d)obj1 * (Matrix.Matrix3x3d)obj2);
            }
            if (obj1 is Matrix.Matrix4x4d)
            {
                return((Matrix.Matrix4x4d)obj1 * (Matrix.Matrix4x4d)obj2);
            }
            if (obj1 is Complexd)
            {
                return((Complexd)obj1 * (Complexd)obj2);
            }
            if (obj1 is Quaterniond)
            {
                return((Quaterniond)obj1 * (Quaterniond)obj2);
            }

            // Integer version:
            if (obj1 is int)
            {
                return((int)obj1 * (int)obj2);
            }
            if (obj1 is Vector2i)
            {
                return(Vector2i.ComponentMultiply((Vector2i)obj1, (Vector2i)obj2));
            }
            if (obj1 is Vector3i)
            {
                return(Vector3i.ComponentMultiply((Vector3i)obj1, (Vector3i)obj2));
            }
            if (obj1 is Vector4i)
            {
                return(Vector4i.ComponentMultiply((Vector4i)obj1, (Vector4i)obj2));
            }

            // Other types.
            if (obj1 is uint)
            {
                return((uint)obj1 * (uint)obj2);
            }
            if (obj1 is short)
            {
                return((short)obj1 * (short)obj2);
            }
            if (obj1 is ushort)
            {
                return((ushort)obj1 * (ushort)obj2);
            }
            if (obj1 is byte)
            {
                return((byte)obj1 * (byte)obj2);
            }
            if (obj1 is ulong)
            {
                return((ulong)obj1 * (ulong)obj2);
            }
            if (obj1 is long)
            {
                return((long)obj1 * (long)obj2);
            }


            throw new NotSupportedException("Unsupported type " + obj1.GetType());
        }