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

Remove redundant parallelism #45

Merged
merged 2 commits into from
Feb 7, 2024
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
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: 2 additions & 2 deletions core/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public static void WriteDebugMesh(IMesh mesh, string sPath)
StandardMeshWriter.WriteFile(sPath, new List<WriteMesh>() { new WriteMesh(mesh) }, options);
}

public static void WriteDebugMeshAndMarkers(IMesh mesh, List<Vector3d> Markers, string sPath, int maxDegreeOfParallelism)
public static void WriteDebugMeshAndMarkers(IMesh mesh, List<Vector3d> Markers, string sPath)
{
WriteOptions options = WriteOptions.Defaults;
options.bWriteGroups = true;
Expand All @@ -254,7 +254,7 @@ public static void WriteDebugMeshAndMarkers(IMesh mesh, List<Vector3d> Markers,
foreach ( Vector3d v in Markers ) {
TrivialBox3Generator boxgen = new TrivialBox3Generator();
boxgen.Box = new Box3d(v, size * Vector3d.One);
boxgen.Generate(maxDegreeOfParallelism);
boxgen.Generate();
DMesh3 m = new DMesh3();
boxgen.MakeMesh(m);
meshes.Add(new WriteMesh(m));
Expand Down
4 changes: 4 additions & 0 deletions geometry3Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@
<EmbeddedResource Remove="test\**" />
<None Remove="test\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>

</Project>
22 changes: 13 additions & 9 deletions math/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,26 @@ 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;
}
public static Vector3d Normal(Vector3d v1, Vector3d v2, Vector3d v3) {
return Normal(ref v1, ref v2, ref v3);

public static Vector3d Normal(Vector3d v1, Vector3d v2, Vector3d v3)
{
Vector3d edge1 = v2 - v1;
Vector3d edge2 = v3 - v2;
Vector3d vCross = edge1.Cross(edge2);
vCross.Normalize();
return vCross;
}


/// <summary>
/// compute vector in direction of triangle normal (cross-product). No normalization.
/// </summary>
/// <returns>The normal direction.</returns>
public static Vector3d FastNormalDirection(ref Vector3d v1, ref Vector3d v2, ref Vector3d v3)
/// <summary>
/// compute vector in direction of triangle normal (cross-product). No normalization.
/// </summary>
/// <returns>The normal direction.</returns>
public static Vector3d FastNormalDirection(ref Vector3d v1, ref Vector3d v2, ref Vector3d v3)
{
Vector3d edge1 = v2 - v1;
Vector3d edge2 = v3 - v1;
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 @@ -46,7 +47,7 @@
}


public MeshDecomposition(DMesh3 mesh, IMeshComponentManager manager)

Check warning on line 50 in mesh/MeshDecomposition.cs

View workflow job for this annotation

GitHub Actions / Test geometry3sharp

Non-nullable field 'mapTo' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 50 in mesh/MeshDecomposition.cs

View workflow job for this annotation

GitHub Actions / Test geometry3sharp

Non-nullable field 'mapToMulti' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
MaxComponentSize = 62000; // max for unity is 64

Expand All @@ -56,7 +57,7 @@



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

Expand Down Expand Up @@ -107,7 +108,7 @@



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 @@



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 @@
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
48 changes: 24 additions & 24 deletions mesh/MeshEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,84 +866,84 @@ public bool AppendMesh(IMesh appendMesh, IndexMap mergeMapV, out int[] mapV, int



public void AppendBox(Frame3f frame, float size, int maxDegreeOfParallelism)
public void AppendBox(Frame3f frame, float size)
{
AppendBox(frame, size * Vector3f.One, maxDegreeOfParallelism);
AppendBox(frame, size * Vector3f.One);
}
public void AppendBox(Frame3f frame, Vector3f size, int maxDegreeOfParallelism)
public void AppendBox(Frame3f frame, Vector3f size)
{
AppendBox(frame, size, Colorf.White, maxDegreeOfParallelism);
AppendBox(frame, size, Colorf.White);
}
public void AppendBox(Frame3f frame, Vector3f size, Colorf color, int maxDegreeOfParallelism)
public void AppendBox(Frame3f frame, Vector3f size, Colorf color)
{
TrivialBox3Generator boxgen = new TrivialBox3Generator() {
Box = new Box3d(frame, size),
NoSharedVertices = false
};
boxgen.Generate(maxDegreeOfParallelism);
boxgen.Generate();
DMesh3 mesh = new DMesh3();
boxgen.MakeMesh(mesh);
if (Mesh.HasVertexColors)
mesh.EnableVertexColors(color);
AppendMesh(mesh, Mesh.AllocateTriangleGroup());
}
public void AppendLine(Segment3d seg, float size, int maxDegreeOfParallelism)
public void AppendLine(Segment3d seg, float size)
{
Frame3f f = new Frame3f(seg.Center);
f.AlignAxis(2, (Vector3f)seg.Direction);
AppendBox(f, new Vector3f(size, size, seg.Extent), maxDegreeOfParallelism);
AppendBox(f, new Vector3f(size, size, seg.Extent));
}
public void AppendLine(Segment3d seg, float size, Colorf color, int maxDegreeOfParallelism)
public void AppendLine(Segment3d seg, float size, Colorf color)
{
Frame3f f = new Frame3f(seg.Center);
f.AlignAxis(2, (Vector3f)seg.Direction);
AppendBox(f, new Vector3f(size, size, seg.Extent), color, maxDegreeOfParallelism);
AppendBox(f, new Vector3f(size, size, seg.Extent), color);
}
public static void AppendBox(DMesh3 mesh, Vector3d pos, float size, int maxDegreeOfParallelism)
public static void AppendBox(DMesh3 mesh, Vector3d pos, float size)
{
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(new Frame3f(pos), size, maxDegreeOfParallelism);
editor.AppendBox(new Frame3f(pos), size);
}
public static void AppendBox(DMesh3 mesh, Vector3d pos, float size, Colorf color, int maxDegreeOfParallelism)
public static void AppendBox(DMesh3 mesh, Vector3d pos, float size, Colorf color)
{
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(new Frame3f(pos), size*Vector3f.One, color, maxDegreeOfParallelism);
editor.AppendBox(new Frame3f(pos), size*Vector3f.One, color);
}
public static void AppendBox(DMesh3 mesh, Vector3d pos, Vector3d normal, float size, int maxDegreeOfParallelism)
public static void AppendBox(DMesh3 mesh, Vector3d pos, Vector3d normal, float size)
{
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(new Frame3f(pos, normal), size, maxDegreeOfParallelism);
editor.AppendBox(new Frame3f(pos, normal), size);
}
public static void AppendBox(DMesh3 mesh, Vector3d pos, Vector3d normal, float size, Colorf color, int maxDegreeOfParallelism)
public static void AppendBox(DMesh3 mesh, Vector3d pos, Vector3d normal, float size, Colorf color)
{
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(new Frame3f(pos, normal), size*Vector3f.One, color, maxDegreeOfParallelism);
editor.AppendBox(new Frame3f(pos, normal), size*Vector3f.One, color);
}
public static void AppendBox(DMesh3 mesh, Frame3f frame, Vector3f size, Colorf color, int maxDegreeOfParallelism)
public static void AppendBox(DMesh3 mesh, Frame3f frame, Vector3f size, Colorf color)
{
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(frame, size, color, maxDegreeOfParallelism);
editor.AppendBox(frame, size, color);
}

public static void AppendLine(DMesh3 mesh, Segment3d seg, float size, int maxDegreeOfParallelism)
public static void AppendLine(DMesh3 mesh, Segment3d seg, float size)
{
Frame3f f = new Frame3f(seg.Center);
f.AlignAxis(2, (Vector3f)seg.Direction);
MeshEditor editor = new MeshEditor(mesh);
editor.AppendBox(f, new Vector3f(size, size, seg.Extent), maxDegreeOfParallelism);
editor.AppendBox(f, new Vector3f(size, size, seg.Extent));
}




public void AppendPathSolid(IEnumerable<Vector3d> vertices, double radius, Colorf color, int maxDegreeOfParallelism)
public void AppendPathSolid(IEnumerable<Vector3d> vertices, double radius, Colorf color)
{
TubeGenerator tubegen = new TubeGenerator() {
Vertices = new List<Vector3d>(vertices),
Polygon = Polygon2d.MakeCircle(radius, 6),
NoSharedVertices = false
};
DMesh3 mesh = tubegen.Generate(maxDegreeOfParallelism).MakeDMesh();
DMesh3 mesh = tubegen.Generate().MakeDMesh();
if (Mesh.HasVertexColors)
mesh.EnableVertexColors(color);
AppendMesh(mesh, Mesh.AllocateTriangleGroup());
Expand Down
Loading
Loading