-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphViewer.cpp
More file actions
985 lines (829 loc) · 35.9 KB
/
GraphViewer.cpp
File metadata and controls
985 lines (829 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
#include "GraphViewer.h"
#include "../GUI/MiniGUI.h"
#include "GVector.h"
#include "Utilities/Unicode.h"
#include <cmath>
#include <set>
#include <unordered_map>
#include <sstream>
#include <cctype>
#include <type_traits>
using namespace MiniGUI;
namespace GraphEditor {
namespace {
/* Useful, not required. */
const std::string kNonbreakingSpace = toUTF8(0xA0);
/* Edge graphics parameters. */
const double kLoopEdgeRadius = GraphEditor::kNodeRadius * 0.75;
/* Length of the invisible line on which to draw the contents of a loop edge. */
const double kLoopLabelLength = 150 / 1000.0;
/* Font and height for edges. */
const double kEdgeTextHeight = 48.0 / 1000; // 24pt in 1000px window
/* Amount to offset the label by relative to the edge. */
const double kEdgeLabelYOffset = 8.0 / 1000;
const double kLoopEdgeYOffset = 30.0 / 1000;
/* How much, in radians, to rotate the origin points of the states when shifting
* start positions of edges.
*/
const double kAvoidanceRotation = -M_PI / 6;
/* Arrowhead parameters. */
const double kArrowheadRotation = M_PI / 8;
const double kArrowheadSize = 0.02;
/* Parameters for avoiding collisions with self-loops. */
const int kLowAngle = -5;
const int kHighAngle = 355;
const int kAngleStep = 10;
}
/* Edges can be either line edges or loop edges. */
struct EdgeRender {
EdgeRender(ViewerBase* editor, Edge* edge): editor(editor), edge(edge) {}
virtual ~EdgeRender() = default;
virtual void draw(GCanvas* canvas, double thickness, MiniGUI::Color lineColor, MiniGUI::Color textColor) const = 0;
virtual bool contains(const GPoint& pt) const = 0;
ViewerBase* editor;
Edge* edge;
};
/* Linear edge. */
struct LineEdge: EdgeRender {
LineEdge(ViewerBase* editor, Edge* edge, GPoint from, GPoint to) : EdgeRender(editor, edge), lineStart(from), lineEnd(to) {}
void draw(GCanvas* canvas, double thickness, MiniGUI::Color lineColor, MiniGUI::Color textColor) const override;
bool contains(const GPoint& pt) const override;
GPoint lineStart, lineEnd;
};
/* Self-loop. */
struct LoopEdge: EdgeRender {
LoopEdge(ViewerBase* editor, Edge* edge, const GPoint& center, const GPoint& arrowPt) : EdgeRender(editor, edge), center(center), arrowPt(arrowPt) {}
void draw(GCanvas* canvas, double thickness, MiniGUI::Color lineColor, MiniGUI::Color textColor) const override;
bool contains(const GPoint& pt) const override;
/* Edge is represented by a circle. Where is the center of that
* circle?
*/
GPoint center;
/* Point where the arrowhead is drawn. */
GPoint arrowPt;
};
void ViewerBase::draw(GCanvas* canvas,
const std::unordered_map<Node*, NodeStyle>& nodeStyles,
const std::unordered_map<Edge*, EdgeStyle>& edgeStyles) {
/* TODO: This is for testing purposes. Please remove this. */
canvas->setColor("red");
canvas->drawRect(baseX, baseY, width, height);
/* Existing edges underdraw the nodes so we don't see the lines. */
for (auto start: edges) {
for (auto end: start.second) {
/* We could have null entries; skip them. */
/* TODO: Is this true? */
if (end.second) {
auto style = edgeStyles.count(end.second.get()) ? edgeStyles.at(end.second.get()) : EdgeStyle();
end.second->style->draw(canvas, style.lineWidth, style.lineColor, style.labelColor);
}
}
}
/* Nodes. */
for (auto node: nodes) {
auto style = nodeStyles.count(node.get())? nodeStyles.at(node.get()) : NodeStyle();
node->draw(this, canvas, style);
}
}
namespace {
bool isCloseTo(GPoint p0, GPoint p1, double distance) {
double dx = p0.x - p1.x;
double dy = p0.y - p1.y;
return dx * dx + dy * dy <= distance * distance;
}
}
double ViewerBase::graphicsToWorld(double width) {
return width / this->width;
}
GPoint ViewerBase::graphicsToWorld(GPoint in) {
return { (in.x - baseX) / width, (in.y - baseY) / width };
}
GRectangle ViewerBase::graphicsToWorld(GRectangle in) {
auto top = graphicsToWorld(GPoint{ in.x, in.y });
auto bot = graphicsToWorld(GPoint{ in.x + in.width, in.y + in.height });
return { top.x, top.y, bot.x - top.x, bot.y - top.y };
}
double ViewerBase::worldToGraphics(double width) {
return width * this->width;
}
GPoint ViewerBase::worldToGraphics(GPoint in) {
return { in.x * width + baseX, in.y * width + baseY };
}
GRectangle ViewerBase::worldToGraphics(GRectangle in) {
auto top = worldToGraphics(GPoint{ in.x, in.y });
auto bot = worldToGraphics(GPoint{ in.x + in.width, in.y + in.height });
return { top.x, top.y, bot.x - top.x, bot.y - top.y };
}
/* All parameters are in world coordinates. */
void ViewerBase::drawArrow(GCanvas* canvas, const GPoint& from, const GPoint& to,
double thickness, MiniGUI::Color color) {
GLine line(worldToGraphics(from), worldToGraphics(to));
line.setLineWidth(ceil(thickness * width));
line.setColor(color.toRGB());
canvas->draw(&line);
drawArrowhead(canvas, from, to, thickness, color);
}
void ViewerBase::drawArrowhead(GCanvas* canvas, const GPoint& from, const GPoint& to,
double thickness, MiniGUI::Color color) {
/* Skip arrowheads if graph is undirected. */
if (type() == Type::UNDIRECTED) return;
/* Draw the arrowheads. First, get a vector pointing from end to start so that
* we can shift it around to compute the endpoints.
*/
GVector v = normalizationOf(from - to);
/* Compute the two endpoints. */
GPoint left = to + rotate(v, kArrowheadRotation) * kArrowheadSize;
GPoint right = to + rotate(v, -kArrowheadRotation) * kArrowheadSize;
GLine line(worldToGraphics(left), worldToGraphics(to));
line.setLineWidth(ceil(thickness * width));
line.setColor(color.toRGB());
canvas->draw(&line);
line.setStartPoint(worldToGraphics(right));
canvas->draw(&line);
}
namespace {
/* Given a quadratic equation, returns whether there are any solutions that
* correspond to a line/circle intersection. This happens if solutions exist
* UNLESS both intersections are less than zero or both intersections are
* greater than one.
*/
size_t quadraticSolnsInRange(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
if (discriminant < 0) return 0;
double x1 = (-b + sqrt(discriminant)) / (2 * a);
double x2 = (-b - sqrt(discriminant)) / (2 * a);
return !((x1 < 0 && x2 < 0) || (x1 > 1 && x2 > 1));
}
/* Counts collisions a circle and a collection of lines. */
size_t collisionsBetween(const GPoint& center, double radius,
const std::vector<std::pair<GPoint, GPoint>>& lines) {
/* Any point (x, y) on a circle satisfies
*
* (x - x_c)^2 + (y - y_c)^2 = r^2.
*
* Any point on the line from p0 to p1 has parametric form
*
* (x(t), y(t)) = p0 + t(p1 - p0).
*
* Substituting, we get
*
* (p0x + t(p1x - p0x) - x_c)^2 + (p0y + t(p1y - p0y) - y_c)^2 = r^2
*
* Everything here except for t is a constant. Isolating t and simplifying, we get
*
* (t(p1x - p0x) + p0x - x_c)^2 + (t(p1y - p0y) + p0y - y_c)^2 = r^2
* --- dx -- --- dy ---
*
* (t * dx + p0x - x_c)^2 + (t * dy + p0y - y_c)^2 = r^2
* --- sx -- -- sy ---
*
* (t * dx + sx)^2 + (t * dy + sy)^2 = r^2
*
* ((dx)^2 t^2 + 2*dx*sx*t + (sx)^2) + ((dy)^2 t^2 + 2*dy*dy*t + (sy)^2) = r^2
*
* ((dx)^2 + (dy)^2) t^2 + 2(dx*sx + dy*sy) t + ((sx)^2 + (sy)^2 - r^2) = 0
* dot(d, d) t^2 + 2 dot(d, s) t + (dot(s, s) - r^2) = 0
*
* At this point this is a quadratic, and we just need to count solutions.
*/
size_t solns = 0;
for (const auto& line: lines) {
GVector d = line.second - line.first;
GVector s = line.first - center;
solns += quadraticSolnsInRange(dot(d, d), 2 * dot(d, s), dot(s, s) - radius * radius);
}
return solns;
}
size_t collisionsBetween(const GPoint&, double,
const std::vector<std::pair<GPoint, double>>&) {
/* TODO: Implement this function to count circle/circle collisions. */
return 0;
}
/* Given a collection of circles and lines and a new circle, determines how many
* collisions there are. This counts the number of colliding ENTITIES, not the
* number of collision points overall.
*/
size_t collisionsBetween(const GPoint& center, double radius,
const std::vector<std::pair<GPoint, GPoint>>& lines,
const std::vector<std::pair<GPoint, double>>& circles) {
return collisionsBetween(center, radius, lines) +
collisionsBetween(center, radius, circles);
}
/* Determines the best angle at which to orient a self-loop, which is one that
* hits the fewest other objects.
*/
double bestThetaFor(const GPoint& nodeCenter, const std::vector<std::pair<GPoint, GPoint>>& lines,
const std::vector<std::pair<GPoint, double>>& circles) {
/* Our algorithm for placing the circle goes as follows. We iterate over a fixed
* number of potential angles that we can use. For each one, we count the number
* of collisions that would result if we put the circle there, forming an array
* that you can think of as a "height map" of the collisions.
*
* We then find the minimum number of collisions and look for the longest range
* in the array (remembering to loop back around when we're done!), which
* corresponds to the widest margin of error we can find. From there, we then
* pick the midpoint of that range.
*
* TODO: It would be a LOT more elegant to do this by using some sort of nice
* and pretty math instead of trial and error. Can you improve upon this?
*/
std::vector<std::size_t> collisions;
for (int degAngle = kLowAngle; degAngle < kHighAngle; degAngle += kAngleStep) {
double theta = degAngle * M_PI / 180;
GPoint center = nodeCenter + unitToward(theta) * kNodeRadius;
collisions.push_back(collisionsBetween(center, kLoopEdgeRadius, lines, circles));
}
/* Find the minimum number of collisions. */
size_t min = *min_element(collisions.begin(), collisions.end());
/* Find the longest range of minimum values. */
size_t bestStart = 0;
size_t bestLength = 0;
size_t currStart = 0;
size_t currLength = 0;
/* To handle wraparound, scan backwards over the array and look for the
* first spot that isn't the min.
*/
for (size_t i = collisions.size(); i > 0; --i) {
if (collisions[i - 1] != min) break;
/* Back up a step. */
currStart = (currStart + collisions.size() - 1) % collisions.size();
currLength++;
}
for (size_t i = 0; i < collisions.size(); i++) {
/* Doesn't match? Gotta stop. */
if (collisions[i] != min) {
if (bestLength < currLength) {
bestLength = currLength;
bestStart = currStart;
}
currLength = 0;
currStart = i+1;
}
/* Otherwise, extend. */
else {
currLength++;
}
}
/* Handle edge case of finding the best at the end. */
if (bestLength < currLength) {
bestLength = currLength;
bestStart = currStart;
}
/* Casts to double necessary here to avoid bizarre integer overflows as negative
* angles turn into massively positive unsigned angles!
*/
double lowTheta = (kLowAngle + double(bestStart * kAngleStep)) * M_PI / 180;
double highTheta = (kLowAngle + double(bestStart + bestLength - 1) * kAngleStep) * M_PI / 180;
return (lowTheta + highTheta) / 2;
}
/* Given the center of a node and the point at which the loop is centered, returns
* a point where they intersect - a place where the arrow can be drawn.
*/
GPoint loopArrowPointFor(const GPoint& nodeCenter, const GPoint& loopCenter) {
/* Trig time! We have two circles where one is centered on the border of
* another. We want to then find one of the intersection points. How do we
* do it?
*
* For now, imagine that everything is colinear, like this:
*
* *
* / \
* r / \ r'
* / r \
* * ----- *
* node loop
* center center
*
* We want to know the angle theta that is made between the node center,
* the loop center, and the third triangle point (their intersection). The
* Law of Cosines tells us that
*
* r'^2 = r^2 + r^2 - 2r^2 cos theta
*
* Which, after some rearranging, gives us
*
* theta = arccos(1 - r'^2 / 2r^2)
*
* Use this to get that angle measure.
*/
double theta = acos(1 - kLoopEdgeRadius * kLoopEdgeRadius / (2 * kNodeRadius * kNodeRadius));
/* Rotate the vector from the node to loop center by this amount. */
return nodeCenter + rotate(loopCenter - nodeCenter, theta);
}
/* Boundaries of the world, represented as lines. */
std::vector<std::pair<GPoint, GPoint>> worldBoundaries(double aspectRatio) {
const double lft = 0;
const double rgt = 1;
const double top = 0;
const double bot = 1 / aspectRatio;
return {
{ { lft, top }, { rgt, top } },
{ { lft, bot }, { rgt, bot } },
{ { lft, top }, { lft, bot } },
{ { rgt, top }, { rgt, bot } },
};
}
}
/* Determines where each edge should start and end. There are dependencies
* across these edges, so we need to do this all at once.
*/
void ViewerBase::calculateEdgeEndpoints() {
/* List of all line segments used. */
std::vector<std::pair<GPoint, GPoint>> lines = worldBoundaries(mAspectRatio);
/* First, handle linear edges. */
forEachEdge([&](Edge* edge) {
if (edge->from() != edge->to()) {
/* Center coordinates. */
GPoint p0 = edge->from()->position();
GPoint p1 = edge->to()->position();
/* If there is a edge running in the reverse direction, we need to shift
* this edge over so that we don't overlap it.
*
* Skip this if the graph is undirected.
*/
if (type() == Type::DIRECTED && hasEdge(edge->to(), edge->from())) {
/* Unit vector pointing in the p0 -> p1 direction saying how much we need to rotate. */
auto p0Delta = rotate(normalizationOf(p1 - p0), kAvoidanceRotation);
/* Unit vector pointing in the p1 -> p0 direction saying how much we need to rotate. */
auto p1Delta = rotate(normalizationOf(p0 - p1), -kAvoidanceRotation);
/* At the borders. */
p0 += p0Delta * kNodeRadius;
p1 += p1Delta * kNodeRadius;
}
/* Otherwise, translate the center points to the borders. */
else {
p0 += normalizationOf(p1 - p0) * kNodeRadius;
p1 += normalizationOf(p0 - p1) * kNodeRadius;
}
edge->style = std::make_shared<LineEdge>(this, edge, p0, p1);
lines.push_back(std::make_pair(p0, p1));
}
});
/* All placed circles. Initially, that's all the nodes. */
std::vector<std::pair<GPoint, double>> circles;
for (auto node: nodes) {
circles.push_back(std::make_pair(node->position(), kNodeRadius));
}
/* Now, place all self-loops. */
forEachEdge([&](Edge* edge) {
if (edge->from() == edge->to()) {
double theta = bestThetaFor(edge->from()->position(), lines, circles);
GPoint center = edge->from()->position() + unitToward(theta) * kNodeRadius;
GPoint arrowPt = loopArrowPointFor(edge->from()->position(), center);
edge->style = std::make_shared<LoopEdge>(this, edge, center, arrowPt);
circles.push_back(std::make_pair(center, kNodeRadius));
}
});
}
/* Linear edge implementation. */
bool LineEdge::contains(const GPoint& pt) const {
/* Our goal is to see both (1) how far from the line we are and (2) how far
* down the line we are.
*
* To do this, we're going to change coordinates. Let b1 be a vector pointing
* 90 degrees to the right of the line, and let b2 be a vector pointing down
* the line. That gives us this coordinate system:
*
* b2
* ^
* |
* |
* ----+----> b1
*
* Now, we rewrite our cursor's position in this coordinate system. We do this
* by multiplying the cursor vector by a rotation matrix [b1, b2].
*
* From here we can see where we are. To be "close enough," we'll say that the
* cursor needs to have x coordinate that places it within the hover width,
* and it needs to have a y coordinate between zero and the length of the line.
*/
/* Switch coordinate systems to place the origin of the edge at (0, 0). */
GVector cursor = pt - lineStart;
/* Get the vectors b1 and b2. */
GVector lineVec = lineEnd - lineStart;
GVector b2 = lineVec / magnitudeOf(lineVec);
GVector b1 = rotate(b2, M_PI / 2);
/* Compute the cursor's representation in this system. That's
*
* | b1x b2x | |cx|
* | b1y b2y | |cy|
*/
GVector result = GMatrix(b1, b2) * cursor;
/* Make sure we're in range. */
return fabs(result.x) <= kEdgeTolerance / 2.0 &&
result.y >= 0 && result.y <= magnitudeOf(lineVec);
}
namespace {
/* char32_t may be signed or unsigned. This code allows us to deduplicate
* the testing logic so that we only check against 0 if char32_t is signed.
*/
template <bool b> struct BoolBox{};
template <typename CharT>
bool isSpaceImpl(CharT ch, BoolBox<true>) {
return ch >= 0 && ch <= 127 && isspace(ch);
}
template <typename CharT>
bool isSpaceImpl(CharT ch, BoolBox<false>) {
return ch <= 127 && isspace(ch);
}
/* Is this a space character? */
bool isSpace(char32_t ch) {
return isSpaceImpl(ch, BoolBox<std::is_signed<char32_t>::value>());
}
/* Given a string, replaces all the spaces in the string with nonbreaking spaces.
*
* TODO: Remove this and replace with LineBreak::NO_BREAK_SPACES.
*/
std::string toNonbreakingSpaces(const std::string& input) {
std::string result;
for (char32_t ch: utf8Reader(input)) {
if (isSpace(ch)) {
result += kNonbreakingSpace;
} else {
result += toUTF8(ch);
}
}
return result;
}
}
void ViewerBase::drawEdgeLabel(GCanvas* canvas,
const GPoint& p0, const GPoint& p1,
const std::string& labelText,
Color color,
bool hugLine) {
GPoint from = worldToGraphics(p0);
GPoint to = worldToGraphics(p1);
std::string label = toNonbreakingSpaces(labelText);
/* Determine the length of this line. */
double length = magnitudeOf(to - from);
/* Determine what font we should use for the label by computing a text render
* and extracting the font it uses.
*/
Font font = TextRender::construct(label, {0, 0, length, width * kEdgeTextHeight }, kEdgeFont.color(color))->computedFont();
/* Create a graphics object for the label. */
GText text(label);
text.setFont(font.stanfordCPPLibFontString());
text.setColor(font.color().toRGB());
/* Figure out where the label needs to go. */
double theta = angleOf(to - from);
/* Never draw text upside-down. See whether we go above or below the line. */
if (theta < -M_PI / 2 || theta > M_PI / 2) {
/* Below the line. Rotate our angle by 180 degrees and exchange the roles
* of the endpoints.
*/
theta += M_PI;
std::swap(to, from);
/* If we are supposed to hug the line, we need to do an extra step and shift the
* line position over so that when we draw on top of it, we appear to have just
* flipped rather than flipped and translated.
*/
if (hugLine) {
/* Get a perpendicular to the line. */
GVector normal = rotate(normalizationOf(to - from), M_PI / 2) * text.getHeight();
from += normal;
to += normal;
}
}
/* We'll aim to draw on the edge as the baseline,
* so we need to compute the (x, y) coordinate of the edge center.
*
* ... except that it's not the exact center. Rather, it's the center of the
* edge, offset by half the width of the label. In other words, we want
* to walk to the center, then advance a bit further
*/
GPoint target = from + normalizationOf(to - from) * (length - text.getWidth()) / 2.0;
/* Now, shift up off the line. */
target += rotate(normalizationOf(to - from), -M_PI / 2) * ceil(width * kEdgeLabelYOffset);
/* GText behaves strangely when rotated. The rotation is done around
* the graphics origin point (0, 0) rather than the center of the object.
* This means that we need to reposition the label so that it rotates the
* text into the exact position we want.
*
* Goal: Given a target position of (x, y), find a position
* (x', y') such that (x', y') rotates onto (x, y).
*
* Given a rotation angle theta, the ACTUAL position where the object
* will be located is ROT(theta) (x', y').
*
* So we want ROT(theta) (x', y') = (x, y), meaning that
* (x', y') = ROT(-theta)(x, y).
*/
GPoint textPos = rotation(-theta) * target;
/* GText rotations are in degrees. */
text.rotate(theta * 180 / M_PI);
text.setLocation(textPos);
canvas->draw(&text);
}
Node* ViewerBase::nodeAt(const GPoint& pt) {
/* TODO: Do we need to do this in reverse so that we pick the
* topmost node?
*/
for (auto node: nodes) {
if (isCloseTo(pt, node->position(), kNodeRadius)) {
return node.get();
}
}
return nullptr;
}
Edge* ViewerBase::edgeAt(const GPoint& pt) {
for (const auto& one: edges) {
for (const auto& two: one.second) {
/* Get the edge itself. */
auto edge = two.second;
if (edge->style->contains(pt)) {
return edge.get();
}
}
}
return nullptr;
}
void LineEdge::draw(GCanvas* canvas, double thickness, Color lineColor, Color labelColor) const {
editor->drawArrow(canvas, lineStart, lineEnd, thickness, lineColor);
editor->drawEdgeLabel(canvas, lineStart, lineEnd, edge->label(), labelColor, false);
}
bool LoopEdge::contains(const GPoint& pt) const {
/* We hit the circle if our distance to the center is within kHover of the
* actual radius.
*/
return fabs(magnitudeOf(pt - center) - kLoopEdgeRadius) < kEdgeTolerance;
}
void LoopEdge::draw(GCanvas* canvas, double width, Color lineColor, Color labelColor) const {
double size = 2 * editor->width * kLoopEdgeRadius;
GPoint pt = editor->worldToGraphics(center);
GOval toDraw(pt.x - size / 2, pt.y - size / 2, size, size);
toDraw.setColor(lineColor.toRGB());
toDraw.setLineWidth(ceil(editor->width * width));
canvas->draw(&toDraw);
/* Draw the arrowhead. You might think that we'd want the arrowhead
* to appear as though it was entering the node normal to the circle
* at the intersection point, but, surprisingly, that doesn't look good.
* Instead, it's better to look like you're hitting the the circle
* tangent to the line drawn between the node center and the loop
* center.
*/
GPoint exterior = arrowPt + (center - edge->from()->position());
editor->drawArrowhead(canvas, exterior, arrowPt, width, lineColor);
/* We will draw the edge contents by imagining there's an invisible tangent
* line to the circle that we'll draw on top of.
*/
/* Get a vector pointing away from the circle center. */
GVector out = normalizationOf(center - edge->from()->position());
/* Move outward to the end of the loop. */
GPoint tangentPoint = center + out * (kLoopEdgeRadius + kLoopEdgeYOffset);
/* Construct a perpendicular vector and use it to form a line. */
GVector tangent = rotate(out, M_PI / 2);
GPoint p0 = tangentPoint + tangent * kLoopLabelLength / 2;
GPoint p1 = tangentPoint - tangent * kLoopLabelLength / 2;
editor->drawEdgeLabel(canvas, p0, p1, edge->label(), labelColor, true);
}
void ViewerBase::forEachNode(std::function<void (Node *)> callback) {
for (const auto& node: nodes) {
callback(node.get());
}
}
void ViewerBase::forEachEdge(std::function<void (Edge *)> callback) {
for (const auto& e1: edges) {
for (const auto& e2: e1.second) {
callback(e2.second.get());
}
}
}
bool ViewerBase::hasEdge(Node* from, Node* to) {
return edgeBetween(from, to) != nullptr;
}
Edge* ViewerBase::edgeBetween(Node* from, Node* to) {
/* If the graph is undirected, get from and to into the right order. */
if (type() == Type::UNDIRECTED && from->index() >= to->index()) {
std::swap(from, to);
}
if (!edges.count(from) || !edges.at(from).count(to)) return nullptr;
return edges.at(from).at(to).get();
}
Type ViewerBase::type() const {
return mType;
}
void ViewerBase::type(Type type) {
/* This could be a no-op if the type already matches our underlying type. */
if (type == mType) return;
/* Converting undirected to directed is basically a no-op internally. */
if (type == Type::DIRECTED) {
// Nothing to do
}
/* Converting directed to undirected requires us to delete all self-loops
* and to break ties between pairs of edges.
*/
else if (type == Type::UNDIRECTED) {
std::vector<Edge*> toNix;
std::vector<Edge*> toFlip;
forEachEdge([&](Edge* edge) {
/* Ensure the invariant that from() < to() for all edges. */
if (edge->from()->index() >= edge->to()->index()) {
/* If the reverse edge exists, then delete it. This also handles
* self-loops.
*/
if (edgeBetween(edge->to(), edge->from())) {
toNix.push_back(edge);
} else {
toFlip.push_back(edge);
}
}
});
/* Delete all edges that need to be dealt with. */
for (Edge* edge: toNix) {
removeEdge(edge);
}
/* Flip all other edges. */
for (Edge* flip: toFlip) {
/* Invert the underlying map. */
edges[flip->to()][flip->from()] = edges[flip->from()][flip->to()];
edges[flip->from()][flip->to()].reset();
/* Change the underlying edge details. */
std::swap(flip->mFrom, flip->mTo);
}
}
/* Set the type for future reference. */
mType = type;
}
void ViewerBase::removeEdge(Edge* edge) {
edges[edge->from()].erase(edge->to());
calculateEdgeEndpoints();
}
Node* ViewerBase::nodeLabeled(const std::string& label) {
for (auto node: nodes) {
if (node->label() == label) return node.get();
}
return nullptr;
}
GRectangle ViewerBase::bounds() const {
return rawBounds;
}
GRectangle ViewerBase::computedBounds() const {
return { baseX, baseY, width, height };
}
size_t ViewerBase::numNodes() {
return nodes.size();
}
void ViewerBase::setBounds(const GRectangle& bounds) {
rawBounds = bounds;
/* Too narrow? */
if (bounds.width / bounds.height <= mAspectRatio) {
width = bounds.width;
height = width / mAspectRatio;
} else {
height = bounds.height;
width = height * mAspectRatio;
}
baseX = bounds.x + (bounds.width - width) / 2.0;
baseY = bounds.y + (bounds.height - height) / 2.0;
}
double ViewerBase::aspectRatio() {
return mAspectRatio;
}
void ViewerBase::aspectRatio(double ratio) {
mAspectRatio = ratio;
setBounds(rawBounds); // Recalculate to the last provided rectangle.
}
Node::Node(ViewerBase* editor, const NodeArgs& args, JSON)
: Node(editor, args) {
// Forwarded!
}
Node::Node(ViewerBase* editor, const NodeArgs& args)
: owner(editor), mPos(args.pt), mIndex(args.index), mLabel(args.label) {
owner->calculateEdgeEndpoints();
}
const std::string& Node::label() {
return mLabel;
}
void Node::label(const std::string& label) {
mLabel = label;
}
const GPoint& Node::position() {
return mPos;
}
size_t Node::index() {
return mIndex;
}
void Node::position(const GPoint& pt) {
/* Clamp to appropriate bounds. */
double x = pt.x;
if (x < kNodeRadius) x = kNodeRadius;
if (x > 1 - kNodeRadius) x = 1 - kNodeRadius;
double y = pt.y;
if (y < kNodeRadius) y = kNodeRadius;
if (y > 1 / owner->aspectRatio() - kNodeRadius) y = 1 / owner->aspectRatio() - kNodeRadius;
mPos = { x, y };
owner->calculateEdgeEndpoints();
}
void Node::draw(ViewerBase* editor, GCanvas* canvas, const NodeStyle& style) {
/* Calculate the size of the node. */
double size = 2.0 * style.radius;
auto bounds = editor->worldToGraphics({ position().x - size / 2.0, position().y - size / 2.0, size, size });
GOval mainNode(bounds.x, bounds.y, bounds.width, bounds.height);
mainNode.setFilled(true);
mainNode.setFillColor(style.fillColor.toRGB());
mainNode.setLineWidth(ceil(editor->worldToGraphics(style.lineWidth)));
mainNode.setColor(style.borderColor.toRGB());
canvas->draw(&mainNode);
/* Draw the node name. */
auto render = TextRender::construct(label(), bounds, kNodeFont.color(style.textColor));
render->alignCenterVertically();
render->alignCenterHorizontally();
render->draw(canvas);
}
Edge::Edge(ViewerBase* owner, const EdgeArgs& args, JSON)
: Edge(owner, args) {
// Forwarded
}
Edge::Edge(ViewerBase* owner, const EdgeArgs& args)
: mOwner(owner), mFrom(args.from), mTo(args.to), mLabel(args.label) {
owner->calculateEdgeEndpoints();
}
Node* Edge::to() {
return mTo;
}
Node* Edge::from() {
return mFrom;
}
std::string Edge::label() {
return mLabel;
}
void Edge::label(const std::string& label) {
mLabel = label;
}
/*** Serialization / Deserialization ***/
/* JSON format is
*
* {"nodes", [<node data>],
* "edges", [<edge data>],
* "aux", <aux data>,
* "type", <graph type>}
*
* Here, each node is encoded as
*
* { "index": <index>, "label": <label>, "pos": [<x>, <y>], "aux": <aux> }
*
* Each edge is encoded as
*
* { "from": <index>, "to": <index>, "label": <label> }
*/
JSON ViewerBase::nodesToJSON() {
std::vector<JSON> result;
for (auto node: nodes) {
result.push_back(toJSON(node.get()));
}
return result;
}
JSON ViewerBase::toJSON(Node* node) {
return JSON::object({
{ "index", node->index() },
{ "label", node->label() },
{ "pos", JSON::array(node->position().x, node->position().y) },
{ "aux", node->toJSON() }
});
}
JSON ViewerBase::edgesToJSON() {
std::vector<JSON> result;
forEachEdge([&](Edge* edge) {
result.push_back(toJSON(edge));
});
return result;
}
JSON ViewerBase::typeToJSON() {
if (mType == Type::DIRECTED) {
return "directed";
} else if (mType == Type::UNDIRECTED) {
return "undirected";
} else {
error("Unknown graph type?");
}
}
/* Default aux data is nothing at all. */
JSON ViewerBase::auxData() {
return nullptr;
}
JSON ViewerBase::toJSON(Edge* edge) {
return JSON::object({
{ "from", edge->from()->index() },
{ "to", edge->to()->index() },
{ "label", edge->label() },
{ "aux", edge->toJSON() }
});
}
/* Serializes to JSON. */
JSON ViewerBase::toJSON() {
/* Pair that with the serialized NFA. */
return JSON::object({
{ "nodes", nodesToJSON() },
{ "edges", edgesToJSON() },
{ "aux", auxData() },
{ "type", typeToJSON() }
});
}
/* Default serializers. */
JSON Node::toJSON() {
return nullptr;
}
JSON Edge::toJSON() {
return nullptr;
}
}