1
0
mirror of git://projects.qi-hardware.com/ben-scans.git synced 2024-11-22 15:58:25 +02:00
ben-scans/solidify/histo.c

40 lines
641 B
C
Raw Normal View History

#include "util.h"
#include "array.h"
#include "histo.h"
struct histo *make_histo(struct array *a)
{
int i;
struct histo *h;
h = alloc_type(struct histo);
h->n = a->max_z-a->min_z+1;
h->b = alloc_size(h->n*sizeof(int));
for (i = 0; i != h->n; i++)
h->b[i] = 0;
for (i = 0; i != (a->max_x-a->min_x+1)*(a->max_y-a->min_y+1); i++)
if (a->data[i] != UNDEF)
h->b[a->data[i]-a->min_z]++;
return h;
}
void free_histo(struct histo *h)
{
free(h);
}
int median(const struct histo *h)
{
int tot = 0, sum = 0;
int i;
for (i = 0; i != h->n; i++)
tot += h->b[i];
for (i = 0; sum < tot/2; i++)
sum += h->b[i];
return i-1;
}