Skip to content

Commit

Permalink
better patitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Sep 27, 2024
1 parent acc5c8d commit 5f7f768
Showing 1 changed file with 69 additions and 31 deletions.
100 changes: 69 additions & 31 deletions newton-4.00/sdk/dCore/ndUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
#include "ndVector.h"
#include "ndMatrix.h"

#define D_VERTEXLIST_INDEX_LIST_BASH (1024)
#define D_USE_YZ_VARIANCE

#ifdef D_USE_YZ_VARIANCE
#define D_VERTEXLIST_INDEX_LIST_BASH (1024 * 8)
#else
#define D_VERTEXLIST_INDEX_LIST_BASH (1024)
#endif

ndFloat64 ndRoundToFloat(ndFloat64 val)
{
Expand Down Expand Up @@ -71,12 +77,13 @@ static ndInt32 SortVertices(
const ndFloat64* const vertexList, ndInt32 stride,
ndInt32 compareCount, ndFloat64 tol,
ndSortKey* const remapIndex,
const ndSortCluster& cluster, ndInt32 baseCount)
const ndSortCluster& cluster, ndInt32 baseCount, ndInt32 firstSortAxis)
{
#ifndef D_USE_YZ_VARIANCE
firstSortAxis = 0;
const ndBigVector origin(cluster.m_sum.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count));
const ndBigVector variance(ndBigVector::m_zero.GetMax(cluster.m_sum2.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count) - origin * origin).Sqrt());

ndInt32 firstSortAxis = 0;
if ((variance.m_y >= variance.m_x) && (variance.m_y >= variance.m_z))
{
firstSortAxis = 1;
Expand All @@ -85,6 +92,7 @@ static ndInt32 SortVertices(
{
firstSortAxis = 2;
}
#endif

class dVertexSortData
{
Expand Down Expand Up @@ -126,8 +134,12 @@ static ndInt32 SortVertices(
};
ndSort<ndSortKey, ndCompareKey>(remapIndex, cluster.m_count, &sortContext);

#ifdef D_USE_YZ_VARIANCE
const ndFloat64 tolerance = ndMax(ndFloat64(tol), ndFloat64(1.0e-8f));
#else
const ndFloat64 minDist = ndMin(ndMin(variance.m_x, variance.m_y), variance.m_z);
const ndFloat64 tolerance = ndMax(ndMin(minDist, ndFloat64(tol)), ndFloat64(1.0e-8f));
#endif
const ndFloat64 sweptWindow = ndFloat64(2.0f) * tolerance;

ndInt32 newCount = 0;
Expand All @@ -136,15 +148,13 @@ static ndInt32 SortVertices(
const ndInt32 ii = remapIndex[i].m_mask;
if (ii == -1)
{
//const ndInt32 i0 = remapIndex[i].m_ordinal;
const ndInt32 iii = remapIndex[i].m_vertexIndex;
const ndFloat64 swept = vertexList[iii * stride + firstSortAxis] + sweptWindow;;
for (ndInt32 j = i + 1; j < cluster.m_count; ++j)
{
const ndInt32 jj = remapIndex[j].m_mask;
if (jj == -1)
{
//const ndInt32 j0 = remapIndex[j].m_ordinal;
const ndInt32 jjj = remapIndex[j].m_vertexIndex;
ndFloat64 val = vertexList[jjj * stride + firstSortAxis];
if (val >= swept)
Expand Down Expand Up @@ -202,7 +212,7 @@ static ndInt32 QuickSortVertices(ndFloat64* const vertListOut, ndInt32 stride, n
ndSortKey* const indirectList = &indirectListBuffer[0];

ndFloat64* const vertList = &pool[0];
memcpy(&vertList[0], &vertListOut[0], cluster.m_count * stride * sizeof(ndFloat64));
ndMemCpy(&vertList[0], &vertListOut[0], cluster.m_count * stride);

for (ndInt32 i = 0; i < cluster.m_count; ++i)
{
Expand All @@ -215,38 +225,68 @@ static ndInt32 QuickSortVertices(ndFloat64* const vertListOut, ndInt32 stride, n
cluster.m_sum2 += x * x;
}

ndInt32 sortIndex = 0;
#ifdef D_USE_YZ_VARIANCE
const ndBigVector originScale(cluster.m_sum.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count));
const ndBigVector varianceScale2((cluster.m_sum2.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count) - originScale * originScale).GetMax (ndBigVector(1.0e-6f)));
ndFloat64 maxValue = varianceScale2.m_x;
for (ndInt32 i = 1; i < 3; ++i)
{
if (varianceScale2[i] > maxValue)
{
sortIndex = i;
maxValue = varianceScale2[i];
}
}
ndInt32 varianceIndex0 = (sortIndex + 1) % 3;
ndInt32 varianceIndex1 = (varianceIndex0 + 1) % 3;
const ndBigVector stdScale(ndFloat64(1.0f) / varianceScale2.m_x, ndFloat64(1.0f) / varianceScale2.m_y, ndFloat64(1.0f) / varianceScale2.m_z, ndFloat64(0.0f));
#endif

ndInt32 baseCount = 0;
if (cluster.m_count > D_VERTEXLIST_INDEX_LIST_BASH)
{
ndSortCluster spliteStack[128];
spliteStack[0] = cluster;

ndInt32 stack = 1;
while (stack)
ndFixSizeArray<ndSortCluster, 128> spliteStack;
spliteStack.PushBack(cluster);

while (spliteStack.GetCount())
{
stack--;
cluster = spliteStack[stack];
cluster = spliteStack.Pop();

const ndBigVector origin(cluster.m_sum.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count));
const ndBigVector variance2(cluster.m_sum2.Scale(ndFloat32(1.0f) / (ndFloat32)cluster.m_count) - origin * origin);
ndFloat64 maxVariance2 = ndMax(ndMax(variance2.m_x, variance2.m_y), variance2.m_z);

ndSortKey* const remapIndex = &indirectList[cluster.m_start];
if ((cluster.m_count <= D_VERTEXLIST_INDEX_LIST_BASH) || (stack > (ndInt32 (sizeof(spliteStack) / sizeof(spliteStack[0])) - 4)) || (maxVariance2 < ndFloat32(4.0f)))

#ifdef D_USE_YZ_VARIANCE
ndFloat64 clusterVariance2_0 = stdScale[varianceIndex0] * ndMax(variance2[varianceIndex0], ndFloat64(0.0f));
ndFloat64 clusterVariance2_1 = stdScale[varianceIndex1] * ndMax(variance2[varianceIndex1], ndFloat64(0.0f));
ndFloat64 maxNormalizedVariance = ndMax (clusterVariance2_0, clusterVariance2_1);
bool doSort = (maxNormalizedVariance < ndFloat32(1.0f/8.0f)) || (spliteStack.GetCount() > (spliteStack.GetCapacity() - 4));
#else
ndFloat64 maxVariance2 = ndMax(ndMax(variance2.m_x, variance2.m_y), variance2.m_z);
bool doSort = (cluster.m_count <= D_VERTEXLIST_INDEX_LIST_BASH) || (spliteStack.GetCount() > (spliteStack.GetCapacity() - 4)) || (maxVariance2 < ndFloat32(4.0f));
#endif
if (doSort)
{
baseCount += SortVertices(vertListOut, indexListOut, vertList, stride, compareCount, tolerance, remapIndex, cluster, baseCount);
ndInt32 newCount = SortVertices(vertListOut, indexListOut, vertList, stride, compareCount, tolerance, remapIndex, cluster, baseCount, sortIndex);
baseCount += newCount;
}
else
{
ndInt32 firstSortAxis = 0;
if ((variance2.m_y >= variance2.m_x) && (variance2.m_y >= variance2.m_z))
{
firstSortAxis = 1;
}
else if ((variance2.m_z >= variance2.m_x) && (variance2.m_z >= variance2.m_y))
{
firstSortAxis = 2;
}
#ifdef D_USE_YZ_VARIANCE
//ndInt32 firstSortAxis = (variance2.m_y >= variance2.m_z) ? 1 : 2;
ndInt32 firstSortAxis = (clusterVariance2_0 > clusterVariance2_1) ? varianceIndex0 : varianceIndex1;
#else
ndInt32 firstSortAxis = 0;
if ((variance2.m_y >= variance2.m_x) && (variance2.m_y >= variance2.m_z))
{
firstSortAxis = 1;
}
else if ((variance2.m_z >= variance2.m_x) && (variance2.m_z >= variance2.m_y))
{
firstSortAxis = 2;
}
#endif
ndFloat64 axisVal = origin[firstSortAxis];

ndInt32 i0 = 0;
Expand Down Expand Up @@ -312,22 +352,20 @@ static ndInt32 QuickSortVertices(ndFloat64* const vertListOut, ndInt32 stride, n
cluster_i1.m_count = cluster.m_count - i0;
cluster_i1.m_sum -= xc;
cluster_i1.m_sum2 -= x2c;
spliteStack[stack] = cluster_i1;
stack++;
spliteStack.PushBack(cluster_i1);

ndSortCluster cluster_i0(cluster);
cluster_i0.m_start = cluster.m_start;
cluster_i0.m_count = i0;
cluster_i0.m_sum = xc;
cluster_i0.m_sum2 = x2c;
spliteStack[stack] = cluster_i0;
stack++;
spliteStack.PushBack(cluster_i0);
}
}
}
else
{
baseCount = SortVertices(vertListOut, indexListOut, vertList, stride, compareCount, tolerance, indirectList, cluster, 0);
baseCount = SortVertices(vertListOut, indexListOut, vertList, stride, compareCount, tolerance, indirectList, cluster, 0, sortIndex);
}
return baseCount;
}
Expand Down

0 comments on commit 5f7f768

Please sign in to comment.