Adaptagrams
chains.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_CHAINS_H
26 #define DIALECT_CHAINS_H
27 
28 #include <deque>
29 #include <memory>
30 #include <vector>
31 #include <map>
32 #include <utility>
33 
34 #include "libdialect/commontypes.h"
35 #include "libdialect/ortho.h"
36 
39 
40 namespace dialect {
41 
47 enum class LinkShape {
48  TLC, // top-left corner
49  H, // horizontal
50  BLC, // bottom-left corner
51  TRC, // top-right corner
52  V, // vertical
53  BRC // bottom-right corner
54 };
55 
56 typedef std::vector<LinkShape> LinkShapes;
57 
59 extern const LinkShapes bentLinkShapeCw;
60 
63 
67 extern const std::map<LinkShape, std::map<CardinalDir, CardinalDir>> applyBendToDir;
68 
73 extern const std::map<LinkShape, CardinalDir> cwIncomingDirForBend;
74 
86 extern const std::map<CompassDir, std::map<CardinalDir, std::map<CardinalDir, std::vector<std::vector<LinkShape>>>>> minimalBendSeqs;
87 
97 std::vector<std::vector<LinkShape>> lookupMinimalBendSeqs(Node_SP A, CardinalDir d0, Node_SP Z, CardinalDir d1);
98 
99 
102 CardinalDirs possibleCardinalDirections(Node_SP node1, Node_SP node2);
103 
104 
109 LinkShape shapeOfLink(Node_SP link);
110 
111 
115 struct BendSequence {
116 
119  BendSequence(LinkShapes &shapes) : bendTypes(shapes) {}
120 
125  BendSequence(LinkShapes &shapes, CardinalDir inDir, CardinalDir outDir)
126  : bendTypes(shapes), incomingDirec(inDir), outgoingDirec(outDir) {}
127 
129  size_t size(void) const { return bendTypes.size(); }
130 
132  LinkShapes bendTypes;
133 
138  std::vector<size_t> bendPoints;
139 
140  double cost = 0;
141  CardinalDir incomingDirec;
142  CardinalDir outgoingDirec;
143 };
144 
145 typedef std::shared_ptr<BendSequence> BendSequence_SP;
146 typedef std::vector<BendSequence_SP> BendSequences;
147 
150 
151  AestheticBend(Edge_SP edge, Node_SP bendNode, Node_SP nbrNode1, Node_SP nbrNode2)
152  : edge(edge), bendNode(bendNode), nbrNode1(nbrNode1), nbrNode2(nbrNode2) {}
153 
155  void addBendToEdge(void);
156 
157  Edge_SP edge;
158  Node_SP bendNode;
159  Node_SP nbrNode1;
160  Node_SP nbrNode2;
161 
162 };
163 
164 typedef std::shared_ptr<AestheticBend> AestheticBend_SP;
165 typedef std::vector<AestheticBend_SP> AestheticBends;
166 
167 typedef std::vector<std::pair<CardinalDir, CardinalDir>> ChainConfigSeq;
168 
173 class Chain {
174 public:
175 
185  Chain(Graph_SP G, std::deque<Node_SP> nodes, bool isCycle=false);
186 
195  Node_SP getNode(int i) const;
196 
205  Edge_SP getEdge(int i) const;
206 
216  double bendCost(LinkShape bendType, size_t i0) const;
217 
219  size_t size(void) const { return m_n; }
220 
226  BendSequences computePossibleBendSequences(void) const;
227 
233  void evaluateBendSequence(BendSequence_SP bendSeq) const;
234 
248  ChainConfigSeq writeConfigSeq(BendSequence_SP bendSeq) const;
249 
257  void takeShapeBasedConfiguration(void);
258 
261  void addAestheticBendsToEdges(void);
262 
266  static const size_t npos;
267 
268 private:
269 
287  double nextLocalOptimalPoint(size_t i0, LinkShape bendType, size_t remaining, size_t &i1) const;
288 
299  double globalOptimalPoint(LinkShape bendType, size_t &i1, size_t beginAt = 0) const;
300 
303  size_t getNumPotentialBendPlaces(void) const { size_t M = 2*m_n - 1; if (m_isCycle) ++M; return M; }
304 
305  Graph_SP m_graph;
306  size_t m_n;
307  Nodes m_nodes;
308  bool m_isCycle;
309  LinkShapes m_linkShapes;
310  Edges m_edges;
311  Node_SP m_anchorNodeLeft = nullptr;
312  Edge_SP m_anchorEdgeLeft = nullptr;
313  Node_SP m_anchorNodeRight = nullptr;
314  Edge_SP m_anchorEdgeRight = nullptr;
315  Edge_SP m_returnEdge = nullptr;
316  AestheticBends m_aestheticBends;
317 
318 };
319 
320 typedef std::shared_ptr<Chain> Chain_SP;
321 typedef std::vector<Chain_SP> Chains;
322 
324 Chains buildAllChainsInGraph(std::shared_ptr<dialect::Graph> graph);
325 
326 
327 
328 } // namespace dialect
329 
330 #endif // DIALECT_CHAINS_H
double bendCost(LinkShape bendType, size_t i0) const
Compute the cost of making a given bend shape at a given position in the chain, given the current geo...
Definition: chains.cpp:273
A bend point deliberately added to a connector route, for aesthetic reasons.
Definition: chains.h:149
const std::map< CompassDir, std::map< CardinalDir, std::map< CardinalDir, std::vector< std::vector< LinkShape > > > > > minimalBendSeqs
Definition: bendseqlookup.cpp:42
size_t size(void) const
Check how many nodes are in the chain.
Definition: chains.h:219
BendSequences computePossibleBendSequences(void) const
Compute the possible bend sequences that this chain could have.
Definition: chains.cpp:404
BendSequence(LinkShapes &shapes)
Basic constructor.
Definition: chains.h:119
Chain(Graph_SP G, std::deque< Node_SP > nodes, bool isCycle=false)
Standard constructor.
Definition: chains.cpp:135
LinkShape shapeOfLink(Node_SP link)
Determine the LinkShape for a given Node of degree 2.
Definition: chains.cpp:117
std::vector< std::vector< LinkShape > > lookupMinimalBendSeqs(Node_SP A, CardinalDir d0, Node_SP Z, CardinalDir d1)
Look up the minimal bend sequences for a Chain.
Definition: chains.cpp:90
libdialect: A library for computing human-like orthogonal network (DiAlEcT) layouts.
Definition: cola.h:44
A Chain is a sequence of degree-2 Nodes, possibly forming a cycle.
Definition: chains.h:173
void addAestheticBendsToEdges(void)
Add any aesthetic bends that were chosen during shape-based configuration, to the Edges to which they...
Definition: chains.cpp:616
void evaluateBendSequence(BendSequence_SP bendSeq) const
For a given BendSequence, determine the best places for those bends to occur in this Chain...
Definition: chains.cpp:459
LinkShape
Definition: chains.h:47
CardinalDirs possibleCardinalDirections(Node_SP node1, Node_SP node2)
List the possible cardinal directions from node1 to node2, if they were to be aligned non-aggressivel...
Definition: chains.cpp:104
Chains buildAllChainsInGraph(std::shared_ptr< dialect::Graph > graph)
Convenience method to build all the chains and cycles in a graph.
const std::map< LinkShape, CardinalDir > cwIncomingDirForBend
Definition: chains.cpp:83
ChainConfigSeq writeConfigSeq(BendSequence_SP bendSeq) const
Determine the direction in which each edge in this chain should be configured, in order to enforce a ...
Definition: chains.cpp:485
LinkShapes bentLinkShapeCwFromStartingPt(LinkShape start)
Get the bent LinkShapes, in clockwise order, starting from a given one.
Definition: chains.cpp:63
const std::map< LinkShape, std::map< CardinalDir, CardinalDir > > applyBendToDir
Definition: chains.cpp:76
void addBendToEdge(void)
Add the bend node to the edge.
Definition: chains.cpp:620
const LinkShapes bentLinkShapeCw
The bent LinkShapes, in clockwise order:
Definition: chains.cpp:59
static const size_t npos
Definition: chains.h:266
Edge_SP getEdge(int i) const
Get an Edge according to its "place" in the Chain.
Definition: chains.cpp:260
void takeShapeBasedConfiguration(void)
Give this chain an orthogonal configuration best fitting its present geometric shape.
Definition: chains.cpp:555
Node_SP getNode(int i) const
Get a Node according to its "place" in the Chain.
Definition: chains.cpp:247
BendSequence(LinkShapes &shapes, CardinalDir inDir, CardinalDir outDir)
Augmented constructor in which incoming and outgoing directions are also set.
Definition: chains.h:125
size_t size(void) const
Report the number of bends in the sequence.
Definition: chains.h:129
LinkShapes bendTypes
Under bendTypes we record what shape bend we want.
Definition: chains.h:132
std::vector< size_t > bendPoints
Definition: chains.h:138
A data structure for managing sequences of bend types, points at which these bends should occur (in a...
Definition: chains.h:115