Cleaned up POV-Ray file handling. New use: solidify -p project

- solidify/Makefile (pov, disp, main.pov): call the main POV-Ray file
  $(NAME).pov, not main.pov
- solidify/Makefile: generate $(NAME).inc
- solidify/solid.h, solidify/povray.c (povray_face, povray): accept file
  name as argument and write to the specified file instead of stdout
- solidify/povray.c (height_field): added file error handling
- solidify/povray.c (height_field, povray): removed useless matrix argument
- solidify/solidify.c (usage, main): new invocation "-p project" to generate
  POV-Ray output. Removed old isatty() hack.
- solidify/Makefile: updated to use solidify -p to generate POV-Ray output
This commit is contained in:
Werner Almesberger 2010-09-26 23:37:03 -03:00
parent 1c183e077a
commit 12f5aa2188
4 changed files with 74 additions and 36 deletions

View File

@ -66,26 +66,25 @@ FACE_B=$(DIR)/ben-$(NAME)-$(BOTTOM)-100um.txt.bz2
.PHONY: new run pov disp
# always regenerate main.pov, e.g., if NAME is changed via the command line
.PHONY: main.pov
new: solidify
rm -f $(NAME).sfy
./solidify $(NAME).sfy $(FACE_A) $(FACE_B) $(D) >$(NAME).inc
./solidify $(NAME).sfy $(FACE_A) $(FACE_B) $(D)
run: solidify
./solidify $(NAME).sfy >$(NAME).inc
./solidify $(NAME).sfy
pov: main.pov
povray +A +P -W1280 -H900 main.pov
pov: $(NAME).pov $(NAME).inc
povray +A +P -W1280 -H900 $(NAME).pov
disp:
display main.png
display $(NAME).png
main.pov: template.pov
$(NAME).pov: template.pov
sed 's/NAME/$(NAME)/' template.pov >$@ || { rm -f $@; exit 1; }
$(NAME).inc: $(NAME).sfy
./solidify -p $<
# ----- Dependencies ----------------------------------------------------------
# compile and generate dependencies, from fped, based on

View File

@ -14,14 +14,14 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "face.h"
#include "solid.h"
static void height_field(const char *name, const struct face *f,
const struct matrix *m)
static void height_field(const char *name, const struct face *f)
{
FILE *file;
int x, y;
@ -34,7 +34,10 @@ static void height_field(const char *name, const struct face *f,
perror(name);
exit(1);
}
fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy);
if (fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy) < 0) {
perror(name);
exit(1);
}
for (y = 0; y != f->sy; y++)
for (x = 0; x != f->sx; x++) {
z = get(f->a, x+f->a->min_x, y+f->a->min_y);
@ -42,9 +45,15 @@ static void height_field(const char *name, const struct face *f,
65535*(z-f->a->min_z)/(f->a->max_z-f->a->min_z);
v[0] = g >> 8;
v[1] = g;
fwrite(v, 2, 1, file);
if (fwrite(v, 2, 1, file) != 1) {
perror(name);
exit(1);
}
}
fclose(file);
if (fclose(file) < 0) {
perror(name);
exit(1);
}
}
@ -70,7 +79,7 @@ static void sanitize(const char *s, char *res)
* relation with the opposing face.
*/
static void povray_face(const struct face *f, const char *side,
static void povray_face(FILE *file, const struct face *f, const char *side,
const char *prefix, int flip, double dist)
{
double a;
@ -82,7 +91,7 @@ static void povray_face(const struct face *f, const char *side,
a = asin(-f->m.a[0][1])/M_PI*180;
if (f->m.a[0][0] < 0)
a = 180-a;
printf(
fprintf(file,
"\theight_field {\n"
"\t pgm \"%s-%s.pgm\"\n"
"\t water_level 0.00001\n"
@ -110,23 +119,40 @@ static void povray_face(const struct face *f, const char *side,
}
void povray(const char *name, const struct solid *s)
void povray(const char *file_name, const char *name, const struct solid *s)
{
struct matrix m;
FILE *file;
char tmp[1000]; /* @@@ enough */
m.a[0][0] = m.a[1][1] = 1;
m.a[0][1] = m.a[1][0] = 0;
m.b[0] = m.b[1] = 0;
file = strcmp(file_name, "-") ? fopen(file_name, "w") : stdout;
if (!file) {
perror(file_name);
exit(1);
}
sprintf(tmp, "%s-top.pgm", name);
height_field(tmp, s->a, &m);
height_field(tmp, s->a);
sprintf(tmp, "%s-bot.pgm", name);
height_field(tmp, s->b, &m);
height_field(tmp, s->b);
sanitize(name, tmp);
printf("#declare Part_%s =\n intersection {\n", tmp);
povray_face(s->a, "top", name, 0, s->dist/2);
povray_face(s->b, "bot", name, 1, -s->dist/2);
printf(" }\n");
fprintf(file, "#declare Part_%s =\n intersection {\n", tmp);
povray_face(file, s->a, "top", name, 0, s->dist/2);
povray_face(file, s->b, "bot", name, 1, -s->dist/2);
fprintf(file, " }\n");
/* @@@ not reliable */
if (ferror(file)) {
perror(file_name);
exit(1);
}
if (fflush(file) == EOF) {
perror(file_name);
exit(1);
}
if (file != stdout)
if (fclose(file) < 0) {
perror(file_name);
exit(1);
}
}

View File

@ -13,12 +13,15 @@
#ifndef SOLID_H
#define SOLID_H
#include <stdio.h>
struct solid {
struct face *a, *b;
double dist;
};
void povray(const char *name, const struct solid *s);
void povray(const char *file_name, const char *name, const struct solid *s);
#endif /* !SOLID_H */

View File

@ -122,6 +122,7 @@ static void gui(void)
static void usage(const char *name)
{
fprintf(stderr, "usage: %s project [top bottom dist]\n", name);
fprintf(stderr, " %s -p project\n", name);
exit(1);
}
@ -129,6 +130,7 @@ static void usage(const char *name)
int main(int argc, char **argv)
{
double dist;
int do_pov = 0;
gtk_init(&argc, &argv);
setlocale(LC_ALL, "C"); /* damage control */
@ -137,6 +139,12 @@ int main(int argc, char **argv)
case 2:
prj = load_project(argv[1]);
break;
case 3:
if (strcmp(argv[1], "-p"))
usage(*argv);
prj = load_project(argv[2]);
do_pov = 1;
break;
case 5:
dist = atof(argv[4]);
prj = new_project(argv[1], argv[2], argv[3], dist);
@ -145,19 +153,21 @@ int main(int argc, char **argv)
usage(*argv);
}
gui();
save_project(prj);
if (!isatty(1)) {
if (do_pov) {
const char *slash = strrchr(prj->name, '/');
char tmp[1000]; /* @@@ enough */
char tmp[1000], tmp2[1000]; /* @@@ enough */
strcpy(tmp, slash ? slash+1 : prj->name);
if (strchr(tmp, '.'))
*strchr(tmp, '.') = 0;
povray(tmp, &prj->s);
sprintf(tmp2, "%s.inc", tmp);
povray(tmp2, tmp, &prj->s);
return 0;
}
gui();
save_project(prj);
return 0;
}