Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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));
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
 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);
 }
Esempio n. 8
0
 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);
 }
Esempio n. 9
0
        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();
            }
        }
Esempio n. 10
0
        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();
            }
        }
Esempio n. 11
0
        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)));
        }
Esempio n. 12
0
 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);
 }
Esempio n. 13
0
 public static Vec4 Alerp(Vec4 a, Vec4 b, Vec4 x)
 {
     return((x - a) / (b - a));
 }
Esempio n. 14
0
 public static Vec4 Lerp(Vec4 a, Vec4 b, Vec4 t)
 {
     return(a + (b - a) * t);
 }
Esempio n. 15
0
 public static bool IsNaV(this Vec4 v)
 {
     return(double.IsNaN(v.x) || double.IsNaN(v.y) || double.IsNaN(v.z) || double.IsNaN(v.w));
 }
Esempio n. 16
0
 public static Vec4 Mirror(Vec4 src, Vec4 axis)
 {
     return(2 * Project(src, axis) - src);
 }
Esempio n. 17
0
 public static Vec4 Reflect(Vec4 src, Vec4 axis)
 {
     return(src - 2 * Project(src, axis));
 }
Esempio n. 18
0
 public static Vec4 Orthogonalize(Vec4 src, Vec4 dst)
 {
     return(src - Dot(src, dst) / SqrLength(dst) * dst);
 }
Esempio n. 19
0
 public static Vec4 Project(Vec4 src, Vec4 dst)
 {
     return(Dot(src, dst) / SqrLength(dst) * dst);
 }
Esempio n. 20
0
 public static double Project01(this Vec4 src, Vec4 dst)
 {
     return(Dot(src, dst) / SqrLength(dst));
 }
Esempio n. 21
0
        public static double Angle(this Vec4 lhsNorm, Vec4 rhsNorm)
        {
            double cos = Dot(lhsNorm, rhsNorm);

            return(Math.Acos(cos < -1 ? -1 : cos > 1 ? 1 : cos));
        }
Esempio n. 22
0
 public static double SqrLength(this Vec4 v)
 {
     return(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
 }
Esempio n. 23
0
 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)));
 }
Esempio n. 24
0
 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));
 }