示例#1
0
        // https://developer.download.nvidia.com/cg/atan2.html
        public static float Atan2(float y, float x)
        {
            float t0, t1, t3, t4;

            t3 = XMath.Abs(x);
            t1 = XMath.Abs(y);
            t0 = XMath.Max(t3, t1);
            t1 = XMath.Min(t3, t1);
            t3 = 1f / t0;
            t3 = t1 * t3;

            t4 = t3 * t3;
            t0 = -0.013480470f;
            t0 = t0 * t4 + 0.057477314f;
            t0 = t0 * t4 - 0.121239071f;
            t0 = t0 * t4 + 0.195635925f;
            t0 = t0 * t4 - 0.332994597f;
            t0 = t0 * t4 + 0.999995630f;
            t3 = t0 * t3;

            t3 = (XMath.Abs(y) > XMath.Abs(x)) ? 1.570796327f - t3 : t3;
            t3 = (x < 0) ? 3.141592654f - t3 : t3;
            t3 = (y < 0) ? -t3 : t3;

            return(t3);
        }
示例#2
0
        /// <summary cref="XMath.Rem(float, float)"/>
        public static float Rem(float x, float y)
        {
            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0f, -result, result));
        }
示例#3
0
        public static double Rem(double x, double y)
        {
            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0, -result, result));
        }
示例#4
0
    private static HitRecord GetTriangleHit(Ray r, dWorldBuffer world, dGPUMesh mesh, float nearerThan)
    {
        Triangle t = new Triangle();
        float    currentNearestDist = nearerThan;
        int      NcurrentIndex      = -1;
        int      material           = 0;
        float    Ndet = 0;

        for (int i = 0; i < mesh.triangleCount; i++)
        {
            t = mesh.GetTriangle(i, world);
            Vec3  tuVec = t.uVector();
            Vec3  tvVec = t.vVector();
            Vec3  pVec  = Vec3.cross(r.b, tvVec);
            float det   = Vec3.dot(tuVec, pVec);

            if (XMath.Abs(det) < nearerThan)
            {
                float invDet = 1.0f / det;
                Vec3  tVec   = r.a - t.Vert0;
                float u      = Vec3.dot(tVec, pVec) * invDet;
                Vec3  qVec   = Vec3.cross(tVec, tuVec);
                float v      = Vec3.dot(r.b, qVec) * invDet;

                if (u > 0 && u <= 1.0f && v > 0 && u + v <= 1.0f)
                {
                    float temp = Vec3.dot(tvVec, qVec) * invDet;
                    if (temp < currentNearestDist)
                    {
                        currentNearestDist = temp;
                        NcurrentIndex      = i;
                        Ndet     = det;
                        material = t.MaterialID;
                    }
                }
            }
        }

        if (NcurrentIndex == -1)
        {
            return(new HitRecord(float.MaxValue, new Vec3(), new Vec3(), false, -1, -1));
        }
        else
        {
            if (Ndet < 0)
            {
                return(new HitRecord(currentNearestDist, r.pointAtParameter(currentNearestDist), -t.faceNormal(), true, material, NcurrentIndex));
            }
            else
            {
                return(new HitRecord(currentNearestDist, r.pointAtParameter(currentNearestDist), t.faceNormal(), false, material, NcurrentIndex));
            }
        }
    }
示例#5
0
        // https://developer.download.nvidia.com/cg/asin.html
        public static float Asin(float x)
        {
            float negate = x < 0 ? 1f : 0f; // float(x < 0);

            x = XMath.Abs(x);
            float ret = -0.0187293f;

            ret *= x;
            ret += 0.0742610f;
            ret *= x;
            ret -= 0.2121144f;
            ret *= x;
            ret += 1.5707288f;
            ret  = 3.14159265358979f * 0.5f - XMath.Sqrt(1.0f - x) * ret;
            return(ret - 2 * negate * ret);
        }
示例#6
0
    public static OrthoNormalBasis fromZ(Vec3 z)
    {
        Vec3 xx;

        if (XMath.Abs(Vec3.dot(z, new Vec3(1, 0, 0))) > 0.99999f)
        {
            xx = Vec3.unitVector(Vec3.cross(new Vec3(0, 1, 0), z));
        }
        else
        {
            xx = Vec3.unitVector(Vec3.cross(new Vec3(1, 0, 0), z));
        }
        Vec3 yy = Vec3.unitVector(Vec3.cross(z, xx));

        return(new OrthoNormalBasis(xx, yy, z));
    }
示例#7
0
        public static void CorrelationMapToRgba32(Index2 index, ArrayView2D <uint> result, ArrayView2D <double> bufIn1)
        {
            double r   = bufIn1[index];
            byte   rad = 0;

            r = XMath.Abs(r);
            if (r > 1)
            {
                r = 1;
            }

            rad = (byte)(255 * r);
            if (rad == 255)
            {
                rad = 254; // because there are issue with saving of white pixels
            }
            result[index] = (uint)(rad + (rad << 8) + (rad << 16) + (255 << 24));
        }
示例#8
0
        public static void CalculateBrightnessStats(Index index, ArrayView3D <short> input, ArrayView <double> meanBrightness, ArrayView <short> maxBrightness, ArrayView <double> standartDeviation)
        {
            long sum = 0;

            short max = 0;

            var band = input.GetSliceView(index).AsLinearView();

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }
                //for deviation and mean sum
                sum += val;
                if (val > max)
                {
                    max = val;
                }
            }

            double mean = sum / (double)band.Length;

            meanBrightness[index] = mean;
            maxBrightness[index]  = max;

            double dividend = 0;

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }

                dividend += XMath.Pow(XMath.Abs(val - mean), 2);
            }

            standartDeviation[index] = XMath.Sqrt(dividend / band.Length);
        }
示例#9
0
        public static void CorrelationMap(Index2 index, ArrayView3D <float> result, ArrayView3D <byte> bufIn)
        {
            if (index.X == bufIn.Width - 1 ||
                index.X == 0 ||
                index.Y == bufIn.Height - 1 ||
                index.Y == 0)
            {
                result[index.X, index.Y, 0] = 1.0f;
                result[index.X, index.Y, 1] = 1.0f;
                result[index.X, index.Y, 2] = 1.0f;
                return;
            }

            var corrX = (float)XMath.Abs(Kernels.PearsonCorrelation(bufIn, new Index2(index.X - 1, index.Y), new Index2(index.X + 1, index.Y)));
            var corrY = (float)XMath.Abs(Kernels.PearsonCorrelation(bufIn, new Index2(index.X, index.Y - 1), new Index2(index.X, index.Y + 1)));

            result[index.X, index.Y, 0] = ((1.0f - corrX));
            result[index.X, index.Y, 1] = (XMath.Sqrt(XMath.Pow(1.0f - corrX, 2) + XMath.Pow(1.0f - corrY, 2)));
            result[index.X, index.Y, 2] = ((1.0f - XMath.Min(corrX, corrY)));
        }
示例#10
0
        public static float Rem(float x, float y)
        {
            if (y == 0.0f ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(float.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0f, -result, result));
        }
示例#11
0
        public static double Rem(double x, double y)
        {
            if (y == 0.0 ||
                XMath.IsInfinity(x) ||
                XMath.IsNaN(x) ||
                XMath.IsNaN(y))
            {
                return(double.NaN);
            }

            if (XMath.IsInfinity(y))
            {
                return(x);
            }

            var xDivY  = XMath.Abs(x * XMath.Rcp(y));
            var result = (xDivY - Floor(xDivY)) * XMath.Abs(y);

            return(Utilities.Select(x < 0.0, -result, result));
        }
        private static void ShrinkKernel(Index2 index,
                                         ArrayView2D <float> xImage,
                                         ArrayView2D <float> bMap,
                                         ArrayView2D <float> aMap,
                                         ArrayView <float> lambdaAlpha,
                                         ArrayView <Pixel> output)
        {
            if (index.X == 0 & index.Y == 0)
            {
                output[0].AbsDiff = 0;
            }

            if (index.InBounds(xImage.Extent))
            {
                var xOld      = xImage[index];
                var gradient  = bMap[index];
                var lipschitz = aMap[index];
                var lambda    = lambdaAlpha[0];
                var alpha     = lambdaAlpha[1];

                var xNew     = GPUProximalOperator(xOld * lipschitz + gradient, lipschitz, lambda, alpha);
                var xAbsDiff = XMath.Abs(xNew - xOld);
                var xIndex   = index.X;
                var yIndex   = index.Y;
                var sign     = XMath.Sign(xNew - xOld);

                var pix = new Pixel()
                {
                    AbsDiff = xAbsDiff,
                    X       = xIndex,
                    Y       = yIndex,
                    Sign    = sign
                };
                Atomic.MakeAtomic(ref output[0], pix, new MaxPixelOperation(), new PixelCompareExchange());
            }
        }
示例#13
0
 static void AbsKernel(Index1 index, ArrayView <float> IO)
 {
     IO[index] = XMath.Abs(IO[index]);
 }
示例#14
0
        private static bool IsOddInteger(float value)
        {
            var remainder = XMath.Abs(value) % 2.0f;

            return(remainder == 1.0f);
        }
示例#15
0
        private static bool IsOddInteger(double value)
        {
            var remainder = XMath.Abs(value) % 2.0;

            return(remainder == 1.0);
        }
示例#16
0
        public static float Pow(float @base, float exp)
        {
            if (exp == 0.0f)
            {
                // Rule #2
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (@base == 1.0f)
            {
                // Rule #14 but ignoring second part about y = NaN
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (XMath.IsNaN(@base) || XMath.IsNaN(exp))
            {
                // Rule #1
                return(float.NaN);
            }
            else if (@base == float.NegativeInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #3
                    return(0.0f);
                }
                else if (IsOddInteger(exp))
                {
                    // Rule #4
                    return(float.NegativeInfinity);
                }
                else
                {
                    // Rule #5
                    return(float.PositiveInfinity);
                }
            }
            else if (@base < 0.0f && !IsInteger(exp) && !XMath.IsInfinity(exp))
            {
                // Rule #6
                return(float.NaN);
            }
            else if (@base == -1.0f && XMath.IsInfinity(exp))
            {
                // Rule #7
                return(1.0f);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.NegativeInfinity)
            {
                // Rule #8
                return(float.PositiveInfinity);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.PositiveInfinity)
            {
                // Rule #9
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.NegativeInfinity)
            {
                // Rule #10
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.PositiveInfinity)
            {
                // Rule #11
                return(float.PositiveInfinity);
            }
            else if (@base == 0.0f)
            {
                if (exp < 0.0f)
                {
                    // Rule #12
                    return(float.PositiveInfinity);
                }
                else
                {
                    // Rule #13
                    // NB: exp == 0.0 already handled by Rule #2
                    return(0.0f);
                }
            }
            else if (@base == float.PositiveInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #15
                    return(0.0f);
                }
                else
                {
                    // Rule #16
                    // NB: exp == 0.0 already handled by Rule #2
                    return(float.PositiveInfinity);
                }
            }

            if (@base < 0.0)
            {
                // 'exp' is an integer, due to Rule #6.
                var sign = IsOddInteger(exp) ? -1.0f : 1.0f;
                return(sign * Exp(exp * Log(XMath.Abs(@base))));
            }

            return(Exp(exp * Log(@base)));
        }
示例#17
0
        public HitRecord hit(Ray ray, float tmin, float tmax)
        {
            if (aabb.hit(ray, tmin, tmax))
            {
                float t = tmin;

                Vec3 pos = ray.a;

                Vec3i iPos = new Vec3i(XMath.Floor(pos.x), XMath.Floor(pos.y), XMath.Floor(pos.z));

                Vec3 step = new Vec3(ray.b.x > 0 ? 1f : -1f, ray.b.y > 0 ? 1f : -1f, ray.b.z > 0 ? 1f : -1f);

                Vec3 tDelta = new Vec3(
                    XMath.Abs(1f / ray.b.x),
                    XMath.Abs(1f / ray.b.y),
                    XMath.Abs(1f / ray.b.z));

                Vec3 dist = new Vec3(
                    step.x > 0 ? (iPos.x + 1 - pos.x) : (pos.x - iPos.x),
                    step.y > 0 ? (iPos.y + 1 - pos.y) : (pos.y - iPos.y),
                    step.z > 0 ? (iPos.z + 1 - pos.z) : (pos.z - iPos.z));

                Vec3 tMax = new Vec3(
                    float.IsInfinity(tDelta.x) ? float.MaxValue : tDelta.x * dist.x,
                    float.IsInfinity(tDelta.y) ? float.MaxValue : tDelta.y * dist.y,
                    float.IsInfinity(tDelta.z) ? float.MaxValue : tDelta.z * dist.z);

                int stepIndex = -1;
                int i         = -1;

                while (i < maxViewDist)
                {
                    int tile = tileAt(pos);
                    i++;

                    if (tile > 0)
                    {
                        Vec3 hitPos  = ray.pointAtParameter(t);
                        Vec3 hitNorm = new Vec3();

                        if (stepIndex == 0)
                        {
                            hitNorm = new Vec3(-step.x, 0, 0);
                        }
                        else if (stepIndex == 1)
                        {
                            hitNorm = new Vec3(0, -step.y, 0);
                        }
                        else if (stepIndex == 2)
                        {
                            hitNorm = new Vec3(0, 0, -step.z);
                        }

                        return(new HitRecord(t, hitPos, hitNorm, false, tileMaterialIDs[tile], tile));
                    }

                    if (tMax.x < tMax.y)
                    {
                        if (tMax.x < tMax.z)
                        {
                            pos.x    += step.x;
                            t         = tMax.x;
                            tMax.x   += tDelta.x;
                            stepIndex = 0;
                        }
                        else
                        {
                            pos.z    += step.z;
                            t         = tMax.z;
                            tMax.z   += tDelta.z;
                            stepIndex = 2;
                        }
                    }
                    else
                    {
                        if (tMax.y < tMax.z)
                        {
                            pos.y    += step.y;
                            t         = tMax.y;
                            tMax.y   += tDelta.y;
                            stepIndex = 1;
                        }
                        else
                        {
                            pos.z    += step.z;
                            t         = tMax.z;
                            tMax.z   += tDelta.z;
                            stepIndex = 2;
                        }
                    }
                }

                return(new HitRecord(float.MaxValue, new Vec3(), new Vec3(), false, -1, -1));
            }
            else
            {
                return(new HitRecord(float.MaxValue, new Vec3(), new Vec3(), false, -1, -1));
            }
        }
示例#18
0
        public static float Pow(float @base, float exp)
        {
            // TODO: The second condition of !XMath.IsNaN(), for the first two if/else conditions,
            // can be removed after fixing https://github.com/m4rs-mt/ILGPU/issues/93
            if (exp == 0.0f && !XMath.IsNaN(exp))
            {
                // Rule #2
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (@base == 1.0f && !XMath.IsNaN(@base))
            {
                // Rule #14 but ignoring second part about y = NaN
                // Ignoring Rule #1
                return(1.0f);
            }
            else if (XMath.IsNaN(@base) || XMath.IsNaN(exp))
            {
                // Rule #1
                return(float.NaN);
            }
            else if (@base == float.NegativeInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #3
                    return(0.0f);
                }
                else if (IsOddInteger(exp))
                {
                    // Rule #4
                    return(float.NegativeInfinity);
                }
                else
                {
                    // Rule #5
                    return(float.PositiveInfinity);
                }
            }
            else if (@base < 0.0f && !IsInteger(exp) && !XMath.IsInfinity(exp))
            {
                // Rule #6
                return(float.NaN);
            }
            else if (@base == -1.0f && XMath.IsInfinity(exp))
            {
                // Rule #7
                return(1.0f);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.NegativeInfinity)
            {
                // Rule #8
                return(float.PositiveInfinity);
            }
            else if (-1.0f < @base && @base < 1.0f && exp == float.PositiveInfinity)
            {
                // Rule #9
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.NegativeInfinity)
            {
                // Rule #10
                return(0.0f);
            }
            else if ((@base < -1.0f || @base > 1.0f) && exp == float.PositiveInfinity)
            {
                // Rule #11
                return(float.PositiveInfinity);
            }
            else if (@base == 0.0f)
            {
                if (exp < 0.0f)
                {
                    // Rule #12
                    return(float.PositiveInfinity);
                }
                else
                {
                    // Rule #13
                    // NB: exp == 0.0 already handled by Rule #2
                    return(0.0f);
                }
            }
            else if (@base == float.PositiveInfinity)
            {
                if (exp < 0.0f)
                {
                    // Rule #14
                    return(0.0f);
                }
                else
                {
                    // Rule #15
                    // NB: exp == 0.0 already handled by Rule #2
                    return(float.PositiveInfinity);
                }
            }

            if (@base < 0.0)
            {
                // 'exp' is an integer, due to Rule #6.
                var sign = IsOddInteger(exp) ? -1.0f : 1.0f;
                return(sign * Exp(exp * Log(XMath.Abs(@base))));
            }

            return(Exp(exp * Log(@base)));
        }