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

transform_mode_trackball.c « transform « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3716826800b7afc7713facb739e953200f14505f (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
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2001-2002 NaN Holding BV. All rights reserved. */

/** \file
 * \ingroup edtransform
 */

#include <stdlib.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 "UI_interface.h"

#include "BLT_translation.h"

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

#include "transform_mode.h"

/* -------------------------------------------------------------------- */
/** \name Transform (Rotation - Trackball) Element
 * \{ */

/**
 * \note Small arrays / data-structures should be stored copied for faster memory access.
 */
struct TransDataArgs_Trackball {
  const TransInfo *t;
  const TransDataContainer *tc;
  const float axis[3];
  const float angle;
  float mat_final[3][3];
};

static void transdata_elem_trackball(const TransInfo *t,
                                     const TransDataContainer *tc,
                                     TransData *td,
                                     const float axis[3],
                                     const float angle,
                                     const float mat_final[3][3])
{
  float mat_buf[3][3];
  const float(*mat)[3] = mat_final;
  if (t->flag & T_PROP_EDIT) {
    axis_angle_normalized_to_mat3(mat_buf, axis, td->factor * angle);
    mat = mat_buf;
  }
  ElementRotation(t, tc, td, mat, t->around);
}

static void transdata_elem_trackball_fn(void *__restrict iter_data_v,
                                        const int iter,
                                        const TaskParallelTLS *__restrict UNUSED(tls))
{
  struct TransDataArgs_Trackball *data = iter_data_v;
  TransData *td = &data->tc->data[iter];
  if (td->flag & TD_SKIP) {
    return;
  }
  transdata_elem_trackball(data->t, data->tc, td, data->axis, data->angle, data->mat_final);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Transform (Rotation - Trackball)
 * \{ */

static void applyTrackballValue_calc_axis_angle(const TransInfo *t,
                                                const float phi[2],
                                                float r_axis[3],
                                                float *r_angle)
{
  float axis1[3], axis2[3];
  normalize_v3_v3(axis1, t->persinv[0]);
  normalize_v3_v3(axis2, t->persinv[1]);

  mul_v3_v3fl(r_axis, axis1, phi[0]);
  madd_v3_v3fl(r_axis, axis2, phi[1]);
  *r_angle = normalize_v3(r_axis);
}

static void applyTrackballValue(TransInfo *t, const float axis[3], const float angle)
{
  float mat_final[3][3];
  int i;

  axis_angle_normalized_to_mat3(mat_final, axis, angle);

  FOREACH_TRANS_DATA_CONTAINER (t, tc) {
    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_trackball(t, tc, td, axis, angle, mat_final);
      }
    }
    else {
      struct TransDataArgs_Trackball data = {
          .t = t,
          .tc = tc,
          .axis = {UNPACK3(axis)},
          .angle = angle,
      };
      copy_m3_m3(data.mat_final, mat_final);

      TaskParallelSettings settings;
      BLI_parallel_range_settings_defaults(&settings);
      BLI_task_parallel_range(0, tc->data_len, &data, transdata_elem_trackball_fn, &settings);
    }
  }
}

static void applyTrackball(TransInfo *t, const int UNUSED(mval[2]))
{
  char str[UI_MAX_DRAW_STR];
  size_t ofs = 0;
  float phi[2];

  copy_v2_v2(phi, t->values);

  transform_snap_increment(t, phi);

  applyNumInput(&t->num, phi);

  copy_v2_v2(t->values_final, phi);

  if (hasNumInput(&t->num)) {
    char c[NUM_STR_REP_LEN * 2];

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

    ofs += BLI_snprintf_rlen(str + ofs,
                             sizeof(str) - ofs,
                             TIP_("Trackball: %s %s %s"),
                             &c[0],
                             &c[NUM_STR_REP_LEN],
                             t->proptext);
  }
  else {
    ofs += BLI_snprintf_rlen(str + ofs,
                             sizeof(str) - ofs,
                             TIP_("Trackball: %.2f %.2f %s"),
                             RAD2DEGF(phi[0]),
                             RAD2DEGF(phi[1]),
                             t->proptext);
  }

  if (t->flag & T_PROP_EDIT_ALL) {
    ofs += BLI_snprintf_rlen(
        str + ofs, sizeof(str) - ofs, TIP_(" Proportional size: %.2f"), t->prop_size);
  }

  float axis_final[3], angle_final;
  applyTrackballValue_calc_axis_angle(t, phi, axis_final, &angle_final);
  applyTrackballValue(t, axis_final, angle_final);

  recalcData(t);

  ED_area_status_text(t->area, str);
}

static void applyTrackballMatrix(TransInfo *t, float mat_xform[4][4])
{
  const float phi[2] = {UNPACK2(t->values_final)};

  float axis_final[3], angle_final;
  applyTrackballValue_calc_axis_angle(t, phi, axis_final, &angle_final);

  float mat3[3][3], mat4[4][4];
  axis_angle_normalized_to_mat3(mat3, axis_final, angle_final);

  copy_m4_m3(mat4, mat3);
  transform_pivot_set_m4(mat4, t->center_global);
  mul_m4_m4m4(mat_xform, mat4, mat_xform);
}

void initTrackball(TransInfo *t)
{
  t->mode = TFM_TRACKBALL;
  t->transform = applyTrackball;
  t->transform_matrix = applyTrackballMatrix;

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

  t->idx_max = 1;
  t->num.idx_max = 1;
  t->snap[0] = DEG2RAD(5.0);
  t->snap[1] = DEG2RAD(1.0);

  copy_v3_fl(t->num.val_inc, t->snap[1]);
  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_ROTATION;

  t->flag |= T_NO_CONSTRAINT;
}

/** \} */