Blender V5.0
COM_algorithm_jump_flooding.hh
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#pragma once
6
7#include "COM_context.hh"
8#include "COM_result.hh"
9
10namespace blender::compositor {
11
12/* Computes a jump flooding table from the given input and writes the result to the output. A jump
13 * flooding table computes for each pixel the texel location of the closest "seed pixel". A seed
14 * pixel is a pixel that is marked as such in the input, more on this later. This table is useful
15 * to compute a Voronoi diagram where the centroids are the seed pixels, it can be used to
16 * accurately approximate an euclidean distance transform, finally, it can be used to flood fill
17 * regions of an image.
18 *
19 * The input is expected to be initialized by the initialize_jump_flooding_value function. Seed
20 * pixels should specify true for the is_seed argument, and false otherwise. The texel input should
21 * be the texel location of the pixel. Both the input and output results should be of type
22 * ResultType::Int2.
23 *
24 * To compute a Voronoi diagram, the pixels lying at the centroid of the Voronoi cell should be
25 * marked as seed pixels. To compute an euclidean distance transform of a region or flood fill a
26 * region, the boundary pixels of the region should be marked as seed.
27 *
28 * The algorithm is based on the paper:
29 *
30 * Rong, Guodong, and Tiow-Seng Tan. "Jump flooding in GPU with applications to Voronoi diagram
31 * and distance transform." Proceedings of the 2006 symposium on Interactive 3D graphics and
32 * games. 2006.
33 *
34 * But uses the more accurate 1+JFA variant from the paper:
35 *
36 * Rong, Guodong, and Tiow-Seng Tan. "Variants of jump flooding algorithm for computing discrete
37 * Voronoi diagrams." 4th international symposium on voronoi diagrams in science and engineering
38 * (ISVD 2007). IEEE, 2007.*
39 *
40 * The algorithm is O(log2(n)) per pixel where n is the maximum dimension of the input, it follows
41 * that the execution time is independent of the number of the seed pixels. However, the developer
42 * should try to minimize the number of seed pixels because their number is proportional to the
43 * error of the algorithm as can be seen in "Figure 3: Errors of variants of JFA" in the variants
44 * paper. */
46
47/* A special value that indicates that the pixel has not be flooded yet, and consequently is not a
48 * seed pixel. */
49#define JUMP_FLOODING_NON_FLOODED_VALUE int2(-1)
50
51/* Given the texel location of the closest seed pixel and whether the pixel is flooded, encode that
52 * information in an int2. */
53inline int2 encode_jump_flooding_value(const int2 &closest_seed_texel, const bool is_flooded)
54{
55 return is_flooded ? closest_seed_texel : JUMP_FLOODING_NON_FLOODED_VALUE;
56}
57
58/* Initialize the pixel at the given texel location for the algorithm as being seed or background.
59 * This essentially calls encode_jump_flooding_value with the texel location, because the pixel is
60 * the closest seed to itself. */
61inline int2 initialize_jump_flooding_value(const int2 &texel, const bool is_seed)
62{
63 return encode_jump_flooding_value(texel, is_seed);
64}
65
66} // namespace blender::compositor
#define JUMP_FLOODING_NON_FLOODED_VALUE
#define input
#define output
int2 encode_jump_flooding_value(const int2 &closest_seed_texel, const bool is_flooded)
int2 initialize_jump_flooding_value(const int2 &texel, const bool is_seed)
void jump_flooding(Context &context, Result &input, Result &output)
VecBase< int32_t, 2 > int2