-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathColorMap.cpp
167 lines (118 loc) · 5.34 KB
/
ColorMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
////////////////////////////////////////////////////////////////////////////////////////////////////
// This file is part of CosmoScout VR //
////////////////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]>
// SPDX-License-Identifier: MIT
#include "ColorMap.hpp"
#include "../cs-utils/doctest.hpp"
#include <nlohmann/json.hpp>
#include <fstream>
namespace cs::graphics {
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace {
////////////////////////////////////////////////////////////////////////////////////////////////////
struct ColorMapData {
std::map<float, glm::vec3> rgbStops;
std::map<float, float> alphaStops;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
void from_json(nlohmann::json const& j, ColorMapData& s) {
auto rgb = j.at("RGB");
for (nlohmann::json::iterator it = rgb.begin(); it != rgb.end(); ++it) {
glm::vec3 value;
for (int i = 0; i < 3; ++i) {
value[i] = it.value().at(i);
}
s.rgbStops[std::stof(it.key())] = value;
}
auto alpha = j.at("Alpha");
for (nlohmann::json::iterator it = alpha.begin(); it != alpha.end(); ++it) {
s.alphaStops[std::stof(it.key())] = it.value();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
T interpolate(float key, const std::map<float, T>& map) {
if (map.find(key) != map.end()) {
return map.at(key);
}
if (key < map.begin()->first) {
return map.begin()->second;
}
if (key > map.rbegin()->first) {
return map.rbegin()->second;
}
auto lower = map.lower_bound(key) == map.begin() ? map.begin() : --(map.lower_bound(key));
auto upper = map.upper_bound(key);
return lower->second + (upper->second - lower->second) * float(key - lower->first) /
std::fabs(upper->first - lower->first);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<glm::vec4> mergeColorMapData(ColorMapData colorMapData, int resolution) {
std::vector<glm::vec4> colors(resolution);
for (size_t i(0); i < colors.size(); ++i) {
float key = static_cast<float>(i) / static_cast<float>(colors.size() - 1);
glm::vec3 color = interpolate(key, colorMapData.rgbStops);
float alpha = interpolate(key, colorMapData.alphaStops);
colors[i] = glm::vec4(color, alpha);
}
return colors;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<VistaTexture> generateTexture(std::vector<glm::vec4> colors) {
std::unique_ptr<VistaTexture> texture = std::make_unique<VistaTexture>(GL_TEXTURE_1D);
texture->UploadTexture((int)colors.size(), 1, colors.data(), false, GL_RGBA, GL_FLOAT);
texture->SetWrapS(GL_CLAMP_TO_EDGE);
texture->SetWrapT(GL_CLAMP_TO_EDGE);
texture->SetMinFilter(GL_LINEAR);
texture->SetMagFilter(GL_LINEAR);
return texture;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace
////////////////////////////////////////////////////////////////////////////////////////////////////
ColorMap::ColorMap(std::string const& sJsonString) {
nlohmann::json json = nlohmann::json::parse(sJsonString);
ColorMapData colorMapData = json;
mRawData = mergeColorMapData(colorMapData, mResolution);
mTexture = generateTexture(mRawData);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ColorMap::ColorMap(boost::filesystem::path const& sJsonPath) {
std::ifstream file(sJsonPath.string());
nlohmann::json json;
file >> json;
ColorMapData colorMapData = json;
mRawData = mergeColorMapData(colorMapData, mResolution);
mTexture = generateTexture(mRawData);
for (size_t i(0); i < mRawData.size(); ++i) {
if (mRawData[i].a < 1.0) {
mUsesAlpha = true;
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void ColorMap::bind(unsigned unit) {
mTexture->Bind(unit);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void ColorMap::unbind(unsigned unit) {
mTexture->Unbind(unit);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<glm::vec4> ColorMap::getRawData() {
return mRawData;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool ColorMap::getUsesAlpha() const {
return mUsesAlpha;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST_CASE("cs::graphics::ColorMap::interpolate") {
std::map<float, glm::vec3> stops = {{0.F, glm::vec3(1.F, 1.F, 0.F)},
{0.5F, glm::vec3(1.F, 0.F, 0.F)}, {1.0F, glm::vec3(1.F, 0.F, 1.F)}};
CHECK(interpolate(0.F, stops) == glm::vec3(1.F, 1.F, 0.F));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace cs::graphics