Blender V5.3
object_warp.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_object_types.h"
10#include "DNA_scene_types.h"
11#include "DNA_view3d_types.h"
12
13#include "BKE_context.hh"
14
15#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18
20
21#include "RNA_access.hh"
22#include "RNA_define.hh"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27#include "ED_object.hh"
28#include "ED_transverts.hh"
29
30#include "object_intern.hh"
31
32namespace blender::ed::object {
33
34static void object_warp_calc_view_matrix(float r_mat_view[4][4],
35 float r_center_view[3],
36 Object *obedit,
37 const float viewmat[4][4],
38 const float center[3],
39 const float offset_angle)
40{
41 float mat_offset[4][4];
42 float viewmat_roll[4][4];
43
44 /* apply the rotation offset by rolling the view */
45 axis_angle_to_mat4_single(mat_offset, 'Z', offset_angle);
46 mul_m4_m4m4(viewmat_roll, mat_offset, viewmat);
47
48 /* apply the view and the object matrix */
49 mul_m4_m4m4(r_mat_view, viewmat_roll, obedit->object_to_world().ptr());
50
51 /* get the view-space cursor */
52 mul_v3_m4v3(r_center_view, viewmat_roll, center);
53}
54
56 const float mat_view[4][4],
57 const float center_view[3],
58 float *r_min,
59 float *r_max)
60{
61 /* no need to apply translation and cursor offset for every vertex, delay this */
62 const float x_ofs = (mat_view[3][0] - center_view[0]);
63 float min = FLT_MAX, max = -FLT_MAX;
64
65 TransVert *tv = tvs->transverts;
66 for (int i = 0; i < tvs->transverts_tot; i++, tv++) {
67 float val;
68
69 /* Convert object-space to view-space. */
70 val = dot_m4_v3_row_x(mat_view, tv->loc);
71
72 min = min_ff(min, val);
73 max = max_ff(max, val);
74 }
75
76 *r_min = min + x_ofs;
77 *r_max = max + x_ofs;
78}
79
81 const float mat_view[4][4],
82 const float center_view[3],
83 const float angle_,
84 const float min,
85 const float max)
86{
87 TransVert *tv;
88 const float angle = -angle_;
89/* cache vars for tiny speedup */
90#if 1
91 const float range = max - min;
92 const float range_inv = 1.0f / range;
93 const float min_ofs = min + (0.5f * range);
94#endif
95
96 float dir_min[2], dir_max[2];
97 float imat_view[4][4];
98
99 invert_m4_m4(imat_view, mat_view);
100
101 /* calculate the direction vectors outside min/max range */
102 {
103 const float phi = angle * 0.5f;
104
105 dir_max[0] = cosf(phi);
106 dir_max[1] = sinf(phi);
107
108 dir_min[0] = -dir_max[0];
109 dir_min[1] = dir_max[1];
110 }
111
112 tv = tvs->transverts;
113 for (int i = 0; i < tvs->transverts_tot; i++, tv++) {
114 float co[3], co_add[2];
115 float val, phi;
116
117 /* Convert object-space to view-space. */
118 mul_v3_m4v3(co, mat_view, tv->loc);
119 sub_v2_v2(co, center_view);
120
121 val = co[0];
122 /* is overwritten later anyway */
123 // co[0] = 0.0f;
124
125 if (val < min) {
126 mul_v2_v2fl(co_add, dir_min, min - val);
127 val = min;
128 }
129 else if (val > max) {
130 mul_v2_v2fl(co_add, dir_max, val - max);
131 val = max;
132 }
133 else {
134 zero_v2(co_add);
135 }
136
137/* map from x axis to (-0.5 - 0.5) */
138#if 0
139 val = ((val - min) / (max - min)) - 0.5f;
140#else
141 val = (val - min_ofs) * range_inv;
142#endif
143
144 /* convert the x axis into a rotation */
145 phi = val * angle;
146
147 co[0] = -sinf(phi) * co[1];
148 co[1] = cosf(phi) * co[1];
149
150 add_v2_v2(co, co_add);
151
152 /* Convert view-space to object-space. */
153 add_v2_v2(co, center_view);
154 mul_v3_m4v3(tv->loc, imat_view, co);
155 }
156}
157
159{
160 const float warp_angle = RNA_float_get(op->ptr, "warp_angle");
161 const float offset_angle = RNA_float_get(op->ptr, "offset_angle");
162
163 TransVertStore tvs = {nullptr};
164 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
165 Object *obedit = CTX_data_edit_object(C);
166
167 /* typically from 'rv3d' and 3d cursor */
168 float viewmat[4][4];
169 float center[3];
170
171 /* 'viewmat' relative vars */
172 float mat_view[4][4];
173 float center_view[3];
174
175 float min, max;
176
177 if (shape_key_report_if_locked(obedit, op->reports)) {
178 return OPERATOR_CANCELLED;
179 }
180
181 if (ED_transverts_check_obedit(obedit)) {
182 const Object *obedit_eval = DEG_get_evaluated(depsgraph, obedit);
184 }
185 if (tvs.transverts == nullptr) {
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, reinterpret_cast<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, reinterpret_cast<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);
217 copy_v3_v3(center, scene->cursor.location);
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 std::swap(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 */
303 ot->srna, "viewmat", 4, 4, nullptr, 0.0f, 0.0f, "Matrix", "", 0.0f, 0.0f);
305
307 ot->srna, "center", 3, nullptr, -FLT_MAX, FLT_MAX, "Center", "", -FLT_MAX, FLT_MAX);
309}
310
311} // namespace blender::ed::object
#define DEG2RADF(_deg)
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define FLT_MAX
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
static wmOperatorStatus object_warp_verts_exec(bContext *C, wmOperator *op)
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)
void TRANSFORM_OT_vertex_warp(wmOperatorType *ot)
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)
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)
MINLINE float min_ff(float a, float b)
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
bool ED_transverts_poll(bContext *C)
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 mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
MINLINE float dot_m4_v3_row_x(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v2_v2(float r[2], const float a[2])
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
Object * CTX_data_edit_object(const bContext *C)
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
void unit_m4(float m[4][4])
RegionView3D * CTX_wm_region_view3d(const bContext *C)
MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_float_vector_xyz(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
MINLINE float max_ff(float a, float b)
void ED_transverts_free(TransVertStore *tvs)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Scene * CTX_data_scene(const bContext *C)
MINLINE void sub_v2_v2(float r[2], const float a[2])
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void copy_m4_m4(float m1[4][4], const float m2[4][4])
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, const int rows, const int columns, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
MINLINE void zero_v2(float r[2])
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
bool ED_transverts_check_obedit(const Object *obedit)
#define sinf
#define cosf
#define min(a, b)
Definition sort.cc:38
View3DCursor cursor
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
struct ReportList * reports