1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/* ISC License */
#include <endian.h>
#include <errno.h>
#include <float.h>
#include <getopt.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PI 3.14159265358979323846
#define die(code, msg) do { fprintf(stderr, "pineapple: %s\n", msg); exit(code); } while (0)
#define diesys(code, msg) do { fprintf(stderr, "pineapple: %s: %s\n", msg, strerror(errno)); exit(code); } while (0)
#define USAGE "pineapple [-v] [-w width] [-h height] [-f | -a]"
#define dieusage() die(100, "usage: " USAGE)
#define ASCII_CHARS "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
typedef struct { float x, y, z; } vec3;
struct triangle {
vec3 n;
vec3 a, b, c;
};
struct __attribute__((__packed__)) triangle_raw {
vec3 n;
vec3 a, b, c;
uint16_t _attr_count; /* unused */
};
struct camera {
vec3 pos;
float theta;
float phi;
float fov;
};
static inline vec3
vadd(vec3 a, vec3 b)
{
return (vec3){ a.x + b.x, a.y + b.y, a.z + b.z };
}
static inline vec3
vsub(vec3 a, vec3 b)
{
return (vec3){ a.x - b.x, a.y - b.y, a.z - b.z };
}
static inline vec3
smul(vec3 v, float s)
{
return (vec3){ v.x * s, v.y * s, v.z * s };
}
static inline float
dot(vec3 a, vec3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static inline vec3
cross(vec3 a, vec3 b)
{
return (vec3){ a.y*b.z - b.y*a.z, a.z*b.x - b.z*a.x, a.x*b.y - b.x*a.y };
}
static inline float
magnitude(vec3 v)
{
return sqrt(dot(v, v));
}
static inline vec3
vnorm(vec3 v)
{
float mag = magnitude(v);
return (vec3){ v.x / mag, v.y / mag, v.z / mag };
}
static float
ray_triangle_intersect(vec3 P, vec3 d, struct triangle tri)
{
vec3 edge1 = vsub(tri.b, tri.a);
vec3 edge2 = vsub(tri.c, tri.a);
vec3 ray_cross_e2 = cross(d, edge2);
float det = dot(edge1, ray_cross_e2);
if (det > -FLT_EPSILON && det < FLT_EPSILON) return NAN;
float inv_det = 1.0 / det;
vec3 s = vsub(P, tri.a);
float u = inv_det * dot(s, ray_cross_e2);
if (u < 0.0 || u > 1.0) return NAN;
vec3 s_cross_e1 = cross(s, edge1);
float v = inv_det * dot(d, s_cross_e1);
if (v < 0.0 || u + v > 1.0) return NAN;
float t = -inv_det * dot(edge2, s_cross_e1);
if (t > FLT_EPSILON) return t;
return NAN;
}
static void
render_frame(struct camera cam, vec3 light,
const struct triangle *triangles, uint32_t len,
float *pixels, uint32_t width, uint32_t height)
{
for (int h = height - 1; h >= 0; --h) {
for (uint32_t w = 0; w < width; ++w) {
float theta = cam.theta - cam.fov/2.0 + cam.fov/height*h;
float phi = cam.phi - cam.fov/2.0 + cam.fov/width*w;
vec3 dir = { sinf(theta) * cosf(phi), sinf(theta) * sinf(phi), cosf(theta) };
dir = vnorm(dir);
float shortest = INFINITY;
vec3 shortest_norm = {0};
for (size_t i = 0; i < len; ++i) {
float d = ray_triangle_intersect(cam.pos, dir, triangles[i]);
if (d != NAN && fabs(d) < fabs(shortest)) {
shortest = d;
shortest_norm = triangles[i].n;
}
}
pixels[w*width + h] = 0.0;
if (shortest != INFINITY) {
float light_angle = acosf(dot(shortest_norm, light) / magnitude(shortest_norm) / magnitude(light));
pixels[w*width + h] = light_angle / PI;
}
}
}
}
int
main(int argc, char *argv[])
{
int verbosity = 0;
uint32_t width = 80;
uint32_t height = 40;
enum { ASCII, FARBFELD } mode = ASCII;
int c;
while ((c = getopt(argc, argv, "afvh:w:")) > 0) {
errno = 0;
switch (c) {
case 'a': mode = ASCII; break;
case 'f': mode = FARBFELD; break;
case 'v': verbosity = 1; break;
case 'h': height = strtoul(optarg, NULL, 10); if (errno) dieusage(); break;
case 'w': width = strtoul(optarg, NULL, 10); if (errno) dieusage(); break;
default: dieusage();
}
}
argv += optind; argc -= optind;
if (argc > 0) dieusage();
char header[80];
if (fread(header, 1, 80, stdin) < 80)
diesys(111, "fatal: unable to read STL header from stdin");
uint32_t len;
if (fread(&len, 4, 1, stdin) < 1)
diesys(111, "fatal: unable to read STL length from stdin");
len = le32toh(len);
struct triangle *triangles = calloc(len, sizeof(struct triangle));
if (!triangles) diesys(111, "fatal: unable to allocate enough memory for STL facets");
struct triangle_raw raw;
for (size_t i = 0; i < len; ++i) {
if (fread(&raw, sizeof(raw), 1, stdin) < 1)
diesys(111, "fatal: unable to read STL facets");
triangles[i] = (struct triangle){ .n = raw.n, .a = raw.a, .b = raw.b, .c = raw.c };
}
if (verbosity >= 1) fprintf(stderr, "finished reading STL: %d facets\n", len);
struct camera cam = {
.pos = { -70.0, 60.0, 60.0 },
.phi = PI * 0.8,
.theta = PI * 0.5,
.fov = 80.0 * PI / 180.0
};
vec3 light = { 0.0, 0.0, -1.0 };
if (mode == FARBFELD) {
uint32_t wbe = htobe32(width);
uint32_t hbe = htobe32(height);
fwrite("farbfeld", 1, 8, stdout);
fwrite(&wbe, 1, 4, stdout);
fwrite(&hbe, 1, 4, stdout);
}
float *pixels = calloc(sizeof(float), width * height);
if (!pixels) diesys(111, "fatal: unable to allocate enough memory for pixel buffer");
render_frame(cam, light, triangles, len, pixels, width, height);
for (int h = height - 1; h >= 0; --h) {
for (uint32_t w = 0; w < width; ++w) {
float brightness = pixels[w*width + h];
if (mode == ASCII) {
printf("%c", ASCII_CHARS[strlen(ASCII_CHARS) - 1 - (int)(brightness * strlen(ASCII_CHARS))]);
} else if (mode == FARBFELD) {
uint16_t x = htobe16((uint16_t)(brightness * 0xffff));
uint16_t pixel[4] = { x, x, x, htobe16(0xffff) };
fwrite(&pixel, 2, 4, stdout);
}
}
if (mode == ASCII) printf("\n");
}
free(pixels);
return 0;
}
|