Blender  V2.93
object_warp.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 by Blender Foundation
17  * All rights reserved.
18  */
19 
24 #include "DNA_object_types.h"
25 #include "DNA_scene_types.h"
26 #include "DNA_view3d_types.h"
27 
28 #include "BLI_math.h"
29 
30 #include "BKE_context.h"
31 
32 #include "RNA_access.h"
33 #include "RNA_define.h"
34 
35 #include "WM_api.h"
36 #include "WM_types.h"
37 
38 #include "ED_transverts.h"
39 
40 #include "object_intern.h"
41 
42 static void object_warp_calc_view_matrix(float r_mat_view[4][4],
43  float r_center_view[3],
44  Object *obedit,
45  const float viewmat[4][4],
46  const float center[3],
47  const float offset_angle)
48 {
49  float mat_offset[4][4];
50  float viewmat_roll[4][4];
51 
52  /* apply the rotation offset by rolling the view */
53  axis_angle_to_mat4_single(mat_offset, 'Z', offset_angle);
54  mul_m4_m4m4(viewmat_roll, mat_offset, viewmat);
55 
56  /* apply the view and the object matrix */
57  mul_m4_m4m4(r_mat_view, viewmat_roll, obedit->obmat);
58 
59  /* get the view-space cursor */
60  mul_v3_m4v3(r_center_view, viewmat_roll, center);
61 }
62 
64  const float mat_view[4][4],
65  const float center_view[3],
66  float *r_min,
67  float *r_max)
68 {
69  /* no need to apply translation and cursor offset for every vertex, delay this */
70  const float x_ofs = (mat_view[3][0] - center_view[0]);
71  float min = FLT_MAX, max = -FLT_MAX;
72 
73  TransVert *tv = tvs->transverts;
74  for (int i = 0; i < tvs->transverts_tot; i++, tv++) {
75  float val;
76 
77  /* Convert object-space to view-space. */
78  val = dot_m4_v3_row_x(mat_view, tv->loc);
79 
80  min = min_ff(min, val);
81  max = max_ff(max, val);
82  }
83 
84  *r_min = min + x_ofs;
85  *r_max = max + x_ofs;
86 }
87 
89  const float mat_view[4][4],
90  const float center_view[3],
91  const float angle_,
92  const float min,
93  const float max)
94 {
95  TransVert *tv;
96  const float angle = -angle_;
97  /* cache vars for tiny speedup */
98 #if 1
99  const float range = max - min;
100  const float range_inv = 1.0f / range;
101  const float min_ofs = min + (0.5f * range);
102 #endif
103 
104  float dir_min[2], dir_max[2];
105  float imat_view[4][4];
106 
107  invert_m4_m4(imat_view, mat_view);
108 
109  /* calculate the direction vectors outside min/max range */
110  {
111  const float phi = angle * 0.5f;
112 
113  dir_max[0] = cosf(phi);
114  dir_max[1] = sinf(phi);
115 
116  dir_min[0] = -dir_max[0];
117  dir_min[1] = dir_max[1];
118  }
119 
120  tv = tvs->transverts;
121  for (int i = 0; i < tvs->transverts_tot; i++, tv++) {
122  float co[3], co_add[2];
123  float val, phi;
124 
125  /* Convert object-space to view-space. */
126  mul_v3_m4v3(co, mat_view, tv->loc);
127  sub_v2_v2(co, center_view);
128 
129  val = co[0];
130  /* is overwritten later anyway */
131  // co[0] = 0.0f;
132 
133  if (val < min) {
134  mul_v2_v2fl(co_add, dir_min, min - val);
135  val = min;
136  }
137  else if (val > max) {
138  mul_v2_v2fl(co_add, dir_max, val - max);
139  val = max;
140  }
141  else {
142  zero_v2(co_add);
143  }
144 
145  /* map from x axis to (-0.5 - 0.5) */
146 #if 0
147  val = ((val - min) / (max - min)) - 0.5f;
148 #else
149  val = (val - min_ofs) * range_inv;
150 #endif
151 
152  /* convert the x axis into a rotation */
153  phi = val * angle;
154 
155  co[0] = -sinf(phi) * co[1];
156  co[1] = cosf(phi) * co[1];
157 
158  add_v2_v2(co, co_add);
159 
160  /* Convert view-space to object-space. */
161  add_v2_v2(co, center_view);
162  mul_v3_m4v3(tv->loc, imat_view, co);
163  }
164 }
165 
167 {
168  const float warp_angle = RNA_float_get(op->ptr, "warp_angle");
169  const float offset_angle = RNA_float_get(op->ptr, "offset_angle");
170 
171  TransVertStore tvs = {NULL};
172  Object *obedit = CTX_data_edit_object(C);
173 
174  /* typically from 'rv3d' and 3d cursor */
175  float viewmat[4][4];
176  float center[3];
177 
178  /* 'viewmat' relative vars */
179  float mat_view[4][4];
180  float center_view[3];
181 
182  float min, max;
183 
185  if (tvs.transverts == NULL) {
186  return OPERATOR_CANCELLED;
187  }
188 
189  /* Get view-matrix. */
190  {
191  PropertyRNA *prop_viewmat = RNA_struct_find_property(op->ptr, "viewmat");
192  if (RNA_property_is_set(op->ptr, prop_viewmat)) {
193  RNA_property_float_get_array(op->ptr, prop_viewmat, (float *)viewmat);
194  }
195  else {
197 
198  if (rv3d) {
199  copy_m4_m4(viewmat, rv3d->viewmat);
200  }
201  else {
202  unit_m4(viewmat);
203  }
204 
205  RNA_property_float_set_array(op->ptr, prop_viewmat, (float *)viewmat);
206  }
207  }
208 
209  /* get center */
210  {
211  PropertyRNA *prop_center = RNA_struct_find_property(op->ptr, "center");
212  if (RNA_property_is_set(op->ptr, prop_center)) {
213  RNA_property_float_get_array(op->ptr, prop_center, center);
214  }
215  else {
216  const Scene *scene = CTX_data_scene(C);
218 
219  RNA_property_float_set_array(op->ptr, prop_center, center);
220  }
221  }
222 
223  object_warp_calc_view_matrix(mat_view, center_view, obedit, viewmat, center, offset_angle);
224 
225  /* get minmax */
226  {
227  PropertyRNA *prop_min = RNA_struct_find_property(op->ptr, "min");
228  PropertyRNA *prop_max = RNA_struct_find_property(op->ptr, "max");
229 
230  if (RNA_property_is_set(op->ptr, prop_min) || RNA_property_is_set(op->ptr, prop_max)) {
231  min = RNA_property_float_get(op->ptr, prop_min);
232  max = RNA_property_float_get(op->ptr, prop_max);
233  }
234  else {
235  /* handy to set the bounds of the mesh */
236  object_warp_transverts_minmax_x(&tvs, mat_view, center_view, &min, &max);
237 
238  RNA_property_float_set(op->ptr, prop_min, min);
239  RNA_property_float_set(op->ptr, prop_max, max);
240  }
241 
242  if (min > max) {
243  SWAP(float, min, max);
244  }
245  }
246 
247  if (min != max) {
248  object_warp_transverts(&tvs, mat_view, center_view, warp_angle, min, max);
249  }
250 
251  ED_transverts_update_obedit(&tvs, obedit);
252  ED_transverts_free(&tvs);
253 
255 
256  return OPERATOR_FINISHED;
257 }
258 
260 {
261  PropertyRNA *prop;
262 
263  /* identifiers */
264  ot->name = "Warp";
265  ot->description = "Warp vertices around the cursor";
266  ot->idname = "TRANSFORM_OT_vertex_warp";
267 
268  /* api callbacks */
271 
272  /* flags */
274 
275  /* props */
276  prop = RNA_def_float(ot->srna,
277  "warp_angle",
278  DEG2RADF(360.0f),
279  -FLT_MAX,
280  FLT_MAX,
281  "Warp Angle",
282  "Amount to warp about the cursor",
283  DEG2RADF(-360.0f),
284  DEG2RADF(360.0f));
286 
287  prop = RNA_def_float(ot->srna,
288  "offset_angle",
289  DEG2RADF(0.0f),
290  -FLT_MAX,
291  FLT_MAX,
292  "Offset Angle",
293  "Angle to use as the basis for warping",
294  DEG2RADF(-360.0f),
295  DEG2RADF(360.0f));
297 
298  prop = RNA_def_float(ot->srna, "min", -1.0f, -FLT_MAX, FLT_MAX, "Min", "", -100.0, 100.0);
299  prop = RNA_def_float(ot->srna, "max", 1.0f, -FLT_MAX, FLT_MAX, "Max", "", -100.0, 100.0);
300 
301  /* hidden props */
302  prop = RNA_def_float_matrix(
303  ot->srna, "viewmat", 4, 4, NULL, 0.0f, 0.0f, "Matrix", "", 0.0f, 0.0f);
305 
307  ot->srna, "center", 3, NULL, -FLT_MAX, FLT_MAX, "Center", "", -FLT_MAX, FLT_MAX);
309 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1296
struct RegionView3D * CTX_wm_region_view3d(const bContext *C)
Definition: context.c:769
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void unit_m4(float m[4][4])
Definition: rct.c:1140
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
#define DEG2RADF(_deg)
void axis_angle_to_mat4_single(float R[4][4], const char axis, const float angle)
MINLINE void sub_v2_v2(float r[2], const float a[2])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE void zero_v2(float r[2])
MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
MINLINE float dot_m4_v3_row_x(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
#define SWAP(type, a, b)
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_transverts_update_obedit(TransVertStore *tvs, struct Object *obedit)
Definition: ed_transverts.c:52
bool ED_transverts_poll(struct bContext *C)
void ED_transverts_free(TransVertStore *tvs)
@ TM_SKIP_HANDLES
Definition: ED_transverts.h:66
@ TM_ALL_JOINTS
Definition: ED_transverts.h:64
void ED_transverts_create_from_obedit(TransVertStore *tvs, struct Object *obedit, const int mode)
NSNotificationCenter * center
@ PROP_SKIP_SAVE
Definition: RNA_types.h:204
@ PROP_HIDDEN
Definition: RNA_types.h:202
@ PROP_ANGLE
Definition: RNA_types.h:132
#define C
Definition: RandGen.cpp:39
#define ND_DRAW
Definition: WM_types.h:362
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NC_OBJECT
Definition: WM_types.h:280
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
Scene scene
#define sinf(x)
#define cosf(x)
static int object_warp_verts_exec(bContext *C, wmOperator *op)
Definition: object_warp.c:166
static void object_warp_transverts(TransVertStore *tvs, const float mat_view[4][4], const float center_view[3], const float angle_, const float min, const float max)
Definition: object_warp.c:88
void TRANSFORM_OT_vertex_warp(struct wmOperatorType *ot)
Definition: object_warp.c:259
static void object_warp_calc_view_matrix(float r_mat_view[4][4], float r_center_view[3], Object *obedit, const float viewmat[4][4], const float center[3], const float offset_angle)
Definition: object_warp.c:42
static void object_warp_transverts_minmax_x(TransVertStore *tvs, const float mat_view[4][4], const float center_view[3], float *r_min, float *r_max)
Definition: object_warp.c:63
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2941
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
Definition: rna_access.c:3033
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:6655
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6355
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Definition: rna_access.c:3132
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
Definition: rna_access.c:2964
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3825
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, int rows, int columns, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3943
PropertyRNA * RNA_def_float_vector_xyz(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3883
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1512
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
Definition: rna_define.c:1563
#define min(a, b)
Definition: sort.c:51
float obmat[4][4]
float viewmat[4][4]
View3DCursor cursor
struct TransVert * transverts
Definition: ED_transverts.h:40
float * loc
Definition: ED_transverts.h:33
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct PointerRNA * ptr
float max
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156