Blender V5.3
transform_constraints.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstdlib>
12#include <cstring>
13
14#include "DNA_scene_types.h"
15#include "DNA_screen_types.h"
16#include "DNA_space_types.h"
17#include "DNA_userdef_types.h"
18#include "DNA_view3d_types.h"
19
20#include "GPU_immediate.hh"
21#include "GPU_matrix.hh"
22#include "GPU_state.hh"
23
24#include "BLI_math_geom_c.hh"
25#include "BLI_math_matrix_c.hh"
27#include "BLI_rect.hh"
28#include "BLI_string_utf8.hh"
29#include "BLI_utildefines.hh"
30
31#include "BLT_translation.hh"
32
33#include "UI_resources.hh"
34
35#include "transform.hh"
36#include "transform_gizmo.hh"
38#include "transform_snap.hh"
39
40/* Own include. */
42
43namespace blender::ed::transform {
44
45static void drawObjectConstraint(TransInfo *t);
46
47/* -------------------------------------------------------------------- */
50
51static void projection_matrix_calc(const TransInfo *t, float r_pmtx[3][3])
52{
53 unit_m3(r_pmtx);
54
55 if (!(t->con.mode & CON_AXIS0)) {
56 zero_v3(r_pmtx[0]);
57 }
58
59 if (!(t->con.mode & CON_AXIS1)) {
60 zero_v3(r_pmtx[1]);
61 }
62
63 if (!(t->con.mode & CON_AXIS2)) {
64 zero_v3(r_pmtx[2]);
65 }
66
67 float mat[3][3];
68 mul_m3_m3m3(mat, r_pmtx, t->spacemtx_inv);
69 mul_m3_m3m3(r_pmtx, t->spacemtx, mat);
70}
71
73
74/* -------------------------------------------------------------------- */
77
78#define CONSTRAIN_EPSILON 0.0001f
79
80static void constraint_plane_normal_calc(const TransInfo *t, float r_plane_no[3])
81{
82 const float *constraint_vector[2];
83 int n = 0;
84 for (int i = 0; i < 3; i++) {
85 if (t->con.mode & (CON_AXIS0 << i)) {
86 constraint_vector[n++] = t->spacemtx[i];
87 if (n == 2) {
88 break;
89 }
90 }
91 }
92 BLI_assert(n == 2);
93
94 cross_v3_v3v3(r_plane_no, constraint_vector[0], constraint_vector[1]);
95 normalize_v3(r_plane_no);
96}
97
98void constraintNumInput(TransInfo *t, float vec[3])
99{
100 int mode = t->con.mode;
101 if (mode & CON_APPLY) {
102 float nval = (t->flag & T_NULL_ONE) ? 1.0f : 0.0f;
103
104 const int dims = getConstraintSpaceDimension(t);
105 if (dims == 2) {
106 int axis = mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
107 if (axis == (CON_AXIS0 | CON_AXIS1)) {
108 // vec[0] = vec[0]; /* Same. */
109 // vec[1] = vec[1]; /* Same. */
110 vec[2] = nval;
111 }
112 else if (axis == (CON_AXIS1 | CON_AXIS2)) {
113 vec[2] = vec[1];
114 vec[1] = vec[0];
115 vec[0] = nval;
116 }
117 else if (axis == (CON_AXIS0 | CON_AXIS2)) {
118 // vec[0] = vec[0]; /* Same. */
119 vec[2] = vec[1];
120 vec[1] = nval;
121 }
122 }
123 else if (dims == 1) {
124 if (mode & CON_AXIS0) {
125 // vec[0] = vec[0]; /* Same. */
126 vec[1] = nval;
127 vec[2] = nval;
128 }
129 else if (mode & CON_AXIS1) {
130 vec[1] = vec[0];
131 vec[0] = nval;
132 vec[2] = nval;
133 }
134 else if (mode & CON_AXIS2) {
135 vec[2] = vec[0];
136 vec[0] = nval;
137 vec[1] = nval;
138 }
139 }
140 }
141}
142
143static void viewAxisCorrectCenter(const TransInfo *t, float t_con_center[3])
144{
145 if (t->spacetype == SPACE_VIEW3D) {
146 // View3D *v3d = t->area->spacedata.first;
147 const float min_dist = 1.0f; /* `v3d->clip_start`. */
148 float dir[3];
149 float l;
150
151 sub_v3_v3v3(dir, t_con_center, t->viewinv[3]);
152 if (dot_v3v3(dir, t->viewinv[2]) < 0.0f) {
153 negate_v3(dir);
154 }
155 project_v3_v3v3(dir, dir, t->viewinv[2]);
156
157 l = len_v3(dir);
158
159 if (l < min_dist) {
160 float diff[3];
161 normalize_v3_v3_length(diff, t->viewinv[2], min_dist - l);
162 sub_v3_v3(t_con_center, diff);
163 }
164 }
165}
166
170static void axisProjection(const TransInfo *t,
171 const float axis[3],
172 const float in[3],
173 float out[3])
174{
175 float vec[3], factor, angle;
176 float t_con_center[3];
177
178 if (is_zero_v3(in)) {
179 return;
180 }
181
182 copy_v3_v3(t_con_center, t->center_global);
183
184 /* Checks for center being too close to the view center. */
185 viewAxisCorrectCenter(t, t_con_center);
186
187 angle = fabsf(angle_v3v3(axis, t->viewinv[2]));
188 if (angle > float(M_PI_2)) {
189 angle = float(M_PI) - angle;
190 }
191
192 /* For when view is parallel to constraint... will cause NaNs otherwise
193 * So we take vertical motion in 3D space and apply it to the
194 * constraint axis. Nice for camera grab + MMB. */
195 if (angle < DEG2RADF(5.0f)) {
196 project_v3_v3v3(vec, in, t->viewinv[1]);
197 factor = dot_v3v3(t->viewinv[1], vec) * 2.0f;
198 /* Since camera distance is quite relative, use quadratic relationship.
199 * holding shift can compensate. */
200 if (factor < 0.0f) {
201 factor *= -factor;
202 }
203 else {
204 factor *= factor;
205 }
206
207 /* -factor makes move down going backwards. */
208 normalize_v3_v3_length(out, axis, -factor);
209 }
210 else {
211 float v[3];
212 float norm[3], norm_center[3];
213 float plane[3];
214
215 view_vector_calc(t, t_con_center, norm_center);
216 cross_v3_v3v3(plane, norm_center, axis);
217
218 project_v3_v3v3(vec, in, plane);
219 sub_v3_v3v3(vec, in, vec);
220
221 add_v3_v3v3(v, vec, t_con_center);
223
224 /* Give arbitrary large value if projection is impossible. */
225 factor = dot_v3v3(axis, norm);
226 if (1.0f - fabsf(factor) < 0.0002f) {
227 copy_v3_v3(out, axis);
228 if (factor > 0) {
229 mul_v3_fl(out, 1000000000.0f);
230 }
231 else {
232 mul_v3_fl(out, -1000000000.0f);
233 }
234 }
235 else {
236 /* Use ray-ray intersection instead of line-line because this gave
237 * precision issues adding small values to large numbers. */
238 float mul;
239 if (isect_ray_ray_v3(t_con_center, axis, v, norm, &mul, nullptr)) {
240 mul_v3_v3fl(out, axis, mul);
241 }
242 else {
243 /* In practice this should never fail. */
244 BLI_assert(0);
245 }
246
247 /* Possible some values become nan when viewpoint and object are both zero. */
248 if (!isfinite(out[0])) {
249 out[0] = 0.0f;
250 }
251 if (!isfinite(out[1])) {
252 out[1] = 0.0f;
253 }
254 if (!isfinite(out[2])) {
255 out[2] = 0.0f;
256 }
257 }
258 }
259}
260
265 const float plane_no[3],
266 float r_out[3])
267{
268 float lambda;
269 const float *edge_snap_point = t->tsnap.snap_target;
270 const float *edge_dir = t->tsnap.snapNormal;
271 bool is_aligned = fabsf(dot_v3v3(edge_dir, plane_no)) < CONSTRAIN_EPSILON;
272 if (!is_aligned && isect_ray_plane_v3_factor(
273 edge_snap_point, edge_dir, t->tsnap.snap_source, plane_no, &lambda))
274 {
275 madd_v3_v3v3fl(r_out, edge_snap_point, edge_dir, lambda);
276 sub_v3_v3(r_out, t->tsnap.snap_source);
277 }
278}
279
280static void UNUSED_FUNCTION(constraint_snap_plane_to_face(const TransInfo *t,
281 const float plane[4],
282 float r_out[3]))
283{
284 float face_plane[4], isect_orig[3], isect_dir[3];
285 const float *face_snap_point = t->tsnap.snap_target;
286 const float *face_normal = t->tsnap.snapNormal;
287 plane_from_point_normal_v3(face_plane, face_snap_point, face_normal);
288 bool is_aligned = fabsf(dot_v3v3(plane, face_plane)) > (1.0f - CONSTRAIN_EPSILON);
289 if (!is_aligned && isect_plane_plane_v3(plane, face_plane, isect_orig, isect_dir)) {
290 closest_to_ray_v3(r_out, face_snap_point, isect_orig, isect_dir);
291 sub_v3_v3(r_out, t->tsnap.snap_source);
292 }
293}
294
296 const float axis[3],
297 float r_out[3])
298{
299 float lambda;
300 const float *edge_snap_point = t->tsnap.snap_target;
301 const float *edge_dir = t->tsnap.snapNormal;
302 bool is_aligned = fabsf(dot_v3v3(axis, edge_dir)) > (1.0f - CONSTRAIN_EPSILON);
303 if (!is_aligned &&
304 isect_ray_ray_v3(t->tsnap.snap_source, axis, edge_snap_point, edge_dir, &lambda, nullptr))
305 {
306 mul_v3_v3fl(r_out, axis, lambda);
307 }
308}
309
311 const float axis[3],
312 float r_out[3])
313{
314 float lambda;
315 const float *face_snap_point = t->tsnap.snap_target;
316 const float *face_normal = t->tsnap.snapNormal;
317 bool is_aligned = fabsf(dot_v3v3(axis, face_normal)) < CONSTRAIN_EPSILON;
318 if (!is_aligned &&
319 isect_ray_plane_v3_factor(t->tsnap.snap_source, axis, face_snap_point, face_normal, &lambda))
320 {
321 mul_v3_v3fl(r_out, axis, lambda);
322 }
323}
324
329static bool isPlaneProjectionViewAligned(const TransInfo *t, const float plane_no[3])
330{
331 const float eps = 0.001f;
332 float view_to_plane[3];
333 view_vector_calc(t, t->center_global, view_to_plane);
334
335 float factor = dot_v3v3(plane_no, view_to_plane);
336 return fabsf(factor) < eps;
337}
338
339static void planeProjection(const TransInfo *t,
340 const float plane_no[3],
341 const float in[3],
342 float out[3])
343{
344
345 float pos[3], view_vec[3], factor;
346
347 add_v3_v3v3(pos, in, t->center_global);
348 view_vector_calc(t, pos, view_vec);
349
350 if (isect_ray_plane_v3_factor(pos, view_vec, t->center_global, plane_no, &factor)) {
351 madd_v3_v3v3fl(out, in, view_vec, factor);
352 }
353}
354
356{
357 short orientation = t->orient[t->orient_curr].type;
358 if (orientation == V3D_ORIENT_CUSTOM_MATRIX) {
359 /* Use the real value of the "orient_type". */
360 orientation = t->orient[O_DEFAULT].type;
361 }
362 return orientation;
363}
364
366 const TransDataContainer *tc,
367 const TransData *td))[3]
368{
370 BLI_assert(t->orient_type_mask & (1 << V3D_ORIENT_GIMBAL));
371 if (t->options & (CTX_POSE_BONE | CTX_OBJECT)) {
372 TransDataExtension *td_ext = &tc->data_ext[td - tc->data];
373 return td_ext->axismtx_gimbal;
374 }
375 }
376 return td->axismtx;
377}
378
379void transform_constraint_get_nearest(const TransInfo *t, const float3 &vec, float r_vec[3])
380{
381 bool is_snap_to_point = false, is_snap_to_edge = false, is_snap_to_face = false;
382
384 if (validSnap(t)) {
385 is_snap_to_edge = (t->tsnap.target_type & SCE_SNAP_TO_EDGE) != 0;
386 is_snap_to_face = (t->tsnap.target_type & SCE_SNAP_TO_FACE) != 0;
387 is_snap_to_point = !is_snap_to_edge && !is_snap_to_face;
388 }
389 }
390
391 /* Fallback for when axes are aligned. */
392 mul_v3_m3v3(r_vec, t->con.pmtx, vec);
393
394 if (is_snap_to_point) {
395 /* Pass. With snap points, a projection is alright, no adjustments needed. */
396 }
397 else {
398 const int dims = getConstraintSpaceDimension(t);
399 if (dims == 2) {
400 if (!is_zero_v3(r_vec)) {
401 float plane_no[3];
402 constraint_plane_normal_calc(t, plane_no);
403
404 if (is_snap_to_edge) {
405 constraint_snap_plane_to_edge(t, plane_no, r_vec);
406 }
407 else if (is_snap_to_face) {
408 /* Disabled, as it has not proven to be really useful. (See #82386). */
409 // constraint_snap_plane_to_face(t, plane, out);
410 }
411 else if (!isPlaneProjectionViewAligned(t, plane_no)) {
412 /* View alignment correction. */
413 planeProjection(t, plane_no, vec, r_vec);
414 }
415 }
416 }
417 else if (dims == 1) {
418 float c[3];
419
420 if (t->con.mode & CON_AXIS0) {
421 copy_v3_v3(c, t->spacemtx[0]);
422 }
423 else if (t->con.mode & CON_AXIS1) {
424 copy_v3_v3(c, t->spacemtx[1]);
425 }
426 else {
428 copy_v3_v3(c, t->spacemtx[2]);
429 }
430
431 if (is_snap_to_edge) {
433 }
434 else if (is_snap_to_face) {
436 }
437 else {
438 /* View alignment correction. */
439 axisProjection(t, c, vec, r_vec);
440 }
441 }
442 }
443}
444
453 const TransDataContainer * /*tc*/,
454 const TransData *td,
455 const float in[3],
456 float out[3])
457{
458 if (td || !(t->con.mode & CON_APPLY)) {
459 copy_v3_v3(out, in);
460 return;
461 }
462
464}
465
477 const TransDataContainer *tc,
478 const TransData *td,
479 const float in[3],
480 float out[3])
481{
482 if (!td) {
483 applyAxisConstraintVec(t, tc, td, in, out);
484 }
485 else {
486 /* Specific TransData's space. */
487 copy_v3_v3(out, in);
488 if (t->con.mode & CON_APPLY) {
490 const float (*axismtx)[3] = transform_object_axismtx_get(t, tc, td);
491 mul_m3_v3(axismtx, out);
492 if (t->flag & T_EDIT) {
493 mul_m3_v3(tc->mat3_unit, out);
494 }
495 }
496 }
497}
498
503 const TransDataContainer * /*tc*/,
504 const TransData *td,
505 float r_smat[3][3])
506{
507 if (!td && t->con.mode & CON_APPLY) {
508 float tmat[3][3];
509
510 if (!(t->con.mode & CON_AXIS0)) {
511 r_smat[0][0] = 1.0f;
512 }
513 if (!(t->con.mode & CON_AXIS1)) {
514 r_smat[1][1] = 1.0f;
515 }
516 if (!(t->con.mode & CON_AXIS2)) {
517 r_smat[2][2] = 1.0f;
518 }
519
520 mul_m3_m3m3(tmat, r_smat, t->spacemtx_inv);
521 mul_m3_m3m3(r_smat, t->spacemtx, tmat);
522 }
523}
524
529 const TransDataContainer *tc,
530 const TransData *td,
531 float r_smat[3][3])
532{
533 if (td && t->con.mode & CON_APPLY) {
534 float tmat[3][3];
535 float imat[3][3];
536
537 const float (*axismtx)[3] = transform_object_axismtx_get(t, tc, td);
538 invert_m3_m3(imat, axismtx);
539
540 if (!(t->con.mode & CON_AXIS0)) {
541 r_smat[0][0] = 1.0f;
542 }
543 if (!(t->con.mode & CON_AXIS1)) {
544 r_smat[1][1] = 1.0f;
545 }
546 if (!(t->con.mode & CON_AXIS2)) {
547 r_smat[2][2] = 1.0f;
548 }
549
550 mul_m3_m3m3(tmat, r_smat, imat);
551 if (t->flag & T_EDIT) {
552 mul_m3_m3m3(r_smat, tc->mat3_unit, r_smat);
553 }
554 mul_m3_m3m3(r_smat, axismtx, tmat);
555 }
556}
557
559 const float axismtx[3][3],
560 float r_axis[3])
561{
563 int mode = t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
564
565 switch (mode) {
566 case CON_AXIS0:
567 case (CON_AXIS1 | CON_AXIS2):
568 copy_v3_v3(r_axis, axismtx[0]);
569 break;
570 case CON_AXIS1:
571 case (CON_AXIS0 | CON_AXIS2):
572 copy_v3_v3(r_axis, axismtx[1]);
573 break;
574 case CON_AXIS2:
575 case (CON_AXIS0 | CON_AXIS1):
576 copy_v3_v3(r_axis, axismtx[2]);
577 break;
578 }
579}
580
590 const TransDataContainer * /*tc*/,
591 const TransData *td,
592 float r_axis[3])
593{
594 if (!td && t->con.mode & CON_APPLY) {
595 constraints_rotation_impl(t, t->spacemtx, r_axis);
596 }
597}
598
608 const TransDataContainer *tc,
609 const TransData *td,
610 float r_axis[3])
611{
612 if (t->con.mode & CON_APPLY) {
613 float tmp_axismtx[3][3];
614 const float (*axismtx)[3];
615
616 /* On setup call, use first object. */
617 if (td == nullptr) {
618 BLI_assert(tc == nullptr);
620 td = tc->data;
621 }
622
623 if (t->flag & T_EDIT) {
624 mul_m3_m3m3(tmp_axismtx, tc->mat3_unit, td->axismtx);
625 axismtx = tmp_axismtx;
626 }
627 else {
628 axismtx = transform_object_axismtx_get(t, tc, td);
629 }
630
631 constraints_rotation_impl(t, axismtx, r_axis);
632 }
633}
634
636
637/* -------------------------------------------------------------------- */
640
641void setConstraint(TransInfo *t, int mode, const char text[])
642{
643 BLI_strncpy_utf8(t->con.text + 1, text, sizeof(t->con.text) - 1);
644 t->con.mode = eTConstraint(mode);
646
648
649 t->con.drawExtra = nullptr;
653 t->redraw = TREDRAW_HARD;
654}
655
656void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
657{
658 BLI_strncpy_utf8(t->con.text + 1, text, sizeof(t->con.text) - 1);
659 t->con.mode = eTConstraint(mode);
661
663
668 t->redraw = TREDRAW_HARD;
669}
670
671void setLocalConstraint(TransInfo *t, int mode, const char text[])
672{
673 if ((t->flag & T_EDIT) || t->data_len_all == 1) {
674 /* Although in edit-mode each object has its local space, use the
675 * orientation of the active object. */
676 setConstraint(t, mode, text);
677 }
678 else {
680 }
681}
682
683void setUserConstraint(TransInfo *t, int mode, const char text_[])
684{
685 char text[256];
686 const short orientation = transform_orientation_or_default(t);
687 const char *spacename = transform_orientations_spacename_get(t, orientation);
688 SNPRINTF_UTF8(text, text_, spacename);
689
690 switch (orientation) {
691 case V3D_ORIENT_LOCAL:
693 setLocalConstraint(t, mode, text);
694 break;
696 if (checkUseAxisMatrix(t)) {
698 break;
699 }
702 case V3D_ORIENT_VIEW:
706 default: {
707 setConstraint(t, mode, text);
708 break;
709 }
710 }
711 t->con.mode |= CON_USER;
712}
713
715
716/* -------------------------------------------------------------------- */
719
720static void drawLine(
721 TransInfo *t, const float center[3], const float dir[3], char axis, short options)
722{
724 return;
725 }
726
727 float v1[3], v2[3], v3[3];
728 uchar col[3], col2[3];
729
730 if (t->spacetype == SPACE_VIEW3D) {
731 View3D *v3d = static_cast<View3D *>(t->view);
732
733 copy_v3_v3(v3, dir);
734 mul_v3_fl(v3, v3d->clip_end);
735
736 sub_v3_v3v3(v2, center, v3);
737 add_v3_v3v3(v1, center, v3);
738 }
739 else if (t->spacetype == SPACE_SEQ) {
740 View2D *v2d = static_cast<View2D *>(t->view);
741
742 copy_v3_v3(v3, dir);
743 float max_dist = max_ff(BLI_rctf_size_x(&v2d->cur), BLI_rctf_size_y(&v2d->cur));
744 mul_v3_fl(v3, max_dist);
745
746 sub_v3_v3v3(v2, center, v3);
747 add_v3_v3v3(v1, center, v3);
748 }
749
751
752 if (options & DRAWLIGHT) {
753 col[0] = col[1] = col[2] = 220;
754 }
755 else {
757 }
758 ui::theme::make_axis_color(col, axis, col2);
759
760 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
761
762 float viewport[4];
763 GPU_viewport_size_get_f(viewport);
765
767 immUniform2fv("viewportSize", &viewport[2]);
768 immUniform1f("lineWidth", U.pixelsize * 2.0f);
769
771
773 immVertex3fv(pos, v1);
774 immVertex3fv(pos, v2);
775 immEnd();
776
778
780}
781
783{
784 TransCon *tc = &(t->con);
785
787 return;
788 }
789 if (!(tc->mode & CON_APPLY)) {
790 return;
791 }
792 if (t->flag & T_NO_CONSTRAINT) {
793 return;
794 }
795
796 if (tc->drawExtra) {
797 tc->drawExtra(t);
798 }
799 else {
800 if (tc->mode & CON_SELECT) {
801 float vec[3];
802
803 convertViewVec(t, vec, (t->mval[0] - t->mouse.imval[0]), (t->mval[1] - t->mouse.imval[1]));
804 add_v3_v3(vec, t->center_global);
805
806 drawLine(t, t->center_global, t->spacemtx[0], 'X', 0);
807 drawLine(t, t->center_global, t->spacemtx[1], 'Y', 0);
808 drawLine(t, t->center_global, t->spacemtx[2], 'Z', 0);
809
810 GPUDepthTest depth_test_enabled = GPU_depth_test_get();
811 if (depth_test_enabled) {
813 }
814
815 const uint shdr_pos = GPU_vertformat_attr_add(
816 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
817
819
820 float viewport_size[4];
821 GPU_viewport_size_get_f(viewport_size);
822 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
823
824 immUniform1i("colors_len", 0); /* "simple" mode. */
825 immUniformColor4f(1.0f, 1.0f, 1.0f, 1.0f);
826 immUniform1f("dash_width", 2.0f);
827 immUniform1f("udash_factor", 0.5f);
828
830 immVertex3fv(shdr_pos, t->center_global);
831 immVertex3fv(shdr_pos, vec);
832 immEnd();
833
835
836 if (depth_test_enabled) {
838 }
839 }
840
841 if (tc->mode & CON_AXIS0) {
842 drawLine(t, t->center_global, t->spacemtx[0], 'X', DRAWLIGHT);
843 }
844 if (tc->mode & CON_AXIS1) {
845 drawLine(t, t->center_global, t->spacemtx[1], 'Y', DRAWLIGHT);
846 }
847 if (tc->mode & CON_AXIS2) {
848 drawLine(t, t->center_global, t->spacemtx[2], 'Z', DRAWLIGHT);
849 }
850 }
851}
852
854{
855 if (t->flag & T_PROP_EDIT) {
856 const RegionView3D *rv3d = nullptr;
857 float tmat[4][4], imat[4][4];
858
859 if (t->spacetype == SPACE_VIEW3D) {
860 if (t->region && (t->region->regiontype == RGN_TYPE_WINDOW)) {
861 rv3d = static_cast<const RegionView3D *>(t->region->regiondata);
862 }
863 }
864
865 if (rv3d != nullptr) {
866 copy_m4_m4(tmat, rv3d->viewmat);
867 invert_m4_m4(imat, tmat);
868 }
869 else {
870 unit_m4(tmat);
871 unit_m4(imat);
872 }
873
875
876 if (t->spacetype == SPACE_VIEW3D) {
877 /* Pass. */
878 }
879 else if (t->spacetype == SPACE_IMAGE) {
880 GPU_matrix_scale_2f(1.0f / t->aspect[0], 1.0f / t->aspect[1]);
881 }
882
883 GPUDepthTest depth_test_enabled = GPU_depth_test_get();
884 if (depth_test_enabled) {
886 }
887
889 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
890
892
893 float viewport[4];
894 GPU_viewport_size_get_f(viewport);
896
897 immUniform2fv("viewportSize", &viewport[2]);
898 immUniform1f("lineWidth", 3.0f * U.pixelsize);
899
901 imm_drawcircball(t->center_global, t->prop_size, imat, pos);
902
903 immUniform1f("lineWidth", 1.0f * U.pixelsize);
905 imm_drawcircball(t->center_global, t->prop_size, imat, pos);
906
908
909 if (depth_test_enabled) {
911 }
912
914 }
915}
916
918{
919 if ((t->flag & T_PROP_EDIT) == 0) {
920 return;
921 }
922
923 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32_32);
924
926
927 float viewport[4];
928 GPU_viewport_size_get_f(viewport);
930
931 immUniform2fv("viewportSize", &viewport[2]);
932
933 View2D *v2d = &t->region->v2d;
934 const float x1 = t->center_global[0] - t->prop_size;
935 const float y1 = v2d->cur.ymin;
936 const float x2 = t->center_global[0] + t->prop_size;
937 const float y2 = v2d->cur.ymax;
938
939 immUniform1f("lineWidth", 3.0f * U.pixelsize);
941 imm_draw_box_wire_3d(pos, x1, y1, x2, y2);
942
943 immUniform1f("lineWidth", 1.0f * U.pixelsize);
945 imm_draw_box_wire_3d(pos, x1, y1, x2, y2);
946
949}
950
952{
953 /* Draw the first one lighter because that's the one who controls the others.
954 * Meaning the transformation is projected on that one and just copied on the others
955 * constraint space.
956 * In a nutshell, the object with light axis is controlled by the user and the others follow.
957 * Without drawing the first light, users have little clue what they are doing.
958 */
959 short options = DRAWLIGHT;
960 float tmp_axismtx[3][3];
961
963 TransData *td = tc->data;
964 for (int i = 0; i < tc->data_len; i++, td++) {
965 float co[3];
966 const float (*axismtx)[3];
967
968 if (t->flag & T_PROP_EDIT) {
969 /* We're sorted, so skip the rest. */
970 if (td->factor == 0.0f) {
971 break;
972 }
973 }
974
975 if (t->options & CTX_GPENCIL_STROKES) {
976 /* Only draw a constraint line for one point, otherwise we can't see anything. */
977 if ((options & DRAWLIGHT) == 0) {
978 break;
979 }
980 }
981
982 if (t->options & CTX_SEQUENCER_IMAGE) {
983 /* Because we construct an "L" shape to deform the sequence, we should skip
984 * all points except the first vertex. Otherwise we will draw the same axis constraint line
985 * 3 times for each strip.
986 */
987 if (i % 3 != 0) {
988 continue;
989 }
990 }
991
992 if (t->flag & T_EDIT) {
993 mul_v3_m4v3(co, tc->mat, td->center);
994
995 mul_m3_m3m3(tmp_axismtx, tc->mat3_unit, td->axismtx);
996 axismtx = tmp_axismtx;
997 }
998 else {
999 if (t->options & CTX_POSE_BONE) {
1000 mul_v3_m4v3(co, tc->mat, td->center);
1001 }
1002 else {
1003 copy_v3_v3(co, td->center);
1004 }
1005 axismtx = transform_object_axismtx_get(t, tc, td);
1006 }
1007
1008 if (t->con.mode & CON_AXIS0) {
1009 drawLine(t, co, axismtx[0], 'X', options);
1010 }
1011 if (t->con.mode & CON_AXIS1) {
1012 drawLine(t, co, axismtx[1], 'Y', options);
1013 }
1014 if (t->con.mode & CON_AXIS2) {
1015 drawLine(t, co, axismtx[2], 'Z', options);
1016 }
1018 }
1019 }
1020}
1021
1023
1024/* -------------------------------------------------------------------- */
1027
1029{
1030 t->con.mode |= CON_APPLY;
1031 *t->con.text = ' ';
1032
1033 /* When `dims` is zero, no constraints are set (or not set *yet*).
1034 * In this case `t->num.idx_max` is unlikely to be used.
1035 * Set to `t->idx_max` as it's the default when transform starts
1036 * to prevent numeric errors, see: #144916. */
1037 const short dims = getConstraintSpaceDimension(t);
1038 t->num.idx_max = (dims > 0) ? std::min<short>(dims - 1, t->idx_max) : t->idx_max;
1039}
1040
1042{
1043 if (t->orient_curr != O_DEFAULT) {
1045 }
1046
1047 t->con.mode &= ~(CON_APPLY | CON_SELECT);
1048 *t->con.text = '\0';
1049 t->num.idx_max = t->idx_max;
1050}
1051
1053
1054/* -------------------------------------------------------------------- */
1057
1059{
1060 if (t->orient_curr == O_DEFAULT) {
1062 }
1063
1065}
1066
1068{
1069 if (t->con.mode & CON_SELECT) {
1070 setNearestAxis(t);
1071 startConstraint(t);
1072 }
1073}
1074
1076{
1077 t->con.mode &= ~CON_SELECT;
1078 if (!(t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2))) {
1079 t->con.mode &= ~CON_APPLY;
1080 }
1081}
1082
1084{
1085 /* Clear any prior constraint flags. */
1086 t->con.mode &= ~(CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
1087
1088 /* No correction needed... just use whichever one is lower. */
1089 float2 dvec = t->mval - t->mouse.imval;
1090 if (abs(dvec.x) < abs(dvec.y)) {
1091 t->con.mode |= CON_AXIS1;
1092 STRNCPY_UTF8(t->con.text, IFACE_(" along Y axis"));
1093 }
1094 else {
1095 t->con.mode |= CON_AXIS0;
1096 STRNCPY_UTF8(t->con.text, IFACE_(" along X axis"));
1097 }
1098}
1099
1101{
1102 /* Clear any prior constraint flags. */
1103 t->con.mode &= ~(CON_AXIS0 | CON_AXIS1 | CON_AXIS2);
1104
1105 float zfac;
1106 float mvec[3], proj[3];
1107 float len[3];
1108 int i;
1109
1110 /* Calculate mouse movement. */
1111 mvec[0] = t->mval[0] - t->mouse.imval[0];
1112 mvec[1] = t->mval[1] - t->mouse.imval[1];
1113 mvec[2] = 0.0f;
1114
1115 /* We need to correct axis length for the current zoom-level of view,
1116 * this to prevent projected values to be clipped behind the camera
1117 * and to overflow the short integers.
1118 * The formula used is a bit stupid, just a simplification of the subtraction
1119 * of two 2D points 30 pixels apart (that's the last factor in the formula) after
1120 * projecting them with #ED_view3d_win_to_delta and then get the length of that vector. */
1122 zfac = len_v3(t->persinv[0]) * 2.0f / t->region->winx * zfac * 30.0f;
1123
1124 for (i = 0; i < 3; i++) {
1125 float axis[3], axis_2d[2];
1126
1127 copy_v3_v3(axis, t->spacemtx[i]);
1128
1129 mul_v3_fl(axis, zfac);
1130 /* Now we can project to get window coordinate. */
1131 add_v3_v3(axis, t->center_global);
1132 projectFloatView(t, axis, axis_2d);
1133
1134 sub_v2_v2v2(axis, axis_2d, t->center2d);
1135 axis[2] = 0.0f;
1136
1137 if (normalize_v3(axis) > 1e-3f) {
1138 project_v3_v3v3(proj, mvec, axis);
1139 sub_v3_v3v3(axis, mvec, proj);
1140 len[i] = normalize_v3(axis);
1141 }
1142 else {
1143 len[i] = 1e10f;
1144 }
1145 }
1146
1147 if (len[0] <= len[1] && len[0] <= len[2]) {
1149 t->con.mode |= (CON_AXIS1 | CON_AXIS2);
1150 SNPRINTF_UTF8(t->con.text, IFACE_(" locking %s X axis"), t->spacename);
1151 }
1152 else {
1153 t->con.mode |= CON_AXIS0;
1154 SNPRINTF_UTF8(t->con.text, IFACE_(" along %s X axis"), t->spacename);
1155 }
1156 }
1157 else if (len[1] <= len[0] && len[1] <= len[2]) {
1159 t->con.mode |= (CON_AXIS0 | CON_AXIS2);
1160 SNPRINTF_UTF8(t->con.text, IFACE_(" locking %s Y axis"), t->spacename);
1161 }
1162 else {
1163 t->con.mode |= CON_AXIS1;
1164 SNPRINTF_UTF8(t->con.text, IFACE_(" along %s Y axis"), t->spacename);
1165 }
1166 }
1167 else if (len[2] <= len[1] && len[2] <= len[0]) {
1169 t->con.mode |= (CON_AXIS0 | CON_AXIS1);
1170 SNPRINTF_UTF8(t->con.text, IFACE_(" locking %s Z axis"), t->spacename);
1171 }
1172 else {
1173 t->con.mode |= CON_AXIS2;
1174 SNPRINTF_UTF8(t->con.text, IFACE_(" along %s Z axis"), t->spacename);
1175 }
1176 }
1177}
1178
1180{
1181 eTConstraint mode_prev = t->con.mode;
1182
1183 /* Constraint setting - depends on spacetype. */
1184 if (t->spacetype == SPACE_VIEW3D) {
1185 /* 3d-view. */
1187 }
1188 else {
1189 /* Assume that this means a 2D-Editor. */
1191 }
1192
1193 if (mode_prev != t->con.mode) {
1196 }
1197}
1198
1200
1201/* -------------------------------------------------------------------- */
1204
1206{
1207 if ((t->con.mode & CON_APPLY) == 0) {
1208 return -1;
1209 }
1210 switch (int(t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2))) {
1211 case (CON_AXIS0):
1212 case (CON_AXIS1 | CON_AXIS2):
1213 return 0;
1214 case (CON_AXIS1):
1215 case (CON_AXIS0 | CON_AXIS2):
1216 return 1;
1217 case (CON_AXIS2):
1218 case (CON_AXIS0 | CON_AXIS1):
1219 return 2;
1220 default:
1221 return -1;
1222 }
1223}
1224
1226{
1227 int mode = t->con.mode;
1228
1229 if ((mode & (CON_AXIS0 | CON_AXIS1)) == (CON_AXIS0 | CON_AXIS1)) {
1230 return true;
1231 }
1232
1233 if ((mode & (CON_AXIS1 | CON_AXIS2)) == (CON_AXIS1 | CON_AXIS2)) {
1234 return true;
1235 }
1236
1237 if ((mode & (CON_AXIS0 | CON_AXIS2)) == (CON_AXIS0 | CON_AXIS2)) {
1238 return true;
1239 }
1240
1241 return false;
1242}
1243
1245{
1246 int n = 0;
1247
1248 if (t->con.mode & CON_AXIS0) {
1249 n++;
1250 }
1251
1252 if (t->con.mode & CON_AXIS1) {
1253 n++;
1254 }
1255
1256 if (t->con.mode & CON_AXIS2) {
1257 n++;
1258 }
1259
1260 return n;
1261 /* Someone willing to do it cryptically could do the following instead:
1262 *
1263 * `return t->con & (CON_AXIS0|CON_AXIS1|CON_AXIS2);`
1264 *
1265 * Based on the assumptions that the axis flags are one after the other and start at 1
1266 */
1267}
1268
1270
1271} // namespace blender::ed::transform
#define ATTR_FALLTHROUGH
#define DEG2RADF(_deg)
#define M_PI_2
#define M_PI
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define ELEM(...)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
CCL_NAMESPACE_BEGIN struct Options options
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define abs
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
static void constraint_plane_normal_calc(const TransInfo *t, float r_plane_no[3])
static void applyAxisConstraintRot(const TransInfo *t, const TransDataContainer *, const TransData *td, float r_axis[3])
void postSelectConstraint(TransInfo *t)
static bool isPlaneProjectionViewAligned(const TransInfo *t, const float plane_no[3])
static short transform_orientation_or_default(const TransInfo *t)
int getConstraintSpaceDimension(const TransInfo *t)
void setUserConstraint(TransInfo *t, int mode, const char text_[])
static void setNearestAxis3d(TransInfo *t)
static void applyAxisConstraintSize(const TransInfo *t, const TransDataContainer *, const TransData *td, float r_smat[3][3])
static void planeProjection(const TransInfo *t, const float plane_no[3], const float in[3], float out[3])
bool isLockConstraint(const TransInfo *t)
bool validSnap(const TransInfo *t)
void setAxisMatrixConstraint(TransInfo *t, int mode, const char text[])
void setLocalConstraint(TransInfo *t, int mode, const char text[])
static void drawLine(TransInfo *t, const float center[3], const float dir[3], char axis, short options)
static void applyObjectConstraintVec(const TransInfo *t, const TransDataContainer *tc, const TransData *td, const float in[3], float out[3])
void transform_constraint_snap_axis_to_face(const TransInfo *t, const float axis[3], float r_out[3])
void setConstraint(TransInfo *t, int mode, const char text[])
void transform_constraint_snap_axis_to_edge(const TransInfo *t, const float axis[3], float r_out[3])
void projectFloatView(TransInfo *t, const float vec[3], float adr[2])
void view_vector_calc(const TransInfo *t, const float focus[3], float r_vec[3])
static const float(* transform_object_axismtx_get(const TransInfo *t, const TransDataContainer *tc, const TransData *td))[3]
void convertViewVec(TransInfo *t, float r_vec[3], double dx, double dy)
static void axisProjection(const TransInfo *t, const float axis[3], const float in[3], float out[3])
static void applyAxisConstraintVec(const TransInfo *t, const TransDataContainer *, const TransData *td, const float in[3], float out[3])
static void applyObjectConstraintRot(const TransInfo *t, const TransDataContainer *tc, const TransData *td, float r_axis[3])
static void constraint_snap_plane_to_edge(const TransInfo *t, const float plane_no[3], float r_out[3])
void transform_gizmo_3d_model_from_constraint_and_mode_set(TransInfo *t)
static void setNearestAxis2d(TransInfo *t)
void transform_constraint_get_nearest(const TransInfo *t, const float3 &vec, float r_vec[3])
bool transform_snap_is_active(const TransInfo *t)
static void projection_matrix_calc(const TransInfo *t, float r_pmtx[3][3])
void initSelectConstraint(TransInfo *t)
static void drawObjectConstraint(TransInfo *t)
static void viewAxisCorrectCenter(const TransInfo *t, float t_con_center[3])
void selectConstraint(TransInfo *t)
void constraintNumInput(TransInfo *t, float vec[3])
static void constraints_rotation_impl(const TransInfo *t, const float axismtx[3][3], float r_axis[3])
const char * transform_orientations_spacename_get(TransInfo *t, const short orient_type)
void drawConstraint(TransInfo *t)
int constraintModeToIndex(const TransInfo *t)
static void UNUSED_FUNCTION(constraint_snap_plane_to_face(const TransInfo *t, const float plane[4], float r_out[3]))
void drawPropCircle(TransInfo *t)
void transform_orientations_current_set(TransInfo *t, const short orient_index)
static void applyObjectConstraintSize(const TransInfo *t, const TransDataContainer *tc, const TransData *td, float r_smat[3][3])
void make_axis_color(const unsigned char col[3], char axis, unsigned char r_col[3])
void get_color_3ubv(int colorid, unsigned char col[3])
bool isect_ray_plane_v3_factor(const float ray_origin[3], const float ray_direction[3], const float plane_co[3], const float plane_no[3], float *r_lambda)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool isect_plane_plane_v3(const float plane_a[4], const float plane_b[4], float r_isect_co[3], float r_isect_no[3]) ATTR_WARN_UNUSED_RESULT
void mul_m3_v3(const float M[3][3], float r[3])
float closest_to_ray_v3(float r_close[3], const float p[3], const float ray_orig[3], const float ray_dir[3])
void immEnd()
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
@ V3D_ORIENT_CUSTOM_MATRIX
ATTR_WARN_UNUSED_RESULT const BMLoop * l
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
GPUDepthTest GPU_depth_test_get()
Definition gpu_state.cc:229
void immUniform2f(const char *name, float x, float y)
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
GPUVertFormat * immVertexFormat()
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void immBegin(GPUPrimType, uint vertex_len)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
ATTR_WARN_UNUSED_RESULT const BMVert * v
void imm_drawcircball(const float cent[3], float radius, const float tmat[4][4], uint pos)
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void immUnbindProgram()
void immUniformColor3ubv(const unsigned char rgb[3])
MINLINE void mul_v3_fl(float r[3], float f)
void GPU_matrix_scale_2f(float x, float y)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
@ GPU_DEPTH_NONE
Definition GPU_state.hh:132
@ GPU_DEPTH_LESS_EQUAL
Definition GPU_state.hh:135
unsigned char uchar
Definition BKE_text.h:58
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
void immUniformColor4f(float r, float g, float b, float a)
MINLINE void negate_v3(float r[3])
void unit_m4(float m[4][4])
void immUniformThemeColorShadeAlpha(int color_id, int color_offset, int alpha_offset)
void imm_draw_box_wire_3d(uint pos, float x1, float y1, float x2, float y2)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
bool isect_ray_ray_v3(const float ray_origin_a[3], const float ray_direction_a[3], const float ray_origin_b[3], const float ray_direction_b[3], float *r_lambda_a, float *r_lambda_b)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void immVertex3fv(uint attr_id, const float data[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void unit_m3(float m[3][3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE float max_ff(float a, float b)
MINLINE float normalize_v3_v3_length(float r[3], const float a[3], float unit_length)
void immUniform2fv(const char *name, const float data[2])
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void immUniform1i(const char *name, int x)
VecBase< float, 3 > float3
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void GPU_matrix_pop()
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void GPU_matrix_push()
const btScalar eps
Definition poly34.cpp:11
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
void(*) drawExtra(TransInfo *t)
Definition transform.hh:598
void(*) applySize(const TransInfo *t, const TransDataContainer *tc, const TransData *td, float r_smat[3][3])
Definition transform.hh:612
void(*) applyRot(const TransInfo *t, const TransDataContainer *tc, const TransData *td, float r_axis[3])
Definition transform.hh:617
void(*) applyVec(const TransInfo *t, const TransDataContainer *tc, const TransData *td, const float in[3], float r_out[3])
Definition transform.hh:606
struct blender::ed::transform::TransInfo::@074260112357037217233016050204226071352346123342 orient[3]
#define TRANS_DATA_CONTAINER_FIRST_OK(t)
Definition transform.hh:43
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
#define CONSTRAIN_EPSILON
#define DRAWLIGHT