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_length(diff, t->viewinv[2], min_dist - l);
191 sub_v3_v3(t_con_center, diff);
196 static void axisProjection(TransInfo *t, const float axis[3], const float in[3], float out[3])
198 float norm[3], vec[3], factor, angle;
199 float t_con_center[3];
201 if (is_zero_v3(in)) {
205 copy_v3_v3(t_con_center, t->center_global);
207 /* checks for center being too close to the view center */
208 viewAxisCorrectCenter(t, t_con_center);
210 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
211 if (angle > (float)M_PI_2) {
212 angle = (float)M_PI - angle;
214 angle = RAD2DEGF(angle);
216 /* For when view is parallel to constraint... will cause NaNs otherwise
217 * So we take vertical motion in 3D space and apply it to the
218 * constraint axis. Nice for camera grab + MMB */
220 project_v3_v3v3(vec, in, t->viewinv[1]);
221 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
222 /* since camera distance is quite relative, use quadratic relationship. holding shift can compensate */
223 if (factor < 0.0f) factor *= -factor;
224 else factor *= factor;
226 /* -factor makes move down going backwards */
227 normalize_v3_v3_length(out, axis, -factor);
230 float v[3], i1[3], i2[3];
232 float norm_center[3];
235 getViewVector(t, t_con_center, norm_center);
236 cross_v3_v3v3(plane, norm_center, axis);
238 project_v3_v3v3(vec, in, plane);
239 sub_v3_v3v3(vec, in, vec);
241 add_v3_v3v3(v, vec, t_con_center);
242 getViewVector(t, v, norm);
244 /* give arbitrary large value if projection is impossible */
245 factor = dot_v3v3(axis, norm);
246 if (1.0f - fabsf(factor) < 0.0002f) {
247 copy_v3_v3(out, axis);
249 mul_v3_fl(out, 1000000000.0f);
252 mul_v3_fl(out, -1000000000.0f);
256 add_v3_v3v3(v2, t_con_center, axis);
257 add_v3_v3v3(v4, v, norm);
259 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
261 sub_v3_v3v3(v, i2, v);
263 sub_v3_v3v3(out, i1, t_con_center);
265 /* possible some values become nan when
266 * viewpoint and object are both zero */
267 if (!isfinite(out[0])) out[0] = 0.0f;
268 if (!isfinite(out[1])) out[1] = 0.0f;
269 if (!isfinite(out[2])) out[2] = 0.0f;
275 * Return true if the 2x axis are both aligned when projected into the view.
276 * In this case, we can't usefully project the cursor onto the plane.
278 static bool isPlaneProjectionViewAligned(TransInfo *t)
280 const float eps = 0.001f;
281 const float *constraint_vector[2];
283 for (int i = 0; i < 3; i++) {
284 if (t->con.mode & (CON_AXIS0 << i)) {
285 constraint_vector[n++] = t->con.mtx[i];
293 float view_to_plane[3], plane_normal[3];
295 getViewVector(t, t->center_global, view_to_plane);
297 cross_v3_v3v3(plane_normal, constraint_vector[0], constraint_vector[1]);
298 normalize_v3(plane_normal);
300 float factor = dot_v3v3(plane_normal, view_to_plane);
301 return fabsf(factor) < eps;
304 static void planeProjection(TransInfo *t, const float in[3], float out[3])
306 float vec[3], factor, norm[3];
308 add_v3_v3v3(vec, in, t->center_global);
309 getViewVector(t, vec, norm);
311 sub_v3_v3v3(vec, out, in);
313 factor = dot_v3v3(vec, norm);
314 if (fabsf(factor) <= 0.001f) {
315 return; /* prevent divide by zero */
317 factor = dot_v3v3(vec, vec) / factor;
319 copy_v3_v3(vec, norm);
320 mul_v3_fl(vec, factor);
322 add_v3_v3v3(out, in, vec);
326 * Generic callback for constant spatial constraints applied to linear motion
328 * The IN vector in projected into the constrained space and then further
329 * projected along the view vector.
330 * (in perspective mode, the view vector is relative to the position on screen)
334 static void applyAxisConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
337 if (!td && t->con.mode & CON_APPLY) {
338 mul_m3_v3(t->con.pmtx, out);
340 // With snap, a projection is alright, no need to correct for view alignment
341 if (!(!ELEM(t->tsnap.mode, SCE_SNAP_MODE_INCREMENT, SCE_SNAP_MODE_GRID) && activeSnap(t))) {
343 const int dims = getConstraintSpaceDimension(t);
345 if (!is_zero_v3(out)) {
346 if (!isPlaneProjectionViewAligned(t)) {
347 planeProjection(t, in, out);
351 else if (dims == 1) {
354 if (t->con.mode & CON_AXIS0) {
355 copy_v3_v3(c, t->con.mtx[0]);
357 else if (t->con.mode & CON_AXIS1) {
358 copy_v3_v3(c, t->con.mtx[1]);
360 else if (t->con.mode & CON_AXIS2) {
361 copy_v3_v3(c, t->con.mtx[2]);
363 axisProjection(t, c, in, out);
366 postConstraintChecks(t, out, pvec);
371 * Generic callback for object based spatial constraints applied to linear motion
373 * At first, the following is applied to the first data in the array
374 * The IN vector in projected into the constrained space and then further
375 * projected along the view vector.
376 * (in perspective mode, the view vector is relative to the position on screen)
378 * Further down, that vector is mapped to each data's space.
381 static void applyObjectConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
384 if (t->con.mode & CON_APPLY) {
386 mul_m3_v3(t->con.pmtx, out);
388 const int dims = getConstraintSpaceDimension(t);
390 if (!is_zero_v3(out)) {
391 if (!isPlaneProjectionViewAligned(t)) {
392 planeProjection(t, in, out);
396 else if (dims == 1) {
399 if (t->con.mode & CON_AXIS0) {
400 copy_v3_v3(c, t->con.mtx[0]);
402 else if (t->con.mode & CON_AXIS1) {
403 copy_v3_v3(c, t->con.mtx[1]);
405 else if (t->con.mode & CON_AXIS2) {
406 copy_v3_v3(c, t->con.mtx[2]);
408 axisProjection(t, c, in, out);
410 postConstraintChecks(t, out, pvec);
411 copy_v3_v3(out, pvec);
416 out[0] = out[1] = out[2] = 0.0f;
417 if (t->con.mode & CON_AXIS0) {
420 if (t->con.mode & CON_AXIS1) {
423 if (t->con.mode & CON_AXIS2) {
427 mul_m3_v3(td->axismtx, out);
428 if (t->flag & T_EDIT) {
429 mul_m3_v3(t->obedit_mat, out);
436 * Generic callback for constant spatial constraints applied to resize motion
439 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
441 if (!td && t->con.mode & CON_APPLY) {
444 if (!(t->con.mode & CON_AXIS0)) {
447 if (!(t->con.mode & CON_AXIS1)) {
450 if (!(t->con.mode & CON_AXIS2)) {
454 mul_m3_m3m3(tmat, smat, t->con.imtx);
455 mul_m3_m3m3(smat, t->con.mtx, tmat);
460 * Callback for object based spatial constraints applied to resize motion
463 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
465 if (td && t->con.mode & CON_APPLY) {
469 invert_m3_m3(imat, td->axismtx);
471 if (!(t->con.mode & CON_AXIS0)) {
474 if (!(t->con.mode & CON_AXIS1)) {
477 if (!(t->con.mode & CON_AXIS2)) {
481 mul_m3_m3m3(tmat, smat, imat);
482 if (t->flag & T_EDIT) {
483 mul_m3_m3m3(smat, t->obedit_mat, smat);
485 mul_m3_m3m3(smat, td->axismtx, tmat);
490 * Generic callback for constant spatial constraints applied to rotations
492 * The rotation axis is copied into VEC.
494 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
495 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
497 * The following only applies when CON_NOFLIP is not set.
498 * The vector is then modified to always point away from the screen (in global space)
499 * This insures that the rotation is always logically following the mouse.
500 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
503 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
505 if (!td && t->con.mode & CON_APPLY) {
506 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
510 case (CON_AXIS1 | CON_AXIS2):
511 copy_v3_v3(vec, t->con.mtx[0]);
514 case (CON_AXIS0 | CON_AXIS2):
515 copy_v3_v3(vec, t->con.mtx[1]);
518 case (CON_AXIS0 | CON_AXIS1):
519 copy_v3_v3(vec, t->con.mtx[2]);
522 /* don't flip axis if asked to or if num input */
523 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
524 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
532 * Callback for object based spatial constraints applied to rotations
534 * The rotation axis is copied into VEC.
536 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
537 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
539 * The following only applies when CON_NOFLIP is not set.
540 * The vector is then modified to always point away from the screen (in global space)
541 * This insures that the rotation is always logically following the mouse.
542 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
545 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
547 if (t->con.mode & CON_APPLY) {
548 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
549 float tmp_axismtx[3][3];
552 /* on setup call, use first object */
557 if (t->flag & T_EDIT) {
558 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
559 axismtx = tmp_axismtx;
562 axismtx = td->axismtx;
567 case (CON_AXIS1 | CON_AXIS2):
568 copy_v3_v3(vec, axismtx[0]);
571 case (CON_AXIS0 | CON_AXIS2):
572 copy_v3_v3(vec, axismtx[1]);
575 case (CON_AXIS0 | CON_AXIS1):
576 copy_v3_v3(vec, axismtx[2]);
579 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
580 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
587 /*--------------------- INTERNAL SETUP CALLS ------------------*/
589 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
591 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
592 copy_m3_m3(t->con.mtx, space);
594 getConstraintMatrix(t);
598 t->con.drawExtra = NULL;
599 t->con.applyVec = applyAxisConstraintVec;
600 t->con.applySize = applyAxisConstraintSize;
601 t->con.applyRot = applyAxisConstraintRot;
602 t->redraw = TREDRAW_HARD;
605 /* applies individual td->axismtx constraints */
606 void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
610 if (t->flag & T_EDIT) {
611 mul_m3_m3m3(axismtx, t->obedit_mat, t->data->axismtx);
614 copy_m3_m3(axismtx, t->data->axismtx);
617 setConstraint(t, axismtx, mode, text);
620 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
621 copy_m3_m3(t->con.mtx, t->data->axismtx);
623 getConstraintMatrix(t);
627 t->con.drawExtra = drawObjectConstraint;
628 t->con.applyVec = applyObjectConstraintVec;
629 t->con.applySize = applyObjectConstraintSize;
630 t->con.applyRot = applyObjectConstraintRot;
631 t->redraw = TREDRAW_HARD;
635 void setLocalConstraint(TransInfo *t, int mode, const char text[])
637 /* edit-mode now allows local transforms too */
638 if (t->flag & T_EDIT) {
639 setConstraint(t, t->obedit_mat, mode, text);
642 setAxisMatrixConstraint(t, mode, text);
647 * Set the constraint according to the user defined orientation
649 * ftext is a format string passed to BLI_snprintf. It will add the name of
650 * the orientation where %s is (logically).
652 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
656 switch (orientation) {
657 case V3D_MANIP_GLOBAL:
660 BLI_snprintf(text, sizeof(text), ftext, IFACE_("global"));
662 setConstraint(t, mtx, mode, text);
665 case V3D_MANIP_LOCAL:
666 BLI_snprintf(text, sizeof(text), ftext, IFACE_("local"));
667 setLocalConstraint(t, mode, text);
669 case V3D_MANIP_NORMAL:
670 BLI_snprintf(text, sizeof(text), ftext, IFACE_("normal"));
671 if (checkUseAxisMatrix(t)) {
672 setAxisMatrixConstraint(t, mode, text);
675 setConstraint(t, t->spacemtx, mode, text);
679 BLI_snprintf(text, sizeof(text), ftext, IFACE_("view"));
680 setConstraint(t, t->spacemtx, mode, text);
682 case V3D_MANIP_GIMBAL:
683 BLI_snprintf(text, sizeof(text), ftext, IFACE_("gimbal"));
684 setConstraint(t, t->spacemtx, mode, text);
686 default: /* V3D_MANIP_CUSTOM */
687 BLI_snprintf(text, sizeof(text), ftext, t->spacename);
688 setConstraint(t, t->spacemtx, mode, text);
692 t->con.orientation = orientation;
694 t->con.mode |= CON_USER;
697 /*----------------- DRAWING CONSTRAINTS -------------------*/
699 void drawConstraint(TransInfo *t)
701 TransCon *tc = &(t->con);
703 if (!ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
705 if (!(tc->mode & CON_APPLY))
707 if (t->flag & T_USES_MANIPULATOR)
709 if (t->flag & T_NO_CONSTRAINT)
716 if (tc->mode & CON_SELECT) {
718 char col2[3] = {255, 255, 255};
719 int depth_test_enabled;
721 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
722 add_v3_v3(vec, t->center_global);
724 drawLine(t, t->center_global, tc->mtx[0], 'X', 0);
725 drawLine(t, t->center_global, tc->mtx[1], 'Y', 0);
726 drawLine(t, t->center_global, tc->mtx[2], 'Z', 0);
728 glColor3ubv((GLubyte *)col2);
730 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
731 if (depth_test_enabled)
732 glDisable(GL_DEPTH_TEST);
736 glVertex3fv(t->center_global);
741 if (depth_test_enabled)
742 glEnable(GL_DEPTH_TEST);
745 if (tc->mode & CON_AXIS0) {
746 drawLine(t, t->center_global, tc->mtx[0], 'X', DRAWLIGHT);
748 if (tc->mode & CON_AXIS1) {
749 drawLine(t, t->center_global, tc->mtx[1], 'Y', DRAWLIGHT);
751 if (tc->mode & CON_AXIS2) {
752 drawLine(t, t->center_global, tc->mtx[2], 'Z', DRAWLIGHT);
757 /* called from drawview.c, as an extra per-window draw option */
758 void drawPropCircle(const struct bContext *C, TransInfo *t)
760 if (t->flag & T_PROP_EDIT) {
761 RegionView3D *rv3d = CTX_wm_region_view3d(C);
762 float tmat[4][4], imat[4][4];
763 int depth_test_enabled;
765 UI_ThemeColor(TH_GRID);
767 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
768 copy_m4_m4(tmat, rv3d->viewmat);
769 invert_m4_m4(imat, tmat);
778 if (t->spacetype == SPACE_VIEW3D) {
781 else if (t->spacetype == SPACE_IMAGE) {
782 glScalef(1.0f / t->aspect[0], 1.0f / t->aspect[1], 1.0f);
784 else if (ELEM(t->spacetype, SPACE_IPO, SPACE_ACTION)) {
786 rcti *mask = &t->ar->v2d.mask;
787 rctf *datamask = &t->ar->v2d.cur;
788 float xsize = BLI_rctf_size_x(datamask);
789 float ysize = BLI_rctf_size_y(datamask);
790 float xmask = BLI_rcti_size_x(mask);
791 float ymask = BLI_rcti_size_y(mask);
792 glScalef(1.0f, (ysize / xsize) * (xmask / ymask), 1.0f);
795 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
796 if (depth_test_enabled)
797 glDisable(GL_DEPTH_TEST);
799 set_inverted_drawing(1);
800 drawcircball(GL_LINE_LOOP, t->center_global, t->prop_size, imat);
801 set_inverted_drawing(0);
803 if (depth_test_enabled)
804 glEnable(GL_DEPTH_TEST);
810 static void drawObjectConstraint(TransInfo *t)
812 /* Draw the first one lighter because that's the one who controls the others.
813 * Meaning the transformation is projected on that one and just copied on the others
815 * In a nutshell, the object with light axis is controlled by the user and the others follow.
816 * Without drawing the first light, users have little clue what they are doing.
818 short options = DRAWLIGHT;
819 TransData *td = t->data;
821 float tmp_axismtx[3][3];
823 for (i = 0; i < t->total; i++, td++) {
827 if (t->flag & T_PROP_EDIT) {
828 /* we're sorted, so skip the rest */
829 if (td->factor == 0.0f) {
834 if (t->options & CTX_GPENCIL_STROKES) {
835 /* only draw a constraint line for one point, otherwise we can't see anything */
836 if ((options & DRAWLIGHT) == 0) {
841 if (t->flag & T_OBJECT) {
842 copy_v3_v3(co, td->ob->obmat[3]);
843 axismtx = td->axismtx;
845 else if (t->flag & T_EDIT) {
846 mul_v3_m4v3(co, t->obedit->obmat, td->center);
848 mul_m3_m3m3(tmp_axismtx, t->obedit_mat, td->axismtx);
849 axismtx = tmp_axismtx;
851 else if (t->flag & T_POSE) {
852 mul_v3_m4v3(co, t->poseobj->obmat, td->center);
853 axismtx = td->axismtx;
856 copy_v3_v3(co, td->center);
857 axismtx = td->axismtx;
860 if (t->con.mode & CON_AXIS0) {
861 drawLine(t, co, axismtx[0], 'X', options);
863 if (t->con.mode & CON_AXIS1) {
864 drawLine(t, co, axismtx[1], 'Y', options);
866 if (t->con.mode & CON_AXIS2) {
867 drawLine(t, co, axismtx[2], 'Z', options);
869 options &= ~DRAWLIGHT;
873 /*--------------------- START / STOP CONSTRAINTS ---------------------- */
875 void startConstraint(TransInfo *t)
877 t->con.mode |= CON_APPLY;
879 t->num.idx_max = min_ii(getConstraintSpaceDimension(t) - 1, t->idx_max);
882 void stopConstraint(TransInfo *t)
884 t->con.mode &= ~(CON_APPLY | CON_SELECT);
886 t->num.idx_max = t->idx_max;
889 void getConstraintMatrix(TransInfo *t)
892 invert_m3_m3(t->con.imtx, t->con.mtx);
893 unit_m3(t->con.pmtx);
895 if (!(t->con.mode & CON_AXIS0)) {
896 zero_v3(t->con.pmtx[0]);
899 if (!(t->con.mode & CON_AXIS1)) {
900 zero_v3(t->con.pmtx[1]);
903 if (!(t->con.mode & CON_AXIS2)) {
904 zero_v3(t->con.pmtx[2]);
907 mul_m3_m3m3(mat, t->con.pmtx, t->con.imtx);
908 mul_m3_m3m3(t->con.pmtx, t->con.mtx, mat);
911 /*------------------------- MMB Select -------------------------------*/
913 void initSelectConstraint(TransInfo *t, float mtx[3][3])
915 copy_m3_m3(t->con.mtx, mtx);
916 t->con.mode |= CON_APPLY;
917 t->con.mode |= CON_SELECT;
920 t->con.drawExtra = NULL;
921 t->con.applyVec = applyAxisConstraintVec;
922 t->con.applySize = applyAxisConstraintSize;
923 t->con.applyRot = applyAxisConstraintRot;
926 void selectConstraint(TransInfo *t)
928 if (t->con.mode & CON_SELECT) {
934 void postSelectConstraint(TransInfo *t)
936 if (!(t->con.mode & CON_SELECT))
939 t->con.mode &= ~CON_AXIS0;
940 t->con.mode &= ~CON_AXIS1;
941 t->con.mode &= ~CON_AXIS2;
942 t->con.mode &= ~CON_SELECT;
947 t->redraw = TREDRAW_HARD;
950 static void setNearestAxis2d(TransInfo *t)
952 /* no correction needed... just use whichever one is lower */
953 if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1])) {
954 t->con.mode |= CON_AXIS1;
955 BLI_strncpy(t->con.text, IFACE_(" along Y axis"), sizeof(t->con.text));
958 t->con.mode |= CON_AXIS0;
959 BLI_strncpy(t->con.text, IFACE_(" along X axis"), sizeof(t->con.text));
963 static void setNearestAxis3d(TransInfo *t)
966 float mvec[3], proj[3];
970 /* calculate mouse movement */
971 mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
972 mvec[1] = (float)(t->mval[1] - t->con.imval[1]);
975 /* we need to correct axis length for the current zoomlevel of view,
976 * this to prevent projected values to be clipped behind the camera
977 * and to overflow the short integers.
978 * The formula used is a bit stupid, just a simplification of the subtraction
979 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
980 * projecting them with ED_view3d_win_to_delta and then get the length of that vector.
982 zfac = mul_project_m4_v3_zfac(t->persmat, t->center_global);
983 zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
985 for (i = 0; i < 3; i++) {
986 float axis[3], axis_2d[2];
988 copy_v3_v3(axis, t->con.mtx[i]);
990 mul_v3_fl(axis, zfac);
991 /* now we can project to get window coordinate */
992 add_v3_v3(axis, t->center_global);
993 projectFloatView(t, axis, axis_2d);
995 sub_v2_v2v2(axis, axis_2d, t->center2d);
998 if (normalize_v3(axis) > 1e-3f) {
999 project_v3_v3v3(proj, mvec, axis);
1000 sub_v3_v3v3(axis, mvec, proj);
1001 len[i] = normalize_v3(axis);
1008 if (len[0] <= len[1] && len[0] <= len[2]) {
1009 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1010 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
1011 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s X axis"), t->spacename);
1014 t->con.mode |= CON_AXIS0;
1015 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s X axis"), t->spacename);
1018 else if (len[1] <= len[0] && len[1] <= len[2]) {
1019 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1020 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
1021 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Y axis"), t->spacename);
1024 t->con.mode |= CON_AXIS1;
1025 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Y axis"), t->spacename);
1028 else if (len[2] <= len[1] && len[2] <= len[0]) {
1029 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
1030 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
1031 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Z axis"), t->spacename);
1034 t->con.mode |= CON_AXIS2;
1035 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Z axis"), t->spacename);
1040 void setNearestAxis(TransInfo *t)
1042 /* clear any prior constraint flags */
1043 t->con.mode &= ~CON_AXIS0;
1044 t->con.mode &= ~CON_AXIS1;
1045 t->con.mode &= ~CON_AXIS2;
1047 /* constraint setting - depends on spacetype */
1048 if (t->spacetype == SPACE_VIEW3D) {
1050 setNearestAxis3d(t);
1053 /* assume that this means a 2D-Editor */
1054 setNearestAxis2d(t);
1057 getConstraintMatrix(t);
1060 /*-------------- HELPER FUNCTIONS ----------------*/
1062 char constraintModeToChar(TransInfo *t)
1064 if ((t->con.mode & CON_APPLY) == 0) {
1067 switch (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
1069 case (CON_AXIS1 | CON_AXIS2):
1072 case (CON_AXIS0 | CON_AXIS2):
1075 case (CON_AXIS0 | CON_AXIS1):
1083 bool isLockConstraint(TransInfo *t)
1085 int mode = t->con.mode;
1087 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1))
1090 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2))
1093 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2))
1100 * Returns the dimension of the constraint space.
1102 * For that reason, the flags always needs to be set to properly evaluate here,
1103 * even if they aren't actually used in the callback function. (Which could happen
1104 * for weird constraints not yet designed. Along a path for example.)
1107 int getConstraintSpaceDimension(TransInfo *t)
1111 if (t->con.mode & CON_AXIS0)
1114 if (t->con.mode & CON_AXIS1)
1117 if (t->con.mode & CON_AXIS2)
1122 * Someone willing to do it cryptically could do the following instead:
1124 * return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);
1126 * Based on the assumptions that the axis flags are one after the other and start at 1