Skip to content

Commit

Permalink
Numerous API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DaZombieKiller committed Sep 16, 2019
1 parent 10b7ef1 commit ef3694d
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 181 deletions.
2 changes: 1 addition & 1 deletion Native/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project('v-hacd-wrapper', 'cpp')
vhacd_prj = subproject('v-hacd')
vhacd_dep = vhacd_prj.get_variable('vhacd_dep_static')

shared_library('vhacd',
shared_library('VHACD',
files(
'source/main.cpp',
'source/context.cpp'
Expand Down
38 changes: 0 additions & 38 deletions VHacdSharp/ComputeParameters.cs

This file was deleted.

10 changes: 8 additions & 2 deletions VHacdSharp/ConvexDecomposition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ namespace VHacdSharp
{
public class ConvexDecomposition
{
public IReadOnlyList<ConvexHull> ConvexHulls { get; internal set; }
public Vector3D CenterOfMass { get; internal set; }
public IReadOnlyList<ConvexHull> ConvexHulls { get; }
public TVector3<double> CenterOfMass { get; }

public ConvexDecomposition(IReadOnlyList<ConvexHull> convexHulls, TVector3<double> centerOfMass)
{
ConvexHulls = convexHulls;
CenterOfMass = centerOfMass;
}
}
}
147 changes: 147 additions & 0 deletions VHacdSharp/ConvexDecompositionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;

namespace VHacdSharp
{
public struct ConvexDecompositionOptions
{
/// <summary>Maximum concavity.</summary>
public double Concavity { get; private set; }

/// <summary>Bias toward clipping along symmetry planes.</summary>
public double Alpha { get; private set; }

/// <summary>Bias toward clipping along revolution axes.</summary>
public double Beta { get; private set; }

/// <summary>Minimum volume to add vertices to convex-hulls.</summary>
public double MinHullVolume { get; private set; }

/// <summary>Maximum number of voxels generated during the voxelization stage.</summary>
public uint Resolution { get; private set; }

/// <summary>Maximum number of vertices per convex-hull.</summary>
public uint MaxHullVertices { get; private set; }

/// <summary>Granularity of the search for the "best" clipping plane.</summary>
public uint PlaneDownsampling { get; private set; }

/// <summary>Precision of the convex-hull generation process during the clipping plane selection stage.</summary>
public uint HullDownsampling { get; private set; }

/// <summary>Enable/disable normalizing the mesh before applying the convex decomposition.</summary>
public bool PrincipalComponentAnalysis { get; private set; }

/// <summary>Approximate convex decomposition mode.</summary>
public DecompositionMode DecompositionMode { get; private set; }

public bool HullApproximation { get; private set; }

public bool OpenCLAcceleration { get; private set; }

/// <summary>Maximum number of convex hulls to produce from the merge operation.</summary>
public uint MaxHulls { get; private set; }

/// <summary>Project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results.</summary>
public bool ProjectHullVertices { get; private set; }

public static readonly ConvexDecompositionOptions Default = new ConvexDecompositionOptions()
.WithResolution(100_000)
.WithConcavity(.001)
.WithPlaneDownsampling(4)
.WithHullDownsampling(4)
.WithAlpha(.05)
.WithBeta(.05)
.WithDecompositionMode(DecompositionMode.Voxel)
.WithMaxHullVertices(64)
.WithMinHullVolume(.0001)
.WithHullApproximation()
.WithOpenCLAcceleration()
.WithMaxHulls(1024)
.WithProjectHullVertices();

ConvexDecompositionOptions(ConvexDecompositionOptions source)
=> this = source;

public ConvexDecompositionOptions WithConcavity(double concavity)
{
if (concavity < 0 || concavity > 1)
throw new ArgumentOutOfRangeException(nameof(concavity));

return new ConvexDecompositionOptions(this) { Concavity = concavity };
}

public ConvexDecompositionOptions WithAlpha(double alpha)
{
if (alpha < 0 || alpha > 1)
throw new ArgumentOutOfRangeException(nameof(alpha));

return new ConvexDecompositionOptions(this) { Alpha = alpha };
}

public ConvexDecompositionOptions WithBeta(double beta)
{
if (beta < 0 || beta > 1)
throw new ArgumentOutOfRangeException(nameof(beta));

return new ConvexDecompositionOptions(this) { Beta = beta };
}

public ConvexDecompositionOptions WithMinHullVolume(double minHullVolume)
{
if (minHullVolume < 0 || minHullVolume > .01)
throw new ArgumentOutOfRangeException(nameof(minHullVolume));

return new ConvexDecompositionOptions(this) { MinHullVolume = minHullVolume };
}

public ConvexDecompositionOptions WithResolution(uint resolution)
{
if (resolution < 10_000 || resolution > 64_000_000)
throw new ArgumentOutOfRangeException(nameof(resolution));

return new ConvexDecompositionOptions(this) { Resolution = resolution };
}

public ConvexDecompositionOptions WithMaxHullVertices(uint maxHullVertices)
{
if (maxHullVertices < 4 || maxHullVertices > 1024)
throw new ArgumentOutOfRangeException(nameof(maxHullVertices));

return new ConvexDecompositionOptions(this) { MaxHullVertices = maxHullVertices };
}

public ConvexDecompositionOptions WithPlaneDownsampling(uint planeDownsampling)
{
if (planeDownsampling < 1 || planeDownsampling > 16)
throw new ArgumentOutOfRangeException(nameof(planeDownsampling));

return new ConvexDecompositionOptions(this) { PlaneDownsampling = planeDownsampling };
}

public ConvexDecompositionOptions WithHullDownsampling(uint hullDownsampling)
{
if (hullDownsampling < 1 || hullDownsampling > 16)
throw new ArgumentOutOfRangeException(nameof(hullDownsampling));

return new ConvexDecompositionOptions(this) { HullDownsampling = hullDownsampling };
}

public ConvexDecompositionOptions WithPrincipalComponentAnalysis(bool enable = true)
=> new ConvexDecompositionOptions(this) { PrincipalComponentAnalysis = enable };

public ConvexDecompositionOptions WithDecompositionMode(DecompositionMode mode)
=> new ConvexDecompositionOptions(this) { DecompositionMode = mode };

public ConvexDecompositionOptions WithHullApproximation(bool enable = true)
=> new ConvexDecompositionOptions(this) { HullApproximation = enable };

public ConvexDecompositionOptions WithOpenCLAcceleration(bool enable = true)
=> new ConvexDecompositionOptions(this) { OpenCLAcceleration = enable };

public ConvexDecompositionOptions WithMaxHulls(uint maxHulls)
=> new ConvexDecompositionOptions(this) { MaxHulls = maxHulls };

public ConvexDecompositionOptions WithProjectHullVertices(bool enable = true)
=> new ConvexDecompositionOptions(this) { ProjectHullVertices = enable };
}
}
18 changes: 13 additions & 5 deletions VHacdSharp/ConvexHull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace VHacdSharp
{
public class ConvexHull
public readonly struct ConvexHull
{
public IReadOnlyList<double> Points { get; internal set; }
public IReadOnlyList<uint> Triangles { get; internal set; }
public double Volume { get; internal set; }
public Vector3D Center { get; internal set; }
public IReadOnlyList<TVector3<double>> Vertices { get; }
public IReadOnlyList<uint> Indices { get; }
public TVector3<double> Center { get; }
public double Volume { get; }

public ConvexHull(IReadOnlyList<TVector3<double>> vertices, IReadOnlyList<uint> indices, double volume, TVector3<double> center)
{
Vertices = vertices;
Indices = indices;
Center = center;
Volume = volume;
}
}
}
4 changes: 2 additions & 2 deletions VHacdSharp/DecompositionMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public enum DecompositionMode
{
Voxel = 0,
Tetrahedron = 1
Voxel,
Tetrahedron
}
}
41 changes: 41 additions & 0 deletions VHacdSharp/TVector3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;

namespace VHacdSharp
{
public struct TVector3<T>
where T : unmanaged, IConvertible
{
public T X, Y, Z;

public TVector3(T x, T y, T z)
=> (X, Y, Z) = (x, y, z);

public override int GetHashCode()
=> HashCode.Combine(X, Y, Z);

public override string ToString()
=> $"({X}, {Y}, {Z})";

public string ToString(IFormatProvider provider)
=> $"({X.ToString(provider)}, {Y.ToString(provider)}, {Z.ToString(provider)})";
}

public static class TVectorExtensions
{
#if UNITY_STANDALONE
public static Vector3 ToUnity(this TVector3<float> vector) => new Vector3
{
x = vector.X,
y = vector.Y,
z = vector.Z
};

public static Vector3Int ToUnity(this TVector3<int> vector) => new Vector3Int
{
x = vector.X,
y = vector.Y,
z = vector.Z
};
#endif
}
}
37 changes: 17 additions & 20 deletions VHacdSharp/VHacd.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace VHacdSharp
{
partial class VHacd
{
const string DllName = "libvhacd";
const string DllName = "libVHACD";

[UnmanagedFunctionPointer(Cdecl)]
delegate void UserLoggerDelegate(IntPtr gcHandle, IntPtr message);
Expand Down Expand Up @@ -84,26 +84,23 @@ struct UnmanagedComputeParameters
public uint m_maxConvexHulls;
public bool m_projectHullVertices;

public static explicit operator UnmanagedComputeParameters(ComputeParameters parameters)
public static UnmanagedComputeParameters FromDecompositionOptions(ConvexDecompositionOptions options) => new UnmanagedComputeParameters
{
return new UnmanagedComputeParameters
{
m_resolution = parameters.Resolution,
m_concavity = parameters.Concavity,
m_planeDownsampling = parameters.PlaneDownsampling,
m_convexhullDownsampling = parameters.HullDownsampling,
m_alpha = parameters.Alpha,
m_beta = parameters.Beta,
m_pca = parameters.PCA ? 1u : 0u,
m_mode = (uint)parameters.DecompositionMode,
m_maxNumVerticesPerCH = parameters.MaxHullVertices,
m_minVolumePerCH = parameters.MinHullVolume,
m_convexhullApproximation = parameters.HullApproximation ? 1u : 0u,
m_oclAcceleration = parameters.OpenCLAcceleration ? 1u : 0u,
m_maxConvexHulls = parameters.MaxHulls,
m_projectHullVertices = parameters.ProjectHullVertices
};
}
m_resolution = options.Resolution,
m_concavity = options.Concavity,
m_planeDownsampling = options.PlaneDownsampling,
m_convexhullDownsampling = options.HullDownsampling,
m_alpha = options.Alpha,
m_beta = options.Beta,
m_pca = options.PrincipalComponentAnalysis ? 1u : 0u,
m_mode = (uint)options.DecompositionMode,
m_maxNumVerticesPerCH = options.MaxHullVertices,
m_minVolumePerCH = options.MinHullVolume,
m_convexhullApproximation = options.HullApproximation ? 1u : 0u,
m_oclAcceleration = options.OpenCLAcceleration ? 1u : 0u,
m_maxConvexHulls = options.MaxHulls,
m_projectHullVertices = options.ProjectHullVertices
};
}
}
}
Loading

0 comments on commit ef3694d

Please sign in to comment.