Blender V5.3
transform_convert_pointcloud.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
8
9#include "BLI_array.hh"
10#include "BLI_array_utils.hh"
11#include "BLI_math_matrix.hh"
12#include "BLI_math_matrix_c.hh"
13#include "BLI_span.hh"
14
16
17#include "BKE_attribute.hh"
18#include "BKE_context.hh"
19#include "BKE_geometry_set.hh"
20
21#include "ED_curves.hh"
22
23#include "MEM_guardedalloc.h"
24
25#include "transform.hh"
26#include "transform_convert.hh"
27#include "transform_snap.hh"
28
29/* -------------------------------------------------------------------- */
32
34
42
44{
45 PointCloudTransformData *transform_data = MEM_new<PointCloudTransformData>(__func__);
46 custom_data.data = transform_data;
47 custom_data.free_cb = [](TransInfo *, TransDataContainer *, TransCustomData *custom_data) {
48 PointCloudTransformData *data = static_cast<PointCloudTransformData *>(custom_data->data);
49 MEM_delete(data);
50 custom_data->data = nullptr;
51 };
52 return transform_data;
53}
54
56{
58 const bool use_proportional_edit = (t->flag & T_PROP_EDIT_ALL) != 0;
59
60 for (const int i : trans_data_contrainers.index_range()) {
61 TransDataContainer &tc = trans_data_contrainers[i];
62 PointCloud &pointcloud = *id_cast<PointCloud *>(tc.obedit->data);
63 bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
65 const VArray selection_attr = *attributes.lookup_or_default<bool>(
66 ".selection", bke::AttrDomain::Point, true);
67 if (use_proportional_edit) {
68 transform_data.selection = IndexMask(pointcloud.totpoint);
69 tc.data_len = transform_data.selection.size();
70 }
71 else {
72 transform_data.selection = IndexMask::from_bools(selection_attr, transform_data.memory);
73 tc.data_len = transform_data.selection.size();
74 }
75 if (tc.data_len == 0) {
76 tc.custom.type.free_cb(t, &tc, &tc.custom.type);
77 continue;
78 }
79
82
83 transform_data.positions.reinitialize(tc.data_len);
85 transform_data.selection,
86 transform_data.positions.as_mutable_span());
87
88 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
89 transform_data.radii.reinitialize(transform_data.selection.size());
91 pointcloud.radius(), transform_data.selection, transform_data.radii.as_mutable_span());
92 }
93
94 const float4x4 &transform = tc.obedit->object_to_world();
95 const float3x3 mtx_base = transform.view<3, 3>();
96 const float3x3 smtx_base = math::pseudo_invert(mtx_base);
97
98 threading::parallel_for(tc_data.index_range(), 1024, [&](const IndexRange range) {
99 for (const int64_t i : range) {
100 TransData &td = tc_data[i];
101 float3 *elem = &transform_data.positions[i];
102 copy_v3_v3(td.iloc, *elem);
103 copy_v3_v3(td.center, *elem);
104 td.loc = *elem;
105
106 td.flag = 0;
107 if (use_proportional_edit) {
108 if (selection_attr[i]) {
109 td.flag = TD_SELECTED;
110 }
111 }
112 else {
113 td.flag = TD_SELECTED;
114 }
115
116 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
117 float *value = &transform_data.radii[i];
118 td.val = value;
119 td.ival = *value;
120 }
121
122 copy_m3_m3(td.smtx, smtx_base.ptr());
123 copy_m3_m3(td.mtx, mtx_base.ptr());
124 }
125 });
126 }
127}
128
130{
131 if (t->state != TRANS_CANCEL) {
133 }
134
135 const Span<TransDataContainer> trans_data_contrainers(t->data_container, t->data_container_len);
136 for (const TransDataContainer &tc : trans_data_contrainers) {
137 const PointCloudTransformData &transform_data = *static_cast<PointCloudTransformData *>(
138 tc.custom.type.data);
139 PointCloud &pointcloud = *id_cast<PointCloud *>(tc.obedit->data);
140 if (t->mode == TFM_CURVE_SHRINKFATTEN) {
142 transform_data.radii.as_span(), transform_data.selection, pointcloud.radius_for_write());
143 pointcloud.tag_radii_changed();
144 }
145 else {
146 array_utils::scatter(transform_data.positions.as_span(),
147 transform_data.selection,
148 pointcloud.positions_for_write());
149 pointcloud.tag_positions_changed();
150 }
152 }
153}
154
156
158 /*flags*/ (T_EDIT | T_POINTS),
159 /*create_trans_data*/ pointcloud::createTransPointCloudVerts,
160 /*recalc_data*/ pointcloud::recalcData_pointcloud,
161 /*special_aftertrans_update*/ nullptr,
162};
163
164} // namespace blender::ed::transform::pointcloud
Read Blender memory allocation and freeing.
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr MutableSpan()=default
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void scatter(const Span< T > src, const Span< IndexT > indices, MutableSpan< T > dst, const Mode mode={})
static void createTransPointCloudVerts(bContext *, TransInfo *t)
static PointCloudTransformData * create_transform_custom_data(TransCustomData &custom_data)
void transform_snap_project_individual_apply(TransInfo *t)
MatBase< T, Size, Size > pseudo_invert(const MatBase< T, Size, Size > &mat, T epsilon=1e-8)
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
MatBase< float, 4, 4 > float4x4
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
MatBase< float, 3, 3 > float3x3
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
TransDataContainer * data_container
Definition transform.hh:828
#define T_PROP_EDIT_ALL
Definition transform.hh:34
conversion and adaptation of different datablocks to a common struct.