mirror of
git://projects.qi-hardware.com/ben-scans.git
synced 2024-11-22 08:19:44 +02:00
b5a2d5d8ef
- solidify/face.c (read_face): moved code into new function read_file - solidify/face.c (read_face): added wrapper for cached wget of http: and https: URLs
121 lines
2.1 KiB
C
121 lines
2.1 KiB
C
#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;
|
|
h = make_histo(f->a);
|
|
f->z_ref = f->a->min_z+median(h);
|
|
free_histo(h);
|
|
f->fx = f->fy = 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;
|
|
}
|