Skip to content

Commit

Permalink
Merge pull request #23 from maslankam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
maslankam authored Jan 15, 2020
2 parents 4077b7c + 97242fb commit 9ef59c1
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 87 deletions.
2 changes: 2 additions & 0 deletions Model/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public Cell(Microelement microelement)
MicroelementMembership = microelement;
}

public bool isBorder;

}


Expand Down
14 changes: 9 additions & 5 deletions Model/CellularAutomaton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CellularAutomaton(int size,
_inclusionSeeder = new InclusionSeeder(boundary);

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

//Constructor for opening saved state
Expand Down Expand Up @@ -85,15 +85,19 @@ public CellularAutomaton(
_executor = executor;
}

public void NextStep()
public void NextStep(int currentPhase)
{
_lastStepSpace = Space.Clone();
_executor.NextState(Space, _lastStepSpace, _transition, _neighbourhood);
_executor.NextState(Space, _lastStepSpace, _transition, _neighbourhood, currentPhase);
}

private void PopulateSimulation(int grainsCount, int inclusionsCount, int minRadius, int maxRadius)
public void PopulateSimulation(int grainsCount, int inclusionsCount, int minRadius, int maxRadius, int grainPhase = 0 )
{
Grains = _grainInitializer.Initialize(grainsCount);
Grains = _grainInitializer.Initialize(grainsCount, grainPhase);
foreach(var g in Grains)
{
g.OwnerAutomaton = this;
}
_grainSeeder.Seed(Space, Grains);
Inclusions = _inclusionInitializer.Initialize(inclusionsCount, minRadius, maxRadius);
_inclusionSeeder.Seed(Space, Inclusions);
Expand Down
16 changes: 5 additions & 11 deletions Model/CelluralSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ public Cell GetCell(int x, int y){
public void SetCellMembership(Microelement element,int x, int y)
{
_space[x, y].MicroelementMembership = element;
}

/*public CelluralSpace Clone(){ //copy cell reference
var newCells = new Cell[Size, Size];
for (int i = 0; i < _space.GetLength(0); i++)
if(element != null)
{
for (int j = 0; j < _space.GetLength(1); j++)
{
newCells[i, j] = _space[i, j];
}
element.Members.Add(_space[x, y]);
}
return new CelluralSpace(newCells);
}*/


}

public CelluralSpace Clone() //copy membership reference
{
Expand Down
9 changes: 7 additions & 2 deletions Model/Executors/CurvatureExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public int ReturnStep()
return Step;
}

public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood)
public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood, int currentPhase)
{
for (int i = 0; i < space.GetXLength(); i++)
{
for (int j = 0; j < space.GetYLength(); j++)
{
// TODO: refactor, injected arguments are not used !!
var phase = lastSpace.GetCell(i,j)?.MicroelementMembership.Phase ?? -2;
if(phase == currentPhase)
{
// TODO: refactor, injected arguments are not used !!
INeighbourhood nei = new MooreNeighbourhood(new AbsorbingBoundary());
ITransitionRule rule = new RuleOne();
Cell[] neighbours = nei.GetNeighbours(lastSpace, i, j);
Expand Down Expand Up @@ -76,6 +79,8 @@ public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionR
neighbours = nei.GetNeighbours(lastSpace, i, j);
element = ruleFour.NextState(space.GetCell(i, j), neighbours);
space.SetCellMembership(element, i, j);
}


}
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Executors/ISimulationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Model.Executors
public interface ISimulationExecutor
{

void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood);
void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood, int currentPhase);
int ReturnStep();
}
}
40 changes: 37 additions & 3 deletions Model/Executors/SimulationExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Model.Neighbourhood;
using Model.Transition;
using System.Linq;

namespace Model.Executors{
public class SimulationExecutor : ISimulationExecutor
Expand All @@ -23,13 +24,46 @@ public int ReturnStep()
return Step;
}

public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood){
public void NextState(CelluralSpace space, CelluralSpace lastSpace, ITransitionRule transition, INeighbourhood neighbourhood, int currentPhase){
for (int i = 0; i < space.GetXLength(); i++)
{
for (int j = 0; j < space.GetYLength(); j++)
{
Cell[] neighbours = neighbourhood.GetNeighbours(lastSpace, i, j); //TODO: Storing neighbourhood in Cell object will increase memory consumption
var element = transition.NextState(space.GetCell(i,j), neighbours); // but may increase performance

Microelements.Microelement element;
var phase = lastSpace.GetCell(i,j)?.MicroelementMembership?.Phase ?? -2;

Cell[] neighbours = neighbourhood.GetNeighbours(lastSpace, i, j); //TODO: Storing neighbourhood in Cell object will increase memory consumption


int neigboursSameCount = 0;

foreach(var n in neighbours)
{
bool isThisSame = Object.ReferenceEquals(n?.MicroelementMembership, lastSpace.GetCell(i, j)?.MicroelementMembership);
if (isThisSame)
{
neigboursSameCount++;
}

}

if(neigboursSameCount == neighbours.Count())
{
space.GetCell(i, j).isBorder = false;
}
else
{
space.GetCell(i, j).isBorder = true;
}


var phaseNeighbours = (from n in neighbours
where n?.MicroelementMembership?.Phase == currentPhase
select n).ToArray();

element = transition.NextState(space.GetCell(i, j), phaseNeighbours); // but may increase performance

space.SetCellMembership(element, i, j);
}
}
Expand Down
38 changes: 34 additions & 4 deletions Model/Microelements/Grain.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
using System.Drawing;
using System.Collections.Generic;
using Model.Neighbourhood;
using System.Linq;


namespace Model.Microelements{

public class Grain : Microelement
{
public sealed override int? Phase{get; set;}
public sealed override Color Color {get; set;}
public sealed override int Id{get; set;}
public bool GenerateDetails { get; set; }
public CellularAutomaton OwnerAutomaton { get; set; }
public sealed override List<Cell> Members { get; set; }
public sealed override int? Phase { get; set; }
public sealed override Color Color { get; set; }
public sealed override int Id { get; set; }

public string Info
{
get => GenerateDetails ? $"Grain Id:{Id}, Phase:{Phase}, Area: {Area}, Border:{Border}"
: $"Grain Id:{Id}, Phase:{Phase}";
}

public int Area { get => Members.Count; }
public int Border { get => (Members.Count((n) => n.isBorder == true)); }

public Grain(int id, int phase, Color color)
{
Id = id;
Color = color;
Phase = phase;
Members = new List<Cell>();
GenerateDetails = false;
}

public override void Delete()
{
foreach (var member in Members)
{
member.MicroelementMembership = null;

}
}



}

}
4 changes: 2 additions & 2 deletions Model/Microelements/GrainInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Model.Microelements
{
public class GrainInitializer
{
public List<Grain> Initialize(int count){
public List<Grain> Initialize(int count, int phase = 0){
var grains = new List<Grain>();
var r = new Random();

for(int i = 0; i < count; i++){
//Check if cell is empty?? with while loop?
grains.Add( new Grain(i, 0, Color.FromArgb(0, r.Next(0,255), r.Next(0,255), r.Next(0,255))) ); //TODO: Use Utility.IColorGenerator
grains.Add( new Grain(i, phase, Color.FromArgb(0, r.Next(0,255), r.Next(0,255), r.Next(0,255))) ); //TODO: Use Utility.IColorGenerator
} // Fixed 0 phase !

return grains;
Expand Down
10 changes: 8 additions & 2 deletions Model/Microelements/GrainSeeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public class GrainSeeder
public void Seed(CelluralSpace space, List<Grain> grains){
var r = new Random();
foreach(var grain in grains){
var x = r.Next(0, space.GetXLength() - 1);
var y = r.Next(0, space.GetYLength() - 1);

int x,y;
do
{ //TODO: infinite loop
x = r.Next(0, space.GetXLength() - 1);
y = r.Next(0, space.GetYLength() - 1);
}while(space.GetCell(x,y).MicroelementMembership != null);

space.SetCellMembership(grain, x, y);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Model/Microelements/Inclusion.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Collections.Generic;
using System.Drawing;

namespace Model.Microelements
{
Expand All @@ -9,13 +10,15 @@ public class Inclusion : Microelement

public sealed override Color Color {get; set;}
public sealed override int Id{get; set;}
public sealed override List<Cell> Members { get; set; }

public Inclusion(int id, int phase, int radius, Color color)
{
Id = id;
Color = color;
Phase = phase;
Radius = radius;
Members = new List<Cell>();
}

}
Expand Down
2 changes: 1 addition & 1 deletion Model/Microelements/InclusionInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public List<Inclusion> Initialize(int count, int minRadius, int maxRadius){

for(int i = 0; i < count; i++){
var col = Color.FromArgb(0, 0, 0, 0);
inclusions.Add( new Inclusion(i, 1, r.Next(minRadius, maxRadius), col)); //TODO: Use Utility.IColorGenerator
inclusions.Add( new Inclusion(i, -1, r.Next(minRadius, maxRadius), col)); //TODO: Use Utility.IColorGenerator
} // 1 is phase, for this momemnt no use of it

return inclusions;
Expand Down
21 changes: 20 additions & 1 deletion Model/Microelements/Microelement.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Drawing;

namespace Model.Microelements{
Expand All @@ -8,7 +9,25 @@ public abstract class Microelement
public abstract Color Color {get; set;}
public abstract int Id {get; set;}


public abstract List<Cell> Members { get; set; }

public virtual void Delete()
{

}

public virtual void GetArea()
{

}

public virtual void GetBorderLenght()
{

}



}

}
2 changes: 1 addition & 1 deletion Model/Transition/InclusionGrowthRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Microelement NextState(Cell cell, Cell[] neighbours)
var inclusions = from c in neighbours
where c?.MicroelementMembership?.Id != null
&& c.MicroelementMembership is Inclusion
select (Inclusion)c.MicroelementMembership;
select (Inclusion)c.MicroelementMembership;
var groups = (from i in inclusions
where i.Radius > _step
group i by i.Id).ToArray();
Expand Down
2 changes: 1 addition & 1 deletion Model/Transition/RuleFour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Microelement NextState(Cell cell, Cell[] neighbours){
{
var groups = (from c in neighbours
where c?.MicroelementMembership?.Id != null && c.MicroelementMembership is Grain
group c by c.MicroelementMembership).ToArray();
group c by c.MicroelementMembership).ToArray();

if (!groups.Any())
{
Expand Down
Loading

0 comments on commit 9ef59c1

Please sign in to comment.