Skip to content

Commit

Permalink
CR fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amakhno committed Feb 7, 2024
1 parent 36e9dea commit 5c5368f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 46 deletions.
11 changes: 6 additions & 5 deletions core/DVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public DVector(DVector<T> copy)
}
}

public DVector(T[] data)
public DVector(T[] data, int? count = null)
{
Initialize(data);
Initialize(data, count);
}

public DVector(IEnumerable<T> init)
Expand Down Expand Up @@ -279,9 +279,10 @@ public byte[] GetBytes()



public void Initialize(T[] data)
public void Initialize(T[] data, int? count = null)
{
int blocks = data.Length / nBlockSize;
int length = count ?? data.Length;
int blocks = length / nBlockSize;
Blocks = new List<T[]>();
int ai = 0;
for (int i = 0; i < blocks; ++i) {
Expand All @@ -290,7 +291,7 @@ public void Initialize(T[] data)
Blocks.Add(block);
ai += nBlockSize;
}
iCurBlockUsed = data.Length - ai;
iCurBlockUsed = length - ai;
if (iCurBlockUsed != 0) {
T[] last = new T[nBlockSize];
Array.Copy(data, ai, last, 0, iCurBlockUsed);
Expand Down
4 changes: 0 additions & 4 deletions math/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,6 @@ public static double Area(Vector3d v1, Vector3d v2, Vector3d v3) {
public static Vector3d Normal(ref Vector3d v1, ref Vector3d v2, ref Vector3d v3) {
Vector3d edge1 = v2 - v1;
Vector3d edge2 = v3 - v2;
edge1.Normalize();
edge2.Normalize();
Vector3d vCross = edge1.Cross(edge2);
vCross.Normalize();
return vCross;
Expand All @@ -459,8 +457,6 @@ public static Vector3d Normal(Vector3d v1, Vector3d v2, Vector3d v3)
{
Vector3d edge1 = v2 - v1;
Vector3d edge2 = v3 - v2;
edge1.Normalize();
edge2.Normalize();
Vector3d vCross = edge1.Cross(edge2);
vCross.Normalize();
return vCross;
Expand Down
51 changes: 31 additions & 20 deletions mesh/MeshDecomposition.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
#nullable enable

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;


namespace g3
Expand Down Expand Up @@ -56,7 +57,7 @@ public MeshDecomposition(DMesh3 mesh, IMeshComponentManager manager)



public void BuildLinear(int maxDegreeOfParallelism)
public void BuildLinear()
{
int NV = mesh.MaxVertexID;

Expand Down Expand Up @@ -107,7 +108,7 @@ public void BuildLinear(int maxDegreeOfParallelism)



int[] tri_order = get_tri_order_by_axis_sort(maxDegreeOfParallelism);
int[] tri_order = get_tri_order_by_axis_sort();
int tri_count = tri_order.Length;

for (int ii = 0; ii < tri_count; ++ii) {
Expand All @@ -131,7 +132,7 @@ public void BuildLinear(int maxDegreeOfParallelism)



int[] get_tri_order_by_axis_sort(int maxDegreeOfParallelism)
int[] get_tri_order_by_axis_sort(ArrayPool<Vector3d>? arrayPool = null)
{
int i = 0;
int[] tri_order = new int[mesh.TriangleCount];
Expand All @@ -142,21 +143,31 @@ int[] get_tri_order_by_axis_sort(int maxDegreeOfParallelism)
tri_order[i++] = ti;
}

// precompute triangle centroids - wildly expensive to
// do it inline in sort (!) I guess O(N) vs O(N log N)
Vector3d[] centroids = new Vector3d[mesh.MaxTriangleID];
gParallel.ForEach(mesh.TriangleIndices(), (ti) => {
if (mesh.IsTriangle(ti))
centroids[ti] = mesh.GetTriCentroid(ti);
}, maxDegreeOfParallelism);


Array.Sort(tri_order, (t0, t1) => {
double f0 = centroids[t0].x;
double f1 = centroids[t1].x;
return (f0 == f1) ? 0 : (f0 < f1) ? -1 : 1;
});
arrayPool ??= ArrayPool<Vector3d>.Shared;
Vector3d[] rentedArray = arrayPool.Rent(mesh.MaxTriangleID);
try
{
// precompute triangle centroids - wildly expensive to
// do it inline in sort (!) I guess O(N) vs O(N log N)
Vector3d[] centroids = new Vector3d[mesh.MaxTriangleID];
for (int tid = 0; tid < mesh.MaxTriangleID; tid++)
{
if (!mesh.IsTriangle(tid))
continue;
centroids[tid] = mesh.GetTriCentroid(tid);
}

Array.Sort(tri_order, (t0, t1) =>
{
double f0 = centroids[t0].x;
double f1 = centroids[t1].x;
return (f0 == f1) ? 0 : (f0 < f1) ? -1 : 1;
});
}
finally
{
arrayPool.Return(rentedArray);
}
return tri_order;
}

Expand Down
65 changes: 48 additions & 17 deletions mesh/MeshNormals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Buffers;
using System.Runtime.InteropServices;

namespace g3
{
Expand Down Expand Up @@ -36,13 +37,19 @@ public void Compute(ArrayPool<Vector3f>? arrayPool = null)
{
arrayPool ??= ArrayPool<Vector3f>.Shared;
Vector3f[] normalsArray = arrayPool.Rent(Mesh.MaxVertexID);
QuickComputeToArray(Mesh, normalsArray);
int NV = Mesh.MaxVertexID;
if (NV != Normals.size)
Normals.resize(NV);
for (int vid = 0; vid < NV; ++vid)
Normals[vid] = (Vector3d)normalsArray[vid];
arrayPool.Return(normalsArray);
try
{
QuickComputeToArray(Mesh, normalsArray);
int NV = Mesh.MaxVertexID;
if (NV != Normals.size)
Normals.resize(NV);
for (int vid = 0; vid < NV; ++vid)
Normals[vid] = (Vector3d)normalsArray[vid];
}
finally
{
arrayPool.Return(normalsArray);
}
}

public Vector3d this[int vid] {
Expand All @@ -64,20 +71,44 @@ public void CopyTo(DMesh3 SetMesh)
}
}

public static void QuickCompute(DMesh3 mesh, ArrayPool<Vector3f>? arrayPool = null)
public static void QuickCompute(DMesh3 mesh,
ArrayPool<Vector3f>? arrayPool = null)
{
arrayPool ??= ArrayPool<Vector3f>.Shared;
Vector3f[] normalsArray = arrayPool.Rent(mesh.MaxVertexID);
QuickComputeToArray(mesh, normalsArray);
// Write to mesh
mesh.EnableVertexNormals(Vector3f.Zero);
for (int vid = 0; vid < mesh.MaxVertexID; vid++)
try
{
if (!mesh.IsVertex(vid))
continue;
mesh.SetVertexNormal(vid, normalsArray[vid]);
QuickComputeToArray(mesh, normalsArray);
CopyToMesh(mesh, normalsArray);
}
finally
{
arrayPool.Return(normalsArray);
}
}

private unsafe static void CopyToMesh(DMesh3 mesh,
Vector3f[] probablyLargerNormalsArray,
ArrayPool<float>? arrayPool = null)
{
// Create a temp array
arrayPool ??= ArrayPool<float>.Shared;
float[] floatNormalsArray = arrayPool.Rent(mesh.MaxVertexID * 3);
try
{
fixed (Vector3f* vectorPointer = &probablyLargerNormalsArray[0])
Marshal.Copy((IntPtr)vectorPointer,
destination: floatNormalsArray,
startIndex: 0,
length: mesh.MaxVertexID * 3);
// We don't need to do mesh.EnableVertexNormals(),
// because it only initializes the buffer
mesh.NormalsBuffer = new DVector<float>(floatNormalsArray, count: mesh.MaxVertexID * 3);
}
finally
{
arrayPool.Return(floatNormalsArray);
}
arrayPool.Return(normalsArray);
}

private static void QuickComputeToArray(DMesh3 mesh, Vector3f[] normalsArray)
Expand All @@ -103,7 +134,7 @@ private static void QuickComputeToArray(DMesh3 mesh, Vector3f[] normalsArray)
for (int vid = 0; vid < mesh.MaxVertexID; vid++)
{
Vector3f vertexNormal = normalsArray[vid];
if (vertexNormal.LengthSquared > MathUtil.ZeroTolerancef)
if (vertexNormal != Vector3f.Zero)
normalsArray[vid] = vertexNormal.Normalized;
}
}
Expand Down

0 comments on commit 5c5368f

Please sign in to comment.