Blender V5.3
editlattice_tools.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
8
10
11#include "DNA_curve_types.h"
12#include "DNA_lattice_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "RNA_access.hh"
17#include "RNA_define.hh"
18
19#include "BKE_context.hh"
20#include "BKE_lattice.hh"
21#include "BKE_layer.hh"
22
23#include "DEG_depsgraph.hh"
24
25#include "ED_object.hh"
26#include "ED_screen.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "lattice_intern.hh"
32
33namespace blender {
34
35/* -------------------------------------------------------------------- */
38
40{
41 Object *ob;
42
44 return true;
45 }
46
48 return (ob && ob->type == OB_LATTICE);
49}
50
52{
53 const Main *bmain = CTX_data_main(C);
54 const Scene *scene = CTX_data_scene(C);
55 ViewLayer *view_layer = CTX_data_view_layer(C);
56 View3D *v3d = CTX_wm_view3d(C);
57 const bool is_editmode = CTX_data_edit_object(C) != nullptr;
58
59 if (is_editmode) {
61 *bmain, scene, view_layer, CTX_wm_view3d(C));
62 for (Object *ob : objects) {
63 Lattice *lt = id_cast<Lattice *>(ob->data);
64
65 if (lt->editlatt->latt == nullptr) {
66 continue;
67 }
68
70 continue;
71 }
72
73 BKE_lattice_resize(lt->editlatt->latt, lt->pntsu, lt->pntsv, lt->pntsw, nullptr);
74
77 }
78 }
79 else {
80 FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob) {
81 if (ob->type != OB_LATTICE) {
82 continue;
83 }
84
85 Lattice *lt = id_cast<Lattice *>(ob->data);
86 BKE_lattice_resize(lt, lt->pntsu, lt->pntsv, lt->pntsw, nullptr);
87
90 }
92 }
93 return OPERATOR_FINISHED;
94}
95
97{
98 /* identifiers */
99 ot->name = "Make Regular";
100 ot->description = "Set UVW control points a uniform distance apart";
101 ot->idname = "LATTICE_OT_make_regular";
102
103 /* API callbacks. */
106
107 /* flags */
109}
110
112
113/* -------------------------------------------------------------------- */
116
117/* flipping options */
123
130 Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
131{
132 BPoint *bp;
133 float diff;
134
135 /* just the point in the middle (unpaired) */
136 bp = &lt->def[BKE_lattice_index_from_uvw(lt, u, v, w)];
137
138 /* flip over axis */
139 diff = mid - bp->vec[axis];
140 bp->vec[axis] = mid + diff;
141}
142
148 Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
149{
150 BPoint *bpA, *bpB;
151
152 int numU = lt->pntsu;
153 int numV = lt->pntsv;
154 int numW = lt->pntsw;
155
156 int u0 = u, u1 = u;
157 int v0 = v, v1 = v;
158 int w0 = w, w1 = w;
159
160 /* get pair index by just overriding the relevant pair-value
161 * - "-1" else buffer overflow
162 */
163 switch (axis) {
164 case LATTICE_FLIP_U:
165 u1 = numU - u - 1;
166 break;
167 case LATTICE_FLIP_V:
168 v1 = numV - v - 1;
169 break;
170 case LATTICE_FLIP_W:
171 w1 = numW - w - 1;
172 break;
173 }
174
175 /* get points to operate on */
176 bpA = &lt->def[BKE_lattice_index_from_uvw(lt, u0, v0, w0)];
177 bpB = &lt->def[BKE_lattice_index_from_uvw(lt, u1, v1, w1)];
178
179 /* Swap all coordinates, so that flipped coordinates belong to
180 * the indices on the correct side of the lattice.
181 *
182 * Coords: (-2 4) |0| (3 4) --> (3 4) |0| (-2 4)
183 * Indices: (0,L) (1,R) --> (0,L) (1,R)
184 */
185 swap_v3_v3(bpA->vec, bpB->vec);
186
187 /* However, we need to mirror the coordinate values on the axis we're dealing with,
188 * otherwise we'd have effectively only rotated the points around. If we don't do this,
189 * we'd just be reimplementing the naive mirroring algorithm, which causes unwanted deforms
190 * such as flipped normals, etc.
191 *
192 * Coords: (3 4) |0| (-2 4) --\
193 * \-> (-3 4) |0| (2 4)
194 * Indices: (0,L) (1,R) --> (0,L) (1,R)
195 */
196 lattice_flip_point_value(lt, u0, v0, w0, mid, axis);
197 lattice_flip_point_value(lt, u1, v1, w1, mid, axis);
198}
199
201{
202 const Main *bmain = CTX_data_main(C);
203 const Scene *scene = CTX_data_scene(C);
204 ViewLayer *view_layer = CTX_data_view_layer(C);
205 bool changed = false;
206 const eLattice_FlipAxes axis = eLattice_FlipAxes(RNA_enum_get(op->ptr, "axis"));
207
209 *bmain, scene, view_layer, CTX_wm_view3d(C));
210 for (Object *obedit : objects) {
211 Lattice *lt;
212
213 int numU, numV, numW;
214 int totP;
215
216 float mid = 0.0f;
217 short isOdd = 0;
218
219 /* get lattice - we need the "edit lattice" from the lattice... confusing... */
220 lt = id_cast<Lattice *>(obedit->data);
221 lt = lt->editlatt->latt;
222
224 continue;
225 }
226
227 numU = lt->pntsu;
228 numV = lt->pntsv;
229 numW = lt->pntsw;
230 totP = numU * numV * numW;
231
232 /* First Pass: determine midpoint - used for flipping center verts if there
233 * are odd number of points on axis */
234 switch (axis) {
235 case LATTICE_FLIP_U:
236 isOdd = numU & 1;
237 break;
238 case LATTICE_FLIP_V:
239 isOdd = numV & 1;
240 break;
241 case LATTICE_FLIP_W:
242 isOdd = numW & 1;
243 break;
244
245 default:
246 printf("lattice_flip(): Unknown flipping axis (%d)\n", axis);
247 return OPERATOR_CANCELLED;
248 }
249
250 if (isOdd) {
251 BPoint *bp;
252 float avgInv = 1.0f / float(totP);
253 int i;
254
255 /* midpoint calculation - assuming that u/v/w are axis-aligned */
256 for (i = 0, bp = lt->def; i < totP; i++, bp++) {
257 mid += bp->vec[axis] * avgInv;
258 }
259 }
260
261 /* Second Pass: swap pairs of vertices per axis, assuming they are all sorted */
262 switch (axis) {
263 case LATTICE_FLIP_U: {
264 int u, v, w;
265
266 /* v/w strips - front to back, top to bottom */
267 for (w = 0; w < numW; w++) {
268 for (v = 0; v < numV; v++) {
269 /* swap coordinates of pairs of vertices on u */
270 for (u = 0; u < (numU / 2); u++) {
271 lattice_swap_point_pairs(lt, u, v, w, mid, axis);
272 }
273
274 /* flip u-coordinate of midpoint (i.e. unpaired point on u) */
275 if (isOdd) {
276 u = (numU / 2);
277 lattice_flip_point_value(lt, u, v, w, mid, axis);
278 }
279 }
280 }
281 break;
282 }
283 case LATTICE_FLIP_V: {
284 int u, v, w;
285
286 /* u/w strips - front to back, left to right */
287 for (w = 0; w < numW; w++) {
288 for (u = 0; u < numU; u++) {
289 /* swap coordinates of pairs of vertices on v */
290 for (v = 0; v < (numV / 2); v++) {
291 lattice_swap_point_pairs(lt, u, v, w, mid, axis);
292 }
293
294 /* flip v-coordinate of midpoint (i.e. unpaired point on v) */
295 if (isOdd) {
296 v = (numV / 2);
297 lattice_flip_point_value(lt, u, v, w, mid, axis);
298 }
299 }
300 }
301 break;
302 }
303 case LATTICE_FLIP_W: {
304 int u, v, w;
305
306 for (v = 0; v < numV; v++) {
307 for (u = 0; u < numU; u++) {
308 /* swap coordinates of pairs of vertices on w */
309 for (w = 0; w < (numW / 2); w++) {
310 lattice_swap_point_pairs(lt, u, v, w, mid, axis);
311 }
312
313 /* flip w-coordinate of midpoint (i.e. unpaired point on w) */
314 if (isOdd) {
315 w = (numW / 2);
316 lattice_flip_point_value(lt, u, v, w, mid, axis);
317 }
318 }
319 }
320 break;
321 }
322 default: /* shouldn't happen, but just in case */
323 break;
324 }
325
326 /* updates */
328 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
329 changed = true;
330 }
331
332 return changed ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
333}
334
336{
337 static const EnumPropertyItem flip_items[] = {
338 {LATTICE_FLIP_U, "U", 0, "U (X) Axis", ""},
339 {LATTICE_FLIP_V, "V", 0, "V (Y) Axis", ""},
340 {LATTICE_FLIP_W, "W", 0, "W (Z) Axis", ""},
341 {0, nullptr, 0, nullptr, nullptr},
342 };
343
344 /* identifiers */
345 ot->name = "Flip (Distortion Free)";
346 ot->description = "Mirror all control points without inverting the lattice deform";
347 ot->idname = "LATTICE_OT_flip";
348
349 /* API callbacks. */
353
354 /* flags */
356
357 /* properties */
358 ot->prop = RNA_def_enum(ot->srna,
359 "axis",
360 flip_items,
362 "Flip Axis",
363 "Coordinates along this axis get flipped");
364}
365
367
368} // namespace blender
#define FOREACH_SELECTED_OBJECT_BEGIN(_view_layer, _v3d, _instance)
Definition BKE_layer.hh:374
#define FOREACH_SELECTED_OBJECT_END
Definition BKE_layer.hh:386
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
BMesh Iterators.
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BKE_lattice_resize(Lattice *lt, int u_new, int v_new, int w_new, Object *lt_ob)
Definition lattice.cc:277
static wmOperatorStatus make_regular_exec(bContext *C, wmOperator *op)
bool ED_operator_editlattice(bContext *C)
void LATTICE_OT_flip(wmOperatorType *ot)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
ATTR_WARN_UNUSED_RESULT const BMVert * v
ViewLayer * CTX_data_view_layer(const bContext *C)
static wmOperatorStatus lattice_flip_exec(bContext *C, wmOperator *op)
void DEG_id_tag_update(ID *id, unsigned int flags)
MINLINE void swap_v3_v3(float a[3], float b[3])
Object * CTX_data_edit_object(const bContext *C)
int BKE_lattice_index_from_uvw(const Lattice *lt, int u, int v, int w)
Definition lattice.cc:199
static void lattice_flip_point_value(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void lattice_swap_point_pairs(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static bool make_regular_poll(bContext *C)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void LATTICE_OT_make_regular(wmOperatorType *ot)
Object * CTX_data_active_object(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct Lattice * latt
struct EditLatt * editlatt
struct BPoint * def
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports