Beispiel #1
0
            private void updateAcceleration(ParticleSystem d, int ID)
            {
                acceleration = new Vec3();

                for (int i = 0; i < d.particles.Length; i++)
                {
                    Vec3  otherPos;
                    float mass;

                    if (i == ID)
                    {
                        //creates a mass at the center of the screen
                        otherPos = d.centerPos;
                        mass     = d.centerMass;
                    }
                    else
                    {
                        otherPos = d.particles[i].position;
                        mass     = 1f;
                    }

                    float deltaPosLength = (position - otherPos).length();
                    float temp           = (d.gc * mass) / XMath.Pow(deltaPosLength, 3f);
                    acceleration += (otherPos - position) * temp;
                }
            }
Beispiel #2
0
        private static float schlick(float cosine, float ref_idx)
        {
            float r0 = (1.0f - ref_idx) / (1.0f + ref_idx);

            r0 = r0 * r0;
            return(r0 + (1.0f - r0) * XMath.Pow((1.0f - cosine), 5.0f));
        }
Beispiel #3
0
            private void updateAcceleration(int ID)
            {
                accelerations[ID] = new Vec3();

                for (int i = 0; i < positions.Length; i++)
                {
                    Vec3  otherPos;
                    float mass;

                    if (i == ID)
                    {
                        //creates a mass at the center of the screen
                        otherPos = centerPos;
                        mass     = centerMass;
                    }
                    else
                    {
                        otherPos = positions[i];
                        mass     = 1f;
                    }

                    float deltaPosLength = (positions[ID] - otherPos).length();
                    float temp           = (gc * mass) / XMath.Pow(deltaPosLength, 3f);
                    accelerations[ID] += (otherPos - positions[ID]) * temp;
                }
            }
        public static short Magnitude(ArrayView3D <short> a, Index2 index)
        {
            var extent = a.Depth;
            int sum    = 0;

            for (int i = 0; i < extent; i++)
            {
                sum = (int)XMath.Pow(a[index.X, index.Y, i], 2);
            }
            return((short)XMath.Sqrt(sum));
        }
        public static short Magnitude(ArrayView <short> a)
        {
            var extent = a.Extent.X;
            int sum    = 0;

            for (int i = 0; i < extent; i++)
            {
                sum = (int)XMath.Pow(a[i], 2);
            }
            return((short)XMath.Sqrt(sum));
        }
Beispiel #6
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);
        }
Beispiel #7
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)));
        }
Beispiel #8
0
        static void ScalarConsecutiveOperationKernel(Index1 index, ArrayView <float> OutPut, ArrayView <float> Input, float Scalar, SpecializedValue <int> operation)
        {
            switch ((Operations)operation.Value)
            {
            case Operations.multiplication:
                OutPut[index] = Input[index] * Scalar;
                break;

            case Operations.addition:
                OutPut[index] = Input[index] + Scalar;
                break;

            case Operations.subtraction:
                OutPut[index] = Input[index] - Scalar;
                break;

            case Operations.flipSubtraction:
                OutPut[index] = Scalar - Input[index];
                break;

            case Operations.division:
                OutPut[index] = Input[index] / Scalar;
                break;

            case Operations.inverseDivision:
                OutPut[index] = Scalar / Input[index];
                break;

            case Operations.power:
                OutPut[index] = XMath.Pow(Input[index], Scalar);
                break;

            case Operations.powerFlipped:
                OutPut[index] = XMath.Pow(Scalar, Input[index]);
                break;

            case Operations.squareOfDiffs:
                OutPut[index] = XMath.Pow((Input[index] - Scalar), 2f);
                break;
            }
        }
Beispiel #9
0
        public static void StandardDeviation(Index index, ArrayView <double> result, ArrayView3D <short> input)
        {
            long sum = 0;

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

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

            double mean     = sum / band.Length;
            double dividend = 0;

            for (int i = 0; i < band.Length; i++)
            {
                dividend += XMath.Pow(band[i] - mean, 2);
            }

            result[index] = XMath.Sqrt(dividend / band.Length);
        }
Beispiel #10
0
        static double Evaluate(int individualIndex, int independentsRowIndex, ArrayView2D <double> independents, ArrayView <NodeGPU> nodes, ArrayView <int> nodeArrayStarts)
        {
            for (int nodeIndex = 0; nodeIndex < nodeArrayStarts.Length; nodeIndex++)
            {
                Index1 currentNodeIndex = new Index1(nodeArrayStarts[individualIndex] + nodeIndex);
                //NodeGPU currentNode = nodes[currentNodeIndex];
                if (nodes[currentNodeIndex].IndependentIndex >= 0)
                {
                    int independentIndex = nodes[currentNodeIndex].IndependentIndex;
                    nodes[currentNodeIndex].Number = independents[independentsRowIndex, independentIndex];
                }
                else if (nodes[currentNodeIndex].OperatorIndex >= 0)
                {
                    Index1 branchIndex1 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch1);
                    Index1 branchIndex2 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch2);
                    if (nodes[currentNodeIndex].OperatorIndex < 6)
                    {
                        if (nodes[currentNodeIndex].OperatorIndex < 4)
                        {
                            if (nodes[currentNodeIndex].OperatorIndex == 2)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex1].Number + nodes[branchIndex2].Number;
                            }
                            else if (nodes[currentNodeIndex].OperatorIndex == 3)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex1].Number - nodes[branchIndex2].Number;
                            }
                        }
                        else
                        {
                            if (nodes[currentNodeIndex].OperatorIndex == 4)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex1].Number * nodes[branchIndex2].Number;
                            }
                            else if (nodes[currentNodeIndex].OperatorIndex == 5)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex1].Number / nodes[branchIndex2].Number;
                            }
                        }
                    }
                    else if (nodes[currentNodeIndex].OperatorIndex >= 6 && nodes[currentNodeIndex].OperatorIndex <= 15)
                    {
                        if (nodes[currentNodeIndex].OperatorIndex == 6)
                        {
                            nodes[currentNodeIndex].Number = -nodes[branchIndex1].Number;
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 8)
                        {
                            nodes[currentNodeIndex].Number = XMath.Sin(nodes[branchIndex1].Number);
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 9)
                        {
                            nodes[currentNodeIndex].Number = XMath.Cos(nodes[branchIndex1].Number);
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 14)
                        {
                            nodes[currentNodeIndex].Number = XMath.Pow(nodes[branchIndex1].Number, nodes[branchIndex2].Number);
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 15)
                        {
                            nodes[currentNodeIndex].Number = XMath.Sign(nodes[branchIndex1].Number);
                        }
                    }
                    else
                    {
                        Index1 branchIndex3 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch3);
                        Index1 branchIndex4 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch4);
                        if (nodes[currentNodeIndex].OperatorIndex == 18)
                        {
                            if (nodes[branchIndex1].Number == nodes[branchIndex2].Number)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex3].Number;
                            }
                            else
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex4].Number;
                            }
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 19)
                        {
                            if (nodes[branchIndex1].Number < nodes[branchIndex2].Number)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex3].Number;
                            }
                            else
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex4].Number;
                            }
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 20)
                        {
                            if (nodes[branchIndex1].Number <= nodes[branchIndex2].Number)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex3].Number;
                            }
                            else
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex4].Number;
                            }
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 21)
                        {
                            if (nodes[branchIndex1].Number == 0)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex2].Number;
                            }
                            else
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex3].Number;
                            }
                        }
                        else if (nodes[currentNodeIndex].OperatorIndex == 22)
                        {
                            if (nodes[branchIndex1].Number == 1)
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex2].Number;
                            }
                            else
                            {
                                nodes[currentNodeIndex].Number = nodes[branchIndex3].Number;
                            }
                        }
                    }
                    if (nodes[currentNodeIndex].Number == double.NaN)
                    {
                        return(double.NaN);
                    }
                }

                if (nodes[currentNodeIndex].IsRoot == 1)
                {
                    return(nodes[currentNodeIndex].Number);
                }
            }
            return(double.NaN);
        }
Beispiel #11
0
        private static void ColorRay(int index,
                                     Ray ray,
                                     dFramebuffer framebuffer,
                                     dWorldBuffer world,
                                     XorShift64Star rng, Camera camera)
        {
            Vec3 attenuation = new Vec3(1f, 1f, 1f);
            Vec3 lighting    = new Vec3();

            Ray  working             = ray;
            bool attenuationHasValue = false;

            float minT = 0.1f;

            for (int i = 0; i < camera.maxBounces; i++)
            {
                HitRecord rec = GetWorldHit(working, world, minT);

                if (rec.materialID == -1)
                {
                    if (i == 0 || attenuationHasValue)
                    {
                        framebuffer.DrawableIDBuffer[index] = -2;
                    }

                    float t = 0.5f * (working.b.y + 1.0f);
                    attenuation *= (1.0f - t) * new Vec3(1.0f, 1.0f, 1.0f) + t * new Vec3(0.5f, 0.7f, 1.0f);
                    break;
                }
                else
                {
                    if (i == 0)
                    {
                        framebuffer.ZBuffer[index]          = rec.t;
                        framebuffer.DrawableIDBuffer[index] = rec.drawableID;
                    }

                    ScatterRecord sRec = Scatter(working, rec, rng, world.materials, minT);
                    if (sRec.materialID != -1)
                    {
                        attenuationHasValue = sRec.mirrorSkyLightingFix;
                        attenuation        *= sRec.attenuation;
                        working             = sRec.scatterRay;
                    }
                    else
                    {
                        framebuffer.DrawableIDBuffer[index] = -1;
                        break;
                    }
                }

                for (int j = 0; j < world.lightSphereIDs.Length; j++)
                {
                    Sphere    s         = world.spheres[world.lightSphereIDs[j]];
                    Vec3      lightDir  = s.center - rec.p;
                    HitRecord shadowRec = GetWorldHit(new Ray(rec.p, lightDir), world, minT);

                    if (shadowRec.materialID != -1 && (shadowRec.p - rec.p).length() > lightDir.length() - (s.radius * 1.1f)) // the second part of this IF could probably be much more efficent
                    {
                        MaterialData material = world.materials[shadowRec.materialID];
                        if (material.type != 1)
                        {
                            lightDir  = Vec3.unitVector(lightDir);
                            lighting += material.color * XMath.Max(0.0f, Vec3.dot(lightDir, rec.normal));
                            lighting *= XMath.Pow(XMath.Max(0.0f, Vec3.dot(-Vec3.reflect(rec.normal, -lightDir), ray.b)), material.reflectivity) * material.color;
                        }
                    }
                }
            }

            int rIndex = index * 3;
            int gIndex = rIndex + 1;
            int bIndex = rIndex + 2;

            framebuffer.ColorFrameBuffer[rIndex] = attenuation.x;
            framebuffer.ColorFrameBuffer[gIndex] = attenuation.y;
            framebuffer.ColorFrameBuffer[bIndex] = attenuation.z;

            framebuffer.LightingFrameBuffer[rIndex] = lighting.x;
            framebuffer.LightingFrameBuffer[gIndex] = lighting.y;
            framebuffer.LightingFrameBuffer[bIndex] = lighting.z;
        }
        public static short Magnitude(params short[] a)
        {
            int sum = a.Sum(x => (int)XMath.Pow(x, 2));

            return((short)XMath.Sqrt(sum));
        }
 public static short Magnitude(short x, short y)
 {
     return((short)XMath.Sqrt(XMath.Pow(x, 2) + XMath.Pow(y, 2)));
 }