Blender  V2.93
transform_mode_mirror.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) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include <stdlib.h>
25 
26 #include "BLI_math.h"
27 #include "BLI_math_bits.h"
28 #include "BLI_string.h"
29 
30 #include "BKE_armature.h"
31 #include "BKE_context.h"
32 
33 #include "ED_screen.h"
34 
35 #include "UI_interface.h"
36 
37 #include "BLT_translation.h"
38 
39 #include "transform.h"
40 #include "transform_mode.h"
41 
42 /* -------------------------------------------------------------------- */
55 static void ElementMirror(TransInfo *t, TransDataContainer *tc, TransData *td, int axis, bool flip)
56 {
57  if ((t->flag & T_V3D_ALIGN) == 0 && td->ext) {
58  /* Size checked needed since the 3D cursor only uses rotation fields. */
59  if (td->ext->size) {
60  float fsize[] = {1.0, 1.0, 1.0};
61 
62  if (axis >= 0) {
63  fsize[axis] = -fsize[axis];
64  }
65  if (flip) {
66  negate_v3(fsize);
67  }
68 
69  protectedSizeBits(td->protectflag, fsize);
70 
71  mul_v3_v3v3(td->ext->size, td->ext->isize, fsize);
72 
73  constraintSizeLim(t, td);
74  }
75 
76  float rmat[3][3];
77  if (axis >= 0) {
78  float imat[3][3];
79  mul_m3_m3m3(rmat, t->spacemtx_inv, td->axismtx);
80  rmat[axis][0] = -rmat[axis][0];
81  rmat[axis][1] = -rmat[axis][1];
82  rmat[axis][2] = -rmat[axis][2];
83  rmat[0][axis] = -rmat[0][axis];
84  rmat[1][axis] = -rmat[1][axis];
85  rmat[2][axis] = -rmat[2][axis];
86  invert_m3_m3(imat, td->axismtx);
87  mul_m3_m3m3(rmat, rmat, imat);
88  mul_m3_m3m3(rmat, t->spacemtx, rmat);
89 
90  ElementRotation_ex(t, tc, td, rmat, td->center);
91 
92  if (td->ext->rotAngle) {
93  *td->ext->rotAngle = -td->ext->irotAngle;
94  }
95  }
96  else {
97  unit_m3(rmat);
98  ElementRotation_ex(t, tc, td, rmat, td->center);
99 
100  if (td->ext->rotAngle) {
101  *td->ext->rotAngle = td->ext->irotAngle;
102  }
103  }
104  }
105 
106  if ((td->flag & TD_NO_LOC) == 0) {
107  float center[3], vec[3];
108 
109  /* Local constraint shouldn't alter center. */
110  if (transdata_check_local_center(t, t->around)) {
111  copy_v3_v3(center, td->center);
112  }
113  else if (t->options & CTX_MOVIECLIP) {
114  if (td->flag & TD_INDIVIDUAL_SCALE) {
115  copy_v3_v3(center, td->center);
116  }
117  else {
119  }
120  }
121  else {
123  }
124 
125  /* For individual element center, Editmode need to use iloc. */
126  if (t->flag & T_POINTS) {
127  sub_v3_v3v3(vec, td->iloc, center);
128  }
129  else {
130  sub_v3_v3v3(vec, td->center, center);
131  }
132 
133  if (axis >= 0) {
134  /* Always do the mirror in global space. */
135  if (t->flag & T_EDIT) {
136  mul_m3_v3(td->mtx, vec);
137  }
138  reflect_v3_v3v3(vec, vec, t->spacemtx[axis]);
139  if (t->flag & T_EDIT) {
140  mul_m3_v3(td->smtx, vec);
141  }
142  }
143  if (flip) {
144  negate_v3(vec);
145  }
146 
147  add_v3_v3(vec, center);
148  if (t->flag & T_POINTS) {
149  sub_v3_v3(vec, td->iloc);
150  }
151  else {
152  sub_v3_v3(vec, td->center);
153  }
154 
155  if (t->options & (CTX_OBJECT | CTX_POSE_BONE)) {
156  mul_m3_v3(td->smtx, vec);
157  }
158 
160  if (td->loc) {
161  add_v3_v3v3(td->loc, td->iloc, vec);
162  }
163 
164  constraintTransLim(t, td);
165  }
166 }
167 
168 static void applyMirror(TransInfo *t, const int UNUSED(mval[2]))
169 {
170  int i;
171  char str[UI_MAX_DRAW_STR];
172  copy_v3_v3(t->values_final, t->values);
173 
174  /* OPTIMIZATION:
175  * This still recalculates transformation on mouse move
176  * while it should only recalculate on constraint change. */
177 
178  /* if an axis has been selected */
179  if (t->con.mode & CON_APPLY) {
180  /* #special_axis is either the constraint plane normal or the constraint axis.
181  * Assuming that CON_AXIS0 < CON_AXIS1 < CON_AXIS2 and CON_AXIS2 is CON_AXIS0 << 2 */
182  BLI_assert(CON_AXIS2 == CON_AXIS0 << 2);
183  int axis_bitmap = (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) / CON_AXIS0;
184  int special_axis_bitmap = 0;
185  int special_axis = -1;
186  int bitmap_len = count_bits_i(axis_bitmap);
187  if (LIKELY(!ELEM(bitmap_len, 0, 3))) {
188  special_axis_bitmap = (bitmap_len == 2) ? ~axis_bitmap : axis_bitmap;
189  special_axis = bitscan_forward_i(special_axis_bitmap);
190  }
191 
192  BLI_snprintf(str, sizeof(str), TIP_("Mirror%s"), t->con.text);
193 
195  TransData *td = tc->data;
196  for (i = 0; i < tc->data_len; i++, td++) {
197  if (td->flag & TD_SKIP) {
198  continue;
199  }
200 
201  ElementMirror(t, tc, td, special_axis, bitmap_len >= 2);
202  }
203  }
204 
205  recalcData(t);
206 
207  ED_area_status_text(t->area, str);
208  }
209  else {
211  TransData *td = tc->data;
212  for (i = 0; i < tc->data_len; i++, td++) {
213  if (td->flag & TD_SKIP) {
214  continue;
215  }
216 
217  ElementMirror(t, tc, td, -1, false);
218  }
219  }
220 
221  recalcData(t);
222 
223  if (t->flag & T_2D_EDIT) {
224  ED_area_status_text(t->area, TIP_("Select a mirror axis (X, Y)"));
225  }
226  else {
227  ED_area_status_text(t->area, TIP_("Select a mirror axis (X, Y, Z)"));
228  }
229  }
230 }
231 
233 {
234  t->transform = applyMirror;
235  initMouseInputMode(t, &t->mouse, INPUT_NONE);
236 
237  t->flag |= T_NULL_ONE;
238  if ((t->flag & T_EDIT) == 0) {
239  t->flag |= T_NO_ZERO;
240  }
241 }
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE int bitscan_forward_i(int a)
MINLINE int count_bits_i(unsigned int n)
void mul_m3_v3(const float M[3][3], float r[3])
Definition: math_matrix.c:930
void unit_m3(float m[3][3])
Definition: math_matrix.c:58
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1161
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
Definition: math_matrix.c:391
void reflect_v3_v3v3(float out[3], const float vec[3], const float normal[3])
Definition: math_vector.c:818
MINLINE void mul_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define UNUSED(x)
#define ELEM(...)
#define LIKELY(x)
#define TIP_(msgid)
void ED_area_status_text(ScrArea *area, const char *str)
Definition: area.c:815
NSNotificationCenter * center
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
#define UI_MAX_DRAW_STR
Definition: UI_interface.h:90
#define str(s)
float center_local[3]
Definition: transform.h:476
float smtx[3][3]
short protectflag
float axismtx[3][3]
float mtx[3][3]
TransDataExtension * ext
@ INPUT_NONE
Definition: transform.h:727
@ CON_APPLY
Definition: transform.h:177
@ CON_AXIS1
Definition: transform.h:180
@ CON_AXIS0
Definition: transform.h:179
@ CON_AXIS2
Definition: transform.h:181
void recalcData(TransInfo *t)
@ CTX_MOVIECLIP
Definition: transform.h:84
@ CTX_POSE_BONE
Definition: transform.h:87
@ CTX_OBJECT
Definition: transform.h:85
@ T_V3D_ALIGN
Definition: transform.h:116
@ T_2D_EDIT
Definition: transform.h:118
@ T_POINTS
Definition: transform.h:104
@ T_NULL_ONE
Definition: transform.h:107
@ T_NO_ZERO
Definition: transform.h:108
@ T_EDIT
Definition: transform.h:102
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition: transform.h:813
@ TD_INDIVIDUAL_SCALE
@ TD_SKIP
@ TD_NO_LOC
void protectedTransBits(short protectflag, float vec[3])
void constraintTransLim(TransInfo *t, TransData *td)
void ElementRotation_ex(TransInfo *t, TransDataContainer *tc, TransData *td, const float mat[3][3], const float *center)
bool transdata_check_local_center(TransInfo *t, short around)
void protectedSizeBits(short protectflag, float size[3])
void constraintSizeLim(TransInfo *t, TransData *td)
transform modes used by different operators.
static void ElementMirror(TransInfo *t, TransDataContainer *tc, TransData *td, int axis, bool flip)
static void applyMirror(TransInfo *t, const int UNUSED(mval[2]))
void initMirror(TransInfo *t)