Blender V5.0
reverse_uv_sampler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <algorithm>
6#include <fmt/format.h>
7
9
10#include "BLI_bounds.hh"
13#include "BLI_map.hh"
14#include "BLI_math_geom.h"
15#include "BLI_math_vector.hh"
16#include "BLI_offset_indices.hh"
17#include "BLI_task.hh"
18
19namespace blender::geometry {
20
33
40
43 int x_min;
44 int x_max;
45};
46
52
57
58static int2 uv_to_cell(const float2 &uv, const int resolution)
59{
60 return int2{uv * resolution};
61}
62
64 const int resolution,
65 const Span<float2> uv_map)
66{
67 const float2 &uv_0 = uv_map[tri[0]];
68 const float2 &uv_1 = uv_map[tri[1]];
69 const float2 &uv_2 = uv_map[tri[2]];
70
71 const int2 cell_0 = uv_to_cell(uv_0, resolution);
72 const int2 cell_1 = uv_to_cell(uv_1, resolution);
73 const int2 cell_2 = uv_to_cell(uv_2, resolution);
74
75 const int2 min_cell = math::min(math::min(cell_0, cell_1), cell_2);
76 const int2 max_cell = math::max(math::max(cell_0, cell_1), cell_2);
77
78 return {min_cell, max_cell};
79}
80
86static void sort_tris_into_rows(const Span<float2> uv_map,
87 const Span<int3> corner_tris,
88 const int resolution,
90{
91 threading::parallel_for(corner_tris.index_range(), 256, [&](const IndexRange tris_range) {
92 LocalData &local_data = data_per_thread.local();
93 for (const int tri_i : tris_range) {
94 const int3 &tri = corner_tris[tri_i];
95
96 /* Compute the cells that the triangle touches approximately. */
97 const Bounds<int2> cell_bounds = tri_to_cell_bounds(tri, resolution, uv_map);
98 const TriWithRange tri_with_range{tri_i, cell_bounds.min.x, cell_bounds.max.x};
99
100 /* Go over each row that the triangle is in. */
101 for (int cell_y = cell_bounds.min.y; cell_y <= cell_bounds.max.y; cell_y++) {
102 LocalRowData &row = *local_data.rows.lookup_or_add_cb(
103 cell_y, [&]() { return local_data.allocator.construct<LocalRowData>(); });
104 row.tris.append(local_data.allocator, tri_with_range);
105 row.x_min = std::min<int>(row.x_min, cell_bounds.min.x);
106 row.x_max = std::max<int>(row.x_max, cell_bounds.max.x);
107 }
108 }
109 });
110}
111
116static void finish_rows(const Span<int> all_ys,
117 const Span<const LocalData *> local_data_vec,
118 const Bounds<int> y_bounds,
119 ReverseUVSampler::LookupGrid &lookup_grid)
120{
121 threading::parallel_for(all_ys.index_range(), 8, [&](const IndexRange all_ys_range) {
122 Vector<const LocalRowData *, 32> local_rows;
123 for (const int y : all_ys.slice(all_ys_range)) {
124 Row &row = lookup_grid.rows[y - y_bounds.min];
125
126 local_rows.clear();
127 for (const LocalData *local_data : local_data_vec) {
128 if (const destruct_ptr<LocalRowData> *local_row = local_data->rows.lookup_ptr(y)) {
129 local_rows.append(local_row->get());
130 }
131 }
132
133 int x_min = INT32_MAX;
134 int x_max = INT32_MIN;
135 for (const LocalRowData *local_row : local_rows) {
136 x_min = std::min(x_min, local_row->x_min);
137 x_max = std::max(x_max, local_row->x_max);
138 }
139
140 const int x_num = x_max - x_min + 1;
141 row.offsets.reinitialize(x_num + 1);
142 {
143 /* Count how many triangles are in each cell in the current row. */
144 MutableSpan<int> counts = row.offsets;
145 counts.fill(0);
146 for (const LocalRowData *local_row : local_rows) {
147 for (const TriWithRange &tri_with_range : local_row->tris) {
148 for (int x = tri_with_range.x_min; x <= tri_with_range.x_max; x++) {
149 counts[x - x_min]++;
150 }
151 }
152 }
153 offset_indices::accumulate_counts_to_offsets(counts);
154 }
155 const int tri_indices_num = row.offsets.last();
156 row.tri_indices.reinitialize(tri_indices_num);
157
158 /* Populate the array containing all triangle indices in all cells in this row. */
159 Array<int, 1000> current_offsets(x_num, 0);
160 for (const LocalRowData *local_row : local_rows) {
161 for (const TriWithRange &tri_with_range : local_row->tris) {
162 for (int x = tri_with_range.x_min; x <= tri_with_range.x_max; x++) {
163 const int offset_x = x - x_min;
164 row.tri_indices[row.offsets[offset_x] + current_offsets[offset_x]] =
165 tri_with_range.tri_index;
166 current_offsets[offset_x]++;
167 }
168 }
169 }
170
171 row.x_min = x_min;
172 row.x_max = x_max;
173 }
174 });
175}
176
178 : uv_map_(uv_map), corner_tris_(corner_tris), lookup_grid_(std::make_unique<LookupGrid>())
179{
180 /* A lower resolution means that there will be fewer cells and more triangles in each cell. Fewer
181 * cells make construction faster, but more triangles per cell make lookup slower. This value
182 * needs to be determined experimentally. */
183 resolution_ = std::max<int>(3, std::sqrt(corner_tris.size()) * 3);
184 if (corner_tris.is_empty()) {
185 return;
186 }
187
189 sort_tris_into_rows(uv_map_, corner_tris_, resolution_, data_per_thread);
190
191 VectorSet<int> all_ys;
192 Vector<const LocalData *> local_data_vec;
193 for (const LocalData &local_data : data_per_thread) {
194 local_data_vec.append(&local_data);
195 for (const int y : local_data.rows.keys()) {
196 all_ys.add(y);
197 }
198 }
199
200 const Bounds<int> y_bounds = *bounds::min_max(all_ys.as_span());
201 lookup_grid_->y_min = y_bounds.min;
202
203 const int rows_num = y_bounds.max - y_bounds.min + 1;
204 lookup_grid_->rows.reinitialize(rows_num);
205
206 finish_rows(all_ys, local_data_vec, y_bounds, *lookup_grid_);
207}
208
210 const ReverseUVSampler::LookupGrid &lookup_grid)
211{
212 if (cell.y < lookup_grid.y_min) {
213 return {};
214 }
215 if (cell.y >= lookup_grid.y_min + lookup_grid.rows.size()) {
216 return {};
217 }
218 const Row &row = lookup_grid.rows[cell.y - lookup_grid.y_min];
219 if (cell.x < row.x_min) {
220 return {};
221 }
222 if (cell.x > row.x_max) {
223 return {};
224 }
225 if (row.tri_indices.is_empty()) {
226 return {};
227 }
228 const int offset = row.offsets[cell.x - row.x_min];
229 const int tris_num = row.offsets[cell.x - row.x_min + 1] - offset;
230 return row.tri_indices.as_span().slice(offset, tris_num);
231}
232
234{
235 const int2 cell = uv_to_cell(query_uv, resolution_);
236 const Span<int> tri_indices = lookup_tris_in_cell(cell, *lookup_grid_);
237
238 float best_dist = FLT_MAX;
239 float3 best_bary_weights;
240 int best_tri_index;
241
242 /* The distance to an edge that is allowed to be inside or outside the triangle. Without this,
243 * the lookup can fail for floating point accuracy reasons when the uv is almost exact on an
244 * edge. */
245 const float edge_epsilon = 0.00001f;
246 /* If uv triangles are very small, it may look like the query hits multiple triangles due to
247 * floating point precision issues. Better just pick one of the triangles instead of failing the
248 * entire operation in this case. */
249 const float area_epsilon = 0.00001f;
250
251 for (const int tri_i : tri_indices) {
252 const int3 &tri = corner_tris_[tri_i];
253 const float2 &uv_0 = uv_map_[tri[0]];
254 const float2 &uv_1 = uv_map_[tri[1]];
255 const float2 &uv_2 = uv_map_[tri[2]];
256 float3 bary_weights;
257 if (!barycentric_coords_v2(uv_0, uv_1, uv_2, query_uv, bary_weights)) {
258 continue;
259 }
260
261 /* If #query_uv is in the triangle, the distance is <= 0. Otherwise, the larger the distance,
262 * the further away the uv is from the triangle. */
263 const float x_dist = std::max(-bary_weights.x, bary_weights.x - 1.0f);
264 const float y_dist = std::max(-bary_weights.y, bary_weights.y - 1.0f);
265 const float z_dist = std::max(-bary_weights.z, bary_weights.z - 1.0f);
266 const float dist = std::max({x_dist, y_dist, z_dist});
267
268 if (dist <= 0.0f && best_dist <= 0.0f) {
269 const float worse_dist = std::max(dist, best_dist);
270 /* Allow ignoring multiple triangle intersections if the uv is almost exactly on an edge. */
271 if (worse_dist < -edge_epsilon) {
272 const int3 &best_tri = corner_tris_[tri_i];
273 const float best_tri_area = area_tri_v2(
274 uv_map_[best_tri[0]], uv_map_[best_tri[1]], uv_map_[best_tri[2]]);
275 const float current_tri_area = area_tri_v2(uv_0, uv_1, uv_2);
276 if (best_tri_area > area_epsilon && current_tri_area > area_epsilon) {
277 /* The uv sample is in multiple triangles. */
279 }
280 }
281 }
282
283 if (dist < best_dist) {
284 best_dist = dist;
285 best_bary_weights = bary_weights;
286 best_tri_index = tri_i;
287 }
288 }
289
290 /* Allow using the closest (but not intersecting) triangle if the uv is almost exactly on an
291 * edge. */
292 if (best_dist < edge_epsilon) {
293 return Result{ResultType::Ok, best_tri_index, math::clamp(best_bary_weights, 0.0f, 1.0f)};
294 }
295
296 return Result{};
297}
298
300
302 MutableSpan<Result> r_results) const
303{
304 BLI_assert(query_uvs.size() == r_results.size());
305 threading::parallel_for(query_uvs.index_range(), 256, [&](const IndexRange range) {
306 for (const int i : range) {
307 r_results[i] = this->sample(query_uvs[i]);
308 }
309 });
310}
311
312} // namespace blender::geometry
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE float area_tri_v2(const float v1[2], const float v2[2], const float v3[2])
bool barycentric_coords_v2(const float v1[2], const float v2[2], const float v3[2], const float co[2], float w[3])
Span< T > as_span() const
Definition BLI_array.hh:243
bool is_empty() const
Definition BLI_array.hh:264
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
bool add(const Key &key)
Span< Key > as_span() const
void append(const T &value)
ReverseUVSampler(Span< float2 > uv_map, Span< int3 > corner_tris)
Result sample(const float2 &query_uv) const
void sample_many(Span< float2 > query_uvs, MutableSpan< Result > r_results) const
#define INT32_MAX
#define INT32_MIN
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:55
static void finish_rows(const Span< int > all_ys, const Span< const LocalData * > local_data_vec, const Bounds< int > y_bounds, ReverseUVSampler::LookupGrid &lookup_grid)
static Span< int > lookup_tris_in_cell(const int2 cell, const ReverseUVSampler::LookupGrid &lookup_grid)
static void sort_tris_into_rows(const Span< float2 > uv_map, const Span< int3 > corner_tris, const int resolution, threading::EnumerableThreadSpecific< LocalData > &data_per_thread)
static Bounds< int2 > tri_to_cell_bounds(const int3 &tri, const int resolution, const Span< float2 > uv_map)
static int2 uv_to_cell(const float2 &uv, const int resolution)
T clamp(const T &a, const T &min, const T &max)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
VecBase< float, 3 > float3
#define FLT_MAX
Definition stdcycles.h:14
Map< int, destruct_ptr< LocalRowData > > rows
linear_allocator::ChunkedList< TriWithRange, 8 > tris