Blender  V3.3
editlattice_tools.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "BLI_math.h"
11 #include "BLI_utildefines.h"
12 
13 #include "DNA_curve_types.h"
14 #include "DNA_lattice_types.h"
15 #include "DNA_object_types.h"
16 #include "DNA_scene_types.h"
17 
18 #include "RNA_access.h"
19 #include "RNA_define.h"
20 
21 #include "BKE_context.h"
22 #include "BKE_lattice.h"
23 #include "BKE_layer.h"
24 
25 #include "DEG_depsgraph.h"
26 
27 #include "ED_screen.h"
28 
29 #include "WM_api.h"
30 #include "WM_types.h"
31 
32 #include "lattice_intern.h"
33 
34 /* -------------------------------------------------------------------- */
39 {
40  Object *ob;
41 
43  return true;
44  }
45 
47  return (ob && ob->type == OB_LATTICE);
48 }
49 
51 {
52  ViewLayer *view_layer = CTX_data_view_layer(C);
53  View3D *v3d = CTX_wm_view3d(C);
54  const bool is_editmode = CTX_data_edit_object(C) != NULL;
55 
56  if (is_editmode) {
57  uint objects_len;
59  view_layer, CTX_wm_view3d(C), &objects_len);
60  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
61  Object *ob = objects[ob_index];
62  Lattice *lt = ob->data;
63 
64  if (lt->editlatt->latt == NULL) {
65  continue;
66  }
67 
68  BKE_lattice_resize(lt->editlatt->latt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
69 
72  }
73  MEM_freeN(objects);
74  }
75  else {
76  FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob) {
77  if (ob->type != OB_LATTICE) {
78  continue;
79  }
80 
81  Lattice *lt = ob->data;
82  BKE_lattice_resize(lt, lt->pntsu, lt->pntsv, lt->pntsw, NULL);
83 
85  WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
86  }
88  }
89  return OPERATOR_FINISHED;
90 }
91 
93 {
94  /* identifiers */
95  ot->name = "Make Regular";
96  ot->description = "Set UVW control points a uniform distance apart";
97  ot->idname = "LATTICE_OT_make_regular";
98 
99  /* api callbacks */
102 
103  /* flags */
105 }
106 
109 /* -------------------------------------------------------------------- */
113 /* flipping options */
114 typedef enum eLattice_FlipAxes {
119 
126  Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
127 {
128  BPoint *bp;
129  float diff;
130 
131  /* just the point in the middle (unpaired) */
132  bp = &lt->def[BKE_lattice_index_from_uvw(lt, u, v, w)];
133 
134  /* flip over axis */
135  diff = mid - bp->vec[axis];
136  bp->vec[axis] = mid + diff;
137 }
138 
144  Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
145 {
146  BPoint *bpA, *bpB;
147 
148  int numU = lt->pntsu;
149  int numV = lt->pntsv;
150  int numW = lt->pntsw;
151 
152  int u0 = u, u1 = u;
153  int v0 = v, v1 = v;
154  int w0 = w, w1 = w;
155 
156  /* get pair index by just overriding the relevant pair-value
157  * - "-1" else buffer overflow
158  */
159  switch (axis) {
160  case LATTICE_FLIP_U:
161  u1 = numU - u - 1;
162  break;
163  case LATTICE_FLIP_V:
164  v1 = numV - v - 1;
165  break;
166  case LATTICE_FLIP_W:
167  w1 = numW - w - 1;
168  break;
169  }
170 
171  /* get points to operate on */
172  bpA = &lt->def[BKE_lattice_index_from_uvw(lt, u0, v0, w0)];
173  bpB = &lt->def[BKE_lattice_index_from_uvw(lt, u1, v1, w1)];
174 
175  /* Swap all coordinates, so that flipped coordinates belong to
176  * the indices on the correct side of the lattice.
177  *
178  * Coords: (-2 4) |0| (3 4) --> (3 4) |0| (-2 4)
179  * Indices: (0,L) (1,R) --> (0,L) (1,R)
180  */
181  swap_v3_v3(bpA->vec, bpB->vec);
182 
183  /* However, we need to mirror the coordinate values on the axis we're dealing with,
184  * otherwise we'd have effectively only rotated the points around. If we don't do this,
185  * we'd just be reimplementing the naive mirroring algorithm, which causes unwanted deforms
186  * such as flipped normals, etc.
187  *
188  * Coords: (3 4) |0| (-2 4) --\
189  * \-> (-3 4) |0| (2 4)
190  * Indices: (0,L) (1,R) --> (0,L) (1,R)
191  */
192  lattice_flip_point_value(lt, u0, v0, w0, mid, axis);
193  lattice_flip_point_value(lt, u1, v1, w1, mid, axis);
194 }
195 
197 {
198  ViewLayer *view_layer = CTX_data_view_layer(C);
199  uint objects_len;
200  bool changed = false;
201  const eLattice_FlipAxes axis = RNA_enum_get(op->ptr, "axis");
202 
204  view_layer, CTX_wm_view3d(C), &objects_len);
205  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
206  Object *obedit = objects[ob_index];
207  Lattice *lt;
208 
209  int numU, numV, numW;
210  int totP;
211 
212  float mid = 0.0f;
213  short isOdd = 0;
214 
215  /* get lattice - we need the "edit lattice" from the lattice... confusing... */
216  lt = (Lattice *)obedit->data;
217  lt = lt->editlatt->latt;
218 
219  numU = lt->pntsu;
220  numV = lt->pntsv;
221  numW = lt->pntsw;
222  totP = numU * numV * numW;
223 
224  /* First Pass: determine midpoint - used for flipping center verts if there
225  * are odd number of points on axis */
226  switch (axis) {
227  case LATTICE_FLIP_U:
228  isOdd = numU & 1;
229  break;
230  case LATTICE_FLIP_V:
231  isOdd = numV & 1;
232  break;
233  case LATTICE_FLIP_W:
234  isOdd = numW & 1;
235  break;
236 
237  default:
238  printf("lattice_flip(): Unknown flipping axis (%u)\n", axis);
239  return OPERATOR_CANCELLED;
240  }
241 
242  if (isOdd) {
243  BPoint *bp;
244  float avgInv = 1.0f / (float)totP;
245  int i;
246 
247  /* midpoint calculation - assuming that u/v/w are axis-aligned */
248  for (i = 0, bp = lt->def; i < totP; i++, bp++) {
249  mid += bp->vec[axis] * avgInv;
250  }
251  }
252 
253  /* Second Pass: swap pairs of vertices per axis, assuming they are all sorted */
254  switch (axis) {
255  case LATTICE_FLIP_U: {
256  int u, v, w;
257 
258  /* v/w strips - front to back, top to bottom */
259  for (w = 0; w < numW; w++) {
260  for (v = 0; v < numV; v++) {
261  /* swap coordinates of pairs of vertices on u */
262  for (u = 0; u < (numU / 2); u++) {
263  lattice_swap_point_pairs(lt, u, v, w, mid, axis);
264  }
265 
266  /* flip u-coordinate of midpoint (i.e. unpaired point on u) */
267  if (isOdd) {
268  u = (numU / 2);
269  lattice_flip_point_value(lt, u, v, w, mid, axis);
270  }
271  }
272  }
273  break;
274  }
275  case LATTICE_FLIP_V: {
276  int u, v, w;
277 
278  /* u/w strips - front to back, left to right */
279  for (w = 0; w < numW; w++) {
280  for (u = 0; u < numU; u++) {
281  /* swap coordinates of pairs of vertices on v */
282  for (v = 0; v < (numV / 2); v++) {
283  lattice_swap_point_pairs(lt, u, v, w, mid, axis);
284  }
285 
286  /* flip v-coordinate of midpoint (i.e. unpaired point on v) */
287  if (isOdd) {
288  v = (numV / 2);
289  lattice_flip_point_value(lt, u, v, w, mid, axis);
290  }
291  }
292  }
293  break;
294  }
295  case LATTICE_FLIP_W: {
296  int u, v, w;
297 
298  for (v = 0; v < numV; v++) {
299  for (u = 0; u < numU; u++) {
300  /* swap coordinates of pairs of vertices on w */
301  for (w = 0; w < (numW / 2); w++) {
302  lattice_swap_point_pairs(lt, u, v, w, mid, axis);
303  }
304 
305  /* flip w-coordinate of midpoint (i.e. unpaired point on w) */
306  if (isOdd) {
307  w = (numW / 2);
308  lattice_flip_point_value(lt, u, v, w, mid, axis);
309  }
310  }
311  }
312  break;
313  }
314  default: /* shouldn't happen, but just in case */
315  break;
316  }
317 
318  /* updates */
321  changed = true;
322  }
323  MEM_freeN(objects);
324 
325  return changed ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
326 }
327 
329 {
330  static const EnumPropertyItem flip_items[] = {
331  {LATTICE_FLIP_U, "U", 0, "U (X) Axis", ""},
332  {LATTICE_FLIP_V, "V", 0, "V (Y) Axis", ""},
333  {LATTICE_FLIP_W, "W", 0, "W (Z) Axis", ""},
334  {0, NULL, 0, NULL, NULL},
335  };
336 
337  /* identifiers */
338  ot->name = "Flip (Distortion Free)";
339  ot->description = "Mirror all control points without inverting the lattice deform";
340  ot->idname = "LATTICE_OT_flip";
341 
342  /* api callbacks */
346 
347  /* flags */
349 
350  /* properties */
351  ot->prop = RNA_def_enum(ot->srna,
352  "axis",
353  flip_items,
355  "Flip Axis",
356  "Coordinates along this axis get flipped");
357 }
358 
typedef float(TangentPoint)[2]
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1370
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1353
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:784
int BKE_lattice_index_from_uvw(struct Lattice *lt, int u, int v, int w)
Definition: lattice.c:205
void BKE_lattice_resize(struct Lattice *lt, int u, int v, int w, struct Object *ltOb)
Definition: lattice.c:280
#define FOREACH_SELECTED_OBJECT_BEGIN(_view_layer, _v3d, _instance)
Definition: BKE_layer.h:303
#define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:542
#define FOREACH_SELECTED_OBJECT_END
Definition: BKE_layer.h:315
MINLINE void swap_v3_v3(float a[3], float b[3])
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNUSED(x)
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
Object is a sort of wrapper for general info.
@ OB_LATTICE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_operator_editlattice(struct bContext *C)
Definition: screen_ops.c:644
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble u1
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define NC_GEOM
Definition: WM_types.h:343
#define ND_DATA
Definition: WM_types.h:456
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
eLattice_FlipAxes
@ LATTICE_FLIP_W
@ LATTICE_FLIP_V
@ LATTICE_FLIP_U
static bool make_regular_poll(bContext *C)
static int make_regular_exec(bContext *C, wmOperator *UNUSED(op))
static void lattice_flip_point_value(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
void LATTICE_OT_flip(wmOperatorType *ot)
static void lattice_swap_point_pairs(Lattice *lt, int u, int v, int w, float mid, eLattice_FlipAxes axis)
static int lattice_flip_exec(bContext *C, wmOperator *op)
void LATTICE_OT_make_regular(wmOperatorType *ot)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
float vec[4]
struct Lattice * latt
struct EditLatt * editlatt
struct BPoint * def
void * data
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))