Ben NanoNote 3D scans
Sign in or create your account | Project List | Help
Ben NanoNote 3D scans Git Source Tree
Root/
Source at commit 598df91d9f406b22d93a21f9f431bbf67a48ab6e created 13 years 6 months ago. By Werner Almesberger, Added copyright headers all over the place. (GPLv2+) | |
---|---|
1 | /* |
2 | * face.c - Data structure and handling of one face of a part |
3 | * |
4 | * Written 2010 by Werner Almesberger |
5 | * Copyright 2010 by Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | #include <unistd.h> |
17 | #include <string.h> |
18 | #include <math.h> |
19 | #include <fcntl.h> |
20 | #include <sys/wait.h> |
21 | |
22 | #include "util.h" |
23 | #include "array.h" |
24 | #include "histo.h" |
25 | #include "face.h" |
26 | |
27 | |
28 | #define CACHE_DIR ".cache" |
29 | |
30 | |
31 | static struct face *read_file(const char *name) |
32 | { |
33 | FILE *file; |
34 | struct face *f; |
35 | struct histo *h; |
36 | float x, y, z; |
37 | int xi, yi, zi; |
38 | |
39 | if (!strcmp(name, "-")) { |
40 | file = stdin; |
41 | } else { |
42 | int len; |
43 | |
44 | len = strlen(name); |
45 | if (len > 4 && !strcmp(name+len-4, ".bz2")) { |
46 | char tmp[1000]; /* @@@ enough */ |
47 | |
48 | sprintf(tmp, "bzcat \"%s\"", name); |
49 | file = popen(tmp, "r"); |
50 | if (!file) { |
51 | perror(tmp); |
52 | exit(1); |
53 | } |
54 | } else { |
55 | file = fopen(name, "r"); |
56 | if (!file) { |
57 | perror(name); |
58 | exit(1); |
59 | } |
60 | } |
61 | } |
62 | f = alloc_type(struct face); |
63 | f->a = new_array(); |
64 | |
65 | while (fscanf(file, "%f,%f,%f\r\n", &x, &y, &z) == 3) { |
66 | /* @@@ hack - should auto-scale */ |
67 | xi = round(x*10.0); |
68 | yi = round(y*10.0); |
69 | zi = round(z*40.0); |
70 | set(f->a, xi, yi, zi); |
71 | } |
72 | if (file != stdin) |
73 | (void) fclose(file); |
74 | |
75 | f->sx = f->a->max_x-f->a->min_x+1; |
76 | f->sy = f->a->max_y-f->a->min_y+1; |
77 | h = make_histo(f->a); |
78 | f->z_ref = f->a->min_z+median(h); |
79 | free_histo(h); |
80 | f->fx = f->fy = 0; |
81 | |
82 | fprintf(stderr, "%d-%d / %d-%d / %d-%d\n", |
83 | f->a->min_x, f->a->max_x, f->a->min_y, f->a->max_y, |
84 | f->a->min_z, f->a->max_z); |
85 | |
86 | return f; |
87 | } |
88 | |
89 | |
90 | struct face *read_face(const char *name) |
91 | { |
92 | const char *p; |
93 | int cwd; |
94 | struct face *face; |
95 | |
96 | if (strncmp(name, "http:", 5) && strncmp(name, "https:", 6)) |
97 | return read_file(name); |
98 | p = strrchr(name, '/'); |
99 | if (!p || !p[1]) { |
100 | fprintf(stderr, "malformed URL: \"%s\"\n", name); |
101 | exit(1); |
102 | } |
103 | cwd = open(".", O_RDONLY); |
104 | if (cwd < 0) { |
105 | perror("."); |
106 | exit(1); |
107 | } |
108 | if (chdir(CACHE_DIR) < 0) { |
109 | perror(CACHE_DIR); |
110 | exit(1); |
111 | } |
112 | if (access(p+1, R_OK) < 0) { |
113 | char tmp[1000]; /* @@@ enough */ |
114 | int res; |
115 | |
116 | sprintf(tmp, "wget '%s'", name); |
117 | res = system(tmp); |
118 | if (res < 0) { |
119 | perror("system"); |
120 | exit(1); |
121 | } |
122 | if (!WIFEXITED(res) || WEXITSTATUS(res)) { |
123 | fprintf(stderr, "%s: status %d\n", tmp, res); |
124 | exit(1); |
125 | } |
126 | } |
127 | face = read_file(p+1); |
128 | if (fchdir(cwd) < 0) { |
129 | perror("fchdir"); |
130 | exit(1); |
131 | } |
132 | return face; |
133 | } |
134 |
Branches:
master