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

69 lines
1.2 KiB
C
Raw Normal View History

#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;
}