Skip to content

Commit

Permalink
Merge pull request #13 from Florian-Barthel/dev
Browse files Browse the repository at this point in the history
Use imgui_bundle instead of imgui2.0
  • Loading branch information
Florian-Barthel authored Jul 12, 2024
2 parents f449d7a + 1789ac2 commit f7e3643
Show file tree
Hide file tree
Showing 15 changed files with 1,255 additions and 205 deletions.
1 change: 0 additions & 1 deletion gaussian-splatting/scene/gaussian_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def log_transform(x):
return torch.sign(x) * torch.log1p(torch.abs(x))

def inverse_log_transform(y):
assert y.max() < 20, "Probably mixed up linear and log values for xyz. These going in here are supposed to be quite small (log scale)"
return torch.sign(y) * (torch.expm1(torch.abs(y)))


Expand Down
863 changes: 863 additions & 0 deletions gui_utils/constants.py

Large diffs are not rendered by default.

192 changes: 127 additions & 65 deletions gui_utils/imgui_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,117 @@
# its affiliates is strictly prohibited.

import contextlib
import imgui
from imgui_bundle import imgui
from gui_utils.constants import *

#----------------------------------------------------------------------------

def set_default_style(color_scheme='dark', spacing=5, indent=10, scrollbar=10):
# ----------------------------------------------------------------------------

def set_default_style(color_scheme='dark', spacing=5, indent=20, scrollbar=10):
s = imgui.get_style()
s.window_padding = [spacing, spacing]
s.item_spacing = [spacing, spacing]
s.item_inner_spacing = [spacing, spacing]
s.columns_min_spacing = spacing
s.indent_spacing = indent
s.scrollbar_size = scrollbar
s.frame_padding = [3, 3]
s.window_border_size = 1
s.child_border_size = 1
s.popup_border_size = 1
s.frame_border_size = 1
s.window_rounding = 0
s.child_rounding = 0
s.popup_rounding = 1
s.frame_rounding = 0
s.scrollbar_rounding = 1
s.grab_rounding = 1
s.window_padding = [spacing, spacing]
s.item_spacing = [spacing, spacing]
s.item_inner_spacing = [spacing, spacing]
s.columns_min_spacing = spacing
s.indent_spacing = indent
s.scrollbar_size = scrollbar
s.frame_padding = [3, 3]
# s.window_border_size = 1
# s.child_border_size = 1
# s.popup_border_size = 1
# s.frame_border_size = 1
# s.window_rounding = 0
# s.child_rounding = 0
# s.popup_rounding = 1
s.frame_rounding = 5
# s.scrollbar_rounding = 1
# s.grab_rounding = 1

getattr(imgui, f'style_colors_{color_scheme}')(s)
c0 = s.colors[imgui.COLOR_MENUBAR_BACKGROUND]
c1 = s.colors[imgui.COLOR_FRAME_BACKGROUND]
s.colors[imgui.COLOR_POPUP_BACKGROUND] = [x * 0.7 + y * 0.3 for x, y in zip(c0, c1)][:3] + [1]

#----------------------------------------------------------------------------
# c0 = colors[COLOR_MENUBAR_BACKGROUND]
# c1 = colors[COLOR_FRAME_BACKGROUND]
# s.set_color_(COLOR_POPUP_BACKGROUND] = [x * 0.7 + y * 0.3 for x, y in zip(c0, c1)][:3] + [1]

s.set_color_(COLOR_TEXT, imgui.ImVec4(1.00, 1.00, 1.00, 1.00))
s.set_color_(COLOR_TEXT_DISABLED, imgui.ImVec4(0.50, 0.50, 0.50, 1.00))
s.set_color_(COLOR_WINDOW_BACKGROUND, imgui.ImVec4(0.30, 0.30, 0.30, 1.00))
s.set_color_(COLOR_CHILD_BACKGROUND, imgui.ImVec4(0.20, 0.20, 0.20, 1.00))
s.set_color_(COLOR_POPUP_BACKGROUND, imgui.ImVec4(0.19, 0.19, 0.19, 0.92))
s.set_color_(COLOR_BORDER, imgui.ImVec4(0.19, 0.19, 0.19, 0.29))
s.set_color_(COLOR_BORDER_SHADOW, imgui.ImVec4(0.00, 0.00, 0.00, 0.24))
s.set_color_(COLOR_FRAME_BACKGROUND, imgui.ImVec4(0.05, 0.05, 0.05, 0.54))
s.set_color_(COLOR_FRAME_BACKGROUND_HOVERED, imgui.ImVec4(0.19, 0.19, 0.19, 0.54))
s.set_color_(COLOR_FRAME_BACKGROUND_ACTIVE, imgui.ImVec4(0.20, 0.22, 0.23, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_ACTIVE, imgui.ImVec4(0.06, 0.06, 0.06, 1.00))
s.set_color_(COLOR_TITLE_BACKGROUND_COLLAPSED, imgui.ImVec4(0.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_MENUBAR_BACKGROUND, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_SCROLLBAR_BACKGROUND, imgui.ImVec4(0.05, 0.05, 0.05, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB, imgui.ImVec4(0.34, 0.34, 0.34, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_HOVERED, imgui.ImVec4(0.40, 0.40, 0.40, 0.54))
s.set_color_(COLOR_SCROLLBAR_GRAB_ACTIVE, imgui.ImVec4(0.56, 0.56, 0.56, 0.54))
s.set_color_(COLOR_CHECK_MARK, imgui.ImVec4(0.33, 0.67, 0.86, 1.00))
s.set_color_(COLOR_SLIDER_GRAB, imgui.ImVec4(0.34, 0.34, 0.34, 0.74))
s.set_color_(COLOR_SLIDER_GRAB_ACTIVE, imgui.ImVec4(0.56, 0.56, 0.56, 0.74))
s.set_color_(COLOR_BUTTON, imgui.ImVec4(0.90, 0.70, 0.00, 0.75))
s.set_color_(COLOR_BUTTON_HOVERED, imgui.ImVec4(0.90, 0.70, 0.00, 0.90))
s.set_color_(COLOR_BUTTON_ACTIVE, imgui.ImVec4(0.90, 0.70, 0.00, 1.00))
s.set_color_(COLOR_HEADER, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_HEADER_HOVERED, imgui.ImVec4(0.00, 0.00, 0.00, 0.36))
s.set_color_(COLOR_HEADER_ACTIVE, imgui.ImVec4(0.20, 0.22, 0.23, 0.33))
s.set_color_(COLOR_SEPARATOR, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_SEPARATOR_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_SEPARATOR_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_RESIZE_GRIP, imgui.ImVec4(0.28, 0.28, 0.28, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_HOVERED, imgui.ImVec4(0.44, 0.44, 0.44, 0.29))
s.set_color_(COLOR_RESIZE_GRIP_ACTIVE, imgui.ImVec4(0.40, 0.44, 0.47, 1.00))
s.set_color_(COLOR_TAB, imgui.ImVec4(1.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_HOVERED, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_TAB_ACTIVE, imgui.ImVec4(0.20, 0.20, 0.20, 0.36))
s.set_color_(COLOR_TAB_UNFOCUSED, imgui.ImVec4(0.00, 0.00, 0.00, 0.52))
s.set_color_(COLOR_TAB_UNFOCUSED_ACTIVE, imgui.ImVec4(0.14, 0.14, 0.14, 1.00))
s.set_color_(COLOR_PLOT_LINES, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_LINES_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_PLOT_HISTOGRAM, imgui.ImVec4(1.00, 0.80, 0.00, 0.90))
s.set_color_(COLOR_PLOT_HISTOGRAM_HOVERED, imgui.ImVec4(1.00, 0.80, 0.00, 1.00))
s.set_color_(COLOR_TEXT_SELECTED_BACKGROUND, imgui.ImVec4(0.20, 0.22, 0.23, 1.00))
s.set_color_(COLOR_NAV_HIGHLIGHT, imgui.ImVec4(1.00, 0.00, 0.00, 1.00))
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(1.00, 0.00, 0.00, 0.70))
s.set_color_(COLOR_NAV_WINDOWING_HIGHLIGHT, imgui.ImVec4(1.00, 0.00, 0.00, 0.20))
s.set_color_(COLOR_NAV_WINDOWING_DIM_BACKGROUND, imgui.ImVec4(1.00, 0.00, 0.00, 0.35))


# ----------------------------------------------------------------------------

@contextlib.contextmanager
def grayed_out(cond=True):
if cond:
s = imgui.get_style()
text = s.colors[imgui.COLOR_TEXT_DISABLED]
grab = s.colors[imgui.COLOR_SCROLLBAR_GRAB]
back = s.colors[imgui.COLOR_MENUBAR_BACKGROUND]
imgui.push_style_color(imgui.COLOR_TEXT, *text)
imgui.push_style_color(imgui.COLOR_CHECK_MARK, *grab)
imgui.push_style_color(imgui.COLOR_SLIDER_GRAB, *grab)
imgui.push_style_color(imgui.COLOR_SLIDER_GRAB_ACTIVE, *grab)
imgui.push_style_color(imgui.COLOR_FRAME_BACKGROUND, *back)
imgui.push_style_color(imgui.COLOR_FRAME_BACKGROUND_HOVERED, *back)
imgui.push_style_color(imgui.COLOR_FRAME_BACKGROUND_ACTIVE, *back)
imgui.push_style_color(imgui.COLOR_BUTTON, *back)
imgui.push_style_color(imgui.COLOR_BUTTON_HOVERED, *back)
imgui.push_style_color(imgui.COLOR_BUTTON_ACTIVE, *back)
imgui.push_style_color(imgui.COLOR_HEADER, *back)
imgui.push_style_color(imgui.COLOR_HEADER_HOVERED, *back)
imgui.push_style_color(imgui.COLOR_HEADER_ACTIVE, *back)
imgui.push_style_color(imgui.COLOR_POPUP_BACKGROUND, *back)
text = s.color_(COLOR_TEXT_DISABLED)
grab = s.color_(COLOR_SCROLLBAR_GRAB)
back = s.color_(COLOR_MENUBAR_BACKGROUND)
imgui.push_style_color(COLOR_TEXT, *text)
imgui.push_style_color(COLOR_CHECK_MARK, *grab)
imgui.push_style_color(COLOR_SLIDER_GRAB, *grab)
imgui.push_style_color(COLOR_SLIDER_GRAB_ACTIVE, *grab)
imgui.push_style_color(COLOR_FRAME_BACKGROUND, *back)
imgui.push_style_color(COLOR_FRAME_BACKGROUND_HOVERED, *back)
imgui.push_style_color(COLOR_FRAME_BACKGROUND_ACTIVE, *back)
imgui.push_style_color(COLOR_BUTTON, *back)
imgui.push_style_color(COLOR_BUTTON_HOVERED, *back)
imgui.push_style_color(COLOR_BUTTON_ACTIVE, *back)
imgui.push_style_color(COLOR_HEADER, *back)
imgui.push_style_color(COLOR_HEADER_HOVERED, *back)
imgui.push_style_color(COLOR_HEADER_ACTIVE, *back)
imgui.push_style_color(COLOR_POPUP_BACKGROUND, *back)
yield
imgui.pop_style_color(14)
else:
yield

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

@contextlib.contextmanager
def item_width(width=None):
Expand All @@ -77,95 +130,104 @@ def item_width(width=None):
else:
yield

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def scoped_by_object_id(method):
def decorator(self, *args, **kwargs):
imgui.push_id(str(id(self)))
res = method(self, *args, **kwargs)
imgui.pop_id()
return res

return decorator

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def button(label, width=0, enabled=True):
with grayed_out(not enabled):
clicked = imgui.button(label, width=width)
clicked = imgui.button(label)
clicked = clicked and enabled
return clicked

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def collapsing_header(text, visible=None, flags=0, default=False, enabled=True, show=True):
expanded = False
if show:
if default:
flags |= imgui.TREE_NODE_DEFAULT_OPEN
flags |= TREE_NODE_DEFAULT_OPEN
if not enabled:
flags |= imgui.TREE_NODE_LEAF
flags |= TREE_NODE_LEAF
with grayed_out(not enabled):
expanded, visible = imgui.collapsing_header(text, visible=visible, flags=flags)
expanded = imgui.collapsing_header(text, flags=flags)
expanded = expanded and enabled
return expanded, visible

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def popup_button(label, width=0, enabled=True):
if button(label, width, enabled):
imgui.open_popup(label)
opened = imgui.begin_popup(label)
return opened

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def input_text(label, value, buffer_length, flags, width=None, help_text=''):
old_value = value
color = list(imgui.get_style().colors[imgui.COLOR_TEXT])
color = list(imgui.get_style().colors[COLOR_TEXT])
if value == '':
color[-1] *= 0.5
with item_width(width):
imgui.push_style_color(imgui.COLOR_TEXT, *color)
# imgui.push_style_color(imgui.COLOR_TEXT, *color)
value = value if value != '' else help_text
changed, value = imgui.input_text(label, value, buffer_length, flags)
value = value if value != help_text else ''
imgui.pop_style_color(1)
if not flags & imgui.INPUT_TEXT_ENTER_RETURNS_TRUE:
# imgui.pop_style_color(1)
if not flags & imgui.InputTextFlags_.enter_returns_true:
changed = (value != old_value)
return changed, value

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def drag_previous_control(enabled=True):
dragging = False
dx = 0
dy = 0
if imgui.begin_drag_drop_source(imgui.DRAG_DROP_SOURCE_NO_PREVIEW_TOOLTIP):
if imgui.begin_drag_drop_source(imgui.DragDropFlags_.source_no_preview_tooltip.value):
if enabled:
dragging = True
dx, dy = imgui.get_mouse_drag_delta()
imgui.reset_mouse_drag_delta()
imgui.end_drag_drop_source()
return dragging, dx, dy

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def drag_button(label, width=0, enabled=True):
clicked = button(label, width=width, enabled=enabled)
dragging, dx, dy = drag_previous_control(enabled=enabled)
return clicked, dragging, dx, dy

#----------------------------------------------------------------------------

# ----------------------------------------------------------------------------

def drag_hidden_window(label, x, y, width, height, enabled=True):
imgui.push_style_color(imgui.COLOR_WINDOW_BACKGROUND, 0, 0, 0, 0)
imgui.push_style_color(imgui.COLOR_BORDER, 0, 0, 0, 0)
imgui.set_next_window_position(x, y)
imgui.set_next_window_size(width, height)
imgui.begin(label, closable=False, flags=(imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE))
imgui.push_style_color(COLOR_WINDOW_BACKGROUND, 0)
imgui.push_style_color(COLOR_BORDER, 0)
imgui.set_next_window_pos(imgui.ImVec2(x, y))
imgui.set_next_window_size(imgui.ImVec2(width, height))
imgui.begin(label, p_open=False, flags=(WINDOW_NO_TITLE_BAR | WINDOW_NO_RESIZE | WINDOW_NO_MOVE))
dragging, dx, dy = drag_previous_control(enabled=enabled)
imgui.end()
imgui.pop_style_color(2)
return dragging, dx, dy

#----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
17 changes: 8 additions & 9 deletions gui_utils/imgui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
# its affiliates is strictly prohibited.

import os
import imgui
import imgui.integrations.glfw
# from imgui_bundle import implot
from imgui_bundle import imgui, implot
from imgui_bundle.python_backends.glfw_backend import GlfwRenderer

from . import glfw_window
from . import imgui_utils
Expand All @@ -30,7 +29,7 @@ def __init__(self, *, title="ImguiWindow", font=None, font_sizes=range(14, 24),

# Init fields.
self._imgui_context = None
# self._implot_context = None
self._implot_context = None
self._imgui_renderer = None
self._imgui_fonts = None
self._cur_font_size = max(font_sizes)
Expand All @@ -41,7 +40,7 @@ def __init__(self, *, title="ImguiWindow", font=None, font_sizes=range(14, 24),

# Init ImGui.
self._imgui_context = imgui.create_context()
# self._implot_context = implot.create_context()
self._implot_context = implot.create_context()
self._imgui_renderer = _GlfwRenderer(self._glfw_window)
self._attach_glfw_callbacks()
imgui.get_io().ini_saving_rate = 0 # Disable creating imgui.ini at runtime.
Expand All @@ -58,9 +57,9 @@ def close(self):
if self._imgui_context is not None:
# imgui.destroy_context(self._imgui_context) # Commented out to avoid creating imgui.ini at the end.
self._imgui_context = None
#if self._implot_context is not None:
# implot.destroy_context(self._implot_context) # Commented out to avoid creating imgui.ini at the end.
# self._implot_context = None
if self._implot_context is not None:
implot.destroy_context(self._implot_context) # Commented out to avoid creating imgui.ini at the end.
self._implot_context = None
super().close()

def _glfw_key_callback(self, *args):
Expand Down Expand Up @@ -104,7 +103,7 @@ def end_frame(self):
# Wrapper class for GlfwRenderer to fix a mouse wheel bug on Linux.


class _GlfwRenderer(imgui.integrations.glfw.GlfwRenderer):
class _GlfwRenderer(GlfwRenderer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mouse_wheel_multiplier = 1
Expand Down
1 change: 1 addition & 0 deletions presets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Default": "gaussian._xyz = gaussian._xyz\ngaussian._rotation = gaussian._rotation\ngaussian._scaling = gaussian._scaling\ngaussian._opacity = gaussian._opacity\ngaussian._features_dc = gaussian._features_dc\ngaussian._features_rest = gaussian._features_rest\nself.bg_color[:] = 0\n", "Point Cloud": "gaussian._xyz = gaussian._xyz\ngaussian._rotation = gaussian._rotation\ngaussian._scaling = gaussian._scaling*0 - 8\ngaussian._opacity = gaussian._opacity*0 + 10\ngaussian._features_dc = gaussian._features_dc\ngaussian._features_rest = gaussian._features_rest\nself.bg_color[:] = 1\n", "Only SH": "gaussian._xyz = gaussian._xyz\ngaussian._rotation = gaussian._rotation\ngaussian._scaling = gaussian._scaling\ngaussian._opacity = gaussian._opacity\ngaussian._features_dc = gaussian._features_dc * 0\ngaussian._features_rest = gaussian._features_rest\nself.bg_color[:] = 1\n", "Mask Sphere": "mask = torch.linalg.norm(gaussian._xyz, dim=-1) < slider.x\n\ngaussian._xyz = gaussian._xyz[mask]\ngaussian._rotation = gaussian._rotation[mask]\ngaussian._scaling = gaussian._scaling[mask]\ngaussian._opacity = gaussian._opacity[mask]\ngaussian._features_dc = gaussian._features_dc[mask]\ngaussian._features_rest = gaussian._features_rest[mask]\nself.bg_color[:] = 1\n", "Mask Small Gaussians": "mask = torch.linalg.norm(gaussian._scaling, dim=-1) < slider.x * 2\n\ngaussian._xyz = gaussian._xyz[mask]\ngaussian._rotation = gaussian._rotation[mask]\ngaussian._scaling = gaussian._scaling[mask]\ngaussian._opacity = gaussian._opacity[mask]\ngaussian._features_dc = gaussian._features_dc[mask]\ngaussian._features_rest = gaussian._features_rest[mask]\nself.bg_color[:] = 1\n"}
Loading

0 comments on commit f7e3643

Please sign in to comment.