Example #1
0
        private int FindLevelByPosition(Bounds3 bounds)
        {
            var furthestMax = Functions.MaximumComponent(Functions.Abs(bounds.Maximum));
            var furthestMin = Functions.MaximumComponent(Functions.Abs(bounds.Minimum));

            return((int)Math.Ceiling(Functions.Log2(Math.Max(furthestMax, furthestMin))));
        }
Example #2
0
        public void TestFunctionAbs()
        {
            mockSparkContextProxy.Setup(m => m.CreateFunction(It.IsAny <string>(), It.IsAny <IColumnProxy>()));
            Column column = GeneratorColum();

            Functions.Abs(column);
            mockSparkContextProxy.Verify(m => m.CreateFunction("abs", column.ColumnProxy), Times.Once);
        }
Example #3
0
        public static double ChessboardMetric(int x1, int y1, int z1, int x2, int y2, int z2)
        {
            double x = Functions.Abs(x1 - x2);
            double y = Functions.Abs(y1 - y2);
            double z = Functions.Abs(z1 - z2);

            return(Functions.Max(x, Functions.Max(y, z)));
        }
Example #4
0
        public static double ManhattanMetric(int x1, int y1, int z1, int x2, int y2, int z2)
        {
            double x = Functions.Abs(x1 - x2);
            double y = Functions.Abs(y1 - y2);
            double z = Functions.Abs(z1 - z2);

            return(x + y + z);
        }
Example #5
0
        public override double Evaluate(double x, double y, double z)
        {
            double total     = 0.0;
            double frequency = Frequency;
            double amplitude = 1.0;

            for (int i = 0; i < Octaves; ++i)
            {
                total += Functions.Abs(Source.Evaluate(x * frequency, y * frequency, z * frequency) * amplitude);

                frequency *= Lacunarity;
                amplitude *= Persistance;
            }
            return(total);
        }
Example #6
0
        /// <summary>
        /// Interpolates between two unit quaternions, using spherical linear interpolation.
        /// </summary>
        /// <param name="start">Start quaternion.</param>
        /// <param name="end">End quaternion.</param>
        /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
        /// <returns>The spherical linear interpolation of the two quaternions.</returns>
        ///  <remarks>
        /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
        /// </remarks>
        public static Quaternionf Slerp(Quaternionf start, Quaternionf end, float amount)
        {
            var cosTheta = Dot(start, end);

            //Cannot use slerp, use lerp instead
            if (Functions.Abs(cosTheta) - 1 < float.Epsilon)
            {
                return(Lerp(start, end, amount));
            }
            var theta    = Functions.Acos(cosTheta);
            var sinTheta = Functions.Sin(theta);
            var t0       = Functions.Sin((1 - amount) * theta) / sinTheta;
            var t1       = Functions.Sin(amount * theta) / sinTheta;

            return(t0 * start + t1 * end);
        }
Example #7
0
        public override double Evaluate(double x, double y, double z, double w, double v, double u)
        {
            x *= Frequency;
            y *= Frequency;
            z *= Frequency;
            w *= Frequency;
            v *= Frequency;
            u *= Frequency;

            double value  = 0.0;
            double weight = 1.0;

            double frequency = 1.0;

            for (int octave = 0; octave < Octaves; ++octave)
            {
                // Get the coherent-noise value.
                double signal = Source.Evaluate(x, y, z, w, v, u);

                // Make the ridges.
                signal = Functions.Abs(signal);
                signal = Offset - signal;

                // Square the signal to increase the sharpness of the ridges.
                signal *= signal;

                // The weighting from the previous octave is applied to the signal.
                // Larger values have higher weights, producing sharp points along the
                // ridges.
                signal *= weight;

                // Weight successive contributions by the previous signal.
                weight = Functions.Clamp(signal * Gain, 0.0, 1.0);

                double spectralWeight = Functions.Pow(frequency, -Sharpness);
                value += (signal * spectralWeight);

                x         *= Lacunarity;
                y         *= Lacunarity;
                z         *= Lacunarity;
                w         *= Lacunarity;
                v         *= Lacunarity;
                u         *= Lacunarity;
                frequency *= Lacunarity;
            }
            return((value * 1.25) - 1.0);
        }
Example #8
0
        /// <summary>
        /// Calculates the bounding box of a cubic bezier.
        /// </summary>
        /// <remarks>
        /// https://iquilezles.org/www/articles/bezierbbox/bezierbbox.htm
        /// </remarks>
        /// <param name="p0"></param>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p3"></param>
        /// <returns></returns>
        public static Bounds3 BoundsCubic(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
        {
            // extremes
            Vector3 min = Vector3.Minimum(p0, p3);
            Vector3 max = Vector3.Maximum(p0, p3);

            // note pascal triangle coefficnets
            Vector3 c = -1 * p0 + 1 * p1;
            Vector3 b = 1 * p0 - 2 * p1 + 1 * p2;
            Vector3 a = -1 * p0 + 3 * p1 - 3 * p2 + 1 * p3;

            Vector3 h = b * b - a * c;

            // real solutions
            if (h.X > 0 || h.Y > 0 || h.Z > 0)
            {
                Vector3 g  = Functions.Sqrt(Functions.Abs(h));
                Vector3 t1 = Functions.Clamp((-b - g) / a, 0, 1);
                Vector3 s1 = Vector3.One - t1;
                Vector3 t2 = Functions.Clamp((-b + g) / a, 0, 1);
                Vector3 s2 = Vector3.One - t2;
                Vector3 q1 = s1 * s1 * s1 * p0 + 3 * s1 * s1 * t1 * p1 + 3 * s1 * t1 * t1 * p2 + t1 * t1 * t1 * p3;
                Vector3 q2 = s2 * s2 * s2 * p0 + 3 * s2 * s2 * t2 * p1 + 3 * s2 * t2 * t2 * p2 + t2 * t2 * t2 * p3;

                if (h.X > 0.0)
                {
                    min.X = Min(min.X, Min(q1.X, q2.X));
                    max.X = Max(max.X, Max(q1.X, q2.X));
                }
                if (h.Y > 0.0)
                {
                    min.Y = Min(min.Y, Min(q1.Y, q2.Y));
                    max.Y = Max(max.Y, Max(q1.Y, q2.Y));
                }
                if (h.Z > 0.0)
                {
                    min.Z = Min(min.Z, Min(q1.Z, q2.Z));
                    max.Z = Max(max.Z, Max(q1.Z, q2.Z));
                }
            }

            return(new Bounds3(min, max));
        }
Example #9
0
 /// <summary>
 /// Returns the absolute value (per component).
 /// </summary>
 /// <param name="value">A point.</param>
 /// <returns>The absolute value (per component) of value.</returns>
 public static Point2d Abs(Point2d value)
 {
     return(new Point2d(Functions.Abs(value.X), Functions.Abs(value.Y)));
 }
Example #10
0
 /// <summary>
 /// Returns the manhatten distance between two points.
 /// </summary>
 /// <param name="value1">The first point.</param>
 /// <param name="value2">The second point.</param>
 /// <returns>The manhatten distance between value1 and value2.</returns>
 public static double ManhattenDistance(Point2d value1, Point2d value2)
 {
     return(Functions.Abs(value2.X - value1.X) + Functions.Abs(value2.Y - value1.Y));
 }
Example #11
0
        /// <summary>
        /// Balances the <see cref="ChemicalEquation"/>.
        /// </summary>
        public void Balance()
        {
            if (Products.Count != 0 &&
                Reactants.Count != 0 &&
                !IsBalanced())
            {
                var elements  = GetUniqueElements(Reactants);
                var molecules = new List <Tuple <Molecule, int> >();
                molecules.AddRange(Reactants);
                molecules.AddRange(Products);

                var matrix = new Matrix(elements.Count, molecules.Count);

                // Populate matrix data with element amounts.
                for (var row_index = 0; row_index < matrix.Data.GetLength(0); ++row_index)
                {
                    for (var col_index = 0; col_index < matrix.Data.GetLength(1); ++col_index)
                    {
                        var element_match = molecules[col_index].Item1.Elements.Find(element_pair => element_pair.Item1.Equals(elements[row_index].Item1));

                        if (element_match != null)
                        {
                            var product_scalar      = (col_index >= Reactants.Count) ? -1 : 1;
                            var element_coefficient = new Fraction(element_match.Item2);

                            element_coefficient = Fraction.Multiply(element_coefficient, product_scalar);
                            matrix.Data[row_index, col_index] = Fraction.Add(matrix.Data[row_index, col_index], element_coefficient);
                        }
                    }
                }

                // Solve the matrix.
                var X = Matrix.Submatrix(matrix, new[] { 0, matrix.Data.GetLength(0) - 1 }, new[] { 0, matrix.Data.GetLength(1) - 2 });
                var Y = Matrix.Submatrix(matrix, new[] { 0, matrix.Data.GetLength(0) - 1 }, new[] { matrix.Data.GetLength(1) - 1, matrix.Data.GetLength(1) - 1 });

                var XtX = Matrix.Multiply(Matrix.Transpose(X), X);
                var XtY = Matrix.Multiply(Matrix.Transpose(X), Y);

                var solution_mat = Matrix.Multiply(Matrix.Inverse(XtX), XtY);

                var    molecule_coefficients = new Fraction[molecules.Count];
                double LCM = 1.00;


                // Create solution vector.
                for (var i = 0; i < molecule_coefficients.Length; ++i)
                {
                    if (i < solution_mat.Data.GetLength(0))
                    {
                        molecule_coefficients[i] = Functions.Abs(solution_mat.Data[i, 0]);
                        LCM = Functions.LeastCommonMultiple(molecule_coefficients[i].Denominator, (long)LCM);
                    }
                    else
                    {
                        molecule_coefficients[i] = new Fraction(1);
                    }
                }

                // Normalize values.
                for (var i = 0; i < molecule_coefficients.Length; ++i)
                {
                    var multiple = (LCM / molecule_coefficients[i].Denominator);
                    molecule_coefficients[i] = Math.Fraction.Multiply(molecule_coefficients[i], new Math.Fraction((int)multiple * (int)LCM, (int)multiple), true);

                    if (i < Reactants.Count)
                    {
                        Reactants[i] = Tuple.Create(Reactants[i].Item1, (int)molecule_coefficients[i].Approximate());
                    }
                    else
                    {
                        Products[System.Math.Abs(Reactants.Count - i)] = Tuple.Create(
                            Products[System.Math.Abs(Reactants.Count - i)].Item1,
                            (int)molecule_coefficients[i].Approximate());
                    }
                }
            }
        }
Example #12
0
 public override double Evaluate(double t)
 {
     return(Amplitude * Functions.Abs((Frequency * t + Phase) - Functions.Floor(Frequency * t + Phase)));
 }
Example #13
0
 /// <summary>
 /// Returns the manhatten distance between two points.
 /// </summary>
 /// <param name="value1">The first point.</param>
 /// <param name="value2">The second point.</param>
 /// <returns>The manhatten distance between value1 and value2.</returns>
 public static float ManhattenDistance(Point3f value1, Point3f value2)
 {
     return(Functions.Abs(value2.X - value1.X) + Functions.Abs(value2.Y - value1.Y) + Functions.Abs(value2.Z - value1.Z));
 }
Example #14
0
 /// <summary>
 /// Returns the absolute value (per component).
 /// </summary>
 /// <param name="value">A point.</param>
 /// <returns>The absolute value (per component) of value.</returns>
 public static Point3f Abs(Point3f value)
 {
     return(new Point3f(Functions.Abs(value.X), Functions.Abs(value.Y), Functions.Abs(value.Z)));
 }
Example #15
0
 public override double Evaluate(double x, double y, double z, double w, double v, double u)
 {
     return(Functions.Pow(Functions.Abs((Source.Evaluate(x, y, z, w, v, u) + 1.0) * 0.5), Exp) * 2.0 - 1.0);
 }
Example #16
0
 public override double Evaluate(double x, double y)
 {
     return(Functions.Pow(Functions.Abs((Source.Evaluate(x, y) + 1.0) * 0.5), Exp) * 2.0 - 1.0);
 }