Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { LineMaterial } from "three-fatline";
export interface GraphNode extends NodeData {
x: number;
y: number;
z: number;
vx: number;
vy: number;
vz: number;
fx: number;
fy: number;
fz: number;
color: string;
}
export interface GraphLink extends LinkData {
material?: LineMaterial;
}
export interface LinkData {
source: NodeData;
target: NodeData;
type?: string;
}
export interface NodeData {
id: string;
name: string;
description?: string;
type?: string;
video?: string;
neighbors: NodeData[];
links: LinkData[];
}
export interface Coordinate {
x: number;
y: number;
z: number;
}
export class Graph {
public nodes: NodeData[];
public links: LinkData[];
private idToNode: Map<string, NodeData>;
constructor(nodes: NodeData[], links: LinkData[]) {
this.nodes = nodes;
this.links = links;
this.idToNode = new Map<string, NodeData>();
nodes.forEach((node) => {
this.idToNode.set(node.id, node);
});
this.updateNodeData();
}
resetNodeData() {
for (const node of this.nodes) {
node.neighbors = [];
node.links = [];
}
}
/**
* Updates the graph data structure to contain additional values.
* Creates a 'neighbors' and 'links' array for each node object.
*/
updateNodeData() {
this.resetNodeData();
this.links.forEach((link) => {
const a = link.source;
const b = link.target;
a.neighbors.push(b);
b.neighbors.push(a);
a.links.push(link);
b.links.push(link);
});
}
node(id: string): NodeData {
return this.idToNode.get(id);
}
/**
* Returns an array containing the different edge types of the graph.
* @returns {*[]}
*/
getLinkClasses(): string[] {
const linkClasses: string[] = [];
this.links.forEach((link) => linkClasses.push(link.type));
return [...new Set(linkClasses)].map((c) => String(c));
}
getNodeClasses(): string[] {
const nodeClasses: string[] = [];
this.nodes.forEach((node) => nodeClasses.push(node.type));
return [...new Set(nodeClasses)].map((c) => String(c));
}
view(
nodeTypes: Map<string, boolean>,
linkTypes: Map<string, boolean>
): Graph {
return new Graph(
this.nodes.filter((l) => nodeTypes.get(l.type)),
this.links.filter((l) => linkTypes.get(l.type))
);
}
}