Ejemplo n.º 1
0
        public static Accum MakeAccum(long val)
        {
            Accum k = new Accum();

            k.longValue = val;
            return(k);
        }
Ejemplo n.º 2
0
        public static Accum Sin(Accum x)
        {
            if (!tablesGenerated)
            {
                GenerateTables();
            }

            long xVal = x.Value;

            while (xVal > PI_2.Value)
            {
                xVal -= PI_2.Value;
            }
            while (xVal < 0)
            {
                xVal += PI_2.Value;
            }

            long x1 = (long)(Math.Floor(((double)xVal / Accum.FracUnit) / (Math.PI * 2 / SineLUTSize))),    //(Floor (xVal / (PI_2 / SineLUTSize))),
                 x2 = x1 + 1;

            Accum y1 = SineLUT [x1],
                  y2 = SineLUT [x2],
                  y  = y1 + (y2 - y1) * (x * new Accum(SineLUTSize) / PI_2 - new Accum(x1));

            return(y1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Scales the Vector3k to unit length.
        /// </summary>
        public void Normalize()
        {
            Accum scale = Accum.One / this.Length;

            x *= scale;
            y *= scale;
            z *= scale;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Rounds a number upwards to the next integer. (Towards positive infinity)
 /// </summary>
 /// <param name="x">The number to round.</param>
 /// <returns>Returns x rounded upwards to the next integer.</returns>
 public static Accum Ceil(Accum x)
 {
     return(Accum.MakeAccum(
                (x.Value < 0) ?
                (x.Value & unchecked ((long)0xFFFFFFFFFFFF0000)) :
                ((x.Value + (Accum.FracUnit - 1)) & unchecked ((long)0xFFFFFFFFFFFF0000))
                ));
 }
Ejemplo n.º 5
0
        public static Vector3k operator /(double val, Vector3k vec)
        {
            var valAccum = new Accum(val);

            vec.x /= valAccum;
            vec.y /= valAccum;
            vec.z /= valAccum;
            return(vec);
        }
Ejemplo n.º 6
0
        public static Vector3k operator +(long val, Vector3k vec)
        {
            var valAccum = new Accum(val);

            vec.x += valAccum;
            vec.y += valAccum;
            vec.z += valAccum;
            return(vec);
        }
Ejemplo n.º 7
0
        public static Vector3k operator -(int val, Vector3k vec)
        {
            var valAccum = new Accum(val);

            vec.x -= valAccum;
            vec.y -= valAccum;
            vec.z -= valAccum;
            return(vec);
        }
Ejemplo n.º 8
0
        // Sin/Cos: pi*2: 411774; LUT size: 131072;

        // LUT generation
        public static void GenerateTables()
        {
            for (int i = 0; i < SineLUT.Length; i++)
            {
                SineLUT [i] = new Accum(Math.Sin((Math.PI * 2 / SineLUTSize) * i));
            }

            tablesGenerated = true;
        }
Ejemplo n.º 9
0
        // Multiplication
        public static Vector3k operator *(Vector3k vec, double val)
        {
            var valAccum = new Accum(val);

            vec.x *= valAccum;
            vec.y *= valAccum;
            vec.z *= valAccum;
            return(vec);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Returns x to the nth potency
        /// </summary>
        /// <param name="x">A number</param>
        /// <param name="n">An exponent</param>
        public static Accum Pow(Accum x, int n)
        {
            Accum ret = x;

            for (; n > 0; n--)
            {
                ret.Value *= x.Value;
            }

            return(ret);
        }
Ejemplo n.º 11
0
        // Square root
        /// <summary>
        /// Returns the square root of a number
        /// </summary>
        /// <param name="x">A number</param>
        public static Accum Sqrt(Accum x)
        {
            if (x == Accum.One)
            {
                return(new Accum(Accum.FracUnit));
            }
            if (x <= Accum.Zero)
            {
                return(Accum.Zero);
            }

            long val  = 150 * Accum.FracUnit,
                 xVal = x.Value;

            for (int i = 0; i < 15; i++)
            {
                val = (val + Accum.SafeDivision(xVal, val)) >> 1;
            }

            return(Accum.MakeAccum(val));
        }
Ejemplo n.º 12
0
        public static Accum WrapAngle(Accum x)
        {
            const int val = 360 << 16; // Just to make sure it doesn't calculate 360 << 16 on every WrapAngle call...

            return(Accum.MakeAccum(x.Value % val));
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Truncates a number, dropping the fractional part.
 /// </summary>
 /// <param name="x">The number to truncate</param>
 /// <returns>Returns x without the fractional part</returns>
 public static Accum Truncate(Accum x)
 {
     return(new Accum(x.Value & 0xFFFF0000));
 }
Ejemplo n.º 14
0
 public Vector3k(Accum newX, Accum newY, Accum newZ)
 {
     x = newX;
     y = newY;
     z = newZ;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Rounds a number to the nearest integer.
 /// </summary>
 /// <param name="x">The number to round.</param>
 /// <returns>Returns x rounded to the nearest integer.</returns>
 public static Accum Round(Accum x)
 {
     return(new Accum((x.Value + (Accum.FracUnit / 2)) & 0xFFFF0000));
 }
Ejemplo n.º 16
0
 // Potentiation (?)
 /// <summary>
 /// Returns the square of a number
 /// </summary>
 /// <param name="x">A number</param>
 public static Accum Square(Accum x)
 {
     return(x * x);
 }
Ejemplo n.º 17
0
 public Accum(Accum val)
 {
     longValue = val.longValue;
 }
Ejemplo n.º 18
0
        /*public static Accum AtanDegrees (Accum x) { return DegreesToRadians (Atan (x)); }
         * public static Accum Atan2Degrees (Accum x, Accum y) { return DegreesToRadians (Atan2 (x, y); }*/
        #endregion

        #region Basic math functions
        // Absolute
        /// <summary>
        /// Returns the absolute value of a number
        /// </summary>
        /// <param name="x">A number</param>
        public static Accum Abs(Accum val)
        {
            return(val.Value < 0 ? -val : val);
        }
Ejemplo n.º 19
0
 public static Accum CosDegrees(Accum x)
 {
     return(Sin(DegreesToRadians(x) + PIOver2));
 }
Ejemplo n.º 20
0
 public static Accum SinDegrees(Accum x)
 {
     return(Sin(DegreesToRadians(x)));
 }
Ejemplo n.º 21
0
 // Clamping
 /// <summary>
 /// Clamps a number between a minimum and a maximum.
 /// </summary>
 /// <param name="n">The number to clamp.</param>
 /// <param name="min">The minimum allowed value.</param>
 /// <param name="max">The maximum allowed value.</param>
 /// <returns>min, if n is lower than min; max, if n is higher than max; n otherwise.</returns>
 public static Accum Clamp(Accum n, Accum min, Accum max)
 {
     return(Accum.MakeAccum(Math.Max(Math.Min(n.Value, max.Value), min.Value)));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Clamps a number between a minimum and a maximum.
 /// </summary>
 /// <param name="n">The number to clamp.</param>
 /// <param name="min">The minimum allowed value.</param>
 /// <param name="max">The maximum allowed value.</param>
 /// <returns>min, if n is lower than min; max, if n is higher than max; n otherwise.</returns>
 public static Accum ClampInt(Accum n, int min, int max)
 {
     return(Accum.MakeAccum(Math.Max(Math.Min(n.Value, max << 16), min << 16)));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Convert radians to degrees.
 /// </summary>
 /// <param name="radians">An angle in radians.</param>
 /// <returns>The angle expressed in degrees.</returns>
 public static Accum RadiansToDegrees(Accum radians)
 {
     return(radians * radToDegAccum);
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Convert degrees to radians.
 /// </summary>
 /// <param name="degrees">An angle in degrees.</param>
 /// <returns>The angle expressed in radians.</returns>
 public static Accum DegreesToRadians(Accum degrees)
 {
     return(degrees * degToRadAccum);
 }
Ejemplo n.º 25
0
 public static Accum Cos(Accum x)
 {
     return(Sin(x + PIOver2));
 }
Ejemplo n.º 26
0
 public Vertex(Accum x, Accum y, Accum z, Accum normal) :
     this(x, y, z)
 {
     Normal = normal;
 }
Ejemplo n.º 27
0
 public static bool IsBetween(Accum x, Accum min, Accum max)
 {
     return(x >= min && x <= max);
 }
Ejemplo n.º 28
0
 public Vertex(Accum x, Accum y, Accum z)
 {
     X = x;
     Y = y;
     Z = z;
 }