载入中...
搜索中...
未找到
UvPaintRegion.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <utility>
6
7namespace eve::image {
8
9Result<void> UvPaintRegion::prepareResult(int width, int height, float centerU, float centerV, float brushRadiusU,
10 float brushRadiusV, float red, float green, float blue, float alpha,
11 bool flipV) {
12 auto prepared = prepareUvPaintRegionResult(width, height, centerU, centerV, brushRadiusU, brushRadiusV, red, green,
13 blue, alpha, flipV);
14 if (!prepared.ok()) return Result<void>::failure(prepared.status());
15 *this = std::move(prepared).takeValue();
16 return Result<void>::success();
17}
18
19Result<UvPaintRegion> prepareUvPaintRegionResult(int targetWidth, int targetHeight, float u, float v, float radiusU,
20 float radiusV, float r, float g, float b, float a, bool flipV) {
21 const auto invalid = [](const char* message, const char* path) {
24 };
25 if (targetWidth <= 0 || targetHeight <= 0) return invalid("paint target dimensions must be positive", "targetSize");
26 if (!std::isfinite(u) || !std::isfinite(v) || !std::isfinite(radiusU) || !std::isfinite(radiusV) || radiusU < 0.f ||
27 radiusV < 0.f)
28 return invalid("UV center and radii must be finite; radii must be non-negative", "brush");
29 if (!std::isfinite(r) || !std::isfinite(g) || !std::isfinite(b) || !std::isfinite(a) || r < 0.f || r > 1.f ||
30 g < 0.f || g > 1.f || b < 0.f || b > 1.f || a < 0.f || a > 1.f)
31 return invalid("paint color channels must be finite values in [0,1]", "color");
32
33 UvPaintRegion region;
34 region.targetWidth = targetWidth;
35 region.targetHeight = targetHeight;
36 region.u = std::clamp(u, 0.f, 1.f);
37 region.v = std::clamp(v, 0.f, 1.f);
38 region.radiusU = radiusU;
39 region.radiusV = radiusV;
40 region.r = r;
41 region.g = g;
42 region.b = b;
43 region.a = a;
44
45 const float pixelU = region.u * float(targetWidth - 1);
46 const float mappedV = flipV ? 1.f - region.v : region.v;
47 const float pixelV = mappedV * float(targetHeight - 1);
48 region.centerX = static_cast<int>(std::lround(pixelU));
49 region.centerY = static_cast<int>(std::lround(pixelV));
50 region.minX = std::clamp(static_cast<int>(std::floor(pixelU - radiusU * targetWidth)), 0, targetWidth - 1);
51 region.maxX = std::clamp(static_cast<int>(std::ceil(pixelU + radiusU * targetWidth)), 0, targetWidth - 1);
52 region.minY = std::clamp(static_cast<int>(std::floor(pixelV - radiusV * targetHeight)), 0, targetHeight - 1);
53 region.maxY = std::clamp(static_cast<int>(std::ceil(pixelV + radiusV * targetHeight)), 0, targetHeight - 1);
54 return Result<UvPaintRegion>::success(region);
55}
56
57} // namespace eve::image
std::string message
tensor::Graph g
Definition GpuGraph.cpp:7
float u
Definition Grass.cpp:233
double r
float v
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string path
Definition PlayHost.cpp:110
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Result< UvPaintRegion > prepareUvPaintRegionResult(int targetWidth, int targetHeight, float u, float v, float radiusU, float radiusV, float r, float g, float b, float a, bool flipV)
Validate and prepare the canonical clipped region for a UV brush.
Reusable renderer-neutral bounds for one normalized UV paint operation.
Result< void > prepareResult(int width, int height, float centerU, float centerV, float brushRadiusU, float brushRadiusV, float red, float green, float blue, float alpha, bool flipV=true)
Atomically replace this reusable scratch with a validated region.
int height() const noexcept
Inclusive clipped height in pixels.
int width() const noexcept
Inclusive clipped width in pixels.