Exemplo n.º 1
0
        public static void TestShadowFromTo()
        {
            var ft1 = new ShadowFromTo(new Point(0, 0), new Point(1, 1));

            var ary = new Horizon[TerrainPatch.DefaultSize][];

            for (var i = 0; i < TerrainPatch.DefaultSize; i++)
            {
                ary[i] = new Horizon[TerrainPatch.DefaultSize];
            }
            for (var i = 0; i < TerrainPatch.DefaultSize; i++)
            {
                for (var j = 0; j < TerrainPatch.DefaultSize; j++)
                {
                    ary[i][j] = new Horizon().Init();
                }
            }
            ft1.Horizons = ary;

            var r            = new Random();
            var totalSamples = TerrainPatch.DefaultSize * TerrainPatch.DefaultSize * Horizon.HorizonSamples;

            for (var i = 0; i < 1000; i++)
            {
                var len   = (int)(100 * r.NextDouble());
                var start = (int)(totalSamples * r.NextDouble());
                var stop  = start + len;
                for (var ptr = start; ptr < stop; ptr++)
                {
                    var index = ptr % totalSamples;
                    var z     = index % Horizon.HorizonSamples;
                    index /= Horizon.HorizonSamples;
                    var y = index % TerrainPatch.DefaultSize;
                    index /= TerrainPatch.DefaultSize;
                    var x = index;
                    ary[x][y].Buffer[z] = ptr;
                }
            }

            const string filename = "test_ft.bin";

            ft1.Write(filename);
            var ft2 = ShadowFromTo.ReadFrom(filename);

            for (var x = 0; x < TerrainPatch.DefaultSize; x++)
            {
                for (var y = 0; y < TerrainPatch.DefaultSize; y++)
                {
                    for (var z = 0; z < Horizon.HorizonSamples; z++)
                    {
                        if (ft1.Horizons[x][y].Buffer[z] != ft2.Horizons[x][y].Buffer[z])
                        {
                            Console.WriteLine($"Error in cell {x} {y} {z}");
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static ShadowFromTo ReadFrom(string path)
        {
            using (var fs = File.OpenRead(path))
                using (var br = new BinaryReader(fs))
                {
                    var from = new Point(br.ReadInt32(), br.ReadInt32());
                    var to   = new Point(br.ReadInt32(), br.ReadInt32());
                    var p    = new ShadowFromTo(from, to)
                    {
                        Path = path
                    };

                    p.Horizons = new Horizon[TerrainPatch.DefaultSize][];
                    for (var h = 0; h < TerrainPatch.DefaultSize; h++)
                    {
                        p.Horizons[h] = new Horizon[TerrainPatch.DefaultSize];
                        for (var w = 0; w < TerrainPatch.DefaultSize; w++)
                        {
                            var horizon = new Horizon().Init();
                            var buf     = horizon.Buffer;
                            var ptr     = 0;
                            while (ptr < Horizon.HorizonSamples)
                            {
                                ptr += br.ReadInt16();       // read and skip toSkip
                                var toRead = br.ReadInt16(); // read m
                                for (var j = 0; j < toRead; j++)
                                {
                                    buf[ptr++] = br.ReadSingle();
                                }
                            }
                            p.Horizons[h][w] = horizon;
                        }
                    }
                    return(p);
                }
        }
Exemplo n.º 3
0
        private void btnGpuCalculate_Click(object sender, EventArgs e)
        {
            if (!(Selection1.HasValue && (Selection2.Count > 0 || cbGpuSelfShadow.Checked)))
            {
                return;
            }

            var processor = new GPUProcessor {
                Terrain = Terrain
            };

            var useCache = cbUseCache.Checked;

            var target = GetPatch(Selection1.Value);

            ShadowFromTo selfShadow = null;

            if (cbGpuSelfShadow.Checked)
            {
                var to_key = target.Id;
                var path   = ShadowFromTo.GenerateSelfShadowPath(to_key);
                if (useCache && File.Exists(path))
                {
                    selfShadow = ShadowFromTo.ReadFrom(path);
                }
                else
                {
                    target.InitializeHorizons();
                    if (false)
                    {
                        target.FillPointsAndMatrices(Terrain);
                        target.ParallelAddNearHorizon(Terrain);
                    }
                    else
                    {
                        target.FillPoints(Terrain);
                        target.FillMatricesRelativeToPoint(Terrain, target.Points[0][0]);

                        // GPU
                        processor.AddNearHorizon(target);
                    }

                    var self_shadow = new ShadowFromTo(to_key, to_key);
                    self_shadow.CopyFrom(target.Horizons);
                    _selfShadowDictionary[to_key] = self_shadow;
                }
            }
            else
            {
                target.InitializeHorizons();
            }

            if (selfShadow != null)
            {
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        target.Horizons[line][sample].Merge(selfShadow.Horizons[line][sample]);
                    }
                }
            }

            // Now, for the main show
            var casters = Selection2.Select(ToGlobalKey).Select(TerrainPatch.FromId).ToList();

            processor.UpdateHorizons(target, casters);

            for (var line = 0; line < TerrainPatch.DefaultSize; line++)
            {
                for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                {
                    target.Horizons[line][sample].ConvertSlopeToDegrees();
                }
            }

            RenderMode = PatchRenderMode.AzEl;
            Invalidate();
        }
Exemplo n.º 4
0
        private void btnShadow2To1_Click(object sender, EventArgs e)
        {
            if (!(Selection1.HasValue && (Selection2.Count > 0 || cbSelfShadow.Checked)))
            {
                return;
            }

            var useCache = cbUseCache.Checked;

            var to_patch = GetPatch(Selection1.Value);
            var to_key   = to_patch.Id;

            foreach (var local_from_key in Selection2)
            {
                var from_key = ToGlobalKey(local_from_key);
                var label    = new FromToLabel(from_key.X, from_key.Y, to_key.X, to_key.Y);
                if (!useCache || !_fromToDictionary.ContainsKey(label))
                {
                    ShadowFromTo fromto = null;
                    if (useCache)
                    {
                        string path = ShadowFromTo.GenerateDefaultPath(from_key.X, from_key.Y, to_key.X, to_key.Y);
                        if (File.Exists(path))
                        {
                            fromto = ShadowFromTo.ReadFrom(path);
                            _fromToDictionary[label] = fromto;
                        }
                    }
                    if (fromto != null)
                    {
                        continue;
                    }
                    var s2 = GetPatch(local_from_key);
                    to_patch.FillPointsAndMatrices(Terrain);
                    s2.FillPoints(Terrain);
                    to_patch.InitializeHorizons();
                    to_patch.UpdateHorizon(s2);
                    fromto                   = new ShadowFromTo(from_key, to_key);
                    fromto.Horizons          = to_patch.Horizons;
                    _fromToDictionary[label] = fromto;
                    to_patch.InitializeHorizons();
                }
            }

            ShadowFromTo selfShadow = null;

            if (cbSelfShadow.Checked)
            {
                var path = ShadowFromTo.GenerateSelfShadowPath(to_key);
                if (File.Exists(path))
                {
                    selfShadow = ShadowFromTo.ReadFrom(path);
                }
                else
                {
                    to_patch.FillPointsAndMatrices(Terrain);
                    to_patch.InitializeHorizons();
                    to_patch.ParallelAddNearHorizon(Terrain);
                    var self_shadow = new ShadowFromTo(to_key, to_key);
                    self_shadow.CopyFrom(to_patch.Horizons);
                    _selfShadowDictionary[to_key] = self_shadow;
                }
            }

            to_patch.InitializeHorizons();
            if (selfShadow != null)
            {
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        to_patch.Horizons[line][sample].Merge(selfShadow.Horizons[line][sample]);
                    }
                }
            }

            foreach (var local_from_key in Selection2)
            {
                var from_key = ToGlobalKey(local_from_key);
                var label    = new FromToLabel(from_key.X, from_key.Y, to_key.X, to_key.Y);
                var other    = _fromToDictionary[label];
                for (var line = 0; line < TerrainPatch.DefaultSize; line++)
                {
                    for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                    {
                        to_patch.Horizons[line][sample].Merge(other.Horizons[line][sample]);
                    }
                }
            }

            for (var line = 0; line < TerrainPatch.DefaultSize; line++)
            {
                for (var sample = 0; sample < TerrainPatch.DefaultSize; sample++)
                {
                    to_patch.Horizons[line][sample].ConvertSlopeToDegrees();
                }
            }

            RenderMode = PatchRenderMode.AzEl;
            Invalidate();
        }