private void ImportBitmap_Click(object sender, RoutedEventArgs e)
        {
            RectangleCanvas.Visibility = Visibility.Hidden;
            currRange = new Range();
            currRange = InitStructures.InitCellularAutomata(properties);
            SetProperties();
            OpenFileDialog openfiledialog = new OpenFileDialog();

            openfiledialog.Title  = "Open Image";
            openfiledialog.Filter = "Image File|*.bmp; *.gif; *.jpg; *.jpeg; *.png;";

            if (openfiledialog.ShowDialog() == true)
            {
                Image.Source = Converters.BitmapToImageSource(new Bitmap(openfiledialog.FileName));
                currRange.StructureBitmap = new Bitmap(openfiledialog.FileName);

                CellularAutomata.UpdateGrainsArray(currRange);
                for (int i = 0; i < currRange.Width; i++)
                {
                    for (int j = 0; j < currRange.Height; j++)
                    {
                        currRange.StructureBitmap.SetPixel(i, j, currRange.GrainsArray[i, j].Color);
                    }
                }
            }

            dispatcher.Stop();
            Clear_Selected_Grains_Click(sender, e);
            SetEnableSubStrAndBoundCheckBoxs();
        }
        // Add initialization of Sub for the current Structure
        public override void ExitTypeStmt([NotNull] BosParser.TypeStmtContext context)
        {
            var currentType = TypesStack.Pop();

            if (InitStructures.ContainsKey(currentType) && InitStructures[currentType].Text.Length > 0)
            {
                StreamRewriter.InsertBefore(context.Stop, InitStructures[currentType].Text);
                StructuresWithInit.Add(currentType);
            }
            else
            {
                InitStructures.Remove(currentType);
            }
            //base.ExitTypeStmt(context);
        }
        private void Previous_Structure_Button_Click(object sender, RoutedEventArgs e)
        {
            prevRange = new Range();
            prevRange = InitStructures.InitCellularAutomata(properties);

            CellularAutomata.UpdateBitmap(currRange);

            prevRange.StructureBitmap = currRange.StructureBitmap;
            CellularAutomata.UpdateGrainsArray(prevRange);
            CellularAutomata.UpdateBitmap(prevRange);
            prevRange = nucleons.EnergyDistributor(currRange, nucleons);

            CellularAutomata.UpdateBitmap(currRange);
            Image.Source = Converters.BitmapToImageSource(currRange.StructureBitmap);
        }
        private void Energy_Vizualization_Button_Click(object sender, RoutedEventArgs e)
        {
            currRange = new Range();
            currRange = InitStructures.InitCellularAutomata(properties);

            CellularAutomata.UpdateBitmap(prevRange);

            currRange.StructureBitmap = prevRange.StructureBitmap;
            CellularAutomata.UpdateGrainsArray(currRange);
            CellularAutomata.UpdateBitmap(currRange);
            nucleons.EnergyDistributor(prevRange, nucleons);
            nucleons.EnergyVisualization(ref prevRange, nucleons);
            CellularAutomata.UpdateBitmap(prevRange);
            Image.Source = Converters.BitmapToImageSource(prevRange.StructureBitmap);
        }
        // we add initialization Sub for the current Structure
        public override void ExitTypeStmt([NotNull] VBAParser.TypeStmtContext context)
        {
            var currentType = Types.Pop();

            if (InitStructures.ContainsKey(currentType) && InitStructures[currentType].Text.Length > 0)
            {
                Rewriter.InsertBefore(context.Stop, InitStructures[currentType].Text);

                StructuresWithInitializer.Add(currentType);
            }
            else
            {
                InitStructures.Remove(currentType);
            }
        }
        // transform a Type in a Structure
        public override void EnterTypeStmt(VBAParser.TypeStmtContext context)
        {
            var typeName = context.ambiguousIdentifier().GetText();

            Types.Push(typeName);
            InitStructures.Add(typeName, new StructureInitializer(typeName));

            Rewriter.Replace(context.TYPE().Symbol, "Structure");
            Rewriter.Replace(context.END_TYPE().Symbol, "End Structure");

            string visibility = context.visibility().GetText();

            foreach (var st in context.typeStmt_Element())
            {
                Rewriter.InsertBefore(st.Start, $"{visibility} ");
            }
        }
        //transform a Type in a Structure
        // typeStmt rule (VBA.g4 Line 502)
        public override void EnterTypeStmt([NotNull] BosParser.TypeStmtContext context)
        {
            // Find the type name or in the grammar the identifier
            var typeName = context.ambiguousIdentifier().GetText();

            TypesStack.Push(typeName);  // Store it in the stack, helpful when recreating the type
            // Used to create the new type
            InitStructures.Add(typeName, new StructureInitializer(typeName));
            StreamRewriter.Replace(context.TYPE().Symbol, "Structure");
            StreamRewriter.Replace(context.END_TYPE().Symbol, "End Structure");

            string visibility = context.visibility().GetText();

            foreach (var st in context.typeStmt_Element())
            {
                StreamRewriter.InsertBefore(st.Start, $"{visibility} ");
            }
        }
Exemple #8
0
        public InitBoundaries(MainProperties properties)
        {
            ClearBoundaries        = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesAll          = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesSelected     = new Range(properties.RangeWidth, properties.RangeHeight);
            BoundariesSingleSelect = new Range(properties.RangeWidth, properties.RangeHeight);

            InitStructures.AddBlackBorder(ClearBoundaries);
            InitStructures.AddBlackBorder(BoundariesAll);
            InitStructures.AddBlackBorder(BoundariesSelected);
            InitStructures.AddBlackBorder(BoundariesSingleSelect);

            // init grains array by default values
            for (int i = 1; i < properties.RangeWidth - 1; i++)
            {
                for (int j = 1; j < properties.RangeHeight - 1; j++)
                {
                    ClearBoundaries.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesAll.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesSelected.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                    BoundariesSingleSelect.GrainsArray[i, j] = new Grain()
                    {
                        Id       = 0,
                        Color    = Color.White,
                        Energy_H = 0
                    };
                }
            }
        }
        private void Play_Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                Mouse.OverrideCursor = Cursors.Wait;
            });
            RectangleCanvas.Visibility = Visibility.Hidden;

            if (Substructures.SubStrucrtuePointsList.Count > 0)
            {
                SetProperties();

                if (MonteCarloRadioButton.IsChecked == true)
                {
                    prevRange = substructures.UpdateSubstructuresMC(currRange, properties);
                }
                else
                {
                    prevRange = substructures.UpdateSubstructuresCA(currRange, properties);
                }
            }
            else
            {
                SetProperties();

                if (MonteCarloRadioButton.IsChecked == true)
                {
                    prevRange             = InitStructures.InitMonteCarlo(properties);
                    SRXCheckBox.IsChecked = false;
                    nucleons.IsEnable     = false;
                }
                else
                {
                    prevRange = InitStructures.InitCellularAutomata(properties);

                    if (inclusions.CreationTime == InclusionsCreationTime.Begin && InclusionsCheckBox.IsChecked == true)
                    {
                        prevRange = inclusions.AddInclusionsAtTheBegining(prevRange);
                    }
                }
            }
            dispatcher.Start();
        }
        private void ImportTXT_Click(object sender, RoutedEventArgs e)
        {
            RectangleCanvas.Visibility = Visibility.Hidden;
            currRange = new Range();
            currRange = InitStructures.InitCellularAutomata(properties);
            SetProperties();
            OpenFileDialog openfiledialog = new OpenFileDialog();

            openfiledialog.Title  = "Open Image";
            openfiledialog.Filter = "Image File|*.txt";

            if (openfiledialog.ShowDialog() == true)
            {
                Image.Source = Converters.BitmapToImageSource(new Bitmap(openfiledialog.FileName));
                currRange.StructureBitmap = new Bitmap(openfiledialog.FileName);

                CellularAutomata.UpdateGrainsArray(currRange);
                CellularAutomata.UpdateBitmap(currRange);
            }

            dispatcher.Stop();
            Clear_Selected_Grains_Click(sender, e);
            SetEnableSubStrAndBoundCheckBoxs();
        }
Exemple #11
0
        public Range Grow(Range prevRange, MainProperties properties)
        {
            var currRange = new Range(prevRange.Width, prevRange.Height, true);

            InitStructures.AddBlackBorder(currRange);

            var isGrowthMoore2 = properties.NeighbourhoodType == NeighbourhoodType.Moore2 ? true : false;

            List <Grain> neighbourhood = new List <Grain>();

            for (int i = 1; i < prevRange.Width - 1; i++)
            {
                for (int j = 1; j < prevRange.Height - 1; j++)
                {
                    if (prevRange.GrainsArray[i, j].Id != (int)SpecialId.Id.Empty)
                    {
                        // just init if there is already some color (not white)
                        currRange.GrainsArray[i, j] = prevRange.GrainsArray[i, j];
                    }
                    else
                    {
                        if (!isGrowthMoore2)
                        {
                            // ordinary types of growth - list of Moore or Neuman neighbourhood
                            switch (properties.NeighbourhoodType)
                            {
                            case NeighbourhoodType.Moore:
                                neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);
                                break;

                            case NeighbourhoodType.Neumann:
                                neighbourhood = TakeNeumannNeighbourhood(i, j, prevRange.GrainsArray);
                                break;
                            }

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                // assign grain which are the most in the list of neighborhoods
                                currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                              .Select(g => g.First()).First();
                            }
                            else
                            {
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                        else
                        {
                            // MOORE 2

                            var grainGrowth = false;

                            // rule 1 - ordinary moore
                            neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                most = most.OrderByDescending(g => g.Count());

                                if (most.First().Count() >= 5 && most.First().Count() <= 8)
                                {
                                    currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                    grainGrowth = true;
                                }
                                else
                                {
                                    // rule 2 - nearest moore
                                    neighbourhood = TakeNearestMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                    most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                           .GroupBy(g => g.Id);

                                    if (most.Any())
                                    {
                                        most = most.OrderByDescending(g => g.Count());
                                        if (most.First().Count() == 3)
                                        {
                                            currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 3 - further moore
                                        neighbourhood = TakeFurtherMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        if (most.Any())
                                        {
                                            most = most.OrderByDescending(g => g.Count());
                                            if (most.First().Count() == 3)
                                            {
                                                currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                                grainGrowth = true;
                                            }
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 4 - ordinary moore with probability
                                        neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        var randomProbability = Random.Next(0, 100);
                                        if (most.Any() && (randomProbability <= properties.GrowthProbability))
                                        {
                                            currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                                          .Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                }
                            }
                            if (!grainGrowth)
                            {
                                // if grain not exist
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                    }
                }
            }
            UpdateBitmap(currRange);
            return(currRange);
        }