Hybrid A* Planner
 All Classes Namespaces Files Functions Variables Friends Pages
gradient.h
1 #ifndef GRADIENT
2 #define GRADIENT
3 
4 #include <vector>
5 
6 using namespace std;
7 
8 namespace HybridAStar {
9 
17  private:
18  struct ColorPoint { // Internal class used to store colors at different points in the gradient.
19  float r, g, b; // Red, green and blue values of our color.
20  float val; // Position of our color along the gradient (between 0 and 1).
21  ColorPoint(float red, float green, float blue, float value)
22  : r(red), g(green), b(blue), val(value) {}
23  };
24  vector<ColorPoint> color; // An array of color points in ascending value.
25 
26  public:
27  //-- Default constructor:
28  ColorGradient() { createDefaultHeatMapGradient(); }
29 
30  //-- Inserts a new color point into its correct position:
31  void addColorPoint(float red, float green, float blue, float value) {
32  for (unsigned int i = 0; i < color.size(); i++) {
33  if (value < color[i].val) {
34  color.insert(color.begin() + i, ColorPoint(red, green, blue, value));
35  return;
36  }
37  }
38 
39  color.push_back(ColorPoint(red, green, blue, value));
40  }
41 
42  //-- Inserts a new color point into its correct position:
43  void clearGradient() { color.clear(); }
44 
45  //-- Places a 5 color heapmap gradient into the "color" vector:
46  void createDefaultHeatMapGradient() {
47  color.clear();
48  color.push_back(ColorPoint(0, 0, 1, 0.0f)); // Blue.
49  color.push_back(ColorPoint(0, 1, 1, 0.25f)); // Cyan.
50  color.push_back(ColorPoint(0, 1, 0, 0.5f)); // Green.
51  color.push_back(ColorPoint(1, 1, 0, 0.75f)); // Yellow.
52  color.push_back(ColorPoint(1, 0, 0, 1.0f)); // Red.
53  }
54 
55  //-- Inputs a (value) between 0 and 1 and outputs the (red), (green) and (blue)
56  //-- values representing that position in the gradient.
57  void getColorAtValue(const float value, float& red, float& green, float& blue) {
58  if (color.size() == 0)
59  { return; }
60 
61  for (unsigned int i = 0; i < color.size(); i++) {
62  ColorPoint& currC = color[i];
63 
64  if (value < currC.val) {
65  ColorPoint& prevC = color[ max(0, (int)i - 1) ];
66  float valueDiff = (prevC.val - currC.val);
67  float fractBetween = (valueDiff == 0) ? 0 : (value - currC.val) / valueDiff;
68  red = (prevC.r - currC.r) * fractBetween + currC.r;
69  green = (prevC.g - currC.g) * fractBetween + currC.g;
70  blue = (prevC.b - currC.b) * fractBetween + currC.b;
71  return;
72  }
73  }
74 
75  red = color.back().r;
76  green = color.back().g;
77  blue = color.back().b;
78  return;
79  }
80 };
81 }
82 #endif // GRADIENT
83 
A color gradient class for visualizing the cost of nodes.
Definition: gradient.h:16