Ejemplo n.º 1
0
        private double GetFitness(Dictionary <string, float> values)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            SetDecimation(polycountTarget, values);
            var timeDecim = sw.Elapsed.TotalMilliseconds;

            Texture2D textureDecimated = CalibrationUtils.CaptureScreenshot(_camera, 1000, 1000);
            Digest    modified         = ImagePhash.ComputeDigest(CalibrationUtils.ToLuminanceImage(textureDecimated));
            var       timePhash        = sw.Elapsed.TotalMilliseconds;

            var fitness       = 1f - ImagePhash.GetCrossCorrelation(_originalDigest, modified);
            var timeCorrelate = sw.Elapsed.TotalMilliseconds;

            sw.Stop();

            Debug.Log($"| Decim:{timeDecim}, Phash:{timePhash}, Correlate:{timeCorrelate}");

            return(fitness);
        }
Ejemplo n.º 2
0
        private void Start()
        {
            _originalMeshes = new List <Mesh>();
            _meshes         = new List <Mesh>();

            Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();

            HashSet <Mesh> uniqueMeshes = new HashSet <Mesh>();

            foreach (Renderer renderer in renderers)
            {
                if (renderer is MeshRenderer meshRenderer)
                {
                    MeshFilter meshFilter = renderer.gameObject.GetComponent <MeshFilter>();
                    if (!meshFilter)
                    {
                        continue;
                    }

                    Mesh mesh = meshFilter.sharedMesh;

                    if (!uniqueMeshes.Add(mesh))
                    {
                        continue;
                    }

                    _originalMeshes.Add(mesh);

                    // Clone mesh
                    mesh = meshFilter.sharedMesh = UnityConverter.ToSharedMesh(mesh).ToUnityMesh();
                    _meshes.Add(mesh);
                }
                else if (renderer is SkinnedMeshRenderer skinnedMeshRenderer)
                {
                    Mesh mesh = skinnedMeshRenderer.sharedMesh;

                    if (!uniqueMeshes.Add(mesh))
                    {
                        continue;
                    }

                    _originalMeshes.Add(mesh);

                    // Clone mesh
                    mesh = skinnedMeshRenderer.sharedMesh = mesh.ToSharedMesh().ToUnityMesh();
                    _meshes.Add(mesh);
                }
            }

            Camera camera = Camera.main;

            Texture2D textureOriginal = CalibrationUtils.CaptureScreenshot(camera, 1000, 1000);

            File.WriteAllBytes(@"C:\Users\oginiaux\Downloads\trace\original.jpg", textureOriginal.EncodeToJPG());
            Digest originalDigest = ImagePhash.ComputeDigest(CalibrationUtils.ToLuminanceImage(textureOriginal));

            List <Dictionary <string, float> > results = new List <Dictionary <string, float> >();

            float highestCorrelation = float.MinValue;

            for (int i = 0; i < iterations; i++)
            {
                Dictionary <string, float> values = new Dictionary <string, float>();

                values["NormalWeight"]          = Random.Range(0f, 100f);
                values["MergeThreshold"]        = Random.Range(0.00001f, 0.1f);
                values["MergeNormalsThreshold"] = MathF.Cos(Random.Range(5f, 140f) * MathF.PI / 180f);
                values["UseEdgeLength"]         = Random.Range(0f, 1f);
                values["UpdateFarNeighbors"]    = Random.Range(0f, 0.75f);
                values["UpdateMinsOnCollapse"]  = Random.Range(0.25f, 1f);
                values["EdgeBorderPenalty"]     = Random.Range(0f, 1000f);
                values["Case"] = i;

                System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();

                SetDecimation(0.5f, values);

                sw.Stop();
                values["Time"] = (float)sw.Elapsed.TotalMilliseconds;

                Texture2D textureDecimated = CalibrationUtils.CaptureScreenshot(camera, 1000, 1000);
                Digest    modified         = ImagePhash.ComputeDigest(CalibrationUtils.ToLuminanceImage(textureDecimated));

                values["Correlation"] = ImagePhash.GetCrossCorrelation(originalDigest, modified);

                if (highestCorrelation < values["Correlation"])
                {
                    highestCorrelation = values["Correlation"];
                    File.WriteAllBytes($@"C:\Users\oginiaux\Downloads\trace\case_{i}.jpg", textureDecimated.EncodeToJPG());
                }

                results.Add(values);
            }

            foreach (Dictionary <string, float> result in results.OrderByDescending(x => x["Correlation"]).Take(3))
            {
                StringBuilder strbldr = new StringBuilder();
                foreach (KeyValuePair <string, float> pair in result.OrderBy(x => x.Key))
                {
                    strbldr.Append($"{pair.Key} = {pair.Value}\n");
                }
                Debug.Log(strbldr);
            }
        }
Ejemplo n.º 3
0
        private void Start()
        {
            _originalMeshes = new List <Mesh>();
            _meshes         = new List <Mesh>();

            Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();

            HashSet <Mesh> uniqueMeshes = new HashSet <Mesh>();

            foreach (Renderer renderer in renderers)
            {
                if (renderer is MeshRenderer meshRenderer)
                {
                    MeshFilter meshFilter = renderer.gameObject.GetComponent <MeshFilter>();
                    if (!meshFilter)
                    {
                        continue;
                    }

                    Mesh mesh = meshFilter.sharedMesh;

                    if (!uniqueMeshes.Add(mesh))
                    {
                        continue;
                    }

                    _originalMeshes.Add(mesh);

                    // Clone mesh
                    mesh = meshFilter.sharedMesh = UnityConverter.ToSharedMesh(mesh).ToUnityMesh();
                    _meshes.Add(mesh);
                }
                else if (renderer is SkinnedMeshRenderer skinnedMeshRenderer)
                {
                    Mesh mesh = skinnedMeshRenderer.sharedMesh;

                    if (!uniqueMeshes.Add(mesh))
                    {
                        continue;
                    }

                    _originalMeshes.Add(mesh);

                    // Clone mesh
                    mesh = skinnedMeshRenderer.sharedMesh = mesh.ToSharedMesh().ToUnityMesh();
                    _meshes.Add(mesh);
                }
            }

            _camera = Camera.main;

            Texture2D textureOriginal = CalibrationUtils.CaptureScreenshot(_camera, 1000, 1000);

            _originalDigest = ImagePhash.ComputeDigest(CalibrationUtils.ToLuminanceImage(textureOriginal));

            switch (metaHeuristicAlgo)
            {
            case MetaHeuristicAlgo.GeneticAlgorithm:
                _metaheuristicAlgorithm = new GeneticAlgorithm <Dictionary <string, float> >(Crossover);
                break;

            case MetaHeuristicAlgo.HillClimbing:
                _metaheuristicAlgorithm = new HillClimb <Dictionary <string, float> >();
                break;
            }

            var config = new Configuration <Dictionary <string, float> >();

            config.initializeSolutionFunction = GetInitialState;
            config.cloneFunction        = Clone;
            config.mutationFunction     = Mutate;
            config.movement             = Search.Direction.Optimization;
            config.selectionFunction    = Selection.RankBased;
            config.objectiveFunction    = GetFitness;
            config.populationSize       = populationSize;
            config.noOfIterations       = iterations;
            config.writeToConsole       = false;
            config.enforceHardObjective = false;
            _metaheuristicAlgorithm.Create(config);

            StartCoroutine(Run());
        }