Replace qsort with std::sort and lambda. NFC.

Removes static variable usage, permits hiding of the underlying pointer
(std::sort uses iterators intead), type safety, etc.
pull/459/head
Ryan Pavlik 2019-05-23 12:04:49 -05:00 committed by whitequark
parent 1b97a006e9
commit 482f0e8de9
2 changed files with 22 additions and 43 deletions

View File

@ -491,21 +491,15 @@ int SKdNodeEdges::AnyEdgeCrossings(Vector a, Vector b, int cnt,
// We have an edge list that contains only collinear edges, maybe with more
// splits than necessary. Merge any collinear segments that join.
//-----------------------------------------------------------------------------
static Vector LineStart, LineDirection;
static int ByTAlongLine(const void *av, const void *bv)
{
SEdge *a = (SEdge *)av,
*b = (SEdge *)bv;
double ta = (a->a.Minus(LineStart)).DivPivoting(LineDirection),
tb = (b->a.Minus(LineStart)).DivPivoting(LineDirection);
return (ta > tb) ? 1 : -1;
}
void SEdgeList::MergeCollinearSegments(Vector a, Vector b) {
LineStart = a;
LineDirection = b.Minus(a);
qsort(l.elem, l.n, sizeof(l.elem[0]), ByTAlongLine);
const Vector lineStart = a;
const Vector lineDirection = b.Minus(a);
std::sort(l.begin(), l.end(), [&](const SEdge &a, const SEdge &b) {
double ta = (a.a.Minus(lineStart)).DivPivoting(lineDirection);
double tb = (b.a.Minus(lineStart)).DivPivoting(lineDirection);
return (ta < tb);
});
l.ClearTags();
SEdge *prev = nullptr;

View File

@ -24,17 +24,6 @@ void SShell::MakeFromDifferenceOf(SShell *a, SShell *b) {
// the intersection of srfA and srfB.) Return a new pwl curve with everything
// split.
//-----------------------------------------------------------------------------
static Vector LineStart, LineDirection;
static int ByTAlongLine(const void *av, const void *bv)
{
SInter *a = (SInter *)av,
*b = (SInter *)bv;
double ta = (a->p.Minus(LineStart)).DivPivoting(LineDirection),
tb = (b->p.Minus(LineStart)).DivPivoting(LineDirection);
return (ta > tb) ? 1 : -1;
}
SCurve SCurve::MakeCopySplitAgainst(SShell *agnstA, SShell *agnstB,
SSurface *srfA, SSurface *srfB) const
{
@ -104,9 +93,14 @@ SCurve SCurve::MakeCopySplitAgainst(SShell *agnstA, SShell *agnstB,
// And now sort them in order along the line. Note that we must
// do that after refining, in case the refining would make two
// points switch places.
LineStart = prev.p;
LineDirection = (p->p).Minus(prev.p);
qsort(il.elem, il.n, sizeof(il.elem[0]), ByTAlongLine);
const Vector lineStart = prev.p;
const Vector lineDirection = (p->p).Minus(prev.p);
std::sort(il.begin(), il.end(), [&](const SInter &a, const SInter &b) {
double ta = (a.p.Minus(lineStart)).DivPivoting(lineDirection);
double tb = (b.p.Minus(lineStart)).DivPivoting(lineDirection);
return (ta < tb);
});
// And now uses the intersections to generate our split pwl edge(s)
Vector prev = Vector::From(VERY_POSITIVE, 0, 0);
@ -775,19 +769,6 @@ SBspUv *SBspUv::Alloc() {
return (SBspUv *)AllocTemporary(sizeof(SBspUv));
}
static int ByLength(const void *av, const void *bv)
{
SEdge *a = (SEdge *)av,
*b = (SEdge *)bv;
double la = (a->a).Minus(a->b).Magnitude(),
lb = (b->a).Minus(b->b).Magnitude();
// Sort in descending order, longest first. This improves numerical
// stability for the normals.
return (la < lb) ? 1 : -1;
}
SBspUv *SBspUv::From(SEdgeList *el, SSurface *srf) {
SEdgeList work = {};
@ -795,8 +776,12 @@ SBspUv *SBspUv::From(SEdgeList *el, SSurface *srf) {
for(se = el->l.First(); se; se = el->l.NextAfter(se)) {
work.AddEdge(se->a, se->b, se->auxA, se->auxB);
}
qsort(work.l.elem, work.l.n, sizeof(work.l.elem[0]), ByLength);
std::sort(work.l.begin(), work.l.end(), [](SEdge const &a, SEdge const &b) {
double la = (a.a).Minus(a.b).Magnitude(), lb = (b.a).Minus(b.b).Magnitude();
// Sort in descending order, longest first. This improves numerical
// stability for the normals.
return la > lb;
});
SBspUv *bsp = NULL;
for(se = work.l.First(); se; se = work.l.NextAfter(se)) {
bsp = InsertOrCreateEdge(bsp, (se->a).ProjectXy(), (se->b).ProjectXy(), srf);