2 * libsortnetwork - src/sn-tex.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
34 #include "sn_network.h"
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;
43 static double x_offset;
44 static int next_vertex_number = 0;
46 #define INPUT_TO_Y(i) (((double) (i)) * vertical_spacing)
48 static void exit_usage (void) /* {{{ */
50 printf ("Usage: sn-tex [options] [file]\n"
52 "Valid options are:\n"
53 " -w <width> Specify the width of the graph (in TikZ default units).\n"
56 } /* }}} void exit_usage */
58 static int read_options (int argc, char **argv) /* {{{ */
62 while ((option = getopt (argc, argv, "w:h?")) != -1)
68 double width = atof (optarg);
71 fprintf (stderr, "Invalid width argument: %s\n", optarg);
86 } /* }}} int read_options */
88 static double determine_stage_width (sn_stage_t *s) /* {{{ */
90 int lines[s->comparators_num];
91 int right[s->comparators_num];
95 if (SN_STAGE_COMP_NUM (s) == 0)
98 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
104 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
107 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
109 for (j = 0; j < lines_used; j++)
110 if (SN_COMP_LEFT (c) > right[j])
114 right[j] = SN_COMP_RIGHT (c);
118 assert (lines_used >= 1);
120 return (((double) (lines_used - 1)) * inner_spacing);
121 } /* }}} double determine_stage_width */
123 static double determine_network_width (sn_network_t *n) /* {{{ */
128 /* Spacing between stages and at the beginning and end of the network */
129 width = (SN_NETWORK_STAGE_NUM (n) + 1) * outer_spacing;
131 /* Spacing required within a stage */
132 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
133 width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
136 } /* }}} double determine_network_width */
138 static int tex_show_stage (sn_stage_t *s) /* {{{ */
140 int lines[s->comparators_num];
141 int right[s->comparators_num];
145 for (i = 0; i < s->comparators_num; i++)
151 for (i = 0; i < s->comparators_num; i++)
154 sn_comparator_t *c = s->comparators + i;
159 min_num = next_vertex_number;
160 next_vertex_number++;
162 max_num = next_vertex_number;
163 next_vertex_number++;
165 for (j = 0; j < lines_used; j++)
166 if (SN_COMP_LEFT (c) > right[j])
170 right[j] = SN_COMP_RIGHT (c);
174 printf ("\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
175 "\\node[vertex] (v%i) at (%.2f,%.2f) {};\n"
176 "\\path[comp] (v%i) -- (v%i);\n"
178 min_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->min),
179 max_num, x_offset + (j * inner_spacing), INPUT_TO_Y (c->max),
183 x_offset = x_offset + ((lines_used - 1) * inner_spacing) + outer_spacing;
186 } /* }}} int tex_show_stage */
188 int main (int argc, char **argv) /* {{{ */
195 read_options (argc, argv);
197 if ((argc - optind) == 0)
199 else if ((argc - optind) == 1)
200 fh = fopen (argv[optind], "r");
205 n = sn_network_read (fh);
208 fprintf (stderr, "Unable to read network from file handle.\n");
212 orig_width = determine_network_width (n);
213 if (orig_width <= 0.0)
215 fprintf (stderr, "determine_network_width returned invalid value %g.\n",
220 scale = output_width / orig_width;
221 inner_spacing *= scale;
222 outer_spacing *= scale;
223 vertical_spacing *= scale;
225 x_offset = outer_spacing;
227 printf ("\\begin{tikzpicture}[auto]\n");
229 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
230 tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
232 for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
233 printf ("\\path[edge] (0,%.2f) -- (%.2f,%.2f);\n",
234 INPUT_TO_Y (i), x_offset, INPUT_TO_Y (i));
236 printf ("\\end{tikzpicture}\n");
241 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */