Blender V5.0
transform_mode_tosphere.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
9#include <cstdlib>
10
11#include "BLI_math_matrix.h"
12#include "BLI_math_vector.h"
13#include "BLI_string_utf8.h"
14#include "BLI_task.hh"
15
16#include "MEM_guardedalloc.h"
17
18#include "BKE_unit.hh"
19
20#include "ED_screen.hh"
21
22#include "BLT_translation.hh"
23
24#include "UI_interface_types.hh"
25
26#include "transform.hh"
27#include "transform_convert.hh"
28#include "transform_snap.hh"
29
30#include "transform_mode.hh"
31
32namespace blender::ed::transform {
33
34/* -------------------------------------------------------------------- */
37
40 float radius;
41};
42
45{
46 ToSphereInfo *data = static_cast<ToSphereInfo *>(t->custom.mode.data);
47 float radius = 0.0f;
48 float vec[3];
49
50 const bool is_local_center = transdata_check_local_center(t, t->around);
51 const bool is_data_space = (t->options & CTX_POSE_BONE) != 0;
52
53 if (t->flag & T_PROP_EDIT_ALL) {
54 int factor_accum = 0.0f;
56 TransData *td = tc->data;
57 for (int i = 0; i < tc->data_len; i++, td++) {
58 if (td->factor == 0.0f) {
59 continue;
60 }
61 const float *center = is_local_center ? td->center : tc->center_local;
62 if (is_data_space) {
63 copy_v3_v3(vec, td->center);
64 }
65 else {
66 copy_v3_v3(vec, td->iloc);
67 }
68
69 sub_v3_v3(vec, center);
70 radius += td->factor * len_v3(vec);
71 factor_accum += td->factor;
72 }
73 }
74 if (factor_accum != 0.0f) {
75 radius /= factor_accum;
76 }
77 }
78 else {
80 TransData *td = tc->data;
81 for (int i = 0; i < tc->data_len; i++, td++) {
82 const float *center = is_local_center ? td->center : tc->center_local;
83 if (is_data_space) {
84 copy_v3_v3(vec, td->center);
85 }
86 else {
87 copy_v3_v3(vec, td->iloc);
88 }
89
90 sub_v3_v3(vec, center);
91 radius += len_v3(vec);
92 }
93 }
94 radius /= float(t->data_len_all);
95 }
96
97 data->prop_size_prev = t->prop_size;
98 data->radius = radius;
99}
100
102
103/* -------------------------------------------------------------------- */
106
107static void transdata_elem_to_sphere(const TransInfo * /*t*/,
108 const TransDataContainer *tc,
109 TransData *td,
110 const float ratio,
111 const ToSphereInfo *to_sphere_info,
112 const bool is_local_center,
113 const bool is_data_space)
114{
115 float vec[3];
116 const float *center = is_local_center ? td->center : tc->center_local;
117 if (is_data_space) {
118 copy_v3_v3(vec, td->center);
119 }
120 else {
121 copy_v3_v3(vec, td->iloc);
122 }
123
124 sub_v3_v3(vec, center);
125 const float radius = normalize_v3(vec);
126 const float tratio = ratio * td->factor;
127 mul_v3_fl(vec, radius * (1.0f - tratio) + to_sphere_info->radius * tratio);
128 add_v3_v3(vec, center);
129
130 if (is_data_space) {
131 sub_v3_v3(vec, td->center);
132 mul_m3_v3(td->smtx, vec);
133 add_v3_v3(vec, td->iloc);
134 }
135
136 copy_v3_v3(td->loc, vec);
137}
138
140
141/* -------------------------------------------------------------------- */
144
146{
147 const bool is_local_center = transdata_check_local_center(t, t->around);
148 const bool is_data_space = (t->options & CTX_POSE_BONE) != 0;
149
150 float ratio;
151 char str[UI_MAX_DRAW_STR];
152
153 ratio = t->values[0] + t->values_modal_offset[0];
154
155 transform_snap_increment(t, &ratio);
156
157 applyNumInput(&t->num, &ratio);
158
159 CLAMP(ratio, 0.0f, 1.0f);
160
161 t->values_final[0] = ratio;
162
163 /* Header print for NumInput. */
164 if (hasNumInput(&t->num)) {
165 char c[NUM_STR_REP_LEN];
166
167 outputNumInput(&(t->num), c, t->scene->unit);
168
169 SNPRINTF_UTF8(str, IFACE_("To Sphere: %s %s"), c, t->proptext);
170 }
171 else {
172 /* Default header print. */
173 SNPRINTF_UTF8(str, IFACE_("To Sphere: %.4f %s"), ratio, t->proptext);
174 }
175
176 const ToSphereInfo *to_sphere_info = static_cast<const ToSphereInfo *>(t->custom.mode.data);
177 if (to_sphere_info->prop_size_prev != t->prop_size) {
179 }
180
182 threading::parallel_for(IndexRange(tc->data_len), 1024, [&](const IndexRange range) {
183 for (const int i : range) {
184 TransData *td = &tc->data[i];
185 if (td->flag & TD_SKIP) {
186 continue;
187 }
188 transdata_elem_to_sphere(t, tc, td, ratio, to_sphere_info, is_local_center, is_data_space);
189 }
190 });
191 }
192
193 recalc_data(t);
194
195 ED_area_status_text(t->area, str);
196}
197
198static void initToSphere(TransInfo *t, wmOperator * /*op*/)
199{
200 t->mode = TFM_TOSPHERE;
201
203
204 t->idx_max = 0;
205 t->num.idx_max = 0;
206 t->increment[0] = 0.1f;
207 t->increment_precision = 0.1f;
208
209 copy_v3_fl(t->num.val_inc, t->increment[0]);
210 t->num.unit_sys = t->scene->unit.system;
211 t->num.unit_type[0] = B_UNIT_NONE;
212
214
216 t->custom.mode.data = data;
217 t->custom.mode.use_free = true;
218
220}
221
223
225 /*flags*/ T_NO_CONSTRAINT,
226 /*init_fn*/ initToSphere,
227 /*transform_fn*/ applyToSphere,
228 /*transform_matrix_fn*/ nullptr,
229 /*handle_event_fn*/ nullptr,
230 /*snap_distance_fn*/ nullptr,
231 /*snap_apply_fn*/ nullptr,
232 /*draw_fn*/ nullptr,
233};
234
235} // namespace blender::ed::transform
@ B_UNIT_NONE
Definition BKE_unit.hh:136
void mul_m3_v3(const float M[3][3], float r[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
#define SNPRINTF_UTF8(dst, format,...)
#define CLAMP(a, b, c)
#define IFACE_(msgid)
#define NUM_STR_REP_LEN
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:190
@ NUM_NO_NEGATIVE
@ NUM_NULL_ONE
void outputNumInput(NumInput *n, char *str, const UnitSettings &unit_settings)
Definition numinput.cc:88
bool hasNumInput(const NumInput *n)
Definition numinput.cc:171
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:851
Read Guarded memory(de)allocation.
#define UI_MAX_DRAW_STR
BMesh const char void * data
nullptr float
#define str(s)
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
bool transform_snap_increment(const TransInfo *t, float *r_val)
static void initToSphere(TransInfo *t, wmOperator *)
static void to_sphere_radius_update(TransInfo *t)
static void transdata_elem_to_sphere(const TransInfo *, const TransDataContainer *tc, TransData *td, const float ratio, const ToSphereInfo *to_sphere_info, const bool is_local_center, const bool is_data_space)
bool transdata_check_local_center(const TransInfo *t, short around)
static void applyToSphere(TransInfo *t)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
short idx_max
short val_flag[NUM_MAX_ELEMENTS]
float val_inc[NUM_MAX_ELEMENTS]
int unit_type[NUM_MAX_ELEMENTS]
struct UnitSettings unit
TransCustomDataContainer custom
Definition transform.hh:974
i
Definition text_draw.cc:230
#define T_PROP_EDIT_ALL
Definition transform.hh:28
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:42
conversion and adaptation of different datablocks to a common struct.
transform modes used by different operators.