From 37a86923278506f26a379e6ee57c1abf2fe97aca Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 27 May 2010 09:53:51 +0200 Subject: [PATCH] sn-svg: Add new tool to display sort network as SVG. --- src/Makefile.am | 5 +- src/sn-svg.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/sn-svg.c diff --git a/src/Makefile.am b/src/Makefile.am index 7612c97..4aa3b3c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,7 +3,7 @@ include_HEADERS = sn_network.h sn_stage.h sn_comparator.h lib_LTLIBRARIES = libsortnetwork.la bin_PROGRAMS = sn-apply sn-batcher sn-check-bf sn-cut sn-info sn-merge \ - sn-normalize sn-oddevenmerge sn-shmoo sn-show sn-tex + sn-normalize sn-oddevenmerge sn-shmoo sn-show sn-svg sn-tex libsortnetwork_la_SOURCES = sn_network.c sn_network.h \ sn_stage.c sn_stage.h \ @@ -41,6 +41,9 @@ sn_shmoo_LDADD = libsortnetwork.la sn_show_SOURCES = sn-show.c sn_show_LDADD = libsortnetwork.la +sn_svg_SOURCES = sn-svg.c +sn_svg_LDADD = libsortnetwork.la + sn_tex_SOURCES = sn-tex.c sn_tex_LDADD = libsortnetwork.la diff --git a/src/sn-svg.c b/src/sn-svg.c new file mode 100644 index 0000000..bfb9a29 --- /dev/null +++ b/src/sn-svg.c @@ -0,0 +1,148 @@ +/** + * libsortnetwork - src/sn-svg.c + * Copyright (C) 2008-2010 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include "config.h" + +#include +#include + +#include "sn_network.h" + +#define INNER_SPACING 15.0 +#define OUTER_SPACING 40.0 +#define RADIUS 4.0 + +#define Y_OFFSET 5 +#define Y_SPACING 40 + +static double x_offset = OUTER_SPACING; +static int next_vertex_number = 0; + +static int tex_show_stage (sn_stage_t *s) +{ + int lines[s->comparators_num]; + int right[s->comparators_num]; + int lines_used = 0; + int i; + + printf (" \n", SN_STAGE_DEPTH (s)); + + for (i = 0; i < SN_STAGE_COMP_NUM (s); i++) + { + lines[i] = -1; + right[i] = -1; + } + + for (i = 0; i < SN_STAGE_COMP_NUM (s); i++) + { + int j; + sn_comparator_t *c = SN_STAGE_COMP_GET (s, i); + + int min_num; + int max_num; + + min_num = next_vertex_number; + next_vertex_number++; + + max_num = next_vertex_number; + next_vertex_number++; + + for (j = 0; j < lines_used; j++) + if (SN_COMP_LEFT (c) > right[j]) + break; + + lines[i] = j; + right[j] = SN_COMP_RIGHT (c); + if (j >= lines_used) + lines_used = j + 1; + + if (1) + { + double x1 = x_offset + (j * INNER_SPACING); + double x2 = x1; + int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING); + int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING); + + printf (" \n" + " \n" + " \n", + x1, y1, x2, y2, + x1, y1, RADIUS, + x2, y2, RADIUS); + } + } + + x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING; + + printf ("\n"); + + return (0); +} /* int tex_show_stage */ + +int main (int argc, char **argv) +{ + sn_network_t *n; + FILE *fh = NULL; + int i; + + if (argc == 1) + fh = stdin; + else if (argc == 2) + fh = fopen (argv[1], "r"); + + if (fh == NULL) + { + printf ("fh == NULL!\n"); + return (1); + } + + n = sn_network_read (fh); + + if (n == NULL) + { + printf ("n == NULL!\n"); + return (1); + } + + printf ("\n" + "\n" + "\n"); + + printf ("\n", PACKAGE_STRING); + + for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++) + tex_show_stage (SN_NETWORK_STAGE_GET (n, i)); + + printf (" \n"); + for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++) + printf (" \n", + 0.0, Y_OFFSET + (i * Y_SPACING), + x_offset, Y_OFFSET + (i * Y_SPACING)); + + printf ("\n"); + + return (0); +} /* int main */ + +/* vim: set shiftwidth=2 softtabstop=2 : */ -- 2.11.0