PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
ConnectedComponents.h
1#ifndef PBAT_GRAPH_CONNECTEDCOMPONENTS_H
2#define PBAT_GRAPH_CONNECTEDCOMPONENTS_H
3
4#include "pbat/Aliases.h"
6#include "pbat/graph/BreadthFirstSearch.h"
7#include "pbat/graph/DepthFirstSearch.h"
9
10#include <Eigen/Core>
11
12namespace pbat::graph {
13
26template <common::CIndex TIndex, class TDerivedP, class TDerivedAdj>
28 Eigen::DenseBase<TDerivedP> const& ptr,
29 Eigen::DenseBase<TDerivedAdj> const& adj,
30 Eigen::Ref<Eigen::Vector<TIndex, Eigen::Dynamic>> components,
32{
33 PBAT_PROFILE_NAMED_SCOPE("pbat.graph.ConnectedComponents");
34 Eigen::Index nVertices = ptr.size() - 1;
35 assert(dfs.NumVertices() == nVertices);
36 TIndex c{0};
37 for (TIndex u = 0; u < nVertices; ++u)
38 {
39 if (components[u] < TIndex(0))
40 {
41 components[u] = c;
42 dfs(ptr, adj, u, [&](TIndex v) { components[v] = c; });
43 ++c;
44 }
45 }
46 return c;
47}
48
61template <common::CIndex TIndex, class TDerivedP, class TDerivedAdj>
63 Eigen::DenseBase<TDerivedP> const& ptr,
64 Eigen::DenseBase<TDerivedAdj> const& adj,
65 Eigen::Ref<Eigen::Vector<TIndex, Eigen::Dynamic>> components,
67{
68 PBAT_PROFILE_NAMED_SCOPE("pbat.graph.ConnectedComponents");
69 Eigen::Index nVertices = ptr.size() - 1;
70 assert(bfs.NumVertices() == nVertices);
71 TIndex c{0};
72 for (TIndex u = 0; u < nVertices; ++u)
73 {
74 if (components[u] < TIndex(0))
75 {
76 components[u] = c;
77 bfs(ptr, adj, u, [&](TIndex v) { components[v] = c; });
78 ++c;
79 }
80 }
81 return c;
82}
83
84} // namespace pbat::graph
85
86#endif // PBAT_GRAPH_CONNECTEDCOMPONENTS_H
Concepts for common types.
Definition Adjacency.h:24
TIndex ConnectedComponents(Eigen::DenseBase< TDerivedP > const &ptr, Eigen::DenseBase< TDerivedAdj > const &adj, Eigen::Ref< Eigen::Vector< TIndex, Eigen::Dynamic > > components, DepthFirstSearch< TIndex > &dfs)
Compute connected components of a graph using depth-first search.
Definition ConnectedComponents.h:27
Profiling utilities for the Physics-Based Animation Toolkit (PBAT)
Definition BreadthFirstSearch.h:14
Eigen::Index NumVertices() const
Get the number of vertices in the graph.
Definition BreadthFirstSearch.h:50
Definition DepthFirstSearch.h:15
Eigen::Index NumVertices() const
Get the number of vertices in the graph.
Definition DepthFirstSearch.h:51