1 /*****************************************************************************
2 * #ident "Id: main.c,v 3.27 2002-01-06 16:23:01+02 rl Exp "
5 * Kaleidoscopic construction of uniform polyhedra
6 * Copyright (c) 1991-2002 Dr. Zvi Har'El <rl@math.technion.ac.il>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by
23 * Dr. Zvi Har'El (http://www.math.technion.ac.il/~rl/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
27 * This software is provided 'as-is', without any express or implied
28 * warranty. In no event will the author be held liable for any
29 * damages arising from the use of this software.
33 * Deptartment of Mathematics,
34 * Technion, Israel Institue of Technology,
35 * Haifa 32000, Israel.
36 * E-Mail: rl@math.technion.ac.il
38 * ftp://ftp.math.technion.ac.il/kaleido/
39 * http://www.mathconsult.ch/showroom/unipoly/
41 * Adapted for xscreensaver by Jamie Zawinski <jwz@jwz.org> 25-Apr-2004
43 *****************************************************************************
57 #include "polyhedra.h"
59 extern const char *progname;
62 #define MAXLONG 0x7FFFFFFF
65 #define MAXDIGITS 10 /* (int)log10((double)MAXLONG) + 1 */
69 #define DBL_EPSILON 2.2204460492503131e-16
71 #define BIG_EPSILON 3e-2
72 #define AZ M_PI/7 /* axis azimuth */
73 #define EL M_PI/17 /* axis elevation */
76 fprintf (stderr, "%s: %s\n", progname, (x)); \
80 #define Free(lvalue) do {\
82 free((char*) lvalue);\
87 #define Matfree(lvalue,n) do {\
89 matfree((char*) lvalue, n);\
93 #define Malloc(lvalue,n,type) do {\
94 if (!(lvalue = (type*) calloc((n), sizeof(type)))) \
98 #define Realloc(lvalue,n,type) do {\
99 if (!(lvalue = (type*) realloc(lvalue, (n) * sizeof(type)))) \
103 #define Calloc(lvalue,n,type) do {\
104 if (!(lvalue = (type*) calloc(n, sizeof(type))))\
108 #define Matalloc(lvalue,n,m,type) do {\
109 if (!(lvalue = (type**) matalloc(n, (m) * sizeof(type))))\
113 #define Sprintfrac(lvalue,x) do {\
114 if (!(lvalue=sprintfrac(x)))\
118 #define numerator(x) (frac(x), frax.n)
119 #define denominator(x) (frac(x), frax.d)
120 #define compl(x) (frac(x), (double) frax.n / (frax.n-frax.d))
127 /* NOTE: some of the int's can be replaced by short's, char's,
128 or even bit fields, at the expense of readability!!!*/
129 int index; /* index to the standard list, the array uniform[] */
130 int N; /* number of faces types (atmost 5)*/
131 int M; /* vertex valency (may be big for dihedral polyhedra) */
132 int V; /* vertex count */
133 int E; /* edge count */
134 int F; /* face count */
136 int chi; /* Euler characteristic */
137 int g; /* order of symmetry group */
138 int K; /* symmetry type: D=2, T=3, O=4, I=5 */
139 int hemi;/* flag hemi polyhedron */
140 int onesided;/* flag onesided polyhedron */
141 int even; /* removed face in pqr| */
142 int *Fi; /* face counts by type (array N)*/
143 int *rot; /* vertex configuration (array M of 0..N-1) */
144 int *snub; /* snub triangle configuration (array M of 0..1) */
145 int *firstrot; /* temporary for vertex generation (array V) */
146 int *anti; /* temporary for direction of ideal vertices (array E) */
147 int *ftype; /* face types (array F) */
148 int **e; /* edges (matrix 2 x E of 0..V-1)*/
149 int **dual_e; /* dual edges (matrix 2 x E of 0..F-1)*/
150 int **incid; /* vertex-face incidence (matrix M x V of 0..F-1)*/
151 int **adj; /* vertex-vertex adjacency (matrix M x V of 0..V-1)*/
152 double p[4]; /* p, q and r; |=0 */
153 double minr; /* smallest nonzero inradius */
154 double gon; /* basis type for dihedral polyhedra */
155 double *n; /* number of side of a face of each type (array N) */
156 double *m; /* number of faces at a vertex of each type (array N) */
157 double *gamma; /* fundamental angles in radians (array N) */
158 char *polyform; /* printable Wythoff symbol */
159 char *config; /* printable vertex configuration */
160 char *group; /* printable group name */
161 char *name; /* name, standard or manifuctured */
162 char *dual_name; /* dual name, standard or manifuctured */
165 Vector *v; /* vertex coordinates (array V) */
166 Vector *f; /* face coordinates (array F)*/
173 static Polyhedron *polyalloc(void);
174 static Vector rotate(Vector vertex, Vector axis, double angle);
176 static Vector sum3(Vector a, Vector b, Vector c);
177 static Vector scale(double k, Vector a);
178 static Vector sum(Vector a, Vector b);
179 static Vector diff(Vector a, Vector b);
180 static Vector pole (double r, Vector a, Vector b, Vector c);
181 static Vector cross(Vector a, Vector b);
182 static double dot(Vector a, Vector b);
183 static int same(Vector a, Vector b, double epsilon);
185 static char *sprintfrac(double x);
187 static void frac(double x);
188 static void matfree(void *mat, int rows);
189 static void *matalloc(int rows, int row_size);
191 static Fraction frax;
194 static const struct {
195 char *Wythoff, *name, *dual, *group, *class, *dual_class;
196 short Coxeter, Wenninger;
199 /****************************************************************************
200 * Dihedral Schwarz Triangles (D5 only)
201 ***************************************************************************/
203 /* {"3|2 5/2", "xyz",
214 /* 1 */ {"2 5|2", "Pentagonal Prism",
215 "Pentagonal Dipyramid",
221 /* 2 */ {"|2 2 5", "Pentagonal Antiprism",
222 "Pentagonal Deltohedron",
227 /* (2 2 5/2) (D2/5) */
228 /* 3 */ {"2 5/2|2", "Pentagrammic Prism",
229 "Pentagrammic Dipyramid",
235 /* 4 */ {"|2 2 5/2", "Pentagrammic Antiprism",
236 "Pentagrammic Deltohedron",
241 /* (5/3 2 2) (D3/5) */
243 /* 5 */ {"|2 2 5/3", "Pentagrammic Crossed Antiprism",
244 "Pentagrammic Concave Deltohedron",
250 /****************************************************************************
252 ***************************************************************************/
255 /* 6 */ {"3|2 3", "Tetrahedron",
257 "Tetrahedral (T[1])",
262 /* 7 */ {"2 3|3", "Truncated Tetrahedron",
263 "Triakistetrahedron",
264 "Tetrahedral (T[1])",
269 /* 8 */ {"3/2 3|3", "Octahemioctahedron",
271 "Tetrahedral (T[2])",
277 /* 9 */ {"3/2 3|2", "Tetrahemihexahedron",
279 "Tetrahedral (T[3])",
284 /****************************************************************************
286 ***************************************************************************/
289 /* 10 */ {"4|2 3", "Octahedron",
296 /* 11 */ {"3|2 4", "Cube",
303 /* 12 */ {"2|3 4", "Cuboctahedron",
304 "Rhombic Dodecahedron",
310 /* 13 */ {"2 4|3", "Truncated Octahedron",
311 "Tetrakishexahedron",
317 /* 14 */ {"2 3|4", "Truncated Cube",
324 /* 15 */ {"3 4|2", "Rhombicuboctahedron",
325 "Deltoidal Icositetrahedron",
331 /* 16 */ {"2 3 4|", "Truncated Cuboctahedron",
332 "Disdyakisdodecahedron",
338 /* 17 */ {"|2 3 4", "Snub Cube",
339 "Pentagonal Icositetrahedron",
344 /* (3/2 4 4) (O2b) */
346 /* 18 */ {"3/2 4|4", "Small Cubicuboctahedron",
347 "Small Hexacronic Icositetrahedron",
348 "Octahedral (O[2b])",
354 /* 19 */ {"3 4|4/3", "Great Cubicuboctahedron",
355 "Great Hexacronic Icositetrahedron",
361 /* 20 */ {"4/3 4|3", "Cubohemioctahedron",
368 /* 21 */ {"4/3 3 4|", "Cubitruncated Cuboctahedron",
369 "Tetradyakishexahedron",
376 /* 22 */ {"3/2 4|2", "Great Rhombicuboctahedron",
377 "Great Deltoidal Icositetrahedron",
383 /* 23 */ {"3/2 2 4|", "Small Rhombihexahedron",
384 "Small Rhombihexacron",
391 /* 24 */ {"2 3|4/3", "Stellated Truncated Hexahedron",
392 "Great Triakisoctahedron",
398 /* 25 */ {"4/3 2 3|", "Great Truncated Cuboctahedron",
399 "Great Disdyakisdodecahedron",
404 /* (4/3 3/2 2) (O11) */
406 /* 26 */ {"4/3 3/2 2|", "Great Rhombihexahedron",
407 "Great Rhombihexacron",
408 "Octahedral (O[11])",
413 /****************************************************************************
415 ***************************************************************************/
418 /* 27 */ {"5|2 3", "Icosahedron",
420 "Icosahedral (I[1])",
425 /* 28 */ {"3|2 5", "Dodecahedron",
427 "Icosahedral (I[1])",
432 /* 29 */ {"2|3 5", "Icosidodecahedron",
433 "Rhombic Triacontahedron",
434 "Icosahedral (I[1])",
439 /* 30 */ {"2 5|3", "Truncated Icosahedron",
440 "Pentakisdodecahedron",
441 "Icosahedral (I[1])",
446 /* 31 */ {"2 3|5", "Truncated Dodecahedron",
447 "Triakisicosahedron",
448 "Icosahedral (I[1])",
453 /* 32 */ {"3 5|2", "Rhombicosidodecahedron",
454 "Deltoidal Hexecontahedron",
455 "Icosahedral (I[1])",
460 /* 33 */ {"2 3 5|", "Truncated Icosidodecahedron",
461 "Disdyakistriacontahedron",
462 "Icosahedral (I[1])",
467 /* 34 */ {"|2 3 5", "Snub Dodecahedron",
468 "Pentagonal Hexecontahedron",
469 "Icosahedral (I[1])",
473 /* (5/2 3 3) (I2a) */
475 /* 35 */ {"3|5/2 3", "Small Ditrigonal Icosidodecahedron",
476 "Small Triambic Icosahedron",
477 "Icosahedral (I[2a])",
482 /* 36 */ {"5/2 3|3", "Small Icosicosidodecahedron",
483 "Small Icosacronic Hexecontahedron",
484 "Icosahedral (I[2a])",
489 /* 37 */ {"|5/2 3 3", "Small Snub Icosicosidodecahedron",
490 "Small Hexagonal Hexecontahedron",
491 "Icosahedral (I[2a])",
495 /* (3/2 5 5) (I2b) */
497 /* 38 */ {"3/2 5|5", "Small Dodecicosidodecahedron",
498 "Small Dodecacronic Hexecontahedron",
499 "Icosahedral (I[2b])",
505 /* 39 */ {"5|2 5/2", "Small Stellated Dodecahedron",
506 "Great Dodecahedron",
507 "Icosahedral (I[3])",
508 "Truncated Kepler-Poinsot Solid",
512 /* 40 */ {"5/2|2 5", "Great Dodecahedron",
513 "Small Stellated Dodecahedron",
514 "Icosahedral (I[3])",
519 /* 41 */ {"2|5/2 5", "Great Dodecadodecahedron",
520 "Medial Rhombic Triacontahedron",
521 "Icosahedral (I[3])",
526 /* 42 */ {"2 5/2|5", "Truncated Great Dodecahedron",
527 "Small Stellapentakisdodecahedron",
528 "Icosahedral (I[3])",
529 "Truncated Kepler-Poinsot Solid",
533 /* 43 */ {"5/2 5|2", "Rhombidodecadodecahedron",
534 "Medial Deltoidal Hexecontahedron",
535 "Icosahedral (I[3])",
540 /* 44 */ {"2 5/2 5|", "Small Rhombidodecahedron",
541 "Small Rhombidodecacron",
542 "Icosahedral (I[3])",
547 /* 45 */ {"|2 5/2 5", "Snub Dodecadodecahedron",
548 "Medial Pentagonal Hexecontahedron",
549 "Icosahedral (I[3])",
555 /* 46 */ {"3|5/3 5", "Ditrigonal Dodecadodecahedron",
556 "Medial Triambic Icosahedron",
557 "Icosahedral (I[4])",
562 /* 47 */ {"3 5|5/3", "Great Ditrigonal Dodecicosidodecahedron",
563 "Great Ditrigonal Dodecacronic Hexecontahedron",
564 "Icosahedral (I[4])",
569 /* 48 */ {"5/3 3|5", "Small Ditrigonal Dodecicosidodecahedron",
570 "Small Ditrigonal Dodecacronic Hexecontahedron",
571 "Icosahedral (I[4])",
576 /* 49 */ {"5/3 5|3", "Icosidodecadodecahedron",
577 "Medial Icosacronic Hexecontahedron",
578 "Icosahedral (I[4])",
583 /* 50 */ {"5/3 3 5|", "Icositruncated Dodecadodecahedron",
584 "Tridyakisicosahedron",
585 "Icosahedral (I[4])",
590 /* 51 */ {"|5/3 3 5", "Snub Icosidodecadodecahedron",
591 "Medial Hexagonal Hexecontahedron",
592 "Icosahedral (I[4])",
596 /* (3/2 3 5) (I6b) */
598 /* 52 */ {"3/2|3 5", "Great Ditrigonal Icosidodecahedron",
599 "Great Triambic Icosahedron",
600 "Icosahedral (I[6b])",
605 /* 53 */ {"3/2 5|3", "Great Icosicosidodecahedron",
606 "Great Icosacronic Hexecontahedron",
607 "Icosahedral (I[6b])",
612 /* 54 */ {"3/2 3|5", "Small Icosihemidodecahedron",
613 "Small Icosihemidodecacron",
614 "Icosahedral (I[6b])",
619 /* 55 */ {"3/2 3 5|", "Small Dodecicosahedron",
620 "Small Dodecicosacron",
621 "Icosahedral (I[6b])",
625 /* (5/4 5 5) (I6c) */
627 /* 56 */ {"5/4 5|5", "Small Dodecahemidodecahedron",
628 "Small Dodecahemidodecacron",
629 "Icosahedral (I[6c])",
635 /* 57 */ {"3|2 5/2", "Great Stellated Dodecahedron",
637 "Icosahedral (I[7])",
642 /* 58 */ {"5/2|2 3", "Great Icosahedron",
643 "Great Stellated Dodecahedron",
644 "Icosahedral (I[7])",
649 /* 59 */ {"2|5/2 3", "Great Icosidodecahedron",
650 "Great Rhombic Triacontahedron",
651 "Icosahedral (I[7])",
652 "Truncated Kepler-Poinsot Solid",
656 /* 60 */ {"2 5/2|3", "Great Truncated Icosahedron",
657 "Great Stellapentakisdodecahedron",
658 "Icosahedral (I[7])",
659 "Truncated Kepler-Poinsot Solid",
663 /* 61 */ {"2 5/2 3|", "Rhombicosahedron",
665 "Icosahedral (I[7])",
670 /* 62 */ {"|2 5/2 3", "Great Snub Icosidodecahedron",
671 "Great Pentagonal Hexecontahedron",
672 "Icosahedral (I[7])",
678 /* 63 */ {"2 5|5/3", "Small Stellated Truncated Dodecahedron",
679 "Great Pentakisdodekahedron",
680 "Icosahedral (I[9])",
685 /* 64 */ {"5/3 2 5|", "Truncated Dodecadodecahedron",
686 "Medial Disdyakistriacontahedron",
687 "Icosahedral (I[9])",
692 /* 65 */ {"|5/3 2 5", "Inverted Snub Dodecadodecahedron",
693 "Medial Inverted Pentagonal Hexecontahedron",
694 "Icosahedral (I[9])",
698 /* (5/3 5/2 3) (I10a) */
700 /* 66 */ {"5/2 3|5/3", "Great Dodecicosidodecahedron",
701 "Great Dodecacronic Hexecontahedron",
702 "Icosahedral (I[10a])",
707 /* 67 */ {"5/3 5/2|3", "Small Dodecahemicosahedron",
708 "Small Dodecahemicosacron",
709 "Icosahedral (I[10a])",
714 /* 68 */ {"5/3 5/2 3|", "Great Dodecicosahedron",
715 "Great Dodecicosacron",
716 "Icosahedral (I[10a])",
721 /* 69 */ {"|5/3 5/2 3", "Great Snub Dodecicosidodecahedron",
722 "Great Hexagonal Hexecontahedron",
723 "Icosahedral (I[10a])",
727 /* (5/4 3 5) (I10b) */
729 /* 70 */ {"5/4 5|3", "Great Dodecahemicosahedron",
730 "Great Dodecahemicosacron",
731 "Icosahedral (I[10b])",
735 /* (5/3 2 3) (I13) */
737 /* 71 */ {"2 3|5/3", "Great Stellated Truncated Dodecahedron",
738 "Great Triakisicosahedron",
739 "Icosahedral (I[13])",
744 /* 72 */ {"5/3 3|2", "Great Rhombicosidodecahedron",
745 "Great Deltoidal Hexecontahedron",
746 "Icosahedral (I[13])",
751 /* 73 */ {"5/3 2 3|", "Great Truncated Icosidodecahedron",
752 "Great Disdyakistriacontahedron",
753 "Icosahedral (I[13])",
758 /* 74 */ {"|5/3 2 3", "Great Inverted Snub Icosidodecahedron",
759 "Great Inverted Pentagonal Hexecontahedron",
760 "Icosahedral (I[13])",
764 /* (5/3 5/3 5/2) (I18a) */
766 /* 75 */ {"5/3 5/2|5/3", "Great Dodecahemidodecahedron",
767 "Great Dodecahemidodecacron",
768 "Icosahedral (I[18a])",
772 /* (3/2 5/3 3) (I18b) */
774 /* 76 */ {"3/2 3|5/3", "Great Icosihemidodecahedron",
775 "Great Icosihemidodecacron",
776 "Icosahedral (I[18b])",
780 /* (3/2 3/2 5/3) (I22) */
782 /* 77 */ {"|3/2 3/2 5/2","Small Retrosnub Icosicosidodecahedron",
783 "Small Hexagrammic Hexecontahedron",
784 "Icosahedral (I[22])",
788 /* (3/2 5/3 2) (I23) */
790 /* 78 */ {"3/2 5/3 2|", "Great Rhombidodecahedron",
791 "Great Rhombidodecacron",
792 "Icosahedral (I[23])",
797 /* 79 */ {"|3/2 5/3 2", "Great Retrosnub Icosidodecahedron",
798 "Great Pentagrammic Hexecontahedron",
799 "Icosahedral (I[23])",
804 /****************************************************************************
806 ***************************************************************************/
808 /* 80 */ {"3/2 5/3 3 5/2", "Great Dirhombicosidodecahedron",
809 "Great Dirhombicosidodecacron",
816 static int last_uniform = sizeof (uniform) / sizeof (uniform[0]);
820 static int unpacksym(char *sym, Polyhedron *P);
821 static int moebius(Polyhedron *P);
822 static int decompose(Polyhedron *P);
823 static int guessname(Polyhedron *P);
824 static int newton(Polyhedron *P, int need_approx);
825 static int exceptions(Polyhedron *P);
826 static int count(Polyhedron *P);
827 static int configuration(Polyhedron *P);
828 static int vertices(Polyhedron *P);
829 static int faces(Polyhedron *P);
830 static int edgelist(Polyhedron *P);
833 kaleido(char *sym, int need_coordinates, int need_edgelist, int need_approx,
838 * Allocate a Polyhedron structure P.
840 if (!(P = polyalloc()))
843 * Unpack input symbol into P.
845 if (!unpacksym(sym, P))
848 * Find Mebius triangle, its density and Euler characteristic.
853 * Decompose Schwarz triangle.
858 * Find the names of the polyhedron and its dual.
865 * Solve Fundamental triangles, optionally printing approximations.
867 if (!newton(P,need_approx))
870 * Deal with exceptional polyhedra.
875 * Count edges and faces, update density and characteristic if needed.
880 * Generate printable vertex configuration.
882 if (!configuration(P))
885 * Compute coordinates.
887 if (!need_coordinates && !need_edgelist)
904 * Allocate a blank Polyhedron structure and initialize some of its nonblank
907 * Array and matrix field are allocated when needed.
913 Calloc(P, 1, Polyhedron);
921 * Free the struture allocated by polyalloc(), as well as all the array and
925 polyfree(Polyhedron *P)
945 Matfree(P->dual_e, 2);
946 Matfree(P->incid, P->M);
947 Matfree(P->adj, P->M);
952 matalloc(int rows, int row_size)
956 if (!(mat = malloc(rows * sizeof (void *))))
958 while ((mat[i] = malloc(row_size)) && ++i < rows)
969 matfree(void *mat, int rows)
972 free(((void **)mat)[rows]);
977 * compute the mathematical modulus function.
982 return (i%=j)>=0?i:j<0?i-j:i+j;
987 * Find the numerator and the denominator using the Euclidean algorithm.
992 static const Fraction zero = {0,1}, inf = {1,0};
999 if (fabs(s) > (double) MAXLONG)
1001 f = (long) floor (s);
1004 frax.n = frax.n * f + r0.n;
1005 frax.d = frax.d * f + r0.d;
1006 if (x == (double)frax.n/(double)frax.d)
1014 * Unpack input symbol: Wythoff symbol or an index to uniform[]. The symbol is
1015 * a # followed by a number, or a three fractions and a bar in some order. We
1016 * allow no bars only if it result from the input symbol #80.
1019 unpacksym(char *sym, Polyhedron *P)
1021 int i = 0, n, d, bars = 0;
1023 while ((c = *sym++) && isspace(c))
1025 if (!c) Err("no data");
1027 while ((c = *sym++) && isspace(c))
1030 Err("no digit after #");
1034 while ((c = *sym++) && isdigit(c))
1035 n = n * 10 + c - '0';
1038 if (n > last_uniform)
1039 Err("index too big");
1041 while ((c = *sym++) && isspace(c))
1044 Err("data exceeded");
1045 sym = uniform[P->index = n - 1].Wythoff;
1050 while ((c = *sym++) && isspace(c))
1053 if (i == 4 && (bars || P->index == last_uniform - 1))
1057 Err("not enough fractions");
1060 Err("data exceeded");
1063 Err("too many bars");
1070 while ((c = *sym++) && isdigit(c))
1071 n = n * 10 + c - '0';
1072 if (c && isspace (c))
1073 while ((c = *sym++) && isspace(c))
1077 if ((P->p[i++] = n) <= 1)
1081 while ((c = *sym++) && isspace(c))
1083 if (!c || !isdigit(c))
1086 while ((c = *sym++) && isdigit(c))
1087 d = d * 10 + c - '0';
1089 Err("zero denominator");
1091 if ((P->p[i++] = (double) n / d) <= 1)
1097 * Using Wythoff symbol (p|qr, pq|r, pqr| or |pqr), find the Moebius triangle
1098 * (2 3 K) (or (2 2 n)) of the Schwarz triangle (pqr), the order g of its
1099 * symmetry group, its Euler characteristic chi, and its covering density D.
1100 * g is the number of copies of (2 3 K) covering the sphere, i.e.,
1102 * g * pi * (1/2 + 1/3 + 1/K - 1) = 4 * pi
1104 * D is the number of times g copies of (pqr) cover the sphere, i.e.
1106 * D * 4 * pi = g * pi * (1/p + 1/q + 1/r - 1)
1108 * chi is V - E + F, where F = g is the number of triangles, E = 3*g/2 is the
1109 * number of triangle edges, and V = Vp+ Vq+ Vr, with Vp = g/(2*np) being the
1110 * number of vertices with angle pi/p (np is the numerator of p).
1113 moebius(Polyhedron *P)
1115 int twos = 0, j, len = 1;
1117 * Arrange Wythoff symbol in a presentable form. In the same time check the
1118 * restrictions on the three fractions: They all have to be greater then one,
1119 * and the numerators 4 or 5 cannot occur together. We count the ocurrences
1120 * of 2 in `two', and save the largest numerator in `P->K', since they
1121 * reflect on the symmetry group.
1124 if (P->index == last_uniform - 1) {
1125 Malloc(P->polyform, ++len, char);
1126 strcpy(P->polyform, "|");
1128 Calloc(P->polyform, len, char);
1129 for (j = 0; j < 4; j++) {
1132 Sprintfrac(s, P->p[j]);
1133 if (j && P->p[j-1]) {
1134 Realloc(P->polyform, len += strlen (s) + 1, char);
1135 strcat(P->polyform, " ");
1137 Realloc (P->polyform, len += strlen (s), char);
1138 strcat(P->polyform, s);
1142 if ((k = numerator (P->p[j])) > P->K) {
1146 } else if (k < P->K && k == 4)
1151 Realloc(P->polyform, ++len, char);
1152 strcat(P->polyform, "|");
1156 * Find the symmetry group P->K (where 2, 3, 4, 5 represent the dihedral,
1157 * tetrahedral, octahedral and icosahedral groups, respectively), and its
1160 if (twos >= 2) {/* dihedral */
1165 Err("numerator too large");
1166 P->g = 24 * P->K / (6 - P->K);
1169 * Compute the nominal density P->D and Euler characteristic P->chi.
1170 * In few exceptional cases, these values will be modified later.
1172 if (P->index != last_uniform - 1) {
1174 P->D = P->chi = - P->g;
1175 for (j = 0; j < 4; j++) if (P->p[j]) {
1176 P->chi += i = P->g / numerator(P->p[j]);
1177 P->D += i * denominator(P->p[j]);
1182 Err("nonpositive density");
1188 * Decompose Schwarz triangle into N right triangles and compute the vertex
1189 * count V and the vertex valency M. V is computed from the number g of
1190 * Schwarz triangles in the cover, divided by the number of triangles which
1191 * share a vertex. It is halved for one-sided polyhedra, because the
1192 * kaleidoscopic construction really produces a double orientable covering of
1193 * such polyhedra. All q' q|r are of the "hemi" type, i.e. have equatorial {2r}
1194 * faces, and therefore are (except 3/2 3|3 and the dihedra 2 2|r) one-sided. A
1195 * well known example is 3/2 3|4, the "one-sided heptahedron". Also, all p q r|
1196 * with one even denominator have a crossed parallelogram as a vertex figure,
1197 * and thus are one-sided as well.
1200 decompose(Polyhedron *P)
1203 if (!P->p[1]) { /* p|q r */
1205 P->M = 2 * numerator(P->p[0]);
1207 Malloc(P->n, P->N, double);
1208 Malloc(P->m, P->N, double);
1209 Malloc(P->rot, P->M, int);
1211 for (j = 0; j < 2; j++) {
1212 P->n[j] = P->p[j+2];
1215 for (j = P->M / 2; j--;) {
1219 } else if (!P->p[2]) { /* p q|r */
1223 Malloc(P->n, P->N, double);
1224 Malloc(P->m, P->N, double);
1225 Malloc(P->rot, P->M, int);
1227 P->n[0] = 2 * P->p[3];
1229 for (j = 1; j < 3; j++) {
1230 P->n[j] = P->p[j-1];
1235 if (fabs(P->p[0] - compl (P->p[1])) < DBL_EPSILON) {/* p = q' */
1236 /* P->p[0]==compl(P->p[1]) should work. However, MSDOS
1237 * yeilds a 7e-17 difference! Reported by Jim Buddenhagen
1238 * <jb1556@daditz.sbc.com> */
1241 if (P->p[0] != 2 && !(P->p[3] == 3 && (P->p[0] == 3 ||
1248 } else if (!P->p[3]) { /* p q r| */
1251 Malloc(P->n, P->N, double);
1252 Malloc(P->m, P->N, double);
1253 Malloc(P->rot, P->M, int);
1255 for (j = 0; j < 3; j++) {
1256 if (!(denominator(P->p[j]) % 2)) {
1257 /* what happens if there is more then one even denominator? */
1258 if (P->p[(j+1)%3] != P->p[(j+2)%3]) { /* needs postprocessing */
1259 P->even = j;/* memorize the removed face */
1260 P->chi -= P->g / numerator(P->p[j]) / 2;
1263 } else {/* for p = q we get a double 2 2r|p */
1264 /* noted by Roman Maeder <maeder@inf.ethz.ch> for 4 4 3/2| */
1265 /* Euler characteristic is still wrong */
1270 P->n[j] = 2 * P->p[j];
1274 } else { /* |p q r - snub polyhedron */
1277 P->V = P->g / 2;/* Only "white" triangles carry a vertex */
1278 Malloc(P->n, P->N, double);
1279 Malloc(P->m, P->N, double);
1280 Malloc(P->rot, P->M, int);
1281 Malloc(P->snub, P->M, int);
1284 P->m[0] = P->n[0] = 3;
1285 for (j = 1; j < 4; j++) {
1295 * Sort the fundamental triangles (using bubble sort) according to decreasing
1296 * n[i], while pushing the trivial triangles (n[i] = 2) to the end.
1303 for (j = 0; j < last; j++) {
1304 if ((P->n[j] < P->n[j+1] || P->n[j] == 2) && P->n[j+1] != 2) {
1308 P->n[j] = P->n[j+1];
1311 P->m[j] = P->m[j+1];
1313 for (i = 0; i < P->M; i++) {
1316 else if (P->rot[i] == j+1)
1319 if (P->even != -1) {
1322 else if (P->even == j+1)
1330 * Get rid of repeated triangles.
1332 for (J = 0; J < P->N && P->n[J] != 2;J++) {
1334 for (j = J+1; j < P->N && P->n[j]==P->n[J]; j++)
1338 for (i = j; i < P->N; i++) {
1339 P->n[i - k] = P->n[i];
1340 P->m[i - k] = P->m[i];
1343 for (i = 0; i < P->M; i++) {
1346 else if (P->rot[i] > J)
1354 * Get rid of trivial triangles.
1357 J = 1; /* hosohedron */
1361 for (i = 0; i < P->M; i++) {
1362 if (P->rot[i] >= P->N) {
1363 for (j = i + 1; j < P->M; j++) {
1364 P->rot[j-1] = P->rot[j];
1366 P->snub[j-1] = P->snub[j];
1375 Realloc(P->n, P->N, double);
1376 Realloc(P->m, P->N, double);
1377 Realloc(P->rot, P->M, int);
1379 Realloc(P->snub, P->M, int);
1384 static int dihedral(Polyhedron *P, char *name, char *dual_name);
1388 * Get the polyhedron name, using standard list or guesswork. Ideally, we
1389 * should try to locate the Wythoff symbol in the standard list (unless, of
1390 * course, it is dihedral), after doing few normalizations, such as sorting
1391 * angles and splitting isoceles triangles.
1394 guessname(Polyhedron *P)
1396 if (P->index != -1) {/* tabulated */
1397 P->name = uniform[P->index].name;
1398 P->dual_name = uniform[P->index].dual;
1399 P->group = uniform[P->index].group;
1400 P->class = uniform[P->index].class;
1401 P->dual_class = uniform[P->index].dual_class;
1403 } else if (P->K == 2) {/* dihedral nontabulated */
1406 Malloc(P->name, sizeof ("Octahedron"), char);
1407 Malloc(P->dual_name, sizeof ("Cube"), char);
1408 strcpy(P->name, "Octahedron");
1409 strcpy(P->dual_name, "Cube");
1412 P->gon = P->n[0] == 3 ? P->n[1] : P->n[0];
1414 return dihedral(P, "Antiprism", "Deltohedron");
1416 return dihedral(P, "Crossed Antiprism", "Concave Deltohedron");
1417 } else if (!P->p[3] ||
1421 Malloc(P->name, sizeof("Cube"), char);
1422 Malloc(P->dual_name, sizeof("Octahedron"), char);
1423 strcpy(P->name, "Cube");
1424 strcpy(P->dual_name, "Octahedron");
1427 P->gon = P->n[0] == 4 ? P->n[1] : P->n[0];
1428 return dihedral(P, "Prism", "Dipyramid");
1429 } else if (!P->p[1] && P->p[0] != 2) {
1431 return dihedral(P, "Hosohedron", "Dihedron");
1434 return dihedral(P, "Dihedron", "Hosohedron");
1436 } else {/* other nontabulated */
1437 static const char *pre[] = {"Tetr", "Oct", "Icos"};
1438 Malloc(P->name, 50, char);
1439 Malloc(P->dual_name, 50, char);
1440 sprintf(P->name, "%sahedral ", pre[P->K - 3]);
1442 strcat (P->name, "One-Sided ");
1444 strcat(P->name, "Convex ");
1446 strcat(P->name, "Nonconvex ");
1447 strcpy(P->dual_name, P->name);
1448 strcat(P->name, "Isogonal Polyhedron");
1449 strcat(P->dual_name, "Isohedral Polyhedron");
1450 Realloc(P->name, strlen (P->name) + 1, char);
1451 Realloc(P->dual_name, strlen (P->dual_name) + 1, char);
1457 dihedral(Polyhedron *P, char *name, char *dual_name)
1461 Sprintfrac(s, P->gon < 2 ? compl (P->gon) : P->gon);
1462 i = strlen(s) + sizeof ("-gonal ");
1463 Malloc(P->name, i + strlen (name), char);
1464 Malloc(P->dual_name, i + strlen (dual_name), char);
1465 sprintf(P->name, "%s-gonal %s", s, name);
1466 sprintf(P->dual_name, "%s-gonal %s", s, dual_name);
1472 * Solve the fundamental right spherical triangles.
1473 * If need_approx is set, print iterations on standard error.
1476 newton(Polyhedron *P, int need_approx)
1479 * First, we find initial approximations.
1483 Malloc(P->gamma, P->N, double);
1485 P->gamma[0] = M_PI / P->m[0];
1488 for (j = 0; j < P->N; j++)
1489 P->gamma[j] = M_PI / 2 - M_PI / P->n[j];
1490 errno = 0; /* may be non-zero from some reason */
1492 * Next, iteratively find closer approximations for gamma[0] and compute
1493 * other gamma[j]'s from Napier's equations.
1496 fprintf(stderr, "Solving %s\n", P->polyform);
1498 double delta = M_PI, sigma = 0;
1499 for (j = 0; j < P->N; j++) {
1501 fprintf(stderr, "%-20.15f", P->gamma[j]);
1502 delta -= P->m[j] * P->gamma[j];
1505 printf("(%g)\n", delta);
1506 if (fabs(delta) < 11 * DBL_EPSILON)
1508 /* On a RS/6000, fabs(delta)/DBL_EPSILON may occilate between 8 and
1509 * 10. Reported by David W. Sanderson <dws@ssec.wisc.edu> */
1510 for (j = 0; j < P->N; j++)
1511 sigma += P->m[j] * tan(P->gamma[j]);
1512 P->gamma[0] += delta * tan(P->gamma[0]) / sigma;
1513 if (P->gamma[0] < 0 || P->gamma[0] > M_PI)
1514 Err("gamma out of bounds");
1515 cosa = cos(M_PI / P->n[0]) / sin(P->gamma[0]);
1516 for (j = 1; j < P->N; j++)
1517 P->gamma[j] = asin(cos(M_PI / P->n[j]) / cosa);
1519 Err(strerror(errno));
1524 * Postprocess pqr| where r has an even denominator (cf. Coxeter &al. Sec.9).
1525 * Remove the {2r} and add a retrograde {2p} and retrograde {2q}.
1528 exceptions(Polyhedron *P)
1531 if (P->even != -1) {
1533 Realloc(P->n, P->N, double);
1534 Realloc(P->m, P->N, double);
1535 Realloc(P->gamma, P->N, double);
1536 Realloc(P->rot, P->M, int);
1537 for (j = P->even + 1; j < 3; j++) {
1538 P->n[j-1] = P->n[j];
1539 P->gamma[j-1] = P->gamma[j];
1541 P->n[2] = compl(P->n[1]);
1542 P->gamma[2] = - P->gamma[1];
1543 P->n[3] = compl(P->n[0]);
1545 P->gamma[3] = - P->gamma[0];
1553 * Postprocess the last polyhedron |3/2 5/3 3 5/2 by taking a |5/3 3 5/2,
1554 * replacing the three snub triangles by four equatorial squares and adding
1555 * the missing {3/2} (retrograde triangle, cf. Coxeter &al. Sec. 11).
1557 if (P->index == last_uniform - 1) {
1560 Realloc(P->n, P->N, double);
1561 Realloc(P->m, P->N, double);
1562 Realloc(P->gamma, P->N, double);
1563 Realloc(P->rot, P->M, int);
1564 Realloc(P->snub, P->M, int);
1567 for (j = 3; j; j--) {
1569 P->n[j] = P->n[j-1];
1570 P->gamma[j] = P->gamma[j-1];
1572 P->m[0] = P->n[0] = 4;
1573 P->gamma[0] = M_PI / 2;
1575 P->n[4] = compl(P->n[1]);
1576 P->gamma[4] = - P->gamma[1];
1577 for (j = 1; j < 6; j += 2) P->rot[j]++;
1587 * Compute edge and face counts, and update D and chi. Update D in the few
1588 * cases the density of the polyhedron is meaningful but different than the
1589 * density of the corresponding Schwarz triangle (cf. Coxeter &al., p. 418 and
1591 * In these cases, spherical faces of one type are concave (bigger than a
1592 * hemisphere), and the actual density is the number of these faces less the
1593 * computed density. Note that if j != 0, the assignment gamma[j] = asin(...)
1594 * implies gamma[j] cannot be obtuse. Also, compute chi for the only
1595 * non-Wythoffian polyhedron.
1598 count(Polyhedron *P)
1601 Malloc(P->Fi, P->N, int);
1602 for (j = 0; j < P->N; j++) {
1603 P->E += temp = P->V * numerator(P->m[j]);
1604 P->F += P->Fi[j] = temp / numerator(P->n[j]);
1607 if (P->D && P->gamma[0] > M_PI / 2)
1608 P->D = P->Fi[0] - P->D;
1609 if (P->index == last_uniform - 1)
1610 P->chi = P->V - P->E + P->F;
1615 * Generate a printable vertex configuration symbol.
1618 configuration(Polyhedron *P)
1621 for (j = 0; j < P->M; j++) {
1623 Sprintfrac(s, P->n[P->rot[j]]);
1624 len += strlen (s) + 2;
1626 Malloc(P->config, len, char);
1627 /* strcpy(P->config, "(");*/
1628 strcpy(P->config, "");
1630 Realloc(P->config, len, char);
1631 strcat(P->config, ", ");
1633 strcat(P->config, s);
1636 /* strcat (P->config, ")");*/
1637 if ((j = denominator (P->m[0])) != 1) {
1638 char s[MAXDIGITS + 2];
1639 sprintf(s, "/%d", j);
1640 Realloc(P->config, len + strlen (s), char);
1641 strcat(P->config, s);
1647 * Compute polyhedron vertices and vertex adjecency lists.
1648 * The vertices adjacent to v[i] are v[adj[0][i], v[adj[1][i], ...
1649 * v[adj[M-1][i], ordered counterclockwise. The algorith is a BFS on the
1650 * vertices, in such a way that the vetices adjacent to a givem vertex are
1651 * obtained from its BFS parent by a cyclic sequence of rotations. firstrot[i]
1652 * points to the first rotaion in the sequence when applied to v[i]. Note that
1653 * for non-snub polyhedra, the rotations at a child are opposite in sense when
1654 * compared to the rotations at the parent. Thus, we fill adj[*][i] from the
1655 * end to signify clockwise rotations. The firstrot[] array is not needed for
1656 * display thus it is freed after being used for face computations below.
1659 vertices(Polyhedron *P)
1663 Malloc(P->v, P->V, Vector);
1664 Matalloc(P->adj, P->M, P->V, int);
1665 Malloc(P->firstrot, P->V, int); /* temporary , put in Polyhedron
1666 structure so that may be freed on
1668 cosa = cos(M_PI / P->n[0]) / sin(P->gamma[0]);
1674 P->v[1].x = 2 * cosa * sqrt(1 - cosa * cosa);
1676 P->v[1].z = 2 * cosa * cosa - 1;
1679 P->adj[0][1] = -1;/* start the other side */
1680 P->adj[P->M-1][1] = 0;
1682 P->firstrot[1] = P->snub[P->M-1] ? 0 : P->M-1 ;
1685 for (i = 0; i < newV; i++) {
1687 int last, one, start, limit;
1688 if (P->adj[0][i] == -1) {
1689 one = -1; start = P->M-2; limit = -1;
1691 one = 1; start = 1; limit = P->M;
1694 for (j = start; j != limit; j += one) {
1697 temp = rotate (P->v[P->adj[j-one][i]], P->v[i],
1698 one * 2 * P->gamma[P->rot[k]]);
1699 for (J=0; J<newV && !same(P->v[J],temp,BIG_EPSILON); J++)
1705 if (J == newV) { /* new vertex */
1706 if (newV == P->V) Err ("too many vertices");
1707 P->v[newV++] = temp;
1712 P->adj[P->M-1][J] = i;
1717 P->firstrot[J] = !P->snub[last] ? last :
1718 !P->snub[k] ? (k+1)%P->M : k ;
1728 * Compute polyhedron faces (dual vertices) and incidence matrices.
1729 * For orientable polyhedra, we can distinguish between the two faces meeting
1730 * at a given directed edge and identify the face on the left and the face on
1731 * the right, as seen from the outside. For one-sided polyhedra, the vertex
1732 * figure is a papillon (in Coxeter &al. terminology, a crossed parallelogram)
1733 * and the two faces meeting at an edge can be identified as the side face
1734 * (n[1] or n[2]) and the diagonal face (n[0] or n[3]).
1737 faces(Polyhedron *P)
1740 Malloc (P->f, P->F, Vector);
1741 Malloc (P->ftype, P->F, int);
1742 Matalloc (P->incid, P->M, P->V, int);
1743 P->minr = 1 / fabs (tan (M_PI / P->n[P->hemi]) * tan (P->gamma[P->hemi]));
1744 for (i = P->M; --i>=0;) {
1746 for (j = P->V; --j>=0;)
1747 P->incid[i][j] = -1;
1749 for (i = 0; i < P->V; i++) {
1751 for (j = 0; j < P->M; j++) {
1753 int pap=0;/* papillon edge type */
1754 if (P->incid[j][i] != -1)
1756 P->incid[j][i] = newF;
1758 Err("too many faces");
1759 P->f[newF] = pole(P->minr, P->v[i], P->v[P->adj[j][i]],
1760 P->v[P->adj[mod(j + 1, P->M)][i]]);
1761 P->ftype[newF] = P->rot[mod(P->firstrot[i] + ((P->adj[0][i] <
1762 P->adj[P->M - 1][i])
1767 pap = (P->firstrot[i] + j) % 2;
1773 if ((i0 = P->adj[J][k]) == i) break;
1774 for (J = 0; J < P->M && P->adj[J][i0] != k; J++)
1777 Err("too many faces");
1778 if (P->onesided && (J + P->firstrot[i0]) % 2 == pap) {
1779 P->incid [J][i0] = newF;
1785 P->incid [J][i0] = newF;
1798 * Compute edge list and graph polyhedron and dual.
1799 * If the polyhedron is of the "hemi" type, each edge has one finite vertex and
1800 * one ideal vertex. We make sure the latter is always the out-vertex, so that
1801 * the edge becomes a ray (half-line). Each ideal vertex is represented by a
1802 * unit Vector, and the direction of the ray is either parallel or
1803 * anti-parallel this Vector. We flag this in the array P->anti[E].
1806 edgelist(Polyhedron *P)
1808 int i, j, *s, *t, *u;
1809 Matalloc(P->e, 2, P->E, int);
1810 Matalloc(P->dual_e, 2, P->E, int);
1813 for (i = 0; i < P->V; i++)
1814 for (j = 0; j < P->M; j++)
1815 if (i < P->adj[j][i]) {
1817 *t++ = P->adj[j][i];
1824 Malloc(P->anti, P->E, int);
1826 for (i = 0; i < P->V; i++)
1827 for (j = 0; j < P->M; j++)
1828 if (i < P->adj[j][i])
1831 *s++ = P->incid[mod(j-1,P->M)][i];
1832 *t++ = P->incid[j][i];
1834 if (P->ftype[P->incid[j][i]]) {
1835 *s = P->incid[j][i];
1836 *t = P->incid[mod(j-1,P->M)][i];
1838 *s = P->incid[mod(j-1,P->M)][i];
1839 *t = P->incid[j][i];
1841 *u++ = dot(P->f[*s++], P->f[*t++]) > 0;
1849 sprintfrac(double x)
1854 Malloc(s, sizeof ("infinity"), char);
1855 strcpy(s, "infinity");
1856 } else if (frax.d == 1) {
1857 char n[MAXDIGITS + 1];
1858 sprintf(n, "%ld", frax.n);
1859 Malloc(s, strlen (n) + 1, char);
1862 char n[MAXDIGITS + 1], d[MAXDIGITS + 1];
1863 sprintf(n, "%ld", frax.n);
1864 sprintf(d, "%ld", frax.d);
1865 Malloc(s, strlen (n) + strlen (d) + 2, char);
1866 sprintf(s, "%s/%s", n, d);
1872 dot(Vector a, Vector b)
1874 return a.x * b.x + a.y * b.y + a.z * b.z;
1878 scale(double k, Vector a)
1887 diff(Vector a, Vector b)
1896 cross(Vector a, Vector b)
1899 p.x = a.y * b.z - a.z * b.y;
1900 p.y = a.z * b.x - a.x * b.z;
1901 p.z = a.x * b.y - a.y * b.x;
1906 sum(Vector a, Vector b)
1915 sum3(Vector a, Vector b, Vector c)
1924 rotate(Vector vertex, Vector axis, double angle)
1927 p = scale(dot (axis, vertex), axis);
1928 return sum3(p, scale(cos(angle), diff(vertex, p)),
1929 scale(sin(angle), cross(axis, vertex)));
1932 static Vector x, y, z;
1935 * rotate the standard frame
1938 rotframe(double azimuth, double elevation, double angle)
1940 static const Vector X = {1,0,0}, Y = {0,1,0}, Z = {0,0,1};
1943 axis = rotate(rotate (X, Y, elevation), Z, azimuth);
1944 x = rotate(X, axis, angle);
1945 y = rotate(Y, axis, angle);
1946 z = rotate(Z, axis, angle);
1950 * rotate an array of n Vectors
1953 rotarray(Vector *new, Vector *old, int n)
1956 *new++ = sum3(scale(old->x, x), scale(old->y, y), scale(old->z, z));
1962 same(Vector a, Vector b, double epsilon)
1964 return fabs(a.x - b.x) < epsilon && fabs(a.y - b.y) < epsilon
1965 && fabs(a.z - b.z) < epsilon;
1969 * Compute the polar reciprocal of the plane containing a, b and c:
1971 * If this plane does not contain the origin, return p such that
1972 * dot(p,a) = dot(p,b) = dot(p,b) = r.
1974 * Otherwise, return p such that
1975 * dot(p,a) = dot(p,b) = dot(p,c) = 0
1980 pole(double r, Vector a, Vector b, Vector c)
1984 p = cross(diff(b, a), diff(c, a));
1987 return scale(1 / sqrt(dot(p, p)), p);
1989 return scale(r/ k , p);
1998 static void rotframe(double azimuth, double elevation, double angle);
1999 static void rotarray(Vector *new, Vector *old, int n);
2000 static int mod (int i, int j);
2004 push_point (polyhedron *p, Vector v)
2006 p->points[p->npoints].x = v.x;
2007 p->points[p->npoints].y = v.y;
2008 p->points[p->npoints].z = v.z;
2013 push_face3 (polyhedron *p, int x, int y, int z)
2015 p->faces[p->nfaces].npoints = 3;
2016 Malloc (p->faces[p->nfaces].points, 3, int);
2017 p->faces[p->nfaces].points[0] = x;
2018 p->faces[p->nfaces].points[1] = y;
2019 p->faces[p->nfaces].points[2] = z;
2024 push_face4 (polyhedron *p, int x, int y, int z, int w)
2026 p->faces[p->nfaces].npoints = 4;
2027 Malloc (p->faces[p->nfaces].points, 4, int);
2028 p->faces[p->nfaces].points[0] = x;
2029 p->faces[p->nfaces].points[1] = y;
2030 p->faces[p->nfaces].points[2] = z;
2031 p->faces[p->nfaces].points[3] = w;
2039 construct_polyhedron (Polyhedron *P, Vector *v, int V, Vector *f, int F,
2040 char *name, char *dual, char *class, char *star,
2041 double azimuth, double elevation, double freeze)
2043 int i, j, k=0, l, ll, ii, *hit=0, facelets;
2048 Malloc (result, 1, polyhedron);
2049 memset (result, 0, sizeof(*result));
2054 rotframe(azimuth, elevation, freeze);
2055 Malloc(temp, V, Vector);
2056 rotarray(temp, v, V);
2058 Malloc(temp, F, Vector);
2059 rotarray(temp, f, F);
2062 result->number = P->index + 1;
2063 result->name = strdup (name);
2064 result->dual = strdup (dual);
2065 result->wythoff = strdup (P->polyform);
2066 result->config = strdup (P->config);
2067 result->group = strdup (P->group);
2068 result->class = strdup (class);
2073 Malloc (result->points, V + F * 13, point);
2074 result->npoints = 0;
2076 result->nedges = P->E;
2077 result->logical_faces = F;
2078 result->logical_vertices = V;
2079 result->density = P->D;
2080 result->chi = P->chi;
2082 for (i = 0; i < V; i++)
2083 push_point (result, v[i]);
2086 * Auxiliary vertices (needed because current VRML browsers cannot handle
2087 * non-simple polygons, i.e., ploygons with self intersections): Each
2088 * non-simple face is assigned an auxiliary vertex. By connecting it to the
2089 * rest of the vertices the face is triangulated. The circum-center is used
2090 * for the regular star faces of uniform polyhedra. The in-center is used for
2091 * the pentagram (#79) and hexagram (#77) of the high-density snub duals, and
2092 * for the pentagrams (#40, #58) and hexagram (#52) of the stellated duals
2093 * with configuration (....)/2. Finally, the self-intersection of the crossed
2094 * parallelogram is used for duals with form p q r| with an even denominator.
2096 * This method do not work for the hemi-duals, whose faces are not
2097 * star-shaped and have two self-intersections each.
2099 * Thus, for each face we need six auxiliary vertices: The self intersections
2100 * and the terminal points of the truncations of the infinite edges. The
2101 * ideal vertices are listed, but are not used by the face-list.
2103 * Note that the face of the last dual (#80) is octagonal, and constists of
2104 * two quadrilaterals of the infinite type.
2107 if (*star && P->even != -1)
2108 Malloc(hit, F, int);
2109 for (i = 0; i < F; i++)
2111 (frac(P->n[P->ftype[i]]), frax.d != 1 && frax.d != frax.n - 1)) ||
2115 denominator (P->m[0]) != 1))) {
2116 /* find the center of the face */
2118 if (!*star && P->hemi && !P->ftype[i])
2121 h = P->minr / dot(f[i],f[i]);
2122 push_point(result, scale (h, f[i]));
2124 } else if (*star && P->even != -1) {
2125 /* find the self-intersection of a crossed parallelogram.
2126 * hit is set if v0v1 intersects v2v3*/
2127 Vector v0, v1, v2, v3, c0, c1, p;
2129 v0 = v[P->incid[0][i]];
2130 v1 = v[P->incid[1][i]];
2131 v2 = v[P->incid[2][i]];
2132 v3 = v[P->incid[3][i]];
2133 d0 = sqrt(dot(diff(v0, v2), diff(v0, v2)));
2134 d1 = sqrt(dot (diff(v1, v3), diff(v1, v3)));
2135 c0 = scale(d1, sum(v0, v2));
2136 c1 = scale(d0, sum(v1, v3));
2137 p = scale(0.5 / (d0 + d1), sum(c0, c1));
2138 push_point (result, p);
2139 p = cross(diff(p, v2), diff(p, v3));
2140 hit[i] = (dot(p, p) < 1e-6);
2141 } else if (*star && P->hemi && P->index != last_uniform - 1) {
2142 /* find the terminal points of the truncation and the
2143 * self-intersections.
2150 Vector v0, v1, v2, v3, v01, v03, v21, v23, v0123, v0321 ;
2152 double t = 1.5;/* truncation adjustment factor */
2153 j = !P->ftype[P->incid[0][i]];
2154 v0 = v[P->incid[j][i]];/* real vertex */
2155 v1 = v[P->incid[j+1][i]];/* ideal vertex (unit vector) */
2156 v2 = v[P->incid[j+2][i]];/* real */
2157 v3 = v[P->incid[(j+3)%4][i]];/* ideal */
2158 /* compute intersections
2159 * this uses the following linear algebra:
2160 * v0123 = v0 + a v1 = v2 + b v3
2161 * v0 x v3 + a (v1 x v3) = v2 x v3
2162 * a (v1 x v3) = (v2 - v0) x v3
2163 * a (v1 x v3) . (v1 x v3) = (v2 - v0) x v3 . (v1 x v3)
2166 v0123 = sum(v0, scale(dot(cross(diff(v2, v0), v3), u) / dot(u,u),
2168 v0321 = sum(v0, scale(dot(cross(diff(v0, v2), v1), u) / dot(u,u),
2170 /* compute truncations */
2171 v01 = sum(v0 , scale(t, diff(v0123, v0)));
2172 v23 = sum(v2 , scale(t, diff(v0123, v2)));
2173 v03 = sum(v0 , scale(t, diff(v0321, v0)));
2174 v21 = sum(v2 , scale(t, diff(v0321, v2)));
2176 push_point(result, v01);
2177 push_point(result, v23);
2178 push_point(result, v0123);
2179 push_point(result, v03);
2180 push_point(result, v21);
2181 push_point(result, v0321);
2183 } else if (*star && P->index == last_uniform - 1) {
2184 /* find the terminal points of the truncation and the
2185 * self-intersections.
2198 Vector v0, v1, v2, v3, v4, v5, v6, v7, v01, v07, v21, v23;
2199 Vector v43, v45, v65, v67, v0123, v0721, v4365, v4567;
2200 double t = 1.5;/* truncation adjustment factor */
2202 for (j = 0; j < 8; j++)
2203 if (P->ftype[P->incid[j][i]] == 3)
2205 v0 = v[P->incid[j][i]];/* real {5/3} */
2206 v1 = v[P->incid[(j+1)%8][i]];/* ideal */
2207 v2 = v[P->incid[(j+2)%8][i]];/* real {3} */
2208 v3 = v[P->incid[(j+3)%8][i]];/* ideal */
2209 v4 = v[P->incid[(j+4)%8][i]];/* real {5/2} */
2210 v5 = v[P->incid[(j+5)%8][i]];/* ideal */
2211 v6 = v[P->incid[(j+6)%8][i]];/* real {3/2} */
2212 v7 = v[P->incid[(j+7)%8][i]];/* ideal */
2213 /* compute intersections */
2215 v0123 = sum(v0, scale(dot(cross(diff(v2, v0), v3), u) / dot(u,u),
2218 v0721 = sum(v0, scale(dot(cross(diff(v2, v0), v1), u) / dot(u,u),
2221 v4567 = sum(v4, scale(dot(cross(diff(v6, v4), v7), u) / dot(u,u),
2224 v4365 = sum(v4, scale(dot(cross(diff(v6, v4), v5), u) / dot(u,u),
2226 /* compute truncations */
2227 v01 = sum(v0 , scale(t, diff(v0123, v0)));
2228 v23 = sum(v2 , scale(t, diff(v0123, v2)));
2229 v07 = sum(v0 , scale(t, diff(v0721, v0)));
2230 v21 = sum(v2 , scale(t, diff(v0721, v2)));
2231 v45 = sum(v4 , scale(t, diff(v4567, v4)));
2232 v67 = sum(v6 , scale(t, diff(v4567, v6)));
2233 v43 = sum(v4 , scale(t, diff(v4365, v4)));
2234 v65 = sum(v6 , scale(t, diff(v4365, v6)));
2236 push_point(result, v01);
2237 push_point(result, v23);
2238 push_point(result, v0123);
2239 push_point(result, v07);
2240 push_point(result, v21);
2241 push_point(result, v0721);
2242 push_point(result, v45);
2243 push_point(result, v67);
2244 push_point(result, v4567);
2245 push_point(result, v43);
2246 push_point(result, v65);
2247 push_point(result, v4365);
2252 * Each face is printed in a separate line, by listing the indices of its
2253 * vertices. In the non-simple case, the polygon is represented by the
2254 * triangulation, each triangle consists of two polyhedron vertices and one
2257 Malloc (result->faces, F * 10, face);
2262 for (i = 0; i < F; i++) {
2266 denominator (P->m[0]) != 1)) {
2267 for (j = 0; j < P->M - 1; j++) {
2268 push_face3 (result, P->incid[j][i], P->incid[j+1][i], ii);
2272 push_face3 (result, P->incid[j][i], P->incid[0][i], ii++);
2275 } else if (P->even != -1) {
2276 if (hit && hit[i]) {
2277 push_face3 (result, P->incid[3][i], P->incid[0][i], ii);
2278 push_face3 (result, P->incid[1][i], P->incid[2][i], ii);
2280 push_face3 (result, P->incid[0][i], P->incid[1][i], ii);
2281 push_face3 (result, P->incid[2][i], P->incid[3][i], ii);
2286 } else if (P->hemi && P->index != last_uniform - 1) {
2287 j = !P->ftype[P->incid[0][i]];
2289 push_face3 (result, ii, ii + 1, ii + 2);
2290 push_face4 (result, P->incid[j][i], ii + 2, P->incid[j+2][i], ii + 5);
2291 push_face3 (result, ii + 3, ii + 4, ii + 5);
2294 } else if (P->index == last_uniform - 1) {
2295 for (j = 0; j < 8; j++)
2296 if (P->ftype[P->incid[j][i]] == 3)
2298 push_face3 (result, ii, ii + 1, ii + 2);
2300 P->incid[j][i], ii + 2, P->incid[(j+2)%8][i], ii + 5);
2301 push_face3 (result, ii + 3, ii + 4, ii + 5);
2303 push_face3 (result, ii + 6, ii + 7, ii + 8);
2305 P->incid[(j+4)%8][i], ii + 8, P->incid[(j+6)%8][i],
2307 push_face3 (result, ii + 9, ii + 10, ii + 11);
2312 result->faces[result->nfaces].npoints = P->M;
2313 Malloc (result->faces[result->nfaces].points, P->M, int);
2314 for (j = 0; j < P->M; j++)
2315 result->faces[result->nfaces].points[j] = P->incid[j][i];
2320 int split = (frac(P->n[P->ftype[i]]),
2321 frax.d != 1 && frax.d != frax.n - 1);
2322 for (j = 0; j < V; j++) {
2323 for (k = 0; k < P->M; k++)
2324 if (P->incid[k][j] == i)
2331 for (l = P->adj[k][j]; l != j; l = P->adj[k][l]) {
2332 for (k = 0; k < P->M; k++)
2333 if (P->incid[k][l] == i)
2335 if (P->adj[k][l] == ll)
2336 k = mod(k + 1 , P->M);
2337 push_face3 (result, ll, l, ii);
2341 push_face3 (result, ll, j, ii++);
2348 Malloc (pp, 100, int);
2352 for (l = P->adj[k][j]; l != j; l = P->adj[k][l]) {
2353 for (k = 0; k < P->M; k++)
2354 if (P->incid[k][l] == i)
2356 if (P->adj[k][l] == ll)
2357 k = mod(k + 1 , P->M);
2361 result->faces[result->nfaces].npoints = pi;
2362 result->faces[result->nfaces].points = pp;
2370 * Face color indices - for polyhedra with multiple face types
2371 * For non-simple faces, the index is repeated as many times as needed by the
2376 if (!*star && P->N != 1) {
2377 for (i = 0; i < F; i++)
2378 if (frac(P->n[P->ftype[i]]), frax.d == 1 || frax.d == frax.n - 1)
2379 result->faces[ff++].color = P->ftype[i];
2381 for (j = 0; j < frax.n; j++)
2382 result->faces[ff++].color = P->ftype[i];
2384 for (i = 0; i < facelets; i++)
2385 result->faces[ff++].color = 0;
2389 if (*star && P->even != -1)
2399 /* External interface (jwz)
2403 free_polyhedron (polyhedron *p)
2413 for (i = 0; i < p->nfaces; i++)
2414 Free (p->faces[i].points);
2422 construct_polyhedra (polyhedron ***polyhedra_ret)
2425 double azimuth = AZ;
2426 double elevation = EL;
2430 polyhedron **result;
2431 Malloc (result, last_uniform * 2 + 3, polyhedron*);
2433 while (index < last_uniform) {
2437 sprintf(sym, "#%d", index + 1);
2438 if (!(P = kaleido(sym, 1, 0, 0, 0))) {
2439 Err (strerror(errno));
2442 result[count++] = construct_polyhedron (P, P->v, P->V, P->f, P->F,
2443 P->name, P->dual_name,
2445 azimuth, elevation, freeze);
2447 result[count++] = construct_polyhedron (P, P->f, P->F, P->v, P->V,
2448 P->dual_name, P->name,
2450 azimuth, elevation, freeze);
2455 *polyhedra_ret = result;
2456 count++; /* leave room for teapot */