Blender V5.3
fill.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_bounds.hh"
6#include "BLI_color_types.hh"
7#include "BLI_delaunay_2d.hh"
8#include "BLI_enum_flags.hh"
9#include "BLI_index_mask.hh"
10#include "BLI_math_base.hh"
11#include "BLI_math_geom_c.hh"
12#include "BLI_math_matrix.hh"
13#include "BLI_math_vector.hh"
15#include "BLI_offset_indices.hh"
16#include "BLI_stack.hh"
17#include "BLI_task.hh"
18
19#include "BKE_attribute.hh"
20#include "BKE_camera.h"
21#include "BKE_context.hh"
22#include "BKE_crazyspace.hh"
23#include "BKE_curves.hh"
24#include "BKE_grease_pencil.hh"
25#include "BKE_image.hh"
26#include "BKE_lib_id.hh"
27#include "BKE_material.hh"
28#include "BKE_paint.hh"
29
30#include "DNA_brush_types.h"
31#include "DNA_curves_types.h"
33#include "DNA_material_types.h"
34#include "DNA_object_types.h"
35#include "DNA_scene_types.h"
36#include "DNA_view3d_types.h"
37
39
40#include "ED_grease_pencil.hh"
41#include "ED_view3d.hh"
42
43#include "IMB_imbuf.hh"
44#include "IMB_imbuf_types.hh"
45
46#include "GPU_state.hh"
47
49
50#include <list>
51#include <optional>
52#include <queue>
53
55
56/* -------------------------------------------------------------------- */
59
61const ColorGeometry4f draw_seed_color = {0, 1, 0, 1};
62
64 Border = (1 << 0),
65 Stroke = (1 << 1),
66 Fill = (1 << 2),
67 Seed = (1 << 3),
68 Debug = (1 << 7),
69};
71
72
73
74/* -------------------------------------------------------------------- */
77
78/* Utility class for access to pixel buffer data. */
80 private:
81 Image *ima_ = nullptr;
82 ImBuf *ibuf_ = nullptr;
83 void *lock_ = nullptr;
85 int2 size_ = int2(0);
86
87 public:
88 bool has_buffer() const
89 {
90 return ibuf_ != nullptr;
91 }
92
94 {
95 BLI_assert(!this->has_buffer());
96 }
97
98 void acquire(Image &ima)
99 {
100 BLI_assert(!this->has_buffer());
101 ima_ = &ima;
102 ibuf_ = BKE_image_acquire_ibuf(&ima, nullptr, &lock_);
103 size_ = {ibuf_->x, ibuf_->y};
105 reinterpret_cast<ColorGeometry4b *>(ibuf_->byte_data_for_write()), ibuf_->x * ibuf_->y);
106 }
107
108 void release()
109 {
110 BLI_assert(this->has_buffer());
111 BKE_image_release_ibuf(ima_, ibuf_, lock_);
112 lock_ = nullptr;
113 ima_ = nullptr;
114 ibuf_ = nullptr;
115 data_ = {};
116 size_ = int2(0);
117 }
118
119 int2 size() const
120 {
121 return this->size_;
122 }
123
124 int width() const
125 {
126 return this->size_.x;
127 }
128
129 int height() const
130 {
131 return this->size_.y;
132 }
133
134 bool is_valid_coord(const int2 &c) const
135 {
136 return c.x >= 0 && c.x < this->size_.x && c.y >= 0 && c.y < this->size_.y;
137 }
138
139 int2 coord_from_index(const int index) const
140 {
141 const div_t d = div(index, this->size_.x);
142 return int2{d.rem, d.quot};
143 }
144
145 int index_from_coord(const int2 &c) const
146 {
147 return c.x + c.y * this->size_.x;
148 }
149
151 {
152 return this->data_;
153 }
154
156 {
157 return this->data_;
158 }
159
161 {
162 return this->data_[index_from_coord(c)];
163 }
164
165 const ColorGeometry4b &pixel_from_coord(const int2 &c) const
166 {
167 return this->data_[index_from_coord(c)];
168 }
169};
170
171static bool get_flag(const ColorGeometry4b &color, const ColorFlag flag)
172{
173 return (color.r & flag) != 0;
174}
175
176static void set_flag(ColorGeometry4b &color, const ColorFlag flag, bool value)
177{
178 color.r = value ? (color.r | flag) : (color.r & (~flag));
179}
180
181/* Set a border to create image limits. */
182/* TODO this shouldn't be necessary if drawing could accurately save flag values. */
184{
185 for (ColorGeometry4b &color : buffer.pixels()) {
186 const bool is_stroke = color.r > 0.0f;
187 const bool is_seed = color.g > 0.0f;
188 color.r = (is_stroke ? ColorFlag::Stroke : 0) | (is_seed ? ColorFlag::Seed : 0);
189 color.g = 0;
190 color.b = 0;
191 color.a = 0;
192 }
193}
194
195/* Set a border to create image limits. */
197{
198 constexpr const ColorGeometry4b output_stroke_color = {255, 0, 0, 255};
199 constexpr const ColorGeometry4b output_seed_color = {127, 127, 0, 255};
200 constexpr const ColorGeometry4b output_border_color = {0, 0, 255, 255};
201 constexpr const ColorGeometry4b output_fill_color = {127, 255, 0, 255};
202 // constexpr const ColorGeometry4b output_extend_color = {25, 255, 0, 255};
203 // constexpr const ColorGeometry4b output_helper_color = {255, 0, 127, 255};
204 constexpr const ColorGeometry4b output_debug_color = {255, 127, 0, 255};
205
206 auto add_colors = [](const ColorGeometry4b &a, const ColorGeometry4b &b) -> ColorGeometry4b {
207 return ColorGeometry4b(std::min(int(a.r) + int(b.r), 255),
208 std::min(int(a.g) + int(b.g), 255),
209 std::min(int(a.b) + int(b.b), 255),
210 std::min(int(a.a) + int(b.a), 255));
211 };
212
213 for (ColorGeometry4b &color : buffer.pixels()) {
214 ColorGeometry4b output_color = ColorGeometry4b(0, 0, 0, 0);
215 if (color.r & ColorFlag::Debug) {
216 output_color = add_colors(output_color, output_debug_color);
217 }
218 if (color.r & ColorFlag::Fill) {
219 output_color = add_colors(output_color, output_fill_color);
220 }
221 if (color.r & ColorFlag::Stroke) {
222 output_color = add_colors(output_color, output_stroke_color);
223 }
224 if (color.r & ColorFlag::Border) {
225 output_color = add_colors(output_color, output_border_color);
226 }
227 if (color.r & ColorFlag::Seed) {
228 output_color = add_colors(output_color, output_seed_color);
229 }
230 color = std::move(output_color);
231 }
232}
233
234/* Set a border to create image limits. */
236{
237 int row_start = 0;
238 /* Fill first row */
239 for (const int i : IndexRange(buffer.width())) {
240 set_flag(buffer.pixels()[row_start + i], ColorFlag::Border, true);
241 }
242 row_start += buffer.width();
243 /* Fill first and last pixel of middle rows. */
244 for ([[maybe_unused]] const int i : IndexRange(buffer.height()).drop_front(1).drop_back(1)) {
245 set_flag(buffer.pixels()[row_start], ColorFlag::Border, true);
246 set_flag(buffer.pixels()[row_start + buffer.width() - 1], ColorFlag::Border, true);
247 row_start += buffer.width();
248 }
249 /* Fill last row */
250 for (const int i : IndexRange(buffer.width())) {
251 set_flag(buffer.pixels()[row_start + i], ColorFlag::Border, true);
252 }
253}
254
255enum class FillResult {
256 Success,
258};
259
261 /* Cancel when hitting the border, fill failed. */
263 /* Allow border contact, continue with other pixels. */
265};
266
267template<FillBorderMode border_mode>
268FillResult flood_fill(ImageBufferAccessor &buffer, const int leak_filter_width = 0)
269{
270 const MutableSpan<ColorGeometry4b> pixels = buffer.pixels();
271 const int width = buffer.width();
272 const int height = buffer.height();
273
274 Stack<int> active_pixels;
275 /* Initialize the stack with filled pixels (dot at mouse position). */
276 for (const int i : pixels.index_range()) {
277 if (get_flag(pixels[i], ColorFlag::Seed)) {
278 active_pixels.push(i);
279 }
280 }
281
282 enum FilterDirection {
283 Horizontal = 1,
284 Vertical = 2,
285 };
286
287 bool border_contact = false;
288 while (!active_pixels.is_empty()) {
289 const int index = active_pixels.pop();
290 const int2 coord = buffer.coord_from_index(index);
291 ColorGeometry4b pixel_value = buffer.pixels()[index];
292
293 if constexpr (border_mode == FillBorderMode::Cancel) {
294 if (get_flag(pixel_value, ColorFlag::Border)) {
295 border_contact = true;
296 break;
297 }
298 }
299 else if constexpr (border_mode == FillBorderMode::Ignore) {
300 if (get_flag(pixel_value, ColorFlag::Border)) {
301 border_contact = true;
302 }
303 }
304
305 if (get_flag(pixel_value, ColorFlag::Fill)) {
306 /* Pixel already filled. */
307 continue;
308 }
309
310 if (get_flag(pixel_value, ColorFlag::Stroke)) {
311 /* Boundary pixel, ignore. */
312 continue;
313 }
314
315 /* Mark as filled. */
316 set_flag(pixels[index], ColorFlag::Fill, true);
317
318 /* Directional box filtering for gap detection. */
319 const IndexRange filter_x_neg = IndexRange(1, std::min(coord.x, leak_filter_width));
320 const IndexRange filter_x_pos = IndexRange(1,
321 std::min(width - 1 - coord.x, leak_filter_width));
322 const IndexRange filter_y_neg = IndexRange(1, std::min(coord.y, leak_filter_width));
323 const IndexRange filter_y_pos = IndexRange(1,
324 std::min(height - 1 - coord.y, leak_filter_width));
325 bool is_boundary_horizontal = false;
326 bool is_boundary_vertical = false;
327 for (const int filter_i : filter_y_neg) {
328 is_boundary_horizontal |= get_flag(buffer.pixel_from_coord(coord - int2(0, filter_i)),
330 }
331 for (const int filter_i : filter_y_pos) {
332 is_boundary_horizontal |= get_flag(buffer.pixel_from_coord(coord + int2(0, filter_i)),
334 }
335 for (const int filter_i : filter_x_neg) {
336 is_boundary_vertical |= get_flag(buffer.pixel_from_coord(coord - int2(filter_i, 0)),
338 }
339 for (const int filter_i : filter_x_pos) {
340 is_boundary_vertical |= get_flag(buffer.pixel_from_coord(coord + int2(filter_i, 0)),
342 }
343
344 /* Activate neighbors */
345 if (coord.x > 0 && !is_boundary_horizontal) {
346 active_pixels.push(buffer.index_from_coord(coord - int2{1, 0}));
347 }
348 if (coord.x < width - 1 && !is_boundary_horizontal) {
349 active_pixels.push(buffer.index_from_coord(coord + int2{1, 0}));
350 }
351 if (coord.y > 0 && !is_boundary_vertical) {
352 active_pixels.push(buffer.index_from_coord(coord - int2{0, 1}));
353 }
354 if (coord.y < height - 1 && !is_boundary_vertical) {
355 active_pixels.push(buffer.index_from_coord(coord + int2{0, 1}));
356 }
357 }
358
359 return border_contact ? FillResult::BorderContact : FillResult::Success;
360}
361
362/* Turn unfilled areas into filled and vice versa. */
364{
365 for (ColorGeometry4b &color : buffer.pixels()) {
366 const bool is_filled = get_flag(color, ColorFlag::Fill);
367 set_flag(color, ColorFlag::Fill, !is_filled);
368 }
369}
370
371constexpr const int num_directions = 8;
373 {-1, -1},
374 {0, -1},
375 {1, -1},
376 {1, 0},
377 {1, 1},
378 {0, 1},
379 {-1, 1},
380 {-1, 0},
381};
382
383static void dilate(ImageBufferAccessor &buffer, int iterations = 1)
384{
385 const MutableSpan<ColorGeometry4b> pixels = buffer.pixels();
386
387 Stack<int> active_pixels;
388 for ([[maybe_unused]] const int iter : IndexRange(iterations)) {
389 for (const int i : pixels.index_range()) {
390 /* Ignore already filled pixels */
391 if (get_flag(pixels[i], ColorFlag::Fill)) {
392 continue;
393 }
394 const int2 coord = buffer.coord_from_index(i);
395
396 /* Add to stack if any neighbor is filled. */
397 for (const int2 offset : offset_by_direction) {
398 if (buffer.is_valid_coord(coord + offset) &&
399 get_flag(buffer.pixel_from_coord(coord + offset), ColorFlag::Fill))
400 {
401 active_pixels.push(i);
402 }
403 }
404 }
405
406 while (!active_pixels.is_empty()) {
407 const int index = active_pixels.pop();
408 set_flag(buffer.pixels()[index], ColorFlag::Fill, true);
409 }
410 }
411}
412
413static void erode(ImageBufferAccessor &buffer, int iterations = 1)
414{
415 const MutableSpan<ColorGeometry4b> pixels = buffer.pixels();
416
417 Stack<int> active_pixels;
418 for ([[maybe_unused]] const int iter : IndexRange(iterations)) {
419 for (const int i : pixels.index_range()) {
420 /* Ignore empty pixels */
421 if (!get_flag(pixels[i], ColorFlag::Fill)) {
422 continue;
423 }
424 const int2 coord = buffer.coord_from_index(i);
425
426 /* Add to stack if any neighbor is empty. */
427 for (const int2 offset : offset_by_direction) {
428 if (buffer.is_valid_coord(coord + offset) &&
429 !get_flag(buffer.pixel_from_coord(coord + offset), ColorFlag::Fill))
430 {
431 active_pixels.push(i);
432 }
433 }
434 }
435
436 while (!active_pixels.is_empty()) {
437 const int index = active_pixels.pop();
438 set_flag(buffer.pixels()[index], ColorFlag::Fill, false);
439 }
440 }
441}
442
443/* Wrap to valid direction, must be less than 3 * num_directions. */
444static int wrap_dir_3n(const int dir)
445{
446 return dir - num_directions * (int(dir >= num_directions) + int(dir >= 2 * num_directions));
447}
448
450 /* Pixel indices making up boundary curves. */
452 /* Offset index for each curve. */
454};
455
463{
464 using BoundarySection = std::list<int>;
465 using BoundaryStartMap = Map<int, BoundarySection>;
466
467 const Span<ColorGeometry4b> pixels = buffer.pixels();
468 const int width = buffer.width();
469 const int height = buffer.height();
470
471 /* Find possible starting points for boundary sections.
472 * Direction 3 == (1, 0) is the starting direction. */
473 constexpr const uint8_t start_direction = 3;
474 auto find_start_coordinates = [&]() -> BoundaryStartMap {
475 BoundaryStartMap starts;
476 for (const int y : IndexRange(height)) {
477 /* Check for empty pixels next to filled pixels. */
478 for (const int x : IndexRange(width).drop_back(1)) {
479 const int index_left = buffer.index_from_coord({x, y});
480 const int index_right = buffer.index_from_coord({x + 1, y});
481 const bool filled_left = get_flag(pixels[index_left], ColorFlag::Fill);
482 const bool filled_right = get_flag(pixels[index_right], ColorFlag::Fill);
483 const bool border_right = get_flag(pixels[index_right], ColorFlag::Border);
484 if (!filled_left && filled_right && !border_right) {
485 /* Empty index list indicates uninitialized section. */
486 starts.add(index_right, {});
487 }
488 }
489 }
490 return starts;
491 };
492
493 struct NeighborIterator {
494 int index;
495 int direction;
496 };
497
498 /* Find the next filled pixel in clockwise direction from the current. */
499 auto find_next_neighbor = [&](NeighborIterator &iter) -> bool {
500 const int2 iter_coord = buffer.coord_from_index(iter.index);
501 for (const int i : IndexRange(num_directions)) {
502 /* Invert direction (add 4) and start at next direction (add 1..n).
503 * This can not be greater than 3*num_directions-1, wrap accordingly. */
504 const int neighbor_dir = wrap_dir_3n(iter.direction + 5 + i);
505 const int2 neighbor_coord = iter_coord + offset_by_direction[neighbor_dir];
506 if (!buffer.is_valid_coord(neighbor_coord)) {
507 continue;
508 }
509 const int neighbor_index = buffer.index_from_coord(neighbor_coord);
510 /* Border pixels are not valid. */
511 if (get_flag(pixels[neighbor_index], ColorFlag::Border)) {
512 continue;
513 }
514 if (get_flag(pixels[neighbor_index], ColorFlag::Fill)) {
515 iter.index = neighbor_index;
516 iter.direction = neighbor_dir;
517 return true;
518 }
519 }
520 return false;
521 };
522
523 BoundaryStartMap boundary_starts = find_start_coordinates();
524
525 /* Find directions and connectivity for all boundary pixels. */
526 for (const int start_index : boundary_starts.keys()) {
527 /* Boundary map entries may get removed, only handle active starts. */
528 if (!boundary_starts.contains(start_index)) {
529 continue;
530 }
531 BoundarySection &section = boundary_starts.lookup(start_index);
532 section.push_back(start_index);
533 NeighborIterator iter = {start_index, start_direction};
534 while (find_next_neighbor(iter)) {
535 /* Loop closed when arriving at start again. */
536 if (iter.index == start_index) {
537 break;
538 }
539
540 /* Join existing sections. */
541 if (boundary_starts.contains(iter.index)) {
542 BoundarySection &next_section = boundary_starts.lookup(iter.index);
543 if (next_section.empty()) {
544 /* Empty sections are only start indices, remove and continue. */
545 boundary_starts.remove(iter.index);
546 }
547 else {
548 /* Merge existing points into the current section. */
549 section.splice(section.end(), next_section);
550 boundary_starts.remove(iter.index);
551 break;
552 }
553 }
554
555 section.push_back(iter.index);
556 }
557 /* Discard un-closed boundaries. */
558 if (iter.index != start_index) {
559 boundary_starts.remove(start_index);
560 }
561 }
562
563 /* Construct final strokes by tracing the boundary. */
564 FillBoundary final_boundary;
565 for (const BoundarySection &section : boundary_starts.values()) {
566 final_boundary.offset_indices.append(final_boundary.pixels.size());
567 for (const int index : section) {
568 final_boundary.pixels.append(index);
569 }
570 }
571 final_boundary.offset_indices.append(final_boundary.pixels.size());
572
573 return final_boundary;
574}
575
576/* Create curves geometry from boundary positions. */
578 const ImageBufferAccessor &buffer,
579 const ed::greasepencil::DrawingPlacement &placement,
580 const float3x3 &image_to_region)
581{
582 /* Curve cannot have 0 points. */
583 if (boundary.offset_indices.is_empty() || boundary.pixels.is_empty()) {
584 return {};
585 }
586
587 bke::CurvesGeometry curves(boundary.pixels.size(), boundary.offset_indices.size() - 1);
588
589 curves.offsets_for_write().copy_from(boundary.offset_indices);
590 MutableSpan<float3> positions = curves.positions_for_write();
591
592 curves.fill_curve_types(CURVE_TYPE_POLY);
593
594 for (const int point_i : curves.points_range()) {
595 const int pixel_index = boundary.pixels[point_i];
596 const int2 pixel_coord = buffer.coord_from_index(pixel_index);
597 const float2 region_coord =
598 math::transform_point(image_to_region, float3(pixel_coord, 1.0f)).xy();
599 const float3 position = placement.project_with_shift(region_coord);
600 positions[point_i] = position;
601 }
602
603 return curves;
604}
605
607 const Brush &brush,
608 const ed::greasepencil::DrawingPlacement &placement,
609 const float3x3 &image_to_region,
610 const bool invert,
611 const bool output_as_colors)
612{
613 constexpr const int leak_filter_width = 3;
614
615 ImageBufferAccessor buffer;
616 buffer.acquire(ima);
617 BLI_SCOPED_DEFER([&]() {
618 if (output_as_colors) {
619 /* For visual output convert bit flags back to colors. */
621 }
622 buffer.release();
623 });
624
626
627 /* Set red borders to create a external limit. */
628 mark_borders(buffer);
629
630 /* Apply boundary fill */
631 if (invert) {
632 /* When inverted accept border fill, image borders are valid boundaries. */
633 FillResult fill_result = flood_fill<FillBorderMode::Ignore>(buffer, leak_filter_width);
635 return {};
636 }
637 /* Make fills into boundaries and vice versa for finding exterior boundaries. */
638 invert_fill(buffer);
639 }
640 else {
641 /* Cancel when encountering a border, counts as failure. */
642 FillResult fill_result = flood_fill<FillBorderMode::Cancel>(buffer, leak_filter_width);
643 if (fill_result != FillResult::Success) {
644 return {};
645 }
646 }
647
648 const int dilate_pixels = brush.gpencil_settings->dilate_pixels;
649 if (dilate_pixels > 0) {
650 dilate(buffer, dilate_pixels);
651 }
652 else if (dilate_pixels < 0) {
653 erode(buffer, -dilate_pixels);
654 }
655
657
658 return boundary_to_curves(boundary, buffer, placement, image_to_region);
659}
660
662
663constexpr const char *attr_material_index = "material_index";
664constexpr const char *attr_is_fill_guide = ".is_fill_guide";
665
667 const DrawingInfo &info,
668 const bool is_boundary_layer,
669 IndexMaskMemory &memory)
670{
671 const bke::CurvesGeometry &strokes = info.drawing.strokes();
672 const bke::AttributeAccessor attributes = strokes.attributes();
673 const VArray<int> materials = *attributes.lookup_or_default<int>(
675
676 auto is_visible_curve = [&](const int curve_i) {
677 /* Check if stroke can be drawn. */
678 const IndexRange points = strokes.points_by_curve()[curve_i];
679 if (points.size() < 2) {
680 return false;
681 }
682
683 /* Check if the material is visible. */
684 const Material *material = BKE_object_material_get(const_cast<Object *>(&object),
685 materials[curve_i] + 1);
686 const MaterialGPencilStyle *gp_style = material ? material->gp_style : nullptr;
687 const bool is_hidden_material = (gp_style->flag & GP_MATERIAL_HIDE);
688 if (gp_style == nullptr || is_hidden_material) {
689 return false;
690 }
691
692 return true;
693 };
694
695 /* On boundary layers only boundary strokes are rendered. */
696 if (is_boundary_layer) {
697 const VArray<bool> fill_guides = *attributes.lookup_or_default<bool>(
699
700 return IndexMask::from_predicate(strokes.curves_range(), memory, [&](const int curve_i) {
701 if (!is_visible_curve(curve_i)) {
702 return false;
703 }
704 const bool is_boundary_stroke = fill_guides[curve_i];
705 return is_boundary_stroke;
706 });
707 }
708
709 return IndexMask::from_predicate(strokes.curves_range(), memory, is_visible_curve);
710}
711
714 const VArray<float> &opacities,
715 const VArray<int> materials,
716 const ColorGeometry4f &tint_color,
717 const std::optional<float> opacity_threshold)
718{
719 if (!opacity_threshold) {
720 return VArray<ColorGeometry4f>::from_single(tint_color, curves.points_num());
721 }
722
723 Array<ColorGeometry4f> colors(curves.points_num());
724 threading::parallel_for(curves.curves_range(), 512, [&](const IndexRange range) {
725 for (const int curve_i : range) {
726 const Material *material = BKE_object_material_get(const_cast<Object *>(&object),
727 materials[curve_i] + 1);
728 const float material_alpha = material && material->gp_style ?
729 material->gp_style->stroke_rgba[3] :
730 1.0f;
731 const IndexRange points = curves.points_by_curve()[curve_i];
732 for (const int point_i : points) {
733 const float alpha = (material_alpha * opacities[point_i] > *opacity_threshold ? 1.0f :
734 0.0f);
735 colors[point_i] = ColorGeometry4f(tint_color.r, tint_color.g, tint_color.b, alpha);
736 }
737 }
738 });
740}
741
743{
744 /* Initialize maximum bound-box size. */
745 return {float2(0), float2(region.winx, region.winy)};
746}
747
748/* Helper: Calc the maximum bounding box size of strokes to get the zoom level of the viewport.
749 * For each stroke, the 2D projected bounding box is calculated and using this data, the total
750 * object bounding box (all strokes) is calculated. */
751static std::optional<Bounds<float2>> get_boundary_bounds(const ARegion &region,
752 const RegionView3D &rv3d,
753 const Object &object,
754 const Object &object_eval,
755 const VArray<bool> &boundary_layers,
756 const Span<DrawingInfo> src_drawings)
757{
760
761 std::optional<Bounds<float2>> boundary_bounds;
762
763 BLI_assert(object.type == OB_GREASE_PENCIL);
764 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
765
766 BLI_assert(grease_pencil.has_active_layer());
767
768 for (const DrawingInfo &info : src_drawings) {
769 const Layer &layer = *grease_pencil.layers()[info.layer_index];
770 const float4x4 layer_to_world = layer.to_world_space(object);
771 const bke::crazyspace::GeometryDeformation deformation =
773 &object_eval, object, info.drawing);
774 const bool only_boundary_strokes = boundary_layers[info.layer_index];
775 const VArray<float> radii = info.drawing.radii();
776 const bke::CurvesGeometry &strokes = info.drawing.strokes();
777 const bke::AttributeAccessor attributes = strokes.attributes();
778 const VArray<int> materials = *attributes.lookup_or_default<int>(
780 const VArray<bool> is_boundary_stroke = *attributes.lookup_or_default<bool>(
782
783 IndexMaskMemory curve_mask_memory;
784 const IndexMask curve_mask = get_visible_boundary_strokes(
785 object, info, only_boundary_strokes, curve_mask_memory);
786
787 curve_mask.foreach_index(
788 [&](const int curve_i) {
789 const IndexRange points = strokes.points_by_curve()[curve_i];
790 /* Check if stroke can be drawn. */
791 if (points.size() < 2) {
792 return;
793 }
794 /* Check if the color is visible. */
795 const int material_index = materials[curve_i];
796 Material *mat = BKE_object_material_get(const_cast<Object *>(&object),
797 material_index + 1);
798 if (mat == nullptr || (mat->gp_style->flag & GP_MATERIAL_HIDE)) {
799 return;
800 }
801
802 /* In boundary layers only boundary strokes should be rendered. */
803 if (only_boundary_strokes && !is_boundary_stroke[curve_i]) {
804 return;
805 }
806
807 for (const int point_i : points) {
808 const float3 pos_world = math::transform_point(layer_to_world,
809 deformation.positions[point_i]);
810 float2 pos_view;
812 &region, pos_world, pos_view, V3D_PROJ_TEST_NOP);
813 if (result == V3D_PROJ_RET_OK) {
814 const float pixels = radii[point_i] / ED_view3d_pixel_size(&rv3d, pos_world);
815 Bounds<float2> point_bounds = {pos_view - float2(pixels), pos_view + float2(pixels)};
816 boundary_bounds = bounds::merge(boundary_bounds, {point_bounds});
817 }
818 }
819 },
821 }
822
823 return boundary_bounds;
824}
825
826static auto fit_strokes_to_view(const ViewContext &view_context,
827 const VArray<bool> &boundary_layers,
828 const Span<DrawingInfo> src_drawings,
829 const FillToolFitMethod fit_method,
830 const float2 fill_point,
831 const bool uniform_zoom,
832 const float max_zoom_factor,
833 const float2 margin,
834 const float pixel_scale)
835{
836 BLI_assert(max_zoom_factor >= 1.0f);
837 const float min_zoom_factor = math::safe_rcp(max_zoom_factor);
838 /* These values are copied from GPv2. */
839 const int2 min_image_size = int2(128, 128);
840
841 switch (fit_method) {
843 return std::make_tuple(float2(1.0f), float2(0.0f), min_image_size, float3x3::identity());
844
846 const Object &object_eval = *DEG_get_evaluated(view_context.depsgraph, view_context.obact);
847 /* Zoom and offset based on bounds, to fit all strokes within the render. */
848 const std::optional<Bounds<float2>> boundary_bounds = get_boundary_bounds(
849 *view_context.region,
850 *view_context.rv3d,
851 *view_context.obact,
852 object_eval,
853 boundary_layers,
854 src_drawings);
855 if (!boundary_bounds) {
856 return std::make_tuple(float2(1.0f), float2(0.0f), min_image_size, float3x3::identity());
857 }
858
859 /* Include fill point for computing zoom. */
860 const Bounds<float2> fill_bounds = [&]() {
861 Bounds<float2> result = bounds::merge(*boundary_bounds, Bounds<float2>(fill_point));
862 result.pad(margin);
863 return result;
864 }();
865
866 const Bounds<float2> region_bounds = get_region_bounds(*view_context.region);
867 const int2 image_size = math::max(int2(region_bounds.size() * pixel_scale), min_image_size);
868 const float2 zoom_factors = math::clamp(
869 math::safe_divide(fill_bounds.size(), region_bounds.size()),
870 float2(min_zoom_factor),
871 float2(max_zoom_factor));
872 /* Use the most zoomed out factor for uniform scale. */
873 const float2 zoom = uniform_zoom ? float2(math::reduce_max(zoom_factors)) : zoom_factors;
874
875 /* Actual rendered bounds based on the final zoom factor. */
876 const Bounds<float2> render_bounds = {
877 fill_bounds.center() - 0.5f * region_bounds.size() * zoom.x,
878 fill_bounds.center() + 0.5f * region_bounds.size() * zoom.y};
879
880 /* Center offset for View3d matrices (strokes to pixels). */
881 const float2 offset = math::safe_divide(render_bounds.center() - region_bounds.center(),
882 region_bounds.size());
883 /* Corner offset for boundary transform (pixels to strokes). */
884 const float3x3 image_to_region = math::from_loc_scale<float3x3>(
885 render_bounds.min - region_bounds.min, zoom * math::safe_rcp(pixel_scale));
886
887 return std::make_tuple(zoom, offset, image_size, image_to_region);
888 }
889 }
890
891 return std::make_tuple(float2(1.0f), float2(0.0f), min_image_size, float3x3::identity());
892}
893
894static Image *render_strokes(const ViewContext &view_context,
895 const Brush &brush,
896 const Scene &scene,
898 const VArray<bool> &boundary_layers,
899 const Span<DrawingInfo> src_drawings,
900 const int2 &image_size,
901 const std::optional<float> opacity_threshold,
902 const float2 &fill_point,
903 const ExtensionData &extensions,
904 const ed::greasepencil::DrawingPlacement &placement,
905 const float2 &zoom,
906 const float2 &offset)
907{
909
910 ARegion &region = *view_context.region;
911 RegionView3D &rv3d = *view_context.rv3d;
912 Object &object = *view_context.obact;
913
914 BLI_assert(object.type == OB_GREASE_PENCIL);
915 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
916
917 /* Scale stroke radius by half to hide gaps between filled areas and boundaries. */
918 const float radius_scale = (brush.gpencil_settings->fill_draw_mode == GP_FILL_DMODE_CONTROL) ?
919 0.0f :
920 0.5f;
921
922 /* Transform mouse coordinates into layer space for rendering alongside strokes. */
923 const float3 fill_point_layer = placement.project(fill_point);
924
925 /* Region size is used for DrawingPlacement projection. */
926 image_render::RegionViewData region_view_data = image_render::region_init(region, image_size);
927 /* Make sure the region is reset on exit. */
928 BLI_SCOPED_DEFER([&]() { image_render::region_reset(region, region_view_data); });
929
930 GPUOffScreen *offscreen_buffer = image_render::image_render_begin(image_size);
931 if (offscreen_buffer == nullptr) {
932 return {};
933 }
934
935 const bool use_xray = false;
936
937 const float4x4 layer_to_world = layer.to_world_space(object);
938 const float4x4 world_to_view = float4x4(rv3d.viewmat);
939 const float4x4 layer_to_view = world_to_view * layer_to_world;
940
942 GPU_depth_mask(true);
943 image_render::compute_view_matrices(view_context, scene, image_size, zoom, offset);
945
946 /* Draw blue point where click with mouse. */
947 const float mouse_dot_size = 4.0f;
948 image_render::draw_dot(layer_to_view, fill_point_layer, mouse_dot_size, draw_seed_color);
949
950 for (const DrawingInfo &info : src_drawings) {
951 const Layer &layer = *grease_pencil.layers()[info.layer_index];
952 if (!layer.is_visible()) {
953 continue;
954 }
955 const float4x4 layer_to_world = layer.to_world_space(object);
956 const bool is_boundary_layer = boundary_layers[info.layer_index];
957 const bke::CurvesGeometry &strokes = info.drawing.strokes();
958 const bke::AttributeAccessor attributes = strokes.attributes();
959 const VArray<float> opacities = info.drawing.opacities();
960 const VArray<int> materials = *attributes.lookup_or_default<int>(
962
963 IndexMaskMemory curve_mask_memory;
964 const IndexMask curve_mask = get_visible_boundary_strokes(
965 object, info, is_boundary_layer, curve_mask_memory);
966
967 const VArray<ColorGeometry4f> stroke_colors = get_stroke_colors(object,
968 info.drawing.strokes(),
969 opacities,
970 materials,
972 opacity_threshold);
973
975 image_size,
976 object,
977 info.drawing,
978 layer_to_world,
979 curve_mask,
980 stroke_colors,
981 use_xray,
982 radius_scale);
983
984 /* Note: extension data is already in world space, only apply world-to-view transform here. */
985
986 const IndexRange lines_range = extensions.lines.starts.index_range();
987 if (!lines_range.is_empty()) {
989 draw_boundary_color, lines_range.size());
990 const float line_width = 1.0f;
991
992 image_render::draw_lines(world_to_view,
993 lines_range,
994 extensions.lines.starts,
995 extensions.lines.ends,
996 line_colors,
997 line_width);
998 }
999 }
1000
1002 GPU_depth_mask(false);
1004
1005 return image_render::image_render_end(*view_context.bmain, offscreen_buffer);
1006}
1007
1009 const Brush &brush,
1010 const Scene &scene,
1012 const VArray<bool> &boundary_layers,
1013 const Span<DrawingInfo> src_drawings,
1014 const bool invert,
1015 const std::optional<float> opacity_threshold,
1016 const float2 &fill_point,
1017 const ExtensionData &extensions,
1018 const FillToolFitMethod fit_method,
1019 const bool keep_images)
1020{
1021 ARegion &region = *view_context.region;
1022 View3D &view3d = *view_context.v3d;
1023 Depsgraph &depsgraph = *view_context.depsgraph;
1024 Object &object = *view_context.obact;
1025
1026 BLI_assert(object.type == OB_GREASE_PENCIL);
1027 const Object &object_eval = *DEG_get_evaluated(&depsgraph, &object);
1028
1029 /* Zoom and offset based on bounds, to fit all strokes within the render. */
1030 const bool uniform_zoom = true;
1031 const float max_zoom_factor = 5.0f;
1032 const float2 margin = float2(20);
1033 /* Pixel scale (aka. "fill_factor", aka. "Precision") to reduce image size. */
1034 const float pixel_scale = brush.gpencil_settings->fill_factor;
1035 const auto [zoom, offset, image_size, image_to_region] = fit_strokes_to_view(view_context,
1036 boundary_layers,
1037 src_drawings,
1038 fit_method,
1039 fill_point,
1040 uniform_zoom,
1041 max_zoom_factor,
1042 margin,
1043 pixel_scale);
1044
1045 ed::greasepencil::DrawingPlacement placement(scene, region, view3d, object_eval, &layer);
1046 if (placement.use_project_to_surface() || placement.use_project_to_stroke()) {
1047 placement.cache_viewport_depths(&depsgraph, &region, &view3d);
1048 }
1049
1050 Image *ima = render_strokes(view_context,
1051 brush,
1052 scene,
1053 layer,
1054 boundary_layers,
1055 src_drawings,
1056 image_size,
1057 opacity_threshold,
1058 fill_point,
1059 extensions,
1060 placement,
1061 zoom,
1062 offset);
1063 if (!ima) {
1064 return {};
1065 }
1066
1067 bke::CurvesGeometry fill_curves = process_image(
1068 *ima, brush, placement, image_to_region, invert, keep_images);
1069
1070 if (!keep_images) {
1071 BKE_id_free(view_context.bmain, ima);
1072 }
1073
1074 return fill_curves;
1075}
1076
1077enum Side : uint8_t { Start = 0, End = 1 };
1078
1081
1082/* We store the side as sign, but because a segment with index zero is valid, we shift by one. */
1084{
1085 return side == Side::Start ? index + 1 : -(index + 1);
1086}
1087
1088static int decode_index(const EncodedConnection encoded)
1089{
1090 return math::abs(encoded) - 1;
1091}
1092
1094{
1095 return encoded < 0 ? Side::End : Side::Start;
1096}
1097
1098/* Both the start and end of every segment is connected to two other edges or null. */
1100
1101constexpr int NULL_INDEX = -1;
1102
1108
1110 const Span<bool> edges_to_keep,
1111 const Span<EdgeConnections> edge_connections)
1112{
1113 BLI_assert(all_edges.size() == edges_to_keep.size());
1114 BLI_assert(all_edges.size() == edge_connections.size());
1115
1116 EdgeCurves edge_curves;
1117 edge_curves.offset_data.append(0);
1118
1119 Array<bool> processed_edges(all_edges.size(), false);
1120 int start_edge = 0;
1121
1122 auto get_next_unprocessed_edge = [&]() {
1123 /* All segment before `start_edge` are guaranteed to be processed, so skip search them.
1124 * This optimization make the algorithm `O(N)` instead of `O(N^2)`.*/
1125 const int empty_num = start_edge;
1126 const int first_segment = processed_edges.as_span().drop_front(empty_num).first_index_try(
1127 false);
1128
1129 if (first_segment == -1) {
1130 return NULL_INDEX;
1131 }
1132 return first_segment + empty_num;
1133 };
1134
1135 /* Mark all edges that are not to keep as processed. */
1136 for (const int seg_i : all_edges.index_range()) {
1137 if (!edges_to_keep[seg_i]) {
1138 processed_edges[seg_i] = true;
1139 }
1140 }
1141
1142 start_edge = get_next_unprocessed_edge();
1143
1144 /* Follow each segment until it loops or ends. */
1145 while (start_edge != NULL_INDEX) {
1146 bool current_backwards = false;
1147 int current_i = start_edge;
1148 const int first_segment = current_i;
1149
1150 /* Loop through forwards, adding edges until ending or looping. */
1151 bool curve_done = false;
1152 while (!curve_done) {
1153 if (processed_edges[current_i] == true) {
1155 break;
1156 }
1157
1158 const int current_edge = all_edges[current_i];
1159 processed_edges[current_i] = true;
1160 edge_curves.edges.append(current_edge);
1161 edge_curves.reversed.append(current_backwards);
1162
1163 const EncodedConnection next_encoded =
1164 edge_connections[current_i][current_backwards ? Side::Start : Side::End];
1165
1166 if (next_encoded == EDGE_CONNECTION_NULL) {
1167 curve_done = true;
1168 break;
1169 }
1170
1171 const int next_segment = decode_index(next_encoded);
1172 const Side next_side = decode_side(next_encoded);
1173
1174 /* Check if we are back to the start. */
1175 if (next_segment == first_segment) {
1176 curve_done = true;
1177
1178 BLI_assert(next_side == Side::Start);
1179 break;
1180 }
1181
1182 BLI_assert(edges_to_keep[next_segment]);
1183 BLI_assert(!processed_edges[next_segment]);
1184
1185 current_i = next_segment;
1186 current_backwards = next_side == Side::End;
1187 }
1188
1189 edge_curves.offset_data.append(edge_curves.edges.size());
1190
1191 start_edge = get_next_unprocessed_edge();
1192 }
1193
1194 return edge_curves;
1195}
1196
1198{
1199 Array<int3> tri_edges(tris.size(), int3(NULL_INDEX));
1200
1201 Map<OrderedEdge, int> edge_to_index;
1202 for (const int edge_index : edges.index_range()) {
1203 edge_to_index.add_new(edges[edge_index], edge_index);
1204 }
1205
1206 threading::parallel_for(tris.index_range(), 512, [&](const IndexRange range) {
1207 for (const int64_t tri_index : range) {
1208 const Vector<int> &face = tris[tri_index];
1209 tri_edges[tri_index] = int3(edge_to_index.lookup(OrderedEdge(face[0], face[1])),
1210 edge_to_index.lookup(OrderedEdge(face[1], face[2])),
1211 edge_to_index.lookup(OrderedEdge(face[2], face[0])));
1212 }
1213 });
1214
1215 return tri_edges;
1216}
1217
1218static Array<int3> get_all_triangle_adjacency(const int num_edges,
1219 const Span<Vector<int>> tris,
1220 const Span<int3> tri_edges)
1221{
1222 Array<int3> tri_adjacency(tris.size(), int3(NULL_INDEX));
1223
1224 Array<std::pair<int, int>> edge_to_tris(num_edges, std::pair<int, int>(NULL_INDEX, NULL_INDEX));
1225
1226 for (const int tri_index : tris.index_range()) {
1227 for (const int j : IndexRange(3)) {
1228 const int edge_index = tri_edges[tri_index][j];
1229
1230 BLI_assert(edge_index != NULL_INDEX);
1231
1232 if (edge_to_tris[edge_index].first == NULL_INDEX) {
1233 edge_to_tris[edge_index].first = tri_index;
1234 }
1235 else {
1236 edge_to_tris[edge_index].second = tri_index;
1237 }
1238 }
1239 }
1240
1241 threading::parallel_for(tris.index_range(), 512, [&](const IndexRange range) {
1242 for (const int64_t tri_index : range) {
1243 for (const int j : IndexRange(3)) {
1244 const int edge = tri_edges[tri_index][j];
1245
1246 const int index_0 = edge_to_tris[edge].first;
1247 if (!ELEM(index_0, tri_index, NULL_INDEX)) {
1248 tri_adjacency[tri_index][j] = index_0;
1249 continue;
1250 }
1251
1252 const int index_1 = edge_to_tris[edge].second;
1253 if (!ELEM(index_1, tri_index, NULL_INDEX)) {
1254 tri_adjacency[tri_index][j] = index_1;
1255 continue;
1256 }
1257
1258 tri_adjacency[tri_index][j] = NULL_INDEX;
1259 }
1260 }
1261 });
1262
1263 return tri_adjacency;
1264}
1265
1267{
1268 Array<float> edge_weights(edges.size());
1269
1270 threading::parallel_for(edges.index_range(), 512, [&](const IndexRange range) {
1271 for (const int64_t edge_index : range) {
1272 const int2 &edge = edges[edge_index];
1273 const double2 &v1 = verts[edge[0]];
1274 const double2 &v2 = verts[edge[1]];
1275 edge_weights[edge_index] = math::distance(v1, v2);
1276 }
1277 });
1278
1279 return edge_weights;
1280}
1281
1282static Array<float> get_tri_max_weight(const int num_tris,
1283 const Span<int3> tri_adjacency,
1284 const Span<int3> tri_edges,
1285 const Span<float> edge_weights,
1286 const Span<bool> is_source_edge)
1287{
1288 Array<float> tri_max_weight(num_tris, 0.0f);
1289
1290 threading::parallel_for(IndexRange(num_tris), 512, [&](const IndexRange range) {
1291 for (const int64_t tri_index : range) {
1292 for (const int j : IndexRange(3)) {
1293 const int next_tri = tri_adjacency[tri_index][j];
1294 const int edge_index = tri_edges[tri_index][j];
1295
1296 if (next_tri == NULL_INDEX) {
1297 continue;
1298 }
1299
1300 if (is_source_edge[edge_index]) {
1301 continue;
1302 }
1303
1304 tri_max_weight[tri_index] = math::max(tri_max_weight[tri_index], edge_weights[edge_index]);
1305 }
1306 }
1307 });
1308
1309 return tri_max_weight;
1310}
1311
1312static void add_weights_for_tri(const Span<int3> tri_adjacency,
1313 const Span<int3> tri_edges,
1314 const Span<float> edge_weights,
1315 const Span<float> tri_max_weight,
1316 const Span<bool> is_source_edge,
1317 const int hint_tri_index,
1318 const int hint_index,
1319 MutableSpan<int> r_tri_hint_index,
1320 MutableSpan<float> r_tri_weights)
1321{
1322 r_tri_hint_index[hint_tri_index] = hint_index;
1323 r_tri_weights[hint_tri_index] = tri_max_weight[hint_tri_index];
1324
1325 std::queue<int> tris_to_check;
1326
1327 tris_to_check.push(hint_tri_index);
1328
1329 while (!tris_to_check.empty()) {
1330 const int tri_index = tris_to_check.front();
1331 tris_to_check.pop();
1332
1333 for (const int j : IndexRange(3)) {
1334 const int next_tri = tri_adjacency[tri_index][j];
1335 const int edge_index = tri_edges[tri_index][j];
1336
1337 if (next_tri == NULL_INDEX) {
1338 continue;
1339 }
1340 if (is_source_edge[edge_index]) {
1341 continue;
1342 }
1343
1344 const float weight = std::min(edge_weights[edge_index], r_tri_weights[tri_index]);
1345
1346 const float next_tri_weight = r_tri_weights[next_tri];
1347 const int next_tri_hint = r_tri_hint_index[next_tri];
1348 if (weight > next_tri_weight || (weight == next_tri_weight && next_tri_hint != hint_index)) {
1349 tris_to_check.push(next_tri);
1350 r_tri_hint_index[next_tri] = hint_index;
1351 r_tri_weights[next_tri] = weight;
1352 }
1353 }
1354 }
1355}
1356
1357static std::optional<meshintersect::CDT_input<double>> get_input_from_drawings(
1358 const Span<DrawingInfo> src_drawings,
1359 const Object &object,
1360 const VArray<bool> &boundary_layers,
1361 const ARegion &region,
1362 const std::optional<float> opacity_threshold,
1363 Array<double2> &r_cdt_verts,
1364 Array<int2> &r_cdt_edges)
1365{
1368
1369 BLI_assert(object.type == OB_GREASE_PENCIL);
1370 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(object.data);
1371 BLI_assert(grease_pencil.has_active_layer());
1372
1373 Array<Vector<double2>> drawing_input_verts(src_drawings.size());
1374 Array<Vector<OrderedEdge>> drawing_input_edges(src_drawings.size());
1375
1376 threading::parallel_for(src_drawings.index_range(), 1, [&](const IndexRange range) {
1377 for (const int drawing_i : range) {
1378 const DrawingInfo &info = src_drawings[drawing_i];
1379
1380 const Layer &layer = *grease_pencil.layers()[info.layer_index];
1381 const float4x4 layer_to_world = layer.to_world_space(object);
1382 const bke::CurvesGeometry &curves = info.drawing.strokes();
1383 const Span<float3> evaluated_positions = curves.evaluated_positions();
1384 const OffsetIndices<int> eval_points_by_curve = curves.evaluated_points_by_curve();
1385 const bool only_boundary_strokes = boundary_layers[info.layer_index];
1386 const VArray<float> radii = info.drawing.radii();
1387 const VArray<float> opacities = info.drawing.opacities();
1388
1389 Array<float> opacities_array(curves.points_num());
1390 array_utils::copy(opacities, opacities_array.as_mutable_span());
1391 curves.ensure_can_interpolate_to_evaluated();
1392
1393 Array<float> eval_opacities(eval_points_by_curve.total_size());
1394 curves.interpolate_to_evaluated(opacities_array.as_span(), eval_opacities.as_mutable_span());
1395
1396 const bke::AttributeAccessor attributes = curves.attributes();
1397 const VArray<bool> cyclic = curves.cyclic();
1398 const VArray<int> materials = *attributes.lookup_or_default<int>(
1399 attr_material_index, bke::AttrDomain::Curve, 0);
1400 const VArray<bool> is_boundary_stroke = *attributes.lookup_or_default<bool>(
1401 attr_is_fill_guide, bke::AttrDomain::Curve, false);
1402 const VArray<int> fill_id = *attributes.lookup_or_default<int>(
1403 "fill_id", bke::AttrDomain::Curve, 0);
1404 const VArray<int> hide_stroke = *attributes.lookup_or_default<int>(
1405 "hide_stroke", bke::AttrDomain::Curve, 0);
1406 const VArray<float> fill_opacities = *attributes.lookup_or_default<float>(
1407 "fill_opacity", bke::AttrDomain::Curve, 1.0f);
1408
1409 IndexMaskMemory curve_mask_memory;
1410 const IndexMask curve_mask = get_visible_boundary_strokes(
1411 object, info, only_boundary_strokes, curve_mask_memory);
1412
1413 curve_mask.foreach_index([&](const int curve_i) {
1414 const IndexRange eval_points = eval_points_by_curve[curve_i];
1415 const bool is_fill = fill_id[curve_i];
1416 const bool is_stroke_hidden = hide_stroke[curve_i];
1417 const bool is_cyclic = cyclic[curve_i] || is_fill;
1418 /* Check if stroke can be drawn. */
1419 if (eval_points.size() < 2) {
1420 return;
1421 }
1422
1423 /* Check if the color is visible. */
1424 const int material_index = materials[curve_i];
1425 Material *mat = BKE_object_material_get(const_cast<Object *>(&object), material_index + 1);
1426 if (mat == nullptr || (mat->gp_style->flag & GP_MATERIAL_HIDE)) {
1427 return;
1428 }
1429 const float material_stroke_alpha = mat->gp_style->stroke_rgba[3];
1430 const float material_fill_alpha = mat->gp_style->fill_rgba[3];
1431
1432 /* In boundary layers only boundary strokes should be rendered. */
1433 if (only_boundary_strokes && !is_boundary_stroke[curve_i]) {
1434 return;
1435 }
1436
1437 if (is_fill && is_stroke_hidden) {
1438 /* Skip transparent curves. */
1439 if (opacity_threshold &&
1440 (material_fill_alpha * fill_opacities[curve_i] < *opacity_threshold))
1441 {
1442 return;
1443 }
1444 }
1445
1446 const int first_point_offset = drawing_input_verts[drawing_i].size();
1447 int point_offset = first_point_offset;
1448 Array<bool> is_point_visible(eval_points.size(), false);
1449 for (const int point_i : eval_points) {
1450 if (!is_fill) {
1451 /* Skip transparent points. */
1452 if (opacity_threshold &&
1453 (material_stroke_alpha * eval_opacities[point_i] < *opacity_threshold))
1454 {
1455 continue;
1456 }
1457 }
1458
1459 const float3 pos_world = math::transform_point(layer_to_world,
1460 evaluated_positions[point_i]);
1461 float2 pos_view;
1462 eV3DProjStatus result = ED_view3d_project_float_global(
1463 &region, pos_world, pos_view, V3D_PROJ_TEST_NOP);
1464 if (result == V3D_PROJ_RET_OK) {
1465 drawing_input_verts[drawing_i].append(double2(pos_view));
1466 is_point_visible[point_i - eval_points.first()] = true;
1467 }
1468 }
1469
1470 for (const int local_i : eval_points.index_range().drop_back(is_cyclic ? 0 : 1)) {
1471 const int local_next = (local_i + 1) % eval_points.size();
1472 const int point_offset_next = local_next == local_i + 1 ? (point_offset + 1) :
1473 first_point_offset;
1474 if (is_point_visible[local_i]) {
1475 if (is_point_visible[local_next]) {
1476 drawing_input_edges[drawing_i].append(OrderedEdge(point_offset, point_offset_next));
1477 }
1478 point_offset++;
1479 }
1480 }
1481 });
1482 }
1483 });
1484
1485 Array<int> drawing_vert_offset_data(src_drawings.size() + 1);
1486 Array<int> drawing_edge_offset_data(src_drawings.size() + 1);
1487
1488 threading::parallel_for(src_drawings.index_range(), 512, [&](const IndexRange range) {
1489 for (const int i : range) {
1490 drawing_vert_offset_data[i] = drawing_input_verts[i].size();
1491 drawing_edge_offset_data[i] = drawing_input_edges[i].size();
1492 }
1493 });
1494
1496 drawing_vert_offset_data);
1498 drawing_edge_offset_data);
1499
1500 if (drawing_vert_offsets.total_size() == 0) {
1501 return std::nullopt;
1502 }
1503
1504 /* Four points are added for the bounding box. */
1505 r_cdt_verts = Array<double2>(drawing_vert_offsets.total_size() + 4);
1506 r_cdt_edges = Array<int2>(drawing_edge_offsets.total_size());
1507
1508 meshintersect::CDT_input<double> input;
1509 input.needed_ids = CDT_ORIG_EDGES | CDT_ORIG_VERTS;
1510 input.vert = r_cdt_verts;
1511 input.edge = r_cdt_edges;
1512
1513 MutableSpan<double2> verts_span = r_cdt_verts.as_mutable_span();
1514 threading::parallel_for(drawing_input_verts.index_range(), 512, [&](const IndexRange range) {
1515 for (const int drawing_i : range) {
1516 const IndexRange drawing_range = drawing_vert_offsets[drawing_i];
1517 array_utils::copy(drawing_input_verts[drawing_i].as_span(), verts_span.slice(drawing_range));
1518 }
1519 });
1520
1521 MutableSpan<int2> edges_span = r_cdt_edges.as_mutable_span();
1522 threading::parallel_for(drawing_input_edges.index_range(), 512, [&](const IndexRange range) {
1523 for (const int drawing_i : range) {
1524 const IndexRange drawing_range = drawing_edge_offsets[drawing_i];
1525 MutableSpan<int2> edges_slice = edges_span.slice(drawing_range);
1526 array_utils::copy(drawing_input_edges[drawing_i].as_span().cast<int2>(), edges_slice);
1527 const IndexRange vert_range = drawing_vert_offsets[drawing_i];
1528 if (vert_range.is_empty()) {
1529 continue;
1530 }
1531 const int vert_offset = vert_range.first();
1532 for (const int edge_i : drawing_range.index_range()) {
1533 edges_slice[edge_i] = int2(edges_slice[edge_i][0] + vert_offset,
1534 edges_slice[edge_i][1] + vert_offset);
1535 }
1536 }
1537 });
1538
1539 Bounds<double2> bound = *bounds::min_max(r_cdt_verts.as_span().drop_back(4));
1540
1541 /* Pad by enough that all edges connected to the boundary are longer than any edge inside the
1542 * shape. */
1543 bound.pad(math::max(bound.size().x, bound.size().y) * 1.1f);
1544
1545 const std::array<double2, 4> corners = bounds::corners(bound);
1546 r_cdt_verts.as_mutable_span().take_back(4).copy_from(corners);
1547
1548 return input;
1549}
1550
1551static std::optional<EdgeCurves> create_connected_edges_from_fill(const Span<bool> tri_to_fill,
1552 const bool invert,
1553 const Span<int3> tri_adjacency,
1554 const Span<int3> tri_edges,
1555 const Span<int2> edges)
1556{
1557 Set<int> boundary_edges;
1558
1559 for (const int tri_index : tri_to_fill.index_range()) {
1560 if (!tri_to_fill[tri_index]) {
1561 continue;
1562 }
1563
1564 for (const int j : IndexRange(3)) {
1565 const int next_tri = tri_adjacency[tri_index][j];
1566 const int edge_index = tri_edges[tri_index][j];
1567
1568 if (next_tri == NULL_INDEX) {
1569 if (!invert) {
1570 /* Return no geometry if we try to fill all of space. */
1571 return std::nullopt;
1572 }
1573 /* When inverting just skip the edge without returning. */
1574 continue;
1575 }
1576
1577 if (tri_to_fill[next_tri]) {
1578 continue;
1579 }
1580
1581 boundary_edges.add_new(edge_index);
1582 }
1583 }
1584
1586
1587 Array<int> all_edges(edges.size());
1588 Array<bool> edges_to_keep(edges.size(), false);
1590
1591 for (const int edge_index : boundary_edges) {
1592 edges_to_keep[edge_index] = true;
1593 }
1594
1595 auto connect = [&](const EncodedConnection point_1, const EncodedConnection point_2) {
1596 edge_connections[decode_index(point_1)][decode_side(point_1)] = encode_index_and_side(
1597 decode_index(point_2), decode_side(point_2));
1598 edge_connections[decode_index(point_2)][decode_side(point_2)] = encode_index_and_side(
1599 decode_index(point_1), decode_side(point_1));
1600 };
1601
1602 MultiValueMap<int, EncodedConnection> vert_to_edge_ends;
1603
1604 for (const int edge_index : boundary_edges) {
1605 const int2 edge = edges[edge_index];
1606
1607 const EncodedConnection point_1 = encode_index_and_side(edge_index, Side::Start);
1608 const EncodedConnection point_2 = encode_index_and_side(edge_index, Side::End);
1609
1610 vert_to_edge_ends.add(edge[0], point_1);
1611 vert_to_edge_ends.add(edge[1], point_2);
1612 }
1613
1614 for (Span<EncodedConnection> edge_ends : vert_to_edge_ends.values()) {
1615 BLI_assert(edge_ends.size() % 2 == 0);
1616 for (const int edge_pair_index : IndexRange(edge_ends.size() / 2)) {
1617 const EncodedConnection end_1 = edge_ends[edge_pair_index * 2];
1618 const EncodedConnection end_2 = edge_ends[edge_pair_index * 2 + 1];
1619
1620 connect(end_1, end_2);
1621 }
1622 }
1623
1624 return std::make_optional(follow_edge_connections(all_edges, edges_to_keep, edge_connections));
1625}
1626
1627std::optional<bke::CurvesGeometry> delaunay_fill_strokes(
1628 const ViewContext &view_context,
1629 const Scene &scene,
1631 const VArray<bool> &boundary_layers,
1632 const Span<DrawingInfo> src_drawings,
1633 const bool invert,
1634 const std::optional<float> opacity_threshold,
1635 const bool internal_gaps,
1636 const float gap_factor,
1637 const GroupedSpan<float2> &fill_points)
1638{
1639 ARegion &region = *view_context.region;
1640 View3D &view3d = *view_context.v3d;
1641 Depsgraph &depsgraph = *view_context.depsgraph;
1642 Object &object = *view_context.obact;
1643
1644 const Object &object_eval = *DEG_get_evaluated(&depsgraph, &object);
1645
1646 ed::greasepencil::DrawingPlacement placement(scene, region, view3d, object_eval, &layer);
1647 if (placement.use_project_to_surface() || placement.use_project_to_stroke()) {
1648 placement.cache_viewport_depths(&depsgraph, &region, &view3d);
1649 }
1650
1651 Array<double2> cdt_verts;
1652 Array<int2> cdt_edges;
1653 const std::optional<meshintersect::CDT_input<double>> input = get_input_from_drawings(
1654 src_drawings, object, boundary_layers, region, opacity_threshold, cdt_verts, cdt_edges);
1655 if (!input) {
1656 return std::nullopt;
1657 }
1658 meshintersect::CDT_result<double> result = delaunay_2d_calc(*input, CDT_FULL);
1659
1660 const IndexRange src_boundary_points = input->vert.index_range().take_back(4);
1661
1662 Array<bool> is_source_edge(result.edge.size(), false);
1663
1664 threading::parallel_for(is_source_edge.index_range(), 512, [&](const IndexRange range) {
1665 for (const int64_t edge_index : range) {
1666 for (const uint32_t orig_id : result.edge_orig[edge_index]) {
1667 if (orig_id < result.face_edge_offset) {
1668 is_source_edge[edge_index] = true;
1669 }
1670 }
1671 }
1672 });
1673
1674 const Array<int3> tri_edges = get_all_triangle_edges(result.edge.as_span(),
1675 result.face.as_span());
1676 const Array<int3> tri_adjacency = get_all_triangle_adjacency(
1677 result.edge.size(), result.face.as_span(), tri_edges);
1678 const Array<float> edge_weights = get_edge_weights(result.edge.as_span(), result.vert.as_span());
1679
1680 auto get_tri_for_point = [&](const float2 &v) {
1681 for (const int tri_index : result.face.index_range()) {
1682 const Vector<int> &tri = result.face[tri_index];
1683 const float2 pos_0 = float2(result.vert[tri[0]]);
1684 const float2 pos_1 = float2(result.vert[tri[1]]);
1685 const float2 pos_2 = float2(result.vert[tri[2]]);
1686
1687 /* Skip triangles that have points too close together. */
1688 if (math::almost_equal_relative(pos_0, pos_1, 1e-4f) ||
1689 math::almost_equal_relative(pos_1, pos_2, 1e-4f) ||
1690 math::almost_equal_relative(pos_2, pos_0, 1e-4f))
1691 {
1692 continue;
1693 }
1694
1695 if (isect_point_tri_v2(v, pos_0, pos_1, pos_2) != 0) {
1696 return tri_index;
1697 }
1698 }
1699
1700 return NULL_INDEX;
1701 };
1702
1703 /* Check if a triangle is touches the boundary. */
1704 auto is_tri_connected_to_boundary = [&](const int tri_index) {
1705 const Vector<int> &tri = result.face[tri_index];
1706
1707 for (const int j : IndexRange(3)) {
1708 for (const int orig_point : result.vert_orig[tri[j]]) {
1709 if (src_boundary_points.contains(orig_point)) {
1710 return true;
1711 }
1712 }
1713 }
1714
1715 return false;
1716 };
1717
1718 /* Get the first triangle that is touching the bounding box. */
1719 auto get_first_boundary_tri = [&]() {
1720 for (const int tri_index : result.face.index_range()) {
1721 if (is_tri_connected_to_boundary(tri_index)) {
1722 return tri_index;
1723 }
1724 }
1726 return NULL_INDEX;
1727 };
1728
1729 Array<float> tri_max_weight = get_tri_max_weight(result.face.size(),
1730 tri_adjacency.as_span(),
1731 tri_edges.as_span(),
1732 edge_weights.as_span(),
1733 is_source_edge.as_span());
1734
1735 Array<int> tri_hint_index(result.face.size(), NULL_INDEX);
1736 Array<float> tri_weights(result.face.size(), 0.0f);
1737
1738 /* Note: Delaunay filling allows for multiple hints being used at the same time, but for bucket
1739 * filling only a single hint is used at a time. */
1740 const Span<float2> hint_points = fill_points[0];
1741
1742 /* Each `hint` is the source of a fill region, each `hint` can have multiple points in it. */
1743 int hint_index = 0;
1744
1745 Set<int> tris_for_hint;
1746 for (const int i : hint_points.index_range()) {
1747 const int hint_tri_index = get_tri_for_point(hint_points[i]);
1748 if (hint_tri_index != NULL_INDEX && !is_tri_connected_to_boundary(hint_tri_index)) {
1749 tris_for_hint.add(hint_tri_index);
1750 }
1751 }
1752
1753 /* Check if the user clicked outside of the bounding box. */
1754 if (tris_for_hint.is_empty()) {
1755 if (!invert) {
1756 return std::nullopt;
1757 }
1758 tris_for_hint.add(get_first_boundary_tri());
1759 }
1760
1761 if (internal_gaps && gap_factor > 0.0f) {
1762 for (const int hint_tri_index : tris_for_hint) {
1763 add_weights_for_tri(tri_adjacency.as_span(),
1764 tri_edges.as_span(),
1765 edge_weights.as_span(),
1766 tri_max_weight.as_span(),
1767 is_source_edge.as_span(),
1768 hint_tri_index,
1769 hint_index,
1770 tri_hint_index.as_mutable_span(),
1771 tri_weights.as_mutable_span());
1772 }
1773
1774 /* Create segmentation of the geometry until all triangles have full or nearly full weights.
1775 * the `gap_factor` is the factor that a triangle can be within and be considered full. */
1776 Set<int> not_full_tris;
1777 for (const int tri_index : result.face.index_range()) {
1778 if (tri_weights[tri_index] < tri_max_weight[tri_index] * gap_factor) {
1779 not_full_tris.add_new(tri_index);
1780 }
1781 }
1782
1783 auto get_next_max_tri_index = [&]() {
1784 if (not_full_tris.is_empty()) {
1785 return NULL_INDEX;
1786 }
1787
1788 int max_not_weight_tri_index = NULL_INDEX;
1789 float max_not_weight_tri_weight = 0.0f;
1790
1791 for (const int tri_index : not_full_tris) {
1792 const float tri_weight = tri_weights[tri_index];
1793 if (max_not_weight_tri_weight < tri_weight) {
1794 max_not_weight_tri_index = tri_index;
1795 max_not_weight_tri_weight = tri_weight;
1796 }
1797 }
1798 return max_not_weight_tri_index;
1799 };
1800
1801 int hint_tri_index = get_next_max_tri_index();
1802 hint_index++;
1803
1804 while (hint_tri_index != NULL_INDEX) {
1805 add_weights_for_tri(tri_adjacency.as_span(),
1806 tri_edges.as_span(),
1807 edge_weights.as_span(),
1808 tri_max_weight.as_span(),
1809 is_source_edge.as_span(),
1810 hint_tri_index,
1811 hint_index,
1812 tri_hint_index.as_mutable_span(),
1813 tri_weights.as_mutable_span());
1814
1815 hint_tri_index = get_next_max_tri_index();
1816 hint_index++;
1817
1818 not_full_tris.remove_if([&](const int tri_index) {
1819 return tri_weights[tri_index] >= tri_max_weight[tri_index] * gap_factor;
1820 });
1821 }
1822 }
1823 else {
1824 int hint_tri_index = get_first_boundary_tri();
1825 add_weights_for_tri(tri_adjacency.as_span(),
1826 tri_edges.as_span(),
1827 edge_weights.as_span(),
1828 tri_max_weight.as_span(),
1829 is_source_edge.as_span(),
1830 hint_tri_index,
1831 hint_index,
1832 tri_hint_index.as_mutable_span(),
1833 tri_weights.as_mutable_span());
1834
1835 hint_index++;
1836 }
1837
1838 if (!invert) {
1839 for (const int hint_tri_index : tris_for_hint) {
1840 /* Add the mouse fill again to make sure it as highest priority. */
1841 add_weights_for_tri(tri_adjacency.as_span(),
1842 tri_edges.as_span(),
1843 edge_weights.as_span(),
1844 tri_max_weight.as_span(),
1845 is_source_edge.as_span(),
1846 hint_tri_index,
1847 hint_index,
1848 tri_hint_index.as_mutable_span(),
1849 tri_weights.as_mutable_span());
1850 }
1851 }
1852
1853 Set<int> indices_to_fill({hint_index});
1854
1855 Array<bool> tri_to_fill(result.face.size(), false);
1856
1857 if (invert) {
1858 threading::parallel_for(result.face.index_range(), 512, [&](const IndexRange range) {
1859 for (const int64_t tri_index : range) {
1860 if (tri_hint_index[tri_index] != 0) {
1861 tri_to_fill[tri_index] = true;
1862 }
1863 }
1864 });
1865 }
1866 else {
1867 threading::parallel_for(result.face.index_range(), 512, [&](const IndexRange range) {
1868 for (const int64_t tri_index : range) {
1869 if (indices_to_fill.contains(tri_hint_index[tri_index])) {
1870 tri_to_fill[tri_index] = true;
1871 }
1872 }
1873 });
1874 }
1875
1876 const std::optional<EdgeCurves> edge_curves = create_connected_edges_from_fill(
1877 tri_to_fill, invert, tri_adjacency, tri_edges, result.edge.as_span());
1878
1879 if (!edge_curves) {
1880 return std::nullopt;
1881 }
1882
1883 /* Because all of the curves are cyclical and have more than 2 points:
1884 * They have the same number of edges as vertices. */
1885 const OffsetIndices<int> output_verts_offset = OffsetIndices<int>(edge_curves->offset_data);
1886
1887 if (output_verts_offset.total_size() == 0) {
1888 return std::nullopt;
1889 }
1890
1891 bke::CurvesGeometry curves(output_verts_offset.total_size(), output_verts_offset.size());
1892 curves.offsets_for_write().copy_from(output_verts_offset.data());
1893
1894 MutableSpan<float3> positions = curves.positions_for_write();
1895
1896 threading::parallel_for(output_verts_offset.index_range(), 512, [&](const IndexRange range) {
1897 for (const int64_t curve_i : range) {
1898 const IndexRange edges_range = output_verts_offset[curve_i];
1899
1900 for (const int point_i : edges_range) {
1901 const int edge_index = edge_curves->edges[point_i];
1902 const bool reversed = edge_curves->reversed[point_i];
1903 const int2 edge = result.edge[edge_index];
1904 const int vert_id = reversed ? edge[1] : edge[0];
1905
1906 const float2 pos_2d = float2(result.vert[vert_id]);
1907 const float3 position = placement.project(pos_2d);
1908 positions[point_i] = position;
1909 }
1910 }
1911 });
1912
1913 curves.cyclic_for_write().fill(true);
1914 curves.fill_curve_types(CURVE_TYPE_POLY);
1915 curves.tag_topology_changed();
1916
1917 return curves;
1918}
1919
1920} // namespace blender::ed::greasepencil
Camera data-block and utility functions.
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define BLI_SCOPED_DEFER(function_to_defer)
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define ELEM(...)
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
Span< T > as_span() const
Definition BLI_array.hh:254
IndexRange index_range() const
Definition BLI_array.hh:371
const T & first() const
Definition BLI_array.hh:292
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr IndexRange()=default
constexpr IndexRange take_back(int64_t n) const
constexpr IndexRange index_range() const
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
void add(const Key &key, const Value &value)
MapType::ValueIterator values() const
constexpr MutableSpan()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
void add_new(const Key &key)
Definition BLI_set.hh:233
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
int64_t size() const
void append(const T &value)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
AttributeAccessor attributes() const
IndexRange curves_range() const
OffsetIndices< int > points_by_curve() const
const bke::CurvesGeometry & strokes() const
void cache_viewport_depths(Depsgraph *depsgraph, ARegion *region, View3D *view3d)
float3 project(float2 co, bool &clipped) const
MutableSpan< ColorGeometry4b > pixels()
Definition fill.cc:155
Span< ColorGeometry4b > pixels() const
Definition fill.cc:150
ColorGeometry4b & pixel_from_coord(const int2 &c)
Definition fill.cc:160
int2 coord_from_index(const int index) const
Definition fill.cc:139
int index_from_coord(const int2 &c) const
Definition fill.cc:145
bool is_valid_coord(const int2 &c) const
Definition fill.cc:134
const ColorGeometry4b & pixel_from_coord(const int2 &c) const
Definition fill.cc:165
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define position
#define layer
float2x2 invert(float2x2 mat)
void fill_index_range(MutableSpan< T > span, const T start=0)
GeometryDeformation get_evaluated_grease_pencil_drawing_deformation(const Object *ob_eval, const Object &ob_orig, const bke::greasepencil::Drawing &drawing_orig)
void fill_points(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, GPointer value, GMutableSpan dst)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
void compute_view_matrices(const ViewContext &view_context, const Scene &scene, const int2 &win_size, const float2 &zoom, const float2 &offset)
void region_reset(ARegion &region, const RegionViewData &data)
void draw_lines(const float4x4 &transform, IndexRange indices, Span< float3 > start_positions, Span< float3 > end_positions, const VArray< ColorGeometry4f > &colors, float line_width)
Image * image_render_end(Main &bmain, GPUOffScreen *buffer)
void draw_grease_pencil_strokes(const RegionView3D &rv3d, const int2 &win_size, const Object &object, const bke::greasepencil::Drawing &drawing, const float4x4 &transform, const IndexMask &strokes_mask, const VArray< ColorGeometry4f > &colors, const bool use_xray, const float radius_scale)
RegionViewData region_init(ARegion &region, const int2 &win_size)
void draw_dot(const float4x4 &transform, const float3 &position, const float point_size, const ColorGeometry4f &color)
GPUOffScreen * image_render_begin(const int2 &win_size)
static Side decode_side(const EncodedConnection encoded)
Definition fill.cc:1093
static IndexMask get_visible_boundary_strokes(const Object &object, const DrawingInfo &info, const bool is_boundary_layer, IndexMaskMemory &memory)
Definition fill.cc:666
static const int2 offset_by_direction[num_directions]
Definition fill.cc:372
static int decode_index(const EncodedConnection encoded)
Definition fill.cc:1088
VecBase< EncodedConnection, 2 > EdgeConnections
Definition fill.cc:1099
bke::CurvesGeometry pixel_fill_strokes(const ViewContext &view_context, const Brush &brush, const Scene &scene, const bke::greasepencil::Layer &layer, const VArray< bool > &boundary_layers, const Span< DrawingInfo > src_drawings, bool invert, const std::optional< float > opacity_threshold, const float2 &fill_point, const ExtensionData &extensions, FillToolFitMethod fit_method, bool keep_images)
Definition fill.cc:1008
static FillBoundary build_fill_boundary(const ImageBufferAccessor &buffer)
Definition fill.cc:462
static Image * render_strokes(const ViewContext &view_context, const Brush &brush, const Scene &scene, const bke::greasepencil::Layer &layer, const VArray< bool > &boundary_layers, const Span< DrawingInfo > src_drawings, const int2 &image_size, const std::optional< float > opacity_threshold, const float2 &fill_point, const ExtensionData &extensions, const ed::greasepencil::DrawingPlacement &placement, const float2 &zoom, const float2 &offset)
Definition fill.cc:894
std::optional< bke::CurvesGeometry > delaunay_fill_strokes(const ViewContext &view_context, const Scene &scene, const bke::greasepencil::Layer &layer, const VArray< bool > &boundary_layers, const Span< DrawingInfo > src_drawings, bool invert, std::optional< float > opacity_threshold, bool internal_gaps, float gap_factor, const GroupedSpan< float2 > &fill_points)
Definition fill.cc:1627
constexpr const char * attr_is_fill_guide
Definition fill.cc:664
constexpr const char * attr_material_index
Definition fill.cc:663
static Bounds< float2 > get_region_bounds(const ARegion &region)
Definition fill.cc:742
static void dilate(ImageBufferAccessor &buffer, int iterations=1)
Definition fill.cc:383
static std::optional< EdgeCurves > create_connected_edges_from_fill(const Span< bool > tri_to_fill, const bool invert, const Span< int3 > tri_adjacency, const Span< int3 > tri_edges, const Span< int2 > edges)
Definition fill.cc:1551
FillResult flood_fill(ImageBufferAccessor &buffer, const int leak_filter_width=0)
Definition fill.cc:268
static void erode(ImageBufferAccessor &buffer, int iterations=1)
Definition fill.cc:413
static Array< float > get_tri_max_weight(const int num_tris, const Span< int3 > tri_adjacency, const Span< int3 > tri_edges, const Span< float > edge_weights, const Span< bool > is_source_edge)
Definition fill.cc:1282
const ColorGeometry4f draw_boundary_color
Definition fill.cc:60
static Array< int3 > get_all_triangle_edges(const Span< int2 > edges, const Span< Vector< int > > tris)
Definition fill.cc:1197
static int wrap_dir_3n(const int dir)
Definition fill.cc:444
static Array< float > get_edge_weights(const Span< int2 > edges, const Span< double2 > verts)
Definition fill.cc:1266
constexpr const int num_directions
Definition fill.cc:371
static VArray< ColorGeometry4f > get_stroke_colors(const Object &object, const bke::CurvesGeometry &curves, const VArray< float > &opacities, const VArray< int > materials, const ColorGeometry4f &tint_color, const std::optional< float > opacity_threshold)
Definition fill.cc:712
static EdgeCurves follow_edge_connections(const Span< int > all_edges, const Span< bool > edges_to_keep, const Span< EdgeConnections > edge_connections)
Definition fill.cc:1109
static Array< int3 > get_all_triangle_adjacency(const int num_edges, const Span< Vector< int > > tris, const Span< int3 > tri_edges)
Definition fill.cc:1218
static bke::CurvesGeometry boundary_to_curves(const FillBoundary &boundary, const ImageBufferAccessor &buffer, const ed::greasepencil::DrawingPlacement &placement, const float3x3 &image_to_region)
Definition fill.cc:577
static void mark_borders(ImageBufferAccessor &buffer)
Definition fill.cc:235
static std::optional< Bounds< float2 > > get_boundary_bounds(const ARegion &region, const RegionView3D &rv3d, const Object &object, const Object &object_eval, const VArray< bool > &boundary_layers, const Span< DrawingInfo > src_drawings)
Definition fill.cc:751
static constexpr EncodedConnection EDGE_CONNECTION_NULL
Definition fill.cc:1080
static void convert_colors_to_flags(ImageBufferAccessor &buffer)
Definition fill.cc:183
static bke::CurvesGeometry process_image(Image &ima, const Brush &brush, const ed::greasepencil::DrawingPlacement &placement, const float3x3 &image_to_region, const bool invert, const bool output_as_colors)
Definition fill.cc:606
static void add_weights_for_tri(const Span< int3 > tri_adjacency, const Span< int3 > tri_edges, const Span< float > edge_weights, const Span< float > tri_max_weight, const Span< bool > is_source_edge, const int hint_tri_index, const int hint_index, MutableSpan< int > r_tri_hint_index, MutableSpan< float > r_tri_weights)
Definition fill.cc:1312
constexpr int NULL_INDEX
Definition fill.cc:1101
static auto fit_strokes_to_view(const ViewContext &view_context, const VArray< bool > &boundary_layers, const Span< DrawingInfo > src_drawings, const FillToolFitMethod fit_method, const float2 fill_point, const bool uniform_zoom, const float max_zoom_factor, const float2 margin, const float pixel_scale)
Definition fill.cc:826
static EncodedConnection encode_index_and_side(const int index, const Side side)
Definition fill.cc:1083
static std::optional< meshintersect::CDT_input< double > > get_input_from_drawings(const Span< DrawingInfo > src_drawings, const Object &object, const VArray< bool > &boundary_layers, const ARegion &region, const std::optional< float > opacity_threshold, Array< double2 > &r_cdt_verts, Array< int2 > &r_cdt_edges)
Definition fill.cc:1357
static void convert_flags_to_colors(ImageBufferAccessor &buffer)
Definition fill.cc:196
static void set_flag(ColorGeometry4b &color, const ColorFlag flag, bool value)
Definition fill.cc:176
const ColorGeometry4f draw_seed_color
Definition fill.cc:61
static bool get_flag(const ColorGeometry4b &color, const ColorFlag flag)
Definition fill.cc:171
static void invert_fill(ImageBufferAccessor &buffer)
Definition fill.cc:363
ENUM_OPERATORS(AxisMutable)
constexpr ParallelGrainSize grain_size(int grain_size)
T clamp(const T &a, const T &min, const T &max)
T safe_rcp(const T &a)
T safe_divide(const T &a, const T &b)
T reduce_max(const VecBase< T, Size > &a)
MatT from_loc_scale(const typename MatT::loc_type &location, const VecBase< typename MatT::base_type, ScaleDim > &scale)
bool almost_equal_relative(const VecBase< T, Size > &a, const VecBase< T, Size > &b, const T &epsilon_factor)
T max(const T &a, const T &b)
T abs(const T &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
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:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
__int64 int64_t
Definition BKE_text.h:90
float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
Material * BKE_object_material_get(Object *ob, short act)
@ GP_FILL_DMODE_CONTROL
void GPU_depth_mask(bool depth)
Definition gpu_state.cc:107
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
int isect_point_tri_v2(const float pt[2], const float v1[2], const float v2[2], const float v3[2])
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
unsigned char uint8_t
Definition BKE_text.h:79
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
MatBase< float, 3, 3 > float3x3
void BKE_id_free(Main *bmain, void *idv)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
T center() const
eMaterialGPencilStyle_Flag flag
RegionView3D * rv3d
Definition ED_view3d.hh:86
Depsgraph * depsgraph
Definition ED_view3d.hh:78
const bke::greasepencil::Drawing & drawing
struct blender::ed::greasepencil::ExtensionData::@147343236172247015273153246111172323157377245354 lines