Blender V5.3
lattice_deform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <algorithm>
12#include <cmath>
13#include <cstdlib>
14#include <cstring>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_math_matrix_c.hh"
19#include "BLI_math_vector_c.hh"
20#include "BLI_simd.hh"
21#include "BLI_task_c.hh"
22#include "BLI_utildefines.hh"
23
24#include "DNA_curve_types.h"
25#include "DNA_lattice_types.h"
26#include "DNA_mesh_types.h"
27#include "DNA_meshdata_types.h"
28#include "DNA_object_types.h"
29
30#include "BKE_curve.hh"
31#include "BKE_displist.h"
32#include "BKE_editmesh.hh"
33#include "BKE_key.hh"
34#include "BKE_lattice.hh"
35#include "BKE_modifier.hh"
36#include "BKE_object.hh"
37#include "BKE_object_types.hh"
38
39#include "BKE_deform.hh"
40
41namespace blender {
42
43/* -------------------------------------------------------------------- */
46
48 /* Convert from object space to deform space */
49 float latmat[4][4];
50 /* Cached reference to the lattice to use for evaluation. When in edit mode this attribute
51 * is set to the edit mode lattice. */
52 const Lattice *lt;
53 /* Preprocessed lattice points (converted to deform space). */
55 /* Prefetched DeformWeights of the lattice. */
57};
58
60{
61 /* we make an array with all differences */
62 Lattice *lt = BKE_object_get_lattice(oblatt);
63 DispList *dl = oblatt->runtime->curve_cache ?
65 nullptr;
66 const float *co = dl ? dl->verts : nullptr;
67 float *fp, imat[4][4];
68 float fu, fv, fw;
69 int u, v, w;
70 float *latticedata;
71 float *lattice_weights = nullptr;
72 float latmat[4][4];
73 LatticeDeformData *lattice_deform_data;
74 /* May be null. */
75 BPoint *bp = lt->def;
76
77 const int32_t num_points = lt->pntsu * lt->pntsv * lt->pntsw;
78 /* We allocate one additional float for SSE2 optimizations. Without this
79 * the SSE2 instructions for the last item would read in unallocated memory. */
80 fp = latticedata = MEM_new_array_uninitialized<float>(3 * size_t(num_points) + 1, "latticedata");
81
82 /* for example with a particle system: (ob == nullptr) */
83 if (ob == nullptr) {
84 /* In deform-space, calc matrix. */
85 invert_m4_m4(latmat, oblatt->object_to_world().ptr());
86
87 /* back: put in deform array */
88 invert_m4_m4(imat, latmat);
89 }
90 else {
91 /* In deform-space, calc matrix. */
92 invert_m4_m4(imat, oblatt->object_to_world().ptr());
93 mul_m4_m4m4(latmat, imat, ob->object_to_world().ptr());
94
95 /* back: put in deform array. */
96 invert_m4_m4(imat, latmat);
97 }
98
99 /* Prefetch lattice deform group weights. */
100 int defgrp_index = -1;
101 const MDeformVert *dvert = BKE_lattice_deform_verts_get(oblatt);
102 if (lt->vgroup[0] && dvert) {
103 defgrp_index = BKE_id_defgroup_name_index(&lt->id, lt->vgroup);
104
105 if (defgrp_index != -1) {
106 lattice_weights = MEM_new_array_uninitialized<float>(size_t(num_points), "lattice_weights");
107 for (int index = 0; index < num_points; index++) {
108 lattice_weights[index] = BKE_defvert_find_weight(dvert + index, defgrp_index);
109 }
110 }
111 }
112
113 for (w = 0, fw = lt->fw; w < lt->pntsw; w++, fw += lt->dw) {
114 for (v = 0, fv = lt->fv; v < lt->pntsv; v++, fv += lt->dv) {
115 for (u = 0, fu = lt->fu; u < lt->pntsu; u++, fp += 3, fu += lt->du) {
116 if (dl) {
117 fp[0] = co[0] - fu;
118 fp[1] = co[1] - fv;
119 fp[2] = co[2] - fw;
120 co += 3;
121 }
122 else {
123 fp[0] = bp->vec[0] - fu;
124 fp[1] = bp->vec[1] - fv;
125 fp[2] = bp->vec[2] - fw;
126 bp++;
127 }
128
129 mul_mat3_m4_v3(imat, fp);
130 }
131 }
132 }
133
134 lattice_deform_data = MEM_new_uninitialized<LatticeDeformData>("Lattice Deform Data");
135 lattice_deform_data->latticedata = latticedata;
136 lattice_deform_data->lattice_weights = lattice_weights;
137 lattice_deform_data->lt = lt;
138 copy_m4_m4(lattice_deform_data->latmat, latmat);
139
140 return lattice_deform_data;
141}
142
146static float lattice_deform_axis_clamp(const float value, const int points_num)
147{
148 /* `int(floor(value))` (performed by the caller) is undefined when `value` is non-finite or
149 * outside the `int` range. This can happen when an object is located near the limits of float.
150 * Clamping `value` rather than the resulting index keeps the fractional `value - floor()`
151 * in `[0, 1)`, as it is for coordinates inside the lattice, see: #160589.
152 *
153 * Regarding the range: `[-2, points_num]`.
154 * The caller samples cells `index - 1` to `index + 2` so values outside this range
155 * all clamp to one boundary cell and the result is unchanged. */
156
157 /* Catches infinity and NaN. */
158 if (!(value > -2.0f)) [[unlikely]] {
159 return -2.0f;
160 }
161 if (value >= float(points_num)) [[unlikely]] {
162 return float(points_num);
163 }
164 return value;
165}
166
168 float co[3],
169 float weight)
170{
171 float *latticedata = lattice_deform_data->latticedata;
172 float *lattice_weights = lattice_deform_data->lattice_weights;
173 BLI_assert(latticedata);
174 const Lattice *lt = lattice_deform_data->lt;
175 float u, v, w, tu[4], tv[4], tw[4];
176 float vec[3];
177 int idx_w, idx_v, idx_u;
178 int ui, vi, wi, uu, vv, ww;
179
180 /* vgroup influence */
181 float co_prev[4] = {0}, weight_blend = 0.0f;
182 copy_v3_v3(co_prev, co);
183#if BLI_HAVE_SSE2
184 __m128 co_vec = _mm_loadu_ps(co_prev);
185#endif
186
187 /* co is in local coords, treat with latmat */
188 mul_v3_m4v3(vec, lattice_deform_data->latmat, co);
189
190 /* u v w coords */
191
192 if (lt->pntsu > 1) {
193 u = lattice_deform_axis_clamp((vec[0] - lt->fu) / lt->du, lt->pntsu);
194 ui = int(floor(u));
195 u -= ui;
197 }
198 else {
199 tu[0] = tu[2] = tu[3] = 0.0;
200 tu[1] = 1.0;
201 ui = 0;
202 }
203
204 if (lt->pntsv > 1) {
205 v = lattice_deform_axis_clamp((vec[1] - lt->fv) / lt->dv, lt->pntsv);
206 vi = int(floor(v));
207 v -= vi;
209 }
210 else {
211 tv[0] = tv[2] = tv[3] = 0.0;
212 tv[1] = 1.0;
213 vi = 0;
214 }
215
216 if (lt->pntsw > 1) {
217 w = lattice_deform_axis_clamp((vec[2] - lt->fw) / lt->dw, lt->pntsw);
218 wi = int(floor(w));
219 w -= wi;
221 }
222 else {
223 tw[0] = tw[2] = tw[3] = 0.0;
224 tw[1] = 1.0;
225 wi = 0;
226 }
227
228 const int w_stride = lt->pntsu * lt->pntsv;
229 const int idx_w_max = (lt->pntsw - 1) * lt->pntsu * lt->pntsv;
230 const int v_stride = lt->pntsu;
231 const int idx_v_max = (lt->pntsv - 1) * lt->pntsu;
232 const int idx_u_max = (lt->pntsu - 1);
233
234 for (ww = wi - 1; ww <= wi + 2; ww++) {
235 w = weight * tw[ww - wi + 1];
236 idx_w = std::clamp(ww * w_stride, 0, idx_w_max);
237 for (vv = vi - 1; vv <= vi + 2; vv++) {
238 v = w * tv[vv - vi + 1];
239 idx_v = std::clamp(vv * v_stride, 0, idx_v_max);
240 for (uu = ui - 1; uu <= ui + 2; uu++) {
241 u = v * tu[uu - ui + 1];
242 idx_u = std::clamp(uu, 0, idx_u_max);
243 const int idx = idx_w + idx_v + idx_u;
244#if BLI_HAVE_SSE2
245 {
246 __m128 weight_vec = _mm_set1_ps(u);
247 /* We need to address special case for last item to avoid accessing invalid memory. */
248 __m128 lattice_vec;
249 if (idx * 3 == idx_w_max) {
250 copy_v3_v3(reinterpret_cast<float *>(&lattice_vec), &latticedata[idx * 3]);
251 }
252 else {
253 /* When not on last item, we can safely access one extra float, it will be ignored
254 * anyway. */
255 lattice_vec = _mm_loadu_ps(&latticedata[idx * 3]);
256 }
257 co_vec = _mm_add_ps(co_vec, _mm_mul_ps(lattice_vec, weight_vec));
258 }
259#else
260 madd_v3_v3fl(co, &latticedata[idx * 3], u);
261#endif
262 if (lattice_weights) {
263 weight_blend += (u * lattice_weights[idx]);
264 }
265 }
266 }
267 }
268#if BLI_HAVE_SSE2
269 {
270 copy_v3_v3(co, reinterpret_cast<float *>(&co_vec));
271 }
272#endif
273
274 if (lattice_weights) {
275 interp_v3_v3v3(co, co_prev, co, weight_blend);
276 }
277}
278
280{
281 if (lattice_deform_data->latticedata) {
282 MEM_delete(lattice_deform_data->latticedata);
283 }
284
285 MEM_delete(lattice_deform_data);
286}
287
289
290/* -------------------------------------------------------------------- */
295
309
311 const int index,
312 const MDeformVert *dvert)
313{
314 if (dvert != nullptr) {
315 const float weight = data->invert_vgroup ?
316 1.0f - BKE_defvert_find_weight(dvert, data->defgrp_index) :
317 BKE_defvert_find_weight(dvert, data->defgrp_index);
318 if (weight > 0.0f) {
320 data->lattice_deform_data, data->vert_coords[index], weight * data->fac);
321 }
322 }
323 else {
325 data->lattice_deform_data, data->vert_coords[index], data->fac);
326 }
327}
328
329static void lattice_deform_vert_task(void *__restrict userdata,
330 const int index,
331 const TaskParallelTLS *__restrict /*tls*/)
332{
333 const LatticeDeformUserdata *data = static_cast<const LatticeDeformUserdata *>(userdata);
334 lattice_deform_vert_with_dvert(data, index, data->dvert ? &data->dvert[index] : nullptr);
335}
336
337static void lattice_vert_task_editmesh(void *__restrict userdata,
338 MempoolIterData *iter,
339 const TaskParallelTLS *__restrict /*tls*/)
340{
341 const LatticeDeformUserdata *data = static_cast<const LatticeDeformUserdata *>(userdata);
342 BMVert *v = reinterpret_cast<BMVert *>(iter);
343 MDeformVert *dvert = static_cast<MDeformVert *>(
344 BM_ELEM_CD_GET_VOID_P(v, data->bmesh.cd_dvert_offset));
346}
347
348static void lattice_vert_task_editmesh_no_dvert(void *__restrict userdata,
349 MempoolIterData *iter,
350 const TaskParallelTLS *__restrict /*tls*/)
351{
352 const LatticeDeformUserdata *data = static_cast<const LatticeDeformUserdata *>(userdata);
353 BMVert *v = reinterpret_cast<BMVert *>(iter);
355}
356
357static void lattice_deform_coords_impl(const Object *ob_lattice,
358 const Object *ob_target,
359 float (*vert_coords)[3],
360 const int vert_coords_len,
361 const short flag,
362 const char *defgrp_name,
363 const float fac,
364 const Mesh *me_target,
365 const BMesh *bm_target)
366{
367 LatticeDeformData *lattice_deform_data;
368 const MDeformVert *dvert = nullptr;
369 int defgrp_index = -1;
370 int cd_dvert_offset = -1;
371 BMesh *bm = const_cast<BMesh *>(bm_target);
372
373 if (ob_lattice->type != OB_LATTICE) {
374 return;
375 }
376
377 lattice_deform_data = BKE_lattice_deform_data_create(ob_lattice, ob_target);
378
379 /* Check whether to use vertex groups (only possible if ob_target is a Mesh or Lattice).
380 * We want either a Mesh/Lattice with no derived data, or derived data with deformverts.
381 */
382 if (defgrp_name && defgrp_name[0] && ob_target && ELEM(ob_target->type, OB_MESH, OB_LATTICE)) {
383 defgrp_index = BKE_id_defgroup_name_index(me_target ? &me_target->id : ob_target->data,
384 defgrp_name);
385
386 if (defgrp_index != -1) {
387 /* if there's derived data without deformverts, don't use vgroups */
388 if (bm) {
389 cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
390 }
391 else if (me_target) {
392 dvert = me_target->deform_verts().data();
393 }
394 else if (ob_target->type == OB_LATTICE) {
395 dvert = (id_cast<Lattice *>(ob_target->data))->dvert;
396 }
397 else {
398 dvert = (id_cast<Mesh *>(ob_target->data))->deform_verts().data();
399 }
400 }
401 }
402
404 data.lattice_deform_data = lattice_deform_data;
405 data.vert_coords = vert_coords;
406 data.dvert = dvert;
407 data.defgrp_index = defgrp_index;
408 data.fac = fac;
409 data.invert_vgroup = (flag & MOD_LATTICE_INVERT_VGROUP) != 0;
410 data.bmesh.cd_dvert_offset = cd_dvert_offset;
411
412 if (bm != nullptr) {
413 /* While this could cause an extra loop over mesh data, in most cases this will
414 * have already been properly set. */
416
417 TaskParallelSettings settings;
419
420 if (cd_dvert_offset != -1) {
422 }
423 else {
425 }
426 }
427 else {
428 TaskParallelSettings settings;
430 settings.min_iter_per_thread = 32;
431 BLI_task_parallel_range(0, vert_coords_len, &data, lattice_deform_vert_task, &settings);
432 }
433
434 BKE_lattice_deform_data_destroy(lattice_deform_data);
435}
436
437void BKE_lattice_deform_coords(const Object *ob_lattice,
438 const Object *ob_target,
439 float (*vert_coords)[3],
440 const int vert_coords_len,
441 const short flag,
442 const char *defgrp_name,
443 float fac)
444{
446 ob_target,
447 vert_coords,
448 vert_coords_len,
449 flag,
450 defgrp_name,
451 fac,
452 nullptr,
453 nullptr);
454}
455
457 const Object *ob_target,
458 float (*vert_coords)[3],
459 const int vert_coords_len,
460 const short flag,
461 const char *defgrp_name,
462 const float fac,
463 const Mesh *me_target)
464{
466 ob_target,
467 vert_coords,
468 vert_coords_len,
469 flag,
470 defgrp_name,
471 fac,
472 me_target,
473 nullptr);
474}
475
477 const Object *ob_target,
478 float (*vert_coords)[3],
479 const int vert_coords_len,
480 const short flag,
481 const char *defgrp_name,
482 const float fac,
483 const BMesh &bm_target)
484{
486 ob_target,
487 vert_coords,
488 vert_coords_len,
489 flag,
490 defgrp_name,
491 fac,
492 nullptr,
493 &bm_target);
494}
495
497
498} // namespace blender
support for deformation groups and hooks.
display list (or rather multi purpose list) stuff.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define ELEM(...)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define floor
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void BKE_lattice_deform_data_eval_co(LatticeDeformData *lattice_deform_data, float co[3], float weight)
void BKE_lattice_deform_coords_with_mesh(const Object *ob_lattice, const Object *ob_target, float(*vert_coords)[3], int vert_coords_len, short flag, const char *defgrp_name, float fac, const Mesh *me_target)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
static void lattice_deform_coords_impl(const Object *ob_lattice, const Object *ob_target, float(*vert_coords)[3], const int vert_coords_len, const short flag, const char *defgrp_name, const float fac, const Mesh *me_target, const BMesh *bm_target)
DispList * BKE_displist_find(ListBaseT< DispList > *lb, int type)
Definition displist.cc:75
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
ATTR_WARN_UNUSED_RESULT const BMVert * v
Lattice * BKE_object_get_lattice(const Object *object)
Definition object.cc:4545
int BKE_id_defgroup_name_index(const ID *id, StringRef name)
Definition deform.cc:557
BLI_INLINE void BLI_parallel_mempool_settings_defaults(TaskParallelSettings *settings)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void BLI_task_parallel_mempool(struct BLI_mempool *mempool, void *userdata, TaskParallelMempoolFunc func, const TaskParallelSettings *settings)
static void lattice_deform_vert_task(void *__restrict userdata, const int index, const TaskParallelTLS *__restrict)
BMesh const char void * data
static void lattice_deform_vert_with_dvert(const LatticeDeformUserdata *data, const int index, const MDeformVert *dvert)
MDeformVert * BKE_lattice_deform_verts_get(const Object *oblatt)
Definition lattice.cc:583
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void BKE_lattice_deform_coords_with_editmesh(const Object *ob_lattice, const Object *ob_target, float(*vert_coords)[3], int vert_coords_len, short flag, const char *defgrp_name, float fac, const BMesh &bm_target)
void BKE_lattice_deform_coords(const Object *ob_lattice, const Object *ob_target, float(*vert_coords)[3], int vert_coords_len, short flag, const char *defgrp_name, float fac)
static void lattice_vert_task_editmesh(void *__restrict userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict)
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
void BKE_lattice_deform_data_destroy(LatticeDeformData *lattice_deform_data)
static void lattice_vert_task_editmesh_no_dvert(void *__restrict userdata, MempoolIterData *iter, const TaskParallelTLS *__restrict)
signed int int32_t
Definition BKE_text.h:78
void key_curve_position_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:310
void copy_m4_m4(float m1[4][4], const float m2[4][4])
LatticeDeformData * BKE_lattice_deform_data_create(const Object *oblatt, const Object *ob) ATTR_WARN_UNUSED_RESULT
static float lattice_deform_axis_clamp(const float value, const int points_num)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< DispList > disp
Definition BKE_curve.hh:44
struct blender::LatticeDeformUserdata::@270046216207341250376143075263164043134151242214 bmesh
LatticeDeformData * lattice_deform_data
struct BPoint * def
bke::ObjectRuntime * runtime