Merge pull request #702 from ArnoChenFx/graph-viewer-improvements

Improve graph viewer UI and rendering
This commit is contained in:
zrguo
2025-02-05 01:20:47 +08:00
committed by GitHub
5 changed files with 287 additions and 37 deletions

Binary file not shown.

View File

@@ -0,0 +1,92 @@
Copyright (c) 2023 Vercel, in collaboration with basement.studio
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION AND CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

View File

@@ -0,0 +1,93 @@
Copyright (c) 2022--2024, atelierAnchor <https://atelier-anchor.com>,
with Reserved Font Name <Smiley> and <得意黑>.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Binary file not shown.

View File

@@ -20,6 +20,9 @@ import os
CUSTOM_FONT = "font.ttf"
DEFAULT_FONT_ENG = "Geist-Regular.ttf"
DEFAULT_FONT_CHI = "SmileySans-Oblique.ttf"
class Node3D:
"""Class representing a 3D node in the graph"""
@@ -172,6 +175,10 @@ class GraphViewer:
np.sin(np.radians(self.pitch)),
)
)
if not imgui.is_window_hovered():
return
if io.mouse_wheel != 0:
self.move_speed += io.mouse_wheel * 0.05
self.move_speed = np.max([self.move_speed, 0.01])
@@ -502,6 +509,14 @@ class GraphViewer:
def load_file(self, filepath: str):
"""Load a GraphML file with error handling"""
try:
# Clear existing data
self.id_node_map.clear()
self.nodes.clear()
self.selected_node = None
self.highlighted_node = None
self.setup_buffers()
# Load new graph
self.graph = nx.read_graphml(filepath)
self.calculate_layout()
self.update_buffers()
@@ -672,10 +687,6 @@ class GraphViewer:
self.position, self.position + self.front, self.up
)
io = imgui.get_io()
self.window_width = int(io.display_size.x)
self.window_height = int(io.display_size.y)
aspect_ratio = self.window_width / self.window_height
self.proj_matrix = glm.perspective(
glm.radians(60.0), # FOV
@@ -839,7 +850,7 @@ class GraphViewer:
def render(self):
"""Render the graph"""
# Clear screen
self.glctx.clear(*self.background_color)
self.glctx.clear(*self.background_color, depth=1)
if not self.graph:
return
@@ -886,11 +897,15 @@ class GraphViewer:
# Render id map
self.render_id_map(mvp)
def render_labels(self):
# Render labels if enabled
if self.show_labels:
if self.show_labels and self.nodes:
# Save current font scale
original_scale = imgui.get_font_size()
self.update_view_proj_matrix()
mvp = self.proj_matrix * self.view_matrix
for node in self.nodes:
# Project node position to screen space
pos = mvp * glm.vec4(
@@ -1013,13 +1028,39 @@ def create_sphere(sectors: int = 32, rings: int = 16) -> Tuple:
return (vbo_vertices, vbo_elements)
def draw_text_with_bg(
text: str,
text_pos: imgui.ImVec2Like,
text_size: imgui.ImVec2Like,
bg_color: int,
):
imgui.get_window_draw_list().add_rect_filled(
(text_pos[0] - 5, text_pos[1] - 5),
(text_pos[0] + text_size[0] + 5, text_pos[1] + text_size[1] + 5),
bg_color,
3.0,
)
imgui.set_cursor_pos(text_pos)
imgui.text(text)
def main():
"""Main application entry point"""
viewer = GraphViewer()
show_fps = True
text_bg_color = imgui.IM_COL32(0, 0, 0, 100)
def gui():
if not viewer.initialized:
viewer.setup()
# # Change the theme
# tweaked_theme = hello_imgui.get_runner_params().imgui_window_params.tweaked_theme
# tweaked_theme.theme = hello_imgui.ImGuiTheme_.darcula_darker
# hello_imgui.apply_tweaked_theme(tweaked_theme)
viewer.window_width = int(imgui.get_window_width())
viewer.window_height = int(imgui.get_window_height())
# Handle keyboard and mouse input
viewer.handle_keyboard_input()
@@ -1089,11 +1130,34 @@ def main():
# Render graph settings window
viewer.render_settings()
window_bg_color.w = 0.0
# Render FPS
if show_fps:
imgui.set_window_font_scale(1)
fps_text = f"FPS: {hello_imgui.frame_rate():.1f}"
text_size = imgui.calc_text_size(fps_text)
cursor_pos = (10, viewer.window_height - text_size.y - 10)
draw_text_with_bg(fps_text, cursor_pos, text_size, text_bg_color)
# Render highlighted node ID
if viewer.highlighted_node:
imgui.set_window_font_scale(1)
node_text = f"Node ID: {viewer.highlighted_node.label}"
text_size = imgui.calc_text_size(node_text)
cursor_pos = (
viewer.window_width - text_size.x - 10,
viewer.window_height - text_size.y - 10,
)
draw_text_with_bg(node_text, cursor_pos, text_size, text_bg_color)
window_bg_color.w = 0
style.set_color_(imgui.Col_.window_bg.value, window_bg_color)
# Render the graph
viewer.render()
# Render labels
viewer.render_labels()
def custom_background():
if viewer.initialized:
viewer.render()
runner_params = hello_imgui.RunnerParams()
runner_params.app_window_params.window_geometry.size = (
@@ -1102,47 +1166,48 @@ def main():
)
runner_params.app_window_params.window_title = "3D GraphML Viewer"
runner_params.callbacks.show_gui = gui
addons = immapp.AddOnsParams()
addons.with_markdown = True
runner_params.callbacks.custom_background = custom_background
def load_font():
io = imgui.get_io()
io.fonts.add_font_default()
# Load font for Chinese character support
# You will need to provide it yourself, or use another font.
font_filename = CUSTOM_FONT
if not os.path.exists("assets/" + font_filename):
io = imgui.get_io()
io.fonts.tex_desired_width = 4096 # Larger texture for better CJK font quality
font_size_pixels = 14
asset_dir = os.path.join(os.path.dirname(__file__), "assets")
# Try to load custom font
if not os.path.isfile(font_filename):
font_filename = os.path.join(asset_dir, font_filename)
if os.path.isfile(font_filename):
custom_font = io.fonts.add_font_from_file_ttf(
filename=font_filename,
size_pixels=font_size_pixels,
glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(),
)
io.font_default = custom_font
return
# Get the full Chinese character range for ImGui
# This includes all Chinese characters supported by ImGui
cn_glyph_ranges_imgui = imgui.get_io().fonts.get_glyph_ranges_chinese_full()
# Set up font loading parameters with Chinese character support
font_loading_params = hello_imgui.FontLoadingParams()
font_loading_params.glyph_ranges = hello_imgui.translate_common_glyph_ranges(
cn_glyph_ranges_imgui
# Load default fonts
io.fonts.add_font_from_file_ttf(
filename=os.path.join(asset_dir, DEFAULT_FONT_ENG),
size_pixels=font_size_pixels,
)
custom_font = hello_imgui.load_font(font_filename, 16.0, font_loading_params)
# # Merge with default font
# font_config = imgui.ImFontConfig()
# font_config.merge_mode = True
# custom_font = io.fonts.add_font_from_file_ttf(
# filename= "assets/" + font_filename,
# size_pixels=16.0,
# font_cfg=font_config,
# glyph_ranges_as_int_list=cn_glyph_ranges_imgui,
# )
font_config = imgui.ImFontConfig()
font_config.merge_mode = True
io.fonts.tex_desired_width = 4096 # Larger texture for better CJK font quality
io.font_default = custom_font
io.font_default = io.fonts.add_font_from_file_ttf(
filename=os.path.join(asset_dir, DEFAULT_FONT_CHI),
size_pixels=font_size_pixels,
font_cfg=font_config,
glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(),
)
runner_params.callbacks.load_additional_fonts = load_font
immapp.run(runner_params, addons)
immapp.run(runner_params)
if __name__ == "__main__":