src/sn-markov.c: Add missing include.
[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
27 #include "sn_network.h"
28
29 #define INNER_SPACING 15.0
30 #define OUTER_SPACING 40.0
31 #define RADIUS 4.0
32
33 #define Y_OFFSET 5
34 #define Y_SPACING 40
35
36 static double x_offset = OUTER_SPACING;
37 static int next_vertex_number = 0;
38
39 static int tex_show_stage (sn_stage_t *s)
40 {
41   int lines[s->comparators_num];
42   int right[s->comparators_num];
43   int lines_used = 0;
44   int i;
45
46   printf ("  <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
47
48   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
49   {
50     lines[i] = -1;
51     right[i] = -1;
52   }
53
54   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
55   {
56     int j;
57     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
58
59     int min_num;
60     int max_num;
61
62     min_num = next_vertex_number;
63     next_vertex_number++;
64
65     max_num = next_vertex_number;
66     next_vertex_number++;
67
68     for (j = 0; j < lines_used; j++)
69       if (SN_COMP_LEFT (c) > right[j])
70         break;
71
72     lines[i] = j;
73     right[j] = SN_COMP_RIGHT (c);
74     if (j >= lines_used)
75       lines_used = j + 1;
76
77     if (1)
78     {
79       double x1 = x_offset + (j * INNER_SPACING);
80       double x2 = x1;
81       int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
82       int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
83
84       printf ("  <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
85           "stroke=\"black\" stroke-width=\"1\" />\n"
86           "  <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
87           "  <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
88           x1, y1, x2, y2,
89           x1, y1, RADIUS,
90           x2, y2, RADIUS);
91     }
92   }
93
94   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
95
96   printf ("\n");
97
98   return (0);
99 } /* int tex_show_stage */
100
101 int main (int argc, char **argv)
102 {
103   sn_network_t *n;
104   FILE *fh = NULL;
105   int i;
106
107   if (argc == 1)
108     fh = stdin;
109   else if (argc == 2)
110     fh = fopen (argv[1], "r");
111
112   if (fh == NULL)
113   {
114     printf ("fh == NULL!\n");
115     return (1);
116   }
117
118   n = sn_network_read (fh);
119
120   if (n == NULL)
121   {
122     printf ("n == NULL!\n");
123     return (1);
124   }
125
126   printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
127       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
128       "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
129       "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n");
130
131   printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
132
133   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
134      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
135
136   printf ("  <!-- horizontal lines -->\n");
137   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
138     printf ("  <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
139         "stroke=\"black\" stroke-width=\"1\" />\n",
140         0.0, Y_OFFSET + (i * Y_SPACING),
141         x_offset, Y_OFFSET + (i * Y_SPACING));
142
143   printf ("</svg>\n");
144
145   return (0);
146 } /* int main */
147
148 /* vim: set shiftwidth=2 softtabstop=2 : */