scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
colormap.h
Go to the documentation of this file.
1
53
54#pragma once
55
56#include <scimesh/types.h>
57#include <vector>
58#include <cstdint>
59#include <cmath>
60#include <cstddef>
61
62namespace scimesh {
63
64// =========================================================================
65// ColorMap
66// =========================================================================
67
79struct ColorMap {
81 std::vector<Color> colors;
82
97 Color sample(float t) const;
98
100 size_t size() const { return colors.size(); }
101
103 bool empty() const { return colors.empty(); }
104
105 // ---------------------------------------------------------------------
106 // Factory: scibar interop (duck-typed, no scibar dependency)
107 // ---------------------------------------------------------------------
108
127 template<typename T>
128 static ColorMap from_uint8_colors(const std::vector<T>& cmap) {
129 ColorMap out;
130 out.colors.reserve(cmap.size());
131 for (const auto& c : cmap) {
132 out.colors.emplace_back(
133 c.r / 255.0f, c.g / 255.0f,
134 c.b / 255.0f, c.a / 255.0f);
135 }
136 return out;
137 }
138
151 static ColorMap from_interleaved(const std::vector<uint8_t>& data,
152 int channels = 3) {
153 ColorMap out;
154 size_t n = data.size() / static_cast<size_t>(channels);
155 out.colors.reserve(n);
156 for (size_t i = 0; i < n; i++) {
157 float r = data[i * channels] / 255.0f;
158 float g = data[i * channels + 1] / 255.0f;
159 float b = data[i * channels + 2] / 255.0f;
160 float a = (channels >= 4) ? data[i * channels + 3] / 255.0f : 1.0f;
161 out.colors.emplace_back(r, g, b, a);
162 }
163 return out;
164 }
165
174 static const ColorMap& viridis();
175};
176
177// =========================================================================
178// ApplyColormapResult — single dataset
179// =========================================================================
180
190 std::vector<Color> colors;
191
194 float data_min = NAN;
195
198 float data_max = NAN;
199
202 float raw_min = NAN;
203
206 float raw_max = NAN;
207
210 float winsor_lo = NAN;
211
214 float winsor_hi = NAN;
215
217 size_t nan_count = 0;
218};
219
220// =========================================================================
221// MultiApplyColormapResult — multiple datasets
222// =========================================================================
223
233 std::vector<ApplyColormapResult> per_dataset;
234
237 float pooled_data_min = NAN;
238
241 float pooled_data_max = NAN;
242
244 float pooled_raw_min = NAN;
245
247 float pooled_raw_max = NAN;
248
250 float pooled_winsor_lo = NAN;
251
253 float pooled_winsor_hi = NAN;
254
256 size_t total_nan_count = 0;
257};
258
259// =========================================================================
260// apply_colormap — function declarations
261// =========================================================================
262
298 const std::vector<float>& data,
299 const ColorMap& colormap,
300 float vmin = NAN,
301 float vmax = NAN,
302 const Color& nan_color = Color{0.5f, 0.5f, 0.5f, 1.0f},
303 float lo_pct = 0.0f,
304 float hi_pct = 100.0f
305);
306
344MultiApplyColormapResult apply_colormap(
345 const std::vector<std::vector<float>>& datasets,
346 const ColorMap& colormap,
347 float vmin = NAN,
348 float vmax = NAN,
349 const Color& nan_color = Color{0.5f, 0.5f, 0.5f, 1.0f},
350 float lo_pct = 0.0f,
351 float hi_pct = 100.0f,
352 bool global_range = false
353);
354
355} // namespace scimesh
ApplyColormapResult apply_colormap(const std::vector< float > &data, const ColorMap &colormap, float vmin, float vmax, const Color &nan_color, float lo_pct, float hi_pct)
Map a single vector of numeric data to RGBA colours using a colormap.
Definition colormap.cpp:345
Result of apply_colormap() for a single dataset.
Definition colormap.h:187
float data_min
The actual data range used for mapping (after winsorizing, or as explicitly set).
Definition colormap.h:194
std::vector< Color > colors
Per-element RGBA colours, same order and length as the input data vector.
Definition colormap.h:190
float raw_max
Raw finite maximum of the input data (before winsorizing).
Definition colormap.h:206
float data_max
The actual data range used for mapping (after winsorizing, or as explicitly set).
Definition colormap.h:198
size_t nan_count
Number of NaN / Inf values in the input data.
Definition colormap.h:217
float winsor_hi
Upper percentile cutoff used for winsorizing.
Definition colormap.h:214
float winsor_lo
Lower percentile cutoff used for winsorizing.
Definition colormap.h:210
float raw_min
Raw finite minimum of the input data (before winsorizing).
Definition colormap.h:202
Self-contained colormap — owns a colour lookup table and supports linearly interpolated sampling.
Definition colormap.h:79
static ColorMap from_interleaved(const std::vector< uint8_t > &data, int channels=3)
Build a ColorMap from a flat interleaved RGB(A) byte array.
Definition colormap.h:151
bool empty() const
True if the colormap has no entries.
Definition colormap.h:103
static const ColorMap & viridis()
The Viridis perceptually-uniform sequential colormap (256 entries).
Definition colormap.cpp:195
size_t size() const
Number of entries in the colormap.
Definition colormap.h:100
Color sample(float t) const
Sample the colormap at a normalized position using linear interpolation.
Definition colormap.cpp:15
static ColorMap from_uint8_colors(const std::vector< T > &cmap)
Build a ColorMap from a vector of scibar-style uint8_t RGBA colours.
Definition colormap.h:128
std::vector< Color > colors
The colour lookup table entries in [0, 1] float RGBA.
Definition colormap.h:81
An RGBA color with floating-point components.
Definition types.h:88
Aggregate result of apply_colormap() for multiple datasets.
Definition colormap.h:231
float pooled_data_max
Pooled effective data range (post-winsorizing) across all datasets.
Definition colormap.h:241
float pooled_winsor_hi
Pooled upper percentile cutoff.
Definition colormap.h:253
size_t total_nan_count
Total number of NaN / Inf values across all datasets.
Definition colormap.h:256
float pooled_winsor_lo
Pooled lower percentile cutoff.
Definition colormap.h:250
std::vector< ApplyColormapResult > per_dataset
One result per input dataset, in the same order.
Definition colormap.h:233
float pooled_raw_max
Pooled raw finite maximum across all datasets.
Definition colormap.h:247
float pooled_data_min
Pooled effective data range (post-winsorizing) across all datasets.
Definition colormap.h:237
float pooled_raw_min
Pooled raw finite minimum across all datasets.
Definition colormap.h:244
Fundamental types used throughout the scimesh rendering engine.