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
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_view3d_types.h"
52 #include "BIF_glutil.h"
54 #include "BKE_context.h"
57 #include "ED_view3d.h"
60 #include "BLI_utildefines.h"
61 #include "BLI_string.h"
63 #include "UI_resources.h"
65 #include "transform.h"
67 static void drawObjectConstraint(TransInfo *t);
69 /* ************************** CONSTRAINTS ************************* */
70 static void constraintAutoValues(TransInfo *t, float vec[3])
72 int mode = t->con.mode;
73 if (mode & CON_APPLY) {
74 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
76 if ((mode & CON_AXIS0) == 0) {
79 if ((mode & CON_AXIS1) == 0) {
82 if ((mode & CON_AXIS2) == 0) {
88 void constraintNumInput(TransInfo *t, float vec[3])
90 int mode = t->con.mode;
91 if (mode & CON_APPLY) {
92 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
94 if (getConstraintSpaceDimension(t) == 2) {
95 int axis = mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
96 if (axis == (CON_AXIS0 | CON_AXIS1)) {
97 /* vec[0] = vec[0]; */ /* same */
98 /* vec[1] = vec[1]; */ /* same */
101 else if (axis == (CON_AXIS1 | CON_AXIS2)) {
106 else if (axis == (CON_AXIS0 | CON_AXIS2)) {
107 /* vec[0] = vec[0]; */ /* same */
112 else if (getConstraintSpaceDimension(t) == 1) {
113 if (mode & CON_AXIS0) {
114 /* vec[0] = vec[0]; */ /* same */
118 else if (mode & CON_AXIS1) {
123 else if (mode & CON_AXIS2) {
132 static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
136 mul_m3_v3(t->con.imtx, vec);
140 if (t->num.flag & T_NULL_ONE) {
141 if (!(t->con.mode & CON_AXIS0))
144 if (!(t->con.mode & CON_AXIS1))
147 if (!(t->con.mode & CON_AXIS2))
151 if (hasNumInput(&t->num)) {
152 applyNumInput(&t->num, vec);
153 removeAspectRatio(t, vec);
154 constraintNumInput(t, vec);
157 /* autovalues is operator param, use that directly but not if snapping is forced */
158 if (t->flag & T_AUTOVALUES && (t->tsnap.status & SNAP_FORCED) == 0) {
159 mul_v3_m3v3(vec, t->con.imtx, t->auto_values);
160 constraintAutoValues(t, vec);
161 /* inverse transformation at the end */
164 if (t->con.mode & CON_AXIS0) {
167 if (t->con.mode & CON_AXIS1) {
170 if (t->con.mode & CON_AXIS2) {
174 mul_m3_v3(t->con.mtx, vec);
177 static void viewAxisCorrectCenter(TransInfo *t, float t_con_center[3])
179 if (t->spacetype == SPACE_VIEW3D) {
180 // View3D *v3d = t->sa->spacedata.first;
181 const float min_dist = 1.0f; /* v3d->near; */
185 sub_v3_v3v3(dir, t_con_center, t->viewinv[3]);
186 if (dot_v3v3(dir, t->viewinv[2]) < 0.0f) {
189 project_v3_v3v3(dir, dir, t->viewinv[2]);
195 normalize_v3_v3(diff, t->viewinv[2]);
196 mul_v3_fl(diff, min_dist - l);
198 sub_v3_v3(t_con_center, diff);
203 static void axisProjection(TransInfo *t, float axis[3], float in[3], float out[3])
205 float norm[3], vec[3], factor, angle;
206 float t_con_center[3];
208 if (in[0] == 0.0f && in[1] == 0.0f && in[2] == 0.0f)
211 copy_v3_v3(t_con_center, t->con.center);
213 /* checks for center being too close to the view center */
214 viewAxisCorrectCenter(t, t_con_center);
216 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
217 if (angle > (float)M_PI / 2.0f) {
218 angle = (float)M_PI - angle;
220 angle = RAD2DEGF(angle);
222 /* For when view is parallel to constraint... will cause NaNs otherwise
223 * So we take vertical motion in 3D space and apply it to the
224 * constraint axis. Nice for camera grab + MMB */
226 project_v3_v3v3(vec, in, t->viewinv[1]);
227 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
228 /* since camera distance is quite relative, use quadratic relationship. holding shift can compensate */
229 if (factor < 0.0f) factor *= -factor;
230 else factor *= factor;
232 copy_v3_v3(out, axis);
234 mul_v3_fl(out, -factor); /* -factor makes move down going backwards */
237 float v[3], i1[3], i2[3];
239 float norm_center[3];
242 getViewVector(t, t_con_center, norm_center);
243 cross_v3_v3v3(plane, norm_center, axis);
245 project_v3_v3v3(vec, in, plane);
246 sub_v3_v3v3(vec, in, vec);
248 add_v3_v3v3(v, vec, t_con_center);
249 getViewVector(t, v, norm);
251 /* give arbitrary large value if projection is impossible */
252 factor = dot_v3v3(axis, norm);
253 if (1.0f - fabsf(factor) < 0.0002f) {
254 copy_v3_v3(out, axis);
256 mul_v3_fl(out, 1000000000.0f);
259 mul_v3_fl(out, -1000000000.0f);
263 add_v3_v3v3(v2, t_con_center, axis);
264 add_v3_v3v3(v4, v, norm);
266 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
268 sub_v3_v3v3(v, i2, v);
270 sub_v3_v3v3(out, i1, t_con_center);
272 /* possible some values become nan when
273 * viewpoint and object are both zero */
274 if (!finite(out[0])) out[0] = 0.0f;
275 if (!finite(out[1])) out[1] = 0.0f;
276 if (!finite(out[2])) out[2] = 0.0f;
281 static void planeProjection(TransInfo *t, float in[3], float out[3])
283 float vec[3], factor, norm[3];
285 add_v3_v3v3(vec, in, t->con.center);
286 getViewVector(t, vec, norm);
288 sub_v3_v3v3(vec, out, in);
290 factor = dot_v3v3(vec, norm);
291 if (fabsf(factor) <= 0.001f) {
292 return; /* prevent divide by zero */
294 factor = dot_v3v3(vec, vec) / factor;
296 copy_v3_v3(vec, norm);
297 mul_v3_fl(vec, factor);
299 add_v3_v3v3(out, in, vec);
303 * Generic callback for constant spatial constraints applied to linear motion
305 * The IN vector in projected into the constrained space and then further
306 * projected along the view vector.
307 * (in perspective mode, the view vector is relative to the position on screen)
311 static void applyAxisConstraintVec(TransInfo *t, TransData *td, float in[3], float out[3], float pvec[3])
314 if (!td && t->con.mode & CON_APPLY) {
315 mul_m3_v3(t->con.pmtx, out);
317 // With snap, a projection is alright, no need to correct for view alignment
318 if (!(t->tsnap.mode != SCE_SNAP_MODE_INCREMENT && activeSnap(t))) {
319 if (getConstraintSpaceDimension(t) == 2) {
320 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
321 planeProjection(t, in, out);
324 else if (getConstraintSpaceDimension(t) == 1) {
327 if (t->con.mode & CON_AXIS0) {
328 copy_v3_v3(c, t->con.mtx[0]);
330 else if (t->con.mode & CON_AXIS1) {
331 copy_v3_v3(c, t->con.mtx[1]);
333 else if (t->con.mode & CON_AXIS2) {
334 copy_v3_v3(c, t->con.mtx[2]);
336 axisProjection(t, c, in, out);
339 postConstraintChecks(t, out, pvec);
344 * Generic callback for object based spatial constraints applied to linear motion
346 * At first, the following is applied to the first data in the array
347 * The IN vector in projected into the constrained space and then further
348 * projected along the view vector.
349 * (in perspective mode, the view vector is relative to the position on screen)
351 * Further down, that vector is mapped to each data's space.
354 static void applyObjectConstraintVec(TransInfo *t, TransData *td, float in[3], float out[3], float pvec[3])
357 if (t->con.mode & CON_APPLY) {
359 mul_m3_v3(t->con.pmtx, out);
360 if (getConstraintSpaceDimension(t) == 2) {
361 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
362 planeProjection(t, in, out);
365 else if (getConstraintSpaceDimension(t) == 1) {
368 if (t->con.mode & CON_AXIS0) {
369 copy_v3_v3(c, t->con.mtx[0]);
371 else if (t->con.mode & CON_AXIS1) {
372 copy_v3_v3(c, t->con.mtx[1]);
374 else if (t->con.mode & CON_AXIS2) {
375 copy_v3_v3(c, t->con.mtx[2]);
377 axisProjection(t, c, in, out);
379 postConstraintChecks(t, out, pvec);
380 copy_v3_v3(out, pvec);
385 out[0] = out[1] = out[2] = 0.0f;
386 if (t->con.mode & CON_AXIS0) {
389 if (t->con.mode & CON_AXIS1) {
392 if (t->con.mode & CON_AXIS2) {
395 mul_m3_v3(td->axismtx, out);
401 * Generic callback for constant spatial constraints applied to resize motion
404 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
406 if (!td && t->con.mode & CON_APPLY) {
409 if (!(t->con.mode & CON_AXIS0)) {
412 if (!(t->con.mode & CON_AXIS1)) {
415 if (!(t->con.mode & CON_AXIS2)) {
419 mul_m3_m3m3(tmat, smat, t->con.imtx);
420 mul_m3_m3m3(smat, t->con.mtx, tmat);
425 * Callback for object based spatial constraints applied to resize motion
428 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
430 if (td && t->con.mode & CON_APPLY) {
434 invert_m3_m3(imat, td->axismtx);
436 if (!(t->con.mode & CON_AXIS0)) {
439 if (!(t->con.mode & CON_AXIS1)) {
442 if (!(t->con.mode & CON_AXIS2)) {
446 mul_m3_m3m3(tmat, smat, imat);
447 mul_m3_m3m3(smat, td->axismtx, tmat);
452 * Generic callback for constant spatial constraints applied to rotations
454 * The rotation axis is copied into VEC.
456 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
457 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
459 * The following only applies when CON_NOFLIP is not set.
460 * The vector is then modified to always point away from the screen (in global space)
461 * This insures that the rotation is always logically following the mouse.
462 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
465 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
467 if (!td && t->con.mode & CON_APPLY) {
468 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
472 case (CON_AXIS1 | CON_AXIS2):
473 copy_v3_v3(vec, t->con.mtx[0]);
476 case (CON_AXIS0 | CON_AXIS2):
477 copy_v3_v3(vec, t->con.mtx[1]);
480 case (CON_AXIS0 | CON_AXIS1):
481 copy_v3_v3(vec, t->con.mtx[2]);
484 /* don't flip axis if asked to or if num input */
485 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
486 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
494 * Callback for object based spatial constraints applied to rotations
496 * The rotation axis is copied into VEC.
498 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
499 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
501 * The following only applies when CON_NOFLIP is not set.
502 * The vector is then modified to always point away from the screen (in global space)
503 * This insures that the rotation is always logically following the mouse.
504 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
507 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
509 if (t->con.mode & CON_APPLY) {
510 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
512 /* on setup call, use first object */
519 case (CON_AXIS1 | CON_AXIS2):
520 copy_v3_v3(vec, td->axismtx[0]);
523 case (CON_AXIS0 | CON_AXIS2):
524 copy_v3_v3(vec, td->axismtx[1]);
527 case (CON_AXIS0 | CON_AXIS1):
528 copy_v3_v3(vec, td->axismtx[2]);
531 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
532 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
539 /*--------------------- INTERNAL SETUP CALLS ------------------*/
541 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
543 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
544 copy_m3_m3(t->con.mtx, space);
546 getConstraintMatrix(t);
550 t->con.drawExtra = NULL;
551 t->con.applyVec = applyAxisConstraintVec;
552 t->con.applySize = applyAxisConstraintSize;
553 t->con.applyRot = applyAxisConstraintRot;
557 void setLocalConstraint(TransInfo *t, int mode, const char text[])
559 if (t->flag & T_EDIT) {
561 copy_m3_m4(obmat, t->scene->obedit->obmat);
563 setConstraint(t, obmat, mode, text);
567 setConstraint(t, t->data->axismtx, mode, text);
570 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
571 copy_m3_m3(t->con.mtx, t->data->axismtx);
573 getConstraintMatrix(t);
577 t->con.drawExtra = drawObjectConstraint;
578 t->con.applyVec = applyObjectConstraintVec;
579 t->con.applySize = applyObjectConstraintSize;
580 t->con.applyRot = applyObjectConstraintRot;
587 * Set the constraint according to the user defined orientation
589 * ftext is a format string passed to BLI_snprintf. It will add the name of
590 * the orientation where %s is (logically).
592 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
596 switch (orientation) {
597 case V3D_MANIP_GLOBAL:
599 float mtx[3][3] = MAT3_UNITY;
600 BLI_snprintf(text, sizeof(text), ftext, "global");
601 setConstraint(t, mtx, mode, text);
604 case V3D_MANIP_LOCAL:
605 BLI_snprintf(text, sizeof(text), ftext, "local");
606 setLocalConstraint(t, mode, text);
608 case V3D_MANIP_NORMAL:
609 BLI_snprintf(text, sizeof(text), ftext, "normal");
610 setConstraint(t, t->spacemtx, mode, text);
613 BLI_snprintf(text, sizeof(text), ftext, "view");
614 setConstraint(t, t->spacemtx, mode, text);
616 case V3D_MANIP_GIMBAL:
617 BLI_snprintf(text, sizeof(text), ftext, "gimbal");
618 setConstraint(t, t->spacemtx, mode, text);
620 default: /* V3D_MANIP_CUSTOM */
621 BLI_snprintf(text, sizeof(text), ftext, t->spacename);
622 setConstraint(t, t->spacemtx, mode, text);
626 t->con.orientation = orientation;
628 t->con.mode |= CON_USER;
631 /*----------------- DRAWING CONSTRAINTS -------------------*/
633 void drawConstraint(TransInfo *t)
635 TransCon *tc = &(t->con);
637 if (!ELEM3(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
639 if (!(tc->mode & CON_APPLY))
641 if (t->flag & T_USES_MANIPULATOR)
643 if (t->flag & T_NO_CONSTRAINT)
646 /* nasty exception for Z constraint in camera view */
648 // if ((t->flag & T_OBJECT) && G.vd->camera==OBACT && G.vd->persp==V3D_CAMOB)
655 if (tc->mode & CON_SELECT) {
657 char col2[3] = {255, 255, 255};
658 int depth_test_enabled;
660 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
661 add_v3_v3(vec, tc->center);
663 drawLine(t, tc->center, tc->mtx[0], 'X', 0);
664 drawLine(t, tc->center, tc->mtx[1], 'Y', 0);
665 drawLine(t, tc->center, tc->mtx[2], 'Z', 0);
667 glColor3ubv((GLubyte *)col2);
669 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
670 if (depth_test_enabled)
671 glDisable(GL_DEPTH_TEST);
674 glBegin(GL_LINE_STRIP);
675 glVertex3fv(tc->center);
680 if (depth_test_enabled)
681 glEnable(GL_DEPTH_TEST);
684 if (tc->mode & CON_AXIS0) {
685 drawLine(t, tc->center, tc->mtx[0], 'X', DRAWLIGHT);
687 if (tc->mode & CON_AXIS1) {
688 drawLine(t, tc->center, tc->mtx[1], 'Y', DRAWLIGHT);
690 if (tc->mode & CON_AXIS2) {
691 drawLine(t, tc->center, tc->mtx[2], 'Z', DRAWLIGHT);
696 /* called from drawview.c, as an extra per-window draw option */
697 void drawPropCircle(const struct bContext *C, TransInfo *t)
699 if (t->flag & T_PROP_EDIT) {
700 RegionView3D *rv3d = CTX_wm_region_view3d(C);
701 float tmat[4][4], imat[4][4];
704 UI_ThemeColor(TH_GRID);
706 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
707 copy_m4_m4(tmat, rv3d->viewmat);
708 invert_m4_m4(imat, tmat);
717 copy_v3_v3(center, t->center);
719 if ((t->spacetype == SPACE_VIEW3D) && t->obedit) {
720 mul_m4_v3(t->obedit->obmat, center); /* because t->center is in local space */
722 else if (t->spacetype == SPACE_IMAGE) {
725 if (t->options & CTX_MASK) {
726 /* untested - mask aspect is TODO */
727 ED_space_image_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
730 ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
732 glScalef(1.0f / aspx, 1.0f / aspy, 1.0);
735 set_inverted_drawing(1);
736 drawcircball(GL_LINE_LOOP, center, t->prop_size, imat);
737 set_inverted_drawing(0);
743 static void drawObjectConstraint(TransInfo *t)
746 TransData *td = t->data;
748 /* Draw the first one lighter because that's the one who controls the others.
749 * Meaning the transformation is projected on that one and just copied on the others
751 * In a nutshell, the object with light axis is controlled by the user and the others follow.
752 * Without drawing the first light, users have little clue what they are doing.
754 if (t->con.mode & CON_AXIS0) {
755 drawLine(t, td->ob->obmat[3], td->axismtx[0], 'X', DRAWLIGHT);
757 if (t->con.mode & CON_AXIS1) {
758 drawLine(t, td->ob->obmat[3], td->axismtx[1], 'Y', DRAWLIGHT);
760 if (t->con.mode & CON_AXIS2) {
761 drawLine(t, td->ob->obmat[3], td->axismtx[2], 'Z', DRAWLIGHT);
766 for (i = 1; i < t->total; i++, td++) {
767 if (t->con.mode & CON_AXIS0) {
768 drawLine(t, td->ob->obmat[3], td->axismtx[0], 'X', 0);
770 if (t->con.mode & CON_AXIS1) {
771 drawLine(t, td->ob->obmat[3], td->axismtx[1], 'Y', 0);
773 if (t->con.mode & CON_AXIS2) {
774 drawLine(t, td->ob->obmat[3], td->axismtx[2], 'Z', 0);
779 /*--------------------- START / STOP CONSTRAINTS ---------------------- */
781 void startConstraint(TransInfo *t)
783 t->con.mode |= CON_APPLY;
785 t->num.idx_max = MIN2(getConstraintSpaceDimension(t) - 1, t->idx_max);
788 void stopConstraint(TransInfo *t)
790 t->con.mode &= ~(CON_APPLY | CON_SELECT);
792 t->num.idx_max = t->idx_max;
795 void getConstraintMatrix(TransInfo *t)
798 invert_m3_m3(t->con.imtx, t->con.mtx);
799 unit_m3(t->con.pmtx);
801 if (!(t->con.mode & CON_AXIS0)) {
804 t->con.pmtx[0][2] = 0.0f;
807 if (!(t->con.mode & CON_AXIS1)) {
810 t->con.pmtx[1][2] = 0.0f;
813 if (!(t->con.mode & CON_AXIS2)) {
816 t->con.pmtx[2][2] = 0.0f;
819 mul_m3_m3m3(mat, t->con.pmtx, t->con.imtx);
820 mul_m3_m3m3(t->con.pmtx, t->con.mtx, mat);
823 /*------------------------- MMB Select -------------------------------*/
825 void initSelectConstraint(TransInfo *t, float mtx[3][3])
827 copy_m3_m3(t->con.mtx, mtx);
828 t->con.mode |= CON_APPLY;
829 t->con.mode |= CON_SELECT;
832 t->con.drawExtra = NULL;
833 t->con.applyVec = applyAxisConstraintVec;
834 t->con.applySize = applyAxisConstraintSize;
835 t->con.applyRot = applyAxisConstraintRot;
838 void selectConstraint(TransInfo *t)
840 if (t->con.mode & CON_SELECT) {
846 void postSelectConstraint(TransInfo *t)
848 if (!(t->con.mode & CON_SELECT))
851 t->con.mode &= ~CON_AXIS0;
852 t->con.mode &= ~CON_AXIS1;
853 t->con.mode &= ~CON_AXIS2;
854 t->con.mode &= ~CON_SELECT;
862 static void setNearestAxis2d(TransInfo *t)
864 /* no correction needed... just use whichever one is lower */
865 if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1]) ) {
866 t->con.mode |= CON_AXIS1;
867 BLI_snprintf(t->con.text, sizeof(t->con.text), " along Y axis");
870 t->con.mode |= CON_AXIS0;
871 BLI_snprintf(t->con.text, sizeof(t->con.text), " along X axis");
875 static void setNearestAxis3d(TransInfo *t)
878 float mvec[3], axis[3], proj[3];
882 /* calculate mouse movement */
883 mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
884 mvec[1] = (float)(t->mval[1] - t->con.imval[1]);
887 /* we need to correct axis length for the current zoomlevel of view,
888 * this to prevent projected values to be clipped behind the camera
889 * and to overflow the short integers.
890 * The formula used is a bit stupid, just a simplification of the subtraction
891 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
892 * projecting them with window_to_3d_delta and then get the length of that vector.
894 zfac = t->persmat[0][3] * t->center[0] + t->persmat[1][3] * t->center[1] + t->persmat[2][3] * t->center[2] + t->persmat[3][3];
895 zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
897 for (i = 0; i < 3; i++) {
898 copy_v3_v3(axis, t->con.mtx[i]);
900 mul_v3_fl(axis, zfac);
901 /* now we can project to get window coordinate */
902 add_v3_v3(axis, t->con.center);
903 projectIntView(t, axis, icoord);
905 axis[0] = (float)(icoord[0] - t->center2d[0]);
906 axis[1] = (float)(icoord[1] - t->center2d[1]);
909 if (normalize_v3(axis) != 0.0f) {
910 project_v3_v3v3(proj, mvec, axis);
911 sub_v3_v3v3(axis, mvec, proj);
912 len[i] = normalize_v3(axis);
915 len[i] = 10000000000.0f;
919 if (len[0] <= len[1] && len[0] <= len[2]) {
920 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
921 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
922 BLI_snprintf(t->con.text, sizeof(t->con.text), " locking %s X axis", t->spacename);
925 t->con.mode |= CON_AXIS0;
926 BLI_snprintf(t->con.text, sizeof(t->con.text), " along %s X axis", t->spacename);
929 else if (len[1] <= len[0] && len[1] <= len[2]) {
930 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
931 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
932 BLI_snprintf(t->con.text, sizeof(t->con.text), " locking %s Y axis", t->spacename);
935 t->con.mode |= CON_AXIS1;
936 BLI_snprintf(t->con.text, sizeof(t->con.text), " along %s Y axis", t->spacename);
939 else if (len[2] <= len[1] && len[2] <= len[0]) {
940 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
941 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
942 BLI_snprintf(t->con.text, sizeof(t->con.text), " locking %s Z axis", t->spacename);
945 t->con.mode |= CON_AXIS2;
946 BLI_snprintf(t->con.text, sizeof(t->con.text), " along %s Z axis", t->spacename);
951 void setNearestAxis(TransInfo *t)
953 /* clear any prior constraint flags */
954 t->con.mode &= ~CON_AXIS0;
955 t->con.mode &= ~CON_AXIS1;
956 t->con.mode &= ~CON_AXIS2;
958 /* constraint setting - depends on spacetype */
959 if (t->spacetype == SPACE_VIEW3D) {
964 /* assume that this means a 2D-Editor */
968 getConstraintMatrix(t);
971 /*-------------- HELPER FUNCTIONS ----------------*/
973 char constraintModeToChar(TransInfo *t)
975 if ((t->con.mode & CON_APPLY) == 0) {
978 switch (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
980 case (CON_AXIS1 | CON_AXIS2):
983 case (CON_AXIS0 | CON_AXIS2):
986 case (CON_AXIS0 | CON_AXIS1):
994 int isLockConstraint(TransInfo *t)
996 int mode = t->con.mode;
998 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1))
1001 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2))
1004 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2))
1011 * Returns the dimension of the constraint space.
1013 * For that reason, the flags always needs to be set to properly evaluate here,
1014 * even if they aren't actually used in the callback function. (Which could happen
1015 * for weird constraints not yet designed. Along a path for example.)
1018 int getConstraintSpaceDimension(TransInfo *t)
1022 if (t->con.mode & CON_AXIS0)
1025 if (t->con.mode & CON_AXIS1)
1028 if (t->con.mode & CON_AXIS2)
1033 * Someone willing to do it cryptically could do the following instead:
1035 * return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);
1037 * Based on the assumptions that the axis flags are one after the other and start at 1