-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
edge.ts
161 lines (143 loc) · 4.45 KB
/
edge.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Sigma.js WebGL Abstract Edge Program
* =====================================
*
* @module
*/
import { Attributes } from "graphology-types";
import Sigma from "../sigma";
import { EdgeDisplayData, NodeDisplayData, RenderParams } from "../types";
import { indexToColor } from "../utils";
import { EdgeLabelDrawingFunction } from "./edge-labels";
import { AbstractProgram, Program } from "./program";
export abstract class AbstractEdgeProgram<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
> extends AbstractProgram<N, E, G> {
abstract drawLabel: EdgeLabelDrawingFunction<N, E, G> | undefined;
abstract process(
edgeIndex: number,
offset: number,
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
): void;
}
export abstract class EdgeProgram<
Uniform extends string = string,
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
>
extends Program<Uniform, N, E, G>
implements AbstractEdgeProgram<N, E, G>
{
drawLabel: EdgeLabelDrawingFunction<N, E, G> | undefined = undefined;
kill(): void {
super.kill();
}
process(
edgeIndex: number,
offset: number,
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
): void {
let i = offset * this.STRIDE;
// NOTE: dealing with hidden items automatically
if (data.hidden || sourceData.hidden || targetData.hidden) {
for (let l = i + this.STRIDE; i < l; i++) {
this.array[i] = 0;
}
return;
}
return this.processVisibleItem(indexToColor(edgeIndex), i, sourceData, targetData, data);
}
abstract processVisibleItem(
edgeIndex: number,
startIndex: number,
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
): void;
}
class EdgeImageClass<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
> implements AbstractEdgeProgram<N, E, G>
{
constructor(_gl: WebGLRenderingContext, _pickingBuffer: WebGLFramebuffer | null, _renderer: Sigma<N, E, G>) {
return this;
}
drawLabel: EdgeLabelDrawingFunction<N, E, G> | undefined = undefined;
kill(): void {
return undefined;
}
reallocate(_capacity: number): void {
return undefined;
}
process(
_edgeIndex: number,
_offset: number,
_sourceData: NodeDisplayData,
_targetData: NodeDisplayData,
_data: EdgeDisplayData,
): void {
return undefined;
}
render(_params: RenderParams): void {
return undefined;
}
}
export type EdgeProgramType<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
> = typeof EdgeImageClass<N, E, G>;
/**
* Helper function combining two or more programs into a single compound one.
* Note that this is more a quick & easy way to combine program than a really
* performant option. More performant programs can be written entirely.
*
* @param {array} programClasses - Program classes to combine.
* @param {function} drawLabel - An optional edge "draw label" function.
* @return {function}
*/
export function createEdgeCompoundProgram<
N extends Attributes = Attributes,
E extends Attributes = Attributes,
G extends Attributes = Attributes,
>(
programClasses: Array<EdgeProgramType<N, E, G>>,
drawLabel?: EdgeLabelDrawingFunction<N, E, G>,
): EdgeProgramType<N, E, G> {
return class EdgeCompoundProgram implements AbstractEdgeProgram<N, E, G> {
programs: Array<AbstractEdgeProgram<N, E, G>>;
constructor(gl: WebGLRenderingContext, pickingBuffer: WebGLFramebuffer | null, renderer: Sigma<N, E, G>) {
this.programs = programClasses.map((Program) => {
return new Program(gl, pickingBuffer, renderer);
});
}
drawLabel = drawLabel;
reallocate(capacity: number): void {
this.programs.forEach((program) => program.reallocate(capacity));
}
process(
edgeIndex: number,
offset: number,
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
): void {
this.programs.forEach((program) => program.process(edgeIndex, offset, sourceData, targetData, data));
}
render(params: RenderParams): void {
this.programs.forEach((program) => program.render(params));
}
kill(): void {
this.programs.forEach((program) => program.kill());
}
};
}