2d hull
This commit is contained in:
parent
441ab15ac0
commit
938681a52e
6 changed files with 418 additions and 70 deletions
|
|
@ -990,7 +990,7 @@ static void b3ReduceManifoldPoints( b3LocalManifold* manifold, int capacity, b3L
|
|||
}
|
||||
|
||||
b3Point2D hull[2 * B3_MAX_CLIP_POINTS];
|
||||
int hullCount = b3Hull2D( pts, count, hull );
|
||||
int hullCount = b3Hull2D( pts, count, hull, 0 );
|
||||
int finalCount = b3SimplifyHull2D( hull, hullCount, target );
|
||||
B3_ASSERT( 0 < finalCount && finalCount <= target );
|
||||
|
||||
|
|
|
|||
186
src/hull.c
186
src/hull.c
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "algorithm.h"
|
||||
#include "math_internal.h"
|
||||
#include "qsort.h"
|
||||
#include "shape.h"
|
||||
#include "simd.h"
|
||||
|
||||
|
|
@ -3098,20 +3099,20 @@ int b3SimplifyHull2D( b3Point2D* hull, int count1, int target )
|
|||
return count2;
|
||||
}
|
||||
|
||||
// Sort comparator: by x, then by y. Insertion since n is typically 20 or less.
|
||||
static void b3Sort2D( b3Point2D* pts, int count )
|
||||
// todo comment
|
||||
static inline bool b3IsPoint2DLess( const b3Point2D* a, const b3Point2D* b )
|
||||
{
|
||||
for ( int i = 1; i < count; ++i )
|
||||
if ( a->p.x != b->p.x )
|
||||
{
|
||||
b3Point2D base = pts[i];
|
||||
int j = i - 1;
|
||||
while ( j >= 0 && ( pts[j].p.x > base.p.x || ( pts[j].p.x == base.p.x && pts[j].p.y > base.p.y ) ) )
|
||||
{
|
||||
pts[j + 1] = pts[j];
|
||||
j -= 1;
|
||||
}
|
||||
pts[j + 1] = base;
|
||||
return a->p.x < b->p.x;
|
||||
}
|
||||
|
||||
if ( a->p.y != b->p.y )
|
||||
{
|
||||
return a->p.y < b->p.y;
|
||||
}
|
||||
|
||||
return a->originalIndex < b->originalIndex;
|
||||
}
|
||||
|
||||
// Welds points that have been sorted O(n) time
|
||||
|
|
@ -3148,79 +3149,144 @@ static int b3Weld2D( b3Point2D* pts, int count1 )
|
|||
return count2;
|
||||
}
|
||||
|
||||
// todo comment
|
||||
static int b3MergeExact2D( b3Point2D* pts, int count1 )
|
||||
{
|
||||
float separationTol = B3_LINEAR_SLOP;
|
||||
|
||||
int count2 = 0;
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
b3Point2D point = pts[i];
|
||||
if ( count2 > 0 && pts[count2 - 1].p.x == point.p.x && pts[count2 - 1].p.y == point.p.y )
|
||||
{
|
||||
if ( point.separation < pts[count2 - 1].separation - separationTol )
|
||||
{
|
||||
pts[count2 - 1] = point;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pts[count2] = point;
|
||||
count2 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
||||
// Andrew's monotone chain convex hull algorithm
|
||||
// hull must have space for 2 * count1 points
|
||||
int b3Hull2D( b3Point2D* pts, int count, b3Point2D* hull )
|
||||
int b3Hull2D( b3Point2D* pts, int count, b3Point2D* hull, int flags )
|
||||
{
|
||||
int count1 = count;
|
||||
|
||||
if ( count1 <= 0 )
|
||||
if ( count <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( count1 == 1 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
}
|
||||
#define LESS( i, j ) b3IsPoint2DLess( pts + (int)( i ), pts + (int)( j ) )
|
||||
#define SWAP( i, j ) \
|
||||
do \
|
||||
{ \
|
||||
b3Point2D swapPoint = pts[(int)( i )]; \
|
||||
pts[(int)( i )] = pts[(int)( j )]; \
|
||||
pts[(int)( j )] = swapPoint; \
|
||||
} \
|
||||
while ( 0 )
|
||||
|
||||
b3Sort2D( pts, count1 );
|
||||
count1 = b3Weld2D( pts, count1 );
|
||||
QSORT( count, LESS, SWAP );
|
||||
|
||||
if ( count1 == 1 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
}
|
||||
#undef LESS
|
||||
#undef SWAP
|
||||
|
||||
if ( count1 == 2 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
hull[1] = pts[1];
|
||||
return 2;
|
||||
}
|
||||
int count1 = ( flags & b3_hull2DExactMerge ) ? b3MergeExact2D( pts, count ) : b3Weld2D( pts, count );
|
||||
bool keepCollinear = ( flags & b3_hull2DKeepCollinear ) != 0;
|
||||
|
||||
int count2 = 0;
|
||||
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
if ( count1 <= 2 )
|
||||
{
|
||||
while ( count2 >= 2 )
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
hull[i] = pts[i];
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
count2 = count1;
|
||||
}
|
||||
|
||||
int lowerCount = count2 + 1;
|
||||
|
||||
for ( int i = count1 - 2; i >= 0; i-- )
|
||||
else
|
||||
{
|
||||
while ( count2 >= lowerCount )
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
while ( count2 >= 2 )
|
||||
{
|
||||
break;
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f || ( keepCollinear && area == 0.0f ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
int lowerCount = count2 + 1;
|
||||
|
||||
// todo comment
|
||||
int lowerIndex = count2 - 2;
|
||||
|
||||
for ( int i = count1 - 2; i >= 0; i-- )
|
||||
{
|
||||
if ( lowerIndex >= 0 && hull[lowerIndex].p.x == pts[i].p.x && hull[lowerIndex].p.y == pts[i].p.y )
|
||||
{
|
||||
lowerIndex -= 1;
|
||||
if ( i > 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
while ( count2 >= lowerCount )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f || ( keepCollinear && area == 0.0f ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( hull[0].originalIndex == hull[count2 - 1].originalIndex );
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( hull[0].originalIndex == hull[count2 - 1].originalIndex );
|
||||
return count2 - 1;
|
||||
B3_VALIDATE( count2 <= count1 );
|
||||
|
||||
if ( flags & b3_hull2DIndexOrder )
|
||||
{
|
||||
#define LESS( i, j ) ( hull[(int)( i )].originalIndex < hull[(int)( j )].originalIndex )
|
||||
#define SWAP( i, j ) \
|
||||
do \
|
||||
{ \
|
||||
b3Point2D swapPoint = hull[(int)( i )]; \
|
||||
hull[(int)( i )] = hull[(int)( j )]; \
|
||||
hull[(int)( j )] = swapPoint; \
|
||||
} \
|
||||
while ( 0 )
|
||||
|
||||
QSORT( count2, LESS, SWAP );
|
||||
|
||||
#undef LESS
|
||||
#undef SWAP
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -349,6 +349,87 @@ static int b3CullPoints( b3Point2D* points, int count )
|
|||
return 4;
|
||||
}
|
||||
|
||||
// todo comment
|
||||
#define B3_CULL_HULL_THRESHOLD 64
|
||||
|
||||
// todo comment
|
||||
static int b3DropInteriorPoints( b3Point2D* points, int count, float margin )
|
||||
{
|
||||
int extremes[8] = { 0 };
|
||||
float bests[8] = {
|
||||
points[0].p.x,
|
||||
points[0].p.x + points[0].p.y,
|
||||
points[0].p.y,
|
||||
points[0].p.y - points[0].p.x,
|
||||
-points[0].p.x,
|
||||
-points[0].p.x - points[0].p.y,
|
||||
-points[0].p.y,
|
||||
points[0].p.x - points[0].p.y,
|
||||
};
|
||||
|
||||
for ( int i = 1; i < count; ++i )
|
||||
{
|
||||
b3Vec2 p = points[i].p;
|
||||
float values[8] = { p.x, p.x + p.y, p.y, p.y - p.x, -p.x, -p.x - p.y, -p.y, p.x - p.y };
|
||||
for ( int k = 0; k < 8; ++k )
|
||||
{
|
||||
if ( values[k] > bests[k] )
|
||||
{
|
||||
bests[k] = values[k];
|
||||
extremes[k] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// todo comment
|
||||
b3Vec2 normals[8];
|
||||
float offsets[8];
|
||||
int edgeCount = 0;
|
||||
for ( int k = 0; k < 8; ++k )
|
||||
{
|
||||
b3Vec2 a = points[extremes[k]].p;
|
||||
b3Vec2 b = points[extremes[( k + 1 ) & 7]].p;
|
||||
b3Vec2 normal = { a.y - b.y, b.x - a.x };
|
||||
float length = b3Length2( normal );
|
||||
if ( length == 0.0f )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
normals[edgeCount] = normal;
|
||||
offsets[edgeCount] = b3Dot2( normal, a ) + margin * length;
|
||||
edgeCount += 1;
|
||||
}
|
||||
|
||||
if ( edgeCount < 3 )
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
int keptCount = 0;
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
b3Vec2 p = points[i].p;
|
||||
bool interior = true;
|
||||
for ( int k = 0; k < edgeCount; ++k )
|
||||
{
|
||||
if ( b3Dot2( normals[k], p ) <= offsets[k] )
|
||||
{
|
||||
interior = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( interior == false )
|
||||
{
|
||||
points[keptCount] = points[i];
|
||||
keptCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return keptCount;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 normal, b3Arena arena )
|
||||
|
|
@ -374,10 +455,38 @@ static int b3ReduceCluster( b3LocalManifoldPoint* points, int count1, b3Vec3 nor
|
|||
|
||||
#if B3_MAX_MANIFOLD_POINTS == 4
|
||||
b3Point2D* hull = pts;
|
||||
int count2 = b3CullPoints( pts, count1 );
|
||||
int count2;
|
||||
if ( count1 <= B3_CULL_HULL_THRESHOLD )
|
||||
{
|
||||
count2 = b3CullPoints( pts, count1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
// todo comment
|
||||
b3Point2D deepest = pts[0];
|
||||
for ( int i = 1; i < count1; ++i )
|
||||
{
|
||||
if ( pts[i].separation < deepest.separation )
|
||||
{
|
||||
deepest = pts[i];
|
||||
}
|
||||
}
|
||||
|
||||
int keptCount = b3DropInteriorPoints( pts, count1, 0.25f * B3_LINEAR_SLOP );
|
||||
hull = b3Bump( &arena, 2 * keptCount * sizeof( b3Point2D ) );
|
||||
int flags = b3_hull2DKeepCollinear | b3_hull2DExactMerge | b3_hull2DIndexOrder;
|
||||
int hullCount = b3Hull2D( pts, keptCount, hull, flags );
|
||||
count2 = b3CullPoints( hull, hullCount );
|
||||
|
||||
// todo comment
|
||||
if ( count2 == 1 )
|
||||
{
|
||||
hull[0] = deepest;
|
||||
}
|
||||
}
|
||||
#else
|
||||
b3Point2D* hull = b3Bump( &arena, 2 * count1 * sizeof( b3Point2D ) );
|
||||
int hullCount = b3Hull2D( pts, count1, hull );
|
||||
int hullCount = b3Hull2D( pts, count1, hull, 0 );
|
||||
int count2 = b3SimplifyHull2D( hull, hullCount, B3_MAX_MANIFOLD_POINTS );
|
||||
#endif
|
||||
B3_ASSERT( count2 <= B3_MAX_MANIFOLD_POINTS );
|
||||
|
|
|
|||
Loading…
Reference in a new issue