Skip to content

Commit 22b44c1

Browse files
Merge pull request #405 from jvanvugt/joris/lfps-avoid-memcpy
Use local variables for line fit stats accumulation
2 parents 2d0466d + ad4a17f commit 22b44c1

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

apriltag_quad_thresh.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -620,42 +620,45 @@ int quad_segment_agg(zarray_t *cluster, struct line_fit_pt *lfps, int indices[4]
620620
*/
621621
struct line_fit_pt* compute_lfps(int sz, zarray_t* cluster, image_u8_t* im) {
622622
struct line_fit_pt *lfps = calloc(sz, sizeof(struct line_fit_pt));
623+
double sum_Mx = 0, sum_My = 0, sum_Mxx = 0, sum_Myy = 0, sum_Mxy = 0, sum_W = 0;
623624

624625
for (int i = 0; i < sz; i++) {
625626
struct pt *p;
626627
zarray_get_volatile(cluster, i, &p);
627628

628-
if (i > 0) {
629-
memcpy(&lfps[i], &lfps[i-1], sizeof(struct line_fit_pt));
630-
}
631-
632-
{
633-
// we now undo our fixed-point arithmetic.
634-
double delta = 0.5; // adjust for pixel center bias
635-
double x = p->x * .5 + delta;
636-
double y = p->y * .5 + delta;
637-
int ix = x, iy = y;
638-
double W = 1;
629+
// we now undo our fixed-point arithmetic.
630+
double delta = 0.5; // adjust for pixel center bias
631+
double x = p->x * .5 + delta;
632+
double y = p->y * .5 + delta;
633+
int ix = x, iy = y;
634+
double W = 1;
639635

640-
if (ix > 0 && ix+1 < im->width && iy > 0 && iy+1 < im->height) {
641-
int grad_x = im->buf[iy * im->stride + ix + 1] -
642-
im->buf[iy * im->stride + ix - 1];
636+
if (ix > 0 && ix+1 < im->width && iy > 0 && iy+1 < im->height) {
637+
int grad_x = im->buf[iy * im->stride + ix + 1] -
638+
im->buf[iy * im->stride + ix - 1];
643639

644-
int grad_y = im->buf[(iy+1) * im->stride + ix] -
645-
im->buf[(iy-1) * im->stride + ix];
640+
int grad_y = im->buf[(iy+1) * im->stride + ix] -
641+
im->buf[(iy-1) * im->stride + ix];
646642

647-
// XXX Tunable. How to shape the gradient magnitude?
648-
W = sqrt(grad_x*grad_x + grad_y*grad_y) + 1;
649-
}
650-
651-
double fx = x, fy = y;
652-
lfps[i].Mx += W * fx;
653-
lfps[i].My += W * fy;
654-
lfps[i].Mxx += W * fx * fx;
655-
lfps[i].Mxy += W * fx * fy;
656-
lfps[i].Myy += W * fy * fy;
657-
lfps[i].W += W;
643+
// XXX Tunable. How to shape the gradient magnitude?
644+
W = sqrt(grad_x*grad_x + grad_y*grad_y) + 1;
658645
}
646+
647+
double fx = x, fy = y;
648+
sum_Mx += W * fx;
649+
sum_My += W * fy;
650+
sum_Mxx += W * fx * fx;
651+
sum_Mxy += W * fx * fy;
652+
sum_Myy += W * fy * fy;
653+
sum_W += W;
654+
655+
// Store cumulative sums
656+
lfps[i].Mx = sum_Mx;
657+
lfps[i].My = sum_My;
658+
lfps[i].Mxx = sum_Mxx;
659+
lfps[i].Mxy = sum_Mxy;
660+
lfps[i].Myy = sum_Myy;
661+
lfps[i].W = sum_W;
659662
}
660663
return lfps;
661664
}

0 commit comments

Comments
 (0)