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

Commit

Permalink
feat: finish sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Aug 23, 2023
1 parent 6107717 commit a73e08f
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 26 deletions.
29 changes: 15 additions & 14 deletions packages/client/src/views/VisualView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,26 @@ function render() {
}
}
const result = Module.layoutGraph(vertIndex.length, edges, vertIndex)
console.log(result)
const vertArray: VertInfo[] = []
const vertMapper: Record<string, VertInfo> = {}
const vertMapper: Record<number, VertInfo> = {}
const edgeArray: EdgeInfo[] = []
for (const part of result) {
for (const part of result.vert) {
for (const [yy, layer] of part.entries()) {
for (const [xx, id] of layer.entries()) {
const vi: VertInfo = {
name: vertIndex[id],
name: id < vertIndex.length ? vertIndex[id] : '',
x: xx,
y: yy
}
vertMapper[vertIndex[id]] = vi
vertMapper[id] = vi
vertArray.push(vi)
}
}
}
for (const from of vertIndex) {
const vf = vertMapper[from]
for (const to of taskForwardIndex.value[from]) {
for (const part of result.edge) {
for (const [from, to] of part) {
const vf = vertMapper[from]
const vt = vertMapper[to]
edgeArray.push({
x1: vf.x,
Expand All @@ -67,11 +68,11 @@ function render() {
}
function mX(x: number) {
return x * 40 + 20
return x * 20 + 10
}
function mY(y: number) {
return y * 40 + 20
return y * 20 + 10
}
</script>

Expand All @@ -83,15 +84,15 @@ function mY(y: number) {
</template>

<div>
<svg width="1000" height="1000">
<svg width="1500" height="1000">
<circle
v-for="info in vertInfo"
:key="info.name"
v-for="(info, idx) in vertInfo"
:key="info.name ?? idx"
:cx="mX(info.x)"
:cy="mY(info.y)"
r="5"
fill="wheat"
@mouseover="() => console.log(info.name)"
:fill="info.name ? 'red' : 'white'"
stroke="black"
></circle>
<line
v-for="(info, idx) in edgeInfo"
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ declare const Module: {
vertCount: number,
edges: number[],
names: string[]
) => number[][][]
) => {
vert: number[][][]
edge: [number, number][][]
}
}
2 changes: 1 addition & 1 deletion render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ project(MaaJsonViewer)

set(CMAKE_CXX_STANDARD 20)

add_executable(render main.cpp graph.cpp deringing.cpp layering.cpp)
add_executable(render main.cpp graph.cpp deringing.cpp layering.cpp layout.cpp)

target_link_libraries(render embind)
10 changes: 10 additions & 0 deletions render/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ GraphWithValue buildIndirectGraphWithOrder(const Graph &edges) {
return result;
}

GraphWithValue buildIndirectTreeGraph(std::vector<std::pair<int, int>> &tree) {
int n = tree.size() + 1;
GraphWithValue result(n);
for (auto [f, t] : tree) {
result[f].emplace(t, 1);
result[t].emplace(f, -1);
}
return result;
}

void travel(int cur, const Graph &edges, std::vector<int> &vis,
std::vector<int> &verts) {
vis[cur] = 1;
Expand Down
13 changes: 13 additions & 0 deletions render/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ Graph getFAC(const Graph &edges);

std::vector<std::vector<int>> getNaiveLayer(const Graph &edges);
void compactLayer(const Graph &edges, std::map<int, int> &layer);
void getCompactSpanTree(const GraphWithValue &edges, std::map<int, int> &layer,
std::vector<std::pair<int, int>> &tree);
void optimzieLayerViaCutValue(const GraphWithValue &edges,
std::map<int, int> &layer,
std::vector<std::pair<int, int>> &tree);

void splitLongEdge(const Graph &edges, std::map<int, int> &layer,
Graph &newEdges, std::map<int, int> &newLayer);
void getNaiveLayerLayout(const Graph &edges, std::map<int, int> &layer,
std::vector<std::vector<int>> &layout);
void optimizeLayerLayoutOrder(const Graph &edges,
std::vector<std::vector<int>> &layout);

Graph filterGraph(const Graph &edges, const std::vector<int> &verts);
Graph buildIndirectGraph(const Graph &edges);
GraphWithValue buildIndirectTreeGraph(std::vector<std::pair<int, int>> &tree);
GraphWithValue buildIndirectGraphWithOrder(const Graph &edges);
std::vector<std::vector<int>> splitGraph(const Graph &edges);
92 changes: 92 additions & 0 deletions render/layering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,96 @@ void compactLayer(const Graph &edges, std::map<int, int> &layer) {
}
}
}

int minLayer = layer[0];
for (auto [v, l]: layer) {
if (l < minLayer) {
minLayer = l;
}
}
for (auto& [v, l]: layer) {
l -= minLayer;
}
}

void travelCompactTree(int cur, std::vector<int> &vis,
const GraphWithValue &edges, std::map<int, int> &layer,
std::vector<std::pair<int, int>> &tree) {
vis[cur] = 1;
for (auto [to, dir] : edges[cur]) {
if (!vis[to] && std::abs(layer[cur] - layer[to]) == 1) {
if (dir == 1) {
tree.emplace_back(cur, to);
} else {
tree.emplace_back(to, cur);
}
travelCompactTree(to, vis, edges, layer, tree);
}
}
}

void getCompactSpanTree(const GraphWithValue &edges, std::map<int, int> &layer,
std::vector<std::pair<int, int>> &tree) {
int n = edges.size();
int startVert = 0;
std::vector<int> vis(n, 0);
travelCompactTree(startVert, vis, edges, layer, tree);
if (tree.size() != n - 1) {
puts("Bad span tree created!");
}
}

void findHeadVerts(int cur, const GraphWithValue &tree, int tail,
std::vector<int> &verts) {
verts[cur] = 1;
for (auto [to, dir] : tree[cur]) {
if (to == tail) {
continue;
}
findHeadVerts(to, tree, tail, verts);
}
}

int caculateCutValue(const GraphWithValue &edges, const GraphWithValue &tree,
int head, int tail) {
int n = edges.size();
std::vector<int> isHeadVerts(n, 0);
findHeadVerts(head, tree, tail, isHeadVerts);
int sum = 0;
for (int i = 0; i < n; i++) {
for (auto [j, dir] : edges[i]) {
if (isHeadVerts[i] != isHeadVerts[j]) {
sum += dir;
}
}
}
return sum;
}

std::vector<std::pair<int, std::pair<int, int>>>
caculateAllCutValue(const GraphWithValue &edges,
std::vector<std::pair<int, int>> &tree) {
auto tg = buildIndirectTreeGraph(tree);
std::vector<std::pair<int, std::pair<int, int>>> result;
for (auto [f, t] : tree) {
int cv = caculateCutValue(edges, tg, f, t);
result.emplace_back(cv, std::make_pair(f, t));
}
return result;
}

void optimzieLayerViaCutValue(const GraphWithValue &edges,
std::map<int, int> &layer,
std::vector<std::pair<int, int>> &tree) {
auto cvs = caculateAllCutValue(edges, tree);
std::sort(cvs.begin(), cvs.end());
cvs.erase(std::remove_if(cvs.begin(), cvs.end(),
[](const std::pair<int, std::pair<int, int>> &pr) {
return pr.first >= 0;
}),
cvs.end());
for (auto [cv, e] : cvs) {
printf("edge %d -> %d cut value %d\n", e.first, e.second, cv);
}
// TODO: optimize it
}
Loading

0 comments on commit a73e08f

Please sign in to comment.