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"
44 #include "BIF_glutil.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_string.h"
51 #include "BKE_context.h"
54 #include "ED_view3d.h"
56 #include "BLT_translation.h"
58 #include "UI_resources.h"
60 #include "transform.h"
62 static void drawObjectConstraint(TransInfo *t);
64 /* ************************** CONSTRAINTS ************************* */
65 static void constraintAutoValues(TransInfo *t, float vec[3])
67 int mode = t->con.mode;
68 if (mode & CON_APPLY) {
69 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
71 if ((mode & CON_AXIS0) == 0) {
74 if ((mode & CON_AXIS1) == 0) {
77 if ((mode & CON_AXIS2) == 0) {
83 void constraintNumInput(TransInfo *t, float vec[3])
85 int mode = t->con.mode;
86 if (mode & CON_APPLY) {
87 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
89 const int dims = getConstraintSpaceDimension(t);
91 int axis = mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
92 if (axis == (CON_AXIS0 | CON_AXIS1)) {
93 /* vec[0] = vec[0]; */ /* same */
94 /* vec[1] = vec[1]; */ /* same */
97 else if (axis == (CON_AXIS1 | CON_AXIS2)) {
102 else if (axis == (CON_AXIS0 | CON_AXIS2)) {
103 /* vec[0] = vec[0]; */ /* same */
108 else if (dims == 1) {
109 if (mode & CON_AXIS0) {
110 /* vec[0] = vec[0]; */ /* same */
114 else if (mode & CON_AXIS1) {
119 else if (mode & CON_AXIS2) {
128 static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
132 mul_m3_v3(t->con.imtx, vec);
134 snapGridIncrement(t, vec);
136 if (t->flag & T_NULL_ONE) {
137 if (!(t->con.mode & CON_AXIS0))
140 if (!(t->con.mode & CON_AXIS1))
143 if (!(t->con.mode & CON_AXIS2))
147 if (applyNumInput(&t->num, vec)) {
148 constraintNumInput(t, vec);
149 removeAspectRatio(t, vec);
152 /* autovalues is operator param, use that directly but not if snapping is forced */
153 if (t->flag & T_AUTOVALUES && (t->tsnap.status & SNAP_FORCED) == 0) {
154 copy_v3_v3(vec, t->auto_values);
155 constraintAutoValues(t, vec);
156 /* inverse transformation at the end */
159 if (t->con.mode & CON_AXIS0) {
162 if (t->con.mode & CON_AXIS1) {
165 if (t->con.mode & CON_AXIS2) {
169 mul_m3_v3(t->con.mtx, vec);
172 static void viewAxisCorrectCenter(TransInfo *t, float t_con_center[3])
174 if (t->spacetype == SPACE_VIEW3D) {
175 // View3D *v3d = t->sa->spacedata.first;
176 const float min_dist = 1.0f; /* v3d->near; */
180 sub_v3_v3v3(dir, t_con_center, t->viewinv[3]);
181 if (dot_v3v3(dir, t->viewinv[2]) < 0.0f) {
184 project_v3_v3v3(dir, dir, t->viewinv[2]);
190 normalize_v3_v3(diff, t->viewinv[2]);
191 mul_v3_fl(diff, 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 copy_v3_v3(out, axis);
230 mul_v3_fl(out, -factor); /* -factor makes move down going backwards */
233 float v[3], i1[3], i2[3];
235 float norm_center[3];
238 getViewVector(t, t_con_center, norm_center);
239 cross_v3_v3v3(plane, norm_center, axis);
241 project_v3_v3v3(vec, in, plane);
242 sub_v3_v3v3(vec, in, vec);
244 add_v3_v3v3(v, vec, t_con_center);
245 getViewVector(t, v, norm);
247 /* give arbitrary large value if projection is impossible */
248 factor = dot_v3v3(axis, norm);
249 if (1.0f - fabsf(factor) < 0.0002f) {
250 copy_v3_v3(out, axis);
252 mul_v3_fl(out, 1000000000.0f);
255 mul_v3_fl(out, -1000000000.0f);
259 add_v3_v3v3(v2, t_con_center, axis);
260 add_v3_v3v3(v4, v, norm);
262 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
264 sub_v3_v3v3(v, i2, v);
266 sub_v3_v3v3(out, i1, t_con_center);
268 /* possible some values become nan when
269 * viewpoint and object are both zero */
270 if (!isfinite(out[0])) out[0] = 0.0f;
271 if (!isfinite(out[1])) out[1] = 0.0f;
272 if (!isfinite(out[2])) out[2] = 0.0f;
278 * Return true if the 2x axis are both aligned when projected into the view.
279 * In this case, we can't usefully project the cursor onto the plane.
281 static bool isPlaneProjectionViewAligned(TransInfo *t)
283 const float eps = 0.001f;
284 const float *constraint_vector[2];
286 for (int i = 0; i < 3; i++) {
287 if (t->con.mode & (CON_AXIS0 << i)) {
288 constraint_vector[n++] = t->con.mtx[i];
296 float view_to_plane[3], plane_normal[3];
298 getViewVector(t, t->center_global, view_to_plane);
300 cross_v3_v3v3(plane_normal, constraint_vector[0], constraint_vector[1]);
301 normalize_v3(plane_normal);
303 float factor = dot_v3v3(plane_normal, view_to_plane);
304 return fabsf(factor) < eps;
307 static void planeProjection(TransInfo *t, const float in[3], float out[3])
309 float vec[3], factor, norm[3];
311 add_v3_v3v3(vec, in, t->center_global);
312 getViewVector(t, vec, norm);
314 sub_v3_v3v3(vec, out, in);
316 factor = dot_v3v3(vec, norm);
317 if (fabsf(factor) <= 0.001f) {
318 return; /* prevent divide by zero */
320 factor = dot_v3v3(vec, vec) / factor;
322 copy_v3_v3(vec, norm);
323 mul_v3_fl(vec, factor);
325 add_v3_v3v3(out, in, vec);
329 * Generic callback for constant spatial constraints applied to linear motion
331 * The IN vector in projected into the constrained space and then further
332 * projected along the view vector.
333 * (in perspective mode, the view vector is relative to the position on screen)
337 static void applyAxisConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
340 if (!td && t->con.mode & CON_APPLY) {
341 mul_m3_v3(t->con.pmtx, out);
343 // With snap, a projection is alright, no need to correct for view alignment
344 if (!(!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t))) {
346 const int dims = getConstraintSpaceDimension(t);
348 if (!is_zero_v3(out)) {
349 if (!isPlaneProjectionViewAligned(t)) {
350 planeProjection(t, in, out);
354 else if (dims == 1) {
357 if (t->con.mode & CON_AXIS0) {
358 copy_v3_v3(c, t->con.mtx[0]);
360 else if (t->con.mode & CON_AXIS1) {
361 copy_v3_v3(c, t->con.mtx[1]);
363 else if (t->con.mode & CON_AXIS2) {
364 copy_v3_v3(c, t->con.mtx[2]);
366 axisProjection(t, c, in, out);
369 postConstraintChecks(t, out, pvec);
374 * Generic callback for object based spatial constraints applied to linear motion
376 * At first, the following is applied to the first data in the array
377 * The IN vector in projected into the constrained space and then further
378 * projected along the view vector.
379 * (in perspective mode, the view vector is relative to the position on screen)
381 * Further down, that vector is mapped to each data's space.
384 static void applyObjectConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
387 if (t->con.mode & CON_APPLY) {
389 mul_m3_v3(t->con.pmtx, out);
391 const int dims = getConstraintSpaceDimension(t);
393 if (!is_zero_v3(out)) {
394 if (!isPlaneProjectionViewAligned(t)) {
395 planeProjection(t, in, out);
399 else if (dims == 1) {
402 if (t->con.mode & CON_AXIS0) {
403 copy_v3_v3(c, t->con.mtx[0]);
405 else if (t->con.mode & CON_AXIS1) {
406 copy_v3_v3(c, t->con.mtx[1]);
408 else if (t->con.mode & CON_AXIS2) {
409 copy_v3_v3(c, t->con.mtx[2]);
411 axisProjection(t, c, in, out);
413 postConstraintChecks(t, out, pvec);
414 copy_v3_v3(out, pvec);
419 out[0] = out[1] = out[2] = 0.0f;
420 if (t->con.mode & CON_AXIS0) {
423 if (t->con.mode & CON_AXIS1) {
426 if (t->con.mode & CON_AXIS2) {
430 mul_m3_v3(td->axismtx, out);
431 if (t->flag & T_EDIT) {
432 mul_m3_v3(t->obedit_mat, out);
439 * Generic callback for constant spatial constraints applied to resize motion
442 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
444 if (!td && t->con.mode & CON_APPLY) {
447 if (!(t->con.mode & CON_AXIS0)) {
450 if (!(t->con.mode & CON_AXIS1)) {
453 if (!(t->con.mode & CON_AXIS2)) {
457 mul_m3_m3m3(tmat, smat, t->con.imtx);
458 mul_m3_m3m3(smat, t->con.mtx, tmat);
463 * Callback for object based spatial constraints applied to resize motion
466 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
468 if (td && t->con.mode & CON_APPLY) {
472 invert_m3_m3(imat, td->axismtx);
474 if (!(t->con.mode & CON_AXIS0)) {
477 if (!(t->con.mode & CON_AXIS1)) {
480 if (!(t->con.mode & CON_AXIS2)) {
484 mul_m3_m3m3(tmat, smat, imat);
485 if (t->flag & T_EDIT) {
486 mul_m3_m3m3(smat, t->obedit_mat, smat);
488 mul_m3_m3m3(smat, td->axismtx, tmat);
493 * Generic callback for constant spatial constraints applied to rotations
495 * The rotation axis is copied into VEC.
497 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
498 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
500 * The following only applies when CON_NOFLIP is not set.
501 * The vector is then modified to always point away from the screen (in global space)
502 * This insures that the rotation is always logically following the mouse.
503 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
506 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
508 if (!td && t->con.mode & CON_APPLY) {
509 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
513 case (CON_AXIS1 | CON_AXIS2):
514 copy_v3_v3(vec, t->con.mtx[0]);
517 case (CON_AXIS0 | CON_AXIS2):
518 copy_v3_v3(vec, t->con.mtx[1]);
521 case (CON_AXIS0 | CON_AXIS1):
522 copy_v3_v3(vec, t->con.mtx[2]);
525 /* don't flip axis if asked to or if num input */
526 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
527 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
535 * Callback for object based spatial constraints applied to rotations
537 * The rotation axis is copied into VEC.
539 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
540 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
542 * The following only applies when CON_NOFLIP is not set.
543 * The vector is then modified to always point away from the screen (in global space)
544 * This insures that the rotation is always logically following the mouse.
545 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
548 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
550 if (t->con.mode & CON_APPLY) {
551 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
552 float tmp_axismtx[3][3];
555 /* on setup call, use first object */
560 if (t->flag & T_EDIT) {
561 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
562 axismtx = tmp_axismtx;
565 axismtx = td->axismtx;
570 case (CON_AXIS1 | CON_AXIS2):
571 copy_v3_v3(vec, axismtx[0]);
574 case (CON_AXIS0 | CON_AXIS2):
575 copy_v3_v3(vec, axismtx[1]);
578 case (CON_AXIS0 | CON_AXIS1):
579 copy_v3_v3(vec, axismtx[2]);
582 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
583 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
590 /*--------------------- INTERNAL SETUP CALLS ------------------*/
592 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
594 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
595 copy_m3_m3(t->con.mtx, space);
597 getConstraintMatrix(t);
601 t->con.drawExtra = NULL;
602 t->con.applyVec = applyAxisConstraintVec;
603 t->con.applySize = applyAxisConstraintSize;
604 t->con.applyRot = applyAxisConstraintRot;
605 t->redraw = TREDRAW_HARD;
608 /* applies individual td->axismtx constraints */
609 void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
613 if (t->flag & T_EDIT) {
614 mul_m3_m3m3(axismtx, t->obedit_mat, t->data->axismtx);
617 copy_m3_m3(axismtx, t->data->axismtx);
620 setConstraint(t, axismtx, mode, text);
623 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
624 copy_m3_m3(t->con.mtx, t->data->axismtx);
626 getConstraintMatrix(t);
630 t->con.drawExtra = drawObjectConstraint;
631 t->con.applyVec = applyObjectConstraintVec;
632 t->con.applySize = applyObjectConstraintSize;
633 t->con.applyRot = applyObjectConstraintRot;
634 t->redraw = TREDRAW_HARD;
638 void setLocalConstraint(TransInfo *t, int mode, const char text[])
640 /* edit-mode now allows local transforms too */
641 if (t->flag & T_EDIT) {
642 setConstraint(t, t->obedit_mat, mode, text);
645 setAxisMatrixConstraint(t, mode, text);
650 * Set the constraint according to the user defined orientation
652 * ftext is a format string passed to BLI_snprintf. It will add the name of
653 * the orientation where %s is (logically).
655 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
659 switch (orientation) {
660 case V3D_MANIP_GLOBAL:
663 BLI_snprintf(text, sizeof(text), ftext, IFACE_("global"));
665 setConstraint(t, mtx, mode, text);
668 case V3D_MANIP_LOCAL:
669 BLI_snprintf(text, sizeof(text), ftext, IFACE_("local"));
670 setLocalConstraint(t, mode, text);
672 case V3D_MANIP_NORMAL:
673 BLI_snprintf(text, sizeof(text), ftext, IFACE_("normal"));
674 if (checkUseAxisMatrix(t)) {
675 setAxisMatrixConstraint(t, mode, text);
678 setConstraint(t, t->spacemtx, mode, text);
682 BLI_snprintf(text, sizeof(text), ftext, IFACE_("view"));
683 setConstraint(t, t->spacemtx, mode, text);
685 case V3D_MANIP_GIMBAL:
686 BLI_snprintf(text, sizeof(text), ftext, IFACE_("gimbal"));
687 setConstraint(t, t->spacemtx, mode, text);
689 default: /* V3D_MANIP_CUSTOM */
690 BLI_snprintf(text, sizeof(text), ftext, t->spacename);
691 setConstraint(t, t->spacemtx, mode, text);
695 t->con.orientation = orientation;
697 t->con.mode |= CON_USER;
700 /*----------------- DRAWING CONSTRAINTS -------------------*/
702 void drawConstraint(TransInfo *t)
704 TransCon *tc = &(t->con);
706 if (!ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
708 if (!(tc->mode & CON_APPLY))
710 if (t->flag & T_USES_MANIPULATOR)
712 if (t->flag & T_NO_CONSTRAINT)
719 if (tc->mode & CON_SELECT) {
721 char col2[3] = {255, 255, 255};
722 int depth_test_enabled;
724 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
725 add_v3_v3(vec, t->center_global);
727 drawLine(t, t->center_global, tc->mtx[0], 'X', 0);
728 drawLine(t, t->center_global, tc->mtx[1], 'Y', 0);
729 drawLine(t, t->center_global, tc->mtx[2], 'Z', 0);
731 glColor3ubv((GLubyte *)col2);
733 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
734 if (depth_test_enabled)
735 glDisable(GL_DEPTH_TEST);
739 glVertex3fv(t->center_global);
744 if (depth_test_enabled)
745 glEnable(GL_DEPTH_TEST);
748 if (tc->mode & CON_AXIS0) {
749 drawLine(t, t->center_global, tc->mtx[0], 'X', DRAWLIGHT);
751 if (tc->mode & CON_AXIS1) {
752 drawLine(t, t->center_global, tc->mtx[1], 'Y', DRAWLIGHT);
754 if (tc->mode & CON_AXIS2) {
755 drawLine(t, t->center_global, tc->mtx[2], 'Z', DRAWLIGHT);
760 /* called from drawview.c, as an extra per-window draw option */
761 void drawPropCircle(const struct bContext *C, TransInfo *t)
763 if (t->flag & T_PROP_EDIT) {
764 RegionView3D *rv3d = CTX_wm_region_view3d(C);
765 float tmat[4][4], imat[4][4];
766 int depth_test_enabled;
768 UI_ThemeColor(TH_GRID);
770 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
771 copy_m4_m4(tmat, rv3d->viewmat);
772 invert_m4_m4(imat, tmat);
781 if (t->spacetype == SPACE_VIEW3D) {
784 else if (t->spacetype == SPACE_IMAGE) {
785 glScalef(1.0f / t->aspect[0], 1.0f / t->aspect[1], 1.0f);
787 else if (ELEM(t->spacetype, SPACE_IPO, SPACE_ACTION)) {
789 rcti *mask = &t->ar->v2d.mask;
790 rctf *datamask = &t->ar->v2d.cur;
791 float xsize = BLI_rctf_size_x(datamask);
792 float ysize = BLI_rctf_size_y(datamask);
793 float xmask = BLI_rcti_size_x(mask);
794 float ymask = BLI_rcti_size_y(mask);
795 glScalef(1.0f, (ysize / xsize) * (xmask / ymask), 1.0f);
798 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
799 if (depth_test_enabled)
800 glDisable(GL_DEPTH_TEST);
802 set_inverted_drawing(1);
803 drawcircball(GL_LINE_LOOP, t->center_global, t->prop_size, imat);
804 set_inverted_drawing(0);
806 if (depth_test_enabled)
807 glEnable(GL_DEPTH_TEST);
813 static void drawObjectConstraint(TransInfo *t)
815 /* Draw the first one lighter because that's the one who controls the others.
816 * Meaning the transformation is projected on that one and just copied on the others
818 * In a nutshell, the object with light axis is controlled by the user and the others follow.
819 * Without drawing the first light, users have little clue what they are doing.
821 short options = DRAWLIGHT;
822 TransData *td = t->data;
824 float tmp_axismtx[3][3];
826 for (i = 0; i < t->total; i++, td++) {
830 if (t->flag & T_PROP_EDIT) {
831 /* we're sorted, so skip the rest */
832 if (td->factor == 0.0f) {
837 if (t->flag & T_OBJECT) {
838 copy_v3_v3(co, td->ob->obmat[3]);
839 axismtx = td->axismtx;
841 else if (t->flag & T_EDIT) {
842 mul_v3_m4v3(co, t->obedit->obmat, td->center);
844 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
845 axismtx = tmp_axismtx;
847 else if (t->flag & T_POSE) {
848 mul_v3_m4v3(co, t->poseobj->obmat, td->center);
849 axismtx = td->axismtx;
852 copy_v3_v3(co, td->center);
853 axismtx = td->axismtx;
856 if (t->con.mode & CON_AXIS0) {
857 drawLine(t, co, axismtx[0], 'X', options);
859 if (t->con.mode & CON_AXIS1) {
860 drawLine(t, co, axismtx[1], 'Y', options);
862 if (t->con.mode & CON_AXIS2) {
863 drawLine(t, co, axismtx[2], 'Z', options);
865 options &= ~DRAWLIGHT;
869 /*--------------------- START / STOP CONSTRAINTS ---------------------- */
871 void startConstraint(TransInfo *t)
873 t->con.mode |= CON_APPLY;
875 t->num.idx_max = min_ii(getConstraintSpaceDimension(t) - 1, t->idx_max);
878 void stopConstraint(TransInfo *t)
880 t->con.mode &= ~(CON_APPLY | CON_SELECT);
882 t->num.idx_max = t->idx_max;
885 void getConstraintMatrix(TransInfo *t)
888 invert_m3_m3(t->con.imtx, t->con.mtx);
889 unit_m3(t->con.pmtx);
891 if (!(t->con.mode & CON_AXIS0)) {
892 zero_v3(t->con.pmtx[0]);
895 if (!(t->con.mode & CON_AXIS1)) {
896 zero_v3(t->con.pmtx[1]);
899 if (!(t->con.mode & CON_AXIS2)) {
900 zero_v3(t->con.pmtx[2]);
903 mul_m3_m3m3(mat, t->con.pmtx, t->con.imtx);
904 mul_m3_m3m3(t->con.pmtx, t->con.mtx, mat);
907 /*------------------------- MMB Select -------------------------------*/
909 void initSelectConstraint(TransInfo *t, float mtx[3][3])
911 copy_m3_m3(t->con.mtx, mtx);
912 t->con.mode |= CON_APPLY;
913 t->con.mode |= CON_SELECT;
916 t->con.drawExtra = NULL;
917 t->con.applyVec = applyAxisConstraintVec;
918 t->con.applySize = applyAxisConstraintSize;
919 t->con.applyRot = applyAxisConstraintRot;
922 void selectConstraint(TransInfo *t)
924 if (t->con.mode & CON_SELECT) {
930 void postSelectConstraint(TransInfo *t)
932 if (!(t->con.mode & CON_SELECT))
935 t->con.mode &= ~CON_AXIS0;
936 t->con.mode &= ~CON_AXIS1;
937 t->con.mode &= ~CON_AXIS2;
938 t->con.mode &= ~CON_SELECT;
943 t->redraw = TREDRAW_HARD;
946 static void setNearestAxis2d(TransInfo *t)
948 /* no correction needed... just use whichever one is lower */
949 if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1])) {
950 t->con.mode |= CON_AXIS1;
951 BLI_strncpy(t->con.text, IFACE_(" along Y axis"), sizeof(t->con.text));
954 t->con.mode |= CON_AXIS0;
955 BLI_strncpy(t->con.text, IFACE_(" along X axis"), sizeof(t->con.text));
959 static void setNearestAxis3d(TransInfo *t)
962 float mvec[3], proj[3];
966 /* calculate mouse movement */
967 mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
968 mvec[1] = (float)(t->mval[1] - t->con.imval[1]);
971 /* we need to correct axis length for the current zoomlevel of view,
972 * this to prevent projected values to be clipped behind the camera
973 * and to overflow the short integers.
974 * The formula used is a bit stupid, just a simplification of the subtraction
975 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
976 * projecting them with ED_view3d_win_to_delta and then get the length of that vector.
978 zfac = mul_project_m4_v3_zfac(t->persmat, t->center);
979 zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
981 for (i = 0; i < 3; i++) {
982 float axis[3], axis_2d[2];
984 copy_v3_v3(axis, t->con.mtx[i]);
986 mul_v3_fl(axis, zfac);
987 /* now we can project to get window coordinate */
988 add_v3_v3(axis, t->center_global);
989 projectFloatView(t, axis, axis_2d);
991 sub_v2_v2v2(axis, axis_2d, t->center2d);
994 if (normalize_v3(axis) > 1e-3f) {
995 project_v3_v3v3(proj, mvec, axis);
996 sub_v3_v3v3(axis, mvec, proj);
997 len[i] = normalize_v3(axis);
1004 if (len[0] <= len[1] && len[0] <= len[2]) {
1005 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1006 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
1007 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s X axis"), t->spacename);
1010 t->con.mode |= CON_AXIS0;
1011 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s X axis"), t->spacename);
1014 else if (len[1] <= len[0] && len[1] <= len[2]) {
1015 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1016 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
1017 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Y axis"), t->spacename);
1020 t->con.mode |= CON_AXIS1;
1021 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Y axis"), t->spacename);
1024 else if (len[2] <= len[1] && len[2] <= len[0]) {
1025 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1026 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
1027 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Z axis"), t->spacename);
1030 t->con.mode |= CON_AXIS2;
1031 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Z axis"), t->spacename);
1036 void setNearestAxis(TransInfo *t)
1038 /* clear any prior constraint flags */
1039 t->con.mode &= ~CON_AXIS0;
1040 t->con.mode &= ~CON_AXIS1;
1041 t->con.mode &= ~CON_AXIS2;
1043 /* constraint setting - depends on spacetype */
1044 if (t->spacetype == SPACE_VIEW3D) {
1046 setNearestAxis3d(t);
1049 /* assume that this means a 2D-Editor */
1050 setNearestAxis2d(t);
1053 getConstraintMatrix(t);
1056 /*-------------- HELPER FUNCTIONS ----------------*/
1058 char constraintModeToChar(TransInfo *t)
1060 if ((t->con.mode & CON_APPLY) == 0) {
1063 switch (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
1065 case (CON_AXIS1 | CON_AXIS2):
1068 case (CON_AXIS0 | CON_AXIS2):
1071 case (CON_AXIS0 | CON_AXIS1):
1079 bool isLockConstraint(TransInfo *t)
1081 int mode = t->con.mode;
1083 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1))
1086 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2))
1089 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2))
1096 * Returns the dimension of the constraint space.
1098 * For that reason, the flags always needs to be set to properly evaluate here,
1099 * even if they aren't actually used in the callback function. (Which could happen
1100 * for weird constraints not yet designed. Along a path for example.)
1103 int getConstraintSpaceDimension(TransInfo *t)
1107 if (t->con.mode & CON_AXIS0)
1110 if (t->con.mode & CON_AXIS1)
1113 if (t->con.mode & CON_AXIS2)
1118 * Someone willing to do it cryptically could do the following instead:
1120 * return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);
1122 * Based on the assumptions that the axis flags are one after the other and start at 1