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
Color.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_GRAPH_COLOR_H
11#define PBAT_GRAPH_COLOR_H
12
13#include "Enums.h"
14#include "pbat/Aliases.h"
15#include "pbat/common/ArgSort.h"
16#include "pbat/common/Stack.h"
18
19#include <algorithm>
20#include <concepts>
21#include <ranges>
22
23namespace pbat {
24namespace graph {
25
40template <
41 class TDerivedPtr,
42 class TDerivedAdj,
43 int NC = 128,
44 std::integral TIndex = typename TDerivedPtr::Scalar>
46 Eigen::DenseBase<TDerivedPtr> const& ptr,
47 Eigen::DenseBase<TDerivedAdj> const& adj,
50 -> Eigen::Vector<TIndex, Eigen::Dynamic>
51{
52 PBAT_PROFILE_NAMED_SCOPE("pbat.graph.GreedyColor");
53
56 TIndex const n = ptr.size() - 1;
57 using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
58 using Colors = IndexVectorType;
59 Colors C(n);
60 C.setConstant(TIndex(-1));
61 // Compute vertex visiting order
62 IndexVectorType ordering(n);
63 std::ranges::copy(std::views::iota(TIndex(0), n), ordering.data());
64 switch (eOrderingStrategy)
65 {
68 ordering = common::ArgSort(n, [&](auto i, auto j) {
69 auto di = ptr(i + 1) - ptr(i);
70 auto dj = ptr(j + 1) - ptr(j);
71 return di < dj;
72 });
73 break;
75 ordering = common::ArgSort(n, [&](auto i, auto j) {
76 auto di = ptr(i + 1) - ptr(i);
77 auto dj = ptr(j + 1) - ptr(j);
78 return di > dj;
79 });
80 break;
81 default: break;
82 }
83 // Color vertices in user-defined order
84 for (TIndex u : ordering)
85 {
86 // Reset usable color flags
87 std::fill(usable.begin(), usable.end(), true);
88 // Determine all unusable colors
89 auto vBegin = ptr(u);
90 auto vEnd = ptr(u + TIndex(1));
91 auto nUnusable{0};
92 for (auto k = vBegin; k < vEnd; ++k)
93 {
94 auto v = adj(k);
95 auto cv = C(v);
96 if (cv >= 0 and usable[cv])
97 {
98 usable[cv] = false;
99 ++nUnusable;
100 }
101 }
102 // Set vertex color
103 bool bPaletteInsufficient = nUnusable == palette.Size();
104 if (bPaletteInsufficient)
105 {
106 // Add color to palette
107 C(u) = palette.Size();
108 palette.Push(TIndex(1));
109 usable.Push({});
110 }
111 else
112 {
113 // Find, in palette, the usable color with the smallest size, ignoring unusable colors.
114 auto colors = std::views::iota(0, palette.Size());
115 auto c = *std::ranges::min_element(colors, [&](auto ci, auto cj) {
116 bool bLess{true};
117 switch (eSelectionStrategy)
118 {
120 bLess =
121 (usable[ci] and usable[cj]) ? palette[ci] < palette[cj] : usable[ci];
122 break;
124 bLess = (usable[ci] and usable[cj]) ? ci < cj : usable[ci];
125 break;
126 default: break;
127 }
128 return bLess;
129 });
130 C(u) = c;
131 ++palette[c];
132 }
133 }
134 return C;
135}
136
137} // namespace graph
138} // namespace pbat
139
140#endif // PBAT_GRAPH_COLOR_H
Non-intrusive sorting.
Fixed-size stack implementation.
Definition Stack.h:26
Fixed-size stack implementation usable in both host and device code.
Enums for graph algorithms API.
auto ArgSort(TIndex n, FLess less) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Computes the indices that would sort an array.
Definition ArgSort.h:32
Definition Adjacency.h:24
auto GreedyColor(Eigen::DenseBase< TDerivedPtr > const &ptr, Eigen::DenseBase< TDerivedAdj > const &adj, EGreedyColorOrderingStrategy eOrderingStrategy=EGreedyColorOrderingStrategy::LargestDegree, EGreedyColorSelectionStrategy eSelectionStrategy=EGreedyColorSelectionStrategy::LeastUsed) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Greedy graph coloring algorithm.
Definition Color.h:45
EGreedyColorSelectionStrategy
Enumeration of color selection strategies for graph coloring algorithms.
Definition Enums.h:19
@ FirstAvailable
Select the first available color from the color palette.
Definition Enums.h:21
@ LeastUsed
Select the least used color from the color palette.
Definition Enums.h:20
EGreedyColorOrderingStrategy
Enumeration of vertex traversal ordering strategies for graph coloring algorithms.
Definition Enums.h:27
@ Natural
Natural ordering of the vertices (i.e. [0,n-1])
Definition Enums.h:28
@ LargestDegree
Always visit the vertex with the largest degree next.
Definition Enums.h:30
@ SmallestDegree
Always visit the vertex with the smallest degree next.
Definition Enums.h:29
The main namespace of the library.
Definition Aliases.h:15
Profiling utilities for the Physics-Based Animation Toolkit (PBAT)