/// <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) { Plane plane = Plane.WorldXY; Curve perimCrv = null; coreCrvs = new List <Curve>(); rectangles = new List <Rectangle3d>(); obstacleCrvs = new List <Curve>(); DA.GetData(IN_autoColor, ref autoColor); DA.GetData(IN_plane, ref plane); DA.GetData(IN_perimCrv, ref perimCrv); DA.GetDataList(IN_voxelRects, rectangles); DA.GetDataList(IN_obstacleCrvs, obstacleCrvs); bool coreReceived = DA.GetDataList(IN_coreCrvs, coreCrvs); _plan = new SmartPlan(perimCrv, coreCrvs, rectangles, obstacleCrvs, plane); Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc; Rhino.UnitSystem system = doc.ModelUnitSystem; _plan.projectUnits = system.ToString() == "Meters" ? 0 : 1; _plan.ComputeCovid(); compromisedMetric = _plan.GetCovidMetric(); DA.SetDataList(OUT_hit, compromisedMetric); DA.SetDataTree(OUT_lines, _plan.covidLines); }
/// <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) { DA.GetData <double>(0, ref sensorElevation); DA.GetData <int>(1, ref waterLevel); DA.GetData <int>(2, ref leftColumns); DA.GetData <int>(3, ref rightColumns); DA.GetData <int>(4, ref topRows); DA.GetData <int>(5, ref bottomRows); DA.GetData <int>(6, ref tickRate); DA.GetData <int>(7, ref averageFrames); DA.GetData <int>(8, ref blurRadius); switch (units.ToString()) { case "Kilometers": unitsMultiplier = 0.0001; break; case "Meters": unitsMultiplier = 0.001; break; case "Decimeters": unitsMultiplier = 0.01; break; case "Centimeters": unitsMultiplier = 0.1; break; case "Millimeters": unitsMultiplier = 1; break; case "Inches": unitsMultiplier = 0.0393701; break; case "Feet": unitsMultiplier = 0.0328084; break; } sensorElevation /= unitsMultiplier; // Standardise to mm to match sensor units Stopwatch timer = Stopwatch.StartNew(); //debugging if (this.kinectSensor == null) { KinectController.AddRef(); this.kinectSensor = KinectController.sensor; } if (this.kinectSensor != null) { if (KinectController.depthFrameData != null) { int trimmedWidth = KinectController.depthWidth - leftColumns - rightColumns; int trimmedHeight = KinectController.depthHeight - topRows - bottomRows; // initialize all arrays pointCloud = new Point3d[trimmedWidth * trimmedHeight]; int[] depthFrameDataInt = new int[trimmedWidth * trimmedHeight]; double[] averagedDepthFrameData = new double[trimmedWidth * trimmedHeight]; // initialize outputs outputMesh = new List <Mesh>(); output = new List <string>(); //debugging Point3d tempPoint = new Point3d(); Core.PixelSize depthPixelSize = Core.GetDepthPixelSpacing(sensorElevation); // trim the depth array and cast ushort values to int Core.CopyAsIntArray(KinectController.depthFrameData, depthFrameDataInt, leftColumns, rightColumns, topRows, bottomRows, KinectController.depthHeight, KinectController.depthWidth); averageFrames = averageFrames < 1 ? 1 : averageFrames; //make sure there is at least one frame in the render buffer // reset everything when resizing Kinect's field of view or changing the amounts of frame to average across if (renderBuffer.Count > averageFrames || quadMesh.Faces.Count != (trimmedWidth - 2) * (trimmedHeight - 2)) { renderBuffer.Clear(); Array.Clear(runningSum, 0, runningSum.Length); renderBuffer.AddLast(depthFrameDataInt); } else { renderBuffer.AddLast(depthFrameDataInt); } output.Add("Render buffer length:".PadRight(30, ' ') + renderBuffer.Count + " frames"); //debugging // average across multiple frames for (int pixel = 0; pixel < depthFrameDataInt.Length; pixel++) { if (depthFrameDataInt[pixel] > 200 && depthFrameDataInt[pixel] <= lookupTable.Length) //We have a valid pixel. TODO remove reference to the lookup table { runningSum[pixel] += depthFrameDataInt[pixel]; } else { if (pixel > 0) //pixel is invalid and we have a neighbor to steal information from { runningSum[pixel] += depthFrameDataInt[pixel - 1]; renderBuffer.Last.Value[pixel] = depthFrameDataInt[pixel - 1]; //replace the zero value from the depth array with the one from the neighboring pixel } else //pixel is invalid and it is the first one in the list. (No neighbor on the left hand side, so we set it to the lowest point on the table) { runningSum[pixel] += (int)sensorElevation; renderBuffer.Last.Value[pixel] = (int)sensorElevation; } } averagedDepthFrameData[pixel] = runningSum[pixel] / renderBuffer.Count; //calculate average values if (renderBuffer.Count >= averageFrames) { runningSum[pixel] -= renderBuffer.First.Value[pixel]; //subtract the oldest value from the sum } } timer.Stop(); output.Add("Frames averaging: ".PadRight(30, ' ') + timer.ElapsedMilliseconds.ToString() + " ms"); timer.Restart(); //debugging if (blurRadius > 1) //apply gaussian blur { GaussianBlurProcessor gaussianBlurProcessor = new GaussianBlurProcessor(blurRadius, trimmedWidth, trimmedHeight); gaussianBlurProcessor.Apply(averagedDepthFrameData); timer.Stop(); output.Add("Gaussian blurring: ".PadRight(30, ' ') + timer.ElapsedMilliseconds.ToString() + " ms"); timer.Restart(); //debugging } // Setup variables for the coloring process Analysis.AnalysisManager.ComputeLookupTables(sensorElevation); // First-run computing of tables var enabledMeshColoring = Analysis.AnalysisManager.GetEnabledMeshColoring(); var enabledColorTable = enabledMeshColoring.lookupTable; var hasColorTable = enabledColorTable.Length > 0; // Setting the 'no analysis' option == empty table vertexColors = new Color[hasColorTable ? trimmedWidth * trimmedHeight : 0]; // A 0-length array wont be used in meshing var pixelsForAnalysis = new Point3d[4]; // Setup variables for per-pixel loop pointCloud = new Point3d[trimmedWidth * trimmedHeight]; int arrayIndex = 0; for (int rows = 0; rows < trimmedHeight; rows++) { for (int columns = 0; columns < trimmedWidth; columns++) { depthPoint = averagedDepthFrameData[arrayIndex]; tempPoint.X = columns * -unitsMultiplier * depthPixelSize.x; tempPoint.Y = rows * -unitsMultiplier * depthPixelSize.y; tempPoint.Z = (depthPoint - sensorElevation) * -unitsMultiplier; pointCloud[arrayIndex] = tempPoint; // Add new point to point cloud itself if (hasColorTable) // Perform analysis as needed and lookup result in table { var pixelIndex = enabledMeshColoring.GetPixelIndexForAnalysis(tempPoint, pixelsForAnalysis); vertexColors[arrayIndex] = enabledColorTable[pixelIndex]; } arrayIndex++; } } //keep only the desired amount of frames in the buffer while (renderBuffer.Count >= averageFrames) { renderBuffer.RemoveFirst(); } //debugging timer.Stop(); output.Add("Point cloud generation/color: ".PadRight(30, ' ') + timer.ElapsedMilliseconds.ToString() + " ms"); timer.Restart(); //debugging quadMesh = Core.CreateQuadMesh(quadMesh, pointCloud, vertexColors, trimmedWidth, trimmedHeight); outputMesh.Add(quadMesh); timer.Stop(); //debugging output.Add("Meshing: ".PadRight(30, ' ') + timer.ElapsedMilliseconds.ToString() + " ms"); } DA.SetDataList(0, outputMesh); DA.SetDataList(1, output); //debugging } if (tickRate > 0) // Allow users to force manual recalculation { base.OnPingDocument().ScheduleSolution(tickRate, new GH_Document.GH_ScheduleDelegate(ScheduleDelegate)); } }
public GHScale GetScale(Rhino.RhinoDoc rhinoDoc) { // Scale factor is based on assumption of feet based units. double scale = 1.0; Rhino.UnitSystem us = rhinoDoc.ModelUnitSystem; string units = us.ToString(); scale = 1.0; switch (units) { case "None": scale = 1; break; case "Microns": scale = 1000000; break; case "Millimeters": scale = 1000; break; case "Centimeters": scale = 100; break; case "Meters": scale = 1; break; case "Kilometers": scale = 0.001; break; case "Microinches": scale = 39370100; break; case "Mils": scale = 39370.0787; break; case "Inches": scale = 39.3701; break; case "Feet": scale = 3.28084; break; case "Miles": scale = 0.000621371; break; case "Angstroms": scale = 10000000000; break; case "Nanometers": scale = 1000000000; break; case "Decimeters": scale = 10; break; case "Dekameters": scale = 0.1; break; case "Hectometers": scale = 0.01; break; case "Megameters": scale = 0.000001; break; case "Gigameters": scale = 0.000000001; break; case "Yards": scale = 1.09361; break; case "PrinterPoint": scale = 1; break; case "PrinterPica": scale = 1; break; case "NauticalMile": scale = 0.000539957; break; case "Astronomical": scale = 0.00000000000668458712; break; case "Lightyears": scale = 0.000000000000000105702341; break; case "Parsecs": scale = 0.0000000000000000324077929; break; default: break; } GHScale ghScale = new GHScale(units, scale); return(ghScale); }
/// <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) { double inputValue = 1.0; double targetUnitSys = 0; double convType = 0; //get the model units from the rhino document Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc; // create variables to hold the input and target unit system Rhino.UnitSystem docUnits = doc.ModelUnitSystem; Rhino.UnitSystem targetUnits = doc.ModelUnitSystem; String outMessage = ""; String sourceUnitName = docUnits.ToString(); String targetUnitName = ""; String unitTypeConvName = ""; String userUnitName = ""; double conversionNumber = 1; //create a a value for raising the number to an expoenet //what are you meausuring which translates in power 1 = length, 2 = area, 3 = volume //update baseed based on the selection of the menu double convExp = conversionExp; //read in the values from sliders/ other components bool success1 = DA.GetData(0, ref inputValue); bool success2 = DA.GetData(1, ref targetUnitSys); bool success3 = DA.GetData(2, ref convType); //initialize a value double convertedValue = 1; if (targetUnitSys == 0) { } else if (targetUnitSys == 1) { targetUnits = (Rhino.UnitSystem)UnitSystem.Microns; } else if (targetUnitSys == 2) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Millimeters; } else if (targetUnitSys == 3) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Centimeters; } else if (targetUnitSys == 4) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Meters; } else if (targetUnitSys == 5) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Kilometers; } else if (targetUnitSys == 6) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Microinches; } else if (targetUnitSys == 7) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Mils; } else if (targetUnitSys == 8) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Inches; } else if (targetUnitSys == 9) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Feet; } else if (targetUnitSys == 10) { //update the unit system targetUnits = (Rhino.UnitSystem)UnitSystem.Miles; } else { //give an error AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Check again your inputs"); } // get the name of the target conversion system targetUnitName = targetUnits.ToString(); // Don't worry about where the Absolute property comes from, we'll get to it soon. if (convType == 0) { //convert from Model Unit System to User Defined conversionNumber = Rhino.RhinoMath.UnitScale(docUnits, targetUnits); // write a message outMessage = "Conversion Done! The Document's Units System is: " + sourceUnitName + " /You are converting TO: " + targetUnitName + " / The unit conversion number is: " + conversionNumber; //convert the value convertedValue = Math.Pow(inputValue * conversionNumber, convExp); } else { // converting from User Defined to Model Units conversionNumber = Rhino.RhinoMath.UnitScale(targetUnits, docUnits); // write a message outMessage = "Conversion Done! The Document's Units System is: " + sourceUnitName + " /You are converting FROM: " + targetUnitName + " / The unit conversion number is: " + conversionNumber; //convert the value convertedValue = Math.Pow(inputValue * conversionNumber, convExp); } DA.SetData(0, convertedValue); DA.SetData(1, outMessage); }