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

Caching of preview meshes to improve performance #656

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
60 changes: 57 additions & 3 deletions Grasshopper_UI/Goos/GH_BakeableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
using BH.Engine.Reflection;
using System.Drawing;
using BH.oM.Graphics;
using Rhino.Render;

namespace BH.UI.Grasshopper.Goos
{
Expand Down Expand Up @@ -174,10 +175,36 @@ public override bool Write(GH_IWriter writer)

public virtual void DrawViewportMeshes(GH_PreviewMeshArgs args)
{
if (m_RhinoGeometry != null)
Render.IRenderRhinoMeshes(m_RhinoGeometry, args, m_Color);
if (!m_IsMeshPreviewable) //Flagged as not previewable - return
return;

if (m_PreviewMesh == null) //No preview mesh set yet
{
//Create and store mesh. Will return values for surface type objects (surfaces, breps, meshes etc)
m_PreviewMesh = Render.ICreatePreviewMesh(m_RhinoGeometry, args.MeshingParameters);

if (m_PreviewMesh == null)
{
m_IsMeshPreviewable = false; //If no mesh could be extracted, set flag as not required to check again for the same geometry
return;
}
}

if (m_PreviewMesh != null)
{
if (m_PreviewMesh.VertexColors.Count > 0) //If colours are set (RenderMeshes) draw these colours
{
args.Pipeline.DrawMeshFalseColors(m_PreviewMesh);
}
else
{
Rhino.Display.DisplayMaterial mat = Render.RenderMaterial(args.Material, m_PreviewMaterial); //If material is default GH material, BHoM default will be used, if not, default GH material is used.
args.Pipeline.DrawMeshShaded(m_PreviewMesh, mat);
}
}
}


/***************************************************/

public virtual void DrawViewportWires(GH_PreviewWireArgs args)
Expand All @@ -193,8 +220,11 @@ public virtual void DrawViewportWires(GH_PreviewWireArgs args)

private bool SetGeometry()
{
ResetPreviewMeshes(); //Clears cashed preview meshes and resets preview flag.
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved

if (Value == null)
{
m_IsMeshPreviewable = false;
return true;
}
else if (Value is IRender)
Expand All @@ -208,6 +238,8 @@ private bool SetGeometry()
m_thickness = (Value as RenderCurve).Thickness;
m_Geometry = (Value as RenderCurve).Curve;
}
double transparency = (255 - m_Color.A) / (double)255;
m_PreviewMaterial = new Rhino.Display.DisplayMaterial(m_Color, transparency);
return true;
}
else if (Value is BHoMObject)
Expand All @@ -224,12 +256,26 @@ private bool SetGeometry()
}
else
{
m_IsMeshPreviewable = false;
return false;
}
}

/***************************************************/

protected virtual void ResetPreviewMeshes()
{
if (m_PreviewMesh != null)
{
m_PreviewMesh.Dispose();
m_PreviewMesh = null;
}

m_IsMeshPreviewable = true; //Default to true until checked
}

/***************************************************/

private Rhino.Geometry.BoundingBox Bounds()
{
try
Expand Down Expand Up @@ -302,8 +348,16 @@ public bool BakeGeometry(RhinoDoc doc, ObjectAttributes att, out Guid obj_guid)

protected Color m_Color = Color.FromArgb(80, 255, 41, 105);//BHoM pink!

protected int m_thickness = 2;
protected int m_thickness = 2;

protected Rhino.Geometry.Mesh m_PreviewMesh = null;

protected bool m_IsMeshPreviewable = true;

protected Rhino.Display.DisplayMaterial m_PreviewMaterial = m_DefaultMaterial;

private static readonly Rhino.Display.DisplayMaterial m_DefaultMaterial = new Rhino.Display.DisplayMaterial(Color.FromArgb(80, 255, 41, 105), 0.58823529);

/***************************************************/
}
}
5 changes: 3 additions & 2 deletions Grasshopper_UI/Grasshopper_UI.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug64</Configuration>
Expand Down Expand Up @@ -200,6 +200,7 @@
<Compile Include="Hints\EnumHint.cs" />
<Compile Include="Hints\IGeometryHint.cs" />
<Compile Include="Hints\TypeHint.cs" />
<Compile Include="Render\CreatePreviewMesh.cs" />
<Compile Include="Render\RenderPrototypeLabel.cs" />
<Compile Include="Render\RenderColour.cs" />
<Compile Include="Render\RenderMaterial.cs" />
Expand Down Expand Up @@ -414,4 +415,4 @@ echo C:\ProgramData\BHoM\Assemblies\$(TargetName).gha &gt; "$(AppData)\Grasshopp
<Error Condition="!Exists('..\packages\Grasshopper.0.9.76\build\net35\Grasshopper.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grasshopper.0.9.76\build\net35\Grasshopper.targets'))" />
</Target>
<Import Project="..\packages\Grasshopper.0.9.76\build\net35\Grasshopper.targets" Condition="Exists('..\packages\Grasshopper.0.9.76\build\net35\Grasshopper.targets')" />
</Project>
</Project>
189 changes: 189 additions & 0 deletions Grasshopper_UI/Render/CreatePreviewMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2022, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using RHG = Rhino.Geometry;
using Rhino.Display;
using Grasshopper.Kernel;
using System;
using BH.oM.Base.Attributes;
using System.Collections.Generic;
using BH.oM.Geometry;
using System.Drawing;
using BH.Engine.Rhinoceros;

namespace BH.UI.Grasshopper
{
public static partial class Render
{
/***************************************************/
/**** Public Methods - Interfaces ****/
/***************************************************/

public static RHG.Mesh ICreatePreviewMesh(this object obj, RHG.MeshingParameters parameters)
{
if (obj == null)
return null;

if (parameters == null)
parameters = RHG.MeshingParameters.Default;

try
{
return CreatePreviewMesh(obj as dynamic, parameters);
}
catch (Exception)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
return null;
}
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(this List<object> geometry, RHG.MeshingParameters parameters)
{
RHG.Mesh mesh = new RHG.Mesh();
foreach (object geo in geometry)
{
RHG.Mesh itemMesh = CreatePreviewMesh(geo as dynamic, parameters);
if (itemMesh != null)
mesh.Append(itemMesh);
}
return mesh;
}


/***************************************************/
/**** Private Methods - Fallback ****/
/***************************************************/

private static RHG.Mesh CreatePreviewMesh(this object fallback, RHG.MeshingParameters parameters)
{
// fallback in case no method is found for the provided runtime type
return null;
}


/***************************************************/
/**** Public Methods - Surfaces ****/
/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Extrusion surface, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(surface.ToBrep(), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.NurbsSurface surface, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(surface.ToBrep(), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Brep brep, RHG.MeshingParameters parameters)
{
RHG.Mesh[] array = RHG.Mesh.CreateFromBrep(brep, parameters);
if (array == null)
return new RHG.Mesh();

if (array.Length == 1)
return array[0];

RHG.Mesh mesh = new RHG.Mesh();
foreach (var faceMesh in array)
{
mesh.Append(faceMesh);
}
return mesh;
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Surface surface, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(surface.ToBrep(), parameters);
}


/***************************************************/
/**** Public Methods - Solids ****/
/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Cone cone, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(cone.ToBrep(true), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Cylinder cylinder, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(cylinder.ToBrep(cylinder.IsFinite, cylinder.IsFinite), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Sphere sphere, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(sphere.ToBrep(), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Torus torus, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(torus.ToRevSurface().ToBrep(), parameters);
}

/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Box box, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(box.ToBrep(), parameters);
}

/***************************************************/
/**** Public Methods - Mesh ****/
/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.Mesh mesh, RHG.MeshingParameters parameters)
{
return mesh;
}


/***************************************************/
/**** Public Methods - Miscellanea ****/
/***************************************************/

public static RHG.Mesh CreatePreviewMesh(RHG.BoundingBox bbBox, RHG.MeshingParameters parameters)
{
return CreatePreviewMesh(bbBox.ToBrep(), parameters);
}

/***************************************************/
}
}



25 changes: 23 additions & 2 deletions Grasshopper_UI/Render/RenderMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static DisplayMaterial RenderMaterial(DisplayMaterial material, Color cus
{
Color pColour = GH.Instances.ActiveCanvas.Document.PreviewColour;
Color ghColour = material.Diffuse;
if (ghColour.R == pColour.R & // If the color sent by PreviewArgs is the default object PreviewColour
ghColour.G == pColour.G &
if (ghColour.R == pColour.R && // If the color sent by PreviewArgs is the default object PreviewColour
ghColour.G == pColour.G &&
ghColour.B == pColour.B) // Excluding Alpha channel from comparison
{
double transparency = (255 - custom.A) / (double)255;
Expand All @@ -50,6 +50,27 @@ public static DisplayMaterial RenderMaterial(DisplayMaterial material, Color cus
}

/***************************************************/

public static DisplayMaterial RenderMaterial(DisplayMaterial material, DisplayMaterial custom)
{
if (custom == null)
return material;

Color pColour = GH.Instances.ActiveCanvas.Document.PreviewColour;
Color ghColour = material.Diffuse;
if (ghColour.R == pColour.R && // If the color sent by PreviewArgs is the default object PreviewColour
ghColour.G == pColour.G &&
ghColour.B == pColour.B) // Excluding Alpha channel from comparison
{
return custom;
}
else
{
return material;
}
}

/***************************************************/
}
}

Expand Down
7 changes: 6 additions & 1 deletion Grasshopper_UI/Render/RenderRhinoWires.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ public static void RenderRhinoWires(RHG.BoundingBox bbBox, Rhino.Display.Display
pipeline.DrawBox(bbBox, bhColour, thickness);
}

/***************************************************/

public static void RenderRhinoWires(RHG.Box box, Rhino.Display.DisplayPipeline pipeline, Color bhColour, int thickness)
{
pipeline.DrawBox(box, bhColour, thickness);
}

/***************************************************/
/**** Public Methods - Representations ****/
Expand All @@ -262,7 +268,6 @@ public static void RenderRhinoWires(Text3d text3D, Rhino.Display.DisplayPipeline
pipeline.Draw3dText(text3D, bhColour, text3D.TextPlane);
}


/***************************************************/
}
}
Expand Down