Blender V5.3
grease_pencil_segments_intern.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 "BKE_grease_pencil.hh"
12
14
15/* When looking for intersections, we need a little padding, otherwise we could miss curves
16 * that intersect for the eye, but not in hard numbers. */
17static constexpr int BBOX_PADDING = 2;
18
19enum Side : uint8_t { Start = 0, End = 1 };
20
25class Segment {
26 public:
27 /* Curve index. */
28 int curve = -1;
29
30 /* The start and end of the original curve is stored, because this segment may go past the end
31 * and have to loop. */
33
34 /* Point range of the segment: Starting point and end point. Matches the point offsets
35 * in a CurvesGeometry. */
36 int points[2] = {-1, -1};
37
38 /* If this segment is a full cyclical segment, note that the segment can start and end at some
39 * intersection point. */
40 bool full_wrap_loop = false;
41
42 /* The normalized distance where the trim segment is intersected by another curve.
43 * For the outer ends of the trim segment the intersection distance is given between:
44 * - [start point] and [start point + 1]
45 * - [end point] and [end point + 1]
46 */
47 float intersection_factor[2] = {0.0f, 0.0f};
48
49 int intersection_index[2] = {-1, -1};
50
51 constexpr Segment() = default;
52
53 bool is_loop() const;
54
55 bool has_intersection(Side side) const;
56 int2 edge(Side side) const;
57
58 int wrap_index(int i) const;
59
60 IndexRange point_range() const;
61 int points_num() const;
62
63 template<typename Fn> void foreach_point(Fn &&fn) const;
64
65 static Segment from_curve(int curve_i, IndexRange points, bool cyclic);
66 static Segment from_intersections(int curve_i,
68 const std::optional<int> point_start,
69 const std::optional<int> point_end,
70 const std::optional<float> factor_start,
71 const std::optional<float> factor_end,
72 const std::optional<int> inter_index_start,
73 const std::optional<int> inter_index_end);
74};
75
76template<typename Fn> void Segment::foreach_point(Fn &&fn) const
77{
78 const IndexRange point_range = this->point_range();
79
80 for (const int64_t pos : point_range.index_range()) {
81 const int i = this->wrap_index(point_range[pos]);
82
83 if constexpr (std::is_invocable_r_v<void, Fn, int64_t, int64_t>) {
84 fn(i, pos);
85 }
86 else {
87 fn(i);
88 }
89 }
90}
91
93 int point_i = -1;
94 int point_j = -1;
95 float factor_i = -1.0f;
96 float factor_j = -1.0f;
97 int curve_i = -1;
98 int curve_j = -1;
99
100 int segment_index_i[2] = {-1, -1};
101 int segment_index_j[2] = {-1, -1};
102
103 constexpr IntersectionPoint() = default;
104
105 float point_for_curve(const int curve) const
106 {
107 BLI_assert(curve == curve_i || curve == curve_j);
108 return curve == curve_i ? point_i : point_j;
109 }
110
111 float factor_for_curve(const int curve) const
112 {
113 BLI_assert(curve == curve_i || curve == curve_j);
114 return curve == curve_i ? factor_i : factor_j;
115 }
116
117 float parameter_for_curve(const int curve) const
118 {
119 return this->point_for_curve(curve) + this->factor_for_curve(curve);
120 }
121
122 int other_curve(const int curve) const
123 {
124 BLI_assert(curve == curve_i || curve == curve_j);
125 return curve == curve_i ? curve_j : curve_i;
126 }
127};
128
129IntersectionPoint create_intersection(const int point_i,
130 const int point_j,
131 const float factor_i,
132 const float factor_j,
133 const int curve_i,
134 const int curve_j);
135
138
139/* We store the side as sign, but because a segment with index zero is valid, we shift by one. */
141int decode_index(const EncodedConnection encoded);
142Side decode_side(const EncodedConnection encoded);
143
144/* Both the start and end of every segment is connected to two other segments or null. */
146
147/* Compute bounding boxes of curves in screen space. The bounding boxes are used to speed
148 * up the search for intersecting curves. */
149void compute_bounding_boxes(const OffsetIndices<int> src_points_by_curve,
150 const Span<float2> screen_space_positions,
151 MutableSpan<Bounds<float2>> screen_space_bbox);
152void compute_bounding_boxes(const OffsetIndices<int> src_points_by_curve,
153 const GroupedSpan<int> shapes,
154 const Span<float2> screen_space_positions,
155 MutableSpan<Bounds<float2>> screen_space_bbox);
156
158 const Span<Segment> segments,
159 const Span<bool> segment_reversed,
160 const Span<bool> cyclic,
161 const OffsetIndices<int> segment_offsets);
162
163void find_intersections_between_all_curves(const Span<float2> screen_space_positions,
164 const Span<Bounds<float2>> screen_space_bbox,
165 const OffsetIndices<int> points_by_curve,
166 const VArray<bool> &cyclic,
167 const IndexMask &visible_curves,
168 Array<Vector<int>> &r_inters_per_curves,
169 Vector<IntersectionPoint> &r_intersections);
170
172 MutableSpan<IntersectionPoint> intersections);
174 const Span<Vector<int>> inters_per_curves,
175 const OffsetIndices<int> points_by_curve,
176 const Span<IntersectionPoint> &intersections,
177 const VArray<bool> &cyclic,
178 Vector<Segment> &all_segments);
179void create_segments_from_intersections(const Span<Vector<int>> inters_per_curves,
180 const OffsetIndices<int> points_by_curve,
181 const Span<IntersectionPoint> &intersections,
182 const VArray<bool> &cyclic,
183 Vector<Segment> &all_segments,
184 MutableSpan<int> segments_num_per_curve);
185void create_connections_from_curves(const OffsetIndices<int> segments_by_curve,
186 const Span<bool> segments_to_keep,
187 const VArray<bool> &is_cyclic,
188 MutableSpan<SegmentConnections> segment_connections);
189void follow_segment_connections(const Span<Segment> all_segments,
190 const Span<bool> segments_to_keep,
191 const Span<SegmentConnections> segment_connections,
192 Vector<Segment> &segments,
193 Vector<int> &segment_offset_data,
194 Vector<bool> &segment_reversed,
195 Vector<bool> &cyclic);
196
197} // namespace blender::ed::greasepencil::segment
Low-level operations for grease pencil.
constexpr IndexRange index_range() const
static Segment from_curve(int curve_i, IndexRange points, bool cyclic)
static Segment from_intersections(int curve_i, IndexRange points, const std::optional< int > point_start, const std::optional< int > point_end, const std::optional< float > factor_start, const std::optional< float > factor_end, const std::optional< int > inter_index_start, const std::optional< int > inter_index_end)
void cut_caps(bke::CurvesGeometry &dst, const Span< Segment > segments, const Span< bool > segment_reversed, const Span< bool > cyclic, const OffsetIndices< int > segment_offsets)
EncodedConnection encode_index_and_side(const int index, const Side side)
void compute_bounding_boxes(const OffsetIndices< int > src_points_by_curve, const Span< float2 > screen_space_positions, MutableSpan< Bounds< float2 > > screen_space_bbox)
IntersectionPoint create_intersection(const int point_i, const int point_j, const float factor_i, const float factor_j, const int curve_i, const int curve_j)
void create_segments_from_intersections(const Span< Vector< int > > inters_per_curves, const OffsetIndices< int > points_by_curve, const Span< IntersectionPoint > &intersections, const VArray< bool > &cyclic, Vector< Segment > &all_segments, MutableSpan< int > segments_num_per_curve)
void store_segment_map_on_intersections(const Span< Segment > all_segments, MutableSpan< IntersectionPoint > intersections)
int decode_index(const EncodedConnection encoded)
int create_segments_from_intersections_single_curve(const int curve_k, const Span< Vector< int > > inters_per_curves, const OffsetIndices< int > points_by_curve, const Span< IntersectionPoint > &intersections, const VArray< bool > &cyclic, Vector< Segment > &all_segments)
Side decode_side(const EncodedConnection encoded)
VecBase< EncodedConnection, 2 > SegmentConnections
void follow_segment_connections(const Span< Segment > all_segments, const Span< bool > segments_to_keep, const Span< SegmentConnections > segment_connections, Vector< Segment > &segments, Vector< int > &segment_offset_data, Vector< bool > &segment_reversed, Vector< bool > &cyclic)
void find_intersections_between_all_curves(const Span< float2 > screen_space_positions, const Span< Bounds< float2 > > screen_space_bbox, const OffsetIndices< int > points_by_curve, const VArray< bool > &cyclic, const IndexMask &visible_curves, Array< Vector< int > > &r_inters_per_curves, Vector< IntersectionPoint > &r_intersections)
void create_connections_from_curves(const OffsetIndices< int > segments_by_curve, const Span< bool > segments_to_keep, const VArray< bool > &is_cyclic, MutableSpan< SegmentConnections > segment_connections)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
VecBase< int32_t, 2 > int2
unsigned char uint8_t
Definition BKE_text.h:79