1
0
mirror of git://projects.qi-hardware.com/ben-scans.git synced 2024-11-22 09:16:15 +02:00
ben-scans/solidify/face.c
Werner Almesberger ecf993e0f2 solidify: set the initial height to the median; uncompress .bz2 on the fly.
- solidify/Makefile, solidify/histo.h, solidify/histo.c: generate a Z
  histogram and find the median height
- solidify/face.c (read_face): set z_ref to the median, not the average
- solidify/face.c (read_face): uncompress .bz2 files on the fly
- solidify/face.c (read_face): don't close standard input (for style)
2010-09-22 17:40:03 -03:00

69 lines
1.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "util.h"
#include "array.h"
#include "histo.h"
#include "face.h"
struct face *read_face(const char *name)
{
FILE *file;
struct face *f;
struct histo *h;
float x, y, z;
int xi, yi, zi;
if (!strcmp(name, "-")) {
file = stdin;
} else {
int len;
len = strlen(name);
if (len > 4 && !strcmp(name+len-4, ".bz2")) {
char tmp[1000]; /* @@@ enough */
sprintf(tmp, "bzcat \"%s\"", name);
file = popen(tmp, "r");
if (!file) {
perror(tmp);
exit(1);
}
} else {
file = fopen(name, "r");
if (!file) {
perror(name);
exit(1);
}
}
}
f = alloc_type(struct face);
f->a = new_array();
while (fscanf(file, "%f,%f,%f\r\n", &x, &y, &z) == 3) {
/* @@@ hack - should auto-scale */
xi = round(x*10.0);
yi = round(y*10.0);
zi = round(z*40.0);
set(f->a, xi, yi, zi);
}
if (file != stdin)
(void) fclose(file);
f->sx = f->a->max_x-f->a->min_x+1;
f->sy = f->a->max_y-f->a->min_y+1;
h = make_histo(f->a);
f->z_ref = f->a->min_z+median(h);
free_histo(h);
fprintf(stderr, "%d-%d / %d-%d / %d-%d\n",
f->a->min_x, f->a->max_x, f->a->min_y, f->a->max_y,
f->a->min_z, f->a->max_z);
return f;
}