Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a separate setup component #35

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions SandWorm/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,42 @@ public static void CopyAsIntArray(ushort[] source, int[] destination, int leftCo
}

}

public static double ConvertDrawingUnits (Rhino.UnitSystem units)
{
double unitsMultiplier = 1.0;

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.0;
break;

case "Inches":
unitsMultiplier = 0.0393701;
break;

case "Feet":
unitsMultiplier = 0.0328084;
break;
}
return unitsMultiplier;
}
}
}
1 change: 1 addition & 0 deletions SandWorm/SandWorm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Core.cs" />
<Compile Include="GaussianBlurProcessor.cs" />
<Compile Include="KinectController.cs" />
<Compile Include="SandWormSetup.cs" />
<Compile Include="SandWormComponent.cs" />
<Compile Include="SandWormInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
93 changes: 31 additions & 62 deletions SandWorm/SandWormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
using System.Windows.Forms;
using System.Linq;

// comment
// In order to load the result of this wizard, you will also need to
// add the output bin/ folder of this project to the list of loaded
// folder in Grasshopper.
// You can use the _GrasshopperDeveloperSettings Rhino command for that.

//Test comment
namespace SandWorm
{
public class SandWorm : GH_Component
Expand All @@ -31,12 +25,14 @@ public class SandWorm : GH_Component
public Color[] vertexColors;
public Mesh quadMesh = new Mesh();

public List<double> options; // List of options coming from the SWSetup component

public double sensorElevation = 1000; // Arbitrary default value (must be >0)
public int leftColumns = 0;
public int rightColumns = 0;
public int topRows = 0;
public int bottomRows = 0;
public int tickRate = 20; // In ms
public int tickRate = 33; // In ms
public int averageFrames = 1;
public int blurRadius = 1;
public static Rhino.UnitSystem units = Rhino.RhinoDoc.ActiveDoc.ModelUnitSystem;
Expand Down Expand Up @@ -64,24 +60,14 @@ public SandWorm()
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddNumberParameter("SensorHeight", "SH", "The height (in document units) of the sensor above your model", GH_ParamAccess.item, sensorElevation);
pManager.AddIntegerParameter("WaterLevel", "WL", "WaterLevel", GH_ParamAccess.item, 1000);
pManager.AddIntegerParameter("LeftColumns", "LC", "Number of columns to trim from the left", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("RightColumns", "RC", "Number of columns to trim from the right", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("TopRows", "TR", "Number of rows to trim from the top", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("BottomRows", "BR", "Number of rows to trim from the bottom", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("TickRate", "TR", "The time interval, in milliseconds, to update geometry from the Kinect. Set as 0 to disable automatic updates.", GH_ParamAccess.item, tickRate);
pManager.AddIntegerParameter("AverageFrames", "AF", "Amount of depth frames to average across. This number has to be greater than zero.", GH_ParamAccess.item, averageFrames);
pManager.AddIntegerParameter("BlurRadius", "BR", "Radius for Gaussian blur.", GH_ParamAccess.item, blurRadius);
pManager.AddNumberParameter("SandWormOptions", "SWO", "Setup & Calibration options", GH_ParamAccess.list);
pManager[0].Optional = true;
pManager[1].Optional = true;
pManager[2].Optional = true;
pManager[3].Optional = true;
pManager[4].Optional = true;
pManager[5].Optional = true;
pManager[6].Optional = true;
pManager[7].Optional = true;
pManager[8].Optional = true;
}

/// <summary>
Expand All @@ -106,6 +92,7 @@ protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown men
}
}


private void SetMeshVisualisation(object sender, EventArgs e)
{
Analysis.AnalysisManager.SetEnabledOptions((ToolStripMenuItem)sender);
Expand All @@ -126,47 +113,24 @@ private void ScheduleDelegate(GH_Document doc)
/// 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;
options = new List<double>();
DA.GetData<int>(0, ref waterLevel);
DA.GetData<int>(1, ref averageFrames);
DA.GetData<int>(2, ref blurRadius);
DA.GetDataList<double>(3, options);

case "Centimeters":
unitsMultiplier = 0.1;
break;

case "Millimeters":
unitsMultiplier = 1;
break;

case "Inches":
unitsMultiplier = 0.0393701;
break;

case "Feet":
unitsMultiplier = 0.0328084;
break;
if (options.Count != 0) // TODO add more robust checking whether all the options have been provided by the user
{
sensorElevation = options[0];
leftColumns = (int)options[1];
rightColumns = (int)options[2];
topRows = (int)options[3];
bottomRows = (int)options[4];
tickRate = (int)options[5];
}
sensorElevation /= unitsMultiplier; // Standardise to mm to match sensor units

unitsMultiplier = Core.ConvertDrawingUnits(units); // Pick the correct multiplier based on the drawing units
sensorElevation /= unitsMultiplier; // Standardise to mm to match sensor units

Stopwatch timer = Stopwatch.StartNew(); //debugging

Expand All @@ -182,7 +146,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
{
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];
Expand All @@ -192,14 +156,15 @@ protected override void SolveInstance(IGH_DataAccess DA)
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))
{
Expand Down Expand Up @@ -230,10 +195,10 @@ protected override void SolveInstance(IGH_DataAccess DA)
renderBuffer.Last.Value[pixel] = (int)sensorElevation;
}
}

averagedDepthFrameData[pixel] = runningSum[pixel] / renderBuffer.Count; //calculate average values

if (renderBuffer.Count >= averageFrames)
if (renderBuffer.Count >= averageFrames)
runningSum[pixel] -= renderBuffer.First.Value[pixel]; //subtract the oldest value from the sum
}

Expand All @@ -250,6 +215,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
timer.Restart(); //debugging
}


// Setup variables for the coloring process
Analysis.AnalysisManager.ComputeLookupTables(sensorElevation); // First-run computing of tables
var enabledMeshColoring = Analysis.AnalysisManager.GetEnabledMeshColoring();
Expand All @@ -258,6 +224,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
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;
Expand All @@ -271,15 +238,17 @@ protected override void SolveInstance(IGH_DataAccess DA)
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)
{
Expand Down
102 changes: 102 additions & 0 deletions SandWorm/SandWormSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;

using Grasshopper.Kernel;
using Rhino.Geometry;

namespace SandWorm
{
public class SandWormSetup : GH_Component
{

public double sensorElevation = 1000; // Arbitrary default value (must be >0)
public int leftColumns = 0;
public int rightColumns = 0;
public int topRows = 0;
public int bottomRows = 0;
public int tickRate = 33; // In ms

public double[] options = new double[6];

/// <summary>
/// Initializes a new instance of the MyComponent1 class.
/// </summary>
public SandWormSetup()
: base("SandWormSetup", "SWSetup",
"This component takes care of all the setup & calibration of your sandbox.",
"Sandworm", "Sandbox")
{
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddNumberParameter("SensorHeight", "SH", "The height (in document units) of the sensor above your model", GH_ParamAccess.item, sensorElevation);
pManager.AddIntegerParameter("LeftColumns", "LC", "Number of columns to trim from the left", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("RightColumns", "RC", "Number of columns to trim from the right", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("TopRows", "TR", "Number of rows to trim from the top", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("BottomRows", "BR", "Number of rows to trim from the bottom", GH_ParamAccess.item, 0);
pManager.AddIntegerParameter("TickRate", "TR", "The time interval, in milliseconds, to update geometry from the Kinect. Set as 0 to disable automatic updates.", GH_ParamAccess.item, tickRate);
pManager[0].Optional = true;
pManager[1].Optional = true;
pManager[2].Optional = true;
pManager[3].Optional = true;
pManager[4].Optional = true;
pManager[5].Optional = true;
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddGenericParameter ("Options", "O", "SandWorm oOptions", GH_ParamAccess.list); //debugging
}

/// <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)
{
DA.GetData<double>(0, ref sensorElevation);
DA.GetData<int>(1, ref leftColumns);
DA.GetData<int>(2, ref rightColumns);
DA.GetData<int>(3, ref topRows);
DA.GetData<int>(4, ref bottomRows);
DA.GetData<int>(5, ref tickRate);

options[0] = sensorElevation;
options[1] = leftColumns;
options[2] = rightColumns;
options[3] = topRows;
options[4] = bottomRows;
options[5] = tickRate;

DA.SetDataList(0, options);
}

/// <summary>
/// Provides an Icon for the component.
/// </summary>
protected override System.Drawing.Bitmap Icon
{
get
{
//You can add image files to your project resources and access them like this:
// return Resources.IconForThisComponent;
return null;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("9ee53381-c269-4fff-9d45-8a2dbefc243c"); }
}
}
}