Пример #1
0
        public static fp Magnitude(fp3 v)
        {
            fp r;

            r.value =
                ((v.x.value * v.x.value) >> fixlut.PRECISION) +
                ((v.y.value * v.y.value) >> fixlut.PRECISION) +
                ((v.z.value * v.z.value) >> fixlut.PRECISION);

            fp r1;

            //manually inlined Sqrt()
            if (r.value == 0)
            {
                r1.value = 0;
            }
            else
            {
                var b = (r.value >> 1) + 1L;
                var c = (b + (r.value / b)) >> 1;

                while (c < b)
                {
                    b = c;
                    c = (b + (r.value / b)) >> 1;
                }

                r1.value = b << (fixlut.PRECISION >> 1);
            }

            r = r1;

            return(r);
        }
Пример #2
0
 public fp4(fp3 v, fp w)
 {
     x      = v.x;
     y      = v.y;
     z      = v.z;
     this.w = w;
 }
Пример #3
0
        public static fp3 Normalize(fp3 v)
        {
            if (v == fp3.zero)
            {
                return(fp3.zero);
            }

            return(v / Magnitude(v));
        }
Пример #4
0
        public static fp3 Cross(fp3 a, fp3 b)
        {
            fp3 r;

            r.x = a.y * b.z - a.z * b.y;
            r.y = a.z * b.x - a.x * b.z;
            r.z = a.x * b.y - a.y * b.x;

            return(r);
        }
Пример #5
0
        public static fp DistanceSqr(fp3 p1, fp3 p2)
        {
            fp3 v;

            v.x.value = p1.x.value - p2.x.value;
            v.y.value = p1.y.value - p2.y.value;
            v.z.value = p1.z.value - p2.z.value;

            return(MagnitudeSqr(v));
        }
Пример #6
0
        public static fp3 Normalize(fp3 v, out fp magnitude)
        {
            if (v == fp3.zero)
            {
                magnitude = fp._0;
                return(fp3.zero);
            }

            magnitude = Magnitude(v);
            return(v / magnitude);
        }
Пример #7
0
        public static fp3 Normalize(fp3 v)
        {
            if (v == fp3.zero)
            {
                return(fp3.zero);
            }

            var magnitude = Magnitude(v);

            return(v / magnitude);
        }
Пример #8
0
        public static fp Magnitude(fp3 v)
        {
            fp r;

            r.value =
                ((v.x.value * v.x.value) >> fixlut.PRECISION) +
                ((v.y.value * v.y.value) >> fixlut.PRECISION) +
                ((v.z.value * v.z.value) >> fixlut.PRECISION);

            return(Sqrt(r));
        }
Пример #9
0
        public static fp Angle(fp3 a, fp3 b)
        {
            var sqr = MagnitudeSqr(a) * MagnitudeSqr(b);
            var n   = Sqrt(sqr);

            if (n < fp.epsilon)
            {
                return(fp._0);
            }

            return(Acos(Clamp(Dot(a, b) / n, fp.minus_one, fp._1)) * fp.rad2deg);
        }
Пример #10
0
        public static fp Dot(fp3 a, fp3 b)
        {
            var x = ((a.x.value * b.x.value) >> fixlut.PRECISION);
            var y = ((a.y.value * b.y.value) >> fixlut.PRECISION);
            var z = ((a.z.value * b.z.value) >> fixlut.PRECISION);

            fp r;

            r.value = x + y + z;

            return(r);
        }
Пример #11
0
        public static fp3 ProjectOnPlane(fp3 vector, fp3 planeNormal)
        {
            var sqrMag = MagnitudeSqr(planeNormal);

            if (sqrMag < fp.epsilon)
            {
                return(vector);
            }

            var dot = Dot(vector, planeNormal);

            return(vector - planeNormal * dot / sqrMag);
        }
Пример #12
0
        public static fp3 Project(fp3 vector, fp3 normal)
        {
            var sqrMag = MagnitudeSqr(normal);

            if (sqrMag < fp.epsilon)
            {
                return(fp3.zero);
            }

            var dot = Dot(vector, normal);

            return(normal * dot / sqrMag);
        }
Пример #13
0
        public static fp3 MoveTowards(fp3 current, fp3 target, fp maxDelta)
        {
            var v            = target - current;
            var sqrMagnitude = MagnitudeSqr(v);

            if (v == fp3.zero || maxDelta >= fp._0 && sqrMagnitude <= maxDelta * maxDelta)
            {
                return(target);
            }

            var magnitude = Sqrt(sqrMagnitude);

            return(current + v / magnitude * maxDelta);
        }
Пример #14
0
        public static fp AngleSigned(fp3 a, fp3 b, fp3 axis)
        {
            var  angle = Angle(a, b);
            long num2  = ((a.y.value * b.z.value) >> fixlut.PRECISION) - ((a.z.value * b.y.value) >> fixlut.PRECISION);
            long num3  = ((a.z.value * b.x.value) >> fixlut.PRECISION) - ((a.x.value * b.z.value) >> fixlut.PRECISION);
            long num4  = ((a.x.value * b.y.value) >> fixlut.PRECISION) - ((a.y.value * b.x.value) >> fixlut.PRECISION);
            var  sign  = (((axis.x.value * num2) >> fixlut.PRECISION) +
                          ((axis.y.value * num3) >> fixlut.PRECISION) +
                          ((axis.z.value * num4) >> fixlut.PRECISION)) < 0
                ? fp.minus_one
                : fp._1;

            return(angle * sign);
        }
Пример #15
0
        public static fp3 MagnitudeClamp(fp3 v, fp length)
        {
            var sqrMagnitude = MagnitudeSqr(v);

            if (sqrMagnitude <= length * length)
            {
                return(v);
            }

            var magnitude  = Sqrt(sqrMagnitude);
            var normalized = v / magnitude;

            return(normalized * length);
        }
Пример #16
0
 public fp3 NextFp3(fp3 max)
 {
     return(NextFp3() * max);
 }
Пример #17
0
        public static fp3 Reflect(fp3 vector, fp3 normal)
        {
            var num = -fp._2 * Dot(normal, vector);

            return(new fp3(num * normal.x + vector.x, num * normal.y + vector.y, num * normal.z + vector.z));
        }
Пример #18
0
 public static fp Sum(fp3 v)
 {
     return(new fp(v.x.value + v.y.value + v.z.value));
 }
Пример #19
0
 public static fp3 LerpUnclamped(fp3 from, fp3 to, fp t)
 {
     return(new fp3(LerpUnclamped(from.x, to.x, t), LerpUnclamped(from.y, to.y, t), LerpUnclamped(from.z, to.z, t)));
 }
Пример #20
0
        public static fp3 Lerp(fp3 from, fp3 to, fp t)
        {
            t = Clamp(t, fp.zero, fp.one);

            return(new fp3(Lerp(from.x, to.x, t), Lerp(from.y, to.y, t), Lerp(from.z, to.z, t)));
        }
Пример #21
0
 public static fp3 Abs(fp3 v)
 {
     return(new fp3(Abs(v.x), Abs(v.y), Abs(v.z)));
 }
Пример #22
0
 public static fp3 Max(fp3 a, fp3 b)
 {
     return(new fp3(Max(a.x, b.x), Max(a.y, b.y), Max(a.z, b.z)));
 }
Пример #23
0
 public static fp3 MagnitudeSet(fp3 v, fp length)
 {
     return(Normalize(v) * length);
 }
Пример #24
0
 public static fp Dot(fp3 a, fp3 b)
 {
     return(new fp(((a.x.value * b.x.value) >> fixlut.PRECISION) + ((a.y.value * b.y.value) >> fixlut.PRECISION) + ((a.z.value * b.z.value) >> fixlut.PRECISION)));
 }
Пример #25
0
 public fp3 NextFp3(fp3 min, fp3 max)
 {
     return(NextFp3() * (max - min) + min);
 }
Пример #26
0
 public static fp RadiansSkipNormalize(fp3 a, fp3 b)
 {
     return(Acos(Clamp(Dot(a, b), fp.minus_one, fp._1)));
 }
Пример #27
0
 public static fp Radians(fp3 a, fp3 b)
 {
     return(Acos(Clamp(Dot(Normalize(a), Normalize(b)), fp.minus_one, fp._1)));
 }