Skip to content

Commit

Permalink
Add and use List::Last(). NFC.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpavlik authored and whitequark committed Sep 10, 2019
1 parent c5f3cd1 commit 231dff6
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/dsc.h
Expand Up @@ -250,6 +250,10 @@ class List {
const T *First() const {
return IsEmpty() ? nullptr : &(elem[0]);
}

T *Last() { return IsEmpty() ? nullptr : &(elem[n - 1]); }
const T *Last() const { return IsEmpty() ? nullptr : &(elem[n - 1]); }

T *NextAfter(T *prev) {
if(IsEmpty() || !prev) return NULL;
if(prev - First() == (n - 1)) return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/exportstep.cpp
Expand Up @@ -121,7 +121,7 @@ int StepFileWriter::ExportCurveLoop(SBezierLoop *loop, bool inner) {

List<int> listOfTrims = {};

SBezier *sb = &(loop->l[loop->l.n - 1]);
SBezier *sb = loop->l.Last();

// Generate "exactly closed" contours, with the same vertex id for the
// finish of a previous edge and the start of the next one. So we need
Expand Down
2 changes: 1 addition & 1 deletion src/file.cpp
Expand Up @@ -343,7 +343,7 @@ bool SolveSpaceUI::SaveToFile(const Platform::Path &filename) {
// A group will have either a mesh or a shell, but not both; but the code
// to print either of those just does nothing if the mesh/shell is empty.

Group *g = SK.GetGroup(SK.groupOrder[SK.groupOrder.n - 1]);
Group *g = SK.GetGroup(*SK.groupOrder.Last());
SMesh *m = &g->runningMesh;
for(i = 0; i < m->l.n; i++) {
STriangle *tr = &(m->l[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/graphicswin.cpp
Expand Up @@ -383,7 +383,7 @@ void GraphicsWindow::Init() {
// And with the last group active
ssassert(!SK.groupOrder.IsEmpty(),
"Group order can't be empty since we will activate the last group.");
activeGroup = SK.groupOrder[SK.groupOrder.n - 1];
activeGroup = *SK.groupOrder.Last();
SK.GetGroup(activeGroup)->Activate();

showWorkplanes = false;
Expand Down
4 changes: 1 addition & 3 deletions src/polygon.cpp
Expand Up @@ -283,9 +283,7 @@ bool SEdgeList::AssemblePolygon(SPolygon *dest, SEdge *errorAt, bool keepDir) co
// Create a new empty contour in our polygon, and finish assembling
// into that contour.
dest->AddEmptyContour();
if(!AssembleContour(first, last, &(dest->l[dest->l.n-1]),
errorAt, keepDir))
{
if(!AssembleContour(first, last, dest->l.Last(), errorAt, keepDir)) {
allClosed = false;
}
// But continue assembling, even if some of the contours are open
Expand Down
8 changes: 4 additions & 4 deletions src/srf/boolean.cpp
Expand Up @@ -293,16 +293,16 @@ void SSurface::FindChainAvoiding(SEdgeList *src, SEdgeList *dest,
{
ssassert(!src->l.IsEmpty(), "Need at least one edge");
// Start with an arbitrary edge.
dest->l.Add(&(src->l[0]));
dest->l.Add(src->l.First());
src->l.ClearTags();
src->l[0].tag = 1;
src->l.First()->tag = 1;

bool added;
do {
added = false;
// The start and finish of the current edge chain
Vector s = dest->l[0].a,
f = dest->l[dest->l.n - 1].b;
Vector s = dest->l.First()->a,
f = dest->l.Last()->b;

// We can attach a new edge at the start or finish, as long as that
// start or finish point isn't in the list of points to avoid.
Expand Down
18 changes: 9 additions & 9 deletions src/srf/curve.cpp
Expand Up @@ -470,15 +470,15 @@ void SBezierLoop::MakePwlInto(SContour *sc, double chordTol) const {
}
}
// Ensure that it's exactly closed, not just within a numerical tolerance.
if((sc->l[sc->l.n - 1].p).Equals(sc->l[0].p)) {
sc->l[sc->l.n - 1] = sc->l[0];
if((sc->l.Last()->p).Equals(sc->l.First()->p)) {
*sc->l.Last() = *sc->l.First();
}
}

bool SBezierLoop::IsClosed() const {
if(l.n < 1) return false;
Vector s = l[0].Start(),
f = l[l.n-1].Finish();
Vector s = l.First()->Start(),
f = l.Last()->Finish();
return s.Equals(f);
}

Expand Down Expand Up @@ -512,7 +512,7 @@ SBezierLoopSet SBezierLoopSet::From(SBezierList *sbl, SPolygon *poly,
} else {
ret.l.Add(&loop);
poly->AddEmptyContour();
loop.MakePwlInto(&(poly->l[poly->l.n-1]), chordTol);
loop.MakePwlInto(poly->l.Last(), chordTol);
}
}

Expand Down Expand Up @@ -553,7 +553,7 @@ double SBezierLoopSet::SignedArea() {
void SBezierLoopSet::MakePwlInto(SPolygon *sp) const {
for(const SBezierLoop *sbl = l.First(); sbl; sbl = l.NextAfter(sbl)) {
sp->AddEmptyContour();
sbl->MakePwlInto(&(sp->l[sp->l.n - 1]));
sbl->MakePwlInto(sp->l.Last());
}
}

Expand Down Expand Up @@ -618,7 +618,7 @@ void SBezierLoopSetSet::FindOuterFacesFrom(SBezierList *sbl, SPolygon *spxyz,
for(pt = sc->l.First(); pt; pt = sc->l.NextAfter(pt)) {
double u, v;
srfuv->ClosestPointTo(pt->p, &u, &v);
spuv.l[spuv.l.n - 1].AddPoint(Vector::From(u, v, 0));
spuv.l.Last()->AddPoint(Vector::From(u, v, 0));
}
}
spuv.normal = Vector::From(0, 0, 1); // must be, since it's in xy plane now
Expand Down Expand Up @@ -850,11 +850,11 @@ STrimBy STrimBy::EntireCurve(SShell *shell, hSCurve hsc, bool backwards) {

if(backwards) {
stb.finish = sc->pts[0].p;
stb.start = sc->pts[sc->pts.n - 1].p;
stb.start = sc->pts.Last()->p;
stb.backwards = true;
} else {
stb.start = sc->pts[0].p;
stb.finish = sc->pts[sc->pts.n - 1].p;
stb.finish = sc->pts.Last()->p;
stb.backwards = false;
}

Expand Down

0 comments on commit 231dff6

Please sign in to comment.