2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/transform/transform_constraints.c
29 * \ingroup edtransform
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_view3d_types.h"
43 #include "BIF_glutil.h"
45 #include "GPU_immediate.h"
46 #include "GPU_matrix.h"
49 #include "BLI_utildefines.h"
50 #include "BLI_string.h"
53 #include "BKE_context.h"
56 #include "ED_view3d.h"
58 #include "BLT_translation.h"
60 #include "UI_resources.h"
62 #include "transform.h"
64 static void drawObjectConstraint(TransInfo *t);
66 /* ************************** CONSTRAINTS ************************* */
67 static void constraintAutoValues(TransInfo *t, float vec[3])
69 int mode = t->con.mode;
70 if (mode & CON_APPLY) {
71 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
73 if ((mode & CON_AXIS0) == 0) {
76 if ((mode & CON_AXIS1) == 0) {
79 if ((mode & CON_AXIS2) == 0) {
85 void constraintNumInput(TransInfo *t, float vec[3])
87 int mode = t->con.mode;
88 if (mode & CON_APPLY) {
89 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
91 const int dims = getConstraintSpaceDimension(t);
93 int axis = mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
94 if (axis == (CON_AXIS0 | CON_AXIS1)) {
95 /* vec[0] = vec[0]; */ /* same */
96 /* vec[1] = vec[1]; */ /* same */
99 else if (axis == (CON_AXIS1 | CON_AXIS2)) {
104 else if (axis == (CON_AXIS0 | CON_AXIS2)) {
105 /* vec[0] = vec[0]; */ /* same */
110 else if (dims == 1) {
111 if (mode & CON_AXIS0) {
112 /* vec[0] = vec[0]; */ /* same */
116 else if (mode & CON_AXIS1) {
121 else if (mode & CON_AXIS2) {
130 static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
134 mul_m3_v3(t->con.imtx, vec);
136 snapGridIncrement(t, vec);
138 if (t->flag & T_NULL_ONE) {
139 if (!(t->con.mode & CON_AXIS0))
142 if (!(t->con.mode & CON_AXIS1))
145 if (!(t->con.mode & CON_AXIS2))
149 if (applyNumInput(&t->num, vec)) {
150 constraintNumInput(t, vec);
151 removeAspectRatio(t, vec);
154 /* autovalues is operator param, use that directly but not if snapping is forced */
155 if (t->flag & T_AUTOVALUES && (t->tsnap.status & SNAP_FORCED) == 0) {
156 copy_v3_v3(vec, t->auto_values);
157 constraintAutoValues(t, vec);
158 /* inverse transformation at the end */
161 if (t->con.mode & CON_AXIS0) {
164 if (t->con.mode & CON_AXIS1) {
167 if (t->con.mode & CON_AXIS2) {
171 mul_m3_v3(t->con.mtx, vec);
174 static void viewAxisCorrectCenter(TransInfo *t, float t_con_center[3])
176 if (t->spacetype == SPACE_VIEW3D) {
177 // View3D *v3d = t->sa->spacedata.first;
178 const float min_dist = 1.0f; /* v3d->near; */
182 sub_v3_v3v3(dir, t_con_center, t->viewinv[3]);
183 if (dot_v3v3(dir, t->viewinv[2]) < 0.0f) {
186 project_v3_v3v3(dir, dir, t->viewinv[2]);
192 normalize_v3_v3_length(diff, t->viewinv[2], min_dist - l);
193 sub_v3_v3(t_con_center, diff);
198 static void axisProjection(TransInfo *t, const float axis[3], const float in[3], float out[3])
200 float norm[3], vec[3], factor, angle;
201 float t_con_center[3];
203 if (is_zero_v3(in)) {
207 copy_v3_v3(t_con_center, t->center_global);
209 /* checks for center being too close to the view center */
210 viewAxisCorrectCenter(t, t_con_center);
212 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
213 if (angle > (float)M_PI_2) {
214 angle = (float)M_PI - angle;
216 angle = RAD2DEGF(angle);
218 /* For when view is parallel to constraint... will cause NaNs otherwise
219 * So we take vertical motion in 3D space and apply it to the
220 * constraint axis. Nice for camera grab + MMB */
222 project_v3_v3v3(vec, in, t->viewinv[1]);
223 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
224 /* since camera distance is quite relative, use quadratic relationship. holding shift can compensate */
225 if (factor < 0.0f) factor *= -factor;
226 else factor *= factor;
228 /* -factor makes move down going backwards */
229 normalize_v3_v3_length(out, axis, -factor);
232 float v[3], i1[3], i2[3];
234 float norm_center[3];
237 getViewVector(t, t_con_center, norm_center);
238 cross_v3_v3v3(plane, norm_center, axis);
240 project_v3_v3v3(vec, in, plane);
241 sub_v3_v3v3(vec, in, vec);
243 add_v3_v3v3(v, vec, t_con_center);
244 getViewVector(t, v, norm);
246 /* give arbitrary large value if projection is impossible */
247 factor = dot_v3v3(axis, norm);
248 if (1.0f - fabsf(factor) < 0.0002f) {
249 copy_v3_v3(out, axis);
251 mul_v3_fl(out, 1000000000.0f);
254 mul_v3_fl(out, -1000000000.0f);
258 add_v3_v3v3(v2, t_con_center, axis);
259 add_v3_v3v3(v4, v, norm);
261 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
263 sub_v3_v3v3(v, i2, v);
265 sub_v3_v3v3(out, i1, t_con_center);
267 /* possible some values become nan when
268 * viewpoint and object are both zero */
269 if (!isfinite(out[0])) out[0] = 0.0f;
270 if (!isfinite(out[1])) out[1] = 0.0f;
271 if (!isfinite(out[2])) out[2] = 0.0f;
277 * Return true if the 2x axis are both aligned when projected into the view.
278 * In this case, we can't usefully project the cursor onto the plane.
280 static bool isPlaneProjectionViewAligned(TransInfo *t)
282 const float eps = 0.001f;
283 const float *constraint_vector[2];
285 for (int i = 0; i < 3; i++) {
286 if (t->con.mode & (CON_AXIS0 << i)) {
287 constraint_vector[n++] = t->con.mtx[i];
295 float view_to_plane[3], plane_normal[3];
297 getViewVector(t, t->center_global, view_to_plane);
299 cross_v3_v3v3(plane_normal, constraint_vector[0], constraint_vector[1]);
300 normalize_v3(plane_normal);
302 float factor = dot_v3v3(plane_normal, view_to_plane);
303 return fabsf(factor) < eps;
306 static void planeProjection(TransInfo *t, const float in[3], float out[3])
308 float vec[3], factor, norm[3];
310 add_v3_v3v3(vec, in, t->center_global);
311 getViewVector(t, vec, norm);
313 sub_v3_v3v3(vec, out, in);
315 factor = dot_v3v3(vec, norm);
316 if (fabsf(factor) <= 0.001f) {
317 return; /* prevent divide by zero */
319 factor = dot_v3v3(vec, vec) / factor;
321 copy_v3_v3(vec, norm);
322 mul_v3_fl(vec, factor);
324 add_v3_v3v3(out, in, vec);
328 * Generic callback for constant spatial constraints applied to linear motion
330 * The IN vector in projected into the constrained space and then further
331 * projected along the view vector.
332 * (in perspective mode, the view vector is relative to the position on screen)
336 static void applyAxisConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
339 if (!td && t->con.mode & CON_APPLY) {
340 mul_m3_v3(t->con.pmtx, out);
342 // With snap, a projection is alright, no need to correct for view alignment
343 if (!(!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t))) {
345 const int dims = getConstraintSpaceDimension(t);
347 if (!is_zero_v3(out)) {
348 if (!isPlaneProjectionViewAligned(t)) {
349 planeProjection(t, in, out);
353 else if (dims == 1) {
356 if (t->con.mode & CON_AXIS0) {
357 copy_v3_v3(c, t->con.mtx[0]);
359 else if (t->con.mode & CON_AXIS1) {
360 copy_v3_v3(c, t->con.mtx[1]);
362 else if (t->con.mode & CON_AXIS2) {
363 copy_v3_v3(c, t->con.mtx[2]);
365 axisProjection(t, c, in, out);
368 postConstraintChecks(t, out, pvec);
373 * Generic callback for object based spatial constraints applied to linear motion
375 * At first, the following is applied to the first data in the array
376 * The IN vector in projected into the constrained space and then further
377 * projected along the view vector.
378 * (in perspective mode, the view vector is relative to the position on screen)
380 * Further down, that vector is mapped to each data's space.
383 static void applyObjectConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
386 if (t->con.mode & CON_APPLY) {
388 mul_m3_v3(t->con.pmtx, out);
390 const int dims = getConstraintSpaceDimension(t);
392 if (!is_zero_v3(out)) {
393 if (!isPlaneProjectionViewAligned(t)) {
394 planeProjection(t, in, out);
398 else if (dims == 1) {
401 if (t->con.mode & CON_AXIS0) {
402 copy_v3_v3(c, t->con.mtx[0]);
404 else if (t->con.mode & CON_AXIS1) {
405 copy_v3_v3(c, t->con.mtx[1]);
407 else if (t->con.mode & CON_AXIS2) {
408 copy_v3_v3(c, t->con.mtx[2]);
410 axisProjection(t, c, in, out);
412 postConstraintChecks(t, out, pvec);
413 copy_v3_v3(out, pvec);
418 out[0] = out[1] = out[2] = 0.0f;
419 if (t->con.mode & CON_AXIS0) {
422 if (t->con.mode & CON_AXIS1) {
425 if (t->con.mode & CON_AXIS2) {
429 mul_m3_v3(td->axismtx, out);
430 if (t->flag & T_EDIT) {
431 mul_m3_v3(t->obedit_mat, out);
438 * Generic callback for constant spatial constraints applied to resize motion
441 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
443 if (!td && t->con.mode & CON_APPLY) {
446 if (!(t->con.mode & CON_AXIS0)) {
449 if (!(t->con.mode & CON_AXIS1)) {
452 if (!(t->con.mode & CON_AXIS2)) {
456 mul_m3_m3m3(tmat, smat, t->con.imtx);
457 mul_m3_m3m3(smat, t->con.mtx, tmat);
462 * Callback for object based spatial constraints applied to resize motion
465 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
467 if (td && t->con.mode & CON_APPLY) {
471 invert_m3_m3(imat, td->axismtx);
473 if (!(t->con.mode & CON_AXIS0)) {
476 if (!(t->con.mode & CON_AXIS1)) {
479 if (!(t->con.mode & CON_AXIS2)) {
483 mul_m3_m3m3(tmat, smat, imat);
484 if (t->flag & T_EDIT) {
485 mul_m3_m3m3(smat, t->obedit_mat, smat);
487 mul_m3_m3m3(smat, td->axismtx, tmat);
492 * Generic callback for constant spatial constraints applied to rotations
494 * The rotation axis is copied into VEC.
496 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
497 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
499 * The following only applies when CON_NOFLIP is not set.
500 * The vector is then modified to always point away from the screen (in global space)
501 * This insures that the rotation is always logically following the mouse.
502 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
505 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
507 if (!td && t->con.mode & CON_APPLY) {
508 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
512 case (CON_AXIS1 | CON_AXIS2):
513 copy_v3_v3(vec, t->con.mtx[0]);
516 case (CON_AXIS0 | CON_AXIS2):
517 copy_v3_v3(vec, t->con.mtx[1]);
520 case (CON_AXIS0 | CON_AXIS1):
521 copy_v3_v3(vec, t->con.mtx[2]);
524 /* don't flip axis if asked to or if num input */
525 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
526 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
534 * Callback for object based spatial constraints applied to rotations
536 * The rotation axis is copied into VEC.
538 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
539 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
541 * The following only applies when CON_NOFLIP is not set.
542 * The vector is then modified to always point away from the screen (in global space)
543 * This insures that the rotation is always logically following the mouse.
544 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
547 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
549 if (t->con.mode & CON_APPLY) {
550 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
551 float tmp_axismtx[3][3];
554 /* on setup call, use first object */
559 if (t->flag & T_EDIT) {
560 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
561 axismtx = tmp_axismtx;
564 axismtx = td->axismtx;
569 case (CON_AXIS1 | CON_AXIS2):
570 copy_v3_v3(vec, axismtx[0]);
573 case (CON_AXIS0 | CON_AXIS2):
574 copy_v3_v3(vec, axismtx[1]);
577 case (CON_AXIS0 | CON_AXIS1):
578 copy_v3_v3(vec, axismtx[2]);
581 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
582 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
589 /*--------------------- INTERNAL SETUP CALLS ------------------*/
591 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
593 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
594 copy_m3_m3(t->con.mtx, space);
596 getConstraintMatrix(t);
600 t->con.drawExtra = NULL;
601 t->con.applyVec = applyAxisConstraintVec;
602 t->con.applySize = applyAxisConstraintSize;
603 t->con.applyRot = applyAxisConstraintRot;
604 t->redraw = TREDRAW_HARD;
607 /* applies individual td->axismtx constraints */
608 void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
612 if (t->flag & T_EDIT) {
613 mul_m3_m3m3(axismtx, t->obedit_mat, t->data->axismtx);
616 copy_m3_m3(axismtx, t->data->axismtx);
619 setConstraint(t, axismtx, mode, text);
622 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
623 copy_m3_m3(t->con.mtx, t->data->axismtx);
625 getConstraintMatrix(t);
629 t->con.drawExtra = drawObjectConstraint;
630 t->con.applyVec = applyObjectConstraintVec;
631 t->con.applySize = applyObjectConstraintSize;
632 t->con.applyRot = applyObjectConstraintRot;
633 t->redraw = TREDRAW_HARD;
637 void setLocalConstraint(TransInfo *t, int mode, const char text[])
639 /* edit-mode now allows local transforms too */
640 if (t->flag & T_EDIT) {
641 setConstraint(t, t->obedit_mat, mode, text);
644 setAxisMatrixConstraint(t, mode, text);
649 * Set the constraint according to the user defined orientation
651 * ftext is a format string passed to BLI_snprintf. It will add the name of
652 * the orientation where %s is (logically).
654 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
658 switch (orientation) {
659 case V3D_MANIP_GLOBAL:
662 BLI_snprintf(text, sizeof(text), ftext, IFACE_("global"));
664 setConstraint(t, mtx, mode, text);
667 case V3D_MANIP_LOCAL:
668 BLI_snprintf(text, sizeof(text), ftext, IFACE_("local"));
669 setLocalConstraint(t, mode, text);
671 case V3D_MANIP_NORMAL:
672 BLI_snprintf(text, sizeof(text), ftext, IFACE_("normal"));
673 if (checkUseAxisMatrix(t)) {
674 setAxisMatrixConstraint(t, mode, text);
677 setConstraint(t, t->spacemtx, mode, text);
681 BLI_snprintf(text, sizeof(text), ftext, IFACE_("view"));
682 setConstraint(t, t->spacemtx, mode, text);
684 case V3D_MANIP_GIMBAL:
685 BLI_snprintf(text, sizeof(text), ftext, IFACE_("gimbal"));
686 setConstraint(t, t->spacemtx, mode, text);
688 case V3D_MANIP_CUSTOM:
690 char orientation_str[128];
691 BLI_snprintf(orientation_str, sizeof(orientation_str), "%s \"%s\"",
692 IFACE_("custom orientation"), t->custom_orientation->name);
693 BLI_snprintf(text, sizeof(text), ftext, orientation_str);
694 setConstraint(t, t->spacemtx, mode, text);
699 t->con.orientation = orientation;
701 t->con.mode |= CON_USER;
704 /*----------------- DRAWING CONSTRAINTS -------------------*/
706 void drawConstraint(TransInfo *t)
708 TransCon *tc = &(t->con);
710 if (!ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
712 if (!(tc->mode & CON_APPLY))
714 if (t->flag & T_NO_CONSTRAINT)
721 if (tc->mode & CON_SELECT) {
723 int depth_test_enabled;
725 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
726 add_v3_v3(vec, t->center_global);
728 drawLine(t, t->center_global, tc->mtx[0], 'X', 0);
729 drawLine(t, t->center_global, tc->mtx[1], 'Y', 0);
730 drawLine(t, t->center_global, tc->mtx[2], 'Z', 0);
732 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
733 if (depth_test_enabled)
734 glDisable(GL_DEPTH_TEST);
736 const uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
738 immBindBuiltinProgram(GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR);
740 float viewport_size[4];
741 glGetFloatv(GL_VIEWPORT, viewport_size);
742 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
744 immUniform1i("num_colors", 0); /* "simple" mode */
745 immUniformColor4f(1.0f, 1.0f, 1.0f, 1.0f);
746 immUniform1f("dash_width", 2.0f);
747 immUniform1f("dash_factor", 0.5f);
749 immBegin(GWN_PRIM_LINES, 2);
750 immVertex3fv(shdr_pos, t->center_global);
751 immVertex3fv(shdr_pos, vec);
756 if (depth_test_enabled)
757 glEnable(GL_DEPTH_TEST);
760 if (tc->mode & CON_AXIS0) {
761 drawLine(t, t->center_global, tc->mtx[0], 'X', DRAWLIGHT);
763 if (tc->mode & CON_AXIS1) {
764 drawLine(t, t->center_global, tc->mtx[1], 'Y', DRAWLIGHT);
766 if (tc->mode & CON_AXIS2) {
767 drawLine(t, t->center_global, tc->mtx[2], 'Z', DRAWLIGHT);
772 /* called from drawview.c, as an extra per-window draw option */
773 void drawPropCircle(const struct bContext *C, TransInfo *t)
775 if (t->flag & T_PROP_EDIT) {
776 RegionView3D *rv3d = CTX_wm_region_view3d(C);
777 float tmat[4][4], imat[4][4];
778 int depth_test_enabled;
780 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
781 copy_m4_m4(tmat, rv3d->viewmat);
782 invert_m4_m4(imat, tmat);
791 if (t->spacetype == SPACE_VIEW3D) {
794 else if (t->spacetype == SPACE_IMAGE) {
795 gpuScale2f(1.0f / t->aspect[0], 1.0f / t->aspect[1]);
797 else if (ELEM(t->spacetype, SPACE_IPO, SPACE_ACTION)) {
799 rcti *mask = &t->ar->v2d.mask;
800 rctf *datamask = &t->ar->v2d.cur;
801 float xsize = BLI_rctf_size_x(datamask);
802 float ysize = BLI_rctf_size_y(datamask);
803 float xmask = BLI_rcti_size_x(mask);
804 float ymask = BLI_rcti_size_y(mask);
805 gpuScale2f(1.0f, (ysize / xsize) * (xmask / ymask));
808 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
809 if (depth_test_enabled)
810 glDisable(GL_DEPTH_TEST);
812 unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
814 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
815 immUniformThemeColor(TH_GRID);
817 set_inverted_drawing(1);
818 imm_drawcircball(t->center_global, t->prop_size, imat, pos);
819 set_inverted_drawing(0);
823 if (depth_test_enabled)
824 glEnable(GL_DEPTH_TEST);
830 static void drawObjectConstraint(TransInfo *t)
832 /* Draw the first one lighter because that's the one who controls the others.
833 * Meaning the transformation is projected on that one and just copied on the others
835 * In a nutshell, the object with light axis is controlled by the user and the others follow.
836 * Without drawing the first light, users have little clue what they are doing.
838 short options = DRAWLIGHT;
839 TransData *td = t->data;
841 float tmp_axismtx[3][3];
843 for (i = 0; i < t->total; i++, td++) {
847 if (t->flag & T_PROP_EDIT) {
848 /* we're sorted, so skip the rest */
849 if (td->factor == 0.0f) {
854 if (t->flag & T_OBJECT) {
855 copy_v3_v3(co, td->ob->obmat[3]);
856 axismtx = td->axismtx;
858 else if (t->flag & T_EDIT) {
859 mul_v3_m4v3(co, t->obedit->obmat, td->center);
861 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
862 axismtx = tmp_axismtx;
864 else if (t->flag & T_POSE) {
865 mul_v3_m4v3(co, t->poseobj->obmat, td->center);
866 axismtx = td->axismtx;
869 copy_v3_v3(co, td->center);
870 axismtx = td->axismtx;
873 if (t->con.mode & CON_AXIS0) {
874 drawLine(t, co, axismtx[0], 'X', options);
876 if (t->con.mode & CON_AXIS1) {
877 drawLine(t, co, axismtx[1], 'Y', options);
879 if (t->con.mode & CON_AXIS2) {
880 drawLine(t, co, axismtx[2], 'Z', options);
882 options &= ~DRAWLIGHT;
886 /*--------------------- START / STOP CONSTRAINTS ---------------------- */
888 void startConstraint(TransInfo *t)
890 t->con.mode |= CON_APPLY;
892 t->num.idx_max = min_ii(getConstraintSpaceDimension(t) - 1, t->idx_max);
895 void stopConstraint(TransInfo *t)
897 t->con.mode &= ~(CON_APPLY | CON_SELECT);
899 t->num.idx_max = t->idx_max;
902 void getConstraintMatrix(TransInfo *t)
905 invert_m3_m3(t->con.imtx, t->con.mtx);
906 unit_m3(t->con.pmtx);
908 if (!(t->con.mode & CON_AXIS0)) {
909 zero_v3(t->con.pmtx[0]);
912 if (!(t->con.mode & CON_AXIS1)) {
913 zero_v3(t->con.pmtx[1]);
916 if (!(t->con.mode & CON_AXIS2)) {
917 zero_v3(t->con.pmtx[2]);
920 mul_m3_m3m3(mat, t->con.pmtx, t->con.imtx);
921 mul_m3_m3m3(t->con.pmtx, t->con.mtx, mat);
924 /*------------------------- MMB Select -------------------------------*/
926 void initSelectConstraint(TransInfo *t, float mtx[3][3])
928 copy_m3_m3(t->con.mtx, mtx);
929 t->con.mode |= CON_APPLY;
930 t->con.mode |= CON_SELECT;
933 t->con.drawExtra = NULL;
934 t->con.applyVec = applyAxisConstraintVec;
935 t->con.applySize = applyAxisConstraintSize;
936 t->con.applyRot = applyAxisConstraintRot;
939 void selectConstraint(TransInfo *t)
941 if (t->con.mode & CON_SELECT) {
947 void postSelectConstraint(TransInfo *t)
949 if (!(t->con.mode & CON_SELECT))
952 t->con.mode &= ~CON_AXIS0;
953 t->con.mode &= ~CON_AXIS1;
954 t->con.mode &= ~CON_AXIS2;
955 t->con.mode &= ~CON_SELECT;
960 t->redraw = TREDRAW_HARD;
963 static void setNearestAxis2d(TransInfo *t)
965 /* no correction needed... just use whichever one is lower */
966 if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1])) {
967 t->con.mode |= CON_AXIS1;
968 BLI_strncpy(t->con.text, IFACE_(" along Y axis"), sizeof(t->con.text));
971 t->con.mode |= CON_AXIS0;
972 BLI_strncpy(t->con.text, IFACE_(" along X axis"), sizeof(t->con.text));
976 static void setNearestAxis3d(TransInfo *t)
979 float mvec[3], proj[3];
983 /* calculate mouse movement */
984 mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
985 mvec[1] = (float)(t->mval[1] - t->con.imval[1]);
988 /* we need to correct axis length for the current zoomlevel of view,
989 * this to prevent projected values to be clipped behind the camera
990 * and to overflow the short integers.
991 * The formula used is a bit stupid, just a simplification of the subtraction
992 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
993 * projecting them with ED_view3d_win_to_delta and then get the length of that vector.
995 zfac = mul_project_m4_v3_zfac(t->persmat, t->center);
996 zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
998 for (i = 0; i < 3; i++) {
999 float axis[3], axis_2d[2];
1001 copy_v3_v3(axis, t->con.mtx[i]);
1003 mul_v3_fl(axis, zfac);
1004 /* now we can project to get window coordinate */
1005 add_v3_v3(axis, t->center_global);
1006 projectFloatView(t, axis, axis_2d);
1008 sub_v2_v2v2(axis, axis_2d, t->center2d);
1011 if (normalize_v3(axis) > 1e-3f) {
1012 project_v3_v3v3(proj, mvec, axis);
1013 sub_v3_v3v3(axis, mvec, proj);
1014 len[i] = normalize_v3(axis);
1021 if (len[0] <= len[1] && len[0] <= len[2]) {
1022 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1023 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
1024 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s X axis"), t->spacename);
1027 t->con.mode |= CON_AXIS0;
1028 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s X axis"), t->spacename);
1031 else if (len[1] <= len[0] && len[1] <= len[2]) {
1032 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1033 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
1034 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Y axis"), t->spacename);
1037 t->con.mode |= CON_AXIS1;
1038 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Y axis"), t->spacename);
1041 else if (len[2] <= len[1] && len[2] <= len[0]) {
1042 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1043 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
1044 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Z axis"), t->spacename);
1047 t->con.mode |= CON_AXIS2;
1048 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Z axis"), t->spacename);
1053 void setNearestAxis(TransInfo *t)
1055 /* clear any prior constraint flags */
1056 t->con.mode &= ~CON_AXIS0;
1057 t->con.mode &= ~CON_AXIS1;
1058 t->con.mode &= ~CON_AXIS2;
1060 /* constraint setting - depends on spacetype */
1061 if (t->spacetype == SPACE_VIEW3D) {
1063 setNearestAxis3d(t);
1066 /* assume that this means a 2D-Editor */
1067 setNearestAxis2d(t);
1070 getConstraintMatrix(t);
1073 /*-------------- HELPER FUNCTIONS ----------------*/
1075 char constraintModeToChar(TransInfo *t)
1077 if ((t->con.mode & CON_APPLY) == 0) {
1080 switch (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
1082 case (CON_AXIS1 | CON_AXIS2):
1085 case (CON_AXIS0 | CON_AXIS2):
1088 case (CON_AXIS0 | CON_AXIS1):
1096 bool isLockConstraint(TransInfo *t)
1098 int mode = t->con.mode;
1100 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1))
1103 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2))
1106 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2))
1113 * Returns the dimension of the constraint space.
1115 * For that reason, the flags always needs to be set to properly evaluate here,
1116 * even if they aren't actually used in the callback function. (Which could happen
1117 * for weird constraints not yet designed. Along a path for example.)
1120 int getConstraintSpaceDimension(TransInfo *t)
1124 if (t->con.mode & CON_AXIS0)
1127 if (t->con.mode & CON_AXIS1)
1130 if (t->con.mode & CON_AXIS2)
1135 * Someone willing to do it cryptically could do the following instead:
1137 * return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);
1139 * Based on the assumptions that the axis flags are one after the other and start at 1