/// <summary> /// A method for creating and saving a Json file from the wallData of the navigation model. /// </summary> /// <param name="wallInfo"> A custom object (WallInformation) that contains wallData in it.</param> /// <param name="filePath"> A directory path for savig the Json file.</param> /// <returns> A string that illustrates result of the serialization and saving process.</returns> private string serialization(WallInformation wallInfo, string filePath) { string result = "Json file was successfully saved."; string json = ""; try { json = JsonConvert.SerializeObject(wallInfo); } catch (System.IO.IOException e) { result = e.ToString(); } if (json != "") { try { System.IO.File.WriteAllText(System.IO.Path.Combine(filePath, "WallInformation.json"), json); } catch (System.IO.IOException e) { result = e.ToString(); } } return(result); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { // First, we need to retrieve all data from the input parameters. // We'll start by declaring variables and assigning them starting values. GH_Structure <GH_Brep> searchAarea = new GH_Structure <GH_Brep>(); int subdivision = new int(); double scaleFactor = new float(); double cellSize = new float(); string filePath = ""; bool run = new bool(); // Then we need to access the input parameters individually. // When data cannot be extracted from a parameter, we should abort this method. if (!DA.GetDataTree <GH_Brep>(0, out searchAarea)) { return; } if (!DA.GetData(1, ref subdivision)) { return; } if (!DA.GetData(2, ref scaleFactor)) { return; } if (!DA.GetData(3, ref cellSize)) { return; } if (!DA.GetData(4, ref filePath)) { return; } if (!DA.GetData(5, ref run)) { return; } // We should now validate the data and warn the user if invalid data is supplied. if (subdivision == 0.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Subdivision level must be larger than zero."); return; } if (scaleFactor < 1.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "ScaleFactor level must be larger than zero."); return; } if (cellSize == 0.0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "CellSize level must be larger than zero."); return; } // We're set to create the spiral now. To keep the size of the SolveInstance() method small, // The actual functionality will be in a different method: string serializationResult = ""; List <object> result = GenerateContext(searchAarea); Brep context = (Brep)result[0]; Mesh searchAreaMesh = (Mesh)result[1]; result = NavigationModel(searchAreaMesh, context, scaleFactor, cellSize, subdivision); DataTree <Polyline> navigationModel = (DataTree <Polyline>)result[0]; DataTree <int> wallData = (DataTree <int>)result[1]; WallInformation wallInfo = (WallInformation)result[2]; if (run == true) { if (filePath != null && filePath != "") { serializationResult = serialization(wallInfo, filePath); } else { serializationResult = "Please provide a directory path for saving the Json text."; } } // Finally assign the spiral to the output parameter. DA.SetData(0, context); DA.SetDataTree(1, navigationModel); DA.SetDataTree(2, wallData); DA.SetData(3, serializationResult); }
/// <summary> /// The Method that generates Navigation Model for the PathFinding algorithm. /// </summary> /// <param name="searchAreaMesh"> A Mesh generated from the input Breps representing the search area.</param> /// <param name="context"> A Brep that represents context of the navigation model.</param> /// <param name="scaleFactor"> A double larger than 1.</param> /// <param name="cellSize"> A double for defining the size of the navigation model cells.</param> /// <param name="subdivision"> Number of subdivision of navigation model's cells.</param> /// <returns> A list of generic types. The first item of the list is a DataTree of Curves for visualizing the navigation model. /// The second item of the list is a DataTree of zeros and ones related to the location of the obstacles and walls in the /// navigation model, which is called wallData. /// The final item of the list is a custom object (WallInformation) that contains the wallData in it. This object will be used /// to generate and save a Json file from the wallData. /// </returns> private List <object> NavigationModel(Mesh searchAreaMesh, Brep context, double scaleFactor, double cellSize, int subdivision) { List <object> result = new List <object>(); int[,] wallDataArray; DataTree <int> wallData = new DataTree <int>(); DataTree <Polyline> navigationCells = new DataTree <Polyline>(); Rectangle3d cell = new Rectangle3d(); Brep scaledContext = new Brep(); scaledContext.Append(context); scaledContext.Transform(Transform.Scale(scaledContext.GetBoundingBox(true).Center, scaleFactor)); int cellCount = Convert.ToInt32(System.Math.Round((context.Edges[0].Domain[1] * scaleFactor) / cellSize)); // Array for serializing to Json wallDataArray = new int[cellCount, cellCount]; Plane modelOrigin = new Plane(scaledContext.GetBoundingBox(true).Corner(true, true, true), Vector3d.ZAxis); Point3d sample = new Point3d(); for (int i = 0; i < cellCount; i++) { for (int j = 0; j < cellCount; j++) { cell = new Rectangle3d(new Plane(new Point3d(modelOrigin.OriginX + (i * cellSize), modelOrigin.OriginY + (j * cellSize), modelOrigin.OriginZ), modelOrigin.Normal), cellSize, cellSize); navigationCells.Add(cell.ToPolyline(), new GH_Path(i)); int intersect = 0; for (double k = 0; k < (subdivision * 4); k++) { sample = cell.PointAt(k * 4 / (subdivision * 4)); if (Rhino.Geometry.Intersect.Intersection.MeshRay(searchAreaMesh, new Ray3d(sample, Vector3f.ZAxis)) >= 0) { intersect++; } } if (intersect >= subdivision * 4) { wallData.Add(0, new GH_Path(i)); wallDataArray[i, j] = 0; } else { wallData.Add(1, new GH_Path(i)); wallDataArray[i, j] = 1; } } } WallInformation wallInfo = new WallInformation(); wallInfo.WallData = wallDataArray; result.Add(navigationCells); result.Add(wallData); result.Add(wallInfo); return(result); }