ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ColorMap.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Logging.h>
11 
12 namespace cloudViewer {
13 
14 namespace {
15 using namespace visualization;
16 
17 class GlobalColorMapSingleton {
18 private:
19  GlobalColorMapSingleton() : color_map_(new ColorMapJet) {
20  utility::LogDebug("Global colormap init.");
21  }
22  GlobalColorMapSingleton(const GlobalColorMapSingleton &) = delete;
23  GlobalColorMapSingleton &operator=(const GlobalColorMapSingleton &) =
24  delete;
25 
26 public:
27  ~GlobalColorMapSingleton() {
28  utility::LogDebug("Global colormap destruct.");
29  }
30 
31 public:
32  static GlobalColorMapSingleton &GetInstance() {
33  static GlobalColorMapSingleton singleton;
34  return singleton;
35  }
36 
37 public:
38  std::shared_ptr<const ColorMap> color_map_;
39 };
40 
41 } // unnamed namespace
42 
43 namespace visualization {
44 Eigen::Vector3d ColorMapGray::GetColor(double value) const {
45  return Eigen::Vector3d(value, value, value);
46 }
47 
48 Eigen::Vector3d ColorMapJet::GetColor(double value) const {
49  return Eigen::Vector3d(JetBase(value * 2.0 - 1.5), // red
50  JetBase(value * 2.0 - 1.0), // green
51  JetBase(value * 2.0 - 0.5)); // blue
52 }
53 
54 Eigen::Vector3d ColorMapSummer::GetColor(double value) const {
55  return Eigen::Vector3d(Interpolate(value, 0.0, 0.0, 1.0, 1.0),
56  Interpolate(value, 0.5, 0.0, 1.0, 1.0), 0.4);
57 }
58 
59 Eigen::Vector3d ColorMapWinter::GetColor(double value) const {
60  return Eigen::Vector3d(0.0, Interpolate(value, 0.0, 0.0, 1.0, 1.0),
61  Interpolate(value, 1.0, 0.0, 0.5, 1.0));
62 }
63 
64 Eigen::Vector3d ColorMapHot::GetColor(double value) const {
65  Eigen::Vector3d edges[4] = {
66  Eigen::Vector3d(1.0, 1.0, 1.0),
67  Eigen::Vector3d(1.0, 1.0, 0.0),
68  Eigen::Vector3d(1.0, 0.0, 0.0),
69  Eigen::Vector3d(0.0, 0.0, 0.0),
70  };
71  if (value < 0.0) {
72  return edges[0];
73  } else if (value < 1.0 / 3.0) {
74  return Interpolate(value, edges[0], 0.0, edges[1], 1.0 / 3.0);
75  } else if (value < 2.0 / 3.0) {
76  return Interpolate(value, edges[1], 1.0 / 3.0, edges[2], 2.0 / 3.0);
77  } else if (value < 1.0) {
78  return Interpolate(value, edges[2], 2.0 / 3.0, edges[3], 1.0);
79  } else {
80  return edges[3];
81  }
82 }
83 
84 const std::shared_ptr<const ColorMap> GetGlobalColorMap() {
85  return GlobalColorMapSingleton::GetInstance().color_map_;
86 }
87 
89  switch (option) {
91  GlobalColorMapSingleton::GetInstance().color_map_.reset(
92  new ColorMapGray);
93  break;
95  GlobalColorMapSingleton::GetInstance().color_map_.reset(
96  new ColorMapSummer);
97  break;
99  GlobalColorMapSingleton::GetInstance().color_map_.reset(
100  new ColorMapWinter);
101  break;
103  GlobalColorMapSingleton::GetInstance().color_map_.reset(
104  new ColorMapHot);
105  break;
107  default:
108  GlobalColorMapSingleton::GetInstance().color_map_.reset(
109  new ColorMapJet);
110  break;
111  }
112 }
113 
114 } // namespace visualization
115 } // namespace cloudViewer
std::shared_ptr< const ColorMap > color_map_
Definition: ColorMap.cpp:38
Eigen::Vector3d GetColor(double value) const final
Function to get a color from a value in [0..1].
Definition: ColorMap.cpp:44
Eigen::Vector3d GetColor(double value) const final
Function to get a color from a value in [0..1].
Definition: ColorMap.cpp:64
See Matlab's Jet colormap.
Definition: ColorMap.h:58
double JetBase(double value) const
Definition: ColorMap.h:63
Eigen::Vector3d GetColor(double value) const final
Function to get a color from a value in [0..1].
Definition: ColorMap.cpp:48
See Matlab's Summer colormap.
Definition: ColorMap.h:79
Eigen::Vector3d GetColor(double value) const final
Function to get a color from a value in [0..1].
Definition: ColorMap.cpp:54
See Matlab's Winter colormap.
Definition: ColorMap.h:85
Eigen::Vector3d GetColor(double value) const final
Function to get a color from a value in [0..1].
Definition: ColorMap.cpp:59
double Interpolate(double value, double y0, double x0, double y1, double x1) const
Definition: ColorMap.h:35
#define LogDebug(...)
Definition: Logging.h:90
ccGuiPythonInstance * GetInstance() noexcept
Definition: Runtime.cpp:72
const std::shared_ptr< const ColorMap > GetGlobalColorMap()
Interface functions.
Definition: ColorMap.cpp:84
void SetGlobalColorMap(ColorMap::ColorMapOption option)
Definition: ColorMap.cpp:88
Generic file read and write utility for python interface.