Blender V5.3
pbvh_pixels_rasterize.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <utility>
12
13#include "BLI_bounds_types.hh"
14#include "BLI_math_vector.hh"
15
16namespace blender::bke::pbvh {
17
23 private:
24 struct Edge {
25 /* Edge function coefficients, so that:
26 *
27 * cross(edge0, edge1, (x, y)) =
28 * coefficients.x * x + coefficients.y * y + coefficients.z
29 */
30 float3 coefficients;
31 /* Are we inside when the edge function is positive? */
32 bool positive_side;
33 };
34
35 Edge edges[3];
36
37 public:
39
40 TriRasterizer(const float2 uv0, const float2 uv1, const float2 uv2, const int2 image_size)
41 {
42 const float2 f_image_size(image_size.x, image_size.y);
43 const float2 p[3] = {uv0 * f_image_size, uv1 * f_image_size, uv2 * f_image_size};
44
45 if (isfinite(p[0].x) && isfinite(p[0].y) && isfinite(p[1].x) && isfinite(p[1].y) &&
46 isfinite(p[2].x) && isfinite(p[2].y))
47 {
48 Bounds<float2> f_bounds(math::min({p[0], p[1], p[2]}), math::max({p[0], p[1], p[2]}));
49 bounds = Bounds<int2>(math::clamp(int2(math::floor(f_bounds.min)), int2(0), image_size),
50 math::clamp(int2(math::ceil(f_bounds.max)), int2(0), image_size));
51 }
52 else {
53 bounds = Bounds<int2>(int2(1), int2(0)); /* Empty bounds. */
54 }
55
56 for (int i = 0; i < 3; i++) {
57 float2 a = p[i];
58 float2 b = p[(i + 1) % 3];
59 const float2 c = p[(i + 2) % 3];
60 /* Sort edges in canonical order. */
61 if (b.x < a.x || (b.x == a.x && b.y < a.y)) {
62 std::swap(a, b);
63 }
64 const float2 d = b - a;
65 edges[i].coefficients = {-d.y, d.x, math::cross(a, d)};
66 /* Check which side the c vertex is on. */
67 edges[i].positive_side = (math::cross(d, c - a) > 0.0f);
68 }
69 }
70
71 /* True if the pixel center at (x, y) is inside. */
72 bool inside(const int x, const int y) const
73 {
74 const float3 xyz = {float(x) + 0.5f, float(y) + 0.5f, 1.0f};
75 const float3 edge_values = {math::dot(edges[0].coefficients, xyz),
76 math::dot(edges[1].coefficients, xyz),
77 math::dot(edges[2].coefficients, xyz)};
78 /* Note the positive side uses >= and negative uses < to make it watertight. */
79 return ((edge_values.x >= 0.0f) == edges[0].positive_side) &
80 ((edge_values.y >= 0.0f) == edges[1].positive_side) &
81 ((edge_values.z >= 0.0f) == edges[2].positive_side);
82 }
83};
84
85} // namespace blender::bke::pbvh
T clamp(const T &a, const T &min, const T &max)
T floor(const T &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T ceil(const T &a)
T max(const T &a, const T &b)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Bounds()=default
TriRasterizer(const float2 uv0, const float2 uv1, const float2 uv2, const int2 image_size)
bool inside(const int x, const int y) const