2010-09-25 10:46:16 +03:00
|
|
|
/*
|
|
|
|
* povray.c - Generate POV-Ray output
|
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2010-09-27 05:37:03 +03:00
|
|
|
#include <string.h>
|
2010-09-25 23:47:50 +03:00
|
|
|
#include <math.h>
|
2010-09-25 10:46:16 +03:00
|
|
|
|
|
|
|
#include "face.h"
|
|
|
|
#include "solid.h"
|
|
|
|
|
|
|
|
|
2010-09-27 05:37:03 +03:00
|
|
|
static void height_field(const char *name, const struct face *f)
|
2010-09-25 10:46:16 +03:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
int x, y;
|
|
|
|
int z;
|
|
|
|
uint16_t g;
|
|
|
|
uint8_t v[2];
|
|
|
|
|
|
|
|
file = fopen(name, "w");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-09-27 05:37:03 +03:00
|
|
|
if (fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy) < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-09-25 10:46:16 +03:00
|
|
|
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);
|
|
|
|
g = z == UNDEF ? 0 :
|
|
|
|
65535*(z-f->a->min_z)/(f->a->max_z-f->a->min_z);
|
|
|
|
v[0] = g >> 8;
|
|
|
|
v[1] = g;
|
2010-09-27 05:37:03 +03:00
|
|
|
if (fwrite(v, 2, 1, file) != 1) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-09-25 10:46:16 +03:00
|
|
|
}
|
2010-09-27 05:37:03 +03:00
|
|
|
if (fclose(file) < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-09-25 10:46:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void sanitize(const char *s, char *res)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if ((*s >= 'A' && *s <= 'Z') ||
|
|
|
|
(*s >= 'a' && *s <= 'z') ||
|
|
|
|
(*s >= '0' && *s <= '9') || *s == '_')
|
|
|
|
*res = *s;
|
|
|
|
else
|
|
|
|
*res = '_';
|
|
|
|
res++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*res = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For now, we put the part such that its x/y/z center is at the origin.
|
|
|
|
* Later, we will also have to consider the changes the user made and the
|
|
|
|
* relation with the opposing face.
|
|
|
|
*/
|
|
|
|
|
2010-09-27 05:37:03 +03:00
|
|
|
static void povray_face(FILE *file, const struct face *f, const char *side,
|
2010-09-25 10:46:16 +03:00
|
|
|
const char *prefix, int flip, double dist)
|
|
|
|
{
|
2010-09-25 23:47:50 +03:00
|
|
|
double a;
|
2010-09-25 10:46:16 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 1/65535 = 0.000015..., so we set the water level a bit lower, e.g.,
|
|
|
|
* to 0.0001
|
|
|
|
*/
|
2010-09-25 23:47:50 +03:00
|
|
|
a = asin(-f->m.a[0][1])/M_PI*180;
|
|
|
|
if (f->m.a[0][0] < 0)
|
|
|
|
a = 180-a;
|
2010-09-27 05:37:03 +03:00
|
|
|
fprintf(file,
|
2010-09-25 10:46:16 +03:00
|
|
|
"\theight_field {\n"
|
|
|
|
"\t pgm \"%s-%s.pgm\"\n"
|
|
|
|
"\t water_level 0.00001\n"
|
|
|
|
"\t smooth\n"
|
2010-09-25 23:47:50 +03:00
|
|
|
"\t scale <%g, %g, %g> /* scale from unit cube to real size */\n"
|
|
|
|
"\t rotate <90, 0, 0> /* swap Y/Z axis */\n"
|
|
|
|
"\t translate <%g, %g, 0> /* move to X/Y center */\n"
|
|
|
|
"\t rotate <%g, %g, 0> /* z0 plane tilt */\n"
|
|
|
|
"\t translate <0, 0, %g> /* min_z offset */\n"
|
|
|
|
"\t translate <0, 0, %g> /* z0 plane offset */\n"
|
|
|
|
"\t rotate <0, 0, %g> /* overlay - rotation */\n"
|
|
|
|
"\t translate <%g, %g, 0> /* overlay - shift */\n"
|
2010-09-25 10:46:16 +03:00
|
|
|
"%s" /* flip bottom face */
|
2010-09-25 23:47:50 +03:00
|
|
|
"\t translate <0, 0, %g> /* half the distance between faces */\n"
|
2010-09-25 10:46:16 +03:00
|
|
|
"\t}\n", prefix, side,
|
2010-09-26 11:48:31 +03:00
|
|
|
f->sx*f->x_step, (f->sz-1)*f->z_step, f->sy*f->y_step,
|
2010-09-25 23:47:50 +03:00
|
|
|
-f->sx*f->x_step/2, f->sy*f->y_step/2,
|
2010-09-27 06:54:39 +03:00
|
|
|
-fy_to_angle(f), fx_to_angle(f),
|
2010-09-25 23:47:50 +03:00
|
|
|
f->a->min_z*f->z_step,
|
2010-09-25 10:46:16 +03:00
|
|
|
-f->z_ref*f->z_step,
|
2010-09-25 23:47:50 +03:00
|
|
|
a,
|
|
|
|
f->m.b[0]*f->x_step, f->m.b[1]*f->y_step,
|
|
|
|
flip ? "\t rotate <0, 180, 0>\t/* flip bottom face */\n" : "",
|
2010-09-25 10:46:16 +03:00
|
|
|
dist*f->z_step);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-27 05:37:03 +03:00
|
|
|
void povray(const char *file_name, const char *name, const struct solid *s)
|
2010-09-25 10:46:16 +03:00
|
|
|
{
|
2010-09-27 05:37:03 +03:00
|
|
|
FILE *file;
|
2010-09-25 10:46:16 +03:00
|
|
|
char tmp[1000]; /* @@@ enough */
|
|
|
|
|
2010-09-27 05:37:03 +03:00
|
|
|
file = strcmp(file_name, "-") ? fopen(file_name, "w") : stdout;
|
|
|
|
if (!file) {
|
|
|
|
perror(file_name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-09-25 10:46:16 +03:00
|
|
|
|
|
|
|
sprintf(tmp, "%s-top.pgm", name);
|
2010-09-27 05:37:03 +03:00
|
|
|
height_field(tmp, s->a);
|
2010-09-25 10:46:16 +03:00
|
|
|
sprintf(tmp, "%s-bot.pgm", name);
|
2010-09-27 05:37:03 +03:00
|
|
|
height_field(tmp, s->b);
|
2010-09-25 10:46:16 +03:00
|
|
|
|
|
|
|
sanitize(name, tmp);
|
2010-09-27 05:37:03 +03:00
|
|
|
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);
|
|
|
|
}
|
2010-09-25 10:46:16 +03:00
|
|
|
}
|