protected IList <int> FindNodesForRepulsion(int currentNode, double radiusOfCells, double searchRadius) { int currXIndex = nodeToCell[currentNode][0]; int currYIndex = nodeToCell[currentNode][1]; int maxXIndex = cellArray.GetUpperBound(0); int maxYIndex = cellArray.GetUpperBound(1); int blockRadius = (int)Math.Ceiling(searchRadius / radiusOfCells); int minX = Math.Max(0, currXIndex - blockRadius); int minY = Math.Max(0, currYIndex - blockRadius); int maxX = Math.Min(currXIndex + blockRadius, maxXIndex); int maxY = Math.Min(currYIndex + blockRadius, maxYIndex); List <int> currList = new List <int>(); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { if (cellArray[x, y] != null) { currList.AddRange(cellArray[x, y]); } } } return(currList); }
//public IList<IList<IList<bool>>> CrossUp { get; private set; } //public IList<IList<IList<bool>>> CrossDn { get; private set; } /// <summary> /// Конструктор индикатора MAxs /// </summary> /// <param name="period">Список периодов для создания индикаторов MaDe</param> /// <param name="input">Вход индикатора</param> /// <param name="logger">Логгер</param> public IndicatorMaDex(IList <double> period, IDataInput <double> input, ILogger logger) { this.Period = period; this.Input = input; this.logger = logger; //создаем список индикаторов, связываем первый с входом и между собой this.MaDe = new List <IndicatorMaDe>(period.Count); var dataInput = input; //Создаем список индикаторов MaDe, по количеству Period.Count foreach (var p in Period) { IndicatorMaDe ma_x = new IndicatorMaDe(p, dataInput, logger); this.MaDe.Add(ma_x); //предыдущий является входом для следующего dataInput = new DataInput <double>(ma_x); } CrossTo = new List <Tuple <int, int> >(); /// создаем массив [I,J] списков List<bool> CrossUp /// создаем массив [I,J] списков List<bool> CrossDn CrossUp = new List <bool> [period.Count, period.Count]; CrossDn = new List <bool> [period.Count, period.Count]; for (int i = 0; i <= CrossUp.GetUpperBound(0); i++) { for (int j = 0; j <= CrossUp.GetUpperBound(1); j++) { CrossUp[i, j] = new List <bool>(); CrossDn[i, j] = new List <bool>(); } } }
public PointToTile() { _gridTracking = new List <GMap.NET.Point> [300, 300]; for (int x = 0; x < _gridTracking.GetUpperBound(0); x++) { for (int y = 0; y < _gridTracking.GetUpperBound(1); y++) { _gridTracking[x, y] = new List <GMap.NET.Point>(); } } }
public SpatialBins(Actor self, SpatialBinsInfo info) { bins = new List<Actor>[ self.World.Map.MapSize.X / info.BinSize, self.World.Map.MapSize.Y / info.BinSize]; scale = Game.CellSize * info.BinSize; for (var j = 0; j <= bins.GetUpperBound(1); j++) for (var i = 0; i <= bins.GetUpperBound(0); i++) bins[i, j] = new List<Actor>(); }
public SpatialBins(Actor self, SpatialBinsInfo info) { bins = new List <Actor> [ self.World.Map.MapSize.X / info.BinSize, self.World.Map.MapSize.Y / info.BinSize]; scale = Game.CellSize * info.BinSize; for (var j = 0; j <= bins.GetUpperBound(1); j++) { for (var i = 0; i <= bins.GetUpperBound(0); i++) { bins[i, j] = new List <Actor>(); } } }
public List <string>[] Select(string query) { List <string>[] hsl = new List <string> [0]; MySqlDataReader reader; this.Open(); using (var cmd = new MySqlCommand(query, this.Conn)) { reader = cmd.ExecuteReader(); if (reader.HasRows) { List <string> temp = new List <string>(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { temp.Add(reader.GetString(i)); } Array.Resize(ref hsl, hsl.Length + 1); hsl[hsl.GetUpperBound(0)] = temp; } } } this.Close(); return(hsl); }
public FrozenActorLayer(World world, FrozenActorLayerInfo info) { this.info = info; frozen = new Dictionary <uint, FrozenActor>(); bins = new List <FrozenActor> [ world.Map.MapSize.X * Game.CellSize / info.BinSize, world.Map.MapSize.Y *Game.CellSize / info.BinSize]; for (var j = 0; j <= bins.GetUpperBound(1); j++) { for (var i = 0; i <= bins.GetUpperBound(0); i++) { bins[i, j] = new List <FrozenActor>(); } } }
public void Add(FrozenActor fa) { frozen.Add(fa.ID, fa); var top = (int)Math.Max(0, fa.Bounds.Top / info.BinSize); var left = (int)Math.Max(0, fa.Bounds.Left / info.BinSize); var bottom = (int)Math.Min(bins.GetUpperBound(1), fa.Bounds.Bottom / info.BinSize); var right = (int)Math.Min(bins.GetUpperBound(0), fa.Bounds.Right / info.BinSize); for (var j = top; j <= bottom; j++) { for (var i = left; i <= right; i++) { bins[i, j].Add(fa); } } }
// Replaces all descendants of "id" in "covering" with "id". // REQUIRES: "covering" contains at least one descendant of "id". private static void ReplaceCellsWithAncestor(List <S2CellId> covering, S2CellId id) { var begin = covering.GetLowerBound(id.RangeMin()); var end = covering.GetUpperBound(id.RangeMax()); System.Diagnostics.Debug.Assert(begin != end); covering.RemoveRange(begin + 1, end); covering[begin] = id; }
public void clear() { for (int i = 0; i <= cache.GetUpperBound(0); i++) { for (int j = 0; j <= cache.GetUpperBound(1); j++) { cache[i, j] = null; } } }
public void TestArrayNDim() { TestGen(cl => { var src = new SampleArrayNDim { A = new int[2, 2] { { 1, 2 }, { 3, 4 } }, B = new string[1, 1, 1] { { { "x" } } } }; var dst = cl.Deep(src); Assert.AreNotEqual(src, dst); Assert.AreNotEqual(src.A, dst.A); Assert.AreNotEqual(src.B, dst.B); src.AssertAreEqual(dst); }); TestGen(cl => { var src = new List <int>[2, 1] { { new List <int> { 1, 2 } }, { new List <int> { 3 } } }; var dst = cl.Deep(src); Assert.AreNotEqual(src, dst); Assert.AreNotEqual(src[0, 0], dst[0, 0]); Assert.AreEqual(src.GetUpperBound(0), dst.GetUpperBound(0)); CollectionAssert.AreEqual(src[0, 0], dst[0, 0]); CollectionAssert.AreEqual(src[1, 0], dst[1, 0]); }); TestGen(cl => { var src = new SampleArrayNDimOfClass { A = new Sample1[1, 1, 2] { { { new Sample1 { X = 8, Y = "q" }, null } } }, }; var dst = cl.Deep(src); Assert.AreNotEqual(src, dst); Assert.AreNotEqual(src.A, dst.A); Assert.AreEqual(src.A[0, 0, 0].X, dst.A[0, 0, 0].X); }); }
public void Tick(Actor self) { for (var j = 0; j <= bins.GetUpperBound(1); j++) { for (var i = 0; i <= bins.GetUpperBound(0); i++) { bins[i, j].Clear(); } } foreach (var a in self.World.ActorsWithTrait <IOccupySpace>()) { var bounds = a.Actor.ExtendedBounds.Value; if (bounds.Right <= Game.CellSize * self.World.Map.Bounds.Left) { continue; } if (bounds.Bottom <= Game.CellSize * self.World.Map.Bounds.Top) { continue; } if (bounds.Left >= Game.CellSize * self.World.Map.Bounds.Right) { continue; } if (bounds.Top >= Game.CellSize * self.World.Map.Bounds.Bottom) { continue; } var i1 = Math.Max(0, bounds.Left / scale); var i2 = Math.Min(bins.GetUpperBound(0), bounds.Right / scale); var j1 = Math.Max(0, bounds.Top / scale); var j2 = Math.Min(bins.GetUpperBound(1), bounds.Bottom / scale); for (var j = j1; j <= j2; j++) { for (var i = i1; i <= i2; i++) { bins[i, j].Add(a.Actor); } } } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { #region INPUTS // import silkwormSettings file List <string> silkwormSettings = new List <string>(); if (!DA.GetDataList(0, silkwormSettings)) { return; } // import raw Grasshopper Geometry to convert to Silkworm Movements List <GH_ObjectWrapper> MovementList = new List <GH_ObjectWrapper>(); if (!DA.GetDataList(1, MovementList)) { return; } int sorted = new int(); if (!DA.GetData(2, ref sorted)) { return; } #endregion #region Utilities //Utility to convert list of strings to Settings Dictionary SilkwormUtility sUtil = new SilkwormUtility(); Dictionary <string, string> Settings = sUtil.convertSettings(silkwormSettings); #endregion double layerHeight = double.Parse(Settings["layer_height"]); int layerHeightDP = CountDecimalPlaces(layerHeight); #region Output Holders //GCode Lines List <GH_String> sGCode = new List <GH_String>(); //Unsorted Movements List <SilkwormMovement> unMovements = new List <SilkwormMovement>(); //Movements sorted by Layer List <SilkwormMovement>[] sMovements = new List <SilkwormMovement> [0]; #endregion //Wrapper List for all types to Parse GH_ObjectWrapper[] Movement = MovementList.ToArray(); //Switch sorting routine by input switch (sorted) { case 1: #region Unsorted (Sort Order of Movements by z then x and y) if (sorted == 1) { //Parse #region Parse Input Type List <Brep> solids = new List <Brep>(); List <Mesh> meshes = new List <Mesh>(); //Incomplete Silkworm Movements List <SilkwormMovement> incmovements = new List <SilkwormMovement>(); List <Brep> shapes = new List <Brep>(); List <Curve> curves = new List <Curve>(); List <Polyline> plines = new List <Polyline>(); List <Line> lines = new List <Line>(); for (int i = 0; i < Movement.Length; i++) { //Sort Types into Container Lists #region IfNull if ((Movement[i].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { incmovements.Add(aMovement); continue; } } #endregion #region Solids else if (Movement[i].Value is GH_Brep) { Brep solid = null; GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); solids.Add(solid); continue; } else if (Movement[i].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); meshes.Add(mesh); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[i].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); curves.Add(curve); continue; } #endregion #region SilkwormLine else if (Movement[i].Value is SilkwormLine) { List <SilkwormLine> slines = new List <SilkwormLine>(); slines.Add((SilkwormLine)Movement[i].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[i].Value)) { unMovements.Add(new SilkwormMovement(slines, new Delimiter())); } else { incmovements.Add(new SilkwormMovement(slines, new Delimiter())); } continue; } #endregion #region Line else if (Movement[i].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); lines.Add(line); continue; } #endregion #region Point else if (Movement[i].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } #endregion #endregion } //Build Movements from Incomplete Parts #region Build Movement #region List<Brep>Add to unMovements if (solids.Count > 0) { List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); } #endregion #region List<Mesh> Add to unMovements //TODO #endregion #region List<Shapes> Add to unMovements //TODO #endregion #region List<Curve> Add to unMovements foreach (Curve curve in curves) { SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); } #endregion #region List<Line> Add to unMovements foreach (Line line in lines) { List <Line> _lines = new List <Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); } #endregion #region List<IncompleteSilkwormMovement> Add to unMovements foreach (SilkwormMovement movement in incmovements) { unMovements.Add(completeMovement(Settings, movement));; continue; } #endregion #endregion } goto SortZ; #endregion break; case 2: #region Partially Sorted (Sort Movements by z only) if (sorted == 2) { for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { unMovements.Add(completeMovement(Settings, aMovement)); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List <Brep> solids = new List <Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List <SilkwormLine> s_lines = new List <SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { unMovements.Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List <Line> lines = new List <Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List <SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List <SilkwormLine> Movements = new List <SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; unMovements.Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List <Line> _lines = new List <Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto SortZ; #endregion break; case 3: #region Completely Sorted if (sorted == 3) { //Initialise sMovements sMovements = new List <SilkwormMovement> [1]; sMovements[0] = new List <SilkwormMovement>(); for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //Make sure it has a config field sMovements[0].Add(aMovement); continue; } else { sMovements[0].Add(completeMovement(Settings, aMovement)); //sMovements[0].Add(new SilkwormMovement(Movements, new Delimiter())); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List <Brep> solids = new List <Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List <SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); sMovements[0].AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); sMovements[0].Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List <SilkwormLine> s_lines = new List <SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { sMovements[0].Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List <Line> lines = new List <Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List <SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List <SilkwormLine> Movements = new List <SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; sMovements[0].Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List <Line> _lines = new List <Line>(); _lines.Add(line); sMovements[0].Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto Compiler; #endregion break; default: break; } #region ss ////Parse //#region Parse Input Type //List<Brep> solids = new List<Brep>(); //List<Mesh> meshes = new List<Mesh>(); ////Incomplete Silkworm Movements //List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); //List<Brep> shapes = new List<Brep>(); //List<Curve> curves = new List<Curve>(); //List<Polyline> plines = new List<Polyline>(); //List<Line> lines = new List<Line>(); //for (int i = 0; i < Movement.Length; i++) //{ ////Sort Types into Container Lists // #region IfNull // if ((Movement[i].Value == null)) // { continue; } // #endregion //#region SilkwormMovement //if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) //{ // SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; // if(aMovement.complete) // { // unMovements.Add(aMovement); // continue; // } // else // { // incmovements.Add(aMovement); // continue; // } //} //#endregion //#region Solids //if (Movement[i].Value is GH_Brep) //{ // Brep solid = null; // GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); // solids.Add(solid); // continue; //} //if (Movement[i].Value is GH_Mesh) //{ // Mesh mesh = null; // GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); // meshes.Add(mesh); // continue; //} //#endregion //#region Regions ////TODO //#endregion //#region Curves //if (Movement[i].Value is GH_Curve) //{ // Curve curve = null; // GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); // curves.Add(curve); // continue; //} //#endregion //#region SilkwormLine //if (Movement[i].Value is SilkwormLine) //{ // List<SilkwormLine> slines = new List<SilkwormLine>(); // slines.Add((SilkwormLine)Movement[i].Value); // //Check if complete // if (isCompleteLine((SilkwormLine)Movement[i].Value)) // { // unMovements.Add(new SilkwormMovement(slines, true)); // } // else // { // incmovements.Add(new SilkwormMovement(slines, true)); // } // continue; //} //#endregion //#region Line // if (Movement[i].Value is GH_Line) //{ // Line line = new Line(); // GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); // lines.Add(line); //} //#endregion //#region Point // if (Movement[i].Value is GH_Point) //{ // //TODO // continue; //} //#endregion //else //{ // AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); // continue; //} //#endregion //} ////Build Movements from Incomplete Parts //#region Build Movement //#region List<Brep>Add to unMovements // //List<Brep> solidS = new List<Brep>(); // //solidS.Add((Brep)solid); // List<Polyline>[] rPerimeter = new List<Polyline>[0]; // List<Polyline>[] rInfill = new List<Polyline>[0]; // //Slice, Skin and Fill Breps (TODO: Add Bridge Finder) // SilkwormSkein skein = new SilkwormSkein(Settings, solids); // if (skein.validPos) // { // rPerimeter = skein.plinePerimeter; // rInfill = skein.plineInfill; // } // List<SilkwormMovement> solidMovements = new List<SilkwormMovement>(); // //Add to ORGANISED List of Movements (i.e. Model) // for (int b = 0; b <= rPerimeter.GetUpperBound(0); b++) // { // foreach (Polyline pline in rPerimeter[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // foreach (Polyline pline in rInfill[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // } // unMovements.AddRange(solidMovements); //#endregion //#region List<Mesh> Add to unMovements ////TODO //#endregion //#region List<Shapes> Add to unMovements ////TODO //#endregion //#region List<Curve> Add to unMovements //foreach (Curve curve in curves) //{ // SilkwormMovement movement = new SilkwormMovement(Settings, curve, true); // unMovements.Add(movement); //} //#endregion //#region List<Line> Add to unMovements //foreach (Line line in lines) //{ // List<Line> _lines = new List<Line>(); // _lines.Add(line); // unMovements.Add(new SilkwormMovement(Settings, _lines, true)); //} //#endregion //#region List<IncompleteSilkwormMovement> Add to unMovements //foreach (SilkwormMovement movement in incmovements) // { // List<Line> lines = new List<Line>(); // SilkwormLine[] s_Movement = new SilkwormLine[movement.Count]; // foreach (SilkwormLine line in movement) // { // lines.Add(line.Curve); // } // List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines,true).sMovement; // List<SilkwormLine> Movements = new List<SilkwormLine>(); // //Complete Movements // for (int j = 0; j < sLines.Count; j++) // { // if (s_Movement[j].Flow == -1) // { // s_Movement[j].Flow = sLines[j].Flow; // } // if (s_Movement[j].Speed == -1) // { // s_Movement[j].Speed = sLines[j].Speed; // } // Movements.Add(s_Movement[j]); // } // //Add Configuration // movement.Configuration = Settings; // unMovements.Add(new SilkwormMovement(Movements, true)); // continue; //} //#endregion //#endregion #endregion //Sort Unorganised Movements into Layered Model SortZ: #region Build Model List <double> uniqueZ = new List <double>(); //Find Unique ZValues uniqueZ.AddRange(FindUniqueZValues(unMovements, layerHeightDP)); //Sort List of Unique Z Levels uniqueZ.Sort(); //Make Dictionary from List of Unique Z Levels Dictionary <double, int> ZLevel = new Dictionary <double, int>(); for (int d = 0; d < uniqueZ.Count; d++) { ZLevel.Add(uniqueZ[d], d); } //Initialise Array of Lists sMovements = new List <SilkwormMovement> [uniqueZ.Count]; for (int a = 0; a < sMovements.Length; a++) { sMovements[a] = new List <SilkwormMovement>(); } //Sort Silkworm Movements into correct layers in Array of Lists foreach (SilkwormMovement movement in unMovements) { sMovements[ZLevel[Math.Round(movement.ZDomain.T0, layerHeightDP)]].Add(movement); } #endregion goto Compiler; //Compile Model to GCode Compiler: #region GCode Compiler #region HEADER //Add Custom Commands at Start string header = Settings["start_gcode"]; //Char[] splitChar = new Char[] {'\\','\n', 'n'}; string[] splitChar = new string[] { "\\n" }; string[] parts = header.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (parts != null) { for (int i = 0; i < parts.Length; i++) { sGCode.Add(new GH_String(parts[i])); } } else { sGCode.Add(new GH_String(header)); } if (int.Parse(Settings["absolute_extrudersteps"]) == 1) //if true use absolute distances for extrusion, otherwise use relative { sGCode.Add(new GH_String("M82 ; use absolute distances for extrusion")); } sGCode.Add(new GH_String("G90 ; use absolute coordinates")); sGCode.Add(new GH_String("G21 ; set units to millimeters")); sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Set Temperature double temp = double.Parse(Settings["temperature"]); sGCode.Add(new GH_String("M104 S" + temp + " ; set temperature")); sGCode.Add(new GH_String("M109 S" + temp + " ; wait for temperature to be reached")); //Extrude a bit of plastic before start sGCode.Add(new GH_String("G1 Z0.0 F360 E1")); #endregion for (int z = 0; z <= sMovements.GetUpperBound(0); z++) { foreach (SilkwormMovement movement in sMovements[z]) { sGCode.AddRange(movement.ToGCode()); } } #region FOOTER sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Add Custom Commands at End string footer = Settings["end_gcode"]; string[] fparts = footer.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (fparts != null) { for (int i = 0; i < fparts.Length; i++) { sGCode.Add(new GH_String(fparts[i])); } } else { sGCode.Add(new GH_String(footer)); } #endregion ////Convert Array of Movements to List //List<SilkwormMovement> outMovements = new List<SilkwormMovement>(); //for (int l = 0; l < sMovements.GetUpperBound(0); l++) //{ // outMovements.AddRange(sMovements[l]); //} #endregion #region Silkworm Model List <SilkwormMovement> s_Model = new List <SilkwormMovement>(); for (int p = 0; p <= sMovements.GetUpperBound(0); p++) { for (int s = 0; s < sMovements[p].Count; s++) { s_Model.Add(sMovements[p][s]); } } List <SilkwormModel> sModel = new List <SilkwormModel>(); sModel.Add(new SilkwormModel(Settings, s_Model)); #endregion //Output GCode and Model #region OUTPUTS DA.SetDataList(0, sGCode); DA.SetDataList(1, sModel); #endregion }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { #region INPUTS // import silkwormSettings file List<string> silkwormSettings = new List<string>(); if (!DA.GetDataList(0, silkwormSettings)) return; // import raw Grasshopper Geometry to convert to Silkworm Movements List<GH_ObjectWrapper> MovementList = new List<GH_ObjectWrapper>(); if (!DA.GetDataList(1, MovementList)) { return; } int sorted = new int(); if (!DA.GetData(2, ref sorted)) { return; } #endregion #region Utilities //Utility to convert list of strings to Settings Dictionary SilkwormUtility sUtil = new SilkwormUtility(); Dictionary<string, string> Settings = sUtil.convertSettings(silkwormSettings); #endregion double layerHeight = double.Parse(Settings["layer_height"]); int layerHeightDP = CountDecimalPlaces(layerHeight); #region Output Holders //GCode Lines List<GH_String> sGCode =new List<GH_String>(); //Unsorted Movements List<SilkwormMovement> unMovements = new List<SilkwormMovement>(); //Movements sorted by Layer List<SilkwormMovement>[] sMovements = new List<SilkwormMovement>[0]; #endregion //Wrapper List for all types to Parse GH_ObjectWrapper[] Movement = MovementList.ToArray(); //Switch sorting routine by input switch (sorted) { case 1: #region Unsorted (Sort Order of Movements by z then x and y) if (sorted == 1) { //Parse #region Parse Input Type List<Brep> solids = new List<Brep>(); List<Mesh> meshes = new List<Mesh>(); //Incomplete Silkworm Movements List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); List<Brep> shapes = new List<Brep>(); List<Curve> curves = new List<Curve>(); List<Polyline> plines = new List<Polyline>(); List<Line> lines = new List<Line>(); for (int i = 0; i < Movement.Length; i++) { //Sort Types into Container Lists #region IfNull if ((Movement[i].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { incmovements.Add(aMovement); continue; } } #endregion #region Solids else if (Movement[i].Value is GH_Brep) { Brep solid = null; GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); solids.Add(solid); continue; } else if (Movement[i].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); meshes.Add(mesh); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[i].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); curves.Add(curve); continue; } #endregion #region SilkwormLine else if (Movement[i].Value is SilkwormLine) { List<SilkwormLine> slines = new List<SilkwormLine>(); slines.Add((SilkwormLine)Movement[i].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[i].Value)) { unMovements.Add(new SilkwormMovement(slines, new Delimiter())); } else { incmovements.Add(new SilkwormMovement(slines, new Delimiter())); } continue; } #endregion #region Line else if (Movement[i].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); lines.Add(line); continue; } #endregion #region Point else if (Movement[i].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } #endregion #endregion } //Build Movements from Incomplete Parts #region Build Movement #region List<Brep>Add to unMovements if (solids.Count > 0) { List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); } #endregion #region List<Mesh> Add to unMovements //TODO #endregion #region List<Shapes> Add to unMovements //TODO #endregion #region List<Curve> Add to unMovements foreach (Curve curve in curves) { SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); } #endregion #region List<Line> Add to unMovements foreach (Line line in lines) { List<Line> _lines = new List<Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); } #endregion #region List<IncompleteSilkwormMovement> Add to unMovements foreach (SilkwormMovement movement in incmovements) { unMovements.Add(completeMovement(Settings, movement)); ; continue; } #endregion #endregion } goto SortZ; #endregion break; case 2: #region Partially Sorted (Sort Movements by z only) if (sorted == 2) { for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //make sure it has a config field unMovements.Add(aMovement); continue; } else { unMovements.Add(completeMovement(Settings, aMovement)); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List<Brep> solids = new List<Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); unMovements.AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); unMovements.Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List<SilkwormLine> s_lines = new List<SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { unMovements.Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List<Line> lines = new List<Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List<SilkwormLine> Movements = new List<SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; unMovements.Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List<Line> _lines = new List<Line>(); _lines.Add(line); unMovements.Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto SortZ; #endregion break; case 3: #region Completely Sorted if (sorted == 3) { //Initialise sMovements sMovements = new List<SilkwormMovement>[1]; sMovements[0] = new List<SilkwormMovement>(); for (int u = 0; u < Movement.Length; u++) { #region IfNull if ((Movement[u].Value == null)) { continue; } #endregion #region SilkwormMovement else if ((Movement[u].Value is Silkworm.Type.SilkwormMovement)) { SilkwormMovement aMovement = (SilkwormMovement)Movement[u].Value; if (aMovement.complete) { aMovement.Configuration = Settings; //Make sure it has a config field sMovements[0].Add(aMovement); continue; } else { sMovements[0].Add(completeMovement(Settings, aMovement)); //sMovements[0].Add(new SilkwormMovement(Movements, new Delimiter())); continue; } } #endregion #region Solids else if (Movement[u].Value is GH_Brep) { Brep solid = null; List<Brep> solids = new List<Brep>(); GH_Convert.ToBrep(Movement[u].Value, ref solid, GH_Conversion.Both); solids.Add(solid); List<SilkwormMovement> solidMovements = addsolidBrep(Settings, solids); sMovements[0].AddRange(solidMovements); continue; } else if (Movement[u].Value is GH_Mesh) { Mesh mesh = null; GH_Convert.ToMesh(Movement[u].Value, ref mesh, GH_Conversion.Both); continue; } #endregion #region Regions //TODO #endregion #region Curves else if (Movement[u].Value is GH_Curve) { Curve curve = null; GH_Convert.ToCurve(Movement[u].Value, ref curve, GH_Conversion.Both); SilkwormMovement movement = new SilkwormMovement(Settings, curve); sMovements[0].Add(movement); continue; } #endregion #region SilkwormLine else if (Movement[u].Value is SilkwormLine) { List<SilkwormLine> s_lines = new List<SilkwormLine>(); s_lines.Add((SilkwormLine)Movement[u].Value); //Check if complete if (isCompleteLine((SilkwormLine)Movement[u].Value)) { sMovements[0].Add(new SilkwormMovement(s_lines, new Delimiter())); } else { List<Line> lines = new List<Line>(); SilkwormLine[] s_Movement = new SilkwormLine[s_lines.Count]; foreach (SilkwormLine line in s_lines) { lines.Add(line.sLine); } List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines).sMovement; List<SilkwormLine> Movements = new List<SilkwormLine>(); //Complete Movements for (int j = 0; j < sLines.Count; j++) { if (s_Movement[j].Flow == -1) { s_Movement[j].Flow = sLines[j].Flow; } if (s_Movement[j].Speed == -1) { s_Movement[j].Speed = sLines[j].Speed; } Movements.Add(s_Movement[j]); } SilkwormMovement newMovement = new SilkwormMovement(Movements, new Delimiter()); //Add Configuration newMovement.Configuration = Settings; sMovements[0].Add(newMovement); } continue; } #endregion #region Line else if (Movement[u].Value is GH_Line) { Line line = new Line(); GH_Convert.ToLine(Movement[u].Value, ref line, GH_Conversion.Both); List<Line> _lines = new List<Line>(); _lines.Add(line); sMovements[0].Add(new SilkwormMovement(Settings, _lines)); continue; } #endregion #region Point else if (Movement[u].Value is GH_Point) { //TODO continue; } #endregion #region Not Supported else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); continue; } } #endregion } goto Compiler; #endregion break; default: break; } #region ss ////Parse //#region Parse Input Type //List<Brep> solids = new List<Brep>(); //List<Mesh> meshes = new List<Mesh>(); ////Incomplete Silkworm Movements //List<SilkwormMovement> incmovements = new List<SilkwormMovement>(); //List<Brep> shapes = new List<Brep>(); //List<Curve> curves = new List<Curve>(); //List<Polyline> plines = new List<Polyline>(); //List<Line> lines = new List<Line>(); //for (int i = 0; i < Movement.Length; i++) //{ ////Sort Types into Container Lists // #region IfNull // if ((Movement[i].Value == null)) // { continue; } // #endregion //#region SilkwormMovement //if ((Movement[i].Value is Silkworm.Type.SilkwormMovement)) //{ // SilkwormMovement aMovement = (SilkwormMovement)Movement[i].Value; // if(aMovement.complete) // { // unMovements.Add(aMovement); // continue; // } // else // { // incmovements.Add(aMovement); // continue; // } //} //#endregion //#region Solids //if (Movement[i].Value is GH_Brep) //{ // Brep solid = null; // GH_Convert.ToBrep(Movement[i].Value, ref solid, GH_Conversion.Both); // solids.Add(solid); // continue; //} //if (Movement[i].Value is GH_Mesh) //{ // Mesh mesh = null; // GH_Convert.ToMesh(Movement[i].Value, ref mesh, GH_Conversion.Both); // meshes.Add(mesh); // continue; //} //#endregion //#region Regions ////TODO //#endregion //#region Curves //if (Movement[i].Value is GH_Curve) //{ // Curve curve = null; // GH_Convert.ToCurve(Movement[i].Value, ref curve, GH_Conversion.Both); // curves.Add(curve); // continue; //} //#endregion //#region SilkwormLine //if (Movement[i].Value is SilkwormLine) //{ // List<SilkwormLine> slines = new List<SilkwormLine>(); // slines.Add((SilkwormLine)Movement[i].Value); // //Check if complete // if (isCompleteLine((SilkwormLine)Movement[i].Value)) // { // unMovements.Add(new SilkwormMovement(slines, true)); // } // else // { // incmovements.Add(new SilkwormMovement(slines, true)); // } // continue; //} //#endregion //#region Line // if (Movement[i].Value is GH_Line) //{ // Line line = new Line(); // GH_Convert.ToLine(Movement[i].Value, ref line, GH_Conversion.Both); // lines.Add(line); //} //#endregion //#region Point // if (Movement[i].Value is GH_Point) //{ // //TODO // continue; //} //#endregion //else //{ // AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Datatype Not Supported!"); // continue; //} //#endregion //} ////Build Movements from Incomplete Parts //#region Build Movement //#region List<Brep>Add to unMovements // //List<Brep> solidS = new List<Brep>(); // //solidS.Add((Brep)solid); // List<Polyline>[] rPerimeter = new List<Polyline>[0]; // List<Polyline>[] rInfill = new List<Polyline>[0]; // //Slice, Skin and Fill Breps (TODO: Add Bridge Finder) // SilkwormSkein skein = new SilkwormSkein(Settings, solids); // if (skein.validPos) // { // rPerimeter = skein.plinePerimeter; // rInfill = skein.plineInfill; // } // List<SilkwormMovement> solidMovements = new List<SilkwormMovement>(); // //Add to ORGANISED List of Movements (i.e. Model) // for (int b = 0; b <= rPerimeter.GetUpperBound(0); b++) // { // foreach (Polyline pline in rPerimeter[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // foreach (Polyline pline in rInfill[b]) // { // //List<SilkwormMovement> sList = new List<SilkwormMovement>(); // //sList.Add(new SilkwormMovement(Settings, pline, true)); // solidMovements.Add(new SilkwormMovement(Settings, pline, true)); // } // } // unMovements.AddRange(solidMovements); //#endregion //#region List<Mesh> Add to unMovements ////TODO //#endregion //#region List<Shapes> Add to unMovements ////TODO //#endregion //#region List<Curve> Add to unMovements //foreach (Curve curve in curves) //{ // SilkwormMovement movement = new SilkwormMovement(Settings, curve, true); // unMovements.Add(movement); //} //#endregion //#region List<Line> Add to unMovements //foreach (Line line in lines) //{ // List<Line> _lines = new List<Line>(); // _lines.Add(line); // unMovements.Add(new SilkwormMovement(Settings, _lines, true)); //} //#endregion //#region List<IncompleteSilkwormMovement> Add to unMovements //foreach (SilkwormMovement movement in incmovements) // { // List<Line> lines = new List<Line>(); // SilkwormLine[] s_Movement = new SilkwormLine[movement.Count]; // foreach (SilkwormLine line in movement) // { // lines.Add(line.Curve); // } // List<SilkwormLine> sLines = new SilkwormMovement(Settings, lines,true).sMovement; // List<SilkwormLine> Movements = new List<SilkwormLine>(); // //Complete Movements // for (int j = 0; j < sLines.Count; j++) // { // if (s_Movement[j].Flow == -1) // { // s_Movement[j].Flow = sLines[j].Flow; // } // if (s_Movement[j].Speed == -1) // { // s_Movement[j].Speed = sLines[j].Speed; // } // Movements.Add(s_Movement[j]); // } // //Add Configuration // movement.Configuration = Settings; // unMovements.Add(new SilkwormMovement(Movements, true)); // continue; //} //#endregion //#endregion #endregion //Sort Unorganised Movements into Layered Model SortZ: #region Build Model List<double> uniqueZ = new List<double>(); //Find Unique ZValues uniqueZ.AddRange(FindUniqueZValues(unMovements, layerHeightDP)); //Sort List of Unique Z Levels uniqueZ.Sort(); //Make Dictionary from List of Unique Z Levels Dictionary<double, int> ZLevel = new Dictionary<double, int>(); for (int d = 0; d < uniqueZ.Count; d++) { ZLevel.Add(uniqueZ[d], d); } //Initialise Array of Lists sMovements = new List<SilkwormMovement>[uniqueZ.Count]; for (int a = 0; a < sMovements.Length; a++) { sMovements[a] = new List<SilkwormMovement>(); } //Sort Silkworm Movements into correct layers in Array of Lists foreach (SilkwormMovement movement in unMovements) { sMovements[ZLevel[Math.Round(movement.ZDomain.T0, layerHeightDP)]].Add(movement); } #endregion goto Compiler; //Compile Model to GCode Compiler: #region GCode Compiler #region HEADER //Add Custom Commands at Start string header = Settings["start_gcode"]; //Char[] splitChar = new Char[] {'\\','\n', 'n'}; string[] splitChar = new string[] { "\\n" }; string[] parts = header.Split(splitChar,StringSplitOptions.RemoveEmptyEntries); if (parts != null) { for (int i = 0; i < parts.Length; i++) { sGCode.Add(new GH_String(parts[i])); } } else { sGCode.Add(new GH_String(header)); } if (int.Parse(Settings["absolute_extrudersteps"]) == 1) //if true use absolute distances for extrusion, otherwise use relative { sGCode.Add(new GH_String("M82 ; use absolute distances for extrusion")); } sGCode.Add(new GH_String("G90 ; use absolute coordinates")); sGCode.Add(new GH_String("G21 ; set units to millimeters")); sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Set Temperature double temp = double.Parse(Settings["temperature"]); sGCode.Add(new GH_String("M104 S" + temp + " ; set temperature")); sGCode.Add(new GH_String("M109 S" + temp + " ; wait for temperature to be reached")); //Extrude a bit of plastic before start sGCode.Add(new GH_String("G1 Z0.0 F360 E1")); #endregion for (int z = 0; z <= sMovements.GetUpperBound(0); z++) { foreach (SilkwormMovement movement in sMovements[z]) { sGCode.AddRange(movement.ToGCode()); } } #region FOOTER sGCode.Add(new GH_String("G92 E0 ; reset extrusion distance")); //Add Custom Commands at End string footer = Settings["end_gcode"]; string[] fparts = footer.Split(splitChar, StringSplitOptions.RemoveEmptyEntries); if (fparts != null) { for (int i = 0; i < fparts.Length; i++) { sGCode.Add(new GH_String(fparts[i])); } } else { sGCode.Add(new GH_String(footer)); } #endregion ////Convert Array of Movements to List //List<SilkwormMovement> outMovements = new List<SilkwormMovement>(); //for (int l = 0; l < sMovements.GetUpperBound(0); l++) //{ // outMovements.AddRange(sMovements[l]); //} #endregion #region Silkworm Model List<SilkwormMovement> s_Model = new List<SilkwormMovement>(); for (int p = 0; p <= sMovements.GetUpperBound(0); p++) { for (int s = 0; s < sMovements[p].Count; s++) { s_Model.Add(sMovements[p][s]); } } List<SilkwormModel> sModel = new List<SilkwormModel>(); sModel.Add(new SilkwormModel(Settings, s_Model)); #endregion //Output GCode and Model #region OUTPUTS DA.SetDataList(0, sGCode); DA.SetDataList(1, sModel); #endregion }
//movimiento x mes para los bitmons public void movimientos(Mapa mapa) { List <Bitmon>[,] bit_mov = new List <Bitmon> [bitmons_simulacion.GetUpperBound(0) + 1, bitmons_simulacion.GetUpperBound(1) + 1]; for (int i = 0; i <= bitmons_simulacion.GetUpperBound(0); i++) { for (int j = 0; j <= bitmons_simulacion.GetUpperBound(0); j++) { bit_mov[i, j] = new List <Bitmon> { }; } } for (int i = 0; i <= bitmons_simulacion.GetUpperBound(0); i++) { for (int j = 0; j <= bitmons_simulacion.GetUpperBound(0); j++) { foreach (Bitmon bitmon in bitmons_simulacion[i, j]) { if (bitmon.especie == "Wetar") { int m = 1; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (bitmon.especie == "Wetar" && mapa.Mterrenos[i + m1, j + m2].getTerreno() == "Agua" && bitmons_simulacion[i + m1, j + m2].Count() < 2 && bit_mov[i + m1, j + m2].Count() < 2) { bit_mov[i + m1, j + m2].Add(bitmon); } m -= 1; } catch { continue; } } } else if (bitmon.especie == "Dorvalo") { int m = 2; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (mapa.Mterrenos[i + m1, j + m2].getTerreno() != "Acuatico" && bitmons_simulacion[i + m1, j + m2].Count() < 2 && bit_mov[i + m1, j + m2].Count() < 2) { bit_mov[i + m1, j + m2].Add(bitmon); } m -= 1; } catch { continue; } } } else if (bitmon.especie == "Ent") { bit_mov[i, j].Add(bitmon); } else { int m = 1; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (bitmons_simulacion[i + m1, j + m2].Count() < 2 && bit_mov[i + m1, j + m2].Count() < 2) { bit_mov[i + m1, j + m2].Add(bitmon); m -= 1; } Console.ReadKey(); } catch { continue; } } } } } } bitmons_simulacion = bit_mov; }
// Positions the iterator at the range containing "target". (Such a range // always exists as long as the target is a valid leaf cell.) // // REQUIRES: target.IsLeaf public void Seek(S2CellId target) { System.Diagnostics.Debug.Assert(target.IsLeaf()); Position = range_nodes_.GetUpperBound(new RangeNode(target, -1)) - 1; }
public FrozenActorLayer(World world, FrozenActorLayerInfo info) { this.info = info; frozen = new Dictionary<uint, FrozenActor>(); bins = new List<FrozenActor>[ world.Map.MapSize.X * Game.CellSize / info.BinSize, world.Map.MapSize.Y * Game.CellSize / info.BinSize]; for (var j = 0; j <= bins.GetUpperBound(1); j++) for (var i = 0; i <= bins.GetUpperBound(0); i++) bins[i, j] = new List<FrozenActor>(); }
/// <summary> /// 图的深度遍历迷宫生成法 /// </summary> private void DfsGraph(int startX, int startY, int widthLimit, int heightLimit) { PathStack = new Stack<Point>();// 用栈来存储路径所通过点的坐标 Point start = new Point(startX, startY), end = new Point(MazeMap.GetUpperBound(0) - 1, MazeMap.GetUpperBound(1) - 1), now; List<int>[,] pointMap = new List<int>[MazeMap.GetUpperBound(0) + 1, MazeMap.GetUpperBound(1) + 1]; for (int i = 0; i <= MazeMap.GetUpperBound(0); i++) { MazeMap[i, 0] = 2; MazeMap[i, MazeMap.GetUpperBound(1)] = 2; } for (int i = 0; i <= MazeMap.GetUpperBound(1); i++) { MazeMap[0, i] = 2; MazeMap[MazeMap.GetUpperBound(1), i] = 2; } for (int i = 0; i <= pointMap.GetUpperBound(0); i++) { for (int j = 0; j <= pointMap.GetUpperBound(1); j++) { pointMap[i, j] = new List<int>(new int[] { 0, 1, 2, 3 }); } } now = new Point(start.X, start.Y); MazeMap[now.X, now.Y] = UnBlock; CreateScript.Add(new ScriptPoint(new Point(now.X, now.Y), false)); PathStack.Push(new Point(now.X, now.Y)); do { if (pointMap[now.X, now.Y].Count == 0) { MazeMap[now.X, now.Y] = Block; CreateScript.Add(new ScriptPoint(new Point(now.X, now.Y), true)); PathStack.Pop(); now = PathStack.Peek(); continue; } int n = _r.Next(0, pointMap[now.X, now.Y].Count), direction; direction = pointMap[now.X, now.Y][n]; pointMap[now.X, now.Y].RemoveAt(n); if (MazeMap[now.X + d[direction, 0], now.Y + d[direction, 1]] == Block) { now = new Point(now.X + d[direction, 0], now.Y + d[direction, 1]); MazeMap[now.X, now.Y] = UnBlock; CreateScript.Add(new ScriptPoint(new Point(now.X, now.Y), false)); } if (now != PathStack.Peek()) { PathStack.Push(new Point(now.X, now.Y)); } if (now == end) { break; } } while (PathStack.Count > 0); for (int i = 0; i <= MazeMap.GetUpperBound(0); i++) { for (int j = 0; j <= MazeMap.GetUpperBound(1); j++) { int rand = _r.Next(0, 2); if (MazeMap[i, j] == Block) { MazeMap[i, j] = rand; CreateScript.Add(new ScriptPoint(new Point(i, j), !Convert.ToBoolean(rand))); } if (MazeMap[i, j] == 2) MazeMap[i, j] = Block; } } }
//movimiento x mes para los bitmons public void movimientos(Mapa mapa) { List <Bitmon>[,] bit_mov = new List <Bitmon> [bitmons_simulacion.GetUpperBound(0) + 1, bitmons_simulacion.GetUpperBound(1) + 1]; for (int i = 0; i <= bitmons_simulacion.GetUpperBound(0); i++) { for (int j = 0; j <= bitmons_simulacion.GetUpperBound(1); j++) { bit_mov[i, j] = new List <Bitmon> { }; } } for (int i = 0; i <= bitmons_simulacion.GetUpperBound(0); i++) { for (int j = 0; j <= bitmons_simulacion.GetUpperBound(1); j++) { foreach (Bitmon bitmon in bitmons_simulacion[i, j]) { if (RevisarAlrededores(bit_mov, i, j)) { if (bitmon.especie == "Wetar") { int m = 1; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (bitmon.especie == "Wetar" && mapa.Mterrenos[i + m1, j + m2].getTerreno() == "Acuatico" && bit_mov[i + m1, j + m2].Count() < 2) { bit_mov[i + m1, j + m2].Add(bitmon); } m -= 1; } catch { continue; } } } else if (bitmon.especie == "Dorvalo") { int m = 2; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (bit_mov[i + m1, j + m2].Count() < 2) { if (RevisarAlrededores(bit_mov, i + m1, j + m2)) { while (true) { int m3 = rnd.Next(-1, 2); int m4 = rnd.Next(-1, 2); try { if (bit_mov[i + m1 + m3, j + m2 + m4].Count() < 2) { bit_mov[i + m1 + m3, j + m2 + m4].Add(bitmon); break; } } catch { } } break; } else { bit_mov[i + m1, j + m2].Add(bitmon); break; } } else { break; } } catch { continue; } } } else if (bitmon.especie == "Ent") { bit_mov[i, j].Add(bitmon); } else { int m = 1; while (m > 0) { int m1 = rnd.Next(-1, 2); int m2 = rnd.Next(-1, 2); try { if (bit_mov[i + m1, j + m2].Count() < 2) { bit_mov[i + m1, j + m2].Add(bitmon); m -= 1; } } catch { continue; } } } } else { bit_mov[i, j].Add(bitmon); continue; } } } } bitmons_simulacion = bit_mov; }
/// <summary> /// Filters the given FASTA files and PDB files with the given options and saves the results to disk. Data needs to be /// cleaned for two reasons, firstly to not pollute or distort the results, and secondly to save unnecessary processing /// operations. /// </summary> /// <param name="cancellationToken"></param> /// <param name="pdbFilesFolders"></param> /// <param name="fastaFiles"></param> /// <param name="proteinOperationOptionFlags"></param> /// <param name="saveFastaFilenameTemplate"></param> /// <param name="consoleTextBox"></param> /// <param name="progressBar"></param> /// <param name="estimatedTimeRemaining"></param> public static void CleanProteins(CancellationToken cancellationToken, decimal maxAtomInterationDistance, string[] pdbFilesFolders, string[] fastaFiles, ProteinOperation proteinOperationOptionFlags, string saveFastaFilenameTemplate, ProgressActionSet progressActionSet, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename) { if (pdbFilesFolders == null || pdbFilesFolders.Length == 0) { if (proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveMultipleModelsInStructure) || proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonInteractingProteinsInStructure) || proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonSymmetricalInStructure) || proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveWrongNumberOfChainsInStructure)) { throw new ArgumentOutOfRangeException(nameof(pdbFilesFolders)); } } if (fastaFiles == null || fastaFiles.Length == 0) { throw new ArgumentOutOfRangeException(nameof(fastaFiles)); } if (string.IsNullOrWhiteSpace(saveFastaFilenameTemplate)) { throw new ArgumentOutOfRangeException(nameof(saveFastaFilenameTemplate)); } string[] pdbFilesArray = ProteinDataBankFileOperations.GetPdbFilesArray(pdbFilesFolders); //List<string> pdbIdList = null; int beforeCount = 0; int afterCount = 0; string saveFilename = saveFastaFilenameTemplate; var currentProteinOperation = ProteinOperation.LoadFile; int[] numberSequencesLoaded; var sequences = new List <ISequence> [3]; //UserProteinInterfaceOperations.TextBoxClear(consoleTextBox); ProgressActionSet.Report("Filtering proteins.", progressActionSet); // Load fasta/sequence files. sequences[0] = SequenceFileHandler.LoadSequenceFileList(fastaFiles, StaticValues.MolNameProteinAcceptedValues, out numberSequencesLoaded, true); var pdbIdChainIdList = ProteinDataBankFileOperations.PdbIdChainIdList(sequences[0]); for (int numberSequencesLoadedIndex = 0; numberSequencesLoadedIndex < numberSequencesLoaded.Length; numberSequencesLoadedIndex++) { if (numberSequencesLoaded[numberSequencesLoadedIndex] > 0) { ProgressActionSet.Report("Loaded " + numberSequencesLoaded[numberSequencesLoadedIndex] / 2 + " proteins from file: " + fastaFiles[numberSequencesLoadedIndex], progressActionSet); } else { ProgressActionSet.Report("Error could not load file: " + fastaFiles[numberSequencesLoadedIndex], progressActionSet); } } if (numberSequencesLoaded.Count(a => a > 0) == 0) { return; } // Replace placeholder variable names. saveFilename = saveFilename.Replace("%date%", DateTime.Now.ToString("yyyy-MM-dd")); saveFilename = saveFilename.Replace("%time%", DateTime.Now.ToString("HH.mm.ss")); // Save initial loaded sequences. if (File.Exists(saveFilename)) { if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename) { saveFilename = FileExistsHandler.FindNextFreeOutputFilename(saveFilename); } else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile) { } else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile) { return; } } // Removes any entries not having a protein alphabet. while (currentProteinOperation != ProteinOperation.Finished) { if (cancellationToken.IsCancellationRequested) { break; } currentProteinOperation = (ProteinOperation)((int)currentProteinOperation * 2); sequences[1] = null; sequences[2] = null; var sequencesDescriptions = new string[3]; if (currentProteinOperation == ProteinOperation.Finished) { break; } if (currentProteinOperation == ProteinOperation.RemoveNonProteinAlphabetInSequence && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonProteinAlphabetInSequence)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveWrongNumberOfChainsInSequence && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveWrongNumberOfChainsInSequence)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveExactDuplicatesInSequence && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveExactDuplicatesInSequence)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveNonHomodimersInSequence && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonHomodimersInSequence)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveWrongNumberOfChainsInStructure && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveWrongNumberOfChainsInStructure)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveMultipleModelsInStructure && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveMultipleModelsInStructure)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveNonInteractingProteinsInStructure && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonInteractingProteinsInStructure)) { continue; } if (currentProteinOperation == ProteinOperation.RemoveNonSymmetricalInStructure && !proteinOperationOptionFlags.HasFlag(ProteinOperation.RemoveNonSymmetricalInStructure)) { continue; } // Count sequences before operation. beforeCount = sequences[0].Count / 2; // Update user about what is happening. ProgressActionSet.Report("", progressActionSet); ProgressActionSet.Report("Removing " + ProteinOperationString(currentProteinOperation) + " entries [from " + beforeCount + " proteins]", progressActionSet); // Start stopwatch to count duration of operation. Stopwatch stopwatch = Stopwatch.StartNew(); // Perform specified operation. switch (currentProteinOperation) { case ProteinOperation.RemoveNonProteinAlphabetInSequence: { sequencesDescriptions[0] = "01 - Removed non-protein sequences (sequence filter)"; sequences[0] = FilterProteins.RemoveNonProteinAlphabetSequences(cancellationToken, sequences[0], progressActionSet); break; } case ProteinOperation.RemoveWrongNumberOfChainsInSequence: { sequencesDescriptions[0] = "02 - Removed non-dimers (sequence filter)"; sequences[0] = FilterProteins.RemoveSequencesWithIncorrectNumberOfChains(cancellationToken, sequences[0], 2, progressActionSet); break; } case ProteinOperation.RemoveExactDuplicatesInSequence: { sequencesDescriptions[0] = "03 - Removed exact duplicates (sequence filter)"; sequences[0] = FilterProteins.RemoveDuplicates(cancellationToken, sequences[0], progressActionSet); break; } case ProteinOperation.RemoveNonHomodimersInSequence: { // homodimers - all types - unfiltered for interactions or symmetry var result = FilterProteins.SplitDimerTypes(cancellationToken, sequences[0], 30, 90, progressActionSet); sequencesDescriptions[0] = "04 - Homodimers only (sequence filter)"; sequences[0] = result.HomoDimerPdbIdList; sequencesDescriptions[1] = "04 - Heterodimers only (sequence filter)"; sequences[1] = result.HeteroDimerPdbIdList; sequencesDescriptions[2] = "04 - Homology dimers only (sequence filter)"; sequences[2] = result.HomologyDimerPdbIdList; break; } case ProteinOperation.RemoveMultipleModelsInStructure: { sequencesDescriptions[0] = "05 - Removed multiple models (structure filter)"; List <string> pdbIdList = FilterProteins.SequenceListToPdbIdList(sequences[0]); pdbIdList = FilterProteins.RemoveMultipleStructureModels(cancellationToken, pdbFilesFolders, pdbIdList, progressActionSet); sequences[0] = FilterProteins.RemoveSequences(cancellationToken, sequences[0], pdbIdList, FilterProteins.RemoveSequencesOptions.RemoveSequencesInList); break; } case ProteinOperation.RemoveWrongNumberOfChainsInStructure: { sequencesDescriptions[0] = "06 - Removed non-dimers (structure filter)"; List <string> pdbIdList = FilterProteins.SequenceListToPdbIdList(sequences[0]); //var pdbIdChainIdList = ProteinDataBankFileOperations.PdbIdChainIdList(sequences[0]); pdbIdList = FilterProteins.RemoveStructuresWithIncorrectNumberOfChains(cancellationToken, pdbFilesFolders, pdbIdList, pdbIdChainIdList, 2, progressActionSet); sequences[0] = FilterProteins.RemoveSequences(cancellationToken, sequences[0], pdbIdList, FilterProteins.RemoveSequencesOptions.RemoveSequencesInList); break; } case ProteinOperation.RemoveNonInteractingProteinsInStructure: { // Make copy of sequences as we will split the list into two parts - with and without interactions. sequences[1] = new List <ISequence>(sequences[0]); // Get pdb id list from sequences, to check for pdb file, load, perform processing. List <string> pdbIdList = FilterProteins.SequenceListToPdbIdList(sequences[0]); // Makes a list of sequences with interactions. pdbIdList = FilterProteins.RemoveSequencesWithoutInteractions(cancellationToken, maxAtomInterationDistance, pdbFilesFolders, pdbIdList, pdbIdChainIdList, progressActionSet); // Remove any protein not in the list, keep the ones in the list. sequencesDescriptions[0] = "08 - dimers - with interactions - unfiltered for symmetry"; sequences[0] = FilterProteins.RemoveSequences(cancellationToken, sequences[0], pdbIdList, FilterProteins.RemoveSequencesOptions.RemoveSequencesNotInList); sequencesDescriptions[1] = "07 - dimers - no observed interactions"; sequences[1] = FilterProteins.RemoveSequences(cancellationToken, sequences[1], pdbIdList, FilterProteins.RemoveSequencesOptions.RemoveSequencesInList); break; } case ProteinOperation.RemoveNonSymmetricalInStructure: { // Make copy of sequences as we will split the list into two parts - with and without symmetry. List <string> pdbIdList = FilterProteins.SequenceListToPdbIdList(sequences[0]); sequences[1] = new List <ISequence>(sequences[0]); sequences[2] = new List <ISequence>(sequences[0]); Dictionary <string, decimal> symmetryPercentage = FilterProteins.CalculateStructureSymmetry(cancellationToken, maxAtomInterationDistance, pdbFilesFolders, pdbIdList, pdbIdChainIdList, progressActionSet); var pdbSymmetrical = new List <string>(); var pdbPartSymmetrical = new List <string>(); var pdbNonSymmetrical = new List <string>(); foreach (var symmetryPercentageKeyValuePair in symmetryPercentage) { if (symmetryPercentageKeyValuePair.Value == 0.0m) { pdbNonSymmetrical.Add(symmetryPercentageKeyValuePair.Key); } else if (symmetryPercentageKeyValuePair.Value == 100.0m) { pdbSymmetrical.Add(symmetryPercentageKeyValuePair.Key); } else if (symmetryPercentageKeyValuePair.Value > 0.0m && symmetryPercentageKeyValuePair.Value < 100.0m) { pdbPartSymmetrical.Add(symmetryPercentageKeyValuePair.Key); } else { ProgressActionSet.Report("Error: Out of bounds symmetry value of " + symmetryPercentageKeyValuePair.Value + " was found in " + symmetryPercentageKeyValuePair.Key + ".", progressActionSet); } } sequencesDescriptions[0] = "11 - dimers - with interactions - 100% symmetrical"; sequences[0] = FilterProteins.RemoveSequences(cancellationToken, sequences[0], pdbSymmetrical, FilterProteins.RemoveSequencesOptions.RemoveSequencesNotInList); sequencesDescriptions[1] = "10 - dimers - with interactions - 1% to 99% symmetrical"; sequences[1] = FilterProteins.RemoveSequences(cancellationToken, sequences[1], pdbPartSymmetrical, FilterProteins.RemoveSequencesOptions.RemoveSequencesNotInList); sequencesDescriptions[2] = "09 - dimers - with interactions - 0% symmetrical"; sequences[2] = FilterProteins.RemoveSequences(cancellationToken, sequences[2], pdbNonSymmetrical, FilterProteins.RemoveSequencesOptions.RemoveSequencesNotInList); break; } } // Stop stopwatch immediately after operation. stopwatch.Stop(); // Count sequences after operation. afterCount = sequences[0].Count / 2; if (!cancellationToken.IsCancellationRequested) { for (int sequencesIndex = sequences.GetLowerBound(0); sequencesIndex <= sequences.GetUpperBound(0); sequencesIndex++) { if (sequences[sequencesIndex] != null) { // Find free filename to save the latest sequence results of operations. string localSaveFilename = saveFilename; localSaveFilename = localSaveFilename.Replace("%fasta_filename%", sequencesDescriptions[sequencesIndex]); bool skipFile = false; if (File.Exists(localSaveFilename)) { if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename) { localSaveFilename = FileExistsHandler.FindNextFreeOutputFilename(localSaveFilename); } else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile) { } else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile) { skipFile = true; } } if (!skipFile) { // Save the sequence results to previous set filename. string savedFile /*s*/ = SequenceFileHandler.SaveSequencesAsFasta(sequences[sequencesIndex], localSaveFilename); // Inform user that file has been saved. //foreach (char savedFile in savedFiles) //{ ProgressActionSet.Report("Saved file: " + savedFile, progressActionSet); //} } } } // Update the user about the results. ProgressActionSet.Report("Removed " + (beforeCount - afterCount) + " proteins. [" + afterCount + " proteins remaining]. Elapsed: " + stopwatch.Elapsed.ToString(@"dd\:hh\:mm\:ss"), progressActionSet); } } if (!cancellationToken.IsCancellationRequested) { ProgressActionSet.Report("Finished all selected filtering operations.", progressActionSet); } else { ProgressActionSet.Report("Cancelled.", progressActionSet); //UserProteinInterfaceOperations.ProgressBarReset(progressBar, 0, 100, 0); ////UserProteinInterfaceOperations.LabelEstimatedTimeRemainingUpdate(estimatedTimeRemaining, 0, 1, 1); ProgressActionSet.StartAction(100, progressActionSet); ProgressActionSet.ProgressAction(100, progressActionSet); ProgressActionSet.FinishAction(false, progressActionSet); } }