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"
58 #include "ED_view3d.h"
61 #include "BLI_utildefines.h"
62 #include "BLI_string.h"
64 //#include "blendef.h"
66 //#include "mydevice.h"
68 #include "UI_resources.h"
71 #include "transform.h"
73 static void drawObjectConstraint(TransInfo *t);
75 /* ************************** CONSTRAINTS ************************* */
76 static void constraintAutoValues(TransInfo *t, float vec[3])
78 int mode = t->con.mode;
79 if (mode & CON_APPLY) {
80 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
82 if ((mode & CON_AXIS0) == 0) {
85 if ((mode & CON_AXIS1) == 0) {
88 if ((mode & CON_AXIS2) == 0) {
94 void constraintNumInput(TransInfo *t, float vec[3])
96 int mode = t->con.mode;
97 if (mode & CON_APPLY) {
98 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
100 if (getConstraintSpaceDimension(t) == 2) {
101 int axis = mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
102 if (axis == (CON_AXIS0 | CON_AXIS1)) {
103 /* vec[0] = vec[0]; */ /* same */
104 /* vec[1] = vec[1]; */ /* same */
107 else if (axis == (CON_AXIS1 | CON_AXIS2)) {
112 else if (axis == (CON_AXIS0 | CON_AXIS2)) {
113 /* vec[0] = vec[0]; */ /* same */
118 else if (getConstraintSpaceDimension(t) == 1) {
119 if (mode & CON_AXIS0) {
120 /* vec[0] = vec[0]; */ /* same */
124 else if (mode & CON_AXIS1) {
129 else if (mode & CON_AXIS2) {
138 static void postConstraintChecks(TransInfo *t, float vec[3], float pvec[3])
142 mul_m3_v3(t->con.imtx, vec);
146 if (t->num.flag & T_NULL_ONE) {
147 if (!(t->con.mode & CON_AXIS0))
150 if (!(t->con.mode & CON_AXIS1))
153 if (!(t->con.mode & CON_AXIS2))
157 if (hasNumInput(&t->num)) {
158 applyNumInput(&t->num, vec);
159 removeAspectRatio(t, vec);
160 constraintNumInput(t, vec);
163 /* autovalues is operator param, use that directly but not if snapping is forced */
164 if (t->flag & T_AUTOVALUES && (t->tsnap.status & SNAP_FORCED) == 0) {
165 mul_v3_m3v3(vec, t->con.imtx, t->auto_values);
166 constraintAutoValues(t, vec);
167 /* inverse transformation at the end */
170 if (t->con.mode & CON_AXIS0) {
173 if (t->con.mode & CON_AXIS1) {
176 if (t->con.mode & CON_AXIS2) {
180 mul_m3_v3(t->con.mtx, vec);
183 static void viewAxisCorrectCenter(TransInfo *t, float t_con_center[3])
185 if (t->spacetype == SPACE_VIEW3D) {
186 // View3D *v3d = t->sa->spacedata.first;
187 const float min_dist = 1.0f; /* v3d->near; */
191 sub_v3_v3v3(dir, t_con_center, t->viewinv[3]);
192 if (dot_v3v3(dir, t->viewinv[2]) < 0.0f) {
195 project_v3_v3v3(dir, dir, t->viewinv[2]);
201 normalize_v3_v3(diff, t->viewinv[2]);
202 mul_v3_fl(diff, min_dist - l);
204 sub_v3_v3(t_con_center, diff);
209 static void axisProjection(TransInfo *t, float axis[3], float in[3], float out[3])
211 float norm[3], vec[3], factor, angle;
212 float t_con_center[3];
214 if (in[0] == 0.0f && in[1] == 0.0f && in[2] == 0.0f)
217 copy_v3_v3(t_con_center, t->con.center);
219 /* checks for center being too close to the view center */
220 viewAxisCorrectCenter(t, t_con_center);
222 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
223 if (angle > (float)M_PI / 2.0f) {
224 angle = (float)M_PI - angle;
226 angle = RAD2DEGF(angle);
228 /* For when view is parallel to constraint... will cause NaNs otherwise
229 * So we take vertical motion in 3D space and apply it to the
230 * constraint axis. Nice for camera grab + MMB */
232 project_v3_v3v3(vec, in, t->viewinv[1]);
233 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
234 /* since camera distance is quite relative, use quadratic relationship. holding shift can compensate */
235 if (factor < 0.0f) factor *= -factor;
236 else factor *= factor;
238 copy_v3_v3(out, axis);
240 mul_v3_fl(out, -factor); /* -factor makes move down going backwards */
243 float v[3], i1[3], i2[3];
245 float norm_center[3];
248 getViewVector(t, t_con_center, norm_center);
249 cross_v3_v3v3(plane, norm_center, axis);
251 project_v3_v3v3(vec, in, plane);
252 sub_v3_v3v3(vec, in, vec);
254 add_v3_v3v3(v, vec, t_con_center);
255 getViewVector(t, v, norm);
257 /* give arbitrary large value if projection is impossible */
258 factor = dot_v3v3(axis, norm);
259 if (1.0f - fabsf(factor) < 0.0002f) {
260 copy_v3_v3(out, axis);
262 mul_v3_fl(out, 1000000000.0f);
265 mul_v3_fl(out, -1000000000.0f);
269 add_v3_v3v3(v2, t_con_center, axis);
270 add_v3_v3v3(v4, v, norm);
272 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
274 sub_v3_v3v3(v, i2, v);
276 sub_v3_v3v3(out, i1, t_con_center);
278 /* possible some values become nan when
279 * viewpoint and object are both zero */
280 if (!finite(out[0])) out[0] = 0.0f;
281 if (!finite(out[1])) out[1] = 0.0f;
282 if (!finite(out[2])) out[2] = 0.0f;
287 static void planeProjection(TransInfo *t, float in[3], float out[3])
289 float vec[3], factor, norm[3];
291 add_v3_v3v3(vec, in, t->con.center);
292 getViewVector(t, vec, norm);
294 sub_v3_v3v3(vec, out, in);
296 factor = dot_v3v3(vec, norm);
297 if (fabs(factor) <= 0.001) {
298 return; /* prevent divide by zero */
300 factor = dot_v3v3(vec, vec) / factor;
302 copy_v3_v3(vec, norm);
303 mul_v3_fl(vec, factor);
305 add_v3_v3v3(out, in, vec);
309 * Generic callback for constant spatial constraints applied to linear motion
311 * The IN vector in projected into the constrained space and then further
312 * projected along the view vector.
313 * (in perspective mode, the view vector is relative to the position on screen)
317 static void applyAxisConstraintVec(TransInfo *t, TransData *td, float in[3], float out[3], float pvec[3])
320 if (!td && t->con.mode & CON_APPLY) {
321 mul_m3_v3(t->con.pmtx, out);
323 // With snap, a projection is alright, no need to correct for view alignment
324 if (!(t->tsnap.mode != SCE_SNAP_MODE_INCREMENT && activeSnap(t))) {
325 if (getConstraintSpaceDimension(t) == 2) {
326 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
327 planeProjection(t, in, out);
330 else if (getConstraintSpaceDimension(t) == 1) {
333 if (t->con.mode & CON_AXIS0) {
334 copy_v3_v3(c, t->con.mtx[0]);
336 else if (t->con.mode & CON_AXIS1) {
337 copy_v3_v3(c, t->con.mtx[1]);
339 else if (t->con.mode & CON_AXIS2) {
340 copy_v3_v3(c, t->con.mtx[2]);
342 axisProjection(t, c, in, out);
345 postConstraintChecks(t, out, pvec);
350 * Generic callback for object based spatial constraints applied to linear motion
352 * At first, the following is applied to the first data in the array
353 * The IN vector in projected into the constrained space and then further
354 * projected along the view vector.
355 * (in perspective mode, the view vector is relative to the position on screen)
357 * Further down, that vector is mapped to each data's space.
360 static void applyObjectConstraintVec(TransInfo *t, TransData *td, float in[3], float out[3], float pvec[3])
363 if (t->con.mode & CON_APPLY) {
365 mul_m3_v3(t->con.pmtx, out);
366 if (getConstraintSpaceDimension(t) == 2) {
367 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
368 planeProjection(t, in, out);
371 else if (getConstraintSpaceDimension(t) == 1) {
374 if (t->con.mode & CON_AXIS0) {
375 copy_v3_v3(c, t->con.mtx[0]);
377 else if (t->con.mode & CON_AXIS1) {
378 copy_v3_v3(c, t->con.mtx[1]);
380 else if (t->con.mode & CON_AXIS2) {
381 copy_v3_v3(c, t->con.mtx[2]);
383 axisProjection(t, c, in, out);
385 postConstraintChecks(t, out, pvec);
386 copy_v3_v3(out, pvec);
391 out[0] = out[1] = out[2] = 0.0f;
392 if (t->con.mode & CON_AXIS0) {
395 if (t->con.mode & CON_AXIS1) {
398 if (t->con.mode & CON_AXIS2) {
401 mul_m3_v3(td->axismtx, out);
407 * Generic callback for constant spatial constraints applied to resize motion
410 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
412 if (!td && t->con.mode & CON_APPLY) {
415 if (!(t->con.mode & CON_AXIS0)) {
418 if (!(t->con.mode & CON_AXIS1)) {
421 if (!(t->con.mode & CON_AXIS2)) {
425 mul_m3_m3m3(tmat, smat, t->con.imtx);
426 mul_m3_m3m3(smat, t->con.mtx, tmat);
431 * Callback for object based spatial constraints applied to resize motion
434 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
436 if (td && t->con.mode & CON_APPLY) {
440 invert_m3_m3(imat, td->axismtx);
442 if (!(t->con.mode & CON_AXIS0)) {
445 if (!(t->con.mode & CON_AXIS1)) {
448 if (!(t->con.mode & CON_AXIS2)) {
452 mul_m3_m3m3(tmat, smat, imat);
453 mul_m3_m3m3(smat, td->axismtx, tmat);
458 * Generic callback for constant spatial constraints applied to rotations
460 * The rotation axis is copied into VEC.
462 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
463 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
465 * The following only applies when CON_NOFLIP is not set.
466 * The vector is then modified to always point away from the screen (in global space)
467 * This insures that the rotation is always logically following the mouse.
468 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
471 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
473 if (!td && t->con.mode & CON_APPLY) {
474 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
478 case (CON_AXIS1 | CON_AXIS2):
479 copy_v3_v3(vec, t->con.mtx[0]);
482 case (CON_AXIS0 | CON_AXIS2):
483 copy_v3_v3(vec, t->con.mtx[1]);
486 case (CON_AXIS0 | CON_AXIS1):
487 copy_v3_v3(vec, t->con.mtx[2]);
490 /* don't flip axis if asked to or if num input */
491 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
492 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
500 * Callback for object based spatial constraints applied to rotations
502 * The rotation axis is copied into VEC.
504 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
505 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
507 * The following only applies when CON_NOFLIP is not set.
508 * The vector is then modified to always point away from the screen (in global space)
509 * This insures that the rotation is always logically following the mouse.
510 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
513 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
515 if (t->con.mode & CON_APPLY) {
516 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
518 /* on setup call, use first object */
525 case (CON_AXIS1 | CON_AXIS2):
526 copy_v3_v3(vec, td->axismtx[0]);
529 case (CON_AXIS0 | CON_AXIS2):
530 copy_v3_v3(vec, td->axismtx[1]);
533 case (CON_AXIS0 | CON_AXIS1):
534 copy_v3_v3(vec, td->axismtx[2]);
537 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
538 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
545 /*--------------------- INTERNAL SETUP CALLS ------------------*/
547 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
549 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
550 copy_m3_m3(t->con.mtx, space);
552 getConstraintMatrix(t);
556 t->con.drawExtra = NULL;
557 t->con.applyVec = applyAxisConstraintVec;
558 t->con.applySize = applyAxisConstraintSize;
559 t->con.applyRot = applyAxisConstraintRot;
563 void setLocalConstraint(TransInfo *t, int mode, const char text[])
565 if (t->flag & T_EDIT) {
567 copy_m3_m4(obmat, t->scene->obedit->obmat);
569 setConstraint(t, obmat, mode, text);
573 setConstraint(t, t->data->axismtx, mode, text);
576 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
577 copy_m3_m3(t->con.mtx, t->data->axismtx);
579 getConstraintMatrix(t);
583 t->con.drawExtra = drawObjectConstraint;
584 t->con.applyVec = applyObjectConstraintVec;
585 t->con.applySize = applyObjectConstraintSize;
586 t->con.applyRot = applyObjectConstraintRot;
593 * Set the constraint according to the user defined orientation
595 * ftext is a format string passed to BLI_snprintf. It will add the name of
596 * the orientation where %s is (logically).
598 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
602 switch (orientation) {
603 case V3D_MANIP_GLOBAL:
605 float mtx[3][3] = MAT3_UNITY;
606 BLI_snprintf(text, sizeof(text), ftext, "global");
607 setConstraint(t, mtx, mode, text);
610 case V3D_MANIP_LOCAL:
611 BLI_snprintf(text, sizeof(text), ftext, "local");
612 setLocalConstraint(t, mode, text);
614 case V3D_MANIP_NORMAL:
615 BLI_snprintf(text, sizeof(text), ftext, "normal");
616 setConstraint(t, t->spacemtx, mode, text);
619 BLI_snprintf(text, sizeof(text), ftext, "view");
620 setConstraint(t, t->spacemtx, mode, text);
622 case V3D_MANIP_GIMBAL:
623 BLI_snprintf(text, sizeof(text), ftext, "gimbal");
624 setConstraint(t, t->spacemtx, mode, text);
626 default: /* V3D_MANIP_CUSTOM */
627 BLI_snprintf(text, sizeof(text), ftext, t->spacename);
628 setConstraint(t, t->spacemtx, mode, text);
632 t->con.orientation = orientation;
634 t->con.mode |= CON_USER;
637 /*----------------- DRAWING CONSTRAINTS -------------------*/
639 void drawConstraint(TransInfo *t)
641 TransCon *tc = &(t->con);
643 if (!ELEM3(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
645 if (!(tc->mode & CON_APPLY))
647 if (t->flag & T_USES_MANIPULATOR)
649 if (t->flag & T_NO_CONSTRAINT)
652 /* nasty exception for Z constraint in camera view */
654 // if ((t->flag & T_OBJECT) && G.vd->camera==OBACT && G.vd->persp==V3D_CAMOB)
661 if (tc->mode & CON_SELECT) {
663 char col2[3] = {255, 255, 255};
664 int depth_test_enabled;
666 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
667 add_v3_v3(vec, tc->center);
669 drawLine(t, tc->center, tc->mtx[0], 'X', 0);
670 drawLine(t, tc->center, tc->mtx[1], 'Y', 0);
671 drawLine(t, tc->center, tc->mtx[2], 'Z', 0);
673 glColor3ubv((GLubyte *)col2);
675 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
676 if (depth_test_enabled)
677 glDisable(GL_DEPTH_TEST);
680 glBegin(GL_LINE_STRIP);
681 glVertex3fv(tc->center);
686 if (depth_test_enabled)
687 glEnable(GL_DEPTH_TEST);
690 if (tc->mode & CON_AXIS0) {
691 drawLine(t, tc->center, tc->mtx[0], 'X', DRAWLIGHT);
693 if (tc->mode & CON_AXIS1) {
694 drawLine(t, tc->center, tc->mtx[1], 'Y', DRAWLIGHT);
696 if (tc->mode & CON_AXIS2) {
697 drawLine(t, tc->center, tc->mtx[2], 'Z', DRAWLIGHT);
702 /* called from drawview.c, as an extra per-window draw option */
703 void drawPropCircle(const struct bContext *C, TransInfo *t)
705 if (t->flag & T_PROP_EDIT) {
706 RegionView3D *rv3d = CTX_wm_region_view3d(C);
707 float tmat[4][4], imat[4][4];
710 UI_ThemeColor(TH_GRID);
712 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
713 copy_m4_m4(tmat, rv3d->viewmat);
714 invert_m4_m4(imat, tmat);
723 copy_v3_v3(center, t->center);
725 if ((t->spacetype == SPACE_VIEW3D) && t->obedit) {
726 mul_m4_v3(t->obedit->obmat, center); /* because t->center is in local space */
728 else if (t->spacetype == SPACE_IMAGE) {
731 ED_space_image_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