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
DepthFirstSearch.h
1#ifndef PBAT_GRAPH_DEPTH_FIRST_SEARCH_H
2#define PBAT_GRAPH_DEPTH_FIRST_SEARCH_H
3
4#include "pbat/Aliases.h"
7
8#include <stack>
9#include <vector>
10
11namespace pbat::graph {
12
13template <common::CIndex TIndex = Index>
14struct DepthFirstSearch
15{
16 using IndexType = TIndex;
17 using StackType = std::stack<IndexType, std::vector<IndexType>>;
18
19 DepthFirstSearch() = default;
24 DepthFirstSearch(Eigen::Index n);
29 void Reserve(Eigen::Index n);
41 template <class FVisit, class TDerivedP, class TDerivedAdj>
42 void operator()(
43 Eigen::DenseBase<TDerivedP> const& ptr,
44 Eigen::DenseBase<TDerivedAdj> const& adj,
45 TIndex start,
46 FVisit fVisit);
51 Eigen::Index NumVertices() const {return visited.size(); }
52
53 Eigen::Vector<bool, Eigen::Dynamic> visited;
55};
56
57template <common::CIndex TIndex>
58inline DepthFirstSearch<TIndex>::DepthFirstSearch(Eigen::Index n)
59{
60 Reserve(n);
61}
62
63template <common::CIndex TIndex>
64inline void DepthFirstSearch<TIndex>::Reserve(Eigen::Index n)
65{
66 PBAT_PROFILE_NAMED_SCOPE("pbat.graph.DepthFirstSearch.Reserve");
67 visited.resize(n);
68 std::vector<IndexType> memory{};
69 memory.reserve(static_cast<std::size_t>(n));
70 stack = StackType(std::move(memory));
71}
72
73template <common::CIndex TIndex>
74template <class FVisit, class TDerivedP, class TDerivedAdj>
76 Eigen::DenseBase<TDerivedP> const& ptr,
77 Eigen::DenseBase<TDerivedAdj> const& adj,
78 TIndex start,
79 FVisit fVisit)
80{
81 PBAT_PROFILE_NAMED_SCOPE("pbat.graph.DepthFirstSearch");
82 visited.setConstant(false);
83 stack.push(start);
84 while (not stack.empty())
85 {
86 IndexType u = stack.top();
87 stack.pop();
88 if (not visited[u])
89 {
90 fVisit(u);
91 visited[u] = true;
92 auto kBegin = ptr[u];
93 auto kEnd = ptr[u + 1];
94 for (auto k = kBegin; k < kEnd; ++k)
95 {
96 IndexType v = static_cast<IndexType>(adj[k]);
97 stack.push(v);
98 }
99 }
100 }
101}
102
103} // namespace pbat::graph
104
105#endif // PBAT_GRAPH_DEPTH_FIRST_SEARCH_H
Concepts for common types.
Definition Adjacency.h:24
Profiling utilities for the Physics-Based Animation Toolkit (PBAT)
std::stack< IndexType, std::vector< IndexType > > StackType
Stack type used for DFS.
Definition DepthFirstSearch.h:17
void Reserve(Eigen::Index n)
Reserve memory for n vertices.
Definition DepthFirstSearch.h:64
TIndex IndexType
Index type used in the depth-first search.
Definition DepthFirstSearch.h:16
Eigen::Index NumVertices() const
Get the number of vertices in the graph.
Definition DepthFirstSearch.h:51
Eigen::Vector< bool, Eigen::Dynamic > visited
|# vertices| x 1 visited mask
Definition DepthFirstSearch.h:53
void operator()(Eigen::DenseBase< TDerivedP > const &ptr, Eigen::DenseBase< TDerivedAdj > const &adj, TIndex start, FVisit fVisit)
Perform depth-first search on the graph.
Definition DepthFirstSearch.h:75
StackType stack
DFS search stack.
Definition DepthFirstSearch.h:54