Skip to content

Commit

Permalink
added pruning options for rendering and percentage pruning of edges f…
Browse files Browse the repository at this point in the history
…or processing
  • Loading branch information
julrog committed Jun 10, 2020
1 parent adf8ba2 commit 59085af
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 27 deletions.
12 changes: 6 additions & 6 deletions gui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def __init__(self):
self.grid_render_settings: RenderSettings = RenderSettings(self.render_frame, "Grid", self.change_setting,
["None", "Cube", "Point"], 0, row=0, column=0)
edge_shader_settings: Dict[str, any] = {"Size": 0.05, "Base Opacity": 0.0, "Base Density Opacity": 0.0,
"Density Exponent": 0.1}
"Density Exponent": 0.1, "Importance Threshold": 0.01}
self.edge_render_settings: RenderSettings = RenderSettings(self.render_frame, "Edge", self.change_setting,
["None", "Sphere", "Sphere_Transparent",
"Ellipsoid_Transparent", "Line", "Point"],
Expand Down Expand Up @@ -276,8 +276,8 @@ def __init__(self):
variable_type="float")
self.sampling_rate: SettingEntry = SettingEntry(self.setting_frame, "Sampling rate:", row=2, column=0,
variable_type="float")
self.importance_threshold: SettingEntry = SettingEntry(self.setting_frame, "Importance threshold:", row=3,
column=0, variable_type="float")
self.prune_percentage: SettingEntry = SettingEntry(self.setting_frame, "Prune percentage:", row=3,
column=0, variable_type="float")
self.node_bandwidth_reduction: SettingEntry = SettingEntry(self.setting_frame, "Node Bandwidth reduction:",
row=4,
column=0, variable_type="float")
Expand All @@ -286,7 +286,7 @@ def __init__(self):
column=0, variable_type="float")

def start(self, layer_data: List[int] = None, layer_distance: float = 1.0, node_size: float = 1.0,
sampling_rate: float = 10.0, importance_threshold: float = 0.0, node_bandwidth_reduction: float = 0.98,
sampling_rate: float = 10.0, prune_percentage: float = 0.0, node_bandwidth_reduction: float = 0.98,
edge_bandwidth_reduction: float = 0.9):
if layer_data is None:
default_layer_data = [4, 9, 4]
Expand All @@ -299,7 +299,7 @@ def start(self, layer_data: List[int] = None, layer_distance: float = 1.0, node_
self.layer_distance.set(layer_distance)
self.layer_width.set(node_size)
self.sampling_rate.set(sampling_rate)
self.importance_threshold.set(importance_threshold)
self.prune_percentage.set(prune_percentage)
self.node_bandwidth_reduction.set(node_bandwidth_reduction)
self.edge_bandwidth_reduction.set(edge_bandwidth_reduction)
self.generate()
Expand Down Expand Up @@ -364,7 +364,7 @@ def generate(self, importance_data: ImportanceDataHandler = None, processed_nn:
self.settings["layer_distance"] = self.layer_distance.get()
self.settings["layer_width"] = self.layer_width.get()
self.settings["sampling_rate"] = self.sampling_rate.get()
self.settings["importance_threshold"] = self.importance_threshold.get()
self.settings["prune_percentage"] = self.prune_percentage.get()
self.settings["node_bandwidth_reduction"] = self.node_bandwidth_reduction.get()
self.settings["edge_bandwidth_reduction"] = self.edge_bandwidth_reduction.get()
self.settings["update_model"] = True
Expand Down
34 changes: 30 additions & 4 deletions models/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
class NetworkModel:
def __init__(self, layer: List[int], layer_width: float, layer_distance: float,
importance_data: ImportanceDataHandler = None, processed_nn: ProcessedNNHandler = None,
importance_prune_threshold: float = 0.1):
prune_percentage: float = 0.1):
self.layer: List[int] = layer
self.layer_width: float = layer_width
self.layer_distance: float = layer_distance
self.importance_prune_threshold: float = importance_prune_threshold
self.prune_percentage: float = prune_percentage

self.bounding_volume: Tuple[Vector3, Vector3] = (
Vector3(
Expand Down Expand Up @@ -83,7 +83,20 @@ def set_nodes(self, node_data: List[Node]):

def generate_edges(self, edge_container_size: int = 1000) -> List[List[List[Edge]]]:
self.pruned_edges = 0
edge_importance_values: List[float] = []
if len(self.edge_data) == 0:
for i in range(len(self.layer) - 1):
for node_one_i, node_one in enumerate(self.layer_nodes[i]):
for node_two_i, node_two in enumerate(self.layer_nodes[i + 1]):
new_edge: Edge = Edge().random_importance_init(node_one, node_two, i, node_one_i * len(
self.layer_nodes[i + 1]) + node_two_i)
edge_importance_values.append(new_edge.data[3] * new_edge.data[6])

min_importance_value: float = -1.0
if self.prune_percentage > 0.0:
min_importance_value: float = np.sort(np.array(edge_importance_values))[
int(len(edge_importance_values) * self.prune_percentage)]

edges: List[List[Edge]] = []
for i in range(len(self.layer) - 1):
layer_edge: List[Edge] = []
Expand All @@ -92,14 +105,26 @@ def generate_edges(self, edge_container_size: int = 1000) -> List[List[List[Edge
new_edge: Edge = Edge().random_importance_init(node_one, node_two, i, node_one_i * len(
self.layer_nodes[i + 1]) + node_two_i)

if new_edge.data[3] * new_edge.data[4] > self.importance_prune_threshold:
if new_edge.data[3] * new_edge.data[6] > min_importance_value:
layer_edge.append(new_edge)
else:
self.pruned_edges += 1
edges.append(layer_edge)
return split_edges_for_buffer(edges, edge_container_size)
else:
if self.edge_importance_only:
for i in range(len(self.layer) - 1):
for node_one_i, node_one in enumerate(self.layer_nodes[i]):
for node_two_i, node_two in enumerate(self.layer_nodes[i + 1]):
new_edge: Edge = Edge().importance_init(node_one, node_two, i, node_one_i * len(
self.layer_nodes[i + 1]) + node_two_i, self.edge_data[i][node_one_i][node_two_i])
edge_importance_values.append(new_edge.data[3] * new_edge.data[6])

min_importance_value: float = -1.0
if self.prune_percentage > 0.0:
min_importance_value: float = np.sort(np.array(edge_importance_values))[
int(len(edge_importance_values) * self.prune_percentage)]

edges: List[List[Edge]] = []
for i in range(len(self.layer) - 1):
layer_edge: List[Edge] = []
Expand All @@ -108,10 +133,11 @@ def generate_edges(self, edge_container_size: int = 1000) -> List[List[List[Edge
new_edge: Edge = Edge().importance_init(node_one, node_two, i, node_one_i * len(
self.layer_nodes[i + 1]) + node_two_i, self.edge_data[i][node_one_i][node_two_i])

if new_edge.data[3] * new_edge.data[6] > self.importance_prune_threshold:
if new_edge.data[3] * new_edge.data[6] > min_importance_value:
layer_edge.append(new_edge)
else:
self.pruned_edges += 1
edges.append(layer_edge)
return split_edges_for_buffer(edges, edge_container_size)
else:
edges: List[List[List[Edge]]] = []
Expand Down
4 changes: 2 additions & 2 deletions processing/network_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, layer_nodes: List[int],
importance_data: ImportanceDataHandler = None,
processed_nn: ProcessedNNHandler = None,
layer_distance: float = 1.0, layer_width: float = 1.0, sampling_rate: float = 10.0,
importance_prune_threshold: float = 0.1, node_bandwidth_reduction: float = 0.98,
prune_percentage: float = 0.1, node_bandwidth_reduction: float = 0.98,
edge_bandwidth_reduction: float = 0.9):
print("[%s] Prepare network processing for network of size: %s" % (LOG_SOURCE, layer_nodes))
self.layer_nodes: List[int] = layer_nodes
Expand All @@ -33,7 +33,7 @@ def __init__(self, layer_nodes: List[int],

print("[%s] Create network model..." % LOG_SOURCE)
self.network: NetworkModel = NetworkModel(self.layer_nodes, self.layer_width, self.layer_distance,
importance_data, processed_nn, importance_prune_threshold)
importance_data, processed_nn, prune_percentage)
self.sample_length: float = self.network.layer_width / sampling_rate
self.grid_cell_size: float = self.sample_length / 3.0
self.sample_radius: float = self.sample_length * 2.0
Expand Down
21 changes: 15 additions & 6 deletions rendering/edge_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,23 @@ def __init__(self, edge_processor: EdgeProcessor, grid: Grid):
range(len(self.edge_processor.sample_buffer[i]))] for i in range(len(self.edge_processor.sample_buffer))]

self.point_render: RenderSetLayered = RenderSetLayered(sample_point_shader, self.data_handler)
self.point_render.set_uniform_label([("Importance Threshold", "importance_threshold")])
self.line_render: RenderSetLayered = RenderSetLayered(sample_line_shader, self.data_handler)
self.line_render.set_uniform_label([("Importance Threshold", "importance_threshold")])
self.sphere_render: RenderSetLayered = RenderSetLayered(sample_sphere_shader, self.data_handler)
self.sphere_render.set_uniform_label([("Size", "object_radius")])
self.sphere_render.set_uniform_label(
[("Size", "object_radius"), ("Importance Threshold", "importance_threshold")])
self.transparent_render: RenderSetLayered = RenderSetLayered(sample_transparent_shader, self.data_handler)
self.transparent_render.set_uniform_label(
[("Size", "object_radius"), ("Base Opacity", "base_opacity"),
("Base Density Opacity", "base_shpere_opacity"),
("Density Exponent", "opacity_exponent")])
("Density Exponent", "opacity_exponent"), ("Importance Threshold", "importance_threshold")])
self.ellipse_render: RenderSetLayered = RenderSetLayered(sample_ellipse_shader, self.data_handler)
self.ellipse_render.set_uniform_label(
[("Size", "object_radius"), ("Base Opacity", "base_opacity"),
("Base Density Opacity", "base_shpere_opacity"),
("Density Exponent", "opacity_exponent")])
("Density Exponent", "opacity_exponent"), ("Importance Threshold", "importance_threshold")])
self.importance_threshold: float = 0.0

@track_time
def render_point(self, window: Window, clear: bool = True, swap: bool = False, options: Dict[str, float] = None):
Expand All @@ -61,15 +65,16 @@ def render_point(self, window: Window, clear: bool = True, swap: bool = False, o
("view", window.cam.view, "mat4"),
("screen_width", 1920.0, "float"),
("screen_height", 1080.0, "float"),
('max_sample_points', self.edge_processor.max_sample_points, 'int')])
('max_sample_points', self.edge_processor.max_sample_points, 'int'),
("importance_threshold", self.importance_threshold, "float")])
self.point_render.set_uniform_labeled_data(options)

def render_function(sample_points: int):
render_setting_0(clear)
glPointSize(10.0)
glDrawArraysInstanced(GL_POINTS, 0, 1, sample_points)

self.line_render.render(render_function, self.edge_processor.get_buffer_points)
self.point_render.render(render_function, self.edge_processor.get_buffer_points)

if swap:
window.swap()
Expand All @@ -80,7 +85,8 @@ def render_line(self, window: Window, clear: bool = True, swap: bool = False, op

self.line_render.set_uniform_data([("projection", window.cam.projection, "mat4"),
("view", window.cam.view, "mat4"),
('max_sample_points', self.edge_processor.max_sample_points, 'int')])
('max_sample_points', self.edge_processor.max_sample_points, 'int'),
("importance_threshold", self.importance_threshold, "float")])
self.line_render.set_uniform_labeled_data(options)

def render_function(sample_points: int):
Expand All @@ -101,6 +107,7 @@ def render_sphere(self, window: Window, sphere_radius: float = 0.05, clear: bool
self.sphere_render.set_uniform_data([("projection", window.cam.projection, "mat4"),
("view", window.cam.view, "mat4"),
("object_radius", sphere_radius, "float"),
("importance_threshold", self.importance_threshold, "float"),
('max_sample_points', self.edge_processor.max_sample_points, 'int')])
self.sphere_render.set_uniform_labeled_data(options)

Expand All @@ -124,6 +131,7 @@ def render_transparent_sphere(self, window: Window, sphere_radius: float = 0.05,
("farthest_point_view_z", far, "float"),
("nearest_point_view_z", near, "float"),
("object_radius", sphere_radius, "float"),
("importance_threshold", self.importance_threshold, "float"),
('max_sample_points', self.edge_processor.max_sample_points, 'int')])
self.transparent_render.set_uniform_labeled_data(options)

Expand All @@ -147,6 +155,7 @@ def render_ellipsoid_transparent(self, window: Window, clear: bool = True, swap:
("farthest_point_view_z", far, "float"),
("nearest_point_view_z", near, "float"),
("object_radius", self.edge_processor.sample_length * 0.5, "float"),
("importance_threshold", self.importance_threshold, "float"),
('max_sample_points', self.edge_processor.max_sample_points, 'int')])
self.ellipse_render.set_uniform_labeled_data(options)

Expand Down
2 changes: 1 addition & 1 deletion shader_src/node/sample_impostor.vert
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const vec3 color_9 = vec3(0.392, 0.584, 0.929);

void main()
{
if (position.w == 0.0 || position.w > 1.0) {
if (position.w == 0.0 || position.w > 1.0 || node_data_2.w == 0.0) {
vs_discard = 1.0;
} else {
vs_discard = 0.0;
Expand Down
5 changes: 3 additions & 2 deletions shader_src/sample/sample.vert
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ flat out vec3 vs_color;
uniform mat4 projection;
uniform mat4 view;
uniform int max_sample_points;
uniform float importance_threshold = 0;

const vec3 color_0 = vec3(0.133, 0.545, 0.133);
const vec3 color_1 = vec3(0, 0, 0.545);
Expand All @@ -30,14 +31,14 @@ const vec3 color_9 = vec3(0.392, 0.584, 0.929);

void main()
{
if (position.w < 1.0 || next_position.w > 1.0) {
if (position.w == 0.0 || position.w == -1.0 || importance_threshold >= edge_data_0.w * edge_data_1.z) {
vs_discard = 1.0;
} else {
vs_discard = 0.0;
}
gl_Position = projection * view * vec4(position.xyz, 1.0);

float t = mod(0, max_sample_points)/edge_data_0.x;
float t = mod(gl_InstanceID + 1, max_sample_points)/edge_data_0.x;
float importance[10];
importance[0] = (1.0 - t) * (edge_data_2.x / edge_data_1.x) + t * (edge_data_4.z / edge_data_1.y);
importance[1] = (1.0 - t) * (edge_data_2.y / edge_data_1.x) + t * (edge_data_4.w / edge_data_1.y);
Expand Down
5 changes: 3 additions & 2 deletions shader_src/sample/sample_impostor.vert
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ out vec3 vs_color;

uniform mat4 view;
uniform int max_sample_points;
uniform float importance_threshold = 0;

const vec3 color_0 = vec3(0.133, 0.545, 0.133);
const vec3 color_1 = vec3(0, 0, 0.545);
Expand All @@ -33,7 +34,7 @@ const vec3 color_9 = vec3(0.392, 0.584, 0.929);

void main()
{
if (position.w < 1.0 || next_position.w > 1.0) {
if (position.w == 0.0 || position.w == -1.0 || importance_threshold >= edge_data_0.w * edge_data_1.z) {
vs_discard = 1.0;
} else {
vs_discard = 0.0;
Expand All @@ -43,7 +44,7 @@ void main()
vec4 new_normal = view * vec4((position.xyz + vec3(0.0, 1.0, 0.0)), 1.0);
vs_normal = normalize(vec3(new_normal.xyz - gl_Position.xyz));

float t = clamp(mod(gl_InstanceID, max_sample_points)/edge_data_0.x, 0.0, 1.0);
float t = clamp(mod(gl_InstanceID + 1, max_sample_points)/edge_data_0.x, 0.0, 1.0);
float importance[10] = float[10](
(1.0 - t) * (edge_data_2.x / (edge_data_1.z * 10.0)) + t * (edge_data_4.z / (edge_data_1.w * 10.0)),
(1.0 - t) * (edge_data_2.y / (edge_data_1.z * 10.0)) + t * (edge_data_4.w / (edge_data_1.w * 10.0)),
Expand Down
6 changes: 2 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def frame():
processed_nn=options.settings["processed_nn"],
layer_width=options.settings["layer_width"],
sampling_rate=options.settings["sampling_rate"],
importance_prune_threshold=options.settings[
"importance_threshold"],
prune_percentage=options.settings["prune_percentage"],
node_bandwidth_reduction=options.settings[
"node_bandwidth_reduction"],
edge_bandwidth_reduction=options.settings[
Expand All @@ -90,8 +89,7 @@ def frame():
layer_distance=options.settings["layer_distance"],
layer_width=options.settings["layer_width"],
sampling_rate=options.settings["sampling_rate"],
importance_prune_threshold=options.settings[
"importance_threshold"],
prune_percentage=options.settings["prune_percentage"],
node_bandwidth_reduction=options.settings[
"node_bandwidth_reduction"],
edge_bandwidth_reduction=options.settings[
Expand Down

0 comments on commit 59085af

Please sign in to comment.