test/planarity_test.cc
changeset 797 30cb42e3e43a
child 798 58c330ad0b5c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/planarity_test.cc	Wed Sep 09 15:32:03 2009 +0200
     1.3 @@ -0,0 +1,259 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2009
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#include <iostream>
    1.23 +
    1.24 +#include <lemon/planarity.h>
    1.25 +
    1.26 +#include <lemon/smart_graph.h>
    1.27 +#include <lemon/lgf_reader.h>
    1.28 +#include <lemon/connectivity.h>
    1.29 +#include <lemon/dim2.h>
    1.30 +
    1.31 +#include "test_tools.h"
    1.32 +
    1.33 +using namespace lemon;
    1.34 +using namespace lemon::dim2;
    1.35 +
    1.36 +const int lgfn = 4;
    1.37 +const std::string lgf[lgfn] = {
    1.38 +  "@nodes\n"
    1.39 +  "label\n"
    1.40 +  "0\n"
    1.41 +  "1\n"
    1.42 +  "2\n"
    1.43 +  "3\n"
    1.44 +  "4\n"
    1.45 +  "@edges\n"
    1.46 +  "     label\n"
    1.47 +  "0 1  0\n"
    1.48 +  "0 2  0\n"
    1.49 +  "0 3  0\n"
    1.50 +  "0 4  0\n"
    1.51 +  "1 2  0\n"
    1.52 +  "1 3  0\n"
    1.53 +  "1 4  0\n"
    1.54 +  "2 3  0\n"
    1.55 +  "2 4  0\n"
    1.56 +  "3 4  0\n",
    1.57 +
    1.58 +  "@nodes\n"
    1.59 +  "label\n"
    1.60 +  "0\n"
    1.61 +  "1\n"
    1.62 +  "2\n"
    1.63 +  "3\n"
    1.64 +  "4\n"
    1.65 +  "@edges\n"
    1.66 +  "     label\n"
    1.67 +  "0 1  0\n"
    1.68 +  "0 2  0\n"
    1.69 +  "0 3  0\n"
    1.70 +  "0 4  0\n"
    1.71 +  "1 2  0\n"
    1.72 +  "1 3  0\n"
    1.73 +  "2 3  0\n"
    1.74 +  "2 4  0\n"
    1.75 +  "3 4  0\n",
    1.76 +
    1.77 +  "@nodes\n"
    1.78 +  "label\n"
    1.79 +  "0\n"
    1.80 +  "1\n"
    1.81 +  "2\n"
    1.82 +  "3\n"
    1.83 +  "4\n"
    1.84 +  "5\n"
    1.85 +  "@edges\n"
    1.86 +  "     label\n"
    1.87 +  "0 3  0\n"
    1.88 +  "0 4  0\n"
    1.89 +  "0 5  0\n"
    1.90 +  "1 3  0\n"
    1.91 +  "1 4  0\n"
    1.92 +  "1 5  0\n"
    1.93 +  "2 3  0\n"
    1.94 +  "2 4  0\n"
    1.95 +  "2 5  0\n",
    1.96 +
    1.97 +  "@nodes\n"
    1.98 +  "label\n"
    1.99 +  "0\n"
   1.100 +  "1\n"
   1.101 +  "2\n"
   1.102 +  "3\n"
   1.103 +  "4\n"
   1.104 +  "5\n"
   1.105 +  "@edges\n"
   1.106 +  "     label\n"
   1.107 +  "0 3  0\n"
   1.108 +  "0 4  0\n"
   1.109 +  "0 5  0\n"
   1.110 +  "1 3  0\n"
   1.111 +  "1 4  0\n"
   1.112 +  "1 5  0\n"
   1.113 +  "2 3  0\n"
   1.114 +  "2 5  0\n"
   1.115 +};
   1.116 +
   1.117 +
   1.118 +
   1.119 +typedef SmartGraph Graph;
   1.120 +GRAPH_TYPEDEFS(Graph);
   1.121 +
   1.122 +typedef PlanarEmbedding<SmartGraph> PE;
   1.123 +typedef PlanarDrawing<SmartGraph> PD;
   1.124 +typedef PlanarColoring<SmartGraph> PC;
   1.125 +
   1.126 +void checkEmbedding(const Graph& graph, PE& pe) {
   1.127 +  int face_num = 0;
   1.128 +
   1.129 +  Graph::ArcMap<int> face(graph, -1);
   1.130 +
   1.131 +  for (ArcIt a(graph); a != INVALID; ++a) {
   1.132 +    if (face[a] == -1) {
   1.133 +      Arc b = a;
   1.134 +      while (face[b] == -1) {
   1.135 +        face[b] = face_num;
   1.136 +        b = pe.next(graph.oppositeArc(b));
   1.137 +      }
   1.138 +      check(face[b] == face_num, "Wrong face");
   1.139 +      ++face_num;
   1.140 +    }
   1.141 +  }
   1.142 +  check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
   1.143 +        countEdges(graph) + 1, "Euler test does not passed");
   1.144 +}
   1.145 +
   1.146 +void checkKuratowski(const Graph& graph, PE& pe) {
   1.147 +  std::map<int, int> degs;
   1.148 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.149 +    int deg = 0;
   1.150 +    for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
   1.151 +      if (pe.kuratowski(e)) {
   1.152 +        ++deg;
   1.153 +      }
   1.154 +    }
   1.155 +    ++degs[deg];
   1.156 +  }
   1.157 +  for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
   1.158 +    check(it->first == 0 || it->first == 2 ||
   1.159 +          (it->first == 3 && it->second == 6) ||
   1.160 +          (it->first == 4 && it->second == 5),
   1.161 +          "Wrong degree in Kuratowski graph");
   1.162 +  }
   1.163 +
   1.164 +  // Not full test
   1.165 +  check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
   1.166 +}
   1.167 +
   1.168 +bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
   1.169 +  int l, r;
   1.170 +  if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
   1.171 +  if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
   1.172 +  if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
   1.173 +  if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
   1.174 +
   1.175 +  l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
   1.176 +  r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
   1.177 +  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
   1.178 +  l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
   1.179 +  r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
   1.180 +  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
   1.181 +  return true;
   1.182 +}
   1.183 +
   1.184 +bool collinear(Point<int> p, Point<int> q, Point<int> r) {
   1.185 +  int v;
   1.186 +  v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
   1.187 +  if (v != 0) return false;
   1.188 +  v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
   1.189 +  if (v < 0) return false;
   1.190 +  return true;
   1.191 +}
   1.192 +
   1.193 +void checkDrawing(const Graph& graph, PD& pd) {
   1.194 +  for (Graph::NodeIt n(graph); n != INVALID; ++n) {
   1.195 +    Graph::NodeIt m(n);
   1.196 +    for (++m; m != INVALID; ++m) {
   1.197 +      check(pd[m] != pd[n], "Two nodes with identical coordinates");
   1.198 +    }
   1.199 +  }
   1.200 +
   1.201 +  for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
   1.202 +    for (Graph::EdgeIt f(e); f != e; ++f) {
   1.203 +      Point<int> e1 = pd[graph.u(e)];
   1.204 +      Point<int> e2 = pd[graph.v(e)];
   1.205 +      Point<int> f1 = pd[graph.u(f)];
   1.206 +      Point<int> f2 = pd[graph.v(f)];
   1.207 +
   1.208 +      if (graph.u(e) == graph.u(f)) {
   1.209 +        check(!collinear(e1, e2, f2), "Wrong drawing");
   1.210 +      } else if (graph.u(e) == graph.v(f)) {
   1.211 +        check(!collinear(e1, e2, f1), "Wrong drawing");
   1.212 +      } else if (graph.v(e) == graph.u(f)) {
   1.213 +        check(!collinear(e2, e1, f2), "Wrong drawing");
   1.214 +      } else if (graph.v(e) == graph.v(f)) {
   1.215 +        check(!collinear(e2, e1, f1), "Wrong drawing");
   1.216 +      } else {
   1.217 +        check(!intersect(e1, e2, f1, f2), "Wrong drawing");
   1.218 +      }
   1.219 +    }
   1.220 +  }
   1.221 +}
   1.222 +
   1.223 +void checkColoring(const Graph& graph, PC& pc, int num) {
   1.224 +  for (NodeIt n(graph); n != INVALID; ++n) {
   1.225 +    check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
   1.226 +          "Wrong coloring");
   1.227 +  }
   1.228 +  for (EdgeIt e(graph); e != INVALID; ++e) {
   1.229 +    check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
   1.230 +          "Wrong coloring");
   1.231 +  }
   1.232 +}
   1.233 +
   1.234 +int main() {
   1.235 +
   1.236 +  for (int i = 0; i < lgfn; ++i) {
   1.237 +    std::istringstream lgfs(lgf[i]);
   1.238 +
   1.239 +    SmartGraph graph;
   1.240 +    graphReader(graph, lgfs).run();
   1.241 +
   1.242 +    check(simpleGraph(graph), "Test graphs must be simple");
   1.243 +
   1.244 +    PE pe(graph);
   1.245 +    if (pe.run()) {
   1.246 +      checkEmbedding(graph, pe);
   1.247 +
   1.248 +      PlanarDrawing<Graph> pd(graph);
   1.249 +      pd.run(pe.embedding());
   1.250 +      checkDrawing(graph, pd);
   1.251 +
   1.252 +      PlanarColoring<Graph> pc(graph);
   1.253 +      pc.runFiveColoring(pe.embedding());
   1.254 +      checkColoring(graph, pc, 5);
   1.255 +
   1.256 +    } else {
   1.257 +      checkKuratowski(graph, pe);
   1.258 +    }
   1.259 +  }
   1.260 +
   1.261 +  return 0;
   1.262 +}