Blender V4.3
curves_utils.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
9#include "BKE_curves_utils.hh"
10#include "BKE_customdata.hh"
11
12namespace blender::bke::curves {
13
14void fill_points(const OffsetIndices<int> points_by_curve,
15 const IndexMask &curve_selection,
16 const GPointer value,
17 GMutableSpan dst)
18{
19 BLI_assert(*value.type() == dst.type());
20 const CPPType &type = dst.type();
21 curve_selection.foreach_index(GrainSize(512), [&](const int i) {
22 const IndexRange points = points_by_curve[i];
23 type.fill_assign_n(value.get(), dst.slice(points).data(), points.size());
24 });
25}
26
28{
29 CurvesGeometry dst_curves(0, src_curves.curves_num());
31 &src_curves.curve_data, &dst_curves.curve_data, CD_MASK_ALL, src_curves.curves_num());
32 dst_curves.runtime->type_counts = src_curves.runtime->type_counts;
33 return dst_curves;
34}
35
37 const std::array<int, CURVE_TYPES_NUM> &type_counts,
38 const CurveType type,
39 const IndexMask &selection,
40 IndexMaskMemory &memory)
41{
42 if (type_counts[type] == types.size()) {
43 return selection;
44 }
45 if (types.is_single()) {
46 return types.get_internal_single() == type ? IndexMask(types.size()) : IndexMask(0);
47 }
48 Span<int8_t> types_span = types.get_internal_span();
49 return IndexMask::from_predicate(selection, GrainSize(4096), memory, [&](const int index) {
50 return types_span[index] == type;
51 });
52}
53
55 const std::array<int, CURVE_TYPES_NUM> &counts,
56 const IndexMask &selection,
57 FunctionRef<void(IndexMask)> catmull_rom_fn,
58 FunctionRef<void(IndexMask)> poly_fn,
59 FunctionRef<void(IndexMask)> bezier_fn,
60 FunctionRef<void(IndexMask)> nurbs_fn)
61{
62 auto call_if_not_empty = [&](const CurveType type, FunctionRef<void(IndexMask)> fn) {
63 IndexMaskMemory memory;
64 const IndexMask mask = indices_for_type(types, counts, type, selection, memory);
65 if (!mask.is_empty()) {
66 fn(mask);
67 }
68 };
69 call_if_not_empty(CURVE_TYPE_CATMULL_ROM, catmull_rom_fn);
70 call_if_not_empty(CURVE_TYPE_POLY, poly_fn);
71 call_if_not_empty(CURVE_TYPE_BEZIER, bezier_fn);
72 call_if_not_empty(CURVE_TYPE_NURBS, nurbs_fn);
73}
74
75namespace bezier {
76
78 const IndexMask &curves_selection)
79{
80 if (curves.curves_num() == 0 || !curves.has_curve_with_type(CURVE_TYPE_BEZIER)) {
81 return {};
82 }
83 const OffsetIndices points_by_curve = curves.points_by_curve();
84 const Span<float3> positions = curves.positions();
85 const Span<float3> handle_positions_left = curves.handle_positions_left();
86 const Span<float3> handle_positions_right = curves.handle_positions_right();
87
88 Array<float3> all_positions(positions.size() * 3);
89 curves_selection.foreach_index(GrainSize(1024), [&](const int curve) {
90 const IndexRange points = points_by_curve[curve];
91 for (const int point : points) {
92 const int index = point * 3;
93 all_positions[index] = handle_positions_left[point];
94 all_positions[index + 1] = positions[point];
95 all_positions[index + 2] = handle_positions_right[point];
96 }
97 });
98
99 return all_positions;
100}
101
103 const IndexMask &curves_selection,
104 const Span<float3> all_positions)
105{
106 if (curves_selection.is_empty() || curves.curves_num() == 0 ||
107 !curves.has_curve_with_type(CURVE_TYPE_BEZIER))
108 {
109 return;
110 }
111 BLI_assert(curves_selection.size() * 3 == all_positions.size());
112
113 const OffsetIndices points_by_curve = curves.points_by_curve();
114 MutableSpan<float3> positions = curves.positions_for_write();
115 MutableSpan<float3> handle_positions_left = curves.handle_positions_left_for_write();
116 MutableSpan<float3> handle_positions_right = curves.handle_positions_right_for_write();
117
118 curves_selection.foreach_index(GrainSize(1024), [&](const int curve) {
119 const IndexRange points = points_by_curve[curve];
120 for (const int point : points) {
121 const int index = point * 3;
122 handle_positions_left[point] = all_positions[index];
123 positions[point] = all_positions[index + 1];
124 handle_positions_right[point] = all_positions[index + 2];
125 }
126 });
127}
128
129} // namespace bezier
130
131} // namespace blender::bke::curves
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
#define BLI_assert(a)
Definition BLI_assert.h:50
CurveType
@ CURVE_TYPE_BEZIER
@ CURVE_TYPE_NURBS
@ CURVE_TYPE_POLY
@ CURVE_TYPE_CATMULL_ROM
#define CD_MASK_ALL
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
GMutableSpan slice(const int64_t start, int64_t size) const
const CPPType & type() const
constexpr int64_t size() const
Definition BLI_span.hh:253
static IndexMask from_predicate(const IndexMask &universe, GrainSize grain_size, IndexMaskMemory &memory, Fn &&predicate)
void foreach_index(Fn &&fn) const
Array< float3 > retrieve_all_positions(const bke::CurvesGeometry &curves, const IndexMask &curves_selection)
void write_all_positions(bke::CurvesGeometry &curves, const IndexMask &curves_selection, Span< float3 > all_positions)
bke::CurvesGeometry copy_only_curve_domain(const bke::CurvesGeometry &src_curves)
IndexMask indices_for_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const CurveType type, const IndexMask &selection, IndexMaskMemory &memory)
void fill_points(OffsetIndices< int > points_by_curve, const IndexMask &curve_selection, GPointer value, GMutableSpan dst)
void foreach_curve_by_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const IndexMask &selection, FunctionRef< void(IndexMask)> catmull_rom_fn, FunctionRef< void(IndexMask)> poly_fn, FunctionRef< void(IndexMask)> bezier_fn, FunctionRef< void(IndexMask)> nurbs_fn)
GPU_SHADER_INTERFACE_INFO(overlay_edit_curve_handle_iface, "vert").flat(Type pos vertex_in(1, Type::UINT, "data") .vertex_out(overlay_edit_curve_handle_iface) .geometry_layout(PrimitiveIn Frequency::GEOMETRY storage_buf(1, Qualifier::READ, "uint", "data[]", Frequency::GEOMETRY) .push_constant(Type Frequency::GEOMETRY selection[]
CurvesGeometryRuntimeHandle * runtime
CustomData curve_data