public static Vec4 Max(Vec4 lhs, Vec4 rhs) { Vec4 nv; nv.x = Math.Max(lhs.x, rhs.x); nv.y = Math.Max(lhs.y, rhs.y); nv.z = Math.Max(lhs.z, rhs.z); nv.w = Math.Max(lhs.w, rhs.w); return(nv); }
public static Vec4 Clamp(Vec4 v, double min = 0, double max = 1) { Vec4 nv; nv.x = v.x <min?min : v.x> max ? max : v.x; nv.y = v.y <min?min : v.y> max ? max : v.y; nv.z = v.z <min?min : v.z> max ? max : v.z; nv.w = v.w <min?min : v.w> max ? max : v.w; return(nv); }
public static Vec4 Clamp(Vec4 v, Vec4 min, Vec4 max) { Vec4 nv; nv.x = v.x <min.x?min.x : v.x> max.x ? max.x : v.x; nv.y = v.y <min.y?min.y : v.y> max.y ? max.y : v.y; nv.z = v.z <min.z?min.z : v.z> max.z ? max.z : v.z; nv.w = v.w <min.w?min.w : v.w> max.w ? max.w : v.w; return(nv); }
public static Vec4 Normalize(this Vec4 v) { double len = v.Length(); if (len == 0) { return(new Vec4()); } return(new Vec4(v.x / len, v.y / len, v.z / len, v.w / len)); }
public static Vec4 Refract(Vec4 srcNorm, Vec4 axisNorm, double eta) { double d = Dot(srcNorm, axisNorm); double k = 1 - eta * eta * (1 - d * d); if (k < 0) { return(new Vec4(srcNorm.x, srcNorm.y, srcNorm.z, srcNorm.w)); } return(eta * srcNorm - (eta * d + Math.Sqrt(k)) * axisNorm); }
public static bool ValueEquals(this Vec4 lhs, Vec4 rhs) { double pt = MathX.TOLERANCE, nt = -pt; double dx = lhs.x - rhs.x; double dy = lhs.y - rhs.y; double dz = lhs.z - rhs.z; double dw = lhs.w - rhs.w; return(dx <= pt && dx >= nt && dy <= pt && dy >= nt && dz <= pt && dz >= nt && dw <= pt && dw >= nt); }
public static int MaxAxis(this Vec4 v) { if (v.x > v.y && v.x > v.z && v.x > v.w) { return(0); } if (v.y > v.z && v.y > v.w) { return(1); } if (v.z > v.w) { return(2); } return(3); }
public static int MinAxis(this Vec4 v) { if (v.x <= v.y && v.x <= v.z && v.x <= v.w) { return(0); } if (v.y <= v.z && v.y <= v.w) { return(1); } if (v.z <= v.w) { return(2); } return(3); }
public unsafe void SetRow(int index, Vec4 row) { if (index >= 0 && index < 4) fixed(double *ptr = &m00) { double *p = ptr + index * 4; p[0] = row.x; p[1] = row.y; p[2] = row.z; p[3] = row.w; } else { throw new IndexOutOfRangeException(); } }
public unsafe void SetColumn(int index, Vec4 column) { if (index >= 0 && index < 4) fixed(double *ptr = &m00) { double *p = ptr + index; p[0] = column.x; p[4] = column.y; p[8] = column.z; p[12] = column.w; } else { throw new IndexOutOfRangeException(); } }
public SqMat4 Inverse() { Vec4 r0 = LinearEquation.SolveEquations( new LnEqn <Vec4>(GetColumn(0), 1), new LnEqn <Vec4>(GetColumn(1), 0), new LnEqn <Vec4>(GetColumn(2), 0), new LnEqn <Vec4>(GetColumn(3), 0)); Vec4 r1 = LinearEquation.SolveEquations( new LnEqn <Vec4>(GetColumn(0), 0), new LnEqn <Vec4>(GetColumn(1), 1), new LnEqn <Vec4>(GetColumn(2), 0), new LnEqn <Vec4>(GetColumn(3), 0)); Vec4 r2 = LinearEquation.SolveEquations( new LnEqn <Vec4>(GetColumn(0), 0), new LnEqn <Vec4>(GetColumn(1), 0), new LnEqn <Vec4>(GetColumn(2), 1), new LnEqn <Vec4>(GetColumn(3), 0)); return(new SqMat4(r0, r1, r2, new Vec4(0, 0, 0, 1))); }
public static double Dot(this Vec4 lhs, Vec4 rhs) { return(lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w); }
public static Vec4 Alerp(Vec4 a, Vec4 b, Vec4 x) { return((x - a) / (b - a)); }
public static Vec4 Lerp(Vec4 a, Vec4 b, Vec4 t) { return(a + (b - a) * t); }
public static bool IsNaV(this Vec4 v) { return(double.IsNaN(v.x) || double.IsNaN(v.y) || double.IsNaN(v.z) || double.IsNaN(v.w)); }
public static Vec4 Mirror(Vec4 src, Vec4 axis) { return(2 * Project(src, axis) - src); }
public static Vec4 Reflect(Vec4 src, Vec4 axis) { return(src - 2 * Project(src, axis)); }
public static Vec4 Orthogonalize(Vec4 src, Vec4 dst) { return(src - Dot(src, dst) / SqrLength(dst) * dst); }
public static Vec4 Project(Vec4 src, Vec4 dst) { return(Dot(src, dst) / SqrLength(dst) * dst); }
public static double Project01(this Vec4 src, Vec4 dst) { return(Dot(src, dst) / SqrLength(dst)); }
public static double Angle(this Vec4 lhsNorm, Vec4 rhsNorm) { double cos = Dot(lhsNorm, rhsNorm); return(Math.Acos(cos < -1 ? -1 : cos > 1 ? 1 : cos)); }
public static double SqrLength(this Vec4 v) { return(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); }
public static Vec4 Abs(this Vec4 v) { return(new Vec4(Math.Abs(v.x), Math.Abs(v.y), Math.Abs(v.z), Math.Abs(v.w))); }
public static double Length(this Vec4 v) { return(Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w)); }