Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
feat: simple draw
Browse files Browse the repository at this point in the history
fix algorithm detail error.
  • Loading branch information
neko-para committed Aug 22, 2023
1 parent e49e81b commit b9dd0aa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
81 changes: 80 additions & 1 deletion packages/client/src/views/VisualView.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<script setup lang="ts">
import { NButton } from 'naive-ui'
import { ref } from 'vue'
import { taskForwardIndex } from '@/data'
import NavigationButtons from '@/components/NavigationButtons.vue'
import MainLayout from '@/layout/MainLayout.vue'
type VertInfo = {
name: string
x: number
y: number
}
type EdgeInfo = {
x1: number
y1: number
x2: number
y2: number
}
const vertInfo = ref<VertInfo[]>([])
const edgeInfo = ref<EdgeInfo[]>([])
function render() {
const vertIndex = Object.keys(taskForwardIndex.value)
const edges: number[] = []
Expand All @@ -15,7 +33,45 @@ function render() {
edges.push(vertIndex.indexOf(to))
}
}
Module.layoutGraph(vertIndex.length, edges, vertIndex)
const result = Module.layoutGraph(vertIndex.length, edges, vertIndex)
const vertArray: VertInfo[] = []
const vertMapper: Record<string, VertInfo> = {}
const edgeArray: EdgeInfo[] = []
for (const part of result) {
for (const [yy, layer] of part.entries()) {
for (const [xx, id] of layer.entries()) {
const vi: VertInfo = {
name: vertIndex[id],
x: xx,
y: yy
}
vertMapper[vertIndex[id]] = vi
vertArray.push(vi)
}
}
}
for (const from of vertIndex) {
const vf = vertMapper[from]
for (const to of taskForwardIndex.value[from]) {
const vt = vertMapper[to]
edgeArray.push({
x1: vf.x,
y1: vf.y,
x2: vt.x,
y2: vt.y
})
}
}
vertInfo.value = vertArray
edgeInfo.value = edgeArray
}
function mX(x: number) {
return x * 40 + 20
}
function mY(y: number) {
return y * 40 + 20
}
</script>

Expand All @@ -25,5 +81,28 @@ function render() {
<NavigationButtons></NavigationButtons>
<NButton @click="render">计算</NButton>
</template>

<div>
<svg width="1000" height="1000">
<circle
v-for="info in vertInfo"
:key="info.name"
:cx="mX(info.x)"
:cy="mY(info.y)"
r="5"
fill="wheat"
@mouseover="() => console.log(info.name)"
></circle>
<line
v-for="(info, idx) in edgeInfo"
:key="idx"
:x1="mX(info.x1)"
:y1="mY(info.y1)"
:x2="mX(info.x2)"
:y2="mY(info.y2)"
style="stroke: rgb(0, 0, 0); stroke-width: 1"
></line>
</svg>
</div>
</MainLayout>
</template>
6 changes: 5 additions & 1 deletion packages/client/src/wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare const Module: {
layoutGraph: (vertCount: number, edges: number[], names: string[]) => void
layoutGraph: (
vertCount: number,
edges: number[],
names: string[]
) => number[][][]
}
12 changes: 10 additions & 2 deletions render/layering.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "graph.h"

#include <stdio.h>

std::vector<std::vector<int>> getNaiveLayer(const Graph &edges) {
int n = edges.size();
std::vector<int> inbound(n, 0);
Expand Down Expand Up @@ -89,10 +91,16 @@ void compactLayer(const Graph &edges, std::map<int, int> &layer) {
}
}
}
int delta;
if (vis[findFrom]) {
layer[findTo] = layer[findFrom] + 1;
delta = layer[findTo] - (layer[findFrom] + 1);
} else {
layer[findFrom] = layer[findTo] - 1;
delta = layer[findFrom] - (layer[findTo] - 1);
}
for (int i = 0; i < n; i++) {
if (vis[i]) {
layer[i] += delta;
}
}
}
}
22 changes: 12 additions & 10 deletions render/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void dumpGraph(const Graph &edges) {
printf("dump graph end\n");
}

void layoutGraph(int n, const emscripten::val &v_edges,
const emscripten::val &v_names) {
emscripten::val layoutGraph(int n, const emscripten::val &v_edges,
const emscripten::val &v_names) {
auto edgeVerts = convertToVector<int>(v_edges);
auto names = convertToVector<std::string>(v_names);
std::vector<std::set<int>> edges(n);
Expand Down Expand Up @@ -54,6 +54,8 @@ void layoutGraph(int n, const emscripten::val &v_edges,

auto vertsGroups = splitGraph(buildIndirectGraph(edges));

emscripten::val result = emscripten::val::array();

for (const auto &toMain : vertsGroups) {
int m = toMain.size();
std::vector<int> toSub(n, -1);
Expand All @@ -69,30 +71,30 @@ void layoutGraph(int n, const emscripten::val &v_edges,

auto initLayers = getNaiveLayer(subGraph);
std::map<int, int> layer;
printf("dump naive layer\n");
for (int i = 0; i < initLayers.size(); i++) {
printf("%d:", i);
for (auto v : initLayers[i]) {
layer[v] = i;
printf(" %s", names[toMain[v]].c_str());
}
puts("");
}
compactLayer(subGraph, layer);

printf("dump compact layer\n");
std::map<int, std::set<int>> flatLayer;
for (auto [vt, lv] : layer) {
flatLayer[lv].insert(vt);
}

emscripten::val partResult = emscripten::val::array();
for (const auto &[lv, vts] : flatLayer) {
printf("%d:", lv);
emscripten::val row = emscripten::val::array(); // printf("%d:", lv);
for (auto v : vts) {
printf(" %s", names[toMain[v]].c_str());
row.call<void>("push", toMain[v]);
}
puts("");
partResult.call<void>("push", row);
}
result.call<void>("push", partResult);
}

return result;
}

EMSCRIPTEN_BINDINGS(my_module) {
Expand Down

0 comments on commit b9dd0aa

Please sign in to comment.