2010-09-24 12:22:48 +03:00
|
|
|
/*
|
2010-09-25 10:40:08 +03:00
|
|
|
* povray.c - Generate POV-Ray output
|
2010-09-24 12:22:48 +03:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include "face.h"
|
|
|
|
#include "solid.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void height_field(const char *name, const struct face *f,
|
|
|
|
const struct matrix *m)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
int x, y;
|
|
|
|
int z;
|
|
|
|
uint16_t g;
|
|
|
|
uint8_t v[2];
|
|
|
|
|
|
|
|
file = fopen(name, "w");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy);
|
|
|
|
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;
|
|
|
|
fwrite(v, 2, 1, file);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-25 03:28:42 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-25 08:44:59 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void povray_face(const struct face *f, const char *side,
|
2010-09-25 10:40:08 +03:00
|
|
|
const char *prefix, int flip, double dist)
|
2010-09-25 08:44:59 +03:00
|
|
|
{
|
|
|
|
int sz = f->a->max_z-f->a->min_z;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1/65535 = 0.000015..., so we set the water level a bit lower, e.g.,
|
|
|
|
* to 0.0001
|
|
|
|
*/
|
|
|
|
printf(
|
|
|
|
"\theight_field {\n"
|
|
|
|
"\t pgm \"%s-%s.pgm\"\n"
|
|
|
|
"\t water_level 0.00001\n"
|
|
|
|
"\t smooth\n"
|
|
|
|
"\t scale <%g, %g, %g>\n"
|
|
|
|
"\t rotate <90, 0, 0>\n"
|
|
|
|
"\t translate <%g, %g, %g>\n"
|
2010-09-25 10:40:08 +03:00
|
|
|
"\t translate <0, 0, %g>\n"
|
|
|
|
"%s" /* flip bottom face */
|
|
|
|
"\t translate <0, 0, %g>\n"
|
2010-09-25 08:44:59 +03:00
|
|
|
"\t}\n", prefix, side,
|
|
|
|
f->sx*f->x_step, sz*f->z_step, f->sy*f->y_step,
|
2010-09-25 10:40:08 +03:00
|
|
|
-f->sx*f->x_step/2, f->sy*f->y_step/2, f->a->min_z*f->z_step,
|
|
|
|
-f->z_ref*f->z_step,
|
|
|
|
flip ? "\t rotate <180, 0, 0>\n" : "",
|
|
|
|
dist*f->z_step);
|
2010-09-25 08:44:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-25 03:28:42 +03:00
|
|
|
void povray(const char *name, const struct solid *s)
|
2010-09-24 12:22:48 +03:00
|
|
|
{
|
|
|
|
struct matrix m;
|
2010-09-25 03:28:42 +03:00
|
|
|
char tmp[1000]; /* @@@ enough */
|
2010-09-24 12:22:48 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-09-25 03:28:42 +03:00
|
|
|
sprintf(tmp, "%s-top.pgm", name);
|
|
|
|
height_field(tmp, s->a, &m);
|
|
|
|
sprintf(tmp, "%s-bot.pgm", name);
|
|
|
|
height_field(tmp, s->b, &m);
|
2010-09-24 12:22:48 +03:00
|
|
|
|
2010-09-25 03:28:42 +03:00
|
|
|
sanitize(name, tmp);
|
2010-09-25 08:44:59 +03:00
|
|
|
printf("#declare Part_%s =\n intersection {\n", tmp);
|
2010-09-25 10:40:08 +03:00
|
|
|
povray_face(s->a, "top", name, 0, s->dist/2);
|
|
|
|
povray_face(s->b, "bot", name, 1, -s->dist/2);
|
2010-09-25 08:44:59 +03:00
|
|
|
printf(" }\n");
|
2010-09-24 12:22:48 +03:00
|
|
|
}
|