Adaptagrams
nearalign.h
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libdialect - A library for computing DiAlEcT layouts:
5  * D = Decompose/Distribute
6  * A = Arrange
7  * E = Expand/Emend
8  * T = Transform
9  *
10  * Copyright (C) 2018 Monash University
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  * See the file LICENSE.LGPL distributed with the library.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  * Author(s): Steve Kieffer <http://skieffer.info>
23 */
24 
25 #ifndef DIALECT_NEARALIGN_H
26 #define DIALECT_NEARALIGN_H
27 
28 #include <vector>
29 #include <memory>
30 
31 #include "libdialect/constraints.h"
32 #include "libdialect/opts.h"
33 #include "libdialect/commontypes.h"
34 
35 namespace dialect {
36 
37 enum class AlignmentFlag {
38  NONE = 0,
39  HALIGN = 1,
40  VALIGN = 2,
41  HINFEAS = 4,
42  VINFEAS = 8
43 };
44 
46 inline AlignmentFlag operator&(AlignmentFlag a, AlignmentFlag b)
47 {return static_cast<AlignmentFlag>(static_cast<int>(a) & static_cast<int>(b));}
48 
49 inline AlignmentFlag& operator|= (AlignmentFlag& a, AlignmentFlag b) { return (AlignmentFlag&)((int&)a |= (int)b); }
50 
51 struct AlignmentTable {
52 
54  AlignmentTable(Graph &graph, const Nodes &ignore);
55 
57  AlignmentTable(Graph &graph, const NodesById &ignore);
58 
64  bool areAligned(id_type uid, id_type vid, AlignmentFlag flag);
65 
70  void addAlignment(id_type uid, id_type vid, AlignmentFlag flag);
71 
75  void addAlignments(const NodesById &nodes, const SepMatrix &matrix);
76 
81  std::vector<id_type> getAlignedIds(id_type uid, AlignmentFlag flag);
82 
90  void noteInfeasibility(id_type uid, id_type vid, AlignmentFlag flag);
91 
97  bool isMarkedInfeasible(id_type uid, id_type vid, AlignmentFlag flag);
98 
100  SparseIdMatrix2d<AlignmentFlag>::type state;
101 };
102 
103 
105 double manhattan(Node_SP u, Node_SP v);
106 
107 
108 struct PotentialAlignment {
109 
111  PotentialAlignment(Node_SP u, Node_SP v, AlignmentFlag flag)
112  : u(u),
113  v(v),
114  flag(flag),
115  cost(manhattan(u, v)) {}
116 
118  void remove(void);
119 
121  void addToTable(AlignmentTable &table) { table.addAlignment(u->id(), v->id(), flag); }
122 
124  void noteInfeasibility(AlignmentTable &table) { table.noteInfeasibility(u->id(), v->id(), flag); }
125 
127  SepCo_SP writeSepCo(void);
128 
130  void addToGraph(Graph &G);
131 
132  Node_SP u;
133  Node_SP v;
134  AlignmentFlag flag;
135  double cost;
136  // For managing linked list:
137  bool removed = false;
138  PotentialAlignment *prev = nullptr;
139  PotentialAlignment *next = nullptr;
140 };
141 
142 typedef std::vector<PotentialAlignment*> PotentialAlignments;
143 
154 size_t doNearAlignments(dialect::Graph &graph, dialect::AlignmentTable &atab, dialect::NodesById &ignore, const dialect::HolaOpts &opts, bool reattempt=false);
155 
156 
157 
158 } // namespace dialect
159 
160 #endif // DIALECT_NEARALIGN_H
The Graph class represents graphs consisting of nodes and edges.
Definition: graphs.h:180
double manhattan(Node_SP u, Node_SP v)
Report the "Manhattan distance" between two nodes.
Definition: nearalign.cpp:126
libdialect: A library for computing human-like orthogonal network (DiAlEcT) layouts.
Definition: cola.h:44
size_t doNearAlignments(dialect::Graph &graph, dialect::AlignmentTable &atab, dialect::NodesById &ignore, const dialect::HolaOpts &opts, bool reattempt=false)
Look for nodes that are nearly aligned, and try to align them.
Definition: nearalign.cpp:156
AlignmentFlag operator &(AlignmentFlag a, AlignmentFlag b)
Thanks to https://stackoverflow.com/a/1448478.
Definition: nearalign.h:46
No constraint: