public static Rectangle toRec2D(Rectangle3d rec) { return(new Rectangle((int)rec.PointAt(0, 0).X, (int)rec.PointAt(0, 0).Y, (int)rec.Width, (int)rec.Height)); }
/// <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); }
/// <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) { bool run = false; Point3d location = new Point3d(); Point3d target = new Point3d(); double length = 50; double bias = 0; int type = 0; Point3d uv = new Point3d(); Rhino.Display.RhinoViewport viewPort = null; DA.GetData("Run", ref run); DA.GetData("CameraLocation", ref location); DA.GetData("CameraTarget", ref target); DA.GetData("FocalLength", ref length); DA.GetData("Bias", ref bias); DA.GetData("CameraType", ref type); DA.GetData("UV", ref uv); string name = ""; bool flag = true; bool useFrame = false; if (DA.GetData("ViewName", ref name)) { foreach (RhinoView view in Rhino.RhinoDoc.ActiveDoc.Views) { if (view.ActiveViewport.Name == name) { viewPort = view.ActiveViewport; flag = false; break; } } } if (flag) { try { viewPort = Frame.Component.RhinoView.ActiveViewport; useFrame = true; } catch { viewPort = RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport; } } Message = viewPort.Name; //viewPort.DisplayMode.DisplayAttributes.FillMode = DisplayPipelineAttributes.FrameBufferFillMode.SolidColor; //viewPort.DisplayMode.DisplayAttributes.SetFill() if (run) { //basic settings viewPort.SetCameraLocations(target, location); viewPort.CameraUp = Vector3d.ZAxis; switch (type) { case 0: viewPort.ChangeToParallelProjection(true); break; case 1: viewPort.ChangeToTwoPointPerspectiveProjection(length); break; case 2: viewPort.ChangeToPerspectiveProjection(true, length); break; default: this.AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input Type must be in 0-2!"); viewPort.ChangeToPerspectiveProjection(true, length); break; } if (((Param_Number)Params.Input[5]).UseDegrees) { bias = RhinoMath.ToRadians(bias); } Vector3d z = Vector3d.ZAxis; z.Rotate(bias, viewPort.CameraZ); viewPort.CameraUp = z; Point3d[] nearCorners = viewPort.GetNearRect(); Point3d[] farCorners = viewPort.GetFarRect(); Plane targetPl = new Plane(target, viewPort.CameraX, viewPort.CameraY); double param; Line ln1 = new Line(nearCorners[0], farCorners[0]); Line ln2 = new Line(nearCorners[3], farCorners[3]); Intersection.LinePlane(ln1, targetPl, out param); Rectangle3d rectTarget = new Rectangle3d(targetPl, ln1.PointAt(param), ln2.PointAt(param)); Point3d X = rectTarget.PointAt(uv.X, uv.Y); viewPort.SetCameraLocations(NewTarget(location, target, X), location); nearCorners = viewPort.GetNearRect(); farCorners = viewPort.GetFarRect(); Point3d[] corners = new Point3d[4] { (nearCorners[0] * 0.99 + farCorners[0] * 0.01), (nearCorners[1] * 0.99 + farCorners[1] * 0.01), (nearCorners[2] * 0.99 + farCorners[2] * 0.01), (nearCorners[3] * 0.99 + farCorners[3] * 0.01) }; Plane recPlane = new Plane(corners[0], corners[1], corners[2]); Rectangle3d rect = new Rectangle3d(recPlane, corners[0], corners[3]); double viewRectWidth = corners[0].DistanceTo(corners[1]); double unitPerPx; if (useFrame) { unitPerPx = viewRectWidth / (Frame.Component.Play ? Frame.Component.PictWidth : viewPort.Size.Width); Message += "\n" + (Frame.Component.Play ? "Product" : "Preview"); } else { unitPerPx = viewRectWidth / viewPort.Size.Width; } DA.SetData("Rectangle", rect); DA.SetData("UnitPerPx", unitPerPx); } }