OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::DecodedHeightTexture Struct Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/TextureDisplacement.hpp>

Collaboration diagram for Slic3r::DecodedHeightTexture:

Classes

struct  TexelTap

Public Member Functions

bool empty () const
bool has_color () const
float sample (const Vec2f &uv, bool tile_enabled=true, TextureTileMethod tile_method=TextureTileMethod::Repeat) const
Vec3f sample_color (const Vec2f &uv, bool tile_enabled=true, TextureTileMethod tile_method=TextureTileMethod::Repeat) const
bool texel_tap (const Vec2f &uv, bool tile_enabled, TextureTileMethod tile_method, TexelTap &out) const

Public Attributes

std::vector< uint8_t > pixels
std::vector< uint8_t > rgb
int width = 0
int height = 0

Detailed Description


Class Documentation

◆ Slic3r::DecodedHeightTexture::TexelTap

struct Slic3r::DecodedHeightTexture::TexelTap
Class Members
float tx = 0.f
float ty = 0.f
int x0 = 0
int x1 = 0
int y0 = 0
int y1 = 0

Member Function Documentation

◆ empty()

bool Slic3r::DecodedHeightTexture::empty ( ) const
inline

◆ has_color()

bool Slic3r::DecodedHeightTexture::has_color ( ) const
inline

◆ sample()

float Slic3r::DecodedHeightTexture::sample ( const Vec2f & uv,
bool tile_enabled = true,
TextureTileMethod tile_method = TextureTileMethod::Repeat ) const
106{
107 TexelTap tap;
108 if (!texel_tap(uv, tile_enabled, tile_method, tap))
109 return 0.f;
110 const int x0 = tap.x0, y0 = tap.y0, x1 = tap.x1, y1 = tap.y1;
111 const float tx = tap.tx, ty = tap.ty;
112
113 auto at = [this](int x, int y) { return float(pixels[size_t(y) * size_t(width) + size_t(x)]) / 255.f; };
114 const float top = at(x0, y0) * (1.f - tx) + at(x1, y0) * tx;
115 const float bottom = at(x0, y1) * (1.f - tx) + at(x1, y1) * tx;
116 return top * (1.f - ty) + bottom * ty;
117}
bool texel_tap(const Vec2f &uv, bool tile_enabled, TextureTileMethod tile_method, TexelTap &out) const
Definition TextureDisplacement.cpp:55
int x0
Definition TextureDisplacement.hpp:561
Definition TextureDisplacement.hpp:560

References pixels, texel_tap(), Slic3r::DecodedHeightTexture::TexelTap::tx, Slic3r::DecodedHeightTexture::TexelTap::ty, width, Slic3r::DecodedHeightTexture::TexelTap::x0, Slic3r::DecodedHeightTexture::TexelTap::x1, Slic3r::DecodedHeightTexture::TexelTap::y0, and Slic3r::DecodedHeightTexture::TexelTap::y1.

Referenced by Slic3r::sample_layer_height().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sample_color()

Vec3f Slic3r::DecodedHeightTexture::sample_color ( const Vec2f & uv,
bool tile_enabled = true,
TextureTileMethod tile_method = TextureTileMethod::Repeat ) const
120{
121 TexelTap tap;
122 if (!has_color() || !texel_tap(uv, tile_enabled, tile_method, tap))
123 return Vec3f::Zero();
124
125 auto at = [this](int x, int y) {
126 const size_t i = (size_t(y) * size_t(width) + size_t(x)) * 3;
127 return Vec3f(float(rgb[i]) / 255.f, float(rgb[i + 1]) / 255.f, float(rgb[i + 2]) / 255.f);
128 };
129 const Vec3f top = at(tap.x0, tap.y0) * (1.f - tap.tx) + at(tap.x1, tap.y0) * tap.tx;
130 const Vec3f bottom = at(tap.x0, tap.y1) * (1.f - tap.tx) + at(tap.x1, tap.y1) * tap.tx;
131 return top * (1.f - tap.ty) + bottom * tap.ty;
132}
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
bool has_color() const
Definition TextureDisplacement.hpp:542

References has_color(), rgb, texel_tap(), Slic3r::DecodedHeightTexture::TexelTap::tx, Slic3r::DecodedHeightTexture::TexelTap::ty, width, Slic3r::DecodedHeightTexture::TexelTap::x0, Slic3r::DecodedHeightTexture::TexelTap::x1, Slic3r::DecodedHeightTexture::TexelTap::y0, and Slic3r::DecodedHeightTexture::TexelTap::y1.

Referenced by Slic3r::sample_layer_color().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ texel_tap()

bool Slic3r::DecodedHeightTexture::texel_tap ( const Vec2f & uv,
bool tile_enabled,
TextureTileMethod tile_method,
TexelTap & out ) const
57{
58 if (empty())
59 return false;
60
61 auto repeat01 = [](float x) {
62 x = std::fmod(x, 1.f);
63 return x < 0.f ? x + 1.f : x;
64 };
65 // Standard mirrored-repeat: reflect back and forth every other unit, so tile edges always
66 // line up with themselves instead of jumping from one edge of the image to the other.
67 auto mirrored_repeat01 = [](float x) {
68 x = std::fmod(std::abs(x), 2.f);
69 return x > 1.f ? 2.f - x : x;
70 };
71
72 if (!tile_enabled && (uv.x() < 0.f || uv.x() >= 1.f || uv.y() < 0.f || uv.y() >= 1.f))
73 // Outside the single, non-repeating placement entirely: no texture there, not "smeared
74 // edge pixel" - clamping the *coordinate* to [0, 1] would otherwise keep returning the
75 // border row/column's height forever in every direction, stretching it out to infinity.
76 return false;
77
78 float u, v;
79 if (!tile_enabled) {
80 u = uv.x();
81 v = uv.y();
82 } else if (tile_method == TextureTileMethod::MirroredRepeat) {
83 u = mirrored_repeat01(uv.x());
84 v = mirrored_repeat01(uv.y());
85 } else {
86 u = repeat01(uv.x());
87 v = repeat01(uv.y());
88 }
89
90 const float fx = u * float(width);
91 const float fy = v * float(height);
92 const int x0 = std::clamp(int(std::floor(fx)), 0, width - 1);
93 const int y0 = std::clamp(int(std::floor(fy)), 0, height - 1);
94 // Neighbour for bilinear filtering: wrap for tiling methods, clamp at the edge otherwise (a
95 // repeating neighbour would incorrectly blend against the opposite edge of the image).
96 tap.x0 = x0;
97 tap.y0 = y0;
98 tap.x1 = tile_enabled ? (x0 + 1) % width : std::min(x0 + 1, width - 1);
99 tap.y1 = tile_enabled ? (y0 + 1) % height : std::min(y0 + 1, height - 1);
100 tap.tx = fx - std::floor(fx);
101 tap.ty = fy - std::floor(fy);
102 return true;
103}
@ MirroredRepeat
Definition TextureDisplacement.hpp:51

References empty(), height, Slic3r::MirroredRepeat, Slic3r::DecodedHeightTexture::TexelTap::tx, Slic3r::DecodedHeightTexture::TexelTap::ty, width, Slic3r::DecodedHeightTexture::TexelTap::x0, Slic3r::DecodedHeightTexture::TexelTap::x1, Slic3r::DecodedHeightTexture::TexelTap::y0, and Slic3r::DecodedHeightTexture::TexelTap::y1.

Referenced by sample(), and sample_color().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ height

◆ pixels

◆ rgb

◆ width


The documentation for this struct was generated from the following files: