sn-tex: Added flip option (-f) to sn-tex
[sort-networks.git] / src / sn-tex.c
1 /**
2  * libsortnetwork - src/sn-tex.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <ff at octo.it>
20  **/
21
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
24 #endif
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <assert.h>
33
34 #include "sn_network.h"
35
36 /* 21cm (DIN-A4) - 2x3cm Rand = 15cm */
37 static double output_width = 15.0;
38 static double scale = 1.0;
39 static double inner_spacing = 0.3;
40 static double outer_spacing = 1.0;
41 static double vertical_spacing = 0.8;
42
43 static double x_offset;
44 static int next_vertex_number = 0;
45 static int flip = 0;
46
47 #define INPUT_TO_Y(i) (((double) (i)) * vertical_spacing)
48
49 static void exit_usage (void) /* {{{ */
50 {
51   printf ("Usage: sn-tex [options] [file]\n"
52       "\n"
53       "Valid options are:\n"
54       "  -w <width>   Specify the width of the graph (in TikZ default units).\n" 
55           "  -f           Flip the sorting network (min wire at the top, max wire at the bottom).\n"
56       "\n");
57   exit (EXIT_FAILURE);
58 } /* }}} void exit_usage */
59
60 static int read_options (int argc, char **argv) /* {{{ */
61 {
62   int option;
63
64   while ((option = getopt (argc, argv, "w:fh?")) != -1)
65   {
66     switch (option)
67     {
68       case 'w':
69       {
70         double width = atof (optarg);
71         if (width <= 0.0)
72         {
73           fprintf (stderr, "Invalid width argument: %s\n", optarg);
74           exit (EXIT_FAILURE);
75         }
76         output_width = width;
77         break;
78       }
79           case 'f':
80                 flip = 1;
81                 break;
82
83       case 'h':
84       case '?':
85       default:
86         exit_usage ();
87     }
88   }
89
90   return (0);
91 } /* }}} int read_options */
92
93 static double determine_stage_width (sn_stage_t *s) /* {{{ */
94 {
95   int lines[s->comparators_num];
96   int right[s->comparators_num];
97   int lines_used = 0;
98   int i;
99
100   if (SN_STAGE_COMP_NUM (s) == 0)
101     return (0.0);
102
103   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
104   {
105     lines[i] = -1;
106     right[i] = -1;
107   }
108
109   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
110   {
111     int j;
112     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
113
114     for (j = 0; j < lines_used; j++)
115       if (SN_COMP_LEFT (c) > right[j])
116         break;
117
118     lines[i] = j;
119     right[j] = SN_COMP_RIGHT (c);
120     if (j >= lines_used)
121       lines_used = j + 1;
122   }
123   assert (lines_used >= 1);
124
125   return (((double) (lines_used - 1)) * inner_spacing);
126 } /* }}} double determine_stage_width */
127
128 static double determine_network_width (sn_network_t *n) /* {{{ */
129 {
130   double width;
131   int i;
132
133   /* Spacing between stages and at the beginning and end of the network */
134   width = (SN_NETWORK_STAGE_NUM (n) + 1) * outer_spacing;
135
136   /* Spacing required within a stage */
137   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
138     width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
139
140   return (width);
141 } /* }}} double determine_network_width */
142
143 static int tex_show_stage (sn_stage_t *s) /* {{{ */
144 {
145   int lines[s->comparators_num];
146   int right[s->comparators_num];
147   int lines_used = 0;
148   int i;
149
150   for (i = 0; i < s->comparators_num; i++)
151   {
152     lines[i] = -1;
153     right[i] = -1;
154   }
155
156   for (i = 0; i < s->comparators_num; i++)
157   {
158     int j;
159     sn_comparator_t *c = s->comparators + i;
160
161     int min_num;
162     int max_num;
163
164     min_num = next_vertex_number;
165     next_vertex_number++;
166
167     max_num = next_vertex_number;
168     next_vertex_number++;
169
170     for (j = 0; j < lines_used; j++)
171       if (SN_COMP_LEFT (c) > right[j])
172         break;
173
174     lines[i] = j;
175     right[j] = SN_COMP_RIGHT (c);
176     if (j >= lines_used)
177       lines_used = j + 1;
178
179     printf ("\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
180         "\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
181         "\\path[comp] (v%i) -- (v%i);\n"
182         "\n",
183         min_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->min),
184         max_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->max),
185         min_num, max_num);
186   }
187
188   x_offset = x_offset + ((lines_used - 1) * inner_spacing) + outer_spacing;
189
190   return (0);
191 } /* }}} int tex_show_stage */
192
193 int main (int argc, char **argv) /* {{{ */
194 {
195   sn_network_t *n;
196   FILE *fh = NULL;
197   double orig_width;
198   int i;
199
200   read_options (argc, argv);
201
202   if ((argc - optind) == 0)
203     fh = stdin;
204   else if ((argc - optind) == 1)
205     fh = fopen (argv[optind], "r");
206
207   if (fh == NULL)
208     exit_usage ();
209
210   n = sn_network_read (fh);
211   if (n == NULL)
212   {
213     fprintf (stderr, "Unable to read network from file handle.\n");
214     exit (EXIT_FAILURE);
215   }
216
217   orig_width = determine_network_width (n);
218   if (orig_width <= 0.0)
219   {
220     fprintf (stderr, "determine_network_width returned invalid value %g.\n",
221         orig_width);
222     exit (EXIT_FAILURE);
223   }
224
225   scale = output_width / orig_width;
226   inner_spacing *= scale;
227   outer_spacing *= scale;
228   vertical_spacing *= scale;
229
230   x_offset = outer_spacing;
231
232   if (flip)
233         printf ("\\begin{tikzpicture}[auto,yscale=-1]\n");
234   else
235         printf ("\\begin{tikzpicture}[auto]\n");
236
237   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
238      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
239
240   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
241     printf ("\\path[edge] (0,%.2f) -- (%.2f,%.2f);\n",
242         INPUT_TO_Y (i), x_offset, INPUT_TO_Y (i));
243
244   printf ("\\end{tikzpicture}\n");
245
246   return (0);
247 } /* }}} int main */
248
249 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */