Skip to content

Commit

Permalink
added closest point component
Browse files Browse the repository at this point in the history
  • Loading branch information
ryein committed Nov 25, 2020
1 parent 9cf68d9 commit 700924b
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 4 deletions.
Binary file modified DendroAPI/DendroAPI.cpp
Binary file not shown.
3 changes: 3 additions & 0 deletions DendroAPI/DendroAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ extern "C" {
extern DENDRO_API void DendroBlend(DendroGrid * bGrid, DendroGrid * eGrid, double bPosition, double bEnd);
extern DENDRO_API void DendroBlendMask(DendroGrid * bGrid, DendroGrid * eGrid, double bPosition, double bEnd, DendroGrid * mask, double min, double max, bool invert);

// utilities and analysis
extern DENDRO_API float* DendroClosestPoint(DendroGrid* grid, float* vPoints, int vCount, int* rSize);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 7 additions & 0 deletions DendroAPI/DendroGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <openvdb/tools/GridTransformer.h>
#include <openvdb/tools/ParticlesToLevelSet.h>
#include <openvdb/Types.h>
#include <openvdb/tools/VolumeToSpheres.h>

#include <cmath>

Expand Down Expand Up @@ -316,6 +317,12 @@ void DendroGrid::Blend(DendroGrid bGrid, double bPosition, double bEnd, DendroGr
morph.advect(bStart, bEnd);
}

void DendroGrid::ClosestPoint(std::vector<openvdb::Vec3R>& points, std::vector<float>& distances)
{
auto csp = openvdb::tools::ClosestSurfacePoint<openvdb::FloatGrid>::create(*mGrid);
csp->searchAndReplace(points, distances);
}

DendroMesh DendroGrid::Display()
{
return mDisplay;
Expand Down
2 changes: 2 additions & 0 deletions DendroAPI/DendroGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class DendroGrid
void Blend(DendroGrid bGrid, double bPosition, double bEnd);
void Blend(DendroGrid bGrid, double bPosition, double bEnd, DendroGrid vMask, double min, double max, bool invert);

void ClosestPoint(std::vector<openvdb::Vec3R>& points, std::vector<float>& distances);

DendroMesh Display();

void UpdateDisplay();
Expand Down
53 changes: 50 additions & 3 deletions DendroGH/Classes/DendroVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class DendroVolume : IDisposable {
[DllImport ("DendroAPI.dll", CallingConvention = CallingConvention.Cdecl)]
static public extern void DendroBlendMask (IntPtr bGrid, IntPtr eGrid, double bPosition, double bEnd, IntPtr mask, double min, double max, bool invert);

[DllImport("DendroAPI.dll", CallingConvention = CallingConvention.Cdecl)]
static private extern IntPtr DendroClosestPoint(IntPtr grid, float[] vertices, int vCount, out int rSize);

[DllImport ("DendroAPI.dll", CallingConvention = CallingConvention.Cdecl)]
static private extern void DendroToMesh (IntPtr grid);

Expand Down Expand Up @@ -264,7 +267,7 @@ private set {
}
#endregion Properties

#region Methods
#region Methods
/// <summary>
/// read a vdb file and build volume
/// </summary>
Expand Down Expand Up @@ -768,9 +771,53 @@ public DendroVolume Blend (DendroVolume bVolume, double bPosition, double bEnd,

return blend;
}
#endregion Methods

#region Display
public List<Point3d> ClosestPoint(List<Point3d> vPoints)
{
// create point array from point3d list so we can pass to c++
float[] points = new float[vPoints.Count * 3];

int i = 0;
foreach (Point3d pt in vPoints)
{
points[i] = (float)pt.X;
points[i + 1] = (float)pt.Y;
points[i + 2] = (float)pt.Z;

i += 3;
}

float[] cpArray = null; // array which holds reconstructed c++ array
IntPtr cppPointer = IntPtr.Zero; // pointer to array on c++

// pinvoke closest point function
cppPointer = DendroClosestPoint(this.Grid, points, points.Length, out int size);


if (cppPointer != IntPtr.Zero)
{
cpArray = new float[size];
Marshal.Copy(cppPointer, cpArray, 0, size);
}

Marshal.FreeHGlobal(cppPointer);

// convert array to Point3d list
List<Point3d> cPoints = new List<Point3d>();
int j = 0;
while (j < size)
{
var cp = new Point3d(cpArray[j], cpArray[j + 1], cpArray[j + 2]);
cPoints.Add(cp);

j += 3;
}

return cPoints;
}
#endregion Methods

#region Display
/// <summary>
/// update the mesh representation of the volume
/// </summary>
Expand Down
78 changes: 78 additions & 0 deletions DendroGH/Components/ClosestPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;

using Grasshopper.Kernel;
using Rhino.Geometry;

namespace DendroGH.Components
{
public class ClosestPoint : GH_Component
{
/// <summary>
/// Initializes a new instance of the ClosestPoint class.
/// </summary>
public ClosestPoint()
: base("Volume Closest Point", "vCP",
"Find the closest point to a volume from a supplied list of points",
"Dendro", "Analysis")

{
}

/// <summary>
/// Registers all the input parameters for this component.
/// </summary>
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddGenericParameter("Volume", "V", "Volume geometry", GH_ParamAccess.item);
pManager.AddPointParameter("Points", "P", "Points to solve for", GH_ParamAccess.list);
}

/// <summary>
/// Registers all the output parameters for this component.
/// </summary>
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddPointParameter("Closest Points", "CP", "Closest Points on the volume", GH_ParamAccess.list);

}

/// <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)
{
DendroVolume volume = new DendroVolume();
List<Point3d> vPoints = new List<Point3d>();

if (!DA.GetData(0, ref volume)) return;
if (!DA.GetDataList(1, vPoints)) return;

List<Point3d> cp = volume.ClosestPoint(vPoints);

DA.SetDataList(0, cp);
}

/// <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 DendroGH.Properties.Resources.ico_cp;
}
}

/// <summary>
/// Gets the unique ID for this component. Do not change this ID after release.
/// </summary>
public override Guid ComponentGuid
{
get { return new Guid("1aace784-5b00-481c-ad49-fa8eaa10a662"); }
}
}
}
4 changes: 4 additions & 0 deletions DendroGH/DendroGH.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="Classes\DendroMask.cs" />
<Compile Include="Classes\DendroSettings.cs" />
<Compile Include="Classes\DendroVolume.cs" />
<Compile Include="Components\ClosestPoint.cs" />
<Compile Include="Components\MaskCreate.cs" />
<Compile Include="Components\MaskParam.cs" />
<Compile Include="Components\ReadFile.cs" />
Expand Down Expand Up @@ -142,6 +143,9 @@
<ItemGroup>
<None Include="Resources\ico_vox_mesh.png" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\ico_cp.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
12 changes: 11 additions & 1 deletion DendroGH/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DendroGH/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
<data name="ico_bool_union" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ico_bool_union.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="ico_cp" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ico_cp.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="ico_curve_vox" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\ico_curve_vox.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
Binary file added DendroGH/Resources/ico_cp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 700924b

Please sign in to comment.