sameport.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /* [email protected], 9-Dec-1997
  11. * merge edges with specified samehead/sametail onto the same port
  12. */
  13. #include <cgraph/list.h>
  14. #include <math.h>
  15. #include <dotgen/dot.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include <util/streq.h>
  19. DEFINE_LIST(edge_list, edge_t*)
  20. typedef struct same_t {
  21. char *id; /* group id */
  22. edge_list_t l; // edges in the group
  23. } same_t;
  24. static void free_same(same_t s) {
  25. edge_list_free(&s.l);
  26. }
  27. DEFINE_LIST_WITH_DTOR(same_list, same_t, free_same)
  28. static void sameedge(same_list_t *same, edge_t *e, char *id);
  29. static void sameport(node_t *u, edge_list_t l);
  30. void dot_sameports(graph_t * g)
  31. /* merge edge ports in G */
  32. {
  33. node_t *n;
  34. edge_t *e;
  35. char *id;
  36. same_list_t samehead = {0};
  37. same_list_t sametail = {0};
  38. E_samehead = agattr(g, AGEDGE, "samehead", NULL);
  39. E_sametail = agattr(g, AGEDGE, "sametail", NULL);
  40. if (!(E_samehead || E_sametail))
  41. return;
  42. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  43. for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
  44. if (aghead(e) == agtail(e)) continue; /* Don't support same* for loops */
  45. if (aghead(e) == n && E_samehead &&
  46. (id = agxget(e, E_samehead))[0])
  47. sameedge(&samehead, e, id);
  48. else if (agtail(e) == n && E_sametail &&
  49. (id = agxget(e, E_sametail))[0])
  50. sameedge(&sametail, e, id);
  51. }
  52. for (size_t i = 0; i < same_list_size(&samehead); i++) {
  53. if (edge_list_size(&same_list_at(&samehead, i)->l) > 1)
  54. sameport(n, same_list_get(&samehead, i).l);
  55. }
  56. same_list_clear(&samehead);
  57. for (size_t i = 0; i < same_list_size(&sametail); i++) {
  58. if (edge_list_size(&same_list_at(&sametail, i)->l) > 1)
  59. sameport(n, same_list_get(&sametail, i).l);
  60. }
  61. same_list_clear(&sametail);
  62. }
  63. same_list_free(&samehead);
  64. same_list_free(&sametail);
  65. }
  66. /// register \p e in the \p same structure of the originating node under \p id
  67. static void sameedge(same_list_t *same, edge_t *e, char *id) {
  68. for (size_t i = 0; i < same_list_size(same); i++)
  69. if (streq(same_list_get(same, i).id, id)) {
  70. edge_list_append(&same_list_at(same, i)->l, e);
  71. return;
  72. }
  73. same_t to_append = {.id = id};
  74. edge_list_append(&to_append.l, e);
  75. same_list_append(same, to_append);
  76. }
  77. static void sameport(node_t *u, edge_list_t l)
  78. /* make all edges in L share the same port on U. The port is placed on the
  79. node boundary and the average angle between the edges. FIXME: this assumes
  80. naively that the edges are straight lines, which is wrong if they are long.
  81. In that case something like concentration could be done.
  82. An arr_port is also computed that's ARR_LEN away from the node boundary.
  83. It's used for edges that don't themselves have an arrow.
  84. */
  85. {
  86. node_t *v;
  87. edge_t *f;
  88. double x = 0, y = 0, x1, y1, x2, y2, r;
  89. /* Compute the direction vector (x,y) of the average direction. We compute
  90. with direction vectors instead of angles because else we have to first
  91. bring the angles within PI of each other. av(a,b)!=av(a,b+2*PI) */
  92. for (size_t i = 0; i < edge_list_size(&l); i++) {
  93. edge_t *e = edge_list_get(&l, i);
  94. if (aghead(e) == u)
  95. v = agtail(e);
  96. else
  97. v = aghead(e);
  98. x1 = ND_coord(v).x - ND_coord(u).x;
  99. y1 = ND_coord(v).y - ND_coord(u).y;
  100. r = hypot(x1, y1);
  101. x += x1 / r;
  102. y += y1 / r;
  103. }
  104. r = hypot(x, y);
  105. x /= r;
  106. y /= r;
  107. /* (x1,y1),(x2,y2) is a segment that must cross the node boundary */
  108. x1 = ND_coord(u).x;
  109. y1 = ND_coord(u).y; /* center of node */
  110. r = fmax(ND_lw(u) + ND_rw(u), ND_ht(u) + GD_ranksep(agraphof(u))); // far away
  111. x2 = x * r + ND_coord(u).x;
  112. y2 = y * r + ND_coord(u).y;
  113. { /* now move (x1,y1) to the node boundary */
  114. pointf curve[4]; /* bezier control points for a straight line */
  115. curve[0].x = x1;
  116. curve[0].y = y1;
  117. curve[1].x = (2 * x1 + x2) / 3;
  118. curve[1].y = (2 * y1 + y2) / 3;
  119. curve[2].x = (2 * x2 + x1) / 3;
  120. curve[2].y = (2 * y2 + y1) / 3;
  121. curve[3].x = x2;
  122. curve[3].y = y2;
  123. shape_clip(u, curve);
  124. x1 = curve[0].x - ND_coord(u).x;
  125. y1 = curve[0].y - ND_coord(u).y;
  126. }
  127. /* compute PORT on the boundary */
  128. port prt = {.p = {.x = round(x1), .y = round(y1)}};
  129. prt.bp = 0;
  130. prt.order =
  131. (MC_SCALE * (ND_lw(u) + prt.p.x)) / (ND_lw(u) + ND_rw(u));
  132. prt.constrained = false;
  133. prt.defined = true;
  134. prt.clip = false;
  135. prt.dyna = false;
  136. prt.theta = 0;
  137. prt.side = 0;
  138. prt.name = NULL;
  139. /* assign one of the ports to every edge */
  140. for (size_t i = 0; i < edge_list_size(&l); i++) {
  141. edge_t *e = edge_list_get(&l, i);
  142. for (; e; e = ED_to_virt(e)) { /* assign to all virt edges of e */
  143. for (f = e; f;
  144. f = ED_edge_type(f) == VIRTUAL &&
  145. ND_node_type(aghead(f)) == VIRTUAL &&
  146. ND_out(aghead(f)).size == 1 ?
  147. ND_out(aghead(f)).list[0] : NULL) {
  148. if (aghead(f) == u)
  149. ED_head_port(f) = prt;
  150. if (agtail(f) == u)
  151. ED_tail_port(f) = prt;
  152. }
  153. for (f = e; f;
  154. f = ED_edge_type(f) == VIRTUAL &&
  155. ND_node_type(agtail(f)) == VIRTUAL &&
  156. ND_in(agtail(f)).size == 1 ?
  157. ND_in(agtail(f)).list[0] : NULL) {
  158. if (aghead(f) == u)
  159. ED_head_port(f) = prt;
  160. if (agtail(f) == u)
  161. ED_tail_port(f) = prt;
  162. }
  163. }
  164. }
  165. ND_has_port(u) = true; /* kinda pointless, because mincross is already done */
  166. }