pos.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * Copyright 2021
  3. * based on priority algorithm from graphlet (C) Universitaet Passau 1986-1991
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * These are the four essential freedoms with GNU GPL software:
  19. * 1: freedom to run the program, for any purpose
  20. * 2: freedom to study how the program works, and change it to make it do what you wish
  21. * 3: freedom to redistribute copies to help your Free Software friends
  22. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  23. * , ,
  24. * / \
  25. * ((__-^^-,-^^-__))
  26. * `-_---' `---_-'
  27. * `--|o` 'o|--'
  28. * \ ` /
  29. * ): :(
  30. * :o_o:
  31. * "-"
  32. *
  33. * SPDX-License-Identifier: GPL-3.0+
  34. * License-Filename: LICENSE
  35. */
  36. /* this is a re-work of 1 file part of graphlet program */
  37. #include "config.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <math.h>
  41. #include "splay-tree.h"
  42. #include "main.h"
  43. #include "hier.h"
  44. #include "pos.h"
  45. #include "pos2.h"
  46. #include "pos3.h"
  47. #include "pos5.h"
  48. #include "dpmem.h"
  49. #include "dot.tab.h"
  50. /* min. distance between 2 nodes */
  51. static int mindist = 1;
  52. struct node_data {
  53. struct gml_node *node;
  54. int priority;
  55. int done;
  56. };
  57. static struct node_data *nl = NULL;
  58. static int is_dummy(struct gml_node *node)
  59. {
  60. if (node->dummy) {
  61. return (1);
  62. } else {
  63. return (0);
  64. }
  65. }
  66. /* how many connection edges from previous level */
  67. static int upper_connectivity(struct gml_node *node)
  68. {
  69. struct gml_elist *el = NULL;
  70. int result;
  71. result = 0;
  72. /* incoming edges for_targetlist(node,edge) */
  73. el = node->incoming_e;
  74. while (el) {
  75. result++;
  76. el = el->next;
  77. }
  78. return (result);
  79. }
  80. /* how many connection edges to next level */
  81. static int lower_connectivity(struct gml_node *node)
  82. {
  83. struct gml_elist *el = NULL;
  84. int result = 0;
  85. result = 0;
  86. /* outgoing edges for_sourcelist(node,edge) */
  87. el = node->outgoing_e;
  88. while (el) {
  89. result++;
  90. el = el->next;
  91. }
  92. return (result);
  93. }
  94. /* avg x pos of incoming edges */
  95. static int upper_barycenter(struct gml_node *node)
  96. {
  97. struct gml_elist *el = NULL;
  98. int result = 0;
  99. double r = 0.0;
  100. /* incoming edges x sum for_targetlist(node,edge) */
  101. el = node->incoming_e;
  102. while (el) {
  103. result += (el->edge->from_node->absx); /*old relx); */
  104. el = el->next;
  105. }
  106. if (result == 0) {
  107. r = (0.0);
  108. } else {
  109. r = (result / upper_connectivity(node));
  110. }
  111. if (0) {
  112. r = round(r);
  113. }
  114. return ((int)r);
  115. }
  116. /* avg x pos of outgoing edges */
  117. static int lower_barycenter(struct gml_node *node)
  118. {
  119. struct gml_elist *el = NULL;
  120. int result = 0;
  121. double r = 0.0;
  122. /* get avg. x pos of outgoing edges for_sourcelist(node,edge) */
  123. el = node->outgoing_e;
  124. while (el) {
  125. result += (el->edge->to_node->absx); /*old relx); */
  126. el = el->next;
  127. }
  128. if (result == 0) {
  129. r = (0.0);
  130. } else {
  131. r = (result / lower_connectivity(node));
  132. }
  133. if (0) {
  134. r = round(r);
  135. }
  136. return ((int)r);
  137. }
  138. static void sort(int n)
  139. {
  140. int i = 0;
  141. int j = 0;
  142. struct node_data h;
  143. for (j = n - 1; j > 0; j--) {
  144. for (i = 0; i < j; i++) {
  145. /* issue here */
  146. if (nl[i].node && nl[i + 1].node) {
  147. if (nl[i].node->relx > nl[i + 1].node->relx) {
  148. /* swap */
  149. h = nl[i];
  150. nl[i] = nl[i + 1];
  151. nl[i + 1] = h;
  152. }
  153. }
  154. }
  155. }
  156. return;
  157. }
  158. static void make_node_list_up(struct gml_graph *g, int l)
  159. {
  160. struct gml_nlist *gnl = NULL;
  161. struct gml_node *n = NULL;
  162. int i = 0;
  163. int prion = 0;
  164. /* for_all_nodes(g,n) */
  165. gnl = g->nodelist;
  166. while (gnl) {
  167. n = gnl->node;
  168. if (n->absy == l) {
  169. nl[i].node = n;
  170. nl[i].done = 0; /* FALSE */
  171. if (is_dummy(n)) {
  172. /* higer value then the highest node in this level */
  173. /*old nl[i].priority = (g->nnodes_of_level[l + 1] + 1000 */
  174. /*old nl[i].priority = (100000 - n->relx); */
  175. nl[i].priority = (1000000 + n->relx);
  176. } else {
  177. prion = lower_connectivity(n);
  178. prion += n->relx;
  179. if (n->elabel) {
  180. /* give edge labels higher prio then node, but lower then dummy nodes */
  181. nl[i].priority = (100000 + prion);
  182. } else {
  183. nl[i].priority = prion;
  184. }
  185. }
  186. i++;
  187. }
  188. gnl = gnl->next;
  189. }
  190. sort(g->nnodes_of_level[l]);
  191. return;
  192. }
  193. static void make_node_list_down(struct gml_graph *g, int l)
  194. {
  195. struct gml_nlist *gnl = NULL;
  196. struct gml_node *n = NULL;
  197. int i = 0;
  198. int prion = 0;
  199. /* for_all_nodes(g,n) */
  200. gnl = g->nodelist;
  201. while (gnl) {
  202. n = gnl->node;
  203. if (n->absy == l) {
  204. nl[i].node = n;
  205. nl[i].done = 0; /* FALSE */
  206. if (is_dummy(n)) {
  207. /* give dummy node uniq high number */
  208. /*old nl[i].priority = (g->nnodes_of_level[l - 1] + 1000 */
  209. /*old nl[i].priority = (100000 - n->relx); */
  210. nl[i].priority = (1000000 + n->relx);
  211. } else {
  212. prion = upper_connectivity(n);
  213. prion += n->relx;
  214. if (n->elabel) {
  215. /* give edge labels higher prio then node, but lower then dummy nodes */
  216. nl[i].priority = (100000 + prion);
  217. } else {
  218. nl[i].priority = prion;
  219. }
  220. }
  221. i++;
  222. }
  223. gnl = gnl->next;
  224. }
  225. sort(g->nnodes_of_level[l]);
  226. return;
  227. }
  228. /* get number of node with highest prio which is not done yet */
  229. static int find_next(int n)
  230. {
  231. int index = 0;
  232. int i = 0;
  233. int highest_priority = 0;
  234. for (i = 0; i < n; i++) {
  235. if ((nl[i].priority >= highest_priority)
  236. && (nl[i].done == 0 /* FALSE */ )) {
  237. index = i;
  238. highest_priority = nl[i].priority;
  239. }
  240. }
  241. return (index);
  242. }
  243. static void do_down(struct gml_graph *g, int l)
  244. {
  245. int i = 0;
  246. int index = 0;
  247. int j = 0;
  248. int optimal_position = 0;
  249. int distance = 0;
  250. int possible_distance = 0;
  251. for (i = 0; i < g->nnodes_of_level[l]; i++) {
  252. index = find_next(g->nnodes_of_level[l]);
  253. optimal_position = upper_barycenter(nl[index].node);
  254. if (optimal_position == 0) {
  255. optimal_position = nl[index].node->absx;
  256. }
  257. if (optimal_position < nl[index].node->absx) {
  258. distance = nl[index].node->absx - optimal_position;
  259. possible_distance = 0;
  260. j = index;
  261. do {
  262. if (j > 0) {
  263. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  264. } else {
  265. /* j==0, no nodes at left */
  266. possible_distance += nl[j].node->absx - mindist;
  267. }
  268. j--;
  269. }
  270. while ((j >= 0) && !(nl[j].done));
  271. if (possible_distance < distance) {
  272. distance = possible_distance;
  273. }
  274. j = index;
  275. while (distance > 0) {
  276. int d = 0;
  277. int k = 0;
  278. if (j == 0) {
  279. d = distance;
  280. } else {
  281. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  282. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  283. } else {
  284. d = distance;
  285. }
  286. }
  287. for (k = j; k <= index; k++) {
  288. nl[k].node->absx -= d;
  289. }
  290. j--;
  291. distance -= d;
  292. }
  293. } else {
  294. distance = optimal_position - nl[index].node->absx;
  295. possible_distance = 0;
  296. j = index;
  297. do {
  298. if (j < g->nnodes_of_level[l] - 1) {
  299. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  300. } else {
  301. /* j == g->nnodes_of_level[l]-1, no nodes rechts */
  302. possible_distance += distance;
  303. }
  304. j++;
  305. }
  306. while ((j < g->nnodes_of_level[l]) && !(nl[j].done));
  307. if (possible_distance < distance) {
  308. distance = possible_distance;
  309. }
  310. j = index;
  311. while (distance > 0) {
  312. int d = 0;
  313. int k = 0;
  314. if (j == g->nnodes_of_level[l] - 1) {
  315. d = distance;
  316. } else {
  317. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  318. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  319. } else {
  320. d = distance;
  321. }
  322. }
  323. for (k = index; k <= j; k++) {
  324. nl[k].node->absx += d;
  325. }
  326. j++;
  327. distance -= d;
  328. }
  329. }
  330. nl[index].done = 1; /* TRUE */
  331. }
  332. return;
  333. }
  334. static void do_up(struct gml_graph *g, int l)
  335. {
  336. int i = 0;
  337. int index = 0;
  338. int j = 0;
  339. int optimal_position = 0;
  340. int distance = 0;
  341. int possible_distance = 0;
  342. for (i = 0; i < g->nnodes_of_level[l]; i++) {
  343. index = find_next(g->nnodes_of_level[l]);
  344. optimal_position = lower_barycenter(nl[index].node);
  345. if (optimal_position == 0) {
  346. optimal_position = nl[index].node->absx;
  347. }
  348. if (optimal_position < nl[index].node->absx) {
  349. distance = nl[index].node->absx - optimal_position;
  350. possible_distance = 0;
  351. j = index;
  352. do {
  353. if (j > 0) {
  354. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  355. } else {
  356. /* j == 0, no nodes links */
  357. possible_distance += nl[0].node->absx - mindist;
  358. }
  359. j--;
  360. }
  361. while ((j >= 0) && !(nl[j].done));
  362. if (possible_distance < distance) {
  363. distance = possible_distance;
  364. }
  365. j = index;
  366. while (distance > 0) {
  367. int d = 0;
  368. int k = 0;
  369. if (j == 0) {
  370. d = distance;
  371. } else {
  372. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  373. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  374. } else {
  375. d = distance;
  376. }
  377. }
  378. for (k = j; k <= index; k++) {
  379. nl[k].node->absx -= d;
  380. }
  381. j--;
  382. distance -= d;
  383. }
  384. } else {
  385. /* optimal_position >= nl[index].node->absx */
  386. distance = optimal_position - nl[index].node->absx;
  387. possible_distance = 0;
  388. j = index;
  389. do {
  390. if (j < g->nnodes_of_level[l] - 1) {
  391. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  392. } else {
  393. /* j == g->nnodes_of_level[l]-1, no nodes rechts */
  394. possible_distance += distance;
  395. }
  396. j++;
  397. }
  398. while ((j < g->nnodes_of_level[l]) && !(nl[j].done));
  399. if (possible_distance < distance) {
  400. distance = possible_distance;
  401. }
  402. j = index;
  403. while (distance > 0) {
  404. int d = 0;
  405. int k = 0;
  406. if (j == g->nnodes_of_level[l] - 1) {
  407. d = distance;
  408. } else {
  409. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  410. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  411. } else {
  412. d = distance;
  413. }
  414. }
  415. for (k = index; k <= j; k++) {
  416. nl[k].node->absx += d;
  417. }
  418. j++;
  419. distance -= d;
  420. }
  421. }
  422. nl[index].done = 1; /* TRUE */
  423. }
  424. return;
  425. }
  426. /* determine relative node pos. from the barycenter rel. node pos. */
  427. static void improve_positions1(struct gml_graph *g)
  428. {
  429. struct gml_nlist *gnl = NULL;
  430. int i = 0;
  431. int count = 0;
  432. int ii = 0;
  433. int mx = 0;
  434. int sl = 0;
  435. /* this can happen */
  436. if (g->nnodes_of_level == NULL) {
  437. /* there is no number of nodes at level info, shouldnothappen */
  438. return;
  439. }
  440. if (g->nsinglenodes) {
  441. /* single nodes in level 0 and skip this level */
  442. sl = 1;
  443. } else {
  444. /* start level is 0 */
  445. sl = 0;
  446. }
  447. /* copy the rel(x,y) pos into abs(x,y) and modify the absx pos here */
  448. gnl = g->nodelist;
  449. while (gnl) {
  450. gnl->node->absx = gnl->node->relx;
  451. gnl->node->absy = gnl->node->rely;
  452. gnl->node->finx = 0;
  453. gnl->node->finy = 0;
  454. gnl->node->lx0 = -1; /* -1 means undefined */
  455. gnl->node->ly0 = -1;
  456. gnl->node->lx1 = -1;
  457. gnl->node->ly1 = -1;
  458. gnl = gnl->next;
  459. }
  460. /* these params can be tuned */
  461. /* min. node dist, minimum 1 */
  462. mindist = 1;
  463. /* number of up/down sweeps of priority placement algorithm, see sugiyama paper and book */
  464. count = 1;
  465. for (ii = 0; ii < count; ii++) {
  466. /* DOWN */
  467. for (i = sl; i <= g->maxlevel; i++) {
  468. if (g->nnodes_of_level[i]) {
  469. nl = (struct node_data *)dp_calloc(g->nnodes_of_level[i], sizeof(struct node_data));
  470. make_node_list_down(g, i);
  471. do_down(g, i);
  472. nl = dp_free(nl);
  473. if (nl) {
  474. }
  475. }
  476. }
  477. /* UP */
  478. for (i = (g->maxlevel - 1); i >= sl; i--) {
  479. if (g->nnodes_of_level[i]) {
  480. nl = (struct node_data *)dp_calloc(g->nnodes_of_level[i], sizeof(struct node_data));
  481. make_node_list_up(g, i);
  482. do_up(g, i);
  483. nl = dp_free(nl);
  484. if (nl) {
  485. }
  486. }
  487. }
  488. }
  489. /* top+bottom update */
  490. if ((sl + 2) < g->maxlevel) {
  491. for (i = sl + 2; i >= sl; i--) {
  492. if (g->nnodes_of_level[i]) {
  493. nl = (struct node_data *)dp_calloc(g->nnodes_of_level[i], sizeof(struct node_data));
  494. make_node_list_up(g, i);
  495. do_up(g, i);
  496. nl = dp_free(nl);
  497. if (nl) {
  498. }
  499. }
  500. }
  501. }
  502. for (i = g->maxlevel - 2; i <= g->maxlevel; i++) {
  503. if (i >= 0) {
  504. if (g->nnodes_of_level[i]) {
  505. nl = (struct node_data *)dp_calloc(g->nnodes_of_level[i], sizeof(struct node_data));
  506. make_node_list_down(g, i);
  507. do_down(g, i);
  508. nl = dp_free(nl);
  509. if (nl) {
  510. }
  511. }
  512. }
  513. }
  514. /* left-align the image */
  515. /* find min. x pos in-use */
  516. mx = 1024 * 1024; /* just some high value */
  517. if (g->nsinglenodes) {
  518. /* single nodes in level 0 and skip this level */
  519. gnl = g->nodelist;
  520. while (gnl) {
  521. /* only level 1...n */
  522. if (gnl->node->rely) {
  523. if (gnl->node->absx < mx) {
  524. mx = gnl->node->absx;
  525. }
  526. }
  527. gnl = gnl->next;
  528. }
  529. /* move whole drawing to the left */
  530. gnl = g->nodelist;
  531. while (gnl) {
  532. /* only level 1...n */
  533. if (gnl->node->rely) {
  534. gnl->node->absx = (gnl->node->absx - mx);
  535. }
  536. gnl = gnl->next;
  537. }
  538. } else {
  539. /* no single nodes and level 0 is in use for the drawing */
  540. gnl = g->nodelist;
  541. while (gnl) {
  542. if (gnl->node->absx < mx) {
  543. mx = gnl->node->absx;
  544. }
  545. gnl = gnl->next;
  546. }
  547. /* move whole drawing to the left */
  548. gnl = g->nodelist;
  549. while (gnl) {
  550. gnl->node->absx = (gnl->node->absx - mx);
  551. gnl = gnl->next;
  552. }
  553. }
  554. return;
  555. }
  556. /* switch between different modes
  557. * pos.c and pos2.c set in absx,absy relative coords
  558. * which are turned into real coords in finalxy()
  559. * pos3 set in absx,absy real coords and finalxy3()
  560. * copies that to finx,finy
  561. * todo add brandes algorithm
  562. * todo add horizontal compaction as graphviz does
  563. */
  564. void improve_positions(struct gml_graph *g)
  565. {
  566. struct gml_nlist *gnl = NULL;
  567. printf("%s(): positioning mode is %d\n", __func__, postype);
  568. fflush(stdout);
  569. if (g == NULL) { /* shouldnothappen */
  570. return;
  571. }
  572. /* the pos routines also must set new level info in ly0, ly1 used in the drawing routines */
  573. switch (postype) {
  574. case 1:
  575. improve_positions1(g);
  576. break;
  577. case 2:
  578. improve_positions2(g);
  579. break;
  580. case 3:
  581. improve_positions3(g);
  582. break;
  583. case 4: /* brandes algo */
  584. improve_positions5(g);
  585. break;
  586. default:
  587. /* shouldnothappen */
  588. improve_positions1(g);
  589. break;
  590. }
  591. if (yydebug || 0) {
  592. gnl = g->nodelist;
  593. while (gnl) {
  594. printf("%s(): node \"%s\" at level %d is at abs(%d,%d) fin(%d,%d) ly0=%d ly1=%d\n", __func__,
  595. gnl->node->name, gnl->node->rely, gnl->node->absx, gnl->node->absy, gnl->node->finx, gnl->node->finy,
  596. gnl->node->ly0, gnl->node->ly1);
  597. gnl = gnl->next;
  598. }
  599. }
  600. return;
  601. }
  602. /* end */