sfgs.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236
  1. /* simpified version not using splay() */
  2. /*
  3. * Copyright t lefering
  4. * parts are (C) Universitaet Passau 1986-1991
  5. * parts are Copyright (C) 1998-2021 Free Software Foundation, Inc.
  6. * parts are Copyright (C) Felix von Leitner from dietlibc
  7. *
  8. * https://notabug.org/mooigraph/sfgraph
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * These are the four essential freedoms with GNU GPL software:
  24. * 1: freedom to run the program, for any purpose
  25. * 2: freedom to study how the program works, and change it to make it do what you wish
  26. * 3: freedom to redistribute copies to help your Free Software friends
  27. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  28. * , ,
  29. * / \
  30. * ((__-^^-,-^^-__))
  31. * `-_---' `---_-'
  32. * `--|o` 'o|--'
  33. * \ ` /
  34. * ): :(
  35. * :o_o:
  36. * "-"
  37. *
  38. * SPDX-License-Identifier: GPL-3.0+
  39. * License-Filename: LICENSE
  40. */
  41. /* Single File Graph layout for directed graphs.
  42. * the api to use this file is in sfg.h
  43. * the demo program how to use is sfgdemo.c
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h> /* for calloc() free() */
  47. /* from <limits.h> for CHAR_BIT is 8 definition */
  48. #define CHAR_BIT 8
  49. /* min (x,y) spacing between nodes */
  50. #define NXSPACING 5
  51. #define NYSPACING 15
  52. #include "sfg.h"
  53. struct gml_graph;
  54. struct gml_node;
  55. struct gml_edge;
  56. struct gml_nlist;
  57. struct gml_elist;
  58. struct gml_graph {
  59. int layouted; /* set if layout is done */
  60. int nodenum; /* highest node number in use */
  61. int nnodes; /* number of nodes in the graph */
  62. int edgenum; /* highest edge number in use */
  63. int nedges; /* number of edges in the graph */
  64. int maxlevel; /* maximum relative level */
  65. int nedgelabels; /* number of edgelabels */
  66. int do_edgelabels; /* if set add edgelabels in the graph */
  67. int nsinglenodes; /* number of single nodes */
  68. int nhedges; /* number of hor edges */
  69. int startnodeslevel; /* level where graph drawing starts */
  70. int nstartnodes; /* number of start node numbers */
  71. int *startnodes; /* array with start node numbers */
  72. int xspacing; /* min x spacing between nodes */
  73. int yspacing; /* min y spacing between nodes */
  74. struct gml_nlist *nodelist; /* list of nodes */
  75. struct gml_nlist *nodelistend;
  76. struct gml_nlist *singlenodelist; /* list of single nodes */
  77. struct gml_nlist *singlenodelistend;
  78. struct gml_elist *edgelist; /* list of edges */
  79. struct gml_elist *edgelistend;
  80. int *nnodes_of_level; /* number of nodes for each level */
  81. int widestnnodes; /* widest number of nodes on one level */
  82. int widestlevel; /* widest level */
  83. int sugi_icrossings; /* initial crossings */
  84. int sugi_fcrossings; /* final crossings */
  85. int sugi_changes; /* sugiyama changes made */
  86. int *numce; /* number of crossings at every level */
  87. int *nume; /* number of edges */
  88. int maxx; /* max x pos of drawing */
  89. int maxy; /* max y pos of drawing */
  90. int nodemin; /* min. node number in use */
  91. int nodemax; /* max. node number in use */
  92. int edgemin; /* min. edge number in use */
  93. int edgemax; /* max. edge number in use */
  94. };
  95. struct gml_node {
  96. int nr; /* uniq node number */
  97. int tx; /* text xsize */
  98. int ty; /* text ysize */
  99. int bbx; /* text xsize */
  100. int bby; /* text ysize */
  101. int dummy; /* set to 1 if dummy node */
  102. int elabel; /* set if node is a edge label */
  103. int enumber; /* orig. edge number of the edge label */
  104. int nselfedges; /* number of self edges at this node */
  105. int done; /* dfs black/white */
  106. int grey; /* dfs grey */
  107. int indegree; /* incoming edges to node */
  108. int outdegree; /* outgoing edges from node */
  109. int hashedge; /* set if node has hor. edge */
  110. void *data; /* user data */
  111. int relx; /* relative xpos */
  112. int rely; /* relative ypos */
  113. int absx; /* absolute xpos */
  114. int absy; /* absolute ypos */
  115. int lx0; /* absolute xpos */
  116. int ly0; /* absolute xpos */
  117. int lx1; /* absolute ypos */
  118. int ly1; /* absolute ypos */
  119. int finx; /* absolute xpos */
  120. int finy; /* absolute ypos */
  121. struct gml_elist *outgoing_e; /* source list, outgoing edges */
  122. struct gml_elist *outgoing_etail; /* source list, outgoing edges */
  123. struct gml_elist *incoming_e; /* target list, incoming edges */
  124. struct gml_elist *incoming_etail; /* target list, incoming edges */
  125. int startnode; /* node belongs to part of graph with this startnode */
  126. struct gml_node *el_fnode; /* in edge-label the from-node */
  127. struct gml_node *el_tnode; /* in edge-label the to-node */
  128. };
  129. struct gml_edge {
  130. int nr; /* uniq edge number */
  131. struct gml_node *from_node; /* from node */
  132. struct gml_node *to_node; /* to node */
  133. int tx; /* text xsize */
  134. int ty; /* text ysize */
  135. int elabel; /* set if there is a edge label */
  136. int reversed; /* set if edge is reversed */
  137. int hedge; /* set if hor. edge */
  138. };
  139. struct gml_nlist {
  140. struct gml_node *node;
  141. struct gml_nlist *next;
  142. };
  143. struct gml_elist {
  144. struct gml_edge *edge;
  145. struct gml_elist *next;
  146. };
  147. /* local vars */
  148. static struct gml_graph *maingraph = NULL;
  149. static struct gml_node *uniqnode(struct gml_graph *g, int nr);
  150. static void uniqnode_add(struct gml_graph *g, struct gml_node *node);
  151. static void clear_nodelist(struct gml_graph *g);
  152. static void clear_edgelist(struct gml_graph *g);
  153. static void prep(struct gml_graph *g);
  154. static void reorg(struct gml_graph *g);
  155. static void uncycle(struct gml_graph *g);
  156. static void make_stlist(struct gml_graph *g);
  157. static void clear_stlist(struct gml_node *node);
  158. static void clear_stlist_all(struct gml_graph *g);
  159. static void ylevels(struct gml_graph *g);
  160. static void set_level2(struct gml_graph *g, struct gml_node *n, int i, int startnode);
  161. static void shorteredges(struct gml_graph *g);
  162. static void edgesdownwards(struct gml_graph *g);
  163. static void edgelen(struct gml_graph *g);
  164. static void doublespacey(struct gml_graph *g);
  165. static void edgelabels(struct gml_graph *g);
  166. static void splitedges(struct gml_graph *g);
  167. static void nodecounts(struct gml_graph *g);
  168. static void barycenter(struct gml_graph *g, int it1v, int it2v);
  169. static void improve_positions(struct gml_graph *g);
  170. static void finalxy(struct gml_graph *g);
  171. static struct gml_edge *findedge(int num);
  172. static void setminmax(struct gml_graph *g);
  173. /* returns a version number as version 1.0 returns int 10 */
  174. int sfg_version(void)
  175. {
  176. return (20);
  177. }
  178. /* init
  179. * returns 0 if oke
  180. * returns -1 if already inited
  181. * returns -2 if other error
  182. */
  183. int sfg_init(void)
  184. {
  185. if (maingraph) {
  186. return (-1);
  187. }
  188. maingraph = calloc(1, sizeof(struct gml_graph));
  189. if (maingraph == NULL) {
  190. return (-2);
  191. }
  192. /* min (x,y) spacing between nodes */
  193. maingraph->xspacing = NXSPACING;
  194. maingraph->yspacing = NYSPACING;
  195. maingraph->do_edgelabels = 1;
  196. return (0);
  197. }
  198. /* de-init
  199. * returns 0 if oke
  200. * returns -1 if not inited
  201. */
  202. int sfg_deinit(void)
  203. {
  204. if (maingraph == NULL) {
  205. return (-1);
  206. }
  207. if (maingraph->numce) {
  208. free(maingraph->numce);
  209. maingraph->numce = NULL;
  210. }
  211. if (maingraph->nnodes_of_level) {
  212. free(maingraph->nnodes_of_level);
  213. maingraph->nnodes_of_level = NULL;
  214. }
  215. if (maingraph->startnodes) {
  216. free(maingraph->startnodes);
  217. maingraph->startnodes = NULL;
  218. }
  219. clear_stlist_all(maingraph);
  220. clear_edgelist(maingraph);
  221. clear_nodelist(maingraph);
  222. free(maingraph);
  223. maingraph = NULL;
  224. return (0);
  225. }
  226. /* add a node with uniq number starting at 1
  227. * with (tx,ty) as rectangle size for label text or (0,0)
  228. * before adding edges all node numbers must be defined
  229. * returns 0 if oke
  230. * returns -1 if not inited
  231. * returns -2 if number is lower then 1
  232. * returns -3 if tx number is lower then 0
  233. * returns -4 if ty number is lower then 0
  234. * returns -5 if layout already done
  235. * returns -6 if node already defined
  236. * returns -7 if other error
  237. */
  238. int sfg_addnode(int number, int tx, int ty)
  239. {
  240. struct gml_node *nn = NULL;
  241. struct gml_nlist *nl = NULL;
  242. if (maingraph == NULL) {
  243. return (-1);
  244. }
  245. if (number < 1) {
  246. return (-2);
  247. }
  248. if (tx < 0) {
  249. return (-3);
  250. }
  251. if (ty < 0) {
  252. return (-4);
  253. }
  254. if (maingraph->layouted) {
  255. return (-5);
  256. }
  257. /* check if node does exist already */
  258. if (uniqnode(maingraph, number)) {
  259. return (-6);
  260. }
  261. /* create the new node */
  262. nn = calloc(1, sizeof(struct gml_node));
  263. if (nn == NULL) {
  264. return (-7);
  265. }
  266. nl = calloc(1, sizeof(struct gml_nlist));
  267. if (nl == NULL) {
  268. free(nn);
  269. return (-7);
  270. }
  271. nn->nr = number;
  272. nn->tx = tx;
  273. nn->ty = ty;
  274. /* data field is inited NULL and is set via other routine */
  275. nl->node = nn;
  276. if (maingraph->nodelist == NULL) {
  277. maingraph->nodelist = nl;
  278. maingraph->nodelistend = nl;
  279. } else {
  280. maingraph->nodelistend->next = nl;
  281. maingraph->nodelistend = nl;
  282. }
  283. if (number > maingraph->nodenum) {
  284. /* highest node number in use */
  285. maingraph->nodenum = number;
  286. }
  287. uniqnode_add(maingraph, nn);
  288. /* number of nodes in the graph */
  289. maingraph->nnodes++;
  290. return (0);
  291. }
  292. /* add a edge with uniq number starting at 1
  293. * the from-node number is in from, the to-node number is in to
  294. * self-edges are allowed but not with a label
  295. * with (tx,ty) as rectangle size for label text or (0,0)
  296. * returns 0 if oke
  297. * returns -1 if not inited
  298. * returns -2 if number is lower then 1
  299. * returns -3 if tx number is lower then 0
  300. * returns -4 if ty number is lower then 0
  301. * returns -5 if from-node number is not defined
  302. * returns -6 if to-node number is not defined
  303. * returns -7 if self-edge with a label
  304. * returns -8 if layout already done
  305. * returns -9 if other error
  306. */
  307. int sfg_addedge(int number, int from, int to, int tx, int ty)
  308. {
  309. struct gml_node *fn = NULL;
  310. struct gml_node *tn = NULL;
  311. struct gml_edge *e = NULL;
  312. struct gml_elist *el = NULL;
  313. if (maingraph == NULL) {
  314. return (-1);
  315. }
  316. if (number < 1) {
  317. return (-2);
  318. }
  319. if (tx < 0) {
  320. return (-3);
  321. }
  322. if (ty < 0) {
  323. return (-4);
  324. }
  325. if (from < 1) {
  326. return (-5);
  327. }
  328. if (to < 1) {
  329. return (-6);
  330. }
  331. if (from == to) {
  332. if (tx || ty) { /* do not allow self-edge with a label */
  333. return (-7);
  334. }
  335. }
  336. if (maingraph->layouted) {
  337. return (-8);
  338. }
  339. fn = uniqnode(maingraph, from);
  340. if (fn == NULL) {
  341. return (-5);
  342. }
  343. tn = uniqnode(maingraph, to);
  344. if (tn == NULL) {
  345. return (-6);
  346. }
  347. if (number > maingraph->edgenum) {
  348. maingraph->edgenum = number;
  349. }
  350. maingraph->nedges++;
  351. if (fn == tn) {
  352. /* at self-edge increase counter at node */
  353. fn->nselfedges++;
  354. } else {
  355. /* fresh new edge */
  356. e = calloc(1, sizeof(struct gml_edge));
  357. if (e == NULL) {
  358. return (-9);
  359. }
  360. el = calloc(1, sizeof(struct gml_elist));
  361. if (el == NULL) {
  362. free(e);
  363. return (-9);
  364. }
  365. e->nr = number;
  366. e->from_node = fn;
  367. e->to_node = tn;
  368. e->tx = tx;
  369. e->ty = ty;
  370. if (tx || ty) {
  371. /* mark there is a edgelabel */
  372. e->elabel = 1;
  373. /* number of edge labels in the graph */
  374. maingraph->nedgelabels++;
  375. }
  376. el->edge = e;
  377. if (maingraph->edgelist == NULL) {
  378. maingraph->edgelist = el;
  379. maingraph->edgelistend = el;
  380. } else {
  381. maingraph->edgelistend->next = el;
  382. maingraph->edgelistend = el;
  383. }
  384. }
  385. return (0);
  386. }
  387. /* run sugiyama barycenter layout
  388. * returns 0 if oke
  389. * returns -1 if not inited
  390. * returns -2 if layout already done
  391. * returns -3 if no nodes in the graph
  392. */
  393. int sfg_layout(void)
  394. {
  395. if (maingraph == NULL) {
  396. return (-1);
  397. }
  398. if (maingraph->layouted) {
  399. return (-2);
  400. }
  401. if (maingraph->nodelist == NULL) {
  402. return (-3);
  403. }
  404. /* prepare */
  405. prep(maingraph);
  406. /* re-organize nodelist */
  407. reorg(maingraph);
  408. /* change cycles in the graph */
  409. uncycle(maingraph);
  410. /* re-organize nodelist */
  411. reorg(maingraph);
  412. /* set y level of all nodes */
  413. ylevels(maingraph);
  414. /* find shorter edges */
  415. shorteredges(maingraph);
  416. /* change edge directions downwards */
  417. edgesdownwards(maingraph);
  418. /* check length of edges */
  419. edgelen(maingraph);
  420. /* doublespace the vertical levels */
  421. doublespacey(maingraph);
  422. /* split edges with label into node->label->node */
  423. edgelabels(maingraph);
  424. /* split longer edges */
  425. splitedges(maingraph);
  426. /* create level node count data */
  427. nodecounts(maingraph);
  428. /* run barycenter using defaults (0,0) or a value */
  429. barycenter(maingraph, 100, 100);
  430. /* re-calc positions */
  431. improve_positions(maingraph);
  432. /* final (x,y) positioning of nodes/edges */
  433. finalxy(maingraph);
  434. /* update node min/max and edge min/max */
  435. setminmax(maingraph);
  436. /* set layout is calculated */
  437. maingraph->layouted = 1;
  438. return (0);
  439. }
  440. /* return edge crossings in the graph
  441. * returns crossings >= 0 if oke
  442. * returns -1 if not inited
  443. * returns -2 if layout not done
  444. */
  445. int sfg_crossings(void)
  446. {
  447. if (maingraph == NULL) {
  448. return (-1);
  449. }
  450. if (maingraph->layouted == 0) {
  451. return (-22);
  452. }
  453. return (maingraph->sugi_fcrossings);
  454. }
  455. /* return initial edge crossings in the graph
  456. * returns crossings >= 0 if oke
  457. * returns -1 if not inited
  458. * returns -2 if layout not done
  459. */
  460. int sfg_initialcrossings(void)
  461. {
  462. if (maingraph == NULL) {
  463. return (-1);
  464. }
  465. if (maingraph->layouted == 0) {
  466. return (-2);
  467. }
  468. return (maingraph->sugi_icrossings);
  469. }
  470. /* set 1 to add edgelabels, or 0 to remove edgelabels
  471. * returns 0 if oke
  472. * returns -1 if not inited
  473. * returns -2 if already layouted
  474. */
  475. int sfg_edgelabels(int status)
  476. {
  477. if (maingraph == NULL) {
  478. return (-1);
  479. }
  480. if (maingraph->layouted) {
  481. return (-2);
  482. }
  483. if (status) {
  484. maingraph->do_edgelabels = 1;
  485. } else {
  486. maingraph->do_edgelabels = 1;
  487. }
  488. return (0);
  489. }
  490. /* return x pos of node with uniq number
  491. * returns >= 0 if oke
  492. * returns -1 if not inited
  493. * returns -2 if layout not done
  494. * returns -3 if number < 1
  495. * returns -4 if node not found
  496. */
  497. int sfg_nodexpos(int num)
  498. {
  499. struct gml_node *n = NULL;
  500. if (maingraph == NULL) {
  501. return (-1);
  502. }
  503. if (maingraph->layouted == 0) {
  504. return (-2);
  505. }
  506. if (num < 1) {
  507. return (-3);
  508. }
  509. n = uniqnode(maingraph, num);
  510. if (n == NULL) {
  511. return (-4);
  512. }
  513. return (n->finx);
  514. }
  515. /* return y pos of node with uniq number
  516. * returns >= 0 if oke
  517. * returns -1 if not inited
  518. * returns -2 if layout not done
  519. * returns -3 if number < 1
  520. * returns -4 if node not found
  521. */
  522. int sfg_nodeypos(int num)
  523. {
  524. struct gml_node *n = NULL;
  525. if (maingraph == NULL) {
  526. return (-1);
  527. }
  528. if (maingraph->layouted == 0) {
  529. return (-2);
  530. }
  531. if (num < 1) {
  532. return (-3);
  533. }
  534. n = uniqnode(maingraph, num);
  535. if (n == NULL) {
  536. return (-4);
  537. }
  538. return (n->finy);
  539. }
  540. /* return relative x pos of node with uniq number
  541. * returns >= 0 if oke
  542. * returns -1 if not inited
  543. * returns -2 if layout not done
  544. * returns -3 if number < 1
  545. * returns -4 if node not found
  546. */
  547. int sfg_noderelxpos(int num)
  548. {
  549. struct gml_node *n = NULL;
  550. if (maingraph == NULL) {
  551. return (-1);
  552. }
  553. if (maingraph->layouted == 0) {
  554. return (-2);
  555. }
  556. if (num < 1) {
  557. return (-3);
  558. }
  559. n = uniqnode(maingraph, num);
  560. if (n == NULL) {
  561. return (-4);
  562. }
  563. return (n->relx);
  564. }
  565. /* return relative y pos of node with uniq number
  566. * returns >= 0 if oke
  567. * returns -1 if not inited
  568. * returns -2 if layout not done
  569. * returns -3 if number < 1
  570. * returns -4 if node not found
  571. */
  572. int sfg_noderelypos(int num)
  573. {
  574. struct gml_node *n = NULL;
  575. if (maingraph == NULL) {
  576. return (-1);
  577. }
  578. if (maingraph->layouted == 0) {
  579. return (-2);
  580. }
  581. if (num < 1) {
  582. return (-3);
  583. }
  584. n = uniqnode(maingraph, num);
  585. if (n == NULL) {
  586. return (-4);
  587. }
  588. return (n->rely);
  589. }
  590. /* return level y start pos of node with uniq number
  591. * returns >= 0 if oke
  592. * returns -1 if not inited
  593. * returns -2 if layout not done
  594. * returns -3 if number < 1
  595. * returns -4 if node not found
  596. */
  597. int sfg_nodely0(int num)
  598. {
  599. struct gml_node *n = NULL;
  600. if (maingraph == NULL) {
  601. return (-1);
  602. }
  603. if (maingraph->layouted == 0) {
  604. return (-2);
  605. }
  606. if (num < 1) {
  607. return (-3);
  608. }
  609. n = uniqnode(maingraph, num);
  610. if (n == NULL) {
  611. return (-4);
  612. }
  613. return (n->ly0);
  614. }
  615. /* return level y end pos of node with uniq number
  616. * returns >= 0 if oke
  617. * returns -1 if not inited
  618. * returns -2 if layout not done
  619. * returns -3 if number < 1
  620. * returns -4 if node not found
  621. */
  622. int sfg_nodely1(int num)
  623. {
  624. struct gml_node *n = NULL;
  625. if (maingraph == NULL) {
  626. return (-1);
  627. }
  628. if (maingraph->layouted == 0) {
  629. return (-2);
  630. }
  631. if (num < 1) {
  632. return (-3);
  633. }
  634. n = uniqnode(maingraph, num);
  635. if (n == NULL) {
  636. return (-4);
  637. }
  638. return (n->ly1);
  639. }
  640. /* return x size of node with uniq number
  641. * returns >= 0 if oke
  642. * returns -1 if not inited
  643. * returns -2 if layout not done
  644. * returns -3 if number < 1
  645. * returns -4 if node not found
  646. */
  647. int sfg_nodexsize(int num)
  648. {
  649. struct gml_node *n = NULL;
  650. if (maingraph == NULL) {
  651. return (-1);
  652. }
  653. if (maingraph->layouted == 0) {
  654. return (-2);
  655. }
  656. if (num < 1) {
  657. return (-3);
  658. }
  659. n = uniqnode(maingraph, num);
  660. if (n == NULL) {
  661. return (-4);
  662. }
  663. return (n->tx);
  664. }
  665. /* return y size of node with uniq number
  666. * returns >= 0 if oke
  667. * returns -1 if not inited
  668. * returns -2 if layout not done
  669. * returns -3 if number < 1
  670. * returns -4 if node not found
  671. */
  672. int sfg_nodeysize(int num)
  673. {
  674. struct gml_node *n = NULL;
  675. if (maingraph == NULL) {
  676. return (-1);
  677. }
  678. if (maingraph->layouted == 0) {
  679. return (-2);
  680. }
  681. if (num < 1) {
  682. return (-3);
  683. }
  684. n = uniqnode(maingraph, num);
  685. if (n == NULL) {
  686. return (-4);
  687. }
  688. return (n->ty);
  689. }
  690. /* set min. x spacing between nodes
  691. * returns 0 if oke
  692. * returns -1 if not inited
  693. * returns -2 if number is lower then 0
  694. * returns -3 if layout already done
  695. */
  696. int sfg_xspacing(int num)
  697. {
  698. if (maingraph == NULL) {
  699. return (-1);
  700. }
  701. if (num < 1) {
  702. return (-2);
  703. }
  704. if (maingraph->layouted) {
  705. return (-3);
  706. }
  707. maingraph->xspacing = num;
  708. return (0);
  709. }
  710. /* set min. y spacing between nodes
  711. * returns 0 if oke
  712. * returns -1 if not inited
  713. * returns -2 if number is lower then 0
  714. * returns -3 if layout already done
  715. */
  716. int sfg_yspacing(int num)
  717. {
  718. if (maingraph == NULL) {
  719. return (-1);
  720. }
  721. if (num < 1) {
  722. return (-2);
  723. }
  724. if (maingraph->layouted) {
  725. return (-3);
  726. }
  727. maingraph->yspacing = num;
  728. return (0);
  729. }
  730. /* get max x pos in drawing
  731. * returns value if oke
  732. * returns -1 if not inited
  733. * returns -2 if layout not done
  734. */
  735. int sfg_maxx(void)
  736. {
  737. if (maingraph == NULL) {
  738. return (-1);
  739. }
  740. if (maingraph->layouted == 0) {
  741. return (-2);
  742. }
  743. return (maingraph->maxx);
  744. }
  745. /* get max y pos in drawing
  746. * returns value if oke
  747. * returns -1 if not inited
  748. * returns -2 if layout not done
  749. */
  750. int sfg_maxy(void)
  751. {
  752. if (maingraph == NULL) {
  753. return (-1);
  754. }
  755. if (maingraph->layouted == 0) {
  756. return (-2);
  757. }
  758. return (maingraph->maxy);
  759. }
  760. /* get min node number in use after layout
  761. * returns value if oke
  762. * returns -1 if not inited
  763. * returns -2 if layout not done
  764. * returns -3 if there are no nodes
  765. */
  766. int sfg_nodemin(void)
  767. {
  768. if (maingraph == NULL) {
  769. return (-1);
  770. }
  771. if (maingraph->layouted == 0) {
  772. return (-2);
  773. }
  774. if (maingraph->nodelist == NULL) {
  775. return (-3);
  776. }
  777. return (maingraph->nodemin);
  778. }
  779. /* get maxc node number in use after layout
  780. * returns value if oke
  781. * returns -1 if not inited
  782. * returns -2 if layout not done
  783. * returns -3 if there are no nodes
  784. */
  785. int sfg_nodemax(void)
  786. {
  787. if (maingraph == NULL) {
  788. return (-1);
  789. }
  790. if (maingraph->layouted == 0) {
  791. return (-2);
  792. }
  793. if (maingraph->nodelist == NULL) {
  794. return (-3);
  795. }
  796. return (maingraph->nodemax);
  797. }
  798. /* get min edge number in use after layout
  799. * returns value if oke
  800. * returns -1 if not inited
  801. * returns -2 if layout not done
  802. * returns -3 if there are no edges
  803. */
  804. int sfg_edgemin(void)
  805. {
  806. if (maingraph == NULL) {
  807. return (-1);
  808. }
  809. if (maingraph->layouted == 0) {
  810. return (-2);
  811. }
  812. if (maingraph->edgelist == NULL) {
  813. return (-3);
  814. }
  815. return (maingraph->edgemin);
  816. }
  817. /* get max edge number in use after layout
  818. * returns value if oke
  819. * returns -1 if not inited
  820. * returns -2 if layout not done
  821. * returns -3 if there are no edges
  822. */
  823. int sfg_edgemax(void)
  824. {
  825. if (maingraph == NULL) {
  826. return (-1);
  827. }
  828. if (maingraph->layouted == 0) {
  829. return (-2);
  830. }
  831. if (maingraph->edgelist == NULL) {
  832. return (-3);
  833. }
  834. return (maingraph->edgemax);
  835. }
  836. /* get number of levels
  837. * returns value if oke
  838. * returns -1 if not inited
  839. * returns -2 if layout not done
  840. */
  841. int sfg_nlevels(void)
  842. {
  843. if (maingraph == NULL) {
  844. return (-1);
  845. }
  846. if (maingraph->layouted == 0) {
  847. return (-2);
  848. }
  849. return (maingraph->maxlevel + 1);
  850. }
  851. /* get number of nodes
  852. * returns value if oke
  853. * returns -1 if not inited
  854. * returns -2 if layout not done
  855. */
  856. int sfg_nnodes(void)
  857. {
  858. if (maingraph == NULL) {
  859. return (-1);
  860. }
  861. if (maingraph->layouted == 0) {
  862. return (-2);
  863. }
  864. return (maingraph->nnodes);
  865. }
  866. /* get number of edges
  867. * returns value if oke
  868. * returns -1 if not inited
  869. * returns -2 if layout not done
  870. */
  871. int sfg_nedges(void)
  872. {
  873. if (maingraph == NULL) {
  874. return (-1);
  875. }
  876. if (maingraph->layouted == 0) {
  877. return (-2);
  878. }
  879. return (maingraph->nedges);
  880. }
  881. /* return type of node with uniq number
  882. * returns type of node, 1=regular, 2=dummy, 3=edgelabel node
  883. * returns -1 not inited
  884. * returns -2 if layout not done
  885. * returns -3 if nodenumber is < 1
  886. * returns -4 if node not found
  887. */
  888. int sfg_nodetype(int num)
  889. {
  890. struct gml_node *n = NULL;
  891. int type = 0;
  892. if (maingraph == NULL) {
  893. return (-1);
  894. }
  895. if (maingraph->layouted == 0) {
  896. return (-2);
  897. }
  898. if (num < 1) {
  899. return (-3);
  900. }
  901. n = uniqnode(maingraph, num);
  902. if (n == NULL) {
  903. return (-4);
  904. }
  905. if (n->dummy) {
  906. type = 2;
  907. } else if (n->elabel) {
  908. type = 3;
  909. } else {
  910. type = 1;
  911. }
  912. return (type);
  913. }
  914. /* return number of selfedges at node
  915. * returns number of selfedges if oke
  916. * returns -1 not inited
  917. * returns -2 if layout not done
  918. * returns -3 if nodenumber is < 1
  919. * returns -4 if node not found
  920. */
  921. int sfg_nodeselfedges(int num)
  922. {
  923. struct gml_node *n = NULL;
  924. if (maingraph == NULL) {
  925. return (-1);
  926. }
  927. if (maingraph->layouted == 0) {
  928. return (-2);
  929. }
  930. if (num < 1) {
  931. return (-3);
  932. }
  933. n = uniqnode(maingraph, num);
  934. if (n == NULL) {
  935. return (-4);
  936. }
  937. return (n->nselfedges);
  938. }
  939. /* return number of incoming edges to node
  940. * returns indegree number if oke
  941. * returns -1 not inited
  942. * returns -2 if layout not done
  943. * returns -3 if nodenumber is < 1
  944. * returns -4 if node not found
  945. */
  946. int sfg_nodeindegree(int num)
  947. {
  948. struct gml_node *n = NULL;
  949. if (maingraph == NULL) {
  950. return (-1);
  951. }
  952. if (maingraph->layouted == 0) {
  953. return (-2);
  954. }
  955. if (num < 1) {
  956. return (-3);
  957. }
  958. n = uniqnode(maingraph, num);
  959. if (n == NULL) {
  960. return (-4);
  961. }
  962. return (n->indegree);
  963. }
  964. /* return number of outgoing edges from node
  965. * returns outdegree number if oke
  966. * returns -1 not inited
  967. * returns -2 if layout not done
  968. * returns -3 if nodenumber is < 1
  969. * returns -4 if node not found
  970. */
  971. int sfg_nodeoutdegree(int num)
  972. {
  973. struct gml_node *n = NULL;
  974. if (maingraph == NULL) {
  975. return (-1);
  976. }
  977. if (maingraph->layouted == 0) {
  978. return (-2);
  979. }
  980. if (num < 1) {
  981. return (-3);
  982. }
  983. n = uniqnode(maingraph, num);
  984. if (n == NULL) {
  985. return (-4);
  986. }
  987. return (n->outdegree);
  988. }
  989. /* return edge number of node if edgelabel node
  990. * returns number of original edge with edgelabel if oke
  991. * returns -1 not inited
  992. * returns -2 if layout not done
  993. * returns -3 if nodenumber is < 1
  994. * returns -4 if node not found
  995. * returns -5 if node is not edgelabel
  996. */
  997. int sfg_nodeenum(int num)
  998. {
  999. struct gml_node *n = NULL;
  1000. if (maingraph == NULL) {
  1001. return (-1);
  1002. }
  1003. if (maingraph->layouted == 0) {
  1004. return (-2);
  1005. }
  1006. if (num < 1) {
  1007. return (-3);
  1008. }
  1009. n = uniqnode(maingraph, num);
  1010. if (n == NULL) {
  1011. return (-4);
  1012. }
  1013. if (n->elabel == 0) {
  1014. return (-5);
  1015. }
  1016. return (n->enumber);
  1017. }
  1018. /* get optional data pointer of node
  1019. * returns data pointer if oke
  1020. * returns NULL if not inited
  1021. * returns NULL if layout not done
  1022. * returns NULL if nodenumber is < 1
  1023. * returns NULL if node not found
  1024. */
  1025. void *sfg_nodedata(int num)
  1026. {
  1027. struct gml_node *n = NULL;
  1028. if (maingraph == NULL) {
  1029. return (NULL);
  1030. }
  1031. if (maingraph->layouted == 0) {
  1032. return (NULL);
  1033. }
  1034. if (num < 1) {
  1035. return (NULL);
  1036. }
  1037. n = uniqnode(maingraph, num);
  1038. if (n == NULL) {
  1039. return (NULL);
  1040. }
  1041. return (n->data);
  1042. }
  1043. /* set optional data pointer of node
  1044. * returns 0 if oke
  1045. * returns -1 if not inited
  1046. * returns -2 if layout not done
  1047. * returns -3 if nodenumber is < 1
  1048. * returns -4 if node not found
  1049. */
  1050. int sfg_setnodedata(int num, void *data)
  1051. {
  1052. struct gml_node *n = NULL;
  1053. if (maingraph == NULL) {
  1054. return (-1);
  1055. }
  1056. if (maingraph->layouted == 0) {
  1057. return (-2);
  1058. }
  1059. if (num < 1) {
  1060. return (-3);
  1061. }
  1062. n = uniqnode(maingraph, num);
  1063. if (n == NULL) {
  1064. return (-4);
  1065. }
  1066. n->data = data;
  1067. return (0);
  1068. }
  1069. /* get node data and the calculated positions
  1070. * the user must supply a pointer to the callback routine
  1071. * this runs a supplied callback routine for every node
  1072. * the callback routine gets the node number as supplied,
  1073. * the level as supplied and the calculated pos position.
  1074. * the data is the supplied data and can be used similar.
  1075. * when the callback function needs to stop the iteration
  1076. * over the node list then it must return a non-zero status
  1077. * and that status is returned by sfg_node_foreach()
  1078. * the parameters in the callback function are
  1079. * int num, uniq node number
  1080. * int level, relative vertical level
  1081. * int pos, relative horizontal level
  1082. * void *data, optional user data
  1083. * int xpos, x-coord of upperleft node label area or 0 at no label
  1084. * int ypos, y-coord of upperleft node label area or 0 at no label
  1085. * int tx, x size of text area
  1086. * int ty, y size of text area
  1087. * int nselfedges, number of self-edges at this node
  1088. * int type, type of node, 1=regular, 2=dummy, 3=edgelabel node
  1089. * int indegree, number of incoming edges to the node
  1090. * int outdegree, number of outgoing edges from the node
  1091. * int ly0, start y of level of node
  1092. * int ly1, end y of level of node
  1093. * returns 0 from this routine if everything is oke or no data.
  1094. * returns -1 if not inited
  1095. * returns -2 if no layout is done
  1096. * returns -3 if no callback routine
  1097. */
  1098. int sfg_node_foreach(int (*getnodedata)
  1099. (int num, int level, int pos, int xpos, int ypos, int tx, int ty, int nselfedges, int type,
  1100. int indegree, int outdegree, int ly0, int ly1))
  1101. {
  1102. struct gml_nlist *nl = NULL;
  1103. struct gml_node *n = NULL;
  1104. int status = 0;
  1105. int type = 0;
  1106. if (maingraph == NULL) {
  1107. return (-1);
  1108. }
  1109. if (maingraph->layouted == 0) {
  1110. return (-2);
  1111. }
  1112. if (getnodedata == NULL) {
  1113. return (-3);
  1114. }
  1115. nl = maingraph->nodelist;
  1116. while (nl) {
  1117. n = nl->node;
  1118. /* todo set type of node here */
  1119. type = 0;
  1120. status =
  1121. (*getnodedata) (n->nr, n->rely, n->relx, n->finx, n->finy, n->tx, n->ty, n->nselfedges, type,
  1122. n->indegree, n->outdegree, n->ly0, n->ly1);
  1123. if (status != 0) {
  1124. break;
  1125. }
  1126. nl = nl->next;
  1127. }
  1128. return (0);
  1129. }
  1130. /* get from-node of edge
  1131. * returns from-node number if oke
  1132. * returns -1 not inited
  1133. * returns -2 if layout not done
  1134. * returns -3 if edgenumber is < 1
  1135. * returns -4 if edge not found
  1136. */
  1137. int sfg_edgefrom(int num)
  1138. {
  1139. struct gml_edge *e = NULL;
  1140. if (maingraph == NULL) {
  1141. return (-1);
  1142. }
  1143. if (maingraph->layouted == 0) {
  1144. return (-2);
  1145. }
  1146. if (num < 1) {
  1147. return (-3);
  1148. }
  1149. e = findedge(num);
  1150. if (e == NULL) {
  1151. return (-4);
  1152. }
  1153. return (e->from_node->nr);
  1154. }
  1155. /* get to-node of edge
  1156. * returns to-node number if oke
  1157. * returns -1 not inited
  1158. * returns -2 if layout not done
  1159. * returns -3 if edgenumber is < 1
  1160. * returns -4 if edge not found
  1161. */
  1162. int sfg_edgeto(int num)
  1163. {
  1164. struct gml_edge *e = NULL;
  1165. if (maingraph == NULL) {
  1166. return (-1);
  1167. }
  1168. if (maingraph->layouted == 0) {
  1169. return (-2);
  1170. }
  1171. if (num < 1) {
  1172. return (-3);
  1173. }
  1174. e = findedge(num);
  1175. if (e == NULL) {
  1176. return (-4);
  1177. }
  1178. return (e->to_node->nr);
  1179. }
  1180. /* get edge type
  1181. * returns type if oke, 1=regular, 2=selfedge, 3=hor. edge
  1182. * returns -1 not inited
  1183. * returns -2 if layout not done
  1184. * returns -3 if edgenumber is < 1
  1185. * returns -4 if edge not found
  1186. */
  1187. int sfg_edgetype(int num)
  1188. {
  1189. struct gml_edge *e = NULL;
  1190. int type = 0;
  1191. if (maingraph == NULL) {
  1192. return (-1);
  1193. }
  1194. if (maingraph->layouted == 0) {
  1195. return (-2);
  1196. }
  1197. if (num < 1) {
  1198. return (-3);
  1199. }
  1200. e = findedge(num);
  1201. if (e == NULL) {
  1202. return (-4);
  1203. }
  1204. if (e->from_node->nr == e->to_node->nr) {
  1205. type = 2;
  1206. } else if (e->hedge) {
  1207. type = 3;
  1208. } else {
  1209. type = 1;
  1210. }
  1211. return (type);
  1212. }
  1213. /* get edge reversed status
  1214. * returns 1 if reversed edge or 0 if not
  1215. * returns -1 not inited
  1216. * returns -2 if layout not done
  1217. * returns -3 if edgenumber is < 1
  1218. * returns -4 if edge not found
  1219. */
  1220. int sfg_edgerev(int num)
  1221. {
  1222. struct gml_edge *e = NULL;
  1223. if (maingraph == NULL) {
  1224. return (-1);
  1225. }
  1226. if (maingraph->layouted == 0) {
  1227. return (-2);
  1228. }
  1229. if (num < 1) {
  1230. return (-3);
  1231. }
  1232. e = findedge(num);
  1233. if (e == NULL) {
  1234. return (-4);
  1235. }
  1236. return (e->reversed);
  1237. }
  1238. /* get edge data and the calculated position
  1239. * the user must supply a pointer to the callback routine
  1240. * this runs a supplied callback routine for every edge
  1241. * when the callback function needs to stop the iteration
  1242. * over the edge list then it must return a non-zero status
  1243. * the parameters in the callback function are
  1244. * int num, uniq edge number
  1245. * int from, uniq from-node number
  1246. * int to, uniq to-node number
  1247. * void *data, optional user data
  1248. * int type, 1=regular, 2=selfedge, 3 hor. edge
  1249. * int rev, set if edge is reversed
  1250. * returns 0 if oke
  1251. * returns -1 if not inited
  1252. * returns -2 if no layout is done
  1253. * returns -3 if no callback routine
  1254. */
  1255. int sfg_edge_foreach(int (*getedgedata)(int num, int from, int to, int type, int rev))
  1256. {
  1257. struct gml_elist *el = NULL;
  1258. struct gml_edge *e = NULL;
  1259. int status = 0;
  1260. int type = 0;
  1261. if (maingraph == NULL) {
  1262. return (-1);
  1263. }
  1264. if (maingraph->layouted == 0) {
  1265. return (-2);
  1266. }
  1267. if (getedgedata == NULL) {
  1268. return (-3);
  1269. }
  1270. el = maingraph->edgelist;
  1271. while (el) {
  1272. e = el->edge;
  1273. /* set type */
  1274. if (e->from_node->nr == e->to_node->nr) {
  1275. type = 2;
  1276. } else if (e->hedge) {
  1277. type = 3;
  1278. } else {
  1279. type = 1;
  1280. }
  1281. status = (*getedgedata) (e->nr, e->from_node->nr, e->to_node->nr, type, e->reversed);
  1282. if (status != 0) {
  1283. break;
  1284. }
  1285. el = el->next;
  1286. }
  1287. return (0);
  1288. }
  1289. /* get node with this number */
  1290. static struct gml_node *uniqnode(struct gml_graph *g, int nr)
  1291. {
  1292. struct gml_node *ret = NULL;
  1293. struct gml_nlist *nl = NULL;
  1294. if (g->nodelist == NULL) {
  1295. return (NULL);
  1296. }
  1297. nl = g->nodelist;
  1298. while (nl) {
  1299. if (nl->node->nr == nr) {
  1300. ret = nl->node;
  1301. break;
  1302. }
  1303. nl = nl->next;
  1304. }
  1305. return (ret);
  1306. }
  1307. /* add node to db */
  1308. static void uniqnode_add(struct gml_graph *g, struct gml_node *node)
  1309. {
  1310. if (g) {
  1311. }
  1312. if (node) {
  1313. }
  1314. return;
  1315. }
  1316. /* clear nodelist with the nodes */
  1317. static void clear_nodelist(struct gml_graph *g)
  1318. {
  1319. struct gml_nlist *lnll = NULL;
  1320. struct gml_nlist *nlnext = NULL;
  1321. lnll = g->nodelist;
  1322. while (lnll) {
  1323. nlnext = lnll->next;
  1324. free(lnll->node);
  1325. lnll->node = NULL;
  1326. free(lnll);
  1327. lnll = NULL;
  1328. lnll = nlnext;
  1329. }
  1330. g->nodelist = NULL;
  1331. g->nodelistend = NULL;
  1332. g->nodenum = 0;
  1333. g->nnodes = 0;
  1334. return;
  1335. }
  1336. /* clear edgelist and edge itself */
  1337. static void clear_edgelist(struct gml_graph *g)
  1338. {
  1339. struct gml_elist *el = NULL;
  1340. struct gml_elist *elnext = NULL;
  1341. el = g->edgelist;
  1342. while (el) {
  1343. elnext = el->next;
  1344. free(el->edge);
  1345. el->edge = NULL;
  1346. free(el);
  1347. el = NULL;
  1348. el = elnext;
  1349. }
  1350. g->edgelist = NULL;
  1351. g->edgelistend = NULL;
  1352. g->nedges = 0;
  1353. g->edgenum = 0;
  1354. return;
  1355. }
  1356. /* optional prepare extra's here */
  1357. static void prep(struct gml_graph *g)
  1358. {
  1359. struct gml_elist *el = NULL;
  1360. el = g->edgelist;
  1361. while (el) {
  1362. /* update degree of nodes */
  1363. el->edge->from_node->outdegree++;
  1364. el->edge->to_node->indegree++;
  1365. el = el->next;
  1366. }
  1367. return;
  1368. }
  1369. /* re-organize nodelist */
  1370. static void reorg(struct gml_graph *g)
  1371. {
  1372. struct gml_nlist *nl = NULL;
  1373. struct gml_nlist *nlnext = NULL;
  1374. struct gml_nlist *nn1 = NULL;
  1375. struct gml_nlist *nn2 = NULL;
  1376. struct gml_nlist *nn3 = NULL;
  1377. struct gml_nlist *nn4 = NULL;
  1378. struct gml_nlist *nnl = NULL;
  1379. struct gml_nlist *nnlend = NULL;
  1380. nl = g->nodelist;
  1381. if (nl == NULL) {
  1382. return;
  1383. }
  1384. while (nl) {
  1385. /* first the single nodes */
  1386. if (nl->node->indegree == 0 && nl->node->outdegree == 0) {
  1387. nn1 = calloc(1, sizeof(struct gml_nlist));
  1388. if (nn1) {
  1389. nn1->node = nl->node;
  1390. if (nnl == NULL) {
  1391. nnl = nn1;
  1392. nnlend = nn1;
  1393. } else {
  1394. nnlend->next = nn1;
  1395. nnlend = nn1;
  1396. }
  1397. }
  1398. }
  1399. nl = nl->next;
  1400. }
  1401. nl = g->nodelist;
  1402. while (nl) {
  1403. /* second the starter nodes */
  1404. if (nl->node->indegree == 0 && nl->node->outdegree != 0) {
  1405. nn2 = calloc(1, sizeof(struct gml_nlist));
  1406. if (nn2) {
  1407. nn2->node = nl->node;
  1408. if (nnl == NULL) {
  1409. nnl = nn2;
  1410. nnlend = nn2;
  1411. } else {
  1412. nnlend->next = nn2;
  1413. nnlend = nn2;
  1414. }
  1415. }
  1416. }
  1417. nl = nl->next;
  1418. }
  1419. nl = g->nodelist;
  1420. while (nl) {
  1421. /* third the middle nodes */
  1422. if (nl->node->indegree != 0 && nl->node->outdegree != 0) {
  1423. nn3 = calloc(1, sizeof(struct gml_nlist));
  1424. if (nn3) {
  1425. nn3->node = nl->node;
  1426. if (nnl == NULL) {
  1427. nnl = nn3;
  1428. nnlend = nn3;
  1429. } else {
  1430. nnlend->next = nn3;
  1431. nnlend = nn3;
  1432. }
  1433. }
  1434. }
  1435. nl = nl->next;
  1436. }
  1437. nl = g->nodelist;
  1438. while (nl) {
  1439. /* fourth the tail nodes */
  1440. if (nl->node->indegree != 0 && nl->node->outdegree == 0) {
  1441. nn4 = calloc(1, sizeof(struct gml_nlist));
  1442. if (nn4) {
  1443. nn4->node = nl->node;
  1444. if (nnl == NULL) {
  1445. nnl = nn4;
  1446. nnlend = nn4;
  1447. } else {
  1448. nnlend->next = nn4;
  1449. nnlend = nn4;
  1450. }
  1451. }
  1452. }
  1453. nl = nl->next;
  1454. }
  1455. /* clear nodelist but keep the nodes */
  1456. nl = g->nodelist;
  1457. while (nl) {
  1458. nlnext = nl->next;
  1459. free(nl);
  1460. nl = NULL;
  1461. nl = nlnext;
  1462. }
  1463. /* set the refreshed nodelist */
  1464. g->nodelist = nnl;
  1465. g->nodelistend = nnlend;
  1466. return;
  1467. }
  1468. /* recursive dfs */
  1469. static int decycle3(struct gml_graph *g, struct gml_node *n, int level, struct gml_edge *e)
  1470. {
  1471. struct gml_node *tmpnode = NULL;
  1472. struct gml_node *source = NULL;
  1473. struct gml_node *target = NULL;
  1474. struct gml_elist *el = NULL;
  1475. struct gml_edge *edge = NULL;
  1476. int changed = 0;
  1477. if (e) {
  1478. }
  1479. if (n->done) {
  1480. if (level > n->rely) {
  1481. n->rely = level;
  1482. }
  1483. return (0);
  1484. }
  1485. n->rely = level;
  1486. n->done = 1;
  1487. /* mark this node is being processed */
  1488. n->grey = 1;
  1489. source = n;
  1490. /* follow outgoing edges */
  1491. el = source->outgoing_e;
  1492. while (el) {
  1493. edge = el->edge;
  1494. /* get the to-node */
  1495. target = edge->to_node;
  1496. if (target->grey) {
  1497. changed++;
  1498. tmpnode = edge->to_node;
  1499. edge->to_node = edge->from_node;
  1500. edge->from_node = tmpnode;
  1501. /* toggle the edge is reversed bit */
  1502. if (edge->reversed) {
  1503. edge->reversed = 0;
  1504. } else {
  1505. edge->reversed = 1;
  1506. }
  1507. } else {
  1508. if (target->done == 0) {
  1509. changed += decycle3(g, target, (level + 1), e);
  1510. }
  1511. }
  1512. el = el->next;
  1513. }
  1514. /* this node is ready being processed */
  1515. n->grey = 0;
  1516. return (changed);
  1517. }
  1518. /* remove cycles in the graph */
  1519. static void uncycle(struct gml_graph *g)
  1520. {
  1521. struct gml_nlist *lnll = NULL;
  1522. int changed = 0;
  1523. /* build the s/tlist of a node */
  1524. clear_stlist_all(g);
  1525. make_stlist(g);
  1526. /* revert cycles at the last edge in the chain */
  1527. g->maxlevel = 0;
  1528. lnll = g->nodelist;
  1529. while (lnll) {
  1530. lnll->node->rely = -1;
  1531. lnll->node->done = 0;
  1532. lnll->node->grey = 0;
  1533. lnll = lnll->next;
  1534. }
  1535. /* first the startnodes */
  1536. lnll = g->nodelist;
  1537. changed = 0;
  1538. while (lnll) {
  1539. /* select start nodes */
  1540. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  1541. if (lnll->node->done == 0) {
  1542. /* use v3 */
  1543. changed += decycle3(g, lnll->node, 0, NULL);
  1544. }
  1545. }
  1546. lnll = lnll->next;
  1547. }
  1548. /* check nodes */
  1549. lnll = g->nodelist;
  1550. while (lnll) {
  1551. /* select other nodes */
  1552. if (lnll->node->rely == -1) {
  1553. /* use v3 */
  1554. changed += decycle3(g, lnll->node, 0, NULL);
  1555. }
  1556. lnll = lnll->next;
  1557. }
  1558. if (changed) {
  1559. /* build the s/tlist of a node */
  1560. clear_stlist_all(g);
  1561. make_stlist(g);
  1562. }
  1563. return;
  1564. }
  1565. /* rebuild nodes st lists */
  1566. static void make_stlist(struct gml_graph *g)
  1567. {
  1568. struct gml_elist *el = NULL;
  1569. struct gml_edge *edge = NULL;
  1570. struct gml_node *sn = NULL;
  1571. struct gml_node *tn = NULL;
  1572. struct gml_elist *ne = NULL;
  1573. struct gml_nlist *lnll = NULL;
  1574. /* refresh degree count of nodes */
  1575. lnll = g->nodelist;
  1576. while (lnll) {
  1577. /* make ure these are cleared */
  1578. lnll->node->outgoing_e = NULL; /* source list, outgoing edges */
  1579. lnll->node->outgoing_etail = NULL; /* source list, outgoing edges */
  1580. lnll->node->incoming_e = NULL; /* target list, incoming edges */
  1581. lnll->node->incoming_etail = NULL; /* target list, incoming edges */
  1582. lnll->node->indegree = 0;
  1583. lnll->node->outdegree = 0;
  1584. lnll = lnll->next;
  1585. }
  1586. el = g->edgelist;
  1587. while (el) {
  1588. edge = el->edge;
  1589. /* from/to nodes */
  1590. sn = edge->from_node;
  1591. tn = edge->to_node;
  1592. ne = calloc(1, sizeof(struct gml_elist));
  1593. if (ne == NULL) {
  1594. return;
  1595. }
  1596. ne->edge = edge;
  1597. /* list of outgoing edges */
  1598. if (sn->outgoing_e == NULL) {
  1599. sn->outgoing_e = ne;
  1600. sn->outgoing_etail = ne;
  1601. } else {
  1602. sn->outgoing_etail->next = ne;
  1603. sn->outgoing_etail = ne;
  1604. }
  1605. sn->outdegree++;
  1606. ne = calloc(1, sizeof(struct gml_elist));
  1607. if (ne == NULL) {
  1608. return;
  1609. }
  1610. ne->edge = edge;
  1611. /* list of incoming edges */
  1612. if (tn->incoming_e == NULL) {
  1613. tn->incoming_e = ne;
  1614. tn->incoming_etail = ne;
  1615. } else {
  1616. tn->incoming_etail->next = ne;
  1617. tn->incoming_etail = ne;
  1618. }
  1619. tn->indegree++;
  1620. el = el->next;
  1621. }
  1622. return;
  1623. }
  1624. /* clear the s/t list of a node */
  1625. static void clear_stlist(struct gml_node *node)
  1626. {
  1627. struct gml_elist *ell = NULL;
  1628. struct gml_elist *ellnext = NULL;
  1629. /* free outgoing edges */
  1630. ell = node->outgoing_e;
  1631. while (ell) {
  1632. ellnext = ell->next;
  1633. free(ell);
  1634. ell = NULL;
  1635. ell = ellnext;
  1636. }
  1637. node->outgoing_e = NULL;
  1638. node->outgoing_etail = NULL;
  1639. node->outdegree = 0;
  1640. /* free incoming edges */
  1641. ell = node->incoming_e;
  1642. while (ell) {
  1643. ellnext = ell->next;
  1644. free(ell);
  1645. ell = NULL;
  1646. ell = ellnext;
  1647. }
  1648. node->incoming_e = NULL;
  1649. node->incoming_etail = NULL;
  1650. node->indegree = 0;
  1651. return;
  1652. }
  1653. /* clear the s/t list of all nodes */
  1654. static void clear_stlist_all(struct gml_graph *g)
  1655. {
  1656. struct gml_nlist *lnll = NULL;
  1657. lnll = g->nodelist;
  1658. while (lnll) {
  1659. clear_stlist(lnll->node);
  1660. lnll = lnll->next;
  1661. }
  1662. return;
  1663. }
  1664. /* add node as single node */
  1665. static void add_singlenode(struct gml_graph *g, struct gml_node *node)
  1666. {
  1667. struct gml_nlist *lnll = NULL;
  1668. lnll = (struct gml_nlist *)calloc(1, sizeof(struct gml_nlist));
  1669. if (lnll) {
  1670. lnll->node = node;
  1671. if (g->singlenodelist == NULL) {
  1672. g->singlenodelist = lnll;
  1673. g->singlenodelistend = lnll;
  1674. } else {
  1675. g->singlenodelistend->next = lnll;
  1676. g->singlenodelistend = lnll;
  1677. }
  1678. }
  1679. return;
  1680. }
  1681. /* set rel. y level of all nodes */
  1682. static void ylevels(struct gml_graph *g)
  1683. {
  1684. struct gml_nlist *lnll = NULL;
  1685. int i = 0;
  1686. int start2 = 0;
  1687. int special = 0;
  1688. int nnodes = 0;
  1689. if (g->nodelist == NULL) {
  1690. /* nothing to do */
  1691. return;
  1692. }
  1693. /* no single nodes in the graph */
  1694. g->nsinglenodes = 0;
  1695. /* set all y levels to undefined */
  1696. lnll = g->nodelist;
  1697. nnodes = 0;
  1698. while (lnll) {
  1699. nnodes++;
  1700. /* y = -1, means undefined */
  1701. lnll->node->rely = -1;
  1702. lnll->node->done = 0;
  1703. lnll->node->grey = 0;
  1704. lnll->node->startnode = -1;
  1705. /* check for single nodes and mark them */
  1706. if (lnll->node->outgoing_e == NULL && lnll->node->incoming_e == NULL) {
  1707. /* set single nodes at fixed level 0 */
  1708. lnll->node->rely = 0;
  1709. lnll->node->done = 1;
  1710. /* node belongs to part of graph with this startnode */
  1711. lnll->node->startnode = 0;
  1712. g->nsinglenodes = (g->nsinglenodes + 1);
  1713. add_singlenode(g, lnll->node);
  1714. }
  1715. lnll = lnll->next;
  1716. }
  1717. /* if there are single nodes on level 0, start the graph at level 1 */
  1718. if (g->nsinglenodes) {
  1719. start2 = 1;
  1720. } else {
  1721. start2 = 0;
  1722. }
  1723. /* number of start nodes in the graph */
  1724. g->nstartnodes = 0;
  1725. /* where the actual drawing starts at y-level */
  1726. g->startnodeslevel = start2;
  1727. special = 0;
  1728. /* dfs */
  1729. lnll = g->nodelist;
  1730. while (lnll) {
  1731. if (lnll->node->rely == -1) {
  1732. /* select start nodes */
  1733. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  1734. g->nstartnodes++;
  1735. set_level2(g, lnll->node, start2, lnll->node->nr);
  1736. }
  1737. }
  1738. lnll = lnll->next;
  1739. }
  1740. /* check that all nodes have y position now */
  1741. lnll = g->nodelist;
  1742. while (lnll) {
  1743. if (lnll->node->rely == -1) {
  1744. set_level2(g, lnll->node, start2, lnll->node->nr);
  1745. }
  1746. lnll = lnll->next;
  1747. }
  1748. /* graph can have zero startnodes.
  1749. * set first node as startnode.
  1750. */
  1751. if (g->nstartnodes == 0) {
  1752. g->nstartnodes++;
  1753. if (g->nodelist) {
  1754. set_level2(g, g->nodelist->node, start2, g->nodelist->node->nr);
  1755. }
  1756. special = 1;
  1757. }
  1758. /* fill the table with startnodes */
  1759. g->startnodes = calloc(1, g->nstartnodes * sizeof(int));
  1760. if (g->startnodes == NULL) {
  1761. return;
  1762. }
  1763. /* special case if there were no startnodes */
  1764. if (special) {
  1765. /* set first node as startnode */
  1766. if (g->nodelist) {
  1767. g->startnodes[0] = g->nodelist->node->nr;
  1768. }
  1769. } else {
  1770. /* copy the startnodes numbers in the (int *)array */
  1771. i = 0;
  1772. lnll = g->nodelist;
  1773. while (lnll) {
  1774. /* no incoming edges and at least one outgoing edge */
  1775. if (lnll->node->indegree == 0 && lnll->node->outdegree != 0) {
  1776. /* set node number. this is not the id number in the input. */
  1777. g->startnodes[i] = lnll->node->nr;
  1778. i++;
  1779. }
  1780. lnll = lnll->next;
  1781. }
  1782. }
  1783. return;
  1784. }
  1785. /* set rel. y level of nodes */
  1786. static void set_level2(struct gml_graph *g, struct gml_node *n, int i, int startnode)
  1787. {
  1788. struct gml_node *target = NULL;
  1789. struct gml_edge *edge = NULL;
  1790. struct gml_elist *el = NULL;
  1791. if (n->done) {
  1792. if (i > n->rely && n->grey == 0) {
  1793. n->rely = i;
  1794. if (i > g->maxlevel) {
  1795. g->maxlevel = i;
  1796. }
  1797. }
  1798. if (n->grey) {
  1799. return;
  1800. }
  1801. if (n->done > 1) {
  1802. return;
  1803. }
  1804. }
  1805. n->grey++;
  1806. n->done++;
  1807. n->rely = i;
  1808. n->startnode = startnode;
  1809. if (i > g->maxlevel) {
  1810. g->maxlevel = i;
  1811. }
  1812. /* follow outgoing edges */
  1813. el = n->outgoing_e;
  1814. while (el) {
  1815. edge = el->edge;
  1816. target = edge->to_node;
  1817. set_level2(g, target, (i + 1), startnode);
  1818. el = el->next;
  1819. }
  1820. n->grey = 0;
  1821. return;
  1822. }
  1823. /* undo reversed edges and refresh node edgelists */
  1824. static void unrev(struct gml_graph *g)
  1825. {
  1826. struct gml_elist *el = NULL;
  1827. struct gml_node *tmpnode = NULL;
  1828. struct gml_node *sn = NULL;
  1829. struct gml_node *tn = NULL;
  1830. struct gml_edge *edge = NULL;
  1831. int changed = 0;
  1832. el = g->edgelist;
  1833. while (el) {
  1834. edge = el->edge;
  1835. if (el->edge->reversed) {
  1836. changed++;
  1837. sn = edge->from_node;
  1838. tn = edge->to_node;
  1839. /* swap */
  1840. tmpnode = tn;
  1841. el->edge->to_node = sn;
  1842. el->edge->from_node = tmpnode;
  1843. el->edge->reversed = 0;
  1844. }
  1845. el = el->next;
  1846. }
  1847. if (changed) {
  1848. /* rebuild the in/out edges lists at the nodes in the modified graph */
  1849. g->maxlevel = 0;
  1850. /* refresh st lists */
  1851. clear_stlist_all(g);
  1852. make_stlist(g);
  1853. }
  1854. return;
  1855. }
  1856. static int do_abs(int i)
  1857. {
  1858. if (i < 0) {
  1859. return (-i);
  1860. } else {
  1861. return (i);
  1862. }
  1863. }
  1864. /* try to find shorter edges */
  1865. static void shorteredges(struct gml_graph *g)
  1866. {
  1867. struct gml_nlist *lnll = NULL;
  1868. struct gml_elist *ine = NULL;
  1869. struct gml_elist *oute = NULL;
  1870. struct gml_edge *etop = NULL;
  1871. struct gml_edge *ebot = NULL;
  1872. struct gml_node *ntop = NULL;
  1873. struct gml_node *nbot = NULL;
  1874. /* undo reversed edges and refresh node edgelists */
  1875. unrev(g);
  1876. /* move in between nodes at halfway */
  1877. lnll = g->nodelist;
  1878. while (lnll) {
  1879. if ((lnll->node->indegree == 1) && (lnll->node->outdegree == 1)) {
  1880. oute = lnll->node->outgoing_e;
  1881. ine = lnll->node->incoming_e;
  1882. etop = ine->edge;
  1883. ebot = oute->edge;
  1884. ntop = etop->from_node;
  1885. nbot = ebot->to_node;
  1886. if ((do_abs(ntop->rely - lnll->node->rely) > 1)
  1887. || (do_abs(nbot->rely - lnll->node->rely) > 1)) {
  1888. /* put node at the middle, does not depend on edge direction up/down */
  1889. lnll->node->rely = ((ntop->rely + nbot->rely) / 2);
  1890. }
  1891. }
  1892. lnll = lnll->next;
  1893. }
  1894. return;
  1895. }
  1896. /* all edges downwards */
  1897. static void edgesdownwards(struct gml_graph *g)
  1898. {
  1899. struct gml_elist *el = NULL;
  1900. struct gml_node *tmpnode = NULL;
  1901. struct gml_node *sn = NULL;
  1902. struct gml_node *tn = NULL;
  1903. struct gml_edge *edge = NULL;
  1904. int changed1 = 0;
  1905. el = g->edgelist;
  1906. while (el) {
  1907. edge = el->edge;
  1908. sn = edge->from_node;
  1909. tn = edge->to_node;
  1910. if ((tn->rely - sn->rely) < 0) {
  1911. /* swap */
  1912. tmpnode = tn;
  1913. el->edge->to_node = el->edge->from_node;
  1914. el->edge->from_node = tmpnode;
  1915. /* toggle the edge is reversed bit */
  1916. if (el->edge->reversed) {
  1917. el->edge->reversed = 0;
  1918. } else {
  1919. el->edge->reversed = 1;
  1920. }
  1921. changed1++;
  1922. }
  1923. el = el->next;
  1924. }
  1925. if (changed1) {
  1926. /* rebuild the in/out edges lists at the nodes in the modified graph */
  1927. g->maxlevel = 0;
  1928. /* refresh st lists */
  1929. clear_stlist_all(g);
  1930. make_stlist(g);
  1931. }
  1932. return;
  1933. }
  1934. /* dfs check again and revers if needed */
  1935. static void edgelen(struct gml_graph *g)
  1936. {
  1937. struct gml_elist *el = NULL;
  1938. struct gml_edge *edge = NULL;
  1939. struct gml_node *sn = NULL;
  1940. struct gml_node *tn = NULL;
  1941. struct gml_node *tmpnode = NULL;
  1942. int changed = 0;
  1943. el = g->edgelist;
  1944. while (el) {
  1945. edge = el->edge;
  1946. sn = edge->from_node;
  1947. tn = edge->to_node;
  1948. if ((tn->rely - sn->rely) < 0) {
  1949. changed++;
  1950. tmpnode = tn;
  1951. el->edge->to_node = el->edge->from_node;
  1952. el->edge->from_node = tmpnode;
  1953. }
  1954. el = el->next;
  1955. }
  1956. if (changed) {
  1957. /* refresh st lists */
  1958. clear_stlist_all(g);
  1959. make_stlist(g);
  1960. }
  1961. return;
  1962. }
  1963. /* doublespace the vertical levels */
  1964. static void doublespacey(struct gml_graph *g)
  1965. {
  1966. struct gml_nlist *lnll = NULL;
  1967. /* same edges now will have different dummy nodes resulting in 2 lines */
  1968. g->maxlevel = 0;
  1969. /* at the odd levels the edge labels will be placed. */
  1970. lnll = g->nodelist;
  1971. while (lnll) {
  1972. lnll->node->rely = (2 * lnll->node->rely);
  1973. if (lnll->node->rely > g->maxlevel) {
  1974. g->maxlevel = lnll->node->rely;
  1975. }
  1976. lnll = lnll->next;
  1977. }
  1978. return;
  1979. }
  1980. /* dummy nodes are only in nodelist, not raw nodelist */
  1981. static void add_new_dummynode(struct gml_graph *g, int foundid)
  1982. {
  1983. struct gml_node *node = NULL;
  1984. struct gml_nlist *lnll = NULL;
  1985. if (uniqnode(maingraph, foundid)) {
  1986. return;
  1987. }
  1988. node = calloc(1, sizeof(struct gml_node));
  1989. if (node == NULL) {
  1990. return;
  1991. }
  1992. node->nr = foundid;
  1993. uniqnode_add(maingraph, node);
  1994. lnll = calloc(1, sizeof(struct gml_nlist));
  1995. if (lnll == NULL) {
  1996. free(node);
  1997. return;
  1998. }
  1999. lnll->node = node;
  2000. if (g->nodelist == NULL) {
  2001. g->nodelist = lnll;
  2002. g->nodelistend = lnll;
  2003. } else {
  2004. g->nodelistend->next = lnll;
  2005. g->nodelistend = lnll;
  2006. }
  2007. return;
  2008. }
  2009. /* edge to dummy node */
  2010. static void add_new_dummyedge(struct gml_graph *g, int foundsource, int foundtarget, int reversed)
  2011. {
  2012. struct gml_node *snode = NULL;
  2013. struct gml_node *tnode = NULL;
  2014. struct gml_edge *edge = NULL;
  2015. struct gml_elist *el = NULL;
  2016. snode = uniqnode(maingraph, foundsource);
  2017. if (snode == NULL) {
  2018. return;
  2019. }
  2020. tnode = uniqnode(maingraph, foundtarget);
  2021. if (tnode == NULL) {
  2022. return;
  2023. }
  2024. edge = calloc(1, sizeof(struct gml_edge));
  2025. if (edge == NULL) {
  2026. return;
  2027. }
  2028. g->edgenum++;
  2029. edge->nr = g->edgenum;
  2030. edge->from_node = snode; /* from-node */
  2031. edge->to_node = tnode; /* to-node */
  2032. edge->reversed = reversed; /* edge attr. edge-is-reversed */
  2033. el = calloc(1, sizeof(struct gml_elist));
  2034. if (el == NULL) {
  2035. free(edge);
  2036. return;
  2037. }
  2038. el->edge = edge;
  2039. if (g->edgelist == NULL) {
  2040. g->edgelist = el;
  2041. g->edgelistend = el;
  2042. } else {
  2043. g->edgelistend->next = el;
  2044. g->edgelistend = el;
  2045. }
  2046. return;
  2047. }
  2048. /* delete edge when replaced with a chain of edges */
  2049. static void del_edge(struct gml_graph *g, struct gml_elist *edgeel)
  2050. {
  2051. struct gml_elist *elprev = NULL;
  2052. struct gml_elist *el = NULL;
  2053. struct gml_elist *elto = NULL;
  2054. if (g->edgelist == NULL) {
  2055. return;
  2056. }
  2057. if (edgeel == g->edgelist) {
  2058. g->edgelist = g->edgelist->next;
  2059. if (g->edgelistend == edgeel) {
  2060. g->edgelistend = NULL;
  2061. } else {
  2062. el = g->edgelist;
  2063. elprev = el;
  2064. while (el) {
  2065. elprev = el;
  2066. if (el->next == edgeel) {
  2067. break;
  2068. }
  2069. el = el->next;
  2070. }
  2071. g->edgelistend = elprev;
  2072. }
  2073. free(edgeel->edge);
  2074. edgeel->edge = NULL;
  2075. free(edgeel);
  2076. } else {
  2077. elto = edgeel->next;
  2078. el = g->edgelist;
  2079. elprev = el;
  2080. while (el) {
  2081. elprev = el;
  2082. if (el->next == edgeel) {
  2083. break;
  2084. }
  2085. el = el->next;
  2086. }
  2087. elprev->next = elto;
  2088. if (g->edgelistend == edgeel) {
  2089. g->edgelistend = elprev;
  2090. }
  2091. free(edgeel->edge);
  2092. edgeel->edge = NULL;
  2093. free(edgeel);
  2094. }
  2095. return;
  2096. }
  2097. /* splits edgelabel edges into node->label->node */
  2098. static void edgelabels(struct gml_graph *g)
  2099. {
  2100. struct gml_elist *el = NULL;
  2101. struct gml_elist *elnext = NULL;
  2102. struct gml_node *ln = NULL;
  2103. char rev = 0;
  2104. int ydiff = 0;
  2105. int enumber = 0;
  2106. if (g->nedgelabels == 0) {
  2107. /* no edge labels, nothing todo here */
  2108. return;
  2109. }
  2110. if (g->do_edgelabels == 0) {
  2111. /* skip edge label processing */
  2112. return;
  2113. }
  2114. /* scan edges all downwards */
  2115. el = g->edgelist;
  2116. while (el) {
  2117. /* make sure, from-node is lower y then to-node */
  2118. if (el->edge->from_node->rely > el->edge->to_node->rely) {
  2119. ln = el->edge->from_node;
  2120. el->edge->from_node = el->edge->to_node;
  2121. el->edge->to_node = ln;
  2122. /* toggle rev flag */
  2123. if (el->edge->reversed) {
  2124. el->edge->reversed = 0;
  2125. } else {
  2126. el->edge->reversed = 1;
  2127. }
  2128. }
  2129. el = el->next;
  2130. }
  2131. /* scan edges for edge labels */
  2132. el = g->edgelist;
  2133. while (el) {
  2134. elnext = el->next;
  2135. /* do if there is a edge label */
  2136. if (el->edge->elabel) {
  2137. /* number of edge with edgelabel */
  2138. enumber = el->edge->nr;
  2139. /* edge attr. is-reversed */
  2140. rev = el->edge->reversed;
  2141. maingraph->nodenum++;
  2142. /* create label node */
  2143. add_new_dummynode(g, maingraph->nodenum);
  2144. /* mark this is a label node and set label text */
  2145. ln = uniqnode(maingraph, maingraph->nodenum);
  2146. /* edge-label-node, original from/to node */
  2147. ln->el_fnode = el->edge->from_node;
  2148. ln->el_tnode = el->edge->to_node;
  2149. /* y level difference between original from/to-nodes */
  2150. ydiff = (ln->el_tnode->rely - ln->el_fnode->rely);
  2151. /* put edge label halfway */
  2152. ln->rely = ln->el_fnode->rely + (ydiff / 2);
  2153. ln->elabel = 1; /* mark this is a edgelabel */
  2154. ln->dummy = 0;
  2155. /* set in the edgelabel node the number of the orig. edge */
  2156. ln->enumber = enumber;
  2157. /* set the size of the edge label text in the new node */
  2158. ln->tx = el->edge->tx;
  2159. ln->ty = el->edge->ty;
  2160. /* node belongs to graph with this startnode */
  2161. ln->startnode = el->edge->from_node->startnode;
  2162. /* create new edges with label node in between */
  2163. add_new_dummyedge(g, el->edge->from_node->nr, maingraph->nodenum, rev);
  2164. add_new_dummyedge(g, maingraph->nodenum, el->edge->to_node->nr, rev);
  2165. /* free old edge */
  2166. del_edge(g, el);
  2167. }
  2168. el = elnext;
  2169. }
  2170. /* refresh st lists */
  2171. clear_stlist_all(g);
  2172. make_stlist(g);
  2173. return;
  2174. }
  2175. /* split longer edges */
  2176. static void splitedges(struct gml_graph *g)
  2177. {
  2178. struct gml_elist *el = NULL;
  2179. struct gml_elist *elnext = NULL;
  2180. struct gml_edge *edge = NULL;
  2181. struct gml_node *sn = NULL;
  2182. struct gml_node *tn = NULL;
  2183. struct gml_node *nlnode = NULL;
  2184. int edgelen = 0;
  2185. int prevnodeid = 0;
  2186. int newid = 0;
  2187. int i = 0;
  2188. int sny = 0;
  2189. char rev = 0;
  2190. el = g->edgelist;
  2191. while (el) {
  2192. elnext = el->next;
  2193. edge = el->edge;
  2194. sn = edge->from_node; /* from-node */
  2195. tn = edge->to_node; /* to-node */
  2196. rev = edge->reversed; /* edge attr. to copy when splitting edge */
  2197. edgelen = (tn->rely - sn->rely);
  2198. /* horizontal edge */
  2199. if (edgelen == 0) {
  2200. /* horizontal edge has original endpoints, used in drawing edges */
  2201. edge->hedge = 1;
  2202. g->nhedges++; /* number of horizontal edges */
  2203. /* mark that nodes have a hor. edge */
  2204. sn->hashedge = 1;
  2205. tn->hashedge = 1;
  2206. } else if (edgelen > 1) {
  2207. prevnodeid = sn->nr;
  2208. sny = sn->rely;
  2209. for (i = 1; i < edgelen; i++) {
  2210. /* dummy node numbers start at first free node nr number */
  2211. maingraph->nodenum++;
  2212. newid = maingraph->nodenum;
  2213. add_new_dummynode(maingraph, newid);
  2214. nlnode = uniqnode(maingraph, newid);
  2215. nlnode->dummy = 1; /* this is a dummy node */
  2216. nlnode->elabel = 0; /* not a edgelabel */
  2217. nlnode->rely = (sny + i);
  2218. nlnode->startnode = sn->startnode;
  2219. add_new_dummyedge(g, prevnodeid, newid, rev);
  2220. prevnodeid = newid;
  2221. }
  2222. add_new_dummyedge(g, prevnodeid, tn->nr, rev);
  2223. del_edge(g, el);
  2224. } else if (edgelen == 1) {
  2225. /* edge len is 1 oke. */
  2226. } else {
  2227. /* shouldnothappen */
  2228. }
  2229. el = elnext;
  2230. }
  2231. return;
  2232. }
  2233. /* create level node count data */
  2234. static void nodecounts(struct gml_graph *g)
  2235. {
  2236. struct gml_nlist *lnll = NULL;
  2237. /* refresh st lists */
  2238. clear_stlist_all(g);
  2239. make_stlist(g);
  2240. /* table with number of nodes per level */
  2241. g->nnodes_of_level = calloc((g->maxlevel + 1), sizeof(int));
  2242. if (g->nnodes_of_level == NULL) {
  2243. return;
  2244. }
  2245. /* determine widest level and how many nodes it has */
  2246. g->widestlevel = 0;
  2247. g->widestnnodes = 0;
  2248. lnll = g->nodelist;
  2249. while (lnll) {
  2250. /* rely used for sugi */
  2251. g->nnodes_of_level[lnll->node->rely] = g->nnodes_of_level[lnll->node->rely] + 1;
  2252. /* x used for sugi, offset 1...n */
  2253. lnll->node->relx = g->nnodes_of_level[lnll->node->rely];
  2254. if (g->nnodes_of_level[lnll->node->rely] >= g->widestnnodes) {
  2255. g->widestnnodes = g->nnodes_of_level[lnll->node->rely];
  2256. g->widestlevel = lnll->node->rely;
  2257. }
  2258. lnll = lnll->next;
  2259. }
  2260. return;
  2261. }
  2262. struct mmatrix {
  2263. int level; /* upper level */
  2264. int nrows; /* nr. of rows (in upper level) */
  2265. int ncols; /* nr. of cols (in level+1) */
  2266. int nbytes; /* bytes used for matrix */
  2267. int *mi0; /* row node id's level i */
  2268. int nmi0; /* how many byte's in mi0 */
  2269. int *m0i; /* col node id's level (i+1) */
  2270. int nm0i; /* how many bytes in m0i */
  2271. int bbytes; /* bytes for double barycenter values */
  2272. double *b; /* buffer barycenter values */
  2273. unsigned char *bits; /* matrix bits */
  2274. };
  2275. static inline void setbit(unsigned char a[], int k)
  2276. {
  2277. if (k == 0) {
  2278. } else {
  2279. a[k / CHAR_BIT] |= (1 << (k % CHAR_BIT));
  2280. }
  2281. return;
  2282. }
  2283. static inline void clearbit(unsigned char a[], int k)
  2284. {
  2285. if (k == 0) {
  2286. } else {
  2287. a[k / CHAR_BIT] &= ~(1 << (k % CHAR_BIT));
  2288. }
  2289. return;
  2290. }
  2291. static inline int testbit(struct mmatrix *m, unsigned char a[], int k)
  2292. {
  2293. int ret = 0;
  2294. unsigned int mask = 0;
  2295. unsigned int mask2 = 0;
  2296. unsigned int i = 0;
  2297. if (k == 0) {
  2298. }
  2299. /* todo here tofix */
  2300. if (k > ((m->ncols + 1) * (m->nrows + 1))) {
  2301. }
  2302. /* compiler issue: the use of << is undefined here */
  2303. /* issue CHAR_BIT is often 8 but does not have to be 8 */
  2304. mask = (k % CHAR_BIT);
  2305. /*old: mask2 = (1 << mask); */
  2306. mask2 = 1;
  2307. for (i = 0; i < mask; i++) {
  2308. mask2 = (mask2 * 2);
  2309. }
  2310. ret = ((a[k / CHAR_BIT]) & mask2);
  2311. /*old return ((a[k / CHAR_BIT] & (1 << (k % CHAR_BIT))) != 0); */
  2312. return (ret);
  2313. }
  2314. /* i cols, j rows */
  2315. static inline int mget(struct mmatrix *m, int i, int j)
  2316. {
  2317. return (testbit(m, m->bits, ((i * (m->ncols + 0)) + j)));
  2318. }
  2319. /* i is the from-node, j is the to-node, value is 1 for edge, otherwise 0 */
  2320. static inline void mget_set(struct mmatrix *m, int i, int j, int value)
  2321. {
  2322. if (value) {
  2323. setbit(m->bits, ((i * (m->ncols + 0)) + j));
  2324. } else {
  2325. clearbit(m->bits, ((i * (m->ncols + 0)) + j));
  2326. }
  2327. return;
  2328. }
  2329. static int number_of_crossings2(struct mmatrix *m, int r, int c)
  2330. {
  2331. int j = 0;
  2332. int k = 0;
  2333. int alpha = 0;
  2334. int beta = 1;
  2335. int result = 0;
  2336. for (j = 1; j <= r - 1; j++) {
  2337. for (k = j + 1; k <= r; k++) {
  2338. for (alpha = 1; alpha <= c - 1; alpha++) {
  2339. for (beta = alpha + 1; beta <= c; beta++) {
  2340. /* here 1*0=0, 0*1=0, 0*0=0 or 1*1=1 */
  2341. result = result + ((mget(m, j, beta) * mget(m, k, alpha)));
  2342. }
  2343. }
  2344. }
  2345. }
  2346. return (result);
  2347. }
  2348. static int number_of_crossings3(struct mmatrix *m, int r, int c)
  2349. {
  2350. int j = 0;
  2351. int k = 0;
  2352. int alpha = 0;
  2353. int beta = 1;
  2354. int result2 = 0;
  2355. if (0) {
  2356. result2 = number_of_crossings2(m, r, c);
  2357. }
  2358. for (j = 1; j <= (r - 1); j++) {
  2359. for (k = (j + 1); k <= r; k++) {
  2360. for (alpha = 1; alpha <= (c - 1); alpha++) {
  2361. /* */
  2362. if (mget(m, k, alpha)) {
  2363. for (beta = alpha + 1; beta <= c; beta++) {
  2364. /* */
  2365. if (mget(m, j, beta)) {
  2366. result2++;
  2367. }
  2368. }
  2369. }
  2370. /* */
  2371. }
  2372. }
  2373. }
  2374. return (result2);
  2375. }
  2376. /* number of crossings in whole graph */
  2377. static int number_of_crossings_a(struct gml_graph *g, struct mmatrix **mm)
  2378. {
  2379. int ktot = 0;
  2380. int k = 0;
  2381. int i = 0;
  2382. for (i = 0; i < g->maxlevel; i++) {
  2383. if (mm[i]) {
  2384. k = number_of_crossings3(mm[i], mm[i]->nrows, mm[i]->ncols);
  2385. /* save number of edge crossings at level */
  2386. g->numce[i] = k;
  2387. ktot = ktot + k;
  2388. }
  2389. }
  2390. return (ktot);
  2391. }
  2392. /* configure matrix data for level l in the graph */
  2393. static void make_matrix(struct gml_graph *g, int l, struct mmatrix *m)
  2394. {
  2395. struct gml_nlist *nl = NULL;
  2396. struct gml_elist *el = NULL;
  2397. int i = 0;
  2398. int j = 0;
  2399. int c = 0;
  2400. int r = 0;
  2401. /* add node numbers in the 0 position */
  2402. nl = g->nodelist;
  2403. while (nl) {
  2404. /* if (level(n) == l) */
  2405. if (nl->node->rely == l) {
  2406. /* rows */
  2407. i = nl->node->relx;
  2408. m->mi0[i] = nl->node->nr; /* uniq node number, not id */
  2409. } else if (nl->node->rely == (l + 1)) {
  2410. /* columns */
  2411. j = nl->node->relx;
  2412. m->m0i[j] = nl->node->nr; /* uniq node number, not id */
  2413. }
  2414. nl = nl->next;
  2415. }
  2416. /* matrix bits config, first init then set bits. */
  2417. r = m->nrows;
  2418. c = m->ncols;
  2419. for (i = 1; i <= r; i++) {
  2420. for (j = 1; j <= c; j++) {
  2421. mget_set(m, i, j, 0);
  2422. }
  2423. }
  2424. nl = g->nodelist;
  2425. while (nl) {
  2426. /* if (level(n) == l) */
  2427. if (nl->node->rely == l) {
  2428. /* outgoing edges : for_sourcelist (n, e) */
  2429. el = nl->node->outgoing_e;
  2430. while (el) {
  2431. /* skip the horizontal edges */
  2432. if (el->edge->hedge == 0) {
  2433. /* from-node rel. x pos */
  2434. i = nl->node->relx;
  2435. /* to-node rel. x pos */
  2436. j = el->edge->to_node->relx;
  2437. /* set this is edge */
  2438. mget_set(m, i, j, 1);
  2439. }
  2440. el = el->next;
  2441. }
  2442. }
  2443. nl = nl->next;
  2444. }
  2445. return;
  2446. }
  2447. /* find node with given id number */
  2448. static struct gml_node *su_find_node_with_number(struct gml_graph *g, int nr)
  2449. {
  2450. return (uniqnode(g, nr));
  2451. }
  2452. static void store_new_positions(struct gml_graph *g, struct mmatrix *m, int level)
  2453. {
  2454. struct gml_node *n = NULL;
  2455. int i = 0;
  2456. if (level) {
  2457. }
  2458. if (m == NULL) {
  2459. return;
  2460. }
  2461. for (i = 1; i <= m->nrows; i++) {
  2462. /* rows */
  2463. n = su_find_node_with_number(g, m->mi0[i]);
  2464. if (n) {
  2465. /* offset is 1, make it into 0..n */
  2466. n->relx = (i - 1);
  2467. } else {
  2468. }
  2469. }
  2470. for (i = 1; i <= m->ncols; i++) {
  2471. /* columns */
  2472. n = su_find_node_with_number(g, m->m0i[i]);
  2473. if (n) {
  2474. /* offset is 1, make it into 0..n */
  2475. n->relx = (i - 1);
  2476. } else {
  2477. }
  2478. }
  2479. return;
  2480. }
  2481. /* parts are Copyright (C) Felix von Leitner from dietlibc */
  2482. static void *do_memmove(void *dst, void *src, size_t count)
  2483. {
  2484. char *a = dst;
  2485. char *b = src;
  2486. if (src != dst) {
  2487. if (src > dst) {
  2488. while (count--)
  2489. *a++ = *b++;
  2490. } else {
  2491. a += count - 1;
  2492. b += count - 1;
  2493. while (count--)
  2494. *a-- = *b--;
  2495. }
  2496. }
  2497. return dst;
  2498. }
  2499. /* copy matrix m1 to m2 */
  2500. static void copy_m(struct mmatrix *m1, struct mmatrix *m2)
  2501. {
  2502. if (m1 && m2) {
  2503. m2->level = m1->level; /* upper level */
  2504. m2->nrows = m1->nrows; /* nr. of rows (in upper level) */
  2505. m2->ncols = m1->ncols; /* nr. of cols (in level+1) */
  2506. m2->nbytes = m1->nbytes; /* bytes used for matrix */
  2507. (void)do_memmove(m2->bits, m1->bits, m1->nbytes); /* matrix bits */
  2508. (void)do_memmove(m2->mi0, m1->mi0, m1->nmi0); /* row node id's level i */
  2509. m2->nmi0 = m1->nmi0; /* how many byte's in mi0 */
  2510. (void)do_memmove(m2->m0i, m1->m0i, m1->nm0i); /* col node id's level (i+1) */
  2511. m2->nm0i = m1->nm0i; /* how many bytes in m0i */
  2512. m2->bbytes = m1->bbytes; /* bytes for double barycenter values */
  2513. (void)do_memmove(m2->b, m1->b, m1->bbytes); /* barycenter values */
  2514. }
  2515. return;
  2516. }
  2517. /* are m1,m2 equal? */
  2518. static int equal_m(struct mmatrix *m1, struct mmatrix *m2, int r, int c)
  2519. {
  2520. int i = 0;
  2521. int j = 0;
  2522. for (i = 1; i <= r; i++) {
  2523. for (j = 1; j <= c; j++) {
  2524. if (mget(m1, i, j) != mget(m2, i, j)) {
  2525. return (0); /* FALSE */
  2526. }
  2527. }
  2528. }
  2529. return (1); /* TRUE */
  2530. }
  2531. /* is whole graph equal */
  2532. static int equal_a(struct gml_graph *g, struct mmatrix **mm1, struct mmatrix **mm2)
  2533. {
  2534. int l = 0;
  2535. if (mm1 == NULL || mm2 == NULL) {
  2536. return (0);
  2537. }
  2538. for (l = 0; l < g->maxlevel; l++) {
  2539. if (equal_m(mm1[l], mm2[l], mm1[l]->nrows /* old g->nnodes_of_level[l] */ ,
  2540. mm1[l]->ncols /* old g->nnodes_of_level[l + 1] */ ) ==
  2541. 0 /* FALSE */ ) {
  2542. return (0); /* FALSE */
  2543. }
  2544. }
  2545. return (1); /* TRUE */
  2546. }
  2547. /* copy whole graph */
  2548. static inline void copy_a(struct gml_graph *g, struct mmatrix **mm1, struct mmatrix **mm2)
  2549. {
  2550. int i = 0;
  2551. for (i = 0; i < g->maxlevel; i++) {
  2552. copy_m(mm1[i], mm2[i]);
  2553. }
  2554. return;
  2555. }
  2556. static void exch_rows(struct mmatrix *m, int r1, int r2)
  2557. {
  2558. int j = 0;
  2559. int id1 = 0;
  2560. int id2 = 0;
  2561. int bit1 = 0;
  2562. int bit2 = 0;
  2563. /*
  2564. * h = m[r1][j];
  2565. * m[r1][j] = m[r2][j];
  2566. * m[r2][j] = h;
  2567. */
  2568. /* swap node id numbers */
  2569. id1 = m->mi0[r1];
  2570. id2 = m->mi0[r2];
  2571. m->mi0[r1] = id2;
  2572. m->mi0[r2] = id1;
  2573. /* swap matrix bits */
  2574. for (j = 1; j <= m->ncols; j++) {
  2575. bit1 = mget(m, r1, j);
  2576. bit2 = mget(m, r2, j);
  2577. mget_set(m, r1, j, bit2);
  2578. mget_set(m, r2, j, bit1);
  2579. }
  2580. return;
  2581. }
  2582. static void exch_columns(struct mmatrix *m, int c1, int c2)
  2583. {
  2584. int i = 0;
  2585. int id1 = 0;
  2586. int id2 = 0;
  2587. int bit1 = 0;
  2588. int bit2 = 0;
  2589. /*
  2590. * h = m[i][c1];
  2591. * m[i][c1] = m[i][c2];
  2592. * m[i][c2] = h;
  2593. */
  2594. /* swap node id numbers */
  2595. id1 = m->m0i[c1];
  2596. id2 = m->m0i[c2];
  2597. m->m0i[c1] = id2;
  2598. m->m0i[c2] = id1;
  2599. /* swap matrix bits */
  2600. for (i = 1; i <= m->nrows; i++) {
  2601. bit1 = mget(m, i, c1);
  2602. bit2 = mget(m, i, c2);
  2603. mget_set(m, i, c1, bit2);
  2604. mget_set(m, i, c2, bit1);
  2605. }
  2606. return;
  2607. }
  2608. static int reverse_r(struct mmatrix *m, int r1, int r2)
  2609. {
  2610. int i = 0;
  2611. int j = 0;
  2612. int ch = 0;
  2613. for (i = r1, j = r2; i < j; i++, j--) {
  2614. ch++;
  2615. exch_rows(m, i, j);
  2616. }
  2617. return (ch);
  2618. }
  2619. static int reverse_c(struct mmatrix *m, int c1, int c2)
  2620. {
  2621. int i = 0;
  2622. int j = 0;
  2623. int ch = 0;
  2624. for (i = c1, j = c2; i < j; i++, j--) {
  2625. ch++;
  2626. exch_columns(m, i, j);
  2627. }
  2628. return (ch);
  2629. }
  2630. static double row_barycenter(struct mmatrix *m, int i, int maxval)
  2631. {
  2632. int j = 0;
  2633. int r1 = 0; /* sum */
  2634. int r2 = 0; /* div */
  2635. for (j = 1; j <= maxval; j++) {
  2636. if (mget(m, i, j)) {
  2637. r1 = r1 + j;
  2638. r2++;
  2639. }
  2640. }
  2641. if (r2 == 0) {
  2642. return (0.0);
  2643. } else {
  2644. return ((double)r1 / (double)r2);
  2645. }
  2646. }
  2647. static double column_barycenter(struct mmatrix *m, int j, int maxval)
  2648. {
  2649. int i = 0;
  2650. int r1 = 0; /* sum */
  2651. int r2 = 0; /* div */
  2652. for (i = 1; i <= maxval; i++) {
  2653. if (mget(m, i, j)) {
  2654. r1 = r1 + i;
  2655. r2++;
  2656. }
  2657. }
  2658. if (r2 == 0) {
  2659. return (0.0);
  2660. } else {
  2661. return ((double)r1 / (double)r2);
  2662. }
  2663. }
  2664. /* reverse rows */
  2665. static int r_r(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  2666. {
  2667. int i = 0;
  2668. int j = 0;
  2669. int ch = 0;
  2670. for (i = 1; i <= max_r; i++) {
  2671. m1->b[i] = row_barycenter(m1, i, max_c);
  2672. }
  2673. for (i = 1; i < max_r; i++) {
  2674. j = i;
  2675. while ((j < max_r) && (m1->b[j + 1] == m1->b[j])) {
  2676. j++;
  2677. }
  2678. if (j > i) {
  2679. ch += reverse_r(m1, i, j);
  2680. if (m2 != NULL) {
  2681. ch += reverse_c(m2, i, j);
  2682. }
  2683. i = j;
  2684. }
  2685. }
  2686. return (ch);
  2687. }
  2688. /* reverse columns */
  2689. static int r_c(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  2690. {
  2691. int i = 0;
  2692. int j = 0;
  2693. int ch = 0;
  2694. for (i = 1; i <= max_c; i++) {
  2695. m1->b[i] = column_barycenter(m1, i, max_r);
  2696. }
  2697. for (i = 1; i < max_c; i++) {
  2698. j = i;
  2699. while ((j < max_c) && (m1->b[j + 1] == m1->b[j])) {
  2700. j++;
  2701. }
  2702. if (j > i) {
  2703. ch += reverse_c(m1, i, j);
  2704. if (m2 != NULL) {
  2705. ch += reverse_r(m2, i, j);
  2706. }
  2707. i = j;
  2708. }
  2709. }
  2710. return (ch);
  2711. }
  2712. /* barycenter rows */
  2713. static int b_r(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  2714. {
  2715. double tmpb = 0.0;
  2716. int i = 0;
  2717. int j = 0;
  2718. int k = 0;
  2719. int ch = 0;
  2720. for (i = 1; i <= max_r; i++) {
  2721. m1->b[i] = row_barycenter(m1, i, max_c);
  2722. }
  2723. for (j = max_r; j > 1; j--) {
  2724. if (m1->b[j] != 0.0) {
  2725. for (i = 1; i < j; i++) {
  2726. if (m1->b[i] != 0.0) {
  2727. k = i + 1;
  2728. while (m1->b[k] == 0.0) {
  2729. k++;
  2730. }
  2731. if (m1->b[i] > m1->b[k]) {
  2732. ch++;
  2733. /* exch_double */
  2734. tmpb = m1->b[k];
  2735. m1->b[k] = m1->b[i];
  2736. m1->b[i] = tmpb;
  2737. exch_rows(m1, i, k);
  2738. if (m2 != NULL) {
  2739. ch++;
  2740. /* exchange cols from i to k */
  2741. exch_columns(m2, i, k);
  2742. }
  2743. }
  2744. }
  2745. }
  2746. }
  2747. }
  2748. return (ch);
  2749. }
  2750. /* barycenter cols */
  2751. static int b_c(struct mmatrix *m1, struct mmatrix *m2, int max_r, int max_c)
  2752. {
  2753. double tmpb = 0.0;
  2754. int i = 0;
  2755. int j = 0;
  2756. int k = 0;
  2757. int ch = 0;
  2758. for (i = 1; i <= max_c; i++) {
  2759. m1->b[i] = column_barycenter(m1, i, max_r);
  2760. }
  2761. for (j = max_c; j > 1; j--) {
  2762. if (m1->b[j] != 0.0) {
  2763. for (i = 1; i < j; i++) {
  2764. if (m1->b[i] != 0.0) {
  2765. k = i + 1;
  2766. while (m1->b[k] == 0.0) {
  2767. k++;
  2768. }
  2769. if (m1->b[i] > m1->b[k]) {
  2770. ch++;
  2771. /* exch_double */
  2772. tmpb = m1->b[k];
  2773. m1->b[k] = m1->b[i];
  2774. m1->b[i] = tmpb;
  2775. /* exchange cols from i to k */
  2776. exch_columns(m1, i, k);
  2777. if (m2 != NULL) {
  2778. ch++;
  2779. exch_rows(m2, i, k);
  2780. }
  2781. }
  2782. }
  2783. }
  2784. }
  2785. }
  2786. return (ch);
  2787. }
  2788. /* test if array is sorted, 1 if sorted from hight-to-low */
  2789. static int sorted(double *vector, int maxval)
  2790. {
  2791. int i = 0;
  2792. for (i = 1; i < maxval; i++) {
  2793. /* but ignore 0.0 values */
  2794. if ((vector[i] > vector[i + 1]) && (vector[i + 1] != 0.0)) {
  2795. return (0); /* FALSE */
  2796. }
  2797. }
  2798. return (1); /* TRUE */
  2799. }
  2800. static inline int phase1_down(struct gml_graph *g, struct mmatrix **mm)
  2801. {
  2802. int i = 0;
  2803. int ch = 0;
  2804. /* from level0 down to level max */
  2805. for (i = 0; i < g->maxlevel - 1; i++) {
  2806. ch += b_c(mm[i], mm[i + 1], mm[i]->nrows, mm[i]->ncols);
  2807. }
  2808. ch += b_c(mm[g->maxlevel - 1], NULL, mm[g->maxlevel - 1]->nrows, mm[g->maxlevel - 1]->ncols);
  2809. return (ch);
  2810. }
  2811. static inline int phase1_up(struct gml_graph *g, struct mmatrix **mm)
  2812. {
  2813. int i = 0;
  2814. int ch = 0;
  2815. if (mm == NULL) {
  2816. return (0);
  2817. }
  2818. /* from level max up to level0 */
  2819. for (i = (g->maxlevel - 1); i > 0; i--) {
  2820. ch += b_r(mm[i], mm[i - 1], mm[i]->nrows /* old g->nnodes_of_level[i] */ ,
  2821. mm[i]->ncols /* old g->nnodes_of_level[i + 1] */ );
  2822. }
  2823. ch += b_r(mm[0], NULL, mm[0]->nrows /* g->nnodes_of_level[0] */ ,
  2824. mm[0]->ncols /* g->nnodes_of_level[1] */ );
  2825. return (ch);
  2826. }
  2827. /* */
  2828. static inline int phase2_down(struct gml_graph *g, struct mmatrix **mm)
  2829. {
  2830. int l = 0; /* Level */
  2831. int i = 0;
  2832. int ch = 0;
  2833. for (l = 0; l < (g->maxlevel - 1); l++) {
  2834. for (i = 1; i <= mm[l]->ncols /* g->nnodes_of_level[l + 1] */ ; i++) {
  2835. mm[l]->b[i] = column_barycenter(mm[l], i, mm[l]->nrows /* g->nnodes_of_level[l] */ );
  2836. }
  2837. if (sorted(mm[l]->b, mm[l]->ncols /* g->nnodes_of_level[l + 1] */ ) ==
  2838. 1 /* TRUE */ ) {
  2839. /* reverse columns */
  2840. ch += r_c(mm[l], mm[l + 1], mm[l]->nrows /* g->nnodes_of_level[l] */ ,
  2841. mm[l]->ncols /* g->nnodes_of_level[l + 1] */ );
  2842. } else {
  2843. return (ch);
  2844. }
  2845. }
  2846. for (i = 1; i <= g->nnodes_of_level[g->maxlevel]; i++) {
  2847. mm[g->maxlevel - 1]->b[i] = column_barycenter(mm[g->maxlevel - 1], i, mm[g->maxlevel - 1]->nrows
  2848. /* g->nnodes_of_level[g->maxlevel - 1] */ );
  2849. }
  2850. if (sorted(mm[g->maxlevel - 1]->b, mm[g->maxlevel - 1]->ncols /* g->nnodes_of_level[g->maxlevel] */ ) ==
  2851. 1 /* TRUE */ ) {
  2852. /* reverse columns */
  2853. ch += r_c(mm[g->maxlevel - 1], NULL, mm[g->maxlevel - 1]->nrows /* g->nnodes_of_level[g->maxlevel - 1] */ ,
  2854. mm[g->maxlevel - 1]->ncols /* g->nnodes_of_level[g->maxlevel] */ );
  2855. }
  2856. return (ch);
  2857. }
  2858. /* */
  2859. static inline int phase2_up(struct gml_graph *g, struct mmatrix **mm)
  2860. {
  2861. int l = 0; /* Level */
  2862. int i = 0;
  2863. int ch = 0;
  2864. if (g) {
  2865. }
  2866. for (l = (g->maxlevel - 1); l > 0; l--) {
  2867. for (i = 1; i <= /* g->nnodes_of_level[l] */ mm[l]->nrows; i++) {
  2868. mm[l]->b[i] = row_barycenter(mm[l], i, /* g->nnodes_of_level[l + 1] */
  2869. mm[l]->ncols);
  2870. }
  2871. if (sorted(mm[l]->b, /* g->nnodes_of_level[l] */ mm[l]->nrows) ==
  2872. 1 /* TRUE */ ) {
  2873. /* reverse rows */
  2874. ch += r_r(mm[l], mm[l - 1], mm[l]->nrows /* g->nnodes_of_level[l] */ ,
  2875. mm[l]->ncols /* g->nnodes_of_level[l + 1] */ );
  2876. } else {
  2877. return (ch);
  2878. }
  2879. }
  2880. for (i = 1; i <= mm[0]->nrows /* g->nnodes_of_level[0] */ ; i++) {
  2881. mm[0]->b[i] = row_barycenter(mm[0], i, mm[0]->ncols /* g->nodes_of_level[1] */ );
  2882. }
  2883. /* if level0 is sorted, r_r */
  2884. if (sorted(mm[0]->b, mm[0]->nrows /* g->nnodes_of_level[0] */ ) ==
  2885. 1 /* TRUE */ ) {
  2886. /* reverse rows */
  2887. ch += r_r(mm[0], NULL, mm[0]->nrows /* g->nnodes_of_level[0] */ ,
  2888. mm[0]->ncols /* g->nnodes_of_level[1] */ );
  2889. }
  2890. return (ch);
  2891. }
  2892. /* here if maxlevel >1 */
  2893. static void bc_n(struct gml_graph *g, int it1value, int it2value)
  2894. {
  2895. struct mmatrix **a = NULL;
  2896. struct mmatrix **a1 = NULL;
  2897. struct mmatrix **a2 = NULL;
  2898. struct mmatrix **as = NULL;
  2899. int i = 0;
  2900. int ks = 0;
  2901. int k = 0;
  2902. int n1 = 0;
  2903. int n2 = 0;
  2904. int cht = 0;
  2905. int ch1 = 0;
  2906. int ch2 = 0;
  2907. int r1 = 0;
  2908. int r2 = 0;
  2909. int r3 = 0;
  2910. int rr1 = 0;
  2911. int rr2 = 0;
  2912. int rr3 = 0;
  2913. int it1 = 20; /* iterations phase1 */
  2914. int it2 = 40; /* iterations pahse2 */
  2915. if (it1value == 0) {
  2916. it1 = 20;
  2917. } else {
  2918. it1 = it1value;
  2919. }
  2920. if (it2value == 0) {
  2921. it2 = 40;
  2922. } else {
  2923. it2 = it2value;
  2924. }
  2925. /* the whole graph structures */
  2926. a = calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  2927. a1 = calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  2928. a2 = calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  2929. as = calloc(1, g->maxlevel * sizeof(struct mmatrix *));
  2930. /* get matrix struct */
  2931. for (i = 0; i < g->maxlevel; i++) {
  2932. a[i] = calloc(1, sizeof(struct mmatrix));
  2933. a1[i] = calloc(1, sizeof(struct mmatrix));
  2934. a2[i] = calloc(1, sizeof(struct mmatrix));
  2935. as[i] = calloc(1, sizeof(struct mmatrix));
  2936. }
  2937. /* get data inside struct */
  2938. for (i = 0; i < g->maxlevel; i++) {
  2939. /* upper level */
  2940. a[i]->level = i;
  2941. a1[i]->level = i;
  2942. a2[i]->level = i;
  2943. as[i]->level = i;
  2944. /* number of rows */
  2945. a[i]->nrows = g->nnodes_of_level[i];
  2946. a1[i]->nrows = g->nnodes_of_level[i];
  2947. a2[i]->nrows = g->nnodes_of_level[i];
  2948. as[i]->nrows = g->nnodes_of_level[i];
  2949. /* number of columns */
  2950. a[i]->ncols = g->nnodes_of_level[i + 1];
  2951. a1[i]->ncols = g->nnodes_of_level[i + 1];
  2952. a2[i]->ncols = g->nnodes_of_level[i + 1];
  2953. as[i]->ncols = g->nnodes_of_level[i + 1];
  2954. /* buffer for barycenter values */
  2955. if (a[i]->nrows > a[i]->ncols) {
  2956. a[i]->bbytes = ((a[i]->nrows + 1) * sizeof(double));
  2957. a1[i]->bbytes = ((a1[i]->nrows + 1) * sizeof(double));
  2958. a2[i]->bbytes = ((a2[i]->nrows + 1) * sizeof(double));
  2959. as[i]->bbytes = ((as[i]->nrows + 1) * sizeof(double));
  2960. } else {
  2961. a[i]->bbytes = ((a[i]->ncols + 1) * sizeof(double));
  2962. a1[i]->bbytes = ((a1[i]->ncols + 1) * sizeof(double));
  2963. a2[i]->bbytes = ((a2[i]->ncols + 1) * sizeof(double));
  2964. as[i]->bbytes = ((as[i]->ncols + 1) * sizeof(double));
  2965. }
  2966. a[i]->b = calloc(1, a[i]->bbytes);
  2967. a1[i]->b = calloc(1, a1[i]->bbytes);
  2968. a2[i]->b = calloc(1, a2[i]->bbytes);
  2969. as[i]->b = calloc(1, as[i]->bbytes);
  2970. /* number of bytes used */
  2971. a[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  2972. a1[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  2973. a2[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  2974. as[i]->nmi0 = ((a[i]->nrows + 1) * sizeof(int));
  2975. /* row node id's (int's) */
  2976. a[i]->mi0 = calloc(1, a[i]->nmi0);
  2977. a1[i]->mi0 = calloc(1, a1[i]->nmi0);
  2978. a2[i]->mi0 = calloc(1, a2[i]->nmi0);
  2979. as[i]->mi0 = calloc(1, as[i]->nmi0);
  2980. /* number of bytes used */
  2981. a[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  2982. a1[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  2983. a2[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  2984. as[i]->nm0i = ((a[i]->ncols + 1) * sizeof(int));
  2985. /* col node id's (int's) */
  2986. a[i]->m0i = calloc(1, a[i]->nm0i);
  2987. a1[i]->m0i = calloc(1, a1[i]->nm0i);
  2988. a2[i]->m0i = calloc(1, a2[i]->nm0i);
  2989. as[i]->m0i = calloc(1, as[i]->nm0i);
  2990. /* bits array for the matrix */
  2991. a[i]->nbytes = 1 + ((((a[i]->nrows + 1) * (a[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  2992. a1[i]->nbytes = 1 + ((((a1[i]->nrows + 1) * (a1[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  2993. a2[i]->nbytes = 1 + ((((a2[i]->nrows + 1) * (a2[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  2994. as[i]->nbytes = 1 + ((((as[i]->nrows + 1) * (as[i]->ncols + 1)) + CHAR_BIT) / CHAR_BIT);
  2995. a[i]->bits = calloc(1, a[i]->nbytes);
  2996. a1[i]->bits = calloc(1, a1[i]->nbytes);
  2997. a2[i]->bits = calloc(1, a2[i]->nbytes);
  2998. as[i]->bits = calloc(1, as[i]->nbytes);
  2999. }
  3000. /* fill the matrix with data for all levels */
  3001. for (i = 0; i < g->maxlevel; i++) {
  3002. make_matrix(g, i, a[i]);
  3003. }
  3004. copy_a(g, a, as);
  3005. ks = number_of_crossings_a(g, as);
  3006. g->sugi_icrossings = ks; /* sugiyama initial crossings */
  3007. if (ks > 0) {
  3008. /* Phase1 */
  3009. ch1 = 0;
  3010. /* first does alwasy improve */
  3011. ch1 += phase1_down(g, a);
  3012. copy_a(g, a, as);
  3013. ch1 += phase1_up(g, a);
  3014. copy_a(g, a, as);
  3015. /* loop phase1 */
  3016. n1 = 0;
  3017. do {
  3018. copy_a(g, a, a1);
  3019. ch1 += phase1_down(g, a);
  3020. k = number_of_crossings_a(g, a);
  3021. if (k < ks) {
  3022. /* reduced crossings, save the new state */
  3023. ks = k;
  3024. copy_a(g, a, as);
  3025. }
  3026. ch1 += phase1_up(g, a);
  3027. k = number_of_crossings_a(g, a);
  3028. if (k < ks) {
  3029. ks = k;
  3030. copy_a(g, a, as);
  3031. }
  3032. cht += ch1;
  3033. if (ks == 0) {
  3034. break;
  3035. }
  3036. /* check if number of crossings changed */
  3037. r1 = r2;
  3038. r2 = r3;
  3039. r3 = ks;
  3040. if (r1 == r2) {
  3041. if (r2 == r3) {
  3042. break;
  3043. }
  3044. }
  3045. }
  3046. while (++n1 < it1 && (equal_a(g, a, a1) == 0 /* FALSE */ ));
  3047. /* if matrices differ, save state */
  3048. if (equal_a(g, a, as) == 0 /* FALSE */ ) {
  3049. copy_a(g, as, a);
  3050. }
  3051. if (ks > 0) {
  3052. /* Phase2 */
  3053. n2 = 0;
  3054. cht += ch1;
  3055. do {
  3056. ch2 = 0;
  3057. copy_a(g, a, a2);
  3058. ch2 += phase2_down(g, a);
  3059. n1 = 0;
  3060. do {
  3061. ch1 = 0;
  3062. copy_a(g, a, a1);
  3063. ch1 += phase1_down(g, a);
  3064. k = number_of_crossings_a(g, a);
  3065. if (k < ks) {
  3066. ks = k;
  3067. copy_a(g, a, as);
  3068. }
  3069. ch1 += phase1_up(g, a);
  3070. k = number_of_crossings_a(g, a);
  3071. if (k < ks) {
  3072. ks = k;
  3073. copy_a(g, a, as);
  3074. }
  3075. if (ks == 0) {
  3076. break;
  3077. }
  3078. /* check if number of crossings changed */
  3079. rr1 = rr2;
  3080. rr2 = rr3;
  3081. rr3 = ks;
  3082. if (rr1 == rr2) {
  3083. if (rr2 == rr3) {
  3084. break;
  3085. }
  3086. }
  3087. }
  3088. while (++n1 < it1 && equal_a(g, a, a1) == 0 /* FALSE */ );
  3089. ch2 += phase2_up(g, a);
  3090. n1 = 0;
  3091. do {
  3092. copy_a(g, a, a1);
  3093. ch1 += phase1_up(g, a);
  3094. k = number_of_crossings_a(g, a);
  3095. if (k < ks) {
  3096. ks = k;
  3097. copy_a(g, a, as);
  3098. }
  3099. ch1 += phase1_down(g, a);
  3100. k = number_of_crossings_a(g, a);
  3101. if (k < ks) {
  3102. ks = k;
  3103. copy_a(g, a, as);
  3104. }
  3105. cht += ch1;
  3106. if (ks == 0) {
  3107. break;
  3108. }
  3109. /* check if number of crossings changed */
  3110. rr1 = rr2;
  3111. rr2 = rr3;
  3112. rr3 = ks;
  3113. if (rr1 == rr2) {
  3114. if (rr2 == rr3) {
  3115. break;
  3116. }
  3117. }
  3118. }
  3119. while (++n1 < it1 && equal_a(g, a, a1) == 0 /* FALSE */ );
  3120. cht += ch1;
  3121. cht += ch2;
  3122. if (ks == 0) {
  3123. break;
  3124. }
  3125. /* check if number of crossings changed */
  3126. r1 = r2;
  3127. r2 = r3;
  3128. r3 = ks;
  3129. if (r1 == r2) {
  3130. if (r2 == r3) {
  3131. break;
  3132. }
  3133. }
  3134. }
  3135. while (++n2 < it2 && equal_a(g, a, a2) == 0 /* FALSE */ );
  3136. }
  3137. }
  3138. /* sugiyama final crossings */
  3139. g->sugi_fcrossings = ks;
  3140. /* sugiyama changes made */
  3141. g->sugi_changes = cht;
  3142. for (i = 0; i < g->maxlevel; i += 2) {
  3143. /* set new node positions for 2 levels */
  3144. store_new_positions(g, as[i], i);
  3145. }
  3146. if (i == g->maxlevel) {
  3147. store_new_positions(g, as[g->maxlevel - 1], (g->maxlevel - 1));
  3148. }
  3149. for (i = 0; i < g->maxlevel; i++) {
  3150. if (a[i]) {
  3151. free(a[i]->b);
  3152. free(a[i]->mi0);
  3153. free(a[i]->m0i);
  3154. free(a[i]->bits);
  3155. }
  3156. if (a1[i]) {
  3157. free(a1[i]->b);
  3158. free(a1[i]->mi0);
  3159. free(a1[i]->m0i);
  3160. free(a1[i]->bits);
  3161. }
  3162. if (a2[i]) {
  3163. free(a2[i]->b);
  3164. free(a2[i]->mi0);
  3165. free(a2[i]->m0i);
  3166. free(a2[i]->bits);
  3167. }
  3168. if (as[i]) {
  3169. free(as[i]->b);
  3170. free(as[i]->mi0);
  3171. free(as[i]->m0i);
  3172. free(as[i]->bits);
  3173. }
  3174. }
  3175. for (i = 0; i < g->maxlevel; i++) {
  3176. free(a[i]);
  3177. free(a1[i]);
  3178. free(a2[i]);
  3179. free(as[i]);
  3180. }
  3181. free(a);
  3182. free(a1);
  3183. free(a2);
  3184. free(as);
  3185. return;
  3186. }
  3187. /*
  3188. This algorithm is for routing hierarchies of elements. A "good route" is
  3189. one that has a minimum number of link crossings. An algorithm that was
  3190. truly optimal (for minimizing link crossings) would be combinatorial and
  3191. therefore cost prohibitive; therefore, this algorithm uses a heuristic
  3192. approach that finds a route with close to the minimum number of crossings
  3193. in a reasonable amount of time.
  3194. This algorithm assumes that all the elements form a directed acyclic graph
  3195. (DAG), which means (1) that links flow one way between elements and (2) for
  3196. any given node there is no way to get back to the node if, starting at the
  3197. node, you traverse the links going from node to node. This algorithm also
  3198. assumes that AT MOST only ONE link may exist between a pair of nodes.
  3199. -------------------------------------------------------------------------------
  3200. OVERVIEW OF ALGORITHM
  3201. All elements that do not have any parents are placed in the first row (row 0).
  3202. Elements are assigned to rows, where the row number for each child is equal to
  3203. the [maximum(row number of all its parents) + 1]. Crossovers are determined
  3204. by examining links between elements on adjacent rows, so if a parent is in a
  3205. row that is not adjacent to its child's row, "dummy" nodes are created on the
  3206. rows in between the parent and child, and the parent and child are connected
  3207. via these dummy nodes.
  3208. Once the elements (now called nodes) are assigned to individual rows, the
  3209. rows are sorted (repeatedly) in order to minimize link crossings. The
  3210. sort criteria involves attempting to both center children under parents and
  3211. to center parents over children. The sort orders are then tweaked by swapping
  3212. nodes that have the same sort value.
  3213. After the column orders are finalized, the nodes are spread out so they are
  3214. more or less centered above their children and below their parents. When
  3215. centering children below parents, a row of children is sorted by which node
  3216. has the greatest number of parents. These get first choice of where to be
  3217. placed under the parents (actually, dummy nodes get first preference, then
  3218. all of the others). Centering parents above children is analogous.
  3219. When done with node placement, there may be some empty columns, and the
  3220. numbering scheme may not start at 0. Therefore, the empty columns must
  3221. be eliminated and every node needs its column renumbered, starting at 0.
  3222. Then you are done.
  3223. -------------------------------------------------------------------------------
  3224. REALIZATION MATRIX
  3225. When it comes to both sorting nodes and horizontally spacing the nodes, two
  3226. adjacent rows are always involved. For example, if we are sorting row[i]
  3227. based on the children of row[i]'s nodes, then row[i+1] is also involved
  3228. at this step. These two rows are called the "i-th realization", and form
  3229. the "i-th realization matrix". A realization matrix shows the parent-child
  3230. relationships between adjacent rows, with the parents on the rows and the
  3231. children on the columns. If there is a parent-child relationship, a 1 is
  3232. stored in the matrix at the position, else a 0 is stored.
  3233. An example:
  3234. A B C D
  3235. \ \ / \ / / |
  3236. \ /\ / \ / |
  3237. /\ / \ / \ |
  3238. / /\ /\ \ |
  3239. // \ / \ \|
  3240. E F G H
  3241. E F G H
  3242. A 0 1 1 0 In this example, parent 'A' has children 'F' and 'G',
  3243. B 1 0 0 1 parent 'B' has children 'E' and 'H',
  3244. C 1 1 0 0 parent 'C' has children 'E' and 'F',
  3245. D 0 0 0 1 and parent 'D' has child 'H'.
  3246. -------------------------------------------------------------------------------
  3247. ROW AND COLUMN BARYCENTERS
  3248. Two other important concepts are the "row barycenter" and the "column
  3249. barycenter" for a node. The "row barycenter" is the basically the average
  3250. of the positions of a node's children. The "column barycenter" is the average
  3251. of the positions of a node's parents. These numbers tell us where a node
  3252. would like to be positioned in its row, depending whether we are positioning
  3253. relative to children or parents.
  3254. For example, using the above realization matrix, we can calculate the row
  3255. barycenters for A, B, C, and D, and the column barycenters for E, F, G, and H.
  3256. Since the row barycenter of a node is equal to the sum of the positions of
  3257. the node's children divided by the number of children of the node, the row
  3258. barycenter for A is (1 + 2)/2 = 1.5. This assumes that we start numbering
  3259. rows and columns at 0. Similarly, the column barycenter of a node is equal
  3260. to the sum of the positions of the node's parents divided by the number of
  3261. parents of the node. So, the column barycenter of F is (0 + 2)/2 = 1.0.
  3262. The complete example is as follows:
  3263. Row
  3264. | E F G H | Barys
  3265. ------+--------------------+-----
  3266. A | 0 1 1 0 | 1.5
  3267. B | 1 0 0 1 | 1.5
  3268. C | 1 1 0 0 | 0.5
  3269. D | 0 0 0 1 | 3.0
  3270. ------+--------------------+-----
  3271. Col | 1.5 1.0 0.0 2.0 |
  3272. Barys | |
  3273. If we were to sort the child nodes here by their column barycenters, the new
  3274. order would be G, F, E, H. If we were to sort the parent nodes here by their
  3275. row barycenters, then the order would be C, A, B, D (if two or more nodes have
  3276. the same value, be sure to keep the same precedence that already exists
  3277. between them, e.g., make sure that order after sorting is not C, B, A, D).
  3278. If a node has no parents then it can't have a column barycenter. This case
  3279. should never happen, as all nodes that have no parents should be in the first
  3280. level of the hierarchy, and these nodes would only be represented in
  3281. realization matrix 0, and they would only have row barycenters.
  3282. If a node has no children then it can't have a row barycenter. In this case,
  3283. while sorting based on row barycenters, sort AROUND these nodes, i.e., do
  3284. not change their positions at all. For example, if we had the following:
  3285. Row
  3286. | W X Y Z | Barys
  3287. ------+--------------------+-----
  3288. Q | 0 1 1 1 | 2.0
  3289. R | 0 0 0 0 | ???
  3290. S | 1 0 0 0 | 0.0
  3291. T | 0 1 0 1 | 2.0
  3292. ------+--------------------+-----
  3293. Col | 2.0 1.5 0.0 1.5 |
  3294. Barys | |
  3295. and we were to sort by row barycenters, the resulting order should be S, R,
  3296. Q, T. Notice how R stayed in its position, and even though Q and T had the
  3297. same barycentric value, Q stayed before T.
  3298. The whole reason for sorting rows and columns by their barycenters is to
  3299. decrease the number of crossovers.
  3300. -------------------------------------------------------------------------------
  3301. CROSSOVERS
  3302. The realization matrix is also used to count the number of crossovers between
  3303. two adjacent rows of nodes. For each row, starting with the second row, if
  3304. a row element has a 1, then sum up all of the matrix elements that are above
  3305. AND to the right of this element. Looking again at the first example:
  3306. A B C D
  3307. \ \ / \ / / |
  3308. \ /\ / \ / |
  3309. /\ / \ / \ |
  3310. / /\ /\ \ |
  3311. // \ / \ \|
  3312. E F G H
  3313. Row
  3314. | E F G H | Barys
  3315. ------+--------------------+-----
  3316. A | 0 1 1 0 | 1.5
  3317. B | 1 0 0 1 | 1.5
  3318. C | 1 1 0 0 | 0.5
  3319. D | 0 0 0 1 | 3.0
  3320. ------+--------------------+-----
  3321. Col | 1.5 1.0 0.0 2.0 |
  3322. Barys | |
  3323. Starting with the second row (parent B's row), position B-E has a 1. Looking
  3324. at positions above and to the right, we see positions A-F and A-G both have
  3325. a 1, so the number of crossovers is currently = 2. Position B-H has a 1, but
  3326. there are no elements above and to the right, so crossovers is still = 2.
  3327. For parent row of C, position C-E crosses over with B-H, A-F, and A-G, so
  3328. crossovers = 5. C-F crosses over with B-H and A-G, so crossovers = 7. For
  3329. parent row D, position D-H doesn't cross over with any other link. So for
  3330. this realization matrix representing these two rows, the number of crossovers
  3331. is 7.
  3332. The total number of crossovers for the whole graph would be the sum of the
  3333. crossovers from each matrix realization.
  3334. -------------------------------------------------------------------------------
  3335. NODE CENTERING
  3336. After the nodes for each row have their final sort order, the nodes need to
  3337. be assigned to grid positions. Their initial grid position will be their
  3338. column position, by which we mean their array position in the row. From now
  3339. on, when we take a row or column barycenter, we will be using grid positions
  3340. instead of column positions.
  3341. Note: The following examples will be based on centering children with respect
  3342. to their parents' positions. Centering parents based on their children's
  3343. positions is analogous.
  3344. When positioning the nodes on a row based on their parents' positions, the
  3345. nodes must be initially sorted to see which nodes get first choice. The dummy
  3346. nodes go first, and the rest of nodes are sorted in descending order based on
  3347. the number of parents the node has. If a dummy node has a parent that has
  3348. multiple dummy nodes, all of these dummy nodes are again sorted by how close
  3349. to the center of the parent's children they are. This is a confusing
  3350. statement, best illustrated by example:
  3351. P1 P2
  3352. \ |
  3353. \ __________^__________
  3354. \| | | | |
  3355. C1 D1 D2 C2 D3
  3356. Here, parent P1 has one child, C1. Parent P2 has five children, and three of
  3357. the child nodes are dummy nodes: D1, D2, and D3. C1 is child 0 of P2, D1 is
  3358. child 1 of P2, D2 is child 2 of P2, C2 is child 3 of P2, and D3 is child 4 of
  3359. P2. The child midpoint underneath the parent is equal to
  3360. (the number of children - 1) / 2, so (5 - 1) / 2 = 2. Since the dummy nodes
  3361. go first, the initial order is D1, D2, D3, C1 (because it has 2 parents), and
  3362. finally C2. All of the dummy nodes have the same parent, so we will sort them
  3363. based on how far away they are from the parent's child midpoint. D1 is child
  3364. 1 of P2, so it is 1 away. D2 is child 2 of P2, so it is 0 away. D3 is child
  3365. 4 of P2, so it is 2 away. Therefore, the final order for choosing positions
  3366. is D2, D1, D3, C1, C2.
  3367. In a situation similar to the dummy nodes, if a non-dummy node has a only one
  3368. parent, and that parent has other children with just one parent, then these
  3369. one parent child nodes that have the same parent need additional sorting in
  3370. the exact same manner that we just did the dummy nodes.
  3371. The whole purpose behind this is so that the left most node doesn't get first
  3372. choice. If it did, we would get graphs that look like:
  3373. A A
  3374. | |
  3375. |_________ instead of _____^_____
  3376. | | | | | |
  3377. B C D B C D
  3378. Anyway, once we have a sort order for the nodes of a row, we place the nodes
  3379. in their preferred positions. Using the previous example, assume that P1
  3380. is in grid position 2 and P2 is in grid position 5. D2 gets first choice,
  3381. and its column barycenter (based now on parent grid positions, not column
  3382. positions) is 5, so we place D2 in position 5. D1 is next, its barycenter
  3383. is also 5. We can't give it 5 since that position is occupied, so we give
  3384. it the closest possible position we can, which in this case is 4. D3 is next,
  3385. and its barycenter is also 5. The closest position that we can give it is
  3386. position 7, since we must allow room for C2. C1 is next, and its barycenter
  3387. is (2 + 5)/2 = 3.5, which we round to 3. Position 3 is open, so we go ahead
  3388. and give it position 3. C2 is last, and its barycenter is 5. However, the
  3389. first position available to it based on its left neighbor is position 6, so
  3390. we assign it position 6.
  3391. -------------------------------------------------------------------------------
  3392. GOING 'UP' OR 'DOWN' THE GRAPH
  3393. "Going down the graph" means taking each realization matrix situation,
  3394. starting with Realization Matrix 0, and performing some action on it, then
  3395. going to the next realization matrix, and continuing until all of the
  3396. realization matrices have been visited.
  3397. "Going up the graph" is analogous, except you start at the bottom with the
  3398. last realization matrix and work up to Realization Matrix 0.
  3399. */
  3400. static void barycenter(struct gml_graph *g, int it1v, int it2v)
  3401. {
  3402. /* number of crossing edges at level */
  3403. if (g->numce == NULL) {
  3404. g->numce = (int *)calloc(1, (g->maxlevel + 1) * sizeof(int));
  3405. }
  3406. if (g->maxlevel == 0) {
  3407. /* if graph has only 1 or more nodes */
  3408. return;
  3409. }
  3410. if (g->nnodes < 2) {
  3411. return;
  3412. }
  3413. if (g->nedges < 2) {
  3414. return;
  3415. }
  3416. bc_n(g, it1v, it2v);
  3417. return;
  3418. }
  3419. /* min. distance between 2 nodes */
  3420. static int mindist = 1;
  3421. /* current startnode nr field */
  3422. static int csn = 0;
  3423. /* node list of part of graph */
  3424. static struct gml_nlist *cnodelist = NULL;
  3425. static struct gml_nlist *cnodelisttail = NULL;
  3426. /* number of nodes at level */
  3427. static int *cnnodes_of_level = NULL;
  3428. /* max. x,y in part of graph */
  3429. static int cmaxx = 0;
  3430. static int cmaxy = 0;
  3431. /* widest x level */
  3432. static int cwidestnnodes = 0;
  3433. /* x width at position */
  3434. static int *cwpos = NULL;
  3435. /* lists per pos. */
  3436. static struct gml_nlist **cposnodes = NULL;
  3437. /* hor pos */
  3438. static int *chpos = NULL;
  3439. /* hor pos */
  3440. static struct gml_nlist **clevelnodes = NULL;
  3441. /* (x,y) spacing */
  3442. static int xspacing = 0;
  3443. static int yspacing = 0;
  3444. /* */
  3445. struct node_data {
  3446. struct gml_node *node;
  3447. int priority;
  3448. int done;
  3449. };
  3450. /* nodes in one level */
  3451. static struct node_data *nl = NULL;
  3452. static int is_dummy(struct gml_node *node)
  3453. {
  3454. if (node->dummy) {
  3455. return (1);
  3456. } else {
  3457. return (0);
  3458. }
  3459. }
  3460. /* how many connection edges from previous level */
  3461. static int upper_connectivity(struct gml_node *node)
  3462. {
  3463. struct gml_elist *el = NULL;
  3464. int result = 0;
  3465. result = 0;
  3466. if (node == NULL) {
  3467. /* shouldnothappen */
  3468. return (0);
  3469. }
  3470. /* incoming edges for_targetlist(node,edge) */
  3471. el = node->incoming_e;
  3472. while (el) {
  3473. /* skip hor. edges */
  3474. if (el->edge->hedge == 0) {
  3475. /* only in this part of graph */
  3476. if (el->edge->from_node->startnode == csn) {
  3477. result++;
  3478. }
  3479. }
  3480. el = el->next;
  3481. }
  3482. return (result);
  3483. }
  3484. /* how many connection edges to next level */
  3485. static int lower_connectivity(struct gml_node *node)
  3486. {
  3487. struct gml_elist *el = NULL;
  3488. int result = 0;
  3489. result = 0;
  3490. if (node == NULL) {
  3491. /* shouldnothappen */
  3492. return (0);
  3493. }
  3494. /* outgoing edges for_sourcelist(node,edge) */
  3495. el = node->outgoing_e;
  3496. while (el) {
  3497. /* skip hor. edges */
  3498. if (el->edge->hedge == 0) {
  3499. /* only in this part of graph */
  3500. if (el->edge->to_node->startnode == csn) {
  3501. result++;
  3502. }
  3503. }
  3504. el = el->next;
  3505. }
  3506. return (result);
  3507. }
  3508. /* simple floor() function */
  3509. static double do_floor(double num)
  3510. {
  3511. double ret = 0.0;
  3512. if (num < 0) {
  3513. ret = (int)(num - 1);
  3514. } else {
  3515. ret = (int)num;
  3516. }
  3517. return (ret);
  3518. }
  3519. /* avg x pos of incoming edges */
  3520. static int upper_barycenter(struct gml_node *node)
  3521. {
  3522. struct gml_elist *el = NULL;
  3523. int result = 0;
  3524. double r = 0.0;
  3525. if (node == NULL) {
  3526. /* shouldnothappen */
  3527. return (0);
  3528. }
  3529. /* incoming edges x sum for_targetlist(node,edge) */
  3530. el = node->incoming_e;
  3531. while (el) {
  3532. /* skip hor. edges */
  3533. if (el->edge->hedge == 0) {
  3534. /* only in this part of graph */
  3535. if (el->edge->from_node->startnode == csn) {
  3536. result += (el->edge->from_node->absx);
  3537. }
  3538. }
  3539. el = el->next;
  3540. }
  3541. if (result == 0) {
  3542. r = (0.0);
  3543. } else {
  3544. if (upper_connectivity(node) == 0) {
  3545. r = 0.0;
  3546. } else {
  3547. r = (result / upper_connectivity(node));
  3548. }
  3549. }
  3550. r = do_floor(r + 0.5);
  3551. return ((int)r);
  3552. }
  3553. /* avg x pos of outgoing edges */
  3554. static int lower_barycenter(struct gml_node *node)
  3555. {
  3556. struct gml_elist *el = NULL;
  3557. int result = 0;
  3558. double r = 0.0;
  3559. if (node == NULL) {
  3560. /* shouldnothappen */
  3561. return (0);
  3562. }
  3563. /* get avg. x pos of outgoing edges for_sourcelist(node,edge) */
  3564. el = node->outgoing_e;
  3565. while (el) {
  3566. /* skip hor. edges */
  3567. if (el->edge->hedge == 0) {
  3568. /* only in this part of graph */
  3569. if (el->edge->to_node->startnode == csn) {
  3570. result += (el->edge->to_node->absx);
  3571. }
  3572. }
  3573. el = el->next;
  3574. }
  3575. if (result == 0) {
  3576. r = (0.0);
  3577. } else {
  3578. if (lower_connectivity(node) == 0) {
  3579. r = 0.0;
  3580. } else {
  3581. r = (result / lower_connectivity(node));
  3582. }
  3583. }
  3584. r = do_floor(r + 0.5);
  3585. return ((int)r);
  3586. }
  3587. static void sort(int n)
  3588. {
  3589. int i = 0;
  3590. int j = 0;
  3591. struct node_data h;
  3592. for (j = n - 1; j > 0; j--) {
  3593. for (i = 0; i < j; i++) {
  3594. /* issue here */
  3595. if (nl[i].node && nl[i + 1].node) {
  3596. if (nl[i].node->relx > nl[i + 1].node->relx) {
  3597. /* swap */
  3598. h = nl[i];
  3599. nl[i] = nl[i + 1];
  3600. nl[i + 1] = h;
  3601. }
  3602. }
  3603. }
  3604. }
  3605. return;
  3606. }
  3607. /* */
  3608. static void make_node_list_up(int l)
  3609. {
  3610. struct gml_nlist *gnl = NULL;
  3611. struct gml_node *n = NULL;
  3612. int i = 0;
  3613. /* for_all_nodes(g,n) */
  3614. gnl = cnodelist;
  3615. i = 0;
  3616. while (gnl) {
  3617. n = gnl->node;
  3618. if (n->absy == l) {
  3619. nl[i].node = n;
  3620. nl[i].done = 0; /* FALSE */
  3621. if (is_dummy(n) == 1) { /* */
  3622. /* higer value then the highest node in this level */
  3623. /*old nl[i].priority = (cnnodes_of_level[l + 1] + 1000 */
  3624. nl[i].priority = (100000 - n->relx);
  3625. } else {
  3626. nl[i].priority = lower_connectivity(n);
  3627. }
  3628. i++;
  3629. }
  3630. gnl = gnl->next;
  3631. }
  3632. sort(cnnodes_of_level[l]);
  3633. return;
  3634. }
  3635. /* */
  3636. static void make_node_list_down(int l)
  3637. {
  3638. struct gml_nlist *gnl = NULL;
  3639. struct gml_node *n = NULL;
  3640. int i = 0;
  3641. /* for_all_nodes(g,n) */
  3642. gnl = cnodelist;
  3643. while (gnl) {
  3644. n = gnl->node;
  3645. if (n->absy == l) {
  3646. nl[i].node = n;
  3647. nl[i].done = 0; /* FALSE */
  3648. if (is_dummy(n) == 1) { /* */
  3649. /* give dummy node uniq high number */
  3650. /*old nl[i].priority = (cnnodes_of_level[l - 1] + 1000 */
  3651. nl[i].priority = (100000 - n->relx);
  3652. } else {
  3653. nl[i].priority = upper_connectivity(n);
  3654. }
  3655. i++;
  3656. }
  3657. gnl = gnl->next;
  3658. }
  3659. sort(cnnodes_of_level[l]);
  3660. return;
  3661. }
  3662. /* get number of node with highest prio which is not done yet */
  3663. static int find_next(int n)
  3664. {
  3665. int index = 0;
  3666. int i = 0;
  3667. int highest_priority = 0;
  3668. for (i = 0; i < n; i++) {
  3669. if ((nl[i].priority >= highest_priority)
  3670. && (nl[i].done == 0 /* FALSE */ )) {
  3671. index = i;
  3672. highest_priority = nl[i].priority;
  3673. }
  3674. }
  3675. return (index);
  3676. }
  3677. static void do_down(int l)
  3678. {
  3679. int i = 0;
  3680. int index = 0;
  3681. int j = 0;
  3682. int optimal_position = 0;
  3683. int distance = 0;
  3684. int possible_distance = 0;
  3685. for (i = 0; i < cnnodes_of_level[l]; i++) {
  3686. index = find_next(cnnodes_of_level[l]);
  3687. if (nl[index].node) {
  3688. optimal_position = upper_barycenter(nl[index].node);
  3689. if (optimal_position == 0) {
  3690. optimal_position = nl[index].node->absx;
  3691. }
  3692. if (optimal_position < nl[index].node->absx) {
  3693. distance = nl[index].node->absx - optimal_position;
  3694. possible_distance = 0;
  3695. j = index;
  3696. do {
  3697. if (j > 0) {
  3698. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  3699. } else {
  3700. /* j==0, no nodes at left */
  3701. possible_distance += nl[j].node->absx - mindist;
  3702. }
  3703. j--;
  3704. }
  3705. while ((j >= 0) && !(nl[j].done));
  3706. if (possible_distance < distance) {
  3707. distance = possible_distance;
  3708. }
  3709. j = index;
  3710. while (distance > 0) {
  3711. int d = 0;
  3712. int k = 0;
  3713. if (j == 0) {
  3714. d = distance;
  3715. } else {
  3716. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  3717. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  3718. } else {
  3719. d = distance;
  3720. }
  3721. }
  3722. for (k = j; k <= index; k++) {
  3723. nl[k].node->absx -= d;
  3724. }
  3725. j--;
  3726. distance -= d;
  3727. }
  3728. } else {
  3729. distance = optimal_position - nl[index].node->absx;
  3730. possible_distance = 0;
  3731. j = index;
  3732. do {
  3733. if (j < cnnodes_of_level[l] - 1) {
  3734. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  3735. } else {
  3736. /* j == cnnodes_of_level[l]-1, no nodes rechts */
  3737. possible_distance += distance;
  3738. }
  3739. j++;
  3740. }
  3741. while ((j < cnnodes_of_level[l]) && !(nl[j].done));
  3742. if (possible_distance < distance) {
  3743. distance = possible_distance;
  3744. }
  3745. j = index;
  3746. while (distance > 0) {
  3747. int d = 0;
  3748. int k = 0;
  3749. if (j == cnnodes_of_level[l] - 1) {
  3750. d = distance;
  3751. } else {
  3752. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  3753. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  3754. } else {
  3755. d = distance;
  3756. }
  3757. }
  3758. for (k = index; k <= j; k++) {
  3759. nl[k].node->absx += d;
  3760. }
  3761. j++;
  3762. distance -= d;
  3763. }
  3764. }
  3765. nl[index].done = 1; /* TRUE */
  3766. }
  3767. }
  3768. return;
  3769. }
  3770. static void do_up(int l)
  3771. {
  3772. int i = 0;
  3773. int index = 0;
  3774. int j = 0;
  3775. int optimal_position = 0;
  3776. int distance = 0;
  3777. int possible_distance = 0;
  3778. for (i = 0; i < cnnodes_of_level[l]; i++) {
  3779. index = find_next(cnnodes_of_level[l]);
  3780. if (nl[index].node) {
  3781. optimal_position = lower_barycenter(nl[index].node);
  3782. if (optimal_position == 0) {
  3783. optimal_position = nl[index].node->absx;
  3784. }
  3785. if (optimal_position < nl[index].node->absx) {
  3786. distance = nl[index].node->absx - optimal_position;
  3787. possible_distance = 0;
  3788. j = index;
  3789. do {
  3790. if (j > 0) {
  3791. possible_distance += nl[j].node->absx - nl[j - 1].node->absx - mindist;
  3792. } else {
  3793. /* j == 0, no nodes links */
  3794. possible_distance += nl[0].node->absx - mindist;
  3795. }
  3796. j--;
  3797. }
  3798. while ((j >= 0) && !(nl[j].done));
  3799. if (possible_distance < distance) {
  3800. distance = possible_distance;
  3801. }
  3802. j = index;
  3803. while (distance > 0) {
  3804. int d = 0;
  3805. int k = 0;
  3806. if (j == 0) {
  3807. d = distance;
  3808. } else {
  3809. if (nl[j].node->absx - nl[j - 1].node->absx - mindist < distance) {
  3810. d = nl[j].node->absx - nl[j - 1].node->absx - mindist;
  3811. } else {
  3812. d = distance;
  3813. }
  3814. }
  3815. for (k = j; k <= index; k++) {
  3816. nl[k].node->absx -= d;
  3817. }
  3818. j--;
  3819. distance -= d;
  3820. }
  3821. } else {
  3822. /* optimal_position >= nl[index].node->absx */
  3823. distance = optimal_position - nl[index].node->absx;
  3824. possible_distance = 0;
  3825. j = index;
  3826. do {
  3827. if (j < cnnodes_of_level[l] - 1) {
  3828. possible_distance += nl[j + 1].node->absx - nl[j].node->absx - mindist;
  3829. } else {
  3830. /* j == cnnodes_of_level[l]-1, no nodes rechts */
  3831. possible_distance += distance;
  3832. }
  3833. j++;
  3834. }
  3835. while ((j < cnnodes_of_level[l]) && !(nl[j].done));
  3836. if (possible_distance < distance) {
  3837. distance = possible_distance;
  3838. }
  3839. j = index;
  3840. while (distance > 0) {
  3841. int d = 0;
  3842. int k = 0;
  3843. if (j == cnnodes_of_level[l] - 1) {
  3844. d = distance;
  3845. } else {
  3846. if (nl[j + 1].node->absx - nl[j].node->absx - mindist < distance) {
  3847. d = nl[j + 1].node->absx - nl[j].node->absx - mindist;
  3848. } else {
  3849. d = distance;
  3850. }
  3851. }
  3852. for (k = index; k <= j; k++) {
  3853. nl[k].node->absx += d;
  3854. }
  3855. j++;
  3856. distance -= d;
  3857. }
  3858. }
  3859. nl[index].done = 1; /* TRUE */
  3860. }
  3861. }
  3862. return;
  3863. }
  3864. /* determine relative node pos. from the barycenter rel. node pos. */
  3865. static void improve_positions2local(struct gml_graph *g)
  3866. {
  3867. int i = 0;
  3868. int count = 0;
  3869. int ii = 0;
  3870. int sl = 0;
  3871. /* start level is 0 */
  3872. sl = 0;
  3873. /* min. node dist */
  3874. mindist = 1;
  3875. /* number of up/down sweeps */
  3876. count = 1;
  3877. for (ii = 0; ii < count; ii++) {
  3878. /* DOWN */
  3879. for (i = sl; i < g->maxlevel; i++) {
  3880. if (cnnodes_of_level[i]) {
  3881. nl = (struct node_data *)calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  3882. make_node_list_down(i);
  3883. do_down(i);
  3884. free(nl);
  3885. nl = NULL;
  3886. }
  3887. }
  3888. /* UP */
  3889. for (i = (g->maxlevel - 1); i >= sl; i--) {
  3890. if (cnnodes_of_level[i]) {
  3891. nl = (struct node_data *)calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  3892. make_node_list_up(i);
  3893. do_up(i);
  3894. free(nl);
  3895. nl = NULL;
  3896. }
  3897. }
  3898. }
  3899. /* top+bottom update */
  3900. if ((sl + 2) < g->maxlevel) {
  3901. for (i = sl + 2; i >= sl; i--) {
  3902. if (cnnodes_of_level[i]) {
  3903. nl = (struct node_data *)calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  3904. make_node_list_up(i);
  3905. do_up(i);
  3906. free(nl);
  3907. nl = NULL;
  3908. }
  3909. }
  3910. }
  3911. for (i = (g->maxlevel - 2); i <= g->maxlevel; i++) {
  3912. if (i >= 0) {
  3913. if (cnnodes_of_level[i]) {
  3914. nl = (struct node_data *)calloc(1, cnnodes_of_level[i] * sizeof(struct node_data));
  3915. make_node_list_down(i);
  3916. do_down(i);
  3917. free(nl);
  3918. nl = NULL;
  3919. }
  3920. }
  3921. }
  3922. return;
  3923. }
  3924. /* create nodes-at-level-count */
  3925. static void make_cnnodes_at_level(struct gml_graph *g)
  3926. {
  3927. struct gml_nlist *gnl = NULL;
  3928. cnnodes_of_level = (int *)calloc(1, ((g->maxlevel + 1) * sizeof(int)));
  3929. gnl = cnodelist;
  3930. while (gnl) {
  3931. cnnodes_of_level[gnl->node->rely] = cnnodes_of_level[gnl->node->rely] + 1;
  3932. gnl = gnl->next;
  3933. }
  3934. return;
  3935. }
  3936. /* clear nodes-at-level-count */
  3937. static void clear_cnnodes_at_level(void)
  3938. {
  3939. /* number of nodes at level */
  3940. if (cnnodes_of_level) {
  3941. free(cnnodes_of_level);
  3942. }
  3943. /* number of nodes at level */
  3944. cnnodes_of_level = NULL;
  3945. return;
  3946. }
  3947. /* copy part of graph */
  3948. static void make_cnodelist(struct gml_graph *g)
  3949. {
  3950. struct gml_nlist *gnl = NULL;
  3951. struct gml_nlist *newnl = NULL;
  3952. gnl = g->nodelist;
  3953. while (gnl) {
  3954. /* check if node belongs to part of graph */
  3955. if (gnl->node->startnode == csn) {
  3956. /* copy node in new list */
  3957. newnl = (struct gml_nlist *)calloc(1, sizeof(struct gml_nlist));
  3958. newnl->node = gnl->node;
  3959. if (cnodelist == NULL) {
  3960. cnodelist = newnl;
  3961. cnodelisttail = newnl;
  3962. } else {
  3963. cnodelisttail->next = newnl;
  3964. cnodelisttail = newnl;
  3965. }
  3966. }
  3967. gnl = gnl->next;
  3968. }
  3969. return;
  3970. }
  3971. /* done with this part of graph */
  3972. static void clear_cnodelist(void)
  3973. {
  3974. struct gml_nlist *gnl = NULL;
  3975. struct gml_nlist *gnlnext = NULL;
  3976. gnl = cnodelist;
  3977. while (gnl) {
  3978. gnlnext = gnl->next;
  3979. (void)free(gnl);
  3980. gnl = gnlnext;
  3981. }
  3982. /* node list of part of graph */
  3983. cnodelist = NULL;
  3984. cnodelisttail = NULL;
  3985. return;
  3986. }
  3987. /* move image of this part of graph */
  3988. static void move0(void)
  3989. {
  3990. struct gml_nlist *gnl = NULL;
  3991. int mx = 0;
  3992. /* find min. x pos in-use */
  3993. mx = 1000 * 1000; /* just some high value */
  3994. gnl = cnodelist;
  3995. while (gnl) {
  3996. if (gnl->node->absx < mx) {
  3997. mx = gnl->node->absx;
  3998. }
  3999. gnl = gnl->next;
  4000. }
  4001. /* move whole drawing to the left */
  4002. gnl = cnodelist;
  4003. while (gnl) {
  4004. gnl->node->absx = (gnl->node->absx - mx);
  4005. gnl = gnl->next;
  4006. }
  4007. return;
  4008. }
  4009. /* */
  4010. static void make_cposnodes(void)
  4011. {
  4012. struct gml_nlist *lnl = NULL;
  4013. struct gml_nlist *newl = NULL;
  4014. int i = 0;
  4015. int lmaxw = 0;
  4016. int maxrx = 0;
  4017. /* widest x level */
  4018. cwidestnnodes = 0;
  4019. /* x width at position */
  4020. cwpos = NULL;
  4021. /* lists per pos. */
  4022. cposnodes = NULL;
  4023. /* extra check max rel. x pos. */
  4024. lnl = cnodelist;
  4025. while (lnl) {
  4026. if (lnl->node->absx > maxrx) {
  4027. maxrx = lnl->node->absx;
  4028. }
  4029. lnl = lnl->next;
  4030. }
  4031. /* pos2.c has moved node in x dir. */
  4032. cwidestnnodes = maxrx;
  4033. /* x width at position */
  4034. cwpos = (int *)calloc(1, (cwidestnnodes + 1) * sizeof(int));
  4035. if (cwpos == NULL) {
  4036. return;
  4037. }
  4038. /* lists with nodes up to down at position */
  4039. cposnodes = (struct gml_nlist **)calloc(1, (cwidestnnodes + 1) * sizeof(struct gml_nlist *));
  4040. if (cposnodes == NULL) {
  4041. return;
  4042. }
  4043. /* create for every postion the list of nodes at that position */
  4044. lnl = cnodelist;
  4045. while (lnl) {
  4046. i = lnl->node->absx;
  4047. newl = (struct gml_nlist *)calloc(1, sizeof(struct gml_nlist));
  4048. if (newl == NULL) {
  4049. return;
  4050. }
  4051. newl->node = lnl->node;
  4052. if (cposnodes[i] == NULL) {
  4053. cposnodes[i] = newl;
  4054. newl->next = NULL;
  4055. } else {
  4056. newl->next = cposnodes[i];
  4057. cposnodes[i] = newl;
  4058. }
  4059. lnl = lnl->next;
  4060. }
  4061. /* determine the max width of a element at vertical pos. */
  4062. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4063. lmaxw = 0;
  4064. /* lists per pos. */
  4065. lnl = cposnodes[i];
  4066. while (lnl) {
  4067. if (lnl->node->bbx > lmaxw) {
  4068. lmaxw = lnl->node->bbx;
  4069. }
  4070. lnl = lnl->next;
  4071. }
  4072. cwpos[i] = lmaxw;
  4073. }
  4074. return;
  4075. }
  4076. /* */
  4077. static void clear_cposnodes(void)
  4078. {
  4079. int i = 0;
  4080. struct gml_nlist *lnl = NULL;
  4081. struct gml_nlist *nlnext = NULL;
  4082. /* width of positions */
  4083. if (cwpos) {
  4084. free(cwpos);
  4085. cwpos = NULL;
  4086. }
  4087. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4088. /* lists per pos. */
  4089. lnl = cposnodes[i];
  4090. while (lnl) {
  4091. nlnext = lnl->next;
  4092. free(lnl);
  4093. lnl = nlnext;
  4094. }
  4095. cposnodes[i] = NULL;
  4096. }
  4097. free(cposnodes);
  4098. cposnodes = NULL;
  4099. return;
  4100. }
  4101. /* y positioning */
  4102. static void make_clevelnodes(struct gml_graph *g)
  4103. {
  4104. struct gml_nlist *lnl = NULL;
  4105. struct gml_nlist *newl = NULL;
  4106. int i = 0;
  4107. int lmaxh = 0;
  4108. chpos = (int *)calloc(1, (g->maxlevel + 1) * sizeof(int));
  4109. if (chpos == NULL) {
  4110. return;
  4111. }
  4112. clevelnodes = calloc(1, (g->maxlevel + 1) * sizeof(struct gml_nlist *));
  4113. if (clevelnodes == NULL) {
  4114. return;
  4115. }
  4116. lnl = cnodelist;
  4117. while (lnl) {
  4118. i = lnl->node->absy;
  4119. newl = calloc(1, sizeof(struct gml_nlist));
  4120. if (newl == NULL) {
  4121. return;
  4122. }
  4123. newl->node = lnl->node;
  4124. if (clevelnodes[i] == NULL) {
  4125. clevelnodes[i] = newl;
  4126. newl->next = NULL;
  4127. } else {
  4128. newl->next = clevelnodes[i];
  4129. clevelnodes[i] = newl;
  4130. }
  4131. lnl = lnl->next;
  4132. }
  4133. /* determine the max width of a element at vertical pos. */
  4134. for (i = 0; i < (g->maxlevel + 1); i++) {
  4135. lmaxh = 0;
  4136. /* lists per pos. */
  4137. lnl = clevelnodes[i];
  4138. while (lnl) {
  4139. if (lnl->node->bby > lmaxh) {
  4140. lmaxh = lnl->node->bby;
  4141. }
  4142. lnl = lnl->next;
  4143. }
  4144. chpos[i] = lmaxh;
  4145. }
  4146. return;
  4147. }
  4148. static void clear_clevelnodes(struct gml_graph *g)
  4149. {
  4150. int i = 0;
  4151. struct gml_nlist *lnl = NULL;
  4152. struct gml_nlist *nlnext = NULL;
  4153. /* width of positions */
  4154. if (chpos) {
  4155. free(chpos);
  4156. chpos = NULL;
  4157. }
  4158. for (i = 0; i < (g->maxlevel + 1); i++) {
  4159. /* lists per pos. */
  4160. lnl = clevelnodes[i];
  4161. while (lnl) {
  4162. nlnext = lnl->next;
  4163. free(lnl);
  4164. lnl = nlnext;
  4165. }
  4166. clevelnodes[i] = NULL;
  4167. }
  4168. free(clevelnodes);
  4169. clevelnodes = NULL;
  4170. return;
  4171. }
  4172. /* determine final (x,y) pos */
  4173. static void cfinalxy(struct gml_graph *g)
  4174. {
  4175. struct gml_nlist *lnl = NULL;
  4176. int hw = 0;
  4177. int xoff = 0;
  4178. int yoff = 0;
  4179. int i = 0;
  4180. int ecount = 0;
  4181. /* x positioning */
  4182. make_cposnodes();
  4183. cmaxx = 0;
  4184. xoff = 0;
  4185. /* scan hor. to adjust the x positions. */
  4186. for (i = 0; i < (cwidestnnodes + 1); i++) {
  4187. /* x spacing between the hor. levels */
  4188. if (0) {
  4189. xoff = xoff + xspacing;
  4190. }
  4191. /* determine half-way of the xpos. */
  4192. if (cwpos[i] == 0) {
  4193. /* if only dummy nodes */
  4194. hw = xspacing / 2;
  4195. } else {
  4196. hw = (cwpos[i] / 2);
  4197. }
  4198. /* update with current x */
  4199. hw = hw + xoff;
  4200. lnl = cposnodes[i];
  4201. /* scan the nodes at this x pos. */
  4202. while (lnl) {
  4203. /* center the node around the half-way */
  4204. lnl->node->finx = (hw - (lnl->node->bbx / 2));
  4205. if ((lnl->node->finx + lnl->node->bbx) > cmaxx) {
  4206. cmaxx = (lnl->node->finx + lnl->node->bbx);
  4207. }
  4208. lnl = lnl->next;
  4209. }
  4210. /* set x0,x1 pos in the nodes */
  4211. lnl = cposnodes[i];
  4212. /* scan the nodes at this x pos. */
  4213. while (lnl) {
  4214. /* */
  4215. lnl->node->lx0 = xoff;
  4216. lnl->node->lx1 = xoff + cwpos[i];
  4217. lnl = lnl->next;
  4218. }
  4219. /* x spacing between the hor. levels */
  4220. xoff = xoff + xspacing;
  4221. /* x to next pos. */
  4222. xoff = xoff + cwpos[i];
  4223. }
  4224. /* */
  4225. clear_cposnodes();
  4226. /* y positioning */
  4227. make_clevelnodes(g);
  4228. cmaxy = 0;
  4229. yoff = 0;
  4230. /* number of edges between level n and n+1 */
  4231. g->nume = (int *)calloc(1, (g->maxlevel + 1) * sizeof(int));
  4232. /* scan vert. to adjust the y positions. */
  4233. for (i = 0; i < (g->maxlevel + 1); i++) {
  4234. /* y spacing between the vert. levels */
  4235. if (0) {
  4236. yoff = (yoff + yspacing);
  4237. }
  4238. /* determine half-way of the ypos. */
  4239. if (chpos[i] == 0) {
  4240. /* if only dummy nodes */
  4241. hw = (yspacing / 2);
  4242. } else {
  4243. hw = (chpos[i] / 2);
  4244. }
  4245. /* update with current y */
  4246. hw = hw + yoff;
  4247. lnl = clevelnodes[i];
  4248. ecount = 0;
  4249. /* scan the nodes at this y pos. */
  4250. while (lnl) {
  4251. /* set start, end of y level */
  4252. lnl->node->ly0 = yoff;
  4253. lnl->node->ly1 = (yoff + chpos[i]);
  4254. /* center the node around the half-way */
  4255. lnl->node->finy = (hw - (lnl->node->bby / 2));
  4256. /* update drawing max y pos used */
  4257. if ((lnl->node->finy + lnl->node->bby) > cmaxy) {
  4258. cmaxy = (lnl->node->finy + lnl->node->bby);
  4259. }
  4260. /* give dummy nodes a vertical size of the level */
  4261. if (lnl->node->dummy) {
  4262. lnl->node->bby = chpos[i];
  4263. /* if only dummy nodes at level, use spacing */
  4264. if (chpos[i] == 0) {
  4265. lnl->node->bby = yspacing;
  4266. }
  4267. }
  4268. /* number of edges between level n and n+1 */
  4269. ecount = ecount + lnl->node->outdegree;
  4270. lnl = lnl->next;
  4271. }
  4272. g->nume[i] = ecount;
  4273. /* y spacing between the vert. levels */
  4274. yoff = yoff + yspacing;
  4275. /* yspacing depends on number of edges at this level
  4276. * turned off, does increase y too much
  4277. * yoff = yoff + (ecount * 2);
  4278. */
  4279. /* yspacing depends on number of crossing edges at this level
  4280. * temp test
  4281. */
  4282. yoff = yoff + (1 * (g->numce[i] / 16));
  4283. /* y to next pos. */
  4284. yoff = yoff + chpos[i];
  4285. }
  4286. clear_clevelnodes(g);
  4287. /* clear number of edges between level n and n+1 */
  4288. if (g->nume) {
  4289. free(g->nume);
  4290. g->nume = NULL;
  4291. }
  4292. return;
  4293. }
  4294. static void movefinal(int xoffset)
  4295. {
  4296. struct gml_nlist *gnl = NULL;
  4297. gnl = cnodelist;
  4298. while (gnl) {
  4299. gnl->node->finx = gnl->node->finx + xoffset;
  4300. gnl->node->lx0 = gnl->node->lx0 + xoffset;
  4301. gnl->node->lx1 = gnl->node->lx1 + xoffset;
  4302. gnl = gnl->next;
  4303. }
  4304. return;
  4305. }
  4306. /* dummy nodes can be centered, or left/right most placed */
  4307. static void tunedummy(struct gml_graph *g)
  4308. {
  4309. struct gml_nlist *gnl = NULL;
  4310. int x1 = 0;
  4311. int x2 = 0;
  4312. int x3 = 0;
  4313. gnl = g->nodelist;
  4314. while (gnl) {
  4315. if (gnl->node->dummy) {
  4316. x1 = gnl->node->finx;
  4317. x2 = gnl->node->incoming_e->edge->from_node->finx + gnl->node->incoming_e->edge->from_node->bbx / 2;
  4318. x3 = gnl->node->outgoing_e->edge->to_node->finx + gnl->node->outgoing_e->edge->to_node->bbx / 2;
  4319. if ((x1 == x2) && (x1 == x3)) {
  4320. /* no move */
  4321. } else {
  4322. if ((x2 < x1) && (x3 < x1)) {
  4323. /* to left */
  4324. gnl->node->finx = gnl->node->lx0;
  4325. }
  4326. if ((x2 > x1) && (x3 > x1)) {
  4327. /* to right */
  4328. gnl->node->finx = gnl->node->lx1;
  4329. }
  4330. }
  4331. }
  4332. gnl = gnl->next;
  4333. }
  4334. return;
  4335. }
  4336. /* move some nodes up/down */
  4337. static void tunenodes(struct gml_graph *g)
  4338. {
  4339. struct gml_nlist *gnl = NULL;
  4340. gnl = g->nodelist;
  4341. while (gnl) {
  4342. /* only at real nodes */
  4343. if (gnl->node->dummy == 0) {
  4344. if (gnl->node->hashedge) {
  4345. /* do not move node with hor. edge */
  4346. } else {
  4347. if (gnl->node->indegree > 0 && gnl->node->outdegree == 0) {
  4348. /* move up */
  4349. gnl->node->finy = gnl->node->ly0;
  4350. }
  4351. if (gnl->node->indegree == 0 && gnl->node->outdegree > 0) {
  4352. /* move down */
  4353. gnl->node->finy = (gnl->node->ly1 - gnl->node->bby);
  4354. }
  4355. if (gnl->node->indegree > 0 && gnl->node->outdegree > 0) {
  4356. if (gnl->node->indegree == gnl->node->outdegree) {
  4357. /* no movement
  4358. *
  4359. */
  4360. } else {
  4361. if (gnl->node->indegree > gnl->node->outdegree) {
  4362. /* move up */
  4363. gnl->node->finy = gnl->node->ly0;
  4364. }
  4365. if (gnl->node->outdegree > gnl->node->indegree) {
  4366. /* move down */
  4367. gnl->node->finy = (gnl->node->ly1 - gnl->node->bby);
  4368. }
  4369. }
  4370. }
  4371. }
  4372. }
  4373. gnl = gnl->next;
  4374. }
  4375. return;
  4376. }
  4377. /* position in parts of graph at each step */
  4378. static void improve_positions(struct gml_graph *g)
  4379. {
  4380. struct gml_nlist *gnl = NULL;
  4381. int i = 0;
  4382. int xoffset = 0;
  4383. xspacing = g->xspacing;
  4384. yspacing = g->yspacing;
  4385. /* copy the rel(x,y) pos into abs(x,y) and modify the absx pos here */
  4386. gnl = g->nodelist;
  4387. while (gnl) {
  4388. gnl->node->bbx = gnl->node->tx;
  4389. gnl->node->bby = gnl->node->ty;
  4390. gnl->node->absx = gnl->node->relx;
  4391. gnl->node->absy = gnl->node->rely;
  4392. gnl->node->finx = 0;
  4393. gnl->node->finy = 0;
  4394. gnl = gnl->next;
  4395. }
  4396. /* offset in drawing of part of graph */
  4397. xoffset = 0;
  4398. for (i = 0; i < g->nstartnodes; i++) {
  4399. /* set current startnode */
  4400. csn = g->startnodes[i];
  4401. /* print progress info */
  4402. if ((i == 0) || (i == g->nstartnodes / 2) || (i == g->nstartnodes - 1)) {
  4403. }
  4404. /* max. x in part of graph */
  4405. cmaxx = 0;
  4406. /* copy part of graph */
  4407. make_cnodelist(g);
  4408. /* create nodes-at-level-count */
  4409. make_cnnodes_at_level(g);
  4410. /* run up/down placement */
  4411. improve_positions2local(g);
  4412. /* move image of this part of graph */
  4413. move0();
  4414. /* set final x,y */
  4415. cfinalxy(g);
  4416. /* tune dummy nodes */
  4417. tunedummy(g);
  4418. /* tune nodes */
  4419. tunenodes(g);
  4420. /* move */
  4421. movefinal(xoffset);
  4422. /* update for next */
  4423. xoffset = xoffset + cmaxx + xspacing;
  4424. /* clear nodes-at-level-count */
  4425. clear_cnnodes_at_level();
  4426. /* done with this part of graph */
  4427. clear_cnodelist();
  4428. }
  4429. /* position level 0, single nodes if any */
  4430. if (g->nsinglenodes) {
  4431. /* done in finalxy() in main.c */
  4432. }
  4433. return;
  4434. }
  4435. /* for pos2.c which does set finx,finy */
  4436. static void finalxy(struct gml_graph *g)
  4437. {
  4438. struct gml_nlist *lnl = NULL;
  4439. int maxx = 0;
  4440. int maxy = 0;
  4441. int curx = 0;
  4442. int my = 0;
  4443. /* position the single nodes */
  4444. if (g->nsinglenodes) {
  4445. lnl = maingraph->singlenodelist;
  4446. while (lnl) {
  4447. lnl->node->finx = curx;
  4448. curx = curx + g->xspacing + lnl->node->bbx;
  4449. if (lnl->node->bby > my) {
  4450. my = lnl->node->bby;
  4451. }
  4452. lnl = lnl->next;
  4453. }
  4454. my = my + g->yspacing;
  4455. /* update level data for singlenodes */
  4456. lnl = maingraph->singlenodelist;
  4457. while (lnl) {
  4458. lnl->node->ly0 = 0;
  4459. lnl->node->ly1 = my;
  4460. lnl = lnl->next;
  4461. }
  4462. }
  4463. /* determine max. x pos in use */
  4464. lnl = maingraph->nodelist;
  4465. while (lnl) {
  4466. if ((lnl->node->finx + lnl->node->bbx) > maxx) {
  4467. maxx = lnl->node->finx + lnl->node->bbx;
  4468. }
  4469. /* correct for height of single nodes if any */
  4470. if (lnl->node->indegree || lnl->node->outdegree) {
  4471. lnl->node->finy = lnl->node->finy + my;
  4472. }
  4473. /* update drawing max y pos used */
  4474. if ((lnl->node->finy + lnl->node->bby) > maxy) {
  4475. maxy = (lnl->node->finy + lnl->node->bby);
  4476. }
  4477. lnl = lnl->next;
  4478. }
  4479. g->maxx = maxx;
  4480. g->maxy = maxy;
  4481. return;
  4482. }
  4483. static struct gml_edge *findedge(int num)
  4484. {
  4485. struct gml_elist *el = NULL;
  4486. struct gml_edge *e = NULL;
  4487. if (maingraph == NULL) {
  4488. return (NULL);
  4489. }
  4490. el = maingraph->edgelist;
  4491. while (el) {
  4492. e = el->edge;
  4493. if (e->nr == num) {
  4494. break;
  4495. }
  4496. el = el->next;
  4497. }
  4498. return (e);
  4499. }
  4500. /* update node min/max and edge min/max */
  4501. static void setminmax(struct gml_graph *g)
  4502. {
  4503. struct gml_nlist *nl = NULL;
  4504. struct gml_elist *el = NULL;
  4505. int count = 0;
  4506. g->nodemin = 0;
  4507. g->nodemax = 0;
  4508. g->edgemin = 0;
  4509. g->edgemax = 0;
  4510. nl = g->nodelist;
  4511. count = 0;
  4512. while (nl) {
  4513. if (count == 0) {
  4514. g->nodemin = nl->node->nr;
  4515. g->nodemax = nl->node->nr;
  4516. } else {
  4517. if (nl->node->nr < g->nodemin) {
  4518. g->nodemin = nl->node->nr;
  4519. }
  4520. if (nl->node->nr > g->nodemax) {
  4521. g->nodemax = nl->node->nr;
  4522. }
  4523. }
  4524. count++;
  4525. nl = nl->next;
  4526. }
  4527. el = g->edgelist;
  4528. count = 0;
  4529. while (el) {
  4530. if (count == 0) {
  4531. g->edgemin = el->edge->nr;
  4532. g->edgemax = el->edge->nr;
  4533. } else {
  4534. if (el->edge->nr < g->edgemin) {
  4535. g->edgemin = el->edge->nr;
  4536. }
  4537. if (el->edge->nr > g->edgemax) {
  4538. g->edgemax = el->edge->nr;
  4539. }
  4540. }
  4541. count++;
  4542. el = el->next;
  4543. }
  4544. return;
  4545. }
  4546. /* end zzzz */