Skip to content

Commit

Permalink
refactor(mesh): mesh simplifier uses the simplification options struct
Browse files Browse the repository at this point in the history
Previously, MeshSimplifier had separate fields for options, but has now
consolidated all those fields by making use of SimplificationOptions.
Additionally, the SimplificationOptions can be set as a block.
  • Loading branch information
amirebrahimi authored Apr 14, 2020
1 parent 66065e4 commit 87d3fa8
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions Runtime/MeshSimplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,9 @@ public sealed class MeshSimplifier
#endregion

#region Fields
private bool preserveBorderEdges = false;
private bool preserveUVSeamEdges = false;
private bool preserveUVFoldoverEdges = false;
private bool preserveSurfaceCurvature = false;
private bool enableSmartLink = true;
private int maxIterationCount = 100;
private double agressiveness = 7.0;
SimplificationOptions simplificationOptions = SimplificationOptions.Default;
private bool verbose = false;

private double vertexLinkDistanceSqr = double.Epsilon;

private int subMeshCount = 0;
private int[] subMeshOffsets = null;
private ResizableArray<Triangle> triangles = null;
Expand All @@ -103,6 +95,16 @@ public sealed class MeshSimplifier
#endregion

#region Properties
/// <summary>
/// Gets or sets all of the simplification options as a single block.
/// Default value: SimplificationOptions.Default
/// </summary>
public SimplificationOptions SimplificationOptions
{
get { return this.simplificationOptions; }
set { this.simplificationOptions = value; }
}

/// <summary>
/// Gets or sets if the border edges should be preserved.
/// Default value: false
Expand All @@ -120,8 +122,8 @@ public bool PreserveBorders
/// </summary>
public bool PreserveBorderEdges
{
get { return preserveBorderEdges; }
set { preserveBorderEdges = value; }
get { return simplificationOptions.PreserveBorderEdges; }
set { simplificationOptions.PreserveBorderEdges = value; }
}

/// <summary>
Expand All @@ -141,8 +143,8 @@ public bool PreserveSeams
/// </summary>
public bool PreserveUVSeamEdges
{
get { return preserveUVSeamEdges; }
set { preserveUVSeamEdges = value; }
get { return simplificationOptions.PreserveUVSeamEdges; }
set { simplificationOptions.PreserveUVSeamEdges = value; }
}

/// <summary>
Expand All @@ -162,8 +164,8 @@ public bool PreserveFoldovers
/// </summary>
public bool PreserveUVFoldoverEdges
{
get { return preserveUVFoldoverEdges; }
set { preserveUVFoldoverEdges = value; }
get { return simplificationOptions.PreserveUVFoldoverEdges; }
set { simplificationOptions.PreserveUVFoldoverEdges = value; }
}

/// <summary>
Expand All @@ -172,8 +174,8 @@ public bool PreserveUVFoldoverEdges
/// </summary>
public bool PreserveSurfaceCurvature
{
get { return preserveSurfaceCurvature; }
set { preserveSurfaceCurvature = value; }
get { return simplificationOptions.PreserveSurfaceCurvature; }
set { simplificationOptions.PreserveSurfaceCurvature = value; }
}

/// <summary>
Expand All @@ -184,8 +186,8 @@ public bool PreserveSurfaceCurvature
/// </summary>
public bool EnableSmartLink
{
get { return enableSmartLink; }
set { enableSmartLink = value; }
get { return simplificationOptions.EnableSmartLink; }
set { simplificationOptions.EnableSmartLink = value; }
}

/// <summary>
Expand All @@ -195,8 +197,8 @@ public bool EnableSmartLink
/// </summary>
public int MaxIterationCount
{
get { return maxIterationCount; }
set { maxIterationCount = value; }
get { return simplificationOptions.MaxIterationCount; }
set { simplificationOptions.MaxIterationCount = value; }
}

/// <summary>
Expand All @@ -205,8 +207,8 @@ public int MaxIterationCount
/// </summary>
public double Agressiveness
{
get { return agressiveness; }
set { agressiveness = value; }
get { return simplificationOptions.Agressiveness; }
set { simplificationOptions.Agressiveness = value; }
}

/// <summary>
Expand All @@ -225,8 +227,8 @@ public bool Verbose
/// </summary>
public double VertexLinkDistance
{
get { return Math.Sqrt(vertexLinkDistanceSqr); }
set { vertexLinkDistanceSqr = (value > double.Epsilon ? value * value : double.Epsilon); }
get { return simplificationOptions.VertexLinkDistance; }
set { simplificationOptions.VertexLinkDistance = value > double.Epsilon ? value : double.Epsilon; }
}

/// <summary>
Expand All @@ -236,8 +238,8 @@ public double VertexLinkDistance
/// </summary>
public double VertexLinkDistanceSqr
{
get { return vertexLinkDistanceSqr; }
set { vertexLinkDistanceSqr = value; }
get { return simplificationOptions.VertexLinkDistance * simplificationOptions.VertexLinkDistance; }
set { simplificationOptions.VertexLinkDistance = Math.Sqrt(value); }
}

/// <summary>
Expand Down Expand Up @@ -496,7 +498,7 @@ private double CurvatureError(ref Vertex vert0, ref Vertex vert1)
{
Vector3d normVecTriangleWithViAndVjBoth = triangleWithViAndVjBoth.n;
double dot = Vector3d.Dot(ref normVecTriangleWithViOrVjOrBoth, ref normVecTriangleWithViAndVjBoth);

if (dot > maxDotInner)
maxDotInner = dot;
}
Expand Down Expand Up @@ -524,7 +526,7 @@ private double CalculateError(ref Vertex vert0, ref Vertex vert1, out Vector3d r
-1.0 / det * q.Determinant4()); // vz = A43/det(q_delta)

double curvatureError = 0;
if (preserveSurfaceCurvature)
if (simplificationOptions.PreserveSurfaceCurvature)
{
curvatureError = CurvatureError(ref vert0, ref vert1);
}
Expand Down Expand Up @@ -796,6 +798,9 @@ private void RemoveVertexPass(int startTrisCount, int targetTrisCount, double th

Vector3d p;
Vector3 barycentricCoord;
var preserveBorderEdges = PreserveBorderEdges;
var preserveUVSeamEdges = PreserveUVSeamEdges;
var preserveUVFoldoverEdges = PreserveUVFoldoverEdges;
for (int tid = 0; tid < triangleCount; tid++)
{
if (triangles[tid].dirty || triangles[tid].deleted || triangles[tid].err3 > threshold)
Expand Down Expand Up @@ -947,6 +952,8 @@ private void UpdateMesh(int iteration)
int borderVertexCount = 0;
double borderMinX = double.MaxValue;
double borderMaxX = double.MinValue;
var enableSmartLink = EnableSmartLink;
var vertexLinkDistanceSqr = VertexLinkDistanceSqr;
for (int i = 0; i < vertexCount; i++)
{
int tstart = vertices[i].tstart;
Expand Down Expand Up @@ -2199,6 +2206,8 @@ public void SimplifyMesh(float quality)
var vertices = this.vertices.Data;
int targetTrisCount = Mathf.RoundToInt(triangleCount * quality);

var maxIterationCount = MaxIterationCount;
var agressiveness = Agressiveness;
for (int iteration = 0; iteration < maxIterationCount; iteration++)
{
if ((startTrisCount - deletedTris) <= targetTrisCount)
Expand Down

0 comments on commit 87d3fa8

Please sign in to comment.