Skip to content

Commit

Permalink
Bug fixes and polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
maslankam committed Nov 30, 2019
1 parent 32e5ef0 commit 6eeba35
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 108 deletions.
92 changes: 29 additions & 63 deletions Model/Neighbourhood/HexagonNeighbourhood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public class HexagonNeighborhood : INeighbourhood
{
private IBoundaryCondition _boundary;

public string Name
{
get { return this.ToString();}
}

public HexagonNeighborhood(IBoundaryCondition boundary)
{
this._boundary = boundary;
Expand All @@ -16,105 +21,66 @@ public Cell[] GetNeighbours(CelluralSpace space, int x, int y)
{
//TODO: any abstraction for N,W,S,E,NW,NE,SW,SE ??? It apears meany times acros neighbourghood classes?? Abstract class ?
// Nested if's are hard to read
Cell[] result = new Cell[5];

//Check N neighbour
if (x - 1 >= 0)
{
result[0] = space.GetCell(x - 1, y);
}
else
{
result[0] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.W);
}

//Check S neighbour
if (x + 1 <= space.GetYLength() - 1)
{
result[1] = space.GetCell(x + 1, y);
}
else
{
result[1] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.E);
}

Cell[] result = new Cell[6];
var vonNeuman = new VonNeumanNeighbourhood(_boundary);
Cell[] vonNeumanNeighbourhood = vonNeuman.GetNeighbours(space, x, y);
result[0] = vonNeumanNeighbourhood[0];
result[1] = vonNeumanNeighbourhood[1];
result[2] = vonNeumanNeighbourhood[2];
result[3] = vonNeumanNeighbourhood[3];

var r = new Random();

if(r.Next(0,100) > 50)
{
//right side

//Check E
if (y + 1 <= space.GetXLength() - 1)
{
result[2] = space.GetCell(x, y + 1);
}
else
{
result[2] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.S);
}

//Check NE
if (x - 1 >= 0 && y + 1 < space.GetYLength())
{
result[3] = space.GetCell(x - 1, y + 1);
result[4] = space.GetCell(x - 1, y + 1);
}
else
{
result[3] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.NE);
result[4] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.NE);
}

//Check SE neighbour
if (x + 1 < space.GetXLength() && y + 1 < space.GetYLength())
//Check SW neighbour
if (x + 1 < space.GetXLength() && y - 1 >= 0)
{
result[4] = space.GetCell(x + 1, y + 1);
result[5] = space.GetCell(x + 1, y - 1);
}
else
{
result[4] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.SE);
result[5] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.SW);
}

}
else
{
//left side

//Check W
if (y - 1 >= 0)
{
result[2] = space.GetCell(x, y - 1);
}
else
{
result[2] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.N);
}
// Check SW neighbour
if (x + 1 < space.GetXLength() && y - 1 >= 0)
//Check SE neighbour
if (x + 1 < space.GetXLength() && y + 1 < space.GetYLength())
{
result[3] = space.GetCell(x + 1, y - 1);
result[4] = space.GetCell(x + 1, y + 1);
}
else
{
result[3] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.SW);
result[4] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.SE);
}

//Check NW neighbour
if (x - 1 >= 0 && y - 1 >= 0)
{
result[4] = space.GetCell(x - 1, y - 1);
result[5] = space.GetCell(x - 1, y - 1);
}
else
{
result[4] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.NW);
result[5] = _boundary.GetBoundaryNeighbour(space, x, y, BoundaryDirection.NW);
}


}

return result;

}
public override string ToString()
{
return "HexagonNeighbourhood";
}

}


Expand Down
9 changes: 9 additions & 0 deletions Model/Neighbourhood/MooreNeighbourhood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class MooreNeighbourhood : INeighbourhood
{
private IBoundaryCondition _boundary;

public string Name
{
get { return this.ToString(); }
}

public MooreNeighbourhood(IBoundaryCondition boundary)
{
this._boundary = boundary;
Expand Down Expand Up @@ -70,6 +75,10 @@ public Cell[] GetNeighbours(CelluralSpace space, int x, int y)
return result;

}
public override string ToString()
{
return "MooreNeighbourhood";
}
}


Expand Down
12 changes: 10 additions & 2 deletions Model/Neighbourhood/PentagonNeighbourhood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ public class PentagonNeighbourhood : INeighbourhood
{
private IBoundaryCondition _boundary;

public PentagonNeighbourhood(IBoundaryCondition boundary)
public string Name
{
get { return this.ToString(); }
}

public PentagonNeighbourhood(IBoundaryCondition boundary)
{
this._boundary = boundary;
}

public Cell[] GetNeighbours(CelluralSpace space, int x, int y)
{


var r = new Random();

Expand Down Expand Up @@ -116,6 +120,10 @@ public Cell[] GetNeighbours(CelluralSpace space, int x, int y)
}

}
public override string ToString()
{
return "PentagonNeighbourhood";
}


}
Expand Down
11 changes: 11 additions & 0 deletions Model/Neighbourhood/VonNeumanNeighbourhood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public class VonNeumanNeighbourhood : INeighbourhood
{
private IBoundaryCondition _boundary;


public string Name
{
get { return this.ToString(); }
}


public VonNeumanNeighbourhood(IBoundaryCondition boundary){
this._boundary = boundary;
}
Expand Down Expand Up @@ -62,6 +69,10 @@ public Cell[] GetNeighbours(CelluralSpace space, int x, int y){
return result;

}
public override string ToString()
{
return "VonNeumanNeighbourhood";
}
}


Expand Down
2 changes: 1 addition & 1 deletion MsmGrainGrowthGui/ApplicationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static INeighbourhood GetNeighbourhoodByName(string name, IBoundaryCondit
case "Model.MooreNeighbourhood":
return new MooreNeighbourhood(boundary);
case "Model.PentagonNeighbourhood":
return new MooreNeighbourhood(boundary);
return new PentagonNeighbourhood(boundary);
case "Model.VonNeumanNeighbourhood":
return new VonNeumanNeighbourhood(boundary);
default: throw new ArgumentException();
Expand Down
71 changes: 54 additions & 17 deletions MsmGrainGrowthGui/CelluralAutomatonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Windows.Media;
using System.Xml.Linq;
using System.IO;
using Microsoft.Win32;

namespace GrainGrowthGui
{
Expand Down Expand Up @@ -56,7 +57,9 @@ public List<INeighbourhood> Neighbourhoods
public string Neighbourhood
{
get { return _neighbourhood.ToString(); }
set { _neighbourhood = ApplicationState.GetNeighbourhoodByName("Model." + value, _boundary); }
set {
_neighbourhood = ApplicationState.GetNeighbourhoodByName("Model." + value, _boundary);
}
}


Expand Down Expand Up @@ -129,7 +132,7 @@ void GenerateExecute()
// TODO: Generate can be replaced by OnModelChange event. This also may enable "save" button

// Lazy initialization of boundary.
_neighbourhood = new MooreNeighbourhood(_boundary);
//_neighbourhood = new MooreNeighbourhood(_boundary);

_automaton = new CelluralAutomaton(
_spaceSize,
Expand Down Expand Up @@ -308,8 +311,18 @@ public ICommand Stop
void OpenExecute()
{
// TODO: Add file dialog!
OpenFileDialog openFileDialog = new OpenFileDialog();
string path = "";
if (openFileDialog.ShowDialog() == true)
{
path = openFileDialog.FileName;
}
else
{
return;
}

var doc = XDocument.Load(@"C:\Users\mikim\Desktop\Nowy folder\hello2.xml");
var doc = XDocument.Load(path);
var reader = new XmlReader();
var state = reader.Read(doc);

Expand All @@ -329,8 +342,6 @@ void OpenExecute()

_imageSource = _renderEngine.Render(_automaton.Space);
ImageRendered.Invoke(this, _imageSource);

System.Windows.MessageBox.Show("File Opened");
}

bool CanOpenExecute()
Expand All @@ -352,6 +363,18 @@ public ICommand Open
#region SaveAsCommand
void SaveAsExecute()
{
string path;
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true)
{
path = saveFileDialog.FileName;
}
else
{
return;
}


var state = new ApplicationState(
_automaton,
_spaceSize,
Expand All @@ -368,10 +391,7 @@ void SaveAsExecute()
var factory = new XmlFactory();
var doc = factory.GetXDocument(state);

doc.Save(@"C:\Users\mikim\Desktop\Nowy folder\hello2.xml");


System.Windows.MessageBox.Show("File Saved");
doc.Save(path);
}

bool CanSaveAsExecute()
Expand All @@ -393,13 +413,20 @@ public ICommand SaveAs
#region ExportCsvCommand
void ExportCsvExecute()
{
string path;
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true)
{
path = saveFileDialog.FileName;
}
else
{
return;
}

var formatter = new CsvSpaceFormatter();
string csv = formatter.Format(_automaton.Space);
CsvWriter.WriteToCsv(csv, @"C:\Users\mikim\Desktop\Nowy folder\hello3.csv");



System.Windows.MessageBox.Show("Exported to Csv");
CsvWriter.WriteToCsv(csv, path);
}

bool CanExportCsvExecute()
Expand All @@ -421,15 +448,25 @@ public ICommand ExportCsv
#region ExportPngCommand
void ExportPngExecute()
{
string path;
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true)
{
path = saveFileDialog.FileName;
}
else
{
return;
}


var image = _imageSource;
using (var fileStream = new FileStream(@"C:\Users\mikim\Desktop\Nowy folder\hello4.png", FileMode.Create))
using (var fileStream = new FileStream(path, FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(fileStream);
}

System.Windows.MessageBox.Show("Exported to Png");
}

bool CanExportPngExecute()
Expand Down
Loading

0 comments on commit 6eeba35

Please sign in to comment.