Skip to content

Commit

Permalink
Merge pull request #18 from maslankam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
maslankam authored Dec 7, 2019
2 parents aa51681 + a8b0fb2 commit 6fe2a96
Show file tree
Hide file tree
Showing 45 changed files with 1,229 additions and 384 deletions.
2 changes: 0 additions & 2 deletions Model/Boundary/AbsorbingBoundary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Model{


Expand Down
2 changes: 1 addition & 1 deletion Model/Boundary/IBoundaryCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum BoundaryDirection{

public interface IBoundaryCondition
{
public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction);
Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction);
}


Expand Down
11 changes: 2 additions & 9 deletions Model/Boundary/PeriodicBoundary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ public string Name
}
}

// AbsorbingBoundary always return null Cell
/// y ->
///x |22|20|21|22|20|
///| |02|00|01|02|00|
///v |12|10|11|12|10|
/// |22|20|21|22|20|
/// |02|00|01|02|00|
public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDirection direction)
{
switch (direction)
Expand Down Expand Up @@ -70,7 +63,7 @@ public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDire
{
if (y == 0)
{
return space.GetCell(0, 2);
return space.GetCell(0, space.GetYLength() - 1);
}
else
{
Expand All @@ -89,7 +82,7 @@ public Cell GetBoundaryNeighbour(CelluralSpace space, int x, int y, BoundaryDire
{
if (x == 0)
{
return space.GetCell(2, 2);
return space.GetCell(space.GetXLength() - 1, space.GetYLength() - 1);
}
else
{
Expand Down
36 changes: 16 additions & 20 deletions Model/CelluralAutomaton.cs → Model/CellularAutomaton.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
using System;
using System.Collections.Generic;
using System.Drawing;


namespace Model{
public class CelluralAutomaton {
public class CellularAutomaton {

public List<Grain> Grains { get; private set; }
public List<Inclusion> Inclusions { get; private set; }

public CelluralSpace Space { get; private set; }
public int Step
{
get { return this._executor.Step; }
private set { }
}
public CelluralSpace Space { get; }
public int Step => this._executor.ReturnStep();

private CelluralSpace _lastStepSpace;
private readonly ITransitionRule _transition;
private readonly INeighbourhood _neighbourhood;
private readonly SimulationExecutor _executor;
private readonly ISimulationExecutor _executor;
//private SpaceRenderingEngine _renderingEngine;
private readonly GrainInitializer _grainInitializer;
private readonly InclusionInitializer _inclusionInitializer;
private readonly GrainSeeder _grainSeeder;
private readonly InclusionSeeder _inclusionSeeder;

public CelluralAutomaton(int size,
int GrainsCount,
public CellularAutomaton(int size,
int grainsCount,
int inclusionsCount,
int minRadius,
int maxRadius,
ITransitionRule transition,
INeighbourhood neighbourhood,
IBoundaryCondition boundary)
IBoundaryCondition boundary,
ISimulationExecutor executor)
{
if(minRadius > maxRadius) throw new ArgumentException("MinRadius cannot be greater than MaxRadius");
if (transition == null || neighbourhood == null || boundary == null) throw new ArgumentNullException();
_transition = transition;
_neighbourhood = neighbourhood;
_lastStepSpace = new CelluralSpace(size);
_executor = new SimulationExecutor();
_executor = executor;
_grainInitializer = new GrainInitializer();
_inclusionInitializer = new InclusionInitializer();
_grainSeeder = new GrainSeeder();
_inclusionSeeder = new InclusionSeeder(boundary);

Space = new CelluralSpace(size);
PopulateSimulation(GrainsCount, inclusionsCount, minRadius, maxRadius);
PopulateSimulation(grainsCount, inclusionsCount, minRadius, maxRadius);
}

//Constructor for opening saved state
public CelluralAutomaton(
public CellularAutomaton(
CelluralSpace space,
List<Grain> grains,
List<Inclusion> inclusions,
ITransitionRule transition,
INeighbourhood neighbourhood,
IBoundaryCondition boundary,
int step)
int step, ISimulationExecutor executor)
{
if (transition == null ||
neighbourhood == null ||
Expand All @@ -70,7 +66,7 @@ public CelluralAutomaton(
_transition = transition;
_neighbourhood = neighbourhood;

_executor = new SimulationExecutor();

_grainInitializer = null;
_inclusionInitializer = null;
_grainSeeder = null;
Expand All @@ -82,7 +78,7 @@ public CelluralAutomaton(
Space = space;
_lastStepSpace = space.Clone();

_executor = new SimulationExecutor(step);
_executor = executor;
}

public void NextStep()
Expand All @@ -91,9 +87,9 @@ public void NextStep()
_executor.NextState(Space, _lastStepSpace, _transition, _neighbourhood);
}

public void PopulateSimulation(int GrainsCount, int inclusionsCount, int minRadius, int maxRadius)
public void PopulateSimulation(int grainsCount, int inclusionsCount, int minRadius, int maxRadius)
{
Grains = _grainInitializer.Initialize(GrainsCount);
Grains = _grainInitializer.Initialize(grainsCount);
_grainSeeder.Seed(Space, Grains);
Inclusions = _inclusionInitializer.Initialize(inclusionsCount, minRadius, maxRadius);
_inclusionSeeder.Seed(Space, Inclusions);
Expand Down
98 changes: 98 additions & 0 deletions Model/CurvatureExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Model.Transition;

namespace Model
{
public class CurvatureExecutor : ISimulationExecutor
{
public string Name
{
get { return ToString(); }
set { }
}


public int Step { get; private set; }

public CurvatureExecutor()
{
}

public CurvatureExecutor(int step)
{
if (step < 0) throw new ArgumentException();
Step = step;
}

public int ReturnStep()
{
return Step;
}

public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood)
{
for (int i = 0; i < space.GetXLength(); i++)
{
for (int j = 0; j < space.GetYLength(); j++)
{
// TODO: refactor, injected arguments are not used !!
IBoundaryCondition boun = new AbsorbingBoundary();
INeighbourhood nei = new MooreNeighbourhood(new AbsorbingBoundary());
ITransitionRule rule = new RuleOne();
Cell[] neighbours = nei.GetNeighbours(lastSpace, i, j);
var element = rule.NextState(space.GetCell(i, j), neighbours);

if (element != null)
{
space.SetCellMembership(element, i, j);
continue;
}

nei = new VonNeumanNeighbourhood(new AbsorbingBoundary());
rule = new RuleTwo();
neighbours = nei.GetNeighbours(lastSpace, i, j);
element = rule.NextState(space.GetCell(i, j), neighbours);

if (element != null)
{
space.SetCellMembership(element, i, j);
continue;
}

nei = new FurtherMooreNeighbourhood(new AbsorbingBoundary());
rule = new RuleTwo();
neighbours = nei.GetNeighbours(lastSpace, i, j);
element = rule.NextState(space.GetCell(i, j), neighbours);

if (element != null)
{
space.SetCellMembership(element, i, j);
continue;
}

nei = new FurtherMooreNeighbourhood(new AbsorbingBoundary());
rule = new RuleFour();
neighbours = nei.GetNeighbours(lastSpace, i, j);
element = rule.NextState(space.GetCell(i, j), neighbours);
space.SetCellMembership(element, i, j);

}
}
Step++;
}

public void Reset()
{
Step = 0;
throw new NotImplementedException();
}

public override string ToString()
{
return "CurvatureExecutor";
}
}
}
14 changes: 14 additions & 0 deletions Model/ISimulationExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;

namespace Model
{
public interface ISimulationExecutor
{

void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood);
int ReturnStep();
}
}
4 changes: 1 addition & 3 deletions Model/Microelements/InclusionExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Model.Transition;

namespace Model
{
Expand Down
79 changes: 79 additions & 0 deletions Model/Neighbourhood/FurtherMooreNeighbourhood.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;

namespace Model{
/// Further Moore checks NE, SE, SW, NW neighbours and return as Cell[]
/// |X|_|X|
/// |_|c|_|
/// |X|_|X|

public class FurtherMooreNeighbourhood : INeighbourhood
{
private IBoundaryCondition _boundary;


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


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


public Cell[] GetNeighbours(CelluralSpace space, int x, int y){
Cell[] result = new Cell[4];

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

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

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

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

return result;

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


}
2 changes: 1 addition & 1 deletion Model/Neighbourhood/INeighbourhood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Model{

public interface INeighbourhood //TODO: This code probably probably won't be use, to be deleted ! THere is static class instead
{
public Cell[] GetNeighbours(CelluralSpace space, int x, int y);
Cell[] GetNeighbours(CelluralSpace space, int x, int y);
}

}
Loading

0 comments on commit 6fe2a96

Please sign in to comment.