UltimateZoneBuilder/Source/Core/Rendering/Vector3.cs
2019-08-08 18:24:33 +02:00

144 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeImp.DoomBuilder.Rendering
{
public struct Vector3
{
public Vector3(float v)
{
X = v;
Y = v;
Z = v;
}
public Vector3(Vector2 xy, float z)
{
X = xy.X;
Y = xy.Y;
Z = z;
}
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public float X;
public float Y;
public float Z;
public static Vector4 Transform(Vector3 vector, Matrix transform)
{
return new Vector4(
(((vector.X * transform.M11) + (vector.Y * transform.M21)) + (vector.Z * transform.M31)) + transform.M41,
(((vector.X * transform.M12) + (vector.Y * transform.M22)) + (vector.Z * transform.M32)) + transform.M42,
(((vector.X * transform.M13) + (vector.Y * transform.M23)) + (vector.Z * transform.M33)) + transform.M43,
(((vector.X * transform.M14) + (vector.Y * transform.M24)) + (vector.Z * transform.M34)) + transform.M44);
}
public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount)
{
float squared = amount * amount;
float cubed = amount * squared;
float part1 = ((2.0f * cubed) - (3.0f * squared)) + 1.0f;
float part2 = (-2.0f * cubed) + (3.0f * squared);
float part3 = (cubed - (2.0f * squared)) + amount;
float part4 = cubed - squared;
return new Vector3(
(((value1.X * part1) + (value2.X * part2)) + (tangent1.X * part3)) + (tangent2.X * part4),
(((value1.Y * part1) + (value2.Y * part2)) + (tangent1.Y * part3)) + (tangent2.Y * part4),
(((value1.Z * part1) + (value2.Z * part2)) + (tangent1.Z * part3)) + (tangent2.Z * part4));
}
public static float DistanceSquared(Vector3 a, Vector3 b)
{
Vector3 c = b - a;
return Vector3.Dot(c, c);
}
public static float Dot(Vector3 a, Vector3 b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
public static Vector3 Cross(Vector3 left, Vector3 right)
{
Vector3 result = new Vector3();
result.X = left.Y * right.Z - left.Z * right.Y;
result.Y = left.Z * right.X - left.X * right.Z;
result.Z = left.X * right.Y - left.Y * right.X;
return result;
}
public float Length()
{
return (float)Math.Sqrt(Vector3.Dot(this, this));
}
public static Vector3 Normalize(Vector3 v)
{
v.Normalize();
return v;
}
public void Normalize()
{
float len = Length();
if (len > 0.0f)
{
X /= len;
Y /= len;
Z /= len;
}
}
public override bool Equals(object o)
{
if (o is Vector3)
{
Vector3 v = (Vector3)o;
return this == v;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode();
}
public static Vector3 operator +(Vector3 left, Vector3 right)
{
return new Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
public static Vector3 operator -(Vector3 left, Vector3 right)
{
return new Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
public static Vector3 operator -(Vector3 v)
{
return new Vector3(-v.X, -v.Y, -v.Z);
}
public static bool operator ==(Vector3 left, Vector3 right)
{
return left.X == right.X && left.Y == right.Y && left.Z == right.Z;
}
public static bool operator !=(Vector3 left, Vector3 right)
{
return left.X != right.X || left.Y != right.Y || left.Z != right.Z;
}
}
}