Skip to content

Commit

Permalink
Merge pull request #21 from philipbelesky/feature/context-menu
Browse files Browse the repository at this point in the history
Added right-click menu to select different visual styles
  • Loading branch information
mariuszhermansdorfer authored Sep 6, 2019
2 parents 6b746a2 + ffa96ad commit f06b2f2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion SandWorm/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public static Mesh CreateQuadMesh(Mesh mesh, List<Point3f> vertices, List<Color>
mesh.Vertices.AddVertices(vertices);
}

mesh.VertexColors.SetColors(colors.ToArray());
if (colors.Count > 0) // Colors only provided if the mesh style permits
{
mesh.VertexColors.SetColors(colors.ToArray());
}
return mesh;
}

Expand Down
43 changes: 40 additions & 3 deletions SandWorm/SandWormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Grasshopper.Kernel;
using Rhino.Geometry;
using Microsoft.Kinect;
using System.Windows.Forms;
// 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
Expand All @@ -25,6 +26,8 @@ public class SandWorm : GH_Component

public static int depthPoint;
public static Color[] lookupTable = new Color[1500]; //to do - fix arbitrary value assuming 1500 mm as max distance from the kinect sensor
enum MeshColorStyle { noColor, byElevation };
private MeshColorStyle selectedColorStyle = MeshColorStyle.byElevation; // Must be private to be less accessible than enum type
public List<Color> vertexColors;
public Mesh quadMesh = new Mesh();

Expand Down Expand Up @@ -87,6 +90,35 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
pManager.AddTextParameter("Output", "O", "Output", GH_ParamAccess.list); //debugging
}

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
{
base.AppendAdditionalComponentMenuItems(menu);
Menu_AppendItem(menu, "Color Mesh by Elevation", SetMeshColorStyle, true, selectedColorStyle == MeshColorStyle.byElevation);
menu.Items[menu.Items.Count - 1].Tag = MeshColorStyle.byElevation;
Menu_AppendItem(menu, "Do Not Color Mesh", SetMeshColorStyle, true, selectedColorStyle == MeshColorStyle.noColor);
menu.Items[menu.Items.Count - 1].Tag = MeshColorStyle.noColor;
}

private void SetMeshColorStyle(object sender, EventArgs e)
{
ToolStripMenuItem selectedItem = (ToolStripMenuItem)sender;
ToolStrip parentMenu = selectedItem.Owner as ToolStrip;
if ((MeshColorStyle)selectedItem.Tag != selectedColorStyle) // Update style if it was changed
{
selectedColorStyle = (MeshColorStyle)selectedItem.Tag;
ExpireSolution(true);
quadMesh.VertexColors.Clear(); // Must flush mesh colors to properly updated display
}
for (int i = 0; i < parentMenu.Items.Count; i++) // Easier than foreach as types differ
{
if (parentMenu.Items[i] is ToolStripMenuItem && parentMenu.Items[i].Tag != null)
{
ToolStripMenuItem menuItem = parentMenu.Items[i] as ToolStripMenuItem;
menuItem.Checked = true; // Toggle state of menu items
}
}
}

private void ScheduleDelegate(GH_Document doc)
{
ExpireSolution(false);
Expand Down Expand Up @@ -142,8 +174,10 @@ protected override void SolveInstance(IGH_DataAccess DA)

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

Core.ComputeLookupTable(waterLevel, lookupTable); //precompute all vertex colors

if (selectedColorStyle == MeshColorStyle.byElevation)
{
Core.ComputeLookupTable(waterLevel, lookupTable); //precompute all vertex colors
}

if (this.kinectSensor == null)
{
Expand Down Expand Up @@ -208,7 +242,10 @@ protected override void SolveInstance(IGH_DataAccess DA)


tempPoint.Z = (float)((depthPoint - sensorElevation) * -unitsMultiplier);
vertexColors.Add(lookupTable[depthPoint]);
if (selectedColorStyle == MeshColorStyle.byElevation)
{
vertexColors.Add(lookupTable[depthPoint]);
}

pointCloud.Add(tempPoint);
}
Expand Down

0 comments on commit f06b2f2

Please sign in to comment.