Skip to content

Commit

Permalink
Merge pull request #8 from maslankam/transition-rule2
Browse files Browse the repository at this point in the history
Variable space, steps and grains
  • Loading branch information
maslankam authored Nov 16, 2019
2 parents 7d2c4ae + 73012a6 commit f481053
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 86 deletions.
49 changes: 32 additions & 17 deletions MsmGrainGrowthGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MsmGrainGrowthGui"
mc:Ignorable="d"
Title="Grain Growth Cellural Automata" Height="595" Width="800">
Title="Grain Growth Cellural Automata" Height="700" Width="800">

<StackPanel>
<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Command="Open">
<Button IsEnabled="False">
<StackPanel Orientation="Horizontal">
<Image Source="/icons/folder.png" />
<TextBlock Margin="5,0">Open</TextBlock>
</StackPanel>
</Button>
<Button Padding="5">
<Button Padding="5" IsEnabled="False">
<StackPanel Orientation="Horizontal">
<Image Source="/icons/diskette.png" />
<TextBlock Margin="5,0">Save As</TextBlock>
</StackPanel>
</Button>
</ToolBar>
<ToolBar>
<Button>
<StackPanel Orientation="Horizontal">
<Button IsEnabled="False">
<StackPanel Orientation="Horizontal" >
<Image Source="/icons/excel.png" />
<TextBlock Margin="5,0">Export to CSV</TextBlock>
</StackPanel>
</Button>
<Button>
<StackPanel Orientation="Horizontal">
<Button IsEnabled="False">
<StackPanel Orientation="Horizontal" IsEnabled="False">
<Image Source="/icons/picture.png" />
<TextBlock Margin="5,0">Save image</TextBlock>
</StackPanel>
Expand All @@ -45,9 +45,13 @@
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Border BorderThickness="1" BorderBrush="#FF000000">
<Image Name="CelluralSpaceView" Grid.Column="0" Height="500" Width="500" Margin="5" ></Image>
</Border>
<StackPanel>
<Border BorderThickness="1" BorderBrush="#FF000000">
<Image Name="CelluralSpaceView" Grid.Column="0" Height="500" Width="500" Margin="5" ></Image>
</Border>
<Label Margin="5" Name="StepLabel" ></Label>
</StackPanel>

<StackPanel Grid.Column="1">
<Label Content="Boundary" />
<ComboBox Margin="5">
Expand All @@ -62,13 +66,24 @@
<ComboBoxItem>Hexagonal</ComboBoxItem>
</ComboBox>
<Label Content="Grains" />
<TextBox Margin="5" Text="7"/>

<Button Margin="5" Padding="5" >Randomize</Button>
<Button Margin="5" Padding="5" Click="NextButton_Click" Name="NextSpaceView">Next Step</Button>
<Button Margin="5" Padding="5" Name="RunButton">Run</Button>
<Button Margin="5" Padding="5" >Stop</Button>
<Button Margin="5" Padding="5" >Reset</Button>
<TextBox Name="Grains_TextBox" Margin="5" Text="{Binding GrainCount}"/>
<Label Content="Space size" />
<TextBox Name="SpaceSize_TextBox" Margin="5" Text="{Binding SpaceSize}" />
<!--<Button Margin="5" Padding="5" >Randomize</Button>-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2.6*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="5" Click="NextButton_Click" Name="NextSpaceView">Next</Button>
<TextBlock Grid.Column="1" Padding="5,5,5,5" TextWrapping="Wrap" >Steps per click :</TextBlock>
<TextBox Grid.Column="2" Name="StepsPerClick_TextBox" Margin="0,5,5,5" Text="{Binding StepsPerClick}"></TextBox>
</Grid>

<!--<Button Margin="5" Padding="5" Name="RunButton">Run</Button>-->
<!--<Button Margin="5" Padding="5" >Stop</Button>-->
<Button Margin="5" Padding="5" Click="ResetButton_Click">Reset</Button>

</StackPanel>
</Grid>
Expand Down
172 changes: 103 additions & 69 deletions MsmGrainGrowthGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,88 +24,106 @@ namespace MsmGrainGrowthGui
/// </summary>
public partial class MainWindow : Window
{
private int spaceSize;
//TODO: Add string validation this is really important!
public string SpaceSize
{
get { return this._spaceSize.ToString(); }
set { this._spaceSize = Convert.ToInt32(value);}
}

public string GrainCount
{
get { return this._grainCount.ToString(); }
set { this._grainCount = Convert.ToInt32(value); }
}

public int Step
{
get { return this._step; }
set { this._step = value; }
}

public string StepsPerClick
{
get { return this._stepsPerClick.ToString(); }
set { this._stepsPerClick = Convert.ToInt32(value); }
}

private int _stepsPerClick;
private int _grainCount;
private int _spaceSize;
private CelluralAutomaton automaton;
private Grain redGrain;
private Grain blueGrain;
private Grain cyanGrain;
private Grain magentaGrain;
private Grain yellowGrain;
private Grain greenGrain;
private Grain darkGreenGrain;
private Grain[] grains;
private Cell[,] space;
private Cell[,] previousSpace;

private string input;
private int step;
private int _step;

public CelluralAutomaton Automaton { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;

this.spaceSize = 500;

this.automaton = new CelluralAutomaton(this.spaceSize);

this.redGrain = new Grain(0, System.Drawing.Color.Red);
this.blueGrain = new Grain(1, System.Drawing.Color.Blue);
this.cyanGrain = new Grain(2, System.Drawing.Color.Cyan);
this.magentaGrain = new Grain(3, System.Drawing.Color.Magenta);
this.yellowGrain = new Grain(4, System.Drawing.Color.Yellow);
this.greenGrain = new Grain(5, System.Drawing.Color.Green);
this.darkGreenGrain = new Grain(6, System.Drawing.Color.DarkGreen);

space = automaton.Space.currentState;
previousSpace = automaton.Space.lastState;

var r = new Random();

r.Next();

space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = redGrain;
space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = blueGrain;
space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = cyanGrain;
space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = magentaGrain;
space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = yellowGrain;
space[r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = greenGrain;
space[ r.Next(0,this.spaceSize - 1), r.Next(0,this.spaceSize - 1)].GrainMembership = darkGreenGrain;

string input = "";
int step = 0;

this.GrainCount = "10";
this.StepsPerClick = "1";
this.SpaceSize = "40";
this.Step = 0;
StepLabel.Content = Step;
}


private void NextButton_Click(object sender, RoutedEventArgs e)
{
PixelFormat pf = PixelFormats.Bgr32;
int width = this.spaceSize;
int height = this.spaceSize;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];
int rawImageIndex = 0;
for(int k = 0; k < Convert.ToInt32(this.StepsPerClick); k++)
{
if (_step == 0)
{
this.automaton = new CelluralAutomaton(this._spaceSize);

// Initialize the image with data.
Random value = new Random();
grains = new Grain[Convert.ToInt32(GrainCount)];


for (int i = 0; i < space.GetLength(0); i++)
{
for (int j = 0; j < space.GetLength(1); j++)
var r = new Random();

space = automaton.Space.currentState;
previousSpace = automaton.Space.lastState;

for (int i = 0; i < this.grains.Length; i++)
{
this.grains[i] = new Grain(i, System.Drawing.Color.FromArgb(0, r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))); // TODO: get random from HSV for fancy colors
space[r.Next(0, this._spaceSize - 1), r.Next(0, this._spaceSize - 1)].GrainMembership = this.grains[i]; //TODO: check if not hiting occuppied cell
}

this.Grains_TextBox.IsEnabled = false;
this.SpaceSize_TextBox.IsEnabled = false;
}


PixelFormat pf = PixelFormats.Bgr32;
int width = this._spaceSize;
int height = this._spaceSize;
int rawStride = (width * pf.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[rawStride * height];
int rawImageIndex = 0;

// Initialize the image with data.
Random value = new Random();


for (int i = 0; i < space.GetLength(0); i++)
{
previousSpace[i, j] = space[i, j];
space[i,j] = new Cell();
for (int j = 0; j < space.GetLength(1); j++)
{
previousSpace[i, j] = space[i, j];
space[i, j] = new Cell();
}
}
}


for (int i = 0; i < space.GetLength(0); i++)
{
for (int j = 0; j < space.GetLength(1); j++)
{
if (step == 0)
if (_step == 0)
{
space[i, j].GrainMembership = previousSpace[i, j].GrainMembership;
}
Expand All @@ -117,7 +135,7 @@ private void NextButton_Click(object sender, RoutedEventArgs e)
System.Drawing.Color pixelColor = space?[i, j]?.GrainMembership?.Color ?? System.Drawing.Color.White;

// write byte[index] with pixelColor
if(rawImageIndex >= rawImage.Length)
if (rawImageIndex >= rawImage.Length)
{
System.Diagnostics.Trace.WriteLine($"pixel [{i},{j}], rawIndex: {rawImageIndex}, outide of {rawImage.Length} bound");
}
Expand All @@ -126,22 +144,38 @@ private void NextButton_Click(object sender, RoutedEventArgs e)
rawImage[rawImageIndex++] = pixelColor.R;
rawImage[rawImageIndex++] = pixelColor.G;
rawImage[rawImageIndex++] = pixelColor.B;
rawImage[rawImageIndex++] = pixelColor.A;
rawImage[rawImageIndex++] = 0;
}
}
this.step++;


}



this.Step++;
this.StepLabel.Content = Step;




// Create a BitmapSource.
BitmapSource bitmap = BitmapSource.Create(width, height,
this._spaceSize, this._spaceSize, pf, null,
rawImage, rawStride);

CelluralSpaceView.Source = bitmap;
}

// Create a BitmapSource.
BitmapSource bitmap = BitmapSource.Create(width, height,
this.spaceSize, this.spaceSize, pf, null,
rawImage, rawStride);

CelluralSpaceView.Source = bitmap;


}

private void ResetButton_Click(object sender, RoutedEventArgs e)
{
this.Step = 0;
this.StepLabel.Content = Step;
this.Grains_TextBox.IsEnabled = true;
this.SpaceSize_TextBox.IsEnabled = true;

}
}
}

0 comments on commit f481053

Please sign in to comment.