{"id":15965204,"url":"https://github.com/bwplotka/w3-graph","last_synced_at":"2025-09-05T08:31:32.839Z","repository":{"id":79051160,"uuid":"45036234","full_name":"bwplotka/w3-graph","owner":"bwplotka","description":"Walrus graph visualization using WebGL via THREE.js lib.","archived":false,"fork":false,"pushed_at":"2016-02-12T11:45:07.000Z","size":895,"stargazers_count":1,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-19T13:33:41.730Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bwplotka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-27T11:26:14.000Z","updated_at":"2017-05-23T13:30:16.000Z","dependencies_parsed_at":"2023-05-01T04:54:54.510Z","dependency_job_id":null,"html_url":"https://github.com/bwplotka/w3-graph","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fw3-graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fw3-graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fw3-graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fw3-graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bwplotka","download_url":"https://codeload.github.com/bwplotka/w3-graph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232031973,"owners_count":18462965,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-07T17:40:23.782Z","updated_at":"2024-12-31T21:33:21.757Z","avatar_url":"https://github.com/bwplotka.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WebGL 3D Graph Visualisations aka 'w3-graph'\n\n![Walrus](https://github.com/Bplotka/w3-graph/blob/master/doc/walrus.jpg)\n\nWalrus graph visualization using WebGL via THREE.js lib.\nIt renders tree graphs in a 3D space within sphere.\n\n## Usage:\n\n1. Create two files `labels` and `links`\n2. Import `labels` using `Menu \u003e Import labels`\n3. Import `links` using `Menu \u003e Import links`\n\n`Labels` should contain pairs `name -\u003e id` delimited by `/t` or space:\n\n```\nNAME1 1\nNAME2 2\n```\n\n`Links` should contain neighbourhood matrix (aka adjacency matrix) delimited by `/t` or space:\n\n```\n1 2 3 4 5 6\n2 7 8 9 10\n```\n\nMake sure there is no `new line` at the end of your files.\n\n## Results:\n![Example](https://github.com/Bplotka/w3-graph/blob/master/doc/w3-walrus-example.PNG)\n\n![Example2](https://github.com/Bplotka/w3-graph/blob/master/doc/w3-walrus-example2.PNG)\n\n## Implementation details:\n\nAuthor: [Bplotka](https://github.com/Bplotka).\n\n\nCurrently, algorithm is able to distribute \u0026 render thousands of nodes in couple of seconds.\n\nThe algorithm includes several steps:\n\n1. _Parsing phase._ Parsing input in two .CSV to the js graph structure.\nFirst .csv contains label -\u003e id map, Second contains adjacency matrix.\nThat data is simply parsed to data structure based on: [chenglou/data-structures](https://github.com/chenglou/data-structures).\nIt contains list of `nodes`. Each node includes two list of edges: `outEdges`,\n`inEdges` and couple of other statistics like:\n\n    ```\n    node = {\n      _outEdges: {},\n      _inEdges: {},\n      name: _name,\n      root: _root,\n      depth: 0,\n      _id: id,\n      obj: null,\n      position: new THREE.Vector3(0, 0, 0),\n      parentId: null,\n      direction: new THREE.Vector3(0, 1, 0),\n      subNodes: 0, // Meaningful children.\n      edgeLength: 0,\n      children: 0,\n      deltaRotByAxisDir: 0,\n      rotByAxisDir: 0,\n      rotByAxisPerpDirA: 0,\n      rotByAxisPerpDirB: 0,\n      distanceToRoot: 0\n    }\n  ```\nStoring other graph types than `tree` is currently blocked. Each node can have only one parent.\nThese statistics are needed for further calculations and filled during `addEdge` function.\nImportant additional structures are `levels`:\n![Level](https://github.com/Bplotka/w3-graph/blob/master/doc/w3-walrus-level.PNG)\nLevel is a group of all nodes without children which share a single parent. They are aggregated and rendered as as half of 3D sphere. Levels are filled in early parsing stage during `addEdge` function.\nLavel structure is simple:\n  ```\n  level = {\n    size: 0,\n    members: []\n  }\n  ```\nThis stage is done in `app/data/graph-parser.js`. Structure is defined in `app/data/data-structures.js`\n\n2. _Statistic phase._ Before we render the tree we need to collect some statistic and store it in graph attributes like:\n  * `maxWidth`: Maximum tree width - maximum number of children having their children and sharing single parent.\n  * `avgWidth`: Average tree width - average number of children having their children and sharing single parent.\n  * `nodesWithChildren`: Number of nodes which have children.\n  * `maxDistanceToRoot`: Longest path from the root to the leaf.\n  * `depth`: Depth of the tree.\nIn this stage we also calculate some node's attribute like `rotation directions`, `distanceToRoot`, `subNodes` (number of children with children of per each node), `edgeLength` (based on number of subNodes: more subNodes, longer edge), `deltaRotByAxisDir` (360 degrees / number of subNodes) and `depth` of each node.\nAdditionally, we calculate the needed `RADIUS` of a sphere in which we render our tree. This is based on longest path in graph from root.\nAll data are calculated using [BFS](https://en.wikipedia.org/wiki/Breadth-first_search) in file `app/data/data-structures.js`.\n\n3. _Rendering phase._ This stage calculates position of the nodes and edges in the sphere.\n\n![Level-Axis](https://github.com/Bplotka/w3-graph/blob/master/doc/w3-walrus-level-axis.PNG)\n\nAlgorithm steps:\n  1. Use BFS and foreach node:\n    1. Get all children of this node.\n    2. If the node has not any children then it is a leaf and was rendered in his parent `level` so continue to i.\n      1. If it's root node than render it in the CENTER (defined in `app/render/scene.js`)\n      2. If it's a different node get his parentNode.\n        1. Copy `direction` from his parent. `direction` is a normalized `Vector3` which specifies direction in which node should be placed.\n        2. Calculate normalized perpendicular `Vector3` to `direction`. (Using cross)\n        3. Apply initial rotation to node `direction` around axis specified by perpendicular direction. (currently it is 45 degree)\n        4. Apply rotation around the parent `direction` axis by `rotByAxisDir` calculated in previous stage to `direction`.\n        5. Apply additional rotation around perpendicular direction axis by different angles to `direction` using this algorithm:\n          1. For root children:\n            * Each fourth child of parent will apply by `rotByAxisPerpDirB` angle. (Currently: -22 degrees)\n            * Each fourth + 1 child of parent will apply by 2 * `rotByAxisPerpDirB` angle. (Currently: -44 degrees)\n            * Each fourth + 2 child of parent will apply by `rotByAxisPerpDirA` angle. (Currently: 75 degrees)\n            * Each fourth + 3 child of parent will apply by 2 * `rotByAxisPerpDirA` angle. (Currently: 150 degrees)\n          2. else:\n            * Always apply by `rotByAxisPerpDirB` angle. (Currently: -22 degrees)\n        6. Sum `rotByAxisDir` with parent `deltaRotByAxisDir`\n        7. Copy `position` from his parent.\n        8. Move `position` by adding `direction` multiplied by parentNode `edgeLength` calculated in previous stage.\n        9. Render vertex knowing `position`, `direction` (if rendering is detailed and includes cubes we need to know the rotation), `sizeNode` and node `name`\n        10. Render edge from parent to node.\n    3. If it has leaf children then render `level` in specified node `direction`:\n      * Check the _rendering level phase_ below\n    4. Add children to `nodesToVisis` BFS list\nThis stage is done in `app/render/model/graph.js`.\n\n4. _Rendering level phase._ This stage is included in _Renderig phase_ but it also quite complex so i have created additional phase for that.\n\n ![Level-Algorithm](https://github.com/Bplotka/w3-graph/blob/master/doc/w3-walrus-level-alg.png)\n\n The algorithm goes as follows:\n\n  1. Calculate perpendicular `Vector3` to given `direction` of the owner.\n  2. Calculate area of the square made with all `members` of the `level` with some interval between (all leafs of the owner)\n  3. Find radius of a circle which covers area calculated in 2\n  4. Check if radius is not smaller than MIN_LVL_RADIUS (which is sphere radius / 10)\n  5. Calculate how many sub levels (rings) can be placed within our circle.\n  6. Calculate relative X angle (How we should move down in 3D sphere around the relative X axis). Divide 120 degrees by number of `subLevels` (We want to have this special `flower` look)\n  7. Calculate initial `sphereVec` which simply indicates the movement from owner to leaf localization.\n  8. `numNodesOnSubLvl` = 1 since we have only one leaf on the top.\n  9. Init `angleRelY` (rotation around relative Y axis). For the first leaf it does not matter. It will indicates on how many degree we should rotate around center of the sphere (360 / `numNodesOnSubLvl`)\n  10. Foreach `level` `member`:\n    1. Copy `position` from `owner`. Compose base position.\n    2. If we need to move down (if `numNodesOnSubLvl` \u003c= 0)\n    3. Apply a `angleRelX` rotation to `sphereVec` to move down the sphere by perpendicular to direction vector\n    4. Add `angleRelX` to `summaricAngleRelX`\n    5. Calculate how many nodes can be in the given radius after moving down:\n    6. Calculate `subRadius` (sin(`summaricAngleRelX`) * radius)\n    7. Calculate circuit length\n    8. Calculate how many nodes (leafs) can be placed within given circuit\n    9. If it is the last `subLvl` or there is no space for any leaf on `subLvl` or there are less nodes to be placed in next `subLvl` than in current:\n      1. Take all leafs which have not been placed yet and place to `numNodesOnSubLvl`.\n    10. Create reverse calculation to obtain the needed `angleRelX` from circuit length made by all `numNodesOnSubLvl`. (This is needed to fix the overlapping leafs)\n    11. Apply new rotation to the `sphereVec`\n    12. Calculate `angleRelY` knowing the number of nodes in `subLvl`\n    13. Move `position` using `sphereVec` indicator.\n    14. Render vertex in `position` in `direction` of the owner\n    15. Render edge from owner to leaf\n    16. Apply a `angleRelY` rotation to `sphereVec` (our direction \u0026 distance indicator)\n    17. Decrement `numNodesOnSubLvl`\n\nThis stage is done in `app/render/model/graph.js`\n\n## Next Steps \u0026 TODOs:\n\n1. When the input contains large, wide tree, the visualization could be not clear due to many intersections. When the input contains large, deep tree, the `w3-walrus` will handle it gracefully. _This should be fixed_ as a first next step. The ideas to mitigate that:\n\n  * Improve distribution of the children using more various angles based on the adjacent nodes. (check  `3 _Rendering phase._ -\u003e i -\u003e b -\u003e b-\u003e e` algorithm step)\n  * Improve rendering algorithm with conflicts detection. If we firstly prepare the `levels` we will be aware of their size. As a result we can implement the [Knapsack algorithm](https://en.wikipedia.org/wiki/Knapsack_problem) which will distribute the `level` properly without conflicts and intersections. This will take a lot of work though. ((:\n\n2. Walrus could have panel for node information retrieval \u0026 editing. Work items:\n  1. Extend node info in `app/data/data-structures.js`\n  2. Create UI panel for parameters presentation and editing\n  3. Exporting to `csv` module\n\n3. Support for other graph formats e.g `GML`, `GRXL`\n4. Validation of graph (too many nodes etc)\n5. Benchmarking (Max number of nodes)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwplotka%2Fw3-graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbwplotka%2Fw3-graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwplotka%2Fw3-graph/lists"}