ref: 24fe53736af84d40f9f513e08876a4e05278b9a4
parent: be641a495dbf3c12757c1cceaec81a83406aa44c
author: qwx <[email protected]>
date: Wed Oct 26 04:35:21 EDT 2022
path(1): overlay reference path from loaded results + fix typos
--- a/app/path/drw.c
+++ b/app/path/drw.c
@@ -26,6 +26,7 @@
Cpath,
Cstart,
Cgoal,
+ Cref,
Cend,
};
static Image *col[Cend];
@@ -131,6 +132,32 @@
}
static void
+drawscenpath(void)
+{
+ int sz;
+ Sim *sp;
+ Vertex *vp, *ve;
+ Node *n;
+ Rectangle r;
+
+ if(sims == nil || curscen >= sims->n)
+ return;
+ sp = (Sim *)sims->p + curscen;
+ if(sp->path == nil || sp->path->n == 0)
+ return;
+ sz = MAX(nodesz - showgrid, 1);
+ for(vp=sp->path->p, ve=vp+sp->path->n; vp<ve; vp++){
+ n = grid + vp->y * gridwidth + vp->x;
+ if(n == goal)
+ return;
+ r.min = n2s(n);
+ r.max = addpt(r.min, Pt(sz, sz));
+ draw(view, r, col[Cref], nil, ZP);
+ }
+ dprint(Lognone, "path::drawscenpath: malformed or wrong path\n");
+}
+
+static void
drawnodes(void)
{
int sz;
@@ -203,6 +230,7 @@
if(showgrid && nodesz > 1)
drawgrid();
drawnodes();
+ drawscenpath();
if(nodesz > 8)
drawfrom();
}
@@ -247,6 +275,7 @@
col[Cpath] = eallocimage(Rect(0,0,1,1), 1, 0xcccc00ff);
col[Cstart] = eallocimage(Rect(0,0,1,1), 1, 0x00cc00ff);
col[Cgoal] = eallocimage(Rect(0,0,1,1), 1, 0xcc0000ff);
+ col[Cref] = eallocimage(Rect(0,0,1,1), 1, 0xcc00cc77);
zx = Dx(screen->r) / gridwidth;
zy = (Dy(screen->r) - font->height*2) / gridheight;
z = MIN(zx, zy);
--- a/app/path/fs.c
+++ b/app/path/fs.c
@@ -27,15 +27,19 @@
n = 0;
v = valloc(sim->steps, sizeof(Vertex));
- while((p = strchr(s, ',')) != nil){
- *p = 0;
+ for(;;){
+ p = strchr(s, ',');
+ if(p != nil)
+ *p = 0;
x = strtoull(s, &t, 10);
if(t == s)
sysfatal("parsepath: invalid node number");
px = V(x % gridwidth, x / gridwidth);
vinsert(v, (char*)&px);
- n++;
+ if(p == nil)
+ break;
s = p + 1;
+ n++;
}
if(n != sim->steps)
sysfatal("parsepath -- phase error");
@@ -53,6 +57,7 @@
assert(sims != nil);
if((bf = Bopen(path, OREAD)) == nil)
sysfatal("readresults: %r");
+ free(Brdstr(bf, '\n', 1)); /* header */
sp = sims->p;
se = sp + sims->n;
while(sp < se){
@@ -62,12 +67,12 @@
werrstr("invalid record length %d not %d", n, nelem(arg));
return -1;
}
- sp->cost = strtod(arg[7], nil);
- sp->steps = atoi(arg[2]);
- sp->opened = atoi(arg[3]);
- sp->expanded = atoi(arg[4]);
- sp->updated = atoi(arg[5]);
- sp->closed = atoi(arg[6]);
+ sp->steps = atoi(arg[1]);
+ sp->opened = atoi(arg[2]);
+ sp->expanded = atoi(arg[3]);
+ sp->updated = atoi(arg[4]);
+ sp->closed = atoi(arg[5]);
+ sp->cost = strtod(arg[6], nil);
if(sp->steps <= 0)
dprint(Lognone, "path::readresults: invalid entry line %zd\n",
sp - (Sim *)sims->p);
@@ -91,7 +96,7 @@
Sim *sp, *se;
Vertex *p, *e;
- print("id\tsteps\topened\texpanded\tupdated\texpanded\tcost\tdist\n");
+ print("id\tsteps\topened\texpanded\tupdated\tclosed\tcost\tdist\tpath\n");
for(i=0, sp=sims->p, se=sp+sims->n; sp<se; sp++, i++){
print("%d\t%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\t",
i, sp->steps, sp->opened, sp->expanded,
--- a/app/path/path.c
+++ b/app/path/path.c
@@ -211,8 +211,8 @@
writeresults();
threadexitsall(nil);
}
- if(res != nil)
- readresults(res);
+ if(res != nil && readresults(res) < 0)
+ dprint(Lognone, "path::readresults: %r\n");
}
initgraphics(grkey, grmouse);
if(scen != nil){