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
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_view3d_types.h"
50 #include "BIF_glutil.h"
53 #include "BLI_utildefines.h"
54 #include "BLI_string.h"
56 #include "BKE_context.h"
59 #include "ED_view3d.h"
61 #include "BLF_translation.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 constraintNumInput(t, vec);
154 removeAspectRatio(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, const float axis[3], const float in[3], float out[3])
205 float norm[3], vec[3], factor, angle;
206 float t_con_center[3];
208 if (is_zero_v3(in)) {
212 copy_v3_v3(t_con_center, t->con.center);
214 /* checks for center being too close to the view center */
215 viewAxisCorrectCenter(t, t_con_center);
217 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
218 if (angle > (float)M_PI / 2.0f) {
219 angle = (float)M_PI - angle;
221 angle = RAD2DEGF(angle);
223 /* For when view is parallel to constraint... will cause NaNs otherwise
224 * So we take vertical motion in 3D space and apply it to the
225 * constraint axis. Nice for camera grab + MMB */
227 project_v3_v3v3(vec, in, t->viewinv[1]);
228 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
229 /* since camera distance is quite relative, use quadratic relationship. holding shift can compensate */
230 if (factor < 0.0f) factor *= -factor;
231 else factor *= factor;
233 copy_v3_v3(out, axis);
235 mul_v3_fl(out, -factor); /* -factor makes move down going backwards */
238 float v[3], i1[3], i2[3];
240 float norm_center[3];
243 getViewVector(t, t_con_center, norm_center);
244 cross_v3_v3v3(plane, norm_center, axis);
246 project_v3_v3v3(vec, in, plane);
247 sub_v3_v3v3(vec, in, vec);
249 add_v3_v3v3(v, vec, t_con_center);
250 getViewVector(t, v, norm);
252 /* give arbitrary large value if projection is impossible */
253 factor = dot_v3v3(axis, norm);
254 if (1.0f - fabsf(factor) < 0.0002f) {
255 copy_v3_v3(out, axis);
257 mul_v3_fl(out, 1000000000.0f);
260 mul_v3_fl(out, -1000000000.0f);
264 add_v3_v3v3(v2, t_con_center, axis);
265 add_v3_v3v3(v4, v, norm);
267 isect_line_line_v3(t_con_center, v2, v, v4, i1, i2);
269 sub_v3_v3v3(v, i2, v);
271 sub_v3_v3v3(out, i1, t_con_center);
273 /* possible some values become nan when
274 * viewpoint and object are both zero */
275 if (!finite(out[0])) out[0] = 0.0f;
276 if (!finite(out[1])) out[1] = 0.0f;
277 if (!finite(out[2])) out[2] = 0.0f;
282 static void planeProjection(TransInfo *t, const float in[3], float out[3])
284 float vec[3], factor, norm[3];
286 add_v3_v3v3(vec, in, t->con.center);
287 getViewVector(t, vec, norm);
289 sub_v3_v3v3(vec, out, in);
291 factor = dot_v3v3(vec, norm);
292 if (fabsf(factor) <= 0.001f) {
293 return; /* prevent divide by zero */
295 factor = dot_v3v3(vec, vec) / factor;
297 copy_v3_v3(vec, norm);
298 mul_v3_fl(vec, factor);
300 add_v3_v3v3(out, in, vec);
304 * Generic callback for constant spatial constraints applied to linear motion
306 * The IN vector in projected into the constrained space and then further
307 * projected along the view vector.
308 * (in perspective mode, the view vector is relative to the position on screen)
312 static void applyAxisConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
315 if (!td && t->con.mode & CON_APPLY) {
316 mul_m3_v3(t->con.pmtx, out);
318 // With snap, a projection is alright, no need to correct for view alignment
319 if (!(t->tsnap.mode != SCE_SNAP_MODE_INCREMENT && activeSnap(t))) {
320 if (getConstraintSpaceDimension(t) == 2) {
321 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
322 planeProjection(t, in, out);
325 else if (getConstraintSpaceDimension(t) == 1) {
328 if (t->con.mode & CON_AXIS0) {
329 copy_v3_v3(c, t->con.mtx[0]);
331 else if (t->con.mode & CON_AXIS1) {
332 copy_v3_v3(c, t->con.mtx[1]);
334 else if (t->con.mode & CON_AXIS2) {
335 copy_v3_v3(c, t->con.mtx[2]);
337 axisProjection(t, c, in, out);
340 postConstraintChecks(t, out, pvec);
345 * Generic callback for object based spatial constraints applied to linear motion
347 * At first, the following is applied to the first data in the array
348 * The IN vector in projected into the constrained space and then further
349 * projected along the view vector.
350 * (in perspective mode, the view vector is relative to the position on screen)
352 * Further down, that vector is mapped to each data's space.
355 static void applyObjectConstraintVec(TransInfo *t, TransData *td, const float in[3], float out[3], float pvec[3])
358 if (t->con.mode & CON_APPLY) {
360 mul_m3_v3(t->con.pmtx, out);
361 if (getConstraintSpaceDimension(t) == 2) {
362 if (out[0] != 0.0f || out[1] != 0.0f || out[2] != 0.0f) {
363 planeProjection(t, in, out);
366 else if (getConstraintSpaceDimension(t) == 1) {
369 if (t->con.mode & CON_AXIS0) {
370 copy_v3_v3(c, t->con.mtx[0]);
372 else if (t->con.mode & CON_AXIS1) {
373 copy_v3_v3(c, t->con.mtx[1]);
375 else if (t->con.mode & CON_AXIS2) {
376 copy_v3_v3(c, t->con.mtx[2]);
378 axisProjection(t, c, in, out);
380 postConstraintChecks(t, out, pvec);
381 copy_v3_v3(out, pvec);
386 out[0] = out[1] = out[2] = 0.0f;
387 if (t->con.mode & CON_AXIS0) {
390 if (t->con.mode & CON_AXIS1) {
393 if (t->con.mode & CON_AXIS2) {
396 mul_m3_v3(td->axismtx, out);
402 * Generic callback for constant spatial constraints applied to resize motion
405 static void applyAxisConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
407 if (!td && t->con.mode & CON_APPLY) {
410 if (!(t->con.mode & CON_AXIS0)) {
413 if (!(t->con.mode & CON_AXIS1)) {
416 if (!(t->con.mode & CON_AXIS2)) {
420 mul_m3_m3m3(tmat, smat, t->con.imtx);
421 mul_m3_m3m3(smat, t->con.mtx, tmat);
426 * Callback for object based spatial constraints applied to resize motion
429 static void applyObjectConstraintSize(TransInfo *t, TransData *td, float smat[3][3])
431 if (td && t->con.mode & CON_APPLY) {
435 invert_m3_m3(imat, td->axismtx);
437 if (!(t->con.mode & CON_AXIS0)) {
440 if (!(t->con.mode & CON_AXIS1)) {
443 if (!(t->con.mode & CON_AXIS2)) {
447 mul_m3_m3m3(tmat, smat, imat);
448 mul_m3_m3m3(smat, td->axismtx, tmat);
453 * Generic callback for constant spatial constraints applied to rotations
455 * The rotation axis is copied into VEC.
457 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
458 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
460 * The following only applies when CON_NOFLIP is not set.
461 * The vector is then modified to always point away from the screen (in global space)
462 * This insures that the rotation is always logically following the mouse.
463 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
466 static void applyAxisConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
468 if (!td && t->con.mode & CON_APPLY) {
469 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
473 case (CON_AXIS1 | CON_AXIS2):
474 copy_v3_v3(vec, t->con.mtx[0]);
477 case (CON_AXIS0 | CON_AXIS2):
478 copy_v3_v3(vec, t->con.mtx[1]);
481 case (CON_AXIS0 | CON_AXIS1):
482 copy_v3_v3(vec, t->con.mtx[2]);
485 /* don't flip axis if asked to or if num input */
486 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
487 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
495 * Callback for object based spatial constraints applied to rotations
497 * The rotation axis is copied into VEC.
499 * In the case of single axis constraints, the rotation axis is directly the one constrained to.
500 * For planar constraints (2 axis), the rotation axis is the normal of the plane.
502 * The following only applies when CON_NOFLIP is not set.
503 * The vector is then modified to always point away from the screen (in global space)
504 * This insures that the rotation is always logically following the mouse.
505 * (ie: not doing counterclockwise rotations when the mouse moves clockwise).
508 static void applyObjectConstraintRot(TransInfo *t, TransData *td, float vec[3], float *angle)
510 if (t->con.mode & CON_APPLY) {
511 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
513 /* on setup call, use first object */
520 case (CON_AXIS1 | CON_AXIS2):
521 copy_v3_v3(vec, td->axismtx[0]);
524 case (CON_AXIS0 | CON_AXIS2):
525 copy_v3_v3(vec, td->axismtx[1]);
528 case (CON_AXIS0 | CON_AXIS1):
529 copy_v3_v3(vec, td->axismtx[2]);
532 if (angle && (mode & CON_NOFLIP) == 0 && hasNumInput(&t->num) == 0) {
533 if (dot_v3v3(vec, t->viewinv[2]) > 0.0f) {
540 /*--------------------- INTERNAL SETUP CALLS ------------------*/
542 void setConstraint(TransInfo *t, float space[3][3], int mode, const char text[])
544 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
545 copy_m3_m3(t->con.mtx, space);
547 getConstraintMatrix(t);
551 t->con.drawExtra = NULL;
552 t->con.applyVec = applyAxisConstraintVec;
553 t->con.applySize = applyAxisConstraintSize;
554 t->con.applyRot = applyAxisConstraintRot;
558 void setLocalConstraint(TransInfo *t, int mode, const char text[])
560 /* edit-mode now allows local transforms too */
562 if ((t->flag & T_EDIT) && (t->around != V3D_LOCAL)) {
564 copy_m3_m4(obmat, t->scene->obedit->obmat);
566 setConstraint(t, obmat, mode, text);
572 setConstraint(t, t->data->axismtx, mode, text);
575 BLI_strncpy(t->con.text + 1, text, sizeof(t->con.text) - 1);
576 copy_m3_m3(t->con.mtx, t->data->axismtx);
578 getConstraintMatrix(t);
582 t->con.drawExtra = drawObjectConstraint;
583 t->con.applyVec = applyObjectConstraintVec;
584 t->con.applySize = applyObjectConstraintSize;
585 t->con.applyRot = applyObjectConstraintRot;
592 * Set the constraint according to the user defined orientation
594 * ftext is a format string passed to BLI_snprintf. It will add the name of
595 * the orientation where %s is (logically).
597 void setUserConstraint(TransInfo *t, short orientation, int mode, const char ftext[])
601 switch (orientation) {
602 case V3D_MANIP_GLOBAL:
604 float mtx[3][3] = MAT3_UNITY;
605 BLI_snprintf(text, sizeof(text), ftext, IFACE_("global"));
606 setConstraint(t, mtx, mode, text);
609 case V3D_MANIP_LOCAL:
610 BLI_snprintf(text, sizeof(text), ftext, IFACE_("local"));
611 setLocalConstraint(t, mode, text);
613 case V3D_MANIP_NORMAL:
614 BLI_snprintf(text, sizeof(text), ftext, IFACE_("normal"));
615 setConstraint(t, t->spacemtx, mode, text);
618 BLI_snprintf(text, sizeof(text), ftext, IFACE_("view"));
619 setConstraint(t, t->spacemtx, mode, text);
621 case V3D_MANIP_GIMBAL:
622 BLI_snprintf(text, sizeof(text), ftext, IFACE_("gimbal"));
623 setConstraint(t, t->spacemtx, mode, text);
625 default: /* V3D_MANIP_CUSTOM */
626 BLI_snprintf(text, sizeof(text), ftext, t->spacename);
627 setConstraint(t, t->spacemtx, mode, text);
631 t->con.orientation = orientation;
633 t->con.mode |= CON_USER;
636 /*----------------- DRAWING CONSTRAINTS -------------------*/
638 void drawConstraint(TransInfo *t)
640 TransCon *tc = &(t->con);
642 if (!ELEM3(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE))
644 if (!(tc->mode & CON_APPLY))
646 if (t->flag & T_USES_MANIPULATOR)
648 if (t->flag & T_NO_CONSTRAINT)
651 /* nasty exception for Z constraint in camera view */
653 // if ((t->flag & T_OBJECT) && G.vd->camera==OBACT && G.vd->persp==V3D_CAMOB)
660 if (tc->mode & CON_SELECT) {
662 char col2[3] = {255, 255, 255};
663 int depth_test_enabled;
665 convertViewVec(t, vec, (t->mval[0] - t->con.imval[0]), (t->mval[1] - t->con.imval[1]));
666 add_v3_v3(vec, tc->center);
668 drawLine(t, tc->center, tc->mtx[0], 'X', 0);
669 drawLine(t, tc->center, tc->mtx[1], 'Y', 0);
670 drawLine(t, tc->center, tc->mtx[2], 'Z', 0);
672 glColor3ubv((GLubyte *)col2);
674 depth_test_enabled = glIsEnabled(GL_DEPTH_TEST);
675 if (depth_test_enabled)
676 glDisable(GL_DEPTH_TEST);
679 glBegin(GL_LINE_STRIP);
680 glVertex3fv(tc->center);
685 if (depth_test_enabled)
686 glEnable(GL_DEPTH_TEST);
689 if (tc->mode & CON_AXIS0) {
690 drawLine(t, tc->center, tc->mtx[0], 'X', DRAWLIGHT);
692 if (tc->mode & CON_AXIS1) {
693 drawLine(t, tc->center, tc->mtx[1], 'Y', DRAWLIGHT);
695 if (tc->mode & CON_AXIS2) {
696 drawLine(t, tc->center, tc->mtx[2], 'Z', DRAWLIGHT);
701 /* called from drawview.c, as an extra per-window draw option */
702 void drawPropCircle(const struct bContext *C, TransInfo *t)
704 if (t->flag & T_PROP_EDIT) {
705 RegionView3D *rv3d = CTX_wm_region_view3d(C);
706 float tmat[4][4], imat[4][4];
709 UI_ThemeColor(TH_GRID);
711 if (t->spacetype == SPACE_VIEW3D && rv3d != NULL) {
712 copy_m4_m4(tmat, rv3d->viewmat);
713 invert_m4_m4(imat, tmat);
722 copy_v3_v3(center, t->center);
724 if ((t->spacetype == SPACE_VIEW3D) && t->obedit) {
725 mul_m4_v3(t->obedit->obmat, center); /* because t->center is in local space */
727 else if (t->spacetype == SPACE_IMAGE) {
730 if (t->options & CTX_MASK) {
731 /* untested - mask aspect is TODO */
732 ED_space_image_get_aspect(t->sa->spacedata.first, &aspx, &aspy);
735 ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
737 glScalef(1.0f / aspx, 1.0f / aspy, 1.0);
740 set_inverted_drawing(1);
741 drawcircball(GL_LINE_LOOP, center, t->prop_size, imat);
742 set_inverted_drawing(0);
748 static void drawObjectConstraint(TransInfo *t)
750 /* Draw the first one lighter because that's the one who controls the others.
751 * Meaning the transformation is projected on that one and just copied on the others
753 * In a nutshell, the object with light axis is controlled by the user and the others follow.
754 * Without drawing the first light, users have little clue what they are doing.
756 short options = DRAWLIGHT;
757 TransData *td = t->data;
760 for (i = 0; i < t->total; i++, td++) {
763 if (t->flag & T_OBJECT) {
764 copy_v3_v3(co, td->ob->obmat[3]);
766 else if (t->flag & T_EDIT) {
767 mul_v3_m4v3(co, t->obedit->obmat, td->center);
769 else if (t->flag & T_POSE) {
770 mul_v3_m4v3(co, t->poseobj->obmat, td->center);
773 copy_v3_v3(co, td->center);
776 if (t->con.mode & CON_AXIS0) {
777 drawLine(t, td->center, td->axismtx[0], 'X', options);
779 if (t->con.mode & CON_AXIS1) {
780 drawLine(t, td->center, td->axismtx[1], 'Y', options);
782 if (t->con.mode & CON_AXIS2) {
783 drawLine(t, td->center, td->axismtx[2], 'Z', options);
785 options &= ~DRAWLIGHT;
789 /*--------------------- START / STOP CONSTRAINTS ---------------------- */
791 void startConstraint(TransInfo *t)
793 t->con.mode |= CON_APPLY;
795 t->num.idx_max = min_ii(getConstraintSpaceDimension(t) - 1, t->idx_max);
798 void stopConstraint(TransInfo *t)
800 t->con.mode &= ~(CON_APPLY | CON_SELECT);
802 t->num.idx_max = t->idx_max;
805 void getConstraintMatrix(TransInfo *t)
808 invert_m3_m3(t->con.imtx, t->con.mtx);
809 unit_m3(t->con.pmtx);
811 if (!(t->con.mode & CON_AXIS0)) {
814 t->con.pmtx[0][2] = 0.0f;
817 if (!(t->con.mode & CON_AXIS1)) {
820 t->con.pmtx[1][2] = 0.0f;
823 if (!(t->con.mode & CON_AXIS2)) {
826 t->con.pmtx[2][2] = 0.0f;
829 mul_m3_m3m3(mat, t->con.pmtx, t->con.imtx);
830 mul_m3_m3m3(t->con.pmtx, t->con.mtx, mat);
833 /*------------------------- MMB Select -------------------------------*/
835 void initSelectConstraint(TransInfo *t, float mtx[3][3])
837 copy_m3_m3(t->con.mtx, mtx);
838 t->con.mode |= CON_APPLY;
839 t->con.mode |= CON_SELECT;
842 t->con.drawExtra = NULL;
843 t->con.applyVec = applyAxisConstraintVec;
844 t->con.applySize = applyAxisConstraintSize;
845 t->con.applyRot = applyAxisConstraintRot;
848 void selectConstraint(TransInfo *t)
850 if (t->con.mode & CON_SELECT) {
856 void postSelectConstraint(TransInfo *t)
858 if (!(t->con.mode & CON_SELECT))
861 t->con.mode &= ~CON_AXIS0;
862 t->con.mode &= ~CON_AXIS1;
863 t->con.mode &= ~CON_AXIS2;
864 t->con.mode &= ~CON_SELECT;
872 static void setNearestAxis2d(TransInfo *t)
874 /* no correction needed... just use whichever one is lower */
875 if (abs(t->mval[0] - t->con.imval[0]) < abs(t->mval[1] - t->con.imval[1]) ) {
876 t->con.mode |= CON_AXIS1;
877 BLI_strncpy(t->con.text, IFACE_(" along Y axis"), sizeof(t->con.text));
880 t->con.mode |= CON_AXIS0;
881 BLI_strncpy(t->con.text, IFACE_(" along X axis"), sizeof(t->con.text));
885 static void setNearestAxis3d(TransInfo *t)
888 float mvec[3], axis[3], proj[3];
892 /* calculate mouse movement */
893 mvec[0] = (float)(t->mval[0] - t->con.imval[0]);
894 mvec[1] = (float)(t->mval[1] - t->con.imval[1]);
897 /* we need to correct axis length for the current zoomlevel of view,
898 * this to prevent projected values to be clipped behind the camera
899 * and to overflow the short integers.
900 * The formula used is a bit stupid, just a simplification of the subtraction
901 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
902 * projecting them with ED_view3d_win_to_delta and then get the length of that vector.
904 zfac = mul_project_m4_v3_zfac(t->persmat, t->center);
905 zfac = len_v3(t->persinv[0]) * 2.0f / t->ar->winx * zfac * 30.0f;
907 for (i = 0; i < 3; i++) {
908 copy_v3_v3(axis, t->con.mtx[i]);
910 mul_v3_fl(axis, zfac);
911 /* now we can project to get window coordinate */
912 add_v3_v3(axis, t->con.center);
913 projectIntView(t, axis, icoord);
915 axis[0] = (float)(icoord[0] - t->center2d[0]);
916 axis[1] = (float)(icoord[1] - t->center2d[1]);
919 if (normalize_v3(axis) != 0.0f) {
920 project_v3_v3v3(proj, mvec, axis);
921 sub_v3_v3v3(axis, mvec, proj);
922 len[i] = normalize_v3(axis);
925 len[i] = 10000000000.0f;
929 if (len[0] <= len[1] && len[0] <= len[2]) {
930 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
931 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
932 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s X axis"), t->spacename);
935 t->con.mode |= CON_AXIS0;
936 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s X axis"), t->spacename);
939 else if (len[1] <= len[0] && len[1] <= len[2]) {
940 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
941 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
942 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Y axis"), t->spacename);
945 t->con.mode |= CON_AXIS1;
946 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Y axis"), t->spacename);
949 else if (len[2] <= len[1] && len[2] <= len[0]) {
950 if (t->modifiers & MOD_CONSTRAINT_PLANE) {
951 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
952 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" locking %s Z axis"), t->spacename);
955 t->con.mode |= CON_AXIS2;
956 BLI_snprintf(t->con.text, sizeof(t->con.text), IFACE_(" along %s Z axis"), t->spacename);
961 void setNearestAxis(TransInfo *t)
963 /* clear any prior constraint flags */
964 t->con.mode &= ~CON_AXIS0;
965 t->con.mode &= ~CON_AXIS1;
966 t->con.mode &= ~CON_AXIS2;
968 /* constraint setting - depends on spacetype */
969 if (t->spacetype == SPACE_VIEW3D) {
974 /* assume that this means a 2D-Editor */
978 getConstraintMatrix(t);
981 /*-------------- HELPER FUNCTIONS ----------------*/
983 char constraintModeToChar(TransInfo *t)
985 if ((t->con.mode & CON_APPLY) == 0) {
988 switch (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
990 case (CON_AXIS1 | CON_AXIS2):
993 case (CON_AXIS0 | CON_AXIS2):
996 case (CON_AXIS0 | CON_AXIS1):
1004 bool isLockConstraint(TransInfo *t)
1006 int mode = t->con.mode;
1008 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1))
1011 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2))
1014 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2))
1021 * Returns the dimension of the constraint space.
1023 * For that reason, the flags always needs to be set to properly evaluate here,
1024 * even if they aren't actually used in the callback function. (Which could happen
1025 * for weird constraints not yet designed. Along a path for example.)
1028 int getConstraintSpaceDimension(TransInfo *t)
1032 if (t->con.mode & CON_AXIS0)
1035 if (t->con.mode & CON_AXIS1)
1038 if (t->con.mode & CON_AXIS2)
1043 * Someone willing to do it cryptically could do the following instead:
1045 * return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);
1047 * Based on the assumptions that the axis flags are one after the other and start at 1