Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-referenced nodes making simulation to blow up #10

Open
blitzarx1 opened this issue Apr 24, 2023 · 5 comments
Open

Self-referenced nodes making simulation to blow up #10

blitzarx1 opened this issue Apr 24, 2023 · 5 comments

Comments

@blitzarx1
Copy link

If I have self-referenced nodes in ForceGraph and update simulation all the coordinates are becoming NaN

@blitzarx1 blitzarx1 reopened this Apr 24, 2023
@blitzarx1
Copy link
Author

blitzarx1 commented Apr 24, 2023

I ll try to provide more details later

@blitzarx1 blitzarx1 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2023
@zicklag
Copy link

zicklag commented Apr 29, 2023

I ran into, this too. All I did was add a node that was connected by an edge to itself, and all the node positions would turn to NaN after updating the simulation a couple times.

@blitzarx1 blitzarx1 reopened this Apr 29, 2023
@blitzarx1
Copy link
Author

blitzarx1 commented Apr 29, 2023

Yeah thanks for update. This is how I hacked around it in my project interactive demo. Basicaly you need to remove all self referenced nodes before the simulation update, and after update add them again.

    fn update_simulation(&mut self) {
        if self.simulation_stopped {
            return;
        }

        // the following manipulations is a hack to avoid having looped edges in the simulation
        // because they cause the simulation to blow up; this is the issue of the fdg_sim engine
        // we use for the simulation
        // * remove loop edges
        // * update simulation
        // * restore loop edges

        let looped_nodes = {
            // remove looped edges
            let graph = self.simulation.get_graph_mut();
            let mut looped_nodes = vec![];
            let mut looped_edges = vec![];
            graph.edge_indices().for_each(|idx| {
                let edge = graph.edge_endpoints(idx).unwrap();
                let looped = edge.0 == edge.1;
                if looped {
                    looped_nodes.push((edge.0, ()));
                    looped_edges.push(idx);
                }
            });

            for idx in looped_edges {
                graph.remove_edge(idx);
            }

            self.simulation.update(SIMULATION_DT);

            looped_nodes
        };

        // restore looped edges
        let graph = self.simulation.get_graph_mut();
        for (idx, _) in looped_nodes.iter() {
            graph.add_edge(*idx, *idx, ());
        }
    }

I ll live this issue open. Maybe we will get some comments from the contributors

@grantshandy
Copy link
Owner

Thanks for taking an interest in this library! It's been a while since I've worked on it and in that time I've really improved my Rust API design skills (and calculus), so this library definitely isn't as polished as I'd like to be. Forces are due for a re-write or atleast some API and documentation changes.

@zicklag: ...All I did was add a node that was connected by an edge to itself, and all the node positions would turn to NaN after updating the simulation a couple times.

Both the attractive and repellent forces are highly dependent on the distance between the two nodes, so if two nodes are linked together my guess is the attractive edge forces will probably evaluate to Infinity/NaN because of a zero-division. Then that new velocity creates a chain reaction where all the connected nodes fly off into NaN territory because they have huge velocities. This is a bug, and I am going to try to address it in my new versions.

@blitzarx1
Copy link
Author

Hi, again!

I am using current master of fdg here (https://github.com/blitzarx1/egui_graphs/tree/master/examples/demo) and I do not think this is an issue anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants