├── Kernel ├── ABMS │ ├── Context │ │ └── Identifiable.cs │ └── Space │ │ ├── Grid │ │ ├── GridAdder.cs │ │ ├── Grid1d.cs │ │ └── Grid2d.cs │ │ └── Projection │ │ ├── IAdder.cs │ │ └── IProjection.cs ├── LSystems │ ├── RewriteRule.cs │ ├── ParametricModule.cs │ ├── LParameter.cs │ ├── OLProductionRule.cs │ ├── SymbolMapping.cs │ ├── Symbol.cs │ ├── ProductionRule.cs │ ├── AbstractStringRewritingSystem.cs │ ├── SymbolMap.cs │ ├── DeterministicLSystem.cs │ ├── Word.cs │ ├── ILProductionRule.cs │ ├── LSystemParser.cs │ └── StochasticLSystem.cs ├── CellularAutomata │ ├── Cells │ │ ├── CellularRule.cs │ │ ├── ICellState.cs │ │ ├── CellState.cs │ │ ├── ICell.cs │ │ ├── CellWrapper.cs │ │ └── Cell.cs │ ├── Configuration │ │ ├── ICAConfig.cs │ │ ├── RandomCAConfig.cs │ │ ├── CAConfigParser.cs │ │ └── CAConfig.cs │ ├── Impl │ │ ├── Greenberg_Hastings │ │ │ ├── ExcitedTransition.cs │ │ │ ├── GreenberHastinsStates.cs │ │ │ └── ExcitableCell.cs │ │ ├── Life │ │ │ ├── LifeLikeRule.cs │ │ │ ├── NeighborhoodEvolutionRule.cs │ │ │ ├── SurviveRule.cs │ │ │ ├── BornRule.cs │ │ │ └── LifeLikeCell.cs │ │ └── Elementary │ │ │ └── ElementaryRule.cs │ ├── Neighborhood │ │ ├── INeighborhoodStrategy.cs │ │ └── MooreNeighborhood.cs │ ├── CellularSpace │ │ ├── SimpleCellularGridBuilder.cs │ │ └── CellularGridBuilder.cs │ └── CA.cs ├── Systems │ └── DynamicalSystems │ │ ├── DynamicalSystem.cs │ │ ├── TimedState.cs │ │ ├── DiscreteTimer.cs │ │ └── TimedMemory.cs ├── Automata │ └── FSM │ │ ├── State.cs │ │ ├── IStateConfig.cs │ │ ├── TransitionRule.cs │ │ ├── FiniteStateMachine.cs │ │ └── InternalMemory.cs ├── RLogo │ ├── Instructions │ │ ├── Instruction.cs │ │ └── Expressions │ │ │ └── Expression.cs │ ├── TurtleGraphics │ │ ├── Commands │ │ │ ├── PopStateTurtleCommand.cs │ │ │ ├── CopyGeometryTurtleCommand.cs │ │ │ ├── TurnLeftTurtleCommand.cs │ │ │ ├── PitchUpTurtleCommand.cs │ │ │ ├── PushStateTurtleCommand.cs │ │ │ ├── RollRightTurtleCommand.cs │ │ │ ├── TurnRightTurtleCommand.cs │ │ │ ├── RollLeftTurtleCommand.cs │ │ │ ├── TurnAroundTurtleCommand.cs │ │ │ ├── PitchDownTurtleCommand.cs │ │ │ ├── MoveForwardTurtleCommand.cs │ │ │ ├── ScaleStepLengthTurtleCommand.cs │ │ │ ├── MoveForwardAndDrawTurtleCommand.cs │ │ │ ├── ReverseHeadingTurtleCommand.cs │ │ │ ├── ScaleThicknessTurtleCommand.cs │ │ │ ├── TurtleCommand.cs │ │ │ ├── EmptyTurtleCommand.cs │ │ │ └── TurtleCommandFactory.cs │ │ ├── Pen.cs │ │ ├── TurtleState.cs │ │ └── Canvas.cs │ ├── Context.cs │ ├── GrammarElement.cs │ ├── Parser │ │ ├── RLogoParser.cs │ │ └── StandardRLogoParser.cs │ └── RLogoInterpreter.cs ├── Language │ ├── IParser.cs │ ├── ILexer.cs │ ├── ITokenStream.cs │ ├── ListTokenStream.cs │ ├── Token.cs │ └── SimpleLexer.cs ├── Utils │ └── PresentationUtils.cs └── Geometry │ └── Util │ ├── PointUtils.cs │ └── SurfaceUtils.cs ├── references └── readme.txt ├── .gitignore ├── README.md ├── GH ├── LSystems │ ├── Component_LSBase.cs │ ├── GH_TubeSettings.cs │ ├── Component_TubeSettings.cs │ └── Component_LSystem.cs ├── CellularAutomata │ ├── GH_PointUtils.cs │ ├── Component_CABase.cs │ ├── OnStateConfig.cs │ ├── GH_TypeUtils.cs │ ├── Component_SurviveRule_OBSOLETE.cs │ ├── Component_BornRule_OBSOLETE.cs │ ├── Component_DiscreteTime.cs │ ├── GH_CellState.cs │ ├── Component_CAEvolver.cs │ ├── Component_CustomStateConfig.cs │ ├── Component_RandomStateConfig.cs │ ├── Component_ExcitableCell.cs │ ├── OnCellularGridBuilder.cs │ ├── Component_LifeLikeCell.cs │ └── Component_ElementaryCell.cs └── Component_RabbitBase.cs ├── Rabbit.sln └── Properties └── AssemblyInfo.cs /Kernel/ABMS/Context/Identifiable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.ABMS.Context 6 | { 7 | public interface Identifiable 8 | { 9 | Object GetId(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /references/readme.txt: -------------------------------------------------------------------------------- 1 | Copy reference DLL and XML files to this directory. 2 | Rabbit is currently compiled against Rhinoceros 4. 3 | 4 | - GH_IO.dll 5 | - GH_IO.xml 6 | - Grasshopper.dll 7 | - Grasshopper.xml 8 | - RhinoCommon.dll 9 | - RhinoCommon.xml 10 | - Rhino_DotNET.dll 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Rabbit.csproj.user 2 | Rabbit.suo 3 | /bin 4 | /obj 5 | /references/GH_IO.dll 6 | /references/GH_IO.xml 7 | /references/Grasshopper.dll 8 | /references/Grasshopper.xml 9 | /references/RhinoCommon.xml 10 | /references/RhinoCommon.dll 11 | /references/Rhino_DotNET.dll 12 | /*.suo -------------------------------------------------------------------------------- /Kernel/LSystems/RewriteRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | public abstract class RewriteRule 8 | { 9 | 10 | //public abstract String Evaluate(Object context); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/CellularRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Cells 8 | { 9 | public interface CellularRule:TransitionRule 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Kernel/Systems/DynamicalSystems/DynamicalSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Systems.DynamicalSystems 6 | { 7 | public interface DynamicalSystem 8 | { 9 | 10 | //State GetCurrentState(); 11 | 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Kernel/Automata/FSM/State.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Automata.FSM 6 | { 7 | /** 8 | * Abstraction of a State of a Finite State Machine. 9 | * Tagging interface. 10 | * 11 | */ 12 | public interface State 13 | { 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Kernel/RLogo/Instructions/Instruction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.RLogo.Instructions 6 | { 7 | public class Instruction: GrammarElement 8 | { 9 | public override Object evaluate(Context c) 10 | { 11 | return null; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Kernel/Language/IParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | public interface IParser 8 | { 9 | 10 | /** 11 | * Parses a stream of tokens into an Abstract Syntax Tree 12 | * 13 | * 14 | */ 15 | Object Parse(ITokenStream tokenStream); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/ICellState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | 7 | 8 | namespace Rabbit.Kernel.CellularAutomata.Cells 9 | { 10 | /** 11 | * Tagging interface. 12 | * Abstraction of a state of a cell. 13 | * 14 | */ 15 | public interface ICellState:State 16 | { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Configuration/ICAConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Configuration 8 | { 9 | public interface ICAConfig:IStateConfig 10 | { 11 | 12 | IEnumerable GetCells(); 13 | 14 | int GetAssociatedTime(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Kernel/Systems/DynamicalSystems/TimedState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Systems.DynamicalSystems 6 | { 7 | public interface TimedState 8 | { 9 | /* 10 | * returns the exact time at which this state was produced 11 | */ 12 | int GetDiscreteTime(); 13 | 14 | void SetTime(int time); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rabbit addon for Grasshopper by Morphocode 2 | ========================================== 3 | RABBIT is a plug-in for McNeel’s Grasshopper that simulates biological and physical processes. 4 | 5 | Rabbit provides an easy way to explore natural phenomena such as pattern formation, self-organization and emergence. The add-on gives architects and designers the opportunity to integrate these models of organization in their own designs. 6 | 7 | [Learn More](http://morphocode.com/rabbit/) 8 | 9 | 10 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/PopStateTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using RMA.OpenNURBS; 2 | 3 | using Rabbit.Kernel.TurtleGraphics; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics.Commands 6 | { 7 | /// 8 | /// Restores the last saved turtle state 9 | /// 10 | public class PopStateTurtleCommand : TurtleCommand 11 | { 12 | public void Execute(Turtle3d Turtle) 13 | { 14 | Turtle.PopState(); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/CopyGeometryTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using RMA.OpenNURBS; 2 | 3 | using Rabbit.Kernel.TurtleGraphics; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics.Commands 6 | { 7 | /// 8 | /// Restores the last saved turtle state 9 | /// 10 | public class CopyGeometryTurtleCommand : TurtleCommand 11 | { 12 | public void Execute(Turtle3d turtle) 13 | { 14 | turtle.CopyGeometry("J"); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/TurnLeftTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle turn left 10 | /// 11 | public class TurnLeftTurtleCommand : TurtleCommand { 12 | public void Execute(Turtle3d Turtle) 13 | { 14 | Turtle.TurnLeft(); 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Greenberg_Hastings/ExcitedTransition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Impl.Greenberg_Hastings 8 | { 9 | public class ExcitedTransition:TransitionRule 10 | { 11 | 12 | public CellState SolveState(CellState currentState, Object input) 13 | { 14 | throw new NotImplementedException(); 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/PitchUpTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle turn left 10 | /// 11 | public class PitchUpTurtleCommand : TurtleCommand 12 | { 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | Turtle.PitchUp(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/Language/ILexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | 8 | /** 9 | * A Lexer(a.k.a.) Scanner separates a given input on tokens. 10 | * A lexer is part of the analysis of a language. 11 | * The lexer separates the content on tokens which could be used by a Parser. 12 | * 13 | * 14 | */ 15 | public interface ILexer 16 | { 17 | 18 | ITokenStream Analyze(String input); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/PushStateTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// Push the state of the turtle in the states stack 10 | /// 11 | public class PushStateTurtleCommand : TurtleCommand 12 | { 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | Turtle.PushState(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/RollRightTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle roll left 10 | /// 11 | public class RollRightTurtleCommand : TurtleCommand { 12 | 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | Turtle.RollRight(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/TurnRightTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle turn right 10 | /// 11 | public class TurnRightTurtleCommand : TurtleCommand 12 | { 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | Turtle.TurnRight(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/RollLeftTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle turn left 10 | /// 11 | public class RollLeftTurtleCommand : TurtleCommand 12 | { 13 | 14 | public void Execute(Turtle3d Turtle) 15 | { 16 | Turtle.RollLeft(); 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/TurnAroundTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle turn right 10 | /// 11 | public class TurnAroundTurtleCommand : TurtleCommand { 12 | 13 | public void Execute(Turtle3d turtle) 14 | { 15 | turtle.TurnAround(); 16 | } 17 | 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/PitchDownTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turtle pitch down. Rotation along the Y Axis 10 | /// 11 | public class PitchDownTurtleCommand : TurtleCommand 12 | { 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | Turtle.PitchDown(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/MoveForwardTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turle move one step forward 10 | /// 11 | public class MoveForwardTurtleCommand : TurtleCommand 12 | { 13 | 14 | public virtual void Execute(Turtle3d turtle) 15 | { 16 | turtle.MoveForward(); 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Greenberg_Hastings/GreenberHastinsStates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.CellularAutomata; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Impl.Greenberg_Hastings 8 | { 9 | public class GreenberHastinsStates 10 | { 11 | 12 | public static CellState RestingState = new CellState("Resting"); 13 | public static CellState ExcitedState = new CellState("Excited"); 14 | public static CellState RecoveryState = new CellState("Recovery"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Neighborhood/INeighborhoodStrategy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.CellularAutomata 6 | { 7 | /** 8 | * Abstraction of a strategy that builds neighborhoods of cells 9 | * 10 | */ 11 | public interface INeighborhoodStrategy 12 | { 13 | 14 | /** 15 | * the list of neighbors of the specified cell 16 | * 17 | */ 18 | IList BuildNeighborhood(ICell cell); 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Pen.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics 6 | { 7 | /** 8 | * The pen that is being hold by the turtle. Has attributes such as color, weight, etc 9 | */ 10 | public class Pen 11 | { 12 | //private Color strokeColor; 13 | private float strokeWidth; 14 | 15 | //private Color fillColor; 16 | private float fillWidth; 17 | 18 | public Pen() 19 | { 20 | //.... 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/ScaleStepLengthTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using Rhino.Geometry; 2 | 3 | using Rabbit.Kernel.TurtleGraphics; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics.Commands 6 | { 7 | /// 8 | /// A Turtle commands that makes the turle move one step forward and draw a line while moving 9 | /// 10 | public class ScaleStepLengthTurtleCommand : TurtleCommand 11 | { 12 | 13 | 14 | public void Execute(Turtle3d turtle) 15 | { 16 | turtle.ScaleStepLength(turtle.GetStepLengthScale()); 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kernel/ABMS/Space/Grid/GridAdder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.ABMS.Space.Projection; 6 | 7 | namespace Rabbit.Kernel.ABMS.Space.Grid 8 | { 9 | public class GridAdder:IAdder.Location, O> 10 | { 11 | 12 | private Grid2d grid; 13 | 14 | public GridAdder(Grid2d grid) 15 | { 16 | this.grid = grid; 17 | 18 | } 19 | 20 | 21 | public void Add(Grid2d.Location location, O o) { 22 | //grid.Add(o, location); 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/MoveForwardAndDrawTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | //using RMA.OpenNURBS; 2 | using Rhino.Geometry; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turle move one step forward and draw a line while moving 10 | /// 11 | public class MoveForwardAndDrawTurtleCommand : MoveForwardTurtleCommand 12 | { 13 | public override void Execute(Turtle3d turtle) 14 | { 15 | turtle.MoveForwardAndDraw(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/ReverseHeadingTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using RMA.OpenNURBS; 2 | using System; 3 | 4 | using Rabbit.Kernel.TurtleGraphics; 5 | 6 | namespace Rabbit.Kernel.TurtleGraphics.Commands 7 | { 8 | /// 9 | /// A Turtle commands that makes the turle reverse it's heading 10 | /// 11 | public class ReverseHeadingTurtleCommand : TurtleCommand 12 | { 13 | public void Execute(Turtle3d Turtle) 14 | { 15 | //Turtle.GetTurtleState().GetHeading().Reverse(); 16 | throw new NotImplementedException(); 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Kernel/ABMS/Space/Projection/IAdder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.ABMS.Space.Projection 6 | { 7 | /** 8 | * 9 | * Interface for classes that wish to add objects to a space. An adder is used by setting it on a space. The space then will use whatever strategy is defined by the Adder to add objects to itself. For example, a random grid adder may add objects at random locations on a grid. 10 | * 11 | * 12 | */ 13 | public interface IAdder 14 | { 15 | 16 | void Add(D destination, O o); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/ScaleThicknessTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using Rhino.Geometry; 2 | 3 | using Rabbit.Kernel.TurtleGraphics; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics.Commands 6 | { 7 | /// 8 | /// A Turtle commands that makes the turle move one step forward and draw a line while moving 9 | /// 10 | public class ScaleThicknessTurtleCommand : TurtleCommand 11 | { 12 | public void Execute(Turtle3d turtle) 13 | { 14 | //scales the thickness using the default thickness scale 15 | turtle.ScaleThickness(); 16 | } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Kernel/Automata/FSM/IStateConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | 6 | namespace Rabbit.Kernel.Automata.FSM 7 | { 8 | /** 9 | * Contains pairs of (StateMachine, State) 10 | * Saves the states of a set of StateMachines 11 | * 12 | * 13 | */ 14 | public interface IStateConfig where S : State 15 | 16 | { 17 | 18 | /** 19 | * the state of the specified state machine 20 | * 21 | */ 22 | S GetCellState(F finiteStateMachine); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Kernel/Automata/FSM/TransitionRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Automata.FSM 6 | { 7 | /** 8 | * Abstraction of a rule that is used by the Finite State Machine to change it's state. 9 | * 10 | * I is a type parameter that specify what input is expected by this Finite State Machine 11 | */ 12 | public interface TransitionRule //where S:State 13 | { 14 | 15 | /** 16 | * The next state 17 | * The next state depends on the current state and on the input(symbol) 18 | */ 19 | S SolveState(S currentState, Object input); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/TurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using Rabbit.Kernel.TurtleGraphics; 2 | 3 | namespace Rabbit.Kernel.TurtleGraphics.Commands 4 | { 5 | /// 6 | /// A TurtleCommand is an abstraction of a command that drives the Turtle. 7 | /// Every operation that a Trutle does is implemented as a Command: MoveForwardCommand, TurnLeft Command, etc. 8 | /// This gives the flexibility of implementing new commands in a generic way. 9 | /// 10 | public interface TurtleCommand 11 | { 12 | /// 13 | /// Does the job. Drives the turtle in a specific way 14 | /// 15 | void Execute(Turtle3d Turtle); 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Kernel/ABMS/Space/Projection/IProjection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.ABMS.Space.Projection 6 | { 7 | /** 8 | * A projection defines the relationships between objects. 9 | * Implementations are quite different: grid, network, etc.. 10 | * 11 | */ 12 | public interface IProjection 13 | { 14 | 15 | /** 16 | * All cells contained on the lattice 17 | */ 18 | IList GetObjects(); 19 | 20 | 21 | 22 | /** 23 | * The cell identified with that id 24 | */ 25 | //O GetAgent(Object id); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Kernel/Language/ITokenStream.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | /*** 8 | * 9 | * The beauty of the token stream concept is that parsers and lexers are not affected--they are merely consumers and producers of streams. Stream objects are filters that produce, process, combine, or separate token streams for use by consumers. Existing lexers and parsers may be combined in new and interesting ways without modification. 10 | * http://www.antlr2.org/doc/streams.html 11 | * 12 | * 13 | * 14 | */ 15 | public interface ITokenStream 16 | { 17 | 18 | Token NextToken(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Kernel/Language/ListTokenStream.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | public class ListTokenStream: ITokenStream 8 | { 9 | private IList tokens; 10 | 11 | private IEnumerator enumerator; 12 | 13 | public ListTokenStream(IList tokens) 14 | { 15 | this.tokens = tokens; 16 | this.enumerator = tokens.GetEnumerator(); 17 | enumerator.Reset(); 18 | } 19 | 20 | public Token NextToken() 21 | { 22 | enumerator.MoveNext(); 23 | return enumerator.Current; 24 | } 25 | 26 | 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Kernel/RLogo/Instructions/Expressions/Expression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.RLogo.Instructions.Expressions 6 | { 7 | /** 8 | * the base class of all expressions, 9 | * the {@link #evaluate evaluate} method uses 10 | * the hook/template method 11 | * {@link #value value} to return a Double value, 12 | * the template method returns a double. 13 | */ 14 | 15 | public abstract class Expression: Instruction 16 | { 17 | public Object evaluate(Context c) 18 | { 19 | return (Double)value(c); 20 | } 21 | 22 | public abstract double value(Context c); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/EmptyTurtleCommand.cs: -------------------------------------------------------------------------------- 1 | using RMA.OpenNURBS; 2 | 3 | using Rabbit.Kernel.TurtleGraphics; 4 | 5 | namespace Rabbit.Kernel.TurtleGraphics.Commands 6 | { 7 | /// 8 | /// A Turtle commands that does nothing. Keeps the consistency of the API. Used in a collection of commands instead of Null 9 | /// 10 | public class EmptyTurtleCommand : TurtleCommand 11 | { 12 | public static EmptyTurtleCommand INSTANCE = new EmptyTurtleCommand(); 13 | 14 | //Could not be initialized from outside. Use the Single Instance 15 | private EmptyTurtleCommand() { } 16 | 17 | public void Execute(Turtle3d Turtle) 18 | { 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Kernel/RLogo/Context.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.RLogo 6 | { 7 | public interface Context 8 | { 9 | /** 10 | * If there local/global variables a variable might 11 | * have a different value depending on the Context, 12 | * so Contexts should support determining the value 13 | * of an identifier (this method may be superflous). 14 | * @param name is the identifier being evaluated in this Context 15 | * @return the value of the identifier in this Context 16 | */ 17 | //public double execute(BuiltInTurtleFunction b); 18 | 19 | //public void ask(Expression[] indices, StrictInstructionList sil); 20 | 21 | //public void tell(Expression[] indices); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /GH/LSystems/Component_LSBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Grasshopper.Kernel; 5 | using System.Drawing; 6 | using Rabbit.Properties; 7 | 8 | 9 | namespace Rabbit.GH.LSystems 10 | { 11 | /** 12 | * Abstract Base component for all components under the CellularAutomata category of Rabbit 13 | * 14 | * @author MORPHOCODE.COM 15 | */ 16 | public abstract class Component_LSBase : Component_RabbitBase 17 | { 18 | 19 | /** 20 | * Constructor 21 | */ 22 | public Component_LSBase(String name, String nick, String description) 23 | : base(name, nick, description, "L-Systems")//base(name, abbreviation, description, category, subcategory) 24 | { 25 | } 26 | 27 | 28 | 29 | 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Kernel/RLogo/GrammarElement.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.RLogo 6 | { 7 | /** 8 | * All elements of the elan language (e.g., 9 | * that make up an abstract syntax tree) should 10 | * extend GrammarElement. Evaluating a language 11 | * construct with a context returns the value of 12 | * the evaluation and may have side-effects. 13 | */ 14 | public abstract class GrammarElement 15 | { 16 | /** 17 | * Evaluate this language construct in some context. The 18 | * evaluation may have side-effects, e.g., moving a turtle. 19 | * @param is the context in which the evaluation takes place 20 | * @return the result of evaluating the construct 21 | */ 22 | public abstract Object evaluate(Context c); 23 | 24 | //static protected Map ourMap = new TreeMap(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Kernel/LSystems/ParametricModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /* 8 | * A parametric module is a symbol witch have some parameters attached. 9 | * There is a formal definition of the parametric module: for ex: M(t) 10 | * And real-value parametric module: M(10) 11 | * 12 | * Parametric modules could have a list of parameters: M(x, y, z) 13 | * 14 | * 15 | */ 16 | public class ParametricModule:Symbol 17 | { 18 | 19 | private String symbolString; 20 | private IList parameters; 21 | 22 | /** 23 | * Constructor 24 | * 25 | */ 26 | public ParametricModule(String symbolString, IList parameters):base(symbolString) 27 | { 28 | this.parameters = parameters; 29 | } 30 | 31 | 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /GH/CellularAutomata/GH_PointUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | //using RMA.OpenNURBS; 6 | using Rhino.Geometry; 7 | using Grasshopper.Kernel.Types; 8 | 9 | namespace Rabbit.GH.CellularAutomata 10 | { 11 | /** 12 | * Utility class that provides some common functionality related to GH to ON points converting 13 | * 14 | */ 15 | public class GH_PointUtils 16 | { 17 | 18 | private GH_PointUtils() { } 19 | 20 | 21 | /** 22 | * converts a list of GH_Points into a list of On3dPoint 23 | */ 24 | public static IList ConvertToOnPoints(IList ghPoints) 25 | { 26 | IList ONPoints = new List(ghPoints.Count); 27 | foreach (GH_Point ghPoint in ghPoints) 28 | ONPoints.Add(ghPoint.Value); 29 | 30 | return ONPoints; 31 | } 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Rabbit.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 2012 for Windows Desktop 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rabbit", "Rabbit.csproj", "{5162CB3D-3CB7-4A27-B72E-CF716F03668B}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {5162CB3D-3CB7-4A27-B72E-CF716F03668B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {5162CB3D-3CB7-4A27-B72E-CF716F03668B}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {5162CB3D-3CB7-4A27-B72E-CF716F03668B}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {5162CB3D-3CB7-4A27-B72E-CF716F03668B}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /GH/LSystems/GH_TubeSettings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Grasshopper.Kernel; 6 | using Grasshopper.Kernel.Data; 7 | using Grasshopper.Kernel.Types; 8 | 9 | using Rhino.Geometry; 10 | 11 | namespace Rabbit.GH.LSystems 12 | { 13 | /** 14 | * Encapsulates settins on how to loft the skeleton produced by the turtle. 15 | */ 16 | public class GH_TubeSettings 17 | { 18 | public double defaultThickness; 19 | public double defaultThicknessScale; 20 | public Curve profile; 21 | public Plane profilePivot; 22 | 23 | public GH_TubeSettings(double defaultThickness, double defaultThicknessScale, Curve profile, Plane profilePivot) { 24 | this.defaultThickness = defaultThickness; 25 | this.defaultThicknessScale = defaultThicknessScale; 26 | this.profile = profile; 27 | this.profilePivot = profilePivot; 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Kernel/LSystems/LParameter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * Parametric L-systems associate a vector of real-valued parameters with 9 | each symbol, collectively forming a parametric module. A module with symbol 10 | s ∈ V and parameters a1, a2, . . . , an ∈ R is written s(a1, a2, . . . , an). Strings of parametric modules form parametric words. It is important to differentiate 11 | the real-valued actual parameters of modules, from the formal parameters 12 | specified in productions. In practice, formal parameters are given unique3 13 | identifier names when specifying productions. 14 | * 15 | * 16 | * 17 | */ 18 | public class LParameter 19 | { 20 | private String identifier; 21 | 22 | public LParameter(String identifier) 23 | { 24 | this.identifier = identifier; 25 | } 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Kernel/RLogo/Parser/RLogoParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.TurtleGraphics; 6 | using Rabbit.Kernel.TurtleGraphics.Commands; 7 | 8 | namespace Rabbit.Kernel.RLogo.Parser 9 | { 10 | public class RLogoParser 11 | { 12 | 13 | protected TurtleCommandFactory turtleCommandFactory; 14 | 15 | /** 16 | * A list of commands derived from the specified String. Each character in the string is mapped to a command 17 | * 18 | */ 19 | public List Parse(String source) 20 | { 21 | List TurtleCommands = new List(); 22 | for (int i = 0; i < source.Length; i++) 23 | { 24 | Char nextSymbol = source[i]; 25 | TurtleCommands.Add(turtleCommandFactory.GetTurtleCommand(nextSymbol)); 26 | } 27 | return TurtleCommands; 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Kernel/Language/Token.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | public class Token 8 | { 9 | //TODO: extends with different types of Tokens? http://web.duke.edu/~mky/slogo/ 10 | 11 | 12 | private String tokenString; 13 | 14 | 15 | 16 | public Token(String tokenString) 17 | { 18 | this.tokenString = tokenString; 19 | } 20 | 21 | public String ToString() 22 | { 23 | return tokenString; 24 | } 25 | 26 | 27 | public override bool Equals(Object obj) 28 | { 29 | if (obj.GetType() != this.GetType()) return false;//TODO: this could break inheritance 30 | Token aToken = (Token)obj; 31 | 32 | return tokenString.Equals(aToken.ToString()); 33 | } 34 | 35 | public override int GetHashCode() 36 | { 37 | return tokenString.GetHashCode(); 38 | } 39 | 40 | 41 | //public enum TokenType { IDENTIFIER, NUMBER, }; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Kernel/LSystems/OLProductionRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * Context-Free production rule 9 | * 10 | */ 11 | public class OLProductionRule:ProductionRule 12 | { 13 | 14 | public OLProductionRule(Symbol predecessor, Word successor) : base(predecessor, successor) { } 15 | 16 | 17 | /** 18 | * Returns the successor string if the symbol is equal to the predecessor of the rule 19 | * returns null if rule could not be applied 20 | * 21 | * 22 | */ 23 | public override Word Rewrite(Symbol symbol, int SymbolIndex, Word word) 24 | { 25 | //check if that rule could be applied for the specified symbol 26 | if (predecessor.Equals(symbol)) 27 | return successor; 28 | else 29 | //throw new System.InvalidOperationException("This rule is not meant to be used with symbol: " + Symbol); 30 | return null; 31 | } 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Life/LifeLikeRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | using Rabbit.Kernel.CellularAutomata.Cells; 7 | 8 | namespace Rabbit.Kernel.CellularAutomata.Impl.Life 9 | { 10 | /** 11 | * The behavior of the cellular automata as a system is drivven by the behavior of each individual cell. 12 | * 13 | * A CellBehavior defines what the cell does in every generation. 14 | * The state of the cell changes as the evolution goes. 15 | * The cell acts according to some simple rules. 16 | * This class defines what are these rules and makes possible to combine multiple evolution rules 17 | * 18 | * A flyweight object. 19 | * 20 | * @author MORPHOCODE.COM 21 | */ 22 | public abstract class LifeLikeRule:CellularRule { 23 | 24 | public LifeLikeRule() 25 | { 26 | } 27 | 28 | public abstract CellState ApplyRule(LifeLikeCell cell); 29 | 30 | 31 | public abstract CellState SolveState(CellState currentState, Object input); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Life/NeighborhoodEvolutionRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.CellularAutomata; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Impl.Life 8 | { 9 | /** 10 | * EvolutionRule which change the state of the cell according to the states of it's neighbors. 11 | * 12 | * 13 | */ 14 | public abstract class NeighborhoodEvolutionRule:LifeLikeRule 15 | { 16 | 17 | public NeighborhoodEvolutionRule():base() { } 18 | 19 | 20 | /** 21 | * 22 | */ 23 | protected int getAliveCellsCount(IList cells) 24 | { 25 | int aliveCellsCount = 0; 26 | foreach (ICell cell in cells) 27 | if (((LifeLikeCell)cell).IsAlive()) 28 | aliveCellsCount++; 29 | 30 | return aliveCellsCount; 31 | } 32 | 33 | public override CellState SolveState(CellState cellState, Object neighbors) 34 | { 35 | throw new NotImplementedException(); 36 | } 37 | 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Kernel/LSystems/SymbolMapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * Maps a symbol to a concrete meaning. 9 | * The meaning is some kind of an object. A symbol could mean an integer, a solig, a curve, etc. 10 | * For ex A=10, or A=Solid, A=Curve, A=TurtleCommand, etc. 11 | * 12 | * 13 | */ 14 | public class SymbolMapping 15 | { 16 | //NOTE: for now juse use the simple SymbolMapper class 17 | //in the future The SymbolMapper could use SymbolMapping objects 18 | //for now just use a simple map 19 | 20 | private Symbol symbol; 21 | private Object meaning; 22 | 23 | public SymbolMapping(Symbol symbol, Object meaning) 24 | { 25 | this.symbol = symbol; 26 | this.meaning = meaning; 27 | } 28 | 29 | public Symbol GetSymbol() 30 | { 31 | return symbol; 32 | } 33 | 34 | public Object GetMeaning() 35 | { 36 | return meaning; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Kernel/Utils/PresentationUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using RMA.OpenNURBS; 6 | 7 | 8 | namespace Rabbit.Kernel.Util 9 | { 10 | /** 11 | * Utility class that provides some common functionality related to GH to ON points converting 12 | * 13 | */ 14 | public class PresentationUtils 15 | { 16 | 17 | private PresentationUtils() { } 18 | 19 | 20 | public static String ListToString(IList list) 21 | { 22 | if (list == null) throw new InvalidOperationException("Invalid list to format!"); 23 | int itemsCount = list.Count; 24 | StringBuilder sb = new StringBuilder(); 25 | int currentItemIndex = 0; 26 | foreach (T element in list) 27 | { 28 | sb.Append(element); 29 | currentItemIndex++; 30 | if (currentItemIndex < itemsCount - 1) 31 | sb.Append(","); 32 | else if (itemsCount>1 && currentItemIndex == itemsCount - 1) 33 | sb.Append(" or "); 34 | } 35 | 36 | return sb.ToString(); 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Kernel/ABMS/Space/Grid/Grid1d.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | using Rabbit.Kernel.ABMS.Space.Projection; 7 | using Rabbit.Kernel.CellularAutomata.Cells; 8 | using Rabbit.Kernel.CellularAutomata; 9 | 10 | 11 | 12 | namespace Rabbit.Kernel.ABMS.Space.Grid 13 | { 14 | public class Grid1d:IProjection 15 | { 16 | private int XDimension; 17 | 18 | private A[] cells; 19 | 20 | private IList cellList; 21 | 22 | public Grid1d(int XDimension) 23 | { 24 | this.XDimension = XDimension; 25 | cells = new A[XDimension]; 26 | cellList = new List(XDimension); 27 | } 28 | 29 | 30 | public void Add(A cell, int xPosition) 31 | { 32 | cells[xPosition] = cell; 33 | cellList.Add((A)cell); 34 | } 35 | 36 | public IList GetObjects() 37 | { 38 | return cellList; 39 | } 40 | 41 | public A GetObjectAt(int xPosition) 42 | { 43 | return cells[xPosition]; 44 | } 45 | 46 | 47 | public override String ToString() 48 | { 49 | return "1D Grid with XDimension=" + XDimension; 50 | } 51 | 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Kernel/RLogo/RLogoInterpreter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.RLogo.Parser; 6 | using Rabbit.Kernel.TurtleGraphics; 7 | 8 | using RMA.OpenNURBS; 9 | 10 | 11 | namespace Rabbit.Kernel.RLogo 12 | { 13 | /** 14 | * The TurtleInterpreter interprets a string(source code) written in a LOGO dialect(Turtle Language). 15 | * 16 | * The interpreter uses a Parser to parse the source into some kind of data structure(Parse tree of instructions) 17 | * 18 | * The Interpreter executes these instructions. 19 | * 20 | * 21 | * 22 | */ 23 | public class RLogoInterpreter 24 | { 25 | 26 | private RLogoParser _turtleParser; 27 | 28 | private Turtle3d _turtle; 29 | 30 | public RLogoInterpreter(Turtle3d turtle) 31 | { 32 | _turtleParser = new StandardRLogoParser(); 33 | _turtle = turtle; 34 | 35 | } 36 | 37 | public void Interpret(String source) 38 | { 39 | //this should be done in the Turtle command execute 40 | //the interpreter should init the context of the command 41 | _turtle.Do(_turtleParser.Parse(source));//TODO: check for arrayOfOfBounds exception here! 42 | } 43 | 44 | 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Kernel/Automata/FSM/FiniteStateMachine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Automata.FSM 6 | { 7 | /** 8 | * The FiniteStateMachine is an abstract machine called also finite state automaton. 9 | * FSMs have a state and a finite number of possible states. 10 | * FSM could change it's state according to a set of Transition rules. 11 | * The FSM is a controller, which controls the set of rules and the current state. 12 | * 13 | * 14 | */ 15 | public interface FiniteStateMachine //It is a good idea to extend from State interface for custom States 16 | where T:TransitionRule 17 | { 18 | 19 | 20 | /** 21 | * Calculates the next state of the FSM 22 | * according to the list of transition rules 23 | * 24 | */ 25 | S UpdateState(); 26 | 27 | /** 28 | * The list of finite states of the FSM 29 | */ 30 | IList GetFiniteStates(); 31 | 32 | /** 33 | * the current state of the FSM 34 | */ 35 | S GetState(); 36 | 37 | 38 | // TRANSITION RULES ------------------------------------------------ 39 | IList GetRules(); 40 | 41 | 42 | //void AddRule(TransitionRule rule); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_CABase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Grasshopper.Kernel; 5 | using System.Drawing; 6 | using Rabbit.Properties; 7 | using Rabbit.Kernel.CellularAutomata; 8 | 9 | namespace Rabbit.GH.CellularAutomata 10 | { 11 | /** 12 | * Abstract Base component for all components under the CellularAutomata category of Rabbit 13 | * 14 | * @author MORPHOCODE.COM 15 | */ 16 | public abstract class Component_CABase : Component_RabbitBase 17 | { 18 | 19 | protected static String CATEGORY_1D_CA = "1D CA"; 20 | protected static String CATEGORY_2D_CA = "2D CA"; 21 | protected static String CATEGORY_CA_MODEL = "Cellular Automata"; 22 | 23 | /** 24 | * Constructor 25 | */ 26 | protected Component_CABase(String name, String nick, String description) 27 | : this(name, nick, description, Component_CABase.CATEGORY_CA_MODEL)//base(name, abbreviation, description, category, subcategory) 28 | { 29 | } 30 | 31 | /** 32 | * Constructor 33 | */ 34 | protected Component_CABase(String name, String nick, String description, String category) 35 | : base(name, nick, description, category)//base(name, abbreviation, description, category, subcategory) 36 | { 37 | } 38 | 39 | 40 | 41 | 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /GH/CellularAutomata/OnStateConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | //using RMA.OpenNURBS; 6 | using Rhino.Geometry; 7 | 8 | using Rabbit.Kernel.Automata.FSM; 9 | 10 | using Rabbit.Kernel.CellularAutomata; 11 | using Rabbit.Kernel.CellularAutomata.Cells; 12 | 13 | namespace Rabbit.GH.CellularAutomata 14 | { 15 | /** 16 | * Specifies a custom state for a collection of points, which a resolved as cells by the OnCellularGridBuilder 17 | * 18 | * 19 | */ 20 | public class OnStateConfig 21 | { 22 | 23 | private IList configurationPoints; 24 | private CellState state; 25 | 26 | public OnStateConfig(IList configurationPoints, CellState state) 27 | { 28 | this.configurationPoints = configurationPoints; 29 | this.state = state; 30 | } 31 | 32 | public CellState GetState() 33 | { 34 | return state; 35 | } 36 | 37 | public IList GetPoints() 38 | { 39 | return configurationPoints; 40 | } 41 | 42 | public void SetPoints(IList points) 43 | { 44 | this.configurationPoints = points; 45 | } 46 | 47 | 48 | public override String ToString() 49 | { 50 | return "Custom State Configuration"; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Configuration/RandomCAConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | 6 | using Rabbit.Kernel.ABMS.Space.Grid; 7 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 8 | using Rabbit.Kernel.Util; 9 | 10 | namespace Rabbit.Kernel.CellularAutomata.Configuration 11 | { 12 | public class RandomCAConfig: ICAConfig 13 | { 14 | 15 | private Random randomGenerator; 16 | 17 | private IList states; 18 | 19 | public RandomCAConfig(IList states) { 20 | randomGenerator = new Random(); 21 | this.states = states; 22 | } 23 | 24 | public CellState GetCellState(ICell cell) 25 | { 26 | //CellState randomCellState; 27 | int randomIndex = randomGenerator.Next(0, states.Count); 28 | return states[randomIndex]; 29 | 30 | } 31 | 32 | public override String ToString() 33 | { 34 | return "Random State Configuration distributing randomly the following Cell states: " + PresentationUtils.ListToString(states); 35 | } 36 | 37 | public IEnumerable GetCells() 38 | { 39 | throw new SystemException("Not implemented!"); 40 | } 41 | 42 | public int GetAssociatedTime() 43 | { 44 | throw new SystemException("Not implemented!"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Commands/TurtleCommandFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.TurtleGraphics; 6 | 7 | namespace Rabbit.Kernel.TurtleGraphics.Commands 8 | { 9 | // 10 | // A mapping that maps a Symbol(Character) <-> Turtle Command 11 | // 12 | // Different symbols could have different commands associated, 13 | // thus each symbol have a different meaning for the turtle. 14 | // 15 | public class TurtleCommandFactory 16 | { 17 | 18 | protected Dictionary CommandsMap; 19 | 20 | /** 21 | * Constructor. 22 | */ 23 | public TurtleCommandFactory() 24 | { 25 | CommandsMap = new Dictionary(); 26 | } 27 | 28 | public void MapTurtleCommand(Char Char, TurtleCommand TurtleCommand) 29 | { 30 | CommandsMap.Add(Char, TurtleCommand); 31 | } 32 | 33 | /** 34 | * Returns the Command mapped to that Char 35 | * Null if there is no associated Command 36 | * 37 | */ 38 | public TurtleCommand GetTurtleCommand(Char Char) 39 | { 40 | if(CommandsMap.ContainsKey(Char)) 41 | return CommandsMap[Char]; 42 | else 43 | return EmptyTurtleCommand.INSTANCE;//do nothing, if a Char<->Command mapping is missing 44 | } 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Kernel/Systems/DynamicalSystems/DiscreteTimer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Systems.DynamicalSystems 6 | { 7 | public class DiscreteTimer 8 | { 9 | private int currentTime; 10 | 11 | public static DiscreteTimer Instance = new DiscreteTimer(); 12 | 13 | //a delegate type for the event must be declared, if none is already declared 14 | public delegate void TickTackEventHandler(object sender, EventArgs e); 15 | 16 | //the event itself 17 | public event TickTackEventHandler TickTackEvent; 18 | 19 | private DiscreteTimer() 20 | { 21 | currentTime = 0; 22 | } 23 | 24 | public int GetTime() 25 | { 26 | return currentTime; 27 | } 28 | 29 | public void SetTime(int time) 30 | { 31 | currentTime = time; 32 | FireTickTackEvent(EventArgs.Empty); 33 | } 34 | 35 | public void TickTack() 36 | { 37 | currentTime++; 38 | FireTickTackEvent(EventArgs.Empty); 39 | } 40 | 41 | public void Reset() 42 | { 43 | currentTime = 0; 44 | FireTickTackEvent(EventArgs.Empty); 45 | } 46 | 47 | 48 | private void FireTickTackEvent(EventArgs e) 49 | { 50 | if (TickTackEvent != null) 51 | TickTackEvent(this, e); 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Rabbit")] 9 | [assembly: AssemblyDescription("RABBIT: Tools for Grasshopper")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("MORPHOCODE.COM")] 12 | [assembly: AssemblyProduct("RABBIT")] 13 | [assembly: AssemblyCopyright("Copyright © MORPHOCODE 2009")] 14 | [assembly: AssemblyTrademark("RABBIT")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fa3f9a78-fb64-44a2-9109-ac1c6258bd3e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("0.1.00")] 36 | [assembly: AssemblyFileVersion("0.1.00")] 37 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Life/SurviveRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Util; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Impl.Life 8 | { 9 | /** 10 | * A cell survives to the next generation depending on a number of neighbors 11 | */ 12 | public class SurviveRule:NeighborhoodEvolutionRule 13 | { 14 | 15 | private IList LivingNeighborsRequirements; 16 | 17 | public SurviveRule(int livingNeighborsRequirement) 18 | { 19 | this.LivingNeighborsRequirements = new List(1); 20 | LivingNeighborsRequirements.Add(livingNeighborsRequirement); 21 | } 22 | 23 | public SurviveRule(IList LivingNeighborsRequirements) 24 | { 25 | this.LivingNeighborsRequirements = LivingNeighborsRequirements; 26 | } 27 | 28 | 29 | public override CellState ApplyRule(LifeLikeCell cell) 30 | { 31 | IList neighbors = cell.GetNeighbors(); 32 | int aliveNeighborsCount = getAliveCellsCount(neighbors); 33 | if (cell.IsAlive() && LivingNeighborsRequirements.Contains(aliveNeighborsCount)) 34 | return cell.GetState(); 35 | else 36 | return null; 37 | } 38 | 39 | public override String ToString() 40 | { 41 | return "A living cell remains alive only when surrounded by " + PresentationUtils.ListToString(LivingNeighborsRequirements) + " living neighbors"; 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Life/BornRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Util; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata.Impl.Life 8 | { 9 | /** 10 | * A cell is born if there is a concrete number of neighbors 11 | */ 12 | public class BornRule:NeighborhoodEvolutionRule 13 | { 14 | 15 | private IList LivingNeighborsRequirements; 16 | 17 | public BornRule(int livingNeighborsRequirement) 18 | { 19 | this.LivingNeighborsRequirements = new List(1); 20 | LivingNeighborsRequirements.Add(livingNeighborsRequirement); 21 | } 22 | 23 | public BornRule(IList LivingNeighborsRequirements) 24 | { 25 | this.LivingNeighborsRequirements = LivingNeighborsRequirements; 26 | } 27 | 28 | 29 | public override CellState ApplyRule(LifeLikeCell cell) 30 | { 31 | IList neighbors = cell.GetNeighbors(); 32 | int aliveNeighborsCount = getAliveCellsCount(neighbors); 33 | if (!cell.IsAlive() && LivingNeighborsRequirements.Contains(aliveNeighborsCount)) 34 | return cell.GetAliveState(); 35 | 36 | else 37 | //return cell.GetState();//CellState.UNRESOLVED; 38 | return null; 39 | } 40 | 41 | 42 | public override String ToString() 43 | { 44 | return "Dead cell comes to life when it has exactly " + PresentationUtils.ListToString(LivingNeighborsRequirements) + " living neighbors"; 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Kernel/LSystems/Symbol.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * A Symbol is a base concept in the LSystems theory. 9 | * It is the base part that compose the words of the LSystem's language. 10 | * (Symbol -> Word -> Language) 11 | * It is the smallest discrete part that defines the grammar of the LSystem. 12 | * The predecessor in the production rules is a LSymbol 13 | * 14 | * 15 | */ 16 | public class Symbol 17 | { 18 | private String stringRepresentation; 19 | 20 | public Symbol(String stringRepresentation) 21 | { 22 | this.stringRepresentation = stringRepresentation; 23 | } 24 | 25 | public Symbol(Char symbolChar) { 26 | stringRepresentation = symbolChar.ToString(); 27 | } 28 | 29 | public override Boolean Equals(Object aSymbol) 30 | { 31 | return this.stringRepresentation.Equals(aSymbol.ToString()); 32 | } 33 | 34 | public override int GetHashCode() { 35 | return stringRepresentation.GetHashCode(); 36 | } 37 | 38 | 39 | public override String ToString() 40 | { 41 | return stringRepresentation; 42 | } 43 | 44 | 45 | public static Symbol valueOf(Char symbolChar) 46 | { 47 | return new Symbol(symbolChar); 48 | } 49 | 50 | public static Symbol valueOf(String symbolString) 51 | { 52 | return new Symbol(symbolString); 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Kernel/RLogo/Parser/StandardRLogoParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.TurtleGraphics; 6 | using Rabbit.Kernel.TurtleGraphics.Commands; 7 | 8 | namespace Rabbit.Kernel.RLogo.Parser 9 | { 10 | public class StandardRLogoParser : RLogoParser 11 | { 12 | 13 | public StandardRLogoParser() 14 | { 15 | base.turtleCommandFactory = new TurtleCommandFactory(); 16 | turtleCommandFactory.MapTurtleCommand('F', new MoveForwardAndDrawTurtleCommand()); 17 | turtleCommandFactory.MapTurtleCommand('f', new MoveForwardTurtleCommand()); 18 | turtleCommandFactory.MapTurtleCommand('+', new TurnLeftTurtleCommand()); 19 | turtleCommandFactory.MapTurtleCommand('-', new TurnRightTurtleCommand()); 20 | turtleCommandFactory.MapTurtleCommand('/', new RollRightTurtleCommand()); 21 | turtleCommandFactory.MapTurtleCommand('\\', new RollLeftTurtleCommand()); 22 | turtleCommandFactory.MapTurtleCommand('^', new PitchUpTurtleCommand()); 23 | turtleCommandFactory.MapTurtleCommand('&', new PitchDownTurtleCommand()); 24 | turtleCommandFactory.MapTurtleCommand('|', new TurnAroundTurtleCommand()); 25 | turtleCommandFactory.MapTurtleCommand('[', new PushStateTurtleCommand()); 26 | turtleCommandFactory.MapTurtleCommand(']', new PopStateTurtleCommand()); 27 | turtleCommandFactory.MapTurtleCommand('"', new ScaleStepLengthTurtleCommand()); 28 | turtleCommandFactory.MapTurtleCommand('!', new ScaleThicknessTurtleCommand()); 29 | turtleCommandFactory.MapTurtleCommand('J', new CopyGeometryTurtleCommand()); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/TurtleState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rhino.Geometry; 6 | 7 | namespace Rabbit.Kernel.TurtleGraphics 8 | { 9 | /** 10 | * The TurtleState is defined by a position in space and heading vector. 11 | * The heading defines the orientation of the turtle 12 | */ 13 | public class TurtleState 14 | { 15 | 16 | private Plane orientation; 17 | /** Position */ //should be accessed directly as a class member in order to be modified 18 | private Point3d position; 19 | private double stepLength; 20 | private double thickness; 21 | private Curve profile; 22 | 23 | public TurtleState(Point3d position, Plane orientation, double currentStepLength, double thickness, Curve profile) 24 | { 25 | this.position = position; 26 | this.orientation = orientation; 27 | this.stepLength = currentStepLength; 28 | this.thickness = thickness; 29 | this.profile = profile; 30 | } 31 | 32 | public double GetThickness() 33 | { 34 | return thickness; 35 | } 36 | 37 | public double GetStepLength() 38 | { 39 | return stepLength; 40 | } 41 | 42 | public void SetStepLength(double newStepLength) 43 | { 44 | this.stepLength = newStepLength; 45 | } 46 | 47 | public Point3d GetPosition() 48 | { 49 | return position; 50 | } 51 | 52 | public void SetPosition(Point3d position) 53 | { 54 | this.position = position; 55 | } 56 | 57 | public Plane GetOrientation() 58 | { 59 | return orientation; 60 | } 61 | 62 | public Curve GetProfile() 63 | { 64 | return profile; 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/CellularSpace/SimpleCellularGridBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using RMA.OpenNURBS; 6 | 7 | using Rabbit.Kernel.ABMS.Space.Grid; 8 | using Rabbit.Kernel.CellularAutomata.Configuration; 9 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 10 | 11 | namespace Rabbit.Kernel.CellularAutomata 12 | { 13 | /** 14 | * Builds a Grid2d out of OpenNurbs points 15 | * 16 | * 17 | */ 18 | public class SimpleCellularGridBuilder: CellularGridBuilder 19 | { 20 | 21 | ICAConfig initialConfiguration; 22 | 23 | private int latticeRows, latticeColumns; 24 | 25 | public SimpleCellularGridBuilder():base(null)//LivingCell.GOL_LivingCellPrototype) 26 | {//second arg should be list of points 27 | 28 | 29 | //analyze the geometric input 30 | //grid size 31 | latticeRows = 10; 32 | latticeColumns = 10; 33 | 34 | //configuration 35 | //initialConfiguration = new CAConfig(0, LivingCell.DeadState); 36 | 37 | 38 | } 39 | 40 | protected override Grid2d CreateCellularGrid() 41 | { 42 | return new Grid2d(latticeRows, latticeColumns); 43 | } 44 | 45 | protected override void PopulateGrid() 46 | { 47 | throw new NotImplementedException(); 48 | } 49 | 50 | 51 | //remove that! 52 | public ICAConfig GetCfg() 53 | { 54 | return initialConfiguration; 55 | } 56 | 57 | protected override ICAConfig GetInitialConfiguration() 58 | { 59 | return initialConfiguration; 60 | } 61 | 62 | 63 | protected override INeighborhoodStrategy GetNeighborhoodStrategy() 64 | { 65 | return new MooreNeighborhoodStrategy(cellularGrid, false); 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Kernel/LSystems/ProductionRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * 9 | * 10 | 11 | * The rules describe how to form strings from the language's alphabet that are valid according to the language's syntax. 12 | * A grammar does not describe the meaning of the strings or what can be done with them in whatever context — only their form. 13 | * 14 | * 15 | * 16 | */ 17 | public abstract class ProductionRule:RewriteRule 18 | { 19 | 20 | protected Symbol predecessor; 21 | 22 | protected Word successor; 23 | 24 | // implicit priority ot the rules 25 | // Context-Sensitive rules have higher priority then Context-Free rules by default 26 | // the priority is used to sort the rules 27 | public static int RulePriority = 0; 28 | 29 | //private double ProbabilityDistribution; 30 | 31 | 32 | public ProductionRule(Symbol predecessor, Word successor) 33 | { 34 | this.predecessor = predecessor; 35 | this.successor = successor; 36 | } 37 | 38 | public Symbol GetPredecessor() 39 | { 40 | return predecessor; 41 | } 42 | 43 | public Word GetSuccessor() 44 | { 45 | return successor; 46 | } 47 | 48 | /** 49 | * returns the successor string for the specified symbol, null if the rule could not be applied 50 | * 51 | * 52 | */ 53 | public abstract Word Rewrite(Symbol symbol, int SymbolIndex, Word word); 54 | 55 | 56 | public override String ToString() 57 | { 58 | StringBuilder ruleDescription = new StringBuilder(); 59 | ruleDescription.Append(predecessor.ToString()).Append(" = ").Append(successor.ToString()); 60 | return ruleDescription.ToString(); 61 | } 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Kernel/LSystems/AbstractStringRewritingSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * 9 | * A string rewriting system consists of an initial 10 | string, called the seed, and a set of rules for specifying 11 | how the symbols in a string are rewritten as 12 | (replaced by) strings. 13 | * 14 | * 15 | */ 16 | public abstract class AbstractStringRewritingSystem 17 | { 18 | 19 | /** 20 | * When a string rewriting system is used, the 21 | rules are applied to the seed to produce a new 22 | string, the rules are again applied to this string to 23 | produce another, and so on, forming a sequence of 24 | generations, with the seed being considered generation 25 | 0. The sequence of generations constitute the 26 | language for the rewriting system. 27 | * 28 | */ 29 | public abstract Word Rewrite(); 30 | 31 | /** 32 | * The seed/axiom of the Rewriting system 33 | */ 34 | public abstract Word GetSeed(); 35 | 36 | /** 37 | * returns the last generation that was generated 38 | * 39 | */ 40 | public abstract Word GetCurrentDerivation(); 41 | 42 | /** 43 | * Adds a rewrite rule to the system 44 | */ 45 | //public abstract void AddRule(RewriteRule rule); 46 | 47 | /** 48 | * The sequence of generations constitute the 49 | language for the rewriting system. 50 | * 51 | */ 52 | public abstract IList GetLanguage(); 53 | 54 | /** 55 | * returns the index of current generation. 56 | * Each rewriting of a string results in new generation 57 | * 58 | */ 59 | public abstract int GetCurrentDerivationIndex(); 60 | 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Kernel/Geometry/Util/PointUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | //using RMA.OpenNURBS; 6 | using Rhino.Geometry; 7 | 8 | namespace Rabbit.Kernel.Geometry.Util 9 | { 10 | /** 11 | * Utility class that provides some common features related to point manipulation 12 | * 13 | */ 14 | public class PointUtils 15 | { 16 | 17 | private PointUtils() { } 18 | 19 | /** 20 | * returns a list of all points from latticePoints that are closer to each point in points 21 | */ 22 | public static Point3d GetClosestPoint(Point3d point, IList latticePoints) 23 | { 24 | if (latticePoints == null || point == null || latticePoints.Count == 0) 25 | throw new InvalidOperationException("point or lattice of points are invalid/empty!"); 26 | 27 | //IList closestPoints = new List(points.Count); 28 | double minDistance = Double.MaxValue; 29 | double distance; 30 | int closestPointIndex = -1; 31 | int pointIndex = 0; 32 | foreach (Point3d latticePoint in latticePoints) 33 | { 34 | distance = point.DistanceTo(latticePoint); 35 | if (distance < minDistance) 36 | { 37 | minDistance = distance; 38 | closestPointIndex = pointIndex; 39 | } 40 | pointIndex++; 41 | } 42 | 43 | return latticePoints[closestPointIndex]; 44 | } 45 | 46 | public static IList GetClosestPoints(IList points, IList latticePoints) 47 | { 48 | List closestPoints = new List(points.Count); 49 | foreach (Point3d point in points) 50 | { 51 | closestPoints.Add(GetClosestPoint(point, latticePoints)); 52 | } 53 | 54 | return closestPoints; 55 | } 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Kernel/Language/SimpleLexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Language 6 | { 7 | 8 | /** 9 | * Simple Lexer Implementation that gets a character sequence as an input and returns a list of tokens as an ouput 10 | * 11 | * 12 | * In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens. A program or function which performs lexical analysis is called a lexical analyzer, lexer or scanner. A lexer often exists as a single function which is called by a parser or another function. 13 | */ 14 | public class SimpleLexer:ILexer 15 | { 16 | 17 | private char[] separators; 18 | 19 | public SimpleLexer(char[] separators) 20 | { 21 | this.separators = separators; 22 | } 23 | 24 | public ITokenStream Analyze(String input) 25 | { 26 | IList tokens = new List(); 27 | /* 28 | String[] tokenStrings = input.Split(separators); 29 | foreach (String tokenString in tokenStrings) 30 | { 31 | tokens.Add(new Token(tokenString)); 32 | }*/ 33 | 34 | for (int i = 0; i < input.Length; i++) 35 | { 36 | StringBuilder tokenBuilder = new StringBuilder(); 37 | char character = input[i]; 38 | //if(char.IsDigit(input, i)) 39 | tokenBuilder.Append(character); 40 | Token token = new Token(character.ToString()); 41 | tokens.Add(token); 42 | } 43 | 44 | return new ListTokenStream(tokens); 45 | } 46 | 47 | /* 48 | private char NextDigit(String input, int currentCharIndex) 49 | { 50 | if(currentCharIndex>=input.Length) return null; 51 | char character = input[currentCharIndex + 1]; 52 | if(character.IsDigit()) return character; 53 | else 54 | return null; 55 | } 56 | * 57 | * */ 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Elementary/ElementaryRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | using Rabbit.Kernel.CellularAutomata.Cells; 7 | 8 | namespace Rabbit.Kernel.CellularAutomata.Impl.Elementary 9 | { 10 | /** 11 | * The behavior of the cellular automata as a system is drivven by the behavior of each individual cell. 12 | * 13 | * A CellBehavior defines what the cell does in every generation. 14 | * The state of the cell changes as the evolution goes. 15 | * The cell acts according to some simple rules. 16 | * This class defines what are these rules and makes possible to combine multiple evolution rules 17 | * 18 | * A flyweight object. 19 | * 20 | * @author MORPHOCODE.COM 21 | */ 22 | public class ElementaryRule : CellularRule { 23 | 24 | private CellState leftCellState, rightCellState, middleCellState, resultingCellState; 25 | 26 | public ElementaryRule(CellState leftCellState, CellState middleCellState, CellState rightCellState, CellState resultingCellState) 27 | { 28 | this.leftCellState = leftCellState; 29 | this.middleCellState = middleCellState; 30 | this.rightCellState = rightCellState; 31 | this.resultingCellState = resultingCellState; 32 | } 33 | 34 | public CellState ApplyRule(ICell cell) 35 | { 36 | throw new SystemException("Not implemented"); 37 | } 38 | 39 | 40 | public CellState SolveState(CellState currentState, Object input) 41 | { 42 | throw new SystemException("Not implemented"); 43 | } 44 | 45 | public CellState GetResultingState() 46 | { 47 | return resultingCellState; 48 | } 49 | 50 | public Boolean matches(CellState currentLeftCellState, CellState currentState, CellState currentRightCellState) 51 | { 52 | return currentLeftCellState.Equals(leftCellState) && currentState.Equals(middleCellState) && currentRightCellState.Equals(rightCellState); 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /GH/CellularAutomata/GH_TypeUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using RMA.OpenNURBS; 6 | using Grasshopper.Kernel.Types; 7 | using Grasshopper.Kernel; 8 | 9 | namespace Rabbit.GH.CellularAutomata 10 | { 11 | /** 12 | * Utility class that provides some common functionality related to GH to ON points converting 13 | * 14 | */ 15 | public class GH_TypeUtils 16 | { 17 | 18 | private GH_TypeUtils() { } 19 | 20 | 21 | /** 22 | * Goo.CastTo<> supports cross-type castings i.e. Integers could be cast to Strings. We need strong typing. 23 | * GH_integer -> integer 24 | * GH_boolean -> boolean 25 | * 26 | * returns the System primitive wrapped by the GH_Goo object 27 | * returns a Boolean out of GH_Boolean 28 | * returns an Integer out of GH_Integer 29 | * 30 | */ 31 | public static Object ConvertToSystemType(GH_Goo goo) 32 | { 33 | if (goo.GetType() == GH_TypeLib.t_gh_bool)//GH_BooleanPrototype.GetType())//check if it is a boolean 34 | { 35 | Boolean result = false; 36 | goo.CastTo(ref result); 37 | return result; 38 | } 39 | else if (goo.GetType() == GH_TypeLib.t_gh_int) 40 | { 41 | int result = -1; 42 | goo.CastTo(ref result); 43 | return result; 44 | } 45 | else if (goo.GetType() == GH_TypeLib.t_gh_string) 46 | { 47 | String result = null; 48 | goo.CastTo(ref result); 49 | return result; 50 | } 51 | else if (goo.GetType() == GH_TypeLib.t_gh_objwrapper) 52 | { 53 | GH_ObjectWrapper result = null; 54 | goo.CastTo(ref result); 55 | return result.Value; 56 | } 57 | throw new NotSupportedException("Could not get the System object for Goo of type" + goo.GetType()); 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/CellState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.CellularAutomata.Cells; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata 8 | { 9 | 10 | /** 11 | * CellState defines the state of a cell. 12 | * Basically it is a wrapper around of some kind of primitive(Boolean, integer, etc.) 13 | * 14 | * 15 | * @author HTTP://MORPHOCODE.COM 16 | * 17 | */ 18 | public class CellState:ICellState 19 | { 20 | 21 | //maps a name to a CellState 22 | //private static Dictionary cacheMap = new Dictionary(); 23 | 24 | protected Object value; 25 | 26 | /** 27 | * Constructor. 28 | * 29 | */ 30 | public CellState(Object value) 31 | { 32 | this.value = value; 33 | } 34 | 35 | 36 | public Object GetValue() 37 | { 38 | return value; 39 | } 40 | 41 | 42 | 43 | public override Boolean Equals(Object that) 44 | { 45 | if (that == null) return false; 46 | if (that.GetType() != this.GetType()) return false; 47 | 48 | CellState thatState = (CellState) that; 49 | 50 | return value.Equals(thatState.value); 51 | } 52 | 53 | 54 | public override string ToString() 55 | { 56 | return value.ToString(); 57 | } 58 | 59 | 60 | /** 61 | * Uses a pool. Caches an already created cell state 62 | */ 63 | /* 64 | public static CellState ValueOf(String nickName) 65 | { 66 | String name = nickName; 67 | if (nickName.Equals("D")) 68 | name = "DEAD"; 69 | else if (nickName.Equals("A")) 70 | name = "ALIVE"; 71 | 72 | if (cacheMap.ContainsKey(name)) 73 | return cacheMap[name]; 74 | else 75 | { 76 | CellState cellState = new CellState(name); 77 | cacheMap.Add(name, cellState);//save to cache for further retrieval 78 | return cellState; 79 | } 80 | } 81 | * */ 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Kernel/LSystems/SymbolMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * A Symbol Mapper maps a meaning to an abstract symbol. 9 | * The meaning is some kind of an object, it could be a value, an expression, a geometry, an instruction(for a turtle for ex.) 10 | * Usually different kind of interpreters expect specific meanings. For ex. The surface growth component expects integer values associated with the symbols 11 | * 12 | */ 13 | public class SymbolMap//M=the meaning 14 | { 15 | 16 | protected Dictionary symbol2MeaningMap; 17 | 18 | /** 19 | * Constructor. 20 | */ 21 | public SymbolMap() 22 | { 23 | symbol2MeaningMap = new Dictionary(); 24 | } 25 | 26 | public void Map(Symbol symbol, M meaning) 27 | { 28 | if (!symbol2MeaningMap.ContainsKey(symbol)) 29 | symbol2MeaningMap.Add(symbol, meaning); 30 | else //replace the meaning 31 | symbol2MeaningMap[symbol] = meaning; 32 | } 33 | 34 | /** 35 | * Returns the Meaning mapped to that Symbol 36 | * Null if there is no associated meanings 37 | * 38 | */ 39 | public M GetMeaning(Symbol symbol) 40 | { 41 | if (symbol2MeaningMap.ContainsKey(symbol)) 42 | return symbol2MeaningMap[symbol]; 43 | else 44 | return default(M); 45 | } 46 | 47 | 48 | public override String ToString() 49 | { 50 | StringBuilder symbolMapDescription = new StringBuilder(); 51 | symbolMapDescription.AppendLine(string.Format("Symbol Map contains the following {0} meanings:", symbol2MeaningMap.Count)); 52 | symbolMapDescription.AppendLine(string.Format("Symbol <-> Meaning")); 53 | //symbolMapDescription.Append(symbol2MeaningMap.ToString()); 54 | foreach (Symbol symbol in symbol2MeaningMap.Keys) 55 | { 56 | symbolMapDescription.AppendLine(String.Format("{0} <-> {1}", symbol, symbol2MeaningMap[symbol])); 57 | } 58 | return symbolMapDescription.ToString(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/ICell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | using Rabbit.Kernel.CellularAutomata.Cells; 7 | 8 | namespace Rabbit.Kernel.CellularAutomata 9 | { 10 | /** 11 | * A cell in a Cellular Automata 12 | * 13 | * 14 | */ 15 | public interface ICell : FiniteStateMachine 16 | { 17 | 18 | /** 19 | * Identifier of the cell 20 | * Could be a position, or an index 21 | */ 22 | Object GetId(); 23 | 24 | void SetId(Object id); 25 | 26 | 27 | // ATTACHED OBJECT - TODO: better use a Wrapper but fix the problem with casting --------------------------------------------------- 28 | Object GetAttachedObject(); 29 | 30 | void SetAttachedObject(Object obj); 31 | 32 | // STATE --------------------------------------------------- 33 | /** 34 | * the state of the cell 35 | */ 36 | new CellState GetState(); 37 | 38 | /** 39 | * all possible states of the cell 40 | */ 41 | new IList GetFiniteStates(); 42 | 43 | /** 44 | * Sets the initial state of the cell 45 | * 46 | */ 47 | void SetState(CellState cellState); 48 | 49 | CellState GetDefaultState(); 50 | 51 | 52 | 53 | // EVOLUTION RULES --------------------------------------------------- 54 | 55 | /** 56 | * The evolution rules used by the cell when changing state 57 | */ 58 | new IList GetRules(); 59 | 60 | void SetTransitionRules(List transitionRules); 61 | 62 | 63 | 64 | // CONNECTIONS/CLUSTERING --------------------------------------------------- 65 | 66 | /** 67 | * The neighbors of the cell 68 | * 69 | */ 70 | IList GetNeighbors(); 71 | 72 | /** 73 | * 74 | * define all neighbors of the current cell 75 | */ 76 | void SetNeighbors(IList neighbors); 77 | 78 | //factory-method 79 | //Prototype design pattern 80 | ICell Clone(); 81 | 82 | 83 | 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Kernel/RLogo/TurtleGraphics/Canvas.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Collections; 5 | 6 | //using RMA.OpenNURBS; 7 | using Rhino.Geometry; 8 | 9 | namespace Rabbit.Kernel.TurtleGraphics 10 | { 11 | /** 12 | * Canvas on which the Turtle draws 13 | * 14 | * 15 | */ 16 | public class Canvas 17 | { 18 | 19 | private IList edges; 20 | private IList vertices; 21 | //generic geometry 22 | private IList geometryList; 23 | private IList planes; 24 | private IList profiles; 25 | 26 | 27 | public Canvas() 28 | { 29 | edges = new List(); 30 | vertices = new List(); 31 | geometryList = new List(); 32 | planes = new List(); 33 | profiles = new List(); 34 | } 35 | 36 | public void AddVertex(Point3d vertex) 37 | { 38 | vertices.Add(vertex); 39 | } 40 | 41 | public IList GetVertices() 42 | { 43 | return vertices; 44 | } 45 | 46 | public void AddEdge(Line line) 47 | { 48 | edges.Add(line); 49 | } 50 | 51 | public IList GetEdges() 52 | { 53 | return edges; 54 | } 55 | 56 | public void AddGeometry(GeometryBase geometry) 57 | { 58 | geometryList.Add(geometry); 59 | } 60 | 61 | public IList GetGeometry() 62 | { 63 | return geometryList; 64 | } 65 | 66 | public void AddPlane(Plane plane) 67 | { 68 | planes.Add(plane); 69 | } 70 | 71 | public IList GetPlanes() 72 | { 73 | return planes; 74 | } 75 | 76 | public void AddProfile(Curve profile) 77 | { 78 | profiles.Add(profile); 79 | } 80 | 81 | public IList GetProfiles() 82 | { 83 | return profiles; 84 | } 85 | 86 | public void Clear() 87 | { 88 | edges.Clear(); 89 | vertices.Clear(); 90 | geometryList.Clear(); 91 | planes.Clear(); 92 | profiles.Clear(); 93 | } 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Kernel/LSystems/DeterministicLSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * the simplest class of L-systems, those which are 9 | * deterministic and context-free, called D0L-systems. 10 | * If there is exactly one production for each symbol, then the L-system is said to be deterministic (a deterministic context-free L-system is popularly called a D0L-system). 11 | * 12 | * 13 | */ 14 | public class DeterministicLSystem:AbstractLSystem 15 | { 16 | 17 | protected Dictionary RulesLookUpMap; 18 | 19 | public DeterministicLSystem(IList Alphabet, Word Axiom) 20 | : base(Alphabet, Axiom) 21 | { 22 | this.RulesLookUpMap = new Dictionary(); 23 | } 24 | 25 | 26 | /** 27 | * do not allow more than 1 rule for a given symbol 28 | * Deterministic L-Systems allow only one production rule for each symbol 29 | * Deterministic LSystems do not allow setting different Probability Distributions for the Rewrite rules 30 | */ 31 | public override void AddRule(ProductionRule RewriteRule) 32 | { 33 | if(RulesLookUpMap.ContainsKey(RewriteRule.GetPredecessor())) 34 | throw new InvalidProgramException("Could not add RewriteRule for Symbol: '" + RewriteRule.GetPredecessor()+"', because there is already a rule added for that symbol. Deterministic L-Systems have only one production rule per symbol by definition!"); 35 | 36 | //Add the rule to the list of rules 37 | base.AddRule(RewriteRule); 38 | 39 | this.RulesLookUpMap.Add(RewriteRule.GetPredecessor(), RewriteRule); 40 | } 41 | 42 | /** 43 | * @returns The RewriteRule associated with the specified Symbol(Predecessor) 44 | * 45 | */ 46 | protected override ProductionRule GetProductionRule(Symbol symbol, int symbolIndex, Word word) 47 | { 48 | if (RulesLookUpMap.ContainsKey(symbol)) 49 | return RulesLookUpMap[symbol]; 50 | else 51 | return null; 52 | } 53 | 54 | /** 55 | * 56 | */ 57 | public override String ToString() 58 | { 59 | return base.ToString(); 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Kernel/LSystems/Word.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * 9 | * Represents a word in the language of a LSystem. 10 | * The word is a sequence of symbols. 11 | * 12 | * If there are parametric symbols the words are called parametric words 13 | * 14 | */ 15 | public class Word 16 | { 17 | //string builder used to represent the word as a string 18 | private StringBuilder lwordBuilder; 19 | 20 | private IList symbols; 21 | 22 | private int size; 23 | 24 | public Word() 25 | { 26 | //lwordBuilder = new StringBuilder(); 27 | symbols = new List(); 28 | size = 0; 29 | } 30 | 31 | public Word(Symbol symbol) 32 | : this() 33 | { 34 | Append(symbol); 35 | } 36 | 37 | public void Append(Symbol lsymbol) 38 | { 39 | symbols.Add(lsymbol); 40 | //lwordBuilder.Append(lsymbol.ToString()); 41 | size++; 42 | } 43 | 44 | /** 45 | * Appends another word to this word by appending all of it's symbols 46 | * 47 | */ 48 | public void Append(Word word) 49 | { 50 | foreach (Symbol symbol in word.GetSymbols()) 51 | Append(symbol); 52 | } 53 | 54 | public IList GetSymbols() 55 | { 56 | return symbols; 57 | } 58 | 59 | public Symbol SymbolAt(int index) 60 | { 61 | if (index >= 0 && index < symbols.Count) 62 | return symbols[index]; 63 | else 64 | return null; 65 | } 66 | 67 | /** 68 | * The number of symbols that compose this word 69 | */ 70 | public int GetSize() 71 | { 72 | return size; 73 | } 74 | 75 | public override String ToString() 76 | { 77 | //FIXME: build this at initialization and modify it only when needed 78 | ///do not build it every time a ToString() is called!! 79 | StringBuilder wordStringBuilder = new StringBuilder(); 80 | foreach (Symbol lsymbol in symbols) 81 | wordStringBuilder.Append(lsymbol.ToString()); 82 | 83 | return wordStringBuilder.ToString(); 84 | } 85 | 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Kernel/Automata/FSM/InternalMemory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Automata.FSM 6 | { 7 | /** 8 | * A Discrete system could have an internal memory that stores the discrete states of the system and the time at which they occured. 9 | * 10 | * 11 | */ 12 | public class InternalMemory where S : State 13 | { 14 | //private Dictionary timeToStateMap; 15 | //private Dictionary stateToTimeMap; 16 | private IList states; 17 | 18 | private int initialCapacity; 19 | 20 | public InternalMemory(int initialCapacity) 21 | { 22 | this.initialCapacity = initialCapacity; 23 | //timeToStateMap = new Dictionary(initialCapacity); 24 | this.states = new List(initialCapacity); 25 | 26 | } 27 | 28 | //public void Save(int time, S state) { 29 | public void Save(S state) 30 | { 31 | //timeToStateMap[time] = state; 32 | states.Add(state); 33 | 34 | } 35 | 36 | /** 37 | * Get the last state stored in the memory 38 | * 39 | */ 40 | public S GetLastState() 41 | { 42 | if (states.Count > 0) 43 | return states[states.Count-1]; 44 | else 45 | return default(S); 46 | } 47 | 48 | public IList GetAllStates() 49 | { 50 | return states; 51 | } 52 | 53 | /* 54 | * the State associated with the specified time 55 | * 56 | public S GetState(int discreteTime) 57 | { 58 | if (timeToStateMap.ContainsKey(discreteTime)) 59 | return timeToStateMap[discreteTime]; 60 | else 61 | return default(S); 62 | } 63 | */ 64 | 65 | /* 66 | * returns the time moment associated with the specified state 67 | * 68 | public int GetTime(S discreteState) 69 | { 70 | //throw new NotImplementedException(); 71 | if (timeToStateMap.ContainsValue(discreteState)) 72 | foreach (KeyValuePair pair in timeToStateMap) 73 | if(pair.Value.Equals(discreteState)) //TODO: there could be multuple equal states!!! what about that?? 74 | return pair.Key; 75 | 76 | return -1; 77 | } 78 | * */ 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Kernel/LSystems/ILProductionRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | /** 8 | * Context-Sensitive Rule that is being applied depending of the context in which the predecessor occurs 9 | * In order to keep specifications of L-systems short, the usual notion 10 | of IL-systems has been modified here by allowing productions with 11 | different context lengths to coexist within a single system. Furthermore, 12 | context-sensitive productions are assumed to have precedence 13 | over context-free productions with the same strict predecessor. Consequently, 14 | if a context-free and a context-sensitive production both apply 15 | to a given letter, the context-sensitive one should be selected. If no production 16 | applies, this letter is replaced by itself as previously assumed 17 | for OL-systems. 18 | * 19 | */ 20 | public class ILProductionRule:ProductionRule 21 | { 22 | private Word leftContext; 23 | 24 | private Word rightContext; 25 | 26 | public ILProductionRule(Symbol predecessor, Word successor, Word leftContext, Word rightContext):base(predecessor, successor) 27 | { 28 | this.leftContext = leftContext; 29 | this.rightContext = rightContext; 30 | } 31 | 32 | 33 | 34 | /** 35 | * Returns a result if the specified symbol matchs the left and right context of the Rule 36 | * 37 | * 38 | */ 39 | public override Word Rewrite(Symbol symbol, int SymbolIndex, Word word) 40 | { 41 | //check if that rule could be applied for the specified symbol 42 | if (predecessor.Equals(symbol)) 43 | { 44 | //TODO: use regex 45 | //string sPattern = "^\\d{3}-\\d{3}-\\d{4}$"; 46 | //if(System.Text.RegularExpressions.Regex.IsMatch(s, sPattern) 47 | 48 | //check if the context matches 49 | String symbolLeft = word.ToString().Substring(0, SymbolIndex); 50 | String symbolRight = word.ToString().Substring(SymbolIndex, word.ToString().Length-1); 51 | if (symbolLeft.EndsWith(leftContext.ToString()) && symbolRight.StartsWith(rightContext.ToString())) 52 | return successor; 53 | else 54 | return null;//context do not matches 55 | } 56 | else 57 | //throw new System.InvalidOperationException("This rule is not meant to be used with symbol: " + Symbol); 58 | return null; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Configuration/CAConfigParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Language; 6 | using Rabbit.Kernel.CellularAutomata.Configuration; 7 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 8 | 9 | 10 | namespace Rabbit.Kernel.CellularAutomata 11 | { 12 | 13 | public class CAConfigParser:IParser 14 | { 15 | 16 | private static Token stateSeparator = new Token(","); 17 | private static Token rowStart = new Token("{"); 18 | private static Token rowEnd = new Token("}"); 19 | //private static Token newLine = new Token("\n"); 20 | 21 | /** 22 | * IPattern 23 | */ 24 | public Object Parse(ITokenStream tokenStream) 25 | { 26 | 27 | Token token; 28 | CAConfig pattern = new CAConfig( 0, null);//LivingCell.DeadState);//Determine the size of the configuration! 29 | //CellState[,] patternState = new CellState[,]; 30 | 31 | IList> rows = new List>(); 32 | IList row = null; 33 | Boolean rowOpened = false; 34 | int rowIndex = 0; 35 | int columnIndex = 0; 36 | while((token=tokenStream.NextToken()) != null) { 37 | 38 | if(token.Equals(stateSeparator)) { 39 | //token=tokenStream.NextToken(); 40 | //do nothing 41 | } 42 | else if(token.Equals(rowStart)) {//init a new row 43 | if(rowOpened) throw new InvalidOperationException(string.Format("Missing enclosing bracket! Please, enclose the row, before creating a new one")); 44 | row = new List(); 45 | rowOpened=true; 46 | rowIndex++; 47 | columnIndex = 0; 48 | } 49 | else if(token.Equals(rowEnd)) {//add the existing row to the list of rows 50 | rows.Add(row); 51 | rowOpened = false; 52 | } 53 | else if(!char.IsWhiteSpace(token.ToString(), 0)) {//State Token 54 | if (row == null || !rowOpened) throw new InvalidOperationException("You should define a cell state within a Row. Use the following format: {D,A,D,D} "); 55 | 56 | CellState cellState = null;//CellState.ValueOf(token.ToString()); 57 | row.Add(cellState); 58 | pattern.AddCellState(null, cellState); 59 | columnIndex++; 60 | } 61 | } 62 | 63 | return pattern; 64 | } 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Configuration/CAConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.CellularAutomata.Configuration; 6 | using Rabbit.Kernel.ABMS.Space.Grid; 7 | 8 | 9 | namespace Rabbit.Kernel.CellularAutomata.Configuration 10 | { 11 | /** 12 | * CAConfiguration defines the state of a cellular automata at a exact point at the Time. 13 | * Holds the state of each cell of the CA. 14 | * 15 | * Memento Design pattern 16 | * 17 | * @author http://MORPHOCODE.COM 18 | */ 19 | public class CAConfig : ICAConfig 20 | { 21 | 22 | private Dictionary cellStates; 23 | 24 | private CellState defaultCellState; 25 | 26 | private int discreteTime;//the time at which this state was produced 27 | 28 | //private CA ca; 29 | 30 | public CAConfig(int discreteTime, CellState defaultCellState) //, ref CA ca) 31 | { 32 | //this.name = name; 33 | this.defaultCellState = defaultCellState; 34 | this.discreteTime = discreteTime; 35 | cellStates = new Dictionary(); 36 | 37 | //this.ca = ca; 38 | } 39 | 40 | public int GetAssociatedTime() 41 | { 42 | return discreteTime; 43 | } 44 | 45 | /* 46 | public CA getCA() 47 | { 48 | return ca; 49 | }*/ 50 | 51 | /** 52 | * Specify a state for that cell. 53 | * If a state was present overwrites it. 54 | */ 55 | public void AddCellState(ICell cell, CellState cellState) 56 | { 57 | if (cellStates.ContainsKey(cell)) 58 | { 59 | //remove the old state if the clien wants to specify a new one 60 | cellStates.Remove(cell); 61 | //throw new InvalidOperationException("Cell state was already specified! You could specify only one state per cell!"); 62 | } 63 | cellStates.Add(cell, cellState); 64 | } 65 | 66 | 67 | public CellState GetCellState(ICell cell) 68 | { 69 | if (cellStates.ContainsKey(cell)) 70 | return cellStates[cell]; 71 | else 72 | return defaultCellState; 73 | //return null; 74 | } 75 | 76 | public IEnumerable GetCells() 77 | { 78 | return cellStates.Keys; 79 | } 80 | 81 | public override string ToString() 82 | { 83 | return string.Format("CA Configuration at t={0}, containing {1} cell states", discreteTime, cellStates.Count); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_SurviveRule_OBSOLETE.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Grasshopper.Kernel; 5 | using Grasshopper.Kernel.Types; 6 | using System.Drawing; 7 | 8 | using Rabbit.Properties; 9 | using Rabbit.Kernel.CellularAutomata; 10 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 11 | 12 | 13 | namespace Rabbit.GH.CellularAutomata 14 | { 15 | /** 16 | * GH Component that creates an DieOnOverCrowdEvolutionRule 17 | */ 18 | public class Component_SurviveRule_OBSOLETE : Component_CABase 19 | { 20 | 21 | /** 22 | * Constructor 23 | */ 24 | public Component_SurviveRule_OBSOLETE() 25 | : base("Survive Rule [OBSOLETE]", "Survive", "A cell survives if surrounded by N neighbors in 'alive' state") 26 | { 27 | } 28 | 29 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 30 | { 31 | inputManager.Register_IntegerParam("Neigbors", "N", "Number of neighbors in 'alive' state", 2, GH_ParamAccess.list);//name, nick, description, default, isList 32 | } 33 | 34 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 35 | { 36 | pManager.Register_GenericParam("Survive Rule", "R", "Survive rule");//name, nick, description 37 | } 38 | 39 | /** 40 | * Does the computation 41 | */ 42 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 43 | { 44 | List livingNeighborsRequirements = new List(); 45 | 46 | //get the input parameters 47 | DA.GetDataList(0, livingNeighborsRequirements);//param index, place holder 48 | 49 | //creates the Evolution Rule: 50 | SurviveRule SurviveRule = new SurviveRule(livingNeighborsRequirements); 51 | 52 | //set the output parameters 53 | DA.SetData(0, SurviveRule); 54 | } 55 | 56 | /** 57 | * Interpretative part of the LSystems 58 | * secondary exposure 59 | */ 60 | public override GH_Exposure Exposure 61 | { 62 | get 63 | { 64 | return GH_Exposure.hidden; 65 | } 66 | } 67 | 68 | /** 69 | * The Guid of the component 70 | */ 71 | public override Guid ComponentGuid 72 | { 73 | get { 74 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A321898397}"); 75 | } 76 | } 77 | 78 | /** 79 | * The icon of the component 80 | */ 81 | protected override Bitmap Internal_Icon_24x24 82 | { 83 | get 84 | { 85 | return Resources.Custom_24x24_SurviveRule; 86 | } 87 | } 88 | 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_BornRule_OBSOLETE.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Grasshopper.Kernel; 5 | using Grasshopper.Kernel.Types; 6 | using System.Drawing; 7 | 8 | using Rabbit.Properties; 9 | using Rabbit.Kernel.CellularAutomata; 10 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 11 | 12 | 13 | namespace Rabbit.GH.CellularAutomata 14 | { 15 | /** 16 | * GH Component that creates a BornRule 17 | * 18 | * @Author MORPHOCODE.COM 19 | */ 20 | public class Component_BornRule_OBSOLETE : Component_CABase 21 | { 22 | 23 | /** 24 | * Constructor 25 | */ 26 | public Component_BornRule_OBSOLETE() 27 | : base("Born Rule [OBSOLETE]", "Born", "A cell is being born if surrounded by N neighbors in 'alive' state", "2D CA") 28 | { 29 | } 30 | 31 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 32 | { 33 | 34 | inputManager.Register_IntegerParam("Neigbors", "N", "Number of neighbors in 'alive' state", 2, GH_ParamAccess.list);//name, nick, description, default, isList 35 | } 36 | 37 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 38 | { 39 | pManager.Register_GenericParam("Born Rule", "R", "Born rule");//name, nick, description 40 | } 41 | 42 | /** 43 | * Interpretative part of the LSystems 44 | * secondary exposure 45 | */ 46 | public override GH_Exposure Exposure 47 | { 48 | get 49 | { 50 | return GH_Exposure.hidden; 51 | } 52 | } 53 | 54 | 55 | /** 56 | * Does the computation 57 | */ 58 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 59 | { 60 | List livingNeighborsRequirements = new List(); 61 | 62 | //get the input parameters 63 | DA.GetDataList(0, livingNeighborsRequirements);//param index, place holder 64 | 65 | //creates the Evolution Rule: 66 | BornRule BornRule = new BornRule(livingNeighborsRequirements); 67 | 68 | //set the output parameters 69 | DA.SetData(0, BornRule); 70 | } 71 | 72 | /** 73 | * The Guid of the component 74 | */ 75 | public override Guid ComponentGuid 76 | { 77 | get { 78 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A245598397}"); 79 | } 80 | } 81 | 82 | 83 | /** 84 | * The icon of the component 85 | */ 86 | protected override Bitmap Internal_Icon_24x24 87 | { 88 | get 89 | { 90 | return Resources.Custom_24x24_BornRule; 91 | } 92 | } 93 | 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/CellularSpace/CellularGridBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.ABMS.Space.Grid; 6 | using Rabbit.Kernel.CellularAutomata.Configuration; 7 | 8 | namespace Rabbit.Kernel.CellularAutomata 9 | { 10 | 11 | /** 12 | * Builds a Cellular Automaton in a few steps: 13 | * 1.) Create a Space (Grid for ex.) 14 | * 2.) Populate the space/grid with a list of cells 15 | * 3.) Build connections between cells - neighborhoods 16 | * 4.) Define evolution rules 17 | * 18 | */ 19 | public abstract class CellularGridBuilder 20 | { 21 | 22 | //protected ICellFactory cellFactory; 23 | protected ICell cellPrototype; 24 | 25 | public CellularGridBuilder(ICell cellPrototype) 26 | { 27 | this.cellPrototype = cellPrototype; 28 | //this.cellularGrid = CreateCellularGrid(); 29 | } 30 | 31 | 32 | protected Grid2d cellularGrid; 33 | 34 | public Grid2d BuildCellularGrid() 35 | { 36 | //create the grid 37 | cellularGrid = CreateCellularGrid(); 38 | 39 | //add cells to the grid: 40 | PopulateGrid(); 41 | 42 | //initialize cell state: 43 | 44 | ICAConfig initialConfiguration = GetInitialConfiguration(); 45 | if (initialConfiguration != null) 46 | foreach (ICell cell in cellularGrid.GetObjects()) 47 | cell.SetState(initialConfiguration.GetCellState(cell)); 48 | 49 | 50 | //build neighborhoods: 51 | foreach (ICell cell in cellularGrid.GetObjects()) 52 | ((ICell)cell).SetNeighbors(GetNeighborhoodStrategy().BuildNeighborhood(cell)); 53 | 54 | //return the grid 55 | return cellularGrid; 56 | } 57 | 58 | 59 | 60 | protected abstract Grid2d CreateCellularGrid(); 61 | 62 | protected abstract void PopulateGrid(); 63 | /* 64 | protected void PopulateGrid() 65 | { 66 | //Populate the grid: 67 | int index = 0; 68 | //populate the grid with cells 69 | for (int r = 0; r < cellularGrid.GetXDimension(); r++) 70 | { 71 | for (int c = 0; c < cellularGrid.GetYDimension(); c++) 72 | { 73 | Grid2d.Location position = new Grid2d.Location(r, c); 74 | ICell cell = cellPrototype.Clone(); 75 | cell.SetId(index); 76 | cellularGrid.Add(cell, position); 77 | index++; 78 | } 79 | } 80 | }*/ 81 | 82 | 83 | protected abstract ICAConfig GetInitialConfiguration(); 84 | 85 | protected abstract INeighborhoodStrategy GetNeighborhoodStrategy(); 86 | 87 | 88 | 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_DiscreteTime.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Data; 9 | using Grasshopper.Kernel.Types; 10 | 11 | using Rabbit.Properties; 12 | using Rabbit.Kernel.CellularAutomata; 13 | 14 | namespace Rabbit.GH.CellularAutomata 15 | { 16 | /** 17 | * GH Component that gets a Cellular Automata as an input and makes it evolves. The Evolution changes the state of the CA. 18 | * 19 | */ 20 | public class Component_DiscreteTime : Component_CABase 21 | { 22 | 23 | //value, kept for each instance of the Component 24 | private int CurrentTime = 0; 25 | 26 | private bool lastResetValue = true; 27 | 28 | /** 29 | * Constructor 30 | */ 31 | public Component_DiscreteTime() 32 | : base("Discrete Time", "Time", "Discrete time controls the evolution of a Cellular Automaton.") 33 | { 34 | } 35 | 36 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 37 | { 38 | inputManager.Register_BooleanParam("Reset", "r", "Time reset.", false, GH_ParamAccess.item); 39 | } 40 | 41 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 42 | { 43 | outputManager.Register_IntegerParam("Discrete time", "t", "Discrete time");//name, nick, description 44 | } 45 | 46 | 47 | /** 48 | * Does the computation 49 | */ 50 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 51 | { 52 | 53 | Boolean resetValue = false; 54 | DA.GetData(0, ref resetValue); 55 | 56 | if (lastResetValue != resetValue) //user clicked the toggle 57 | { 58 | this.CurrentTime = 0; 59 | this.lastResetValue = resetValue; 60 | } 61 | else // default behavior: increment the time 62 | this.CurrentTime++; 63 | 64 | //set the output parameters 65 | DA.SetData(0, this.CurrentTime); 66 | 67 | } 68 | 69 | /** 70 | * Controller component 71 | */ 72 | public override GH_Exposure Exposure 73 | { 74 | get 75 | { 76 | return GH_Exposure.quarternary; 77 | } 78 | } 79 | 80 | /** 81 | * The Guid of the component 82 | */ 83 | public override Guid ComponentGuid 84 | { 85 | get { 86 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A290431398}"); 87 | } 88 | } 89 | 90 | 91 | /** 92 | * The icon of the component 93 | */ 94 | protected override Bitmap Internal_Icon_24x24 95 | { 96 | get 97 | { 98 | return Resources.Custom_24x24_Time; 99 | } 100 | } 101 | 102 | 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/CellWrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.CellularAutomata.Cells 6 | { 7 | /** 8 | * Wraps a cell and attach an object to that cell. 9 | * 10 | * NOTE: CellWrapper does not do the job because ICells are casted to specific types 11 | * FIXME: use a custom TypeConvertor 12 | * so that (LivingCell) CellWrapper acutally calls (LivingCell)cellWrapper.getCell() 13 | * http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx 14 | */ 15 | public class CellWrapper:ICell 16 | { 17 | 18 | private ICell wrappedCell; 19 | //the attached object 20 | private O obj; 21 | 22 | public CellWrapper(ICell wrappedCell, O obj) 23 | { 24 | this.wrappedCell = wrappedCell; 25 | this.obj = obj; 26 | } 27 | 28 | 29 | //ICell delegates -------------------------------------------- 30 | public Object GetId() 31 | { 32 | return wrappedCell.GetId(); 33 | } 34 | 35 | public void SetId(Object id) 36 | { 37 | wrappedCell.SetId(id); 38 | } 39 | 40 | public Object GetAttachedObject() 41 | { 42 | return wrappedCell.GetAttachedObject(); 43 | } 44 | 45 | public void SetAttachedObject(Object obj) 46 | { 47 | wrappedCell.SetAttachedObject(obj); 48 | } 49 | 50 | public CellState GetState() 51 | { 52 | return wrappedCell.GetState(); 53 | } 54 | 55 | public IList GetFiniteStates() 56 | { 57 | return wrappedCell.GetFiniteStates(); 58 | } 59 | 60 | public void SetState(CellState cellState) 61 | { 62 | wrappedCell.SetState(cellState); 63 | } 64 | 65 | public CellState GetDefaultState() 66 | { 67 | return wrappedCell.GetDefaultState(); 68 | } 69 | 70 | public IList GetRules() 71 | { 72 | return wrappedCell.GetRules(); 73 | } 74 | 75 | public void SetTransitionRules(List transitionRules) 76 | { 77 | wrappedCell.SetTransitionRules(transitionRules); 78 | } 79 | 80 | public IList GetNeighbors() 81 | { 82 | return wrappedCell.GetNeighbors(); 83 | } 84 | 85 | public void SetNeighbors(IList neighbors) 86 | { 87 | wrappedCell.SetNeighbors(neighbors); 88 | } 89 | 90 | public ICell Clone() 91 | { 92 | return wrappedCell.Clone(); 93 | } 94 | 95 | public CellState UpdateState() 96 | { 97 | return wrappedCell.UpdateState(); 98 | } 99 | 100 | //CellWrapper ----------------------------------------------------- 101 | public ICell GetCell() 102 | { 103 | return wrappedCell; 104 | } 105 | 106 | public O GetObject() 107 | { 108 | return obj; 109 | } 110 | 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Kernel/Geometry/Util/SurfaceUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.Geometry.Util 6 | { 7 | /** 8 | * Utility class that provides some common features related to surface manipulation 9 | * 10 | */ 11 | public class SurfaceUtils 12 | { 13 | 14 | private SurfaceUtils() { } 15 | 16 | /* 17 | public static SurfaceFromPointGrid(List points, int onUPointsCount, ) { 18 | { 19 | int num; 20 | List list = new List(); 21 | GH_Boolean destination = null; 22 | if (iData.GetData(1, out num) && iData.GetData(2, out destination)) 23 | { 24 | if (num < 2) 25 | { 26 | base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid UCount value. Count must be larger than 1."); 27 | } 28 | else if (iData.GetDataList(0, list)) 29 | { 30 | int num2 = Convert.ToInt32((double) (((double) list.Count) / ((double) num))); 31 | if ((num2 * num) != list.Count) 32 | { 33 | base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The UCount value is not valid for this amount of points."); 34 | } 35 | else 36 | { 37 | OnNurbsSurface surface; 38 | int[] numArray2 = new int[] { num, num2 }; 39 | int[] degree = new int[] { Math.Min(3, num - 1), Math.Min(3, num2 - 1) }; 40 | bool[] flagArray = new bool[] { false, false }; 41 | On3dPointArray array = new On3dPointArray(list.Count); 42 | int num4 = list.Count - 1; 43 | for (int i = 0; i <= num4; i++) 44 | { 45 | if (!list[i].IsValid) 46 | { 47 | this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "A point in the grid failed to load. Fitting operation aborted"); 48 | return; 49 | } 50 | array.Append(list[i].Point); 51 | } 52 | if (destination.Value) 53 | { 54 | surface = RhUtil.RhinoSrfPtGrid(numArray2, degree, flagArray, array); 55 | } 56 | else 57 | { 58 | surface = RhUtil.RhinoSrfControlPtGrid(numArray2, degree, array); 59 | } 60 | if (surface == null) 61 | { 62 | base.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Surface could not be fitted"); 63 | } 64 | else 65 | { 66 | iData.SetData(0, new GH_Surface(surface)); 67 | } 68 | } 69 | } 70 | } 71 | } 72 | 73 | 74 | */ 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Kernel/LSystems/LSystemParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Language; 6 | 7 | namespace Rabbit.Kernel.LSystems 8 | { 9 | /** 10 | * 11 | * LSystem Interpreter interprets a specified LSystem and generates a LSystemLanguage described by that LSystem rewrite system( which is a variant of formal grammar) 12 | * 13 | * 14 | * 15 | */ 16 | public class LSystemParser:IParser 17 | { 18 | public static Char RuleDerivationChar = '='; 19 | 20 | 21 | public static LSystemParser Instance = new LSystemParser(); 22 | 23 | private ILexer lexer; 24 | 25 | protected LSystemParser() 26 | { 27 | char[] separators = {}; 28 | lexer = new SimpleLexer(separators); 29 | } 30 | 31 | /** 32 | * Parses a specified String source written in a specified format that describes the Grammar of the LSystem. 33 | * That means the axiom, the rules, etc.. 34 | * 35 | */ 36 | public /*AbstractLSystem*/Object Parse(ITokenStream input) 37 | { 38 | //should returna an Abstract LSystem 39 | throw new NotImplementedException(); 40 | } 41 | 42 | 43 | /** 44 | * Parses a ProductionRule object from the specified source string 45 | * 46 | * 47 | */ 48 | public ProductionRule ParseRule(String source) 49 | { 50 | try 51 | { 52 | //DUMB implementation for now 53 | //Use a real language parser!! 54 | 55 | //the simplest case: OL Production Rule: 56 | //TODO: check for an exception 57 | 58 | String predecessorStr = source.Substring(0, source.IndexOf(RuleDerivationChar)); 59 | Symbol predecessor = Symbol.valueOf(predecessorStr); 60 | int arrowIndex = source.IndexOf(RuleDerivationChar); 61 | int length = source.Length; 62 | 63 | 64 | String successorString = source.Substring(arrowIndex + 1, length - arrowIndex - 1);//index, length 65 | Word successor = ParseWord(successorString); 66 | //System.Console.WriteLine(source.IndexOf(ArrowToken)); 67 | //System.Console.WriteLine(predecessorStr); 68 | //System.Console.WriteLine(successor); 69 | 70 | return new OLProductionRule(predecessor, successor); 71 | } 72 | catch (Exception e) 73 | { 74 | throw new InvalidOperationException("Invalid production rule format: "+source+"! Please use the following format: PREDECESSOR=SUCCESSOR"); 75 | } 76 | } 77 | 78 | public Word ParseWord(String wordSource) 79 | { 80 | Word word = new Word(); 81 | 82 | //using the lexer to parse the character sequence into symbols 83 | ITokenStream tokenStream = lexer.Analyze(wordSource); 84 | Token nextToken = null; 85 | while ((nextToken = tokenStream.NextToken()) != null) 86 | { 87 | word.Append(Symbol.valueOf(nextToken.ToString()));//in this case a token = a symbol 88 | } 89 | 90 | return word; 91 | } 92 | 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /GH/Component_RabbitBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Grasshopper.Kernel; 5 | using System.Drawing; 6 | using Rabbit.Properties; 7 | 8 | namespace Rabbit.GH 9 | { 10 | 11 | /** 12 | * Base component for all components within Rabbit. 13 | * Every component should inherit this one. 14 | * Provides place for common code shared by every component within Rabbit. 15 | * 16 | * @author HTTP://MORPHOCODE.COM 17 | * 18 | */ 19 | public abstract class Component_RabbitBase : GH_Component 20 | { 21 | 22 | public static String RABBIT_CATEGORY = "Rabbit"; 23 | 24 | /** 25 | * Constructor 26 | */ 27 | public Component_RabbitBase(String name, String nick, String description, String subCategory): 28 | base(name, nick, "MORPHOCODE: " + description, Component_RabbitBase.RABBIT_CATEGORY, subCategory)//base(name, abbreviation, description, category, subcategory) 29 | { 30 | 31 | } 32 | 33 | 34 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 35 | { 36 | //common code here 37 | } 38 | 39 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 40 | { 41 | //common code here 42 | } 43 | 44 | /** 45 | * Provides place for common logic for each component 46 | * 47 | */ 48 | protected sealed override void SolveInstance(IGH_DataAccess DA) 49 | { 50 | 51 | //do the Component computation 52 | SolveRabbitInstance(DA); 53 | } 54 | 55 | /** 56 | * Rabbit components should implement this method instead of the standard GH SolveInstance 57 | */ 58 | protected abstract void SolveRabbitInstance(IGH_DataAccess DA); 59 | 60 | /** 61 | * The Guid of the component 62 | */ 63 | public override Guid ComponentGuid 64 | { 65 | get { 66 | return new Guid("{AFFF17BD-5975-460b-9883-525AE0677088}"); 67 | } 68 | } 69 | 70 | /** 71 | * The icon of the component 72 | * 73 | protected override Bitmap Internal_Icon_24x24 74 | { 75 | get 76 | { 77 | return Resources.Resources.Custom_24x24_Rabbit; 78 | } 79 | }*/ 80 | 81 | /* 82 | protected override string HelpDescription 83 | { 84 | get 85 | { 86 | return (this.Description + " Distance is measured from the tangent discontinuities along the curve. Since it is highly unlikely that 3D curves have coplanar segments a given distance from the corners, fillet segments will be composed of Bi-Arcs rather than single arcs."); 87 | } 88 | }*/ 89 | 90 | /* 91 | * //specifies whether the component is displayed in the toolbar or only in the drop down list of subcategory 92 | public override GH_Exposure Exposure 93 | { 94 | get 95 | { 96 | return GH_Exposure.dropdown_only; 97 | } 98 | } 99 | * */ 100 | 101 | 102 | 103 | 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /Kernel/Systems/DynamicalSystems/TimedMemory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace Rabbit.Kernel.Systems.DynamicalSystems 7 | { 8 | /** 9 | * Memory that stores Timed states 10 | * 11 | */ 12 | public class TimedMemory 13 | { 14 | 15 | //private LinkedList states; 16 | //private Stack states; 17 | //stores an object related to a specific time moment 18 | private Dictionary memoryMap; 19 | private Dictionary reverseMemoryMap; 20 | 21 | 22 | private O lastObject; 23 | //private int currentTime; 24 | 25 | public TimedMemory() 26 | { 27 | //states = new LinkedList(); 28 | //states = new Stack(); 29 | //currentTime = 0; 30 | this.memoryMap = new Dictionary(); 31 | this.reverseMemoryMap = new Dictionary< O, int>(); 32 | } 33 | 34 | public void Save(int time, O obj ) 35 | { 36 | //ts.SetTime(currentTime); 37 | //currentTime++; 38 | //states.AddLast(ts); 39 | //states.Push(ts); 40 | memoryMap.Add(time, obj); 41 | reverseMemoryMap.Add(obj, time); 42 | lastObject = obj; 43 | } 44 | 45 | public O GetObject(int time) 46 | { 47 | /* 48 | foreach (O obj in states) 49 | if (timedState.GetDiscreteTime() == time) 50 | return timedState; 51 | 52 | //no State for that time 53 | return default(TS); 54 | */ 55 | if(memoryMap.ContainsKey(time)) 56 | return memoryMap[time]; 57 | else 58 | return default(O); 59 | } 60 | 61 | public O GetLastState() 62 | { 63 | //return states.Last.Value; 64 | //return states.Pop(); 65 | return lastObject; 66 | } 67 | 68 | //returns the time moment associated with this object 69 | public int GetTime(O obj) 70 | { 71 | return reverseMemoryMap[obj]; 72 | } 73 | 74 | //returns all states from t=0 to time=t 75 | public ICollection GetStates(int time) 76 | { 77 | List states = new List(); 78 | foreach(KeyValuePair pair in memoryMap) 79 | if(pair.Key<=time) 80 | states.Add(pair.Value); 81 | //return memoryMap.Values; 82 | return states; 83 | } 84 | 85 | public ICollection GetObjects() 86 | { 87 | //return states; 88 | return memoryMap.Values; 89 | //return states.ToArray(); 90 | } 91 | 92 | public void Clear() 93 | { 94 | //states.Clear(); 95 | //currentTime = 0; 96 | memoryMap.Clear(); 97 | } 98 | 99 | //removes the last 'n' states 100 | public void Clear(int numberOfStates) 101 | { 102 | /* 103 | currentTime -= numberOfStates; 104 | 105 | for (int i = 0; i < numberOfStates; i++) 106 | //states.Pop(); 107 | states.RemoveLast(); 108 | */ 109 | throw new NotImplementedException(); 110 | } 111 | } 112 | } 113 | 114 | -------------------------------------------------------------------------------- /GH/CellularAutomata/GH_CellState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Grasshopper.Kernel; 6 | using Grasshopper.Kernel.Types; 7 | 8 | using Rabbit.Kernel.CellularAutomata; 9 | 10 | namespace Rabbit.GH.CellularAutomata 11 | { 12 | /** 13 | * CellState that wraps GH primitives. 14 | * Overrides Equals method by comparing the inner values of the Goo objects 15 | * 16 | * 17 | */ 18 | public class GH_CellState:CellState 19 | { 20 | //private GH_TypeHandler typeHandler; 21 | 22 | public GH_CellState(IGH_Goo goo):base(goo) 23 | { 24 | } 25 | 26 | public override Boolean Equals(Object that) 27 | { 28 | if (that == null) return false; 29 | if (that.GetType() != this.GetType()) return false; 30 | 31 | //FIXME: use a reflection to get the Value property of the GH_Goo 32 | //if a value property is missing, it seems that we could not compare Goos (or different method should be used) 33 | 34 | // GH_Goo.Equals(GH_Goo) compare the GUIDs which are different in the common case 35 | // that's why we need to compare GH_Goo inner values 36 | // unfortunately the Value property is not generic for GH_Goo 37 | // only some GH_Goo have value assigned 38 | GH_CellState thatState = (GH_CellState)that; 39 | 40 | if (value.GetType() == GH_TypeLib.t_gh_colour) 41 | { 42 | GH_Colour thisGH_Colour = (GH_Colour)value; 43 | GH_Colour thatGH_Colour = (GH_Colour)thatState.value; 44 | 45 | return thisGH_Colour.Value.Equals(thatGH_Colour.Value); 46 | } 47 | else if (value.GetType() == GH_TypeLib.t_gh_bool) 48 | { 49 | GH_Boolean thisGH_Boolean = (GH_Boolean)this.value; 50 | GH_Boolean thatGH_Boolean = (GH_Boolean)thatState.value; 51 | return thisGH_Boolean.Value.Equals(thatGH_Boolean.Value); 52 | } 53 | else if (value.GetType() == GH_TypeLib.t_gh_int) 54 | { 55 | GH_Integer thisGH_Integer = (GH_Integer)this.value; 56 | GH_Integer thatGH_Integer = (GH_Integer)thatState.value; 57 | return thisGH_Integer.Value.Equals(thatGH_Integer.Value); 58 | } 59 | else if (value.GetType() == GH_TypeLib.t_gh_string) 60 | { 61 | GH_String thisGH_String = (GH_String)this.value; 62 | GH_String thatGH_String = (GH_String)thatState.value; 63 | return thisGH_String.Value.Equals(thatGH_String.Value); 64 | } 65 | 66 | throw new NotSupportedException("Could not compare GH_Goo type: " + value.GetType()); 67 | } 68 | 69 | public override int GetHashCode() 70 | { 71 | if (value.GetType() == GH_TypeLib.t_gh_colour) 72 | return ((GH_Colour)value).Value.GetHashCode(); 73 | else if (value.GetType() == GH_TypeLib.t_gh_bool) 74 | return ((GH_Boolean)value).Value.GetHashCode(); 75 | else if (value.GetType() == GH_TypeLib.t_gh_int) 76 | return ((GH_Integer)value).Value.GetHashCode(); 77 | else if (value.GetType() == GH_TypeLib.t_gh_string) 78 | return ((GH_String)value).Value.GetHashCode(); 79 | else 80 | throw new NotSupportedException("Could not get the hashCode of GH_Goo type: " + value.GetType()); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_CAEvolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Data; 9 | using Grasshopper.Kernel.Types; 10 | 11 | using Rabbit.Properties; 12 | using Rabbit.Kernel.CellularAutomata; 13 | using Rabbit.Kernel.CellularAutomata.Configuration; 14 | using Rabbit.Kernel.Systems.DynamicalSystems; 15 | 16 | 17 | namespace Rabbit.GH.CellularAutomata 18 | { 19 | /** 20 | * GH Component that gets a Cellular Automata as an input and makes it evolves. The Evolution changes the state of the CA. 21 | * 22 | */ 23 | public class Component_CAEvolver : Component_CABase 24 | { 25 | 26 | /** 27 | * Constructor 28 | */ 29 | public Component_CAEvolver() 30 | : base("CA Evolver", "CA Evolver", "Drives the evolution of the cellular automaton according to the specified time. ") 31 | { 32 | } 33 | 34 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 35 | { 36 | inputManager.Register_IntegerParam("Discrete time", "t", "Discrete Time.", 0, GH_ParamAccess.item); 37 | inputManager.Register_GenericParam("Cellular Automata", "CA", "Cellular Automaton.", GH_ParamAccess.item);//name, nick, description, defaul, isList 38 | } 39 | 40 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 41 | { 42 | outputManager.Register_GenericParam("CA Configuration", "C", "The last CA configuration calculated for time=t");//name, nick, description 43 | outputManager.Register_GenericParam("Memory", "M", "Memory, containg a list of all CA configurations");//name, nick, description 44 | } 45 | 46 | 47 | /** 48 | * Does the computation 49 | */ 50 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 51 | { 52 | GH_ObjectWrapper CAWrapper = null; 53 | //get the input parameters 54 | DA.GetData(1, ref CAWrapper);//param index, place holder 55 | 56 | //unwrap the CA object 57 | CA CA = (CA) CAWrapper.Value; 58 | 59 | //get the time 60 | int time = 0; 61 | DA.GetData(0, ref time); 62 | 63 | DiscreteTimer.Instance.SetTime(time); 64 | 65 | ICAConfig configuration = CA.GetCurrentConfiguration();//CalculateConfiguration(time); 66 | 67 | //set the output parameters 68 | DA.SetData(0, configuration); 69 | DA.SetDataList(1, CA.GetMemory().GetStates(DiscreteTimer.Instance.GetTime())); 70 | //DA.SetData(2, CA); 71 | 72 | } 73 | 74 | /** 75 | * Controller component 76 | */ 77 | public override GH_Exposure Exposure 78 | { 79 | get 80 | { 81 | return GH_Exposure.quarternary; 82 | } 83 | } 84 | 85 | /** 86 | * The Guid of the component 87 | */ 88 | public override Guid ComponentGuid 89 | { 90 | get { 91 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A290598398}"); 92 | } 93 | } 94 | 95 | 96 | /** 97 | * The icon of the component 98 | */ 99 | protected override Bitmap Internal_Icon_24x24 100 | { 101 | get 102 | { 103 | return Resources.Custom_24x24_Evolve; 104 | } 105 | } 106 | 107 | 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_CustomStateConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Types; 9 | using Grasshopper.Kernel.Data; 10 | using Grasshopper.GUI; 11 | using Grasshopper; 12 | 13 | using Rabbit.Properties; 14 | using Rabbit.Kernel.CellularAutomata; 15 | using Rabbit.Kernel.CellularAutomata.Cells; 16 | using Rabbit.Kernel.ABMS.Space.Grid; 17 | using Rabbit.Kernel.Geometry.Util; 18 | 19 | using Rabbit.GH.CellularAutomata; 20 | 21 | 22 | using RMA.OpenNURBS; 23 | 24 | namespace Rabbit.GH.CellularAutomata 25 | { 26 | /** 27 | * GH Component that create a Cellular Automata object, based on specified number of rows and columns 28 | */ 29 | public class CustomStateConfigComponent : Component_CABase 30 | { 31 | 32 | /** 33 | * Constructor 34 | */ 35 | public CustomStateConfigComponent() 36 | : base("Custom State configuration", "C State", "Custom State Configuration. Specifies custom state for a selection of cells/points") 37 | { 38 | } 39 | 40 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 41 | { 42 | inputManager.Register_PointParam("Points", "P", "Selection of Points/cells with a custom initial state", GH_ParamAccess.list);//name, nick, description, defaul, isList 43 | inputManager.Register_GenericParam("Custom initial State", "S", "Custom initial State for the selected cells/points", GH_ParamAccess.item);//name, nick, description, defaul, isList 44 | } 45 | 46 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 47 | { 48 | outputManager.Register_GenericParam("Custom State Configuration", "SC", "Custom State Configuration");//name, nick, description 49 | } 50 | 51 | /** 52 | * Does the computation 53 | */ 54 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 55 | { 56 | 57 | 58 | //get the user-defined points that define custom Cell State 59 | List userConfigurationGHPoints = new List(); 60 | DA.GetDataList(0, userConfigurationGHPoints); 61 | 62 | //Get the specified initial state: 63 | IGH_Goo gooCellStateValue = null; 64 | DA.GetData(1, ref gooCellStateValue); 65 | //CellState initialCellState = new CellState(GH_TypeUtils.ConvertToSystemType(gooCellStateValue)); 66 | CellState initialCellState = new GH_CellState(gooCellStateValue); 67 | 68 | //creates the Cellular space 69 | OnStateConfig stateConfig = new OnStateConfig(GH_PointUtils.ConvertToOnPoints(userConfigurationGHPoints), initialCellState); 70 | 71 | //set the output parameters 72 | DA.SetData(0, stateConfig); 73 | } 74 | 75 | /** 76 | * The Guid of the component 77 | */ 78 | public override Guid ComponentGuid 79 | { 80 | get { 81 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9B310218397}"); 82 | } 83 | } 84 | 85 | /** 86 | * Space/Config component 87 | */ 88 | public override GH_Exposure Exposure 89 | { 90 | get 91 | { 92 | return GH_Exposure.tertiary; 93 | } 94 | } 95 | 96 | /** 97 | * The icon of the component 98 | */ 99 | protected override Bitmap Internal_Icon_24x24 100 | { 101 | get 102 | { 103 | return Resources.Custom_24x24_CustomStateConfig; 104 | } 105 | } 106 | 107 | 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_RandomStateConfig.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Types; 9 | using Grasshopper.Kernel.Data; 10 | using Grasshopper.GUI; 11 | using Grasshopper; 12 | 13 | using Rabbit.Properties; 14 | using Rabbit.Kernel.CellularAutomata; 15 | using Rabbit.Kernel.CellularAutomata.Configuration; 16 | using Rabbit.Kernel.CellularAutomata.Cells; 17 | using Rabbit.Kernel.ABMS.Space.Grid; 18 | using Rabbit.Kernel.Geometry.Util; 19 | using Rabbit.GH.CellularAutomata; 20 | 21 | using RMA.OpenNURBS; 22 | 23 | namespace Rabbit.GH.CellularAutomata 24 | { 25 | /** 26 | * GH Component that create a Random configuration for a set of states 27 | */ 28 | public class RandomStateConfigComponent : Component_CABase 29 | { 30 | 31 | /** 32 | * Constructor 33 | */ 34 | public RandomStateConfigComponent() 35 | : base("Random State configuration", "R State", "Random State configuration") 36 | { 37 | } 38 | 39 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 40 | { 41 | inputManager.Register_GenericParam("Set of states", "S", "A Set of valid states for the current Cell Prototype.", GH_ParamAccess.list);//name, nick, description, defaul, isList 42 | } 43 | 44 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 45 | { 46 | outputManager.Register_GenericParam("Random State Configuration", "R", "Random State Configuration");//name, nick, description 47 | } 48 | 49 | 50 | protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown iMenu) 51 | { 52 | ToolStripMenuItem item = GH_DocumentObject.Menu_AppendGenericMenuItem(iMenu, "New Random Configuration...", new EventHandler(this.Menu_NewRandomConfigClicked), null, null, true, false); 53 | item.Font = new Font(item.Font, FontStyle.Bold); 54 | } 55 | 56 | private void Menu_NewRandomConfigClicked(Object sender, EventArgs ea) 57 | { 58 | //update the solution 59 | base.ExpireSolution(true); 60 | } 61 | 62 | /** 63 | * Does the computation 64 | */ 65 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 66 | { 67 | List states = new List(); 68 | 69 | //get all Goo states 70 | List gooStates = new List(); 71 | DA.GetDataList(0, gooStates); 72 | foreach (IGH_Goo gooState in gooStates) 73 | states.Add(new GH_CellState(gooState)); 74 | 75 | //creates the Ranom configuration 76 | RandomCAConfig randomConfig = new RandomCAConfig(states); 77 | 78 | //set the output parameters 79 | DA.SetData(0, randomConfig); 80 | } 81 | 82 | /** 83 | * The Guid of the component 84 | */ 85 | public override Guid ComponentGuid 86 | { 87 | get { 88 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9B330211697}"); 89 | } 90 | } 91 | 92 | /** 93 | * Space/Config component 94 | */ 95 | public override GH_Exposure Exposure 96 | { 97 | get 98 | { 99 | return GH_Exposure.tertiary; 100 | } 101 | } 102 | 103 | /** 104 | * The icon of the component 105 | */ 106 | protected override Bitmap Internal_Icon_24x24 107 | { 108 | get 109 | { 110 | return Resources.Custom_24x24_RandomStateConfig; 111 | } 112 | } 113 | 114 | 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /GH/LSystems/Component_TubeSettings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Drawing; 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | 7 | using Rhino.Geometry; 8 | 9 | using Grasshopper.Kernel; 10 | using Grasshopper.Kernel.Types; 11 | 12 | using Rabbit.Properties; 13 | using Rabbit.Kernel.TurtleGraphics; 14 | using Rabbit.Kernel.TurtleGraphics.Commands; 15 | using Rabbit.Kernel.RLogo; 16 | 17 | using RMA.OpenNURBS; 18 | 19 | 20 | 21 | namespace Rabbit.GH.LSystems 22 | { 23 | /** 24 | * GH Component 25 | * 26 | * @Author MORPHOCODE.COM 27 | */ 28 | public class Component_TubeSettings : Component_LSBase 29 | { 30 | 31 | 32 | /** 33 | * Constructor 34 | */ 35 | public Component_TubeSettings() 36 | : base("Tube Settings", "Tube S.", "Custom tube settings.") 37 | { 38 | } 39 | 40 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 41 | { 42 | 43 | inputManager.Register_DoubleParam("Thickness", "T", "Default thickness", 1.0, GH_ParamAccess.item);//name, nick, description, default, isList 44 | inputManager.Register_DoubleParam("Thickness scale", "dT", "Default thickness scale", 0.9, GH_ParamAccess.item);//name, nick, description, default, isList 45 | //profile settings 46 | inputManager.Register_CurveParam("Profile curve", "P", "Default profile curve", GH_ParamAccess.item);//name, nick, description, default, isList 47 | inputManager.Register_PlaneParam("Profile Pivot", "PP", "Profile pivot plane", Plane.WorldXY, GH_ParamAccess.item);//name, nick, description, default, isList 48 | } 49 | 50 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 51 | { 52 | pManager.Register_GenericParam("Tube Settings", "TS", "Tube Settings");//name, nick, description 53 | } 54 | 55 | /** 56 | * Does the computation 57 | */ 58 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 59 | { 60 | 61 | //GET THE INPUT PARAMS: 62 | Double defaultThickness = 1.0; 63 | DA.GetData(0, ref defaultThickness); 64 | 65 | Double defaultThicknessScale = 0.7; 66 | DA.GetData(1, ref defaultThicknessScale);//param index, variable 67 | 68 | //Curve defaultProfile = new Circle(5.0).ToNurbsCurve(); 69 | Curve profile = null; 70 | DA.GetData(2, ref profile);//param index, variable 71 | 72 | Plane profilePivot = Plane.Unset; 73 | DA.GetData(3, ref profilePivot);//param index, variable 74 | 75 | //init the LoftSkeletonSettings object 76 | GH_TubeSettings settings = new GH_TubeSettings(defaultThickness, defaultThicknessScale, profile, profilePivot); 77 | 78 | //Set the resulting Graphics in the output: 79 | DA.SetData(0, settings); 80 | 81 | } 82 | 83 | /** 84 | * Interpretative part of the LSystems 85 | * secondary exposure 86 | */ 87 | public override GH_Exposure Exposure 88 | { 89 | get 90 | { 91 | return GH_Exposure.tertiary; 92 | } 93 | } 94 | 95 | /** 96 | * The Guid of the component 97 | */ 98 | public override Guid ComponentGuid 99 | { 100 | get { 101 | return new Guid("{9D2582DD-6CF5-497a-8C40-C9E245391672}"); 102 | } 103 | } 104 | 105 | 106 | /** 107 | * The icon of the component 108 | */ 109 | protected override Bitmap Internal_Icon_24x24 110 | { 111 | get 112 | { 113 | return Resources.Custom_24x24_LoftSkeletonSettings; 114 | } 115 | } 116 | 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Neighborhood/MooreNeighborhood.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.ABMS.Space.Grid; 6 | 7 | namespace Rabbit.Kernel.CellularAutomata 8 | { 9 | /** 10 | * In cellular automata, the Moore neighborhood comprises the eight cells surrounding a central cell on a two-dimensional square lattice. The neighborhood is named after Edward F. Moore, a pioneer of cellular automata theory. It is one of the two most commonly used neighborhood types, the other one being the 4-cell von Neumann neighborhood. The well known Conway's Game of Life, for example, uses the Moore neighborhood. It is similar to the notion of 8-connected pixels in computer graphics. 11 | * The concept can be extended to higher dimensions, for example forming a 26-cell cubic neighborhood for a cellular automaton in three dimensions. 12 | * The Moore neighbourhood of a point is the points at a Chebyshev distance of 1. 13 | * 14 | */ 15 | public class MooreNeighborhoodStrategy:INeighborhoodStrategy 16 | { 17 | 18 | private Grid2d grid2d; 19 | 20 | //whether or not to include the cell itself in the neighbourhood 21 | private Boolean includeCell; 22 | 23 | public MooreNeighborhoodStrategy(Grid2d regularCellularLattice, bool includeCell) 24 | { 25 | this.grid2d = regularCellularLattice; 26 | this.includeCell = includeCell; 27 | } 28 | 29 | 30 | /** 31 | * Builds the neighbors list for this cell. 32 | */ 33 | public IList BuildNeighborhood(ICell cell) 34 | { 35 | return BuildMooreNeighbors(cell); 36 | } 37 | 38 | private int GetWestX(int x) 39 | { 40 | if (x == 0) return grid2d.GetXDimension() - 1;//inifinite boundary - torus 41 | else return x - 1; 42 | } 43 | 44 | private int GetEastX(int x) 45 | { 46 | if (x == grid2d.GetXDimension() - 1) return 0;//inifinite boundary - torus 47 | else return x + 1; 48 | } 49 | 50 | private int GetNorthY(int Y) 51 | { 52 | if (Y == grid2d.GetYDimension() - 1) return 0;//inifinite boundary - torus 53 | else return Y + 1; 54 | } 55 | 56 | private int GetSouthY(int Y) 57 | { 58 | if (Y == 0) return grid2d.GetYDimension() - 1;//inifinite boundary - torus 59 | else return Y - 1; 60 | } 61 | 62 | /** 63 | * @return the so-called Moore neighborhood: the 8 neighbors of a cell at North, South, East, West, NW, NE, SW, SE 64 | */ 65 | private IList BuildMooreNeighbors(ICell cell) 66 | { 67 | IList neighbors = new List(); 68 | Grid2d.Location cellPosition = grid2d.GetPosition(cell); 69 | int X = cellPosition.GetX(); 70 | int Y = cellPosition.GetY(); 71 | 72 | ICell northNeighbor = grid2d.GetCell(X, GetNorthY(Y)); 73 | ICell southNeighbor = grid2d.GetCell(X, GetSouthY(Y)); 74 | ICell eastNeighbor = grid2d.GetCell(GetEastX(X), Y); 75 | ICell westNeighbor = grid2d.GetCell(GetWestX(X), Y); 76 | ICell northEastNeighbor = grid2d.GetCell(GetEastX(X), GetNorthY(Y)); 77 | ICell northWestNeighbor = grid2d.GetCell(GetWestX(X), GetNorthY(Y)); 78 | ICell southEastNeighbor = grid2d.GetCell(GetEastX(X), GetSouthY(Y)); 79 | ICell southWestNeighbor = grid2d.GetCell(GetWestX(X), GetSouthY(Y)); 80 | 81 | 82 | //add the neighbors to the list, if they exist(at the boundary cells have no all of the 4 neighbors) 83 | if (northNeighbor != null) neighbors.Add(northNeighbor); 84 | if (southNeighbor != null) neighbors.Add(southNeighbor); 85 | if (eastNeighbor != null) neighbors.Add(eastNeighbor); 86 | if (westNeighbor != null) neighbors.Add(westNeighbor); 87 | if (northEastNeighbor != null) neighbors.Add(northEastNeighbor); 88 | if (northWestNeighbor != null) neighbors.Add(northWestNeighbor); 89 | if (southEastNeighbor != null) neighbors.Add(southEastNeighbor); 90 | if (southWestNeighbor != null) neighbors.Add(southWestNeighbor); 91 | 92 | //add the cell-itself to the neighborhood 93 | if (includeCell) 94 | neighbors.Add(cell); 95 | 96 | return neighbors; 97 | } 98 | 99 | 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_ExcitableCell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Types; 9 | using Grasshopper.Kernel.Data; 10 | using Grasshopper.GUI; 11 | using Grasshopper; 12 | 13 | using Rabbit.Properties; 14 | using Rabbit.Kernel.CellularAutomata; 15 | using Rabbit.Kernel.CellularAutomata.Cells; 16 | using Rabbit.Kernel.CellularAutomata.Impl.Greenberg_Hastings; 17 | using Rabbit.Kernel.Geometry.Util; 18 | using Rabbit.GH.CellularAutomata; 19 | 20 | using RMA.OpenNURBS; 21 | 22 | namespace Rabbit.GH.CellularAutomata 23 | { 24 | /** 25 | * GH Component that create a Cellular Automata object, based on specified number of rows and columns 26 | */ 27 | public class ExcitableCellComponent : Component_CABase 28 | { 29 | 30 | /** 31 | * Constructor 32 | */ 33 | public ExcitableCellComponent() 34 | : base("Excitable Cell", "E Cell", "Excitable cell: represents a fraction of the excitable medium. Each cell can be in one of the three following states: Resting, Excited, Refractory.", Component_CABase.CATEGORY_CA_MODEL) 35 | { 36 | } 37 | 38 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 39 | { 40 | inputManager.Register_IntegerParam("Treshold", "T", "Treshold", GH_ParamAccess.item); 41 | inputManager.Register_GenericParam("Resting State(s)", "R", "Resting state(s)", GH_ParamAccess.item); 42 | inputManager.Register_GenericParam("Excited State", "E", "Excited state", GH_ParamAccess.list); 43 | inputManager.Register_GenericParam("Refractory State(s)", "R", "Refractory state(s)", GH_ParamAccess.list); 44 | } 45 | 46 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 47 | { 48 | outputManager.Register_GenericParam("Excitable Cell Prototype", "EC", "Excitable cell prototype.");//name, nick, description 49 | } 50 | 51 | 52 | /** 53 | * Does the computation 54 | */ 55 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 56 | { 57 | 58 | //Get the treshold 59 | int treshold = 0; 60 | DA.GetData(0, ref treshold); 61 | 62 | //Get the resting colour 63 | IGH_Goo restingColour = null; 64 | DA.GetData(1, ref restingColour); 65 | GH_CellState restingState = new GH_CellState(restingColour); 66 | 67 | //Get the excited states --------------------------------------------- 68 | List excitedColours = new List(); 69 | DA.GetDataList(2, excitedColours); 70 | List excitedStates = new List(); 71 | foreach (IGH_Goo colour in excitedColours) 72 | excitedStates.Add(new GH_CellState(colour)); 73 | 74 | //Get the refractory states ------------------------------------------ 75 | List refractoryColours = new List(); 76 | DA.GetDataList(3, refractoryColours);//param index, place holder 77 | List refractoryStates = new List(); 78 | foreach (IGH_Goo colour in refractoryColours) 79 | refractoryStates.Add(new GH_CellState(colour)); 80 | 81 | ExcitableCell prototype = new ExcitableCell(-1, restingState, refractoryStates, excitedStates, treshold);//, //EvolutionRules); 82 | 83 | //set the output parameters 84 | DA.SetData(0, prototype); 85 | } 86 | 87 | 88 | /** 89 | * Controller component 90 | */ 91 | public override GH_Exposure Exposure 92 | { 93 | get 94 | { 95 | return GH_Exposure.secondary; 96 | } 97 | } 98 | 99 | /** 100 | * The Guid of the component 101 | */ 102 | public override Guid ComponentGuid 103 | { 104 | get { 105 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A290318197}"); 106 | } 107 | } 108 | 109 | /** 110 | * The icon of the component 111 | */ 112 | protected override Bitmap Internal_Icon_24x24 113 | { 114 | get 115 | { 116 | return Resources.Custom_24x24_ExcitableMedia; 117 | } 118 | } 119 | 120 | 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /GH/LSystems/Component_LSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | 6 | using Grasshopper.Kernel; 7 | using Grasshopper.Kernel.Data; 8 | using Grasshopper.Kernel.Types; 9 | 10 | using Rabbit.Properties; 11 | using Rabbit.Kernel.TurtleGraphics; 12 | using Rabbit.Kernel.LSystems; 13 | 14 | namespace Rabbit.GH.LSystems 15 | { 16 | public class Component_LSystem:Component_LSBase 17 | { 18 | 19 | private static LSystemParser lsystemParser = LSystemParser.Instance; 20 | 21 | /** 22 | * Constructor 23 | */ 24 | public Component_LSystem() 25 | : base("LSystem", "LSystem", "LSystem, based on a specified axiom and a set of production rules.") 26 | { 27 | } 28 | 29 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 30 | { 31 | 32 | inputManager.Register_StringParam("Axiom", "A", "The Axiom is the first Word in the LSystem. It is also called 'seed' or 'initiator'.", "", GH_ParamAccess.item);//name, nick, description, default, isList 33 | inputManager.Register_GenericParam("Production Rules", "PR", "List of Production Rules used by the LSystem to generate the Words in the LSystem language.", GH_ParamAccess.list); 34 | inputManager.Register_IntegerParam("Number of generations", "n", "Number of words to be generated by the LSystem", 2, GH_ParamAccess.item); 35 | } 36 | 37 | protected override void RegisterOutputParams(GH_OutputParamManager pManager) 38 | { 39 | pManager.Register_StringParam("Word", "W", "The last word derived by the LSystem");//name, nick, description 40 | pManager.Register_GenericParam("List of Words", "LW", "List of words generated by the L-System. The list contains all words, starting by the axiom, ending with the last generated word W.");//name, nick, description 41 | pManager.Register_GenericParam("LSystem", "LS", "The LSystem object, based on the specified axiom and production rules.");//name, nick, description 42 | } 43 | 44 | /** 45 | * Does the computation 46 | */ 47 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 48 | { 49 | //PARSE THE INPUT PARAMETERS: 50 | //ALPHABET------------------------------------------------------------------------------ 51 | IList Alphabet = new List(); 52 | 53 | 54 | //SEED/INITIATOR/AXIOM------------------------------------------------------------------- 55 | String Axiom = null; 56 | DA.GetData(0, ref Axiom);//param index, place holder 57 | 58 | //init the LSystem: 59 | DeterministicLSystem LSystem = new DeterministicLSystem(Alphabet, lsystemParser.ParseWord(Axiom)); 60 | 61 | //Init the LSYSTEM + RULES---------------------------------------------------------------------------------- 62 | //Parse the Rules using the LSystemParser and add it to the LSystem 63 | List ruleStrings = new List(); 64 | DA.GetDataList(1, ruleStrings); 65 | foreach (GH_String ruleString in ruleStrings) 66 | { 67 | LSystem.AddRule(lsystemParser.ParseRule(ruleString.Value)); 68 | } 69 | 70 | //GENERATE THE STRINGS------------------------------------------------------------------- 71 | int NumberOfStrings = 0; 72 | DA.GetData(2, ref NumberOfStrings);//param index, place holder 73 | LSystem.Rewrite(NumberOfStrings); 74 | 75 | //OUTPUT 76 | //the list of words, generated by the LSystem 77 | //DA.SetDataList(0, LSystem.GetLanguage()); 78 | DA.SetData(0, LSystem.GetCurrentDerivation()); 79 | DA.SetDataList(1, LSystem.GetLanguage()); 80 | DA.SetData(2, LSystem); 81 | 82 | 83 | } 84 | 85 | /** 86 | * The Guid of the component 87 | */ 88 | public override Guid ComponentGuid 89 | { 90 | get { 91 | return new Guid("{9D2583DD-6CF5-497c-8C40-B92541528337}"); 92 | } 93 | } 94 | 95 | 96 | /** 97 | * The icon of the component 98 | */ 99 | protected override Bitmap Internal_Icon_24x24 100 | { 101 | get 102 | { 103 | return Resources.Custom_24x24_LSystem; 104 | } 105 | } 106 | 107 | } 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | } 116 | -------------------------------------------------------------------------------- /GH/CellularAutomata/OnCellularGridBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | //using RMA.OpenNURBS; 6 | using Rhino.Geometry; 7 | 8 | using Rabbit.Kernel.ABMS.Space.Grid; 9 | using Rabbit.Kernel.CellularAutomata; 10 | using Rabbit.Kernel.CellularAutomata.Cells; 11 | using Rabbit.Kernel.CellularAutomata.Configuration; 12 | 13 | 14 | namespace Rabbit.GH.CellularAutomata 15 | { 16 | /** 17 | * Builds a Grid2d out of OpenNurbs points 18 | * 19 | * 20 | */ 21 | public class OnCellularGridBuilder: CellularGridBuilder 22 | { 23 | 24 | private ICAConfig initialConfiguration; 25 | 26 | private INeighborhoodStrategy neighborhoodStrategy; 27 | 28 | private int XDimension, YDimension; 29 | 30 | private IList pointLattice; 31 | 32 | private OnStateConfig stateConfig; 33 | private Boolean random; 34 | private Boolean custom; 35 | 36 | private RandomCAConfig randomConfig; 37 | 38 | public OnCellularGridBuilder(ICell cellPrototype, IList pointLattice, int XDimension, int YDimension) 39 | : base(cellPrototype) 40 | { 41 | this.XDimension = XDimension; 42 | this.YDimension = YDimension; 43 | this.pointLattice = pointLattice; 44 | } 45 | 46 | public OnCellularGridBuilder(ICell cellPrototype, IList pointLattice, int XDimension, int YDimension, OnStateConfig stateConfig) 47 | : this(cellPrototype, pointLattice, XDimension, YDimension) 48 | { 49 | random = false; 50 | custom = true; 51 | this.stateConfig = stateConfig; 52 | } 53 | 54 | public OnCellularGridBuilder(ICell cellPrototype, IList pointLattice, int XDimension, int YDimension, RandomCAConfig randomConfig) 55 | : this(cellPrototype, pointLattice, XDimension, YDimension) 56 | { 57 | random = true; 58 | custom = false; 59 | this.randomConfig = randomConfig; 60 | } 61 | 62 | protected override Grid2d CreateCellularGrid() 63 | { 64 | return new Grid2d(XDimension, YDimension); 65 | } 66 | 67 | protected override void PopulateGrid() 68 | { 69 | int index = 0; 70 | //populate the grid with cells 71 | for (int r = 0; r < cellularGrid.GetXDimension(); r++) 72 | { 73 | for (int c = 0; c < cellularGrid.GetYDimension(); c++) 74 | { 75 | Grid2d.Location position = new Grid2d.Location(r, c); 76 | ICell cell = cellPrototype.Clone(); 77 | cell.SetAttachedObject(pointLattice[index]); 78 | cell.SetId(index); 79 | cellularGrid.Add(cell, position); 80 | index++; 81 | } 82 | } 83 | } 84 | 85 | //private ICAConfig CreateICAConfig(IList latticePoints, IList configurationPoints) 86 | //creates an ICAConfig instance out of the OpenNurbs state configuration 87 | private ICAConfig CreateICAConfig(OnStateConfig stateConfig) 88 | { 89 | 90 | CAConfig cfg = new CAConfig(0, cellPrototype.GetState()); 91 | int latticePointIndex = 0; 92 | foreach (Point3d latticePoint in pointLattice) 93 | { 94 | foreach (Point3d configurationPoint in stateConfig.GetPoints()) 95 | if (latticePoint.X==configurationPoint.X && latticePoint.Y==configurationPoint.Y && latticePoint.Z==configurationPoint.Z) 96 | cfg.AddCellState(cellularGrid.GetObject(latticePointIndex), stateConfig.GetState()); 97 | 98 | latticePointIndex++; 99 | }//foreach 100 | 101 | return cfg; 102 | } 103 | 104 | 105 | protected override ICAConfig GetInitialConfiguration() 106 | { 107 | if (initialConfiguration == null) 108 | { 109 | if (random) initialConfiguration = randomConfig; 110 | else if (custom) initialConfiguration = CreateICAConfig(stateConfig); 111 | else initialConfiguration = null;//no configuration defined 112 | } 113 | return initialConfiguration; 114 | } 115 | 116 | 117 | protected override INeighborhoodStrategy GetNeighborhoodStrategy() 118 | { 119 | if(neighborhoodStrategy==null)//lazy initialization 120 | neighborhoodStrategy = new MooreNeighborhoodStrategy(cellularGrid, false); 121 | 122 | return neighborhoodStrategy; 123 | } 124 | 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Kernel/LSystems/StochasticLSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Rabbit.Kernel.LSystems 6 | { 7 | 8 | /** 9 | * 10 | * A stochastic OL-system is an ordered quadruplet Gπ = V, ω, P, π. 11 | The alphabet V , the axiom ω and the set of productions P are defined 12 | as in an OL-system (page 4). Function π : P → (0, 1], called the 13 | probability distribution, maps the set of productions into the set of 14 | production probabilities. It is assumed that for any letter a ∈ V , the 15 | sum of probabilities of all productions with the predecessor a is equal 16 | to 1. 17 | * 18 | * 19 | */ 20 | public class StochasticLSystem:AbstractLSystem 21 | { 22 | 23 | private static Random RANDOM = new Random(); 24 | 25 | protected Dictionary RulesProbabilityDistributionLookUpMap; 26 | protected Dictionary> RulesLookUpMap; 27 | 28 | public StochasticLSystem(IList Alphabet, Word Axiom) 29 | : base(Alphabet, Axiom) 30 | { 31 | this.RulesProbabilityDistributionLookUpMap = new Dictionary(); 32 | this.RulesLookUpMap = new Dictionary>(); 33 | } 34 | 35 | /** 36 | * Adds a RewriteRule with a Probability Distribution of 1.0 37 | * 38 | */ 39 | public override void AddRule(ProductionRule RewriteRule) 40 | { 41 | AddRewriteRule(RewriteRule, 1.0); 42 | } 43 | 44 | /** 45 | * Adds a RewriteRule with a certain Probability Distribution(should be between [0.0 - 1.0]). 46 | * It is assumed that for any letter a ∈ V , the 47 | sum of probabilities of all productions with the predecessor a is equal 48 | to 1. 49 | * 50 | */ 51 | public void AddRewriteRule(ProductionRule RewriteRule, Double ProbabilityDistrubution) 52 | { 53 | base.AddRule(RewriteRule); 54 | //associate the rule to a given probability distribution 55 | RulesProbabilityDistributionLookUpMap.Add(RewriteRule, ProbabilityDistrubution); 56 | 57 | //Add the Rule to the List of Rules associated to that predecessor symbol: 58 | if(RulesLookUpMap.ContainsKey(RewriteRule.GetPredecessor())) { 59 | IList PredecessorRules = RulesLookUpMap[RewriteRule.GetPredecessor()]; 60 | PredecessorRules.Add(RewriteRule); 61 | } 62 | else {//init the list if that's the first rule to be added for that symbol 63 | IList PredecessorRules = new List(1); 64 | PredecessorRules.Add(RewriteRule); 65 | //add to the lookup map: 66 | RulesLookUpMap.Add(RewriteRule.GetPredecessor(), PredecessorRules); 67 | } 68 | } 69 | 70 | 71 | /** 72 | * @returns The RewriteRule associated with the specified Symbol(Predecessor) according to the Probability Distribution factor 73 | * The higher the Probability Distribution of a rule, the higher it is likely to be used 74 | * 75 | */ 76 | protected override ProductionRule GetProductionRule(Symbol Symbol, int SymbolIndex, Word Word) 77 | { 78 | //TODO: Get all rules for that symbol, order them by probability 79 | //Get a random number and choose one of them 80 | if (RulesLookUpMap.ContainsKey(Symbol)) 81 | { 82 | IList PredecessorRules = RulesLookUpMap[Symbol]; 83 | return GetMostProbableRewriteRule(PredecessorRules); 84 | } 85 | else 86 | return null; 87 | } 88 | 89 | //TODO: find the most probable rule according to the probability distribution of each rule in the list 90 | private ProductionRule GetMostProbableRewriteRule(IList RewriteRules) 91 | { 92 | SortedList Probabilities = new SortedList(); 93 | foreach (ProductionRule RewriteRule in RewriteRules) 94 | Probabilities.Add(RulesProbabilityDistributionLookUpMap[RewriteRule], RewriteRule); 95 | 96 | 97 | double Random = RANDOM.NextDouble(); 98 | foreach (KeyValuePair pair in Probabilities) 99 | { 100 | System.Console.WriteLine("Probability: " + pair+"; Random: "+Random); 101 | if (Random ConwayGOLRules = new List(2) { 35 | SurviveOn2LivingNeighbors, 36 | SurviveOn3LivingNeighbors, 37 | BornOnExactly3LivingNeighbors 38 | }; 39 | 40 | //temp, use booleans instead 41 | //private static CellState[] LivingStates = { aliveState, deadState }; 42 | 43 | private CellState aliveState; 44 | private CellState deadState; 45 | 46 | 47 | public LifeLikeCell(int id, CellState aliveState, CellState deadState, IList livingRules) 48 | : base(id, new List(2) { aliveState, deadState }, livingRules) 49 | { 50 | this.aliveState = aliveState; 51 | this.deadState = deadState; 52 | this.cellState = deadState; 53 | } 54 | 55 | /* 56 | public LivingCell(int id, IList livingRules) 57 | : base(id, LivingStates, livingRules) 58 | { 59 | this.cellState = deadState; 60 | }*/ 61 | 62 | 63 | public override CellState GetDefaultState() 64 | { 65 | return deadState; 66 | } 67 | 68 | 69 | public Boolean IsAlive() 70 | { 71 | return cellState.Equals(aliveState); 72 | } 73 | 74 | public CellState GetAliveState() 75 | { 76 | return aliveState; 77 | } 78 | 79 | public CellState GetDeadState() 80 | { 81 | return deadState; 82 | } 83 | 84 | public override ICell Clone() 85 | { 86 | return new LifeLikeCell(-1, aliveState, deadState, transitionRules); 87 | } 88 | 89 | /** 90 | * In the process of computation. Each cell evolves according to it's context. 91 | * Each cell mutates depending on its neighbors. 92 | * @return The new state of the cell 93 | * 94 | */ 95 | //none of the rules returned a new CellState, so DIE: 96 | public override CellState UpdateState() 97 | { 98 | //_time++; 99 | CellState EvolvedState = GetEvolvedState(); 100 | if (EvolvedState != null) 101 | return EvolvedState; 102 | else 103 | return GetDeadState(); 104 | } 105 | // 106 | 107 | 108 | protected CellState GetEvolvedState() 109 | { 110 | //iterate over all Evolution Rules, 111 | // The priority of the rules is by their index 112 | //The first Rule that returns a Resolve CellState defines the new State of the cell 113 | //if every Rule returns an UNRESOLVED cell state 114 | // the Cell does not change its state(does not evolve) 115 | foreach (LifeLikeRule evolutionRule in transitionRules) 116 | { 117 | CellState EvolvedState = evolutionRule.ApplyRule(this); 118 | if (EvolvedState != null) 119 | return EvolvedState; 120 | } 121 | return null;//LivingCell.DeadState; 122 | } 123 | 124 | public override String ToString() 125 | { 126 | String cellInfo = string.Format("Life-Like Cell [Current State: {0}]; Evolution Rules:", (IsAlive() ? "Alive" : "Dead")); 127 | foreach (LifeLikeRule evolutionRule in transitionRules) 128 | cellInfo += "\n" + evolutionRule.ToString(); 129 | return cellInfo; 130 | } 131 | 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Impl/Greenberg_Hastings/ExcitableCell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.CellularAutomata; 6 | using Rabbit.Kernel.Automata.FSM; 7 | 8 | namespace Rabbit.Kernel.CellularAutomata.Impl.Greenberg_Hastings 9 | { 10 | public class ExcitableCell:Cell 11 | { 12 | 13 | //tresholds 14 | private int excitedNeighborsTreshold = 3; 15 | 16 | 17 | //Excitable Cell states 18 | private CellState restingState; 19 | private IList refractoryStates; 20 | private IList excitedStates; 21 | 22 | 23 | private static Random random = new Random(); 24 | 25 | private static double ExcitedProbability = 1.5 / 100.0; 26 | 27 | private int currentRefractoryIndex; 28 | private int currentExcitedIndex; 29 | 30 | public ExcitableCell(int id, CellState restingState, IList refractoryStates, IList excitedStates, int excitedNeighborsTreshold) //, int state)//, IList finiteStates, List transitions) 31 | : base(id, null, null) 32 | { 33 | this.restingState = restingState; 34 | this.refractoryStates = refractoryStates; 35 | this.excitedStates = excitedStates; 36 | this.excitedNeighborsTreshold = excitedNeighborsTreshold; 37 | 38 | //init the finite states of the cell 39 | finiteStates = new List(); 40 | //set all possible states 41 | finiteStates.Add(restingState); 42 | foreach(CellState refractoryState in refractoryStates) 43 | finiteStates.Add(refractoryState); 44 | foreach (CellState excitedState in excitedStates) 45 | finiteStates.Add(excitedState); 46 | 47 | cellState = GetDefaultState(); 48 | 49 | } 50 | 51 | /** 52 | * Resting cells could become active by some probability 53 | */ 54 | private CellState GetNoiseState() 55 | { 56 | double probability = random.NextDouble(); 57 | if (probability < 0.01) 58 | return excitedStates[0]; 59 | else 60 | return cellState; 61 | } 62 | 63 | public override ICell Clone() 64 | { 65 | return new ExcitableCell(-1, restingState, refractoryStates, excitedStates, excitedNeighborsTreshold); 66 | } 67 | 68 | public override CellState GetDefaultState() 69 | { 70 | return restingState; 71 | } 72 | 73 | public Boolean IsExcited() 74 | { 75 | return excitedStates.Contains(cellState); 76 | } 77 | public Boolean IsRefractory() 78 | { 79 | return refractoryStates.Contains(cellState); 80 | } 81 | public Boolean IsResting() 82 | { 83 | return cellState.Equals(restingState); 84 | } 85 | 86 | public override CellState UpdateState() 87 | { 88 | 89 | //is excited: 90 | if (IsExcited()) { 91 | currentExcitedIndex++; 92 | if (currentExcitedIndex < excitedStates.Count) 93 | return excitedStates[currentExcitedIndex]; 94 | else //no more excited states -> go refractory 95 | { 96 | currentExcitedIndex = 0;//reset the index 97 | return refractoryStates[currentRefractoryIndex]; 98 | } 99 | } 100 | //Refractory 101 | else if (IsRefractory()) { 102 | currentRefractoryIndex++; 103 | if (currentRefractoryIndex < refractoryStates.Count) 104 | return refractoryStates[currentRefractoryIndex]; 105 | else 106 | { 107 | currentRefractoryIndex = 0;//reset the index 108 | return restingState; 109 | } 110 | } 111 | //Resting 112 | else if (IsResting()) 113 | { 114 | int excitedNeighborsCount = 0; 115 | //check for excited neighbors: 116 | foreach (ICell neighbor in GetNeighbors()) 117 | { 118 | CellState neighborState = neighbor.GetState(); 119 | if (excitedStates.Contains(neighborState))//check if the neighbor is excited 120 | excitedNeighborsCount++; 121 | } 122 | if (excitedNeighborsCount >= excitedNeighborsTreshold) 123 | return excitedStates[0]; //go excited 124 | } 125 | //no change in the status 126 | return cellState; 127 | 128 | //return GetNoiseState(); 129 | //return GetRandomState(); 130 | //throw new InvalidOperationException("Invalid stated of an excitable cell!: "+cellState+":"+restingState+":"+cellState.Equals(restingState)); 131 | } 132 | 133 | 134 | } 135 | } 136 | 137 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_LifeLikeCell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Types; 9 | using Grasshopper.Kernel.Data; 10 | using Grasshopper.GUI; 11 | using Grasshopper; 12 | 13 | using Rabbit.Properties; 14 | using Rabbit.Kernel.CellularAutomata; 15 | using Rabbit.Kernel.CellularAutomata.Cells; 16 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 17 | using Rabbit.Kernel.ABMS.Space.Grid; 18 | using Rabbit.Kernel.Geometry.Util; 19 | using Rabbit.GH.CellularAutomata; 20 | 21 | using RMA.OpenNURBS; 22 | 23 | namespace Rabbit.GH.CellularAutomata 24 | { 25 | /** 26 | * GH Component that create a Cellular Automata object, based on specified number of rows and columns 27 | */ 28 | public class Component_LifeLikeCell : Component_CABase 29 | { 30 | 31 | /** 32 | * Constructor 33 | */ 34 | public Component_LifeLikeCell() 35 | : base("Life-Like Cell", "Life Cell", "Life-Like Cell Prototype. Each cell can be in one of the two possible states: Dead=0 or Alive=1. At each iteration, the new state of the cell is determinced according to a set of Survive / Born rules.", Component_CABase.CATEGORY_CA_MODEL) 36 | { 37 | } 38 | 39 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 40 | { 41 | //inputManager.Register_GenericParam("Rules", "R", "Evolution rules that drive the behavior of a living cell. According to the rules a cell will either die or survive.", GH_ParamAccess.list); 42 | inputManager.Register_IntegerParam("Born Rules (Number of neigbors)", "B", "Born Rule(s). A new cell is born if surrounded by n 'alive' neighbors", GH_ParamAccess.list);//name, nick, description, default, isList 43 | Params.Input[0].Optional = true; 44 | inputManager.Register_IntegerParam("Survive Rules (Number of neighbors)", "S", "Survive Rule(s). A cell survives if surrounded by n 'alive' neighbors", GH_ParamAccess.list);//name, nick, description, default, isList 45 | Params.Input[1].Optional = true; 46 | } 47 | 48 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 49 | { 50 | outputManager.Register_GenericParam("Living Cell Prototype", "LC", "Living cell prototype, to be populated on a grid"); 51 | } 52 | 53 | 54 | /** 55 | * Does the computation 56 | */ 57 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 58 | { 59 | /* 60 | //Get the evolution rules of the cell 61 | List EvolutionRulesWrapper = new List(); 62 | DA.GetDataList(0, EvolutionRulesWrapper);//param index, place holder 63 | 64 | //init the list of evolution rules: 65 | List EvolutionRules = new List(); 66 | foreach (GH_ObjectWrapper EvolutionRuleWrapper in EvolutionRulesWrapper) 67 | EvolutionRules.Add((CellularRule)EvolutionRuleWrapper.Value); 68 | */ 69 | 70 | //get the inputs 71 | List bornRuleNeighbors = new List(); 72 | DA.GetDataList(0, bornRuleNeighbors);//param index, place holder 73 | List survuveRuleNeighbors = new List(); 74 | DA.GetDataList(1, survuveRuleNeighbors);//param index, place holder 75 | 76 | if (bornRuleNeighbors.Count == 0 && survuveRuleNeighbors.Count == 0) { 77 | this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Unsufficient evolution rules: At least one rule should be defined."); 78 | return; 79 | } 80 | //---------------------------------------------------------------------- 81 | //init the list of evolution rules: 82 | List EvolutionRules = new List(); 83 | 84 | //init the Born Rules(if any) 85 | if (bornRuleNeighbors.Count > 0) 86 | { 87 | //creates the Evolution Rule: 88 | BornRule bornRule = new BornRule(bornRuleNeighbors); 89 | EvolutionRules.Add(bornRule); 90 | } 91 | 92 | //Init the Survive Rules(if any) 93 | if (survuveRuleNeighbors.Count > 0) 94 | { 95 | //creates the Evolution Rule: 96 | SurviveRule surviveRule = new SurviveRule(survuveRuleNeighbors); 97 | EvolutionRules.Add(surviveRule); 98 | } 99 | 100 | //Create the Life-Like Cell Prototype 101 | CellState ghAliveState = new GH_CellState(new GH_Boolean(true)); 102 | CellState ghDeadState = new GH_CellState(new GH_Boolean(false)); 103 | LifeLikeCell prototype = new LifeLikeCell(-1, ghAliveState, ghDeadState, EvolutionRules); 104 | 105 | //set the output parameters 106 | DA.SetData(0, prototype); 107 | } 108 | 109 | 110 | 111 | /** 112 | * The Guid of the component 113 | */ 114 | public override Guid ComponentGuid 115 | { 116 | get { 117 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A290248197}"); 118 | } 119 | } 120 | 121 | public override GH_Exposure Exposure 122 | { 123 | get 124 | { 125 | return GH_Exposure.secondary; 126 | } 127 | } 128 | 129 | /** 130 | * The icon of the component 131 | */ 132 | protected override Bitmap Internal_Icon_24x24 133 | { 134 | get 135 | { 136 | return Resources.Custom_24x24_LivingCell; 137 | } 138 | } 139 | 140 | 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /Kernel/ABMS/Space/Grid/Grid2d.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | using Rabbit.Kernel.ABMS.Space.Projection; 7 | 8 | namespace Rabbit.Kernel.ABMS.Space.Grid 9 | { 10 | /** 11 | * Grid space that contains objects 12 | * TODO: implement this as matrix 13 | * 14 | */ 15 | public class Grid2d:IProjection 16 | { 17 | 18 | private int XDimension; 19 | private int YDimension; 20 | 21 | private Dictionary gridMap; 22 | private Dictionary reverseGridMap; 23 | //private ICell[,] cellArray; 24 | private IList objectsList; 25 | 26 | 27 | public Grid2d(int XDimension, int YDimension) 28 | { 29 | this.XDimension = XDimension; 30 | this.YDimension = YDimension; 31 | 32 | this.objectsList = new List(XDimension * YDimension); 33 | this.gridMap = new Dictionary(XDimension * YDimension); 34 | this.reverseGridMap = new Dictionary(XDimension * YDimension); 35 | 36 | } 37 | 38 | 39 | /** 40 | * Adds a cell on a specific location in the grid 41 | */ 42 | public void Add(A obj, Location location) 43 | { 44 | if (!isValidPosition(location)) 45 | throw new IndexOutOfRangeException(location + " is out of the Geography2D dimentions!"); 46 | //if (objectsList.Contains(obj) || gridMap.ContainsKey(obj) || reverseGridMap.ContainsKey(location)) 47 | //throw new InvalidOperationException("Could not add more than one object on the same grid location!"); 48 | 49 | //cellArray[position.GetX(), position.GetY()] = cell; 50 | objectsList.Add(obj); 51 | gridMap.Add(obj, location); 52 | reverseGridMap.Add(location, obj); 53 | 54 | } 55 | 56 | public A GetCell(int x, int y) 57 | { 58 | Location location = new Location(x, y); 59 | return GetObjectAt(location); 60 | } 61 | 62 | /** 63 | * @returns A cell on the specified position 64 | */ 65 | public A GetObjectAt(Location location) 66 | { 67 | if (isValidPosition(location)) 68 | return reverseGridMap[location]; 69 | else 70 | return default(A); 71 | } 72 | 73 | /** 74 | * @return The position of a cell 75 | */ 76 | public Location GetPosition(A obj) 77 | { 78 | if (gridMap.ContainsKey(obj)) 79 | return gridMap[obj];// worldMap[cell]; 80 | else 81 | throw new InvalidOperationException("The specified cell is not part of the current lattice!"); 82 | } 83 | 84 | /** 85 | * @return True - if the position is within the dimension of the world. 86 | */ 87 | private Boolean isValidPosition(Location location) 88 | { 89 | return isValidPosition(location.GetX(), location.GetY()); 90 | } 91 | 92 | private Boolean isValidPosition(int row, int column) 93 | { 94 | if (row >= 0 && row < XDimension && column >= 0 && column < YDimension) 95 | return true; 96 | else 97 | return false; 98 | } 99 | 100 | 101 | /** 102 | * @returns The number of rows of this World 103 | */ 104 | public int GetXDimension() 105 | { 106 | return XDimension; 107 | } 108 | 109 | public int GetYDimension() 110 | { 111 | return YDimension; 112 | } 113 | 114 | 115 | 116 | public override String ToString() 117 | { 118 | return "2D Grid with XDimension=" + XDimension + ", Y Dimension=" + YDimension; 119 | } 120 | 121 | 122 | public IList GetObjects() 123 | { 124 | return objectsList; 125 | } 126 | 127 | public A GetObject(Object id) 128 | { 129 | int cellIndex = int.Parse(id.ToString()); 130 | return GetObjects()[cellIndex]; 131 | } 132 | 133 | 134 | 135 | /** 136 | * Position defines the exact location of a Cell within the 2D world. 137 | * 138 | */ 139 | public class Location 140 | { 141 | private int X; 142 | private int Y; 143 | 144 | private String hashCode; 145 | /** 146 | * Constructor. 147 | */ 148 | public Location(int X, int Y) 149 | { 150 | this.X = X; 151 | this.Y = Y; 152 | hashCode = X + ":" + Y; 153 | } 154 | 155 | public int GetX() 156 | { 157 | return X; 158 | } 159 | 160 | public int GetY() 161 | { 162 | return Y; 163 | } 164 | 165 | public override Boolean Equals(Object obj) 166 | { 167 | if (obj == null) 168 | return false; 169 | if (!obj.GetType().Equals(this.GetType())) 170 | return false; 171 | else 172 | { 173 | Location p2 = (Location)obj; 174 | return (p2.X == this.X) && (p2.Y == this.Y); 175 | } 176 | } 177 | 178 | public override int GetHashCode() 179 | { 180 | return hashCode.GetHashCode(); 181 | } 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/Cells/Cell.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System; 3 | using System.Text; 4 | 5 | using Rabbit.Kernel.Automata.FSM; 6 | using Rabbit.Kernel.ABMS.Space.Grid; 7 | using Rabbit.Kernel.CellularAutomata.Cells; 8 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 9 | using Rabbit.Kernel.Util; 10 | 11 | namespace Rabbit.Kernel.CellularAutomata 12 | { 13 | /** 14 | * A cell is a Finite State Machine. It has a state and a finite number of states. 15 | * 16 | * For ex. a cell could have 2 states: Dead or Alive 17 | * The cell changes it's state according to a set of Evolution rules. 18 | */ 19 | public abstract class Cell : ICell 20 | { 21 | 22 | //current state of the cell 23 | protected CellState cellState; 24 | 25 | //finite number of possible states for this cell 26 | protected IList finiteStates; 27 | 28 | //is the state of the cell initialized 29 | private Boolean isInitialized = false; 30 | 31 | /** the unique Id of the Cell */ 32 | //protected double id; 33 | //FIXME: implement this as a GUID 34 | private int id; 35 | 36 | //private int _time; 37 | 38 | //rules that drive the change in state 39 | protected IList transitionRules; 40 | 41 | // Networking 42 | private IList neighbors; 43 | 44 | 45 | //Attached object 46 | private Object attachedObject; 47 | 48 | /** 49 | * Constructor. 50 | * Cell with a random state. Could be alive or dead 51 | */ 52 | public Cell(int id, IList finiteStates, IList transitionRules) 53 | { 54 | this.id = id; 55 | this.finiteStates = finiteStates; 56 | this.transitionRules = transitionRules; 57 | } 58 | 59 | 60 | // PROTOTYPE -------------------------------------------------------------------------------------------- 61 | public abstract ICell Clone(); 62 | 63 | // ATTACHED Object -------------------------------------------------------------------------------------------- 64 | public Object GetAttachedObject() 65 | { 66 | return attachedObject; 67 | } 68 | 69 | public void SetAttachedObject(Object obj) 70 | { 71 | this.attachedObject = obj; 72 | } 73 | 74 | // IDENTIFIER -------------------------------------------------------------------------------------------- 75 | public Object GetId() 76 | { 77 | return id; 78 | } 79 | 80 | public void SetId(Object id) 81 | { 82 | this.id = (int)id; 83 | } 84 | 85 | // STATES -------------------------------------------------------------------------------------------- 86 | public void SetState(CellState cellState) 87 | { 88 | //if (isInitialized) throw new InvalidOperationException("Cell was already initialized!"); 89 | 90 | //check if this state is one of the finite states of this cell 91 | if(!finiteStates.Contains(cellState)) 92 | throw new InvalidOperationException(string.Format("The specified state: {0} is invalid for this type of cell! Please use one of the following: {1}", cellState, PresentationUtils.ListToString(finiteStates))); 93 | //if it is valid, initialize the cell 94 | this.cellState = cellState; 95 | isInitialized = true; 96 | } 97 | 98 | /* 99 | public void SetState(CellState CellState) 100 | { 101 | this.cellState = CellState; 102 | }*/ 103 | 104 | public CellState GetState() 105 | { 106 | return cellState; 107 | } 108 | 109 | public IList GetFiniteStates() 110 | { 111 | return finiteStates; 112 | } 113 | 114 | public abstract CellState GetDefaultState(); 115 | 116 | //NETWORKING ----------------------------------------------------------- 117 | /** 118 | * @returns the list of neighbors of that cell 119 | */ 120 | public IList GetNeighbors() { 121 | return neighbors; 122 | } 123 | 124 | //this is called by the cellular space when all cells are resolved 125 | public void SetNeighbors(IList neighbors) 126 | { 127 | this.neighbors = neighbors; 128 | } 129 | 130 | 131 | /** 132 | * The Cellular automaton updates fires events on the cell. Each cell change it's state according to the transition rules. 133 | * Each cell mutates depending on its neighbors. 134 | * @return The new state of the cell 135 | * 136 | */ 137 | public abstract CellState UpdateState(); 138 | 139 | 140 | // EVOLUTION RULES --------------------------------------------------- 141 | 142 | /** 143 | * The evolution rules used by the cell when changing state 144 | */ 145 | public IList GetRules() 146 | { 147 | return transitionRules; 148 | } 149 | 150 | public void SetTransitionRules(List livingRules) 151 | { 152 | //IList castedList = (IList)livingRules; 153 | //this.livingRules = castedList; 154 | transitionRules = livingRules; 155 | } 156 | 157 | 158 | 159 | public override bool Equals(object obj) 160 | { 161 | 162 | if(obj==null || !obj.GetType().Equals(this.GetType())) 163 | return false; 164 | 165 | ICell thatCell = (ICell)obj; 166 | return thatCell.GetId().Equals(this.id); 167 | } 168 | 169 | public override int GetHashCode() 170 | { 171 | return id.GetHashCode(); 172 | } 173 | 174 | public override String ToString() 175 | { 176 | return "[Cell: state=" + cellState + "]"; 177 | } 178 | 179 | }//Cell 180 | 181 | } 182 | -------------------------------------------------------------------------------- /Kernel/CellularAutomata/CA.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Collections; 3 | using System; 4 | 5 | 6 | using Rabbit.Kernel.Systems.DynamicalSystems; 7 | using Rabbit.Kernel.ABMS.Space.Grid; 8 | using Rabbit.Kernel.ABMS.Space.Projection; 9 | using Rabbit.Kernel.CellularAutomata.Configuration; 10 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 11 | 12 | /** 13 | * AUTHOR: MORPHOCODE.COM 14 | * Licence: Creative commons 15 | */ 16 | namespace Rabbit.Kernel.CellularAutomata 17 | { 18 | /** 19 | * A CellularAutomata is a computational model that puts together a finite number of cells within a n-th dimensional space. 20 | * The Cells are connected with each other. Could be connected locally(in a neighborhood). 21 | * Each Cell could interract with the cells that it is connected to. 22 | * Each cell has some specific behavior and could change it's own state depending on the interactions. 23 | * 24 | * The Cellular Automaton is a dynamical system i.e. it depends on time(time series). The states of the cells are function of time. 25 | * 26 | * @author http://MORPHOCODE.COM 27 | */ 28 | public class CA : DynamicalSystem { 29 | 30 | //EVENTS ----------------------------------------------------------------------------- 31 | // Change Events fired when the CA is changed 32 | //a delegate type for the event must be declared, if none is already declared 33 | public delegate void ChangedEventHandler(object sender, EventArgs e); 34 | 35 | //the event itself 36 | public event ChangedEventHandler ChangeEvent; 37 | 38 | 39 | //Cellular space 40 | private IProjection grid; 41 | 42 | //list of all states calculated by the Cellular Automaton 43 | private TimedMemory _memory; 44 | 45 | private ICAConfig currentConfiguration; 46 | 47 | private int currentTime = 0;//TODO: get that from the timer!! 48 | 49 | 50 | /** 51 | * Constructor. Creates a cellular automata 52 | */ 53 | public CA(IProjection grid) 54 | { 55 | 56 | this.grid = grid; 57 | this._memory = new TimedMemory(); 58 | ICAConfig initialConfig = BuildConfiguration(grid); 59 | _memory.Save(0, initialConfig); 60 | this.currentConfiguration = initialConfig; 61 | 62 | this.grid = grid; 63 | 64 | //listen for events on the global clock: 65 | DiscreteTimer.Instance.TickTackEvent += new DiscreteTimer.TickTackEventHandler(OnTickTack); 66 | } 67 | 68 | private ICAConfig BuildConfiguration(IProjection grid) 69 | { 70 | CAConfig configuration = new CAConfig(_memory.GetObjects().Count, grid.GetObjects()[0].GetDefaultState()); 71 | foreach (ICell cell in grid.GetObjects()) 72 | configuration.AddCellState(cell, cell.GetState()); 73 | 74 | return configuration; 75 | } 76 | 77 | 78 | private void OnTickTack(Object sender, EventArgs args) 79 | { 80 | currentConfiguration = Update(DiscreteTimer.Instance.GetTime()); 81 | //notify all listeners that the CA has changed 82 | FireEvent(EventArgs.Empty); 83 | } 84 | 85 | 86 | /** 87 | * @returns The World in which the cells are positioned 88 | */ 89 | public IProjection GetGrid() 90 | { 91 | return grid; 92 | } 93 | 94 | public ICAConfig GetCurrentConfiguration() 95 | { 96 | return currentConfiguration; 97 | } 98 | 99 | private void FireEvent(EventArgs e) 100 | { 101 | if (ChangeEvent != null) 102 | ChangeEvent(this, e); 103 | } 104 | 105 | 106 | public TimedMemory GetMemory() 107 | { 108 | return _memory; 109 | } 110 | 111 | /** 112 | * the CA configuration for t=discreteTime 113 | * 114 | */ 115 | public ICAConfig Update(int discreteTime) 116 | { 117 | 118 | ICAConfig configuration = _memory.GetObject(discreteTime); 119 | int timeGap = discreteTime - _memory.GetObjects().Count; 120 | 121 | if (configuration == null) 122 | { 123 | for (int t = 0; t <= timeGap; t++) 124 | { 125 | configuration = Update(); 126 | _memory.Save(currentTime, configuration); 127 | } 128 | return configuration; 129 | } 130 | else 131 | { 132 | return configuration;//_memory.GetState(discreteTime); 133 | } 134 | } 135 | 136 | /** 137 | * The CA evolves to a new state, according to the behavior of each cell. 138 | * The old generations is archieved in the list of generations 139 | */ 140 | public ICAConfig Update() { 141 | currentTime++; 142 | CAConfig nextConfiguration = new CAConfig(_memory.GetObjects().Count, grid.GetObjects()[0].GetDefaultState()); 143 | foreach (ICell cell in grid.GetObjects()) 144 | { 145 | //do not alter the cells' states directly 146 | //as the states affect the evolution 147 | //the result of the evolution is saved 148 | //and the cells are updated when all cells evolved 149 | CellState evolvedCellState = cell.UpdateState(); 150 | nextConfiguration.AddCellState(cell, evolvedCellState); 151 | } 152 | //update the cells's states according to the evolution 153 | foreach (ICell cell in grid.GetObjects()) 154 | cell.SetState(nextConfiguration.GetCellState(cell)); 155 | 156 | return nextConfiguration; 157 | } 158 | 159 | public override String ToString() 160 | { 161 | return "Cellular Automata evolved " + DiscreteTimer.Instance.GetTime() + " times, based on "+grid.ToString() ; 162 | } 163 | 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /GH/CellularAutomata/Component_ElementaryCell.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Drawing; 5 | using System.Windows.Forms; 6 | 7 | using Grasshopper.Kernel; 8 | using Grasshopper.Kernel.Types; 9 | using Grasshopper.Kernel.Data; 10 | using Grasshopper.GUI; 11 | using Grasshopper; 12 | 13 | using Rabbit.Properties; 14 | using Rabbit.Kernel.CellularAutomata; 15 | using Rabbit.Kernel.CellularAutomata.Cells; 16 | using Rabbit.Kernel.CellularAutomata.Impl.Life; 17 | using Rabbit.Kernel.CellularAutomata.Impl.Elementary; 18 | using Rabbit.Kernel.ABMS.Space.Grid; 19 | using Rabbit.Kernel.Geometry.Util; 20 | using Rabbit.GH.CellularAutomata; 21 | 22 | using RMA.OpenNURBS; 23 | 24 | namespace Rabbit.GH.CellularAutomata 25 | { 26 | /** 27 | * GH Component that create a Cellular Automata object, based on specified number of rows and columns 28 | */ 29 | public class Component_ElementaryCell : Component_CABase 30 | { 31 | 32 | private static CellState GH_ALIVE_STATE = new GH_CellState(new GH_Boolean(true)); 33 | private static CellState GH_DEAD_STATE = new GH_CellState(new GH_Boolean(false)); 34 | 35 | /** 36 | * Constructor 37 | */ 38 | public Component_ElementaryCell() 39 | : base("Elementary Cell", "El. Cell", "Elementary Cells have two states: Dead = 0 or Alive = 1. At each iteration, the new state of the cell is determined according to it's current state and the states of the two nearest cells(left and right neighbour).", Component_CABase.CATEGORY_CA_MODEL) 40 | { 41 | } 42 | 43 | protected override void RegisterInputParams(GH_Component.GH_InputParamManager inputManager) 44 | { 45 | inputManager.Register_BooleanParam("111", "111", "New state for this configuration of states", GH_ParamAccess.item); 46 | inputManager.Register_BooleanParam("110", "110", "New state for this configuration of states", GH_ParamAccess.item); 47 | inputManager.Register_BooleanParam("101", "101", "New state for this configuration of states", GH_ParamAccess.item); 48 | inputManager.Register_BooleanParam("100", "100", "New state for this configuration of states", GH_ParamAccess.item); 49 | inputManager.Register_BooleanParam("011", "011", "New state for this configuration of states", GH_ParamAccess.item); 50 | inputManager.Register_BooleanParam("010", "010", "New state for this configuration of states", GH_ParamAccess.item); 51 | inputManager.Register_BooleanParam("001", "001", "New state for this configuration of states", GH_ParamAccess.item); 52 | inputManager.Register_BooleanParam("000", "000", "New state for this configuration of states", GH_ParamAccess.item); 53 | } 54 | 55 | protected override void RegisterOutputParams(GH_OutputParamManager outputManager) 56 | { 57 | outputManager.Register_GenericParam("Elementary Cell Prototype", "C", "Elementary cell prototype."); 58 | } 59 | 60 | 61 | /** 62 | * Does the computation 63 | */ 64 | protected override void SolveRabbitInstance(IGH_DataAccess DA) 65 | { 66 | //Get the evolution rules of the cell 67 | Boolean rule111_state = false; 68 | DA.GetData(0, ref rule111_state); 69 | Boolean rule110_state = false; 70 | DA.GetData(1, ref rule110_state); 71 | Boolean rule101_state = false; 72 | DA.GetData(2, ref rule101_state); 73 | Boolean rule100_state = false; 74 | DA.GetData(3, ref rule100_state); 75 | Boolean rule011_state = false; 76 | DA.GetData(4, ref rule011_state); 77 | Boolean rule010_state = false; 78 | DA.GetData(5, ref rule010_state); 79 | Boolean rule001_state = false; 80 | DA.GetData(6, ref rule001_state); 81 | Boolean rule000_state = false; 82 | DA.GetData(7, ref rule000_state); 83 | 84 | ElementaryRule rule111 = new ElementaryRule(GH_ALIVE_STATE, GH_ALIVE_STATE, GH_ALIVE_STATE, (rule111_state)? GH_ALIVE_STATE : GH_DEAD_STATE); 85 | ElementaryRule rule110 = new ElementaryRule(GH_ALIVE_STATE, GH_ALIVE_STATE, GH_DEAD_STATE, (rule110_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 86 | ElementaryRule rule101 = new ElementaryRule(GH_ALIVE_STATE, GH_DEAD_STATE, GH_ALIVE_STATE, (rule101_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 87 | ElementaryRule rule100 = new ElementaryRule(GH_ALIVE_STATE, GH_DEAD_STATE, GH_DEAD_STATE, (rule100_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 88 | ElementaryRule rule011 = new ElementaryRule(GH_DEAD_STATE, GH_ALIVE_STATE, GH_ALIVE_STATE, (rule011_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 89 | ElementaryRule rule010 = new ElementaryRule(GH_DEAD_STATE, GH_ALIVE_STATE, GH_DEAD_STATE, (rule010_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 90 | ElementaryRule rule001 = new ElementaryRule(GH_DEAD_STATE, GH_DEAD_STATE, GH_ALIVE_STATE, (rule001_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 91 | ElementaryRule rule000 = new ElementaryRule(GH_DEAD_STATE, GH_DEAD_STATE, GH_DEAD_STATE, (rule000_state) ? GH_ALIVE_STATE : GH_DEAD_STATE); 92 | 93 | List rules = new List(); 94 | rules.Add(rule111); 95 | rules.Add(rule110); 96 | rules.Add(rule101); 97 | rules.Add(rule100); 98 | rules.Add(rule011); 99 | rules.Add(rule010); 100 | rules.Add(rule001); 101 | rules.Add(rule000); 102 | 103 | 104 | 105 | ElementaryCell prototype = new ElementaryCell(-1, GH_ALIVE_STATE, GH_DEAD_STATE, rules); 106 | prototype.setIsPrototype(true); 107 | 108 | //set the output parameters 109 | DA.SetData(0, prototype); 110 | } 111 | 112 | 113 | 114 | /** 115 | * The Guid of the component 116 | */ 117 | public override Guid ComponentGuid 118 | { 119 | get { 120 | return new Guid("{9D2583DD-6CF5-497c-8C40-C9A290338527}"); 121 | } 122 | } 123 | 124 | public override GH_Exposure Exposure 125 | { 126 | get 127 | { 128 | return GH_Exposure.primary; 129 | } 130 | } 131 | 132 | /** 133 | * The icon of the component 134 | */ 135 | protected override Bitmap Internal_Icon_24x24 136 | { 137 | get 138 | { 139 | return Resources.Custom_24x24_ElementaryCell; 140 | } 141 | } 142 | 143 | 144 | } 145 | } 146 | --------------------------------------------------------------------------------