Skip to content

Commit

Permalink
Get layout to work again
Browse files Browse the repository at this point in the history
  • Loading branch information
grtlr committed Nov 27, 2024
1 parent 45a2dc7 commit a800af0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
47 changes: 44 additions & 3 deletions crates/viewer/re_space_view_graph/src/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
mod hash;
use std::hash::{Hash as _, Hasher as _};

use egui::Pos2;
pub(crate) use hash::GraphNodeHash;
mod index;
pub(crate) use index::NodeIndex;

use re_chunk::EntityPath;
use re_log_types::Instance;
use re_types::{blueprint::components, components::GraphType, ArrowString};
use re_types::{components::GraphType, ArrowString};

use crate::{
ui::draw::DrawableLabel,
visualizers::{EdgeData, EdgeInstance, NodeData, NodeInstance},
visualizers::{EdgeData, NodeData},
};

pub enum Node {
Expand Down Expand Up @@ -50,6 +53,7 @@ pub struct Edge {
}

pub struct Graph {
entity: EntityPath,
nodes: Vec<Node>,
edges: Vec<Edge>,
kind: GraphType,
Expand All @@ -58,6 +62,7 @@ pub struct Graph {
impl Graph {
pub fn new<'a>(
ui: &egui::Ui,
entity: EntityPath,
node_data: Option<&'a NodeData>,
edge_data: Option<&'a EdgeData>,
) -> Self {
Expand Down Expand Up @@ -111,7 +116,12 @@ impl Graph {
(Vec::new(), GraphType::default())
};

Self { nodes, edges, kind }
Self {
entity,
nodes,
edges,
kind,
}
}

pub fn nodes(&self) -> &[Node] {
Expand All @@ -125,4 +135,35 @@ impl Graph {
pub fn kind(&self) -> GraphType {
self.kind
}

pub fn size_hash(&self) -> u64 {
let mut hasher = ahash::AHasher::default();
for node in &self.nodes {
match node {
Node::Explicit {
id,
position,
label,
// The following fields can be ignored:
instance: _instance,
node: _node,
} => {
id.hash(&mut hasher);

position.as_ref().map(bytemuck::bytes_of).hash(&mut hasher);
bytemuck::bytes_of(&label.size()).hash(&mut hasher);
}
Node::Implicit {
id,
label,
// The following fields can be ignored:
node: _node,
} => {
id.hash(&mut hasher);
bytemuck::bytes_of(&label.size()).hash(&mut hasher);
}
}
}
hasher.finish()
}
}
13 changes: 1 addition & 12 deletions crates/viewer/re_space_view_graph/src/ui/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use egui::emath::TSTransform;
use egui::Rect;
use re_format::format_f32;
use re_types::blueprint::components::VisualBounds2D;
Expand Down Expand Up @@ -61,17 +60,7 @@ impl SpaceViewState for GraphSpaceViewState {
}
}

/// Used to determine if a layout is up-to-date or outdated. For now we use a
/// hash of the data the comes from the visualizers.
#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct Discriminator(u64);

impl Discriminator {
pub fn new(hash: u64) -> Self {
Self(hash)
}
}
pub type Discriminator = u64;

/// The following is a simple state machine that keeps track of the different
/// layouts and if they need to be recomputed. It also holds the state of the
Expand Down
43 changes: 27 additions & 16 deletions crates/viewer/re_space_view_graph/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Display a graph of nodes and edges.
let edge_data = &system_output.view_systems.get::<EdgesVisualizer>()?.data;

let graphs = merge(node_data, edge_data)
.map(|(ent, nodes, edges)| (ent, Graph::new(ui, nodes, edges)))
.map(|(ent, nodes, edges)| Graph::new(ui, ent.clone(), nodes, edges))
.collect::<Vec<_>>();

let state = state.downcast_mut::<GraphSpaceViewState>()?;
Expand All @@ -163,10 +163,17 @@ Display a graph of nodes and edges.

let view_rect = ui.max_rect();

// let text = "hello world";
// The descriminator is used to determine if the layout needs to be recomputed.
let discriminator = {
let mut hasher = ahash::AHasher::default();
for graph in &graphs {
graph.size_hash().hash(&mut hasher);
}
hasher.finish()
};

// let node = DrawableLabel::text(ui, text, None, Default::default());
// let circle_node = DrawableLabel::circle(ui, None, None);
let layout_was_empty = state.layout_state.is_none();
let layout = state.layout_state.get(discriminator, &graphs);

let (resp, new_bounds) = zoom_pan_area(
ui,
Expand All @@ -176,10 +183,18 @@ Display a graph of nodes and edges.
|ui, world_to_view| {
let mut world_bounding_rect = egui::Rect::NOTHING;

for (entity, graph) in graphs {
for graph in graphs {
for node in graph.nodes() {
let center = layout.get(&node.id()).unwrap_or(egui::Rect::ZERO).center();

// TODO(grtlr): Add proper highlights here:
let resp = draw_node(ui, Pos2::new(400., 400.), world_to_view, node.label(), Default::default());
let resp = draw_node(
ui,
center,
world_to_view,
node.label(),
Default::default(),
);
world_bounding_rect = world_bounding_rect.union(resp.rect);
}
}
Expand All @@ -201,16 +216,14 @@ Display a graph of nodes and edges.
// Update stored bounds on the state, so visualizers see an up-to-date value.
state.world_bounds = Some(bounds);

if state.layout_state.is_in_progress() {
ui.ctx().request_repaint();
}

Ok(())

//
// // We could move this computation to the visualizers to improve
// // performance if needed.
// let discriminator = {
// let mut hasher = ahash::AHasher::default();
// graphs.hash(&mut hasher);
// Discriminator::new(hasher.finish())
// };

//
// let state = state.downcast_mut::<GraphSpaceViewState>()?;
//
Expand Down Expand Up @@ -309,9 +322,7 @@ Display a graph of nodes and edges.
// ui.ctx().request_discard("layout needed a remeasure");
// }
//
// if state.layout_state.is_in_progress() {
// ui.ctx().request_repaint();
// }

//
// Ok(())
}
Expand Down

0 comments on commit a800af0

Please sign in to comment.