Welcome to mirror list, hosted at ThFree Co, Russian Federation.

transform_mode_bend.c « transform « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d84c397fa66a863eeedf734ffbf73dddd99b232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup edtransform
 */

#include <stdlib.h>

#include "MEM_guardedalloc.h"

#include "DNA_gpencil_types.h"

#include "BLI_math.h"
#include "BLI_string.h"
#include "BLI_task.h"

#include "BKE_context.h"
#include "BKE_unit.h"

#include "ED_screen.h"

#include "WM_api.h"
#include "WM_types.h"

#include "UI_interface.h"

#include "BLT_translation.h"

#include "transform.h"
#include "transform_convert.h"
#include "transform_snap.h"

#include "transform_mode.h"

/* -------------------------------------------------------------------- */
/** \name Transform (Bend) Custom Data
 * \{ */

/**
 * Custom data, stored in #TransInfo.custom.mode.data
 */
struct BendCustomData {
  /* All values are in global space. */
  float warp_sta[3];
  float warp_end[3];

  float warp_nor[3];
  float warp_tan[3];

  /* for applying the mouse distance */
  float warp_init_dist;
};

/** \} */

/* -------------------------------------------------------------------- */
/** \name Transform (Bend) Element
 * \{ */

/**
 * \note Small arrays / data-structures should be copied for faster memory access.
 */
struct TransDataArgs_Bend {
  const TransInfo *t;
  const TransDataContainer *tc;

  float angle;
  struct BendCustomData bend_data;

  const float warp_sta_local[3];
  const float warp_end_local[3];
  const float warp_end_radius_local[3];
  const float pivot_local[3];
  bool is_clamp;
};

static void transdata_elem_bend(const TransInfo *t,
                                const TransDataContainer *tc,
                                TransData *td,
                                float angle,
                                const struct BendCustomData *bend_data,
                                const float warp_sta_local[3],
                                const float UNUSED(warp_end_local[3]),
                                const float warp_end_radius_local[3],
                                const float pivot_local[3],

                                bool is_clamp)
{
  if (UNLIKELY(angle == 0.0f)) {
    copy_v3_v3(td->loc, td->iloc);
    return;
  }

  float vec[3];
  float mat[3][3];
  float delta[3];
  float fac, fac_scaled;

  copy_v3_v3(vec, td->iloc);
  mul_m3_v3(td->mtx, vec);

  fac = line_point_factor_v3(vec, warp_sta_local, warp_end_radius_local);
  if (is_clamp) {
    CLAMP(fac, 0.0f, 1.0f);
  }

  if (t->options & CTX_GPENCIL_STROKES) {
    /* grease pencil multiframe falloff */
    bGPDstroke *gps = (bGPDstroke *)td->extra;
    if (gps != NULL) {
      fac_scaled = fac * td->factor * gps->runtime.multi_frame_falloff;
    }
    else {
      fac_scaled = fac * td->factor;
    }
  }
  else {
    fac_scaled = fac * td->factor;
  }

  axis_angle_normalized_to_mat3(mat, bend_data->warp_nor, angle * fac_scaled);
  interp_v3_v3v3(delta, warp_sta_local, warp_end_radius_local, fac_scaled);
  sub_v3_v3(delta, warp_sta_local);

  /* delta is subtracted, rotation adds back this offset */
  sub_v3_v3(vec, delta);

  sub_v3_v3(vec, pivot_local);
  mul_m3_v3(mat, vec);
  add_v3_v3(vec, pivot_local);

  mul_m3_v3(td->smtx, vec);

  /* rotation */
  if ((t->flag & T_POINTS) == 0) {
    ElementRotation(t, tc, td, mat, V3D_AROUND_LOCAL_ORIGINS);
  }

  /* location */
  copy_v3_v3(td->loc, vec);
}

static void transdata_elem_bend_fn(void *__restrict iter_data_v,
                                   const int iter,
                                   const TaskParallelTLS *__restrict UNUSED(tls))
{
  struct TransDataArgs_Bend *data = iter_data_v;
  TransData *td = &data->tc->data[iter];
  if (td->flag & TD_SKIP) {
    return;
  }
  transdata_elem_bend(data->t,
                      data->tc,
                      td,
                      data->angle,
                      &data->bend_data,
                      data->warp_sta_local,
                      data->warp_end_local,
                      data->warp_end_radius_local,
                      data->pivot_local,
                      data->is_clamp);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Transform (Bend)
 * \{ */

static eRedrawFlag handleEventBend(TransInfo *UNUSED(t), const wmEvent *event)
{
  eRedrawFlag status = TREDRAW_NOTHING;

  if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
    status = TREDRAW_HARD;
  }

  return status;
}

static void Bend(TransInfo *t, const int UNUSED(mval[2]))
{
  float pivot_global[3];
  float warp_end_radius_global[3];
  int i;
  char str[UI_MAX_DRAW_STR];
  const struct BendCustomData *bend_data = t->custom.mode.data;
  const bool is_clamp = (t->flag & T_ALT_TRANSFORM) == 0;

  union {
    struct {
      float angle, scale;
    };
    float vector[2];
  } values;

  /* amount of radians for bend */
  copy_v2_v2(values.vector, t->values);

#if 0
  snapGrid(t, angle_rad);
#else
  /* hrmf, snapping radius is using 'angle' steps, need to convert to something else
   * this isn't essential but nicer to give reasonable snapping values for radius. */
  if (t->tsnap.mode & SCE_SNAP_MODE_INCREMENT) {
    const float radius_snap = 0.1f;
    const float snap_hack = (t->snap[0] * bend_data->warp_init_dist) / radius_snap;
    values.scale *= snap_hack;
    transform_snap_increment(t, values.vector);
    values.scale /= snap_hack;
  }
#endif

  if (applyNumInput(&t->num, values.vector)) {
    values.scale = values.scale / bend_data->warp_init_dist;
  }

  copy_v2_v2(t->values_final, values.vector);

  /* header print for NumInput */
  if (hasNumInput(&t->num)) {
    char c[NUM_STR_REP_LEN * 2];

    outputNumInput(&(t->num), c, &t->scene->unit);

    BLI_snprintf(str,
                 sizeof(str),
                 TIP_("Bend Angle: %s Radius: %s Alt, Clamp %s"),
                 &c[0],
                 &c[NUM_STR_REP_LEN],
                 WM_bool_as_string(is_clamp));
  }
  else {
    /* default header print */
    BLI_snprintf(str,
                 sizeof(str),
                 TIP_("Bend Angle: %.3f Radius: %.4f, Alt, Clamp %s"),
                 RAD2DEGF(values.angle),
                 values.scale * bend_data->warp_init_dist,
                 WM_bool_as_string(is_clamp));
  }

  values.angle *= -1.0f;
  values.scale *= bend_data->warp_init_dist;

  /* calc 'data->warp_end' from 'data->warp_end_init' */
  copy_v3_v3(warp_end_radius_global, bend_data->warp_end);
  dist_ensure_v3_v3fl(warp_end_radius_global, bend_data->warp_sta, values.scale);
  /* done */

  /* calculate pivot */
  copy_v3_v3(pivot_global, bend_data->warp_sta);
  if (values.angle > 0.0f) {
    madd_v3_v3fl(pivot_global,
                 bend_data->warp_tan,
                 -values.scale * shell_angle_to_dist((float)M_PI_2 - values.angle));
  }
  else {
    madd_v3_v3fl(pivot_global,
                 bend_data->warp_tan,
                 +values.scale * shell_angle_to_dist((float)M_PI_2 + values.angle));
  }

  /* TODO(campbell): xform, compensate object center. */
  FOREACH_TRANS_DATA_CONTAINER (t, tc) {

    float warp_sta_local[3];
    float warp_end_local[3];
    float warp_end_radius_local[3];
    float pivot_local[3];

    if (tc->use_local_mat) {
      sub_v3_v3v3(warp_sta_local, bend_data->warp_sta, tc->mat[3]);
      sub_v3_v3v3(warp_end_local, bend_data->warp_end, tc->mat[3]);
      sub_v3_v3v3(warp_end_radius_local, warp_end_radius_global, tc->mat[3]);
      sub_v3_v3v3(pivot_local, pivot_global, tc->mat[3]);
    }
    else {
      copy_v3_v3(warp_sta_local, bend_data->warp_sta);
      copy_v3_v3(warp_end_local, bend_data->warp_end);
      copy_v3_v3(warp_end_radius_local, warp_end_radius_global);
      copy_v3_v3(pivot_local, pivot_global);
    }

    if (tc->data_len < TRANSDATA_THREAD_LIMIT) {
      TransData *td = tc->data;

      for (i = 0; i < tc->data_len; i++, td++) {
        if (td->flag & TD_SKIP) {
          continue;
        }
        transdata_elem_bend(t,
                            tc,
                            td,
                            values.angle,
                            bend_data,
                            warp_sta_local,
                            warp_end_local,
                            warp_end_radius_local,
                            pivot_local,
                            is_clamp);
      }
    }
    else {
      struct TransDataArgs_Bend data = {
          .t = t,
          .tc = tc,
          .angle = values.angle,
          .bend_data = *bend_data,
          .warp_sta_local = {UNPACK3(warp_sta_local)},
          .warp_end_local = {UNPACK3(warp_end_local)},
          .warp_end_radius_local = {UNPACK3(warp_end_radius_local)},
          .pivot_local = {UNPACK3(pivot_local)},
          .is_clamp = is_clamp,
      };
      TaskParallelSettings settings;
      BLI_parallel_range_settings_defaults(&settings);
      BLI_task_parallel_range(0, tc->data_len, &data, transdata_elem_bend_fn, &settings);
    }
  }

  recalcData(t);

  ED_area_status_text(t->area, str);
}

void initBend(TransInfo *t)
{
  const float mval_fl[2] = {UNPACK2(t->mval)};
  const float *curs;
  float tvec[3];
  struct BendCustomData *data;

  t->mode = TFM_BEND;
  t->transform = Bend;
  t->handleEvent = handleEventBend;

  initMouseInputMode(t, &t->mouse, INPUT_ANGLE_SPRING);

  t->idx_max = 1;
  t->num.idx_max = 1;
  t->snap[0] = SNAP_INCREMENTAL_ANGLE;
  t->snap[1] = t->snap[0] * 0.2;

  copy_v3_fl(t->num.val_inc, t->snap[0]);
  t->num.unit_sys = t->scene->unit.system;
  t->num.unit_use_radians = (t->scene->unit.system_rotation == USER_UNIT_ROT_RADIANS);
  t->num.unit_type[0] = B_UNIT_ROTATION;
  t->num.unit_type[1] = B_UNIT_LENGTH;

  t->flag |= T_NO_CONSTRAINT;

  // copy_v3_v3(t->center, ED_view3d_cursor3d_get(t->scene, t->view));
  if ((t->flag & T_OVERRIDE_CENTER) == 0) {
    calculateCenterCursor(t, t->center_global);
  }
  calculateCenterLocal(t, t->center_global);

  data = MEM_callocN(sizeof(*data), __func__);

  curs = t->scene->cursor.location;
  copy_v3_v3(data->warp_sta, curs);
  ED_view3d_win_to_3d(
      (View3D *)t->area->spacedata.first, t->region, curs, mval_fl, data->warp_end);

  copy_v3_v3(data->warp_nor, t->viewinv[2]);
  normalize_v3(data->warp_nor);

  /* tangent */
  sub_v3_v3v3(tvec, data->warp_end, data->warp_sta);
  cross_v3_v3v3(data->warp_tan, tvec, data->warp_nor);
  normalize_v3(data->warp_tan);

  data->warp_init_dist = len_v3v3(data->warp_end, data->warp_sta);

  t->custom.mode.data = data;
  t->custom.mode.use_free = true;
}
/** \} */