16 namespace visualization {
20 static const int kNGradientPixels = 512;
21 static const int kNChannels = 4;
23 void SetPixel(std::vector<uint8_t>& img,
25 const Eigen::Vector4f&
color) {
26 int i = kNChannels * idx;
27 img[i] = uint8_t(std::round(255.0f *
color[0]));
28 img[i + 1] = uint8_t(std::round(255.0f *
color[1]));
29 img[i + 2] = uint8_t(std::round(255.0f *
color[2]));
30 img[i + 3] = uint8_t(std::round(255.0f *
color[3]));
49 for (
size_t i = 0; i < points_.size(); ++i) {
50 if (points_[i].value < 0.0f || points_[i].value > 1.0f) {
52 "Gradient point {} must be in range [0.0, 1.0], clamping",
70 if (textures_.find(&renderer) == textures_.end()) {
71 auto img = std::make_shared<geometry::Image>();
72 if (!points_.empty()) {
73 int n_points = int(points_.size());
75 img->Prepare(kNGradientPixels, 1, kNChannels, 1);
76 auto n = float(kNGradientPixels - 1);
78 for (
int img_idx = 0; img_idx < kNGradientPixels; ++img_idx) {
79 auto x = float(img_idx) / n;
80 while (idx < n_points && x > points_[idx].value) {
85 SetPixel(img->data_, img_idx, points_[0].color);
86 }
else if (idx == n_points) {
87 SetPixel(img->data_, img_idx,
88 points_[n_points - 1].color);
90 auto& p0 = points_[idx - 1];
91 auto& p1 = points_[idx];
92 auto dist = p1.value - p0.value;
94 auto w0 = 1.0f - (x - p0.value) /
dist;
95 auto w1 = (x - p0.value) /
dist;
96 auto r = w0 * p0.color[0] + w1 * p1.color[0];
97 auto g = w0 * p0.color[1] + w1 * p1.color[1];
98 auto b = w0 * p0.color[2] + w1 * p1.color[2];
99 auto a = w0 * p0.color[3] + w1 * p1.color[3];
100 SetPixel(img->data_, img_idx, {r, g, b, a});
104 img->Prepare(n_points, 1, kNChannels, 1);
105 for (
int i = 0; i < n_points; ++i) {
106 SetPixel(img->data_, i, points_[i].color);
110 img->Prepare(1, 1, kNChannels, 1);
111 SetPixel(img->data_, 0, {1.0f, 0.0, 1.0f, 1.0f});
114 textures_[&renderer] = renderer.
AddTexture(img);
116 return textures_[&renderer];
TextureHandle GetTextureHandle(Renderer &renderer)
const std::vector< Gradient::Point > & GetPoints() const
void SetPoints(const std::vector< Gradient::Point > &points)
virtual TextureHandle AddTexture(const ResourceLoadRequest &request, bool srgb=false)=0
static double dist(double x1, double y1, double x2, double y2)
Generic file read and write utility for python interface.