1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include "ColorMap.h"
28 
29 #include <Core/Utility/Console.h>
30 
31 namespace three{
32 
33 namespace {
34 
35 class GlobalColorMapSingleton
36 {
37 private:
GlobalColorMapSingleton()38     GlobalColorMapSingleton() : color_map_(new ColorMapJet) {
39         PrintDebug("Global colormap init.\n");
40     }
41     GlobalColorMapSingleton(const GlobalColorMapSingleton &) = delete;
42     GlobalColorMapSingleton &operator=(const GlobalColorMapSingleton &) =
43             delete;
44 public:
~GlobalColorMapSingleton()45     ~GlobalColorMapSingleton() {
46         PrintDebug("Global colormap destruct.\n");
47     }
48 
49 public:
GetInstance()50     static GlobalColorMapSingleton &GetInstance() {
51         static GlobalColorMapSingleton singleton;
52         return singleton;
53     }
54 
55 public:
56     std::shared_ptr<const ColorMap> color_map_;
57 };
58 
59 }    // unnamed namespace
60 
GetColor(double value) const61 Eigen::Vector3d ColorMapGray::GetColor(double value) const
62 {
63     return Eigen::Vector3d(value, value, value);
64 }
65 
GetColor(double value) const66 Eigen::Vector3d ColorMapJet::GetColor(double value) const
67 {
68     return Eigen::Vector3d(
69             JetBase(value * 2.0 - 1.5),        // red
70             JetBase(value * 2.0 - 1.0),        // green
71             JetBase(value * 2.0 - 0.5));    // blue
72 }
73 
GetColor(double value) const74 Eigen::Vector3d ColorMapSummer::GetColor(double value) const
75 {
76     return Eigen::Vector3d(
77             Interpolate(value, 0.0, 0.0, 1.0, 1.0),
78             Interpolate(value, 0.5, 0.0, 1.0, 1.0),
79             0.4);
80 }
81 
GetColor(double value) const82 Eigen::Vector3d ColorMapWinter::GetColor(double value) const
83 {
84     return Eigen::Vector3d(
85             0.0,
86             Interpolate(value, 0.0, 0.0, 1.0, 1.0),
87             Interpolate(value, 1.0, 0.0, 0.5, 1.0));
88 }
89 
GetColor(double value) const90 Eigen::Vector3d ColorMapHot::GetColor(double value) const
91 {
92     Eigen::Vector3d edges[4] = {
93         Eigen::Vector3d(1.0, 1.0, 1.0),
94         Eigen::Vector3d(1.0, 1.0, 0.0),
95         Eigen::Vector3d(1.0, 0.0, 0.0),
96         Eigen::Vector3d(0.0, 0.0, 0.0),
97     };
98     if (value < 0.0) {
99         return edges[0];
100     } else if (value < 1.0 / 3.0) {
101         return Interpolate(value, edges[0], 0.0, edges[1], 1.0 / 3.0);
102     } else if (value < 2.0 / 3.0) {
103         return Interpolate(value, edges[1], 1.0 / 3.0, edges[2], 2.0 / 3.0);
104     } else if (value < 1.0) {
105         return Interpolate(value, edges[2], 2.0 / 3.0, edges[3], 1.0);
106     } else {
107         return edges[3];
108     }
109 }
110 
GetGlobalColorMap()111 const std::shared_ptr<const ColorMap> GetGlobalColorMap()
112 {
113     return GlobalColorMapSingleton::GetInstance().color_map_;
114 }
115 
SetGlobalColorMap(ColorMap::ColorMapOption option)116 void SetGlobalColorMap(ColorMap::ColorMapOption option)
117 {
118     switch (option) {
119     case ColorMap::ColorMapOption::Gray:
120         GlobalColorMapSingleton::GetInstance().color_map_.reset(
121                 new ColorMapGray);
122         break;
123     case ColorMap::ColorMapOption::Summer:
124         GlobalColorMapSingleton::GetInstance().color_map_.reset(
125                 new ColorMapSummer);
126         break;
127     case ColorMap::ColorMapOption::Winter:
128         GlobalColorMapSingleton::GetInstance().color_map_.reset(
129                 new ColorMapWinter);
130         break;
131     case ColorMap::ColorMapOption::Hot:
132         GlobalColorMapSingleton::GetInstance().color_map_.reset(
133                 new ColorMapHot);
134         break;
135     case ColorMap::ColorMapOption::Jet:
136     default:
137         GlobalColorMapSingleton::GetInstance().color_map_.reset(
138                 new ColorMapJet);
139         break;
140     }
141 }
142 
143 }    // namespace three
144