1
0
mirror of git://projects.qi-hardware.com/ben-scans.git synced 2024-11-28 23:59:42 +02:00
ben-scans/solidify/face.c

142 lines
2.7 KiB
C
Raw Normal View History

/*
* face.c - Data structure and handling of one face of a part
*
* Written 2010 by Werner Almesberger
* Copyright 2010 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <sys/wait.h>
#include "util.h"
#include "array.h"
#include "histo.h"
#include "face.h"
#define CACHE_DIR ".cache"
static struct face *read_file(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;
f->cx = (f->a->min_x+f->a->max_x)/2;
f->cy = (f->a->min_y+f->a->max_y)/2;
h = make_histo(f->a);
f->z_ref = f->a->min_z+median(h);
free_histo(h);
f->fx = f->fy = 0;
f->m.a[0][0] = f->m.a[1][1] = 1;
f->m.a[0][1] = f->m.a[1][0] = 0;
f->m.b[0] = f->m.b[1] = 0;
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;
}
struct face *read_face(const char *name)
{
const char *p;
int cwd;
struct face *face;
if (strncmp(name, "http:", 5) && strncmp(name, "https:", 6))
return read_file(name);
p = strrchr(name, '/');
if (!p || !p[1]) {
fprintf(stderr, "malformed URL: \"%s\"\n", name);
exit(1);
}
cwd = open(".", O_RDONLY);
if (cwd < 0) {
perror(".");
exit(1);
}
if (chdir(CACHE_DIR) < 0) {
perror(CACHE_DIR);
exit(1);
}
if (access(p+1, R_OK) < 0) {
char tmp[1000]; /* @@@ enough */
int res;
sprintf(tmp, "wget '%s'", name);
res = system(tmp);
if (res < 0) {
perror("system");
exit(1);
}
if (!WIFEXITED(res) || WEXITSTATUS(res)) {
fprintf(stderr, "%s: status %d\n", tmp, res);
exit(1);
}
}
face = read_file(p+1);
if (fchdir(cwd) < 0) {
perror("fchdir");
exit(1);
}
return face;
}