2 * libsortnetwork - src/sn-svg.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>
28 #include "sn_network.h"
30 #define INNER_SPACING 15.0
31 #define OUTER_SPACING 40.0
37 static double x_offset = OUTER_SPACING;
39 static double determine_stage_width (sn_stage_t *s)
41 int lines[s->comparators_num];
42 int right[s->comparators_num];
46 if (SN_STAGE_COMP_NUM (s) == 0)
49 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
55 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
58 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
60 for (j = 0; j < lines_used; j++)
61 if (SN_COMP_LEFT (c) > right[j])
65 right[j] = SN_COMP_RIGHT (c);
69 assert (lines_used >= 1);
71 return (((double) (lines_used - 1)) * INNER_SPACING);
74 static double determine_network_width (sn_network_t *n) /* {{{ */
79 /* Spacing between stages and at the beginning and end of the network */
80 width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
82 /* Spacing required within a stage */
83 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
84 width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
87 } /* }}} double determine_network_width */
89 static int sn_svg_show_stage (sn_stage_t *s)
91 int lines[s->comparators_num];
92 int right[s->comparators_num];
96 printf (" <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
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);
120 double x1 = x_offset + (j * INNER_SPACING);
122 int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
123 int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
125 printf (" <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
126 "stroke=\"black\" stroke-width=\"1\" />\n"
127 " <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
128 " <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
135 x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
140 } /* int sn_svg_show_stage */
142 int main (int argc, char **argv)
155 fh = fopen (argv[1], "r");
159 printf ("fh == NULL!\n");
163 n = sn_network_read (fh);
167 printf ("n == NULL!\n");
171 svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
172 svg_width = determine_network_width (n);
174 printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
175 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
176 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
177 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" "
178 "width=\"%gpt\" height=\"%gpt\" viewBox=\"0 0 %g %g\">\n",
179 svg_width, svg_height, svg_width, svg_height);
181 printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
183 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
184 sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
186 printf (" <!-- horizontal lines -->\n");
187 for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
188 printf (" <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
189 "stroke=\"black\" stroke-width=\"1\" />\n",
190 0.0, Y_OFFSET + (i * Y_SPACING),
191 x_offset, Y_OFFSET + (i * Y_SPACING));
198 /* vim: set shiftwidth=2 softtabstop=2 : */