2 * Generates an STL file
3 * usage: ./output+<size> <thickness> <unit-circle-diameter>
4 * thickness is in original moebius unit circle units
5 * unit circle diameter is in whatever dimensions the STL file uses
9 * - define a shape for the rim so that it is solid
10 * - scale the coordinates
11 * - translate the coordinates so they're all positive
15 * http://www.ennex.com/~fabbers/StL.asp
16 * http://en.wikipedia.org/wiki/STL_%28file_format%29
17 * saved here as stl-format-wikipedia.txt
21 * We add thickness by adding vertices as follows:
29 * *E *G | :*E *G *E *G *E *G *E
31 * *D ____________|*D ____________ *D ____________ *D ____________ *D __
33 * / \ / :\ / \ / \ / \
34 * \ *A 4/ \: \5 *B / \ *B / \ *B / \
37 * *B \ / *B2| \2 1/ 0*A \ / *A \ / *A
38 * \ / : | \ / . ' \ / \ /
39 * _______ *C _____ :_|___ *C' __________ *C ___________ *C _________
40 * /\ : | 3 /\. 0 /\ /\
41 * / \ :3| / \ ` . / \ / \
42 * *A / \ *A | /4 5\ 5*B / \ *B / \ *B
43 * / \ 1: / / \ / \ / \
44 * / \ :/ / 4 \ / \ / \
45 * / *B2 2\ / /1 *A \ / *A \ / *A \ /
46 * \ / \/:/ 0 \ / \ / \ /
47 * *C ____________|*C ____________ *C ___________ *C ___________ *C __
49 * / \ / :\ / \ / \ / \
50 * \ *A3 4/ \: \5 5*B / \ *B / \ *B / \
53 * *B \ / *B | \ / *A \ / *A \ / *A
54 * \ / : | \ / . ' \ / \ /
55 * _______ *C _____ :_|___ *C' __________ *C ___________ *C _________
56 * /\ : | 3 /\. 0 /\ /\
57 * / \ : | / \ ` . / \ / \
62 * \ / : | \ / . ' \ / \ /
63 * _______ *C _____ :_|___ *C' __________ *C ___________ *C _________
64 * /\ : | 3 /\. 0 /\ /\
65 * / \ : | / \ ` . / \ / \
66 * *A / \ *A | /4 5\ *B / \ *B / \ *B
67 * / \ 1: / / \ / \ / \
69 * / *B2 2\ / /1 0*A \ / *A \ / *A \ /
70 * \ / \/:/ \ / \ / \ /
71 * *D ____________|*D ____________ *D ___________ *D ___________ *D __
73 * *E *G | :*E *G *E *G *E *G *E
78 * Each A,B,C,D represents two vertices - one on each side of the
79 * surface. Each E,F,G represents a several vertices in an arc around
80 * the rim. Digits are `e' values for edges or relative AB
83 * The constructions we use are:
85 * A, B: Extend the normal vector of the containing intriangle
86 * from its centroid for thickness.
88 * C: Take mean position (centroid) of all surrounding
89 * computed A and B, and extend from base point in
90 * direction of that centroid for thickness.
93 * Compute notional rim vector R as mean of the two
94 * adjoining rim edges, and consider plane P as
95 * that passing through the base point normal to R.
96 * Project the centroid of the two adjoining non-rim
97 * vertices onto P. Now D(EF)+ED is the semicircle
98 * in P centred on the base point with radius thickness
99 * and which is opposite that centroid.
101 * G: Each F is the mean of those two adjacent Es with the
102 * same angle in their respective Ps.
104 * The outtriangles are:
106 * For each non-rim vertex on each side, the six triangles formed by
107 * its C and the surrounding A's and B's.
109 * For each rim vertex on each side, the two triangles formed by its
110 * D and the nearest As and Bs (two As and one B or vice versa).
112 * For each rim edge on each side, the triangle formed by that edge's
113 * ends' Ds and the corresponding A or B.
115 * For each G, the six triangles formed by that G and the adjacent
116 * four Fs (or two Fs and two Ds) and two Es.
122 /*---------- declarations and useful subroutines ----------*/
124 #define FOR_SIDE for (side=0; side<2; side++)
131 #define NDEF (NG*2+1)
133 static OutVertex ovAB[N][2][2]; /* indices: vertex to W, A=0 B=1, side */
134 static OutVertex ovC[N][2];
135 static OutVertex ovDEF[X][2][NDEF]; /* x, !!y, angle */
136 static OutVertex ovG[X][2][NG]; /* x, !!y, angle; for G to the East of x */
140 static double thick; /* in input units */
143 static void normalise_thick(double a[D3]) {
144 /* multiplies a by a scalar so that its magnitude is thick */
146 double multby= thick / magnD(a);
150 static void triangle_normal(double normal[D3], const double a[D3],
151 const double b[D3], const double c[D3]) {
152 double ab[D3], ac[D3];
155 K ab[k]= b[k] - a[k];
156 K ac[k]= c[k] - a[k];
160 static OutVertex *invertex2outvertexab(int v0, int e, int side) {
163 case 0: vref= v0 ; ab=0; break;
164 case 1: vref=EDGE_END2(v0,2); ab=1; break;
165 case 2: vref=EDGE_END2(v0,3); ab=0; break;
166 case 3: vref=EDGE_END2(v0,3); ab=1; break;
167 case 4: vref=EDGE_END2(v0,4); ab=0; break;
168 case 5: vref= v0 ; ab=1; break;
171 if (vref<0) return 0;
172 int sw= VERTICES_SPAN_JOIN_P(v0,vref);
173 return &ovAB[vref][ab^sw][side^sw];
176 /*---------- output vertices ----------*/
178 static void compute_outvertices(void) {
179 int v0,k,side,ab,x,y;
182 for (ab=0; ab<2; ab++) {
183 int v1= EDGE_END2(v0, ab?5:0);
184 int v2= EDGE_END2(v0, ab?0:1);
186 K ovAB[v0][ab][0].p[k]= ovAB[v0][ab][1].p[k]= NAN;
189 double normal[D3], centroid[D3];
190 triangle_normal(normal, in[v0],in[v1],in[v2]);
191 normalise_thick(normal);
192 K centroid[k]= (in[v0][k] + in[v1][k] + in[v2][k]) / 3.0;
193 K ovAB[v0][ab][0].p[k]= centroid[k] + normal[k];
194 K ovAB[v0][ab][1].p[k]= centroid[k] - normal[k];
199 int vw= EDGE_END2(v0,3);
200 int vnw= EDGE_END2(v0,2);
201 int vsw= EDGE_END2(v0,4);
202 if (vnw<0 || vsw<0 || vw<0) {
203 K ovC[v0][0].p[k]= ovC[v0][1].p[k]= NAN;
209 OutVertex *ovab= invertex2outvertexab(v0,e,side);
210 K adjust[k] += ovab->k[k];
213 K adjust[k] -= in[v0][k];
214 normalise_thick(adjust);
215 K ovC[v0][side].p[k]= in[v0][k] + adjust[k];
218 FOR_RIM_VERTEX(y,x,v0) {
219 double rim[D3], inner[D3], radius_cos[D3];, radius_sin[D3];
220 int vback, vfwd, around;
222 /* compute mean rim vector, which is just the vector between
223 * the two adjacent rim vertex (ignoring the base vertex) */
224 vback= EDGE_END2(v0,3);
225 vfwd= EDGE_END2(v0,0);
226 assert(vback>=0 && vfwd>=0);
227 K rim[k]= in[fwd][k] - in[vback][k];
229 /* compute the inner centroid */
230 vback= EDGE_END2(v0,4);
231 if (vback>=0) { /* North rim */
232 vfwd= EDGE_END2(v0,5);
233 } else { /* South rim */
234 vback= EDGE_END2(v0,2);
235 vfwd= EDGE_END2(v0,1);
237 assert(vback>=0 && vfwd>=0);
238 K inner[k]= (in[vback][k] + in[vfwd][k]) / 2;
239 K inner[k] -= in[v0][k];
241 /* we compute the radius cos and sin vectors by cross producting
242 * the vector to the inner with the rim, and then again, and
244 xprod(radius_cos,rim,inner);
245 xprod(radius_sin,rim,radius_cos);
246 normalise_thick(radius_cos);
247 normalise_thick(radius_sin);
249 for (around=0; around<NDEF; around++) {
250 double angle= around * PI / (NDEF-1);
252 K ovDEF[x][!!y][around].p[k]=
254 cos(angle) * radius_cos[k] +
255 sin(angle) * radius_sin[k];
258 FOR_RIM_VERTEX(y,x,v0) {
259 int vfwd= EDGE_END2(v0,0);
261 for (around=0; around<NG; around++) {
262 K ovG[x][!!y][around].p[k]=
263 (ovDEF[ x ][!!y][around*2].p[k] +
264 ovDEF[vfwd & XMASK][!!y][around*2].p[k]) / 2;
269 /*---------- output triangles ----------*/
271 static void outtriangles_around(int reverse, OutVertex *middle,
272 int nsurr, OutVertex *surround[nsurr]) {
273 /* Some entries in surround may be 0, in which case all affected
274 * triangles will be skipped */
276 for (i=0; i<nsurr; i++) {
277 OutVertex *s0= surround[i];
278 OutVertex *s1= surround[(i+1) % nsurr];
279 if (!s0 || !s1) continue;
280 outtriangle(reverse, middle,s0,s1);
284 static void outtriangles(void) {
288 OutVertex *defs=0, *defs1=0;
289 int (*defs1aroundmap)(int)=0, rimy;
290 if (RIM_VERTEX_P(v0)) {
292 rimy= !!(v0 & ~XMASK);
293 int v1= EDGE_END2(v0,0); assert(v1>=0);
294 gs= ovG [v0 & XMASK][rimy];
295 defs= ovDEF[v0 & XMASK][rimy];
296 defs1= ovDEF[v1 & XMASK][rimy];
297 defs1aroundmap= VERTICES_SPAN_JOIN_P(v0,v1)
298 ? defs_aroundmap_swap : int_identity_function;
300 for (aroung=0; aroung<NG; aroung++) {
301 int around= aroung*2;
302 OutVertex *surround[6];
303 for (e=0; e<3; e++) {
304 surround[e ]= &defs1[defs1aroundmap(around +e)];
305 surround[e+3]= &defs [ around+2-e ];
307 outtriangles_around(rimy, &gs[aroung], 6,surround);
313 int around= side ? NDEF-1 : 0;
315 OutVertex *ab= ovAB[v0][!rimy][side];
316 OutVertex *cd1= &defs1[defs1aroundmap(around)];
317 outtriangle(side^rimy,cd,ab,cd1);
323 abs[e0]= invertex2outvertexab(v0,e,side);
324 outtriangles_around(side, cd, 6,abs);
329 /*---------- transformation (scale and perhaps shift) ----------*/
331 static void scaleshift_outvertex_array(int n, OutVertex ovX[n]) {
333 K ovX[i].p[k] *= scale;
335 * double min[D3]= thick;
336 * if (ovX[i].p[k] < min)
338 * for (i=0; i<n; i++) {
339 * K ovX[k].p[k] -= min;
343 #define SCALESHIFT_OUTVERTEX_ARRAY(ovX) \
344 scaleshift_outvertex_array(sizeof((ovX))/sizeof(OutVertex),(OutVertex*)(ovX))
346 static void scaleshift_outvertices(void) {
347 SCALESHIFT_OUTVERTEX_ARRAY(ovAB);
348 SCALESHIFT_OUTVERTEX_ARRAY(ovC);
349 SCALESHIFT_OUTVERTEX_ARRAY(ovDEF);
350 SCALESHIFT_OUTVERTEX_ARRAY(ovG);
353 /*---------- output file ----------*/
355 static void wr(const void *p, size_t sz) {
356 if (fwrite(p,sz,1,stdout) != 1)
360 #define WR(x) wr((const void*)&(x), sizeof((x)))
362 static void wf(double d) {
364 typedef float ieee754single;
367 #if defined(BIG_ENDIAN)
368 union { Byte b[4]; ieee754single f; } value; value.f= d;
369 int i; for (i=3; i>=0; i--) WR(value.b[i]);
370 #elif defined(LITTLE_ENDIAN)
374 # error not little or big endian!
378 static uint32_t nouttriangles;
379 static uint32_t nouttriangles_counted;
381 static void outtriangle(int rev, OutVertex *a, OutVertex *b, OutVertex *c) {
382 if (rev) { outtriangle(0, c,b,a); return; }
384 if (!~nouttriangles_counted) return;
386 triangle_normal(normal, a.p, b.p, c.p);
387 double multby= 1/magnD(normal);
388 K normal[k] *= multby;
398 static void write_file(void) {
399 static const char header[80]= "#!/usr/bin/meshlab\n" "binary STL file\n";
401 if (isatty(stdout)) die("will not write binary stl to tty!");
405 nouttriangles_counted=~(uint32_t)0;
410 nouttriangles_counted= nouttriangles;
413 assert(nouttriangles == nouttriangles_counted);
415 if (fflush(stdout)) diee("fflush stdout");
419 /*---------- main program etc. ----------*/
421 int main(int argc, const char *const *argv) {
424 if (argc!=3 || argv[1][0]=='-') { fputs("bad usage\n",stderr); exit(8); }
425 thick= atof(argv[1]);
426 scale= atof(argv[2]) * 0.5; /* circle is unit radius but arg is diameter */
428 errno= 0; r= fread(&in,sizeof(in),1,stdin);
429 if (r!=1) diee("fread");
431 compute_outvertices();
432 scaleshift_outvertices();
434 if (fclose(stdout)) diee("fclose stdout");