Root/cameo/stl.c

1/*
2 * stl.c - Genererate STL slice from polygon set
3 *
4 * Written 2013 by Werner Almesberger
5 * Copyright 2013 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 <stdlib.h>
15#include <stdio.h>
16#include <math.h>
17
18#include "poly2d.h"
19
20#include "path.h"
21#include "stl.h"
22
23
24static void facet(FILE *file, const struct f2d *f, int a, int b, int c,
25    double za, double zb, double zc, double nx, double ny, double nz)
26{
27    double d;
28
29    d = sqrt(nx*nx+ny*ny+nz*nz);
30    if (!d)
31        d = 1;
32
33    fprintf(file, "facet normal %e %e %e\n", nx/d, ny/d, nz/d);
34    fprintf(file, "\touter loop\n");
35    fprintf(file, "\t\tvertex %e %e %e\n", f->x[a], f->y[a], za);
36    fprintf(file, "\t\tvertex %e %e %e\n", f->x[b], f->y[b], zb);
37    fprintf(file, "\t\tvertex %e %e %e\n", f->x[c], f->y[c], zc);
38    fprintf(file, "\tendloop\n");
39    fprintf(file, "endfacet\n");
40}
41
42
43static void surface(FILE *file, const struct f2d *f)
44{
45    facet(file, f, 0, 1, 2, 1, 1, 1, 0, 0, 1);
46    facet(file, f, 2, 1, 0, 0, 0, 0, 0, 0, -1);
47}
48
49
50static void side(FILE *file, const struct f2d *f, int a, int b)
51{
52    double nx, ny;
53
54    nx = f->y[b]-f->y[a];
55    ny = f->x[a]-f->x[b];
56    facet(file, f, a, b, b, 0, 0, 1, nx, ny, 0);
57    facet(file, f, b, a, a, 1, 1, 0, nx, ny, 0);
58}
59
60
61static void stl_write(FILE *file, const struct path *paths)
62{
63    struct p2d *polys;
64    struct f2d *faces;
65    const struct f2d *f;
66    int i;
67
68    fprintf(file, "solid cameo\n");
69    polys = paths_to_polys(paths);
70    faces = f2d_tri(polys);
71    for (f = faces; f; f = f->next) {
72        surface(file, f);
73        for (i = 0; i != 3; i++)
74            if (f->side[i])
75                side(file, f, i, (i+1) % 3);
76    }
77    p2d_free_all(polys);
78    f2d_free_all(faces);
79    fprintf(file, "endsolid cameo\n");
80}
81
82
83void stl(const char *name, const struct path *paths)
84{
85    FILE *file = stdout;
86
87    if (name) {
88        file = fopen(name, "w");
89        if (!file) {
90            perror(name);
91            exit(1);
92        }
93    }
94    stl_write(file, paths);
95    if (name && fclose(file)) {
96        perror(name);
97        exit(1);
98    }
99}
100

Archive Download this file

Branches:
master



interactive