Blender V5.3
geometry/intern/transform.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
8
9#ifdef WITH_OPENVDB
10# include <openvdb/openvdb.h>
11#endif
12
13#include "GEO_transform.hh"
14
15#include "BLI_math_matrix.hh"
16#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector.hh"
18#include "BLI_task.hh"
19
21#include "DNA_mesh_types.h"
23
24#include "BKE_attribute.hh"
25#include "BKE_curves.hh"
27#include "BKE_geometry_set.hh"
28#include "BKE_grease_pencil.hh"
29#include "BKE_instances.hh"
30#include "BKE_mesh.hh"
31#include "BKE_pointcloud.hh"
32#include "BKE_volume.hh"
33
34namespace blender::geometry {
35
36static void translate_positions(MutableSpan<float3> positions, const float3 &translation)
37{
38 threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) {
39 for (float3 &position : positions.slice(range)) {
40 position += translation;
41 }
42 });
43}
44
45static void translate_pointcloud(PointCloud &pointcloud, const float3 translation)
46{
47 if (math::is_zero(translation)) {
48 return;
49 }
50
51 std::optional<Bounds<float3>> bounds;
52 if (pointcloud.runtime->bounds_cache.is_cached()) {
53 bounds = pointcloud.runtime->bounds_cache.data();
54 }
55
56 bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
58 "position", bke::AttrDomain::Point);
59 translate_positions(position.span, translation);
60 position.finish();
61
62 if (bounds) {
63 bounds->min += translation;
64 bounds->max += translation;
65 pointcloud.runtime->bounds_cache.ensure([&](Bounds<float3> &r_data) { r_data = *bounds; });
66 }
67}
68
69static void transform_pointcloud(PointCloud &pointcloud, const float4x4 &transform)
70{
71 bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
73 "position", bke::AttrDomain::Point);
75 position.finish();
76}
77
79{
80 using namespace blender::bke::greasepencil;
81 for (const int layer_index : grease_pencil.layers().index_range()) {
82 Layer &layer = grease_pencil.layer(layer_index);
83 float4x4 local_transform = layer.local_transform();
84 local_transform.location() += translation;
85 layer.set_local_transform(local_transform);
86 }
87}
88
90{
91 using namespace blender::bke::greasepencil;
92 for (const int layer_index : grease_pencil.layers().index_range()) {
93 Layer &layer = grease_pencil.layer(layer_index);
94 float4x4 local_transform = layer.local_transform();
95 local_transform = transform * local_transform;
96 layer.set_local_transform(local_transform);
97 }
98}
99
100static void translate_instances(bke::Instances &instances, const float3 translation)
101{
102 MutableSpan<float4x4> transforms = instances.transforms_for_write();
103 threading::parallel_for(transforms.index_range(), 1024, [&](const IndexRange range) {
104 for (float4x4 &instance_transform : transforms.slice(range)) {
105 add_v3_v3(instance_transform.ptr()[3], translation);
106 }
107 });
108}
109
111{
112 MutableSpan<float4x4> transforms = instances.transforms_for_write();
113 threading::parallel_for(transforms.index_range(), 1024, [&](const IndexRange range) {
114 for (float4x4 &instance_transform : transforms.slice(range)) {
115 instance_transform = transform * instance_transform;
116 }
117 });
118}
119
120static void transform_volume(Volume &volume,
121 const float4x4 &transform,
122 TransformGeometryErrors &r_errors)
123{
124#ifdef WITH_OPENVDB
125 openvdb::Mat4s vdb_matrix;
126 memcpy(vdb_matrix.asPointer(), &transform, sizeof(float[4][4]));
127 openvdb::Mat4d vdb_matrix_d{vdb_matrix};
128
129 const int grids_num = BKE_volume_num_grids(&volume);
130 for (const int i : IndexRange(grids_num)) {
131 bke::VolumeGridData *volume_grid = BKE_volume_grid_get_for_write(&volume, i);
132
134 grid_matrix = transform * grid_matrix;
135 const float determinant = math::determinant(grid_matrix);
137 r_errors.volume_too_small = true;
138 /* Clear the tree because it is too small. */
140 if (determinant == 0) {
141 /* Reset rotation and scale. */
142 grid_matrix.x_axis() = float3(1, 0, 0);
143 grid_matrix.y_axis() = float3(0, 1, 0);
144 grid_matrix.z_axis() = float3(0, 0, 1);
145 }
146 else {
147 /* Keep rotation but reset scale. */
148 grid_matrix.x_axis() = math::normalize(grid_matrix.x_axis());
149 grid_matrix.y_axis() = math::normalize(grid_matrix.y_axis());
150 grid_matrix.z_axis() = math::normalize(grid_matrix.z_axis());
151 }
152 }
153 try {
155 }
156 catch (...) {
157 r_errors.bad_volume_transform = true;
158 }
159 }
160
161#else
162 UNUSED_VARS(volume, transform, r_errors);
163#endif
164}
165
166static void translate_volume(Volume &volume, const float3 translation)
167{
169 transform_volume(volume, math::from_location<float4x4>(translation), errors);
170}
171
173{
174 if (const std::optional<MutableSpan<float3>> positions = edit_hints.positions_for_write()) {
176 }
177 float3x3 deform_mat;
178 copy_m3_m4(deform_mat.ptr(), transform.ptr());
179 if (edit_hints.deform_mats.has_value()) {
180 MutableSpan<float3x3> deform_mats = *edit_hints.deform_mats;
181 threading::parallel_for(deform_mats.index_range(), 1024, [&](const IndexRange range) {
182 for (const int64_t i : range) {
183 deform_mats[i] = deform_mat * deform_mats[i];
184 }
185 });
186 }
187 else {
188 edit_hints.deform_mats.emplace(edit_hints.curves_id_orig.geometry.point_num, deform_mat);
189 }
190}
191
193 const float4x4 &transform)
194{
195 if (!edit_hints.drawing_hints) {
196 return;
197 }
198
199 for (bke::GreasePencilDrawingEditHints &drawing_hints : *edit_hints.drawing_hints) {
200 if (const std::optional<MutableSpan<float3>> positions = drawing_hints.positions_for_write()) {
202 }
203 float3x3 deform_mat = transform.view<3, 3>();
204 if (drawing_hints.deform_mats.has_value()) {
205 MutableSpan<float3x3> deform_mats = *drawing_hints.deform_mats;
206 threading::parallel_for(deform_mats.index_range(), 1024, [&](const IndexRange range) {
207 for (const int64_t i : range) {
208 deform_mats[i] = deform_mat * deform_mats[i];
209 }
210 });
211 }
212 else if (drawing_hints.drawing_orig) {
213 drawing_hints.deform_mats.emplace(drawing_hints.drawing_orig->strokes().points_num(),
214 deform_mat);
215 }
216 }
217}
218
220{
221 for (float4x4 &m : edit_hints.gizmo_transforms.values()) {
222 m = transform * m;
223 }
224}
225
226static void translate_curve_edit_hints(bke::CurvesEditHints &edit_hints, const float3 &translation)
227{
228 if (const std::optional<MutableSpan<float3>> positions = edit_hints.positions_for_write()) {
229 translate_positions(*positions, translation);
230 }
231}
232
233static void translate_gizmos_edit_hints(bke::GizmoEditHints &edit_hints, const float3 &translation)
234{
235 for (float4x4 &m : edit_hints.gizmo_transforms.values()) {
236 m.location() += translation;
237 }
238}
239
241{
242 if (math::is_zero(translation)) {
243 return;
244 }
245 if (Curves *curves = geometry.get_curves_for_write()) {
246 curves->geometry.wrap().translate(translation);
247 }
248 if (Mesh *mesh = geometry.get_mesh_for_write()) {
249 bke::mesh_translate(*mesh, translation, false);
250 }
251 if (PointCloud *pointcloud = geometry.get_pointcloud_for_write()) {
252 translate_pointcloud(*pointcloud, translation);
253 }
254 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
256 }
257 if (Volume *volume = geometry.get_volume_for_write()) {
258 translate_volume(*volume, translation);
259 }
260 if (bke::Instances *instances = geometry.get_instances_for_write()) {
261 translate_instances(*instances, translation);
262 }
263 if (bke::CurvesEditHints *curve_edit_hints = geometry.get_curve_edit_hints_for_write()) {
264 translate_curve_edit_hints(*curve_edit_hints, translation);
265 }
266 if (bke::GizmoEditHints *gizmo_edit_hints = geometry.get_gizmo_edit_hints_for_write()) {
267 translate_gizmos_edit_hints(*gizmo_edit_hints, translation);
268 }
269}
270
271std::optional<TransformGeometryErrors> transform_geometry(bke::GeometrySet &geometry,
272 const float4x4 &transform)
273{
274 if (transform == float4x4::identity()) {
275 return std::nullopt;
276 }
278 if (Curves *curves = geometry.get_curves_for_write()) {
279 curves->geometry.wrap().transform(transform);
280 }
281 if (Mesh *mesh = geometry.get_mesh_for_write()) {
283 }
284 if (PointCloud *pointcloud = geometry.get_pointcloud_for_write()) {
285 transform_pointcloud(*pointcloud, transform);
286 }
287 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
289 }
290 if (Volume *volume = geometry.get_volume_for_write()) {
291 transform_volume(*volume, transform, errors);
292 }
293 if (bke::Instances *instances = geometry.get_instances_for_write()) {
294 transform_instances(*instances, transform);
295 }
296 if (bke::CurvesEditHints *curve_edit_hints = geometry.get_curve_edit_hints_for_write()) {
297 transform_curve_edit_hints(*curve_edit_hints, transform);
298 }
299 if (bke::GreasePencilEditHints *grease_pencil_edit_hints =
300 geometry.get_grease_pencil_edit_hints_for_write())
301 {
302 transform_grease_pencil_edit_hints(*grease_pencil_edit_hints, transform);
303 }
304 if (bke::GizmoEditHints *gizmo_edit_hints = geometry.get_gizmo_edit_hints_for_write()) {
305 transform_gizmo_edit_hints(*gizmo_edit_hints, transform);
306 }
307
308 if (errors.volume_too_small) {
309 return errors;
310 }
311 return std::nullopt;
312}
313
315 const float3 translation,
316 const math::Quaternion rotation,
317 const float3 scale)
318{
319 const float4x4 matrix = math::from_loc_rot_scale<float4x4>(translation, rotation, scale);
320 bke::mesh_transform(mesh, matrix, false);
321}
322
323} // namespace blender::geometry
Low-level operations for curves.
Low-level operations for grease pencil.
General operations for point clouds.
Volume data-block.
#define UNUSED_VARS(...)
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
std::optional< MutableSpan< float3 > > positions_for_write()
std::optional< Array< float3x3 > > deform_mats
std::optional< MutableSpan< float3 > > positions_for_write()
std::optional< Array< float3x3 > > deform_mats
std::optional< Array< GreasePencilDrawingEditHints > > drawing_hints
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:242
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
const bke::CurvesGeometry & strokes() const
#define position
#define layer
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
float3x3 scale(float3x3 mat, float2 scale)
float4x4 get_transform_matrix(const VolumeGridData &grid)
void clear_tree(VolumeGridData &grid)
void set_transform_matrix(VolumeGridData &grid, const float4x4 &matrix)
void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
void mesh_translate(Mesh &mesh, const float3 &translation, bool do_shape_keys)
static void translate_positions(MutableSpan< float3 > positions, const float3 &translation)
static void translate_greasepencil(GreasePencil &grease_pencil, const float3 translation)
static void transform_instances(bke::Instances &instances, const float4x4 &transform)
void translate_geometry(bke::GeometrySet &geometry, const float3 translation)
static void transform_grease_pencil_edit_hints(bke::GreasePencilEditHints &edit_hints, const float4x4 &transform)
static void translate_gizmos_edit_hints(bke::GizmoEditHints &edit_hints, const float3 &translation)
static void translate_instances(bke::Instances &instances, const float3 translation)
static void transform_curve_edit_hints(bke::CurvesEditHints &edit_hints, const float4x4 &transform)
static void transform_gizmo_edit_hints(bke::GizmoEditHints &edit_hints, const float4x4 &transform)
void transform_mesh(Mesh &mesh, float3 translation, math::Quaternion rotation, float3 scale)
static void translate_curve_edit_hints(bke::CurvesEditHints &edit_hints, const float3 &translation)
static void translate_volume(Volume &volume, const float3 translation)
std::optional< TransformGeometryErrors > transform_geometry(bke::GeometrySet &geometry, const float4x4 &transform)
static void transform_pointcloud(PointCloud &pointcloud, const float4x4 &transform)
static void transform_volume(Volume &volume, const float4x4 &transform, TransformGeometryErrors &r_errors)
static void translate_pointcloud(PointCloud &pointcloud, const float3 translation)
static void transform_greasepencil(GreasePencil &grease_pencil, const float4x4 &transform)
QuaternionBase< float > Quaternion
bool is_zero(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
MatT from_location(const typename MatT::loc_type &location)
T determinant(const MatBase< T, Size, Size > &mat)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
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
bke::VolumeGridData * BKE_volume_grid_get_for_write(Volume *volume, int grid_index)
int BKE_volume_num_grids(const Volume *volume)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 3 > float3x3
bool BKE_volume_grid_determinant_valid(double determinant)
VecBase< float, 3 > float3
const c_style_mat & ptr() const
bke::PointCloudRuntime * runtime
SharedCache< Bounds< float3 > > bounds_cache