Merge remote branch 'origin/master'
[sort-networks.git] / src / sn-svg.c
1 /**
2  * libsortnetwork - src/sn-svg.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 #include "config.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <assert.h>
28
29 #include "sn_network.h"
30
31 #define INNER_SPACING 15.0
32 #define OUTER_SPACING 40.0
33 #define RADIUS 4.0
34
35 #define Y_OFFSET 5
36 #define Y_SPACING 40
37
38 static double x_offset = OUTER_SPACING;
39 static _Bool embedded = 0;
40
41 static void exit_usage (void) /* {{{ */
42 {
43   printf ("Usage: sn-svg [options] [file]\n"
44       "\n"
45       "Valid options are:\n"
46       "  -e    Create output suitable for embedding into other XML files.\n" 
47       "\n");
48   exit (EXIT_FAILURE);
49 } /* }}} void exit_usage */
50
51 static int read_options (int argc, char **argv) /* {{{ */
52 {
53   int option;
54
55   while ((option = getopt (argc, argv, "eh?")) != -1)
56   {
57     switch (option)
58     {
59       case 'e':
60         embedded = 1;
61         break;
62
63       case 'h':
64       case '?':
65       default:
66         exit_usage ();
67     }
68   }
69
70   return (0);
71 } /* }}} int read_options */
72
73 static double determine_stage_width (sn_stage_t *s)
74 {
75   int lines[s->comparators_num];
76   int right[s->comparators_num];
77   int lines_used = 0;
78   int i;
79
80   if (SN_STAGE_COMP_NUM (s) == 0)
81     return (0.0);
82
83   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
84   {
85     lines[i] = -1;
86     right[i] = -1;
87   }
88
89   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
90   {
91     int j;
92     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
93
94     for (j = 0; j < lines_used; j++)
95       if (SN_COMP_LEFT (c) > right[j])
96         break;
97
98     lines[i] = j;
99     right[j] = SN_COMP_RIGHT (c);
100     if (j >= lines_used)
101       lines_used = j + 1;
102   }
103   assert (lines_used >= 1);
104
105   return (((double) (lines_used - 1)) * INNER_SPACING);
106 }
107
108 static double determine_network_width (sn_network_t *n) /* {{{ */
109 {
110   double width;
111   int i;
112
113   /* Spacing between stages and at the beginning and end of the network */
114   width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
115
116   /* Spacing required within a stage */
117   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
118     width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
119
120   return (width);
121 } /* }}} double determine_network_width */
122
123 static int sn_svg_show_stage (sn_stage_t *s) /* {{{ */
124 {
125   int lines[s->comparators_num];
126   int right[s->comparators_num];
127   int lines_used = 0;
128   int i;
129
130   printf ("  <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
131
132   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
133   {
134     lines[i] = -1;
135     right[i] = -1;
136   }
137
138   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
139   {
140     int j;
141     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
142
143     for (j = 0; j < lines_used; j++)
144       if (SN_COMP_LEFT (c) > right[j])
145         break;
146
147     lines[i] = j;
148     right[j] = SN_COMP_RIGHT (c);
149     if (j >= lines_used)
150       lines_used = j + 1;
151
152     if (1)
153     {
154       double x1 = x_offset + (j * INNER_SPACING);
155       double x2 = x1;
156       int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
157       int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
158
159       printf ("  <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
160           "stroke=\"black\" stroke-width=\"1\" />\n"
161           "  <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
162           "  <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
163           x1, y1, x2, y2,
164           x1, y1, RADIUS,
165           x2, y2, RADIUS);
166     }
167   }
168
169   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
170
171   printf ("\n");
172
173   return (0);
174 } /* }}} int sn_svg_show_stage */
175
176 int main (int argc, char **argv) /* {{{ */
177 {
178   sn_network_t *n;
179   FILE *fh = NULL;
180
181   double svg_height;
182   double svg_width;
183
184   int i;
185
186   read_options (argc, argv);
187
188   if ((argc - optind) == 0)
189     fh = stdin;
190   else if ((argc - optind) == 1)
191     fh = fopen (argv[optind], "r");
192
193   if (fh == NULL)
194     exit_usage ();
195
196   n = sn_network_read (fh);
197
198   if (n == NULL)
199   {
200     printf ("n == NULL!\n");
201     return (1);
202   }
203
204   svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
205   svg_width = determine_network_width (n);
206
207   if (embedded)
208     printf ("<svg:svg version=\"1.1\" viewBox=\"0 0 %g %g\">\n",
209         svg_width, svg_height);
210   else
211     printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
212         "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
213         "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
214         "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" "
215         "width=\"%gpt\" height=\"%gpt\" viewBox=\"0 0 %g %g\">\n",
216         svg_width, svg_height, svg_width, svg_height);
217
218   printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
219
220   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
221      sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
222
223   printf ("  <!-- horizontal lines -->\n");
224   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
225     printf ("  <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
226         "stroke=\"black\" stroke-width=\"1\" />\n",
227         0.0, Y_OFFSET + (i * Y_SPACING),
228         x_offset, Y_OFFSET + (i * Y_SPACING));
229
230   printf ("</svg:svg>\n");
231
232   return (0);
233 } /* }}} int main */
234
235 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */