Skip to content

Commit

Permalink
Merge pull request #7 from maslankam/transition-rule2
Browse files Browse the repository at this point in the history
Rendering implemented
  • Loading branch information
maslankam authored Nov 16, 2019
2 parents d38621c + 2830500 commit 7d2c4ae
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 156 deletions.
10 changes: 0 additions & 10 deletions Model/AbsorbingBoundary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ public static class AbsorbingBoundary
/// |nl|nl|nl|nl|nl|
public static Tuple<int,int> BoundaryCondition(Cell[,] space, int x, int y, BoundaryDirection direction) {

switch(direction){
case BoundaryDirection.N : break;
case BoundaryDirection.NE : break;
case BoundaryDirection.E : break;
case BoundaryDirection.SE : break;
case BoundaryDirection.S : break;
case BoundaryDirection.SW : break;
case BoundaryDirection.W : break;
case BoundaryDirection.NW : break;
default : break; }

return null;
}
Expand Down
14 changes: 7 additions & 7 deletions Model/CelluralAutomaton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ public class CelluralAutomaton
{
public CelluralSpace Space { get; private set; }
public List<Grain> Grains { get; private set; }
/*public Bitmap View
{
get { return Space.RenderCelluralSpace(); }
}*/


public CelluralAutomaton()
{
this.Space = new CelluralSpace(30); //TODO: replace magic number with resizable control


this.Space = new CelluralSpace(500); //TODO: replace magic number with resizable control
}

public CelluralAutomaton(int spaceSize)
{
this.Space = new CelluralSpace(spaceSize);
}


Expand All @@ -28,3 +27,4 @@ public CelluralAutomaton()


}

12 changes: 12 additions & 0 deletions MsmGrainGrowthGui/CelluralAutomatonViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GrainGrowthGui
{
class CelluralAutomatonViewModel
{


}
}
22 changes: 11 additions & 11 deletions MsmGrainGrowthGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
<Label Content="Boundary" />
<ComboBox Margin="5">
<ComboBoxItem IsSelected="True">Absorbing</ComboBoxItem>
<!-- TODO -->
<!--<ComboBoxItem>Periodic</ComboBoxItem>-->
<ComboBoxItem>Periodic</ComboBoxItem>
</ComboBox>
<Label Content="Neighborhood" />
<ComboBox Margin="5">
<ComboBoxItem IsSelected="True">von Neumann</ComboBoxItem>
<!--<ComboBoxItem>Moore</ComboBoxItem>-->
<!--<ComboBoxItem>Pentagonal</ComboBoxItem>-->
<!--<ComboBoxItem>Hexagonal</ComboBoxItem>-->
</ComboBox>
<Label Content="Colors" />
<TextBox Margin="5" Text="3"/>
<Button Margin="5" Padding="5" Click="Button_Click">Randomize</Button>
<Button Margin="5" Padding="5" >Next Step</Button>
<Button Margin="5" Padding="5" >Run</Button>
<ComboBoxItem>Moore</ComboBoxItem>
<ComboBoxItem>Pentagonal</ComboBoxItem>
<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>

Expand Down
120 changes: 113 additions & 7 deletions MsmGrainGrowthGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -14,28 +12,136 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Diagnostics;

using Model;


namespace MsmGrainGrowthGui
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
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 Cell[,] space;
private Cell[,] previousSpace;

private string input;
private int step;

public CelluralAutomaton Automaton { get; set; }
public MainWindow()
{
this.Automaton = new CelluralAutomaton();
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;

}

private void Button_Click(object sender, RoutedEventArgs e)

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;

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


for (int i = 0; i < space.GetLength(0); i++)
{
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)
{
space[i, j].GrainMembership = previousSpace[i, j].GrainMembership;
}
else
{
space[i, j].GrainMembership = TransitionRule.NextState(previousSpace, i, j);
}

System.Drawing.Color pixelColor = space?[i, j]?.GrainMembership?.Color ?? System.Drawing.Color.White;

// write byte[index] with pixelColor
if(rawImageIndex >= rawImage.Length)
{
System.Diagnostics.Trace.WriteLine($"pixel [{i},{j}], rawIndex: {rawImageIndex}, outide of {rawImage.Length} bound");
}
else
{
rawImage[rawImageIndex++] = pixelColor.R;
rawImage[rawImageIndex++] = pixelColor.G;
rawImage[rawImageIndex++] = pixelColor.B;
rawImage[rawImageIndex++] = pixelColor.A;
}
}
this.step++;
}




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

CelluralSpaceView.Source = bitmap;

}
}
}
6 changes: 0 additions & 6 deletions MultiscaleModeling.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utility", "Utility\Utility.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{0C15D3C3-3FA8-4986-9D31-A24B3F26E2A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{410821EC-E5EA-42E7-89F1-2AC37872850D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,10 +33,6 @@ Global
{0C15D3C3-3FA8-4986-9D31-A24B3F26E2A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C15D3C3-3FA8-4986-9D31-A24B3F26E2A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C15D3C3-3FA8-4986-9D31-A24B3F26E2A9}.Release|Any CPU.Build.0 = Release|Any CPU
{410821EC-E5EA-42E7-89F1-2AC37872850D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{410821EC-E5EA-42E7-89F1-2AC37872850D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{410821EC-E5EA-42E7-89F1-2AC37872850D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{410821EC-E5EA-42E7-89F1-2AC37872850D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
103 changes: 0 additions & 103 deletions ProofOfConceptConsoleApp/Program.cs

This file was deleted.

12 changes: 0 additions & 12 deletions ProofOfConceptConsoleApp/ProofOfConceptConsoleApp.csproj

This file was deleted.

0 comments on commit 7d2c4ae

Please sign in to comment.