Date:2010-09-25 08:23:19 (12 years 11 months ago)
Author:Werner Almesberger
Commit:49ea351443b518ab71ca8c241149f04a9587d1a2
Message:All there is in solid.c is about POV-Ray, so rename it to povray.c

- solidify/solid.c: renamed to solidify/povray.c
- solidify/Makefile (OBJS): change solid.o to povray.o
Files: solidify/Makefile (1 diff)
solidify/povray.c (1 diff)
solidify/solid.c (1 diff)

Change Details

solidify/Makefile
1212
1313SHELL = /bin/bash
1414
15OBJS = array.o face.o histo.o level.o matrix.o overlap.o project.o solid.o \
15OBJS = array.o face.o histo.o level.o matrix.o overlap.o povray.o project.o \
1616       solidify.o style.o util.o
1717
1818CFLAGS_WARN = -Wall -Wshadow -Wmissing-prototypes \
solidify/povray.c
1/*
2 * solid.c - Data structure and handling of a solid made of two opposing faces
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 <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "face.h"
19#include "solid.h"
20
21
22static void height_field(const char *name, const struct face *f,
23    const struct matrix *m)
24{
25    FILE *file;
26    int x, y;
27    int z;
28    uint16_t g;
29    uint8_t v[2];
30
31    file = fopen(name, "w");
32    if (!file) {
33        perror(name);
34        exit(1);
35    }
36    fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy);
37    for (y = 0; y != f->sy; y++)
38        for (x = 0; x != f->sx; x++) {
39            z = get(f->a, x+f->a->min_x, y+f->a->min_y);
40            g = z == UNDEF ? 0 :
41                65535*(z-f->a->min_z)/(f->a->max_z-f->a->min_z);
42            v[0] = g >> 8;
43            v[1] = g;
44            fwrite(v, 2, 1, file);
45        }
46    fclose(file);
47}
48
49
50static void sanitize(const char *s, char *res)
51{
52    while (*s) {
53        if ((*s >= 'A' && *s <= 'Z') ||
54            (*s >= 'a' && *s <= 'z') ||
55            (*s >= '0' && *s <= '9') || *s == '_')
56            *res = *s;
57        else
58            *res = '_';
59        res++;
60        s++;
61    }
62    *res = 0;
63}
64
65
66/*
67 * For now, we put the part such that its x/y/z center is at the origin.
68 * Later, we will also have to consider the changes the user made and the
69 * relation with the opposing face.
70 */
71
72static void povray_face(const struct face *f, const char *side,
73    const char *prefix)
74{
75    int sz = f->a->max_z-f->a->min_z;
76
77    /*
78     * 1/65535 = 0.000015..., so we set the water level a bit lower, e.g.,
79     * to 0.0001
80     */
81    printf(
82"\theight_field {\n"
83"\t pgm \"%s-%s.pgm\"\n"
84"\t water_level 0.00001\n"
85"\t smooth\n"
86"\t scale <%g, %g, %g>\n"
87"\t rotate <90, 0, 0>\n"
88"\t translate <%g, %g, %g>\n"
89"\t}\n", prefix, side,
90    f->sx*f->x_step, sz*f->z_step, f->sy*f->y_step,
91    -f->sx*f->x_step/2, f->sy*f->y_step/2, -sz*f->z_step/2);
92}
93
94
95void povray(const char *name, const struct solid *s)
96{
97    struct matrix m;
98    char tmp[1000]; /* @@@ enough */
99
100    m.a[0][0] = m.a[1][1] = 1;
101    m.a[0][1] = m.a[1][0] = 0;
102    m.b[0] = m.b[1] = 0;
103
104    sprintf(tmp, "%s-top.pgm", name);
105    height_field(tmp, s->a, &m);
106    sprintf(tmp, "%s-bot.pgm", name);
107    height_field(tmp, s->b, &m);
108
109    sanitize(name, tmp);
110    printf("#declare Part_%s =\n intersection {\n", tmp);
111    povray_face(s->a, "top", name);
112    povray_face(s->b, "bot", name);
113    printf(" }\n");
114}
solidify/solid.c
1/*
2 * solid.c - Data structure and handling of a solid made of two opposing faces
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 <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "face.h"
19#include "solid.h"
20
21
22static void height_field(const char *name, const struct face *f,
23    const struct matrix *m)
24{
25    FILE *file;
26    int x, y;
27    int z;
28    uint16_t g;
29    uint8_t v[2];
30
31    file = fopen(name, "w");
32    if (!file) {
33        perror(name);
34        exit(1);
35    }
36    fprintf(file, "P5\n%d %d\n65535\n", f->sx, f->sy);
37    for (y = 0; y != f->sy; y++)
38        for (x = 0; x != f->sx; x++) {
39            z = get(f->a, x+f->a->min_x, y+f->a->min_y);
40            g = z == UNDEF ? 0 :
41                65535*(z-f->a->min_z)/(f->a->max_z-f->a->min_z);
42            v[0] = g >> 8;
43            v[1] = g;
44            fwrite(v, 2, 1, file);
45        }
46    fclose(file);
47}
48
49
50static void sanitize(const char *s, char *res)
51{
52    while (*s) {
53        if ((*s >= 'A' && *s <= 'Z') ||
54            (*s >= 'a' && *s <= 'z') ||
55            (*s >= '0' && *s <= '9') || *s == '_')
56            *res = *s;
57        else
58            *res = '_';
59        res++;
60        s++;
61    }
62    *res = 0;
63}
64
65
66/*
67 * For now, we put the part such that its x/y/z center is at the origin.
68 * Later, we will also have to consider the changes the user made and the
69 * relation with the opposing face.
70 */
71
72static void povray_face(const struct face *f, const char *side,
73    const char *prefix)
74{
75    int sz = f->a->max_z-f->a->min_z;
76
77    /*
78     * 1/65535 = 0.000015..., so we set the water level a bit lower, e.g.,
79     * to 0.0001
80     */
81    printf(
82"\theight_field {\n"
83"\t pgm \"%s-%s.pgm\"\n"
84"\t water_level 0.00001\n"
85"\t smooth\n"
86"\t scale <%g, %g, %g>\n"
87"\t rotate <90, 0, 0>\n"
88"\t translate <%g, %g, %g>\n"
89"\t}\n", prefix, side,
90    f->sx*f->x_step, sz*f->z_step, f->sy*f->y_step,
91    -f->sx*f->x_step/2, f->sy*f->y_step/2, -sz*f->z_step/2);
92}
93
94
95void povray(const char *name, const struct solid *s)
96{
97    struct matrix m;
98    char tmp[1000]; /* @@@ enough */
99
100    m.a[0][0] = m.a[1][1] = 1;
101    m.a[0][1] = m.a[1][0] = 0;
102    m.b[0] = m.b[1] = 0;
103
104    sprintf(tmp, "%s-top.pgm", name);
105    height_field(tmp, s->a, &m);
106    sprintf(tmp, "%s-bot.pgm", name);
107    height_field(tmp, s->b, &m);
108
109    sanitize(name, tmp);
110    printf("#declare Part_%s =\n intersection {\n", tmp);
111    povray_face(s->a, "top", name);
112    povray_face(s->b, "bot", name);
113    printf(" }\n");
114}

Archive Download the corresponding diff file

Branches:
master



interactive