Ejemplo n.º 1
0
        public void TestPathTracer()
        {
            PCG pcg = new PCG();

            for (int i = 0; i < 5; i++)
            {
                World world = new World();

                float    emittedRadiance   = pcg.randomFloat();
                float    reflectance       = pcg.randomFloat();
                Material enclosureMaterial = new Material(
                    Brdf: new DiffuseBRDF(pig: new UniformPigment(new Color(1f, 1f, 1f) * reflectance)),
                    EmittedRadiance: new UniformPigment(new Color(1f, 1f, 1f) * emittedRadiance)
                    );

                world.addShape(new Sphere(material: enclosureMaterial));

                PathTracer pathTracer = new PathTracer(
                    world: world,
                    pcg: pcg,
                    numOfRays: 1,
                    maxDepth: 100,
                    russianRouletteLimit: 101
                    );

                Ray   ray      = new Ray(origin: new Point(0f, 0f, 0f), dir: new Vec(1f, 0f, 0f));
                Color color    = pathTracer.computeRadiance(ray);
                float expected = emittedRadiance / (1.0f - reflectance);
                Assert.True(Utility.areClose(expected, color.r, 1e-3f), $"TestPathTracer failed - Assert i={i}, 1/3");
                Assert.True(Utility.areClose(expected, color.g, 1e-3f), $"TestPathTracer failed - Assert i={i}, 2/3");
                Assert.True(Utility.areClose(expected, color.b, 1e-3f), $"TestPathTracer failed - Assert i={i}, 3/3");
            }
        }
Ejemplo n.º 2
0
        public void TestSpecularBRDF()
        {
            Material material = new Material(Brdf: new SpecularBRDF());
            Plane    plane    = new Plane(material: material);
            PCG      pgc      = new PCG();

            Vec    direction1 = new Vec(1f, 0f, -1f).Normalize();
            Vec    direction2 = new Vec(1f, 1f, -1f).Normalize();
            Normal normal     = new Normal(0f, 0f, 1f);

            Ray scatteredRay1 = plane.material.brdf.scatterRay(pgc, direction1, Constant.Origin, normal, 1);
            Ray scatteredRay2 = plane.material.brdf.scatterRay(pgc, direction2, Constant.Origin, normal, 1);

            Ray expectedRay1 = new Ray(
                origin: Constant.Origin,
                dir: new Vec(1f, 0f, 1f).Normalize(),
                tm: 1e-5f,
                tM: Single.PositiveInfinity,
                dep: 1
                );

            Ray expectedRay2 = new Ray(
                origin: Constant.Origin,
                dir: new Vec(1f, 1f, 1f).Normalize(),
                tm: 1e-5f,
                tM: Single.PositiveInfinity,
                dep: 1
                );

            Assert.True(scatteredRay1.isClose(expectedRay1), "TestSpecularBRDF failed - Assert 1/2");
            Assert.True(scatteredRay2.isClose(expectedRay2), "TestSpecularBRDF failed - Assert 2/2");
        }
Ejemplo n.º 3
0
        ilPSP.LinSolvers.monkey.Solver GetSolver(IMutableMatrixEx Mtx)
        {
            ilPSP.LinSolvers.monkey.Solver solver;


            switch (WhichSolver)
            {
            case _whichSolver.CG: {
                var _solver = new CG(); solver = _solver;
                _solver.DevType       = ilPSP.LinSolvers.monkey.DeviceType.Cuda;
                _solver.MaxIterations = Switcher <int>(_solver.MaxIterations, LinConfig.MaxSolverIterations);
                _solver.Tolerance     = Switcher <double>(_solver.Tolerance, LinConfig.ConvergenceCriterion);
                break;
            }

            case _whichSolver.PCG: {
                var _solver = new PCG(); solver = _solver;
                _solver.DevType       = ilPSP.LinSolvers.monkey.DeviceType.Cuda;
                _solver.MaxIterations = Switcher <int>(_solver.MaxIterations, LinConfig.MaxSolverIterations);
                _solver.Tolerance     = Switcher <double>(_solver.Tolerance, LinConfig.ConvergenceCriterion);
                break;
            }

            default:
                throw new NotImplementedException();
            }

            solver.DefineMatrix(Mtx);

            return(solver);
        }
Ejemplo n.º 4
0
        public void TestRandom()
        {
            PCG pcg = new PCG();

            Assert.True(pcg.state == 1753877967969059832UL, "TestRandom failed - 1/8");
            Assert.True(pcg.inc == 109UL, "TestRandom failed - 2/8");

            uint[] expected =
            {
                2707161783U,
                2068313097U,
                3122475824U,
                2211639955U,
                3215226955U,
                3421331566U
            };
            int count = 3;

            foreach (uint rand in expected)
            {
                uint result = pcg.random();
                Assert.True(rand == result, $"TestRandom failed - Assert {count}/8");
                count++;
            }
        }
Ejemplo n.º 5
0
        ISparseSolver GetSolver(IMutableMatrixEx Mtx)
        {
            ISparseSolver solver;


            switch (WhichSolver)
            {
            case _whichSolver.PARDISO:
                if (LinConfig != null)
                {
                    SingletonPARDISO.SetParallelism(LinConfig.Parallelism);
                }
                solver = new PARDISOSolver();
                ((PARDISOSolver)solver).CacheFactorization = true;
                ((PARDISOSolver)solver).UseDoublePrecision = true;
                break;

            case _whichSolver.MUMPS:
                if (LinConfig != null)
                {
                    SingletonMumps.SetParallelism(LinConfig.Parallelism);
                }
                solver = new MUMPSSolver();
                break;

            case _whichSolver.Matlab:
                solver = new MatlabSolverWrapper();
                break;

            case _whichSolver.Lapack:
                solver = new DenseSolverWrapper();
                break;

            case _whichSolver.CG:
                solver = new CG();
                ((CG)solver).DevType       = ilPSP.LinSolvers.monkey.DeviceType.Cuda;
                ((CG)solver).MaxIterations = Switcher <int>(((CG)solver).MaxIterations, LinConfig.MaxSolverIterations);
                ((CG)solver).Tolerance     = Switcher <double>(((CG)solver).Tolerance, LinConfig.ConvergenceCriterion);
                break;

            case _whichSolver.PCG:
                solver = new PCG();
                break;

            default:
                throw new NotImplementedException();
            }

            solver.DefineMatrix(Mtx);

            return(solver);
        }
Ejemplo n.º 6
0
    private Color RandomColor()
    {
        if (!randInitialized)
        {
            rand            = new PCG(0, (ulong)ResourceManager.Instance.seed);
            randInitialized = true;
        }

        return(Color.HSVToRGB(
                   (float)rand.SFloatInclusive(sfloat.Zero, sfloat.One),
                   (float)rand.SFloatInclusive(sfloat.Zero, sfloat.One),
                   (float)rand.SFloatInclusive((sfloat)0.5f, sfloat.One)
                   ));
    }
Ejemplo n.º 7
0
        public void TestDiffusiveBRDF()
        {
            Material m = new Material(Brdf: new DiffuseBRDF());
            Sphere   s = new Sphere(material: m);
            PCG      r = new PCG();

            // this part of the code is used to check which are the first, few, casual directions
            // so that a numeric test can be implemented.

            // using (StreamWriter sw = File.AppendText("./uniformSphere.txt"))
            // {
            //     for (int i = 0; i < 1000; i++)
            //     {
            //         Ray ray = s.material.brdf.scatterRay(r,
            //                                              new Vec(1f, 0f, 0f),
            //                                              new Point(0f, 0f, 0f),
            //                                              new Normal(0f, 0f, 1f),
            //                                              1);
            //         HitRecord? hit = s.rayIntersection(ray);
            //         sw.WriteLine(hit?.worldPoint.x + " " + hit?.worldPoint.y);
            //     }
            // }

            List <Ray> expectedRays = new List <Ray>();

            expectedRays.Add(new Ray(Constant.Origin,
                                     new Vec(-0.6039477f, 0.07026359f, 0.7939208f)));
            expectedRays.Add(new Ray(Constant.Origin,
                                     new Vec(-0.5201868f, -0.04896636f, 0.85264766f)));
            expectedRays.Add(new Ray(Constant.Origin,
                                     new Vec(0.14469035f, -0.48006395f, 0.86521876f)));

            for (int i = 0; i < 3; i++)
            {
                Ray ray = s.material.brdf.scatterRay(r,
                                                     new Vec(1f, 0f, 0f),
                                                     new Point(0f, 0f, 0f),
                                                     new Normal(0f, 0f, 1f),
                                                     1);
                Assert.True(ray.isClose(expectedRays[i]), $"Attention: ray number {i + 1} has a problem - Test Failed");
            }
        }
Ejemplo n.º 8
0
        public void ONBRandomTestingVec()
        {
            PCG pcg = new PCG();

            for (int i = 0; i < 1E5; i++) {
                Vec n = new Vec(pcg.randomFloat(), pcg.randomFloat(), pcg.randomFloat()).Normalize();
                
                List<Vec> onb = n.createONBfromZ();

                Assert.True(onb[2].isClose(n), "ONBRandomTesting Failed! Assert 1");
        
                Assert.True(Utility.areClose(onb[0] * onb[1], 0f), $"{onb[0] * onb[1]} is not close to {0f}!!");
                Assert.True(Utility.areClose(onb[1] * onb[2], 0f), $"{onb[1] * onb[2]} is not close to {0f}!!");
                Assert.True(Utility.areClose(onb[0] * onb[2], 0f), $"{onb[0] * onb[2]} is not close to {0f}!!");

                Assert.True(Utility.areClose(onb[0].getSquaredNorm(),1f), "ONBRandomTesting Failed! Assert 5");
                Assert.True(Utility.areClose(onb[1].getSquaredNorm(),1f), "ONBRandomTesting Failed! Assert 6");
                Assert.True(Utility.areClose(onb[2].getSquaredNorm(),1f), "ONBRandomTesting Failed! Assert 7");
            }
        }
Ejemplo n.º 9
0
        private ISolver CreateSolver(CCS a)
        {
            ISolver buf;

            switch (parent.LastResult.SolverType)
            {
            case BuiltInSolverType.CholeskyDecomposition:
                buf = new CholeskySolver(a);
                break;

            case BuiltInSolverType.ConjugateGradient:
                buf   = new PCG(new SSOR());
                buf.A = a;
                break;

            default:
                throw new NotImplementedException();
            }

            return(buf);
        }
Ejemplo n.º 10
0
 // Use this for initialization
 void Start()
 {
     number = GetComponent <PCG>();
 }
Ejemplo n.º 11
0
 // Use this for initialization
 void Start()
 {
     pcg          = GameObject.FindGameObjectWithTag("LevelGenerator").GetComponent <PCG> ();
     roomTemplate = GameObject.FindGameObjectWithTag("RoomTemplate").GetComponent <RoomTemplate>();
     Invoke("GenerateRoom", 0.1f);
 }
Ejemplo n.º 12
0
        public static void ExecuteDemo(int width, int height, int angle, bool orthogonal, string pfmFile,
                                       string ldrFile, int scene, float?luminosity, int spp, char rendType)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            Console.WriteLine("Starting Demo with these parameters:\n");

            Console.WriteLine("Width: " + width + " pixels");
            Console.WriteLine("Height: " + height + " pixels");
            Console.WriteLine("Angle: " + angle + " degrees");
            Console.WriteLine(orthogonal ? "Orthogonal Camera" : "Perspective Camera");
            Console.WriteLine("pfmFile: " + pfmFile);
            Console.WriteLine("ldrFile: " + ldrFile);
            Console.WriteLine("Samples per pixel: " + spp);
            Console.WriteLine("Render type: " + dictRend[rendType]);

            Console.WriteLine("\n");

            HdrImage image = new HdrImage(width, height);

            // Camera initialization
            Console.WriteLine("Creating the camera...");
            // var cameraTransf = Transformation.RotationZ(Utility.DegToRad(angle)) * Transformation.Translation(-2.0f, 0.0f, 0.5f) * Tsf.RotationY(Utility.DegToRad(15));
            var    cameraTransf = Transformation.Translation(-2.0f, 0.0f, 0.0f);
            Camera camera;

            if (orthogonal)
            {
                camera = new OrthogonalCamera(aspectRatio: (float)width / height, transformation: cameraTransf);
            }
            else
            {
                camera = new PerspectiveCamera(aspectRatio: (float)width / height, transformation: cameraTransf);
            }

            // Default value on/off renderer
            Render?renderer = null;

            Console.WriteLine("Creating the scene...");
            World        world    = new World();
            List <float> Vertices = new List <float>()
            {
                -0.5f, 0.5f
            };

            switch (scene)
            {
            case 1:
                //One sphere for each vertex of the cube
                foreach (var x in Vertices)
                {
                    foreach (var y in Vertices)
                    {
                        foreach (var z in Vertices)
                        {
                            world.addShape(new Sphere(Tsf.Translation(new Vec(x, y, z))
                                                      * Tsf.Scaling(new Vec(0.1f, 0.1f, 0.1f))));
                        } // z
                    }     // y
                }         // x

                //Adding two more spheres to break simmetry
                world.addShape(new Sphere(Tsf.Translation(new Vec(0f, 0f, -0.5f))
                                          * Tsf.Scaling(0.1f)));
                world.addShape(new Sphere(Tsf.Translation(new Vec(0f, 0.5f, 0f))
                                          * Tsf.Scaling(0.1f)));
                break;

            case 2:
                HdrImage img      = new HdrImage();
                string   inputpfm = "Texture/CokeTexture.pfm";
                using (FileStream inputStream = File.OpenRead(inputpfm))
                {
                    img.readPfm(inputStream);
                    Console.WriteLine($"Texture {inputpfm} has been correctly read from disk.");
                }
                Material groundM = new Material(new DiffuseBRDF(new CheckeredPigment(CC.BrightGreen, CC.Orange, 4)), new UniformPigment(CC.Black));

                world.addShape(CC.SKY);
                world.addShape(new Plane(Tsf.Translation(0f, 0f, -3f), groundM));
                world.addShape(
                    new Cylinder(
                        transformation: Tsf.Translation(.5f, -1f, -1f) * Transformation.Scaling(.6f, 0.6f, 1.3f) * Tsf.RotationY(Utility.DegToRad(45)),
                        material: new Material(
                            Brdf: new DiffuseBRDF(new ImagePigment(img))
                            //EmittedRadiance: new UniformPigment(CC.Red)// new ImagePigment(img)
                            )
                        )
                    );
                world.addShape(
                    new Cylinder(
                        transformation: Tsf.Translation(.5f, 1f, -1f) * Transformation.Scaling(.6f, 0.6f, 1.3f) * Tsf.RotationY(Utility.DegToRad(-45)),
                        material: new Material(
                            Brdf: new DiffuseBRDF(new ImagePigment(img))
                            //EmittedRadiance: new UniformPigment(CC.Red)// new ImagePigment(img)
                            )
                        )
                    );
                break;

            case 3:
                PCG      pcg     = new PCG();
                Material sph1Mat = new Material(new DiffuseBRDF(new UniformPigment(CC.BlueChill)));
                Material sph2Mat = new Material(new DiffuseBRDF(new UniformPigment(Color.random())));
                Material boxMat  = new Material(new DiffuseBRDF(new UniformPigment(CC.BrightGreen)));


                world.addShape(new Sphere(Tsf.Scaling(500f), CC.skyMat));
                world.addShape(new Plane(Tsf.Translation(0f, 0f, -1f), CC.groundMat));
                world.addShape(new CSGUnion(new Sphere(Transformation.Translation(0.5f, -2.6f, 1f) * Transformation.Scaling(0.6f), sph2Mat),
                                            new Box(new Point(0f, -2.25f, 0.9f), new Point(1f, -3.25f, 1.8f), null, boxMat)));
                world.addShape(new Sphere(Tsf.Translation(3f, 5f, 1.6f) * Tsf.Scaling(2.0f, 4.0f, 2.0f), CC.refMat));
                world.addShape(new Sphere(Tsf.Translation(4f, -1f, 1.3f) * Tsf.Scaling(1.0f), sph1Mat));
                world.addShape(new Sphere(Tsf.Translation(-4f, -0.5f, 1f) * Tsf.Scaling(2f), sph2Mat));

                break;

            case 4:
                Material mat = new Material(null, new UniformPigment(new Color(10f, 10f, 10f)));
                world.addShape(CC.SKY);
                world.addShape(new Plane(Tsf.Scaling(-3f, 0f, 0f) * Tsf.RotationY(Utility.DegToRad(270)), mat));

                world.addShape(CC.wikiShape(Tsf.RotationZ(Utility.DegToRad(23))));
                // world.addShape(CC.wikiShape(Tsf.RotationZ(Utility.DegToRad(45))));

                break;

            case 5:
                Material skyM      = new Material(new DiffuseBRDF(new UniformPigment(CC.SkyBlue)), new UniformPigment(CC.SkyBlue));
                Material checkered = new Material(new DiffuseBRDF(new CheckeredPigment(CC.Blue, CC.Yellow)), new UniformPigment(CC.Black));
                Material ground    = new Material(new DiffuseBRDF(new CheckeredPigment(CC.LightRed, CC.Orange)), new UniformPigment(CC.Black));


                world.addShape(new Sphere(Tsf.Scaling(500f), skyM));
                world.addShape(new Cylinder(Tsf.Translation(0f, 2f, -0.5f) * Tsf.Scaling(0.5f), checkered));
                world.addShape(new Cone(r: 0.5f, material: checkered));
                world.addShape(new Plane(Tsf.Translation(0f, 0f, -1f), ground));

                break;

            default:
                break;
            }

            switch (rendType)
            {
            case 'o':
                renderer = new OnOffRender(world);
                break;

            case 'f':
                renderer = new FlatRender(world);
                break;

            case 'p':
                renderer = new PointLightRender(world);
                break;

            case 'r':
                renderer = new PathTracer(world, CC.Black, new PCG());
                break;

            default:
                break;
            }

            // Ray tracing
            Console.WriteLine("Rendering the scene...");
            var rayTracer = new ImageTracer(image, camera, (int)Math.Sqrt(spp));

            if (renderer == null)
            {
                renderer = new OnOffRender(world);
            }

            rayTracer.fireAllRays(renderer);

            // Write PFM image
            Console.WriteLine("Saving in pfm format...");
            using (FileStream outpfmstream = File.OpenWrite(pfmFile))
            {
                image.savePfm(outpfmstream);
                Console.WriteLine($"Image saved in {pfmFile}");
            }

            Convert.ExecuteConvert(pfmFile, ldrFile, Default.factor, Default.gamma, luminosity);

            sw.Stop();
            TimeSpan ts          = sw.Elapsed;
            string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                 ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

            Console.WriteLine("RunTime " + elapsedTime);
        }//Demo